-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-fsbl
149 lines (125 loc) · 3.45 KB
/
build-fsbl
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
#!/bin/sh
# xilinx-getting-started/build-fsbl
set -euvx
readonly this="$(readlink -f "$0")"
readonly here="$(dirname "${this}")"
readonly whatami="$(basename "${here}").$(basename "${this}")"
readonly tmpdir="$(mkdir -p "${HOME}/tmp" && mktemp -d "${HOME}/tmp/${whatami}.XXXXXX")"
log() { echo "${whatami}[$$]: $@" >&2; }
error() { log "ERROR: $@"; }
warning() { log "WARNING: $@"; }
info() { log "INFO: $@"; }
die() {
error "$@"
usage >&2
exit 1
}
cleanup() {
status="$?"
rm -rf "${tmpdir}"
if command -v docker >/dev/null 2>&1; then
docker system prune --force --filter until=6h >&2 || true
fi
return "${status}"
}
usage() {
cat <<EOF
Usage: $0 [OPTION] HDF_FILE
Build the First Stage Boot Loader from the given HDF file.
Options:
-h print usage and exit
-C WORKDIR generate artifacts in WORKDIR (default: \$PWD/fsbl)
Examples:
\$ $0 -h
\$ $0 ~/Downloads/zynq_linux.hdf
\$ $0 -C \$(mktemp -dt fsbl.XXXXXX) ~/Downloads/zynq_linux.hdf
EOF
}
# vet given (possibly relative) path hdf_file; for good vetting, print
# resulting full path and return success; otherwise, print nothing and return
# failure
vet_hdf_file() {
local result=""
if ! result="$(readlink -f "$1")"; then
error "bad path: $1"
return 1
fi
if ! [ -f "${result}" ]; then
error "bad file: ${result}"
return 1
fi
local regex='^[[:alnum:]._-]+[.][Hh][Dd][Ff]$'
if ! basename "${result}" | grep -Eq "${regex}"; then
error "bad file name: ${result}"
return 1
fi
if ! file -b "${result}" | grep -q '^Zip archive data'; then
error "bad file type: $(file "${result}")"
return 1
fi
echo "${result}"
return 0
}
################################################################################
trap cleanup EXIT
export TMPDIR="${tmpdir}"
export LC_ALL=C
while getopts ":hC:" opt; do
case "${opt}" in
h)
usage
exit 0
;;
C)
if ! workdir="$(readlink -f "${OPTARG}")"; then
die "missing path: ${OPTARG}"
fi
if ! [ -d "${workdir}" ]; then
die "not a directory: ${workdir}"
fi
readonly workdir="${workdir}"
;;
:) die "missing argument: -${OPTARG}" ;;
\?) die "bad option: -${OPTARG}" ;;
esac
done
shift "$((${OPTIND} - 1))"
# hdf_file
if ! [ 1 -eq "$#" ]; then
die "bad args"
fi
if ! hdf_file="$(vet_hdf_file "$1")"; then
die "FAILURE: vet_hdf_file $1"
fi
readonly hdf_file="${hdf_file}"
# workdir
if [ -n "${workdir:-}" ]; then
info "workdir: ${workdir}"
else
readonly workdir="${PWD}/fsbl"
mkdir -vp "${workdir}"
warning "defaulting workdir: ${workdir}"
fi
if ! command -v hsi >/dev/null 2>&1; then
die "missing program: hsi"
fi
# > ERROR: [Common 17-70] Application Exception: Not found in path: gmake
#
# -- stupid hsi, when it can't find gmake
if ! command -v gmake >/dev/null 2>&1; then
warning "missing program: gmake"
if ! command -v make >/dev/null 2>&1; then
die "missing program: make"
fi
fake_bin="$(mktemp -dt fake_bin.XXXXXX)"
(
cd "${fake_bin}"
ln -vnsrf "$(command -v make)" gmake
)
export PATH="${fake_bin}:${PATH}"
fi
hsi <<EOF
set hwdsgn [ open_hw_design ${hdf_file} ]
generate_app -hw \$hwdsgn -os standalone -proc ps7_cortexa9_0 -app zynq_fsbl -compile -sw fsbl -dir ${workdir}
EOF
exit "$?"