coreboot 6.3 KB

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