ota.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef __OTA_H__
  2. #define __OTA_H__
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #define WBJW 0x57424A57
  6. #define MAX_BIN_SIZE (256*1024) //单个bin文件最大值;
  7. #define MAX_OLDFILES_NUM 8 //旧bin文件的个数的最大值;
  8. #define BLOCK_MAX 8 //块数量的最大值;
  9. #define NEW_EP (0X80000000) //入口地址;
  10. #define MAX_PATH 8
  11. /**************************** 以下为结构体定义*****************************/
  12. //升级固件类型
  13. #define FIRMTYPE_BT 1
  14. #define FIRMTYPE_PT 2
  15. #define FIRMTYPE_APP 3
  16. #define DF_PKG 1 //差分包
  17. #define FS_PKG 2 //原始包
  18. //升级信息
  19. typedef struct _ota_info_t
  20. {
  21. uint8_t firmware_type; //固件类型
  22. uint32_t old_ver; //旧版本号
  23. uint32_t old_size; //旧字节数
  24. uint32_t old_crc32; //旧crc32
  25. uint8_t pkg_type; //升级包类型
  26. uint8_t update_enum; //固件标记
  27. uint8_t tail_flag; //是否包含尾部20字节
  28. uint8_t erase_app1; //是否已擦除app1
  29. }ota_info_t;
  30. extern ota_info_t ota_info;
  31. //升级过程结果
  32. enum {
  33. OTA_RET_SUCCESS = 0, //执行成功
  34. OTA_RET_APP3_CRC_ERR = 0x31, //APP3校验错误
  35. OTA_RET_NO_BLK_ERR, //没找到对应的块
  36. OTA_RET_NEWSIZE_ERR, //升级后的字节数错误
  37. OTA_RET_NEWCRC_ERR, //升级后的crc错误
  38. OTA_RET_OLDSIZE_ERR, //旧固件的字节数错误
  39. OTA_RET_OLDCRC_ERR, //旧固件的crc错误
  40. OTA_RET_FIRMWARE_ERR, //固件类型错误
  41. };
  42. typedef struct{
  43. bool ok;
  44. char* path;
  45. char drive[MAX_PATH];
  46. char dir[MAX_PATH];
  47. char filename[MAX_PATH];
  48. char ext[MAX_PATH];
  49. uint8_t* addr;
  50. uint32_t size;
  51. uint32_t app_ver;
  52. uint16_t dev_type;
  53. uint32_t crc32;
  54. }bin_file_t;
  55. #pragma pack(1) // 1字节对齐
  56. // 下载包,结构体
  57. typedef struct{
  58. uint8_t* pkg_addr; // 下载包的首地址;
  59. uint32_t pkg_size; // 下载包的大小,完整的;从数据包里解析出来的原始字段值
  60. uint32_t pkg_crc32; // 下载包的crc32;从数据包里解析出来的原始字段值
  61. uint32_t calc_pkg_size; // 下载包的大小,完整的;经过计算的值 calc_size == pkg_size
  62. uint32_t calc_pkg_crc32; // 下载包的crc32;经过计算的值 calc_crc32 = crc32(pkg_addr,calc_size - 4)
  63. }DFOTA_pkg_t;
  64. // 下载包,头,结构体,
  65. // header_size = sizeof(DFOTA_pkg_header_t)
  66. typedef struct{
  67. uint8_t pkg_magic[16]; // ="DFOTA_PK_V002"; 魔数,差分升级标记,整个下载包的起始标记
  68. uint32_t pkg_time; // 打包的时间戳;
  69. uint32_t pkg_size; // 从数据包里解析出来的原始字段值
  70. }DFOTA_pkg_header_t;
  71. // 块,结构体
  72. // DFOTA_blk_t block[BLOCK_MAX] // 数组定义
  73. typedef struct{
  74. uint8_t* blk_addr; // 第n个块的首地址;
  75. uint32_t blk_size; // 第n个块的大小,完整的;从数据包里解析出来的原始字段值
  76. uint32_t blk_crc32; // 第n个块的crc32;从数据包里解析出来的原始字段值
  77. uint32_t calc_blk_size; // 第n个块的大小,完整的;经过计算的值 calc_size == blk_size
  78. uint32_t calc_blk_crc32; // 第n个块的crc32;经过计算的值 calc_crc32 = crc32(blk_addr,calc_size - 4)
  79. uint8_t* payload_addr; // 载荷的首地址;
  80. uint32_t payload_size; // 载荷的大小,完整的
  81. }DFOTA_blk_t;
  82. //块,头,结构体,header_size = sizeof(DFOTA_pkg_header_t)
  83. typedef struct{
  84. uint8_t blk_magic[4]; // ="BLOK"//魔数,块标记
  85. uint32_t blk_size; // 此字段之后crc32之前的字节数
  86. uint8_t d_flag; // 差分算法标记
  87. uint8_t z_flag; // 压缩算法标记
  88. uint16_t reserve; // 预留
  89. uint32_t old_ver; // 旧固件[old.bin]的版本号
  90. uint32_t old_size; // 旧固件[old.bin]的字节数
  91. uint32_t old_crc32; // 旧固件[old.bin]的crc32
  92. uint32_t new_ver; // 新固件[new.bin]的版本号
  93. uint32_t new_size; // 新固件[new.bin]的字节数
  94. uint32_t new_crc32; // 新固件[new.bin]的crc32
  95. uint32_t new_ep; // 新固件[new.bin]的入口地址
  96. uint32_t pad_size; // block尾部填充字节数
  97. }DFOTA_blk_header_t;
  98. // 载荷,结构体
  99. typedef struct{
  100. uint8_t* diff_header_addr; // 差分头首地址
  101. uint8_t* compr_header_addr; // 压缩头首地址
  102. uint8_t* data_addr; // data首地址
  103. uint32_t data_size; // data的大小;
  104. }DFOTA_payload_t;
  105. // 差分,头,结构体,header_size = sizeof(DFOTA_bsdiff_header_t)
  106. typedef struct{
  107. uint8_t diff_magic[16]; // ="ENDSLEY/BSDIFF43"
  108. uint32_t new_size; // new file size in Byte(s)(new.bin的字节数)
  109. }DFOTA_bsdiff_header_t;
  110. // 压缩,头,结构体,header_size = sizeof(DFOTA_lzma_header_t)
  111. typedef struct{
  112. uint8_t props; // Special LZMA properties (lc,lp, pb in encoded form)
  113. uint32_t dic; // Dictionary size (little endian)
  114. uint64_t raw_size; // 没压缩时的字节数diff_raw.bin的字节数)
  115. }DFOTA_lzma_header_t;
  116. // DFOTA设备相关参数结构体定义
  117. typedef struct{
  118. uint32_t current_ver; // 当前版本
  119. uint32_t current_size; // 当前版本
  120. uint32_t current_crc32; // 当前版本
  121. uint32_t blk_sum; // 实际解析到的块的个数
  122. uint32_t blk_match_index; // 索引到的匹配的块下标
  123. //.....
  124. }DFOTA_dev_t;
  125. #pragma pack() // 取消1字节对齐,恢复为默认4字节对齐
  126. #define PKG_HEADER_LEN sizeof(DFOTA_pkg_header_t) //24字节
  127. #define BLK_HEADER_LEN sizeof(DFOTA_blk_header_t) //44字节
  128. uint8_t firmware_tail_check(uint32_t part, uint32_t *firm_len);
  129. uint8_t ota_process(void);
  130. uint8_t blk_check(uint32_t addr, uint32_t *len);
  131. void myiap_task(void);
  132. #endif // !