roms 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Copyright (c) 2014-2016,2020-2021,2023-2024 Leah Rowe <leah@libreboot.org>
  4. # Copyright (c) 2021-2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  5. # Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  6. # Copyright (c) 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  7. # Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com>
  8. set -u -e
  9. . "include/lib.sh"
  10. seavgabiosrom="elf/seabios/default/libgfxinit/vgabios.bin"
  11. cfgsdir="config/coreboot"
  12. picosrc="src/pico-serprog"
  13. picosdk="src/pico-sdk"
  14. stm32src="src/stm32-vserprog"
  15. tmpcfg="$tmpdir/tmpcfg"
  16. # Disable all payloads by default.
  17. # target.cfg files have to specifically enable [a] payload(s)
  18. pv="payload_uboot payload_grub_withseabios payload_seabios payload_memtest t"
  19. pv="$pv payload_seabios_withgrub payload_seabios_grubonly payload_grub mt86bin"
  20. v="romdir cbrom initmode displaymode cbcfg targetdir tree release ubootelf"
  21. v="$v grub_timeout board grub_scan_disk uboot_config grubtree grubelf tmpmv"
  22. eval "$(setvars "n" $pv)"
  23. eval "$(setvars "" $v boards targets serprog_boards_dir)"
  24. main()
  25. {
  26. while [ $# -gt 0 ]; do
  27. [ "$1" = "list" ] && eval "x_ ls -1 config/coreboot; return 0"
  28. [ "$1" = "serprog" ] && shift && eval "mkserprog $@; return 0"
  29. [ "$1" = "all" ] && shift && continue
  30. boards="$1 $boards"; shift 1
  31. done
  32. [ -n "$boards" ] || boards="$(ls -1 config/coreboot)" || \
  33. $err "Cannot generate list of boards for building"
  34. for x in $boards; do
  35. [ -d "config/coreboot/$x/config" ] && configure_target "$x" \
  36. && build_payloads && build_board && [ -d "bin/$board" ] \
  37. && targets="$targets, $x"; continue
  38. done
  39. [ -z "$targets" ] && $err "No ROM images were compiled"
  40. printf "Check these ROM directories in bin/: %s\n" "${targets#, }"
  41. printf "DO NOT flash images from elf/ - please use bin/ instead.\n"
  42. }
  43. mkserprog()
  44. {
  45. [ -z "${1+x}" ] && badcmd
  46. [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && $err "bad command"
  47. if [ "$1" = "rp2040" ]; then
  48. serprog_boards_dir="$picosdk/src/boards/include/boards"
  49. [ -d "$picosrc" ] || x_ ./update trees -f "pico-serprog"
  50. elif [ "$1" = "stm32" ]; then
  51. serprog_boards_dir="$stm32src/boards"
  52. [ -d "$stm32src" ] || x_ ./update trees -f "stm32-vserprog"
  53. fi
  54. x_ mkdir -p "bin/serprog_$1"
  55. if [ $# -gt 1 ]; then
  56. [ "$2" = "list" ] && serlist "$serprog_boards_dir" && return 0
  57. [ "$2" = "list" ] || build_${1}_rom "$2"
  58. else
  59. serlist "$serprog_boards_dir" | \
  60. while read -r board; do
  61. build_${1}_rom "$board"
  62. done
  63. fi
  64. [ "$xbmk_release" = "y" ] && mkrom_tarball "bin/serprog_$1"; return 0
  65. }
  66. build_rp2040_rom()
  67. {
  68. x_ cmake -DPICO_BOARD="$1" -DPICO_SDK_PATH="$picosdk" \
  69. -B "$picosrc/build" "$picosrc"
  70. x_ cmake --build "$picosrc/build"
  71. x_ mv "$picosrc/build/pico_serprog.uf2" \
  72. "bin/serprog_rp2040/serprog_$1.uf2"
  73. printf "output to bin/serprog_rp2040/serprog_%s.uf2\n" "$1"
  74. }
  75. build_stm32_rom()
  76. {
  77. x_ make -C "$stm32src" libopencm3-just-make BOARD=$1
  78. x_ make -C "$stm32src" BOARD=$1
  79. x_ mv "$stm32src/stm32-vserprog.hex" \
  80. "bin/serprog_stm32/serprog_$1.hex"
  81. printf "output to bin/serprog_stm32/serprog_%s.hex\n" "$1"
  82. }
  83. serlist()
  84. {
  85. basename -a -s .h "$1/"*.h || $err "$1: can't list boards"
  86. }
  87. configure_target()
  88. {
  89. eval "$(setvars "n" $pv) $(setvars "" $v)"
  90. board="$1"
  91. targetdir="$cfgsdir/$board"
  92. [ -f "$targetdir/target.cfg" ] || $err "$board: target.cfg missing"
  93. # Override the above defaults using target.cfg
  94. . "$targetdir/target.cfg"
  95. [ -z "$grub_scan_disk" ] && grub_scan_disk="nvme ahci ata"
  96. [ -n "$grubtree" ] || grubtree="default"
  97. grubelf="elf/grub/$grubtree/payload/grub.elf"
  98. [ -z "$tree" ] && $err "$board: tree not defined"
  99. [ "$payload_memtest" != "y" ] && payload_memtest="n"
  100. [ "$(uname -m)" = "x86_64" ] || payload_memtest="n"
  101. [ "$payload_grub_withseabios" = "y" ] && eval "$(setvars "y" \
  102. payload_grub payload_seabios payload_seabios_withgrub)"
  103. [ "$payload_seabios_withgrub" = "y" ] && payload_seabios="y"
  104. [ "$payload_seabios_grubonly" = "y" ] && \
  105. eval "$(setvars "y" payload_seabios payload_seabios_withgrub)"
  106. # The reverse logic must not be applied. If SeaBIOS-with-GRUB works,
  107. # that doesn't mean GRUB-with-SeaBIOS will, e.g. VGA ROM execution
  108. [ "$payload_grub" != "y" ] && [ "$payload_seabios" != "y" ] && \
  109. [ "$payload_uboot" != "y" ] && $err "'$board' defines no payload"
  110. [ "$payload_uboot" != "n" ] && [ "$payload_uboot" != "y" ] && \
  111. payload_uboot="n"
  112. [ -n "$uboot_config" ] || uboot_config="default"
  113. [ "$xbmk_release" = "y" ] && [ "$release" = "n" ] && return 1
  114. [ "$board" = "$tree" ] && return 1; return 0
  115. }
  116. build_payloads()
  117. {
  118. romdir="bin/$board"
  119. cbdir="src/coreboot/$board"
  120. [ "$board" = "$tree" ] || cbdir="src/coreboot/$tree"
  121. cbfstool="elf/cbfstool/$tree/cbfstool"
  122. cbrom="$cbdir/build/coreboot.rom"
  123. [ -f "$cbfstool" ] || x_ ./update trees -b coreboot utils $tree
  124. mt86bin="elf/memtest86plus/memtest.bin"
  125. [ "$payload_memtest" != "y" ] || [ -f "$mt86bin" ] || \
  126. x_ ./update trees -b memtest86plus
  127. [ "$payload_seabios" = "y" ] && x_ ./update trees -b seabios
  128. if [ "$payload_grub" = "y" ] || [ "$payload_seabios_withgrub" = "y" ] \
  129. || [ "$payload_seabios_grubonly" = "y" ]; then
  130. [ -f "$grubelf" ] || x_ ./update trees -b grub $grubtree
  131. fi
  132. [ "$payload_uboot" = "y" ] && build_uboot_payload; return 0
  133. }
  134. build_uboot_payload()
  135. {
  136. x_ ./update trees -b u-boot $board
  137. ubdir="elf/u-boot/$board/$uboot_config"
  138. ubootelf="$ubdir/u-boot.elf" && [ ! -f "$ubootelf" ] && \
  139. ubootelf="$ubdir/u-boot"
  140. [ -f "$ubootelf" ] || $err "$board: Can't find u-boot"; return 0
  141. }
  142. build_board()
  143. {
  144. x_ rm -Rf "$romdir"
  145. for it in "normal" "vgarom" "libgfxinit"; do
  146. initmode="$it"
  147. hmode="vesafb"
  148. [ "$initmode" = "vgarom" ] || hmode="corebootfb"
  149. modes="$hmode txtmode"
  150. for y in $modes; do
  151. displaymode="$y"
  152. [ "$initmode" = "normal" ] && \
  153. [ "$displaymode" != "txtmode" ] && continue
  154. cbcfg="$targetdir/config/${initmode}_$displaymode"
  155. [ "$initmode" = "normal" ] && cbcfg="${cbcfg%_*}"
  156. e "$cbcfg" f not || build_roms; x_ rm -f "$cbrom"
  157. done
  158. done
  159. }
  160. build_roms()
  161. {
  162. x_ ./update trees -b coreboot $board
  163. _cbrom="$cbelfdir/$board/${initmode}_$displaymode"
  164. [ "$initmode" = "normal" ] && _cbrom="${_cbrom%"_$displaymode"}"
  165. _cbrom="$_cbrom/coreboot.rom"
  166. cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  167. x_ cp "$_cbrom" "$cbrom"
  168. [ "$payload_memtest" != "y" ] || cbfs "$cbrom" "$mt86bin" img/memtest
  169. [ "$payload_seabios" = "y" ] && build_seabios_roms
  170. [ "$payload_grub" != "y" ] || build_grub_roms "$cbrom" "grub"
  171. [ "$payload_uboot" = "y" ] || return 0
  172. x_ cp "$_cbrom" "$cbrom"
  173. build_uboot_roms
  174. }
  175. build_seabios_roms()
  176. {
  177. if [ "$payload_seabios_withgrub" = "y" ]; then
  178. t="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  179. x_ cp "$cbrom" "$t"
  180. build_grub_roms "$t" "seabios_withgrub"
  181. else
  182. t="$(mkSeabiosRom "$cbrom" "fallback/payload")" || \
  183. $err "build_seabios_roms: cannot build tmprom"
  184. newrom="$romdir/seabios_${board}_${initmode}_$displaymode"
  185. [ "$initmode" = "normal" ] && newrom="$romdir/seabios" \
  186. && newrom="${newrom}_${board}_$initmode"
  187. cprom cp "$t" "$newrom.rom"
  188. fi
  189. x_ rm -f "$t"
  190. }
  191. # Make separate ROM images with GRUB payload
  192. build_grub_roms()
  193. {
  194. tmprom="$1"
  195. payload1="$2" # allow values: grub, seabios, seabios_withgrub
  196. grub_cbfs="fallback/payload"
  197. if [ "$payload1" = "grub" ] && [ "$payload_grub_withseabios" = "y" ]
  198. then
  199. tmpmv="$(mkSeabiosRom "$tmprom" "seabios.elf")" || \
  200. $err "build_grub_roms 1 $board: can't build tmprom"
  201. elif [ "$payload1" != "grub" ] && [ "$payload_seabios_withgrub" = "y" ]
  202. then
  203. grub_cbfs="img/grub2"
  204. tmpmv="$(mkSeabiosRom "$tmprom" fallback/payload)" || \
  205. $err "build_grub_roms 2 $board: can't build tmprom"
  206. fi
  207. [ -n "$tmpmv" ] && [ -f "$tmpmv" ] && x_ mv "$tmpmv" "$tmprom"
  208. # we only need insert grub.elf once, for each coreboot config:
  209. cbfs "$tmprom" "$grubelf" "$grub_cbfs"
  210. printf "set grub_scan_disk=\"%s\"\n" "$grub_scan_disk" > "$tmpcfg" || \
  211. $err "set grub_scan_disk, $grub_scan_disk, $tmpcfg"
  212. cbfs "$tmprom" "$tmpcfg" scan.cfg raw
  213. printf "set timeout=%s\n" "$grub_timeout" > "$tmpcfg" || $err "!time"
  214. [ -z "$grub_timeout" ] || cbfs "$tmprom" "$tmpcfg" timeout.cfg raw
  215. newrom="$romdir/${payload1}_${board}_${initmode}_$displaymode.rom"
  216. [ "$initmode" = "normal" ] && newrom="$romdir/${payload1}_" \
  217. && newrom="$newrom${board}_$initmode.rom"
  218. cprom cp "$tmprom" "$newrom"
  219. if [ "$payload_seabios_withgrub" = "y" ] && \
  220. [ "$payload1" != "grub" ]; then
  221. cbfs "$tmprom" "$grubdata/bootorder" bootorder raw
  222. cprom cp "$tmprom" "${newrom%.rom}_grubfirst.rom"
  223. if [ "$payload_seabios_grubonly" = "y" ]; then
  224. x_ "$cbfstool" "$tmprom" add-int -i 0 \
  225. -n etc/show-boot-menu
  226. cprom cp "$tmprom" "${newrom%.rom}_grubonly.rom"
  227. fi
  228. fi
  229. }
  230. # make a rom in /tmp/ and then print the path of that ROM
  231. mkSeabiosRom() {
  232. tmprom="$(mktemp -t coreboot_rom.XXXXXXXXXX)" # 1=cbrom, 2=cbfs path
  233. _seabioself="elf/seabios/default/$initmode/bios.bin.elf"
  234. x_ cp "$1" "$tmprom"
  235. cbfs "$tmprom" "$_seabioself" "$2"
  236. x_ "$cbfstool" "$tmprom" add-int -i 3000 -n etc/ps2-keyboard-spinup
  237. z="2"; [ "$initmode" = "vgarom" ] && z="0"
  238. x_ "$cbfstool" "$tmprom" add-int -i $z -n etc/pci-optionrom-exec
  239. x_ "$cbfstool" "$tmprom" add-int -i 0 -n etc/optionroms-checksum
  240. [ "$initmode" != "libgfxinit" ] || cbfs "$tmprom" "$seavgabiosrom" \
  241. vgaroms/seavgabios.bin raw
  242. printf "%s\n" "$tmprom"
  243. }
  244. build_uboot_roms()
  245. {
  246. tmprom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  247. newrom="$romdir/uboot_payload_${board}_${initmode}_$displaymode.rom"
  248. x_ cp "$cbrom" "$tmprom"
  249. cbfs "$tmprom" "$ubootelf" "fallback/payload"
  250. cprom mv "$tmprom" "$newrom"
  251. }
  252. cprom()
  253. {
  254. printf "Creating target image: %s\n" "$3"
  255. x_ mkdir -p "${3%/*}"
  256. x_ $1 "$2" "$3"
  257. }
  258. main $@