dfu_init.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup nrf_dfu_init Init packet handling in DFU
  15. * @{
  16. *
  17. * @brief Device Firmware Update module type and function declaration for init packet handling.
  18. *
  19. * @details This header contains basic functionality for performing safety checks on software
  20. * updates for \nRFXX based devices. It provides a skeleton for pre-checking an init packet
  21. * to ensure the following image is compatible with this device. A safety check should
  22. * always be performed to prevent accidental flashing of unsupported applications or a
  23. * wrong combination of application and SoftDevice.
  24. * The device information contains information such as:
  25. * - Device type (2 bytes), for example Heart Rate. The device type is a number defined by
  26. * the customer. It can be located in UICR or FICR.
  27. * - Device revision (2 bytes), for example major revision 1, minor revision 0. The device
  28. * revision is a number defined by the customer. It can be located in UICR or FICR.
  29. * - List of SoftDevices supported by this application, for example
  30. * 0x0049 = S110v6_0_0
  31. * 0xFFFE = S110 development (any SoftDevice accepted),
  32. * - CRC or hash of firmware image
  33. *
  34. * @note This module does not support security features such as image signing, but the corresponding
  35. * implementation allows for such extensions.
  36. * If the init packet is signed by a trusted source, it must be decrypted before it can be
  37. * processed.
  38. */
  39. #ifndef DFU_INIT_H__
  40. #define DFU_INIT_H__
  41. #include <stdint.h>
  42. #include "nrf.h"
  43. /**@brief Structure contained in an init packet. Contains information on device type, revision, and
  44. * supported SoftDevices.
  45. */
  46. typedef struct
  47. {
  48. 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. */
  49. 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. */
  50. 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. */
  51. uint16_t softdevice_len; /**< Number of different SoftDevice revisions compatible with this application. The list of SoftDevice firmware IDs is defined in @ref softdevice. */
  52. 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. */
  53. } dfu_init_packet_t;
  54. /**@brief Structure holding basic device information settings.
  55. */
  56. typedef struct
  57. {
  58. 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. */
  59. 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. */
  60. } dfu_device_info_t;
  61. /** The device info offset can be modified to place the device info settings at a different location.
  62. * If the customer reserved UICR location is used for other application specific data, the offset
  63. * must be updated to avoid collision with that data.
  64. */
  65. /** [DFU UICR DEV offset] */
  66. #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. */
  67. /** [DFU UICR DEV offset] */
  68. #define UICR_CUSTOMER_RESERVED_OFFSET 0x80 /**< Customer reserved area in the UICR. The area from UICR + 0x80 is reserved for customer usage. */
  69. #define DFU_DEVICE_INFO_BASE (NRF_UICR_BASE + \
  70. UICR_CUSTOMER_RESERVED_OFFSET + \
  71. UICR_CUSTOMER_DEVICE_INFO_OFFSET) /**< The device information base address inside of UICR. */
  72. #define DFU_DEVICE_INFO ((dfu_device_info_t *)DFU_DEVICE_INFO_BASE) /**< The memory mapped structure for device information data. */
  73. #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. */
  74. #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. */
  75. #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. */
  76. /**@brief DFU prevalidate call for pre-checking the received init packet.
  77. *
  78. * @details Pre-validation will safety check the firmware image to be transfered in second stage.
  79. * The function currently checks the device type, device revision, application firmware
  80. * version, and supported SoftDevices. More checks should be added according to
  81. * customer-specific requirements.
  82. *
  83. * @param[in] p_init_data Pointer to the init packet. If the init packet is encrypted or signed,
  84. * it must first be decrypted before being checked.
  85. * @param[in] init_data_len Length of the init data.
  86. *
  87. * @retval NRF_SUCCESS If the pre-validation succeeded, that means the image is
  88. * supported by the device and it is considered to come from a
  89. * trusted source (signing).
  90. * @retval NRF_ERROR_INVALID_DATA If the pre-validation failed, that means the image is not
  91. * supported by the device or comes from an un-trusted source
  92. * (signing).
  93. * @retval NRF_ERROR_INVALID_LENGTH If the size of the init packet is not within the limits of
  94. * the init packet handler.
  95. */
  96. uint32_t dfu_init_prevalidate(uint8_t * p_init_data, uint32_t init_data_len);
  97. /**@brief DFU postvalidate call for post-checking the received image using the init packet.
  98. *
  99. * @details Post-validation can verify the integrity check the firmware image received before
  100. * activating the image.
  101. * Checks performed can be:
  102. * - A simple CRC as shown in the corresponding implementation of this API in the file
  103. * dfu_init_template.c
  104. * - A hash for better verification of the image.
  105. * - A signature to ensure the image originates from a trusted source.
  106. * Checks are intended to be expanded for customer-specific requirements.
  107. *
  108. * @param[in] p_image Pointer to the received image. The init data provided in the call
  109. * \ref dfu_init_prevalidate will be used for validating the image.
  110. * @param[in] image_len Length of the image data.
  111. *
  112. * @retval NRF_SUCCESS If the post-validation succeeded, that meant the integrity of the
  113. * image has been verified and the image originates from a trusted
  114. * source (signing).
  115. * @retval NRF_ERROR_INVALID_DATA If the post-validation failed, that meant the post check of the
  116. * image failed such as the CRC is not matching the image transfered
  117. * or the verification of the image fails (signing).
  118. */
  119. uint32_t dfu_init_postvalidate(uint8_t * p_image, uint32_t image_len);
  120. #endif // DFU_INIT_H__
  121. /**@} */