forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbed_example.sh
executable file
·232 lines (185 loc) · 8.76 KB
/
mbed_example.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env bash
#
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cd "$(dirname "$0")"/../..
CHIP_ROOT=$PWD
SUPPORTED_TOOLCHAIN=(GCC_ARM ARM)
SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W)
SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app all-clusters-minimal-app shell ota-requestor-app)
SUPPORTED_PROFILES=(release develop debug)
SUPPORTED_COMMAND=(build flash build-flash)
SUPPORTED_TYPE=(simple boot upgrade)
COMMAND=build
APP=lock-app
TARGET_BOARD=CY8CPROTO_062_4343W
TOOLCHAIN=GCC_ARM
PROFILE=release
TYPE=simple
TARGET_MEMORY_ALIGN[CY8CPROTO_062_4343W]=8
TARGET_BOOT_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0
TARGET_UPGRADE_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0xff
for i in "$@"; do
case $i in
-a=* | --app=*)
APP="${i#*=}"
shift
;;
-b=* | --board=*)
TARGET_BOARD="${i#*=}"
shift
;;
-t=* | --toolchain=*)
TOOLCHAIN="${i#*=}"
shift
;;
-p=* | --profile=*)
PROFILE="${i#*=}"
shift
;;
-c=* | --command=*)
COMMAND="${i#*=}"
shift
;;
-T=* | --type=*)
TYPE="${i#*=}"
shift
;;
*)
# unknown option
;;
esac
done
if [[ ! " ${SUPPORTED_COMMAND[@]} " =~ " ${COMMAND} " ]]; then
echo "ERROR: Command $COMMAND not supported"
exit 1
fi
if [[ ! " ${SUPPORTED_TARGET_BOARD[@]} " =~ " ${TARGET_BOARD} " ]]; then
echo "ERROR: Target $TARGET_BOARD not supported"
exit 1
fi
if [[ ! " ${SUPPORTED_APP[@]} " =~ " ${APP} " ]]; then
echo "ERROR: Application $APP not supported"
exit 1
fi
if [[ ! " ${SUPPORTED_TOOLCHAIN[@]} " =~ " ${TOOLCHAIN} " ]]; then
echo "ERROR: Toolchain $TOOLCHAIN not supported"
exit 1
fi
if [[ ! " ${SUPPORTED_PROFILES[@]} " =~ " ${PROFILE} " ]]; then
echo "ERROR: Profile $PROFILE not supported"
exit 1
fi
if [[ ! " ${SUPPORTED_TYPE[@]} " =~ " ${TYPE} " ]]; then
echo "ERROR: Type $TYPE not supported"
exit 1
fi
if [[ "$TYPE" == "boot" ]] && [[ "$PROFILE" == "debug" ]]; then
echo "ERROR: The $TYPE application type does not supprort ""$PROFILE profile"
exit 1
fi
set -e # Exit immediately if a command exits with a non-zero status.
# Activate Matter environment
source "$CHIP_ROOT"/scripts/activate.sh
# Application directory setup
APP_DIRECTORY="$CHIP_ROOT"/examples/"$APP"/mbed
# Build directory setup
BUILD_DIRECTORY="$APP_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"
# Set bootloader root directory
BOOTLOADER_ROOT_DIRECTORY="$CHIP_ROOT"/examples/platform/mbed/bootloader
# Set bootloader build directory
BOOTLOADER_BUILD_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"/
# Set encryption key directory
ENC_KEY_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/enc-key
if [[ "$COMMAND" == *"build"* ]]; then
echo "Build $TYPE $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile"
# Set Mbed OS path
MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo
# Set Mbed OS posix socket submodule path
MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo
# Set Mbed MCU boot path
MBED_MCU_BOOT_PATH="$CHIP_ROOT"/third_party/mbed-mcu-boot/repo
if [[ "$TYPE" == "boot" ]]; then
cd "$BOOTLOADER_ROOT_DIRECTORY"
# Install mcuboot requirements (silently)
pip install -q -r "$MBED_MCU_BOOT_PATH"/scripts/requirements.txt
# Run mcuboot setup script
python "$MBED_MCU_BOOT_PATH"/scripts/setup.py install
# Check if encryption key exists, if not generate it
if [[ ! -f "$ENC_KEY_DIRECTORY"/enc-key.pem ]]; then
mkdir -p "$ENC_KEY_DIRECTORY"
"$MBED_MCU_BOOT_PATH"/scripts/imgtool.py keygen -k "$ENC_KEY_DIRECTORY"/enc-key.pem -t rsa-2048
fi
# Create the signing keys source fille
"$MBED_MCU_BOOT_PATH"/scripts/imgtool.py getpub -k "$ENC_KEY_DIRECTORY"/enc-key.pem >signing_keys.c
ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed mcuboot
# Generate config file for selected target, toolchain and hardware
mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -o "$BOOTLOADER_BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH"
# Remove old artifacts to force linking
rm -rf "$BOOTLOADER_BUILD_DIRECTORY/chip-"*
# Build application
cmake -S . -B "$BOOTLOADER_BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH"
cmake --build "$BOOTLOADER_BUILD_DIRECTORY"
cd "$CHIP_ROOT"/examples
fi
if [[ "$TYPE" == "upgrade" ]]; then
# Check if encryption key exists
if [[ ! -f "$ENC_KEY_DIRECTORY"/enc-key.pem ]]; then
echo "ERROR: encryption key for upgrade image not exist"
exit 1
fi
fi
# Set Mbed OS posix socket submodule path
MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo
if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then
ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed "$APP_DIRECTORY"/mcuboot
fi
# Generate config file for selected target, toolchain and hardware
mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -p "$APP_DIRECTORY" -o "$BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH"
# Remove old artifacts to force linking
rm -rf "$BUILD_DIRECTORY/chip-"*
# Build application
cmake -S "$APP_DIRECTORY" -B "$BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_OS_POSIX_SOCKET_PATH="$MBED_OS_POSIX_SOCKET_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH" -DMBED_APP_TYPE="$TYPE"
cmake --build "$BUILD_DIRECTORY"
if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then
APP_VERSION=$(jq '.config."version-number-str".value' "$APP_DIRECTORY"/mbed_app.json | tr -d '\\"')
HEADER_SIZE=$(jq '.target_overrides.'\""$TARGET_BOARD"\"'."mcuboot.header-size"' "$APP_DIRECTORY"/mbed_app.json | tr -d \")
SLOT_SIZE=$(jq '.target_overrides.'\""$TARGET_BOARD"\"'."mcuboot.slot-size"' "$APP_DIRECTORY"/mbed_app.json | tr -d \")
if [[ "$TYPE" == "boot" ]]; then
# Signed the primary application
"$MBED_MCU_BOOT_PATH"/scripts/imgtool.py sign -k "$ENC_KEY_DIRECTORY"/enc-key.pem \
--align "${TARGET_MEMORY_ALIGN[$TARGET_BOARD]}" -v "$APP_VERSION" --header-size $(($HEADER_SIZE)) --pad-header -S "$SLOT_SIZE" -R "${TARGET_BOOT_IMAGE_ERASE_VALUE[$TARGET_BOARD]}" \
"$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex
# Create the factory firmware (bootloader + signed primary application)
hexmerge.py -o "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex --no-start-addr "$BOOTLOADER_BUILD_DIRECTORY"/chip-mbed-bootloader.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex
elif [[ "$TYPE" == "upgrade" ]]; then
# Signed the secondary application
"$MBED_MCU_BOOT_PATH"/scripts/imgtool.py sign -k "$ENC_KEY_DIRECTORY"/enc-key.pem \
--align "${TARGET_MEMORY_ALIGN[$TARGET_BOARD]}" -v "$APP_VERSION" --header-size $(($HEADER_SIZE)) --pad-header -S "$SLOT_SIZE" -R "${TARGET_UPGRADE_IMAGE_ERASE_VALUE[$TARGET_BOARD]}" \
"$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex
# Convert hex image to raw binary file
arm-none-eabi-objcopy -I ihex -O binary "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.bin
python "$CHIP_ROOT"/examples/platform/mbed/ota/generate_ota_list_image.py "$APP_DIRECTORY"/mbed_app.json "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.bin
fi
fi
fi
if [[ "$COMMAND" == *"flash"* ]]; then
echo "Flash $TYPE $APP app to $TARGET_BOARD target [$TOOLCHAIN toolchain, $PROFILE profile]"
# Flash scripts path setup
MBED_FLASH_SCRIPTS_PATH=$CHIP_ROOT/config/mbed/scripts
APP_PATH="$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.elf
# Flash application
"$OPENOCD_PATH"/bin/openocd -f "$MBED_FLASH_SCRIPTS_PATH/$TARGET_BOARD".tcl -c "program $APP_PATH verify reset exit"
fi