fw_version.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <kernel.h>
  2. #include <init.h>
  3. #include <soc.h>
  4. #include "fw_version.h"
  5. #include <crc.h>
  6. #include <drivers/nvram_config.h>
  7. static struct fw_version fw_ver;
  8. static int fw_version_get_factory_ver(struct fw_version *ver)
  9. {
  10. int rlen;
  11. struct code_res_version code_res;
  12. memset(&code_res, 0x0, sizeof(struct code_res_version));
  13. rlen = nvram_config_get_factory(FIRMWARE_VERSION, &code_res, sizeof(struct code_res_version));
  14. if (rlen != sizeof(struct code_res_version)) {
  15. printk("cannot found FIRMWARE_VERSION\n");
  16. code_res.version_code = ver->version_code;
  17. code_res.version_res = ver->version_code;
  18. nvram_config_set_factory(FIRMWARE_VERSION, &code_res, sizeof(struct code_res_version));
  19. }else{
  20. ver->version_code = code_res.version_code;
  21. ver->version_res = code_res.version_res;
  22. }
  23. return 0;
  24. }
  25. const struct fw_version *fw_version_get_current(void)
  26. {
  27. const struct fw_version *ver =
  28. (struct fw_version *)soc_boot_get_fw_ver_addr();
  29. memcpy(&fw_ver, ver, sizeof(struct fw_version));
  30. fw_version_get_factory_ver(&fw_ver);
  31. return &fw_ver;
  32. }
  33. void fw_version_dump(const struct fw_version *ver)
  34. {
  35. printk("*** Current Firmware Version ***\n");
  36. printk(" Firmware Version: 0x%08x\n", ver->version_code);
  37. printk(" res Version: 0x%08x\n", ver->version_res);
  38. printk(" System Version: 0x%08x\n", ver->system_version_code);
  39. printk(" Version Name: %s\n", ver->version_name);
  40. printk(" Board Name: %s\n", ver->board_name);
  41. }
  42. int fw_version_check(const struct fw_version *ver)
  43. {
  44. uint32_t checksum;
  45. if (ver->magic != FIRMWARE_VERSION_MAGIC)
  46. return -1;
  47. checksum = utils_crc32(0, (const u8_t *)ver, sizeof(struct fw_version) - 4);
  48. if (ver->checksum != checksum)
  49. return -1;
  50. return 0;
  51. }
  52. static int fw_version_init(const struct device *dev)
  53. {
  54. ARG_UNUSED(dev);
  55. const struct fw_version *ver = fw_version_get_current();
  56. if (fw_version_check((struct fw_version *)soc_boot_get_fw_ver_addr())) {
  57. printk("BAD firmware version !!!\n");
  58. return -1;
  59. }
  60. fw_version_dump(ver);
  61. return 0;
  62. }
  63. SYS_INIT(fw_version_init, APPLICATION, 1);