roms 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # SPDX-FileCopyrightText: 2014-2016,2020,2021,2023,2024 Leah Rowe <leah@libreboot.org>
  4. # SPDX-FileCopyrightText: 2021,2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  5. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  6. # SPDX-FileCopyrightText: 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  7. # SPDX-FileCopyrightText: 2023 Riku Viitanen <riku.viitanen@protonmail.com>
  8. set -u -e
  9. . "include/err.sh"
  10. . "include/option.sh"
  11. seavgabiosrom="elf/seabios/default/libgfxinit/vgabios.bin"
  12. grub_background="background1280x800.png"
  13. grubelf="elf/grub/grub.elf"
  14. cfgsdir="config/coreboot"
  15. # Disable all payloads by default.
  16. # target.cfg files have to specifically enable [a] payload(s)
  17. pv="payload_grub payload_grub_withseabios payload_seabios payload_memtest t"
  18. pv="${pv} payload_seabios_withgrub payload_seabios_grubonly payload_uboot memtest_bin"
  19. v="romdir cbrom initmode displaymode cbcfg targetdir tree keymaps"
  20. v="${v} grub_timeout ubdir board grub_scan_disk uboot_config"
  21. eval "$(setvars "n" ${pv})"
  22. eval "$(setvars "" ${v} boards _displaymode _payload _keyboard all targets)"
  23. main()
  24. {
  25. check_project
  26. while [ $# -gt 0 ]; do
  27. case ${1} in
  28. help)
  29. usage
  30. exit 0 ;;
  31. list)
  32. items config/coreboot || :
  33. exit 0 ;;
  34. -d) _displaymode="${2}" ;;
  35. -p) _payload="${2}" ;;
  36. -k) _keyboard="${2}" ;;
  37. *)
  38. [ "${1}" = "all" ] && all="y"
  39. boards="${1} ${boards}"
  40. shift && continue ;;
  41. esac
  42. shift 2
  43. done
  44. [ "${all}" != "y" ] || boards=$(items config/coreboot) || \
  45. err "Cannot generate list of boards for building"
  46. for x in ${boards}; do
  47. eval "$(setvars "n" ${pv}) $(setvars "" ${v})"
  48. grub_background="background1280x800.png"
  49. board="${x}"
  50. configure_target
  51. build_payloads
  52. build_target_mainboard
  53. [ -d "bin/${board}" ] || continue
  54. targets="* bin/${board}\n${targets}"
  55. done
  56. [ -z "${targets}" ] && err "No ROM images were compiled"
  57. printf "\nROM images available in these directories:\n"
  58. eval "printf \"${targets}\""
  59. printf "^^ ROM images available in these directories.\n\n"
  60. printf "DO NOT flash images from elf/ - please use bin/ instead.\n"
  61. }
  62. configure_target()
  63. {
  64. targetdir="${cfgsdir}/${board}"
  65. [ -f "${targetdir}/target.cfg" ] || \
  66. err "Missing target.cfg for target: ${board}"
  67. # Override the above defaults using target.cfg
  68. . "${targetdir}/target.cfg"
  69. [ -z "${grub_scan_disk}" ] && grub_scan_disk="both"
  70. [ "$grub_scan_disk" != "both" ] && [ "$grub_scan_disk" != "ata" ] \
  71. && [ "${grub_scan_disk}" != "ahci" ] && \
  72. grub_scan_disk="both"
  73. [ -z "$tree" ] && err "$board: tree not defined"
  74. [ "${payload_memtest}" != "y" ] && payload_memtest="n"
  75. [ "${payload_grub_withseabios}" = "y" ] && payload_grub="y"
  76. [ "${payload_grub_withseabios}" = "y" ] && \
  77. eval "$(setvars "y" payload_seabios payload_seabios_withgrub)"
  78. [ "$payload_seabios_withgrub" = "y" ] && payload_seabios="y"
  79. [ "$payload_seabios_grubonly" = "y" ] && payload_seabios="y"
  80. [ "$payload_seabios_grubonly" = "y" ] && payload_seabios_withgrub="y"
  81. # The reverse logic must not be applied. If SeaBIOS-with-GRUB works,
  82. # that doesn't mean GRUB-withSeaBIOS will. For example, the board
  83. # might have a graphics card whose vga rom coreboot doesn't execute
  84. [ "$payload_grub" != "y" ] && [ "$payload_seabios" != "y" ] && \
  85. [ "${payload_uboot}" != "y" ] && \
  86. for configfile in "${targetdir}/config/"*; do
  87. [ -e "${configfile}" ] || continue
  88. err "target '${board}' defines no payload"
  89. done
  90. [ "$payload_uboot" != "n" ] && [ "$payload_uboot" != "y" ] && \
  91. payload_uboot="n"
  92. [ "$payload_uboot" = "y" ] && [ -z "$uboot_config" ] && \
  93. uboot_config="default"
  94. # Override all payload directives with cmdline args
  95. [ -z "${_payload}" ] && return 0
  96. printf "setting payload to: %s\n" "${_payload}"
  97. eval "$(setvars "n" payload_grub payload_memtest payload_seabios \
  98. payload_seabios_withgrub payload_uboot payload_grub_withseabios \
  99. payload_seabios_grubonly)"
  100. eval "payload_${_payload}=y"
  101. }
  102. build_payloads()
  103. {
  104. romdir="bin/${board}"
  105. cbdir="src/coreboot/${board}"
  106. [ "${board}" = "${tree}" ] || cbdir="src/coreboot/${tree}"
  107. cbfstool="cbutils/${tree}/cbfstool"
  108. cbrom="${cbdir}/build/coreboot.rom"
  109. [ -f "$cbfstool" ] || x_ ./update trees -b coreboot utils $tree
  110. memtest_bin="memtest86plus/memtest"
  111. [ "${payload_memtest}" != "y" ] || [ -f "src/${memtest_bin}" ] || \
  112. x_ ./update trees -b memtest86plus
  113. [ "$payload_seabios" = "y" ] && x_ ./update trees -b seabios
  114. if [ "$payload_grub" = "y" ] || [ "$payload_seabios_withgrub" = "y" ] \
  115. || [ "$payload_seabios_grubonly" = "y" ]; then build_grub_payload
  116. fi
  117. [ "${payload_uboot}" = "y" ] && build_uboot_payload; return 0
  118. }
  119. build_grub_payload()
  120. {
  121. x_ mkdir -p elf/grub
  122. for keymapfile in config/grub/keymap/*.gkb; do
  123. [ -f "${keymapfile}" ] || continue
  124. keymaps="${keymaps} ${keymapfile}"
  125. done
  126. [ -z "$_keyboard" ] || [ -f "$grubcfgsdir/keymap/$_keyboard.gkb" ] || \
  127. err "build_grub_payload: $_keyboard layout not defined"
  128. [ -n "$_keyboard" ] && keymaps="${grubcfgsdir}/keymap/${_keyboard}.gkb"
  129. [ -f "$grubelf" ] && return 0
  130. [ -f "src/grub/grub-mkstandalone" ] || x_ ./update trees -b grub
  131. ./src/grub/grub-mkstandalone \
  132. --grub-mkimage="src/grub/grub-mkimage" \
  133. -O i386-coreboot \
  134. -o "elf/grub/grub.elf" \
  135. -d "src/grub/grub-core/" \
  136. --fonts= --themes= --locales= \
  137. --modules="${grub_modules}" \
  138. --install-modules="${grub_install_modules}" \
  139. "/boot/grub/grub.cfg=${grubcfgsdir}/config/grub_memdisk.cfg" \
  140. "/boot/grub/grub_default.cfg=${grubcfgsdir}/config/grub.cfg" || \
  141. err "could not generate grub.elf"
  142. }
  143. build_uboot_payload()
  144. {
  145. x_ ./update trees -b u-boot ${board}
  146. ubdir="elf/u-boot/${board}/${uboot_config}"
  147. ubootelf="${ubdir}/u-boot.elf"
  148. [ ! -f "${ubootelf}" ] && [ -f "${ubdir}/u-boot" ] && \
  149. ubootelf="${ubdir}/u-boot"
  150. [ -f "${ubootelf}" ] && return 0
  151. err "Can't find u-boot build for board, $board";
  152. }
  153. build_target_mainboard()
  154. {
  155. rm -f "${romdir}/"* || err "!prepare, rm files, ${romdir}"
  156. for x in "normal" "vgarom" "libgfxinit"; do
  157. initmode="${x}"
  158. hmode="vesafb"
  159. [ "${initmode}" = "vgarom" ] || hmode="corebootfb"
  160. modes="${hmode} txtmode"
  161. [ -z "${_displaymode}" ] || modes="${_displaymode}"
  162. for y in ${modes}; do
  163. displaymode="${y}"
  164. [ "${initmode}" = "normal" ] && \
  165. [ "$displaymode" != "txtmode" ] && continue
  166. cbcfg="${targetdir}/config/${initmode}_${displaymode}"
  167. [ "${initmode}" = "normal" ] && cbcfg="${cbcfg%_*}"
  168. build_roms "${cbcfg}"
  169. done
  170. done
  171. }
  172. # Main ROM building function. This calls all other functions below
  173. build_roms()
  174. {
  175. cbcfg="${1}"
  176. if [ ! -f "${cbcfg}" ]; then
  177. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  178. "$cbcfg" "$board" "$displaymode" "$initmode" 1>&2
  179. return 0
  180. fi
  181. x_ ./update trees -b coreboot ${board}
  182. _cbrom="elf/coreboot_nopayload_DO_NOT_FLASH"
  183. _cbrom="${_cbrom}/${board}/${initmode}_${displaymode}"
  184. [ "${initmode}" = "normal" ] && \
  185. _cbrom="${_cbrom%"_${displaymode}"}"
  186. _cbrom="${_cbrom}/coreboot.rom"
  187. cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  188. x_ cp "${_cbrom}" "${cbrom}"
  189. [ "$displaymode" != "txtmode" ] || [ "${payload_memtest}" != "y" ] || \
  190. x_ "${cbfstool}" "${cbrom}" add-payload \
  191. -f "src/${memtest_bin}" -n img/memtest -c lzma
  192. [ "${payload_seabios}" = "y" ] && build_seabios_roms
  193. [ "$payload_grub" != "y" ] || x_ build_grub_roms "$cbrom" "grub"
  194. [ "${payload_uboot}" = "y" ] || return 0
  195. x_ cp "${_cbrom}" "${cbrom}"
  196. build_uboot_roms
  197. }
  198. build_seabios_roms()
  199. {
  200. if [ "${payload_seabios_withgrub}" = "y" ]; then
  201. t=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  202. x_ cp "${cbrom}" "${t}"
  203. x_ build_grub_roms "${t}" "seabios_withgrub"
  204. else
  205. t=$(mkSeabiosRom "${cbrom}" "fallback/payload") || \
  206. err "build_seabios_roms: cannot build tmprom"
  207. newrom="${romdir}/seabios_${board}_${initmode}_${displaymode}"
  208. [ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \
  209. && newrom="${newrom}_${board}_${initmode}"
  210. x_ moverom "${t}" "${newrom}.rom"
  211. fi
  212. x_ rm -f "${t}"
  213. }
  214. # Make separate ROM images with GRUB payload, for each supported keymap
  215. build_grub_roms()
  216. {
  217. tmprom="${1}"
  218. payload1="${2}" # allow values: grub, seabios, seabios_withgrub
  219. grub_cbfs="fallback/payload"
  220. if [ "$payload1" = "grub" ] && [ "$payload_grub_withseabios" = "y" ]
  221. then
  222. _tmpmvrom=$(mkSeabiosRom "$tmprom" "seabios.elf") || \
  223. err "build_grub_roms 1 $board: can't build tmprom"
  224. x_ mv "$_tmpmvrom" "$tmprom"
  225. elif [ "$payload1" != "grub" ] && [ "$payload_seabios_withgrub" = "y" ]
  226. then
  227. grub_cbfs="img/grub2"
  228. _tmpmvrom=$(mkSeabiosRom "$tmprom" fallback/payload) || \
  229. err "build_grub_roms 2 $board: can't build tmprom"
  230. x_ mv "$_tmpmvrom" "$tmprom"
  231. fi
  232. # we only need insert grub.elf once, for each coreboot config:
  233. x_ "${cbfstool}" "${tmprom}" add-payload -f "${grubelf}" \
  234. -n ${grub_cbfs} -c lzma
  235. # we only need insert background.png once, for each coreboot config:
  236. if [ "${displaymode}" = "vesafb" ] || \
  237. [ "${displaymode}" = "corebootfb" ]; then
  238. backgroundfile="config/grub/background/${grub_background}"
  239. "${cbfstool}" "${tmprom}" add -f ${backgroundfile} \
  240. -n background.png -t raw || \
  241. err "insert background, ${backgroundfile}"
  242. fi
  243. tmpcfg=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  244. printf "set grub_scan_disk=\"%s\"\n" "$grub_scan_disk" >"$tmpcfg" \
  245. || err "set grub_scandisk, $grub_scan_disk, $tmpcfg"
  246. [ "${grub_scan_disk}" = "both" ] || \
  247. x_ "$cbfstool" "$tmprom" add -f "$tmpcfg" -n scan.cfg -t raw
  248. printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \
  249. err "set timeout, ${grub_timeout}, ${tmpcfg}"
  250. [ -z "${grub_timeout}" ] || x_ "${cbfstool}" "${tmprom}" add \
  251. -f "${tmpcfg}" -n timeout.cfg -t raw
  252. x_ rm -f "${tmpcfg}"
  253. for keymapfile in ${keymaps}; do
  254. [ -f "${keymapfile}" ] || continue
  255. keymap="${keymapfile##*/}"
  256. keymap="${keymap%.gkb}"
  257. tmpgrubrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  258. x_ cp "${tmprom}" "${tmpgrubrom}"
  259. x_ "$cbfstool" "$tmpgrubrom" add -f "$keymapfile" \
  260. -n keymap.gkb -t raw
  261. newrom="${romdir}/${payload1}_${board}_${initmode}_"
  262. newrom="${newrom}${displaymode}_${keymap}.rom"
  263. [ "${initmode}" = "normal" ] && \
  264. newrom="${romdir}/${payload1}_${board}_" && \
  265. newrom="${newrom}${initmode}_${keymap}.rom"
  266. x_ moverom "${tmpgrubrom}" "${newrom}"
  267. [ "${payload_seabios_grubonly}" = "y" ] && \
  268. mkSeabiosGrubonlyRom "${tmpgrubrom}" "${newrom}"
  269. x_ rm -f "${tmpgrubrom}"
  270. done
  271. }
  272. # make a rom in /tmp/ and then print the path of that ROM
  273. mkSeabiosRom() {
  274. _cbrom="${1}" # rom to insert seabios in. will not be touched
  275. # (a tmpfile will be made instead)
  276. _seabios_cbfs_path="${2}" # e.g. fallback/payload
  277. _seabioself="elf/seabios/default/${initmode}/bios.bin.elf"
  278. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  279. x_ cp "${_cbrom}" "${tmprom}"
  280. x_ "$cbfstool" "$tmprom" add-payload -f "$_seabioself" \
  281. -n "${_seabios_cbfs_path}" -c lzma
  282. x_ "$cbfstool" "$tmprom" add-int -i 3000 -n etc/ps2-keyboard-spinup
  283. z="2"; [ "$initmode" = "vgarom" ] && z="0"
  284. x_ "$cbfstool" "$tmprom" add-int -i $z -n etc/pci-optionrom-exec
  285. x_ "$cbfstool" "$tmprom" add-int -i 0 -n etc/optionroms-checksum
  286. [ "$initmode" != "libgfxinit" ] || \
  287. x_ "$cbfstool" "$tmprom" add -f "$seavgabiosrom" \
  288. -n vgaroms/seavgabios.bin -t raw
  289. printf "%s\n" "${tmprom}"
  290. }
  291. # SeaGRUB configuration
  292. mkSeabiosGrubonlyRom()
  293. {
  294. _grubrom="${1}"
  295. _newrom="${2}"
  296. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  297. # only load grub, by inserting a custom bootorder file
  298. printf "/rom@img/grub2\n" > "$tmpbootorder" || err "printf bootorder"
  299. x_ "${cbfstool}" "${_grubrom}" \
  300. add -f "${tmpbootorder}" -n bootorder -t raw
  301. x_ rm -f "${tmpbootorder}"
  302. x_ "${cbfstool}" "${_grubrom}" add-int -i 0 -n etc/show-boot-menu
  303. x_ moverom "${_grubrom}" "${_newrom%.rom}_grubonly.rom"
  304. }
  305. build_uboot_roms()
  306. {
  307. tmprom=$(mkUbootRom "${cbrom}" "fallback/payload") || \
  308. err "build_uboot_roms $board: could not create tmprom"
  309. newrom="${romdir}/uboot_payload_${board}_${initmode}_${displaymode}.rom"
  310. x_ moverom "${tmprom}" "${newrom}"
  311. x_ rm -f "${tmprom}"
  312. }
  313. # make a rom in /tmp/ and then print the path of that ROM
  314. mkUbootRom() {
  315. _cbrom="${1}"
  316. _uboot_cbfs_path="${2}"
  317. _ubdir="elf/u-boot/${board}/${uboot_config}"
  318. _ubootelf="${_ubdir}/u-boot.elf"
  319. [ -f "${_ubootelf}" ] || _ubootelf="${_ubdir}/u-boot"
  320. [ -f "$_ubootelf" ] || err "mkUbootRom: $board: cant find u-boot"
  321. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  322. x_ cp "${_cbrom}" "${tmprom}"
  323. x_ "$cbfstool" "$tmprom" add-payload -f "$_ubootelf" \
  324. -n "${_uboot_cbfs_path}" -c lzma
  325. printf "%s\n" "${tmprom}"
  326. }
  327. moverom()
  328. {
  329. printf "Creating target image: %s\n" "$2"
  330. x_ mkdir -p "${2%/*}"
  331. x_ cp "$1" "$2"
  332. }
  333. usage()
  334. {
  335. cat <<- EOF
  336. USAGE: ./build roms targetname
  337. To build *all* boards, do this: ./build roms all
  338. To list *all* boards, do this: ./build roms list
  339. Optional Flags:
  340. -d: displaymode
  341. -p: payload
  342. -k: keyboard layout
  343. Example commands:
  344. ./build roms x60
  345. ./build roms x200_8mb x60
  346. ./build roms x60 -p grub -d corebootfb -k usqwerty
  347. possible values for 'target':
  348. $(items "config/coreboot")
  349. Refer to the ${projectname} documentation for more information.
  350. EOF
  351. }
  352. main $@