12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157 |
- #include <drivers/flash.h>
- #include <string.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <fs/nvs.h>
- #include <sys/crc.h>
- #include "nvs_priv.h"
- #include <logging/log.h>
- LOG_MODULE_REGISTER(fs_nvs, CONFIG_NVS_LOG_LEVEL);
- static inline size_t nvs_al_size(struct nvs_fs *fs, size_t len)
- {
- uint8_t write_block_size = fs->flash_parameters->write_block_size;
- if (write_block_size <= 1U) {
- return len;
- }
- return (len + (write_block_size - 1U)) & ~(write_block_size - 1U);
- }
- static int nvs_flash_al_wrt(struct nvs_fs *fs, uint32_t addr, const void *data,
- size_t len)
- {
- const uint8_t *data8 = (const uint8_t *)data;
- int rc = 0;
- off_t offset;
- size_t blen;
- uint8_t buf[NVS_BLOCK_SIZE];
- if (!len) {
-
- return 0;
- }
- offset = fs->offset;
- offset += fs->sector_size * (addr >> ADDR_SECT_SHIFT);
- offset += addr & ADDR_OFFS_MASK;
- blen = len & ~(fs->flash_parameters->write_block_size - 1U);
- if (blen > 0) {
- rc = flash_write(fs->flash_device, offset, data8, blen);
- if (rc) {
-
- goto end;
- }
- len -= blen;
- offset += blen;
- data8 += blen;
- }
- if (len) {
- memcpy(buf, data8, len);
- (void)memset(buf + len, fs->flash_parameters->erase_value,
- fs->flash_parameters->write_block_size - len);
- rc = flash_write(fs->flash_device, offset, buf,
- fs->flash_parameters->write_block_size);
- }
- end:
- return rc;
- }
- static int nvs_flash_rd(struct nvs_fs *fs, uint32_t addr, void *data,
- size_t len)
- {
- int rc;
- off_t offset;
- offset = fs->offset;
- offset += fs->sector_size * (addr >> ADDR_SECT_SHIFT);
- offset += addr & ADDR_OFFS_MASK;
- rc = flash_read(fs->flash_device, offset, data, len);
- return rc;
- }
- static int nvs_flash_ate_wrt(struct nvs_fs *fs, const struct nvs_ate *entry)
- {
- int rc;
- rc = nvs_flash_al_wrt(fs, fs->ate_wra, entry,
- sizeof(struct nvs_ate));
- fs->ate_wra -= nvs_al_size(fs, sizeof(struct nvs_ate));
- return rc;
- }
- static int nvs_flash_data_wrt(struct nvs_fs *fs, const void *data, size_t len)
- {
- int rc;
- rc = nvs_flash_al_wrt(fs, fs->data_wra, data, len);
- fs->data_wra += nvs_al_size(fs, len);
- return rc;
- }
- static int nvs_flash_ate_rd(struct nvs_fs *fs, uint32_t addr,
- struct nvs_ate *entry)
- {
- return nvs_flash_rd(fs, addr, entry, sizeof(struct nvs_ate));
- }
- static int nvs_flash_block_cmp(struct nvs_fs *fs, uint32_t addr, const void *data,
- size_t len)
- {
- const uint8_t *data8 = (const uint8_t *)data;
- int rc;
- size_t bytes_to_cmp, block_size;
- uint8_t buf[NVS_BLOCK_SIZE];
- block_size =
- NVS_BLOCK_SIZE & ~(fs->flash_parameters->write_block_size - 1U);
- while (len) {
- bytes_to_cmp = MIN(block_size, len);
- rc = nvs_flash_rd(fs, addr, buf, bytes_to_cmp);
- if (rc) {
- return rc;
- }
- rc = memcmp(data8, buf, bytes_to_cmp);
- if (rc) {
- return 1;
- }
- len -= bytes_to_cmp;
- addr += bytes_to_cmp;
- data8 += bytes_to_cmp;
- }
- return 0;
- }
- static int nvs_flash_cmp_const(struct nvs_fs *fs, uint32_t addr, uint8_t value,
- size_t len)
- {
- int rc;
- size_t bytes_to_cmp, block_size;
- uint8_t cmp[NVS_BLOCK_SIZE];
- block_size =
- NVS_BLOCK_SIZE & ~(fs->flash_parameters->write_block_size - 1U);
- (void)memset(cmp, value, block_size);
- while (len) {
- bytes_to_cmp = MIN(block_size, len);
- rc = nvs_flash_block_cmp(fs, addr, cmp, bytes_to_cmp);
- if (rc) {
- return rc;
- }
- len -= bytes_to_cmp;
- addr += bytes_to_cmp;
- }
- return 0;
- }
- static int nvs_flash_block_move(struct nvs_fs *fs, uint32_t addr, size_t len)
- {
- int rc;
- size_t bytes_to_copy, block_size;
- uint8_t buf[NVS_BLOCK_SIZE];
- block_size =
- NVS_BLOCK_SIZE & ~(fs->flash_parameters->write_block_size - 1U);
- while (len) {
- bytes_to_copy = MIN(block_size, len);
- rc = nvs_flash_rd(fs, addr, buf, bytes_to_copy);
- if (rc) {
- return rc;
- }
- rc = nvs_flash_data_wrt(fs, buf, bytes_to_copy);
- if (rc) {
- return rc;
- }
- len -= bytes_to_copy;
- addr += bytes_to_copy;
- }
- return 0;
- }
- static int nvs_flash_erase_sector(struct nvs_fs *fs, uint32_t addr)
- {
- int rc;
- off_t offset;
- addr &= ADDR_SECT_MASK;
- offset = fs->offset;
- offset += fs->sector_size * (addr >> ADDR_SECT_SHIFT);
- LOG_DBG("Erasing flash at %lx, len %d", (long int) offset,
- fs->sector_size);
- rc = flash_erase(fs->flash_device, offset, fs->sector_size);
- if (rc) {
- return rc;
- }
- if (nvs_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value,
- fs->sector_size)) {
- rc = -ENXIO;
- }
- return rc;
- }
- static void nvs_ate_crc8_update(struct nvs_ate *entry)
- {
- uint8_t crc8;
- crc8 = crc8_ccitt(0xff, entry, offsetof(struct nvs_ate, crc8));
- entry->crc8 = crc8;
- }
- static int nvs_ate_crc8_check(const struct nvs_ate *entry)
- {
- uint8_t crc8;
- crc8 = crc8_ccitt(0xff, entry, offsetof(struct nvs_ate, crc8));
- if (crc8 == entry->crc8) {
- return 0;
- }
- return 1;
- }
- static int nvs_ate_cmp_const(const struct nvs_ate *entry, uint8_t value)
- {
- const uint8_t *data8 = (const uint8_t *)entry;
- int i;
- for (i = 0; i < sizeof(struct nvs_ate); i++) {
- if (data8[i] != value) {
- return 1;
- }
- }
- return 0;
- }
- static int nvs_ate_valid(struct nvs_fs *fs, const struct nvs_ate *entry)
- {
- size_t ate_size;
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- if ((nvs_ate_crc8_check(entry)) ||
- (entry->offset >= (fs->sector_size - ate_size))) {
- return 0;
- }
- return 1;
- }
- static int nvs_close_ate_valid(struct nvs_fs *fs, const struct nvs_ate *entry)
- {
- size_t ate_size;
- if ((!nvs_ate_valid(fs, entry)) || (entry->len != 0U) ||
- (entry->id != 0xFFFF)) {
- return 0;
- }
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- if ((fs->sector_size - entry->offset) % ate_size) {
- return 0;
- }
- return 1;
- }
- static int nvs_flash_wrt_entry(struct nvs_fs *fs, uint16_t id, const void *data,
- size_t len)
- {
- int rc;
- struct nvs_ate entry;
- size_t ate_size;
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- entry.id = id;
- entry.offset = (uint16_t)(fs->data_wra & ADDR_OFFS_MASK);
- entry.len = (uint16_t)len;
- entry.part = 0xff;
- nvs_ate_crc8_update(&entry);
- rc = nvs_flash_data_wrt(fs, data, len);
- if (rc) {
- return rc;
- }
- rc = nvs_flash_ate_wrt(fs, &entry);
- if (rc) {
- return rc;
- }
- return 0;
- }
- static int nvs_recover_last_ate(struct nvs_fs *fs, uint32_t *addr)
- {
- uint32_t data_end_addr, ate_end_addr;
- struct nvs_ate end_ate;
- size_t ate_size;
- int rc;
- LOG_DBG("Recovering last ate from sector %d",
- (*addr >> ADDR_SECT_SHIFT));
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- *addr -= ate_size;
- ate_end_addr = *addr;
- data_end_addr = *addr & ADDR_SECT_MASK;
- while (ate_end_addr > data_end_addr) {
- rc = nvs_flash_ate_rd(fs, ate_end_addr, &end_ate);
- if (rc) {
- return rc;
- }
- if (nvs_ate_valid(fs, &end_ate)) {
-
- data_end_addr &= ADDR_SECT_MASK;
- data_end_addr += end_ate.offset + end_ate.len;
- *addr = ate_end_addr;
- }
- ate_end_addr -= ate_size;
- }
- return 0;
- }
- static int nvs_prev_ate(struct nvs_fs *fs, uint32_t *addr, struct nvs_ate *ate)
- {
- int rc;
- struct nvs_ate close_ate;
- size_t ate_size;
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- rc = nvs_flash_ate_rd(fs, *addr, ate);
- if (rc) {
- return rc;
- }
- *addr += ate_size;
- if (((*addr) & ADDR_OFFS_MASK) != (fs->sector_size - ate_size)) {
- return 0;
- }
-
- if (((*addr) >> ADDR_SECT_SHIFT) == 0U) {
- *addr += ((fs->sector_count - 1) << ADDR_SECT_SHIFT);
- } else {
- *addr -= (1 << ADDR_SECT_SHIFT);
- }
- rc = nvs_flash_ate_rd(fs, *addr, &close_ate);
- if (rc) {
- return rc;
- }
- rc = nvs_ate_cmp_const(&close_ate, fs->flash_parameters->erase_value);
-
- if (!rc) {
- *addr = fs->ate_wra;
- return 0;
- }
-
- if (nvs_close_ate_valid(fs, &close_ate)) {
- (*addr) &= ADDR_SECT_MASK;
- (*addr) += close_ate.offset;
- return 0;
- }
-
- return nvs_recover_last_ate(fs, addr);
- }
- static void nvs_sector_advance(struct nvs_fs *fs, uint32_t *addr)
- {
- *addr += (1 << ADDR_SECT_SHIFT);
- if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) {
- *addr -= (fs->sector_count << ADDR_SECT_SHIFT);
- }
- }
- static int nvs_sector_close(struct nvs_fs *fs)
- {
- int rc;
- struct nvs_ate close_ate;
- size_t ate_size;
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- close_ate.id = 0xFFFF;
- close_ate.len = 0U;
- close_ate.offset = (uint16_t)((fs->ate_wra + ate_size) & ADDR_OFFS_MASK);
- fs->ate_wra &= ADDR_SECT_MASK;
- fs->ate_wra += (fs->sector_size - ate_size);
- nvs_ate_crc8_update(&close_ate);
- rc = nvs_flash_ate_wrt(fs, &close_ate);
- nvs_sector_advance(fs, &fs->ate_wra);
- fs->data_wra = fs->ate_wra & ADDR_SECT_MASK;
- return 0;
- }
- static int nvs_add_gc_done_ate(struct nvs_fs *fs)
- {
- struct nvs_ate gc_done_ate;
- LOG_DBG("Adding gc done ate at %x", fs->ate_wra & ADDR_OFFS_MASK);
- gc_done_ate.id = 0xffff;
- gc_done_ate.len = 0U;
- gc_done_ate.offset = (uint16_t)(fs->data_wra & ADDR_OFFS_MASK);
- nvs_ate_crc8_update(&gc_done_ate);
- return nvs_flash_ate_wrt(fs, &gc_done_ate);
- }
- static int nvs_gc(struct nvs_fs *fs)
- {
- int rc;
- struct nvs_ate close_ate, gc_ate, wlk_ate;
- uint32_t sec_addr, gc_addr, gc_prev_addr, wlk_addr, wlk_prev_addr,
- data_addr, stop_addr;
- size_t ate_size;
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- sec_addr = (fs->ate_wra & ADDR_SECT_MASK);
- nvs_sector_advance(fs, &sec_addr);
- gc_addr = sec_addr + fs->sector_size - ate_size;
-
- rc = nvs_flash_ate_rd(fs, gc_addr, &close_ate);
- if (rc < 0) {
-
- return rc;
- }
- rc = nvs_ate_cmp_const(&close_ate, fs->flash_parameters->erase_value);
- if (!rc) {
- goto gc_done;
- }
- stop_addr = gc_addr - ate_size;
- if (nvs_close_ate_valid(fs, &close_ate)) {
- gc_addr &= ADDR_SECT_MASK;
- gc_addr += close_ate.offset;
- } else {
- rc = nvs_recover_last_ate(fs, &gc_addr);
- if (rc) {
- return rc;
- }
- }
- do {
- gc_prev_addr = gc_addr;
- rc = nvs_prev_ate(fs, &gc_addr, &gc_ate);
- if (rc) {
- return rc;
- }
- if (!nvs_ate_valid(fs, &gc_ate)) {
- continue;
- }
- wlk_addr = fs->ate_wra;
- do {
- wlk_prev_addr = wlk_addr;
- rc = nvs_prev_ate(fs, &wlk_addr, &wlk_ate);
- if (rc) {
- return rc;
- }
-
- if ((wlk_ate.id == gc_ate.id) &&
- (nvs_ate_valid(fs, &wlk_ate))) {
- break;
- }
- } while (wlk_addr != fs->ate_wra);
-
- if ((wlk_prev_addr == gc_prev_addr) && gc_ate.len) {
-
- LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len);
- data_addr = (gc_prev_addr & ADDR_SECT_MASK);
- data_addr += gc_ate.offset;
- gc_ate.offset = (uint16_t)(fs->data_wra & ADDR_OFFS_MASK);
- nvs_ate_crc8_update(&gc_ate);
- rc = nvs_flash_block_move(fs, data_addr, gc_ate.len);
- if (rc) {
- return rc;
- }
- rc = nvs_flash_ate_wrt(fs, &gc_ate);
- if (rc) {
- return rc;
- }
- }
- } while (gc_prev_addr != stop_addr);
- gc_done:
-
- if (fs->ate_wra >= (fs->data_wra + ate_size)) {
- rc = nvs_add_gc_done_ate(fs);
- if (rc) {
- return rc;
- }
- }
-
- rc = nvs_flash_erase_sector(fs, sec_addr);
- if (rc) {
- return rc;
- }
- return 0;
- }
- static int nvs_startup(struct nvs_fs *fs)
- {
- int rc;
- struct nvs_ate last_ate;
- size_t ate_size, empty_len;
-
- uint32_t addr = 0U;
- uint16_t i, closed_sectors = 0;
- uint8_t erase_value = fs->flash_parameters->erase_value;
- k_mutex_lock(&fs->nvs_lock, K_FOREVER);
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
-
- for (i = 0; i < fs->sector_count; i++) {
- addr = (i << ADDR_SECT_SHIFT) +
- (uint16_t)(fs->sector_size - ate_size);
- rc = nvs_flash_cmp_const(fs, addr, erase_value,
- sizeof(struct nvs_ate));
- if (rc) {
-
- closed_sectors++;
- nvs_sector_advance(fs, &addr);
- rc = nvs_flash_cmp_const(fs, addr, erase_value,
- sizeof(struct nvs_ate));
- if (!rc) {
-
- break;
- }
- }
- }
-
- if (closed_sectors == fs->sector_count) {
- rc = -EDEADLK;
- goto end;
- }
- if (i == fs->sector_count) {
-
- rc = nvs_flash_cmp_const(fs, addr - ate_size, erase_value,
- sizeof(struct nvs_ate));
- if (!rc) {
-
- nvs_sector_advance(fs, &addr);
- }
- }
-
- rc = nvs_recover_last_ate(fs, &addr);
- if (rc) {
- goto end;
- }
-
- fs->ate_wra = addr;
- fs->data_wra = addr & ADDR_SECT_MASK;
- while (fs->ate_wra >= fs->data_wra) {
- rc = nvs_flash_ate_rd(fs, fs->ate_wra, &last_ate);
- if (rc) {
- goto end;
- }
- rc = nvs_ate_cmp_const(&last_ate, erase_value);
- if (!rc) {
-
- break;
- }
- if (nvs_ate_valid(fs, &last_ate)) {
-
- fs->data_wra = addr & ADDR_SECT_MASK;
-
- fs->data_wra += nvs_al_size(fs, last_ate.offset + last_ate.len);
-
- if (fs->ate_wra == fs->data_wra && last_ate.len) {
-
- rc = -ESPIPE;
- goto end;
- }
- }
- fs->ate_wra -= ate_size;
- }
-
- addr = fs->ate_wra & ADDR_SECT_MASK;
- nvs_sector_advance(fs, &addr);
- rc = nvs_flash_cmp_const(fs, addr, erase_value, fs->sector_size);
- if (rc < 0) {
- goto end;
- }
- if (rc) {
-
- bool gc_done_marker = false;
- struct nvs_ate gc_done_ate;
- addr = fs->ate_wra + ate_size;
- while ((addr & ADDR_OFFS_MASK) < (fs->sector_size - ate_size)) {
- rc = nvs_flash_ate_rd(fs, addr, &gc_done_ate);
- if (rc) {
- goto end;
- }
- if (nvs_ate_valid(fs, &gc_done_ate) &&
- (gc_done_ate.id == 0xffff) &&
- (gc_done_ate.len == 0U)) {
- gc_done_marker = true;
- break;
- }
- addr += ate_size;
- }
- if (gc_done_marker) {
-
- LOG_INF("GC Done marker found");
- addr = fs->ate_wra & ADDR_SECT_MASK;
- nvs_sector_advance(fs, &addr);
- rc = nvs_flash_erase_sector(fs, addr);
- goto end;
- }
- LOG_INF("No GC Done marker found: restarting gc");
- rc = nvs_flash_erase_sector(fs, fs->ate_wra);
- if (rc) {
- goto end;
- }
- fs->ate_wra &= ADDR_SECT_MASK;
- fs->ate_wra += (fs->sector_size - 2 * ate_size);
- fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK);
- rc = nvs_gc(fs);
- goto end;
- }
-
- while (fs->ate_wra > fs->data_wra) {
- empty_len = fs->ate_wra - fs->data_wra;
- rc = nvs_flash_cmp_const(fs, fs->data_wra, erase_value,
- empty_len);
- if (rc < 0) {
- goto end;
- }
- if (!rc) {
- break;
- }
- fs->data_wra += fs->flash_parameters->write_block_size;
- }
-
- if (((fs->ate_wra + 2 * ate_size) == fs->sector_size) &&
- (fs->data_wra != (fs->ate_wra & ADDR_SECT_MASK))) {
- rc = nvs_flash_erase_sector(fs, fs->ate_wra);
- if (rc) {
- goto end;
- }
- fs->data_wra = fs->ate_wra & ADDR_SECT_MASK;
- }
- end:
-
- if ((!rc) && ((fs->ate_wra & ADDR_OFFS_MASK) ==
- (fs->sector_size - 2 * ate_size))) {
- rc = nvs_add_gc_done_ate(fs);
- }
- k_mutex_unlock(&fs->nvs_lock);
- return rc;
- }
- int nvs_clear(struct nvs_fs *fs)
- {
- int rc;
- uint32_t addr;
- if (!fs->ready) {
- LOG_ERR("NVS not initialized");
- return -EACCES;
- }
- for (uint16_t i = 0; i < fs->sector_count; i++) {
- addr = i << ADDR_SECT_SHIFT;
- rc = nvs_flash_erase_sector(fs, addr);
- if (rc) {
- return rc;
- }
- }
-
- fs->ready = false;
- return 0;
- }
- int nvs_init(struct nvs_fs *fs, const char *dev_name)
- {
- int rc;
- struct flash_pages_info info;
- size_t write_block_size;
- k_mutex_init(&fs->nvs_lock);
- fs->flash_device = device_get_binding(dev_name);
- if (!fs->flash_device) {
- LOG_ERR("No valid flash device found");
- return -ENXIO;
- }
- fs->flash_parameters = flash_get_parameters(fs->flash_device);
- if (fs->flash_parameters == NULL) {
- LOG_ERR("Could not obtain flash parameters");
- return -EINVAL;
- }
- write_block_size = flash_get_write_block_size(fs->flash_device);
-
- if (write_block_size > NVS_BLOCK_SIZE || write_block_size == 0) {
- LOG_ERR("Unsupported write block size");
- return -EINVAL;
- }
-
- rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info);
- if (rc) {
- LOG_ERR("Unable to get page info");
- return -EINVAL;
- }
- if (!fs->sector_size || fs->sector_size % info.size) {
- LOG_ERR("Invalid sector size");
- return -EINVAL;
- }
-
- if (fs->sector_count < 2) {
- LOG_ERR("Configuration error - sector count");
- return -EINVAL;
- }
- rc = nvs_startup(fs);
- if (rc) {
- return rc;
- }
-
- fs->ready = true;
- LOG_INF("%d Sectors of %d bytes", fs->sector_count, fs->sector_size);
- LOG_INF("alloc wra: %d, %x",
- (fs->ate_wra >> ADDR_SECT_SHIFT),
- (fs->ate_wra & ADDR_OFFS_MASK));
- LOG_INF("data wra: %d, %x",
- (fs->data_wra >> ADDR_SECT_SHIFT),
- (fs->data_wra & ADDR_OFFS_MASK));
- return 0;
- }
- ssize_t nvs_write(struct nvs_fs *fs, uint16_t id, const void *data, size_t len)
- {
- int rc, gc_count;
- size_t ate_size, data_size;
- struct nvs_ate wlk_ate;
- uint32_t wlk_addr, rd_addr;
- uint16_t required_space = 0U;
- bool prev_found = false;
- if (!fs->ready) {
- LOG_ERR("NVS not initialized");
- return -EACCES;
- }
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- data_size = nvs_al_size(fs, len);
-
- if ((len > (fs->sector_size - 4 * ate_size)) ||
- ((len > 0) && (data == NULL))) {
- return -EINVAL;
- }
-
- wlk_addr = fs->ate_wra;
- rd_addr = wlk_addr;
- while (1) {
- rd_addr = wlk_addr;
- rc = nvs_prev_ate(fs, &wlk_addr, &wlk_ate);
- if (rc) {
- return rc;
- }
- if ((wlk_ate.id == id) && (nvs_ate_valid(fs, &wlk_ate))) {
- prev_found = true;
- break;
- }
- if (wlk_addr == fs->ate_wra) {
- break;
- }
- }
- if (prev_found) {
-
- rd_addr &= ADDR_SECT_MASK;
- rd_addr += wlk_ate.offset;
- if (len == 0) {
-
- if (wlk_ate.len == 0U) {
-
- return 0;
- }
- } else if (len == wlk_ate.len) {
-
-
- rc = nvs_flash_block_cmp(fs, rd_addr, data, len);
- if (rc <= 0) {
- return rc;
- }
- }
- } else {
-
- if (len == 0) {
- return 0;
- }
- }
-
- if (data_size) {
-
- required_space = data_size + ate_size;
- }
- k_mutex_lock(&fs->nvs_lock, K_FOREVER);
- gc_count = 0;
- while (1) {
- if (gc_count == fs->sector_count) {
-
- rc = -ENOSPC;
- goto end;
- }
- if (fs->ate_wra >= (fs->data_wra + required_space)) {
- rc = nvs_flash_wrt_entry(fs, id, data, len);
- if (rc) {
- goto end;
- }
- break;
- }
- rc = nvs_sector_close(fs);
- if (rc) {
- goto end;
- }
- rc = nvs_gc(fs);
- if (rc) {
- goto end;
- }
- gc_count++;
- }
- rc = len;
- end:
- k_mutex_unlock(&fs->nvs_lock);
- return rc;
- }
- int nvs_delete(struct nvs_fs *fs, uint16_t id)
- {
- return nvs_write(fs, id, NULL, 0);
- }
- ssize_t nvs_read_hist(struct nvs_fs *fs, uint16_t id, void *data, size_t len,
- uint16_t cnt)
- {
- int rc;
- uint32_t wlk_addr, rd_addr;
- uint16_t cnt_his;
- struct nvs_ate wlk_ate;
- size_t ate_size;
- if (!fs->ready) {
- LOG_ERR("NVS not initialized");
- return -EACCES;
- }
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- if (len > (fs->sector_size - 2 * ate_size)) {
- return -EINVAL;
- }
- cnt_his = 0U;
- wlk_addr = fs->ate_wra;
- rd_addr = wlk_addr;
- while (cnt_his <= cnt) {
- rd_addr = wlk_addr;
- rc = nvs_prev_ate(fs, &wlk_addr, &wlk_ate);
- if (rc) {
- goto err;
- }
- if ((wlk_ate.id == id) && (nvs_ate_valid(fs, &wlk_ate))) {
- cnt_his++;
- }
- if (wlk_addr == fs->ate_wra) {
- break;
- }
- }
- if (((wlk_addr == fs->ate_wra) && (wlk_ate.id != id)) ||
- (wlk_ate.len == 0U) || (cnt_his < cnt)) {
- return -ENOENT;
- }
- rd_addr &= ADDR_SECT_MASK;
- rd_addr += wlk_ate.offset;
- rc = nvs_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len));
- if (rc) {
- goto err;
- }
- return wlk_ate.len;
- err:
- return rc;
- }
- ssize_t nvs_read(struct nvs_fs *fs, uint16_t id, void *data, size_t len)
- {
- int rc;
- rc = nvs_read_hist(fs, id, data, len, 0);
- return rc;
- }
- ssize_t nvs_calc_free_space(struct nvs_fs *fs)
- {
- int rc;
- struct nvs_ate step_ate, wlk_ate;
- uint32_t step_addr, wlk_addr;
- size_t ate_size, free_space;
- if (!fs->ready) {
- LOG_ERR("NVS not initialized");
- return -EACCES;
- }
- ate_size = nvs_al_size(fs, sizeof(struct nvs_ate));
- free_space = 0;
- for (uint16_t i = 1; i < fs->sector_count; i++) {
- free_space += (fs->sector_size - ate_size);
- }
- step_addr = fs->ate_wra;
- while (1) {
- rc = nvs_prev_ate(fs, &step_addr, &step_ate);
- if (rc) {
- return rc;
- }
- wlk_addr = fs->ate_wra;
- while (1) {
- rc = nvs_prev_ate(fs, &wlk_addr, &wlk_ate);
- if (rc) {
- return rc;
- }
- if ((wlk_ate.id == step_ate.id) ||
- (wlk_addr == fs->ate_wra)) {
- break;
- }
- }
- if ((wlk_addr == step_addr) && step_ate.len &&
- (nvs_ate_valid(fs, &step_ate))) {
-
- free_space -= nvs_al_size(fs, step_ate.len);
- free_space -= ate_size;
- }
- if (step_addr == fs->ate_wra) {
- break;
- }
- }
- return free_space;
- }
|