coreboot 7.5 KB

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