Changeset 14047 in vbox
- Timestamp:
- Nov 10, 2008 10:42:14 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/biossums.c
r13438 r14047 23 23 #include <stdio.h> 24 24 #include <string.h> 25 #include <stdarg.h> 25 26 #include <errno.h> 26 27 … … 28 29 29 30 static uint8_t abBios[64*1024]; 31 static FILE *g_pIn = NULL; 32 static FILE *g_pOut = NULL; 33 static const char *g_pszOutFile = NULL; 34 static const char *g_argv0; 35 36 /** 37 * Find where the filename starts in the given path. 38 */ 39 static const char *name(const char *pszPath) 40 { 41 const char *psz = strrchr(pszPath, '/'); 42 #if defined(_MSC_VER) || defined(__OS2__) 43 const char *psz2 = strrchr(pszPath, '\\'); 44 if (!psz2) 45 psz2 = strrchr(pszPath, ':'); 46 if (psz2 && (!psz || psz2 > psz)) 47 psz = psz2; 48 #endif 49 return psz ? psz + 1 : pszPath; 50 } 51 52 /** 53 * Report an error. 54 */ 55 static int fatal(const char *pszFormat, ...) 56 { 57 va_list va; 58 59 fprintf(stderr, "%s: ", name(g_argv0)); 60 61 va_start(va, pszFormat); 62 vfprintf(stderr, pszFormat, va); 63 va_end(va); 64 65 /* clean up */ 66 if (g_pIn) 67 fclose(g_pIn); 68 if (g_pOut) 69 fclose(g_pOut); 70 if (g_pszOutFile) 71 unlink(g_pszOutFile); 72 73 return 1; 74 } 30 75 31 76 /** 32 77 * Calculate the checksum. 33 78 */ 34 static uint8_t calculateChecksum(uint8_t *pb, size_t cb, unsigned int iChecksum)35 { 36 uint8_t 37 unsigned int i;79 static uint8_t calculateChecksum(uint8_t *pb, size_t cb, size_t iChecksum) 80 { 81 uint8_t u8Sum = 0; 82 size_t i; 38 83 39 84 for (i = 0; i < cb; i++) … … 45 90 46 91 /** 92 * Find a header in the binary. 93 * 47 94 * @param pb Where to search for the signature 48 95 * @param cb Size of the search area … … 73 120 int fAdapterBios = 0; 74 121 122 g_argv0 = argv[0]; 123 75 124 if (argc != 3) 76 { 77 printf("Input file name and output file name required.\n"); 78 exit(-1); 79 } 80 81 pIn = fopen(argv[1], "rb"); 125 return fatal("Input file name and output file name required.\n"); 126 127 pIn = g_pIn = fopen(argv[1], "rb"); 82 128 if (!pIn) 83 { 84 printf("Error opening '%s' for reading (%s).\n", argv[1], strerror(errno)); 85 exit(-1); 86 } 87 88 pOut = fopen(argv[2], "wb"); 129 return fatal("Error opening '%s' for reading (%s).\n", argv[1], strerror(errno)); 130 131 pOut = g_pOut = fopen(argv[2], "wb"); 89 132 if (!pOut) 90 { 91 printf("Error opening '%s' for writing (%s).\n", argv[2], strerror(errno)); 92 exit(-1); 93 } 94 95 /* safety precaution */ 133 return fatal("Error opening '%s' for writing (%s).\n", argv[2], strerror(errno)); 134 g_pszOutFile = argv[2]; 135 136 /* safety precaution (aka. complete paranoia :-) */ 96 137 memset(abBios, 0, sizeof(abBios)); 97 138 98 139 cbIn = fread(abBios, 1, sizeof(abBios), pIn); 99 140 if (ferror(pIn)) 100 { 101 printf("Error reading from '%s' (%s).\n", argv[1], strerror(errno)); 102 fclose(pIn); 103 exit(-1); 104 } 141 return fatal("Error reading from '%s' (%s).\n", argv[1], strerror(errno)); 142 g_pIn = NULL; 105 143 fclose(pIn); 106 144 … … 112 150 113 151 if (!fAdapterBios && cbIn != 64*1024) 114 { 115 printf("Size of system BIOS is not 64KB!\n"); 116 fclose(pOut); 117 exit(-1); 118 } 152 return fatal("Size of system BIOS is not 64KB!\n"); 119 153 120 154 if (fAdapterBios) 121 155 { 122 156 /* adapter BIOS */ 123 157 124 158 /* set the length indicator */ 125 159 abBios[2] = (uint8_t)(cbIn / 512); … … 136 170 { 137 171 case 0: 138 printf("No BIOS32 header not found!\n"); 139 exit(-1); 172 return fatal("No BIOS32 header not found!\n"); 140 173 case 2: 141 printf("More than one BIOS32 header found!\n"); 142 exit(-1); 174 return fatal("More than one BIOS32 header found!\n"); 143 175 case 1: 144 176 cbChecksum = (size_t)pbHeader[9] * 16; … … 153 185 { 154 186 case 0: 155 printf("No PCI IRQ routing table found!\n"); 156 exit(-1); 187 return fatal("No PCI IRQ routing table found!\n"); 157 188 case 2: 158 printf("More than one PCI IRQ routing table found!\n"); 159 exit(-1); 189 return fatal("More than one PCI IRQ routing table found!\n"); 160 190 case 1: 161 191 cbChecksum = (size_t)pbHeader[6] + (size_t)pbHeader[7] * 256; … … 170 200 { 171 201 case 0: 172 printf("No SMBIOS header found!\n"); 173 exit(-1); 202 return fatal("No SMBIOS header found!\n"); 174 203 case 2: 175 printf("More than one SMBIOS header found!\n"); 176 exit(-1); 204 return fatal("More than one SMBIOS header found!\n"); 177 205 case 1: 178 206 /* at first fix the DMI header starting at SMBIOS header offset 16 */ … … 193 221 cbOut = fwrite(abBios, 1, cbIn, pOut); 194 222 if (ferror(pOut)) 195 { 196 printf("Error writing to '%s' (%s).\n", argv[2], strerror(errno)); 197 fclose(pOut); 198 exit(-1); 199 } 200 201 fclose(pOut); 223 return fatal("Error writing to '%s' (%s).\n", g_pszOutFile, strerror(errno)); 224 g_pOut = NULL; 225 if (fclose(pOut)) 226 return fatal("Error closing '%s' (%s).\n", g_pszOutFile, strerror(errno)); 202 227 203 228 return 0; 204 229 } 230
Note:
See TracChangeset
for help on using the changeset viewer.