1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

mtd_mapper: count offset in sectors

The offset of MTD regions must be aligned with erase sectors.
So in order not to waste address space, avoid misconfiguration and
eventually support storage media > 4 GiB, give the offset in sectors
instead of bytes.
This commit is contained in:
Benjamin Valentin 2020-05-08 23:45:34 +02:00
parent ed95944ed7
commit 81b07895af
3 changed files with 14 additions and 11 deletions

View File

@ -41,7 +41,7 @@
* .page_size = PAGE_SIZE,
* },
* .parent = &parent,
* .offset = PAGE_PER_SECTOR * PAGE_SIZE * SECTOR_COUNT / 2
* .sector = SECTOR_COUNT / 2
* };
*
* mtd_dev_t *dev = &region.mtd;
@ -96,7 +96,7 @@ typedef struct {
typedef struct {
mtd_dev_t mtd; /**< MTD context */
mtd_mapper_parent_t *parent; /**< MTD mapper parent device */
uint32_t offset; /**< Offset address to start this region */
uint32_t sector; /**< first sector of the region */
} mtd_mapper_region_t;
/**

View File

@ -45,6 +45,12 @@ static uint32_t _region_size(mtd_mapper_region_t *region)
region->mtd.sector_count;
}
static uint32_t _byte_offset(mtd_mapper_region_t *region)
{
return region->mtd.pages_per_sector * region->mtd.page_size *
region->sector;
}
static int _init_target(mtd_mapper_region_t *region)
{
mtd_mapper_parent_t *parent = region->parent;
@ -68,10 +74,7 @@ static int _init(mtd_dev_t *mtd)
assert(backing_mtd->sector_count >= region->mtd.sector_count);
/* offset + region size must not exceed the backing device */
assert(region->offset + _region_size(
region) <=
backing_mtd->pages_per_sector * backing_mtd->sector_count *
backing_mtd->page_size);
assert(region->sector + region->mtd.sector_count <= backing_mtd->sector_count);
/* avoid unused variable warning if compiled with NDEBUG */
(void)backing_mtd;
@ -92,7 +95,7 @@ static int _write(mtd_dev_t *mtd, const void *src, uint32_t addr,
}
_lock(region);
int res = mtd_write(region->parent->mtd, src, addr + region->offset, count);
int res = mtd_write(region->parent->mtd, src, addr + _byte_offset(region), count);
_unlock(region);
return res;
}
@ -106,7 +109,7 @@ static int _read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t count)
}
_lock(region);
int res = mtd_read(region->parent->mtd, dest, addr + region->offset, count);
int res = mtd_read(region->parent->mtd, dest, addr + _byte_offset(region), count);
_unlock(region);
return res;
}
@ -120,7 +123,7 @@ static int _erase(mtd_dev_t *mtd, uint32_t addr, uint32_t count)
}
_lock(region);
int res = mtd_erase(region->parent->mtd, addr + region->offset, count);
int res = mtd_erase(region->parent->mtd, addr + _byte_offset(region), count);
_unlock(region);
return res;
}

View File

@ -131,7 +131,7 @@ static mtd_mapper_region_t _region_a = {
.page_size = PAGE_SIZE,
},
.parent = &_parent,
.offset = 0,
.sector = 0,
};
static mtd_mapper_region_t _region_b = {
@ -142,7 +142,7 @@ static mtd_mapper_region_t _region_b = {
.page_size = PAGE_SIZE,
},
.parent = &_parent,
.offset = PAGE_PER_SECTOR * PAGE_SIZE * SECTOR_COUNT / 2,
.sector = SECTOR_COUNT / 2,
};
static mtd_dev_t *_dev_a = &_region_a.mtd;