iap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <string.h>
  2. #include "app/iap.h"
  3. #include "app/nv_storage.h"
  4. #include "bsp/fmc_flash.h"
  5. #include "bsp/shark_rtc.h"
  6. #include "libs/logger.h"
  7. static int iap_write_image(uint8_t *data, int len);
  8. static int iap_check_image(uint8_t *data, int len);
  9. void _reboot_timer_handler(shark_timer_t *t);
  10. void iap_read_string(can_frame_t *frame);
  11. static u8 _write_success = 0;
  12. static int _write_position = 0;
  13. static shark_timer_t _reboot_timer = {.handler = _reboot_timer_handler,};
  14. void iap_read_chip_id(can_frame_t *frame)
  15. {
  16. u8 buff[24];
  17. u8 len;
  18. buff[0] = buff[1] = 0x00;
  19. len = shark_read_chip_id(buff + 2) + 2;
  20. protocol_send_bms_info(frame->head.can_addr, frame->key, buff, len);
  21. }
  22. void process_iap_message(can_frame_t *frame, int len){
  23. uint8_t *data = NULL;
  24. int data_len = 0;
  25. switch(frame->key) {
  26. case CAN_KEY_IAP_ENTER:
  27. if (gd32_flash_size() < 128 * 1024){
  28. wdog_reload();
  29. fmc_iap_write_magic(0xFFFFFFFF);
  30. shark_rtc_set_backup(0x3000);
  31. nv_save_all_soc();
  32. NVIC_SystemReset();
  33. while(1);
  34. }
  35. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  36. break;
  37. case CAN_KEY_IAP_BEGIN:
  38. if (gd32_flash_size() < 128 * 1024){
  39. protocol_send_ack(frame->head.can_addr, frame->key, 1);
  40. break;
  41. }
  42. fmc_write_image_begin();
  43. _write_position = 0;
  44. _write_success = 0;
  45. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  46. break;
  47. case CAN_KEY_IAP_WRITE:
  48. _write_success = 0;
  49. data_len = iap_write_image(frame->data, len);
  50. data = frame->data;
  51. break;
  52. case CAN_KEY_IAP_CHECK:
  53. fmc_write_image_end();
  54. data_len = iap_check_image(frame->data, len);
  55. data = frame->data;
  56. break;
  57. case CAN_KEY_IAP_BOOT:
  58. if (_write_success) {
  59. nv_save_all_soc();
  60. NVIC_SystemReset();
  61. while(1);
  62. } else {
  63. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  64. }
  65. break;
  66. case CAN_KEY_IAP_STAT:
  67. if (len > 0 && frame->data[0] == 0x01) {
  68. iap_read_chip_id(frame);
  69. } else {
  70. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  71. }
  72. break;
  73. case CAN_EEY_IAP_READ_STRING:
  74. iap_read_string(frame);
  75. break;
  76. case CAN_KEY_REBOOT:
  77. if (len == 4 && shark_decode_u32(frame->data) == SHARK_IAP_MAGIC_SUCCESS) {
  78. fmc_iap_write_magic(0xFFFFFFFF);
  79. }
  80. shark_rtc_set_backup(0x3003);
  81. shark_timer_post(&_reboot_timer, 100);
  82. protocol_send_ack(frame->head.can_addr, frame->key, 1);
  83. break;
  84. case CAN_KET_ERASE_NV:
  85. shark_rtc_set_backup(0x3002);
  86. if (len == 0) {
  87. nv_erase_all_soc(1);
  88. }else {
  89. if (frame->data[0] == 1) {//erase and clear cycle
  90. nv_erase_all_soc(0);
  91. }else {
  92. nv_erase_all_soc(1);
  93. }
  94. }
  95. shark_timer_post(&_reboot_timer, 100);
  96. protocol_send_ack(frame->head.can_addr, frame->key, 1);
  97. break;
  98. }
  99. if (data != NULL && data_len > 0){
  100. protocol_send_bms_info(frame->head.can_addr, frame->key, frame->data, data_len);
  101. }
  102. }
  103. void _reboot_timer_handler(shark_timer_t *t){
  104. nv_save_all_soc();
  105. NVIC_SystemReset();
  106. }
  107. static int iap_write_image(uint8_t *data, int len){
  108. int w_pos = shark_decode_u24(data);
  109. if (w_pos == _write_position && len > 3) {
  110. fmc_write_image_continue(data + 3, len - 3);
  111. _write_position += len - 3;
  112. }
  113. data[0] = 0;
  114. shark_encode_u24(data+1, _write_position);
  115. return 4;
  116. }
  117. void iap_read_string(can_frame_t *frame)
  118. {
  119. uint8_t buff[64];
  120. const char *text;
  121. uint32_t addr;
  122. uint32_t len;
  123. addr = ((uint32_t) frame->data[2]) << 16 | ((uint32_t) frame->data[1]) << 8 | frame->data[0];
  124. text = (const char *) (0x08000000 + addr);
  125. len = strlen(text);
  126. if (len > (sizeof(buff)-4)) {
  127. len = sizeof(buff) - 4;
  128. }
  129. buff[0] = 0;
  130. memcpy(buff+1, frame->data, 3);
  131. memcpy(buff + 4, text, len);
  132. protocol_send_bms_info(frame->head.can_addr, frame->key, buff, len + 4);
  133. }
  134. static int iap_check_image(uint8_t *data, int len) {
  135. uint32_t size, checksum;
  136. size = shark_decode_u24(data);
  137. checksum = shark_decode_u32(data + 3);
  138. uint32_t d_checksum = shark_iap_checksum_init();
  139. d_checksum = shark_iap_checksum_put(d_checksum, (const u8 *)fmc_iap_image_addr(), size);
  140. d_checksum = shark_iap_checksum_finish(d_checksum);
  141. if (checksum != d_checksum) {
  142. data[0] = 1;
  143. return 1;
  144. }
  145. fmc_write_magic(size, checksum, SHARK_IAP_MAGIC_FLASH);
  146. _write_success = 1;
  147. data[0] = 0;
  148. return 1;
  149. }