1#!/usr/bin/env python 2# 3# Demonstrates the use of otatool.py, a tool for performing ota partition level 4# operations. 5# 6# Copyright 2018 Espressif Systems (Shanghai) PTE LTD 7# 8# Licensed under the Apache License, Version 2.0 (the "License"); 9# you may not use this file except in compliance with the License. 10# You may obtain a copy of the License at 11# 12# http:#www.apache.org/licenses/LICENSE-2.0 13# 14# Unless required by applicable law or agreed to in writing, software 15# distributed under the License is distributed on an "AS IS" BASIS, 16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17# See the License for the specific language governing permissions and 18# limitations under the License. 19import argparse 20import os 21import sys 22 23from get_running_partition import get_running_partition 24 25 26def assert_file_same(file1, file2, err): 27 with open(file1, 'rb') as f1: 28 with open(file2, 'rb') as f2: 29 f1 = f1.read() 30 f2 = f2.read() 31 32 if len(f1) < len(f2): 33 f2 = f2[:len(f1)] 34 else: 35 f1 = f1[:len(f2)] 36 37 if not f1 == f2: 38 raise Exception(err) 39 40 41def assert_running_partition(expected, port=None): 42 running = get_running_partition(port) 43 if running != expected: 44 raise Exception('Running partition %s does not match expected %s' % (running, expected)) 45 46 47def main(): 48 COMPONENTS_PATH = os.path.expandvars(os.path.join('$IDF_PATH', 'components')) 49 OTATOOL_DIR = os.path.join(COMPONENTS_PATH, 'app_update') 50 51 sys.path.append(OTATOOL_DIR) 52 from otatool import OtatoolTarget 53 54 parser = argparse.ArgumentParser('ESP-IDF OTA Tool Example') 55 56 parser.add_argument('--port', '-p', help='port where the device to perform operations on is connected') 57 parser.add_argument('--binary', '-b', help='path to built example binary', default=os.path.join('build', 'otatool.bin')) 58 args = parser.parse_args() 59 60 target = OtatoolTarget(args.port) 61 62 print('Writing factory firmware to ota_0') 63 target.write_ota_partition(0, args.binary) 64 65 print('Writing factory firmware to ota_1') 66 target.write_ota_partition('ota_1', args.binary) 67 68 # Verify that the contents of the two ota slots are the same as that of the factory partition 69 print('Checking written firmware to ota_0 and ota_1 match factory firmware') 70 target.read_ota_partition('ota_0', 'app0.bin') 71 target.read_ota_partition(1, 'app1.bin') 72 73 assert_file_same('app0.bin', args.binary, 'Slot 0 app does not match factory app') 74 assert_file_same('app1.bin', args.binary, 'Slot 1 app does not match factory app') 75 76 # Switch to factory app 77 print('Switching to factory app') 78 target.erase_otadata() 79 assert_running_partition('factory') 80 81 # Switch to slot 0 82 print('Switching to OTA slot 0') 83 target.switch_ota_partition(0) 84 assert_running_partition('ota_0') 85 86 # Switch to slot 1 twice in a row 87 print('Switching to OTA slot 1 (twice in a row)') 88 target.switch_ota_partition(1) 89 assert_running_partition('ota_1') 90 target.switch_ota_partition('ota_1') 91 assert_running_partition('ota_1') 92 93 # Switch to slot 0 twice in a row 94 print('Switching to OTA slot 0 (twice in a row)') 95 target.switch_ota_partition(0) 96 assert_running_partition('ota_0') 97 target.switch_ota_partition('ota_0') 98 assert_running_partition('ota_0') 99 100 # Switch to factory app 101 print('Switching to factory app') 102 target.erase_otadata() 103 assert_running_partition('factory') 104 105 # Switch to slot 1 106 print('Switching to OTA slot 1') 107 target.switch_ota_partition(1) 108 assert_running_partition('ota_1') 109 110 # Example end and cleanup 111 print('\nOTA tool operations executed successfully!') 112 clean_files = ['app0.bin', 'app1.bin'] 113 for clean_file in clean_files: 114 os.unlink(clean_file) 115 116 117if __name__ == '__main__': 118 main() 119