coreboot 6.6 KB

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