To get started with Seeed Tiny BLE, include detecting motion, button and battery level.
Dependencies: BLE_API eMPL_MPU6050 mbed nRF51822
nRF51822/nordic-sdk/components/libraries/bootloader_dfu/dfu_init_template.c@1:fc2f9d636751, 2015-04-22 (annotated)
- Committer:
- yihui
- Date:
- Wed Apr 22 07:47:17 2015 +0000
- Revision:
- 1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:fc2f9d636751 | 1 | /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. |
yihui | 1:fc2f9d636751 | 2 | * |
yihui | 1:fc2f9d636751 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
yihui | 1:fc2f9d636751 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
yihui | 1:fc2f9d636751 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
yihui | 1:fc2f9d636751 | 6 | * |
yihui | 1:fc2f9d636751 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
yihui | 1:fc2f9d636751 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
yihui | 1:fc2f9d636751 | 9 | * the file. |
yihui | 1:fc2f9d636751 | 10 | * |
yihui | 1:fc2f9d636751 | 11 | */ |
yihui | 1:fc2f9d636751 | 12 | |
yihui | 1:fc2f9d636751 | 13 | /**@file |
yihui | 1:fc2f9d636751 | 14 | * |
yihui | 1:fc2f9d636751 | 15 | * @defgroup nrf_dfu_init_template Template file with an DFU init packet handling example. |
yihui | 1:fc2f9d636751 | 16 | * @{ |
yihui | 1:fc2f9d636751 | 17 | * |
yihui | 1:fc2f9d636751 | 18 | * @ingroup nrf_dfu |
yihui | 1:fc2f9d636751 | 19 | * |
yihui | 1:fc2f9d636751 | 20 | * @brief This file contains a template on how to implement DFU init packet handling. |
yihui | 1:fc2f9d636751 | 21 | * |
yihui | 1:fc2f9d636751 | 22 | * @details The template shows how device type and revision can be used for a safety check of the |
yihui | 1:fc2f9d636751 | 23 | * received image. It shows how validation can be performed in two stages: |
yihui | 1:fc2f9d636751 | 24 | * - Stage 1: Pre-check of firmware image before transfer to ensure the firmware matches: |
yihui | 1:fc2f9d636751 | 25 | * - Device Type. |
yihui | 1:fc2f9d636751 | 26 | * - Device Revision. |
yihui | 1:fc2f9d636751 | 27 | * Installed SoftDevice. |
yihui | 1:fc2f9d636751 | 28 | * This template can be extended with additional checks according to needs. |
yihui | 1:fc2f9d636751 | 29 | * For example, such a check could be the origin of the image (trusted source) |
yihui | 1:fc2f9d636751 | 30 | * based on a signature scheme. |
yihui | 1:fc2f9d636751 | 31 | * - Stage 2: Post-check of the image after image transfer but before installing firmware. |
yihui | 1:fc2f9d636751 | 32 | * For example, such a check could be an integrity check in form of hashing or |
yihui | 1:fc2f9d636751 | 33 | * verification of a signature. |
yihui | 1:fc2f9d636751 | 34 | * In this template, a simple CRC check is carried out. |
yihui | 1:fc2f9d636751 | 35 | * The CRC check can be replaced with other mechanisms, like signing. |
yihui | 1:fc2f9d636751 | 36 | * |
yihui | 1:fc2f9d636751 | 37 | * @note This module does not support security features such as image signing, but the |
yihui | 1:fc2f9d636751 | 38 | * implementation allows for such extension. |
yihui | 1:fc2f9d636751 | 39 | * If the init packet is signed by a trusted source, it must be decrypted before it can be |
yihui | 1:fc2f9d636751 | 40 | * processed. |
yihui | 1:fc2f9d636751 | 41 | */ |
yihui | 1:fc2f9d636751 | 42 | |
yihui | 1:fc2f9d636751 | 43 | #include "dfu_init.h" |
yihui | 1:fc2f9d636751 | 44 | #include <stdint.h> |
yihui | 1:fc2f9d636751 | 45 | #include <string.h> |
yihui | 1:fc2f9d636751 | 46 | #include "dfu_types.h" |
yihui | 1:fc2f9d636751 | 47 | #include "nrf_error.h" |
yihui | 1:fc2f9d636751 | 48 | #include "crc16.h" |
yihui | 1:fc2f9d636751 | 49 | |
yihui | 1:fc2f9d636751 | 50 | #define DFU_INIT_PACKET_EXT_LENGTH_MIN 2 //< Minimum length of the extended init packet. The extended init packet may contain a CRC, a HASH, or other data. This value must be changed according to the requirements of the system. The template uses a minimum value of two in order to hold a CRC. */ |
yihui | 1:fc2f9d636751 | 51 | #define DFU_INIT_PACKET_EXT_LENGTH_MAX 10 //< Maximum length of the extended init packet. The extended init packet may contain a CRC, a HASH, or other data. This value must be changed according to the requirements of the system. The template uses a maximum value of 10 in order to hold a CRC and any padded data on transport layer without overflow. */ |
yihui | 1:fc2f9d636751 | 52 | |
yihui | 1:fc2f9d636751 | 53 | static uint8_t m_extended_packet[DFU_INIT_PACKET_EXT_LENGTH_MAX]; //< Data array for storage of the extended data received. The extended data follows the normal init data of type \ref dfu_init_packet_t. Extended data can be used for a CRC, hash, signature, or other data. */ |
yihui | 1:fc2f9d636751 | 54 | static uint8_t m_extended_packet_length; //< Length of the extended data received with init packet. */ |
yihui | 1:fc2f9d636751 | 55 | |
yihui | 1:fc2f9d636751 | 56 | |
yihui | 1:fc2f9d636751 | 57 | uint32_t dfu_init_prevalidate(uint8_t * p_init_data, uint32_t init_data_len) |
yihui | 1:fc2f9d636751 | 58 | { |
yihui | 1:fc2f9d636751 | 59 | uint32_t i = 0; |
yihui | 1:fc2f9d636751 | 60 | |
yihui | 1:fc2f9d636751 | 61 | // In order to support signing or encryption then any init packet decryption function / library |
yihui | 1:fc2f9d636751 | 62 | // should be called from here or implemented at this location. |
yihui | 1:fc2f9d636751 | 63 | |
yihui | 1:fc2f9d636751 | 64 | // Length check to ensure valid data are parsed. |
yihui | 1:fc2f9d636751 | 65 | if (init_data_len < sizeof(dfu_init_packet_t)) |
yihui | 1:fc2f9d636751 | 66 | { |
yihui | 1:fc2f9d636751 | 67 | return NRF_ERROR_INVALID_LENGTH; |
yihui | 1:fc2f9d636751 | 68 | } |
yihui | 1:fc2f9d636751 | 69 | |
yihui | 1:fc2f9d636751 | 70 | // Current template uses clear text data so they can be casted for pre-check. |
yihui | 1:fc2f9d636751 | 71 | dfu_init_packet_t * p_init_packet = (dfu_init_packet_t *)p_init_data; |
yihui | 1:fc2f9d636751 | 72 | |
yihui | 1:fc2f9d636751 | 73 | m_extended_packet_length = ((uint32_t)p_init_data + init_data_len) - |
yihui | 1:fc2f9d636751 | 74 | (uint32_t)&p_init_packet->softdevice[p_init_packet->softdevice_len]; |
yihui | 1:fc2f9d636751 | 75 | if (m_extended_packet_length < DFU_INIT_PACKET_EXT_LENGTH_MIN) |
yihui | 1:fc2f9d636751 | 76 | { |
yihui | 1:fc2f9d636751 | 77 | return NRF_ERROR_INVALID_LENGTH; |
yihui | 1:fc2f9d636751 | 78 | } |
yihui | 1:fc2f9d636751 | 79 | |
yihui | 1:fc2f9d636751 | 80 | if (((uint32_t)p_init_data + init_data_len) < |
yihui | 1:fc2f9d636751 | 81 | (uint32_t)&p_init_packet->softdevice[p_init_packet->softdevice_len]) |
yihui | 1:fc2f9d636751 | 82 | { |
yihui | 1:fc2f9d636751 | 83 | return NRF_ERROR_INVALID_LENGTH; |
yihui | 1:fc2f9d636751 | 84 | } |
yihui | 1:fc2f9d636751 | 85 | |
yihui | 1:fc2f9d636751 | 86 | memcpy(m_extended_packet, |
yihui | 1:fc2f9d636751 | 87 | &p_init_packet->softdevice[p_init_packet->softdevice_len], |
yihui | 1:fc2f9d636751 | 88 | m_extended_packet_length); |
yihui | 1:fc2f9d636751 | 89 | |
yihui | 1:fc2f9d636751 | 90 | /** [DFU init application version] */ |
yihui | 1:fc2f9d636751 | 91 | // In order to support application versioning this check should be updated. |
yihui | 1:fc2f9d636751 | 92 | // This template allows for any application to be installed however customer could place a |
yihui | 1:fc2f9d636751 | 93 | // revision number at bottom of application to be verified by bootloader. This could be done at |
yihui | 1:fc2f9d636751 | 94 | // a relative location to this papplication for example Application start address + 0x0100. |
yihui | 1:fc2f9d636751 | 95 | /** [DFU init application version] */ |
yihui | 1:fc2f9d636751 | 96 | |
yihui | 1:fc2f9d636751 | 97 | // First check to verify the image to be transfered matches the device type. |
yihui | 1:fc2f9d636751 | 98 | // If no Device type is present in DFU_DEVICE_INFO then any image will be accepted. |
yihui | 1:fc2f9d636751 | 99 | if ((DFU_DEVICE_INFO->device_type != DFU_DEVICE_TYPE_EMPTY) && |
yihui | 1:fc2f9d636751 | 100 | (p_init_packet->device_type != DFU_DEVICE_INFO->device_type)) |
yihui | 1:fc2f9d636751 | 101 | { |
yihui | 1:fc2f9d636751 | 102 | return NRF_ERROR_INVALID_DATA; |
yihui | 1:fc2f9d636751 | 103 | } |
yihui | 1:fc2f9d636751 | 104 | |
yihui | 1:fc2f9d636751 | 105 | // Second check to verify the image to be transfered matches the device revision. |
yihui | 1:fc2f9d636751 | 106 | // If no Device revision is present in DFU_DEVICE_INFO then any image will be accepted. |
yihui | 1:fc2f9d636751 | 107 | if ((DFU_DEVICE_INFO->device_rev != DFU_DEVICE_REVISION_EMPTY) && |
yihui | 1:fc2f9d636751 | 108 | (p_init_packet->device_rev != DFU_DEVICE_INFO->device_rev)) |
yihui | 1:fc2f9d636751 | 109 | { |
yihui | 1:fc2f9d636751 | 110 | return NRF_ERROR_INVALID_DATA; |
yihui | 1:fc2f9d636751 | 111 | } |
yihui | 1:fc2f9d636751 | 112 | |
yihui | 1:fc2f9d636751 | 113 | // Third check: Check the array of supported SoftDevices by this application. |
yihui | 1:fc2f9d636751 | 114 | // If the installed SoftDevice does not match any SoftDevice in the list then an |
yihui | 1:fc2f9d636751 | 115 | // error is returned. |
yihui | 1:fc2f9d636751 | 116 | while (i < p_init_packet->softdevice_len) |
yihui | 1:fc2f9d636751 | 117 | { |
yihui | 1:fc2f9d636751 | 118 | if (p_init_packet->softdevice[i] == DFU_SOFTDEVICE_ANY || |
yihui | 1:fc2f9d636751 | 119 | p_init_packet->softdevice[i++] == SOFTDEVICE_INFORMATION->firmware_id) |
yihui | 1:fc2f9d636751 | 120 | { |
yihui | 1:fc2f9d636751 | 121 | return NRF_SUCCESS; |
yihui | 1:fc2f9d636751 | 122 | } |
yihui | 1:fc2f9d636751 | 123 | } |
yihui | 1:fc2f9d636751 | 124 | |
yihui | 1:fc2f9d636751 | 125 | // No matching SoftDevice found - Return NRF_ERROR_INVALID_DATA. |
yihui | 1:fc2f9d636751 | 126 | return NRF_ERROR_INVALID_DATA; |
yihui | 1:fc2f9d636751 | 127 | } |
yihui | 1:fc2f9d636751 | 128 | |
yihui | 1:fc2f9d636751 | 129 | |
yihui | 1:fc2f9d636751 | 130 | uint32_t dfu_init_postvalidate(uint8_t * p_image, uint32_t image_len) |
yihui | 1:fc2f9d636751 | 131 | { |
yihui | 1:fc2f9d636751 | 132 | uint16_t image_crc; |
yihui | 1:fc2f9d636751 | 133 | uint16_t received_crc; |
yihui | 1:fc2f9d636751 | 134 | |
yihui | 1:fc2f9d636751 | 135 | // In order to support hashing (and signing) then the (decrypted) hash should be fetched and |
yihui | 1:fc2f9d636751 | 136 | // the corresponding hash should be calculated over the image at this location. |
yihui | 1:fc2f9d636751 | 137 | // If hashing (or signing) is added to the system then the CRC validation should be removed. |
yihui | 1:fc2f9d636751 | 138 | |
yihui | 1:fc2f9d636751 | 139 | // calculate CRC from active block. |
yihui | 1:fc2f9d636751 | 140 | image_crc = crc16_compute(p_image, image_len, NULL); |
yihui | 1:fc2f9d636751 | 141 | |
yihui | 1:fc2f9d636751 | 142 | // Decode the received CRC from extended data. |
yihui | 1:fc2f9d636751 | 143 | received_crc = uint16_decode((uint8_t *)&m_extended_packet[0]); |
yihui | 1:fc2f9d636751 | 144 | |
yihui | 1:fc2f9d636751 | 145 | // Compare the received and calculated CRC. |
yihui | 1:fc2f9d636751 | 146 | if (image_crc != received_crc) |
yihui | 1:fc2f9d636751 | 147 | { |
yihui | 1:fc2f9d636751 | 148 | return NRF_ERROR_INVALID_DATA; |
yihui | 1:fc2f9d636751 | 149 | } |
yihui | 1:fc2f9d636751 | 150 | |
yihui | 1:fc2f9d636751 | 151 | return NRF_SUCCESS; |
yihui | 1:fc2f9d636751 | 152 | } |
yihui | 1:fc2f9d636751 | 153 |