u-boot 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #
  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. # set this when you want to modify each u-boot tree
  23. # for example, you want to test custom patches
  24. # NODELETE= ./download coreboot
  25. deleteblobs="true"
  26. [ "x${NODELETE+set}" = 'xset' ] && deleteblobs="false"
  27. # Error handling is extreme in this script.
  28. # This script handles the internet, and Git. Both are inherently unreliable.
  29. [[ -f build_error ]] && rm -f build_error
  30. # Make sure that older revision are first as code uses that order to
  31. # find the latest supported revision.
  32. supported_uboot_revisions=" \
  33. v2021.07 \
  34. "
  35. downloadfor() {
  36. uboot_revision="v2021.07"
  37. uboot_dir="u-boot/u-boot"
  38. if [ -d "${uboot_dir}" ]; then
  39. printf \
  40. "REMARK: '%s' directory already exists. Skipping setup.\n" \
  41. "${uboot_dir}"
  42. return 0
  43. fi
  44. if [ ! -d "${uboot_dir}" ]; then
  45. mkdir -p "${uboot_dir}"
  46. fi
  47. if [ ! -d "${uboot_dir}" ]; then
  48. printf \
  49. "ERROR: '%s' directory not created. Check file system permissions\n" \
  50. "${uboot_dir}"
  51. return 1
  52. fi
  53. if [ ! -d "${uboot_dir}/.git" ] && [ -d "${uboot_dir}" ]; then
  54. rm -Rf "${uboot_dir}"
  55. fi
  56. if [ ! -d "${uboot_dir}" ]; then
  57. printf "Download u-boot from upstream:\n"
  58. git clone https://source.denx.de/u-boot/u-boot.git \
  59. "${uboot_dir}" || \
  60. rm -Rf "${uboot_dir}"
  61. if [ ! -d "${uboot_dir}" ]; then
  62. printf \
  63. "ERROR: %s: Problem with git-clone. Network issue?\n" \
  64. "download/u-boot"
  65. return 1
  66. fi
  67. else
  68. git -C "${uboot_dir}" pull || touch build_error
  69. if [ -f build_error ]; then
  70. printf \
  71. "ERROR: %s: Problem with git-pull. Network issue?\n" \
  72. "download/u-boot"
  73. return 1
  74. fi
  75. fi
  76. git -C "${uboot_dir}" reset --hard ${uboot_revision} || \
  77. touch build_error
  78. if [ -f build_error ]; then
  79. printf \
  80. "ERROR: %s: Unable to reset to commit ID/tag '%s' for board '%s' on tree '%s'\n" \
  81. "download/u-boot" "${uboot_revision}" "${1}" "${uboot_dir}"
  82. return 1
  83. fi
  84. }
  85. strip_comments()
  86. {
  87. file="$1"
  88. # Remove comments
  89. sed 's/#.*//' "${file}" | \
  90. # Remove lines composed of whitespaces only
  91. sed '/^\W\+$/d' | \
  92. # Remove empty lines
  93. sed '/^$/d'
  94. }
  95. generate_deblob_script()
  96. {
  97. blob_list_file="$1"
  98. output="$2"
  99. # Add sheebang and copyright headers from this file
  100. awk '{
  101. if ($0 !~ /^#/ && NF > 0) {
  102. stop=1;
  103. }
  104. if (stop !=1) {
  105. printf("%s\n", $0);
  106. }
  107. }' $0 >> ${output}
  108. # Add rm -rf before directories and rm -f before files
  109. awk \
  110. '{
  111. }{
  112. if (NF == 0) {
  113. printf("\n");
  114. } else {
  115. if ($0 !~ /#/ && $NF ~ /\/$/) {
  116. printf("rm -rf %s\n", $0);
  117. } else if ($0 !~ /#/) {
  118. printf("rm -f %s\n", $0);
  119. } else {
  120. # We have comments, try to see if they are
  121. # still valid paths before the comments
  122. comments_found=0
  123. last_field=0
  124. for(i=1; i<=NF; i++) {
  125. if($i ~ /#/) {
  126. comments_found=1;
  127. } else if(comments_found != 1) {
  128. last_field=i;
  129. }
  130. }
  131. if (last_field == 0) {
  132. printf("%s\n", $0);
  133. } else if ($last_field ~ /\/$/) {
  134. printf("rm -rf %s\n", $0);
  135. } else {
  136. printf("rm -f %s\n", $0);
  137. }
  138. }
  139. }
  140. }' "${blob_list_file}" >> "${output}"
  141. }
  142. usage()
  143. {
  144. progname="./download u-boot"
  145. printf "Usage:\n"
  146. printf "\t%s # %s\n" \
  147. "${progname}" \
  148. "Download latest u-boot git revision and deblob it"
  149. printf "\t%s [revision] # %s\n" \
  150. "${progname}" \
  151. "Download given u-boot git revision and deblob it"
  152. printf "\t%s --blobs-list # %s\n" \
  153. "${progname}" \
  154. "Print the path of the blobs.list file for the latest supported u-boot revision"
  155. printf "\t%s --blobs-list [revision] # %s\n" \
  156. "${progname}" \
  157. "Print the path of the blobs.list file for the given u-boot revision"
  158. printf "\t%s --gen-deblob-script # %s\n" \
  159. "${progname}" \
  160. "Print the path of the generated deblob script for the latest supported u-boot revision"
  161. printf "\t%s --gen-deblob-script [revision] # %s\n" \
  162. "${progname}" \
  163. "Print the path of the generated deblob script for the given u-boot revision"
  164. printf "\t%s --list-revisions # %s\n" \
  165. "${progname}" \
  166. "List supported u-boot revisions"
  167. printf "\t%s --help # %s\n" \
  168. "${progname}" \
  169. "Prints this help"
  170. }
  171. download_uboot_revision()
  172. {
  173. git_revision="$1"
  174. printf "Downloading u-boot "
  175. printf "and (if exist in build system) applying patches\n"
  176. downloadfor "${git_revision}"
  177. rm -f "build_error"
  178. printf "\n\n"
  179. if [ "${deleteblobs}" = "true" ]; then
  180. blobslist="resources/u-boot/default/blobs.list"
  181. for blob_path in $(strip_comments "${blobslist}"); do
  182. if echo "${blob_path}" | \
  183. grep '/$' 2>&1 >/dev/null ; then
  184. printf "Deleting blob directory: '%s/%s'\n" \
  185. "${uboot_dir}" "${blob_path}"
  186. rm -rf "${uboot_dir}/${blob_path}"
  187. else
  188. printf "Deleting blob file: '%s/%s'\n" \
  189. "${uboot_dir}" "${blob_path}"
  190. rm -f "${uboot_dir}/${blob_path}"
  191. fi
  192. done
  193. fi
  194. }
  195. print_blobs_list_path()
  196. {
  197. printf "resources/u-boot/default/blobs.list\n"
  198. }
  199. print_deblob_script_path()
  200. {
  201. version="$1"
  202. path="$(mktemp)"
  203. generate_deblob_script "$(print_blobs_list_path ${version})" "${path}"
  204. printf "%s\n" ${path}
  205. }
  206. if [ $# -eq 0 ] ; then
  207. latest_revision="$(echo ${supported_uboot_revisions} | tail -n1)"
  208. download_uboot_revision "${latest_revision}"
  209. exit 0
  210. elif [ $# -eq 1 -a "$1" == "--help" ] ; then
  211. usage
  212. exit 0
  213. elif [ $# -eq 1 -a "$1" == "--list-revisions" ] ; then
  214. for revision in ${supported_uboot_revisions} ; do
  215. printf "${revision}\n"
  216. done
  217. exit 0
  218. elif [ $# -eq 1 -a "$1" == "--blobs-list" ] ; then
  219. latest_revision="$(echo ${supported_uboot_revisions} | tail -n1)"
  220. print_blobs_list_path "${latest_revision}"
  221. exit 0
  222. elif [ $# -eq 2 -a "$1" == "--blobs-list" ] ; then
  223. found=0
  224. for revision in ${supported_uboot_revisions} ; do
  225. if [ "${revision}" = "$2" ] ; then
  226. print_blobs_list_path "$2"
  227. exit 0
  228. fi
  229. done
  230. printf "Error: Revision '${1}' is not supported\n"
  231. exit 1
  232. elif [ $# -eq 1 -a "$1" == "--gen-deblob-script" ] ; then
  233. latest_revision="$(echo ${supported_uboot_revisions} | tail -n1)"
  234. print_deblob_script_path "${latest_revision}"
  235. exit 0
  236. elif [ $# -eq 2 -a "$1" == "--gen-deblob-script" ] ; then
  237. found=0
  238. for revision in ${supported_uboot_revisions} ; do
  239. if [ "${revision}" = "$2" ] ; then
  240. print_deblob_script_path "$2"
  241. exit 0
  242. fi
  243. done
  244. printf "Error: Revision '${1}' is not supported\n"
  245. exit 1
  246. elif [ $# -eq 1 ] ; then
  247. found=0
  248. for revision in ${supported_uboot_revisions} ; do
  249. if [ "${revision}" = "$1" ] ; then
  250. download_uboot_revision "$1"
  251. exit 0
  252. fi
  253. done
  254. printf "Error: Revision '${1}' is not supported\n"
  255. exit 1
  256. fi
  257. exit 0