Any particular reason you're using mmap to read in the flash file? that just seems like it's adding one more place for a seg fault. why not change it to use read? something like this:
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define UADDR1 0xAAA
#define UADDR2 0x555
int main(int argc, char *argv[])
{
int fd_mem, fd_flash, i, i2;
volatile unsigned short *bios, *flash;
unsigned long off,valint;
unsigned short val, wrote;
if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
perror("Can not open /dev/mem");
exit(1);
}
if ((fd_flash = open("flash.rom", O_RDONLY)) < 0) {
perror("Can not open flash.rom");
exit(1);
}
bios = mmap(0, 0x20000, PROT_WRITE | PROT_READ, MAP_SHARED,
fd_mem, (off_t) (0x70000000));
if (bios == MAP_FAILED) {
perror("Error MMAP /dev/mem");
exit(1);
}
/* Enter unlock byp butt mode */
bios[UADDR1] = 0xAA;
bios[UADDR2] = 0x55;
bios[UADDR1] = 0x20;
for (i=0x0;i<0x10000;i++) {
if (bios[i] == 0xffff) {
// wrote = flash[i];
read(flash_fd,&wrote,2);
printf("Setting 0x%02x=0x%04x\n", i, wrote);
bios[0] = 0xA0;
bios[i] = wrote;
usleep(100);
while ((val = bios[i]) != wrote) {
printf("0x%02x=0x%04x\n", i, val);
usleep(100);
}
printf("0x%02x=0x%04x\n", i, val);
} else {
printf("0x%02x isn't erased\n", i);
}
}
/* Exit unlock bypa s s mode */
bios[0] = 0x90;
bios[0] = 0x00;
munmap((void *) bios, 0x20000);
// munmap((void *) flash, 0x20000);
return 0;
}