-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfw2tar
More file actions
executable file
·197 lines (169 loc) · 6.23 KB
/
Copy pathfw2tar
File metadata and controls
executable file
·197 lines (169 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
set -eu
BOLD=""
RESET=""
RED=""
GREEN=""
if [ -t 0 ]; then
# Can only use colors if we're in a terminal
BOLD=$(tput bold)
RESET=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
fi
image="rehosting/fw2tar"
fw2tar_run() {
local cmd=()
local maps=()
local build=false
local verbose=false
local image="rehosting/fw2tar" # Name of container instance
# Process each command-line argument
while [[ $# -gt 0 ]]; do
case "$1" in
--wrapper-help)
echo "Usage: fw2tar [WRAPPER FLAGS] [FLAGS] FIRMWARE_FILE"
echo "Wrapper script for running FW2TAR in a Docker container"
echo ""
echo "Wrapper-specific flags may be passed in *before* the fw2tar flags and args. If a value is required, it must be specified immediately after the flag with a space."
echo " --build: Build the fw2tar container (with Nix) before running the specified command. If no other arguments are provided, the container will be built and the script will exit."
echo " --image: Which image to run. Default: $image"
echo " --verbose: Print verbose output for fw2tar wrapper (e.g., filesystem mappings, docker command)"
echo " --wrapper-help: this message"
echo ""
echo "All other arguments will be passed through to the main fw2tar command in the container."
echo "For example try:"
echo " fw2tar --help"
echo " fw2tar ./your_firmware.bin"
exit 0
;;
--build)
build=true
shift
;;
--image)
image="$2"
shift 2
;;
--verbose)
verbose=true
shift
;;
*) # Default case: If no more known options, keep as part of command
cmd=("$@")
break
;;
esac
done
# If command is empty, parse any un-shifted args into array - probably means we were run with wrapper flags only (i.e., --build)
if [[ ${#cmd[@]} -eq 0 ]]; then
cmd=("$@")
fi
# If verbose, log all wrapper args and command
if $verbose; then
echo "${BOLD}Wrapper args:${RESET}"
echo " build: $build"
echo " image: $image"
echo " verbose: $verbose"
echo " fw2tar cmd: ${cmd[*]}"
echo
fi
if $build; then
echo "Running with container build (--build). The image is built with Nix and loaded as $image."
# The image is produced by this repo's flake (flake.nix), not a
# Dockerfile. Build it and load the resulting tarball into Docker.
if [ ! -f "flake.nix" ]; then
echo "flake.nix not found in current directory; run --build from the fw2tar repo."
exit 1
fi
if ! command -v nix >/dev/null 2>&1; then
echo "nix not found; install Nix (https://nixos.org/download) to build the fw2tar image."
exit 1
fi
nix build .#dockerImage || { echo "nix build .#dockerImage failed"; exit 1; }
docker load < result || { echo "docker load failed"; exit 1; }
# If we have no other args, exit 0
if [[ ${#cmd[@]} -eq 0 ]]; then
echo "$image built. Exiting as no command was specified."
exit 0
fi
fi
# Check for firmware file and map it
for i in "${!cmd[@]}"; do
if [[ -f "${cmd[$i]}" ]] && [[ "${cmd[$((i - 1))]}" != "--output" ]] && [[ "${cmd[$((i - 1))]}" != "--scratch-dir" ]]; then
local arg="${cmd[$i]}"
local abspath=$(realpath "$arg")
local host_path=$(dirname "$abspath")
local guest_path="/host_$(basename "$host_path")"
maps+=("$host_path:$guest_path")
cmd[$i]="/host_$(basename "$host_path")/$(basename "$arg")"
fi
done
# Check for "--output" flag, create and map the directory as necessary
for ((i=0; i<${#cmd[@]}; i++)); do
if [[ "${cmd[$i]}" == "--output" && $((i+1)) -lt ${#cmd[@]} ]]; then
output_dir="${cmd[$i+1]}"
if [[ ! -d "$output_dir" ]]; then
mkdir -p "$output_dir"
fi
# Add mapping for the output directory
local abspath=$(realpath "$output_dir")
local guest_path="/host_$(basename "$abspath")"
maps+=("$abspath:$guest_path")
cmd[$i+1]="/host_$(basename "$abspath")/$(basename "$output_dir")"
fi
done
# Check for "--scratch-dir" flag, create and map the directory as necessary
for ((i=0; i<${#cmd[@]}; i++)); do
if [[ "${cmd[$i]}" == "--scratch-dir" && $((i+1)) -lt ${#cmd[@]} ]]; then
scratch_dir="${cmd[$i+1]}"
if [[ ! -d "$scratch_dir" ]]; then
mkdir -p "$scratch_dir"
fi
# Add mapping for the scratch directory
local abspath=$(realpath "$scratch_dir")
local guest_path="/scratch"
maps+=("$abspath:$guest_path")
cmd[$i+1]="/scratch"
fi
done
# Sort mappings by path length
IFS=$'\n' maps=($(sort -r <<<"${maps[*]}"))
unset IFS
if $verbose; then
echo "${BOLD}Mappings from host paths to guest paths: $RESET"
for map in "${maps[@]}"; do
echo " $map"
done
echo
fi
# Build Docker command
docker_cmd=("docker" "run" "--rm")
docker_cmd+=("-u" "$(id -u):$(id -g)") # XXX will fakeroot be okay?
# Add mappings to docker command
for map in "${maps[@]}"; do
docker_cmd+=("-v" "$map")
done
# Add env variable of penguin bash script hash
hash=$(sha1sum "$0" | awk '{print $1}')
docker_cmd+=("-e" "FW2TAR_HASH=$hash")
docker_cmd+=("$image")
docker_cmd+=("fakeroot_fw2tar")
docker_cmd+=("${cmd[@]}")
if $verbose; then
echo "${BOLD}Fw2tar command:${RESET}"
echo " fw2tar ${cmd[@]}"
echo
echo "${BOLD}Complete docker commands:${RESET}"
echo " ${docker_cmd[*]}"
echo
echo "${BOLD}Command output:${RESET}"
fi
# Run the Docker command
"${docker_cmd[@]}"
}
# Main function
main() {
fw2tar_run "$@"
}
main "$@"