rom.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # SPDX-License-Identifier: GPL-3.0-or-later
  2. # Copyright (c) 2014-2016,2020-2021,2023-2025 Leah Rowe <leah@libreboot.org>
  3. # Copyright (c) 2021-2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. # Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  5. # Copyright (c) 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  6. # Copyright (c) 2023-2024 Riku Viitanen <riku.viitanen@protonmail.com>
  7. mkserprog()
  8. {
  9. [ $# -lt 1 ] && $err "mkserprog: no arguments provided"
  10. [ "$_f" = "-d" ] && return 0 # dry run
  11. [ "$1" = "pico" ] && mkpicotool
  12. basename -as .h "$serdir/"*.h > "$TMPDIR/ser" || $err "!mk $1 $TMPDIR"
  13. while read -r sertarget; do
  14. [ "$1" = "pico" ] &&
  15. x_ rm -rf "$sersrc/build" \
  16. && (pt=$(x_ grep "pico_cmake_set" \
  17. "$picosdk/src/boards/include/boards/$sertarget.h" \
  18. | grep "PICO_PLATFORM" | cut -d= -f2 | tr -d [:blank:])
  19. mkdir -p "$sersrc/build_$pt"
  20. ln -srf "$sersrc/build_$pt/" "$sersrc/build") \
  21. && x_ cmake -DPICO_BOARD="$sertarget" \
  22. -DPICO_SDK_PATH="$picosdk" -B "$sersrc/build" "$sersrc" \
  23. -Dpicotool_DIR="$picotool/picotool" \
  24. && x_ cmake --build "$sersrc/build"
  25. [ "$1" = "stm32" ] && x_ make -C "$sersrc" \
  26. libopencm3-just-make BOARD=$sertarget && x_ make -C \
  27. "$sersrc" BOARD=$sertarget; x_ mkdir -p "bin/serprog_$1"
  28. x_ mv "$serx" "bin/serprog_$1/serprog_$sertarget.${serx##*.}"
  29. done < "$TMPDIR/ser"
  30. [ "$XBMK_RELEASE" = "y" ] && mkrom_tarball "bin/serprog_$1"; return 0
  31. }
  32. mkpicotool()
  33. {
  34. rm -Rf "$picotool" || $err "Can't remove picotool builddir"
  35. (
  36. x_ cd src/picotool
  37. cmake -DCMAKE_INSTALL_PREFIX=xbmkbin -DPICOTOOL_FLAT_INSTALL=1 \
  38. -DPICO_SDK_PATH=../pico-sdk || \
  39. $err "Can't prep picotool"
  40. make install || $err "Can't build picotool"; :
  41. ) || $err "Can't build picotool"; :
  42. }
  43. copyps1bios()
  44. {
  45. x_ rm -Rf bin/playstation
  46. x_ mkdir -p bin/playstation
  47. x_ cp src/pcsx-redux/src/mips/openbios/openbios.bin bin/playstation
  48. printf "MIT License\n\nCopyright (c) 2019-2024 PCSX-Redux authors\n\n" \
  49. > bin/playstation/COPYING.txt || $err "!pcsx-redux copyright"
  50. cat config/snippet/mit >>bin/playstation/COPYING.txt || $err "!pcsx MIT"
  51. }
  52. mkpayload_grub()
  53. {
  54. eval "`setvars "" grub_modules grub_install_modules`"
  55. $dry eval "`setcfg "$grubdata/module/$tree"`"
  56. $dry x_ rm -f "$srcdir/grub.elf"; $dry \
  57. "$srcdir/grub-mkstandalone" --grub-mkimage="$srcdir/grub-mkimage" \
  58. -O i386-coreboot -o "$srcdir/grub.elf" -d "${srcdir}/grub-core/" \
  59. --fonts= --themes= --locales= --modules="$grub_modules" \
  60. --install-modules="$grub_install_modules" \
  61. "/boot/grub/grub_default.cfg=${srcdir}/.config" \
  62. "/boot/grub/grub.cfg=$grubdata/memdisk.cfg" || \
  63. $err "$tree: cannot build grub.elf"; return 0
  64. }
  65. mkvendorfiles()
  66. {
  67. [ -z "$mode" ] && $dry cook_coreboot_config
  68. check_coreboot_utils "$tree"
  69. printf "%s\n" "${version%%-*}" > "$srcdir/.coreboot-version" || \
  70. $err "!mk $srcdir .coreboot-version"
  71. [ -z "$mode" ] && [ "$target" != "$tree" ] && \
  72. x_ ./mk download "$target"; return 0
  73. }
  74. cook_coreboot_config()
  75. {
  76. [ -f "$srcdir/.config" ] || return 0
  77. printf "CONFIG_CCACHE=y\n" >> "$srcdir/.config" || \
  78. $err "$srcdir/.config: Could not enable ccache"
  79. make -C "$srcdir" oldconfig || $err "Could not cook $srcdir/.config"; :
  80. }
  81. check_coreboot_utils()
  82. {
  83. for util in cbfstool ifdtool; do
  84. [ "$badhash" = "y" ] && x_ rm -f "elf/$util/$1/$util"
  85. e "elf/$util/$1/$util" f && continue
  86. utilelfdir="elf/$util/$1"
  87. utilsrcdir="src/coreboot/$1/util/$util"
  88. utilmode="" && [ -n "$mode" ] && utilmode="clean"
  89. x_ make -C "$utilsrcdir" $utilmode -j$XBMK_THREADS $makeargs
  90. if [ -z "$mode" ] && [ ! -f "$utilelfdir/$util" ]; then
  91. x_ mkdir -p "$utilelfdir"
  92. x_ cp "$utilsrcdir/$util" "$utilelfdir"
  93. [ "$util" = "cbfstool" ] || continue
  94. x_ cp "$utilsrcdir/rmodtool" "$utilelfdir"
  95. elif [ -n "$mode" ]; then
  96. x_ rm -Rf "$utilelfdir"
  97. fi; continue
  98. done; return 0
  99. }
  100. mkcorebootbin()
  101. {
  102. [ "$target" = "$tree" ] && return 0
  103. tmprom="$TMPDIR/coreboot.rom"
  104. $dry x_ cp "$srcdir/build/coreboot.rom" "$tmprom"
  105. initmode="${defconfig##*/}"; displaymode="${initmode##*_}"
  106. [ "$displaymode" = "$initmode" ] && displaymode="" # "normal" config
  107. initmode="${initmode%%_*}"
  108. cbfstool="elf/cbfstool/$tree/cbfstool"
  109. [ "$payload_uboot_i386" = "y" ] && \
  110. [ "$payload_uboot_amd64" = "y" ] && \
  111. $err "'$target' enables 32- and 64-bit x86 U-Boot"
  112. if [ "$payload_uboot_i386" = "y" ] || \
  113. [ "$payload_uboot_amd64" = "y" ]; then
  114. printf "'%s' has x86 U-Boot; assuming SeaBIOS=y\n" \
  115. "$target" 1>&2
  116. payload_seabios="y"
  117. fi
  118. [ -n "$uboot_config" ] || uboot_config="default"
  119. [ "$payload_uboot" = "y" ] || payload_seabios="y"
  120. [ "$payload_grub" = "y" ] && payload_seabios="y"
  121. [ "$payload_seabios" = "y" ] && [ "$payload_uboot" = "y" ] && \
  122. $dry $err "$target: U-Boot(arm64) and SeaBIOS/GRUB both enabled."
  123. [ -z "$grub_scan_disk" ] && grub_scan_disk="nvme ahci ata"
  124. [ -n "$grubtree" ] || grubtree="default"
  125. grubelf="elf/grub/$grubtree/payload/grub.elf"
  126. [ "$payload_memtest" = "y" ] || payload_memtest="n"
  127. [ "$(uname -m)" = "x86_64" ] || payload_memtest="n"
  128. [ "$payload_grubsea" = "y" ] && [ "$initmode" = "normal" ] && \
  129. payload_grubsea="n"
  130. [ "$payload_grub" = "y" ] || payload_grubsea="n"
  131. if $dry grep "CONFIG_PAYLOAD_NONE=y" "$defconfig"; then
  132. [ "$payload_seabios" = "y" ] && pname="seabios" && \
  133. $dry add_seabios
  134. [ "$payload_uboot" = "y" ] && pname="uboot" && $dry add_uboot
  135. else
  136. pname="custom" && $dry cprom; :
  137. fi; :
  138. }
  139. add_seabios()
  140. {
  141. if [ "$payload_uboot_i386" = "y" ] || \
  142. [ "$payload_uboot_amd64" = "y" ]; then
  143. $dry add_uboot
  144. fi
  145. _seabioself="elf/seabios/default/$initmode/bios.bin.elf"
  146. _seaname="fallback/payload" && [ "$payload_grubsea" = "y" ] && \
  147. _seaname="seabios.elf"
  148. cbfs "$tmprom" "$_seabioself" "$_seaname"
  149. x_ "$cbfstool" "$tmprom" add-int -i 3000 -n etc/ps2-keyboard-spinup
  150. _z="2"; [ "$initmode" = "vgarom" ] && _z="0"
  151. x_ "$cbfstool" "$tmprom" add-int -i $_z -n etc/pci-optionrom-exec
  152. x_ "$cbfstool" "$tmprom" add-int -i 0 -n etc/optionroms-checksum
  153. [ "$initmode" = "libgfxinit" ] && \
  154. cbfs "$tmprom" "$seavgabiosrom" vgaroms/seavgabios.bin raw
  155. [ "$payload_memtest" = "y" ] && cbfs "$tmprom" \
  156. "elf/memtest86plus/memtest.bin" img/memtest
  157. [ "$payload_grub" = "y" ] && add_grub
  158. [ "$payload_grubsea" != "y" ] && cprom
  159. [ "$payload_uboot_amd64" = "y" ] && [ "$displaymode" != "txtmode" ] && \
  160. [ "$initmode" != "normal" ] && [ "$payload_grubsea" != "y" ] && \
  161. pname="seauboot" && cprom "seauboot"
  162. [ "$payload_grub" = "y" ] && pname="seagrub" && mkseagrub; :
  163. }
  164. add_grub()
  165. {
  166. _grubname="img/grub2" && [ "$payload_grubsea" = "y" ] && \
  167. _grubname="fallback/payload"
  168. cbfs "$tmprom" "$grubelf" "$_grubname"
  169. printf "set grub_scan_disk=\"%s\"\n" "$grub_scan_disk" \
  170. > "$TMPDIR/tmpcfg" || $err "$target: !insert scandisk"
  171. cbfs "$tmprom" "$TMPDIR/tmpcfg" scan.cfg raw
  172. [ "$initmode" != "normal" ] && [ "$displaymode" != "txtmode" ] && \
  173. cbfs "$tmprom" "$grubdata/background/background1280x800.png" \
  174. "background.png" raw; :
  175. }
  176. mkseagrub()
  177. {
  178. if [ "$payload_grubsea" = "y" ]; then
  179. pname="grub"
  180. else
  181. cbfs "$tmprom" "$grubdata/bootorder" bootorder raw
  182. fi
  183. for keymap in config/data/grub/keymap/*.gkb; do
  184. [ -f "$keymap" ] && cprom "${keymap##*/}"; :
  185. done; :
  186. }
  187. add_uboot()
  188. {
  189. if [ "$displaymode" = "txtmode" ]; then
  190. printf "cb/%s: Cannot use U-Boot in text mode\n" \
  191. "$target" 1>&2
  192. return 0
  193. elif [ "$initmode" = "normal" ]; then
  194. printf "cb/%s: Cannot use U-Boot in normal initmode\n" \
  195. "$target" 1>&2
  196. return 0
  197. fi
  198. # TODO: re-work to allow each coreboot target to say which ub tree
  199. # instead of hardcoding as in the current logic below:
  200. # aarch64 targets:
  201. ubcbfsargs=""
  202. ubpath="fallback/payload"
  203. ubtarget="$target"
  204. # override for x86/x86_64 targets:
  205. if [ "$payload_uboot_i386" = "y" ] || \
  206. [ "$payload_uboot_amd64" = "y" ]; then
  207. ubcbfsargs="-l 0x1110000 -e 0x1110000" # 64-bit and 32-bit
  208. # on 64-bit, 0x1120000 is the SPL, and stub before that
  209. ubpath="img/u-boot" # 64-bit
  210. ubtarget="amd64coreboot"
  211. [ "$payload_uboot_i386" = "y" ] && ubpath="u-boot" # 32-bit
  212. [ "$payload_uboot_i386" = "y" ] && ubtarget="i386coreboot"; :
  213. fi
  214. ubdir="elf/u-boot/$ubtarget/$uboot_config"
  215. # aarch64 targets:
  216. ubootelf="$ubdir/u-boot.elf" && [ ! -f "$ubootelf" ] && \
  217. ubootelf="$ubdir/u-boot"
  218. # override for x86/x86_64 targets:
  219. [ "$payload_uboot_i386" = "y" ] && ubootelf="$ubdir/u-boot-dtb.bin"
  220. [ "$payload_uboot_amd64" = "y" ] && \
  221. ubootelf="$ubdir/u-boot-x86-with-spl.bin" # EFI-compatible
  222. [ -f "$ubootelf" ] || $err "cb/$ubtarget: Can't find u-boot"
  223. cbfs "$tmprom" "$ubootelf" "$ubpath" $ubcbfsargs
  224. [ "$payload_seabios" = "y" ] || cprom; :
  225. }
  226. cprom()
  227. {
  228. newrom="bin/$target/${pname}_${target}_$initmode.rom"
  229. [ -n "$displaymode" ] && newrom="${newrom%.rom}_$displaymode.rom"
  230. [ $# -gt 0 ] && [ "$1" != "seauboot" ] && \
  231. newrom="${newrom%.rom}_${1%.gkb}.rom"
  232. x_ mkdir -p "bin/$target"
  233. x_ cp "$tmprom" "$newrom" && [ $# -gt 0 ] && [ "$1" != "seauboot" ] && \
  234. cbfs "$newrom" "config/data/grub/keymap/$1" keymap.gkb raw
  235. [ $# -gt 0 ] && [ "$1" = "seauboot" ] && \
  236. cbfs "$newrom" "config/data/grub/bootorder_uboot" bootorder raw; :
  237. }
  238. mkcoreboottar()
  239. {
  240. [ "$target" = "$tree" ] && return 0
  241. [ "$XBMK_RELEASE" = "y" ] || return 0
  242. [ "$release" != "n" ] || return 0
  243. $dry mkrom_tarball "bin/$target"
  244. $dry ./mk inject "bin/${relname}_${target}.tar.xz" nuke || \
  245. $err "Can't delete vendorfiles in 'bin/${relname}_$target.tar.xz'"
  246. return 0
  247. }