coreboot 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/usr/bin/env bash
  2. # helper script: download coreboot
  3. #
  4. # Copyright (C) 2014, 2015, 2016, 2020, 2021 Leah Rowe <info@minifree.org>
  5. # Copyright (C) 2022 Alper Nebi Yasak <alpernebiyasak@gmail.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. [ "x${DEBUG+set}" = 'xset' ] && set -v
  21. set -u -e
  22. list_supported_boards()
  23. {
  24. for board in resources/coreboot/*; do
  25. echo $board | sed 's#resources/coreboot/##'
  26. done
  27. }
  28. usage()
  29. {
  30. progname="./download coreboot"
  31. printf "Usage:\n"
  32. printf "\t%s # %s\n" \
  33. "${progname}" \
  34. "Download and deblob Coreboot for all the boards"
  35. printf "\t%s [board [board] ...] # %s\n" \
  36. "${progname}" \
  37. "Download and deblob Coreboot for the given boards"
  38. printf "\t%s --list-boards # %s\n" \
  39. "${progname}" \
  40. "Prints this help"
  41. printf "\t%s --help # %s\n" \
  42. "${progname}" \
  43. "List supported boards"
  44. printf "\t%s --help # %s\n" \
  45. "${progname}" \
  46. "Prints this help"
  47. }
  48. # In this script, set -u is used to check for undefined variables, and
  49. # the test command doesn't do any lazy evaluation, so we can't use
  50. # a syntax like that: [ $# -eq 1 -a "$1" == "--help" ].
  51. if [ $# -eq 1 ] && [ "$1" == "--help" ] ; then
  52. usage
  53. exit 0
  54. elif [ $# -eq 1 ] && [ "$1" == "--list-boards" ] ; then
  55. list_supported_boards
  56. exit 0
  57. fi
  58. # set this when you want to modify each coreboot tree
  59. # for example, you want to test custom patches
  60. # NODELETE= ./download coreboot
  61. deletegit="true"
  62. deleteblobs="true"
  63. if [ "x${NODELETE+set}" = 'xset' ]; then
  64. [ "x${NODELETE:-all}" = "xgit" ] && deletegit="false"
  65. [ "x${NODELETE:-all}" = "xall" ] && deleteblobs="false" && deletegit="false"
  66. fi
  67. # Error handling is extreme in this script.
  68. # This script handles the internet, and Git. Both are inherently unreliable.
  69. [[ -f build_error ]] && rm -f build_error
  70. rm -f resources/coreboot/*/seen
  71. downloadfor() {
  72. board="${1}"
  73. cbtree="undefined"
  74. cbrevision="undefined"
  75. # The loop will always exit, but this while loop is crafted
  76. # such that a tree referencing a tree that references another tree is possible
  77. # (and so on)
  78. while true; do
  79. cbrevision="undefined"
  80. cbtree="undefined"
  81. if [ ! -f "resources/coreboot/${board}/board.cfg" ]; then
  82. printf "ERROR: download/coreboot: board.cfg does not exist for '%s'\n" "${board}"
  83. return 1
  84. fi
  85. if [ -f "resources/coreboot/${board}/seen" ]; then
  86. printf "ERROR: download/coreboot: logical loop; '%s' board.cfg refers to another tree, which ultimately refers back to '%s'.\n" "${board}" "${board}"
  87. return 1
  88. fi
  89. # This is to override $cbrevision and $cbtree
  90. source "resources/coreboot/${board}/board.cfg" || touch ../build_error
  91. if [ -f build_error ]; then
  92. printf "ERROR: download/coreboot: problem sourcing %s/board.cfg\n" "${board}"
  93. return 1
  94. fi
  95. touch "resources/coreboot/${board}/seen"
  96. if [ "${board}" != "${cbtree}" ]; then
  97. board="${cbtree}"
  98. else
  99. if [ "${cbtree}" = "undefined" ]; then
  100. printf "ERROR: download/coreboot: tree name undefined for '%s\n'" "${board}"
  101. return 1
  102. fi
  103. if [ "${cbrevision}" = "undefined" ]; then
  104. printf "ERROR: download/coreboot: commit ID undefined for '%s'\n" "${board}"
  105. return 1
  106. fi
  107. break
  108. fi
  109. done
  110. rm -f resources/coreboot/*/seen
  111. if [ -d "coreboot/${cbtree}" ]; then
  112. printf "REMARK: download/coreboot: directory for '%s' already exists. Skipping setup.\n" "${cbtree}"
  113. if [ "${cbtree}" != "${1}" ]; then
  114. printf "(for board: '${1}')\n"
  115. fi
  116. return 0
  117. fi
  118. if [ ! -d coreboot ]; then
  119. mkdir "coreboot/"
  120. fi
  121. if [ ! -d coreboot ]; then
  122. printf "ERROR: download/coreboot: directory not created. Check file system permissions\n"
  123. return 1
  124. fi
  125. cd "coreboot/"
  126. if [ ! -d coreboot/.git ] && [ -d coreboot ]; then
  127. rm -Rf coreboot/
  128. fi
  129. if [ ! -d coreboot ]; then
  130. printf "Download coreboot from upstream:\n"
  131. git clone --depth=1 https://review.coreboot.org/coreboot || rm -Rf coreboot
  132. if [ ! -d coreboot ]; then
  133. printf "WARNING: Upstream failed. Trying backup github repository:\n"
  134. git clone --depth=1 https://github.com/coreboot/coreboot.git || rm -Rf coreboot
  135. fi
  136. if [ ! -d coreboot ]; then
  137. printf "ERROR: download/coreboot: Problem with git-clone. Network issue?\n"
  138. cd ../; return 1
  139. fi
  140. fi
  141. ( cd coreboot/; git fetch --depth=1 origin "${cbrevision}" || touch ../build_error )
  142. if [ -f ../build_error ]; then
  143. printf "ERROR: download/coreboot: Problem with git-fetch. Network issue?\n"
  144. cd ../; return 1
  145. fi
  146. cp -R coreboot "${cbtree}" || touch ../build_error
  147. if [ -f ../build_error ]; then
  148. printf "ERROR: download/coreboot: Unable to copy directory. Check file system permissions or free space.\n"
  149. rm -Rf "${cbtree}/"
  150. cd ../; return 1
  151. fi
  152. cd ${cbtree}/
  153. git reset --hard ${cbrevision} || touch ../../build_error
  154. if [ -f ../../build_error ]; then
  155. printf "ERROR: download/coreboot: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" "${cbrevision}" "${1}" "${cbtree}"
  156. cd ../../; return 1
  157. fi
  158. git submodule update --init --depth=1 || touch ../../build_error
  159. if [ -f ../../build_error ]; then
  160. printf "ERROR: download/coreboot: Unable to update submodules for tree '%s'\n" "${cbtree}"
  161. cd ../../; return 1
  162. fi
  163. for patch in ../../resources/coreboot/${cbtree}/patches/*.patch; do
  164. if [ ! -f "${patch}" ]; then
  165. continue
  166. fi
  167. git am "${patch}" || touch ../../build_error
  168. if [ -f ../../build_error ]; then
  169. printf "ERROR: download/coreboot: Unable to apply patch '%s' for board '%s' on tree '%s'" "${patch}" "${1}" "${cbtree}"
  170. git am --abort
  171. cd ../../; return 1
  172. fi
  173. done
  174. # extra.sh could be used to patch submodules, if you wanted to
  175. # It's impossible to predict what submodules will be available, and
  176. # it's rare that you'd want to patch them, so this is handled by
  177. # extra.sh on a per-board basis
  178. # In fact, extra.sh can be used for anything you want.
  179. if [ -f "../../resources/coreboot/${board}/extra.sh" ]; then
  180. "../../resources/coreboot/${board}/extra.sh" || touch ../../build_error
  181. if [ -f ../../build_error ]; then
  182. cd ../../; return 1
  183. fi
  184. return 0
  185. else
  186. cd ../../
  187. return 0
  188. fi
  189. }
  190. printf "Downloading coreboot and (if exist in build system) applying patches\n"
  191. if [ $# -gt 0 ]; then
  192. for board in "${@}"; do
  193. rm -f resources/coreboot/*/seen
  194. downloadfor "${board}"
  195. if [ -f build_error ]; then break; fi
  196. done
  197. else
  198. for board in resources/coreboot/*; do
  199. rm -f resources/coreboot/*/seen
  200. if [ ! -d "${board}/" ]; then
  201. continue
  202. fi
  203. downloadfor "${board##*/}"
  204. if [ -f build_error ]; then break; fi
  205. done
  206. fi
  207. rm -f resources/coreboot/*/seen
  208. rm -f "build_error"
  209. printf "\n\n"
  210. if [ "${deleteblobs}" = "true" ]; then
  211. if [ "${deletegit}" = "true" ]; then
  212. rm -Rf coreboot/coreboot/
  213. rm -Rf coreboot/.git* coreboot/*/.git* coreboot/*/3rdparty/*/.git*
  214. rm -Rf coreboot/*/util/nvidia/cbootimage/.git*
  215. fi
  216. for cbdir in coreboot/*; do
  217. if [ ! -d "${cbdir}" ]; then continue; fi
  218. cbtree="${cbdir##coreboot/}"
  219. cbtree="${cbtree%/}"
  220. if [ ! -d "coreboot/${cbtree}" ]; then continue; fi
  221. bloblist="resources/coreboot/${cbtree}/blobs.list"
  222. if [ -f "${bloblist}" ]; then
  223. for blobfile in $(cat "${bloblist}"); do
  224. printf "Deleting blob: 'coreboot/%s/%s'\n" "${cbtree}" "${blobfile}"
  225. rm -f "coreboot/${cbtree}/${blobfile}"
  226. done
  227. fi
  228. rmlist="resources/coreboot/${cbtree}/rm.list"
  229. if [ -f "${rmlist}" ]; then
  230. for rmentry in $(cat "${rmlist}"); do
  231. printf "Deleting directory to save space: 'coreboot/%s/%s'\n" "${cbtree}" "${rmentry}"
  232. rm -Rf "coreboot/${cbtree}/${rmentry}"
  233. done
  234. fi
  235. done
  236. fi
  237. exit 0