vendor.sh 12 KB

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