Skip to content

Commit

Permalink
Refactor fail function to remove mode parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
sudotensor committed Aug 4, 2021
1 parent 83d22ec commit 67d89c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
18 changes: 9 additions & 9 deletions scripts/fota.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ valid_mount () {
say note "Mount point not provided - binaries will not be flashed."
skip=1
else
[ -d "$mount" ] || fail exit "Mount point invalid" \
[ -d "$mount" ] || fail "Mount point invalid" \
"Please check the path and if the board is connected" \
"Tip: Use mbed-ls to identify the mount point path"
fi
Expand All @@ -105,7 +105,7 @@ valid_mount () {
valid_example () {
case $example in
mock|mcuboot) ;;
*) fail exit "Invalid example" "Supported examples - [mock|mcuboot]" ;;
*) fail "Invalid example" "Supported examples - [mock|mcuboot]" ;;
esac
}

Expand All @@ -114,15 +114,15 @@ valid_example () {
valid_board () {
case $board in
NRF52840_DK) ;;
*) fail exit "Unsupported board" "The only supported board is NRF52840_DK"
*) fail "Unsupported board" "The only supported board is NRF52840_DK"
esac
}

# Checks if the toolchain is GCC_ARM as it's the only one supported. The ARM toolchain may be supported in the future.
valid_toolchain () {
case $toolchain in
GCC_ARM) ;;
*) fail exit "Unsupported toolchain" "The only supported toolchain is GCC_ARM"
*) fail "Unsupported toolchain" "The only supported toolchain is GCC_ARM"
esac
}

