[modrom] Avoid clobbering near jump with checksum
[gpxe.git] / contrib / rom-scan / rom-scan.c
blobc5e4829916fe2bebb833312b239df5b8fe966510
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #ifndef __TURBOC__
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #endif
12 #ifdef __TURBOC__
13 #define HUGE huge
14 #else
15 #define HUGE
16 #endif
18 #define ROMSTART 0xC8000
19 #define ROMEND 0xE8000
20 #define ROMINCREMENT 0x00800
21 #define ROMMASK 0x03FFF
23 #ifndef MAP_FAILED
24 #define MAP_FAILED ((void *)(long long)-1)
25 #endif
27 typedef struct Images {
28 struct Images *next;
29 long start;
30 long size;
31 } Images;
33 static void rom_scan(const unsigned char HUGE *rom,long offset,long len)
35 static Images *images = NULL;
36 Images *ptr;
37 /* The assignments to dummy are to overcome a bug in TurboC */
38 long dummy, size;
39 int chksum;
40 long i;
42 if (rom[offset] != 0x55 || rom[dummy = offset+1] != 0xAA)
43 return;
44 size = (long)rom[dummy = offset+2]*512L;
45 printf("Found ROM header at %04lX:0000; "
46 "announces %ldk image (27C%02d EPROM)\n",
47 offset/16,(size+512)/1024,
48 size <= 1024 ? 8 :
49 size <= 2048 ? 16 :
50 size <= 4096 ? 32 :
51 size <= 8192 ? 64 :
52 size <= 16384 ? 128 :
53 size <= 32768 ? 256 :
54 size <= 65536 ? 512 : 11);
55 if (offset & ROMMASK)
56 printf(" This is a unusual position; not all BIOSs might find it.\n"
57 " Try to move to a 16kB boundary.\n");
58 if (size > len) {
59 printf(" This image extends beyond %04X:0000. "
60 "It clashes with the system BIOS\n",
61 ROMEND/16);
62 size = len; }
63 for (chksum=0, i = size; i--; chksum += rom[dummy = offset+i]);
64 if (chksum % 256)
65 printf(" Checksum does not match. This image is not active\n");
66 ptr = malloc(sizeof(Images));
67 ptr->next = images;
68 ptr->start = offset;
69 ptr->size = size;
70 images = ptr;
71 for (ptr = ptr->next; ptr != NULL; ptr = ptr->next) {
72 for (i = 0; i < size && i < ptr->size; i++)
73 if (rom[dummy = ptr->start+i] != rom[dummy = offset+i])
74 break;
75 if (i > 32) {
76 printf(" Image is identical with image at %04lX:0000 "
77 "for the first %ld bytes\n",
78 ptr->start/16,i);
79 if (i >= 1024)
80 if (i == size)
81 printf(" this means that you misconfigured the EPROM size!\n");
82 else
83 printf(" this could suggest that you misconfigured the "
84 "EPROM size\n");
85 else
86 printf(" this is probably harmless. Just ignore it...\n"); } }
87 return;
90 int main(void)
92 long i;
93 unsigned char HUGE *rom;
95 #ifndef __TURBOC__
96 int fh;
97 if ((fh = open("/dev/kmem",O_RDONLY|O_SYNC)) < 0) {
98 fprintf(stderr,"Could not open \"/dev/kmem\": %s\n",0 );//strerror(errno));
99 return(1); }
100 if ((rom = mmap(NULL,ROMEND-ROMSTART,PROT_READ,MAP_SHARED,fh,
101 ROMSTART)) == MAP_FAILED) {
102 fprintf(stderr,"Could not mmap \"/dev/kmem\": %s\n",0); //strerror(errno));
103 close(fh);
104 return(1); }
105 close(fh);
106 #endif
107 for (i = ROMEND; (i -= ROMINCREMENT) >= ROMSTART; )
108 #ifdef __TURBOC__
109 rom_scan(0,i,ROMEND-i);
110 #else
111 rom_scan(rom-ROMSTART,i,ROMEND-i);
112 munmap(rom,ROMEND-ROMSTART);
113 #endif
114 return(0);