---
# System prepended metadata

title: Cross-compiling Wpa Supplicant for ARM Architecture

---

# Cross-compiling Wpa Supplicant for ARM Architecture
:::info
Contribute: 縵欣
Reference: [web](https://www.cnblogs.com/wanglouxiaozi/p/12829235.html)
:::

## Installation environment prerequisites
* adb
command-line tool used for communication between an Android device and a computer
~~* gcc-arm-linux-gnueabi **(not used)**
ARM architecture cross-compilation toolchain~~
* scrcpy
Screen projection tool for Android phones

Installation
```
sudo apt install adb

# Compile
# We do not use these
# (X) sudo apt install gcc-arm-linux-gnueabi
# (X) sudo apt install gcc-arm-linux-gnueabihf

# screen copy tool
git clone https://github.com/Genymobile/scrcpy.git
```
## Musl Cross Make
- The cross compiler that can support static linking for `glibc`
```shell=
# git clone --depth 1 https://github.com/richfelker/musl-cross-make.git
# cd musl-cross-make
# cp config.mak.dist config.mak
```
- Modify the setting in `config.mak`
```make=
TARGET = arm-linux-musleabihf
COMMON_CONFIG += CFLAGS="-g0 -O3" CXXFLAGS="-g0 -O3" LDFLAGS="-s"
GCC_CONFIG += --enable-default-pie --enable-static-pie
```
- Compile and install
```shell=
make -j`nproc --all` && make install
```
- Copy the output of the binary to the memorizable place
```shell=
sudo cp -r output /opt/musl
```
- Replace the libdirectory to the current one and modify the `installed` options to make the compilation static instead of using dynamic linking
```shell=
# sudo find /opt/musl -iname "*.la" -type f -exec sed -i "s/libdir='/libdir='\/opt\/musl/g" "{}" \;
# sudo find /opt/musl -iname "*.la" -type f -exec sed -i "s/installed=yes/installed=no/g" "{}" \;
```
- After this, the binary `/opt/musl/bin/arm-linux-musleabihf-gcc` would be used to generate Openssl, libnl, wpa_supplicant
## OpenSSL
- [Download from webpage](https://www.openssl.org/source/gitrepo.html)
- The following script can be used to compile the openssl. Copy it in openssl directory and run it for the static compilation of openssl. Create `build` folder before the execution
```shell=
export CC="/opt/musl/bin/arm-linux-musleabihf-gcc -static"
./Configure no-shared -latomic linux-armv4 no-asm no-threads --prefix="$(pwd)/build"
make clean
make -j`nproc --all`
make install
```
- There would be some folders required for wpa_supplicant in `./build`.
## Libnl
- [Download from webpage](https://www.linuxfromscratch.org/blfs/view/svn/basicnet/libnl.html)
```shell=
# wget https://github.com/thom311/libnl/releases/download/libnl3_7_0/libnl-3.7.0.tar.gz
# tar -xzf libnl-3.7.0.tar.gz
```
- Configure with similar arguments. Export the PATH to append `/opt/musl/bin` before the execution.
```shell=
./configure --enable-static --disable-shared --disable-cli CC=arm-linux-musleabihf-gcc --host=arm-linux --prefix=$PWD/build
make clean
make -j`nproc --all`
make install
```
- There would be some folders required for wpa_supplicant in `./build`.
## wpa_supplicant
- Download from [webpage](https://w1.fi/wpa_supplicant/) or private gitlab repo with vendor-specific extension
- Add extension PKG_CONFIG_PATH for both `openssl` and `libnl`.
```shell=
export PKG_CONFIG_PATH=/home/nems/cross_compile/openssl/build/lib/pkgconfig:/home/nems/cross_compile/libnl-.7.0/build/lib/pkgconfig
```
- modify `wpa_supplicant/.config`
```shell=
# Used to be ...
# CONFIG_LIBNL32=y
# Additional directories for cross-compilation on Linux host for mingw target
#CFLAGS += -I/opt/mingw/mingw32/include/ddk
#LIBS += -L/opt/mingw/mingw32/lib
#CC=mingw32-gcc

# Modified result
CONFIG_LIBNL32=y
CC=arm-linux-musleabihf-gcc -static
CFLAGS += -I/home/nems/cross_compile/openssl/build/include
LIBS += -L/home/nems/cross_compile/openssl/build/lib -lssl -lcrypto
CFLAGS += -I/home/nems/cross_compile/libnl-3.7.0/build/include
LIBS += -L/home/nems/cross_compile/libnl-3.7.0/build/lib -lnl-genl-3 -lnl-3
```
- If `bus/dbus.h: No such file or directory` shows up, modify `wpa_supplicant/.config`
```bash=
#CONFIG_CTRL_IFACE_DBUS_NEW=y
#CONFIG_CTRL_IFACE_DBUS_INTRO=y
```
- Make with
```shell=
make -j`nproc --all` LDFLAGS+="-static"
```
:::success
Notice: Make sure that the .so files are in ARM structure by `file` command.

For now, the `wpa_supplicant` binary should result in `dynamic linked` file, but the result is actually runnable in Andriod devices
:::

## libudev (USB dependency for DHCP)

## dhcpcd (DHCP Client)
- The program in cellphone needs it for the DHCP IP searching. We need to compile it statically and shift it on the cellphone

## Makefile (Deprecated for Other Simpler Program)

```c
CC = gcc
ARMCC = arm-linux-gnueabi-gcc
CFLAGS = -std=gnu11 -Wall -I include -g -Wno-unused-result
ARMFLAGS = -march=armv7-a -static -DARM

APP = binary

DEPS = \
	src/dev.c             \
	src/net.c             \
	src/esp.c             \
	src/sha1.c            \
	src/hmac.c            \
	src/transport.c       \
	src/sip.c             \
	src/replay.c          \
	src/utils.c

all: $(APP)

binary: src/publish.c $(DEPS)
	$(ARMCC) $^ -o $@ $(CFLAGS) $(ARMFLAGS) -O2

clean:
	rm -f $(APP)
```

## Phone Screen Projection
```shell
$ adb devices # Display the serial numbers of all phones connected via USB.
List of devices attached
RFCR91K78DK     device
R5CR91X3V6J     device

$ scrcpy -s [S/N] # Project the screen of a specific phone
```


## Run
* Push the file into the phone
```shell
$ adb push [biarny] /data/local/tmp
```
* Execute the file
```shell
$ adb shell
o1q:/ $ su
o1q:/ # cd data/local/tmp
o1q:/ $ ./[binary file]
```