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