iap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <string.h>
  2. #include "app/iap.h"
  3. #include "bsp/fmc_flash.h"
  4. static int iap_write_image(uint8_t *data, int len);
  5. static int iap_check_image(uint8_t *data, int len);
  6. void _reboot_timer_handler(shark_timer_t *t);
  7. void iap_read_string(can_frame_t *frame);
  8. static u8 _write_success = 0;
  9. static int _write_position = 0;
  10. static shark_timer_t _reboot_timer = {.handler = _reboot_timer_handler,};
  11. void process_iap_message(can_frame_t *frame, int len){
  12. uint8_t *data = NULL;
  13. int data_len = 0;
  14. switch(frame->key) {
  15. case CAN_KEY_IAP_ENTER:
  16. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  17. break;
  18. case CAN_KEY_IAP_BEGIN:
  19. fmc_erase_image();
  20. _write_position = 0;
  21. _write_success = 0;
  22. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  23. break;
  24. case CAN_KEY_IAP_WRITE:
  25. _write_success = 0;
  26. data_len = iap_write_image(frame->data, len);
  27. data = frame->data;
  28. break;
  29. case CAN_KEY_IAP_CHECK:
  30. data_len = iap_check_image(frame->data, len);
  31. data = frame->data;
  32. break;
  33. case CAN_KEY_IAP_BOOT:
  34. if (_write_success) {
  35. shark_timer_post(&_reboot_timer, 100);
  36. } else {
  37. protocol_send_ack(frame->head.can_addr, frame->key, 0);
  38. }
  39. break;
  40. case CAN_EEY_IAP_READ_STRING:
  41. iap_read_string(frame);
  42. break;
  43. }
  44. if (data != NULL && data_len > 0){
  45. protocol_send_bms_info(frame->head.can_addr, frame->key, frame->data, data_len);
  46. }
  47. }
  48. void _reboot_timer_handler(shark_timer_t *t){
  49. NVIC_SystemReset();
  50. }
  51. static int iap_write_image(uint8_t *data, int len){
  52. int w_pos = shark_decode_u24(data);
  53. if (w_pos == _write_position) {
  54. fmc_write_image(data + 3, len - 3);
  55. _write_position += len - 3;
  56. }
  57. data[0] = 0;
  58. shark_encode_u24(data+1, _write_position);
  59. return 4;
  60. }
  61. void iap_read_string(can_frame_t *frame)
  62. {
  63. uint8_t buff[64];
  64. const char *text;
  65. uint32_t addr;
  66. uint32_t len;
  67. addr = ((uint32_t) frame->data[2]) << 16 | ((uint32_t) frame->data[1]) << 8 | frame->data[0];
  68. text = (const char *) (0x08000000 + addr);
  69. len = strlen(text);
  70. if (len > (sizeof(buff)-4)) {
  71. len = sizeof(buff) - 4;
  72. }
  73. buff[0] = 0;
  74. memcpy(buff+1, frame->data, 3);
  75. memcpy(buff + 4, text, len);
  76. protocol_send_bms_info(frame->head.can_addr, frame->key, buff, len + 4);
  77. }
  78. static int iap_check_image(uint8_t *data, int len) {
  79. uint32_t size, checksum;
  80. size = shark_decode_u24(data);
  81. checksum = shark_decode_u32(data + 3);
  82. uint32_t d_checksum = shark_iap_checksum_init();
  83. d_checksum = shark_iap_checksum_put(d_checksum, (const u8 *)fmc_iap_image_addr(), size);
  84. d_checksum = shark_iap_checksum_finish(d_checksum);
  85. if (checksum != d_checksum) {
  86. data[0] = 1;
  87. return 1;
  88. }
  89. fmc_write_magic(size, checksum, SHARK_IAP_MAGIC_FLASH);
  90. _write_success = 1;
  91. data[0] = 0;
  92. return 1;
  93. }