vendor.sh 14 KB

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