From 722e71eb3170c72fea98bdf536d598514209e176 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 30 Mar 2017 21:40:36 +0200 Subject: [PATCH] modtrezorconfig: use uint16_t for data length --- micropython/extmod/modtrezorconfig/.gitignore | 2 ++ .../extmod/modtrezorconfig/modtrezorconfig.c | 3 +- micropython/extmod/modtrezorconfig/norcow.c | 30 +++++++++---------- micropython/extmod/modtrezorconfig/norcow.h | 4 +-- .../extmod/modtrezorconfig/norcow_test.c | 3 +- 5 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 micropython/extmod/modtrezorconfig/.gitignore diff --git a/micropython/extmod/modtrezorconfig/.gitignore b/micropython/extmod/modtrezorconfig/.gitignore new file mode 100644 index 000000000..bcac6781c --- /dev/null +++ b/micropython/extmod/modtrezorconfig/.gitignore @@ -0,0 +1,2 @@ +*.o +norcow_test diff --git a/micropython/extmod/modtrezorconfig/modtrezorconfig.c b/micropython/extmod/modtrezorconfig/modtrezorconfig.c index 6893ed070..db4285a9f 100644 --- a/micropython/extmod/modtrezorconfig/modtrezorconfig.c +++ b/micropython/extmod/modtrezorconfig/modtrezorconfig.c @@ -37,9 +37,8 @@ STATIC mp_obj_t mod_TrezorConfig_Config_make_new(const mp_obj_type_t *type, size STATIC mp_obj_t mod_TrezorConfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) { uint8_t a = mp_obj_get_int(app); uint8_t k = mp_obj_get_int(key); - uint16_t appkey = a << 8 | k; + uint16_t appkey = a << 8 | k, len; const void *val; - uint32_t len; bool r = norcow_get(appkey, &val, &len); if (!r || len == 0) { return mp_const_empty_bytes; diff --git a/micropython/extmod/modtrezorconfig/norcow.c b/micropython/extmod/modtrezorconfig/norcow.c index 8b62d8fda..2afe5399e 100644 --- a/micropython/extmod/modtrezorconfig/norcow.c +++ b/micropython/extmod/modtrezorconfig/norcow.c @@ -59,9 +59,9 @@ static bool norcow_erase(uint8_t sector) EraseInitStruct.TypeErase = TYPEERASE_SECTORS; EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V EraseInitStruct.NbSectors = 1; - uint32_t SectorError = 0; EraseInitStruct.Sector = NORCOW_START_SECTOR + sector; HAL_StatusTypeDef r; + uint32_t SectorError = 0; r = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError); HAL_FLASH_Lock(); return r == HAL_OK; @@ -93,7 +93,7 @@ static const void *norcow_ptr(uint8_t sector, uint32_t offset, uint32_t size) /* * Writes data to given sector, starting from offset */ -static bool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const uint8_t *data, uint32_t len) +static bool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const uint8_t *data, uint16_t len) { if (offset % 4) { // we write only at 4-byte boundary return false; @@ -108,7 +108,7 @@ static bool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const if ((*(uint32_t *)ptr & prefix) != prefix) { return false; } - for (uint32_t i = 0; i < len; i++) { + for (uint16_t i = 0; i < len; i++) { if ((ptr[sizeof(uint32_t) + i] & data[i]) != data[i]) { return false; } @@ -144,7 +144,7 @@ static inline void align4(uint32_t *pos) /* * Reads one item starting from offset */ -static bool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void **val, uint32_t *len, uint32_t *pos) +static bool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void **val, uint16_t *len, uint32_t *pos) { *pos = offset; @@ -171,7 +171,7 @@ static bool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void /* * Writes one item starting from offset */ -static bool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void *val, uint32_t len, uint32_t *pos) +static bool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void *val, uint16_t len, uint32_t *pos) { uint32_t prefix = (len << 16) | key; *pos = offset + sizeof(uint32_t) + len; @@ -182,15 +182,15 @@ static bool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void /* * Finds item in given sector */ -static bool find_item(uint8_t sector, uint16_t key, const void **val, uint32_t *len) +static bool find_item(uint8_t sector, uint16_t key, const void **val, uint16_t *len) { *val = 0; *len = 0; uint32_t offset = 0; for (;;) { - uint16_t k; + uint16_t k, l; const void *v; - uint32_t l, pos; + uint32_t pos; bool r = read_item(sector, offset, &k, &v, &l, &pos); if (!r) break; if (key == k) { @@ -209,9 +209,9 @@ static uint32_t find_free_offset(uint8_t sector) { uint32_t offset = 0; for (;;) { - uint16_t key; + uint16_t key, len; const void *val; - uint32_t len, pos; + uint32_t pos; bool r = read_item(sector, offset, &key, &val, &len, &pos); if (!r) break; offset = pos; @@ -230,16 +230,16 @@ static void compact() for (;;) { // read item - uint16_t k; + uint16_t k, l; const void *v; - uint32_t l, pos; + uint32_t pos; bool r = read_item(norcow_active_sector, offset, &k, &v, &l, &pos); if (!r) break; offset = pos; // check if not already saved const void *v2; - uint32_t l2; + uint16_t l2; r = find_item(norcow_next_sector, k, &v2, &l2); if (r) { continue; @@ -318,7 +318,7 @@ bool norcow_wipe(void) /* * Looks for the given key, returns status of the operation */ -bool norcow_get(uint16_t key, const void **val, uint32_t *len) +bool norcow_get(uint16_t key, const void **val, uint16_t *len) { return find_item(norcow_active_sector, key, val, len); } @@ -326,7 +326,7 @@ bool norcow_get(uint16_t key, const void **val, uint32_t *len) /* * Sets the given key, returns status of the operation */ -bool norcow_set(uint16_t key, const void *val, uint32_t len) +bool norcow_set(uint16_t key, const void *val, uint16_t len) { // check whether there is enough free space // and compact if full diff --git a/micropython/extmod/modtrezorconfig/norcow.h b/micropython/extmod/modtrezorconfig/norcow.h index 50f7d1c22..1502b2f40 100644 --- a/micropython/extmod/modtrezorconfig/norcow.h +++ b/micropython/extmod/modtrezorconfig/norcow.h @@ -24,11 +24,11 @@ bool norcow_wipe(void); /* * Looks for the given key, returns status of the operation */ -bool norcow_get(uint16_t key, const void **val, uint32_t *len); +bool norcow_get(uint16_t key, const void **val, uint16_t *len); /* * Sets the given key, returns status of the operation */ -bool norcow_set(uint16_t key, const void *val, uint32_t len); +bool norcow_set(uint16_t key, const void *val, uint16_t len); #endif diff --git a/micropython/extmod/modtrezorconfig/norcow_test.c b/micropython/extmod/modtrezorconfig/norcow_test.c index a64d83aad..468c69c3f 100644 --- a/micropython/extmod/modtrezorconfig/norcow_test.c +++ b/micropython/extmod/modtrezorconfig/norcow_test.c @@ -9,8 +9,7 @@ #define MAXVALLEN 1024 uint8_t val[MAXVALLEN], *v; -uint16_t key; -uint32_t vallen, vlen; +uint16_t key, vallen, vlen; int main() {