|
1 | 1 | __all__ = ["build"]
|
2 | 2 |
|
| 3 | +from halo import Halo |
| 4 | +from pathlib import Path |
3 | 5 |
|
4 |
| -def build(): |
| 6 | +from picsi.vendored.mkmanifest import mkmanifest |
| 7 | +from picsi.vendored.run_commands import run_commands |
| 8 | +from picsi.vendored.get_output import get_output |
| 9 | +from picsi.vendored.get_uname import get_uname |
| 10 | + |
| 11 | + |
| 12 | +def build( |
| 13 | + url: str = "https://github.com/seemoo-lab/nexmon_csi", |
| 14 | + branch: str = "master", |
| 15 | + nexmon_url: str = "https://github.com/seemoo-lab/nexmon.git", |
| 16 | + nexmon_branch: str = "master", |
| 17 | +): |
5 | 18 | """
|
6 |
| - Build Nexmon_CSI |
| 19 | + Build Nexmon_CSI from source |
7 | 20 | """
|
8 | 21 |
|
9 |
| - print("build Nexmon_CSI") |
| 22 | + Path("/home/pi/.picsi/").mkdir(exist_ok=True) |
| 23 | + |
| 24 | + path_nexmon = Path("/home/pi/.picsi/nexmon") |
| 25 | + path_nexmon_csi = Path( |
| 26 | + "/home/pi/.picsi/nexmon/patches/bcm43455c0/7_45_189/nexmon_csi" |
| 27 | + ) |
| 28 | + path_nexmon_csi_bin = Path(f"/home/pi/.picsi/bins/{get_uname('-r')}") |
| 29 | + |
| 30 | + nexmon_kversion = ".".join(get_uname("-r").split(".")[:2]) + ".y" |
| 31 | + |
| 32 | + with Halo(spinner="dots") as spinner: |
| 33 | + |
| 34 | + # fmt: off |
| 35 | + run_commands([ |
| 36 | + "# Setting up WiFi", |
| 37 | + ["/usr/sbin/rfkill", "unblock", "all"], |
| 38 | + ["/usr/bin/raspi-config", "nonint", "do_wifi_country", "US"], |
| 39 | + |
| 40 | + "# Expanding SD card", |
| 41 | + ["/usr/bin/raspi-config", "nonint", "do_expand_rootfs"], |
| 42 | + |
| 43 | + "# Installing Dependencies", |
| 44 | + ["/usr/bin/apt", "update"], |
| 45 | + ["/usr/bin/apt", "install", "-y", |
| 46 | + "automake", "bc", "bison", "flex", "gawk", "git", |
| 47 | + "libgmp3-dev", "libncurses5-dev", "libssl-dev", |
| 48 | + "libtool-bin", "make", "python-is-python2", "qpdf", |
| 49 | + "raspberrypi-kernel-headers", "texinfo", |
| 50 | + ], |
| 51 | + ], spinner) |
| 52 | + # fmt: on |
| 53 | + |
| 54 | + uname_r = get_uname("-r") |
| 55 | + kernel_headers = Path(f"/lib/modules/{uname_r}/build/Kconfig") |
| 56 | + |
| 57 | + if not kernel_headers.is_file(): |
| 58 | + # fmt: off |
| 59 | + run_commands([ |
| 60 | + "# Installing Kernel headers", |
| 61 | + ["/usr/bin/wget", "https://raw.githubusercontent.com/RPi-Distro/rpi-source/master/rpi-source", "-O", "/usr/local/bin/rpi-source"], |
| 62 | + ["/usr/bin/chmod", "+x", "/usr/local/bin/rpi-source"], |
| 63 | + ["/usr/local/bin/rpi-source", "-q", "--tag-update"], |
| 64 | + ["/usr/local/bin/rpi-source"] |
| 65 | + ], spinner) |
| 66 | + # fmt: on |
| 67 | + |
| 68 | + # fmt: off |
| 69 | + run_commands([ |
| 70 | + "# Backing up original binaries", |
| 71 | + ["/usr/bin/mkdir", "-p", |
| 72 | + path_nexmon_csi_bin / "makecsiparams/", |
| 73 | + path_nexmon_csi_bin / "nexutil/", |
| 74 | + path_nexmon_csi_bin / "original/", |
| 75 | + path_nexmon_csi_bin / "patched/", |
| 76 | + ], |
| 77 | + ["/usr/bin/cp", |
| 78 | + "/lib/firmware/brcm/brcmfmac43455-sdio.bin", |
| 79 | + path_nexmon_csi_bin / "original/", |
| 80 | + ], |
| 81 | + ["/usr/bin/cp", |
| 82 | + get_output(["/usr/sbin/modinfo", "brcmfmac", "-n"]), |
| 83 | + path_nexmon_csi_bin / "original/", |
| 84 | + ], |
| 85 | + |
| 86 | + "# Downloading Nexmon", |
| 87 | + ["/usr/bin/git", "clone", nexmon_url, path_nexmon], |
| 88 | + "cd " + str(path_nexmon), |
| 89 | + ["/usr/bin/git", "checkout", nexmon_branch], |
| 90 | + |
| 91 | + "# Downloading Nexmon_CSI", |
| 92 | + ["/usr/bin/git", "clone", url, path_nexmon_csi], |
| 93 | + "cd " + str(path_nexmon_csi), |
| 94 | + ["/usr/bin/git", "checkout", branch], |
| 95 | + |
| 96 | + "# Building libISL", |
| 97 | + "cd " + str(path_nexmon / "buildtools/isl-0.10/"), |
| 98 | + ["/usr/bin/autoreconf", "-f", "-i"], |
| 99 | + [path_nexmon / "buildtools/isl-0.10/configure"], |
| 100 | + ["/usr/bin/make"], |
| 101 | + ["/usr/bin/make", "install"], |
| 102 | + ["/usr/bin/ln", |
| 103 | + "-s", "/usr/local/lib/libisl.so", |
| 104 | + "/usr/lib/arm-linux-gnueabihf/libisl.so.10" |
| 105 | + ], |
| 106 | + |
| 107 | + "# Building libMPFR", |
| 108 | + "cd " + str(path_nexmon / "buildtools/mpfr-3.1.4/"), |
| 109 | + ["/usr/bin/autoreconf", "-f", "-i"], |
| 110 | + [path_nexmon / "buildtools/mpfr-3.1.4/configure"], |
| 111 | + ["/usr/bin/make"], |
| 112 | + ["/usr/bin/make", "install"], |
| 113 | + ["/usr/bin/ln", |
| 114 | + "-s", "/usr/local/lib/libmpfr.so", |
| 115 | + "/usr/lib/arm-linux-gnueabihf/libmpfr.so.4" |
| 116 | + ], |
| 117 | + ], spinner) |
| 118 | + # fmt: on |
| 119 | + |
| 120 | + env = { |
| 121 | + "ARCH": "arm", |
| 122 | + "SUBARCH": "arm", |
| 123 | + "KERNEL": "kernel7", |
| 124 | + "HOSTUNAME": get_uname("-s"), |
| 125 | + "PLATFORMUNAME": get_uname("-m"), |
| 126 | + "NEXMON_ROOT": str(path_nexmon), |
| 127 | + "CC": str( |
| 128 | + path_nexmon |
| 129 | + / "buildtools/gcc-arm-none-eabi-5_4-2016q2-linux-armv7l/bin/arm-none-eabi-" |
| 130 | + ), |
| 131 | + "CCPLUGIN": str(path_nexmon / "buildtools/gcc-nexmon-plugin-arm/nexmon.so"), |
| 132 | + "ZLIBFLATE": "zlib-flate -compress", |
| 133 | + "Q": "@", |
| 134 | + "NEXMON_SETUP_ENV": "1", |
| 135 | + } |
| 136 | + |
| 137 | + # fmt: off |
| 138 | + run_commands([ |
| 139 | + "# Extracting UCODE, TemplateRAM, and FlashPatches", |
| 140 | + "cd " + str(path_nexmon), |
| 141 | + ["sudo", "-E", "bash", "-c", "/usr/bin/make"], |
| 142 | + |
| 143 | + "# Building Nexmon_CSI", |
| 144 | + "cd " + str(path_nexmon_csi), |
| 145 | + ["sudo", "-E", "bash", "-c", "/usr/bin/make install-firmware"], |
| 146 | + |
| 147 | + "# Building Makecsiparams", |
| 148 | + "cd " + str(path_nexmon_csi / "utils/makecsiparams"), |
| 149 | + ["sudo", "-E", "bash", "-c", "/usr/bin/make"], |
| 150 | + |
| 151 | + "# Building Nexutil", |
| 152 | + "cd " + str(path_nexmon / "utilities/nexutil/"), |
| 153 | + ["sudo", "-E", "bash", "-c", "/usr/bin/make"], |
| 154 | + |
| 155 | + "# Packaging Binaries", |
| 156 | + ["/usr/bin/cp", |
| 157 | + path_nexmon / "utilities/nexutil/nexutil", |
| 158 | + path_nexmon_csi_bin / "nexutil/" |
| 159 | + ], |
| 160 | + ["/usr/bin/cp", |
| 161 | + path_nexmon_csi / "utils/makecsiparams/makecsiparams", |
| 162 | + path_nexmon_csi_bin / "makecsiparams/", |
| 163 | + ], |
| 164 | + ["/usr/bin/cp", |
| 165 | + path_nexmon_csi / ("brcmfmac_" + nexmon_kversion + "-nexmon/brcmfmac.ko"), |
| 166 | + path_nexmon_csi_bin / "patched/", |
| 167 | + ], |
| 168 | + ["/usr/bin/cp", |
| 169 | + path_nexmon_csi / "brcmfmac43455-sdio.bin", |
| 170 | + path_nexmon_csi_bin / "patched/", |
| 171 | + ], |
| 172 | + ], spinner, env=env) |
| 173 | + # fmt: on |
| 174 | + |
| 175 | + # Writes the manifest.toml file |
| 176 | + mkmanifest( |
| 177 | + path_nexmon_csi_bin, |
| 178 | + path_nexmon, |
| 179 | + path_nexmon_csi, |
| 180 | + url, |
| 181 | + branch, |
| 182 | + nexmon_url, |
| 183 | + nexmon_branch, |
| 184 | + ) |
| 185 | + |
| 186 | + # fmt: off |
| 187 | + run_commands([ |
| 188 | + ["/usr/bin/tar", "-cvJf", f"{path_nexmon_csi_bin}.tar.xz", f"{path_nexmon_csi_bin}"] |
| 189 | + ], spinner) |
| 190 | + # fmt: on |
0 commit comments