This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild_idfboot.sh
executable file
·166 lines (136 loc) · 4.59 KB
/
build_idfboot.sh
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
#!/usr/bin/env bash
#
# Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
#
# SPDX-License-Identifier: Apache-2.0
#
SCRIPT_ROOTDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
IDF_PATH="${IDF_PATH:-${SCRIPT_ROOTDIR}/esp-idf}"
set -eo pipefail
supported_targets=("esp32" "esp32s2" "esp32s3" "esp32c2" "esp32c3" "esp32c6" "esp32h2")
usage() {
echo ""
echo "USAGE: ${SCRIPT_NAME} [-h] [-s] -c <chip> -f <config> -p <partinfo>"
echo ""
echo "Where:"
echo " -c <chip> Target chip (options: ${supported_targets[*]})"
echo " -f <config> Path to file containing bootloader configuration options"
echo " -p <partinfo> Path to file containing partition table information"
echo " -s Setup environment"
echo " -h Show usage and terminate"
echo ""
}
setup() {
if [ "${IDF_PATH}" == "${SCRIPT_ROOTDIR}/esp-idf" ]; then
git -C "${SCRIPT_ROOTDIR}" submodule update --init esp-idf
fi
}
build_idfboot() {
local target=${1}
local config=${2}
local partinfo=${3}
local build_dir=".build-${target}"
local source_dir="${IDF_PATH}/components/bootloader/subproject"
local output_dir="${SCRIPT_ROOTDIR}/out"
local toolchain_file="${IDF_PATH}/tools/cmake/toolchain-${target}.cmake"
local sdkconfig_defaults="${SCRIPT_ROOTDIR}/sdkconfig.defaults"
local idfboot_config
local idfboot_partinfo
local idfboot_partoffset
local idfboot_flashsize
local make_generator
# Prioritize a chip-specific configuration for IDF if one exists.
if [ -f "${sdkconfig_defaults}.${target}" ]; then
sdkconfig_defaults="${sdkconfig_defaults}.${target}"
fi
idfboot_config=$(realpath "${config:-${sdkconfig_defaults}}")
idfboot_partinfo=$(realpath "${partinfo:-${SCRIPT_ROOTDIR}/partitions.csv}")
# Try parsing Flash Size and Partition Table offset values from the sdkconfig
# file.
# If found, pass them to the Partition Table generator script. Otherwise, the
# script will assume default values.
idfboot_partoffset=$(sed -n 's/^CONFIG_PARTITION_TABLE_OFFSET=//p' "${idfboot_config}")
if [ -n "${idfboot_partoffset}" ]; then
idfboot_partoffset="--offset ${idfboot_partoffset}"
fi
idfboot_flashsize=$(sed -n 's/^CONFIG_ESPTOOLPY_FLASHSIZE_\(.*\)MB=y/\1MB/p' "${idfboot_config}")
if [ -n "${idfboot_flashsize}" ]; then
idfboot_flashsize="--flash-size ${idfboot_flashsize}"
fi
pushd "${SCRIPT_ROOTDIR}" &>/dev/null
mkdir -p "${output_dir}" &>/dev/null
# Build with Ninja if installed
if command -v ninja &>/dev/null; then
make_generator="-GNinja"
fi
# Build bootloader for selected target
export IDF_PATH=${IDF_PATH}
cmake -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}" \
-DSDKCONFIG_DEFAULTS="${idfboot_config}" \
-DSDKCONFIG="${output_dir}/sdkconfig-${target}" \
-DIDF_PATH="${IDF_PATH}" \
-DIDF_TARGET="${target}" \
-DPYTHON_DEPS_CHECKED=1 \
-B "${build_dir}" \
"${make_generator}" \
"${source_dir}"
cmake --build "${build_dir}"/
# Copy bootloader binary file to output directory
cp "${build_dir}"/bootloader.bin "${output_dir}"/bootloader-"${target}".bin
# Generate partition table binary file
# shellcheck disable=SC2086 # Intentionally split words from variables
python "${IDF_PATH}"/components/partition_table/gen_esp32part.py \
${idfboot_partoffset} \
${idfboot_flashsize} \
"${idfboot_partinfo}" \
"${output_dir}"/partition-table-"${target}".bin
# Remove build directory
rm -rf "${build_dir}" &>/dev/null
popd &>/dev/null
}
while getopts ":hc:f:p:s" arg; do
case "${arg}" in
c)
chip=${OPTARG}
;;
f)
config=${OPTARG}
;;
p)
partinfo=${OPTARG}
;;
s)
setup
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
if [ -z "${chip}" ]; then
printf "ERROR: Missing target chip.\n"
usage
exit 1
fi
if [ -n "${config}" ] && [ ! -f "${config}" ]; then
printf "ERROR: Bootloader configuration file %s not found.\n" "${config}"
usage
exit 1
fi
if [ -n "${partinfo}" ] && [ ! -f "${partinfo}" ]; then
printf "ERROR: Partition table file %s not found.\n" "${partinfo}"
usage
exit 1
fi
if [[ ! "${supported_targets[*]}" =~ ${chip} ]]; then
printf "ERROR: Target \"%s\" is not supported!\n" "${chip}"
usage
exit 1
fi
build_idfboot "${chip}" "${config}" "${partinfo}"