David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright 2019 The Hafnium Authors. |
| 3 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style |
| 5 | # license that can be found in the LICENSE file or at |
| 6 | # https://opensource.org/licenses/BSD-3-Clause. |
Andrew Walbran | 5e71e9b | 2020-06-17 15:44:49 +0100 | [diff] [blame] | 7 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 8 | set -euo pipefail |
| 9 | |
David Brazdil | 4a51d65 | 2019-12-20 13:27:54 +0000 | [diff] [blame] | 10 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | ROOT_DIR="$(dirname ${SCRIPT_DIR})" |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 12 | |
| 13 | source "${SCRIPT_DIR}/docker/common.inc" |
| 14 | |
| 15 | if [ "${HAFNIUM_HERMETIC_BUILD:-}" == "inside" ] |
| 16 | then |
| 17 | echo "ERROR: Invoked $0 recursively" 1>&2 |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | # Set up a temp directory and register a cleanup function on exit. |
| 22 | TMP_DIR="$(mktemp -d)" |
| 23 | function cleanup() { |
| 24 | rm -rf "${TMP_DIR}" |
| 25 | } |
| 26 | trap cleanup EXIT |
| 27 | |
| 28 | # Build local image and write its hash to a temporary file. |
| 29 | IID_FILE="${TMP_DIR}/imgid.txt" |
| 30 | "${DOCKER}" build \ |
| 31 | --build-arg LOCAL_UID="$(id -u)" \ |
| 32 | --build-arg LOCAL_GID="$(id -g)" \ |
| 33 | --iidfile="${IID_FILE}" \ |
J-Alves | 108b045 | 2024-02-03 15:32:59 +0000 | [diff] [blame] | 34 | -f "${SCRIPT_DIR}/docker/Dockerfile" \ |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 35 | "${SCRIPT_DIR}/docker" |
| 36 | IMAGE_ID="$(cat ${IID_FILE})" |
| 37 | |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 38 | # Parse command line arguments |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 39 | INTERACTIVE=false |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 40 | ALLOW_PTRACE=false |
Karl Meakin | 5c78aa4 | 2024-11-17 20:07:56 +0000 | [diff] [blame] | 41 | TTY=true |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 42 | while true |
| 43 | do |
| 44 | case "${1:-}" in |
Karl Meakin | 5c78aa4 | 2024-11-17 20:07:56 +0000 | [diff] [blame] | 45 | --tty) |
| 46 | TTY=${2:-} |
| 47 | shift; shift |
| 48 | ;; |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 49 | -i) |
| 50 | INTERACTIVE=true |
| 51 | shift |
| 52 | ;; |
| 53 | -p) |
| 54 | ALLOW_PTRACE=true |
| 55 | shift |
| 56 | ;; |
| 57 | -*) |
| 58 | echo "ERROR: Unknown command line flag: $1" 1>&2 |
Karl Meakin | 5c78aa4 | 2024-11-17 20:07:56 +0000 | [diff] [blame] | 59 | echo "Usage: $0 [-i] [-p] [--tty true|false] <command>" |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 60 | exit 1 |
| 61 | ;; |
| 62 | *) |
| 63 | break |
| 64 | ;; |
| 65 | esac |
| 66 | done |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 67 | |
| 68 | ARGS=() |
| 69 | # Run with a pseduo-TTY for nicer logging. |
Karl Meakin | 5c78aa4 | 2024-11-17 20:07:56 +0000 | [diff] [blame] | 70 | ARGS+=(--tty=${TTY}) |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 71 | # Run interactive if this script was invoked with '-i'. |
| 72 | if [ "${INTERACTIVE}" == "true" ] |
| 73 | then |
| 74 | ARGS+=(-i) |
| 75 | fi |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 76 | # Allow ptrace() syscall if invoked with '-p'. |
| 77 | if [ "${ALLOW_PTRACE}" == "true" ] |
| 78 | then |
| 79 | echo "WARNING: Docker seccomp profile is disabled!" 1>&2 |
| 80 | ARGS+=(--cap-add=SYS_PTRACE --security-opt seccomp=unconfined) |
| 81 | fi |
J-Alves | 16000be | 2025-06-17 14:12:01 +0100 | [diff] [blame] | 82 | |
| 83 | if [ -z "${HAFNIUM_FVP-}" ] |
| 84 | then |
| 85 | HAFNIUM_FVP_DIR="${ROOT_DIR}/../fvp" |
| 86 | else |
| 87 | HAFNIUM_FVP_DIR=$(dirname "$HAFNIUM_FVP") |
| 88 | fi |
| 89 | |
| 90 | echo "Using FVP in: ${HAFNIUM_FVP_DIR}" |
| 91 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 92 | # Propagate "HAFNIUM_*" environment variables. |
| 93 | # Note: Cannot use `env | while` because the loop would run inside a child |
| 94 | # process and would not have any effect on variables in the parent. |
| 95 | while read -r ENV_LINE |
| 96 | do |
| 97 | VAR_NAME="$(echo ${ENV_LINE} | cut -d= -f1)" |
| 98 | case "${VAR_NAME}" in |
| 99 | HAFNIUM_HERMETIC_BUILD) |
| 100 | # Skip this one. It will be overridden below. |
| 101 | ;; |
| 102 | HAFNIUM_*) |
| 103 | ARGS+=(-e "${ENV_LINE}") |
| 104 | ;; |
| 105 | esac |
| 106 | done <<< "$(env)" |
Jayanth Dodderi Chidanand | 2435318 | 2025-08-13 13:05:58 +0100 | [diff] [blame] | 107 | |
| 108 | # Forward PLATFORM to ensure it's preserved inside the container. |
| 109 | if [ -n "${PLATFORM:-}" ]; then |
| 110 | ARGS+=(-e "PLATFORM=${PLATFORM}") |
| 111 | fi |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 112 | # Set environment variable informing the build that we are running inside |
| 113 | # a container. |
| 114 | ARGS+=(-e HAFNIUM_HERMETIC_BUILD=inside) |
Karl Meakin | 7150268 | 2024-11-17 19:04:25 +0000 | [diff] [blame] | 115 | # Bind-mount the Hafnium root directory and the FVP directory. We mount them at |
| 116 | # the same absolute location so that all paths match across the host and guest. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 117 | ARGS+=(-v "${ROOT_DIR}":"${ROOT_DIR}") |
J-Alves | 16000be | 2025-06-17 14:12:01 +0100 | [diff] [blame] | 118 | ARGS+=(-v "${HAFNIUM_FVP_DIR}":"${HAFNIUM_FVP_DIR}") |
Jayanth Dodderi Chidanand | 82ba745 | 2025-07-24 18:08:02 +0100 | [diff] [blame] | 119 | |
| 120 | # Mount TF-A and TF-A-Tests source directories for use in shrinkwrap |
| 121 | # Allow overriding via environment variables. |
| 122 | TFA_DIR="${TFA_DIR:-${ROOT_DIR}/../trusted-firmware-a}" |
| 123 | TFTF_DIR="${TFTF_DIR:-${ROOT_DIR}/../tf-a-tests}" |
| 124 | |
| 125 | # Note: |
| 126 | # By default, this setup assumes that TF-A and TF-A-Tests repositories are checked |
| 127 | # out in directories adjacent to the Hafnium source tree. |
| 128 | # You can override these paths by setting TFA_DIR and TFTF_DIR environment variables. |
| 129 | ARGS+=(-v "${TFA_DIR}:${TFA_DIR}") |
| 130 | ARGS+=(-v "${TFTF_DIR}:${TFTF_DIR}") |
| 131 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 132 | # Make all files outside of the Hafnium directory read-only to ensure that all |
| 133 | # generated files are written there. |
| 134 | ARGS+=(--read-only) |
| 135 | # Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files. |
| 136 | ARGS+=(--tmpfs /tmp) |
| 137 | # Set working directory. |
| 138 | ARGS+=(-w "${ROOT_DIR}") |
| 139 | |
Jayanth Dodderi Chidanand | 24f286f | 2025-07-21 22:31:57 +0100 | [diff] [blame] | 140 | # Initialize Shrinkwrap environment before running the user-provided command |
| 141 | # inside the container. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 142 | echo "Running in container: $*" 1>&2 |
Jayanth Dodderi Chidanand | 24f286f | 2025-07-21 22:31:57 +0100 | [diff] [blame] | 143 | |
| 144 | # NOTE: |
| 145 | # shrinkwrap_setup_env.sh is sourced here at runtime (not configured within the Dockerfile) |
| 146 | # because it depends on host-mounted Hafnium repo paths that are only available |
| 147 | # when the container is launched. This approach ensures compatibility with |
| 148 | # the Hafnium developer Docker environment under build/docker/. |
| 149 | CMD="export PATH=${HAFNIUM_FVP_DIR}/Base_RevC_AEMvA_pkg/models/Linux64_GCC-9.3:\$PATH && \ |
| 150 | source tools/shrinkwrap/shrinkwrap_setup_env.sh && \ |
| 151 | bash -c \"$*\"" |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 152 | ${DOCKER} run \ |
Jayanth Dodderi Chidanand | 24f286f | 2025-07-21 22:31:57 +0100 | [diff] [blame] | 153 | "${ARGS[@]}" \ |
| 154 | "${IMAGE_ID}" \ |
| 155 | /bin/bash -c "$CMD" |