mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Also finished migrating tests with test_relocations and test_exhaustion. The issue I was running into when migrating these tests was a lack of flexibility with what you could do with the block devices. It was possible to hack in some hooks for things like bad blocks and power loss, but it wasn't clean or easily extendable. The solution here was to just put all of these test extensions into a third block device, testbd, that uses the other two example block devices internally. testbd has several useful features for testing. Note this makes it a pretty terrible block device _example_ since these hooks look more complicated than a block device needs to be. - testbd can simulate different erase values, supporting 1s, 0s, other byte patterns, or no erases at all (which can cause surprising bugs). This actually depends on the simulated erase values in ramdb and filebd. I did try to move this out of rambd/filebd, but it's not possible to simulate erases in testbd without buffering entire blocks and creating an excessive amount of extra write operations. - testbd also helps simulate power-loss by containing a "power cycles" counter that is decremented every write operation until it calls exit. This is notably faster than the previous gdb approach, which is valuable since the reentrant tests tend to take a while to resolve. - testbd also tracks wear, which can be manually set and read. This is very useful for testing things like bad block handling, wear leveling, or even changing the effective size of the block device at runtime.
		
			
				
	
	
		
			139 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * Block device emulated in RAM
 | |
|  *
 | |
|  * Copyright (c) 2017, Arm Limited. All rights reserved.
 | |
|  * SPDX-License-Identifier: BSD-3-Clause
 | |
|  */
 | |
| #include "rambd/lfs_rambd.h"
 | |
| 
 | |
| int lfs_rambd_createcfg(const struct lfs_config *cfg,
 | |
|         const struct lfs_rambd_config *bdcfg) {
 | |
|     LFS_TRACE("lfs_rambd_createcfg(%p {.context=%p, "
 | |
|                 ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
 | |
|                 ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
 | |
|                 ".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
 | |
|                 "%p {.erase_value=%"PRId32", .buffer=%p})",
 | |
|             (void*)cfg, cfg->context,
 | |
|             (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
 | |
|             (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
 | |
|             cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
 | |
|             (void*)bdcfg, bdcfg->erase_value, bdcfg->buffer);
 | |
|     lfs_rambd_t *bd = cfg->context;
 | |
|     bd->cfg = bdcfg;
 | |
| 
 | |
|     // allocate buffer?
 | |
|     if (bd->cfg->buffer) {
 | |
|         bd->buffer = bd->cfg->buffer;
 | |
|     } else {
 | |
|         bd->buffer = lfs_malloc(cfg->block_size * cfg->block_count);
 | |
|         if (!bd->buffer) {
 | |
|             LFS_TRACE("lfs_rambd_createcfg -> %d", LFS_ERR_NOMEM);
 | |
|             return LFS_ERR_NOMEM;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // zero for reproducability?
 | |
|     if (bd->cfg->erase_value != -1) {
 | |
|         memset(bd->buffer, bd->cfg->erase_value,
 | |
|                 cfg->block_size * cfg->block_count);
 | |
|     }
 | |
| 
 | |
|     LFS_TRACE("lfs_rambd_createcfg -> %d", 0);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_create(const struct lfs_config *cfg) {
 | |
|     LFS_TRACE("lfs_rambd_create(%p {.context=%p, "
 | |
|                 ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
 | |
|                 ".read_size=%"PRIu32", .prog_size=%"PRIu32", "
 | |
|                 ".block_size=%"PRIu32", .block_count=%"PRIu32"})",
 | |
|             (void*)cfg, cfg->context,
 | |
|             (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
 | |
|             (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
 | |
|             cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count);
 | |
|     static const struct lfs_rambd_config defaults = {.erase_value=-1};
 | |
|     int err = lfs_rambd_createcfg(cfg, &defaults);
 | |
|     LFS_TRACE("lfs_rambd_create -> %d", err);
 | |
|     return err;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_destroy(const struct lfs_config *cfg) {
 | |
|     LFS_TRACE("lfs_rambd_destroy(%p)", (void*)cfg);
 | |
|     // clean up memory
 | |
|     lfs_rambd_t *bd = cfg->context;
 | |
|     if (!bd->cfg->buffer) {
 | |
|         lfs_free(bd->buffer);
 | |
|     }
 | |
|     LFS_TRACE("lfs_rambd_destroy -> %d", 0);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_read(const struct lfs_config *cfg, lfs_block_t block,
 | |
|         lfs_off_t off, void *buffer, lfs_size_t size) {
 | |
|     LFS_TRACE("lfs_rambd_read(%p, 0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
 | |
|             (void*)cfg, block, off, buffer, size);
 | |
|     lfs_rambd_t *bd = cfg->context;
 | |
| 
 | |
|     // check if read is valid
 | |
|     LFS_ASSERT(off  % cfg->read_size == 0);
 | |
|     LFS_ASSERT(size % cfg->read_size == 0);
 | |
|     LFS_ASSERT(block < cfg->block_count);
 | |
| 
 | |
|     // read data
 | |
|     memcpy(buffer, &bd->buffer[block*cfg->block_size + off], size);
 | |
| 
 | |
|     LFS_TRACE("lfs_rambd_read -> %d", 0);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_prog(const struct lfs_config *cfg, lfs_block_t block,
 | |
|         lfs_off_t off, const void *buffer, lfs_size_t size) {
 | |
|     LFS_TRACE("lfs_rambd_prog(%p, 0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
 | |
|             (void*)cfg, block, off, buffer, size);
 | |
|     lfs_rambd_t *bd = cfg->context;
 | |
| 
 | |
|     // check if write is valid
 | |
|     LFS_ASSERT(off  % cfg->prog_size == 0);
 | |
|     LFS_ASSERT(size % cfg->prog_size == 0);
 | |
|     LFS_ASSERT(block < cfg->block_count);
 | |
| 
 | |
|     // check that data was erased? only needed for testing
 | |
|     if (bd->cfg->erase_value != -1) {
 | |
|         for (lfs_off_t i = 0; i < size; i++) {
 | |
|             LFS_ASSERT(bd->buffer[block*cfg->block_size + off + i] ==
 | |
|                     bd->cfg->erase_value);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // program data
 | |
|     memcpy(&bd->buffer[block*cfg->block_size + off], buffer, size);
 | |
| 
 | |
|     LFS_TRACE("lfs_rambd_prog -> %d", 0);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_erase(const struct lfs_config *cfg, lfs_block_t block) {
 | |
|     LFS_TRACE("lfs_rambd_erase(%p, 0x%"PRIx32")", (void*)cfg, block);
 | |
|     lfs_rambd_t *bd = cfg->context;
 | |
| 
 | |
|     // check if erase is valid
 | |
|     LFS_ASSERT(block < cfg->block_count);
 | |
| 
 | |
|     // erase, only needed for testing
 | |
|     if (bd->cfg->erase_value != -1) {
 | |
|         memset(&bd->buffer[block*cfg->block_size],
 | |
|                 bd->cfg->erase_value, cfg->block_size);
 | |
|     }
 | |
| 
 | |
|     LFS_TRACE("lfs_rambd_erase -> %d", 0);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int lfs_rambd_sync(const struct lfs_config *cfg) {
 | |
|     LFS_TRACE("lfs_rambd_sync(%p)", (void*)cfg);
 | |
|     // sync does nothing because we aren't backed by anything real
 | |
|     (void)cfg;
 | |
|     LFS_TRACE("lfs_rambd_sync -> %d", 0);
 | |
|     return 0;
 | |
| }
 |