stream_flash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2017, 2020 Nordic Semiconductor ASA
  3. * Copyright (c) 2017 Linaro Limited
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #define LOG_MODULE_NAME STREAM_FLASH
  8. #define LOG_LEVEL CONFIG_STREAM_FLASH_LOG_LEVEL
  9. #include <logging/log.h>
  10. LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_STREAM_FLASH_LOG_LEVEL);
  11. #include <zephyr/types.h>
  12. #include <string.h>
  13. #include <drivers/flash.h>
  14. #include <storage/stream_flash.h>
  15. #ifdef CONFIG_STREAM_FLASH_PROGRESS
  16. #include <settings/settings.h>
  17. static int settings_direct_loader(const char *key, size_t len,
  18. settings_read_cb read_cb, void *cb_arg,
  19. void *param)
  20. {
  21. struct stream_flash_ctx *ctx = (struct stream_flash_ctx *) param;
  22. /* Handle the subtree if it is an exact key match. */
  23. if (settings_name_next(key, NULL) == 0) {
  24. size_t bytes_written = 0;
  25. ssize_t len = read_cb(cb_arg, &bytes_written,
  26. sizeof(bytes_written));
  27. if (len != sizeof(ctx->bytes_written)) {
  28. LOG_ERR("Unable to read bytes_written from storage");
  29. return len;
  30. }
  31. /* Check that loaded progress is not outdated. */
  32. if (bytes_written >= ctx->bytes_written) {
  33. ctx->bytes_written = bytes_written;
  34. } else {
  35. LOG_WRN("Loaded outdated bytes_written %zu < %zu",
  36. bytes_written, ctx->bytes_written);
  37. return 0;
  38. }
  39. #ifdef CONFIG_STREAM_FLASH_ERASE
  40. int rc;
  41. struct flash_pages_info page;
  42. off_t offset = (off_t) (ctx->offset + ctx->bytes_written) - 1;
  43. /* Update the last erased page to avoid deleting already
  44. * written data.
  45. */
  46. if (ctx->bytes_written > 0) {
  47. rc = flash_get_page_info_by_offs(ctx->fdev, offset,
  48. &page);
  49. if (rc != 0) {
  50. LOG_ERR("Error %d while getting page info", rc);
  51. return rc;
  52. }
  53. ctx->last_erased_page_start_offset = page.start_offset;
  54. } else {
  55. ctx->last_erased_page_start_offset = -1;
  56. }
  57. #endif /* CONFIG_STREAM_FLASH_ERASE */
  58. }
  59. return 0;
  60. }
  61. #endif /* CONFIG_STREAM_FLASH_PROGRESS */
  62. #ifdef CONFIG_STREAM_FLASH_ERASE
  63. int stream_flash_erase_page(struct stream_flash_ctx *ctx, off_t off)
  64. {
  65. int rc;
  66. struct flash_pages_info page;
  67. rc = flash_get_page_info_by_offs(ctx->fdev, off, &page);
  68. if (rc != 0) {
  69. LOG_ERR("Error %d while getting page info", rc);
  70. return rc;
  71. }
  72. if (ctx->last_erased_page_start_offset == page.start_offset) {
  73. return 0;
  74. }
  75. LOG_DBG("Erasing page at offset 0x%08lx", (long)page.start_offset);
  76. rc = flash_erase(ctx->fdev, page.start_offset, page.size);
  77. if (rc != 0) {
  78. LOG_ERR("Error %d while erasing page", rc);
  79. } else {
  80. ctx->last_erased_page_start_offset = page.start_offset;
  81. }
  82. return rc;
  83. }
  84. #endif /* CONFIG_STREAM_FLASH_ERASE */
  85. static int flash_sync(struct stream_flash_ctx *ctx)
  86. {
  87. int rc = 0;
  88. size_t write_addr = ctx->offset + ctx->bytes_written;
  89. size_t buf_bytes_aligned;
  90. size_t fill_length;
  91. uint8_t filler;
  92. if (ctx->buf_bytes == 0) {
  93. return 0;
  94. }
  95. if (IS_ENABLED(CONFIG_STREAM_FLASH_ERASE)) {
  96. rc = stream_flash_erase_page(ctx,
  97. write_addr + ctx->buf_bytes - 1);
  98. if (rc < 0) {
  99. LOG_ERR("stream_flash_erase_page err %d offset=0x%08zx",
  100. rc, write_addr);
  101. return rc;
  102. }
  103. }
  104. fill_length = flash_get_write_block_size(ctx->fdev);
  105. if (ctx->buf_bytes % fill_length) {
  106. fill_length -= ctx->buf_bytes % fill_length;
  107. filler = flash_get_parameters(ctx->fdev)->erase_value;
  108. memset(ctx->buf + ctx->buf_bytes, filler, fill_length);
  109. } else {
  110. fill_length = 0;
  111. }
  112. buf_bytes_aligned = ctx->buf_bytes + fill_length;
  113. rc = flash_write(ctx->fdev, write_addr, ctx->buf, buf_bytes_aligned);
  114. if (rc != 0) {
  115. LOG_ERR("flash_write error %d offset=0x%08zx", rc,
  116. write_addr);
  117. return rc;
  118. }
  119. if (ctx->callback) {
  120. /* Invert to ensure that caller is able to discover a faulty
  121. * flash_read() even if no error code is returned.
  122. */
  123. for (int i = 0; i < ctx->buf_bytes; i++) {
  124. ctx->buf[i] = ~ctx->buf[i];
  125. }
  126. rc = flash_read(ctx->fdev, write_addr, ctx->buf,
  127. ctx->buf_bytes);
  128. if (rc != 0) {
  129. LOG_ERR("flash read failed: %d", rc);
  130. return rc;
  131. }
  132. rc = ctx->callback(ctx->buf, ctx->buf_bytes, write_addr);
  133. if (rc != 0) {
  134. LOG_ERR("callback failed: %d", rc);
  135. return rc;
  136. }
  137. }
  138. ctx->bytes_written += ctx->buf_bytes;
  139. ctx->buf_bytes = 0U;
  140. return rc;
  141. }
  142. int stream_flash_buffered_write(struct stream_flash_ctx *ctx, const uint8_t *data,
  143. size_t len, bool flush)
  144. {
  145. int processed = 0;
  146. int rc = 0;
  147. int buf_empty_bytes;
  148. if (!ctx) {
  149. return -EFAULT;
  150. }
  151. if (ctx->bytes_written + ctx->buf_bytes + len > ctx->available) {
  152. return -ENOMEM;
  153. }
  154. while ((len - processed) >=
  155. (buf_empty_bytes = ctx->buf_len - ctx->buf_bytes)) {
  156. memcpy(ctx->buf + ctx->buf_bytes, data + processed,
  157. buf_empty_bytes);
  158. ctx->buf_bytes = ctx->buf_len;
  159. rc = flash_sync(ctx);
  160. if (rc != 0) {
  161. return rc;
  162. }
  163. processed += buf_empty_bytes;
  164. }
  165. /* place rest of the data into ctx->buf */
  166. if (processed < len) {
  167. memcpy(ctx->buf + ctx->buf_bytes,
  168. data + processed, len - processed);
  169. ctx->buf_bytes += len - processed;
  170. }
  171. if (flush && ctx->buf_bytes > 0) {
  172. rc = flash_sync(ctx);
  173. }
  174. return rc;
  175. }
  176. size_t stream_flash_bytes_written(struct stream_flash_ctx *ctx)
  177. {
  178. return ctx->bytes_written;
  179. }
  180. struct _inspect_flash {
  181. size_t buf_len;
  182. size_t total_size;
  183. };
  184. static bool find_flash_total_size(const struct flash_pages_info *info,
  185. void *data)
  186. {
  187. struct _inspect_flash *ctx = (struct _inspect_flash *) data;
  188. if (ctx->buf_len > info->size) {
  189. LOG_ERR("Buffer size is bigger than page");
  190. ctx->total_size = 0;
  191. return false;
  192. }
  193. ctx->total_size += info->size;
  194. return true;
  195. }
  196. int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
  197. uint8_t *buf, size_t buf_len, size_t offset, size_t size,
  198. stream_flash_callback_t cb)
  199. {
  200. if (!ctx || !fdev || !buf) {
  201. return -EFAULT;
  202. }
  203. #ifdef CONFIG_STREAM_FLASH_PROGRESS
  204. int rc = settings_subsys_init();
  205. if (rc != 0) {
  206. LOG_ERR("Error %d initializing settings subsystem", rc);
  207. return rc;
  208. }
  209. #endif
  210. struct _inspect_flash inspect_flash_ctx = {
  211. .buf_len = buf_len,
  212. .total_size = 0
  213. };
  214. if (buf_len % flash_get_write_block_size(fdev)) {
  215. LOG_ERR("Buffer size is not aligned to minimal write-block-size");
  216. return -EFAULT;
  217. }
  218. /* Calculate the total size of the flash device */
  219. flash_page_foreach(fdev, find_flash_total_size, &inspect_flash_ctx);
  220. /* The flash size counted should never be equal zero */
  221. if (inspect_flash_ctx.total_size == 0) {
  222. return -EFAULT;
  223. }
  224. if ((offset + size) > inspect_flash_ctx.total_size ||
  225. offset % flash_get_write_block_size(fdev)) {
  226. LOG_ERR("Incorrect parameter");
  227. return -EFAULT;
  228. }
  229. ctx->fdev = fdev;
  230. ctx->buf = buf;
  231. ctx->buf_len = buf_len;
  232. ctx->bytes_written = 0;
  233. ctx->buf_bytes = 0U;
  234. ctx->offset = offset;
  235. ctx->available = (size == 0 ? inspect_flash_ctx.total_size - offset :
  236. size);
  237. ctx->callback = cb;
  238. #ifdef CONFIG_STREAM_FLASH_ERASE
  239. ctx->last_erased_page_start_offset = -1;
  240. #endif
  241. return 0;
  242. }
  243. #ifdef CONFIG_STREAM_FLASH_PROGRESS
  244. int stream_flash_progress_load(struct stream_flash_ctx *ctx,
  245. const char *settings_key)
  246. {
  247. if (!ctx || !settings_key) {
  248. return -EFAULT;
  249. }
  250. int rc = settings_load_subtree_direct(settings_key,
  251. settings_direct_loader,
  252. (void *) ctx);
  253. if (rc != 0) {
  254. LOG_ERR("Error %d while loading progress for \"%s\"",
  255. rc, settings_key);
  256. }
  257. return rc;
  258. }
  259. int stream_flash_progress_save(struct stream_flash_ctx *ctx,
  260. const char *settings_key)
  261. {
  262. if (!ctx || !settings_key) {
  263. return -EFAULT;
  264. }
  265. int rc = settings_save_one(settings_key,
  266. &ctx->bytes_written,
  267. sizeof(ctx->bytes_written));
  268. if (rc != 0) {
  269. LOG_ERR("Error %d while storing progress for \"%s\"",
  270. rc, settings_key);
  271. }
  272. return rc;
  273. }
  274. int stream_flash_progress_clear(struct stream_flash_ctx *ctx,
  275. const char *settings_key)
  276. {
  277. if (!ctx || !settings_key) {
  278. return -EFAULT;
  279. }
  280. int rc = settings_delete(settings_key);
  281. if (rc != 0) {
  282. LOG_ERR("Error %d while deleting progress for \"%s\"",
  283. rc, settings_key);
  284. }
  285. return rc;
  286. }
  287. #endif /* CONFIG_STREAM_FLASH_PROGRESS */