roms_helper 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #!/usr/bin/env bash
  2. # helper script: create ROM images for a given mainboard
  3. #
  4. # Copyright (C) 2020,2021 Leah Rowe <info@minifree.org>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # This script assumes that the working directory is the root
  20. # of git or release archive
  21. [ "x${DEBUG+set}" = 'xset' ] && set -v
  22. set -u -e
  23. projectname="$(cat projectname)"
  24. if (( $# != 1 )); then
  25. printf "Usage: ./build boot roms boardname\n"
  26. printf "Example: ./build boot roms x60\n"
  27. printf "Example: ./build boot roms x60 x200_8mb\n"
  28. printf "Example: ./build boot roms all\n"
  29. printf "You need to specify exactly 1 argument\n"
  30. exit 1
  31. fi
  32. board="${1}"
  33. if [ ! -d "resources/coreboot/${board}" ]; then
  34. printf "build/roms: Target %s does not exist in the %s build system. Skipping build.\n" "${projectname}" "${board}"
  35. exit 1
  36. fi
  37. if [ ! -f "resources/coreboot/${board}/board.cfg" ]; then
  38. printf "build/roms: Target %s does not have a board.cfg. Skipping build.\n" "${board}"
  39. exit 1
  40. fi
  41. cbtree="undefined"
  42. romtype="normal" # optional parameter in board.cfg. "normal" is default
  43. arch="undefined"
  44. # Disable all payloads by default.
  45. # board.cfg files have to specifically enable [a] payload(s)
  46. payload_grub="n"
  47. payload_grub_withseabios="n" # seabios chainloaded from grub
  48. payload_grub_withtianocore="n" # tianocore chainloaded from grub
  49. payload_seabios="n"
  50. payload_seabios_withgrub="n" # i386-coreboot grub accessible from SeaBIOS boot menu
  51. payload_tianocore="n"
  52. seabios_opromloadonly="0"
  53. payload_memtest="n"
  54. # Override the above defaults using board.cfg
  55. source "resources/coreboot/${board}/board.cfg"
  56. if [ "${cbtree}" = "undefined" ]; then
  57. printf "build/roms: Target %s does not define a coreboot tree. Skipping build.\n" "${board}"
  58. exit 1
  59. fi
  60. if [ "${arch}" = "undefined" ]; then
  61. printf "build/roms: Target %s does not define a CPU type. Skipping build.\n" "${board}"
  62. exit 1
  63. fi
  64. if [ "${seabios_opromloadonly}" != "0" ] && \
  65. [ "${seabios_opromloadonly}" != "1" ]; then
  66. seabios_opromloadonly="0"
  67. fi
  68. if [ "${payload_memtest}" != "n" ] && \
  69. [ "${payload_memtest}" != "y" ]; then
  70. payload_memtest="n"
  71. fi
  72. if [ "${payload_grub_withseabios}" = "y" ] \
  73. || [ "${payload_grub_withtianocore}" = "y" ]; then
  74. payload_grub="y"
  75. fi
  76. if [ "${payload_grub_withseabios}" = "y" ]; then
  77. payload_seabios="y"
  78. payload_seabios_withgrub="y" # if grub-first works, then seabios-with-grub will also work
  79. fi
  80. if [ "${payload_seabios_withgrub}" = "y" ]; then
  81. payload_seabios="y" # if seabios-with-grub works, then SeaBIOS-alone should also work
  82. fi
  83. # NOTE: reverse logic must not be applied. If SeaBIOS-with-GRUB works, that doesn't
  84. # necessarily mean GRUB-with-SeaBIOS will work nicely. for example, the board might
  85. # only have an add-on GPU available, where it's recommended to boot SeaBIOS first
  86. if [ "${payload_grub_withtianocore}" = "y" ]; then
  87. payload_tianocore="y"
  88. fi
  89. if [ "${payload_grub}" != "y" ] && [ "${payload_seabios}" != "y" ] \
  90. && [ "${payload_tianocore}" != "y" ]; then
  91. while true; do
  92. for configfile in "resources/coreboot/${board}/config/"*; do
  93. if [ -f "${configfile}" ]; then
  94. printf "ERROR build/roms: Target '%s' does not define a payload. Exiting.\n" "${board}"
  95. exit 1
  96. fi
  97. done
  98. break
  99. done
  100. fi
  101. if [ "${payload_memtest}" = "y" ]; then
  102. if [ ! -f "memtest86plus/memtest" ]; then
  103. ./build module memtest86plus
  104. fi
  105. fi
  106. romdir="bin/${board}"
  107. cbdir="coreboot/${board}"
  108. if [ "${board}" != "${cbtree}" ]; then
  109. cbdir="coreboot/${cbtree}"
  110. fi
  111. cbfstool="${cbdir}/util/cbfstool/cbfstool"
  112. corebootrom="${cbdir}/build/coreboot.rom"
  113. seavgabiosrom="payload/seabios/seavgabios.bin"
  114. tianocoreelf="payload/tianocore/tianocore.elf"
  115. if [ ! -d "${cbdir}" ]; then
  116. ./download coreboot ${cbtree}
  117. fi
  118. if [ "${arch}" = "x86_32" ] || [ "${arch}" = "x86_64" ]; then
  119. if [ ! -d "${cbdir}/util/crossgcc/xgcc/i386-elf/" ]; then
  120. (
  121. cat version > "${cbdir}/.coreboot-version"
  122. cd "${cbdir}"
  123. make crossgcc-i386 CPUS=$(nproc) # even for 64-bit machines, coreboot builds
  124. # 32-bit ROM images, so we only need to worry about i386-elf
  125. )
  126. fi
  127. fi
  128. if [ "${arch}" != "x86_64" ]; then
  129. payload_tianocore="n"
  130. payload_grub_withtianocore="n"
  131. fi
  132. if [ ! -f "${cbfstool}" ]; then
  133. ./build module cbutils ${cbtree}
  134. fi
  135. if [ ! -f "${tianocoreelf}" ]; then
  136. if [ "${payload_tianocore}" = "y" ]; then
  137. ./build payload tianocore
  138. elif [ "${payload_grub}" = "y" ] \
  139. && [ "${payload_grub_withtianocore}" = "y" ]; then
  140. ./build payload tianocore
  141. fi
  142. fi
  143. if [ ! -f "${seavgabiosrom}" ] \
  144. || [ ! -f payload/seabios/seabios_libgfxinit.elf ] \
  145. || [ ! -f payload/seabios/seabios_vgarom.elf ]; then
  146. if [ "${payload_seabios}" = "y" ]; then
  147. ./build payload seabios
  148. elif [ "${payload_grub}" = "y" ] \
  149. && [ "${payload_grub_withseabios}" = "y" ]; then
  150. ./build payload seabios
  151. fi
  152. fi
  153. [ -d "${romdir}/" ] || mkdir -p "${romdir}/"
  154. rm -f "${romdir}"/*
  155. if [ "${payload_grub}" = "y" ] || [ "${payload_seabios_withgrub}" = "y" ]; then
  156. if [ -f "payload/grub/grub_usqwerty.cfg" ]; then
  157. grubrefchecksum="$(sha1sum resources/grub/config/grub.cfg)"
  158. grubrefchecksum="${grubrefchecksum% resources/grub/config/grub.cfg}"
  159. grubbuildchecksum="$(sha1sum payload/grub/grub_usqwerty.cfg)"
  160. grubbuildchecksum="${grubbuildchecksum% payload/grub/grub_usqwerty.cfg}"
  161. if [ "${grubrefchecksum}" != "${grubbuildchecksum}" ]; then
  162. rm -Rf payload/grub/
  163. printf "Changes detected to GRUB. Re-building now:\n"
  164. fi
  165. else
  166. printf "Required GRUB payloads not yet built. Building now:\n"
  167. rm -Rf payload/grub/ # just in case
  168. fi
  169. for keymapfile in resources/grub/keymap/*; do
  170. if [ ! -f "${keymapfile}" ]; then
  171. continue
  172. fi
  173. keymap="${keymapfile##*/}"
  174. keymap="${keymap%.gkb}"
  175. grubelf="payload/grub/grub_${keymap}.elf"
  176. grubcfg="payload/grub/grub_${keymap}.cfg"
  177. grubtestcfg="payload/grub/grub_${keymap}_test.cfg"
  178. if [ ! -f "${grubelf}" ] || [ ! -f "${grubcfg}" ] || \
  179. [ ! -f "${grubtestcfg}" ]; then
  180. ./build payload grub
  181. fi
  182. done
  183. fi
  184. # it is assumed that no other work will be done on the ROM
  185. # after calling this function. therefore this function is "final"
  186. moverom() {
  187. rompath="$1"
  188. newrompath="$2"
  189. cuttype="$3"
  190. printf "\nCreating new ROM image: %s\n" "${newrompath}"
  191. if [ "${cuttype}" = "4MiB IFD BIOS region" ]; then
  192. dd if=${rompath} of=${newrompath} bs=1 skip=$[$(stat -c %s ${rompath}) - 0x400000] count=4194304
  193. else
  194. cp ${rompath} ${newrompath}
  195. fi
  196. for romsize in 4 8 16; do
  197. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOR flash" ]; then
  198. if [ ! -f "descriptors/ich9m/ich9fdgbe_${romsize}m.bin" ]; then
  199. ./build descriptors ich9m
  200. fi
  201. dd if=descriptors/ich9m/ich9fdgbe_${romsize}m.bin of=${newrompath} bs=1 count=12k conv=notrunc
  202. fi
  203. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOGBE NOR flash" ]; then
  204. if [ ! -f "descriptors/ich9m/ich9fdnogbe_${romsize}m.bin" ]; then
  205. ./build descriptors ich9m
  206. fi
  207. dd if=descriptors/ich9m/ich9fdnogbe_${romsize}m.bin of=${newrompath} bs=1 count=4k conv=notrunc
  208. fi
  209. done
  210. if [ "${cuttype}" = "i945 laptop" ]; then
  211. dd if=${newrompath} of=top64k.bin bs=1 skip=$[$(stat -c %s ${newrompath}) - 0x10000] count=64k
  212. dd if=top64k.bin of=${newrompath} bs=1 seek=$[$(stat -c %s ${newrompath}) - 0x20000] count=64k conv=notrunc
  213. rm -f top64k.bin
  214. fi
  215. }
  216. # expected: configs must not specify a payload
  217. mkCoreboot() {
  218. cbdir="${1}" # e.g. coreboot/default
  219. cbcfgpath="${2}" # e.g. resources/coreboot/x200_8mb/config/libgfxinit_txtmode
  220. if [ ! -f "${cbcfgpath}" ]; then
  221. printf "\nmkCoreboot: Coreboot config '%s' does not exist. Skipping build.\n" \
  222. "${cbcfgpath}"
  223. return 0
  224. fi
  225. printf "%s-%s\n" "$(cat projectname)" "$(cat version)" > "${cbdir}/.coreboot-version"
  226. (
  227. cd "${cbdir}"
  228. make distclean
  229. )
  230. cp "${cbcfgpath}" "${cbdir}"/.config
  231. (
  232. cd "${cbdir}"
  233. make -j$(nproc)
  234. )
  235. }
  236. mkRomWithTianocoreOnly() {
  237. rompath="${1}"
  238. initmode="${2}"
  239. if [ "${payload_tianocore}" = "y" ] && [ "${arch}" = "x86_64" ]; then
  240. # do not include on 32-bit-only machines. this is 64-bit tianocore
  241. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  242. cp "${corebootrom}" "${tmprom}"
  243. "${cbfstool}" "${tmprom}" add-payload -f ${tianocoreelf} -n fallback/payload -c lzma
  244. moverom "${tmprom}" "${romdir}/tianocore_${board}_${initmode}.rom" "${romtype}"
  245. rm -f "${tmprom}"
  246. fi
  247. }
  248. # make a rom in /tmp/ and then print the path of that ROM
  249. make_seabios_rom() {
  250. target_cbrom="${1}" # rom to insert seabios in. this rom won't be touched
  251. # a tmpfile will be made instead
  252. target_seabios_cbfs_path="${2}" # e.g. fallback/payload
  253. target_opromloadonly="${3}" # 0 or 1. if 1, only load but don't execute oproms
  254. target_initmode="${4}" # e.g. libgfxinit
  255. cbfstool_path="${5}"
  256. if [ "${target_initmode}" = "normal" ]; then
  257. target_seabioself="payload/seabios/seabios_vgarom.elf"
  258. # if normal, etc/pci-optionrom-exec will be set to 2
  259. else
  260. target_seabioself="payload/seabios/seabios_${target_initmode}.elf"
  261. # if libgfxinit, etc/pci-optionrom-exec will be set to 2
  262. # if vgarom, etc/pci-optionrom-exec will be set to 0
  263. fi
  264. target_seavgabios_rom="payload/seabios/seavgabios.bin"
  265. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  266. cp "${target_cbrom}" "${tmprom}"
  267. "${cbfstool}" "${tmprom}" add-payload -f "${target_seabioself}" -n ${target_seabios_cbfs_path} -c lzma
  268. "${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup
  269. if [ "${target_initmode}" = "normal" ] || [ "${target_initmode}" = "libgfxinit" ]; then
  270. "${cbfstool}" "${tmprom}" add-int -i 2 -n etc/pci-optionrom-exec
  271. elif [ "${target_initmode}" = "vgarom" ]; then
  272. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/pci-optionrom-exec
  273. fi # for undefined modes, don't add this integer. rely on SeaBIOS defaults
  274. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/optionroms-checksum
  275. "${cbfstool}" "${tmprom}" add-int -i ${target_opromloadonly} -n etc/only-load-option-roms
  276. if [ "${target_initmode}" = "libgfxinit" ]; then
  277. "${cbfstool_path}" "${tmprom}" add -f "${target_seavgabios_rom}" -n vgaroms/seavgabios.bin -t raw
  278. fi
  279. printf "%s\n" "${tmprom}"
  280. }
  281. # make a rom in /tmp/ and then print the path of that ROM
  282. make_grubrom_from_keymap() {
  283. target_keymap="${1}"
  284. target_cbrom="${2}"
  285. cbfstool_path="${3}"
  286. target_grubelf_cbfs_path="${4}" # e.g. fallback/payload
  287. grubelf="payload/grub/grub_${target_keymap}.elf"
  288. grubcfg="payload/grub/grub_${target_keymap}.cfg"
  289. grubtestcfg="payload/grub/grub_${target_keymap}_test.cfg"
  290. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  291. cp "${target_cbrom}" "${tmprom}"
  292. "${cbfstool_path}" "${tmprom}" add-payload -f "${grubelf}" -n ${target_grubelf_cbfs_path} -c lzma
  293. "${cbfstool_path}" "${tmprom}" add -f "${grubcfg}" -n grub.cfg -t raw
  294. "${cbfstool_path}" "${tmprom}" add -f "${grubtestcfg}" -n grubtest.cfg -t raw
  295. printf "%s\n" "${tmprom}"
  296. }
  297. # Make separate ROM images with GRUB payload, for each supported keymap
  298. mkRomsWithGrub() {
  299. tmprompath="${1}"
  300. initmode="${2}"
  301. displaymode="${3}"
  302. firstpayloadname="${4}" # allow values: grub, seabios, seabios_withgrub, seabios_grubfirst
  303. if [ "${payload_grub_withtianocore}" = "y" ] && [ "${firstpayloadname}" = "grub" ]; then
  304. "${cbfstool}" "${tmprompath}" add-payload -f ${tianocoreelf} -n tianocore.elf -c lzma
  305. fi
  306. if [ "${payload_grub_withseabios}" = "y" ] && [ "${firstpayloadname}" = "grub" ]; then
  307. mv "$(make_seabios_rom "${tmprompath}" "seabios.elf" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  308. elif [ "${payload_seabios_withgrub}" ] && [ "${firstpayloadname}" != "grub" ]; then
  309. mv "$(make_seabios_rom "${tmprompath}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  310. if [ "${firstpayloadname}" = "seabios_grubfirst" ]; then
  311. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  312. printf "/rom@img/grub2\n" > "${tmpbootorder}"
  313. "${cbfstool}" "${tmprompath}" add -f "${tmpbootorder}" -n bootorder -t raw
  314. rm -f "${tmpbootorder}"
  315. "${cbfstool}" "${tmprompath}" add-int -i 0 -n etc/show-boot-menu
  316. fi
  317. fi
  318. for keymapfile in resources/grub/keymap/*; do
  319. if [ ! -f "${keymapfile}" ]; then
  320. continue
  321. fi
  322. keymap="${keymapfile##*/}"
  323. keymap="${keymap%.gkb}"
  324. grub_path_in_cbfs="fallback/payload"
  325. if [ "${firstpayloadname}" != "grub" ]; then
  326. grub_path_in_cbfs="img/grub2"
  327. fi
  328. tmpgrubrom="$(make_grubrom_from_keymap "${keymap}" "${tmprompath}" "${cbfstool}" "${grub_path_in_cbfs}")"
  329. if [ "${initmode}" = "normal" ]; then
  330. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${keymap}.rom"
  331. else
  332. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${displaymode}_${keymap}.rom"
  333. fi
  334. moverom "${tmpgrubrom}" "${newrompath}" "${romtype}"
  335. rm -f "${tmpgrubrom}"
  336. done
  337. }
  338. # Main ROM building function. This calls all other functions
  339. mkRoms() {
  340. tianocoreRequiredDisplayMode="${1}"
  341. cbcfgpath="${2}"
  342. displaymode="${3}"
  343. initmode="${4}"
  344. if [ ! -f "${cbcfgpath}" ]; then
  345. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  346. "${cbcfgpath}" "${board}" "${displaymode}" "${initmode}"
  347. return 0
  348. fi
  349. mkCoreboot "${cbdir}" "${cbcfgpath}"
  350. if [ "${displaymode}" = "${tianocoreRequiredDisplayMode}" ]; then
  351. mkRomWithTianocoreOnly "${corebootrom}" "${initmode}"
  352. fi
  353. if [ "${displaymode}" = "txtmode" ] && [ "${payload_memtest}" = "y" ]; then
  354. "${cbfstool}" "${corebootrom}" add-payload -f memtest86plus/memtest -n img/memtest -c lzma
  355. fi
  356. if [ "${payload_seabios}" = "y" ]; then
  357. if [ "${payload_seabios_withgrub}" = "n" ]; then
  358. tmpseabiosrom="$(make_seabios_rom "${corebootrom}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")"
  359. if [ "${initmode}" = "normal" ]; then
  360. newrompath="${romdir}/seabios_${board}_${initmode}.rom"
  361. else
  362. newrompath="${romdir}/seabios_${board}_${initmode}_${displaymode}.rom"
  363. fi
  364. moverom "${tmpseabiosrom}" "${newrompath}" "${romtype}"
  365. rm -f "${tmpseabiosrom}"
  366. else
  367. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  368. cp "${corebootrom}" "${tmprom}"
  369. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_withgrub"
  370. cp "${corebootrom}" "${tmprom}"
  371. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_grubfirst"
  372. rm -f "${tmprom}"
  373. fi
  374. fi
  375. if [ "${payload_grub}" = "y" ]; then
  376. mkRomsWithGrub "${corebootrom}" "${initmode}" "${displaymode}" "grub"
  377. fi
  378. }
  379. initmode="libgfxinit"
  380. tianocoreRequiredDisplayMode="corebootfb"
  381. for displaymode in corebootfb txtmode; do
  382. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  383. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  384. done
  385. initmode="vgarom"
  386. tianocoreRequiredDisplayMode="vesafb"
  387. for displaymode in vesafb txtmode; do
  388. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  389. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  390. done
  391. initmode="normal"
  392. displaymode="txtmode"
  393. tianocoreRequiredDisplayMode="unsupported"
  394. cbcfgpath="resources/coreboot/${board}/config/${initmode}"
  395. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  396. (
  397. cd "${cbdir}"
  398. make distclean
  399. )