libreboot-release 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/bin/bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. BUILD_SYSTEM="paper"
  17. SOURCES="sources"
  18. SYSTEMS="systems"
  19. IMAGES="images"
  20. TOOLS="tools"
  21. TAR_XZ="tar.xz"
  22. SHA256SUM="sha256sum"
  23. ASC="asc"
  24. usage() {
  25. printf "$executable [action] [projects...]\n" >&2
  26. printf "\nActions:\n" >&2
  27. printf " download - Download project files\n" >&2
  28. printf " sources - Download project sources\n" >&2
  29. printf " verify - Verify project files\n" >&2
  30. printf " extract - Extract project files\n" >&2
  31. printf " prepare - Download, verify and extract project files\n" >&2
  32. printf "\nEnvironment variables:\n" >&2
  33. printf " MACHINE - Machine architecture to use\n" >&2
  34. printf " DOWNLOAD_URL - Base URL to download files from\n" >&2
  35. }
  36. download() {
  37. local project=$1
  38. local ifs_save
  39. local prefix
  40. local directory
  41. local path
  42. local url
  43. for prefix in "$SYSTEMS/$MACHINE" "$IMAGES" "$TOOLS/$MACHINE"
  44. do
  45. ifs_save=$IFS
  46. IFS=$'-'
  47. directory=""
  48. for part in $project
  49. do
  50. if [ -z "$directory" ]
  51. then
  52. directory="$part"
  53. else
  54. directory="$directory-$part"
  55. fi
  56. path="$root/$prefix/$directory/$project.$TAR_XZ"
  57. url="$DOWNLOAD_URL/$prefix/$directory/$project.$TAR_XZ"
  58. if wget --quiet --spider "$url"
  59. then
  60. mkdir -p "$( dirname "$path" )"
  61. wget -O "$path" "$url"
  62. wget -O "$path.$SHA256SUM" "$url.$SHA256SUM"
  63. wget -O "$path.$ASC" "$url.$ASC"
  64. printf "\nDownloaded $project\n"
  65. return 0
  66. fi
  67. done
  68. IFS=$ifs_save
  69. done
  70. printf "Could not download $project from $DOWNLOAD_URL\n" >&2
  71. return 1
  72. }
  73. sources() {
  74. local project=$1
  75. local url="$DOWNLOAD_URL/$SOURCES/$project/$project.$TAR_XZ"
  76. local path="$root/$SOURCES/$project/$project.$TAR_XZ"
  77. if wget --quiet --spider "$url"
  78. then
  79. mkdir -p "$( dirname "$path" )"
  80. wget -O "$path" "$url"
  81. wget -O "$path.$SHA256SUM" "$url.$SHA256SUM"
  82. wget -O "$path.$ASC" "$url.$ASC"
  83. printf "\nDownloaded $project sources\n"
  84. else
  85. printf "Could not download $project sources from $DOWNLOAD_URL\n" >&2
  86. return 1
  87. fi
  88. }
  89. verify() {
  90. local project=$1
  91. local checksum_path
  92. local signature_path
  93. local ifs_save
  94. local prefix
  95. local directory
  96. local path
  97. for prefix in "$SYSTEMS/$MACHINE" "$IMAGES" "$TOOLS/$MACHINE"
  98. do
  99. ifs_save=$IFS
  100. IFS=$'-'
  101. directory=""
  102. for part in $project
  103. do
  104. if [ -z "$directory" ]
  105. then
  106. directory="$part"
  107. else
  108. directory="$directory-$part"
  109. fi
  110. path="$root/$prefix/$directory/$project.$TAR_XZ"
  111. if ! [ -f "$path" ]
  112. then
  113. continue
  114. fi
  115. checksum_path="$path.$SHA256SUM"
  116. signature_path="$path.$ASC"
  117. if [ -f "$checksum_path" ]
  118. then
  119. (
  120. cd "$( dirname "$path" )"
  121. sha256sum -c "$project.$TAR_XZ.$SHA256SUM"
  122. )
  123. else
  124. printf "Could not verify $project checksum!\n" >&2
  125. fi
  126. if [ -f "$signature_path" ]
  127. then
  128. gpg --armor --verify "$signature_path" "$path"
  129. else
  130. printf "Could not verify $project signature!\n" >&2
  131. fi
  132. printf "\nVerified $project\n"
  133. return 0
  134. done
  135. IFS=$ifs_save
  136. done
  137. printf "Could not verify $project\n" >&2
  138. return 1
  139. }
  140. extract() {
  141. local project=$1
  142. local extract_path
  143. local ifs_save
  144. local prefix
  145. local directory
  146. local path
  147. for prefix in "$SYSTEMS/$MACHINE" "$IMAGES" "$TOOLS/$MACHINE"
  148. do
  149. ifs_save=$IFS
  150. IFS=$'-'
  151. directory=""
  152. for part in $project
  153. do
  154. if [ -z "$directory" ]
  155. then
  156. directory="$part"
  157. else
  158. directory="$directory-$part"
  159. fi
  160. path="$root/$prefix/$directory/$project.$TAR_XZ"
  161. if ! [ -f "$path" ]
  162. then
  163. continue
  164. fi
  165. if [ "$prefix" = "$SYSTEMS/$MACHINE" ]
  166. then
  167. printf "Skiping $project extract\n"
  168. return 0
  169. fi
  170. extract_path=$( dirname "$path" )
  171. tar -xf "$path" -ps -C "$extract_path"
  172. printf "Extracted $project\n"
  173. return 0
  174. done
  175. IFS=$ifs_save
  176. done
  177. printf "Could not extract $project\n" >&2
  178. return 1
  179. }
  180. requirements() {
  181. local requirement
  182. local requirement_path
  183. for requirement in "$@"
  184. do
  185. requirement_path=$( which "$requirement" || true )
  186. if [ -z "$requirement_path" ]
  187. then
  188. printf "Missing requirement: $requirement\n" >&2
  189. exit 1
  190. fi
  191. done
  192. }
  193. setup() {
  194. root=$( realpath "$( dirname "$0" )" )
  195. executable=$( basename "$0" )
  196. if [ -z "$MACHINE" ]
  197. then
  198. MACHINE=$( uname -m )
  199. fi
  200. if [ -z "$DOWNLOAD_URL" ]
  201. then
  202. printf "Missing download URL\n" >&2
  203. exit 1
  204. fi
  205. }
  206. libreboot_release() {
  207. local action=$1
  208. shift
  209. set -e
  210. setup "$@"
  211. if [ -z "$action" ]
  212. then
  213. usage
  214. exit 1
  215. fi
  216. requirements "tar" "sha256sum" "gpg"
  217. case $action in
  218. "download")
  219. for project in "$@"
  220. do
  221. download "$project"
  222. done
  223. ;;
  224. "sources")
  225. for project in "$@"
  226. do
  227. sources "$project"
  228. done
  229. ;;
  230. "verify")
  231. for project in "$@"
  232. do
  233. verify "$project"
  234. done
  235. ;;
  236. "extract")
  237. for project in "$@"
  238. do
  239. extract "$project"
  240. done
  241. ;;
  242. "prepare")
  243. for project in "$@"
  244. do
  245. download "$project"
  246. verify "$project"
  247. extract "$project"
  248. done
  249. ;;
  250. esac
  251. }
  252. libreboot_release "$@"