int flash_erase(uint32_t address, uint32_t num_bytes) { const struct flash_area_desc *area; uint32_t end; int i; flash_native_ensure_file_open(); end = address + num_bytes; for (i = 0; i < FLASH_NUM_AREAS; i++) { area = flash_area_descs + i; if (area->fad_offset >= end) { return 0; } if (address >= area->fad_offset && address < area->fad_offset + area->fad_length) { flash_native_erase(area->fad_offset, area->fad_length); } } return 0; }
int flash_erase_sector(uint32_t sector_address) { int next_sector_id; int sector_id; int area_id; flash_native_ensure_file_open(); area_id = find_area(sector_address); if (area_id == -1) { return -1; } sector_id = flash_area_descs[area_id].fad_sector_id; while (1) { flash_native_erase(sector_address, flash_area_descs[area_id].fad_length); area_id++; if (area_id >= FLASH_NUM_AREAS) { break; } next_sector_id = flash_area_descs[area_id].fad_sector_id; if (next_sector_id != sector_id) { break; } sector_id = next_sector_id; } return 0; }
static int native_flash_read(uint32_t address, void *dst, uint32_t length) { flash_native_ensure_file_open(); memcpy(dst, (char *)file_loc + address, length); return 0; }
int flash_read(uint32_t address, void *dst, uint32_t length) { flash_native_ensure_file_open(); fseek(file, address, SEEK_SET); fread(dst, length, 1, file); return 0; }
static int flash_native_write_internal(uint32_t address, const void *src, uint32_t length, int allow_overwrite) { static uint8_t buf[256]; uint32_t cur; uint32_t end; int chunk_sz; int rc; int i; if (length == 0) { return 0; } end = address + length; flash_native_ensure_file_open(); fseek(file, address, SEEK_SET); cur = address; while (cur < end) { if (end - cur < sizeof buf) { chunk_sz = end - cur; } else { chunk_sz = sizeof buf; } /* Ensure data is not being overwritten. */ if (!allow_overwrite) { rc = flash_read(cur, buf, chunk_sz); assert(rc == 0); for (i = 0; i < chunk_sz; i++) { assert(buf[i] == 0xff); } } cur += chunk_sz; } fseek(file, address, SEEK_SET); rc = fwrite(src, length, 1, file); assert(rc == 1); fflush(file); return 0; }
static int native_flash_erase_sector(uint32_t sector_address) { int area_id; uint32_t len; flash_native_ensure_file_open(); area_id = find_area(sector_address); if (area_id == -1) { return -1; } len = flash_sector_len(area_id); flash_native_erase(sector_address, len); return 0; }