u-boot 6.7 KB

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