@@ -37,6 +37,14 @@ typedef struct
3737 rg_stats_t statistics ;
3838} panic_trace_t ;
3939
40+ typedef struct
41+ {
42+ uint32_t magicWord ;
43+ char name [32 ];
44+ char args [256 ];
45+ uint32_t flags ;
46+ } boot_config_t ;
47+
4048typedef struct
4149{
4250 int32_t totalFrames , fullFrames , partFrames , ticks ;
@@ -78,6 +86,7 @@ static struct
7886
7987// The trace will survive a software reset
8088static RTC_NOINIT_ATTR panic_trace_t panicTrace ;
89+ static RTC_NOINIT_ATTR boot_config_t bootConfig ;
8190static RTC_NOINIT_ATTR time_t rtcValue ;
8291static bool panicTraceCleared = false;
8392static bool exitCalled = false;
@@ -87,9 +96,6 @@ static rg_stats_t statistics;
8796static rg_app_t app ;
8897static rg_task_t tasks [8 ];
8998
90- static const char * SETTING_BOOT_NAME = "BootName" ;
91- static const char * SETTING_BOOT_ARGS = "BootArgs" ;
92- static const char * SETTING_BOOT_FLAGS = "BootFlags" ;
9399static const char * SETTING_TIMEZONE = "Timezone" ;
94100static const char * SETTING_INDICATOR_MASK = "Indicators" ;
95101
@@ -117,6 +123,35 @@ IRAM_ATTR void esp_panic_putchar_hook(char c)
117123 logbuf_putc (& panicTrace , c );
118124}
119125
126+ static bool update_boot_config (const char * part , const char * name , const char * args , uint32_t flags )
127+ {
128+ memset (& bootConfig , 0 , sizeof (bootConfig ));
129+ bootConfig .magicWord = RG_STRUCT_MAGIC ;
130+ strncpy (bootConfig .name , name ?: "" , sizeof (bootConfig .name ));
131+ strncpy (bootConfig .args , args ?: "" , sizeof (bootConfig .args ));
132+ bootConfig .flags = flags ;
133+ rg_storage_write_file (RG_BASE_PATH_CACHE "/boot.bin" , & bootConfig , sizeof (bootConfig ), 0 );
134+
135+ #if defined(ESP_PLATFORM )
136+ // Check if the OTA settings are already correct, and if so do not call esp_ota_set_boot_partition
137+ // This is simply to avoid an unecessary flash write...
138+ const esp_partition_t * current = esp_ota_get_boot_partition ();
139+ if (current && part && strncmp (current -> label , part , 16 ) == 0 )
140+ {
141+ RG_LOGI ("Boot partition already set to desired app!" );
142+ return true;
143+ }
144+ esp_err_t err = esp_ota_set_boot_partition (esp_partition_find_first (
145+ ESP_PARTITION_TYPE_APP , ESP_PARTITION_SUBTYPE_ANY , part ));
146+ if (err != ESP_OK )
147+ {
148+ RG_LOGE ("esp_ota_set_boot_partition returned 0x%02X!" , err );
149+ return false;
150+ }
151+ #endif
152+ return true;
153+ }
154+
120155static void update_memory_statistics (void )
121156{
122157#ifdef ESP_PLATFORM
@@ -445,15 +480,25 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
445480 update_memory_statistics ();
446481 app .lowMemoryMode = statistics .totalMemoryExt == 0 ;
447482
448- rg_settings_init ();
449- app .configNs = rg_settings_get_string (NS_BOOT , SETTING_BOOT_NAME , app .name );
450- app .bootArgs = rg_settings_get_string (NS_BOOT , SETTING_BOOT_ARGS , "" );
451- app .bootFlags = rg_settings_get_number (NS_BOOT , SETTING_BOOT_FLAGS , 0 );
483+ // Check if we have a valid boot config (selected emulator, rom file, save state, etc)
484+ if (bootConfig .magicWord != RG_STRUCT_MAGIC || app .isColdBoot )
485+ {
486+ memset (& bootConfig , 0 , sizeof (bootConfig ));
487+ void * data_ptr = (void * )& bootConfig ;
488+ size_t data_len = sizeof (bootConfig );
489+ rg_storage_read_file (RG_BASE_PATH_CACHE "/boot.bin" , & data_ptr , & data_len , RG_FILE_USER_BUFFER );
490+ }
491+ if (bootConfig .magicWord == RG_STRUCT_MAGIC ) // && strncmp(bootConfig.part, part, sizeof(bootConfig.part)) == 0
492+ {
493+ app .configNs = strdup (bootConfig .name );
494+ app .bootArgs = strdup (bootConfig .args );
495+ app .bootFlags = bootConfig .flags ;
496+ }
452497 app .saveSlot = (app .bootFlags & RG_BOOT_SLOT_MASK ) >> 4 ;
453498 app .romPath = app .bootArgs ;
454- // app.isLauncher = strcmp(app.name, RG_APP_LAUNCHER) == 0; // Might be overriden after init
455- app .indicatorsMask = rg_settings_get_number (NS_GLOBAL , SETTING_INDICATOR_MASK , app .indicatorsMask );
456499
500+ rg_settings_init ();
501+ app .indicatorsMask = rg_settings_get_number (NS_GLOBAL , SETTING_INDICATOR_MASK , app .indicatorsMask );
457502 rg_display_init ();
458503 rg_gui_init ();
459504 rg_gui_draw_hourglass ();
@@ -476,13 +521,7 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
476521 rg_gui_alert ("External memory not detected" , "Boot will continue but it will surely crash..." );
477522
478523 if (app .bootFlags & RG_BOOT_ONCE )
479- {
480- rg_storage_delete (RG_BASE_PATH_CONFIG "/boot.json" );
481- #ifdef ESP_PLATFORM
482- esp_ota_set_boot_partition (esp_partition_find_first (
483- ESP_PARTITION_TYPE_APP , ESP_PARTITION_SUBTYPE_ANY , RG_APP_LAUNCHER ));
484- #endif
485- }
524+ update_boot_config (RG_APP_LAUNCHER , NULL , NULL , 0 );
486525
487526 rg_task_create ("rg_sysmon" , & system_monitor_task , NULL , 3 * 1024 , RG_TASK_PRIORITY_5 , -1 );
488527 app .initialized = true;
@@ -868,36 +907,10 @@ void rg_system_switch_app(const char *partition, const char *name, const char *a
868907{
869908 RG_LOGI ("Switching to app %s (%s)" , partition ?: "-" , name ?: "-" );
870909
871- if (app .initialized )
872- {
873- rg_settings_set_string (NS_BOOT , SETTING_BOOT_NAME , name );
874- rg_settings_set_string (NS_BOOT , SETTING_BOOT_ARGS , args );
875- rg_settings_set_number (NS_BOOT , SETTING_BOOT_FLAGS , flags );
876- rg_settings_commit ();
877- }
878- else
879- {
880- rg_storage_delete (RG_BASE_PATH_CONFIG "/boot.json" );
881- }
882- #if defined(ESP_PLATFORM )
883- // Check if the OTA settings are already correct, and if so do not call esp_ota_set_boot_partition
884- // This is simply to avoid an unecessary flash write...
885- const esp_partition_t * current = esp_ota_get_boot_partition ();
886- if (current && partition && strncmp (current -> label , partition , 16 ) == 0 )
887- {
888- RG_LOGI ("Boot partition already set to desired app!" );
910+ if (update_boot_config (partition , name , args , flags ))
889911 rg_system_restart ();
890- }
891- esp_err_t err = esp_ota_set_boot_partition (esp_partition_find_first (
892- ESP_PARTITION_TYPE_APP , ESP_PARTITION_SUBTYPE_ANY , partition ));
893- if (err != ESP_OK )
894- {
895- RG_LOGE ("esp_ota_set_boot_partition returned 0x%02X!" , err );
896- RG_PANIC ("Unable to set boot app!" );
897- }
898- rg_system_restart ();
899- #endif
900- RG_PANIC ("Switch not implemented!" );
912+
913+ RG_PANIC ("Failed to switch app!" );
901914}
902915
903916bool rg_system_have_app (const char * app )
@@ -1214,8 +1227,7 @@ static void emu_update_save_slot(uint8_t slot)
12141227 app .bootFlags &= ~RG_BOOT_SLOT_MASK ;
12151228 app .bootFlags |= app .saveSlot << 4 ;
12161229 app .bootFlags |= RG_BOOT_RESUME ;
1217- rg_settings_set_number (NS_BOOT , SETTING_BOOT_FLAGS , app .bootFlags );
1218- rg_settings_commit ();
1230+ update_boot_config (NULL , app .configNs , app .bootArgs , app .bootFlags );
12191231 }
12201232
12211233 rg_storage_commit ();
0 commit comments