user_interface.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file user_interface.c
  3. * @author your name (you@domain.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-11-09
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "user_interface.h"
  12. #include "bs_type.h"
  13. //#include "flash_ep.h"
  14. /**
  15. * @brief 将差分文件写入用户的flash,用户自己决定是否在写之前擦除
  16. *
  17. * @param addr
  18. * @param p
  19. * @param len
  20. * @return int
  21. */
  22. int bs_flash_write(uint32_t addr, const void *p, uint32_t len)
  23. {
  24. #if 0
  25. if (xmq25qxx_write(addr, (uint8_t *)p, len))
  26. {
  27. printf("xmq25qxx_write err");
  28. return 1; /* 写入flash失败返回错误码 */
  29. }
  30. #else
  31. #endif
  32. return 0;
  33. }
  34. #if 0
  35. 1#ifdef BSDIFF_UPGRADE
  36. 2 image_header_t recv_head;
  37. 3 uint32_t recv_hcrc; /* 接收到的文件头CRC */
  38. 4 uint32_t calculation_crc; /* 计算出来的文件头CRC */
  39. 5 uint32_t spi_flash_addr = UPGRADE_PROGRAM_ADDR;
  40. 6
  41. 7 memcpy(&recv_head, (uint8_t *)APPLICATION_A, sizeof(image_header_t));
  42. 8 recv_hcrc = BigtoLittle32(recv_head.ih_hcrc);
  43. 9 recv_head.ih_hcrc = 0;
  44. 10 calculation_crc = crc32((uint8_t *)&recv_head, sizeof(image_header_t));
  45. 11
  46. 12 if (recv_hcrc == calculation_crc)
  47. 13 {
  48. 14 recv_head.ih_hcrc = recv_hcrc;
  49. 15 recv_head.ih_time = BigtoLittle32(recv_head.ih_time);
  50. 16 recv_head.ih_size = BigtoLittle32(recv_head.ih_size);
  51. 17 recv_head.ih_dcrc = BigtoLittle32(recv_head.ih_dcrc);
  52. 18 recv_head.ih_ocrc = BigtoLittle32(recv_head.ih_ocrc);
  53. 19 /* 差分升级包 */
  54. 20 recv_head.ih_hcrc = calculation_crc;
  55. 21 if (crc32((uint8_t *)APPLICATION_RUN, recv_head.ih_load) != recv_head.ih_ocrc)
  56. 22 {
  57. 23 APP_ERR_PRINT("file oldcrc err,calcrc:0X%08X, ih_oldbin_crc:0X%08X,",
  58. 24 crc32((uint8_t *)APPLICATION_RUN,
  59. 25 recv_head.ih_load), recv_head.ih_ocrc);
  60. 26 goto bsdiff_out;
  61. 27 }
  62. 28 RTOS_LOCK();
  63. 29 disable_task_monitoring(ALL_TASK_RUNFLAG_BIT, true);
  64. 30 // flash_erase_sector(UPGRADE_PROGRAM_ADDR, UPGRADE_PROGRAM_PAGE);
  65. 31 recv_hcrc = iap_patch((uint8_t *)APPLICATION_RUN, recv_head.ih_load,
  66. 32 (uint8_t *)(APPLICATION_A + sizeof(image_header_t)),
  67. 33 recv_head.ih_size, UPGRADE_PROGRAM_ADDR);
  68. 34 if (recv_hcrc != recv_head.ih_ep)
  69. 35 {
  70. 36 APP_ERR_PRINT("iap_patch len err.");
  71. 37 APP_ERR_PRINT("iap_patch len: %lu, new_len: %lu", recv_hcrc, recv_head.ih_ep);
  72. 38 goto bsdiff_out;
  73. 39 }
  74. 40 if (erase_program(APPLICATION_A))
  75. 41 {
  76. 42 APP_ERR_PRINT("I erase program failed.");
  77. 43 goto bsdiff_out;
  78. 44 }
  79. 45
  80. 46 current_flash_write_addr = APPLICATION_A;
  81. 47 for (uint32_t i = 0; i < (recv_head.ih_ep / 1024); i++)
  82. 48 {
  83. 49 xmq25qxx_read(spi_flash_addr, spi_read_cache, 1024);
  84. 50 if (xflash_write(current_flash_write_addr, spi_read_cache, 1024))
  85. 51 {
  86. 52 APP_ERR_PRINT("I write program failed.");
  87. 53 goto bsdiff_out;
  88. 54 }
  89. 55 spi_flash_addr += 1024;
  90. 56 current_flash_write_addr += 1024;
  91. 57 APP_PRINT("current_flash_write_addr: 0X%08X", current_flash_write_addr);
  92. 58 }
  93. 59 if (recv_head.ih_ep % 1024 != 0)
  94. 60 {
  95. 61 memset(spi_read_cache, 0XFF, 1024);
  96. 62 xmq25qxx_read(spi_flash_addr, spi_read_cache, recv_head.ih_ep % 1024);
  97. 63
  98. 64 if (xflash_write(current_flash_write_addr, spi_read_cache, 1024))
  99. 65 {
  100. 66 APP_ERR_PRINT("I write program failed.");
  101. 67 goto bsdiff_out;
  102. 68 }
  103. 69 }
  104. 70 if (crc32((uint8_t *)APPLICATION_A, recv_head.ih_ep) != recv_head.ih_dcrc)
  105. 71 {
  106. 72 APP_ERR_PRINT("file newcrc err,calcrc:0X%08X, newcrc:0X%08X, len: %lu",
  107. 73 crc32((uint8_t *)APPLICATION_A, recv_head.ih_ep),
  108. 74 recv_head.ih_load, recv_head.ih_dcrc);
  109. 75 goto bsdiff_out;
  110. 76 }
  111. 77 /* 下载成功,开始升级 */
  112. 78 if (check_bin_file((bin_info_t *)(APPLICATION_A + BIN_INFO_OFFSET))) /* bin文件非法 */
  113. 79 {
  114. 80 APP_ERR_PRINT("check_bin_file err.");
  115. 81 goto bsdiff_out;
  116. 82 }
  117. 83 recv_head.ih_dcrc = CRT_CRC16_check(0, (uint8_t *)APPLICATION_A,
  118. 84 recv_head.ih_ep);
  119. 85 readwrite_app_run_bin_info(0, &recv_head.ih_ep, (uint16_t *)&recv_head.ih_dcrc);
  120. 86 /* 整体校验成功,确认升级 */
  121. 87 if (switch_program_stage(STAGE_1))
  122. 88 {
  123. 89 APP_ERR_PRINT("I write switch_program_stage STAGE_0 failed."); /* 置位升级标志写失败 */
  124. 90 goto bsdiff_out;
  125. 91 }
  126. 92 APP_PRINT("upgrade success.");
  127. 93 bsdiff_out:
  128. 94 SYSTEM_RESET();
  129. 95 }
  130. #endif