iap.c 3.5 KB

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