Skip to content

Commit 83e372a

Browse files
committed
pbio/sys/storage: Fix user read area.
Adapt it so that it is correct even as we add new fields in between going forward.
1 parent 31c36c6 commit 83e372a

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

lib/pbio/include/pbsys/storage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ typedef struct _pbsys_storage_data_map_t {
4949
*/
5050
uint32_t stored_firmware_version;
5151
/**
52-
* End-user read-write accessible data.
52+
* End-user read-write accessible data. Everything after this is also
53+
* user-readable but not writable.
5354
*/
5455
uint8_t user_data[PBSYS_CONFIG_STORAGE_USER_DATA_SIZE];
5556
/**

lib/pbio/sys/storage.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, u
7272
}
7373

7474
/**
75-
* Gets pointer to user data or user program.
75+
* Gets pointer to user data, settings, or program.
7676
*
7777
* @param [in] offset Offset from the base address.
7878
* @param [in] data The data reference.
@@ -81,8 +81,9 @@ pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, u
8181
* Otherwise, ::PBIO_SUCCESS.
8282
*/
8383
pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32_t size) {
84-
// User is allowed to read beyond user storage to include program data.
85-
if (offset + size > sizeof(map->user_data) + sizeof(map->program_size) + map->program_size) {
84+
// User is allowed to read beyond user storage to include settings and
85+
// program data.
86+
if (offset + size > (map->program_data - map->user_data) + map->program_size) {
8687
return PBIO_ERROR_INVALID_ARG;
8788
}
8889
*data = map->user_data + offset;
@@ -112,7 +113,8 @@ static void pbsys_storage_update_checksum(void) {
112113
// Add checksum for each word in the written data and empty checked size.
113114
for (uint32_t offset = 0; offset < checksize; offset += sizeof(uint32_t)) {
114115
uint32_t *word = (uint32_t *)((uint8_t *)map + offset);
115-
// Assume that everything after written data is erased.
116+
// Assume that everything after written data is erased by the block
117+
// device driver prior to writing.
116118
checksum += offset < map->write_size ? *word : 0xFFFFFFFF;
117119
}
118120

0 commit comments

Comments
 (0)