fs: add block_is_erased() to check if a block is truly erased
This commit is contained in:
parent
31740bbb20
commit
2448ddcd4e
3 changed files with 33 additions and 0 deletions
2
Makefile
2
Makefile
|
@ -23,6 +23,8 @@ obj-y += source/shell/flash.o
|
|||
|
||||
obj-y += source/core/flash.o
|
||||
|
||||
obj-y += source/fs/block.o
|
||||
|
||||
obj = $(addprefix $(BUILD)/, $(obj-y))
|
||||
|
||||
# Include the dependencies.
|
||||
|
|
3
include/fs/block.h
Normal file
3
include/fs/block.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
int block_is_erased(struct flash_dev *dev, uint32_t addr, uint32_t len);
|
28
source/fs/block.c
Normal file
28
source/fs/block.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <flash.h>
|
||||
#include <macros.h>
|
||||
|
||||
#include <fs/block.h>
|
||||
|
||||
int block_is_erased(struct flash_dev *dev, uint32_t addr, uint32_t len)
|
||||
{
|
||||
char buf[32], cmp[32];
|
||||
size_t count;
|
||||
|
||||
memset(cmp, 0xff, sizeof cmp);
|
||||
|
||||
for (; len; addr += count, len -= count) {
|
||||
count = min(len, sizeof buf);
|
||||
|
||||
if (flash_read(dev, addr, buf, count) < 0)
|
||||
return -1;
|
||||
|
||||
if (memcmp(buf, cmp, count) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue