ota_storage_fs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA storage interface
  9. */
  10. #include <kernel.h>
  11. #include <drivers/flash.h>
  12. #include <string.h>
  13. #include <mem_manager.h>
  14. #include <ota_storage.h>
  15. #include <os_common_api.h>
  16. #ifdef CONFIG_FILE_SYSTEM
  17. #include <fs/fs.h>
  18. #endif
  19. #include <stream.h>
  20. #define CONFIG_XSPI_NOR_ACTS_DEV_NAME "spi_flash"
  21. #define XIP_DEV_NAME CONFIG_XSPI_NOR_ACTS_DEV_NAME
  22. #define CONFIG_MMC_SDCARD_DEV_NAME "sd"
  23. struct ota_storage
  24. {
  25. struct device *dev;
  26. const char *dev_name;
  27. struct fs_file_t *fs;
  28. int max_write_seg;
  29. int storage_id; /* code run on this device? */
  30. };
  31. static struct ota_storage global_ota_storage;
  32. int ota_storage_bind_fs(struct fs_file_t *fs)
  33. {
  34. global_ota_storage.fs = fs;
  35. return 0;
  36. }
  37. int ota_storage_unbind_fs(struct fs_file_t *fs)
  38. {
  39. if (global_ota_storage.fs == fs)
  40. global_ota_storage.fs = NULL;
  41. return 0;
  42. }
  43. int ota_storage_sync(struct ota_storage *storage)
  44. {
  45. int res = -EINVAL;
  46. if (storage && storage->fs)
  47. res = fs_sync(storage->fs);
  48. return res;
  49. }
  50. void ota_storage_set_max_write_seg(struct ota_storage *storage, int max_write_seg)
  51. {
  52. if (max_write_seg <= 0)
  53. return;
  54. storage->max_write_seg = max_write_seg;
  55. }
  56. int ota_storage_get_storage_id(struct ota_storage *storage)
  57. {
  58. return storage->storage_id;
  59. }
  60. int ota_storage_write(struct ota_storage *storage, int offs, uint8_t *buf, int size)
  61. {
  62. if (storage->fs) {
  63. fs_seek(storage->fs, offs, SEEK_DIR_BEG);
  64. fs_write(storage->fs, buf, size);
  65. }
  66. return 0;
  67. }
  68. int ota_storage_read(struct ota_storage *storage, int offs, uint8_t *buf, int size)
  69. {
  70. if (storage->fs) {
  71. fs_seek(storage->fs, offs, SEEK_DIR_BEG);
  72. fs_read(storage->fs, buf, size);
  73. }
  74. return 0;
  75. }
  76. int ota_storage_is_clean(struct ota_storage *storage, int offs, int size,
  77. uint8_t *buf, int buf_size)
  78. {
  79. SYS_LOG_INF("fs: ignore check clean");
  80. return 1;
  81. }
  82. int ota_storage_erase(struct ota_storage *storage, int offs, int size)
  83. {
  84. return 0;
  85. }
  86. struct ota_storage *ota_storage_init(const char *storage_name)
  87. {
  88. struct ota_storage *storage;
  89. const struct device *nor_dev;
  90. SYS_LOG_INF("init storage %s\n", storage_name);
  91. nor_dev = device_get_binding(storage_name);
  92. if (!nor_dev) {
  93. SYS_LOG_ERR("cannot found storage device %s", storage_name);
  94. return NULL;
  95. }
  96. storage = &global_ota_storage;
  97. memset(storage, 0x0, sizeof(struct ota_storage));
  98. storage->dev = nor_dev;
  99. storage->dev_name = storage_name;
  100. storage->max_write_seg = OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE;
  101. if (strcmp(storage_name, CONFIG_XSPI_NOR_ACTS_DEV_NAME) == 0)
  102. storage->storage_id = 0;
  103. else
  104. storage->storage_id = 1;
  105. return storage;
  106. }
  107. void ota_storage_exit(struct ota_storage *storage)
  108. {
  109. SYS_LOG_INF("exit");
  110. storage = NULL;
  111. }