libreboot 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. # Copyright (C) 2017 Andrew Robbins <contact@andrewrobbins.info>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. GLOBIGNORE=".:.." # This enables the shell option 'dotglob' as well.
  18. shopt -s nullglob extglob
  19. libreboot_usage() {
  20. local action
  21. local target
  22. printf '%s\n' "$executable [action] [target] [arguments]"
  23. printf '\n%s\n' 'Generic project actions:'
  24. for action in "${PROJECT_ACTIONS_GENERIC[@]}"; do
  25. printf '%s\n' " $action"
  26. done
  27. printf '\n%s\n' 'Virtual project actions:'
  28. printf '%s\n' ' sources'
  29. printf '%s\n' ' produce'
  30. printf '%s\n' ' test'
  31. printf '\n%s\n' 'Project targets:'
  32. for target in "$root/$PROJECTS"/*; do
  33. if project_check "$target"; then
  34. printf '%s\n' " ${target##*/}"
  35. fi
  36. done
  37. printf '\n%s\n' 'Generic tool actions:'
  38. for action in "${TOOL_ACTIONS_GENERIC[@]}"; do
  39. printf '%s\n' " $action"
  40. done
  41. printf '\n%s\n' 'Tool targets:'
  42. for target in "$root/$TOOLS"/*; do
  43. if tool_check "$target"; then
  44. printf '%s\n' " ${target##*/}"
  45. fi
  46. done
  47. printf '\n%s\n' 'Environment variables:'
  48. printf '%s\n' ' PROJECTS_FORCE - Projects to always perform actions for'
  49. printf '%s\n' ' TOOLS_FORCE - Tools to always perform actions for'
  50. printf '%s\n' ' RELEASE_KEY - GPG key to use for release'
  51. printf '%s\n' ' VBOOT_KEYS_PATH - Path to the vboot keys'
  52. printf '%s\n' ' LIBFAKETIME_PATH - Path to libfaketime'
  53. printf '%s\n' ' TASKS - Number of simultaneous tasks to run'
  54. printf '%s\n' ' VERSION - Version string to use'
  55. printf '\n%s\n' 'Configuration files:'
  56. printf '%s\n' " $BUILD_SYSTEM.conf - Environment variables configuration"
  57. }
  58. libreboot_project() {
  59. action="$1"
  60. shift
  61. project="$1"
  62. shift
  63. case "$action" in
  64. 'sources')
  65. if project_action_arguments 'extract' "$project" "$@"; then
  66. return
  67. else
  68. printf 1>&2 '\n%s\n\n' 'Attempting to download instead...'
  69. project_action_arguments 'download' "$project" "$@"
  70. fi
  71. ;;
  72. 'produce')
  73. for action in 'build' 'install' 'release'; do
  74. project_action_arguments "$action" "$project" "$@"
  75. done
  76. ;;
  77. 'test')
  78. for action in "${PROJECT_ACTIONS[@]}"; do
  79. project_action_arguments "$action" "$project" "$@"
  80. done
  81. ;;
  82. *)
  83. if ! project_function_check "$project" "$action"; then
  84. libreboot_usage
  85. exit 1
  86. elif [[ $action == usage ]]; then
  87. project_action_usage "$project" "$@"
  88. else
  89. project_action_arguments "$action" "$project" "$@"
  90. fi
  91. ;;
  92. esac
  93. }
  94. libreboot_tool() {
  95. action="$1"
  96. shift
  97. tool="$1"
  98. shift
  99. if ! tool_function_check "$tool" "$action"; then
  100. libreboot_usage
  101. exit 1
  102. elif [[ "$action" == 'usage' ]]; then
  103. tool_action "$action" "$tool" "$@"
  104. else
  105. tool_action_arguments_recursive "$action" "$tool" "$@"
  106. fi
  107. }
  108. libreboot_setup() {
  109. root="$(readlink -f "$(dirname "$0")")"
  110. executable="$(basename "$0")"
  111. libreboot_setup_include
  112. libreboot_setup_tool_actions
  113. libreboot_setup_project_actions
  114. requirements tar sed gpg sha256sum git mmd mcopy grep mkfs.fat
  115. libreboot_setup_variables
  116. }
  117. libreboot_setup_include() {
  118. local libs_path="$root/libs"
  119. local conf_path
  120. source "$libs_path/project"
  121. source "$libs_path/tool"
  122. source "$libs_path/common"
  123. source "$libs_path/git"
  124. conf_path="$root/$BUILD_SYSTEM.conf"
  125. if [[ -f "$conf_path" ]]; then
  126. source "$conf_path"
  127. fi
  128. }
  129. libreboot_setup_tool_actions() {
  130. local ignore="${TOOL_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  131. local -a tool_actions
  132. for ((i=0, nogeneric=${#TOOL_ACTIONS_GENERIC[@]}; i<nogeneric; ++i)); do
  133. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]}")
  134. if [[ "${TOOL_ACTIONS_GENERIC[i]}" == !(${ignore// /|}) ]]; then
  135. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]/%/_check}")
  136. fi
  137. done
  138. TOOL_ACTIONS=("${TOOL_ACTIONS_HELPERS[@]}" "${tool_actions[@]}")
  139. }
  140. libreboot_setup_project_actions() {
  141. local ignore="${PROJECT_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  142. local -a project_actions
  143. for ((i=0, nogeneric=${#PROJECT_ACTIONS_GENERIC[@]}; i<nogeneric; ++i)); do
  144. project_actions+=("${PROJECT_ACTIONS_GENERIC[i]}")
  145. if [[ "${PROJECT_ACTIONS_GENERIC[i]}" == !(${ignore// /|}) ]]; then
  146. project_actions+=("${PROJECT_ACTIONS_GENERIC[i]/%/_check}")
  147. fi
  148. done
  149. PROJECT_ACTIONS=("${PROJECT_ACTIONS_HELPERS[@]}" "${project_actions[@]}")
  150. }
  151. libreboot_setup_variables() {
  152. local vboot_tools_path="$(project_install_path 'vboot' 'tools')"
  153. local version_path="$root/$DOTVERSION"
  154. if [[ -z "$VERSION" ]]; then
  155. if git_check "$root"; then
  156. VERSION="$BUILD_SYSTEM-$(git_describe "$root" 2> /dev/null || echo 'git')"
  157. elif [[ -f "$version_path" ]]; then
  158. VERSION="$(< "$version_path")"
  159. else
  160. VERSION="$BUILD_SYSTEM"
  161. fi
  162. fi
  163. if [[ -d "$vboot_tools_path/devkeys/" ]]; then
  164. VBOOT_KEYS_PATH="${VBOOT_KEYS_PATH:-$vboot_tools_path/devkeys/}"
  165. fi
  166. libreboot_setup_reproducible_builds_variables
  167. }
  168. libreboot_setup_reproducible_builds_variables() {
  169. local epoch_path="$root/$DOTEPOCH"
  170. local rnd_seed_path="$root/$DOTRNDSEED"
  171. # Used by GCC, e.g., -frandom-seed="$RANDOM_SEED"
  172. if [[ -z "$RANDOM_SEED" ]]; then
  173. if [[ -f "$rnd_seed_path" ]]; then
  174. RANDOM_SEED="$(< "$rnd_seed_path")"
  175. else
  176. RANDOM_SEED="$RANDOM" # True randomness is unnecessary
  177. fi
  178. export RANDOM_SEED
  179. fi
  180. # Also used by GCC, but as an environment variable
  181. if [[ -z "$SOURCE_DATE_EPOCH" ]]; then
  182. if git_check "$root"; then
  183. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  184. elif [[ -f "$epoch_path" ]]; then
  185. SOURCE_DATE_EPOCH="$(< "$epoch_path")"
  186. else
  187. SOURCE_DATE_EPOCH="$(date +%s)"
  188. fi
  189. export SOURCE_DATE_EPOCH
  190. fi
  191. # Relevant only when libfaketime path is given in $BUILD_SYSTEM.conf
  192. if [[ -n "$LIBFAKETIME_PATH" ]]; then
  193. BUILD_DATE_FMT="%Y-%m-%d %H:%M:%S"
  194. BUILD_DATE="$(date -u -d "@$SOURCE_DATE_EPOCH" "+$BUILD_DATE_FMT" 2>/dev/null || date -u -r "$SOURCE_DATE_EPOCH" "+$BUILD_DATE_FMT" 2>/dev/null || date -u "+$BUILD_DATE_FMT")"
  195. FAKETIME="@$BUILD_DATE"
  196. LC_ALL='C.UTF-8'
  197. LD_PRELOAD="$LIBFAKETIME_PATH"
  198. TZ='UTC'
  199. export BUILD_DATE_FMT BUILD_DATE FAKETIME LC_ALL LD_PRELOAD TZ
  200. fi
  201. }
  202. libreboot() {
  203. action="$1"
  204. shift
  205. target="$1"
  206. shift
  207. set -e
  208. libreboot_setup "$@"
  209. if project_check "$target"; then
  210. libreboot_project "$action" "$target" "$@"
  211. elif tool_check "$target"; then
  212. libreboot_tool "$action" "$target" "$@"
  213. else
  214. libreboot_usage
  215. exit 1
  216. fi
  217. }
  218. libreboot "$@"