coreboot 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # set this when you want to modify each coreboot tree
  22. # for example, you want to test custom patches
  23. # NODELETE= ./download coreboot
  24. deleteblobs="true"
  25. [ "x${NODELETE+set}" = 'xset' ] && deleteblobs="false"
  26. # Error handling is extreme in this script.
  27. # This script handles the internet, and Git. Both are inherently unreliable.
  28. [[ -f build_error ]] && rm -f build_error
  29. rm -f resources/coreboot/*/seen
  30. downloadfor() {
  31. board="${1}"
  32. cbtree="undefined"
  33. cbrevision="undefined"
  34. # The loop will always exit, but this while loop is crafted
  35. # such that a tree referencing a tree that references another tree is possible
  36. # (and so on)
  37. while true; do
  38. cbrevision="undefined"
  39. cbtree="undefined"
  40. if [ ! -f "resources/coreboot/${board}/board.cfg" ]; then
  41. printf "ERROR: download/coreboot: board.cfg does not exist for '%s'\n" "${board}"
  42. return 1
  43. fi
  44. if [ -f "resources/coreboot/${board}/seen" ]; then
  45. printf "ERROR: download/coreboot: logical loop; '%s' board.cfg refers to another tree, which ultimately refers back to '%s'.\n" "${board}" "${board}"
  46. return 1
  47. fi
  48. # This is to override $cbrevision and $cbtree
  49. source "resources/coreboot/${board}/board.cfg" || touch ../build_error
  50. if [ -f build_error ]; then
  51. printf "ERROR: download/coreboot: problem sourcing %s/board.cfg\n" "${board}"
  52. return 1
  53. fi
  54. touch "resources/coreboot/${board}/seen"
  55. if [ "${board}" != "${cbtree}" ]; then
  56. board="${cbtree}"
  57. else
  58. if [ "${cbtree}" = "undefined" ]; then
  59. printf "ERROR: download/coreboot: tree name undefined for '%s\n'" "${board}"
  60. return 1
  61. fi
  62. if [ "${cbrevision}" = "undefined" ]; then
  63. printf "ERROR: download/coreboot: commit ID undefined for '%s'\n" "${board}"
  64. return 1
  65. fi
  66. break
  67. fi
  68. done
  69. rm -f resources/coreboot/*/seen
  70. if [ -d "coreboot/${cbtree}" ]; then
  71. printf "REMARK: download/coreboot: directory for '%s' already exists. Skipping setup.\n" "${cbtree}"
  72. if [ "${cbtree}" != "${1}" ]; then
  73. printf "(for board: '${1}')\n"
  74. fi
  75. return 0
  76. fi
  77. if [ ! -d coreboot ]; then
  78. mkdir "coreboot/"
  79. fi
  80. if [ ! -d coreboot ]; then
  81. printf "ERROR: download/coreboot: directory not created. Check file system permissions\n"
  82. return 1
  83. fi
  84. cd "coreboot/"
  85. if [ ! -d coreboot/.git ] && [ -d coreboot ]; then
  86. rm -Rf coreboot/
  87. fi
  88. if [ ! -d coreboot ]; then
  89. printf "Download coreboot from upstream:\n"
  90. git clone https://review.coreboot.org/coreboot || rm -Rf coreboot
  91. if [ ! -d coreboot ]; then
  92. printf "WARNING: Upstream failed. Trying backup github repository:\n"
  93. git clone https://github.com/coreboot/coreboot.git || rm -Rf coreboot
  94. fi
  95. if [ ! -d coreboot ]; then
  96. printf "ERROR: download/coreboot: Problem with git-clone. Network issue?\n"
  97. cd ../; return 1
  98. fi
  99. else
  100. ( cd coreboot/; git pull || touch ../build_error )
  101. if [ -f ../build_error ]; then
  102. printf "ERROR: download/coreboot: Problem with git-pull. Network issue?\n"
  103. cd ../; return 1
  104. fi
  105. fi
  106. cp -R coreboot "${cbtree}" || touch ../build_error
  107. if [ -d ../build_error ]; then
  108. printf "ERROR: download/coreboot: Unable to copy directory. Check file system permissions or free space.\n"
  109. rm -Rf "${cbtree}/"
  110. cd ../; return 1
  111. fi
  112. cd ${cbtree}/
  113. git reset --hard ${cbrevision} || touch ../../build_error
  114. if [ -f ../../build_error ]; then
  115. printf "ERROR: download/coreboot: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" "${cbrevision}" "${1}" "${cbtree}"
  116. cd ../../; return 1
  117. fi
  118. git submodule update --init || touch ../../build_error
  119. if [ -f ../../build_error ]; then
  120. printf "ERROR: download/coreboot: Unable to update submodules for tree '%s'\n" "${cbtree}"
  121. cd ../../; return 1
  122. fi
  123. for patch in ../../resources/coreboot/${cbtree}/patches/*.patch; do
  124. if [ ! -f "${patch}" ]; then
  125. continue
  126. fi
  127. git am "${patch}" || touch ../../build_error
  128. if [ -f ../../build_error ]; then
  129. printf "ERROR: download/coreboot: Unable to apply patch '%s' for board '%s' on tree '%s'" "${patch}" "${1}" "${cbtree}"
  130. git am --abort
  131. cd ../../; return 1
  132. fi
  133. done
  134. # extra.sh could be used to patch submodules, if you wanted to
  135. # It's impossible to predict what submodules will be available, and
  136. # it's rare that you'd want to patch them, so this is handled by
  137. # extra.sh on a per-board basis
  138. # In fact, extra.sh can be used for anything you want.
  139. if [ -f "../../resources/coreboot/${board}/extra.sh" ]; then
  140. "../../resources/coreboot/${board}/extra.sh" || touch ../../build_error
  141. if [ -f ../../build_error ]; then
  142. cd ../../; return 1
  143. fi
  144. return 0
  145. else
  146. cd ../../
  147. return 0
  148. fi
  149. }
  150. printf "Downloading coreboot and (if exist in build system) applying patches\n"
  151. if [ $# -gt 0 ]; then
  152. for board in "${@}"; do
  153. rm -f resources/coreboot/*/seen
  154. downloadfor "${board}"
  155. if [ -f build_error ]; then break; fi
  156. done
  157. else
  158. for board in resources/coreboot/*; do
  159. rm -f resources/coreboot/*/seen
  160. if [ ! -d "${board}/" ]; then
  161. continue
  162. fi
  163. downloadfor "${board##*/}"
  164. if [ -f build_error ]; then break; fi
  165. done
  166. fi
  167. rm -f resources/coreboot/*/seen
  168. rm -f "build_error"
  169. printf "\n\n"
  170. if [ "${deleteblobs}" = "true" ]; then
  171. rm -Rf coreboot/coreboot/
  172. rm -Rf coreboot/.git* coreboot/*/.git* coreboot/*/3rdparty/*/.git*
  173. rm -Rf coreboot/*/util/nvidia/cbootimage/.git*
  174. for cbdir in coreboot/*; do
  175. if [ ! -d "${cbdir}" ]; then continue; fi
  176. cbtree="${cbdir##coreboot/}"
  177. cbtree="${cbtree%/}"
  178. if [ ! -d "coreboot/${cbtree}" ]; then continue; fi
  179. bloblist="resources/coreboot/${cbtree}/blobs.list"
  180. if [ -f "${bloblist}" ]; then
  181. for blobfile in $(cat "${bloblist}"); do
  182. printf "Deleting blob: 'coreboot/%s/%s'\n" "${cbtree}" "${blobfile}"
  183. rm -f "coreboot/${cbtree}/${blobfile}"
  184. done
  185. fi
  186. rmlist="resources/coreboot/${cbtree}/rm.list"
  187. if [ -f "${rmlist}" ]; then
  188. for rmentry in $(cat "${rmlist}"); do
  189. printf "Deleting directory to save space: 'coreboot/%s/%s'\n" "${cbtree}" "${rmentry}"
  190. rm -Rf "coreboot/${cbtree}/${rmentry}"
  191. done
  192. fi
  193. done
  194. fi
  195. exit 0