roms_helper 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #!/bin/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 [ ! -f "mrc/haswell/mrc.bin" ] && [ "${board}" = "t440p_12mb" ]; then
  116. ./download mrc
  117. if [ ! -f "mrc/haswell/mrc.bin" ]; then
  118. printf "t440p build: mrc.bin missing. Skipping build.\n"
  119. exit 1
  120. fi
  121. fi
  122. if [ ! -d "${cbdir}" ]; then
  123. ./download coreboot ${cbtree}
  124. fi
  125. if [ "${arch}" = "x86_32" ] || [ "${arch}" = "x86_64" ]; then
  126. if [ ! -d "${cbdir}/util/crossgcc/xgcc/i386-elf/" ]; then
  127. (
  128. cat version > "${cbdir}/.coreboot-version"
  129. cd "${cbdir}"
  130. make crossgcc-i386 CPUS=$(nproc) # even for 64-bit machines, coreboot builds
  131. # 32-bit ROM images, so we only need to worry about i386-elf
  132. )
  133. fi
  134. fi
  135. if [ "${arch}" != "x86_64" ]; then
  136. payload_tianocore="n"
  137. payload_grub_withtianocore="n"
  138. fi
  139. if [ ! -f "${cbfstool}" ]; then
  140. ./build module cbutils ${cbtree}
  141. fi
  142. if [ ! -f "${tianocoreelf}" ]; then
  143. if [ "${payload_tianocore}" = "y" ]; then
  144. ./build payload tianocore
  145. elif [ "${payload_grub}" = "y" ] \
  146. && [ "${payload_grub_withtianocore}" = "y" ]; then
  147. ./build payload tianocore
  148. fi
  149. fi
  150. if [ ! -f "${seavgabiosrom}" ] \
  151. || [ ! -f payload/seabios/seabios_libgfxinit.elf ] \
  152. || [ ! -f payload/seabios/seabios_vgarom.elf ]; then
  153. if [ "${payload_seabios}" = "y" ]; then
  154. ./build payload seabios
  155. elif [ "${payload_grub}" = "y" ] \
  156. && [ "${payload_grub_withseabios}" = "y" ]; then
  157. ./build payload seabios
  158. fi
  159. fi
  160. [ -d "${romdir}/" ] || mkdir -p "${romdir}/"
  161. rm -f "${romdir}"/*
  162. if [ "${payload_grub}" = "y" ] || [ "${payload_seabios_withgrub}" = "y" ]; then
  163. if [ -f "payload/grub/grub_usqwerty.cfg" ]; then
  164. grubrefchecksum="$(sha1sum resources/grub/config/grub.cfg)"
  165. grubrefchecksum="${grubrefchecksum% resources/grub/config/grub.cfg}"
  166. grubbuildchecksum="$(sha1sum payload/grub/grub_usqwerty.cfg)"
  167. grubbuildchecksum="${grubbuildchecksum% payload/grub/grub_usqwerty.cfg}"
  168. if [ "${grubrefchecksum}" != "${grubbuildchecksum}" ]; then
  169. rm -Rf payload/grub/
  170. printf "Changes detected to GRUB. Re-building now:\n"
  171. fi
  172. else
  173. printf "Required GRUB payloads not yet built. Building now:\n"
  174. rm -Rf payload/grub/ # just in case
  175. fi
  176. for keymapfile in resources/grub/keymap/*; do
  177. if [ ! -f "${keymapfile}" ]; then
  178. continue
  179. fi
  180. keymap="${keymapfile##*/}"
  181. keymap="${keymap%.gkb}"
  182. grubelf="payload/grub/grub_${keymap}.elf"
  183. grubcfg="payload/grub/grub_${keymap}.cfg"
  184. grubtestcfg="payload/grub/grub_${keymap}_test.cfg"
  185. if [ ! -f "${grubelf}" ] || [ ! -f "${grubcfg}" ] || \
  186. [ ! -f "${grubtestcfg}" ]; then
  187. ./build payload grub
  188. fi
  189. done
  190. fi
  191. # it is assumed that no other work will be done on the ROM
  192. # after calling this function. therefore this function is "final"
  193. moverom() {
  194. rompath="$1"
  195. newrompath="$2"
  196. cuttype="$3"
  197. printf "\nCreating new ROM image: %s\n" "${newrompath}"
  198. if [ "${cuttype}" = "4MiB IFD BIOS region" ]; then
  199. dd if=${rompath} of=${newrompath} bs=1 skip=$[$(stat -c %s ${rompath}) - 0x400000] count=4194304
  200. else
  201. cp ${rompath} ${newrompath}
  202. fi
  203. for romsize in 4 8 16; do
  204. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOR flash" ]; then
  205. if [ ! -f "descriptors/ich9m/ich9fdgbe_${romsize}m.bin" ]; then
  206. ./build descriptors ich9m
  207. fi
  208. dd if=descriptors/ich9m/ich9fdgbe_${romsize}m.bin of=${newrompath} bs=1 count=12k conv=notrunc
  209. fi
  210. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOGBE NOR flash" ]; then
  211. if [ ! -f "descriptors/ich9m/ich9fdnogbe_${romsize}m.bin" ]; then
  212. ./build descriptors ich9m
  213. fi
  214. dd if=descriptors/ich9m/ich9fdnogbe_${romsize}m.bin of=${newrompath} bs=1 count=4k conv=notrunc
  215. fi
  216. done
  217. if [ "${cuttype}" = "i945 laptop" ]; then
  218. dd if=${newrompath} of=top64k.bin bs=1 skip=$[$(stat -c %s ${newrompath}) - 0x10000] count=64k
  219. dd if=top64k.bin of=${newrompath} bs=1 seek=$[$(stat -c %s ${newrompath}) - 0x20000] count=64k conv=notrunc
  220. rm -f top64k.bin
  221. fi
  222. }
  223. # expected: configs must not specify a payload
  224. mkCoreboot() {
  225. cbdir="${1}" # e.g. coreboot/default
  226. cbcfgpath="${2}" # e.g. resources/coreboot/x200_8mb/config/libgfxinit_txtmode
  227. if [ ! -f "${cbcfgpath}" ]; then
  228. printf "\nmkCoreboot: Coreboot config '%s' does not exist. Skipping build.\n" \
  229. "${cbcfgpath}"
  230. return 0
  231. fi
  232. printf "%s-%s\n" "$(cat projectname)" "$(cat version)" > "${cbdir}/.coreboot-version"
  233. (
  234. cd "${cbdir}"
  235. make distclean
  236. )
  237. cp "${cbcfgpath}" "${cbdir}"/.config
  238. (
  239. cd "${cbdir}"
  240. make -j$(nproc)
  241. )
  242. }
  243. mkRomWithTianocoreOnly() {
  244. rompath="${1}"
  245. initmode="${2}"
  246. if [ "${payload_tianocore}" = "y" ] && [ "${arch}" = "x86_64" ]; then
  247. # do not include on 32-bit-only machines. this is 64-bit tianocore
  248. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  249. cp "${corebootrom}" "${tmprom}"
  250. "${cbfstool}" "${tmprom}" add-payload -f ${tianocoreelf} -n fallback/payload -c lzma
  251. moverom "${tmprom}" "${romdir}/tianocore_${board}_${initmode}.rom" "${romtype}"
  252. rm -f "${tmprom}"
  253. fi
  254. }
  255. # make a rom in /tmp/ and then print the path of that ROM
  256. make_seabios_rom() {
  257. target_cbrom="${1}" # rom to insert seabios in. this rom won't be touched
  258. # a tmpfile will be made instead
  259. target_seabios_cbfs_path="${2}" # e.g. fallback/payload
  260. target_opromloadonly="${3}" # 0 or 1. if 1, only load but don't execute oproms
  261. target_initmode="${4}" # e.g. libgfxinit
  262. cbfstool_path="${5}"
  263. if [ "${target_initmode}" = "normal" ]; then
  264. target_seabioself="payload/seabios/seabios_vgarom.elf"
  265. # if normal, etc/pci-optionrom-exec will be set to 2
  266. else
  267. target_seabioself="payload/seabios/seabios_${target_initmode}.elf"
  268. # if libgfxinit, etc/pci-optionrom-exec will be set to 2
  269. # if vgarom, etc/pci-optionrom-exec will be set to 0
  270. fi
  271. target_seavgabios_rom="payload/seabios/seavgabios.bin"
  272. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  273. cp "${target_cbrom}" "${tmprom}"
  274. "${cbfstool}" "${tmprom}" add-payload -f "${target_seabioself}" -n ${target_seabios_cbfs_path} -c lzma
  275. "${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup
  276. if [ "${target_initmode}" = "normal" ] || [ "${target_initmode}" = "libgfxinit" ]; then
  277. "${cbfstool}" "${tmprom}" add-int -i 2 -n etc/pci-optionrom-exec
  278. elif [ "${target_initmode}" = "vgarom" ]; then
  279. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/pci-optionrom-exec
  280. fi # for undefined modes, don't add this integer. rely on SeaBIOS defaults
  281. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/optionroms-checksum
  282. "${cbfstool}" "${tmprom}" add-int -i ${target_opromloadonly} -n etc/only-load-option-roms
  283. if [ "${target_initmode}" = "libgfxinit" ]; then
  284. "${cbfstool_path}" "${tmprom}" add -f "${target_seavgabios_rom}" -n vgaroms/seavgabios.bin -t raw
  285. fi
  286. printf "%s\n" "${tmprom}"
  287. }
  288. # make a rom in /tmp/ and then print the path of that ROM
  289. make_grubrom_from_keymap() {
  290. target_keymap="${1}"
  291. target_cbrom="${2}"
  292. cbfstool_path="${3}"
  293. target_grubelf_cbfs_path="${4}" # e.g. fallback/payload
  294. grubelf="payload/grub/grub_${target_keymap}.elf"
  295. grubcfg="payload/grub/grub_${target_keymap}.cfg"
  296. grubtestcfg="payload/grub/grub_${target_keymap}_test.cfg"
  297. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  298. cp "${target_cbrom}" "${tmprom}"
  299. "${cbfstool_path}" "${tmprom}" add-payload -f "${grubelf}" -n ${target_grubelf_cbfs_path} -c lzma
  300. "${cbfstool_path}" "${tmprom}" add -f "${grubcfg}" -n grub.cfg -t raw
  301. "${cbfstool_path}" "${tmprom}" add -f "${grubtestcfg}" -n grubtest.cfg -t raw
  302. printf "%s\n" "${tmprom}"
  303. }
  304. # Make separate ROM images with GRUB payload, for each supported keymap
  305. mkRomsWithGrub() {
  306. tmprompath="${1}"
  307. initmode="${2}"
  308. displaymode="${3}"
  309. firstpayloadname="${4}" # allow values: grub, seabios, seabios_withgrub, seabios_grubfirst
  310. if [ "${payload_grub_withtianocore}" = "y" ] && [ "${firstpayloadname}" = "grub" ]; then
  311. "${cbfstool}" "${tmprompath}" add-payload -f ${tianocoreelf} -n tianocore.elf -c lzma
  312. fi
  313. if [ "${payload_grub_withseabios}" = "y" ] && [ "${firstpayloadname}" = "grub" ]; then
  314. mv "$(make_seabios_rom "${tmprompath}" "seabios.elf" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  315. elif [ "${payload_seabios_withgrub}" ] && [ "${firstpayloadname}" != "grub" ]; then
  316. mv "$(make_seabios_rom "${tmprompath}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  317. if [ "${firstpayloadname}" = "seabios_grubfirst" ]; then
  318. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  319. printf "/rom@img/grub2\n" > "${tmpbootorder}"
  320. "${cbfstool}" "${tmprompath}" add -f "${tmpbootorder}" -n bootorder -t raw
  321. rm -f "${tmpbootorder}"
  322. "${cbfstool}" "${tmprompath}" add-int -i 0 -n etc/show-boot-menu
  323. fi
  324. fi
  325. for keymapfile in resources/grub/keymap/*; do
  326. if [ ! -f "${keymapfile}" ]; then
  327. continue
  328. fi
  329. keymap="${keymapfile##*/}"
  330. keymap="${keymap%.gkb}"
  331. grub_path_in_cbfs="fallback/payload"
  332. if [ "${firstpayloadname}" != "grub" ]; then
  333. grub_path_in_cbfs="img/grub2"
  334. fi
  335. tmpgrubrom="$(make_grubrom_from_keymap "${keymap}" "${tmprompath}" "${cbfstool}" "${grub_path_in_cbfs}")"
  336. if [ "${initmode}" = "normal" ]; then
  337. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${keymap}.rom"
  338. else
  339. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${displaymode}_${keymap}.rom"
  340. fi
  341. moverom "${tmpgrubrom}" "${newrompath}" "${romtype}"
  342. rm -f "${tmpgrubrom}"
  343. done
  344. }
  345. # Main ROM building function. This calls all other functions
  346. mkRoms() {
  347. tianocoreRequiredDisplayMode="${1}"
  348. cbcfgpath="${2}"
  349. displaymode="${3}"
  350. initmode="${4}"
  351. if [ ! -f "${cbcfgpath}" ]; then
  352. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  353. "${cbcfgpath}" "${board}" "${displaymode}" "${initmode}"
  354. return 0
  355. fi
  356. mkCoreboot "${cbdir}" "${cbcfgpath}"
  357. if [ "${displaymode}" = "${tianocoreRequiredDisplayMode}" ]; then
  358. mkRomWithTianocoreOnly "${corebootrom}" "${initmode}"
  359. fi
  360. if [ "${displaymode}" = "txtmode" ] && [ "${payload_memtest}" = "y" ]; then
  361. "${cbfstool}" "${corebootrom}" add-payload -f memtest86plus/memtest -n img/memtest -c lzma
  362. fi
  363. if [ "${payload_seabios}" = "y" ]; then
  364. if [ "${payload_seabios_withgrub}" = "n" ]; then
  365. tmpseabiosrom="$(make_seabios_rom "${corebootrom}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")"
  366. if [ "${initmode}" = "normal" ]; then
  367. newrompath="${romdir}/seabios_${board}_${initmode}.rom"
  368. else
  369. newrompath="${romdir}/seabios_${board}_${initmode}_${displaymode}.rom"
  370. fi
  371. moverom "${tmpseabiosrom}" "${newrompath}" "${romtype}"
  372. rm -f "${tmpseabiosrom}"
  373. else
  374. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  375. cp "${corebootrom}" "${tmprom}"
  376. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_withgrub"
  377. cp "${corebootrom}" "${tmprom}"
  378. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_grubfirst"
  379. rm -f "${tmprom}"
  380. fi
  381. fi
  382. if [ "${payload_grub}" = "y" ]; then
  383. mkRomsWithGrub "${corebootrom}" "${initmode}" "${displaymode}" "grub"
  384. fi
  385. }
  386. initmode="libgfxinit"
  387. tianocoreRequiredDisplayMode="corebootfb"
  388. for displaymode in corebootfb txtmode; do
  389. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  390. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  391. done
  392. initmode="vgarom"
  393. tianocoreRequiredDisplayMode="vesafb"
  394. for displaymode in vesafb txtmode; do
  395. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  396. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  397. done
  398. initmode="normal"
  399. displaymode="txtmode"
  400. tianocoreRequiredDisplayMode="unsupported"
  401. cbcfgpath="resources/coreboot/${board}/config/${initmode}"
  402. mkRoms "${tianocoreRequiredDisplayMode}" "${cbcfgpath}" "${displaymode}" "${initmode}"
  403. (
  404. cd "${cbdir}"
  405. make distclean
  406. )