inject 4.8 KB

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