ota_storage.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef __OTA_STORAGE_H__
  11. #define __OTA_STORAGE_H__
  12. #include <device.h>
  13. #ifdef CONFIG_FILE_SYSTEM
  14. #include <fs/fs.h>
  15. #endif
  16. #define OTA_STORAGE_DEFAULT_WRITE_SEGMENT_SIZE (4*1024)
  17. #define OTA_STORAGE_DEFAULT_READ_SEGMENT_SIZE (4*1024)
  18. #define OTA_STORAGE_DEFAULT_ERASE_SEGMENT_SIZE (64*1024)
  19. #define OTA_STORAGE_MAX_ERASE_SEGMENT_SIZE (1024*1024)
  20. struct ota_storage;
  21. /**
  22. * @brief ota storage init funcion
  23. *
  24. * This routine calls to init ota storage,called by libota
  25. *
  26. * @param storage_name the storage name
  27. *
  28. * @return pointer to storage struct address.
  29. * @return NULL if init failed.
  30. */
  31. struct ota_storage *ota_storage_init(const char *storage_name);
  32. /**
  33. * @brief ota storage find funcion
  34. *
  35. * This routine calls to get storage pinter,called by libota
  36. *
  37. * @param storage_id the storage id
  38. *
  39. * @return pointer to storage struct address.
  40. * @return NULL if init failed.
  41. */
  42. struct ota_storage *ota_storage_find(int storage_id);
  43. /**
  44. * @brief ota storage exit funcion
  45. *
  46. * This routine calls to make storage pinter to NULL,called by libota
  47. *
  48. * @param storage pointer to storage struct address
  49. *
  50. */
  51. void ota_storage_exit(struct ota_storage *storage);
  52. /**
  53. * Read data from storage.
  54. * @param storage pointer to storage
  55. * @param offs starting offset of storage for read
  56. * @param buf storage will write its data here
  57. * @param size bytes user want to read from storage
  58. * @return 0: read success.
  59. * @return other: read fail.
  60. */
  61. int ota_storage_read(struct ota_storage *storage, int offs,
  62. uint8_t *buf, int size);
  63. /**
  64. * write data to storage.
  65. * @param storage pointer to storage
  66. * @param offs starting offset of storage for the write
  67. * @param buf data poninter of write
  68. * @param size bytes user want to write to storage
  69. * @return 0: write success.
  70. * @return other: write fail.
  71. */
  72. int ota_storage_write(struct ota_storage *storage, int offs,
  73. uint8_t *buf, int size);
  74. /**
  75. * erase the storage.
  76. * @param storage pointer to storage
  77. * @param offs erase area starting offset
  78. * @param size size of area to be erased
  79. * @return 0: erase success.
  80. * @return other: erase fail.
  81. */
  82. int ota_storage_erase(struct ota_storage *storage, int offs, int size);
  83. /**
  84. * check the area of the storage is clean.
  85. * @param storage pointer to storage
  86. * @param offs starting offset of storage for the check
  87. * @param size bytes user want to check from storage
  88. * @param buf read data to here from storage
  89. * @param buf_size size of the buf
  90. * @return 0: clean.
  91. * @return other: dirty.
  92. */
  93. int ota_storage_is_clean(struct ota_storage *storage, int offs, int size,
  94. uint8_t *buf, int buf_size);
  95. void ota_storage_set_max_write_seg(struct ota_storage *storage, int max_write_seg);
  96. void ota_storage_set_max_erase_seg(struct ota_storage *storage, int max_erase_seg);
  97. /**
  98. * get the storage id.
  99. * @param storage pointer to storage
  100. * @return storage id.
  101. */
  102. int ota_storage_get_storage_id(struct ota_storage *storage);
  103. #ifdef CONFIG_FILE_SYSTEM
  104. /**
  105. * bind fs to the the storage.
  106. * @param fs pointer to fs
  107. * @return 0.
  108. */
  109. int ota_storage_bind_fs(struct fs_file_t *fs);
  110. /**
  111. * unbind fs to the the storage.
  112. * @param fs pointer to fs
  113. * @return 0.
  114. */
  115. int ota_storage_unbind_fs(struct fs_file_t *fs);
  116. #endif
  117. /**
  118. * @brief Flush cached write data buffers of an open file
  119. *
  120. * The function flushes the cache of an open file; it can be invoked to ensure
  121. * data gets written to the storage media immediately, e.g. to avoid data loss
  122. * @param storage pointer to storage
  123. * in case if power is removed unexpectedly.
  124. * @return 0 on success;
  125. */
  126. int ota_storage_sync(struct ota_storage *storage);
  127. #endif /* __OTA_STORAGE_H__ */