|
4 | 4 | #include <stdio.h> |
5 | 5 | #include <stdlib.h> |
6 | 6 | #include <string.h> |
| 7 | +#include <strings.h> |
7 | 8 |
|
8 | | -#include <esp_spi_flash.h> |
| 9 | +#include <esp_flash_partitions.h> |
9 | 10 | #include <esp_partition.h> |
| 11 | +#include <esp_spi_flash.h> |
10 | 12 |
|
11 | 13 | #ifdef RG_UPDATER_DOWNLOAD_LOCATION |
12 | 14 | #define DOWNLOAD_LOCATION RG_UPDATER_DOWNLOAD_LOCATION |
|
20 | 22 | #define CAN_UPDATE_PARTITION_TABLE 0 |
21 | 23 | #endif |
22 | 24 |
|
| 25 | +#define RETRO_GO_IMG_MAGIC 0x31304752 // "RG01" |
| 26 | +typedef struct |
| 27 | +{ |
| 28 | + uint32_t magic; |
| 29 | + uint32_t crc32; |
| 30 | + uint32_t timestamp; |
| 31 | + char target[32]; |
| 32 | + char version[32]; |
| 33 | + char reserved[180]; |
| 34 | +} img_info_t; |
| 35 | + |
| 36 | +#define CONFIRM(title, message...) \ |
| 37 | + { \ |
| 38 | + snprintf(message_buffer, sizeof(message_buffer), message); \ |
| 39 | + if (!rg_gui_confirm(_(title), message_buffer, false)) \ |
| 40 | + return false; \ |
| 41 | + rg_display_clear(C_BLACK); \ |
| 42 | + } |
| 43 | +#define ALERT(title, message...) \ |
| 44 | + { \ |
| 45 | + snprintf(message_buffer, sizeof(message_buffer), message); \ |
| 46 | + rg_gui_alert(_(title), message_buffer); \ |
| 47 | + rg_display_clear(C_BLACK); \ |
| 48 | + } |
| 49 | +#define ERROR(message...) \ |
| 50 | + { \ |
| 51 | + ALERT("Error", message); \ |
| 52 | + return false; \ |
| 53 | + } |
| 54 | +#define TRY(cond, error_message) \ |
| 55 | + if (!(cond)) \ |
| 56 | + ERROR(error_message); |
| 57 | + |
| 58 | +static rg_app_t *app; |
| 59 | +static esp_partition_info_t device_partition_table[ESP_PARTITION_TABLE_MAX_ENTRIES]; |
| 60 | +static char message_buffer[256]; |
| 61 | + |
| 62 | +static bool do_update(const char *filename) |
| 63 | +{ |
| 64 | + esp_partition_info_t partition_table[16]; // ESP_PARTITION_TABLE_MAX_ENTRIES |
| 65 | + int num_partitions = 0; |
| 66 | + img_info_t img_info; |
| 67 | + int filesize = 0; |
| 68 | + FILE *fp; |
| 69 | + |
| 70 | + RG_LOGI("Filename: %s", filename); |
| 71 | + rg_display_clear(C_BLACK); |
| 72 | + |
| 73 | + TRY(fp = fopen(filename, "rb"), "File open failed"); |
| 74 | + TRY(fseek(fp, 0, SEEK_END) == 0, "File seek failed"); |
| 75 | + TRY(fseek(fp, -sizeof(img_info_t), SEEK_CUR) == 0, "File seek failed"); |
| 76 | + filesize = ftell(fp); // Size without the footer |
| 77 | + TRY(fread(&img_info, sizeof(img_info), 1, fp), "File read failed"); |
| 78 | + |
| 79 | + img_info.target[sizeof(img_info.target) - 1] = 0; // Just in case |
| 80 | + img_info.version[sizeof(img_info.version) - 1] = 0; // Just in case |
| 81 | + |
| 82 | + if (img_info.magic != RETRO_GO_IMG_MAGIC) // Invalid image |
| 83 | + { |
| 84 | + CONFIRM("Warning", "File is not a valid Retro-Go image.\nContinue anyway?"); |
| 85 | + } |
| 86 | + else if (strcasecmp(img_info.target, RG_TARGET_NAME) != 0) // Wrong target |
| 87 | + { |
| 88 | + CONFIRM("Warning", |
| 89 | + "The file appears to be for a different device.\n" |
| 90 | + "Current device: %s\n" |
| 91 | + "Image device: %s\n\n" |
| 92 | + "Do you want to continue anyway?", |
| 93 | + RG_TARGET_NAME, |
| 94 | + img_info.target); |
| 95 | + } |
| 96 | + else // Valid image |
| 97 | + { |
| 98 | + CONFIRM("Version", "Current version: %s\nNew version: %s\n\nContinue?", app->version, img_info.version); |
| 99 | + } |
| 100 | + |
| 101 | + // TODO: Also support images that truncate the first 0x1000, just in case |
| 102 | + TRY(fseek(fp, ESP_PARTITION_TABLE_OFFSET, SEEK_SET) == 0, "File seek failed"); |
| 103 | + TRY(fread(&partition_table, sizeof(partition_table), 1, fp), "File read failed"); |
| 104 | + |
| 105 | + if (esp_partition_table_verify(partition_table, true, &num_partitions) != ESP_OK) |
| 106 | + ERROR("File is not a valid esp32 image.\nCannot continue."); |
| 107 | + |
| 108 | + ALERT("Info", "Image contains %d partitions.", num_partitions); |
| 109 | + |
| 110 | + // We can work around this, this is for debugging purposes |
| 111 | + if (memcmp(&device_partition_table, &partition_table, sizeof(partition_table))) |
| 112 | + ALERT("Error", "Partition table mismatch."); |
| 113 | + |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
23 | 117 | void app_main(void) |
24 | 118 | { |
25 | | - rg_app_t *app = rg_system_init(32000, NULL, NULL); |
| 119 | + app = rg_system_init(32000, NULL, NULL); |
| 120 | + // This is a hack to hide the status bar with bogus speed info. A better solution will be |
| 121 | + // needed as we still want the battery icon! |
| 122 | + app->isLauncher = true; |
26 | 123 |
|
27 | 124 | if (!rg_storage_ready()) |
28 | 125 | { |
29 | | - rg_display_clear(C_SKY_BLUE); |
30 | | - rg_gui_alert(_("SD Card Error"), _("Storage mount failed.\nMake sure the card is FAT32.")); |
31 | | - // What do at this point? Reboot? Switch back to launcher? |
32 | | - goto launcher; |
| 126 | + ALERT("Error", "Storage mount failed.\nMake sure the card is FAT32."); |
| 127 | + rg_system_exit(); |
33 | 128 | } |
34 | 129 |
|
35 | | - // Hopefully we'll receive the filename from the launcher which has a nicer file browser |
36 | | - const char *filename = app->romPath; |
37 | | - |
38 | | - if (!filename || !rg_storage_exists(filename)) |
39 | | - filename = rg_gui_file_picker(_("Select update"), DOWNLOAD_LOCATION, NULL, true); |
40 | | - if (!filename) |
41 | | - goto launcher; |
| 130 | + if (spi_flash_read(ESP_PARTITION_TABLE_OFFSET, &device_partition_table, sizeof(device_partition_table)) != ESP_OK) |
| 131 | + { |
| 132 | + ALERT("Error", "Failed to read device's partition table!"); |
| 133 | + rg_system_exit(); |
| 134 | + } |
42 | 135 |
|
43 | | - RG_LOGI("Filename: %s", filename); |
| 136 | + const char *filename = app->romPath; |
| 137 | + while (true) |
| 138 | + { |
| 139 | + if (!filename || !*filename) |
| 140 | + { |
| 141 | + filename = rg_gui_file_picker("Select update", DOWNLOAD_LOCATION, NULL, true, true); |
| 142 | + if (!filename || !*filename) |
| 143 | + break; |
| 144 | + } |
| 145 | + if (do_update(filename)) |
| 146 | + break; |
| 147 | + filename = NULL; |
| 148 | + } |
44 | 149 |
|
45 | | -launcher: |
46 | | - rg_system_switch_app(RG_APP_LAUNCHER, 0, 0, 0, 0); |
| 150 | + rg_system_exit(); |
47 | 151 | } |
0 commit comments