coreboot 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. [[ -f build_error ]] && rm -f build_error
  59. rm -f resources/coreboot/*/seen
  60. downloadfor() {
  61. board="${1}"
  62. cbtree="undefined"
  63. cbrevision="undefined"
  64. # The loop will always exit, but this while loop is crafted
  65. # such that a tree referencing a tree that references another tree is possible
  66. # (and so on)
  67. while true; do
  68. cbrevision="undefined"
  69. cbtree="undefined"
  70. if [ ! -f "resources/coreboot/${board}/board.cfg" ]; then
  71. printf "ERROR: download/coreboot: board.cfg does not exist for '%s'\n" "${board}"
  72. return 1
  73. fi
  74. if [ -f "resources/coreboot/${board}/seen" ]; then
  75. printf "ERROR: download/coreboot: logical loop; '%s' board.cfg refers to another tree, which ultimately refers back to '%s'.\n" "${board}" "${board}"
  76. return 1
  77. fi
  78. # This is to override $cbrevision and $cbtree
  79. source "resources/coreboot/${board}/board.cfg" || touch ../build_error
  80. if [ -f build_error ]; then
  81. printf "ERROR: download/coreboot: problem sourcing %s/board.cfg\n" "${board}"
  82. return 1
  83. fi
  84. touch "resources/coreboot/${board}/seen"
  85. if [ "${board}" != "${cbtree}" ]; then
  86. board="${cbtree}"
  87. else
  88. if [ "${cbtree}" = "undefined" ]; then
  89. printf "ERROR: download/coreboot: tree name undefined for '%s\n'" "${board}"
  90. return 1
  91. fi
  92. if [ "${cbrevision}" = "undefined" ]; then
  93. printf "ERROR: download/coreboot: commit ID undefined for '%s'\n" "${board}"
  94. return 1
  95. fi
  96. break
  97. fi
  98. done
  99. rm -f resources/coreboot/*/seen
  100. if [ -d "coreboot/${cbtree}" ]; then
  101. printf "REMARK: download/coreboot: directory for '%s' already exists. Skipping setup.\n" "${cbtree}"
  102. if [ "${cbtree}" != "${1}" ]; then
  103. printf "(for board: '${1}')\n"
  104. fi
  105. return 0
  106. fi
  107. if [ ! -d coreboot ]; then
  108. mkdir "coreboot/"
  109. fi
  110. if [ ! -d coreboot ]; then
  111. printf "ERROR: download/coreboot: directory not created. Check file system permissions\n"
  112. return 1
  113. fi
  114. cd "coreboot/"
  115. if [ ! -d coreboot/.git ] && [ -d coreboot ]; then
  116. rm -Rf coreboot/
  117. fi
  118. if [ ! -d coreboot ]; then
  119. printf "Download coreboot from upstream:\n"
  120. git clone https://review.coreboot.org/coreboot || rm -Rf coreboot
  121. if [ ! -d coreboot ]; then
  122. printf "WARNING: Upstream failed. Trying backup github repository:\n"
  123. git clone https://github.com/coreboot/coreboot.git || rm -Rf coreboot
  124. fi
  125. if [ ! -d coreboot ]; then
  126. printf "ERROR: download/coreboot: Problem with git-clone. Network issue?\n"
  127. cd ../; return 1
  128. fi
  129. else
  130. ( cd coreboot/; git pull || touch ../build_error )
  131. if [ -f ../build_error ]; then
  132. printf "ERROR: download/coreboot: Problem with git-pull. Network issue?\n"
  133. cd ../; return 1
  134. fi
  135. fi
  136. cp -R coreboot "${cbtree}" || touch ../build_error
  137. if [ -d ../build_error ]; then
  138. printf "ERROR: download/coreboot: Unable to copy directory. Check file system permissions or free space.\n"
  139. rm -Rf "${cbtree}/"
  140. cd ../; return 1
  141. fi
  142. cd ${cbtree}/
  143. git reset --hard ${cbrevision} || touch ../../build_error
  144. if [ -f ../../build_error ]; then
  145. printf "ERROR: download/coreboot: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" "${cbrevision}" "${1}" "${cbtree}"
  146. cd ../../; return 1
  147. fi
  148. git submodule update --init --checkout || touch ../../build_error
  149. if [ -f ../../build_error ]; then
  150. printf "ERROR: download/coreboot: Unable to update submodules for tree '%s'\n" "${cbtree}"
  151. cd ../../; return 1
  152. fi
  153. for patch in ../../resources/coreboot/${cbtree}/patches/*.patch; do
  154. if [ ! -f "${patch}" ]; then
  155. continue
  156. fi
  157. git am "${patch}" || touch ../../build_error
  158. if [ -f ../../build_error ]; then
  159. printf "ERROR: download/coreboot: Unable to apply patch '%s' for board '%s' on tree '%s'" "${patch}" "${1}" "${cbtree}"
  160. git am --abort
  161. cd ../../; return 1
  162. fi
  163. done
  164. # extra.sh could be used to patch submodules, if you wanted to
  165. # It's impossible to predict what submodules will be available, and
  166. # it's rare that you'd want to patch them, so this is handled by
  167. # extra.sh on a per-board basis
  168. # In fact, extra.sh can be used for anything you want.
  169. if [ -f "../../resources/coreboot/${board}/extra.sh" ]; then
  170. "../../resources/coreboot/${board}/extra.sh" || touch ../../build_error
  171. if [ -f ../../build_error ]; then
  172. cd ../../; return 1
  173. fi
  174. return 0
  175. else
  176. cd ../../
  177. return 0
  178. fi
  179. }
  180. printf "Downloading coreboot and (if exist in build system) applying patches\n"
  181. if [ $# -gt 0 ]; then
  182. for board in "${@}"; do
  183. rm -f resources/coreboot/*/seen
  184. downloadfor "${board}"
  185. if [ -f build_error ]; then break; fi
  186. done
  187. else
  188. for board in resources/coreboot/*; do
  189. rm -f resources/coreboot/*/seen
  190. if [ ! -d "${board}/" ]; then
  191. continue
  192. fi
  193. downloadfor "${board##*/}"
  194. if [ -f build_error ]; then break; fi
  195. done
  196. fi
  197. rm -f resources/coreboot/*/seen
  198. rm -f "build_error"
  199. printf "\n\n"
  200. exit 0