Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file bootloader_update.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief Logic to perform a bootloader update when enabled
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2016-2019, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #ifdef DRAG_N_DROP_SUPPORT
Pawel Zarembski 0:01f31e923fe2 23 #include <stdbool.h>
Pawel Zarembski 0:01f31e923fe2 24 #include <string.h>
Pawel Zarembski 0:01f31e923fe2 25 #include "flash_manager.h"
Pawel Zarembski 0:01f31e923fe2 26 #include "util.h"
Pawel Zarembski 0:01f31e923fe2 27 #include "bootloader.h"
Pawel Zarembski 0:01f31e923fe2 28 #include "info.h"
Pawel Zarembski 0:01f31e923fe2 29 #include "daplink.h"
Pawel Zarembski 0:01f31e923fe2 30 #include "crc.h"
Pawel Zarembski 0:01f31e923fe2 31
Pawel Zarembski 0:01f31e923fe2 32 // Supress the warning 'null argument provided for parameter marked with attribute "nonnull"'
Pawel Zarembski 0:01f31e923fe2 33 // since the vector table is at address 0
Pawel Zarembski 0:01f31e923fe2 34 #pragma diag_suppress 2748
Pawel Zarembski 0:01f31e923fe2 35
Pawel Zarembski 0:01f31e923fe2 36 #if !defined(DAPLINK_BOOTLOADER_UPDATE)
Pawel Zarembski 0:01f31e923fe2 37 #define DAPLINK_BOOTLOADER_UPDATE 0
Pawel Zarembski 0:01f31e923fe2 38 #endif
Pawel Zarembski 0:01f31e923fe2 39
Pawel Zarembski 0:01f31e923fe2 40 #if DAPLINK_BOOTLOADER_UPDATE
Pawel Zarembski 0:01f31e923fe2 41 // The bootloader must be built first or this header will not be found
Pawel Zarembski 0:01f31e923fe2 42 #include "bootloader_image.c"
Pawel Zarembski 0:01f31e923fe2 43 #else //DAPLINK_BOOTLOADER_UPDATE
Pawel Zarembski 0:01f31e923fe2 44 static const unsigned int image_start = 0;
Pawel Zarembski 0:01f31e923fe2 45 static const unsigned int image_size = 0;
Pawel Zarembski 0:01f31e923fe2 46 static const char image_data[1];
Pawel Zarembski 0:01f31e923fe2 47 #endif //DAPLINK_BOOTLOADER_UPDATE
Pawel Zarembski 0:01f31e923fe2 48
Pawel Zarembski 0:01f31e923fe2 49 static bool interface_image_valid()
Pawel Zarembski 0:01f31e923fe2 50 {
Pawel Zarembski 0:01f31e923fe2 51 uint32_t stored_crc;
Pawel Zarembski 0:01f31e923fe2 52 uint32_t computed_crc;
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 stored_crc = *(uint32_t *)(DAPLINK_ROM_IF_START + DAPLINK_ROM_IF_SIZE - 4);
Pawel Zarembski 0:01f31e923fe2 55 computed_crc = crc32((void *)DAPLINK_ROM_IF_START, DAPLINK_ROM_IF_SIZE - 4);
Pawel Zarembski 0:01f31e923fe2 56 return computed_crc == stored_crc;
Pawel Zarembski 0:01f31e923fe2 57 }
Pawel Zarembski 0:01f31e923fe2 58
Pawel Zarembski 0:01f31e923fe2 59 void bootloader_check_and_update(void)
Pawel Zarembski 0:01f31e923fe2 60 {
Pawel Zarembski 0:01f31e923fe2 61 int same;
Pawel Zarembski 0:01f31e923fe2 62 error_t ret;
Pawel Zarembski 0:01f31e923fe2 63 bool update_present = image_size > 0;
Pawel Zarembski 0:01f31e923fe2 64
Pawel Zarembski 0:01f31e923fe2 65 if (!update_present) {
Pawel Zarembski 0:01f31e923fe2 66 return;
Pawel Zarembski 0:01f31e923fe2 67 }
Pawel Zarembski 0:01f31e923fe2 68
Pawel Zarembski 0:01f31e923fe2 69 if (info_get_bootloader_present() &&
Pawel Zarembski 0:01f31e923fe2 70 (info_get_bootloader_version() > DAPLINK_VERSION)) {
Pawel Zarembski 0:01f31e923fe2 71 // Bootloader is more recent than the one we have so
Pawel Zarembski 0:01f31e923fe2 72 // don't change it
Pawel Zarembski 0:01f31e923fe2 73 return;
Pawel Zarembski 0:01f31e923fe2 74 }
Pawel Zarembski 0:01f31e923fe2 75
Pawel Zarembski 0:01f31e923fe2 76 if (!interface_image_valid()) {
Pawel Zarembski 0:01f31e923fe2 77 // The interface is corrupt so don't attempt
Pawel Zarembski 0:01f31e923fe2 78 // to apply the update
Pawel Zarembski 0:01f31e923fe2 79 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 80 return;
Pawel Zarembski 0:01f31e923fe2 81 }
Pawel Zarembski 0:01f31e923fe2 82
Pawel Zarembski 0:01f31e923fe2 83 same = memcmp((void*)image_start, image_data, image_size) == 0;
Pawel Zarembski 0:01f31e923fe2 84 if (!same) {
Pawel Zarembski 0:01f31e923fe2 85 ret = flash_manager_init(flash_intf_iap_protected);
Pawel Zarembski 0:01f31e923fe2 86 if (ret != ERROR_SUCCESS) {
Pawel Zarembski 0:01f31e923fe2 87 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 88 return;
Pawel Zarembski 0:01f31e923fe2 89 }
Pawel Zarembski 0:01f31e923fe2 90
Pawel Zarembski 0:01f31e923fe2 91 ret = flash_manager_data(image_start, (const uint8_t*)image_data, image_size);
Pawel Zarembski 0:01f31e923fe2 92 if (ret != ERROR_SUCCESS) {
Pawel Zarembski 0:01f31e923fe2 93 flash_manager_uninit();
Pawel Zarembski 0:01f31e923fe2 94 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 95 return;
Pawel Zarembski 0:01f31e923fe2 96 }
Pawel Zarembski 0:01f31e923fe2 97
Pawel Zarembski 0:01f31e923fe2 98 ret = flash_manager_uninit();
Pawel Zarembski 0:01f31e923fe2 99 if (ret != ERROR_SUCCESS) {
Pawel Zarembski 0:01f31e923fe2 100 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 101 return;
Pawel Zarembski 0:01f31e923fe2 102 }
Pawel Zarembski 0:01f31e923fe2 103 }
Pawel Zarembski 0:01f31e923fe2 104 }
Pawel Zarembski 0:01f31e923fe2 105 #else //DRAG_N_DROP_SUPPORT
Pawel Zarembski 0:01f31e923fe2 106
Pawel Zarembski 0:01f31e923fe2 107 void bootloader_check_and_update(void) {}
Pawel Zarembski 0:01f31e923fe2 108
Pawel Zarembski 0:01f31e923fe2 109 #endif //DRAG_N_DROP_SUPPORT