Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 1 | #!/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 | |
| 19 | cd "$(dirname "$0")"/../.. |
| 20 | CHIP_ROOT=$PWD |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 21 | |
| 22 | SUPPORTED_TOOLCHAIN=(GCC_ARM ARM) |
Artur Tynecki | 35fb13e | 2021-10-12 18:23:45 +0200 | [diff] [blame] | 23 | SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W) |
Boris Zbarsky | 583ac0d | 2022-05-28 13:08:33 -0400 | [diff] [blame] | 24 | SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app all-clusters-minimal-app shell ota-requestor-app) |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 25 | SUPPORTED_PROFILES=(release develop debug) |
| 26 | SUPPORTED_COMMAND=(build flash build-flash) |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 27 | SUPPORTED_TYPE=(simple boot upgrade) |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 28 | |
| 29 | COMMAND=build |
Artur Tynecki | 35fb13e | 2021-10-12 18:23:45 +0200 | [diff] [blame] | 30 | APP=lock-app |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 31 | TARGET_BOARD=CY8CPROTO_062_4343W |
| 32 | TOOLCHAIN=GCC_ARM |
| 33 | PROFILE=release |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 34 | TYPE=simple |
| 35 | |
| 36 | TARGET_MEMORY_ALIGN[CY8CPROTO_062_4343W]=8 |
| 37 | TARGET_BOOT_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0 |
| 38 | TARGET_UPGRADE_IMAGE_ERASE_VALUE[CY8CPROTO_062_4343W]=0xff |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 39 | |
| 40 | for 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 Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 62 | -T=* | --type=*) |
| 63 | TYPE="${i#*=}" |
| 64 | shift |
| 65 | ;; |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 66 | *) |
| 67 | # unknown option |
| 68 | ;; |
| 69 | esac |
| 70 | done |
| 71 | |
| 72 | if [[ ! " ${SUPPORTED_COMMAND[@]} " =~ " ${COMMAND} " ]]; then |
| 73 | echo "ERROR: Command $COMMAND not supported" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | if [[ ! " ${SUPPORTED_TARGET_BOARD[@]} " =~ " ${TARGET_BOARD} " ]]; then |
| 78 | echo "ERROR: Target $TARGET_BOARD not supported" |
| 79 | exit 1 |
| 80 | fi |
| 81 | |
| 82 | if [[ ! " ${SUPPORTED_APP[@]} " =~ " ${APP} " ]]; then |
| 83 | echo "ERROR: Application $APP not supported" |
| 84 | exit 1 |
| 85 | fi |
| 86 | |
| 87 | if [[ ! " ${SUPPORTED_TOOLCHAIN[@]} " =~ " ${TOOLCHAIN} " ]]; then |
| 88 | echo "ERROR: Toolchain $TOOLCHAIN not supported" |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | if [[ ! " ${SUPPORTED_PROFILES[@]} " =~ " ${PROFILE} " ]]; then |
| 93 | echo "ERROR: Profile $PROFILE not supported" |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 97 | if [[ ! " ${SUPPORTED_TYPE[@]} " =~ " ${TYPE} " ]]; then |
| 98 | echo "ERROR: Type $TYPE not supported" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
| 102 | if [[ "$TYPE" == "boot" ]] && [[ "$PROFILE" == "debug" ]]; then |
| 103 | echo "ERROR: The $TYPE application type does not supprort ""$PROFILE profile" |
| 104 | exit 1 |
| 105 | fi |
| 106 | |
Artur Tynecki | 35fb13e | 2021-10-12 18:23:45 +0200 | [diff] [blame] | 107 | set -e # Exit immediately if a command exits with a non-zero status. |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 108 | |
| 109 | # Activate Matter environment |
| 110 | source "$CHIP_ROOT"/scripts/activate.sh |
| 111 | |
Artur Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 112 | # Application directory setup |
| 113 | APP_DIRECTORY="$CHIP_ROOT"/examples/"$APP"/mbed |
| 114 | |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 115 | # Build directory setup |
Artur Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 116 | BUILD_DIRECTORY="$APP_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE" |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 117 | |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 118 | # Set bootloader root directory |
| 119 | BOOTLOADER_ROOT_DIRECTORY="$CHIP_ROOT"/examples/platform/mbed/bootloader |
| 120 | |
| 121 | # Set bootloader build directory |
| 122 | BOOTLOADER_BUILD_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"/ |
| 123 | |
| 124 | # Set encryption key directory |
| 125 | ENC_KEY_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/enc-key |
| 126 | |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 127 | if [[ "$COMMAND" == *"build"* ]]; then |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 128 | echo "Build $TYPE $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile" |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 129 | |
Artur Tynecki | 35fb13e | 2021-10-12 18:23:45 +0200 | [diff] [blame] | 130 | # Set Mbed OS path |
Artur Tynecki | 8716608 | 2021-11-02 14:58:55 +0100 | [diff] [blame] | 131 | MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 132 | |
Artur Tynecki | 35fb13e | 2021-10-12 18:23:45 +0200 | [diff] [blame] | 133 | # Set Mbed OS posix socket submodule path |
Artur Tynecki | 8716608 | 2021-11-02 14:58:55 +0100 | [diff] [blame] | 134 | MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 135 | |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 136 | # 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 Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 184 | ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed "$APP_DIRECTORY"/mcuboot |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 185 | fi |
| 186 | |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 187 | # Generate config file for selected target, toolchain and hardware |
Artur Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 188 | mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -p "$APP_DIRECTORY" -o "$BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH" |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 189 | |
| 190 | # Remove old artifacts to force linking |
| 191 | rm -rf "$BUILD_DIRECTORY/chip-"* |
| 192 | |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 193 | # Build application |
Artur Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 194 | 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 Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 195 | cmake --build "$BUILD_DIRECTORY" |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 196 | |
| 197 | if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then |
Artur Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 198 | 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 Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 201 | |
| 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 Tynecki | fbf8bab | 2022-02-16 06:36:17 +0100 | [diff] [blame] | 216 | 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 Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 217 | fi |
| 218 | fi |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 219 | fi |
| 220 | |
| 221 | if [[ "$COMMAND" == *"flash"* ]]; then |
| 222 | |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 223 | echo "Flash $TYPE $APP app to $TARGET_BOARD target [$TOOLCHAIN toolchain, $PROFILE profile]" |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 224 | |
| 225 | # Flash scripts path setup |
| 226 | MBED_FLASH_SCRIPTS_PATH=$CHIP_ROOT/config/mbed/scripts |
| 227 | |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 228 | APP_PATH="$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.elf |
| 229 | |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 230 | # Flash application |
Artur Tynecki | 42a7b0a | 2022-01-26 05:46:24 +0100 | [diff] [blame] | 231 | "$OPENOCD_PATH"/bin/openocd -f "$MBED_FLASH_SCRIPTS_PATH/$TARGET_BOARD".tcl -c "program $APP_PATH verify reset exit" |
Vincent Coubard | cc13560 | 2021-06-29 16:58:47 +0100 | [diff] [blame] | 232 | fi |