blob: e2120b6bfda17f877ad95994c0329d3ca7480547 [file] [log] [blame]
Vincent Coubardcc135602021-06-29 16:58:47 +01001#!/usr/bin/env bash
2
3#
4# Copyright (c) 2020 Project CHIP Authors
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19cd "$(dirname "$0")"/../..
20CHIP_ROOT=$PWD
Vincent Coubardcc135602021-06-29 16:58:47 +010021
22SUPPORTED_TOOLCHAIN=(GCC_ARM ARM)
Artur Tynecki35fb13e2021-10-12 18:23:45 +020023SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W)
Boris Zbarsky583ac0d2022-05-28 13:08:33 -040024SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app all-clusters-minimal-app shell ota-requestor-app)
Vincent Coubardcc135602021-06-29 16:58:47 +010025SUPPORTED_PROFILES=(release develop debug)
26SUPPORTED_COMMAND=(build flash build-flash)
Artur Tynecki42a7b0a2022-01-26 05:46:24 +010027SUPPORTED_TYPE=(simple boot upgrade)
Vincent Coubardcc135602021-06-29 16:58:47 +010028
29COMMAND=build
Artur Tynecki35fb13e2021-10-12 18:23:45 +020030APP=lock-app
Vincent Coubardcc135602021-06-29 16:58:47 +010031TARGET_BOARD=CY8CPROTO_062_4343W
32TOOLCHAIN=GCC_ARM
33PROFILE=release
Artur Tynecki42a7b0a2022-01-26 05:46:24 +010034TYPE=simple
35
36TARGET_MEMORY_ALIGN[CY8CPROTO_062_4343W]=8
37TARGET_BOOT_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0
38TARGET_UPGRADE_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0xff
Vincent Coubardcc135602021-06-29 16:58:47 +010039
40for i in "$@"; do
41 case $i in
42 -a=* | --app=*)
43 APP="${i#*=}"
44 shift
45 ;;
46 -b=* | --board=*)
47 TARGET_BOARD="${i#*=}"
48 shift
49 ;;
50 -t=* | --toolchain=*)
51 TOOLCHAIN="${i#*=}"
52 shift
53 ;;
54 -p=* | --profile=*)
55 PROFILE="${i#*=}"
56 shift
57 ;;
58 -c=* | --command=*)
59 COMMAND="${i#*=}"
60 shift
61 ;;
Artur Tynecki42a7b0a2022-01-26 05:46:24 +010062 -T=* | --type=*)
63 TYPE="${i#*=}"
64 shift
65 ;;
Vincent Coubardcc135602021-06-29 16:58:47 +010066 *)
67 # unknown option
68 ;;
69 esac
70done
71
72if [[ ! " ${SUPPORTED_COMMAND[@]} " =~ " ${COMMAND} " ]]; then
73 echo "ERROR: Command $COMMAND not supported"
74 exit 1
75fi
76
77if [[ ! " ${SUPPORTED_TARGET_BOARD[@]} " =~ " ${TARGET_BOARD} " ]]; then
78 echo "ERROR: Target $TARGET_BOARD not supported"
79 exit 1
80fi
81
82if [[ ! " ${SUPPORTED_APP[@]} " =~ " ${APP} " ]]; then
83 echo "ERROR: Application $APP not supported"
84 exit 1
85fi
86
87if [[ ! " ${SUPPORTED_TOOLCHAIN[@]} " =~ " ${TOOLCHAIN} " ]]; then
88 echo "ERROR: Toolchain $TOOLCHAIN not supported"
89 exit 1
90fi
91
92if [[ ! " ${SUPPORTED_PROFILES[@]} " =~ " ${PROFILE} " ]]; then
93 echo "ERROR: Profile $PROFILE not supported"
94 exit 1
95fi
96
Artur Tynecki42a7b0a2022-01-26 05:46:24 +010097if [[ ! " ${SUPPORTED_TYPE[@]} " =~ " ${TYPE} " ]]; then
98 echo "ERROR: Type $TYPE not supported"
99 exit 1
100fi
101
102if [[ "$TYPE" == "boot" ]] && [[ "$PROFILE" == "debug" ]]; then
103 echo "ERROR: The $TYPE application type does not supprort ""$PROFILE profile"
104 exit 1
105fi
106
Artur Tynecki35fb13e2021-10-12 18:23:45 +0200107set -e # Exit immediately if a command exits with a non-zero status.
Vincent Coubardcc135602021-06-29 16:58:47 +0100108
109# Activate Matter environment
110source "$CHIP_ROOT"/scripts/activate.sh
111
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100112# Application directory setup
113APP_DIRECTORY="$CHIP_ROOT"/examples/"$APP"/mbed
114
Vincent Coubardcc135602021-06-29 16:58:47 +0100115# Build directory setup
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100116BUILD_DIRECTORY="$APP_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"
Vincent Coubardcc135602021-06-29 16:58:47 +0100117
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100118# Set bootloader root directory
119BOOTLOADER_ROOT_DIRECTORY="$CHIP_ROOT"/examples/platform/mbed/bootloader
120
121# Set bootloader build directory
122BOOTLOADER_BUILD_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"/
123
124# Set encryption key directory
125ENC_KEY_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/enc-key
126
Vincent Coubardcc135602021-06-29 16:58:47 +0100127if [[ "$COMMAND" == *"build"* ]]; then
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100128 echo "Build $TYPE $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile"
Vincent Coubardcc135602021-06-29 16:58:47 +0100129
Artur Tynecki35fb13e2021-10-12 18:23:45 +0200130 # Set Mbed OS path
Artur Tynecki87166082021-11-02 14:58:55 +0100131 MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo
Vincent Coubardcc135602021-06-29 16:58:47 +0100132
Artur Tynecki35fb13e2021-10-12 18:23:45 +0200133 # Set Mbed OS posix socket submodule path
Artur Tynecki87166082021-11-02 14:58:55 +0100134 MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo
Vincent Coubardcc135602021-06-29 16:58:47 +0100135
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100136 # Set Mbed MCU boot path
137 MBED_MCU_BOOT_PATH="$CHIP_ROOT"/third_party/mbed-mcu-boot/repo
138
139 if [[ "$TYPE" == "boot" ]]; then
140 cd "$BOOTLOADER_ROOT_DIRECTORY"
141
142 # Install mcuboot requirements (silently)
143 pip install -q -r "$MBED_MCU_BOOT_PATH"/scripts/requirements.txt
144
145 # Run mcuboot setup script
146 python "$MBED_MCU_BOOT_PATH"/scripts/setup.py install
147
148 # Check if encryption key exists, if not generate it
149 if [[ ! -f "$ENC_KEY_DIRECTORY"/enc-key.pem ]]; then
150 mkdir -p "$ENC_KEY_DIRECTORY"
151 "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py keygen -k "$ENC_KEY_DIRECTORY"/enc-key.pem -t rsa-2048
152 fi
153
154 # Create the signing keys source fille
155 "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py getpub -k "$ENC_KEY_DIRECTORY"/enc-key.pem >signing_keys.c
156
157 ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed mcuboot
158
159 # Generate config file for selected target, toolchain and hardware
160 mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -o "$BOOTLOADER_BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH"
161
162 # Remove old artifacts to force linking
163 rm -rf "$BOOTLOADER_BUILD_DIRECTORY/chip-"*
164
165 # Build application
166 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"
167 cmake --build "$BOOTLOADER_BUILD_DIRECTORY"
168
169 cd "$CHIP_ROOT"/examples
170 fi
171
172 if [[ "$TYPE" == "upgrade" ]]; then
173 # Check if encryption key exists
174 if [[ ! -f "$ENC_KEY_DIRECTORY"/enc-key.pem ]]; then
175 echo "ERROR: encryption key for upgrade image not exist"
176 exit 1
177 fi
178 fi
179
180 # Set Mbed OS posix socket submodule path
181 MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo
182
183 if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100184 ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed "$APP_DIRECTORY"/mcuboot
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100185 fi
186
Vincent Coubardcc135602021-06-29 16:58:47 +0100187 # Generate config file for selected target, toolchain and hardware
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100188 mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -p "$APP_DIRECTORY" -o "$BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH"
Vincent Coubardcc135602021-06-29 16:58:47 +0100189
190 # Remove old artifacts to force linking
191 rm -rf "$BUILD_DIRECTORY/chip-"*
192
Vincent Coubardcc135602021-06-29 16:58:47 +0100193 # Build application
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100194 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"
Vincent Coubardcc135602021-06-29 16:58:47 +0100195 cmake --build "$BUILD_DIRECTORY"
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100196
197 if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100198 APP_VERSION=$(jq '.config."version-number-str".value' "$APP_DIRECTORY"/mbed_app.json | tr -d '\\"')
199 HEADER_SIZE=$(jq '.target_overrides.'\""$TARGET_BOARD"\"'."mcuboot.header-size"' "$APP_DIRECTORY"/mbed_app.json | tr -d \")
200 SLOT_SIZE=$(jq '.target_overrides.'\""$TARGET_BOARD"\"'."mcuboot.slot-size"' "$APP_DIRECTORY"/mbed_app.json | tr -d \")
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100201
202 if [[ "$TYPE" == "boot" ]]; then
203 # Signed the primary application
204 "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py sign -k "$ENC_KEY_DIRECTORY"/enc-key.pem \
205 --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]}" \
206 "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex
207 # Create the factory firmware (bootloader + signed primary application)
208 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
209 elif [[ "$TYPE" == "upgrade" ]]; then
210 # Signed the secondary application
211 "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py sign -k "$ENC_KEY_DIRECTORY"/enc-key.pem \
212 --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]}" \
213 "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex
214 # Convert hex image to raw binary file
215 arm-none-eabi-objcopy -I ihex -O binary "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.bin
Artur Tyneckifbf8bab2022-02-16 06:36:17 +0100216 python "$CHIP_ROOT"/examples/platform/mbed/ota/generate_ota_list_image.py "$APP_DIRECTORY"/mbed_app.json "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.bin
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100217 fi
218 fi
Vincent Coubardcc135602021-06-29 16:58:47 +0100219fi
220
221if [[ "$COMMAND" == *"flash"* ]]; then
222
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100223 echo "Flash $TYPE $APP app to $TARGET_BOARD target [$TOOLCHAIN toolchain, $PROFILE profile]"
Vincent Coubardcc135602021-06-29 16:58:47 +0100224
225 # Flash scripts path setup
226 MBED_FLASH_SCRIPTS_PATH=$CHIP_ROOT/config/mbed/scripts
227
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100228 APP_PATH="$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.elf
229
Vincent Coubardcc135602021-06-29 16:58:47 +0100230 # Flash application
Artur Tynecki42a7b0a2022-01-26 05:46:24 +0100231 "$OPENOCD_PATH"/bin/openocd -f "$MBED_FLASH_SCRIPTS_PATH/$TARGET_BOARD".tcl -c "program $APP_PATH verify reset exit"
Vincent Coubardcc135602021-06-29 16:58:47 +0100232fi