u-boot 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #!/usr/bin/env bash
  2. # helper script: download u-boot
  3. #
  4. # Copyright (C) 2014, 2015, 2016, 2020, 2021 Leah Rowe <info@minifree.org>
  5. # Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  6. # Copyright (C) 2022 Alper Nebi Yasak <alpernebiyasak@gmail.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. # set this when you want to modify each u-boot tree
  24. # for example, you want to test custom patches
  25. # NODELETE= ./download u-boot
  26. deletegit="true"
  27. deleteblobs="true"
  28. if [ "x${NODELETE+set}" = 'xset' ]; then
  29. [ "x${NODELETE:-all}" = "xgit" ] && deletegit="false"
  30. [ "x${NODELETE:-all}" = "xall" ] && deleteblobs="false" && deletegit="false"
  31. fi
  32. # Error handling is extreme in this script.
  33. # This script handles the internet, and Git. Both are inherently unreliable.
  34. [[ -f build_error ]] && rm -f build_error
  35. list_supported_boards() {
  36. for board in resources/u-boot/*; do
  37. if [ -d ${board} ]; then
  38. echo "${board#resources/u-boot/}"
  39. fi
  40. done
  41. }
  42. downloadfor() {
  43. board="${1}"
  44. # The loop will always exit, but this while loop is crafted
  45. # such that a tree referencing a tree that references another tree is possible
  46. # (and so on)
  47. while true; do
  48. ubrevision="undefined"
  49. ubtree="undefined"
  50. if [ ! -f "resources/u-boot/${board}/board.cfg" ]; then
  51. printf "ERROR: %s: board.cfg does not exist for '%s'\n" \
  52. "download/u-boot" "${board}"
  53. return 1
  54. fi
  55. if [ -f "resources/u-boot/${board}/seen" ]; then
  56. printf "ERROR: %s: logical loop; '%s' board.cfg refers to another tree, which ultimately refers back to '%s'.\n" \
  57. "download/u-boot" "${board}" "${board}"
  58. return 1
  59. fi
  60. # This is to override $ubrevision and $ubtree
  61. source "resources/u-boot/${board}/board.cfg" || touch build_error
  62. if [ -f build_error ]; then
  63. printf "ERROR: %s: problem sourcing %s/board.cfg\n" \
  64. "download/u-boot" "${board}"
  65. return 1
  66. fi
  67. touch "resources/u-boot/${board}/seen"
  68. if [ "${board}" != "${ubtree}" ]; then
  69. board="${ubtree}"
  70. else
  71. if [ "${ubtree}" = "undefined" ]; then
  72. printf "ERROR: %s: tree name undefined for '%s\n'" \
  73. "download/u-boot" "${board}"
  74. return 1
  75. fi
  76. if [ "${ubrevision}" = "undefined" ]; then
  77. printf "ERROR: %s: commit ID undefined for '%s'\n" \
  78. "download/u-boot" "${board}"
  79. return 1
  80. fi
  81. break
  82. fi
  83. done
  84. rm -f resources/u-boot/*/seen
  85. ubtree="u-boot/${ubtree}"
  86. if [ -d "${ubtree}" ]; then
  87. printf \
  88. "REMARK: '%s' directory already exists. Skipping setup.\n" \
  89. "${ubtree}"
  90. if [ "${ubtree}" != "u-boot/${board}" ]; then
  91. printf "(for board: '${board}')\n"
  92. fi
  93. return 0
  94. fi
  95. if [ ! -d "u-boot" ]; then
  96. mkdir -p "u-boot"
  97. fi
  98. if [ ! -d "u-boot" ]; then
  99. printf \
  100. "ERROR: '%s' directory not created. Check file system permissions\n" \
  101. "u-boot"
  102. return 1
  103. fi
  104. uboot_dir="u-boot/u-boot"
  105. if [ ! -d "${uboot_dir}/.git" ] && [ -d "${uboot_dir}" ]; then
  106. rm -Rf "${uboot_dir}"
  107. fi
  108. if [ ! -d "${uboot_dir}" ]; then
  109. printf "Download u-boot from upstream:\n"
  110. git clone --depth=1 https://source.denx.de/u-boot/u-boot.git \
  111. "${uboot_dir}" || \
  112. rm -Rf "${uboot_dir}"
  113. if [ ! -d "${uboot_dir}" ]; then
  114. printf "WARNING: Upstream failed. Trying backup github repository:\n"
  115. git clone --depth=1 https://github.com/u-boot/u-boot.git \
  116. "${uboot_dir}" || \
  117. rm -Rf coreboot
  118. fi
  119. if [ ! -d "${uboot_dir}" ]; then
  120. printf \
  121. "ERROR: %s: Problem with git-clone. Network issue?\n" \
  122. "download/u-boot"
  123. return 1
  124. fi
  125. fi
  126. git -C "${uboot_dir}" fetch --depth=1 origin "${ubrevision}" || touch build_error
  127. if [ -f build_error ]; then
  128. printf \
  129. "ERROR: %s: Problem with git-fetch. Network issue?\n" \
  130. "download/u-boot"
  131. return 1
  132. fi
  133. cp -R "${uboot_dir}" "${ubtree}" || touch build_error
  134. if [ -f build_error ]; then
  135. printf "ERROR: %s: Unable to copy directory. Check file system permissions or free space.\n" \
  136. "download/u-boot"
  137. rm -Rf "${ubtree}/"
  138. return 1
  139. fi
  140. git -C "${ubtree}" reset --hard ${ubrevision} || \
  141. touch build_error
  142. if [ -f build_error ]; then
  143. printf \
  144. "ERROR: %s: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" \
  145. "download/u-boot" "${ubrevision}" "${board}" "${ubtree}"
  146. return 1
  147. fi
  148. git -C "${ubtree}" submodule update --init --depth=1 || touch build_error
  149. if [ -f build_error ]; then
  150. printf "ERROR: %s: Unable to update submodules for tree '%s'\n" \
  151. "${ubtree}"
  152. return 1
  153. fi
  154. for patch in resources/u-boot/${board}/patches/*.patch; do
  155. if [ ! -f "${patch}" ]; then
  156. continue
  157. fi
  158. git -C "${ubtree}" am "$(pwd)/${patch}" || touch build_error
  159. if [ -f build_error ]; then
  160. printf "ERROR: %s: Unable to apply patch '%s' for board '%s' on tree '%s'" \
  161. "download/u-boot" "${patch}" "${board}" "${ubtree}"
  162. git -C "${ubtree}" am --abort
  163. return 1
  164. fi
  165. done
  166. # extra.sh could be used to patch submodules, if you wanted to
  167. # It's impossible to predict what submodules will be available, and
  168. # it's rare that you'd want to patch them, so this is handled by
  169. # extra.sh on a per-board basis
  170. # In fact, extra.sh can be used for anything you want.
  171. if [ -f "resources/u-boot/${board}/extra.sh" ]; then
  172. "resources/u-boot/${board}/extra.sh" || touch build_error
  173. if [ -f build_error ]; then
  174. return 1
  175. fi
  176. return 0
  177. else
  178. return 0
  179. fi
  180. }
  181. strip_comments()
  182. {
  183. file="$1"
  184. # Remove comments
  185. sed 's/#.*//' "${file}" | \
  186. # Remove lines composed of whitespaces only
  187. sed '/^\W\+$/d' | \
  188. # Remove empty lines
  189. sed '/^$/d'
  190. }
  191. generate_deblob_script()
  192. {
  193. blob_list_file="$1"
  194. output="$2"
  195. # Add sheebang and copyright headers from this file
  196. awk '{
  197. if ($0 !~ /^#/ && NF > 0) {
  198. stop=1;
  199. }
  200. if (stop !=1) {
  201. printf("%s\n", $0);
  202. }
  203. }' $0 >> ${output}
  204. # Add rm -rf before directories and rm -f before files
  205. awk \
  206. '{
  207. }{
  208. if (NF == 0) {
  209. printf("\n");
  210. } else {
  211. if ($0 !~ /#/ && $NF ~ /\/$/) {
  212. printf("rm -rf %s\n", $0);
  213. } else if ($0 !~ /#/) {
  214. printf("rm -f %s\n", $0);
  215. } else {
  216. # We have comments, try to see if they are
  217. # still valid paths before the comments
  218. comments_found=0
  219. last_field=0
  220. for(i=1; i<=NF; i++) {
  221. if($i ~ /#/) {
  222. comments_found=1;
  223. } else if(comments_found != 1) {
  224. last_field=i;
  225. }
  226. }
  227. if (last_field == 0) {
  228. printf("%s\n", $0);
  229. } else if ($last_field ~ /\/$/) {
  230. printf("rm -rf %s\n", $0);
  231. } else {
  232. printf("rm -f %s\n", $0);
  233. }
  234. }
  235. }
  236. }' "${blob_list_file}" >> "${output}"
  237. }
  238. usage()
  239. {
  240. progname="./download u-boot"
  241. printf "Usage:\n"
  242. printf "\t%s # %s\n" \
  243. "${progname}" \
  244. "Download and deblob u-boot for all boards"
  245. printf "\t%s [board] # %s\n" \
  246. "${progname}" \
  247. "Download and deblob u-boot for the given board"
  248. printf "\t%s --blobs-list # %s\n" \
  249. "${progname}" \
  250. "Print the path of the generic blobs.list file"
  251. printf "\t%s --blobs-list [board] # %s\n" \
  252. "${progname}" \
  253. "Print the path of the blobs.list file for the given board"
  254. printf "\t%s --gen-deblob-script # %s\n" \
  255. "${progname}" \
  256. "Print the path of a generated generic deblob script"
  257. printf "\t%s --gen-deblob-script [board] # %s\n" \
  258. "${progname}" \
  259. "Print the path of a generated deblob script for the given board"
  260. printf "\t%s --list-boards # %s\n" \
  261. "${progname}" \
  262. "List supported boards"
  263. printf "\t%s --help # %s\n" \
  264. "${progname}" \
  265. "Prints this help"
  266. }
  267. download_uboot_board()
  268. {
  269. board="${1}"
  270. ubtree="u-boot/${board}"
  271. printf "Downloading u-boot "
  272. printf "and (if exist in build system) applying patches\n"
  273. downloadfor "${board}"
  274. rm -f "build_error"
  275. printf "\n\n"
  276. if [ "${deleteblobs}" = "true" ]; then
  277. if [ "${deletegit}" = "true" ]; then
  278. rm -rf "${ubtree}"/.git* "${ubtree}"/*/.git*
  279. fi
  280. blobslist="$(print_blobs_list_path "${board}")"
  281. for blob_path in $(strip_comments "${blobslist}"); do
  282. if echo "${blob_path}" | \
  283. grep '/$' 2>&1 >/dev/null ; then
  284. printf "Deleting blob directory: '%s/%s'\n" \
  285. "${ubtree}" "${blob_path}"
  286. rm -rf "${ubtree}/${blob_path}"
  287. else
  288. printf "Deleting blob file: '%s/%s'\n" \
  289. "${ubtree}" "${blob_path}"
  290. rm -f "${ubtree}/${blob_path}"
  291. fi
  292. done
  293. fi
  294. }
  295. print_blobs_list_path()
  296. {
  297. board="$1"
  298. if [ -f "resources/u-boot/${board}/blobs.list" ]; then
  299. printf "resources/u-boot/${board}/blobs.list\n"
  300. else
  301. printf "resources/u-boot/default/blobs.list\n"
  302. fi
  303. }
  304. print_deblob_script_path()
  305. {
  306. board="$1"
  307. path="$(mktemp)"
  308. generate_deblob_script "$(print_blobs_list_path ${board})" "${path}"
  309. printf "%s\n" ${path}
  310. }
  311. if [ $# -eq 0 ] ; then
  312. for board in $(list_supported_boards); do
  313. download_uboot_board "${board}"
  314. done
  315. if [ "${deletegit}" = "true" ]; then
  316. rm -rf u-boot/u-boot/ u-boot/.git*
  317. fi
  318. exit 0
  319. elif [ $# -eq 1 -a "$1" == "--help" ] ; then
  320. usage
  321. exit 0
  322. elif [ $# -eq 1 -a "$1" == "--list-boards" ] ; then
  323. list_supported_boards
  324. exit 0
  325. elif [ $# -eq 1 -a "$1" == "--blobs-list" ] ; then
  326. print_blobs_list_path "default"
  327. exit 0
  328. elif [ $# -eq 2 -a "$1" == "--blobs-list" ] ; then
  329. found=0
  330. for board in $(list_supported_boards) ; do
  331. if [ "${board}" = "$2" ] ; then
  332. print_blobs_list_path "$2"
  333. exit 0
  334. fi
  335. done
  336. printf "Error: Board '${2}' is not supported\n"
  337. exit 1
  338. elif [ $# -eq 1 -a "$1" == "--gen-deblob-script" ] ; then
  339. print_deblob_script_path "default"
  340. exit 0
  341. elif [ $# -eq 2 -a "$1" == "--gen-deblob-script" ] ; then
  342. found=0
  343. for board in $(list_supported_boards) ; do
  344. if [ "$board" = "$2" ] ; then
  345. print_deblob_script_path "$2"
  346. exit 0
  347. fi
  348. done
  349. printf "Error: Board '${2}' is not supported\n"
  350. exit 1
  351. elif [ $# -eq 1 ] ; then
  352. for board in $(list_supported_boards) ; do
  353. if [ "$board" = "$1" ] ; then
  354. download_uboot_board "$1"
  355. exit 0
  356. fi
  357. done
  358. if [ "${deletegit}" = "true" ]; then
  359. rm -rf u-boot/u-boot/ u-boot/.git*
  360. fi
  361. printf "Error: Board '${1}' is not supported\n"
  362. exit 1
  363. fi
  364. exit 0