From 6ecac32cff9bac29a5e37952e896276a8a2f725e Mon Sep 17 00:00:00 2001 From: Lucas Eriksson Date: Tue, 4 Sep 2018 23:59:37 +0200 Subject: [PATCH] eeprom: Read EEPROM from file --- hw/xbox/xbox.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++- hw/xbox/xbox.h | 3 ++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/hw/xbox/xbox.c b/hw/xbox/xbox.c index e74e3e9b28..e3faa2a426 100644 --- a/hw/xbox/xbox.c +++ b/hw/xbox/xbox.c @@ -224,11 +224,62 @@ static void xbox_memory_init(PCMachineState *pcms, xbox_flash_init(rom_memory); } +uint8_t *load_eeprom(void) +{ + char *filename; + int fd; + int rc; + int eeprom_file_size; + const int eeprom_size = 256; + + uint8_t *eeprom_data = g_malloc(eeprom_size); + + const char *eeprom_file = object_property_get_str(qdev_get_machine(), + "eeprom", NULL); + if ((eeprom_file != NULL) && *eeprom_file) { + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, eeprom_file); + assert(filename); + + eeprom_file_size = get_image_size(filename); + if (eeprom_size != eeprom_file_size) { + fprintf(stderr, + "qemu: EEPROM file size != %d bytes. (Is %d bytes)\n", + eeprom_size, eeprom_file_size); + g_free(filename); + exit(1); + return NULL; + } + + fd = open(filename, O_RDONLY | O_BINARY); + if (fd < 0) { + fprintf(stderr, "qemu: EEPROM file '%s' could not be opened.\n", filename); + g_free(filename); + exit(1); + return NULL; + } + + rc = read(fd, eeprom_data, eeprom_size); + if (rc != eeprom_size) { + fprintf(stderr, "qemu: Could not read the full EEPROM file.\n"); + close(fd); + g_free(filename); + exit(1); + return NULL; + } + + close(fd); + g_free(filename); + } else { + memcpy(eeprom_data, default_eeprom, eeprom_size); + } + return eeprom_data; +} /* PC hardware initialisation */ static void xbox_init(MachineState *machine) { - xbox_init_common(machine, default_eeprom, NULL, NULL); + uint8_t *eeprom_data = load_eeprom(); + xbox_init_common(machine, eeprom_data, NULL, NULL); } void xbox_init_common(MachineState *machine, @@ -407,6 +458,22 @@ static void machine_set_bootrom(Object *obj, const char *value, ms->bootrom = g_strdup(value); } +static char *machine_get_eeprom(Object *obj, Error **errp) +{ + XboxMachineState *ms = XBOX_MACHINE(obj); + + return g_strdup(ms->eeprom); +} + +static void machine_set_eeprom(Object *obj, const char *value, + Error **errp) +{ + XboxMachineState *ms = XBOX_MACHINE(obj); + + g_free(ms->eeprom); + ms->eeprom = g_strdup(value); +} + static void machine_set_short_animation(Object *obj, bool value, Error **errp) { @@ -428,6 +495,11 @@ static inline void xbox_machine_initfn(Object *obj) object_property_set_description(obj, "bootrom", "Xbox bootrom file", NULL); + object_property_add_str(obj, "eeprom", machine_get_eeprom, + machine_set_eeprom, NULL); + object_property_set_description(obj, "eeprom", + "Xbox EEPROM file", NULL); + object_property_add_bool(obj, "short-animation", machine_get_short_animation, machine_set_short_animation, NULL); diff --git a/hw/xbox/xbox.h b/hw/xbox/xbox.h index 56e9098522..af4c0f0331 100644 --- a/hw/xbox/xbox.h +++ b/hw/xbox/xbox.h @@ -25,6 +25,8 @@ #define MAX_IDE_BUS 2 +uint8_t *load_eeprom(void); + void xbox_init_common(MachineState *machine, const uint8_t *eeprom, PCIBus **pci_bus_out, @@ -44,6 +46,7 @@ typedef struct XboxMachineState { /*< public >*/ char *bootrom; + char *eeprom; bool short_animation; } XboxMachineState;