download 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. (
  158. printf "Entering %s\n" "${cdir}"
  159. cd "${cdir}" || exit 1
  160. for i in *; do
  161. if [ -f "${_me_destination}" ]; then
  162. # me.bin found, so avoid needless further traversal
  163. break
  164. elif [ -L "${i}" ]; then
  165. # symlinks are a security risk, in this context
  166. continue
  167. elif [ -f "${i}" ]; then
  168. "${mecleaner}" -r -t -O "${_me_destination}" "${i}" \
  169. && break # (we found me.bin)
  170. "${me7updateparser}" -O ${_me_destination} "${i}" \
  171. && break
  172. _7ztest="${_7ztest}a"
  173. 7z x "${i}" -o${_7ztest} || continue
  174. Bruteforce_extract_me "${_me_destination}" "${cdir}/${_7ztest}"
  175. cdir="${1}"
  176. cd "${cdir}"
  177. elif [ -d "$i" ]; then
  178. Bruteforce_extract_me "${_me_destination}" "${cdir}/${i}"
  179. cdir="${1}"
  180. cd "${cdir}"
  181. else
  182. printf "SKIPPING: %s\n" "${i}"
  183. fi
  184. done
  185. )
  186. if [ ! -f "${_me_destination}" ]; then
  187. printf "me.bin not found in vendor update for board: `%s`\n" ${board}
  188. return 1
  189. else
  190. return 0
  191. fi
  192. }
  193. Download_ec() {
  194. printf "Downloading KBC1126 EC firmware for HP laptop\n"
  195. Fetch_update ec || return 1
  196. Extract_ec || return 1
  197. return 0
  198. }
  199. Extract_ec() {
  200. printf "Extracting KBC1126 EC firmware for board: `%s`\n" ${board}
  201. _ec_destination=${CONFIG_KBC1126_FW1#../../}
  202. if [ ! -d "${_ec_destination%/*}" ]; then
  203. mkdir -p "${_ec_destination%/*}"
  204. fi
  205. if [ -d "${appdir}" ]; then
  206. rm -Rf "${appdir}"
  207. fi
  208. if [ -f "${_ec_destination}" ]; then
  209. printf "ec already downloaded\n"
  210. return 0
  211. fi
  212. unar "${dl_path}" -o "${appdir}"
  213. (
  214. cd "${appdir}/${dl_path##*/}"
  215. mv Rompaq/68*.BIN ec.bin
  216. "${kbc1126_ec_dump}" ec.bin
  217. )
  218. for i in 1 2; do
  219. if [ ! -f "${appdir}/${dl_path##*/}/ec.bin.fw${i}" ]; then
  220. printf "Not found: `%s/%s/ec.bin.fw%s`\n" \
  221. ${appdir} ${dl_path##*/} ${i}
  222. printf "Could not extract EC firmware for board: `%s`\n" \
  223. ${board}
  224. return 1
  225. fi
  226. done
  227. cp "${appdir}/${dl_path##*/}"/ec.bin.fw* "${_ec_destination%/*}/"
  228. }
  229. Fetch_update() {
  230. printf "Fetching vendor update for board: `%s`\n" ${board}
  231. fw_type="${1}"
  232. dl=""
  233. dl_bkup=""
  234. dlsum=""
  235. if [ "${fw_type}" = "me" ]; then
  236. dl=${dl_url}
  237. dl_bkup=${dl_url_bkup}
  238. dlsum=${dl_hash}
  239. elif [ "${fw_type}" = "ec" ]; then
  240. dl=${ec_url}
  241. dl_bkup=${ec_url_bkup}
  242. dlsum=${ec_hash}
  243. else
  244. printf "Unsupported download type: `%s`\n" ${fw_type}
  245. return 1
  246. fi
  247. if [ -z "${dl_url+x}" ]; then
  248. printf "No vendor update specified for board: `%s`\n" ${board}
  249. return 1
  250. fi
  251. Vendor_checksum ${dlsum} || \
  252. curl ${dl} > ${dl_path} || curl ${dl_bkup} > ${dl_path}
  253. Vendor_checksum ${dlsum} || Fail \
  254. "Cannot guarantee intergity of vendor update for board: `${board}`"
  255. return 0
  256. }
  257. Vendor_checksum() {
  258. sha1=$1
  259. if [ ! -f "${dl_path}" ]; then
  260. printf "Vendor update not found on disk for board: `%s`\n" ${board}
  261. return 1
  262. fi
  263. if [ "$(sha1sum ${dl_path} | awk '{print $1}')" != "${sha1}" ]; then
  264. printf "Bad checksum on vendor update for board: `%s`\n" ${board}
  265. rm ${dl_path}
  266. return 1
  267. fi
  268. return 0
  269. }
  270. Main