From 1c45ac1ccf0569b6f99c4bc5eca7a262fd4aef08 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 12 Oct 2025 21:13:24 -0400 Subject: misc: Add initial fw ROM loading Signed-off-by: Ian Moffett --- emux64/sample/nop.rom | Bin 0 -> 14 bytes emux64/src/include/rom.h | 65 ++++++++++++++++++++++++++++++++++++ emux64/src/main.c | 30 +++++++++++++++-- emux64/src/rom.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 emux64/sample/nop.rom create mode 100644 emux64/src/include/rom.h create mode 100644 emux64/src/rom.c diff --git a/emux64/sample/nop.rom b/emux64/sample/nop.rom new file mode 100644 index 0000000..ddeddbc Binary files /dev/null and b/emux64/sample/nop.rom differ diff --git a/emux64/src/include/rom.h b/emux64/src/include/rom.h new file mode 100644 index 0000000..8d3501c --- /dev/null +++ b/emux64/src/include/rom.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ROM_H +#define ROM_H + +#include +#include + +/* + * Represents a firmware ROM file + * + * @buf: Buffer containing ROM data + * @length: Length of ROM + */ +struct osmx_rom { + uint8_t *buf; + size_t length; +}; + +/* + * Open a ROM file by path + * + * @path: Path to ROM file + * @res: Result of ROM descriptor + * + * Returns zero on success, otherwise a less than + * zero value on failure. + */ +int rom_open(const char *path, struct osmx_rom *res); + +/* + * Release resources associated with a ROM file + * + * @rom: ROM to release from host memory + */ +void rom_release(struct osmx_rom *rom); + +#endif /* ROM_H */ diff --git a/emux64/src/main.c b/emux64/src/main.c index c23a48f..8c42655 100644 --- a/emux64/src/main.c +++ b/emux64/src/main.c @@ -32,8 +32,10 @@ #include #include #include "cpu/cpu.h" +#include "rom.h" static char *rom_file = NULL; +struct osmx_rom g_firmware; static void help(void) @@ -52,6 +54,31 @@ cleanup(void) free(rom_file); rom_file = NULL; } + + rom_release(&g_firmware); +} + +static int +run(struct osmx_core *core) +{ + int error; + + if (rom_file == NULL) { + return -1; + } + + /* Open the firmware ROM */ + cpu_dump(core); + error = rom_open(rom_file, &g_firmware); + if (error < 0) { + return error; + } + + printf("ROM: "); + for (int i = 0; i < g_firmware.length; ++i) { + printf("%02X ", g_firmware.buf[i]); + } + printf("\n"); } int @@ -84,6 +111,5 @@ main(int argc, char **argv) return error; } - cpu_dump(&core); - return 0; + return run(&core); } diff --git a/emux64/src/rom.c b/emux64/src/rom.c new file mode 100644 index 0000000..24d447d --- /dev/null +++ b/emux64/src/rom.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "rom.h" + +int +rom_open(const char *path, struct osmx_rom *res) +{ + int fd; + + if (path == NULL || res == NULL) { + return -EINVAL; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + perror("open"); + return fd; + } + + /* Get the ROM length */ + res->length = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + + /* Allocate space for the ROM */ + res->buf = malloc(res->length); + if (res->buf == NULL) { + printf("error: out of host memory\n"); + close(fd); + return -ENOMEM; + } + + read(fd, res->buf, res->length); + close(fd); + return 0; +} + +void +rom_release(struct osmx_rom *rom) +{ + if (rom == NULL) { + return; + } + + if (rom->buf == NULL) { + return; + } + + free(rom->buf); + rom->buf = NULL; +} -- cgit v1.2.3