iap.c 3.9 KB

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