vendor.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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-2024 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. cv="CONFIG_HAVE_ME_BIN CONFIG_ME_BIN_PATH CONFIG_INCLUDE_SMSC_SCH5545_EC_FW \
  13. CONFIG_SMSC_SCH5545_EC_FW_FILE CONFIG_KBC1126_FIRMWARE CONFIG_KBC1126_FW1 \
  14. CONFIG_KBC1126_FW2 CONFIG_KBC1126_FW1_OFFSET CONFIG_KBC1126_FW2_OFFSET \
  15. CONFIG_VGA_BIOS_FILE CONFIG_VGA_BIOS_ID CONFIG_BOARD_DELL_E6400 \
  16. CONFIG_HAVE_MRC CONFIG_MRC_FILE CONFIG_HAVE_REFCODE_BLOB \
  17. CONFIG_REFCODE_BLOB_FILE CONFIG_GBE_BIN_PATH CONFIG_IFD_BIN_PATH"
  18. eval `setvars "" EC_url_bkup EC_hash DL_hash DL_url_bkup MRC_refcode_gbe vcfg \
  19. E6400_VGA_DL_hash E6400_VGA_DL_url E6400_VGA_DL_url_bkup E6400_VGA_offset \
  20. E6400_VGA_romname SCH5545EC_DL_url_bkup SCH5545EC_DL_hash _dest tree \
  21. mecleaner kbc1126_ec_dump MRC_refcode_cbtree new_mac _dl SCH5545EC_DL_url \
  22. archive EC_url boarddir rom cbdir DL_url nukemode cbfstoolref vrelease \
  23. verify _7ztest ME_bootguard IFD_platform ifdprefix $cv`
  24. vendor_download()
  25. {
  26. [ $# -gt 0 ] || $err "No argument given"; export PATH="$PATH:/sbin"
  27. board="$1"; readcfg && readkconfig && bootstrap && getfiles; :
  28. }
  29. readkconfig()
  30. {
  31. check_defconfig "$boarddir" 1>"$TMPDIR/vendorcfg.list" && return 1
  32. rm -f "$TMPDIR/tmpcbcfg" || $err "!rm -f \"$TMPDIR/tmpcbcfg\""
  33. while read -r cbcfgfile; do
  34. for cbc in $cv; do
  35. rm -f "$TMPDIR/tmpcbcfg2" || \
  36. $err "!rm $TMPDIR/tmpcbcfg2"
  37. grep "$cbc" "$cbcfgfile" 1>"$TMPDIR/tmpcbcfg2" \
  38. 2>/dev/null || :
  39. [ -f "$TMPDIR/tmpcbcfg2" ] || continue
  40. cat "$TMPDIR/tmpcbcfg2" >> "$TMPDIR/tmpcbcfg" || \
  41. $err "!cat $TMPDIR/tmpcbcfg2"
  42. done
  43. done < "$TMPDIR/vendorcfg.list"
  44. eval `setcfg "$TMPDIR/tmpcbcfg"`
  45. for c in CONFIG_HAVE_MRC CONFIG_HAVE_ME_BIN CONFIG_KBC1126_FIRMWARE \
  46. CONFIG_VGA_BIOS_FILE CONFIG_INCLUDE_SMSC_SCH5545_EC_FW; do
  47. eval "[ \"\${$c}\" = \"/dev/null\" ] && continue"
  48. eval "[ -z \"\${$c}\" ] && continue"
  49. eval `setcfg "config/vendor/$vcfg/pkg.cfg"`; return 0
  50. done
  51. printf "Vendor files not needed for: %s\n" "$board" 1>&2; return 1
  52. }
  53. bootstrap()
  54. {
  55. x_ ./mk -f coreboot ${cbdir##*/}
  56. mk -b uefitool biosutilities bios_extract
  57. [ -d "${kbc1126_ec_dump%/*}" ] && x_ make -C "$cbdir/util/kbc1126"
  58. [ -n "$MRC_refcode_cbtree" ] && \
  59. cbfstoolref="elf/cbfstool/$MRC_refcode_cbtree/cbfstool" && \
  60. x_ ./mk -d coreboot $MRC_refcode_cbtree; return 0
  61. }
  62. getfiles()
  63. {
  64. [ -z "$CONFIG_HAVE_ME_BIN" ] || fetch intel_me "$DL_url" \
  65. "$DL_url_bkup" "$DL_hash" "$CONFIG_ME_BIN_PATH"
  66. [ -z "$CONFIG_INCLUDE_SMSC_SCH5545_EC_FW" ] || fetch sch5545ec \
  67. "$SCH5545EC_DL_url" "$SCH5545EC_DL_url_bkup" "$SCH5545EC_DL_hash" \
  68. "$CONFIG_SMSC_SCH5545_EC_FW_FILE"
  69. [ -z "$CONFIG_KBC1126_FIRMWARE" ] || fetch kbc1126ec "$EC_url" \
  70. "$EC_url_bkup" "$EC_hash" "$CONFIG_KBC1126_FW1"
  71. [ -z "$CONFIG_VGA_BIOS_FILE" ] || fetch e6400vga "$E6400_VGA_DL_url" \
  72. "$E6400_VGA_DL_url_bkup" "$E6400_VGA_DL_hash" "$CONFIG_VGA_BIOS_FILE"
  73. [ -z "$CONFIG_HAVE_MRC" ] || fetch "mrc" "$MRC_url" "$MRC_url_bkup" \
  74. "$MRC_hash" "$CONFIG_MRC_FILE"; return 0
  75. }
  76. fetch()
  77. {
  78. dl_type="$1"; dl="$2"; dl_bkup="$3"; dlsum="$4"; _dest="${5##*../}"
  79. [ "$5" = "/dev/null" ] && return 0; _dl="$XBMK_CACHE/file/$dlsum"
  80. download "$dl" "$dl_bkup" "$_dl" "$dlsum"
  81. rm -Rf "${_dl}_extracted" || $err "!rm -Rf ${_ul}_extracted"
  82. e "$_dest" f && return 0
  83. mkdir -p "${_dest%/*}" || $err "mkdirs: !mkdir -p ${_dest%/*}"
  84. remkdir "$appdir"; extract_archive "$_dl" "$appdir" || \
  85. [ "$dl_type" = "e6400vga" ] || $err "mkd $_dest $dl_type: !extract"
  86. eval "extract_$dl_type"; set -u -e
  87. e "$_dest" f missing && $err "!extract_$dl_type"; :
  88. }
  89. extract_intel_me()
  90. {
  91. e "$mecleaner" f not && $err "$cbdir: me_cleaner missing"
  92. _me="$PWD/$_dest"; cdir="$PWD/$appdir"
  93. if [ "$ME_bootguard" = "me11disreguard" ]; then
  94. # run mkukri's util to extract me.bin and disable bootguard
  95. # for Dell OptiPlex 3050 Micro, using the deguard util.
  96. extract_deguard_me "$cdir" "$_me"
  97. return 0
  98. fi
  99. # All other ME setups are extracted with brute force and me_cleaner:
  100. [ $# -gt 0 ] && _me="${1}" && cdir="$2"
  101. e "$_me" f && return 0
  102. sdir="$(mktemp -d)"; [ -z "$sdir" ] && return 0
  103. mkdir -p "$sdir" || $err "extract_intel_me: !mkdir -p \"$sdir\""
  104. set +u +e
  105. (
  106. [ "${cdir#/a}" != "$cdir" ] && cdir="${cdir#/}"
  107. cd "$cdir" || $err "extract_intel_me: !cd \"$cdir\""
  108. for i in *; do
  109. [ -f "$_me" ] && break
  110. [ -L "$i" ] && continue
  111. if [ -f "$i" ]; then
  112. "$mecleaner" -r -t -O "$sdir/vendorfile" \
  113. -M "$_me" "$i" && break
  114. "$mecleaner" -r -t -O "$_me" "$i" && break
  115. "$me7updateparser" -O "$_me" "$i" && break
  116. _7ztest="${_7ztest}a"
  117. extract_archive "$i" "$_7ztest" || continue
  118. extract_intel_me "$_me" "$cdir/$_7ztest"
  119. elif [ -d "$i" ]; then
  120. extract_intel_me "$_me" "$cdir/$i"
  121. else
  122. continue
  123. fi
  124. cdir="$1"; [ "${cdir#/a}" != "$cdir" ] && cdir="${cdir#/}"
  125. cd "$cdir" || :
  126. done
  127. )
  128. rm -Rf "$sdir" || $err "extract_intel_me: !rm -Rf $sdir"
  129. }
  130. extract_deguard_me()
  131. {
  132. x_ ./mk -f deguard
  133. cp -R src/deguard "$1/disreguard" || \
  134. $err "Cannot make temporary deguard clone in $1/disreguard"
  135. if [ ! -e "$1/disreguard/.git" ]; then
  136. git -C "$1/disreguard" init || $err "!init $1/disreguard"
  137. git -C "$1/disreguard" add -A . || $err "!add $1/disreguard"
  138. git -C "$1/disreguard" commit -m "tmp" || \
  139. $err "!commit $1/disreguard"
  140. fi
  141. git -C "$1/disreguard" am "$PWD/config/data/deguard/appdir.patch" || \
  142. $err "Cannot temporarily patch deguard clone in $1/disreguard"
  143. (
  144. cd "$1/disreguard" || $err "Cannot cd to '$1/disreguard'"
  145. x_ ./RUNME.sh
  146. ) || $err "$1/disreguard: RUNME.sh returned error status"
  147. "$mecleaner" --whitelist MFS --truncate "$1/disreguard/me.bin" || \
  148. $err "extract_intel_me: Can't truncate disreguarded ME"
  149. cp "$cdir/disreguard/me.bin" "$2" || \
  150. $err "extract_intel_me: Can't move disreguarded me.bin"
  151. }
  152. extract_archive()
  153. {
  154. innoextract "$1" -d "$2" || python "$pfs_extract" "$1" -e || 7z x \
  155. "$1" -o"$2" || unar "$1" -o "$2" || unzip "$1" -d "$2" || return 1
  156. }
  157. extract_kbc1126ec()
  158. {
  159. e "$kbc1126_ec_dump" f missing && $err "$cbdir: kbc1126 util missing"
  160. (
  161. x_ cd "$appdir/"; mv Rompaq/68*.BIN ec.bin || :
  162. if [ ! -f "ec.bin" ]; then
  163. unar -D ROM.CAB Rom.bin || unar -D Rom.CAB Rom.bin || \
  164. unar -D 68*.CAB Rom.bin || $err "can't extract Rom.bin"
  165. x_ mv Rom.bin ec.bin
  166. fi
  167. [ -f ec.bin ] || $err "extract_kbc1126_ec $board: can't extract"
  168. "$kbc1126_ec_dump" ec.bin || $err "!1126ec $board extract ecfw"
  169. ) || $err "can't extract kbc1126 ec firmware"
  170. e "$appdir/ec.bin.fw1" f not && $err "$board: kbc1126ec fetch failed"
  171. e "$appdir/ec.bin.fw2" f not && $err "$board: kbc1126ec fetch failed"
  172. cp "$appdir/"ec.bin.fw* "${_dest%/*}/" || $err "!cp 1126ec $_dest"
  173. }
  174. extract_e6400vga()
  175. {
  176. set +u +e
  177. chkvars E6400_VGA_offset E6400_VGA_romname
  178. tail -c +$E6400_VGA_offset "$_dl" | gunzip > "$appdir/bios.bin" || :
  179. (
  180. x_ cd "$appdir"
  181. [ -f "bios.bin" ] || $err "extract_e6400vga: can't extract bios.bin"
  182. "$e6400_unpack" bios.bin || printf "TODO: fix dell extract util\n"
  183. ) || $err "can't extract e6400 vga rom"
  184. cp "$appdir/$E6400_VGA_romname" "$_dest" || \
  185. $err "extract_e6400vga $board: can't copy vga rom to $_dest"
  186. }
  187. extract_sch5545ec()
  188. {
  189. # full system ROM (UEFI), to extract with UEFIExtract:
  190. _bios="${_dl}_extracted/Firmware/1 $dlsum -- 1 System BIOS vA.28.bin"
  191. # this is the SCH5545 firmware, inside of the extracted UEFI ROM:
  192. _sch5545ec_fw="$_bios.dump/4 7A9354D9-0468-444A-81CE-0BF617D890DF"
  193. _sch5545ec_fw="$_sch5545ec_fw/54 D386BEB8-4B54-4E69-94F5-06091F67E0D3"
  194. _sch5545ec_fw="$_sch5545ec_fw/0 Raw section/body.bin" # <-- this!
  195. "$uefiextract" "$_bios" || $err "sch5545 !extract"
  196. cp "$_sch5545ec_fw" "$_dest" || $err "$_dest: !sch5545 copy"
  197. }
  198. vendor_inject()
  199. {
  200. set +u +e; [ $# -lt 1 ] && $err "No options specified."
  201. [ "$1" = "listboards" ] && eval "ls -1 config/coreboot || :; exit 0"
  202. archive="$1"; while getopts n:r:b:m: option; do
  203. case "$option" in
  204. n) nukemode="$OPTARG" ;;
  205. r) rom="$OPTARG" ;;
  206. b) board="$OPTARG" ;;
  207. m) new_mac="$OPTARG"; chkvars new_mac ;;
  208. *) : ;;
  209. esac
  210. done
  211. check_board || return 0
  212. [ "$nukemode" = "nuke" ] || x_ ./vendor download $board
  213. [ "$vrelease" != "y" ] && patch_rom "$rom"
  214. [ "$vrelease" = "y" ] && patch_release_roms; :
  215. }
  216. check_board()
  217. {
  218. failcheck="y" && check_release "$archive" && failcheck="n"
  219. if [ "$failcheck" = "y" ]; then
  220. [ -f "$rom" ] || $err "check_board \"$rom\": invalid path"
  221. [ -z "${rom+x}" ] && $err "check_board: no rom specified"
  222. [ -n "${board+x}" ] || board="$(detect_board "$rom")"
  223. else
  224. vrelease="y"; board="$(detect_board "$archive")"
  225. fi
  226. readcfg || return 1; return 0
  227. }
  228. check_release()
  229. {
  230. [ -f "$archive" ] || return 1
  231. [ "${archive##*.}" = "xz" ] || return 1
  232. printf "%s\n" "Release archive $archive detected"
  233. }
  234. # This function tries to determine the board from the filename of the rom.
  235. # It will only succeed if the filename is not changed from the build/download
  236. detect_board()
  237. {
  238. path="$1"; filename="$(basename "$path")"
  239. case "$filename" in
  240. grub_*|seagrub_*|custom_*)
  241. board="$(echo "$filename" | cut -d '_' -f2-3)" ;;
  242. seabios_withgrub_*)
  243. board="$(echo "$filename" | cut -d '_' -f3-4)" ;;
  244. *.tar.xz) _stripped_prefix="${filename#*_}"
  245. board="${_stripped_prefix%.tar.xz}" ;;
  246. *) $err "detect_board $filename: could not detect board type"
  247. esac; printf "%s\n" "$board"
  248. }
  249. readcfg()
  250. {
  251. if [ "$board" = "serprog_rp2040" ] || \
  252. [ "$board" = "serprog_stm32" ]; then
  253. return 1
  254. fi; boarddir="$cbcfgsdir/$board"
  255. eval `setcfg "$boarddir/target.cfg"`; chkvars vcfg tree
  256. cbdir="src/coreboot/$tree"
  257. cbfstool="elf/cbfstool/$tree/cbfstool"
  258. mecleaner="$PWD/$cbdir/util/me_cleaner/me_cleaner.py"
  259. kbc1126_ec_dump="$PWD/$cbdir/util/kbc1126/kbc1126_ec_dump"
  260. cbfstool="elf/cbfstool/$tree/cbfstool"
  261. ifdtool="elf/ifdtool/$tree/ifdtool"
  262. [ -n "$IFD_platform" ] && ifdprefix="-p $IFD_platform"
  263. x_ ./mk -d coreboot $tree
  264. }
  265. patch_release_roms()
  266. {
  267. remkdir "tmp/romdir"; tar -xf "$archive" -C "tmp/romdir" || \
  268. $err "patch_release_roms: !tar -xf \"$archive\" -C \"tmp/romdir\""
  269. for x in "tmp/romdir/bin/"*/*.rom ; do
  270. patch_rom "$x"
  271. done
  272. (
  273. cd "tmp/romdir/bin/"* || $err "patch roms: !cd tmp/romdir/bin/*"
  274. # NOTE: For compatibility with older rom releases, defer to sha1
  275. [ "$verify" != "y" ] || [ "$nukemode" = "nuke" ] || \
  276. sha512sum --status -c vendorhashes || \
  277. sha1sum --status -c vendorhashes || sha512sum --status -c \
  278. blobhashes || sha1sum --status -c blobhashes || \
  279. $err "patch_release_roms: ROMs did not match expected hashes"
  280. ) || $err "can't verify vendor hashes"
  281. [ -n "$new_mac" ] && for x in "tmp/romdir/bin/"*/*.rom ; do
  282. [ -f "$x" ] && modify_gbe "$x"
  283. done
  284. x_ mkdir -p bin/release
  285. mv tmp/romdir/bin/* bin/release/ || $err "$board: !mv release roms"
  286. }
  287. patch_rom()
  288. {
  289. rom="$1"
  290. readkconfig || exit 0
  291. [ "$CONFIG_HAVE_MRC" = "y" ] && inject "mrc.bin" "$CONFIG_MRC_FILE" \
  292. "mrc" "0xfffa0000"
  293. [ -n "$CONFIG_HAVE_REFCODE_BLOB" ] && inject "fallback/refcode" \
  294. "$CONFIG_REFCODE_BLOB_FILE" "stage"
  295. [ "$CONFIG_HAVE_ME_BIN" = "y" ] && inject IFD "$CONFIG_ME_BIN_PATH" me
  296. [ "$CONFIG_KBC1126_FIRMWARE" = "y" ] && inject ecfw1.bin \
  297. "$CONFIG_KBC1126_FW1" raw "$CONFIG_KBC1126_FW1_OFFSET" && inject \
  298. ecfw2.bin "$CONFIG_KBC1126_FW2" raw "$CONFIG_KBC1126_FW2_OFFSET"
  299. [ -n "$CONFIG_VGA_BIOS_FILE" ] && [ -n "$CONFIG_VGA_BIOS_ID" ] && \
  300. inject "pci$CONFIG_VGA_BIOS_ID.rom" "$CONFIG_VGA_BIOS_FILE" optionrom
  301. [ "$CONFIG_INCLUDE_SMSC_SCH5545_EC_FW" = "y" ] && \
  302. [ -n "$CONFIG_SMSC_SCH5545_EC_FW_FILE" ] && \
  303. inject sch5545_ecfw.bin "$CONFIG_SMSC_SCH5545_EC_FW_FILE" raw
  304. [ -n "$new_mac" ] && [ "$vrelease" != "y" ] && modify_gbe "$rom"
  305. printf "ROM image successfully patched: %s\n" "$rom"
  306. }
  307. inject()
  308. {
  309. [ $# -lt 3 ] && $err "$@, $rom: usage: inject name path type (offset)"
  310. [ "$2" = "/dev/null" ] && return 0; verify="y"
  311. eval `setvars "" cbfsname _dest _t _offset`
  312. cbfsname="$1"; _dest="${2##*../}"; _t="$3"
  313. [ $# -gt 3 ] && _offset="-b $4" && [ -z "$4" ] && \
  314. $err "inject $@, $rom: offset passed, but empty (not defined)"
  315. e "$_dest" f n && [ "$nukemode" != "nuke" ] && $err "!inject $dl_type"
  316. if [ "$cbfsname" = "IFD" ]; then
  317. [ "$nukemode" = "nuke" ] || "$ifdtool" $ifdprefix -i \
  318. $_t:$_dest "$rom" -O "$rom" || \
  319. $err "failed: inject '$_t' '$_dest' on '$rom'"
  320. [ "$nukemode" != "nuke" ] || "$ifdtool" $ifdprefix --nuke $_t \
  321. "$rom" -O "$rom" || $err "$rom: !nuke IFD/$_t"; return 0
  322. elif [ "$nukemode" = "nuke" ]; then
  323. "$cbfstool" "$rom" remove -n "$cbfsname" || \
  324. $err "inject $rom: can't remove $cbfsname"; return 0
  325. fi
  326. [ "$_t" != "stage" ] || "$cbfstool" "$rom" add-stage -f \
  327. "$_dest" -n "$cbfsname" -t stage -c lzma || $err "$rom: !add ref"
  328. [ "$_t" = "stage" ] || "$cbfstool" "$rom" add -f "$_dest" \
  329. -n "$cbfsname" -t $_t $_offset || $err "$rom !add $_t ($_dest)"; :
  330. }
  331. modify_gbe()
  332. {
  333. chkvars CONFIG_GBE_BIN_PATH
  334. e "${CONFIG_GBE_BIN_PATH##*../}" f n && $err "missing gbe file"
  335. x_ make -C util/nvmutil
  336. x_ cp "${CONFIG_GBE_BIN_PATH##*../}" "$TMPDIR/gbe"
  337. x_ "util/nvmutil/nvm" "$TMPDIR/gbe" setmac $new_mac
  338. "$ifdtool" $ifdprefix -i GbE:"$TMPDIR/gbe" "$1" -O "$1" || \
  339. $err "Cannot insert modified GbE region into target image."
  340. }