iap.c 3.6 KB

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