from bbc microbit library

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dfu_init.h Source File

dfu_init.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 /**@file
00034  *
00035  * @defgroup nrf_dfu_init Init packet handling in DFU
00036  * @{
00037  *
00038  * @brief Device Firmware Update module type and function declaration for init packet handling.
00039  *
00040  * @details This header contains basic functionality for performing safety checks on software 
00041  *          updates for \nRFXX based devices. It provides a skeleton for pre-checking an init packet
00042  *          to ensure the following image is compatible with this device. A safety check should 
00043  *          always be performed to prevent accidental flashing of unsupported applications or a
00044  *          wrong combination of application and SoftDevice.
00045  *          The device information contains information such as:
00046  *          - Device type (2 bytes), for example Heart Rate. The device type is a number defined by
00047  *            the customer. It can be located in UICR or FICR.
00048  *          - Device revision (2 bytes), for example major revision 1, minor revision 0. The device
00049  *            revision is a number defined by the customer. It can be located in UICR or FICR.
00050  *          - List of SoftDevices supported by this application, for example 
00051  *              0x0049 = S110v6_0_0 
00052  *              0xFFFE = S110 development (any SoftDevice accepted),
00053  *          - CRC or hash of firmware image
00054  *
00055  * @note This module does not support security features such as image signing, but the corresponding
00056  *       implementation allows for such extensions.
00057  *       If the init packet is signed by a trusted source, it must be decrypted before it can be 
00058  *       processed.
00059  */
00060 
00061 #ifndef DFU_INIT_H__
00062 #define DFU_INIT_H__
00063 
00064 #include <stdint.h>
00065 #include "nrf.h"
00066 
00067 /**@brief Structure contained in an init packet. Contains information on device type, revision, and 
00068  *        supported SoftDevices.
00069  */
00070 typedef struct
00071 {
00072     uint16_t device_type;                                                                   /**< Device type (2 bytes), for example Heart Rate. This number must be defined by the customer before production. It can be located in UICR or FICR. */
00073     uint16_t device_rev;                                                                    /**< Device revision (2 bytes), for example major revision 1, minor revision 0. This number must be defined by the customer before production. It can be located in UICR or FICR. */
00074     uint32_t app_version;                                                                   /**< Application version for the image software. This field allows for additional checking, for example ensuring that a downgrade is not allowed. */
00075     uint16_t softdevice_len;                                                                /**< Number of different SoftDevice revisions compatible with this application. The list of SoftDevice firmware IDs is defined in @ref softdevice. */
00076     uint16_t softdevice[1];                                                                 /**< Variable length array of SoftDevices compatible with this application. The length of the array is specified in the length field. SoftDevice firmware id 0xFFFE indicates any SoftDevice. */
00077 } dfu_init_packet_t;
00078 
00079 /**@brief Structure holding basic device information settings.
00080  */
00081 typedef struct
00082 {
00083     uint16_t device_type;                                                                   /**< Device type (2 bytes), for example Heart Rate. This number must be defined by the customer before production. It can be located in UICR or FICR. */
00084     uint16_t device_rev;                                                                    /**< Device revision (2 bytes), for example major revision 1, minor revision 0. This number must be defined by the customer before production. It can be located in UICR or FICR. */
00085 } dfu_device_info_t;
00086 
00087 /** The device info offset can be modified to place the device info settings at a different location.
00088   * If the customer reserved UICR location is used for other application specific data, the offset
00089   * must be updated to avoid collision with that data.
00090   */
00091 /** [DFU UICR DEV offset] */
00092 #define UICR_CUSTOMER_DEVICE_INFO_OFFSET    0x0                                             /**< Device info offset inside the customer UICR reserved area. Customers may change this value to place the device information in a user-preferred location. */
00093 /** [DFU UICR DEV offset] */
00094 
00095 #define UICR_CUSTOMER_RESERVED_OFFSET       0x80                                            /**< Customer reserved area in the UICR. The area from UICR + 0x80 is reserved for customer usage. */
00096 #define DFU_DEVICE_INFO_BASE                (NRF_UICR_BASE + \
00097                                              UICR_CUSTOMER_RESERVED_OFFSET + \
00098                                              UICR_CUSTOMER_DEVICE_INFO_OFFSET)              /**< The device information base address inside of UICR. */
00099 #define DFU_DEVICE_INFO                     ((dfu_device_info_t *)DFU_DEVICE_INFO_BASE)     /**< The memory mapped structure for device information data. */
00100 
00101 #define DFU_DEVICE_TYPE_EMPTY               ((uint16_t)0xFFFF)                              /**< Mask indicating no device type is present in UICR. 0xFFFF is default flash pattern when not written with data. */
00102 #define DFU_DEVICE_REVISION_EMPTY           ((uint16_t)0xFFFF)                              /**< Mask indicating no device revision is present in UICR. 0xFFFF is default flash pattern when not written with data. */
00103 #define DFU_SOFTDEVICE_ANY                  ((uint16_t)0xFFFE)                              /**< Mask indicating that any SoftDevice is allowed for updating this application. Allows for easy development. Not to be used in production images. */
00104 
00105 
00106 /**@brief DFU prevalidate call for pre-checking the received init packet.
00107  *
00108  * @details  Pre-validation will safety check the firmware image to be transfered in second stage.
00109  *           The function currently checks the device type, device revision, application firmware 
00110  *           version, and supported SoftDevices. More checks should be added according to 
00111  *           customer-specific requirements.
00112  * 
00113  * @param[in] p_init_data    Pointer to the init packet. If the init packet is encrypted or signed,
00114  *                           it must first be decrypted before being checked.
00115  * @param[in] init_data_len  Length of the init data.
00116  *
00117  * @retval NRF_SUCCESS              If the pre-validation succeeded, that means the image is 
00118  *                                  supported by the device and it is considered to come from a 
00119  *                                  trusted source (signing).
00120  * @retval NRF_ERROR_INVALID_DATA   If the pre-validation failed, that means the image is not 
00121  *                                  supported by the device or comes from an un-trusted source 
00122  *                                  (signing).
00123  * @retval NRF_ERROR_INVALID_LENGTH If the size of the init packet is not within the limits of 
00124  *                                  the init packet handler.
00125  */
00126 uint32_t dfu_init_prevalidate(uint8_t * p_init_data, uint32_t init_data_len);
00127 
00128 /**@brief DFU postvalidate call for post-checking the received image using the init packet.
00129  *
00130  * @details  Post-validation can verify the integrity check the firmware image received before 
00131  *           activating the image.
00132  *           Checks performed can be: 
00133  *           - A simple CRC as shown in the corresponding implementation of this API in the file
00134  *             dfu_init_template.c
00135  *           - A hash for better verification of the image.
00136  *           - A signature to ensure the image originates from a trusted source.
00137  *           Checks are intended to be expanded for customer-specific requirements.
00138  * 
00139  * @param[in] p_image    Pointer to the received image. The init data provided in the call 
00140  *                       \ref dfu_init_prevalidate will be used for validating the image.
00141  * @param[in] image_len  Length of the image data.
00142  *
00143  * @retval NRF_SUCCESS             If the post-validation succeeded, that meant the integrity of the
00144  *                                 image has been verified and the image originates from a trusted 
00145  *                                 source (signing).
00146  * @retval NRF_ERROR_INVALID_DATA  If the post-validation failed, that meant the post check of the 
00147  *                                 image failed such as the CRC is not matching the image transfered
00148  *                                 or the verification of the image fails (signing).
00149  */
00150 uint32_t dfu_init_postvalidate(uint8_t * p_image, uint32_t image_len);
00151 
00152 #endif // DFU_INIT_H__
00153 
00154 /**@} */