[sanboot] Prevent leaking a stack reference for "keep-san" AoE
[gpxe.git] / contrib / romid / romid.c
blobf6049bfdf497a9c7beede8df1a828d20a97d13d1
1 /* This little program is my try to provide information about the
2 EtherBoot rom via environment variables to a batch file. This
3 could be done better, I think, but it works...
4 The program compiles with Borland C 3.1; other versions not tested.
5 The C code for setting the environment variables I got from an
6 archive, it was written by Richard Marks <rmarks@KSP.unisys.COM>.
7 ROMID is written by Guenter Knauf <eflash@gmx.net>
8 */
9 #define VERSION "0.6"
10 #define VDATE "2003-08-24"
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #define ROMSTART 0xC8000
18 #define ROMEND 0xE8000
19 #define ROMINCREMENT 0x00800
20 #define ROMMASK 0x03FFF
22 int verbose = 0;
24 int settheenv(char *symbol, char *val);
26 static int rom_scan(const unsigned char huge *rom,long offset,long len) {
27 long size,i,j;
28 char symbol[16];
29 char val[64];
30 char romid[64];
31 char *rptr;
33 if (rom[offset] != 0x55 || rom[offset+1] != 0xAA)
34 return 0;
36 size = (long)rom[offset+2]*512L;
37 if (verbose) {
38 printf("Found ROM header at %04lX:0000; announces %ldk image\n", offset/16,(size+512)/1024);
39 if (offset & ROMMASK)
40 printf(" This is a unusual position; not all BIOSs might find it.\n"
41 " Try to move to a 16kB boundary.\n");
42 if (size > len) {
43 printf(" This image extends beyond %04X:0000. It clashes with the system BIOS\n", ROMEND/16);
44 size = len;
48 for (i=0; i<64; i++) {
49 if (rom[offset+size-3-i] == 0xff)
50 break;
52 if (20<i && i<63) {
53 i--;
54 for (j=0; j<i; j++)
55 val[j] = rom[offset+size-3-i+j];
56 val[i] = 0;
57 } else
58 return 0;
60 if (strstr(val, "therboot") == NULL)
61 return 0;
63 if (verbose)
64 printf("ROM Signature '%s'\n", val);
65 if ((rptr = strstr(val, "rom")) != NULL) {
66 for (i=1; i<4; i++) {
67 rptr--;
68 if (rptr[0] == 0x2E)
69 break;
71 i = 0;
72 while (!(rptr[0] == 0x20 || rptr < val)) {
73 i++;
74 rptr--;
76 rptr++;
77 i--;
78 strncpy(romid, rptr, i);
79 romid[i] = 0;
80 if (verbose)
81 printf("ROM Driver ID '%s'\n", romid);
82 strcpy(symbol, "ROMID");
83 if (settheenv(symbol, romid))
84 printf("Error setting evironment var %s with value %s\n", symbol, romid);
85 } else {
86 if (verbose)
87 printf("Couldnt find driver name!\n");
88 return 0;
90 if (rom[offset+0x1C] == 'P' && rom[offset+0x1D] == 'C' && rom[offset+0x1E] == 'I') {
91 sprintf(val, "%02X%02X:%02X%02X", rom[offset+0x21], rom[offset+0x20],
92 rom[offset+0x23], rom[offset+0x22]);
93 if (verbose)
94 printf("ROM Vendor ID '%s'\n", val);
95 strcpy(symbol, "PCIID");
96 if (settheenv(symbol, val))
97 printf("Error setting evironment var %s with value %s\n", symbol, val);
99 return 1;
102 /* **************** main stuff **************** */
103 int main (int argc, char *argv[]) {
104 long i;
106 printf("\nROM-ID for Etherboot v%s (c) G. Knauf %s\n", VERSION, VDATE);
107 if (argc > 1) {
108 /* parse input parameters */
109 for (argc--, argv++; *argv; argc--, argv++) {
110 if ((strnicmp (*argv, "-", 1) == 0) || (strnicmp (*argv, "/", 1) == 0)) {
111 if ((strnicmp (*argv, "-V", 2) == 0) || (strnicmp (*argv, "/V", 2) == 0)) {
112 verbose = 1;
113 } else {
114 printf("Usage: %s [-v]\n");
119 for (i = ROMEND; (i -= ROMINCREMENT) >= ROMSTART;)
120 if (rom_scan(0,i,ROMEND-i))
121 break;
123 return 0;