1
0

waouw-cryptroot 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/sh
  2. # /etc/initramfs-tools/hooks/waouw-cryptroot
  3. #
  4. # initramfs-tools hook: includes WiFi drivers, networking tools,
  5. # and the waouw-keyfetch keyscript into the initramfs image.
  6. #
  7. # Installed to: /etc/initramfs-tools/hooks/waouw-cryptroot
  8. # Run by: update-initramfs -u
  9. #
  10. # Depends on cryptroot hook running first so LUKS infrastructure is present.
  11. PREREQ="cryptroot"
  12. prereqs() {
  13. echo "$PREREQ"
  14. }
  15. case "$1" in
  16. prereqs)
  17. prereqs
  18. exit 0
  19. ;;
  20. esac
  21. . /usr/share/initramfs-tools/hook-functions
  22. # -------------------------------------------------------------------
  23. # 1. WiFi kernel modules (Broadcom BCM43455 on Raspberry Pi 3B+/4/5)
  24. # -------------------------------------------------------------------
  25. manual_add_modules brcmfmac
  26. manual_add_modules brcmutil
  27. manual_add_modules cfg80211
  28. manual_add_modules rfkill
  29. # -------------------------------------------------------------------
  30. # 2. WiFi firmware (BCM43455 SDIO — RPi 3B+/4/Zero 2W)
  31. # -------------------------------------------------------------------
  32. FIRMWARE_DIR="/lib/firmware/brcm"
  33. if [ -d "$FIRMWARE_DIR" ]; then
  34. mkdir -p "${DESTDIR}${FIRMWARE_DIR}"
  35. for f in "${FIRMWARE_DIR}"/brcmfmac43455-sdio.*; do
  36. [ -e "$f" ] && cp -a "$f" "${DESTDIR}${FIRMWARE_DIR}/"
  37. done
  38. # Also grab the regulatory database if present
  39. for f in /lib/firmware/regulatory.db /lib/firmware/regulatory.db.p7s; do
  40. if [ -e "$f" ]; then
  41. mkdir -p "${DESTDIR}/lib/firmware"
  42. cp -a "$f" "${DESTDIR}/lib/firmware/"
  43. fi
  44. done
  45. else
  46. echo "W: waouw-cryptroot: firmware directory ${FIRMWARE_DIR} not found" >&2
  47. fi
  48. # -------------------------------------------------------------------
  49. # 3. Networking binaries
  50. # -------------------------------------------------------------------
  51. # wpa_supplicant + wpa_cli for WiFi authentication
  52. copy_exec /sbin/wpa_supplicant /sbin
  53. copy_exec /sbin/wpa_cli /sbin
  54. # DHCP client — prefer udhcpc (busybox), fall back to dhclient
  55. if [ -x /usr/bin/udhcpc ]; then
  56. copy_exec /usr/bin/udhcpc /usr/bin
  57. elif [ -x /sbin/udhcpc ]; then
  58. copy_exec /sbin/udhcpc /sbin
  59. elif [ -x /sbin/dhclient ]; then
  60. copy_exec /sbin/dhclient /sbin
  61. else
  62. echo "W: waouw-cryptroot: no DHCP client (udhcpc or dhclient) found" >&2
  63. fi
  64. # curl for HTTPS key fetch
  65. if [ -x /usr/bin/curl ]; then
  66. copy_exec /usr/bin/curl /usr/bin
  67. else
  68. echo "E: waouw-cryptroot: curl not found — key fetch will fail" >&2
  69. fi
  70. # ip for network interface management (iproute2)
  71. if [ -x /sbin/ip ]; then
  72. copy_exec /sbin/ip /sbin
  73. elif [ -x /usr/sbin/ip ]; then
  74. copy_exec /usr/sbin/ip /usr/sbin
  75. fi
  76. # openssl for HMAC-SHA256 computation
  77. if [ -x /usr/bin/openssl ]; then
  78. copy_exec /usr/bin/openssl /usr/bin
  79. else
  80. echo "E: waouw-cryptroot: openssl not found — HMAC auth will fail" >&2
  81. fi
  82. # xxd for hex encoding (often part of vim-common)
  83. if [ -x /usr/bin/xxd ]; then
  84. copy_exec /usr/bin/xxd /usr/bin
  85. else
  86. echo "W: waouw-cryptroot: xxd not found — will use od fallback" >&2
  87. fi
  88. # -------------------------------------------------------------------
  89. # 4. CA certificates for HTTPS (curl needs them)
  90. # -------------------------------------------------------------------
  91. CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"
  92. if [ -f "$CA_BUNDLE" ]; then
  93. mkdir -p "${DESTDIR}/etc/ssl/certs"
  94. cp "$CA_BUNDLE" "${DESTDIR}/etc/ssl/certs/"
  95. else
  96. echo "W: waouw-cryptroot: CA bundle not found at ${CA_BUNDLE}" >&2
  97. fi
  98. # -------------------------------------------------------------------
  99. # 5. WiFi configuration (prepared during SD card setup)
  100. # -------------------------------------------------------------------
  101. WIFI_CONF="/boot/firmware/wifi-initramfs.conf"
  102. if [ -f "$WIFI_CONF" ]; then
  103. mkdir -p "${DESTDIR}/boot/firmware"
  104. cp "$WIFI_CONF" "${DESTDIR}/boot/firmware/"
  105. # Restrict permissions — contains WiFi password
  106. chmod 600 "${DESTDIR}/boot/firmware/wifi-initramfs.conf"
  107. else
  108. echo "I: waouw-cryptroot: no WiFi config at ${WIFI_CONF} (Ethernet-only mode)" >&2
  109. fi
  110. # Also copy the device-id file if present
  111. DEVICE_ID="/boot/firmware/waouw-device-id"
  112. if [ -f "$DEVICE_ID" ]; then
  113. mkdir -p "${DESTDIR}/boot/firmware"
  114. cp "$DEVICE_ID" "${DESTDIR}/boot/firmware/"
  115. fi
  116. # -------------------------------------------------------------------
  117. # 6. The keyscript itself
  118. # -------------------------------------------------------------------
  119. KEYSCRIPT="/etc/waouw/waouw-keyfetch"
  120. if [ -f "$KEYSCRIPT" ]; then
  121. copy_exec "$KEYSCRIPT" /etc/waouw
  122. else
  123. echo "E: waouw-cryptroot: keyscript not found at ${KEYSCRIPT}" >&2
  124. echo "E: waouw-cryptroot: LUKS unlock will fail without it" >&2
  125. fi
  126. # -------------------------------------------------------------------
  127. # 7. udhcpc default script (needed if using busybox udhcpc)
  128. # -------------------------------------------------------------------
  129. UDHCPC_SCRIPT="/etc/waouw/udhcpc-initramfs.script"
  130. if [ -f "$UDHCPC_SCRIPT" ]; then
  131. copy_exec "$UDHCPC_SCRIPT" /etc/waouw
  132. elif [ -f "/usr/share/udhcpc/default.script" ]; then
  133. copy_exec "/usr/share/udhcpc/default.script" /usr/share/udhcpc
  134. fi
  135. # -------------------------------------------------------------------
  136. # 8. Encryption script (for first-boot encryption from initramfs)
  137. # -------------------------------------------------------------------
  138. ENCRYPT_SCRIPT="/etc/initramfs-tools/scripts/local-premount/waouw-encrypt"
  139. if [ -f "$ENCRYPT_SCRIPT" ]; then
  140. mkdir -p "${DESTDIR}/scripts/local-premount"
  141. cp "$ENCRYPT_SCRIPT" "${DESTDIR}/scripts/local-premount/waouw-encrypt"
  142. chmod +x "${DESTDIR}/scripts/local-premount/waouw-encrypt"
  143. fi
  144. # cryptsetup (should already be included by cryptroot hook, but ensure)
  145. if [ -x /sbin/cryptsetup ]; then
  146. copy_exec /sbin/cryptsetup /sbin
  147. fi
  148. # mkfs.ext4 for creating filesystem inside LUKS
  149. if [ -x /sbin/mkfs.ext4 ]; then
  150. copy_exec /sbin/mkfs.ext4 /sbin
  151. elif [ -x /usr/sbin/mkfs.ext4 ]; then
  152. copy_exec /usr/sbin/mkfs.ext4 /usr/sbin
  153. fi
  154. # -------------------------------------------------------------------
  155. # 9. Boot partition files needed by encryption/keyfetch
  156. # -------------------------------------------------------------------
  157. for f in .waouw-lot-id .waouw-encrypt-pending waouw-admin-url.txt .waouw-luks-localkey waouw-device-id; do
  158. src="/boot/firmware/$f"
  159. [ -f "$src" ] || src="/boot/$f"
  160. if [ -f "$src" ]; then
  161. mkdir -p "${DESTDIR}/boot/firmware"
  162. cp "$src" "${DESTDIR}/boot/firmware/"
  163. fi
  164. done
  165. echo "I: waouw-cryptroot: hook completed successfully" >&2
  166. exit 0