luat_ota.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "luat_base.h"
  2. #include "luat_ota.h"
  3. #include "luat_fs.h"
  4. #include "luat_timer.h"
  5. #include "luat_malloc.h"
  6. #include "luat_flash.h"
  7. #define LUAT_LOG_TAG "ota"
  8. #include "luat_log.h"
  9. LUAT_WEAK void luat_ota_reboot(int timeout_ms) {
  10. if (timeout_ms > 0)
  11. luat_timer_mdelay(timeout_ms);
  12. luat_os_reboot(1);
  13. }
  14. #include "luat_crypto.h"
  15. #include "luat_md5.h"
  16. #ifdef LUAT_USE_CRYPTO
  17. #include "mbedtls/md5.h"
  18. #undef luat_md5_init
  19. #undef luat_md5_update
  20. #undef luat_md5_finalize
  21. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  22. #define luat_md5_init mbedtls_md5_init
  23. #define luat_md5_starts mbedtls_md5_starts
  24. #define luat_md5_update mbedtls_md5_update
  25. #define luat_md5_finalize mbedtls_md5_finish
  26. #else
  27. #define luat_md5_init mbedtls_md5_init
  28. #define luat_md5_starts mbedtls_md5_starts_ret
  29. #define luat_md5_update mbedtls_md5_update_ret
  30. #define luat_md5_finalize mbedtls_md5_finish_ret
  31. #endif
  32. #endif
  33. #define OTA_CHECK_BUFF_SIZE (64) // 超过64字节的话, luat_md5报错, 待查
  34. typedef struct ota_md5
  35. {
  36. uint8_t buff[OTA_CHECK_BUFF_SIZE];
  37. #ifdef LUAT_USE_CRYPTO
  38. mbedtls_md5_context context;
  39. #else
  40. struct md5_context context;
  41. #endif
  42. struct md5_digest digest;
  43. }ota_md5_t;
  44. int luat_ota_checkfile(const char* path) {
  45. int ret = 0;
  46. FILE * fd = luat_fs_fopen(path, "rb");
  47. if (fd == NULL) {
  48. LLOGE("no such file");
  49. return -1;
  50. }
  51. size_t binsize = luat_fs_fsize(path);
  52. if (binsize < 512 || binsize > 1024*1024) {
  53. luat_fs_fclose(fd);
  54. LLOGE("%s is too small/big %d", path, binsize);
  55. return -1;
  56. }
  57. ota_md5_t* ota = luat_heap_malloc(sizeof(ota_md5_t));
  58. if (ota == NULL) {
  59. luat_fs_fclose(fd);
  60. LLOGE("out of memory when check ota file md5");
  61. return -1;
  62. }
  63. unsigned int len = 0;
  64. int remain = binsize - 16;
  65. luat_md5_init(&ota->context);
  66. #ifdef LUAT_USE_CRYPTO
  67. luat_md5_starts(&ota->context);
  68. #endif
  69. while (remain > 0) {
  70. if (remain > OTA_CHECK_BUFF_SIZE) {
  71. len = luat_fs_fread(ota->buff, OTA_CHECK_BUFF_SIZE, 1, fd);
  72. }
  73. else {
  74. len = luat_fs_fread(ota->buff, remain, 1, fd);
  75. }
  76. //LLOGD("ota read %d byte", len);
  77. if (len == 0) { // 不可能的事
  78. break;
  79. }
  80. if (len < 0 || len > 512) {
  81. luat_heap_free(ota);
  82. luat_fs_fclose(fd);
  83. LLOGE("read file fail");
  84. return -1;
  85. }
  86. remain -= len;
  87. luat_md5_update(&ota->context, ota->buff, len);
  88. }
  89. luat_md5_finalize(&ota->context, &ota->digest);
  90. #ifdef LUAT_USE_CRYPTO
  91. mbedtls_md5_free(&ota->context);
  92. #endif
  93. // 应该还有16字节的md5
  94. //memset(ota->buff, 0, OTA_CHECK_BUFF_SIZE);
  95. luat_fs_fread(ota->buff, 16, 1, fd);
  96. // 读完就可以关了
  97. luat_fs_fclose(fd);
  98. // 判断一下md5
  99. // uint8_t *expect_md5 = ota->buff + 64;
  100. // uint8_t *face_md5 = ota->buff + 128;
  101. if (!memcmp(ota->buff, ota->digest.bytes, 16)) {
  102. LLOGD("ota file MD5 ok");
  103. ret = 0;
  104. }
  105. else {
  106. LLOGE("ota file MD5 FAIL");
  107. ret = -1;
  108. }
  109. luat_heap_free(ota);
  110. return ret;
  111. }
  112. #ifdef LUAT_USE_OTA
  113. int luat_ota(uint32_t luadb_addr){
  114. #ifdef LUAT_USE_ZLIB
  115. FILE *fd_out = NULL;
  116. FILE *fd_in = NULL;
  117. extern int zlib_decompress(FILE *source, FILE *dest);
  118. //检测是否有压缩升级文件
  119. if(luat_fs_fexist(UPDATE_TGZ_PATH)){
  120. LLOGI("found update.tgz, decompress ...");
  121. fd_in = luat_fs_fopen(UPDATE_TGZ_PATH, "r");
  122. if (fd_in == NULL){
  123. LLOGE("open the input file : %s error!", UPDATE_TGZ_PATH);
  124. goto _close_decompress;
  125. }
  126. luat_fs_remove(UPDATE_BIN_PATH);
  127. fd_out = luat_fs_fopen(UPDATE_BIN_PATH, "w+");
  128. if (fd_out == NULL){
  129. LLOGE("open the output file : %s error!", UPDATE_BIN_PATH);
  130. goto _close_decompress;
  131. }
  132. int ret = zlib_decompress(fd_in, fd_out);
  133. if (ret != 0){
  134. LLOGE("decompress file error!");
  135. }
  136. _close_decompress:
  137. if(fd_in != NULL){
  138. luat_fs_fclose(fd_in);
  139. }
  140. if(fd_out != NULL){
  141. luat_fs_fclose(fd_out);
  142. }
  143. //不论成功与否都删掉避免每次启动都执行一遍
  144. luat_fs_remove(UPDATE_TGZ_PATH);
  145. }
  146. #endif
  147. int ret = -1;
  148. //检测是否有升级文件
  149. if(luat_fs_fexist(UPDATE_BIN_PATH)){
  150. LLOGI("found update.bin, checking");
  151. if (luat_ota_checkfile(UPDATE_BIN_PATH) == 0) {
  152. LLOGI("update.bin ok, updating... %08X", luadb_addr);
  153. #define UPDATE_BUFF_SIZE 4096
  154. uint8_t* buff = luat_heap_malloc(UPDATE_BUFF_SIZE);
  155. int len = 0;
  156. int offset = 0;
  157. if (buff != NULL) {
  158. FILE* fd = luat_fs_fopen(UPDATE_BIN_PATH, "rb");
  159. if (fd){
  160. while (1) {
  161. memset(buff, 0, UPDATE_BUFF_SIZE);
  162. len = luat_fs_fread(buff, sizeof(uint8_t), UPDATE_BUFF_SIZE, fd);
  163. if (len < 1)
  164. break;
  165. luat_flash_erase(luadb_addr + offset, UPDATE_BUFF_SIZE);
  166. luat_flash_write((char*)buff, luadb_addr + offset, UPDATE_BUFF_SIZE);
  167. offset += len;
  168. }
  169. }else{
  170. ret = -1;
  171. LLOGW("update.bin open error");
  172. }
  173. ret = 0;
  174. }
  175. }
  176. else {
  177. ret = -1;
  178. LLOGW("update.bin NOT ok, skip");
  179. }
  180. luat_fs_remove(UPDATE_BIN_PATH);
  181. }
  182. return ret;
  183. }
  184. #endif