roms_helper 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. # Copyright (C) 2021 Vitali64 <vitali64pmemail@protonmail.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # This script assumes that the working directory is the root
  21. # of git or release archive
  22. [ "x${DEBUG+set}" = 'xset' ] && set -v
  23. set -u -e
  24. projectname="$(cat projectname)"
  25. if (( $# != 1 )); then
  26. printf "Usage: ./build boot roms boardname\n"
  27. printf "Example: ./build boot roms x60\n"
  28. printf "Example: ./build boot roms x60 x200_8mb\n"
  29. printf "Example: ./build boot roms all\n"
  30. printf "You need to specify exactly 1 argument\n"
  31. exit 1
  32. fi
  33. board="${1}"
  34. if [ ! -d "resources/coreboot/${board}" ]; then
  35. printf "build/roms: Target %s does not exist in the %s build system. Skipping build.\n" "${projectname}" "${board}"
  36. exit 1
  37. fi
  38. if [ ! -f "resources/coreboot/${board}/board.cfg" ]; then
  39. printf "build/roms: Target %s does not have a board.cfg. Skipping build.\n" "${board}"
  40. exit 1
  41. fi
  42. # Workaround to grub's slow boot
  43. grub_scan_disk="undefined" # both: scan ata and ahci (slow), there is ata and ahci too
  44. # as an option
  45. cbtree="undefined"
  46. romtype="normal" # optional parameter in board.cfg. "normal" is default
  47. arch="undefined"
  48. # Disable all payloads by default.
  49. # board.cfg files have to specifically enable [a] payload(s)
  50. payload_grub="n"
  51. payload_grub_withseabios="n" # seabios chainloaded from grub
  52. payload_seabios="n"
  53. payload_seabios_withgrub="n" # i386-coreboot grub accessible from SeaBIOS boot menu
  54. seabios_opromloadonly="0"
  55. payload_memtest="n"
  56. # Override the above defaults using board.cfg
  57. source "resources/coreboot/${board}/board.cfg"
  58. if [ "${grub_scan_disk}" = "undefined" ]; then
  59. printf "build/roms: Target %s does not define grub_scan_disk. Defaulting to 'both'.\n" "${board}"
  60. grub_scan_disk="both"
  61. fi
  62. if [ "${grub_scan_disk}" != "both" ] && \
  63. [ "${grub_scan_disk}" != "ata" ] && \
  64. [ "${grub_scan_disk}" != "ahci" ]; then
  65. printf "build/roms: Target %s defines an invalid grub_scan_disk setting. Defaulting to 'both'.\n" "${board}"
  66. grub_scan_disk="both"
  67. # erroring out would be silly. just use the default
  68. fi
  69. if [ "${cbtree}" = "undefined" ]; then
  70. printf "build/roms: Target %s does not define a coreboot tree. Skipping build.\n" "${board}"
  71. exit 1
  72. fi
  73. if [ "${arch}" = "undefined" ]; then
  74. printf "build/roms: Target %s does not define a CPU type. Skipping build.\n" "${board}"
  75. exit 1
  76. fi
  77. if [ "${seabios_opromloadonly}" != "0" ] && \
  78. [ "${seabios_opromloadonly}" != "1" ]; then
  79. seabios_opromloadonly="0"
  80. fi
  81. if [ "${payload_memtest}" != "n" ] && \
  82. [ "${payload_memtest}" != "y" ]; then
  83. payload_memtest="n"
  84. fi
  85. if [ "${payload_grub_withseabios}" = "y" ]; then
  86. payload_grub="y"
  87. fi
  88. if [ "${payload_grub_withseabios}" = "y" ]; then
  89. payload_seabios="y"
  90. payload_seabios_withgrub="y" # if grub-first works, then seabios-with-grub will also work
  91. fi
  92. if [ "${payload_seabios_withgrub}" = "y" ]; then
  93. payload_seabios="y" # if seabios-with-grub works, then SeaBIOS-alone should also work
  94. fi
  95. # NOTE: reverse logic must not be applied. If SeaBIOS-with-GRUB works, that doesn't
  96. # necessarily mean GRUB-with-SeaBIOS will work nicely. for example, the board might
  97. # only have an add-on GPU available, where it's recommended to boot SeaBIOS first
  98. if [ "${payload_grub}" != "y" ] && [ "${payload_seabios}" != "y" ]; then
  99. while true; do
  100. for configfile in "resources/coreboot/${board}/config/"*; do
  101. if [ -f "${configfile}" ]; then
  102. printf "ERROR build/roms: Target '%s' does not define a payload. Exiting.\n" "${board}"
  103. exit 1
  104. fi
  105. done
  106. break
  107. done
  108. fi
  109. if [ "${payload_memtest}" = "y" ]; then
  110. if [ ! -f "memtest86plus/memtest" ]; then
  111. ./build module memtest86plus
  112. fi
  113. fi
  114. romdir="bin/${board}"
  115. cbdir="coreboot/${board}"
  116. if [ "${board}" != "${cbtree}" ]; then
  117. cbdir="coreboot/${cbtree}"
  118. fi
  119. cbfstool="${cbdir}/util/cbfstool/cbfstool"
  120. corebootrom="${cbdir}/build/coreboot.rom"
  121. seavgabiosrom="payload/seabios/seavgabios.bin"
  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. elif [ "${arch}" = "ARMv7" ]; then
  135. (
  136. cat version > "${cbdir}/.coreboot-version"
  137. cd "${cbdir}"
  138. make crossgcc-arm CPUS=$(nproc) # This is for armv7, doesn't apply to aarch64
  139. )
  140. elif [ "${arch}" = "AArch64" ]; then
  141. (
  142. cat version > "${cbdir}/.coreboot-version"
  143. cd "${cbdir}"
  144. make crossgcc-aarch64 CPUS=$(nproc) # This is for aarch64, doesn't apply to armv7
  145. )
  146. fi
  147. if [ ! -f "${cbfstool}" ]; then
  148. ./build module cbutils ${cbtree}
  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. # pike2008 cards cause a system hang when loading the option rom in seabios
  204. # if there is an empty option rom in cbfs, no option rom will be loaded
  205. if [ "${cuttype}" = "d8d16sas" ]; then
  206. emptyrom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  207. rm -f "${emptyrom}"
  208. touch "${emptyrom}"
  209. for deviceID in "0072" "3050"; do
  210. "${cbfstool}" "${newrompath}" add -f "${emptyrom}" -n pci1000,${deviceID}.rom -t raw
  211. done
  212. rm -f "${emptyrom}"
  213. fi
  214. for romsize in 4 8 16; do
  215. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOR flash" ]; then
  216. if [ ! -f "descriptors/ich9m/ich9fdgbe_${romsize}m.bin" ]; then
  217. ./build descriptors ich9m
  218. fi
  219. dd if=descriptors/ich9m/ich9fdgbe_${romsize}m.bin of=${newrompath} bs=1 count=12k conv=notrunc
  220. fi
  221. if [ "${cuttype}" = "${romsize}MiB ICH9 IFD NOGBE NOR flash" ]; then
  222. if [ ! -f "descriptors/ich9m/ich9fdnogbe_${romsize}m.bin" ]; then
  223. ./build descriptors ich9m
  224. fi
  225. dd if=descriptors/ich9m/ich9fdnogbe_${romsize}m.bin of=${newrompath} bs=1 count=4k conv=notrunc
  226. fi
  227. done
  228. if [ "${cuttype}" = "i945 laptop" ]; then
  229. dd if=${newrompath} of=top64k.bin bs=1 skip=$[$(stat -c %s ${newrompath}) - 0x10000] count=64k
  230. dd if=top64k.bin of=${newrompath} bs=1 seek=$[$(stat -c %s ${newrompath}) - 0x20000] count=64k conv=notrunc
  231. rm -f top64k.bin
  232. fi
  233. }
  234. # expected: configs must not specify a payload
  235. mkCoreboot() {
  236. cbdir="${1}" # e.g. coreboot/default
  237. cbcfgpath="${2}" # e.g. resources/coreboot/x200_8mb/config/libgfxinit_txtmode
  238. if [ ! -f "${cbcfgpath}" ]; then
  239. printf "\nmkCoreboot: Coreboot config '%s' does not exist. Skipping build.\n" \
  240. "${cbcfgpath}"
  241. return 0
  242. fi
  243. printf "%s-%s\n" "$(cat projectname)" "$(cat version)" > "${cbdir}/.coreboot-version"
  244. (
  245. cd "${cbdir}"
  246. make distclean
  247. )
  248. cp "${cbcfgpath}" "${cbdir}"/.config
  249. (
  250. cd "${cbdir}"
  251. make -j$(nproc)
  252. )
  253. }
  254. # make a rom in /tmp/ and then print the path of that ROM
  255. make_seabios_rom() {
  256. target_cbrom="${1}" # rom to insert seabios in. this rom won't be touched
  257. # a tmpfile will be made instead
  258. target_seabios_cbfs_path="${2}" # e.g. fallback/payload
  259. target_opromloadonly="${3}" # 0 or 1. if 1, only load but don't execute oproms
  260. target_initmode="${4}" # e.g. libgfxinit
  261. cbfstool_path="${5}"
  262. if [ "${target_initmode}" = "normal" ]; then
  263. target_seabioself="payload/seabios/seabios_vgarom.elf"
  264. # if normal, etc/pci-optionrom-exec will be set to 2
  265. else
  266. target_seabioself="payload/seabios/seabios_${target_initmode}.elf"
  267. # if libgfxinit, etc/pci-optionrom-exec will be set to 2
  268. # if vgarom, etc/pci-optionrom-exec will be set to 0
  269. fi
  270. target_seavgabios_rom="payload/seabios/seavgabios.bin"
  271. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  272. cp "${target_cbrom}" "${tmprom}"
  273. "${cbfstool}" "${tmprom}" add-payload -f "${target_seabioself}" -n ${target_seabios_cbfs_path} -c lzma
  274. "${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup
  275. if [ "${target_initmode}" = "normal" ] || [ "${target_initmode}" = "libgfxinit" ]; then
  276. "${cbfstool}" "${tmprom}" add-int -i 2 -n etc/pci-optionrom-exec
  277. elif [ "${target_initmode}" = "vgarom" ]; then
  278. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/pci-optionrom-exec
  279. fi # for undefined modes, don't add this integer. rely on SeaBIOS defaults
  280. "${cbfstool}" "${tmprom}" add-int -i 0 -n etc/optionroms-checksum
  281. "${cbfstool}" "${tmprom}" add-int -i ${target_opromloadonly} -n etc/only-load-option-roms
  282. if [ "${target_initmode}" = "libgfxinit" ]; then
  283. "${cbfstool_path}" "${tmprom}" add -f "${target_seavgabios_rom}" -n vgaroms/seavgabios.bin -t raw
  284. fi
  285. printf "%s\n" "${tmprom}"
  286. }
  287. # make a rom in /tmp/ and then print the path of that ROM
  288. make_grubrom_from_keymap() {
  289. target_keymap="${1}"
  290. target_cbrom="${2}"
  291. cbfstool_path="${3}"
  292. target_grubelf_cbfs_path="${4}" # e.g. fallback/payload
  293. grubelf="payload/grub/grub_${target_keymap}.elf"
  294. grubcfg="payload/grub/grub_${target_keymap}.cfg"
  295. grubtestcfg="payload/grub/grub_${target_keymap}_test.cfg"
  296. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  297. cp "${target_cbrom}" "${tmprom}"
  298. "${cbfstool_path}" "${tmprom}" add-payload -f "${grubelf}" -n ${target_grubelf_cbfs_path} -c lzma
  299. if [ "${grub_scan_disk}" = "ahci" ]; then
  300. sed 's/search_grub ata//' "${grubcfg}" > "${grubcfg}"
  301. elif [ "${grub_scan_disk}" = "ata" ]; then
  302. sed 's/search_grub ahci//' "${grubcfg}" > "${grubcfg}"
  303. fi
  304. "${cbfstool_path}" "${tmprom}" add -f "${grubcfg}" -n grub.cfg -t raw
  305. "${cbfstool_path}" "${tmprom}" add -f "${grubtestcfg}" -n grubtest.cfg -t raw
  306. backgroundfile="background1280x800.png"
  307. if [ "${board}" = "x60" ] || [ "${board}" = "t60_intelgpu" ]; then
  308. # TODO: don't hardcode this check. do it in board.cfg per board
  309. backgroundfile="background1024x768.png"
  310. fi
  311. backgroundfile="resources/grub/background/${backgroundfile}"
  312. "${cbfstool_path}" "${tmprom}" add -f ${backgroundfile} -n background.png -t raw
  313. printf "%s\n" "${tmprom}"
  314. }
  315. # Make separate ROM images with GRUB payload, for each supported keymap
  316. mkRomsWithGrub() {
  317. tmprompath="${1}"
  318. initmode="${2}"
  319. displaymode="${3}"
  320. firstpayloadname="${4}" # allow values: grub, seabios, seabios_withgrub, seabios_grubfirst
  321. if [ "${payload_grub_withseabios}" = "y" ] && [ "${firstpayloadname}" = "grub" ]; then
  322. mv "$(make_seabios_rom "${tmprompath}" "seabios.elf" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  323. elif [ "${payload_seabios_withgrub}" ] && [ "${firstpayloadname}" != "grub" ]; then
  324. mv "$(make_seabios_rom "${tmprompath}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")" "${tmprompath}"
  325. if [ "${firstpayloadname}" = "seabios_grubfirst" ]; then
  326. tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  327. printf "/rom@img/grub2\n" > "${tmpbootorder}"
  328. "${cbfstool}" "${tmprompath}" add -f "${tmpbootorder}" -n bootorder -t raw
  329. rm -f "${tmpbootorder}"
  330. "${cbfstool}" "${tmprompath}" add-int -i 0 -n etc/show-boot-menu
  331. fi
  332. fi
  333. for keymapfile in resources/grub/keymap/*; do
  334. if [ ! -f "${keymapfile}" ]; then
  335. continue
  336. fi
  337. keymap="${keymapfile##*/}"
  338. keymap="${keymap%.gkb}"
  339. grub_path_in_cbfs="fallback/payload"
  340. if [ "${firstpayloadname}" != "grub" ]; then
  341. grub_path_in_cbfs="img/grub2"
  342. fi
  343. tmpgrubrom="$(make_grubrom_from_keymap "${keymap}" "${tmprompath}" "${cbfstool}" "${grub_path_in_cbfs}")"
  344. if [ "${initmode}" = "normal" ]; then
  345. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${keymap}.rom"
  346. else
  347. newrompath="${romdir}/${firstpayloadname}_${board}_${initmode}_${displaymode}_${keymap}.rom"
  348. fi
  349. moverom "${tmpgrubrom}" "${newrompath}" "${romtype}"
  350. rm -f "${tmpgrubrom}"
  351. done
  352. }
  353. # Main ROM building function. This calls all other functions
  354. mkRoms() {
  355. cbcfgpath="${1}"
  356. displaymode="${2}"
  357. initmode="${3}"
  358. if [ ! -f "${cbcfgpath}" ]; then
  359. printf "'%s' does not exist. Skipping build for %s %s %s\n" \
  360. "${cbcfgpath}" "${board}" "${displaymode}" "${initmode}"
  361. return 0
  362. fi
  363. mkCoreboot "${cbdir}" "${cbcfgpath}"
  364. if [ "${displaymode}" = "txtmode" ] && [ "${payload_memtest}" = "y" ]; then
  365. "${cbfstool}" "${corebootrom}" add-payload -f memtest86plus/memtest -n img/memtest -c lzma
  366. fi
  367. if [ "${payload_seabios}" = "y" ]; then
  368. if [ "${payload_seabios_withgrub}" = "n" ]; then
  369. tmpseabiosrom="$(make_seabios_rom "${corebootrom}" "fallback/payload" "${seabios_opromloadonly}" "${initmode}" "${cbfstool}")"
  370. if [ "${initmode}" = "normal" ]; then
  371. newrompath="${romdir}/seabios_${board}_${initmode}.rom"
  372. else
  373. newrompath="${romdir}/seabios_${board}_${initmode}_${displaymode}.rom"
  374. fi
  375. moverom "${tmpseabiosrom}" "${newrompath}" "${romtype}"
  376. rm -f "${tmpseabiosrom}"
  377. else
  378. tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
  379. cp "${corebootrom}" "${tmprom}"
  380. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_withgrub"
  381. cp "${corebootrom}" "${tmprom}"
  382. mkRomsWithGrub "${tmprom}" "${initmode}" "${displaymode}" "seabios_grubfirst"
  383. rm -f "${tmprom}"
  384. fi
  385. fi
  386. if [ "${payload_grub}" = "y" ]; then
  387. mkRomsWithGrub "${corebootrom}" "${initmode}" "${displaymode}" "grub"
  388. fi
  389. }
  390. initmode="libgfxinit"
  391. for displaymode in corebootfb txtmode; do
  392. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  393. mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}"
  394. done
  395. initmode="vgarom"
  396. for displaymode in vesafb txtmode; do
  397. cbcfgpath="resources/coreboot/${board}/config/${initmode}_${displaymode}"
  398. mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}"
  399. done
  400. initmode="normal"
  401. displaymode="txtmode"
  402. cbcfgpath="resources/coreboot/${board}/config/${initmode}"
  403. mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}"
  404. (
  405. cd "${cbdir}"
  406. make distclean
  407. )