From d38996a021eb8d608b914e70fa57fb6afe9d6261 Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Mon, 12 Apr 2021 19:04:46 +0200 Subject: [PATCH] Implement ens_fs and move ens stuff to ens directory --- for_future_use.txt | 219 ++++++++++++++++++++++++++++++++++++++++ src/contacts.c | 2 +- src/ens/ens_fs.c | 61 +++++++++++ src/ens/ens_fs.h | 88 ++++++++++++++++ src/{ => ens}/records.c | 0 src/{ => ens}/records.h | 0 src/{ => ens}/storage.c | 6 +- src/{ => ens}/storage.h | 2 +- src/main.c | 14 +-- 9 files changed, 380 insertions(+), 12 deletions(-) create mode 100644 for_future_use.txt create mode 100644 src/ens/ens_fs.c create mode 100644 src/ens/ens_fs.h rename src/{ => ens}/records.c (100%) rename src/{ => ens}/records.h (100%) rename src/{ => ens}/storage.c (98%) rename src/{ => ens}/storage.h (96%) diff --git a/for_future_use.txt b/for_future_use.txt new file mode 100644 index 0000000..45f0be4 --- /dev/null +++ b/for_future_use.txt @@ -0,0 +1,219 @@ + + // ens_fs_t fs; + // int rc = ens_fs_init(&fs, FLASH_AREA_ID(storage), sizeof(uint32_t)); + // if(rc) { + // printk("FS init err %d\n", rc); + // } + + // printk("Size: %d\n", fs.area->fa_size); + + // rc = ens_fs_page_erase(&fs, 0, 1); + // if(rc) { + // printk("Erase err %d\n", rc); + // } + + // uint32_t test; + // rc = ens_fs_read(&fs, 0, &test); + // if(rc) { + // printk("1 read error %d\n", rc); + // } + // printk("test: %ul\n", test); + + // test = 6; + // rc = ens_fs_write(&fs, 0, &test); + // if(rc) { + // printk("1 write error %d\n", rc); + // } + + // rc = ens_fs_read(&fs, 0, &test); + // if(rc) { + // printk("2 read error %d\n", rc); + // } + // printk("test: %ul\n", test); + + // test = 5; + // rc = ens_fs_write(&fs, 0, &test); + // if(rc) { + // printk("2 write error %d\n", rc); + // } + + // rc = ens_fs_read(&fs, 0, &test); + // if(rc) { + // printk("3 read error %d\n", rc); + // } + // printk("test: %ul\n", test); + + + // printf("\n" FLASH_NAME " SPI flash testing\n"); + // printf("==========================\n"); + + + // const struct device* flash_dev = device_get_binding(FLASH_DEVICE); + + // if (!flash_dev) { + // printf("SPI flash driver %s was not found!\n", FLASH_DEVICE); + // return; + // } + + // /* Write protection needs to be disabled before each write or + // * erase, since the flash component turns on write protection + // * automatically after completion of write and erase + // * operations. + // */ + // // printf("\nTest 1: Flash erase\n"); + // // flash_write_protection_set(flash_dev, false); + + // // rc = flash_erase(flash_dev, FLASH_TEST_REGION_OFFSET, FLASH_SECTOR_SIZE); + // // if (rc != 0) { + // // printf("Flash erase failed! %d\n", rc); + // // } else { + // // printf("Flash erase succeeded!\n"); + // // } + + // printf("\nTest 2: Flash write\n"); + + // printk("pages: %d\n", flash_get_page_count(flash_dev)); + // uint32_t offset = FLASH_AREA_OFFSET(storage); + // struct flash_pages_info info; + // flash_get_page_info_by_offs(flash_dev, offset, &info); + + // printk("page_size: %d\n", info.size); + + // flash_write_protection_set(flash_dev, false); + // if(flash_erase(flash_dev, offset, info.size)) { + // printk("erase err\n"); + // } + + // uint8_t test; + // flash_read(flash_dev, offset, &test, sizeof(test)); + // printk("Test: %u\n", test); + + // test = 6; + // flash_write_protection_set(flash_dev, false); + // flash_write(flash_dev, offset, &test, sizeof(test)); + // flash_read(flash_dev, offset, &test, sizeof(test)); + // printk("Test: %u\n", test); + + // test = 5; + // flash_write_protection_set(flash_dev, false); + // flash_write(flash_dev, offset, &test, sizeof(test)); + // flash_read(flash_dev, offset, &test, sizeof(test)); + // printk("Test: %u\n", test); + + // uint32_t start_time = k_cycle_get_32(); + // for(int i = 0; i < 100; i++) { + // contact_t contact; + // contact.cnt = i; + // flash_write_protection_set(flash_dev, false); + // if(flash_write(flash_dev, offset + i * 32, &contact, sizeof(contact))) { + // printk("write err\n"); + // } + // // if(flash_read(flash_dev, offset + i * 32, &contact, sizeof(contact))) { + // // printk("read err\n"); + // // } + // // printk("round: %d, contact: %d, len: %d\n", i, contact.cnt, sizeof(contact)); + // } + + // uint64_t nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time); + // printk("adding 100 contacts %llu ms\n", nanoseconds_spent); + // printk("average: %llu\n", nanoseconds_spent / 100); + + // start_time = k_cycle_get_32(); + // for(int i = 0; i < 100; i++) { + // contact_t contact; + // if(flash_read(flash_dev, offset + i * 32, &contact, sizeof(contact))) { + // printk("read err\n"); + // } + // if(contact.cnt != i) { + // printk("Expected does not match!\n"); + // } + // // printk("round: %d, contact: %d, len: %d\n", i, contact.cnt, sizeof(contact)); + // } + + // nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time); + // printk("loading 100 contacts %llu ms\n", nanoseconds_spent); + // printk("average: %llu\n", nanoseconds_spent / 100); + + + // for(int i = 0; i < 100; i++) { + // memset(&expected, 0, len); + // expected.cnt = i; + // flash_write_protection_set(flash_dev, false); + // rc = flash_erase(flash_dev, FLASH_TEST_REGION_OFFSET, len); + // if (rc != 0) { + // printf("Flash erase failed! %d\n", rc); + // } else { + // printf("Flash erase succeeded!\n"); + // } + // flash_write_protection_set(flash_dev, false); + // rc = flash_write(flash_dev, FLASH_TEST_REGION_OFFSET, &expected, len); + // if (rc != 0) { + // printf("Flash write failed! %d\n", rc); + // return; + // } + + // memset(&buf, 0, len); + // rc = flash_read(flash_dev, FLASH_TEST_REGION_OFFSET, &buf, len); + // if (rc != 0) { + // printf("Flash read failed! %d\n", rc); + // return; + // } + + // if (memcmp(&expected, &buf, len) == 0) { + // printf("Data read matches data written. Good!!\n"); + // } else { + // // const uint8_t* wp = &expected; + // // const uint8_t* rp = &buf; + // // const uint8_t* rpe = rp + len; + + // printf("Data read does not match data written!!\n"); + // // while (rp < rpe) { + // // printf("%08x wrote %02x read %02x %s\n", (uint32_t)(FLASH_TEST_REGION_OFFSET + (rp - buf)), *wp, *rp, + // // (*rp == *wp) ? "match" : "MISMATCH"); + // // ++rp; + // // ++wp; + // // } + // } + // } + + // const struct flash_area *area; + // err = flash_area_open(FLASH_AREA_ID(storage), &area); + // if(err) { + // printk("Error during flash_area_open (err %d)\n", err); + // } + // const struct device* dev = device_get_binding(area->fa_dev_name); + + // printk("write block size: %d\n", flash_get_write_block_size(dev)); + + // uint32_t start_time = k_cycle_get_32(); + // uint32_t test; + // for(int i = 0; i < 100; i++) { + // test = i; + // flash_write_protection_set(dev, false); + // int rc = flash_write(dev, area->fa_off + i * sizeof(test), &test, sizeof(test)); + // if(rc) { + // printk("Run %d, err %d\n", i, rc); + // } + // // contact.cnt = 0; + // flash_write_protection_set(dev, false); + // rc = flash_read(dev, area->fa_off + i * sizeof(test), &test, sizeof(test)); + // if(rc) { + // printk("Run %d, err %d\n", i, rc); + // } + // printk("Round: %d, test: %d, size: %d\n", i, test, sizeof(test)); + // } + // uint64_t nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time); + // printk("adding 100 contacts %llu ms\n", nanoseconds_spent); + // printk("average: %llu\n", nanoseconds_spent / 100); + + // start_time = k_cycle_get_32(); + // for(int i = 0; i < 100; i++) { + // contact_t contact; + // int rc = flash_read(dev, i * 32, &contact, sizeof(contact)); + // if(rc) { + // printk("Run %d, err %d\n", i, rc); + // } + // } + // nanoseconds_spent = k_cyc_to_ms_floor64(k_cycle_get_32() - start_time); + // printk("loading 100 contacts %llu ms\n", nanoseconds_spent); + // printk("average: %llu\n", nanoseconds_spent / 100); diff --git a/src/contacts.c b/src/contacts.c index 0145f46..7228bef 100644 --- a/src/contacts.c +++ b/src/contacts.c @@ -23,7 +23,7 @@ #include "contacts.h" #include "exposure-notification.h" #include "covid.h" -#include "storage.h" +#include "ens/storage.h" static contact_t contacts[MAX_CONTACTS]; static uint32_t contact_count = 0; diff --git a/src/ens/ens_fs.c b/src/ens/ens_fs.c new file mode 100644 index 0000000..ba9b943 --- /dev/null +++ b/src/ens/ens_fs.c @@ -0,0 +1,61 @@ +#include "ens_fs.h" +#include +#include + +int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size) { + int rc = flash_area_open(id, &fs->area); + if (rc) { + return rc; + } + + const struct device* dev = flash_area_get_device(fs->area); + + fs->sector_count = flash_get_page_count(dev); + struct flash_pages_info info; + rc = flash_get_page_info_by_offs(dev, fs->area->fa_off, &info); + fs->sector_size = info.size; + fs->entry_size = entry_size; + return 0; +} + +int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist) { + + uint64_t offset = id * fs->entry_size; + + int rc = flash_area_read(fs->area, offset, dist, fs->entry_size); + if(rc) { + goto end; + } + + // TODO lome: Read deleted flag and CRC +end: + + return rc; +} + +int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data) { + + uint64_t offset = id * fs->entry_size; + + // TODO lome: set CRC + int rc = flash_area_write(fs->area, offset, data, fs->entry_size); + if(rc) { + goto end; + } + +end: + return rc; +} + +int ens_fs_delete(ens_fs_t* fs, uint64_t id) { + // TODO lome: Overwrite deleted flag. + return 0; +} + +int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count) { + uint64_t start = (offset - offset % fs->sector_size) * fs->entry_size; + + int rc = flash_area_erase(fs->area, start, fs->sector_size * sector_count); + + return rc; +} diff --git a/src/ens/ens_fs.h b/src/ens/ens_fs.h new file mode 100644 index 0000000..5788295 --- /dev/null +++ b/src/ens/ens_fs.h @@ -0,0 +1,88 @@ +#ifndef FCB_H +#define FCB_H + +#include +#include +#include + +#include "fs/fs.h" +#include "stdint.h" + +typedef struct ens_fs { + /** + * Flash area for this file system. + */ + const struct flash_area* area; + /** + * Size of each individual entry. + */ + size_t entry_size; + /** + * Size of each sector of this fs. + */ + uint16_t sector_size; + /** + * Amount of sectors in this fs. + */ + uint16_t sector_count; + /** + * Lock for this fs. + */ + // struct k_mutex* lock; +} ens_fs_t; + +/** + * Initialize the file system. + * + * @param fs file system + * @param id id of the partition + * @param size of each entry in the file-system. Has to power of 2 + * + * @return 0 on success, -errno otherwise + */ +int ens_fs_init(ens_fs_t* fs, uint8_t id, uint64_t entry_size); + +/** + * Read an entry from this file system. + * + * @param fs file system + * @param id id of entry + * @param data destination address of data to be read + * + * @return 0 on success, -errno otherwise + */ +int ens_fs_read(ens_fs_t* fs, uint64_t id, void* dist); + +/** + * Write data to the file system. + * + * @param fs file system + * @param id id of the entry + * @param data data to be written + * + * @return 0 on success, -errno otherwise + */ +int ens_fs_write(ens_fs_t* fs, uint64_t id, void* data); + +/** + * Delete an entry from the file system. + * + * @param fs file system + * @param id id of the entry to be deleted + * + * @return 0 on success, -errno otherwise + */ +int ens_fs_delete(ens_fs_t* fs, uint64_t id); + +/** + * Erase a given amount of flash sectors. Starting at offset (if offset is not at page start, it will be rounded down). + * + * @param fs file system + * @param offset offset to start erasing + * @param sector_count count of sectors to erase + * + * @return 0 on success, -errno otherwise + */ +int ens_fs_page_erase(ens_fs_t* fs, uint64_t offset, uint64_t sector_count); + +#endif \ No newline at end of file diff --git a/src/records.c b/src/ens/records.c similarity index 100% rename from src/records.c rename to src/ens/records.c diff --git a/src/records.h b/src/ens/records.h similarity index 100% rename from src/records.h rename to src/ens/records.h diff --git a/src/storage.c b/src/ens/storage.c similarity index 98% rename from src/storage.c rename to src/ens/storage.c index 58120e2..5a432c5 100644 --- a/src/storage.c +++ b/src/ens/storage.c @@ -6,7 +6,7 @@ #include #include -#include "sequencenumber.h" +#include "../sequencenumber.h" #include "storage.h" // Get external flash device @@ -21,7 +21,7 @@ #endif // Maybe use this as param for init function -#define SEC_COUNT 192U +#define SEC_COUNT 96U #define STORED_CONTACTS_INFO_ID 0 #define CONTACTS_OFFSET 1 @@ -90,7 +90,7 @@ int init_contact_storage(void) { // Error during retrieval of page information return rc; } - fs.sector_size = info.size * 4; + fs.sector_size = info.size * 8; fs.sector_count = SEC_COUNT; rc = nvs_init(&fs, FLASH_DEVICE); diff --git a/src/storage.h b/src/ens/storage.h similarity index 96% rename from src/storage.h rename to src/ens/storage.h index 17ca635..3efc518 100644 --- a/src/storage.h +++ b/src/ens/storage.h @@ -1,7 +1,7 @@ #ifndef CONTACT_STORAGE_H #define CONTACT_STORAGE_H -#include "contacts.h" // Requires contact_t in contacts.h! +#include "../contacts.h" // Requires contact_t in contacts.h! #include "sequencenumber.h" typedef uint16_t storage_id_t; diff --git a/src/main.c b/src/main.c index 67481c1..7393250 100644 --- a/src/main.c +++ b/src/main.c @@ -9,14 +9,13 @@ #include #include #include - #include "contacts.h" #include "covid.h" #include "covid_types.h" #include "exposure-notification.h" #include "gatt_service.h" #include "io.h" -#include "storage.h" +#include "ens/storage.h" void main(void) { int err = 0; @@ -30,11 +29,12 @@ void main(void) { return; } - err = init_contact_storage(); - if (err) { - printk("init storage failed (err %d)\n", err); - return; - } + // TODO lome: Cleanup storage + // err = init_contact_storage(); + // if (err) { + // printk("init storage failed (err %d)\n", err); + // return; + // } init_contacts(); err = init_io();