Skip to content

Commit

Permalink
pbio/sys/storage: Delete user data on different builds.
Browse files Browse the repository at this point in the history
We were already deleting it across firmware releases, but this makes it delete data between different
local builds, which is helpful for development and testing.
  • Loading branch information
laurensvalk committed Sep 17, 2024
1 parent 9399ae0 commit ed10485
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
7 changes: 7 additions & 0 deletions bricks/_common/micropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <pybricks/common.h>
#include <pybricks/util_mp/pb_obj_helper.h>

#include "genhdr/mpversion.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/interrupt_char.h"
Expand Down Expand Up @@ -326,6 +327,12 @@ pbio_error_t pbsys_main_program_validate(pbsys_main_program_t *program) {
return PBIO_SUCCESS;
}

const char *pbsys_main_get_application_version_hash(void) {
// This is (somewhat confusingly) passed in as the MICROPY_GIT_HASH.
// REVISIT: Make PYBRICKS_GIT_HASH available in a pbio header via a build step.
return MICROPY_GIT_HASH;
}

// Runs MicroPython with the given program data.
void pbsys_main_run_program(pbsys_main_program_t *program) {

Expand Down
12 changes: 12 additions & 0 deletions lib/pbio/include/pbsys/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ void pbsys_main_stop_program(bool force_stop);
*/
bool pbsys_main_stdin_event(uint8_t c);

/**
* Gets the 9-symbol git hash of pybricks.
*
* @returns Git hash string.
*/
const char *pbsys_main_get_application_version_hash(void);

#else // PBSYS_CONFIG_MAIN

static inline pbio_error_t pbsys_main_program_request_start(pbio_pybricks_user_program_id_t id) {
Expand All @@ -110,6 +117,11 @@ static inline bool pbsys_main_stdin_event(uint8_t c) {
return false;
}

static inline const char *pbsys_main_get_application_version_hash(void) {
return NULL;
}


#endif // PBSYS_CONFIG_MAIN


Expand Down
16 changes: 9 additions & 7 deletions lib/pbio/sys/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ typedef struct {
volatile uint32_t checksum_complement;
#endif
/**
* Firmware version used to create the stored data. See pbio/version.
* Human-readable when printed as hex. If this value does not match
* the version of the running firmware, user data will be reset to 0.
* First 8 symbols of the git hash of the firmware version used to create
* this data map. If this does not match the version of the running
* firmware, user data will be reset to 0.
*/
uint32_t stored_firmware_version;
char stored_firmware_hash[8];
/**
* End-user read-write accessible data. Everything after this is also
* user-readable but not writable.
Expand Down Expand Up @@ -437,15 +437,17 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
// Read the available data into RAM.
PROCESS_PT_SPAWN(&pt, pbdrv_block_device_read(&pt, 0, (uint8_t *)map, map->saved_data_size, &err));

// Test that storage matches current firmware version.
if (err != PBIO_SUCCESS || map->stored_firmware_version != PBIO_HEXVERSION) {
bool is_bad_version = strncmp(map->stored_firmware_hash, pbsys_main_get_application_version_hash(), sizeof(map->stored_firmware_hash));

// Test that storage successfully loaded and matches current firmware.
if (err != PBIO_SUCCESS || is_bad_version) {
// Reset storage except for program data. It is sufficient to set its
// size to 0, which is what happens here since it is in the map.
memset(map, 0, sizeof(pbsys_storage_data_map_t));
pbsys_storage_settings_set_defaults(&map->settings);

// Set firmware version used to create current storage map.
map->stored_firmware_version = PBIO_HEXVERSION;
strncpy(map->stored_firmware_hash, pbsys_main_get_application_version_hash(), sizeof(map->stored_firmware_hash));

// Ensure new firmware version and default settings are written.
pbsys_storage_request_write();
Expand Down

0 comments on commit ed10485

Please sign in to comment.