Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bootloader_update.c Source File

bootloader_update.c

Go to the documentation of this file.
00001 /**
00002  * @file    bootloader_update.c
00003  * @brief   Logic to perform a bootloader update when enabled
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2016-2019, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #ifdef DRAG_N_DROP_SUPPORT
00023 #include <stdbool.h>
00024 #include <string.h>
00025 #include "flash_manager.h"
00026 #include "util.h"
00027 #include "bootloader.h"
00028 #include "info.h"
00029 #include "daplink.h"
00030 #include "crc.h"
00031 
00032 // Supress the warning 'null argument provided for parameter marked with attribute "nonnull"'
00033 // since the vector table is at address 0
00034 #pragma diag_suppress 2748
00035 
00036 #if !defined(DAPLINK_BOOTLOADER_UPDATE)
00037     #define DAPLINK_BOOTLOADER_UPDATE       0
00038 #endif
00039 
00040 #if DAPLINK_BOOTLOADER_UPDATE
00041     // The bootloader must be built first or this header will not be found
00042     #include "bootloader_image.c"
00043 #else //DAPLINK_BOOTLOADER_UPDATE
00044     static const unsigned int image_start = 0;
00045     static const unsigned int image_size = 0;
00046     static const char image_data[1];
00047 #endif //DAPLINK_BOOTLOADER_UPDATE
00048 
00049 static bool interface_image_valid()
00050 {
00051     uint32_t stored_crc;
00052     uint32_t computed_crc;
00053     
00054     stored_crc = *(uint32_t *)(DAPLINK_ROM_IF_START + DAPLINK_ROM_IF_SIZE - 4);
00055     computed_crc = crc32((void *)DAPLINK_ROM_IF_START, DAPLINK_ROM_IF_SIZE - 4);
00056     return computed_crc == stored_crc;
00057 }
00058 
00059 void bootloader_check_and_update(void)
00060 {
00061     int same;
00062     error_t ret;
00063     bool update_present = image_size > 0;
00064 
00065     if (!update_present) {
00066         return;
00067     }
00068 
00069     if (info_get_bootloader_present() &&
00070             (info_get_bootloader_version() > DAPLINK_VERSION)) {
00071         // Bootloader is more recent than the one we have so
00072         // don't change it
00073         return;
00074     }
00075 
00076     if (!interface_image_valid()) {
00077         // The interface is corrupt so don't attempt
00078         // to apply the update
00079         util_assert(0);
00080         return;
00081     }
00082 
00083     same = memcmp((void*)image_start, image_data, image_size) == 0;
00084     if (!same) {
00085         ret = flash_manager_init(flash_intf_iap_protected);
00086         if (ret != ERROR_SUCCESS) {
00087             util_assert(0);
00088             return;
00089         }
00090 
00091         ret = flash_manager_data(image_start, (const uint8_t*)image_data, image_size);
00092         if (ret != ERROR_SUCCESS) {
00093             flash_manager_uninit();
00094             util_assert(0);
00095             return;
00096         }
00097 
00098         ret = flash_manager_uninit();
00099         if (ret != ERROR_SUCCESS) {
00100             util_assert(0);
00101             return;
00102         }
00103     }
00104 }
00105 #else //DRAG_N_DROP_SUPPORT
00106 
00107 void bootloader_check_and_update(void) {}
00108 
00109 #endif //DRAG_N_DROP_SUPPORT