Add size option and checks to flash_rom

Peter Fox peter.fox at aeroflex.com
Thu Nov 6 03:43:00 CET 2003


The patch below adds a size option to flash_rom, and also verifies
that the bios image being flashed matches the rom size.
(Previously, if the rom image was too small, the image would
be flashed at the beginning of the rom, so the reset vector
would be in the wrong place.)

The advantage of this change is that it is no longer necessary
to pack the bios image up to the rom size, thus saving programming
time. And it is also not necessary to hack the code if you've
wired the top bit to a switch as a BIOS saviour.

It still doesn't check for an image too big, though that should
be taken care of in the build process.

Index: flash_rom.c
===================================================================
RCS file: /home/fox/freebios-latest/freebios/util/flash_and_burn/flash_rom.c,v
retrieving revision 1.23
diff -u -r1.23 flash_rom.c
--- flash_rom.c	2003/09/12 22:41:53	1.23
+++ flash_rom.c	2003/11/06 08:41:33
@@ -272,11 +272,12 @@
   return 0;
 }

-struct flashchip * probe_flash(struct flashchip * flash)
+struct flashchip * probe_flash(struct flashchip * flash, int pretend_size)
 {
     int fd_mem;
     volatile char * bios;
     unsigned long size;
+    int total_size;

     if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
 	perror("Can not open /dev/mem");
@@ -288,15 +289,18 @@
 	    flash++;
 	    continue;
 	}
-	printf("Trying %s, %d KB\n", flash->name, flash->total_size);
-	size = flash->total_size * 1024;
+	total_size = flash->total_size;
+	if((pretend_size != -1)&&(pretend_size < total_size))
+	  total_size = pretend_size;
+	printf("Trying %s, %d KB\n", flash->name, total_size);
+	size = total_size * 1024;
 /* BUG? what happens if getpagesize() > size!?
    -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
 	if(getpagesize() > size)
 	{
 		size = getpagesize();
 		printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
-	 		flash->total_size * 1024, (unsigned long)size);
+	 		total_size * 1024, (unsigned long)size);
 	}
 	bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
 		     fd_mem, (off_t) (0 - size));
@@ -310,6 +314,7 @@
 	if (flash->probe(flash) == 1) {
 	    printf ("%s found at physical address: 0x%lx\n",
 		    flash->name, (0 - size));
+	    flash->total_size = total_size;
 	    return flash;
 	}
 	munmap ((void *) bios, size);
@@ -441,11 +446,12 @@

 void usage(const char *name)
 {
-    printf("usage: %s [-rwv] [-c chipname][file]\n", name);
+    printf("usage: %s [-rwv] [-c chipname] [-s size] [file]\n", name);
     printf("-r: read flash and save into file\n"
         "-w: write file into flash (default when file is specified)\n"
         "-v: verify flash against file\n"
         "-c: probe only for specified flash chip\n"
+        "-s: pretend device is smaller, specify the size in k\n"
         " If no file is specified, then all that happens\n"
         " is that flash info is dumped\n");
     exit(1);
@@ -456,6 +462,7 @@
 {
     char * buf;
     unsigned long size;
+    int pretend_size = -1;
     FILE * image;
     struct flashchip * flash;
     int opt;
@@ -464,7 +471,7 @@

     setbuf(stdout, NULL);

-    while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
+    while ((opt = getopt(argc, argv, "rwvc:s:")) != EOF) {
         switch (opt) {
         case 'r':
             read_it = 1;
@@ -478,6 +485,9 @@
         case 'c':
             chip_to_probe = strdup(optarg);
             break;
+        case 's':
+            pretend_size = atoi(optarg);
+            break;
         default:
             usage(argv[0]);
             break;
@@ -500,7 +510,7 @@
      */
     (void) enable_flash_write();

-    if ((flash = probe_flash (flashchips)) == NULL) {
+    if ((flash = probe_flash (flashchips, pretend_size)) == NULL) {
 	printf("EEPROM not found\n");
 	exit(1);
     }
@@ -527,12 +537,18 @@
         fclose(image);
         printf("done\n");
     } else {
+      unsigned long filesize = 0;
         if ((image = fopen (filename, "r")) == NULL) {
             perror(filename);
             exit(1);
         }
-        fread (buf, sizeof(char), size, image);
+        filesize = fread (buf, sizeof(char), size, image);
         fclose(image);
+	if(filesize != size)
+	  {
+	    fprintf(stderr, "File size doesn't match device size\n");
+	    exit(1);
+	  }
     }

     if (write_it || (!read_it && !verify_it))

--
Peter Fox <peter.fox at aeroflex.com>     Aeroflex Test Solutions
Principal Design Engineer                            Stevenage
Any opinions expressed above are      http://www.aeroflex.com/
not necessarily those of Aeroflex.   Tel: + 44 (0) 1438 742200




More information about the coreboot mailing list