Expand All @@ -144,7 +144,7 @@ setup_virtualenv () {
# Create the venv directory and setup the virtual environment
# shellcheck disable=SC2015
mkdir venv && python3 -m venv venv \
|| fail exit "Virtual environment creation failed!" "Tip: Check your python installation!"
|| fail "Virtual environment creation failed!" "Tip: Check your python installation!"
fi
source venv/bin/activate
say success "Virtual environment activated"
Expand All @@ -155,7 +155,7 @@ install_requirements () {
say message "Installing/updating requirements silently..."
# shellcheck disable=SC2015
pip install -q --upgrade pip && pip install -q -r "$root/requirements.txt" \
|| fail exit "Unable to install requirements!" "Please take a look at requirements.txt"
|| fail "Unable to install requirements!" "Please take a look at requirements.txt"
say success "General requirements installed/updated"
}

Expand All @@ -172,7 +172,7 @@ build_example () {
# Cleanup routine that runs when the program exits (either successfully or abruptly)
cleanup () {
# If unsuccessful termination, then clean the build
[ "$1" -eq 1 ] && clean
if [[ "$1" -eq 1 ]]; then clean; fi
}

#-----------------------------------------------------------------------------------------------------------------------
Expand All @@ -189,7 +189,7 @@ main () {
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &>/dev/null && pwd -P)

setup_formatting
parse_options "$@" || fail exit "Unrecognised option" "Please use -h or --help for usage"
parse_options "$@" || fail "Unrecognised option" "Please use -h or --help for usage"

check_usage
setup_virtualenv
Expand All @@ -198,4 +198,4 @@ main () {
}

main "$@"
exit
exit
34 changes: 17 additions & 17 deletions scripts/mcuboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ mcuboot_build () {
# 1. Install application dependencies - mbed-os (silently)
# shellcheck disable=SC2015
cd "$application" && mbed-tools deploy > /dev/null 2>&1 || \
fail exit "Unable to install application dependencies" \
fail "Unable to install application dependencies" \
"Please check mcuboot.lib, mbed-os.lib, and mbed-os-experimental-ble-services.lib"

# 2. Install bootloader dependencies (silently)
# shellcheck disable=SC2015
cd "$bootloader" && mbed-tools deploy > /dev/null 2>&1 || \
fail exit "Unable to install bootloader dependencies" \
fail "Unable to install bootloader dependencies" \
"Please check mcuboot.lib and mbed-os.lib"

# 3. Install mbed-os python dependencies (silently)
pip install -q -r mbed-os/requirements.txt || \
fail exit "Unable to install mbed-os requirements" "Please take a look at mbed-os/requirements.txt"
fail "Unable to install mbed-os requirements" "Please take a look at mbed-os/requirements.txt"

# A short message addressing the known Click dependency conflict - this should be removed once resolved.
say note "Click dependency conflict" \
Expand All @@ -60,11 +60,11 @@ mcuboot_build () {

# 4. Install mcuboot requirements (silently)
pip install -q -r mcuboot/scripts/requirements.txt || \
fail exit "Unable to install mcuboot requirements" "Please take a look at mcuboot/scripts/requirements.txt"
fail "Unable to install mcuboot requirements" "Please take a look at mcuboot/scripts/requirements.txt"

# 5. Run mcuboot setup script (silently)
python mcuboot/scripts/setup.py install > /dev/null 2>&1 || \
fail exit "MCUboot setup script failed"
fail "MCUboot setup script failed"

say success "Example requirements installed/updated"
say message "Creating the signing keys and building the bootloader..."
Expand All @@ -74,12 +74,12 @@ mcuboot_build () {
# shellcheck disable=SC2015
mcuboot/scripts/imgtool.py keygen -k signing-keys.pem -t rsa-2048 >/dev/null && \
mcuboot/scripts/imgtool.py getpub -k signing-keys.pem >> signing_keys.c || \
fail exit "Unable to create the signing keys"
fail "Unable to create the signing keys"

# 7. Build the bootloader using the old mbed-cli
# Note: This does not silence errors
mbed compile -t "$toolchain" -m "$board" >/dev/null || \
fail exit "Failed to compile the bootloader" "Please check the sources"
fail "Failed to compile the bootloader" "Please check the sources"

say success "Created signing keys and built the bootloader"
say message "Building and signing the primary application..."
Expand All @@ -88,14 +88,14 @@ mcuboot_build () {
# Note: This does not silence errors
# shellcheck disable=SC2015
cd "$application" && mbed compile -t "$toolchain" -m "$board" >/dev/null || \
fail exit "Failed to compile the bootloader" "Please check the sources"
fail "Failed to compile the bootloader" "Please check the sources"

# shellcheck disable=SC2015
cp "BUILD/$board/$toolchain/application.hex" "$bootloader" && cd "$bootloader" && \
mcuboot/scripts/imgtool.py sign -k signing-keys.pem \
--align 4 -v 0.1.0 --header-size 4096 --pad-header -S 0xC0000 \
--pad application.hex signed_application.hex || \
fail exit "Unable to sign the primary application"
fail "Unable to sign the primary application"

say success "Built and signed the primary application"
say message "Deactivating virtual environment to setup a new one..."
Expand All @@ -111,7 +111,7 @@ mcuboot_build () {
# 10. Create a new, temporary virtual environment just for pyocd and intelhex
# shellcheck disable=SC2015
mkdir venv && python3 -m venv venv || \
fail exit "Virtual environment creation failed!" "Tip: Check your python installation!"
fail "Virtual environment creation failed!" "Tip: Check your python installation!"

# 11. Activate temporary virtual environment
source venv/bin/activate
Expand All @@ -122,18 +122,18 @@ mcuboot_build () {
# 12. Install requirements (pyocd and intelhex) for temporary environment (silently)
# shellcheck disable=SC2015
pip install -q --upgrade pip && pip install -q pyocd==0.30.3 intelhex==2.3.0 || \
fail exit "Unable to install temporary venv requirements" "Please check scripts/mcuboot.sh"
fail "Unable to install temporary venv requirements" "Please check scripts/mcuboot.sh"

say success "Requirements installed/updated"

# 13. Create the factory firmware
hexmerge.py -o merged.hex --no-start-addr "BUILD/$board/$toolchain/bootloader.hex" signed_application.hex || \
fail exit "Unable to create factory firmware"
fail "Unable to create factory firmware"

# 14. Flash the board with the binary (if skip is 0)
if [[ "$skip" -eq 0 ]]; then
pyocd erase --chip && cp merged.hex "$mount" \\
fail exit "Unable to flash firmware!" "Please ensure the board is connected"
fail "Unable to flash firmware!" "Please ensure the board is connected"
say success "Factory firmware flashed"
else
say message "Factory firmware at $root/mcuboot/target/bootloader/merged.hex"
Expand Down Expand Up @@ -163,24 +163,24 @@ mcuboot_build () {
cd "$application" && \
jq '."config"."version-number"."value" = "\"0.1.1\""' --indent 4 mbed_app.json > tmp.$$.json \
&& mv tmp.$$.json mbed_app.json || \
fail exit "Failed in updating application/mbed_app.json" "Please check scripts/mcuboot.sh"
fail "Failed in updating application/mbed_app.json" "Please check scripts/mcuboot.sh"
fi

say message "Creating the update binary..."

# shellcheck disable=SC2015
cd "$application" && mbed compile -t "$toolchain" -m "$board" >/dev/null || \
fail exit "Failed to compile the application" "Please check the sources"
fail "Failed to compile the application" "Please check the sources"

# shellcheck disable=SC2015
cp "BUILD/$board/$toolchain/application.hex" "$bootloader" && cd "$bootloader" && \
mcuboot/scripts/imgtool.py sign -k signing-keys.pem \
--align 4 -v 0.1.1 --header-size 4096 --pad-header -S 0x55000 \
application.hex signed_update.hex || \
fail exit "Unable to sign the updated application"
fail "Unable to sign the updated application"

arm-none-eabi-objcopy -I ihex -O binary signed_update.hex signed_update.bin || \
fail exit "Failed to extract binary from elf" "Tip: Check if arm-none-eabi-objcopy is in your path"
fail "Failed to extract binary from elf" "Tip: Check if arm-none-eabi-objcopy is in your path"

say message "Update binary at $root/mcuboot/target/bootloader/signed_update.hex"
say success "Build Complete" "Please refer to the documentation for demonstration instructions"
Expand Down
10 changes: 5 additions & 5 deletions scripts/mock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ mock_build () {
# 1. Install mbed-os and mbed-os experimental-ble-services (silently)
# shellcheck disable=SC2015
cd "$root/mock/target" && mbed-tools deploy > /dev/null 2>&1 || \
fail exit "Unable to install mbed-os or mbed-os-experimental-ble-services dependency"
fail "Unable to install mbed-os or mbed-os-experimental-ble-services dependency"

# 2. Install mbed-os python dependencies
pip install -q -r mbed-os/requirements.txt || \
fail exit "Unable to install mbed-os requirements" "Please take a look at mbed-os/requirements.txt"
fail "Unable to install mbed-os requirements" "Please take a look at mbed-os/requirements.txt"

# A short message addressing the known Click dependency conflict - this should be removed once resolved.
say note "Click dependency conflict" \
Expand All @@ -41,17 +41,17 @@ mock_build () {
# 3. Compile the example with the target board and toolchain
# Note: This does not silence errors.
mbed-tools compile -t "$toolchain" -m "$board" >/dev/null || \
fail exit "Failed to compile the example" "Please check the sources"
fail "Failed to compile the example" "Please check the sources"

# 4. Convert the output .elf executable to a .bin as it's what NRF52840_DK requires
arm-none-eabi-objcopy -O binary "$out/BLE_GattServer_FOTAService.elf" "$out/BLE_GattServer_FOTAService.bin" || \
fail exit "Failed to extract binary from elf" "Tip: Check if arm-none-eabi-objcopy is in your path"
fail "Failed to extract binary from elf" "Tip: Check if arm-none-eabi-objcopy is in your path"

# 5. Flash the board with the binary (if skip is 0)
# shellcheck disable=SC2015
if [[ "$skip" -eq 0 ]]; then
cp "$out/BLE_GattServer_FOTAService.bin" "$mount" || \
fail exit "Unable to flash binary!" "Please ensure the board is connected"
fail "Unable to flash binary!" "Please ensure the board is connected"
say success "Binary flashed"
else
say message "Binary at $root/mock/target/$out/BLE_GattServer_FOTAService.bin"
Expand Down
6 changes: 2 additions & 4 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ say () {

# Prints an error message to stderr and exits (or returns) with a code of 1
fail () {
local mode=${1}
local msg=( "${@:2}" )
say error "${msg[@]}"
$mode 1
say error "$@"
exit 1
}

0 comments on commit 67d89c5

Please sign in to comment.