roms 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # SPDX-FileCopyrightText: 2014-2016,2020,2021,2023 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. kmapdir="config/grub/keymap"
  16. # Disable all payloads by default.
  17. # target.cfg files have to specifically enable [a] payload(s)
  18. pv="payload_grub payload_grub_withseabios payload_seabios payload_memtest"
  19. pv="${pv} payload_seabios_withgrub payload_seabios_grubonly payload_uboot memtest_bin"
  20. v="romdir cbrom initmode displaymode cbcfg targetdir tree arch"
  21. v="${v} grub_timeout ubdir vendorfiles board grub_scan_disk uboot_config"
  22. eval "$(setvars "n" ${pv})"
  23. eval "$(setvars "" ${v} boards _displaymode _payload _keyboard all targets)"
  24. main()
  25. {
  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})"
  48. eval "$(setvars "" ${v})"
  49. grub_background="background1280x800.png"
  50. board="${x}"
  51. check_target
  52. prepare_target
  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. printf "%s^^ ROM images available in these directories.\n\n" "${targets}"
  59. printf "DO NOT flash ROM images from elf/ - please use bin/ instead.\n"
  60. }
  61. check_target()
  62. {
  63. targetdir="${cfgsdir}/${board}"
  64. [ -f "${targetdir}/target.cfg" ] || \
  65. err "Missing target.cfg for target: ${board}"
  66. # Override the above defaults using target.cfg
  67. . "${targetdir}/target.cfg"
  68. [ -z "${grub_scan_disk}" ] && \
  69. 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}" ] && \
  74. err "Target '${board}' defines no tree. Skipping build."
  75. [ -z "${arch}" ] && \
  76. err "Target '${board}' defines no arch. Skipping build."
  77. [ "${payload_memtest}" != "y" ] && \
  78. payload_memtest="n"
  79. [ "${payload_grub_withseabios}" = "y" ] && \
  80. payload_grub="y"
  81. [ "${payload_grub_withseabios}" = "y" ] && \
  82. eval "$(setvars "y" payload_seabios payload_seabios_withgrub)"
  83. [ "${payload_seabios_withgrub}" = "y" ] && \
  84. payload_seabios="y"
  85. if [ "${payload_seabios_grubonly}" = "y" ]; then
  86. payload_seabios="y"
  87. payload_seabios_withgrub="y"
  88. fi
  89. # The reverse logic must not be applied. If SeaBIOS-with-GRUB works,
  90. # that doesn't mean GRUB-withSeaBIOS will. For example, the board
  91. # might have a graphics card whose vga rom coreboot doesn't execute
  92. [ "${payload_grub}" != "y" ] && [ "${payload_seabios}" != "y" ] && \
  93. [ "${payload_uboot}" != "y" ] && \
  94. for configfile in "${targetdir}/config/"*; do
  95. [ -e "${configfile}" ] || continue
  96. err "target '${board}' defines no payload"
  97. done
  98. [ "${payload_uboot}" != "n" ] && [ "${payload_uboot}" != "y" ] && \
  99. payload_uboot="n"
  100. [ "${payload_uboot}" = "y" ] && [ -z "${uboot_config}" ] && \
  101. uboot_config="default"
  102. [ "${vendorfiles}" != "n" ] && [ "${vendorfiles}" != "y" ] && \
  103. vendorfiles="y"
  104. # Override all payload directives with cmdline args
  105. [ -z "${_payload}" ] && return 0
  106. printf "setting payload to: %s\n" "${_payload}"
  107. eval "$(setvars "n" payload_grub payload_memtest payload_seabios \
  108. payload_seabios_withgrub payload_uboot payload_grub_withseabios \
  109. payload_seabios_grubonly)"
  110. eval "payload_${_payload}=y"
  111. }
  112. prepare_target()
  113. {
  114. romdir="bin/${board}"
  115. cbdir="src/coreboot/${board}"
  116. [ "${board}" = "${tree}" ] || cbdir="src/coreboot/${tree}"
  117. cbfstool="cbutils/${tree}/cbfstool"
  118. cbrom="${cbdir}/build/coreboot.rom"
  119. [ -f "${cbfstool}" ] || \
  120. x_ ./update trees -b coreboot utils ${tree}
  121. build_dependency_seabios
  122. memtest_bin="memtest86plus/build64/memtest.bin"
  123. [ "${payload_memtest}" != "y" ] || [ -f "src/${memtest_bin}" ] || \
  124. x_ ./update trees -b memtest86plus
  125. rm -f "${romdir}/"* || err "!prepare, rm files, ${romdir}"
  126. build_dependency_grub
  127. build_dependency_uboot
  128. build_target
  129. }
  130. build_dependency_seabios()
  131. {
  132. [ "${payload_seabios}" = "y" ] || return 0
  133. [ -f "${seavgabiosrom}" ] && \
  134. [ -f elf/seabios/default/libgfxinit/bios.bin.elf ] && \
  135. [ -f elf/seabios/default/vgarom/bios.bin.elf ] && \
  136. [ -f elf/seabios/default/normal/bios.bin.elf ] && return 0
  137. x_ ./update trees -b seabios
  138. }
  139. build_dependency_grub()
  140. {
  141. [ "${payload_grub}" != "y" ] && \
  142. [ "${payload_seabios_withgrub}" != "y" ] && \
  143. [ "${payload_seabios_grubonly}" != "y" ] && return 0
  144. rebuild_grub="n"
  145. [ -f "${grubelf}" ] || rebuild_grub="y"
  146. for keymapfile in "${kmapdir}"/*.gkb; do
  147. [ "${rebuild_grub}" = "y" ] || break
  148. [ -f "${keymapfile}" ] || continue
  149. keymap="${keymapfile##*/}"
  150. keymap="${keymap%.gkb}"
  151. [ ! -f "elf/grub/keymap_${keymap}.cfg" ] && \
  152. rebuild_grub="y" && break
  153. done
  154. [ "${rebuild_grub}" = "y" ] || return 0
  155. x_ ./build grub
  156. }
  157. build_dependency_uboot()
  158. {
  159. [ "${payload_uboot}" = "y" ] || return 0
  160. x_ ./update trees -b u-boot ${board}
  161. ubdir="elf/u-boot/${board}/${uboot_config}"
  162. ubootelf="${ubdir}/u-boot.elf"
  163. [ ! -f "${ubootelf}" ] && [ -f "${ubdir}/u-boot" ] && \
  164. ubootelf="${ubdir}/u-boot"
  165. [ -f "${ubootelf}" ] && return 0
  166. err "Could not find u-boot build for board, ${board}"
  167. }
  168. build_target()
  169. {
  170. for x in "normal" "vgarom" "libgfxinit"; do
  171. initmode="${x}"
  172. hmode="vesafb"
  173. [ "${initmode}" = "vgarom" ] || hmode="corebootfb"
  174. modes="${hmode} txtmode"
  175. [ -z "${_displaymode}" ] || modes="${_displaymode}"
  176. for y in ${modes}; do
  177. displaymode="${y}"
  178. [ "${initmode}" = "normal" ] && \
  179. [ "$displaymode" != "txtmode" ] && continue
  180. cbcfg="${targetdir}/config/${initmode}_${displaymode}"
  181. [ "${initmode}" = "normal" ] && cbcfg="${cbcfg%_*}"
  182. build_roms "${cbcfg}"
  183. done
  184. done
  185. }
  186. # Main ROM building function. This calls all other functions below
  187. build_roms()
  188. {
  189. cbcfg="${1}"
  190. if [ ! -f "${cbcfg}" ]; then
  191. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  192. "${cbcfg}" "${board}" "${displaymode}" "${initmode}" 1>&2
  193. return 0
  194. fi
  195. x_ ./update trees -b coreboot ${board}
  196. _cbrom="elf/coreboot_nopayload_DO_NOT_FLASH/${board}/${initmode}_${displaymode}"
  197. [ "${initmode}" = "normal" ] && \
  198. _cbrom="${_cbrom%"_${displaymode}"}"
  199. _cbrom="${_cbrom}/coreboot.rom"
  200. cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
  201. x_ cp "${_cbrom}" "${cbrom}"
  202. [ "${payload_memtest}" != "y" ] || \
  203. x_ "${cbfstool}" "${cbrom}" add-payload \
  204. -f "src/${memtest_bin}" -n img/memtest -c lzma
  205. [ "${payload_seabios}" = "y" ] && \
  206. build_seabios_roms
  207. [ "${payload_grub}" != "y" ] || \
  208. x_ build_grub_roms "${cbrom}" "grub"
  209. [ "${payload_uboot}" = "y" ] || return 0
  210. x_ cp "${_cbrom}" "${cbrom}"
  211. build_uboot_roms
  212. }
  213. build_seabios_roms()
  214. {
  215. if [ "${payload_seabios_withgrub}" = "y" ]; then
  216. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  217. x_ cp "${cbrom}" "${tmprom}"
  218. x_ build_grub_roms "${tmprom}" "seabios_withgrub"
  219. x_ rm -f "${tmprom}"
  220. else
  221. t=$(mkSeabiosRom "${cbrom}" "fallback/payload") || \
  222. err "build_seabios_roms: cannot build tmprom"
  223. newrom="${romdir}/seabios_${board}_${initmode}_${displaymode}"
  224. [ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \
  225. && newrom="${newrom}_${board}_${initmode}"
  226. x_ moverom "${t}" "${newrom}.rom"
  227. x_ rm -f "${t}"
  228. fi
  229. }
  230. # Make separate ROM images with GRUB payload, for each supported keymap
  231. build_grub_roms()
  232. {
  233. tmprom="${1}"
  234. payload1="${2}" # allow values: grub, seabios, seabios_withgrub
  235. grub_cbfs="fallback/payload"
  236. [ "${payload1}" = "grub" ] && [ "${payload_grub_withseabios}" = "y" ] \
  237. && x_ mv "$(mkSeabiosRom "${tmprom}" "seabios.elf")" "${tmprom}"
  238. [ "${payload1}" != "grub" ] && [ "${payload_seabios_withgrub}" = "y" ] \
  239. && grub_cbfs="img/grub2" && \
  240. x_ mv "$(mkSeabiosRom "${tmprom}" fallback/payload)" "${tmprom}"
  241. # we only need insert grub.elf once, for each coreboot config:
  242. x_ "${cbfstool}" "${tmprom}" add-payload -f "${grubelf}" \
  243. -n ${grub_cbfs} -c lzma
  244. # we only need insert background.png once, for each coreboot config:
  245. if [ "${displaymode}" = "vesafb" ] || \
  246. [ "${displaymode}" = "corebootfb" ]; then
  247. backgroundfile="config/grub/background/${grub_background}"
  248. "${cbfstool}" "${tmprom}" add -f ${backgroundfile} \
  249. -n background.png -t raw || err "insert background, ${backgroundfile}"
  250. fi
  251. tmpcfg=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  252. printf "set grub_scan_disk=\"%s\"\n" "${grub_scan_disk}" >"${tmpcfg}" \
  253. || err "set grub_scandisk, ${grub_scan_disk}, ${tmpcfg}"
  254. [ "${grub_scan_disk}" = "both" ] || \
  255. x_ "${cbfstool}" "${tmprom}" add -f "${tmpcfg}" -n scan.cfg -t raw
  256. printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \
  257. err "set timeout, ${grub_timeout}, ${tmpcfg}"
  258. [ -z "${grub_timeout}" ] || x_ "${cbfstool}" "${tmprom}" add \
  259. -f "${tmpcfg}" -n timeout.cfg -t raw
  260. x_ rm -f "${tmpcfg}"
  261. keymaps=""
  262. for kmapfile in "${kmapdir}"/*; do
  263. keymaps="${keymaps} ${kmapfile}"
  264. done
  265. [ -z "${_keyboard}" ] || keymaps="${kmapdir}/${_keyboard}.gkb"
  266. for keymapfile in ${keymaps}; do
  267. [ -f "${keymapfile}" ] || continue
  268. keymap="${keymapfile##*/}"
  269. keymap="${keymap%.gkb}"
  270. tmpgrubrom="$(mkGrubRom "${keymap}" "${tmprom}")"
  271. newrom="${romdir}/${payload1}_${board}_${initmode}_"
  272. newrom="${newrom}${displaymode}_${keymap}.rom"
  273. [ "${initmode}" = "normal" ] && \
  274. newrom="${romdir}/${payload1}_${board}_" && \
  275. newrom="${newrom}${initmode}_${keymap}.rom"
  276. x_ moverom "${tmpgrubrom}" "${newrom}"
  277. [ "${payload_seabios_grubonly}" = "y" ] && \
  278. mkSeabiosGrubonlyRom "${tmpgrubrom}" "${newrom}"
  279. x_ rm -f "${tmpgrubrom}"
  280. done
  281. }
  282. # make a rom in /tmp/ and then print the path of that ROM
  283. mkGrubRom() {
  284. _keymap="${1}"
  285. _cbrom="${2}"
  286. keymapcfg="elf/grub/keymap_${_keymap}.cfg"
  287. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  288. x_ cp "${_cbrom}" "${tmprom}"
  289. x_ "${cbfstool}" "${tmprom}" add -f "${keymapcfg}" -n keymap.cfg -t raw
  290. printf "%s\n" "${tmprom}"
  291. }
  292. # make a rom in /tmp/ and then print the path of that ROM
  293. mkSeabiosRom() {
  294. _cbrom="${1}" # rom to insert seabios in. will not be touched
  295. # (a tmpfile will be made instead)
  296. _seabios_cbfs_path="${2}" # e.g. fallback/payload
  297. _seabioself="elf/seabios/default/${initmode}/bios.bin.elf"
  298. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  299. x_ cp "${_cbrom}" "${tmprom}"
  300. x_ "${cbfstool}" "${tmprom}" add-payload -f "${_seabioself}" \
  301. -n "${_seabios_cbfs_path}" -c lzma
  302. x_ "${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup
  303. z="2"; [ "${initmode}" = "vgarom" ] && z="0"
  304. x_ "${cbfstool}" "${tmprom}" add-int -i $z -n etc/pci-optionrom-exec
  305. x_ "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/optionroms-checksum
  306. [ "${initmode}" != "libgfxinit" ] || \
  307. x_ "${cbfstool}" "${tmprom}" add -f "${seavgabiosrom}" \
  308. -n vgaroms/seavgabios.bin -t raw
  309. printf "%s\n" "${tmprom}"
  310. }
  311. # SeaGRUB configuration
  312. mkSeabiosGrubonlyRom()
  313. {
  314. _grubrom="${1}"
  315. _newrom="${2}"
  316. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  317. # only load grub, by inserting a custom bootorder file
  318. printf "/rom@img/grub2\n" > "${tmpbootorder}" || err "printf bootorder"
  319. x_ "${cbfstool}" "${_grubrom}" \
  320. add -f "${tmpbootorder}" -n bootorder -t raw
  321. x_ rm -f "${tmpbootorder}"
  322. x_ "${cbfstool}" "${_grubrom}" add-int -i 0 -n etc/show-boot-menu
  323. x_ moverom "${_grubrom}" "${_newrom%.rom}_grubonly.rom"
  324. }
  325. build_uboot_roms()
  326. {
  327. tmprom="$(mkUbootRom "${cbrom}" "fallback/payload")"
  328. newrom="${romdir}/uboot_payload_${board}_${initmode}_${displaymode}.rom"
  329. [ "${initmode}" = "normal" ] && \
  330. newrom="${romdir}/uboot_payload_${board}_${initmode}.rom"
  331. x_ moverom "${tmprom}" "${newrom}"
  332. x_ rm -f "${tmprom}"
  333. }
  334. # make a rom in /tmp/ and then print the path of that ROM
  335. mkUbootRom() {
  336. _cbrom="${1}"
  337. _uboot_cbfs_path="${2}"
  338. _ubdir="elf/u-boot/${board}/${uboot_config}"
  339. _ubootelf="${_ubdir}/u-boot.elf"
  340. [ -f "${_ubootelf}" ] || _ubootelf="${_ubdir}/u-boot"
  341. [ -f "${_ubootelf}" ] || err "mkUbootRom: ${board}: cant find u-boot"
  342. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  343. x_ cp "${_cbrom}" "${tmprom}"
  344. x_ "${cbfstool}" "${tmprom}" add-payload -f "${_ubootelf}" \
  345. -n "${_uboot_cbfs_path}" -c lzma
  346. printf "%s\n" "${tmprom}"
  347. }
  348. # it is assumed that no other work will be done on the ROM
  349. # after calling this function. therefore this function is "final"
  350. moverom() {
  351. rompath="${1}"
  352. newrom="${2}"
  353. [ "${vendorfiles}" = "n" ] && newrom="${newrom%.rom}_noblobs.rom"
  354. printf "Creating target image: %s\n" "${newrom}"
  355. [ -d "${newrom%/*}" ] || x_ mkdir -p "${newrom%/*}/"
  356. x_ modify_coreboot_rom
  357. x_ cp "${rompath}" "${newrom}"
  358. }
  359. modify_coreboot_rom()
  360. {
  361. tmpmvrom="$(mktemp -t rom.XXXXXXXXXX)"
  362. x_ rm -f "${tmpmvrom}"
  363. if [ "${romtype}" = "d8d16sas" ]; then
  364. # pike2008 roms hang seabios. an empty rom will override
  365. # the built-in one, thus disabling all execution of it
  366. x_ touch "${tmpmvrom}"
  367. for deviceID in "0072" "3050"; do
  368. x_ "${cbfstool}" "${rompath}" add -f "${tmpmvrom}" \
  369. -n "pci1000,${deviceID}.rom" -t raw
  370. done
  371. elif [ "${romtype}" = "i945 laptop" ]; then
  372. # for bucts-based installation method from factory bios
  373. dd if="${rompath}" of="${tmpmvrom}" bs=1 \
  374. skip=$(($(stat -c %s "${rompath}") - 0x10000)) \
  375. count=64k || err "modrom 1, copy bootblock"
  376. dd if="${tmpmvrom}" of="${rompath}" bs=1 \
  377. seek=$(($(stat -c %s "${rompath}") - 0x20000)) count=64k \
  378. conv=notrunc || err "modrom 2, insert new bootblock"
  379. x_ rm -f "${tmpmvrom}"
  380. fi
  381. x_ rm -f "${tmpmvrom}"
  382. }
  383. usage()
  384. {
  385. cat <<- EOF
  386. USAGE: ./build roms targetname
  387. To build *all* boards, do this: ./build roms all
  388. To list *all* boards, do this: ./build roms list
  389. Optional Flags:
  390. -d: displaymode
  391. -p: payload
  392. -k: keyboard layout
  393. Example commands:
  394. ./build roms x60
  395. ./build roms x200_8mb x60
  396. ./build roms x60 -p grub -d corebootfb -k usqwerty
  397. possible values for 'target':
  398. $(items "config/coreboot")
  399. Refer to the ${projectname} documentation for more information.
  400. EOF
  401. }
  402. main $@