antfs_ota.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. This software is subject to the license described in the license.txt file included with this software distribution.
  3. You may not use this file except in compliance with this license.
  4. Copyright © Dynastream Innovations Inc. 2014
  5. All rights reserved.
  6. */
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include "crc.h"
  10. #include "ant_interface.h" // ant stack softdevice access
  11. #include "ant_boot_settings.h"
  12. #include "antfs.h"
  13. #include "antfs_ota.h"
  14. #include "app_error.h"
  15. #include <dfu_types.h> //
  16. #include "version.c"
  17. #define HEADER_CRC_SEED 0
  18. static const uint8_t identifier_string[4] = {".SUF"};
  19. static uint8_t m_ota_image_header_size;
  20. static uint8_t m_ota_image_header_count = 0;
  21. static uint8_t m_ota_image_header[OTA_IMAGE_HEADER_SIZE_MAX];
  22. static const ota_image_header_t * mp_ota_image_header = (ota_image_header_t * )&m_ota_image_header[0];
  23. static uint8_t m_ota_update_info_file[OTA_UPDATE_INFO_FILE_SIZE];
  24. void antfs_ota_init (void)
  25. {
  26. m_ota_image_header_size = 0;
  27. m_ota_image_header_count = 0;
  28. }
  29. /**/
  30. static void antfs_ota_update_information_file_update(void)
  31. {
  32. uint32_t u32_temp, err_code;
  33. m_ota_update_info_file[OTA_INFO_FILE_STRUCTURE_VERSION_OFFSET] = OTA_INFO_FILE_STRUCTURE_VERSION; /* File Structure Version */
  34. m_ota_update_info_file[OTA_INFO_HARDWARE_VERSION_OFFSET] = OTA_INFO_HARDWARE_VERSION; /* Hardware Version */
  35. m_ota_update_info_file[OTA_INFO_REGION_PRODUCT_ID_OFFSET] = OTA_INFO_REGION_PRODUCT_ID; /* Region/Product Identifier*/
  36. u32_temp = DFU_IMAGE_MAX_SIZE_FULL; /* Maximum swap space */
  37. (void)uint32_encode(u32_temp, &m_ota_update_info_file[OTA_INFO_MAXIMUM_SWAP_SPACE_OFFSET]);
  38. u32_temp = OTA_INFO_WIRELESS_STACK_VERSION_ID;
  39. (void)uint32_encode(u32_temp, &m_ota_update_info_file[OTA_INFO_WIRELESS_STACK_VERSION_ID_OFFSET]); /* Wireless Protocol Stack Version Identifier*/
  40. m_ota_update_info_file[OTA_INFO_WIRELESS_STACK_VERSION_LENGTH_OFFSET] = OTA_INFO_WIRELESS_STACK_VERSION_STRING_BYTES; /* Wireless Protocol Stack Version String Length*/
  41. err_code = sd_ant_version_get(&m_ota_update_info_file[OTA_INFO_WIRELESS_STACK_VERSION_STRING_OFFSET]); /* Wireless Protocol Stack Version String*/
  42. APP_ERROR_CHECK(err_code);
  43. u32_temp = OTA_INFO_BOOTLOADER_VERSION_ID;
  44. (void)uint32_encode(u32_temp, &m_ota_update_info_file[OTA_INFO_BOOTLOADER_VERSION_ID_OFFSET]); /* Bootloader Version Identifier*/
  45. m_ota_update_info_file[OTA_INFO_BOOTLOADER_VERSION_LENGTH_OFFSET] = OTA_INFO_BOOTLOADER_VERSION_STRING_BYTES; /* Bootloader Version String Length*/
  46. memcpy(&m_ota_update_info_file[OTA_INFO_BOOTLOADER_VERSION_STRING_OFFSET], /* Bootloader Version String*/
  47. ac_bootloader_version,
  48. sizeof(ac_bootloader_version));
  49. u32_temp = OTA_INFO_APPLICATION_VERSION_ID;
  50. (void)uint32_encode(u32_temp, &m_ota_update_info_file[OTA_INFO_APPLICATION_VERSION_ID_OFFSET]); /* Application Version Identifier*/
  51. m_ota_update_info_file[OTA_INFO_APPLICATION_VERSION_LENGTH_OFFSET] = OTA_INFO_APPLICATION_VERSION_STRING_BYTES; /* Application Version String Length*/
  52. memcpy(&m_ota_update_info_file[OTA_INFO_APPLICATION_VERSION_STRING_OFFSET], /* Application Version String*/
  53. ANT_BOOT_APP_VERSION,
  54. OTA_INFO_APPLICATION_VERSION_STRING_BYTES);
  55. }
  56. void antfs_ota_update_information_file_get (uint32_t * p_length, uint8_t ** pp_data)
  57. {
  58. static bool ota_info_file_prepared = false;
  59. if (!ota_info_file_prepared)
  60. {
  61. antfs_ota_update_information_file_update();
  62. }
  63. *pp_data = m_ota_update_info_file;
  64. *p_length = OTA_UPDATE_INFO_FILE_SIZE;
  65. }
  66. bool antfs_ota_image_header_parsing(uint8_t ** pp_data, uint32_t * p_length)
  67. {
  68. while (*p_length)
  69. {
  70. if (!m_ota_image_header_size)
  71. {
  72. m_ota_image_header_size = *(*pp_data);
  73. }
  74. if( m_ota_image_header_count < m_ota_image_header_size)
  75. {
  76. m_ota_image_header[m_ota_image_header_count] = *(*pp_data);
  77. (*pp_data)++; // advance the pointer,
  78. (*p_length)--; // decrease the length
  79. m_ota_image_header_count++; // increase the version info count
  80. }
  81. else
  82. {
  83. // no implementation
  84. }
  85. if( m_ota_image_header_count == m_ota_image_header_size)
  86. {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. ota_image_header_t * antfs_ota_image_header_get (void)
  93. {
  94. /*
  95. * Few sanity checks to make sure header is valid.
  96. */
  97. if( m_ota_image_header_count != m_ota_image_header_size) // Check if we got all the header first.
  98. {
  99. return NULL;
  100. }
  101. if ((mp_ota_image_header->file_struct_version < OTA_IMAGE_FILE_STRUCT_VERSION_RANGE_START) || // Check if we can handle this file structure version.
  102. (mp_ota_image_header->file_struct_version > OTA_IMAGE_FILE_STRUCT_VERSION_RANGE_END))
  103. {
  104. return NULL;
  105. }
  106. for (int i=0; i<OTA_IMAGE_ID_STRING_SIZE_MAX; i++) // Check if it is .SUF file
  107. {
  108. if (mp_ota_image_header->identifier_string[i] != identifier_string[i])
  109. {
  110. return false;
  111. }
  112. }
  113. return (ota_image_header_t *) mp_ota_image_header;
  114. }
  115. uint16_t antfs_ota_image_header_crc_get (void)
  116. {
  117. return crc_crc16_update(HEADER_CRC_SEED, m_ota_image_header, m_ota_image_header_count);
  118. }