libreboot 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/env 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. GLOBIGNORE=".:.." # This enables the shell option 'dotglob' as well.
  17. shopt -s nullglob extglob
  18. libreboot_usage() {
  19. local action
  20. local target
  21. printf '%s\n' "${executable} [action] [target] [arguments]" >&2
  22. printf '\n%s\n' 'Generic project actions:' >&2
  23. for action in "${PROJECT_ACTIONS_GENERIC[@]}"; do
  24. printf '%s\n' " ${action}" >&2
  25. done
  26. printf '\n%s\n' 'Virtual project actions:' >&2
  27. printf '%s\n' ' sources'
  28. printf '%s\n' ' produce'
  29. printf '%s\n' ' test'
  30. printf '\n%s\n' 'Project targets:' >&2
  31. for target in "${root}/${PROJECTS}"/*; do
  32. if project_check "${target}"; then
  33. printf '%s\n' " ${target}" >&2
  34. fi
  35. done
  36. printf '\n%s\n' 'Generic tool actions:' >&2
  37. for action in "${TOOL_ACTIONS_GENERIC[@]}"; do
  38. printf '%s\n' " ${action}" >&2
  39. done
  40. printf '\n%s\n' 'Tool targets:' >&2
  41. for target in "${root}/${TOOLS}"/*; do
  42. if tool_check "${target}"; then
  43. printf '%s\n' " ${target}" >&2
  44. fi
  45. done
  46. printf '\n%s\n' 'Environment variables:' >&2
  47. printf '%s\n' ' PROJECTS_FORCE - Projects to always perform actions for' >&2
  48. printf '%s\n' ' TOOLS_FORCE - Tools to always perform actions for' >&2
  49. printf '%s\n' ' RELEASE_KEY - GPG key to use for release' >&2
  50. printf '%s\n' ' VBOOT_KEYS_PATH - Path to the vboot keys' >&2
  51. printf '%s\n' ' LIBFAKETIME_PATH - Path to the libfaketime shared library' >&2
  52. printf '%s\n' ' TASKS - Number of simultaneous tasks to run' >&2
  53. printf '%s\n' ' VERSION - Version string to use' >&2
  54. printf '\n%s\n' 'Configuration files:' >&2
  55. printf '%s\n' " ${BUILD_SYSTEM}.conf - Environment variables configuration" >&2
  56. }
  57. libreboot_project() {
  58. action="$1"
  59. shift
  60. project="$1"
  61. shift
  62. case "${action}" in
  63. 'sources')
  64. (
  65. set +e
  66. project_action_arguments 'extract' "${project}" "$@" && return 0
  67. project_action_arguments 'download' "${project}" "$@" && return 0
  68. )
  69. ;;
  70. 'produce')
  71. for action in 'build' 'install' 'release'; do
  72. project_action_arguments "${action}" "${project}" "$@"
  73. done
  74. ;;
  75. 'test')
  76. for action in "${PROJECT_ACTIONS[@]}"; do
  77. project_action_arguments "${action}" "${project}" "$@"
  78. done
  79. ;;
  80. *)
  81. if ! project_function_check "${project}" "${action}"; then
  82. libreboot_usage
  83. exit 1
  84. elif [[ "${action}" == 'usage' ]]; then
  85. project_action "${action}" "${project}" "$@"
  86. else
  87. project_action_arguments "${action}" "${project}" "$@"
  88. fi
  89. ;;
  90. esac
  91. }
  92. libreboot_tool() {
  93. action="$1"
  94. shift
  95. tool="$1"
  96. shift
  97. if ! tool_function_check "${tool}" "${action}"; then
  98. libreboot_usage
  99. exit 1
  100. elif [[ "${action}" == 'usage' ]]; then
  101. tool_action "${action}" "${tool}" "$@"
  102. else
  103. tool_action_arguments_recursive "${action}" "${tool}" "$@"
  104. fi
  105. }
  106. libreboot_setup() {
  107. root="$(readlink -f "$(dirname "$0")")"
  108. executable="$(basename "$0")"
  109. libreboot_setup_include
  110. libreboot_setup_tool_actions
  111. libreboot_setup_project_actions
  112. requirements 'tar' 'sed' 'gpg' 'sha256sum' 'git'
  113. libreboot_setup_variables
  114. }
  115. libreboot_setup_include() {
  116. local libs_path="${root}/libs"
  117. local conf_path
  118. source "${libs_path}/project"
  119. source "${libs_path}/tool"
  120. source "${libs_path}/common"
  121. source "${libs_path}/git"
  122. conf_path="${root}/${BUILD_SYSTEM}.conf"
  123. if [[ -f "${conf_path}" ]]; then
  124. source "${conf_path}"
  125. fi
  126. }
  127. libreboot_setup_tool_actions() {
  128. local -i tool_actions_count="${#TOOL_ACTIONS_GENERIC[@]}"
  129. local ignore="${TOOL_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  130. local -a tool_actions
  131. for ((i=0; i<"${tool_actions_count}"; i++)); do
  132. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]}")
  133. if [[ "${TOOL_ACTIONS_GENERIC[i]}" == !(${ignore// /|}) ]]; then
  134. tool_actions+=("${TOOL_ACTIONS_GENERIC[i]/%/_check}")
  135. fi
  136. done
  137. TOOL_ACTIONS=("${TOOL_ACTIONS_HELPERS[@]}" "${tool_actions[@]}")
  138. }
  139. libreboot_setup_project_actions() {
  140. local -i project_actions_count="${#PROJECT_ACTIONS_GENERIC[@]}"
  141. local ignore="${PROJECT_ACTIONS_GENERIC_IGNORE_CHECK[*]}"
  142. local -a project_actions
  143. for ((i=0; i<"${project_actions_count}"; 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. fi
  179. # Also used by GCC, but as an environment variable
  180. if [[ -z "${SOURCE_DATE_EPOCH}" ]]; then
  181. if git_check "${root}"; then
  182. SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
  183. elif [[ -f "${epoch_path}" ]]; then
  184. SOURCE_DATE_EPOCH="$(< "${epoch_path}")"
  185. else
  186. SOURCE_DATE_EPOCH="$(date +%s)"
  187. fi
  188. fi
  189. # Relevant only when libfaketime is preloaded
  190. if [[ -n "${LIBFAKETIME_PATH}" ]]; then
  191. BUILD_DATE_FMT="%Y-%m-%d %H:%M:%S"
  192. 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}")"
  193. FAKETIME="@${BUILD_DATE}"
  194. LC_ALL='C.UTF-8'
  195. LD_PRELOAD="${LIBFAKETIME_PATH}"
  196. TZ='UTC'
  197. fi
  198. }
  199. libreboot() {
  200. action="$1"
  201. shift
  202. target="$1"
  203. shift
  204. set -e
  205. libreboot_setup "$@"
  206. if project_check "${target}"; then
  207. libreboot_project "${action}" "${target}" "$@"
  208. elif tool_check "${target}"; then
  209. libreboot_tool "${action}" "${target}" "$@"
  210. else
  211. libreboot_usage
  212. exit 1
  213. fi
  214. }
  215. libreboot "$@"