|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Embedded Planet |
| 3 | + * Copyright (c) 2020 ARM Limited |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "mbed.h" |
| 8 | + |
| 9 | +#include "bootutil/bootutil.h" |
| 10 | +#include "bootutil/image.h" |
| 11 | +#include "flash_map_backend/secondary_bd.h" |
| 12 | + |
| 13 | +#include "FlashIAP/FlashIAPBlockDevice.h" |
| 14 | +#include "blockdevice/BufferedBlockDevice.h" |
| 15 | +#include "drivers/InterruptIn.h" |
| 16 | + |
| 17 | +#define TRACE_GROUP "UpdaterApp" |
| 18 | +#include "mbed-trace/mbed_trace.h" |
| 19 | + |
| 20 | +#if DEMO_BUTTON_ACTIVE_LOW |
| 21 | +#define DEMO_BUTTON_IS_PRESSED !btn |
| 22 | +#else |
| 23 | +#define DEMO_BUTTON_IS_PRESSED btn |
| 24 | +#endif |
| 25 | + |
| 26 | +// SimpleApp.bin gets loaded into ram between these addresses. |
| 27 | +// See CMakeLists.txt for details on how this is done. |
| 28 | +extern "C" uint8_t _binary_SimpleApp_bin_start; |
| 29 | +extern "C" uint8_t _binary_SimpleApp_bin_end; |
| 30 | +const size_t SimpleApp_bin_length = &_binary_SimpleApp_bin_end - &_binary_SimpleApp_bin_start; |
| 31 | + |
| 32 | +int main() |
| 33 | +{ |
| 34 | + // Enable traces from relevant trace groups |
| 35 | + mbed_trace_init(); |
| 36 | + mbed_trace_include_filters_set("UpdaterApp,MCUb,BL"); |
| 37 | + |
| 38 | + DigitalIn btn(DEMO_BUTTON); |
| 39 | + |
| 40 | + // Erase secondary slot |
| 41 | + // On the first boot, the secondary BlockDevice needs to be clean |
| 42 | + // If the first boot is not normal, please run the erase step, then reboot |
| 43 | + |
| 44 | + tr_info("> Press button to erase secondary slot"); |
| 45 | + |
| 46 | + while(!DEMO_BUTTON_IS_PRESSED) { |
| 47 | + ThisThread::sleep_for(10ms); |
| 48 | + } |
| 49 | + |
| 50 | + BlockDevice *secondary_bd = get_secondary_bd(); |
| 51 | + int ret = secondary_bd->init(); |
| 52 | + if (ret == 0) { |
| 53 | + tr_info("Secondary BlockDevice inited"); |
| 54 | + } else { |
| 55 | + tr_error("Cannot init secondary BlockDevice: %d", ret); |
| 56 | + } |
| 57 | + |
| 58 | + // Use a buffered block device to allow arbitrary length writes to the underlying BD |
| 59 | + BufferedBlockDevice bufferedSecBD(secondary_bd); |
| 60 | + |
| 61 | + tr_info("Erasing secondary BlockDevice..."); |
| 62 | + ret = bufferedSecBD.erase(0, bufferedSecBD.size()); |
| 63 | + if (ret == 0) { |
| 64 | + tr_info("Secondary BlockDevice erased"); |
| 65 | + } else { |
| 66 | + tr_error("Cannot erase secondary BlockDevice: %d", ret); |
| 67 | + } |
| 68 | + |
| 69 | + tr_info("> Press button to copy update image to secondary BlockDevice"); |
| 70 | + |
| 71 | + while(!DEMO_BUTTON_IS_PRESSED) { |
| 72 | + ThisThread::sleep_for(10ms); |
| 73 | + } |
| 74 | + |
| 75 | + // Copy the update image from internal flash to secondary BlockDevice |
| 76 | + bufferedSecBD.program(&_binary_SimpleApp_bin_start, 0, SimpleApp_bin_length); |
| 77 | + |
| 78 | + // Activate the image in the secondary BlockDevice |
| 79 | + tr_info("> Image copied to secondary BlockDevice, press button to activate"); |
| 80 | + |
| 81 | + while(!DEMO_BUTTON_IS_PRESSED) { |
| 82 | + ThisThread::sleep_for(10ms); |
| 83 | + } |
| 84 | + |
| 85 | + ret = boot_set_pending(false); |
| 86 | + if (ret == 0) { |
| 87 | + tr_info("> Secondary image pending, reboot to update"); |
| 88 | + } else { |
| 89 | + tr_error("Failed to set secondary image pending: %d", ret); |
| 90 | + } |
| 91 | +} |
0 commit comments