int rom_add_file(const char *file, const char *fw_dir,
                 hwaddr addr, int32_t bootindex,
                 bool option_rom, MemoryRegion *mr)
{
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
    Rom *rom;
    int rc, fd = -1;
    char devpath[100];
    rom = g_malloc0(sizeof(*rom));
    rom->name = g_strdup(file);
    rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
    if (rom->path == NULL) {
        rom->path = g_strdup(file);
    fd = open(rom->path, O_RDONLY | O_BINARY);
    if (fd == -1) {
        fprintf(stderr, "Could not open option rom '%s': %s\n",
                rom->path, strerror(errno));
        goto err;
        rom->fw_dir  = g_strdup(fw_dir);
        rom->fw_file = g_strdup(file);
    rom->addr     = addr;
    rom->romsize  = lseek(fd, 0, SEEK_END);
    if (rom->romsize == -1) {
        fprintf(stderr, "rom: file %-20s: get size error: %s\n",
                rom->name, strerror(errno));
        goto err;
    rom->datasize = rom->romsize;
    rom->data     = g_malloc0(rom->datasize);
    lseek(fd, 0, SEEK_SET);
    rc = read(fd, rom->data, rom->datasize);
    if (rc != rom->datasize) {
        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
                rom->name, rc, rom->datasize);
        goto err;
    close(fd);
    rom_insert(rom);
    if (rom->fw_file && fw_cfg) {
        const char *basename;
        char fw_file_name[FW_CFG_MAX_FILE_PATH];
        void *data;
        basename = strrchr(rom->fw_file, '/');
        if (basename) {
            basename++;
        } else {
            basename = rom->fw_file;
        snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
                 basename);
        snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
        if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
            data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
        } else {
            data = rom->data;
        fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
    } else {
        if (mr) {
            rom->mr = mr;
            snprintf(devpath, sizeof(devpath), "/rom@%s", file);
        } else {
            snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
    add_boot_device_path(bootindex, NULL, devpath);
    return 0;
err:
    if (fd != -1)
        close(fd);
    g_free(rom->data);
    g_free(rom->path);
    g_free(rom->name);
    g_free(rom);
    return -1;
int rom_add_file(const char *file, const char *fw_dir,
                 hwaddr addr, int32_t bootindex,
                 bool option_rom, MemoryRegion *mr)
{
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
    Rom *rom;
    int rc, fd = -1;
    char devpath[100];
    rom = g_malloc0(sizeof(*rom));
    rom->name = g_strdup(file);
    rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
    if (rom->path == NULL) {
        rom->path = g_strdup(file);
    fd = open(rom->path, O_RDONLY | O_BINARY);
    if (fd == -1) {
        fprintf(stderr, "Could not open option rom '%s': %s\n",
                rom->path, strerror(errno));
        goto err;
        rom->fw_dir  = g_strdup(fw_dir);
        rom->fw_file = g_strdup(file);
    rom->addr     = addr;
    rom->romsize  = lseek(fd, 0, SEEK_END);
    if (rom->romsize == -1) {
        fprintf(stderr, "rom: file %-20s: get size error: %s\n",
                rom->name, strerror(errno));
        goto err;
    rom->datasize = rom->romsize;
    rom->data     = g_malloc0(rom->datasize);
    lseek(fd, 0, SEEK_SET);
    rc = read(fd, rom->data, rom->datasize);
    if (rc != rom->datasize) {
        fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
                rom->name, rc, rom->datasize);
        goto err;
    close(fd);
    rom_insert(rom);
    if (rom->fw_file && fw_cfg) {
        const char *basename;
        char fw_file_name[FW_CFG_MAX_FILE_PATH];
        void *data;
        basename = strrchr(rom->fw_file, '/');
        if (basename) {
            basename++;
        } else {
            basename = rom->fw_file;
        snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
                 basename);
        snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
        if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
            data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
        } else {
            data = rom->data;
        fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
    } else {
        if (mr) {
            rom->mr = mr;
            snprintf(devpath, sizeof(devpath), "/rom@%s", file);
        } else {
            snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
    add_boot_device_path(bootindex, NULL, devpath);
    return 0;
err:
    if (fd != -1)
        close(fd);
    g_free(rom->data);
    g_free(rom->path);
    g_free(rom->name);
    g_free(rom);
    return -1;
int FUNC_0(const char *VAR_0, const char *VAR_1,
                 hwaddr VAR_2, int32_t VAR_3,
                 bool VAR_4, MemoryRegion *VAR_5)
{
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
    Rom *rom;
    int VAR_6, VAR_7 = -1;
    char VAR_8[100];
    rom = g_malloc0(sizeof(*rom));
    rom->name = g_strdup(VAR_0);
    rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
    if (rom->path == NULL) {
        rom->path = g_strdup(VAR_0);
    VAR_7 = open(rom->path, O_RDONLY | O_BINARY);
    if (VAR_7 == -1) {
        fprintf(stderr, "Could not open option rom '%s': %s\n",
                rom->path, strerror(errno));
        goto err;
        rom->VAR_1  = g_strdup(VAR_1);
        rom->fw_file = g_strdup(VAR_0);
    rom->VAR_2     = VAR_2;
    rom->romsize  = lseek(VAR_7, 0, SEEK_END);
    if (rom->romsize == -1) {
        fprintf(stderr, "rom: VAR_0 %-20s: get size error: %s\n",
                rom->name, strerror(errno));
        goto err;
    rom->datasize = rom->romsize;
    rom->VAR_11     = g_malloc0(rom->datasize);
    lseek(VAR_7, 0, SEEK_SET);
    VAR_6 = read(VAR_7, rom->VAR_11, rom->datasize);
    if (VAR_6 != rom->datasize) {
        fprintf(stderr, "rom: VAR_0 %-20s: read error: VAR_6=%d (expected %zd)\n",
                rom->name, VAR_6, rom->datasize);
        goto err;
    close(VAR_7);
    rom_insert(rom);
    if (rom->fw_file && fw_cfg) {
        const char *VAR_9;
        char VAR_10[FW_CFG_MAX_FILE_PATH];
        void *VAR_11;
        VAR_9 = strrchr(rom->fw_file, '/');
        if (VAR_9) {
            VAR_9++;
        } else {
            VAR_9 = rom->fw_file;
        snprintf(VAR_10, sizeof(VAR_10), "%s/%s", rom->VAR_1,
                 VAR_9);
        snprintf(VAR_8, sizeof(VAR_8), "/rom@%s", VAR_10);
        if ((!VAR_4 || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
            VAR_11 = rom_set_mr(rom, OBJECT(fw_cfg), VAR_8);
        } else {
            VAR_11 = rom->VAR_11;
        fw_cfg_add_file(fw_cfg, VAR_10, VAR_11, rom->romsize);
    } else {
        if (VAR_5) {
            rom->VAR_5 = VAR_5;
            snprintf(VAR_8, sizeof(VAR_8), "/rom@%s", VAR_0);
        } else {
            snprintf(VAR_8, sizeof(VAR_8), "/rom@" TARGET_FMT_plx, VAR_2);
    add_boot_device_path(VAR_3, NULL, VAR_8);
    return 0;
err:
    if (VAR_7 != -1)
        close(VAR_7);
    g_free(rom->VAR_11);
    g_free(rom->path);
    g_free(rom->name);
    g_free(rom);
    return -1;
int FUNC_0(const char *VAR_0, const char *VAR_1,
hwaddr VAR_2, int32_t VAR_3,
bool VAR_4, MemoryRegion *VAR_5)
{ MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); Rom *rom; int VAR_6, VAR_7 = -1; char VAR_8[100]; rom = g_malloc0(sizeof(*rom)); rom->name = g_strdup(VAR_0); rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name); if (rom->path == NULL) { rom->path = g_strdup(VAR_0); VAR_7 = open(rom->path, O_RDONLY | O_BINARY); if (VAR_7 == -1) { fprintf(stderr, "Could not open option rom '%s': %s\n",
rom->path, strerror(errno)); goto err; rom->VAR_1  = g_strdup(VAR_1); rom->fw_file = g_strdup(VAR_0); rom->VAR_2     = VAR_2; rom->romsize  = lseek(VAR_7, 0, SEEK_END); if (rom->romsize == -1) { fprintf(stderr, "rom: VAR_0 %-20s: get size error: %s\n",
rom->name, strerror(errno)); goto err; rom->datasize = rom->romsize; rom->VAR_11     = g_malloc0(rom->datasize); lseek(VAR_7, 0, SEEK_SET); VAR_6 = read(VAR_7, rom->VAR_11, rom->datasize); if (VAR_6 != rom->datasize) { fprintf(stderr, "rom: VAR_0 %-20s: read error: VAR_6=%d (expected %zd)\n",
rom->name, VAR_6, rom->datasize); goto err; close(VAR_7); rom_insert(rom); if (rom->fw_file && fw_cfg) { const char *VAR_9; char VAR_10[FW_CFG_MAX_FILE_PATH]; void *VAR_11; VAR_9 = strrchr(rom->fw_file, '/'); if (VAR_9) { VAR_9++; } else { VAR_9 = rom->fw_file; snprintf(VAR_10, sizeof(VAR_10), "%s/%s", rom->VAR_1,
VAR_9); snprintf(VAR_8, sizeof(VAR_8), "/rom@%s", VAR_10); if ((!VAR_4 || mc->option_rom_has_mr) && mc->rom_file_has_mr) { VAR_11 = rom_set_mr(rom, OBJECT(fw_cfg), VAR_8); } else { VAR_11 = rom->VAR_11; fw_cfg_add_file(fw_cfg, VAR_10, VAR_11, rom->romsize); } else { if (VAR_5) { rom->VAR_5 = VAR_5; snprintf(VAR_8, sizeof(VAR_8), "/rom@%s", VAR_0); } else { snprintf(VAR_8, sizeof(VAR_8), "/rom@" TARGET_FMT_plx, VAR_2); add_boot_device_path(VAR_3, NULL, VAR_8); return 0; err:
if (VAR_7 != -1)
close(VAR_7); g_free(rom->VAR_11); g_free(rom->path); g_free(rom->name); g_free(rom); return -1;