/*
	dump_rom.c  -- dump image of ROM to stdout.
*/

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#define ROM_SIZE 0xffff
#define ROM_OFFSET 0xF0000

int main(void) {

	int fd;
	void *bios_rom;

	fd = open("/dev/mem", O_RDONLY);
	if (fd == -1) {
		perror("open /dev/mem");
		exit(-1);
	}

	bios_rom = mmap(0, ROM_SIZE, PROT_READ, MAP_SHARED, fd, ROM_OFFSET);

	if ((long)bios_rom == -1) {
		perror("memmap /dev/mem");
		exit(-1);
	}

	if (write(STDOUT_FILENO, bios_rom, ROM_SIZE) != ROM_SIZE) {
		perror("write stdout");
		exit(-1);
	}

	if (munmap(bios_rom, ROM_SIZE) == -1) {
		perror("munmap");
		exit(-1);
	}

	if (close(fd) == -1) {
		perror("close");
		exit(-1);
	}

	return 0;
}
