roms 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 memtest_bin"
  19. v="romdir cbrom initmode displaymode cbcfg targetdir tree keymaps"
  20. v="${v} grub_timeout board grub_scan_disk"
  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. for configfile in "${targetdir}/config/"*; do
  86. [ -e "${configfile}" ] || continue
  87. err "target '${board}' defines no payload"
  88. done
  89. # Override all payload directives with cmdline args
  90. [ -z "${_payload}" ] && return 0
  91. printf "setting payload to: %s\n" "${_payload}"
  92. eval "$(setvars "n" payload_grub payload_memtest payload_seabios \
  93. payload_seabios_withgrub payload_grub_withseabios \
  94. payload_seabios_grubonly)"
  95. eval "payload_${_payload}=y"
  96. }
  97. build_payloads()
  98. {
  99. romdir="bin/${board}"
  100. cbdir="src/coreboot/${board}"
  101. [ "${board}" = "${tree}" ] || cbdir="src/coreboot/${tree}"
  102. cbfstool="cbutils/${tree}/cbfstool"
  103. cbrom="${cbdir}/build/coreboot.rom"
  104. [ -f "$cbfstool" ] || x_ ./update trees -b coreboot utils $tree
  105. memtest_bin="memtest86plus/build64/memtest.bin"
  106. [ "${payload_memtest}" != "y" ] || [ -f "src/${memtest_bin}" ] || \
  107. x_ ./update trees -b memtest86plus
  108. [ "$payload_seabios" = "y" ] && x_ ./update trees -b seabios
  109. if [ "$payload_grub" = "y" ] || [ "$payload_seabios_withgrub" = "y" ] \
  110. || [ "$payload_seabios_grubonly" = "y" ]; then build_grub_payload
  111. fi
  112. }
  113. build_grub_payload()
  114. {
  115. x_ mkdir -p elf/grub
  116. for keymapfile in config/grub/keymap/*.gkb; do
  117. [ -f "${keymapfile}" ] || continue
  118. keymaps="${keymaps} ${keymapfile}"
  119. keymap="${keymapfile##*/}"
  120. keymap="${keymap%.gkb}"
  121. [ -f "elf/grub/keymap_$keymap.cfg" ] || printf "keymap %s\n" \
  122. "$keymap" > "elf/grub/keymap_$keymap.cfg" || err "!key"; :
  123. done
  124. [ -n "${_keyboard}" ] && keymaps="${kmapdir}/${_keyboard}.gkb"
  125. [ -f "$grubelf" ] && return 0
  126. [ -f "src/grub/grub-mkstandalone" ] || x_ ./update trees -b grub
  127. ./src/grub/grub-mkstandalone \
  128. --grub-mkimage="src/grub/grub-mkimage" \
  129. -O i386-coreboot \
  130. -o "elf/grub/grub.elf" \
  131. -d "src/grub/grub-core/" \
  132. --fonts= --themes= --locales= \
  133. --modules="${grub_modules}" \
  134. --install-modules="${grub_install_modules}" \
  135. "${layoutdir}/colemak.gkb=${grubcfgsdir}/keymap/colemak.gkb" \
  136. "${layoutdir}/deqwertz.gkb=${grubcfgsdir}/keymap/deqwertz.gkb" \
  137. "${layoutdir}/esqwerty.gkb=${grubcfgsdir}/keymap/esqwerty.gkb" \
  138. "${layoutdir}/frazerty.gkb=${grubcfgsdir}/keymap/frazerty.gkb" \
  139. "${layoutdir}/frdvbepo.gkb=${grubcfgsdir}/keymap/frdvbepo.gkb" \
  140. "${layoutdir}/itqwerty.gkb=${grubcfgsdir}/keymap/itqwerty.gkb" \
  141. "${layoutdir}/svenska.gkb=${grubcfgsdir}/keymap/svenska.gkb" \
  142. "${layoutdir}/trqwerty.gkb=${grubcfgsdir}/keymap/trqwerty.gkb" \
  143. "${layoutdir}/ukdvorak.gkb=${grubcfgsdir}/keymap/ukdvorak.gkb" \
  144. "${layoutdir}/ukqwerty.gkb=${grubcfgsdir}/keymap/ukqwerty.gkb" \
  145. "${layoutdir}/usdvorak.gkb=${grubcfgsdir}/keymap/usdvorak.gkb" \
  146. "${layoutdir}/usqwerty.gkb=${grubcfgsdir}/keymap/usqwerty.gkb" \
  147. "/boot/grub/grub.cfg=${grubcfgsdir}/config/grub_memdisk.cfg" \
  148. "/boot/grub/grub_default.cfg=${grubcfgsdir}/config/grub.cfg" || \
  149. err "could not generate grub.elf"
  150. }
  151. build_target_mainboard()
  152. {
  153. rm -f "${romdir}/"* || err "!prepare, rm files, ${romdir}"
  154. for x in "normal" "vgarom" "libgfxinit"; do
  155. initmode="${x}"
  156. hmode="vesafb"
  157. [ "${initmode}" = "vgarom" ] || hmode="corebootfb"
  158. modes="${hmode} txtmode"
  159. [ -z "${_displaymode}" ] || modes="${_displaymode}"
  160. for y in ${modes}; do
  161. displaymode="${y}"
  162. [ "${initmode}" = "normal" ] && \
  163. [ "$displaymode" != "txtmode" ] && continue
  164. cbcfg="${targetdir}/config/${initmode}_${displaymode}"
  165. [ "${initmode}" = "normal" ] && cbcfg="${cbcfg%_*}"
  166. build_roms "${cbcfg}"
  167. done
  168. done
  169. }
  170. # Main ROM building function. This calls all other functions below
  171. build_roms()
  172. {
  173. cbcfg="${1}"
  174. if [ ! -f "${cbcfg}" ]; then
  175. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  176. "$cbcfg" "$board" "$displaymode" "$initmode" 1>&2
  177. return 0
  178. fi
  179. x_ ./update trees -b coreboot ${board}
  180. _cbrom="elf/coreboot_nopayload_DO_NOT_FLASH"
  181. _cbrom="${_cbrom}/${board}/${initmode}_${displaymode}"
  182. [ "${initmode}" = "normal" ] && \
  183. _cbrom="${_cbrom%"_${displaymode}"}"
  184. _cbrom="${_cbrom}/coreboot.rom"
  185. cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  186. x_ cp "${_cbrom}" "${cbrom}"
  187. [ "${payload_memtest}" != "y" ] || \
  188. x_ "${cbfstool}" "${cbrom}" add-payload \
  189. -f "src/${memtest_bin}" -n img/memtest -c lzma
  190. [ "${payload_seabios}" = "y" ] && build_seabios_roms
  191. [ "$payload_grub" != "y" ] || x_ build_grub_roms "$cbrom" "grub"
  192. return 0
  193. }
  194. build_seabios_roms()
  195. {
  196. if [ "${payload_seabios_withgrub}" = "y" ]; then
  197. t=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  198. x_ cp "${cbrom}" "${t}"
  199. x_ build_grub_roms "${t}" "seabios_withgrub"
  200. else
  201. t=$(mkSeabiosRom "${cbrom}" "fallback/payload") || \
  202. err "build_seabios_roms: cannot build tmprom"
  203. newrom="${romdir}/seabios_${board}_${initmode}_${displaymode}"
  204. [ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \
  205. && newrom="${newrom}_${board}_${initmode}"
  206. x_ moverom "${t}" "${newrom}.rom"
  207. fi
  208. x_ rm -f "${t}"
  209. }
  210. # Make separate ROM images with GRUB payload, for each supported keymap
  211. build_grub_roms()
  212. {
  213. tmprom="${1}"
  214. payload1="${2}" # allow values: grub, seabios, seabios_withgrub
  215. grub_cbfs="fallback/payload"
  216. if [ "$payload1" = "grub" ] && [ "$payload_grub_withseabios" = "y" ]
  217. then
  218. _tmpmvrom=$(mkSeabiosRom "$tmprom" "seabios.elf") || \
  219. err "build_grub_roms 1 $board: can't build tmprom"
  220. x_ mv "$_tmpmvrom" "$tmprom"
  221. elif [ "$payload1" != "grub" ] && [ "$payload_seabios_withgrub" = "y" ]
  222. then
  223. grub_cbfs="img/grub2"
  224. _tmpmvrom=$(mkSeabiosRom "$tmprom" fallback/payload) || \
  225. err "build_grub_roms 2 $board: can't build tmprom"
  226. x_ mv "$_tmpmvrom" "$tmprom"
  227. fi
  228. # we only need insert grub.elf once, for each coreboot config:
  229. x_ "${cbfstool}" "${tmprom}" add-payload -f "${grubelf}" \
  230. -n ${grub_cbfs} -c lzma
  231. # we only need insert background.png once, for each coreboot config:
  232. if [ "${displaymode}" = "vesafb" ] || \
  233. [ "${displaymode}" = "corebootfb" ]; then
  234. backgroundfile="config/grub/background/${grub_background}"
  235. "${cbfstool}" "${tmprom}" add -f ${backgroundfile} \
  236. -n background.png -t raw || \
  237. err "insert background, ${backgroundfile}"
  238. fi
  239. tmpcfg=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  240. printf "set grub_scan_disk=\"%s\"\n" "$grub_scan_disk" >"$tmpcfg" \
  241. || err "set grub_scandisk, $grub_scan_disk, $tmpcfg"
  242. [ "${grub_scan_disk}" = "both" ] || \
  243. x_ "$cbfstool" "$tmprom" add -f "$tmpcfg" -n scan.cfg -t raw
  244. printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \
  245. err "set timeout, ${grub_timeout}, ${tmpcfg}"
  246. [ -z "${grub_timeout}" ] || x_ "${cbfstool}" "${tmprom}" add \
  247. -f "${tmpcfg}" -n timeout.cfg -t raw
  248. x_ rm -f "${tmpcfg}"
  249. for keymapfile in ${keymaps}; do
  250. [ -f "${keymapfile}" ] || continue
  251. keymap="${keymapfile##*/}"
  252. keymap="${keymap%.gkb}"
  253. tmpgrubrom=$(mkGrubRom "${keymap}" "${tmprom}") || \
  254. err "build_grub_roms ${board}: could not create tmprom"
  255. newrom="${romdir}/${payload1}_${board}_${initmode}_"
  256. newrom="${newrom}${displaymode}_${keymap}.rom"
  257. [ "${initmode}" = "normal" ] && \
  258. newrom="${romdir}/${payload1}_${board}_" && \
  259. newrom="${newrom}${initmode}_${keymap}.rom"
  260. x_ moverom "${tmpgrubrom}" "${newrom}"
  261. [ "${payload_seabios_grubonly}" = "y" ] && \
  262. mkSeabiosGrubonlyRom "${tmpgrubrom}" "${newrom}"
  263. x_ rm -f "${tmpgrubrom}"
  264. done
  265. }
  266. # make a rom in /tmp/ and then print the path of that ROM
  267. mkGrubRom() {
  268. _keymap="${1}"
  269. _cbrom="${2}"
  270. keymapcfg="elf/grub/keymap_${_keymap}.cfg"
  271. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  272. x_ cp "${_cbrom}" "${tmprom}"
  273. x_ "$cbfstool" "$tmprom" add -f "$keymapcfg" -n keymap.cfg -t raw
  274. printf "%s\n" "${tmprom}"
  275. }
  276. # make a rom in /tmp/ and then print the path of that ROM
  277. mkSeabiosRom() {
  278. _cbrom="${1}" # rom to insert seabios in. will not be touched
  279. # (a tmpfile will be made instead)
  280. _seabios_cbfs_path="${2}" # e.g. fallback/payload
  281. _seabioself="elf/seabios/default/${initmode}/bios.bin.elf"
  282. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  283. x_ cp "${_cbrom}" "${tmprom}"
  284. x_ "$cbfstool" "$tmprom" add-payload -f "$_seabioself" \
  285. -n "${_seabios_cbfs_path}" -c lzma
  286. x_ "$cbfstool" "$tmprom" add-int -i 3000 -n etc/ps2-keyboard-spinup
  287. z="2"; [ "$initmode" = "vgarom" ] && z="0"
  288. x_ "$cbfstool" "$tmprom" add-int -i $z -n etc/pci-optionrom-exec
  289. x_ "$cbfstool" "$tmprom" add-int -i 0 -n etc/optionroms-checksum
  290. [ "$initmode" != "libgfxinit" ] || \
  291. x_ "$cbfstool" "$tmprom" add -f "$seavgabiosrom" \
  292. -n vgaroms/seavgabios.bin -t raw
  293. printf "%s\n" "${tmprom}"
  294. }
  295. # SeaGRUB configuration
  296. mkSeabiosGrubonlyRom()
  297. {
  298. _grubrom="${1}"
  299. _newrom="${2}"
  300. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  301. # only load grub, by inserting a custom bootorder file
  302. printf "/rom@img/grub2\n" > "$tmpbootorder" || err "printf bootorder"
  303. x_ "${cbfstool}" "${_grubrom}" \
  304. add -f "${tmpbootorder}" -n bootorder -t raw
  305. x_ rm -f "${tmpbootorder}"
  306. x_ "${cbfstool}" "${_grubrom}" add-int -i 0 -n etc/show-boot-menu
  307. x_ moverom "${_grubrom}" "${_newrom%.rom}_grubonly.rom"
  308. }
  309. moverom()
  310. {
  311. printf "Creating target image: %s\n" "$2"
  312. x_ mkdir -p "${2%/*}"
  313. x_ cp "$1" "$2"
  314. }
  315. usage()
  316. {
  317. cat <<- EOF
  318. USAGE: ./build roms targetname
  319. To build *all* boards, do this: ./build roms all
  320. To list *all* boards, do this: ./build roms list
  321. Optional Flags:
  322. -d: displaymode
  323. -p: payload
  324. -k: keyboard layout
  325. Example commands:
  326. ./build roms x60
  327. ./build roms x200_8mb x60
  328. ./build roms x60 -p grub -d corebootfb -k usqwerty
  329. possible values for 'target':
  330. $(items "config/coreboot")
  331. Refer to the ${projectname} documentation for more information.
  332. EOF
  333. }
  334. main $@