libreboot 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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]" >&2
  23. printf '\n%s\n' 'Generic project actions:' >&2
  24. for action in "${PROJECT_ACTIONS_GENERIC[@]}"; do
  25. printf '%s\n' " ${action}" >&2
  26. done
  27. printf '\n%s\n' 'Virtual project actions:' >&2
  28. printf '%s\n' ' sources'
  29. printf '%s\n' ' produce'
  30. printf '%s\n' ' test'
  31. printf '\n%s\n' 'Project targets:' >&2
  32. for target in "${root}/${PROJECTS}"/*; do
  33. if project_check "${target}"; then
  34. printf '%s\n' " ${target}" >&2
  35. fi
  36. done
  37. printf '\n%s\n' 'Generic tool actions:' >&2
  38. for action in "${TOOL_ACTIONS_GENERIC[@]}"; do
  39. printf '%s\n' " ${action}" >&2
  40. done
  41. printf '\n%s\n' 'Tool targets:' >&2
  42. for target in "${root}/${TOOLS}"/*; do
  43. if tool_check "${target}"; then
  44. printf '%s\n' " ${target}" >&2
  45. fi
  46. done
  47. printf '\n%s\n' 'Environment variables:' >&2
  48. printf '%s\n' ' PROJECTS_FORCE - Projects to always perform actions for' >&2
  49. printf '%s\n' ' TOOLS_FORCE - Tools to always perform actions for' >&2
  50. printf '%s\n' ' RELEASE_KEY - GPG key to use for release' >&2
  51. printf '%s\n' ' VBOOT_KEYS_PATH - Path to the vboot keys' >&2
  52. printf '%s\n' ' LIBFAKETIME_PATH - Path to the libfaketime shared library' >&2
  53. printf '%s\n' ' TASKS - Number of simultaneous tasks to run' >&2
  54. printf '%s\n' ' VERSION - Version string to use' >&2
  55. printf '\n%s\n' 'Configuration files:' >&2
  56. printf '%s\n' " ${BUILD_SYSTEM}.conf - Environment variables configuration" >&2
  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 '\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 "${action}" "${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'
  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 -i tool_actions_count="${#TOOL_ACTIONS_GENERIC[@]}"
  131. local ignore="${TOOL_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  132. local -a tool_actions
  133. for ((i=0; i<"${tool_actions_count}"; i++)); do
  134. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]}")
  135. if [[ "${TOOL_ACTIONS_GENERIC[i]}" == !(${ignore// /|}) ]]; then
  136. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]/%/_check}")
  137. fi
  138. done
  139. TOOL_ACTIONS=("${TOOL_ACTIONS_HELPERS[@]}" "${tool_actions[@]}")
  140. }
  141. libreboot_setup_project_actions() {
  142. local -i project_actions_count="${#PROJECT_ACTIONS_GENERIC[@]}"
  143. local ignore="${PROJECT_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  144. local -a project_actions
  145. for ((i=0; i<"${project_actions_count}"; i++)); do
  146. project_actions+=("${PROJECT_ACTIONS_GENERIC[i]}")
  147. if [[ "${PROJECT_ACTIONS_GENERIC[i]}" == !(${ignore// /|}) ]]; then
  148. project_actions+=("${PROJECT_ACTIONS_GENERIC[i]/%/_check}")
  149. fi
  150. done
  151. PROJECT_ACTIONS=("${PROJECT_ACTIONS_HELPERS[@]}" "${project_actions[@]}")
  152. }
  153. libreboot_setup_variables() {
  154. local vboot_tools_path="$(project_install_path 'vboot' 'tools')"
  155. local version_path="${root}/${DOTVERSION}"
  156. if [[ -z "${VERSION}" ]]; then
  157. if git_check "${root}"; then
  158. VERSION="${BUILD_SYSTEM}-$(git_describe "${root}" 2> /dev/null || echo 'git')"
  159. elif [[ -f "${version_path}" ]]; then
  160. VERSION="$(< "${version_path}")"
  161. else
  162. VERSION="${BUILD_SYSTEM}"
  163. fi
  164. fi
  165. if [[ -d "${vboot_tools_path}/devkeys/" ]]; then
  166. VBOOT_KEYS_PATH="${VBOOT_KEYS_PATH:-${vboot_tools_path}/devkeys/}"
  167. fi
  168. libreboot_setup_reproducible_builds_variables
  169. }
  170. libreboot_setup_reproducible_builds_variables() {
  171. local epoch_path="${root}/${DOTEPOCH}"
  172. local rnd_seed_path="${root}/${DOTRNDSEED}"
  173. # Used by GCC, e.g., -frandom-seed="${RANDOM_SEED}"
  174. if [[ -z "${RANDOM_SEED}" ]]; then
  175. if [[ -f "${rnd_seed_path}" ]]; then
  176. RANDOM_SEED="$(< "${rnd_seed_path}")"
  177. else
  178. RANDOM_SEED="${RANDOM}" # True randomness is unnecessary
  179. fi
  180. fi
  181. # Also used by GCC, but as an environment variable
  182. if [[ -z "${SOURCE_DATE_EPOCH}" ]]; then
  183. if git_check "${root}"; then
  184. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  185. elif [[ -f "${epoch_path}" ]]; then
  186. SOURCE_DATE_EPOCH="$(< "${epoch_path}")"
  187. else
  188. SOURCE_DATE_EPOCH="$(date +%s)"
  189. fi
  190. fi
  191. # Relevant only when libfaketime is preloaded
  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. fi
  200. }
  201. libreboot() {
  202. action="$1"
  203. shift
  204. target="$1"
  205. shift
  206. set -e
  207. libreboot_setup "$@"
  208. if project_check "${target}"; then
  209. libreboot_project "${action}" "${target}" "$@"
  210. elif tool_check "${target}"; then
  211. libreboot_tool "${action}" "${target}" "$@"
  212. else
  213. libreboot_usage
  214. exit 1
  215. fi
  216. }
  217. libreboot "$@"