Skip to content

Commit abe134f

Browse files
committed
eeprom: Backup eeprom before saving
1 parent c76b106 commit abe134f

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

menu_eeprom.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct
3636
} eeprom_factory_settings_t;
3737

3838
static void update_eeprom_text(void);
39+
static void query_eeprom(void);
3940

4041
// https://github.com/xemu-project/xemu/blob/9d5cf0926aa6f8eb2221e63a2e92bd86b02afae0/hw/xbox/eeprom_generation.c#L25
4142
static unsigned int xbox_eeprom_crc(unsigned char *data, unsigned int len)
@@ -52,8 +53,48 @@ static unsigned int xbox_eeprom_crc(unsigned char *data, unsigned int len)
5253
return ~(high + low);
5354
}
5455

56+
static void restore_backup()
57+
{
58+
HANDLE eeprom_file = CreateFileA("E:\\eeprom.bin", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
59+
if (eeprom_file != INVALID_HANDLE_VALUE) {
60+
unsigned char eeprom_data[256];
61+
DWORD bytes_read;
62+
ReadFile(eeprom_file, eeprom_data, sizeof(eeprom_data), &bytes_read, NULL);
63+
CloseHandle(eeprom_file);
64+
65+
for (unsigned int i = 0; i < sizeof(eeprom_data); i++) {
66+
HalWriteSMBusValue(EEPROM_SMBUS_ADDRESS, i, FALSE, eeprom_data[i]);
67+
}
68+
69+
query_eeprom();
70+
update_eeprom_text();
71+
dirty = 0;
72+
73+
static MenuItem menu_item = {"Backup restored successfully", NULL};
74+
static Menu menu = {
75+
.item = &menu_item,
76+
.item_count = 1,
77+
.selected_index = 0,
78+
.scroll_offset = 0};
79+
menu_push(&menu);
80+
}
81+
}
82+
5583
static void apply_settings(void)
5684
{
85+
// First create a full backup of the EEPROM to E:\\eeprom.bin
86+
SetFileAttributesA("E:\\eeprom.bin", FILE_ATTRIBUTE_NORMAL);
87+
HANDLE eeprom_file = CreateFileA("E:\\eeprom.bin", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
88+
if (eeprom_file != INVALID_HANDLE_VALUE) {
89+
unsigned char eeprom_data[256];
90+
for (unsigned int i = 0; i < sizeof(eeprom_data); i++) {
91+
HalReadSMBusValue(EEPROM_SMBUS_ADDRESS, i, FALSE, (ULONG *)&eeprom_data[i]);
92+
}
93+
DWORD bytes_written;
94+
WriteFile(eeprom_file, eeprom_data, sizeof(eeprom_data), &bytes_written, NULL);
95+
CloseHandle(eeprom_file);
96+
}
97+
5798
ULONG type = 4;
5899
ExSaveNonVolatileSetting(XC_DVD_REGION, type, &dvd_region_index, sizeof(dvd_region_index));
59100
ExSaveNonVolatileSetting(XC_LANGUAGE, type, &language_index, sizeof(language_index));
@@ -221,7 +262,7 @@ static void increment_timezone_bios(void)
221262
// to account for time zones with 30 minute offsets.
222263
time_zone_offset -= 30;
223264
if (time_zone_offset < -720) { // -12 hours
224-
time_zone_offset = 720; // wrap around to 12 hours
265+
time_zone_offset = 720; // wrap around to 12 hours
225266
}
226267
dirty = 1;
227268
update_eeprom_text();
@@ -288,6 +329,10 @@ static void update_eeprom_text(void)
288329
push_line(line++, apply_settings, "Apply");
289330
}
290331

332+
DWORD fileAttr = GetFileAttributesA("E:\\eeprom.bin");
333+
const BOOL eeprom_backup_exists = (fileAttr != INVALID_FILE_ATTRIBUTES && !(fileAttr & FILE_ATTRIBUTE_DIRECTORY));
334+
push_line(line++, (eeprom_backup_exists) ? restore_backup : NULL, "Restore EEPROM Backup");
335+
291336
const char *game_region;
292337
switch (game_region_index) {
293338
case 0x00000001: game_region = "North America"; break;
@@ -369,14 +414,14 @@ static void update_eeprom_text(void)
369414
mac_address[0], mac_address[1], mac_address[2],
370415
mac_address[3], mac_address[4], mac_address[5]);
371416

372-
push_line(line++, increment_timezone_bios, "Time Zone Offset: %.1f hours", ((float)-time_zone_offset)/60.0f);
417+
push_line(line++, increment_timezone_bios, "Time Zone Offset: %.1f hours", ((float)-time_zone_offset) / 60.0f);
373418

374419
menu.item_count = line;
375-
printf("pool_offset: %d, menu.item_count: %d\n", pool_offset, menu.item_count);
376420
}
377421

378422
void menu_eeprom_activate(void)
379423
{
424+
dirty = 0;
380425
query_eeprom();
381426
update_eeprom_text();
382427

0 commit comments

Comments
 (0)