1#!/usr/bin/env bash
2#
3# Demonstrates command-line interface of OTA Partitions Tool, otatool.py
4#
5#
6# $1 - serial port where target device to operate on is connnected to, by default the first found valid serial port
7# $2 - path to this example's built binary file (parttool.bin), by default $PWD/build/otatool.bin
8
9PORT=$1
10OTATOOL_PY="python $IDF_PATH/components/app_update/otatool.py -q"
11
12if [[ "$PORT" != "" ]]; then
13    OTATOOL_PY="$OTATOOL_PY --port $PORT"
14fi
15
16BINARY=$2
17
18if [[ "$BINARY" == "" ]]; then
19    BINARY=build/otatool.bin
20fi
21
22function assert_file_same()
23{
24    sz_a=$(stat -c %s $1)
25    sz_b=$(stat -c %s $2)
26    sz=$((sz_a < sz_b ? sz_a : sz_b))
27    res=$(cmp -s -n $sz $1 $2) ||
28        (echo "!!!!!!!!!!!!!!!!!!!"
29        echo "FAILURE: $3"
30        echo "!!!!!!!!!!!!!!!!!!!")
31}
32
33function assert_running_partition()
34{
35    running=$(python get_running_partition.py)
36    if [[ "$running" != "$1" ]]; then
37        echo "!!!!!!!!!!!!!!!!!!!"
38        echo "FAILURE: Running partition '$running' does not match expected '$1'"
39        echo "!!!!!!!!!!!!!!!!!!!"
40        exit 1
41    fi
42}
43
44# Flash the example firmware to OTA partitions. The first write uses slot number to identify OTA
45# partition, the second one uses the name.
46echo "Writing factory firmware to ota_0"
47$OTATOOL_PY write_ota_partition --slot 0 --input $BINARY
48
49echo "Writing factory firmware to ota_1"
50$OTATOOL_PY write_ota_partition --name ota_1 --input $BINARY
51
52# Read back the written firmware
53$OTATOOL_PY read_ota_partition --name ota_0 --output app0.bin
54$OTATOOL_PY read_ota_partition --slot 1 --output app1.bin
55
56assert_file_same $BINARY app0.bin "Slot 0 app does not match factory app"
57assert_file_same $BINARY app1.bin "Slot 1 app does not match factory app"
58
59# Switch to factory app
60echo "Switching to factory app"
61$OTATOOL_PY erase_otadata
62assert_running_partition factory
63
64# Switch to slot 0
65echo "Switching to OTA slot 0"
66$OTATOOL_PY switch_ota_partition --slot 0
67assert_running_partition ota_0
68
69# Switch to slot 1 twice in a row
70echo "Switching to OTA slot 1 (twice in a row)"
71$OTATOOL_PY switch_ota_partition --slot 1
72assert_running_partition ota_1
73$OTATOOL_PY switch_ota_partition --name ota_1
74assert_running_partition ota_1
75
76# Switch to slot 0 twice in a row
77echo "Switching to OTA slot 0 (twice in a row)"
78$OTATOOL_PY switch_ota_partition --slot 0
79assert_running_partition ota_0
80$OTATOOL_PY switch_ota_partition --name ota_0
81assert_running_partition ota_0
82
83# Switch to factory app
84echo "Switching to factory app"
85$OTATOOL_PY erase_otadata
86assert_running_partition factory
87
88# Switch to slot 1
89echo "Switching to OTA slot 1"
90$OTATOOL_PY switch_ota_partition --slot 1
91assert_running_partition ota_1
92
93# Example end and cleanup
94printf "\nPartition tool operations performed successfully\n"
95rm -rf app0.bin app1.bin
96