@@ -37,6 +37,14 @@ typedef struct
37
37
rg_stats_t statistics ;
38
38
} panic_trace_t ;
39
39
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
+
40
48
typedef struct
41
49
{
42
50
int32_t totalFrames , fullFrames , partFrames , ticks ;
@@ -78,6 +86,7 @@ static struct
78
86
79
87
// The trace will survive a software reset
80
88
static RTC_NOINIT_ATTR panic_trace_t panicTrace ;
89
+ static RTC_NOINIT_ATTR boot_config_t bootConfig ;
81
90
static RTC_NOINIT_ATTR time_t rtcValue ;
82
91
static bool panicTraceCleared = false;
83
92
static bool exitCalled = false;
@@ -87,9 +96,6 @@ static rg_stats_t statistics;
87
96
static rg_app_t app ;
88
97
static rg_task_t tasks [8 ];
89
98
90
- static const char * SETTING_BOOT_NAME = "BootName" ;
91
- static const char * SETTING_BOOT_ARGS = "BootArgs" ;
92
- static const char * SETTING_BOOT_FLAGS = "BootFlags" ;
93
99
static const char * SETTING_TIMEZONE = "Timezone" ;
94
100
static const char * SETTING_INDICATOR_MASK = "Indicators" ;
95
101
@@ -117,6 +123,35 @@ IRAM_ATTR void esp_panic_putchar_hook(char c)
117
123
logbuf_putc (& panicTrace , c );
118
124
}
119
125
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
+
120
155
static void update_memory_statistics (void )
121
156
{
122
157
#ifdef ESP_PLATFORM
@@ -442,18 +477,28 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
442
477
memset (& panicTrace , 0 , sizeof (panicTrace ));
443
478
panicTraceCleared = true;
444
479
480
+ // Check if we have a valid boot config (selected emulator, rom file, save state, etc)
481
+ if (bootConfig .magicWord != RG_STRUCT_MAGIC || app .isColdBoot )
482
+ {
483
+ memset (& bootConfig , 0 , sizeof (bootConfig ));
484
+ void * data_ptr = (void * )& bootConfig ;
485
+ size_t data_len = sizeof (bootConfig );
486
+ rg_storage_read_file (RG_BASE_PATH_CACHE "/boot.bin" , & data_ptr , & data_len , RG_FILE_USER_BUFFER );
487
+ }
488
+ if (bootConfig .magicWord == RG_STRUCT_MAGIC ) // && strncmp(bootConfig.part, part, sizeof(bootConfig.part)) == 0
489
+ {
490
+ app .configNs = strdup (bootConfig .name );
491
+ app .bootArgs = strdup (bootConfig .args );
492
+ app .bootFlags = bootConfig .flags ;
493
+ }
494
+ app .saveSlot = (app .bootFlags & RG_BOOT_SLOT_MASK ) >> 4 ;
495
+ app .romPath = app .bootArgs ;
496
+
445
497
update_memory_statistics ();
446
498
app .lowMemoryMode = statistics .totalMemoryExt == 0 ;
447
499
448
500
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 );
452
- app .saveSlot = (app .bootFlags & RG_BOOT_SLOT_MASK ) >> 4 ;
453
- app .romPath = app .bootArgs ;
454
- // app.isLauncher = strcmp(app.name, RG_APP_LAUNCHER) == 0; // Might be overriden after init
455
501
app .indicatorsMask = rg_settings_get_number (NS_GLOBAL , SETTING_INDICATOR_MASK , app .indicatorsMask );
456
-
457
502
rg_display_init ();
458
503
rg_gui_init ();
459
504
rg_gui_draw_hourglass ();
@@ -476,13 +521,7 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
476
521
rg_gui_alert ("External memory not detected" , "Boot will continue but it will surely crash..." );
477
522
478
523
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 );
486
525
487
526
rg_task_create ("rg_sysmon" , & system_monitor_task , NULL , 3 * 1024 , RG_TASK_PRIORITY_5 , -1 );
488
527
app .initialized = true;
@@ -868,36 +907,10 @@ void rg_system_switch_app(const char *partition, const char *name, const char *a
868
907
{
869
908
RG_LOGI ("Switching to app %s (%s)" , partition ?: "-" , name ?: "-" );
870
909
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 ))
889
911
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!" );
901
914
}
902
915
903
916
bool rg_system_have_app (const char * app )
@@ -1214,8 +1227,7 @@ static void emu_update_save_slot(uint8_t slot)
1214
1227
app .bootFlags &= ~RG_BOOT_SLOT_MASK ;
1215
1228
app .bootFlags |= app .saveSlot << 4 ;
1216
1229
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 );
1219
1231
}
1220
1232
1221
1233
rg_storage_commit ();
0 commit comments