wm_fwup.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "wm_internal_flash.h"
  2. #include "wm_flash.h"
  3. #include "wm_crypto_hard.h"
  4. #include "wm_flash_map.h"
  5. #include "wm_fwup.h"
  6. #define CODE_UPD_HEADER_ADDR 0x80FC000
  7. #define WM_UPDATE_BASE (0x8090000)
  8. void tls_fwup_img_update_header(T_BOOTER* img_param)
  9. {
  10. unsigned char current_img;
  11. psCrcContext_t crcContext;
  12. T_BOOTER imgheader[2];
  13. tls_fls_read(CODE_RUN_HEADER_ADDR, (unsigned char *)&imgheader[0], sizeof(T_BOOTER));
  14. tls_fls_read(CODE_UPD_HEADER_ADDR, (unsigned char *)&imgheader[1], sizeof(T_BOOTER));
  15. //将两个upd_no中较大的那个值取出来,再将其加1后赋值给 CODE_UPD_HEADER_ADDR 处的header;
  16. if (tls_fwup_img_header_check(&imgheader[1]))
  17. {
  18. current_img = (imgheader[1].upd_no > imgheader[0].upd_no);
  19. }
  20. else
  21. {
  22. current_img = 0;
  23. }
  24. img_param->upd_no = imgheader[current_img].upd_no + 1;
  25. tls_crypto_crc_init(&crcContext, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, 3);
  26. tls_crypto_crc_update(&crcContext, (unsigned char *)img_param, sizeof(T_BOOTER)-4);
  27. tls_crypto_crc_final(&crcContext, &img_param->hd_checksum);
  28. tls_fls_write(CODE_UPD_HEADER_ADDR, (unsigned char *)img_param, sizeof(T_BOOTER));
  29. }
  30. int tls_fwup_img_write(unsigned int offset, void *buf, int size)
  31. {
  32. uint32_t addr = WM_UPDATE_BASE + offset;
  33. return tls_fls_write(addr, buf, size);
  34. }
  35. int tls_fwup_flash_erase(unsigned int offset)
  36. {
  37. uint32_t addr = WM_UPDATE_BASE + offset;
  38. return tls_fls_erase(addr/4096);
  39. }