vendor.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # SPDX-License-Identifier: GPL-3.0-only
  2. # Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # Copyright (c) 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. # Copyright (c) 2023-2025 Leah Rowe <leah@libreboot.org>
  5. e6400_unpack="$PWD/src/bios_extract/dell_inspiron_1100_unpacker.py"
  6. me7updateparser="$PWD/util/me7_update_parser/me7_update_parser.py"
  7. pfs_extract="$PWD/src/biosutilities/Dell_PFS_Extract.py"
  8. uefiextract="$PWD/elf/uefitool/uefiextract"
  9. vendir="vendorfiles"
  10. appdir="$vendir/app"
  11. cbcfgsdir="config/coreboot"
  12. hashfiles="vendorhashes blobhashes" # blobhashes for backwards compatibility
  13. dontflash="!!! AN ERROR OCCURED! Please DO NOT flash if injection failed. !!!"
  14. vfix="DO_NOT_FLASH_YET._FIRST,_INJECT_FILES_VIA_INSTRUCTIONS_ON_LIBREBOOT.ORG_"
  15. vguide="https://libreboot.org/docs/install/ivy_has_common.html"
  16. tmpromdel="$PWD/tmp/DO_NOT_FLASH"
  17. cv="CONFIG_HAVE_ME_BIN CONFIG_ME_BIN_PATH CONFIG_INCLUDE_SMSC_SCH5545_EC_FW \
  18. CONFIG_SMSC_SCH5545_EC_FW_FILE CONFIG_KBC1126_FIRMWARE CONFIG_KBC1126_FW1 \
  19. CONFIG_KBC1126_FW2 CONFIG_KBC1126_FW1_OFFSET CONFIG_KBC1126_FW2_OFFSET \
  20. CONFIG_VGA_BIOS_FILE CONFIG_VGA_BIOS_ID CONFIG_BOARD_DELL_E6400 \
  21. CONFIG_HAVE_MRC CONFIG_MRC_FILE CONFIG_HAVE_REFCODE_BLOB \
  22. CONFIG_REFCODE_BLOB_FILE CONFIG_GBE_BIN_PATH CONFIG_IFD_BIN_PATH \
  23. CONFIG_LENOVO_TBFW_BIN CONFIG_FSP_FD_PATH CONFIG_FSP_M_FILE \
  24. CONFIG_FSP_S_FILE CONFIG_FSP_S_CBFS CONFIG_FSP_M_CBFS CONFIG_FSP_USE_REPO \
  25. CONFIG_FSP_FULL_FD"
  26. eval "`setvars "" has_hashes EC_hash DL_hash DL_url_bkup MRC_refcode_gbe vcfg \
  27. E6400_VGA_DL_hash E6400_VGA_DL_url E6400_VGA_DL_url_bkup E6400_VGA_offset \
  28. E6400_VGA_romname SCH5545EC_DL_url_bkup SCH5545EC_DL_hash _dest tree \
  29. mecleaner kbc1126_ec_dump MRC_refcode_cbtree new_mac _dl SCH5545EC_DL_url \
  30. archive EC_url boarddir rom cbdir DL_url nukemode cbfstoolref FSPFD_hash \
  31. _7ztest ME11bootguard ME11delta ME11version ME11sku ME11pch tmpromdir \
  32. IFD_platform ifdprefix cdir sdir _me _metmp mfs TBFW_url_bkup TBFW_url \
  33. TBFW_hash TBFW_size hashfile xromsize xchanged EC_url_bkup need_files \
  34. vfile $cv`"
  35. vendor_download()
  36. {
  37. [ $# -gt 0 ] || $err "No argument given"; export PATH="$PATH:/sbin"
  38. board="$1"; readcfg && readkconfig && bootstrap && getfiles; :
  39. }
  40. readkconfig()
  41. {
  42. check_defconfig "$boarddir" 1>"$TMPDIR/vendorcfg.list" && return 1
  43. rm -f "$TMPDIR/tmpcbcfg" || $err "!rm $TMPDIR/tmpcbcfg - $dontflash"
  44. while read -r cbcfgfile; do
  45. for cbc in $cv; do
  46. rm -f "$TMPDIR/tmpcbcfg2" || \
  47. $err "!rm $TMPDIR/tmpcbcfg2 - $dontflash"
  48. grep "$cbc" "$cbcfgfile" 1>"$TMPDIR/tmpcbcfg2" \
  49. 2>/dev/null || :
  50. [ -f "$TMPDIR/tmpcbcfg2" ] || continue
  51. cat "$TMPDIR/tmpcbcfg2" >> "$TMPDIR/tmpcbcfg" || \
  52. $err "!cat $TMPDIR/tmpcbcfg2 - $dontflash"
  53. done
  54. done < "$TMPDIR/vendorcfg.list"
  55. eval "`setcfg "$TMPDIR/tmpcbcfg"`"
  56. for c in CONFIG_HAVE_MRC CONFIG_HAVE_ME_BIN CONFIG_KBC1126_FIRMWARE \
  57. CONFIG_VGA_BIOS_FILE CONFIG_INCLUDE_SMSC_SCH5545_EC_FW \
  58. CONFIG_LENOVO_TBFW_BIN CONFIG_FSP_M_FILE CONFIG_FSP_S_FILE; do
  59. eval "[ \"\${$c}\" = \"/dev/null\" ] && continue"
  60. eval "[ -z \"\${$c}\" ] && continue"
  61. eval "`setcfg "$vfile"`"; return 0
  62. done
  63. printf "Vendor files not needed for: %s\n" "$board" 1>&2; return 1
  64. }
  65. bootstrap()
  66. {
  67. x_ ./mk -f coreboot ${cbdir##*/}
  68. mk -b uefitool biosutilities bios_extract
  69. [ -d "${kbc1126_ec_dump%/*}" ] && x_ make -C "$cbdir/util/kbc1126"
  70. [ -n "$MRC_refcode_cbtree" ] && \
  71. cbfstoolref="elf/cbfstool/$MRC_refcode_cbtree/cbfstool" && \
  72. x_ ./mk -d coreboot "$MRC_refcode_cbtree"; return 0
  73. }
  74. getfiles()
  75. {
  76. [ -z "$CONFIG_HAVE_ME_BIN" ] || fetch intel_me "$DL_url" \
  77. "$DL_url_bkup" "$DL_hash" "$CONFIG_ME_BIN_PATH"
  78. [ -z "$CONFIG_INCLUDE_SMSC_SCH5545_EC_FW" ] || fetch sch5545ec \
  79. "$SCH5545EC_DL_url" "$SCH5545EC_DL_url_bkup" "$SCH5545EC_DL_hash" \
  80. "$CONFIG_SMSC_SCH5545_EC_FW_FILE"
  81. [ -z "$CONFIG_KBC1126_FIRMWARE" ] || fetch kbc1126ec "$EC_url" \
  82. "$EC_url_bkup" "$EC_hash" "$CONFIG_KBC1126_FW1"
  83. [ -z "$CONFIG_VGA_BIOS_FILE" ] || fetch e6400vga "$E6400_VGA_DL_url" \
  84. "$E6400_VGA_DL_url_bkup" "$E6400_VGA_DL_hash" "$CONFIG_VGA_BIOS_FILE"
  85. [ -z "$CONFIG_HAVE_MRC" ] || fetch "mrc" "$MRC_url" "$MRC_url_bkup" \
  86. "$MRC_hash" "$CONFIG_MRC_FILE"
  87. [ -z "$CONFIG_LENOVO_TBFW_BIN" ] || fetch "tbfw" "$TBFW_url" \
  88. "$TBFW_url_bkup" "$TBFW_hash" "$CONFIG_LENOVO_TBFW_BIN"
  89. #
  90. # in the future, we might have libre fsp-s and then fsp-m.
  91. # therefore, handle them separately, in case one of them is libre; if
  92. # one of them was, the path wouldn't be set.
  93. #
  94. [ -z "$CONFIG_FSP_M_FILE" ] || fetch "fspm" "$CONFIG_FSP_FD_PATH" \
  95. "$CONFIG_FSP_FD_PATH" "$FSPFD_hash" "$CONFIG_FSP_M_FILE" copy
  96. [ -z "$CONFIG_FSP_S_FILE" ] || fetch "fsps" "$CONFIG_FSP_FD_PATH" \
  97. "$CONFIG_FSP_FD_PATH" "$FSPFD_hash" "$CONFIG_FSP_S_FILE" copy; :
  98. }
  99. fetch()
  100. {
  101. dl_type="$1"; dl="$2"; dl_bkup="$3"; dlsum="$4"; _dest="${5##*../}"
  102. [ "$5" = "/dev/null" ] && return 0; _dl="$XBMK_CACHE/file/$dlsum"
  103. if [ "$dl_type" = "fspm" ] || [ "$dl_type" = "fsps" ]; then
  104. # HACK: if grabbing fsp from coreboot, fix the path for lbmk
  105. for _cdl in dl dl_bkup; do
  106. eval "$_cdl=\"\${$_cdl##*../}\"; _cdp=\"\$$_cdl\""
  107. [ -f "$_cdp" ] || _cdp="$cbdir/$_cdp"
  108. [ -f "$_cdp" ] && eval "$_cdl=\"$_cdp\""
  109. done
  110. fi
  111. dlop="curl" && [ $# -gt 5 ] && dlop="$6"
  112. download "$dl" "$dl_bkup" "$_dl" "$dlsum" "$dlop"
  113. rm -Rf "${_dl}_extracted" || $err "!rm ${_ul}_extracted. $dontflash"
  114. e "$_dest" f && return 0
  115. mkdir -p "${_dest%/*}" || \
  116. $err "mkdirs: !mkdir -p ${_dest%/*} - $dontflash"
  117. remkdir "$appdir"; extract_archive "$_dl" "$appdir" "$dl_type" || \
  118. [ "$dl_type" = "e6400vga" ] || \
  119. $err "mkd $_dest $dl_type: !extract. $dontflash"
  120. eval "extract_$dl_type"; set -u -e
  121. e "$_dest" f missing && $err "!extract_$dl_type. $dontflash"; :
  122. }
  123. extract_intel_me()
  124. {
  125. e "$mecleaner" f not && $err "$cbdir: me_cleaner missing. $dontflash"
  126. cdir="$PWD/$appdir"
  127. _me="$PWD/$_dest"
  128. _metmp="$PWD/tmp/me.bin"
  129. mfs="" && [ "$ME11bootguard" = "y" ] && mfs="--whitelist MFS" && \
  130. chkvars ME11delta ME11version ME11sku ME11pch
  131. [ "$ME11bootguard" = "y" ] && x_ ./mk -f deguard
  132. x_ mkdir -p tmp
  133. extract_intel_me_bruteforce
  134. if [ "$ME11bootguard" = "y" ]; then
  135. apply_me11_deguard_mod
  136. else
  137. mv "$_metmp" "$_me" || $err "!mv $_metmp $_me - $dontflash"
  138. fi
  139. }
  140. extract_intel_me_bruteforce()
  141. {
  142. [ $# -gt 0 ] && cdir="$1"
  143. e "$_metmp" f && return 0
  144. [ -z "$sdir" ] && sdir="$(mktemp -d)"
  145. mkdir -p "$sdir" || \
  146. $err "extract_intel_me: !mkdir -p \"$sdir\" - $dontflash"
  147. set +u +e
  148. (
  149. [ "${cdir#/a}" != "$cdir" ] && cdir="${cdir#/}"
  150. cd "$cdir" || $err "extract_intel_me: !cd \"$cdir\" - $dontflash"
  151. for i in *; do
  152. [ -f "$_metmp" ] && break
  153. [ -L "$i" ] && continue
  154. if [ -f "$i" ]; then
  155. _r="-r" && [ -n "$mfs" ] && _r=""
  156. "$mecleaner" $mfs $_r -t -O "$sdir/vendorfile" \
  157. -M "$_metmp" "$i" && break
  158. "$mecleaner" $mfs $_r -t -O "$_metmp" "$i" && break
  159. "$me7updateparser" -O "$_metmp" "$i" && break
  160. _7ztest="${_7ztest}a"
  161. extract_archive "$i" "$_7ztest" || continue
  162. extract_intel_me_bruteforce "$cdir/$_7ztest"
  163. elif [ -d "$i" ]; then
  164. extract_intel_me_bruteforce "$cdir/$i"
  165. else
  166. continue
  167. fi
  168. cdir="$1"; [ "${cdir#/a}" != "$cdir" ] && cdir="${cdir#/}"
  169. cd "$cdir" || :
  170. done
  171. )
  172. rm -Rf "$sdir" || $err "extract_intel_me: !rm -Rf $sdir - $dontflash"
  173. }
  174. apply_me11_deguard_mod()
  175. {
  176. (
  177. x_ cd src/deguard/
  178. ./finalimage.py --delta "data/delta/$ME11delta" \
  179. --version "$ME11version" \
  180. --pch "$ME11pch" --sku "$ME11sku" --fake-fpfs data/fpfs/zero \
  181. --input "$_metmp" --output "$_me" || \
  182. $err "Error running deguard for $_me - $dontflash"
  183. ) || $err "Error running deguard for $_me - $dontflash"
  184. }
  185. extract_archive()
  186. {
  187. if [ $# -gt 2 ]; then
  188. if [ "$3" = "fspm" ] || [ "$3" = "fsps" ]; then
  189. decat_fspfd "$1" "$2"
  190. return 0
  191. fi
  192. fi
  193. innoextract "$1" -d "$2" || python "$pfs_extract" "$1" -e || 7z x \
  194. "$1" -o"$2" || unar "$1" -o "$2" || unzip "$1" -d "$2" || return 1
  195. [ ! -d "${_dl}_extracted" ] || cp -R "${_dl}_extracted" "$2" || \
  196. $err "!mv '${_dl}_extracted' '$2' - $dontflash"; :
  197. }
  198. decat_fspfd()
  199. {
  200. _fspfd="$1"
  201. _fspdir="$2"
  202. _fspsplit="$cbdir/3rdparty/fsp/Tools/SplitFspBin.py"
  203. $python "$_fspsplit" split -f "$_fspfd" -o "$_fspdir" -n "Fsp.fd" || \
  204. $err "decat_fspfd '$1' '$2': Can't de-concatenate; $dontflash"; :
  205. }
  206. extract_kbc1126ec()
  207. {
  208. e "$kbc1126_ec_dump" f missing && \
  209. $err "$cbdir: kbc1126 util missing - $dontflash"
  210. (
  211. x_ cd "$appdir/"; mv Rompaq/68*.BIN ec.bin || :
  212. if [ ! -f "ec.bin" ]; then
  213. unar -D ROM.CAB Rom.bin || unar -D Rom.CAB Rom.bin || \
  214. unar -D 68*.CAB Rom.bin || \
  215. $err "can't extract Rom.bin - $dontflash"
  216. x_ mv Rom.bin ec.bin
  217. fi
  218. [ -f ec.bin ] || \
  219. $err "extract_kbc1126_ec $board: can't extract - $dontflash"
  220. "$kbc1126_ec_dump" ec.bin || \
  221. $err "!1126ec $board extract ecfw - $dontflash"
  222. ) || $err "can't extract kbc1126 ec firmware - $dontflash"
  223. e "$appdir/ec.bin.fw1" f not && \
  224. $err "$board: kbc1126ec fetch failed - $dontflash"
  225. e "$appdir/ec.bin.fw2" f not && \
  226. $err "$board: kbc1126ec fetch failed - $dontflash"
  227. cp "$appdir/"ec.bin.fw* "${_dest%/*}/" || \
  228. $err "!cp 1126ec $_dest - $dontflash"; :
  229. }
  230. extract_e6400vga()
  231. {
  232. set +u +e
  233. chkvars E6400_VGA_offset E6400_VGA_romname
  234. tail -c +$E6400_VGA_offset "$_dl" | gunzip > "$appdir/bios.bin" || :
  235. (
  236. x_ cd "$appdir"
  237. [ -f "bios.bin" ] || \
  238. $err "extract_e6400vga: can't extract bios.bin - $dontflash"
  239. "$e6400_unpack" bios.bin || printf "TODO: fix dell extract util\n"
  240. ) || $err "can't extract e6400 vga rom - $dontflosh"
  241. cp "$appdir/$E6400_VGA_romname" "$_dest" || \
  242. $err "extract_e6400vga $board: can't cp $_dest - $dontflash"; :
  243. }
  244. extract_sch5545ec()
  245. {
  246. # full system ROM (UEFI), to extract with UEFIExtract:
  247. _bios="${_dl}_extracted/Firmware/1 $dlsum -- 1 System BIOS vA.28.bin"
  248. # this is the SCH5545 firmware, inside of the extracted UEFI ROM:
  249. _sch5545ec_fw="$_bios.dump/4 7A9354D9-0468-444A-81CE-0BF617D890DF"
  250. _sch5545ec_fw="$_sch5545ec_fw/54 D386BEB8-4B54-4E69-94F5-06091F67E0D3"
  251. _sch5545ec_fw="$_sch5545ec_fw/0 Raw section/body.bin" # <-- this!
  252. "$uefiextract" "$_bios" || $err "sch5545 !extract - $dontflash"
  253. cp "$_sch5545ec_fw" "$_dest" || \
  254. $err "$_dest: !sch5545 copy - $dontflash"; :
  255. }
  256. # Lenovo ThunderBolt firmware updates:
  257. # https://pcsupport.lenovo.com/us/en/products/laptops-and-netbooks/thinkpad-t-series-laptops/thinkpad-t480-type-20l5-20l6/20l5/solutions/ht508988
  258. extract_tbfw()
  259. {
  260. chkvars TBFW_size # size in bytes, matching TBFW's flash IC
  261. x_ mkdir -p tmp
  262. x_ rm -f tmp/tb.bin
  263. find "$appdir" -type f -name "TBT.bin" > "tmp/tb.txt" || \
  264. $err "extract_tbfw $_dest: Can't extract TBT.bin - $dontflash"
  265. while read -r f; do
  266. [ -f "$f" ] || continue
  267. [ -L "$f" ] && continue
  268. cp "$f" "tmp/tb.bin" || \
  269. $err "extract_tbfw $_dest: Can't copy TBT.bin - $dontflash"
  270. break
  271. done < "tmp/tb.txt"
  272. dd if=/dev/null of=tmp/tb.bin bs=1 seek=$TBFW_size || \
  273. $err "extract_tbfw $_dest: Can't pad TBT.bin - $dontflash"
  274. cp "tmp/tb.bin" "$_dest" || \
  275. $err "extract_tbfw $_dest: copy error - $dontflash "; :
  276. }
  277. extract_fspm()
  278. {
  279. copy_fsp M; :
  280. }
  281. extract_fsps()
  282. {
  283. copy_fsp S; :
  284. }
  285. # this copies the fsp s/m; re-base is handled by ./mk inject
  286. copy_fsp()
  287. {
  288. cp "$appdir/Fsp_$1.fd" "$_dest" || \
  289. $err "copy_fsp: Can't copy $1 to $_dest - $dontflash"; :
  290. }
  291. fail_inject()
  292. {
  293. [ -L "$tmpromdel" ] || [ ! -d "$tmpromdel" ] || \
  294. rm -Rf "$tmpromdel" || :
  295. printf "\n\n%s\n\n" "$dontflash" 1>&2
  296. printf "WARNING: File '%s' was NOT modified.\n\n" "$archive" 1>&2
  297. printf "Please MAKE SURE vendor files are inserted before flashing\n\n"
  298. fail "$1"
  299. }
  300. vendor_inject()
  301. {
  302. need_files="n" # will be set to "y" if vendorfiles needed
  303. _olderr="$err"
  304. err="fail_inject"
  305. remkdir "$tmpromdel"
  306. set +u +e; [ $# -lt 1 ] && $err "No options specified. - $dontflash"
  307. eval "`setvars "" nukemode new_mac xchanged`"
  308. # randomise the MAC address by default
  309. # TODO: support setting CBFS MAC address for GA-G41M-ES2L
  310. new_mac="??:??:??:??:??:??"
  311. archive="$1";
  312. [ $# -gt 1 ] && case "$2" in
  313. nuke)
  314. new_mac=""
  315. nukemode="nuke" ;;
  316. setmac)
  317. [ $# -gt 2 ] && new_mac="$3" && \
  318. [ -z "$new_mac" ] && $err \
  319. "You set an empty MAC address string" ;;
  320. *) $err "Unrecognised inject mode: '$2'"
  321. esac
  322. # allow the user to skip setting MAC addresses.
  323. # if new_mac is empty, this script skips running nvmutil
  324. [ "$new_mac" = "keep" ] && new_mac=""
  325. # we don't allow the *user* to clear new_mac, in the setmac
  326. # command, in case the build system is being integrated with
  327. # another, where setmac is relied upon and is being set
  328. # explicitly. this is a preventative error handle, as a courtes
  329. # to that hypothetical user e.g. Linux distro package maintainer
  330. # integrating this build system into their distro. if they used
  331. # a variable for that, and they forgot to initialise it, they'll know.
  332. check_release "$archive" || \
  333. $err "You must run this script on a release archive. - $dontflash"
  334. [ "$new_mac" = "restore" ] && \
  335. printf "Restoring default GbE for '$archive', board '$board'\n"
  336. readcfg && need_files="y"
  337. if [ "$need_files" = "y" ] || [ -n "$new_mac" ]; then
  338. [ "$nukemode" = "nuke" ] || x_ ./mk download "$board"
  339. patch_release_roms
  340. fi
  341. [ "$need_files" != "y" ] && printf \
  342. "\nTarball '%s' (board '%s) doesn't need vendorfiles.\n" \
  343. "$archive" "$board" 1>&2
  344. xtype="patched" && [ "$nukemode" = "nuke" ] && xtype="nuked"
  345. [ "$xchanged" != "y" ] && \
  346. printf "\nRelease archive '%s' was *NOT* modified.\n" \
  347. "$archive" && [ "$has_hashes" = "y" ] && \
  348. printf "WARNING: '%s' contains '%s'. DO NOT FLASH!\n" \
  349. "$archive" "$hashfile" 1>&2 && \
  350. printf "(vendorfiles may be needed and aren't there)\n" \
  351. 1>&2
  352. [ "$xchanged" = "y" ] && \
  353. printf "\nRelease archive '%s' successfully %s.\n" \
  354. "$archive" "$xtype" && [ "$nukemode" != "nuke" ] && \
  355. printf "You may now extract '%s' and flash images from it.\n" \
  356. "$archive"
  357. [ "$xchanged" = "y" ] && [ "$nukemode" = "nuke" ] && \
  358. printf "WARNING! Vendorfiles *removed*. DO NOT FLASH.\n" 1>&2 \
  359. && printf "DO NOT flash images from '%s'\n" \
  360. "$archive" 1>&2
  361. [ "$need_files" = "n" ] && printf \
  362. "Board '%s' doesn't use vendorfiles, so none were inserted.\n" \
  363. "$board"
  364. #
  365. # catch-all error handler, for libreboot release opsec:
  366. #
  367. # if vendor files defined, and a hash file was missing, that means
  368. # a nuke must succeed, if specified. if no hashfile was present,
  369. # that means vendorfiles had been injected, so a nuke must succeed.
  370. # this check is here in case of future bugs in lbmk's handling
  371. # of vendorfile deletions on release archives, which absolutely
  372. # must always be 100% reliable, so paranoia is paramount:
  373. #
  374. if [ "$xchanged" != "y" ] && [ "$need_files" = "y" ] && \
  375. [ "$nukemode" = "nuke" ] && [ "$has_hashes" != "y" ]; then
  376. printf "FAILED NUKE: tarball '$archive', board '$board'\n" 1>&2
  377. $err "Unhandled vendorfile deletion: DO NOT RELEASE TO RSYNC"
  378. fi # of course, we assume that those variables are also set right
  379. err="$_olderr"
  380. return 0
  381. }
  382. check_release()
  383. {
  384. [ -L "$archive" ] && \
  385. $err "'$archive' is a symlink, not a file - $dontflash"
  386. [ -f "$archive" ] || return 1
  387. archivename="`basename "$archive"`"
  388. [ -z "$archivename" ] && \
  389. $err "Cannot determine archive file name - $dontflash"
  390. case "$archivename" in
  391. *_src.tar.xz)
  392. $err "'$archive' is a src archive, silly!" ;;
  393. grub_*|seagrub_*|custom_*|seauboot_*|seabios_withgrub_*)
  394. return 1 ;;
  395. *.tar.xz) _stripped_prefix="${archivename#*_}"
  396. board="${_stripped_prefix%.tar.xz}" ;;
  397. *) $err "'$archive': could not detect board type - $dontflash"
  398. esac; :
  399. }
  400. readcfg()
  401. {
  402. if [ "$board" = "serprog_rp2040" ] || \
  403. [ "$board" = "serprog_stm32" ] || \
  404. [ "$board" = "serprog_pico" ]; then
  405. return 1
  406. fi
  407. boarddir="$cbcfgsdir/$board"
  408. eval "`setcfg "$boarddir/target.cfg"`"
  409. chkvars tree
  410. x_ ./mk -d coreboot "$tree" # even if vendorfiles not used, see: setmac
  411. [ -z "$vcfg" ] && return 1
  412. vfile="config/vendor/$vcfg/pkg.cfg"
  413. [ -L "$vfile" ] && $err "'$archive', '$board': $vfile is a symlink"
  414. [ -f "$vfile" ] || $err "'$archive', '$board': $vfile doesn't exist"
  415. cbdir="src/coreboot/$tree"
  416. cbfstool="elf/cbfstool/$tree/cbfstool"
  417. rmodtool="elf/cbfstool/$tree/rmodtool"
  418. mecleaner="$PWD/$cbdir/util/me_cleaner/me_cleaner.py"
  419. kbc1126_ec_dump="$PWD/$cbdir/util/kbc1126/kbc1126_ec_dump"
  420. cbfstool="elf/cbfstool/$tree/cbfstool"
  421. ifdtool="elf/ifdtool/$tree/ifdtool"
  422. [ -n "$IFD_platform" ] && ifdprefix="-p $IFD_platform"; :
  423. }
  424. patch_release_roms()
  425. {
  426. has_hashes="n"
  427. tmpromdir="tmp/DO_NOT_FLASH/bin/$board"
  428. remkdir "${tmpromdir%"/bin/$board"}"
  429. tar -xf "$archive" -C "${tmpromdir%"/bin/$board"}" || \
  430. $err "Can't extract '$archive'"
  431. for _hashes in $hashfiles; do
  432. [ -L "$tmpromdir/$_hashes" ] && \
  433. $err "'$archive' -> the hashfile is a symlink. $dontflash"
  434. [ -f "$tmpromdir/$_hashes" ] && has_hashes="y" && \
  435. hashfile="$_hashes" && break; :
  436. done
  437. x_ mkdir -p "tmp"; [ -L "tmp/rom.list" ] && \
  438. $err "'$archive' -> tmp/rom.list is a symlink - $dontflash"
  439. x_ rm -f "tmp/rom.list" "tmp/zero.1b"
  440. x_ dd if=/dev/zero of=tmp/zero.1b bs=1 count=1
  441. find "$tmpromdir" -maxdepth 1 -type f -name "*.rom" > "tmp/rom.list" \
  442. || $err "'$archive' -> Can't make tmp/rom.list - $dontflash"
  443. if readkconfig; then
  444. while read -r _xrom ; do
  445. process_release_rom "$_xrom" || break
  446. done < "tmp/rom.list"
  447. rm -f "$tmpromdir/README.md" || :
  448. [ "$nukemode" != "nuke" ] || \
  449. printf "Make sure you inserted vendor files: %s\n" \
  450. "$vguide" > "$tmpromdir/README.md" || :
  451. else
  452. printf "Skipping vendorfiles on '%s'\n" "$archive" 1>&2
  453. fi
  454. (
  455. cd "$tmpromdir" || $err "patch '$archive': can't cd $tmpromdir"
  456. # NOTE: For compatibility with older rom releases, defer to sha1
  457. if [ "$has_hashes" = "y" ] && [ "$nukemode" != "nuke" ]; then
  458. sha512sum --status -c "$hashfile" || \
  459. sha1sum --status -c "$hashfile" || \
  460. $err "'$archive' -> Can't verify vendor hashes. $dontflash"
  461. rm -f "$hashfile" || \
  462. $err "$archive: Can't rm hashfile. $dontflash"
  463. fi
  464. ) || $err "'$archive' -> Can't verify vendor hashes. $dontflash"
  465. if [ -n "$new_mac" ]; then
  466. if ! modify_mac_addresses; then
  467. printf "\nNo GbE region defined for '%s'\n" "$board" \
  468. 1>&2
  469. printf "Therefore, changing the MAC is impossible.\n" \
  470. 1>&2
  471. printf "This board probably lacks Intel ethernet.\n" \
  472. 1>&2
  473. printf "(or it's pre-IFD Intel with Intel GbE NIC)\n" \
  474. 1>&2
  475. fi
  476. fi
  477. [ "$xchanged" = "y" ] || rm -Rf "$tmpromdel" || :
  478. [ "$xchanged" = "y" ] || return 0
  479. (
  480. cd "${tmpromdir%"/bin/$board"}" || \
  481. $err "Can't cd '${tmpromdir%"/bin/$board"}'; $dontflash"
  482. # ../../ is the root of lbmk
  483. mkrom_tarball "bin/$board"
  484. ) || $err "Cannot re-generate '$archive' - $dontflash"
  485. mv "${tmpromdir%"/bin/$board"}/bin/${relname}_${board}.tar.xz" \
  486. "$archive" || \
  487. $err "'$archive' -> Cannot overwrite - $dontflash"; :
  488. }
  489. process_release_rom()
  490. {
  491. _xrom="$1"; _xromname="${1##*/}"
  492. [ -L "$_xrom" ] && \
  493. $err "$archive -> '${_xrom#"tmp/DO_NOT_FLASH/"}' is a symlink"
  494. [ -f "$_xrom" ] || return 0
  495. [ -z "${_xromname#"$vfix"}" ] && \
  496. $err "'$_xromname'->'"${_xromname#"$vfix"}"' empty. $dontflash"
  497. # Remove the prefix and 1-byte pad
  498. if [ "$nukemode" != "nuke" ] && \
  499. [ "${_xromname#"$vfix"}" != "$_xromname" ]; then
  500. _xromnew="${_xrom%/*}/${_xromname#"$vfix"}"
  501. # Remove the 1-byte padding
  502. stat -c '%s' "$_xrom" > "tmp/rom.size" || \
  503. $err "$_xrom: Can't get rom size. $dontflash"
  504. read -r xromsize < "tmp/rom.size" || \
  505. $err "$_xrom: Can't read rom size. $dontflash"
  506. expr "X$xromsize" : "X-\{0,1\}[0123456789][0123456789]*$" \
  507. 1>/dev/null 2>/dev/null || $err "$_xrom size non-integer"
  508. [ $xromsize -lt 2 ] && $err \
  509. "$_xrom: Will not create empty file. $dontflash"
  510. # TODO: check whether the size would be a multiple of 64KB
  511. # the smallest rom images we do are 512kb
  512. xromsize="`expr $xromsize - 1`"
  513. [ $xromsize -lt 524288 ] && \
  514. $err "$_xrom size too small; likely not a rom. $dontflash"
  515. dd if="$_xrom" of="$_xromnew" bs=$xromsize count=1 || \
  516. $err "$_xrom: Can't resize. $dontflash"
  517. rm -f "$_xrom" || $err "Can't rm $_xrom - $dontflash"
  518. _xrom="$_xromnew"
  519. fi
  520. [ "$nukemode" = "nuke" ] && \
  521. mksha512sum "$_xrom" "vendorhashes"
  522. patch_rom "$_xrom" || return 1 # if break return, can still change MAC
  523. [ "$nukemode" != "nuke" ] && return 0
  524. # Rename the file, prefixing a warning saying not to flash
  525. # the target image, which now has vendor files removed. Also
  526. # pad it so that flashprog returns an error if the user tries
  527. # to flash it, due to mismatching ROM size vs chip size
  528. cat "$_xrom" tmp/zero.1b > "${_xrom%/*}/$vfix${_xrom##*/}" || \
  529. $err "'$archive' -> can't pad/rename '$_xrom'. $dontflash"
  530. rm -f "$_xrom" || $err "'$archive' -> can't rm '$_xrom'. $dontflash"
  531. }
  532. patch_rom()
  533. {
  534. rom="$1"
  535. # regarding ifs below:
  536. # if a hash file exists, we only want to allow inject.
  537. # if a hash file is missing, we only want to allow nuke.
  538. # this logical rule prevents double-nuke and double-inject
  539. # if injecting without a hash file i.e. inject what was injected
  540. # (or inject where no vendor files are needed, covered previously)
  541. if [ "$has_hashes" != "y" ] && [ "$nukemode" != "nuke" ]; then
  542. printf "inject: '%s' has no hash file. Skipping.\n" \
  543. "$archive" 1>&2
  544. return 1
  545. fi
  546. # nuking *with* a hash file, i.e. nuking what was nuked before
  547. if [ "$has_hashes" = "y" ] && [ "$nukemode" = "nuke" ]; then
  548. printf "inject nuke: '%s' has a hash file. Skipping nuke.\n" \
  549. "$archive" 1>&2
  550. return 1
  551. fi
  552. [ -n "$CONFIG_HAVE_REFCODE_BLOB" ] && inject "fallback/refcode" \
  553. "$CONFIG_REFCODE_BLOB_FILE" "stage"
  554. [ "$CONFIG_HAVE_MRC" = "y" ] && inject "mrc.bin" "$CONFIG_MRC_FILE" \
  555. "mrc" "0xfffa0000"
  556. [ "$CONFIG_HAVE_ME_BIN" = "y" ] && inject IFD "$CONFIG_ME_BIN_PATH" me
  557. [ "$CONFIG_KBC1126_FIRMWARE" = "y" ] && inject ecfw1.bin \
  558. "$CONFIG_KBC1126_FW1" raw "$CONFIG_KBC1126_FW1_OFFSET" && inject \
  559. ecfw2.bin "$CONFIG_KBC1126_FW2" raw "$CONFIG_KBC1126_FW2_OFFSET"
  560. [ -n "$CONFIG_VGA_BIOS_FILE" ] && [ -n "$CONFIG_VGA_BIOS_ID" ] && \
  561. inject "pci$CONFIG_VGA_BIOS_ID.rom" "$CONFIG_VGA_BIOS_FILE" optionrom
  562. [ "$CONFIG_INCLUDE_SMSC_SCH5545_EC_FW" = "y" ] && \
  563. [ -n "$CONFIG_SMSC_SCH5545_EC_FW_FILE" ] && \
  564. inject sch5545_ecfw.bin "$CONFIG_SMSC_SCH5545_EC_FW_FILE" raw
  565. #
  566. # coreboot adds FSP-M first. so we shall add it first, then S:
  567. # NOTE:
  568. # We skip the fetch if CONFIG_FSP_USE_REPO or CONFIG_FSP_FULL_FD is set
  569. # but only for inject/nuke. we still run fetch (see above) because on
  570. # _fsp targets, coreboot still needs them, but coreboot Kconfig uses
  571. # makefile syntax and puts $(obj) in the path, which makes no sense
  572. # in sh. So we modify the path there, but lbmk only uses the file
  573. # in vendorfiles/ if neither CONFIG_FSP_USE_REPO nor CONFIG_FSP_FULL_FD
  574. # are set
  575. #
  576. [ -z "$CONFIG_FSP_USE_REPO" ] && [ -z "$CONFIG_FSP_FULL_FD" ] && \
  577. [ -n "$CONFIG_FSP_M_FILE" ] && \
  578. inject "$CONFIG_FSP_M_CBFS" "$CONFIG_FSP_M_FILE" fsp --xip
  579. [ -z "$CONFIG_FSP_USE_REPO" ] && [ -z "$CONFIG_FSP_FULL_FD" ] && \
  580. [ -n "$CONFIG_FSP_S_FILE" ] && \
  581. inject "$CONFIG_FSP_S_CBFS" "$CONFIG_FSP_S_FILE" fsp
  582. # TODO: modify gbe *after checksum verification only*
  583. # TODO: insert default gbe if doing -n nuke
  584. printf "ROM image successfully patched: %s\n" "$rom"
  585. xchanged="y"
  586. }
  587. inject()
  588. {
  589. [ $# -lt 3 ] && $err "$*, $rom: usage: inject name path type (offset)"
  590. [ "$2" = "/dev/null" ] && return 0
  591. eval "`setvars "" cbfsname _dest _t _offset`"
  592. cbfsname="$1"; _dest="${2##*../}"; _t="$3"
  593. if [ "$_t" = "fsp" ]; then
  594. [ $# -gt 3 ] && _offset="$4"
  595. else
  596. [ $# -gt 3 ] && _offset="-b $4" && [ -z "$4" ] && \
  597. $err "inject $*, $rom: offset given but empty (undefined)"
  598. fi
  599. e "$_dest" f n && [ "$nukemode" != "nuke" ] && $err "!inject $dl_type"
  600. if [ "$cbfsname" = "IFD" ]; then
  601. [ "$nukemode" = "nuke" ] || "$ifdtool" $ifdprefix -i \
  602. $_t:$_dest "$rom" -O "$rom" || \
  603. $err "failed: inject '$_t' '$_dest' on '$rom'"
  604. [ "$nukemode" != "nuke" ] || "$ifdtool" $ifdprefix --nuke $_t \
  605. "$rom" -O "$rom" || $err "$rom: !nuke IFD/$_t"
  606. xchanged="y"
  607. return 0
  608. elif [ "$nukemode" = "nuke" ]; then
  609. "$cbfstool" "$rom" remove -n "$cbfsname" || \
  610. $err "inject $rom: can't remove $cbfsname"
  611. xchanged="y"
  612. return 0
  613. fi
  614. if [ "$_t" = "stage" ]; then # the only stage we handle is refcode
  615. x_ mkdir -p tmp; x_ rm -f "tmp/refcode"
  616. "$rmodtool" -i "$_dest" -o "tmp/refcode" || "!reloc refcode"
  617. "$cbfstool" "$rom" add-stage -f "tmp/refcode" -n "$cbfsname" \
  618. -t stage || $err "$rom: !add ref"
  619. else
  620. "$cbfstool" "$rom" add -f "$_dest" -n "$cbfsname" \
  621. -t $_t $_offset || $err "$rom !add $_t ($_dest)"
  622. fi; xchanged="y"; :
  623. }
  624. modify_mac_addresses()
  625. {
  626. [ "$nukemode" = "nuke" ] && \
  627. $err "Cannot modify MAC addresses while nuking vendor files"
  628. # chkvars CONFIG_GBE_BIN_PATH
  629. [ -n "$CONFIG_GBE_BIN_PATH" ] || return 1
  630. e "${CONFIG_GBE_BIN_PATH##*../}" f n && $err "missing gbe file"
  631. [ "$new_mac" != "restore" ] && \
  632. x_ make -C util/nvmutil
  633. x_ mkdir -p tmp
  634. [ -L "tmp/gbe" ] && $err "tmp/gbe exists but is a symlink"
  635. [ -d "tmp/gbe" ] && $err "tmp/gbe exists but is a directory"
  636. if [ -e "tmp/gbe" ]; then
  637. [ -f "tmp/gbe" ] || $err "tmp/gbe exists and is not a file"
  638. fi
  639. x_ cp "${CONFIG_GBE_BIN_PATH##*../}" "tmp/gbe"
  640. [ "$new_mac" != "restore" ] && \
  641. x_ "util/nvmutil/nvm" "tmp/gbe" setmac "$new_mac"
  642. find "$tmpromdir" -maxdepth 1 -type f -name "*.rom" > "tmp/rom.list" \
  643. || $err "'$archive' -> Can't make tmp/rom.list - $dontflash"
  644. while read -r _xrom; do
  645. [ -L "$_xrom" ] && continue
  646. [ -f "$_xrom" ] || continue
  647. "$ifdtool" $ifdprefix -i GbE:"tmp/gbe" "$_xrom" -O \
  648. "$_xrom" || $err "'$_xrom': Can't insert new GbE file"
  649. xchanged="y"
  650. done < "tmp/rom.list"
  651. printf "\nThe following GbE NVM words were written in '%s':\n" \
  652. "$archive"
  653. x_ util/nvmutil/nvm tmp/gbe dump
  654. [ "$new_mac" = "restore" ] && \
  655. printf "\nNOTE: User specified setmac 'restore' argument.\n" && \
  656. printf "Default GbE file '%s' written without running nvmutil.\n" \
  657. "${CONFIG_GBE_BIN_PATH##*../}"; :
  658. }