Skip to content

Commit d49dbb1

Browse files
committed
rg_system: Boot config in no longer using rg_settings
The main advantage is that we can now easily pass boot config via the RTC memory rather than the SD card.
1 parent 4768aff commit d49dbb1

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

components/retro-go/rg_settings.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ static cJSON *json_root(const char *name, bool set_dirty)
2121
name = rg_basename(rg_system_get_app()->romPath);
2222
else if (name == NS_WIFI)
2323
name = "wifi";
24-
else if (name == NS_BOOT)
25-
name = "boot";
2624

2725
cJSON *branch = cJSON_GetObjectItem(config_root, name);
2826
if (!branch)
@@ -80,7 +78,6 @@ void rg_settings_init(void)
8078
{
8179
config_root = cJSON_CreateObject();
8280
json_root(NS_GLOBAL, 0);
83-
json_root(NS_BOOT, 0);
8481
}
8582

8683
void rg_settings_commit(void)

components/retro-go/rg_settings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#define NS_APP ((char *)1)
99
#define NS_FILE ((char *)2)
1010
#define NS_WIFI ((char *)3)
11-
#define NS_BOOT ((char *)4)
1211

1312
void rg_settings_init(void);
1413
void rg_settings_commit(void);

components/retro-go/rg_system.c

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4048
typedef struct
4149
{
4250
int32_t totalFrames, fullFrames, partFrames, ticks;
@@ -78,6 +86,7 @@ static struct
7886

7987
// The trace will survive a software reset
8088
static RTC_NOINIT_ATTR panic_trace_t panicTrace;
89+
static RTC_NOINIT_ATTR boot_config_t bootConfig;
8190
static RTC_NOINIT_ATTR time_t rtcValue;
8291
static bool panicTraceCleared = false;
8392
static bool exitCalled = false;
@@ -87,9 +96,6 @@ static rg_stats_t statistics;
8796
static rg_app_t app;
8897
static 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";
9399
static const char *SETTING_TIMEZONE = "Timezone";
94100
static 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+
120155
static void update_memory_statistics(void)
121156
{
122157
#ifdef ESP_PLATFORM
@@ -442,18 +477,28 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
442477
memset(&panicTrace, 0, sizeof(panicTrace));
443478
panicTraceCleared = true;
444479

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+
445497
update_memory_statistics();
446498
app.lowMemoryMode = statistics.totalMemoryExt == 0;
447499

448500
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
455501
app.indicatorsMask = rg_settings_get_number(NS_GLOBAL, SETTING_INDICATOR_MASK, app.indicatorsMask);
456-
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

903916
bool 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

Comments
 (0)