inject 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-only
  3. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  4. # SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  5. # SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
  6. . "include/err.sh"
  7. . "include/option.sh"
  8. nvmutil="util/nvmutil/nvm"
  9. eval "$(setvars "" archive rom modifygbe nukemode release new_mac)"
  10. main()
  11. {
  12. [ $# -lt 1 ] && err "No options specified."
  13. if [ "${1}" = "listboards" ]; then
  14. items config/coreboot || :
  15. exit 0
  16. fi
  17. archive="${1}"
  18. while getopts n:r:b:m: option; do
  19. case "${option}" in
  20. n) nukemode="${OPTARG}" ;;
  21. r) rom=${OPTARG} ;;
  22. b) board=${OPTARG} ;;
  23. m) modifygbe=true
  24. new_mac=${OPTARG} ;;
  25. *) : ;;
  26. esac
  27. done
  28. check_board
  29. build_dependencies
  30. inject_vendorfiles
  31. [ "${nukemode}" = "nuke" ] && return 0
  32. printf "Friendly reminder (this is *not* an error message):\n"
  33. printf "Please always ensure that the files were inserted correctly.\n"
  34. }
  35. check_board()
  36. {
  37. failcheck="n"
  38. check_release "${archive}" || failcheck="y"
  39. if [ "${failcheck}" = "y" ]; then
  40. [ -f "${rom}" ] || \
  41. err "check_board: \"${rom}\" is not a valid path"
  42. [ -z "${rom+x}" ] && \
  43. err "check_board: no rom specified"
  44. [ -n "${board+x}" ] || \
  45. board=$(detect_board "${rom}")
  46. else
  47. release="y"
  48. board=$(detect_board "${archive}")
  49. fi
  50. boarddir="${cbcfgsdir}/${board}"
  51. [ -d "${boarddir}" ] && return 0
  52. err "check_board: board ${board} not found"
  53. }
  54. check_release()
  55. {
  56. [ -f "${archive}" ] || return 1
  57. [ "${archive##*.}" = "xz" ] || return 1
  58. printf "%s\n" "Release archive ${archive} detected"
  59. }
  60. # This function tries to determine the board from the filename of the rom.
  61. # It will only succeed if the filename is not changed from the build/download
  62. detect_board()
  63. {
  64. path="${1}"
  65. filename=$(basename "${path}")
  66. case ${filename} in
  67. grub_*)
  68. board=$(echo "${filename}" | cut -d '_' -f2-3) ;;
  69. seabios_withgrub_*)
  70. board=$(echo "${filename}" | cut -d '_' -f3-4) ;;
  71. *.tar.xz)
  72. _stripped_prefix=${filename#*_}
  73. board="${_stripped_prefix%.tar.xz}" ;;
  74. *)
  75. err "detect_board $filename: could not detect board type"
  76. esac
  77. [ -d "${boarddir}/" ] || \
  78. err "detect_board: dir, ${boarddir}, doesn't exist"
  79. printf "%s\n" "${board}"
  80. }
  81. build_dependencies()
  82. {
  83. [ -d "${cbdir}" ] || x_ ./update trees -f coreboot default
  84. if [ ! -f "${cbfstool}" ] || [ ! -f "${ifdtool}" ]; then
  85. x_ ./update trees -b coreboot utils default
  86. fi
  87. [ -z "${new_mac}" ] || [ -f "${nvmutil}" ] || x_ make -C util/nvmutil
  88. [ "${nukemode}" = "nuke" ] && return 0
  89. x_ ./vendor download ${board}
  90. }
  91. inject_vendorfiles()
  92. {
  93. if [ "${release}" != "y" ]; then
  94. patch_rom "${rom}"
  95. return 0
  96. fi
  97. printf "patching release images\n"
  98. patch_release_roms
  99. }
  100. patch_release_roms()
  101. {
  102. _tmpdir="tmp/romdir"
  103. x_ rm -Rf "${_tmpdir}"
  104. x_ mkdir -p "${_tmpdir}"
  105. tar -xf "${archive}" -C "${_tmpdir}" || \
  106. err "patch_release_roms: !tar -xf \"${archive}\" -C \"${_tmpdir}\""
  107. for x in "${_tmpdir}"/bin/*/*.rom ; do
  108. printf "patching rom: %s\n" "$x"
  109. patch_rom "${x}"
  110. done
  111. for x in "${_tmpdir}"/bin/*/*_nomicrocode.rom ; do
  112. [ -f "${x}" ] || continue
  113. [ -f "${x%_nomicrocode.rom}.rom" ] || continue
  114. cp "${x%_nomicrocode.rom}.rom" "${x}" || \
  115. err "patch_r: !cp \"${x%_nomicrocode.rom}.rom\" \"${x}\""
  116. x_ "${cbfstool}" "${x}" remove -n cpu_microcode_blob.bin
  117. done
  118. (
  119. x_ cd "${_tmpdir}/bin/"* # TODO: very dodgy, re-write accordingly
  120. # NOTE: For compatibility with older rom releases, defer to sha1
  121. [ "${nukemode}" = "nuke" ] || \
  122. sha512sum --status -c vendorhashes || \
  123. sha1sum --status -c vendorhashes || \
  124. sha512sum --status -c blobhashes || \
  125. sha1sum --status -c blobhashes || \
  126. err "patch_release_roms: ROMs did not match expected hashes"
  127. ) || err "can't verify vendor hashes"
  128. if [ "${modifygbe}" = "true" ]; then
  129. for x in "${_tmpdir}"/bin/*/*.rom ; do
  130. modify_gbe "${x}"
  131. done
  132. fi
  133. [ -d bin/release ] || x_ mkdir -p bin/release
  134. x_ mv "${_tmpdir}"/bin/* bin/release/
  135. printf "Success! Your ROMs are in bin/release\n"
  136. x_ rm -Rf "${_tmpdir}"
  137. }
  138. patch_rom()
  139. {
  140. rom="${1}"
  141. # we don't process no-microcode roms; these are
  142. # instead re-created at the end, after re-inserting
  143. # on roms with microcode, by copying and then removing,
  144. # so that the hashes will match (otherwise, cbfstool
  145. # may sometimes insert certain vendor files at the wrong offset)
  146. # (unless nomicrocode is the only config provided)
  147. [ "${rom}" != "${rom%_nomicrocode.rom}.rom" ] && \
  148. [ -f "${rom%_nomicrocode.rom}.rom" ] && \
  149. [ "${release}" = "y" ] && return 0
  150. x_ check_defconfig "${boarddir}"
  151. set -- "${boarddir}/config/"*
  152. . "${1}" 2>/dev/null
  153. [ "$CONFIG_HAVE_MRC" = "y" ] && \
  154. inject "mrc.bin" "${CONFIG_MRC_FILE}" "mrc" "0xfffa0000"
  155. [ "${CONFIG_HAVE_ME_BIN}" = "y" ] && \
  156. inject "IFD" "${CONFIG_ME_BIN_PATH}" "me"
  157. [ "${CONFIG_KBC1126_FIRMWARE}" = "y" ] && \
  158. inject "ecfw1.bin" "$CONFIG_KBC1126_FW1" "raw" \
  159. "${CONFIG_KBC1126_FW1_OFFSET}" && \
  160. inject "ecfw2.bin" "$CONFIG_KBC1126_FW2" "raw" \
  161. "${CONFIG_KBC1126_FW2_OFFSET}"
  162. [ -n "${CONFIG_VGA_BIOS_FILE}" ] && \
  163. [ -n "${CONFIG_VGA_BIOS_ID}" ] && \
  164. inject "pci${CONFIG_VGA_BIOS_ID}.rom" \
  165. "${CONFIG_VGA_BIOS_FILE}" "optionrom"
  166. [ "${CONFIG_INCLUDE_SMSC_SCH5545_EC_FW}" = "y" ] && \
  167. [ -n "${CONFIG_SMSC_SCH5545_EC_FW_FILE}" ] && \
  168. inject "sch5545_ecfw.bin" "$CONFIG_SMSC_SCH5545_EC_FW_FILE" raw
  169. [ "${modifygbe}" = "true" ] && ! [ "${release}" = "y" ] && \
  170. inject "IFD" "${CONFIG_GBE_BIN_PATH}" "GbE"
  171. printf "ROM image successfully patched: %s\n" "${rom}"
  172. }
  173. inject()
  174. {
  175. [ $# -lt 3 ] && \
  176. err "inject $@, $rom: usage: inject name path type (offset)"
  177. eval "$(setvars "" cbfsname _dest _t _offset)"
  178. cbfsname="${1}"
  179. _dest="${2##*../}"
  180. _t="${3}"
  181. [ $# -gt 3 ] && _offset="-b ${4}" && [ -z "${4}" ] && \
  182. err "inject $@, $rom: offset passed, but empty (not defined)"
  183. [ -z "${_dest}" ] && err "inject $@, ${rom}: empty destination path"
  184. [ ! -f "${_dest}" ] && [ "${nukemode}" != "nuke" ] && \
  185. err "inject_${dl_type}: file missing, ${_dest}"
  186. [ "$nukemode" = "nuke" ] || \
  187. printf "Inserting %s/%s into file: %s\n" \
  188. "${cbfsname}" "${_t}" "$rom"
  189. if [ "${_t}" = "GbE" ]; then
  190. x_ mkdir -p tmp
  191. cp "${_dest}" "tmp/gbe.bin" || \
  192. err "inject: !cp \"${_dest}\" \"tmp/gbe.bin\""
  193. _dest="tmp/gbe.bin"
  194. "${nvmutil}" "${_dest}" setmac "${new_mac}" || \
  195. err "inject ${_dest}: can't change mac address"
  196. fi
  197. if [ "${cbfsname}" = "IFD" ]; then
  198. if [ "${nukemode}" != "nuke" ]; then
  199. "${ifdtool}" -i ${_t}:${_dest} "${rom}" -O "$rom" || \
  200. err "inject: can't insert $_t ($dest) into $rom"
  201. else
  202. "${ifdtool}" --nuke ${_t} "${rom}" -O "${rom}" || \
  203. err "inject ${rom}: can't nuke ${_t} in IFD"
  204. fi
  205. else
  206. if [ "${nukemode}" != "nuke" ]; then
  207. "${cbfstool}" "${rom}" add -f "${_dest}" \
  208. -n "${cbfsname}" -t ${_t} ${_offset} || \
  209. err "inject $rom: can't insert $_t file $_dest"
  210. else
  211. "${cbfstool}" "${rom}" remove -n "${cbfsname}" || \
  212. err "inject $rom: can't remove ${cbfsname}"
  213. fi
  214. fi
  215. }
  216. usage()
  217. {
  218. cat <<- EOF
  219. USAGE: ./vendor inject -r [rom path] -b [boardname] -m [macaddress]
  220. Example: ./vendor inject -r x230_12mb.rom -b x230_12mb
  221. Adding a macadress to the gbe is optional.
  222. If the [-m] parameter is left blank, the gbe will not be touched.
  223. Type './vendor inject listboards' to get a list of valid boards
  224. EOF
  225. }
  226. main $@