download 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. ec_url=""
  7. ec_url_bkup=""
  8. ec_hash=""
  9. blobdir="blobs"
  10. dl_path="${blobdir}/vendorupdate"
  11. appdir="${blobdir}/app"
  12. _7ztest="a"
  13. mecleaner="$(pwd)/me_cleaner/me_cleaner.py"
  14. me7updateparser="$(pwd)/resources/blobs/me7_update_parser.py"
  15. kbc1126_ec_dump="$(pwd)/coreboot/default/util/kbc1126/kbc1126_ec_dump"
  16. board="${1}"
  17. # A shorthand for each board, to avoid duplicating configs per flash size
  18. board_short=${board%%_*mb}
  19. set -- "resources/coreboot/${board}/config/*"
  20. . ${1} 2>/dev/null
  21. . "resources/coreboot/${board}/board.cfg"
  22. if [ "${CONFIG_HAVE_MRC}" = "y" ]; then
  23. printf 'haswell board detected, downloading mrc\n'
  24. needs="${needs} MRC"
  25. fi
  26. if [ "${CONFIG_HAVE_IFD_BIN}" = "y" ]; then
  27. printf 'board needs intel firmware descriptor\n'
  28. needs="${needs} IFD"
  29. fi
  30. if [ "${CONFIG_HAVE_ME_BIN}" = "y" ]; then
  31. printf 'board needs intel management engine\n'
  32. needs="${needs} ME"
  33. fi
  34. if [ "${CONFIG_HAVE_GBE_BIN}" = "y" ]; then
  35. printf 'board needs gigabit ethernet firmware\n'
  36. needs="${needs} GBE"
  37. fi
  38. if [ "${CONFIG_KBC1126_FIRMWARE}" = "y" ]; then
  39. printf "HP board with KBC1126 EC detected, downloading ec\n"
  40. needs="${needs} EC"
  41. fi
  42. # Quickly exit without wasting more time if there are no blobs needed (GM45)
  43. if [ -z ${needs+x} ]; then
  44. printf 'No binary blobs needed for this board\n'
  45. exit 0
  46. fi
  47. while read -r line ; do
  48. case ${line} in
  49. EC_url*)
  50. set ${line}
  51. ec_url=${2}
  52. ;;
  53. EC_url_bkup*)
  54. set ${line}
  55. ec_url_bkup=${2}
  56. ;;
  57. EC_hash*)
  58. set ${line}
  59. ec_hash=${2}
  60. ;;
  61. DL_hash*)
  62. set ${line}
  63. dl_hash=${2}
  64. ;;
  65. DL_url*)
  66. set ${line}
  67. dl_url=${2}
  68. ;;
  69. DL_url_bkup*)
  70. set ${line}
  71. dl_url_bkup=${2}
  72. ;;
  73. esac
  74. done << EOF
  75. $(eval "awk ' /\{.*${board_short}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }' resources/blobs/sources")
  76. EOF
  77. Main() {
  78. Build_deps
  79. Download_needed
  80. }
  81. Fail(){
  82. printf "\nERROR: $@\n"
  83. exit 1
  84. }
  85. Build_deps(){
  86. if [ ! -d me_cleaner ]; then
  87. printf "downloading me_cleaner\n"
  88. ./download me_cleaner || Fail 'could not download me_cleaner'
  89. fi
  90. if [ ! -d coreboot/default ]; then
  91. printf "downloading coreboot\n"
  92. ./download coreboot default || Fail 'could not download coreboot'
  93. fi
  94. if [ ! -f coreboot/default/util/kbc1126/kbc1126_ec_dump ]; then
  95. printf "Building kbc1126_ec_dump from coreboot\n"
  96. make -BC coreboot/default/util/kbc1126 || Fail \
  97. "could not build kbc1126_ec_dump"
  98. fi
  99. if [ ! -f "coreboot/default/util/ifdtool/ifdtool" ]; then
  100. printf "building ifdtool from coreboot\n"
  101. make -C coreboot/default/util/ifdtool || Fail 'could not build ifdtool'
  102. fi
  103. }
  104. Download_needed(){
  105. for need in ${needs}; do
  106. case ${need} in
  107. *ME*)
  108. Download_me || _failed="${_failed} me"
  109. ;;
  110. *MRC*)
  111. ./download mrc || _failed="${_failed} mrc"
  112. ;;
  113. *EC*)
  114. Download_ec || _failed="${_failed} ec"
  115. ;;
  116. esac
  117. done
  118. if [ ! -z ${_failed+x} ]; then
  119. Fail "failed to obtain ${_failed}\nYou may try manually extracting blobs with './blobutil extract'"
  120. fi
  121. }
  122. Download_me() {
  123. printf "Downloading neutered ME for board: %s\n" ${board}
  124. Fetch_update me || return 1
  125. Extract_me || return 1
  126. return 0
  127. }
  128. Extract_me(){
  129. printf "Extracting neutered ME for ${board}\n"
  130. _me_destination=${CONFIG_ME_BIN_PATH#../../}
  131. if [ ! -d "${_me_destination%/*}" ]; then
  132. mkdir -p ${_me_destination%/*}
  133. fi
  134. if [ -d "${appdir}" ]; then
  135. rm -r ${appdir}
  136. fi
  137. if [ -f "${_me_destination}" ]; then
  138. printf 'me already downloaded\n'
  139. return 0
  140. fi
  141. printf 'extracting and stripping intel management engine\n'
  142. innoextract ${dl_path} -d ${blobdir} \
  143. || 7z x ${dl_path} -o${appdir} \
  144. || Fail 'could not extract me executable with innoextract'
  145. Bruteforce_extract_me "$(pwd)/${_me_destination}" "$(pwd)/${appdir}" \
  146. || return 1
  147. printf "Truncated and cleaned me output to ${_me_destination}\n"
  148. return 0
  149. }
  150. # cursed, carcinogenic code. TODO rewrite it better
  151. Bruteforce_extract_me() {
  152. _me_destination="${1}"
  153. cdir="${2}" # must be an absolute path, not relative
  154. if [ -f "${_me_destination}" ]; then
  155. return 0
  156. fi
  157. sdir="$(mktemp -d)"
  158. mkdir -p "${sdir}" || return 1
  159. (
  160. printf "Entering %s\n" "${cdir}"
  161. cd "${cdir}" || exit 1
  162. for i in *; do
  163. if [ -f "${_me_destination}" ]; then
  164. # me.bin found, so avoid needless further traversal
  165. break
  166. elif [ -L "${i}" ]; then
  167. # symlinks are a security risk, in this context
  168. continue
  169. elif [ -f "${i}" ]; then
  170. "${mecleaner}" -r -t -O "${sdir}/vendorfile" -M "${_me_destination}" "${i}" \
  171. && break # (we found me.bin)
  172. "${mecleaner}" -r -t -O "${_me_destination}" "${i}" \
  173. && break # (we found me.bin)
  174. "${me7updateparser}" -O ${_me_destination} "${i}" \
  175. && break
  176. _7ztest="${_7ztest}a"
  177. 7z x "${i}" -o${_7ztest} || continue
  178. Bruteforce_extract_me "${_me_destination}" "${cdir}/${_7ztest}"
  179. cdir="${1}"
  180. cd "${cdir}"
  181. elif [ -d "$i" ]; then
  182. Bruteforce_extract_me "${_me_destination}" "${cdir}/${i}"
  183. cdir="${1}"
  184. cd "${cdir}"
  185. else
  186. printf "SKIPPING: %s\n" "${i}"
  187. fi
  188. done
  189. )
  190. rm -Rf "${sdir}"
  191. if [ ! -f "${_me_destination}" ]; then
  192. printf "me.bin not found in vendor update for board: %s\n" ${board}
  193. return 1
  194. else
  195. return 0
  196. fi
  197. }
  198. Download_ec() {
  199. printf "Downloading KBC1126 EC firmware for HP laptop\n"
  200. Fetch_update ec || return 1
  201. Extract_ec || return 1
  202. return 0
  203. }
  204. Extract_ec() {
  205. printf "Extracting KBC1126 EC firmware for board: %s\n" ${board}
  206. _ec_destination=${CONFIG_KBC1126_FW1#../../}
  207. if [ ! -d "${_ec_destination%/*}" ]; then
  208. mkdir -p "${_ec_destination%/*}"
  209. fi
  210. if [ -d "${appdir}" ]; then
  211. rm -Rf "${appdir}"
  212. fi
  213. if [ -f "${_ec_destination}" ]; then
  214. printf "ec already downloaded\n"
  215. return 0
  216. fi
  217. unar "${dl_path}" -o "${appdir}"
  218. (
  219. cd "${appdir}/${dl_path##*/}"
  220. mv Rompaq/68*.BIN ec.bin
  221. "${kbc1126_ec_dump}" ec.bin
  222. )
  223. for i in 1 2; do
  224. if [ ! -f "${appdir}/${dl_path##*/}/ec.bin.fw${i}" ]; then
  225. printf "Not found: %s/%s/ec.bin.fw%s\n" \
  226. ${appdir} ${dl_path##*/} ${i}
  227. printf "Could not extract EC firmware for board: %s\n" \
  228. ${board}
  229. return 1
  230. fi
  231. done
  232. cp "${appdir}/${dl_path##*/}"/ec.bin.fw* "${_ec_destination%/*}/"
  233. }
  234. Fetch_update() {
  235. printf "Fetching vendor update for board: %s\n" ${board}
  236. fw_type="${1}"
  237. dl=""
  238. dl_bkup=""
  239. dlsum=""
  240. if [ "${fw_type}" = "me" ]; then
  241. dl=${dl_url}
  242. dl_bkup=${dl_url_bkup}
  243. dlsum=${dl_hash}
  244. elif [ "${fw_type}" = "ec" ]; then
  245. dl=${ec_url}
  246. dl_bkup=${ec_url_bkup}
  247. dlsum=${ec_hash}
  248. else
  249. printf "Unsupported download type: %s\n" ${fw_type}
  250. return 1
  251. fi
  252. if [ -z "${dl_url+x}" ]; then
  253. printf "No vendor update specified for board: %s\n" ${board}
  254. return 1
  255. fi
  256. Vendor_checksum ${dlsum} || \
  257. curl ${dl} > ${dl_path} || curl ${dl_bkup} > ${dl_path}
  258. Vendor_checksum ${dlsum} || Fail \
  259. "Cannot guarantee intergity of vendor update for board: ${board}"
  260. return 0
  261. }
  262. Vendor_checksum() {
  263. sha1=$1
  264. if [ ! -f "${dl_path}" ]; then
  265. printf "Vendor update not found on disk for board: %s\n" ${board}
  266. return 1
  267. fi
  268. if [ "$(sha1sum ${dl_path} | awk '{print $1}')" != "${sha1}" ]; then
  269. printf "Bad checksum on vendor update for board: %s\n" ${board}
  270. rm ${dl_path}
  271. return 1
  272. fi
  273. return 0
  274. }
  275. Main