u-boot 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env bash
  2. # helper script: download u-boot
  3. #
  4. # Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.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. [[ -f build_error ]] && rm -f build_error
  23. list_supported_boards() {
  24. for board in resources/u-boot/*; do
  25. if [ -d ${board} ]; then
  26. echo "${board#resources/u-boot/}"
  27. fi
  28. done
  29. }
  30. downloadfor() {
  31. board="${1}"
  32. # The loop will always exit, but this while loop is crafted
  33. # such that a tree referencing a tree that references another tree is possible
  34. # (and so on)
  35. while true; do
  36. ubrevision="undefined"
  37. ubtree="undefined"
  38. if [ ! -f "resources/u-boot/${board}/board.cfg" ]; then
  39. printf "ERROR: %s: board.cfg does not exist for '%s'\n" \
  40. "download/u-boot" "${board}"
  41. return 1
  42. fi
  43. if [ -f "resources/u-boot/${board}/seen" ]; then
  44. printf "ERROR: %s: logical loop; '%s' board.cfg refers to another tree, which ultimately refers back to '%s'.\n" \
  45. "download/u-boot" "${board}" "${board}"
  46. return 1
  47. fi
  48. # This is to override $ubrevision and $ubtree
  49. source "resources/u-boot/${board}/board.cfg" || touch build_error
  50. if [ -f build_error ]; then
  51. printf "ERROR: %s: problem sourcing %s/board.cfg\n" \
  52. "download/u-boot" "${board}"
  53. return 1
  54. fi
  55. touch "resources/u-boot/${board}/seen"
  56. if [ "${board}" != "${ubtree}" ]; then
  57. board="${ubtree}"
  58. else
  59. if [ "${ubtree}" = "undefined" ]; then
  60. printf "ERROR: %s: tree name undefined for '%s\n'" \
  61. "download/u-boot" "${board}"
  62. return 1
  63. fi
  64. if [ "${ubrevision}" = "undefined" ]; then
  65. printf "ERROR: %s: commit ID undefined for '%s'\n" \
  66. "download/u-boot" "${board}"
  67. return 1
  68. fi
  69. break
  70. fi
  71. done
  72. rm -f resources/u-boot/*/seen
  73. ubtree="u-boot/${ubtree}"
  74. if [ -d "${ubtree}" ]; then
  75. printf \
  76. "REMARK: '%s' directory already exists. Skipping setup.\n" \
  77. "${ubtree}"
  78. if [ "${ubtree}" != "u-boot/${board}" ]; then
  79. printf "(for board: '${board}')\n"
  80. fi
  81. return 0
  82. fi
  83. if [ ! -d "u-boot" ]; then
  84. mkdir -p "u-boot"
  85. fi
  86. if [ ! -d "u-boot" ]; then
  87. printf \
  88. "ERROR: '%s' directory not created. Check file system permissions\n" \
  89. "u-boot"
  90. return 1
  91. fi
  92. uboot_dir="u-boot/u-boot"
  93. if [ ! -d "${uboot_dir}/.git" ] && [ -d "${uboot_dir}" ]; then
  94. rm -Rf "${uboot_dir}"
  95. fi
  96. if [ ! -d "${uboot_dir}" ]; then
  97. printf "Download u-boot from upstream:\n"
  98. git clone https://source.denx.de/u-boot/u-boot.git \
  99. "${uboot_dir}" || \
  100. rm -Rf "${uboot_dir}"
  101. if [ ! -d "${uboot_dir}" ]; then
  102. printf "WARNING: Upstream failed. Trying backup github repository:\n"
  103. git clone https://github.com/u-boot/u-boot.git \
  104. "${uboot_dir}" || \
  105. rm -Rf coreboot
  106. fi
  107. if [ ! -d "${uboot_dir}" ]; then
  108. printf \
  109. "ERROR: %s: Problem with git-clone. Network issue?\n" \
  110. "download/u-boot"
  111. return 1
  112. fi
  113. fi
  114. git -C "${uboot_dir}" fetch origin "${ubrevision}" || touch build_error
  115. if [ -f build_error ]; then
  116. printf \
  117. "ERROR: %s: Problem with git-fetch. Network issue?\n" \
  118. "download/u-boot"
  119. return 1
  120. fi
  121. cp -R "${uboot_dir}" "${ubtree}" || touch build_error
  122. if [ -f build_error ]; then
  123. printf "ERROR: %s: Unable to copy directory. Check file system permissions or free space.\n" \
  124. "download/u-boot"
  125. rm -Rf "${ubtree}/"
  126. return 1
  127. fi
  128. git -C "${ubtree}" reset --hard ${ubrevision} || \
  129. touch build_error
  130. if [ -f build_error ]; then
  131. printf \
  132. "ERROR: %s: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" \
  133. "download/u-boot" "${ubrevision}" "${board}" "${ubtree}"
  134. return 1
  135. fi
  136. git -C "${ubtree}" submodule update --init || touch build_error
  137. if [ -f build_error ]; then
  138. printf "ERROR: %s: Unable to update submodules for tree '%s'\n" \
  139. "${ubtree}"
  140. return 1
  141. fi
  142. for patch in resources/u-boot/${board}/patches/*.patch; do
  143. if [ ! -f "${patch}" ]; then
  144. continue
  145. fi
  146. git -C "${ubtree}" am "$(pwd)/${patch}" || touch build_error
  147. if [ -f build_error ]; then
  148. printf "ERROR: %s: Unable to apply patch '%s' for board '%s' on tree '%s'" \
  149. "download/u-boot" "${patch}" "${board}" "${ubtree}"
  150. git -C "${ubtree}" am --abort
  151. return 1
  152. fi
  153. done
  154. # extra.sh could be used to patch submodules, if you wanted to
  155. # It's impossible to predict what submodules will be available, and
  156. # it's rare that you'd want to patch them, so this is handled by
  157. # extra.sh on a per-board basis
  158. # In fact, extra.sh can be used for anything you want.
  159. if [ -f "resources/u-boot/${board}/extra.sh" ]; then
  160. "resources/u-boot/${board}/extra.sh" || touch build_error
  161. if [ -f build_error ]; then
  162. return 1
  163. fi
  164. return 0
  165. else
  166. return 0
  167. fi
  168. }
  169. strip_comments()
  170. {
  171. file="$1"
  172. # Remove comments
  173. sed 's/#.*//' "${file}" | \
  174. # Remove lines composed of whitespaces only
  175. sed '/^\W\+$/d' | \
  176. # Remove empty lines
  177. sed '/^$/d'
  178. }
  179. usage()
  180. {
  181. progname="./download u-boot"
  182. printf "Usage:\n"
  183. printf "\t%s [board] # %s\n" \
  184. "${progname}" \
  185. "Download u-boot for the given board"
  186. printf "\t%s --list-boards # %s\n" \
  187. "${progname}" \
  188. "List supported boards"
  189. printf "\t%s --help # %s\n" \
  190. "${progname}" \
  191. "Prints this help"
  192. }
  193. download_uboot_board()
  194. {
  195. board="${1}"
  196. ubtree="u-boot/${board}"
  197. printf "Downloading u-boot "
  198. printf "and (if exist in build system) applying patches\n"
  199. downloadfor "${board}"
  200. rm -f "build_error"
  201. printf "\n\n"
  202. }
  203. if [ $# -eq 0 ] ; then
  204. for board in $(list_supported_boards); do
  205. download_uboot_board "${board}"
  206. done
  207. if [ "${deletegit}" = "true" ]; then
  208. rm -rf u-boot/u-boot/ u-boot/.git*
  209. fi
  210. exit 0
  211. elif [ $# -eq 1 -a "$1" == "--help" ] ; then
  212. usage
  213. exit 0
  214. elif [ $# -eq 1 -a "$1" == "--list-boards" ] ; then
  215. list_supported_boards
  216. exit 0
  217. elif [ $# -eq 1 ] ; then
  218. for board in $(list_supported_boards) ; do
  219. if [ "$board" = "$1" ] ; then
  220. download_uboot_board "$1"
  221. exit 0
  222. fi
  223. done
  224. if [ "${deletegit}" = "true" ]; then
  225. rm -rf u-boot/u-boot/ u-boot/.git*
  226. fi
  227. printf "Error: Board '${1}' is not supported\n"
  228. exit 1
  229. fi
  230. exit 0