inject 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env sh
  2. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. # SPDX-FileCopyrightText: 2023 Leah Rowe <info@minifree.org>
  5. # SPDX-License-Identifier: GPL-3.0-only
  6. Fail(){
  7. if [ ! -z ${@+x} ]; then
  8. printf "\nERROR: ${@}\n"
  9. fi
  10. cat <<- EOF
  11. USAGE: ./blobutil inject -r [/path/to/rom] -b [boardname] -m [macaddress]
  12. Example: ./blobutil inject -r x230_12mb.rom -b x230_12mb
  13. Adding a macadress to the gbe is optional.
  14. If the [-m] parameter is left blank, the gbe will not be touched.
  15. Type './blobutil inject listboards' to get a list of valid boards
  16. EOF
  17. exit 1
  18. }
  19. Modify_gbe(){
  20. rom=${1}
  21. printf "changing mac address in gbe to ${new_mac}\n"
  22. _gbe_location=${CONFIG_GBE_BIN_PATH#../../}
  23. if [ ! -f util/nvmutil/nvm ]; then
  24. make -C util/nvmutil || Fail 'failed to build nvmutil'
  25. fi
  26. _gbe_tmp=$(mktemp -t gbeXXXX.bin)
  27. cp ${_gbe_location} ${_gbe_tmp}
  28. ./util/nvmutil/nvm ${_gbe_tmp} setmac ${new_mac} || Fail 'failed to modify mac address\nmake sure the mac address in the correct format'
  29. ./coreboot/default/util/ifdtool/ifdtool -i GbE:${_gbe_tmp} ${rom} -O ${rom} || exit 1
  30. rm ${_gbe_tmp}
  31. }
  32. listboards() {
  33. for boarddir in resources/coreboot/*; do
  34. if [ ! -d "${boarddir}" ]; then continue; fi
  35. board="${boarddir##resources/coreboot/}"
  36. board="${board%/}"
  37. printf '%s\n' "${board##*/}"
  38. done
  39. }
  40. # This function tries to determine the board from the filename of the rom.
  41. # It will only succeed if the filename is not changed from the build/download
  42. Detect_board(){
  43. path=${1}
  44. filename=$(basename ${path})
  45. case ${filename} in
  46. grub_*)
  47. board=$(echo "${filename}" | cut -d '_' -f2-3)
  48. ;;
  49. seabios_withgrub_*)
  50. board=$(echo "${filename}" | cut -d '_' -f3-4)
  51. ;;
  52. *.tar.xz)
  53. _stripped_prefix=${filename#*_}
  54. board="${_stripped_prefix%.tar.xz}"
  55. ;;
  56. *)
  57. return 1
  58. esac
  59. if [ -d "resources/coreboot/${board}/" ]; then
  60. printf '%s\n' "${board}"
  61. else
  62. return 1
  63. fi
  64. }
  65. Patch(){
  66. rom="${1}"
  67. set -- "resources/coreboot/${board}/config/*"
  68. . ${1} 2>/dev/null
  69. . "resources/coreboot/${board}/board.cfg"
  70. if [ "$CONFIG_HAVE_MRC" = "y" ]; then
  71. printf 'adding mrc\n'
  72. ./coreboot/default/util/cbfstool/cbfstool ${rom} add -f mrc/haswell/mrc.bin -n mrc.bin -t mrc -b 0x78fe00 || exit 1
  73. fi
  74. if [ "${CONFIG_HAVE_ME_BIN}" = "y" ]; then
  75. _me_location=${CONFIG_ME_BIN_PATH#../../}
  76. printf 'adding intel management engine\n'
  77. ./coreboot/default/util/ifdtool/ifdtool -i me:${_me_location} ${rom} -O ${rom} || exit 1
  78. fi
  79. if [ "${CONFIG_KBC1126_FIRMWARE}" = "y" ]; then
  80. _ec1_location="${CONFIG_KBC1126_FW1#../../}"
  81. _ec1_offset="${CONFIG_KBC1126_FW1_OFFSET}"
  82. _ec2_location="${CONFIG_KBC1126_FW2#../../}"
  83. _ec2_offset="${CONFIG_KBC1126_FW2_OFFSET}"
  84. printf "adding hp kbc1126 ec firmware\n"
  85. if [ "${_ec1_offset}" = "" ] || [ "${_ec1_offset}" = "" ];
  86. then
  87. printf "EC offsets not declared for board: %s\n" \
  88. "${board}"
  89. exit 1
  90. fi
  91. if [ "${_ec1_location}" = "" ] || [ "${_ec2_location}" = "" ];
  92. then
  93. printf "EC firmware path not declared for board: %s\n" \
  94. "${board}"
  95. fi
  96. if [ ! -f "${_ec1_location}" ] || [ ! -f "${_ec2_location}" ];
  97. then
  98. printf "EC firmware not downloaded for board: %s\n" \
  99. "${board}"
  100. exit 1
  101. fi
  102. ./coreboot/default/util/cbfstool/cbfstool "${rom}" add -f ${_ec1_location} -n ecfw1.bin -b ${_ec1_offset} -t raw || exit 1
  103. ./coreboot/default/util/cbfstool/cbfstool "${rom}" add -f ${_ec2_location} -n ecfw2.bin -b ${_ec2_offset} -t raw || exit 1
  104. fi
  105. if [ "${modifygbe}" = "true" ] && ! [ "${release}" = "true" ]; then
  106. Modify_gbe ${rom}
  107. fi
  108. }
  109. Patch_release(){
  110. _tmpdir=$(mktemp -d "/tmp/${board}_tmpXXXX")
  111. tar xf "${releasearchive}" -C "${_tmpdir}" || \
  112. Fail 'could not extract release archive'
  113. for rom in ${_tmpdir}/bin/*/*.rom ; do
  114. echo "patching rom $rom"
  115. Patch ${rom} || \
  116. Fail "could not patch ${rom}"
  117. done
  118. ( cd ${_tmpdir}/bin/*
  119. sha1sum --status -c blobhashes || \
  120. Fail 'ROMs did not match expected hashes'
  121. )
  122. if [ "${modifygbe}" = "true" ]; then
  123. for rom in ${_tmpdir}/bin/*/*.rom ; do
  124. Modify_gbe ${rom}
  125. done
  126. fi
  127. if ! [ -d bin/release ]; then
  128. mkdir -p bin/release
  129. fi
  130. mv ${_tmpdir}/bin/* bin/release/ && \
  131. printf '%s\n' 'Success! Your ROMs are in bin/release'
  132. rm -r "${_tmpdir}"
  133. }
  134. Check_release(){
  135. if ! [ -f ${1} ]; then
  136. return 1
  137. fi
  138. _filetype=$(file -b "${1}")
  139. if [ "${_filetype%%,*}" = "XZ compressed data" ]; then
  140. printf "%s\n" "Release archive ${1} detected"
  141. else
  142. return 1
  143. fi
  144. }
  145. if [ "${1}" = "listboards" ]; then
  146. listboards
  147. exit 0
  148. fi
  149. # Implementing parameter parsing now so more options can be added later
  150. while getopts r:b:m: option
  151. do
  152. case "${option}"
  153. in
  154. r)rom=${OPTARG};;
  155. b)board=${OPTARG};;
  156. m)
  157. modifygbe=true
  158. new_mac=${OPTARG}
  159. ;;
  160. esac
  161. done
  162. if ! Check_release ${1} ; then
  163. if [ ! -f "${rom}" ]; then
  164. Fail "${rom} is not a valid path"
  165. elif [ -z ${rom+x} ]; then
  166. Fail 'no rom specified'
  167. elif [ -z ${board+x} ]; then
  168. board=$(Detect_board ${rom}) || \
  169. Fail 'no board specified'
  170. fi
  171. else
  172. release=true
  173. releasearchive="${1}"
  174. board=$(Detect_board ${1}) || \
  175. Fail 'Could not detect board type'
  176. fi
  177. if [ ! -d "resources/coreboot/${board}/" ]; then
  178. Fail "board ${board} not found"
  179. fi
  180. if [ ! -d coreboot/default ]; then
  181. printf "downloading coreboot\n"
  182. ./download coreboot default
  183. fi
  184. if [ ! -f "coreboot/default/util/ifdtool/ifdtool" ]; then
  185. printf "building ifdtool from coreboot\n"
  186. ./build module cbutils default || Fail 'could not build ifdtool'
  187. fi
  188. if [ ! -f "coreboot/default/util/cbfstool/cbfstool" ]; then
  189. printf "building cbfstool from coreboot\n"
  190. ./build module cbutils default || Fail 'could not build cbfstool'
  191. fi
  192. ./blobutil download ${board} || \
  193. Fail "Could not download blobs for ${board}, check network connection"
  194. if [ "${release}" = "true" ]; then
  195. echo 'patching release file'
  196. Patch_release
  197. else
  198. Patch ${rom}
  199. fi