1 | /* $Id: MakeAlternativeSource.cpp 50167 2014-01-22 19:47:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MakeAlternative - Generate an Alternative BIOS Source that requires less tools.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/asm.h>
|
---|
23 | #include <iprt/buildconfig.h>
|
---|
24 | #include <iprt/ctype.h>
|
---|
25 | #include <iprt/dbg.h>
|
---|
26 | #include <iprt/file.h>
|
---|
27 | #include <iprt/getopt.h>
|
---|
28 | #include <iprt/initterm.h>
|
---|
29 | #include <iprt/list.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #include <iprt/message.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/x86.h>
|
---|
35 |
|
---|
36 | #include <VBox/dis.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Structures and Typedefs *
|
---|
41 | *******************************************************************************/
|
---|
42 | /**
|
---|
43 | * A BIOS segment.
|
---|
44 | */
|
---|
45 | typedef struct BIOSSEG
|
---|
46 | {
|
---|
47 | char szName[32];
|
---|
48 | char szClass[32];
|
---|
49 | char szGroup[32];
|
---|
50 | RTFAR16 Address;
|
---|
51 | uint32_t uFlatAddr;
|
---|
52 | uint32_t cb;
|
---|
53 | } BIOSSEG;
|
---|
54 | /** Pointer to a BIOS segment. */
|
---|
55 | typedef BIOSSEG *PBIOSSEG;
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * A BIOS object file.
|
---|
60 | */
|
---|
61 | typedef struct BIOSOBJFILE
|
---|
62 | {
|
---|
63 | RTLISTNODE Node;
|
---|
64 | char *pszSource;
|
---|
65 | char *pszObject;
|
---|
66 | } BIOSOBJFILE;
|
---|
67 | /** A BIOS object file. */
|
---|
68 | typedef BIOSOBJFILE *PBIOSOBJFILE;
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Pointer to a BIOS map parser handle.
|
---|
73 | */
|
---|
74 | typedef struct BIOSMAP
|
---|
75 | {
|
---|
76 | /** The stream pointer. */
|
---|
77 | PRTSTREAM hStrm;
|
---|
78 | /** The file name. */
|
---|
79 | const char *pszMapFile;
|
---|
80 | /** Set when EOF has been reached. */
|
---|
81 | bool fEof;
|
---|
82 | /** The current line number (0 based).*/
|
---|
83 | uint32_t iLine;
|
---|
84 | /** The length of the current line. */
|
---|
85 | uint32_t cch;
|
---|
86 | /** The offset of the first non-white character on the line. */
|
---|
87 | uint32_t offNW;
|
---|
88 | /** The line buffer. */
|
---|
89 | char szLine[16384];
|
---|
90 | } BIOSMAP;
|
---|
91 | /** Pointer to a BIOS map parser handle. */
|
---|
92 | typedef BIOSMAP *PBIOSMAP;
|
---|
93 |
|
---|
94 |
|
---|
95 | /*******************************************************************************
|
---|
96 | * Global Variables *
|
---|
97 | *******************************************************************************/
|
---|
98 | /** The verbosity level.*/
|
---|
99 | static unsigned g_cVerbose = 1 /*0*/;
|
---|
100 | /** Pointer to the BIOS image. */
|
---|
101 | static uint8_t const *g_pbImg;
|
---|
102 | /** The size of the BIOS image. */
|
---|
103 | static size_t g_cbImg;
|
---|
104 |
|
---|
105 | /** Debug module for the map file. */
|
---|
106 | static RTDBGMOD g_hMapMod = NIL_RTDBGMOD;
|
---|
107 | /** The number of BIOS segments found in the map file. */
|
---|
108 | static uint32_t g_cSegs = 0;
|
---|
109 | /** Array of BIOS segments from the map file. */
|
---|
110 | static BIOSSEG g_aSegs[32];
|
---|
111 | /** List of BIOSOBJFILE. */
|
---|
112 | static RTLISTANCHOR g_ObjList;
|
---|
113 |
|
---|
114 | /** The output stream. */
|
---|
115 | static PRTSTREAM g_hStrmOutput = NULL;
|
---|
116 |
|
---|
117 | /** The type of BIOS we're working on. */
|
---|
118 | static enum BIOSTYPE
|
---|
119 | {
|
---|
120 | kBiosType_System = 0,
|
---|
121 | kBiosType_Vga
|
---|
122 | } g_enmBiosType = kBiosType_System;
|
---|
123 | /** The flat ROM base address. */
|
---|
124 | static uint32_t g_uBiosFlatBase = 0xf0000;
|
---|
125 |
|
---|
126 |
|
---|
127 | static bool outputPrintfV(const char *pszFormat, va_list va)
|
---|
128 | {
|
---|
129 | int rc = RTStrmPrintfV(g_hStrmOutput, pszFormat, va);
|
---|
130 | if (RT_FAILURE(rc))
|
---|
131 | {
|
---|
132 | RTMsgError("Output error: %Rrc\n", rc);
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | static bool outputPrintf(const char *pszFormat, ...)
|
---|
140 | {
|
---|
141 | va_list va;
|
---|
142 | va_start(va, pszFormat);
|
---|
143 | bool fRc = outputPrintfV(pszFormat, va);
|
---|
144 | va_end(va);
|
---|
145 | return fRc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Opens the output file for writing.
|
---|
151 | *
|
---|
152 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
|
---|
153 | * @param pszOutput Path to the output file.
|
---|
154 | */
|
---|
155 | static RTEXITCODE OpenOutputFile(const char *pszOutput)
|
---|
156 | {
|
---|
157 | if (!pszOutput)
|
---|
158 | g_hStrmOutput = g_pStdOut;
|
---|
159 | else
|
---|
160 | {
|
---|
161 | int rc = RTStrmOpen(pszOutput, "w", &g_hStrmOutput);
|
---|
162 | if (RT_FAILURE(rc))
|
---|
163 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open output file '%s': %Rrc", pszOutput, rc);
|
---|
164 | }
|
---|
165 | return RTEXITCODE_SUCCESS;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Displays a disassembly error and returns @c false.
|
---|
171 | *
|
---|
172 | * @returns @c false.
|
---|
173 | * @param pszFormat The error format string.
|
---|
174 | * @param ... Format argument.
|
---|
175 | */
|
---|
176 | static bool disError(const char *pszFormat, ...)
|
---|
177 | {
|
---|
178 | va_list va;
|
---|
179 | va_start(va, pszFormat);
|
---|
180 | RTMsgErrorV(pszFormat, va);
|
---|
181 | va_end(va);
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Output the disassembly file header.
|
---|
188 | *
|
---|
189 | * @returns @c true on success,
|
---|
190 | */
|
---|
191 | static bool disFileHeader(void)
|
---|
192 | {
|
---|
193 | bool fRc;
|
---|
194 | fRc = outputPrintf("; $Id: MakeAlternativeSource.cpp 50167 2014-01-22 19:47:00Z vboxsync $ \n"
|
---|
195 | ";; @file\n"
|
---|
196 | "; Auto Generated source file. Do not edit.\n"
|
---|
197 | ";\n"
|
---|
198 | );
|
---|
199 | if (!fRc)
|
---|
200 | return fRc;
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * List the header of each source file, up to and including the
|
---|
204 | * copyright notice.
|
---|
205 | */
|
---|
206 | bool fNeedLgplDisclaimer = false;
|
---|
207 | PBIOSOBJFILE pObjFile;
|
---|
208 | RTListForEach(&g_ObjList, pObjFile, BIOSOBJFILE, Node)
|
---|
209 | {
|
---|
210 | PRTSTREAM hStrm;
|
---|
211 | int rc = RTStrmOpen(pObjFile->pszSource, "r", &hStrm);
|
---|
212 | if (RT_SUCCESS(rc))
|
---|
213 | {
|
---|
214 | fRc = outputPrintf("\n"
|
---|
215 | ";\n"
|
---|
216 | "; Source file: %Rbn\n"
|
---|
217 | ";\n"
|
---|
218 | , pObjFile->pszSource);
|
---|
219 | uint32_t iLine = 0;
|
---|
220 | bool fSeenCopyright = false;
|
---|
221 | char szLine[4096];
|
---|
222 | while ((rc = RTStrmGetLine(hStrm, szLine, sizeof(szLine))) == VINF_SUCCESS)
|
---|
223 | {
|
---|
224 | iLine++;
|
---|
225 |
|
---|
226 | /* Check if we're done. */
|
---|
227 | char *psz = RTStrStrip(szLine);
|
---|
228 | if ( fSeenCopyright
|
---|
229 | && ( (psz[0] == '*' && psz[1] == '/')
|
---|
230 | || psz[0] == '\0') )
|
---|
231 | break;
|
---|
232 |
|
---|
233 | /* Strip comment suffix. */
|
---|
234 | size_t cch = strlen(psz);
|
---|
235 | if (cch >= 2 && psz[cch - 1] == '/' && psz[cch - 2] == '*')
|
---|
236 | {
|
---|
237 | psz[cch - 2] = '\0';
|
---|
238 | RTStrStripR(psz);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Skip line prefix. */
|
---|
242 | if (psz[0] == '/' && psz[1] == '*')
|
---|
243 | psz += 2;
|
---|
244 | else if (psz[0] == '*')
|
---|
245 | psz += 1;
|
---|
246 | else
|
---|
247 | while (*psz == ';')
|
---|
248 | psz++;
|
---|
249 | if (RT_C_IS_SPACE(*psz))
|
---|
250 | psz++;
|
---|
251 |
|
---|
252 | /* Skip the doxygen file tag line. */
|
---|
253 | if (!strcmp(psz, "* @file") || !strcmp(psz, "@file"))
|
---|
254 | continue;
|
---|
255 |
|
---|
256 | /* Detect copyright section. */
|
---|
257 | if ( !fSeenCopyright
|
---|
258 | && ( strstr(psz, "Copyright")
|
---|
259 | || strstr(psz, "copyright")) )
|
---|
260 | fSeenCopyright = true;
|
---|
261 |
|
---|
262 | /* Detect LGPL. */
|
---|
263 | if (strstr(psz, "LGPL"))
|
---|
264 | fNeedLgplDisclaimer = true;
|
---|
265 |
|
---|
266 | fRc = outputPrintf("; %s\n", psz) && fRc;
|
---|
267 | }
|
---|
268 |
|
---|
269 | RTStrmClose(hStrm);
|
---|
270 | if (rc != VINF_SUCCESS)
|
---|
271 | return disError("Error reading '%s': rc=%Rrc iLine=%u", pObjFile->pszSource, rc, iLine);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Add Oracle LGPL disclaimer.
|
---|
277 | */
|
---|
278 | if (fNeedLgplDisclaimer)
|
---|
279 | outputPrintf("\n"
|
---|
280 | ";\n"
|
---|
281 | "; Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice\n"
|
---|
282 | "; other than GPL or LGPL is available it will apply instead, Oracle elects to use only\n"
|
---|
283 | "; the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where\n"
|
---|
284 | "; a choice of LGPL license versions is made available with the language indicating\n"
|
---|
285 | "; that LGPLv2 or any later version may be used, or where a choice of which version\n"
|
---|
286 | "; of the LGPL is applied is otherwise unspecified.\n"
|
---|
287 | ";\n"
|
---|
288 | "\n");
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Set the org.
|
---|
292 | */
|
---|
293 | fRc = outputPrintf("\n"
|
---|
294 | "\n"
|
---|
295 | "\n"
|
---|
296 | ) && fRc;
|
---|
297 | return fRc;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Checks if a byte sequence could be a string litteral.
|
---|
303 | *
|
---|
304 | * @returns @c true if it is, @c false if it isn't.
|
---|
305 | * @param uFlatAddr The address of the byte sequence.
|
---|
306 | * @param cb The length of the sequence.
|
---|
307 | */
|
---|
308 | static bool disIsString(uint32_t uFlatAddr, uint32_t cb)
|
---|
309 | {
|
---|
310 | if (cb < 6)
|
---|
311 | return false;
|
---|
312 |
|
---|
313 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
314 | while (cb > 0)
|
---|
315 | {
|
---|
316 | if ( !RT_C_IS_PRINT(*pb)
|
---|
317 | && *pb != '\r'
|
---|
318 | && *pb != '\n'
|
---|
319 | && *pb != '\t')
|
---|
320 | {
|
---|
321 | if (*pb == '\0')
|
---|
322 | {
|
---|
323 | do
|
---|
324 | {
|
---|
325 | pb++;
|
---|
326 | cb--;
|
---|
327 | } while (cb > 0 && *pb == '\0');
|
---|
328 | return cb == 0;
|
---|
329 | }
|
---|
330 | return false;
|
---|
331 | }
|
---|
332 | pb++;
|
---|
333 | cb--;
|
---|
334 | }
|
---|
335 |
|
---|
336 | return true;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Checks if a dword could be a far 16:16 BIOS address.
|
---|
342 | *
|
---|
343 | * @returns @c true if it is, @c false if it isn't.
|
---|
344 | * @param uFlatAddr The address of the dword.
|
---|
345 | */
|
---|
346 | static bool disIsFarBiosAddr(uint32_t uFlatAddr)
|
---|
347 | {
|
---|
348 | uint16_t const *pu16 = (uint16_t const *)&g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
349 | if (pu16[1] < 0xf000)
|
---|
350 | return false;
|
---|
351 | if (pu16[1] > 0xfff0)
|
---|
352 | return false;
|
---|
353 | uint32_t uFlatAddr2 = (uint32_t)(pu16[1] << 4) | pu16[0];
|
---|
354 | if (uFlatAddr2 >= g_uBiosFlatBase + g_cbImg)
|
---|
355 | return false;
|
---|
356 | return true;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | static bool disByteData(uint32_t uFlatAddr, uint32_t cb)
|
---|
361 | {
|
---|
362 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
363 | size_t cbOnLine = 0;
|
---|
364 | while (cb-- > 0)
|
---|
365 | {
|
---|
366 | bool fRc;
|
---|
367 | if (cbOnLine >= 16)
|
---|
368 | {
|
---|
369 | fRc = outputPrintf("\n"
|
---|
370 | " db 0%02xh", *pb);
|
---|
371 | cbOnLine = 1;
|
---|
372 | }
|
---|
373 | else if (!cbOnLine)
|
---|
374 | {
|
---|
375 | fRc = outputPrintf(" db 0%02xh", *pb);
|
---|
376 | cbOnLine = 1;
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | fRc = outputPrintf(", 0%02xh", *pb);
|
---|
381 | cbOnLine++;
|
---|
382 | }
|
---|
383 | if (!fRc)
|
---|
384 | return false;
|
---|
385 | pb++;
|
---|
386 | }
|
---|
387 | return outputPrintf("\n");
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | static bool disWordData(uint32_t uFlatAddr, uint32_t cb)
|
---|
392 | {
|
---|
393 | if (cb & 1)
|
---|
394 | return disError("disWordData expects word aligned size: cb=%#x uFlatAddr=%#x", uFlatAddr, cb);
|
---|
395 |
|
---|
396 | uint16_t const *pu16 = (uint16_t const *)&g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
397 | size_t cbOnLine = 0;
|
---|
398 | while (cb > 0)
|
---|
399 | {
|
---|
400 | bool fRc;
|
---|
401 | if (cbOnLine >= 16)
|
---|
402 | {
|
---|
403 | fRc = outputPrintf("\n"
|
---|
404 | " dw 0%04xh", *pu16);
|
---|
405 | cbOnLine = 2;
|
---|
406 | }
|
---|
407 | else if (!cbOnLine)
|
---|
408 | {
|
---|
409 | fRc = outputPrintf(" dw 0%04xh", *pu16);
|
---|
410 | cbOnLine = 2;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | fRc = outputPrintf(", 0%04xh", *pu16);
|
---|
415 | cbOnLine += 2;
|
---|
416 | }
|
---|
417 | if (!fRc)
|
---|
418 | return false;
|
---|
419 | pu16++;
|
---|
420 | cb -= 2;
|
---|
421 | }
|
---|
422 | return outputPrintf("\n");
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | static bool disDWordData(uint32_t uFlatAddr, uint32_t cb)
|
---|
427 | {
|
---|
428 | if (cb & 3)
|
---|
429 | return disError("disWordData expects dword aligned size: cb=%#x uFlatAddr=%#x", uFlatAddr, cb);
|
---|
430 |
|
---|
431 | uint32_t const *pu32 = (uint32_t const *)&g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
432 | size_t cbOnLine = 0;
|
---|
433 | while (cb > 0)
|
---|
434 | {
|
---|
435 | bool fRc;
|
---|
436 | if (cbOnLine >= 16)
|
---|
437 | {
|
---|
438 | fRc = outputPrintf("\n"
|
---|
439 | " dd 0%08xh", *pu32);
|
---|
440 | cbOnLine = 4;
|
---|
441 | }
|
---|
442 | else if (!cbOnLine)
|
---|
443 | {
|
---|
444 | fRc = outputPrintf(" dd 0%08xh", *pu32);
|
---|
445 | cbOnLine = 4;
|
---|
446 | }
|
---|
447 | else
|
---|
448 | {
|
---|
449 | fRc = outputPrintf(", 0%08xh", *pu32);
|
---|
450 | cbOnLine += 4;
|
---|
451 | }
|
---|
452 | if (!fRc)
|
---|
453 | return false;
|
---|
454 | pu32++;
|
---|
455 | cb -= 4;
|
---|
456 | }
|
---|
457 | return outputPrintf("\n");
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | static bool disStringData(uint32_t uFlatAddr, uint32_t cb)
|
---|
462 | {
|
---|
463 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
464 | uint32_t cchOnLine = 0;
|
---|
465 | while (cb > 0)
|
---|
466 | {
|
---|
467 | /* Line endings and beginnings. */
|
---|
468 | if (cchOnLine >= 72)
|
---|
469 | {
|
---|
470 | if (!outputPrintf("\n"))
|
---|
471 | return false;
|
---|
472 | cchOnLine = 0;
|
---|
473 | }
|
---|
474 | if ( !cchOnLine
|
---|
475 | && !outputPrintf(" db "))
|
---|
476 | return false;
|
---|
477 |
|
---|
478 | /* See how many printable character we've got. */
|
---|
479 | uint32_t cchPrintable = 0;
|
---|
480 | while ( cchPrintable < cb
|
---|
481 | && RT_C_IS_PRINT(pb[cchPrintable])
|
---|
482 | && pb[cchPrintable] != '\'')
|
---|
483 | cchPrintable++;
|
---|
484 |
|
---|
485 | bool fRc = true;
|
---|
486 | if (cchPrintable)
|
---|
487 | {
|
---|
488 | if (cchPrintable + cchOnLine > 72)
|
---|
489 | cchPrintable = 72 - cchOnLine;
|
---|
490 | if (cchOnLine)
|
---|
491 | {
|
---|
492 | fRc = outputPrintf(", '%.*s'", cchPrintable, pb);
|
---|
493 | cchOnLine += 4 + cchPrintable;
|
---|
494 | }
|
---|
495 | else
|
---|
496 | {
|
---|
497 | fRc = outputPrintf("'%.*s'", cchPrintable, pb);
|
---|
498 | cchOnLine += 2 + cchPrintable;
|
---|
499 | }
|
---|
500 | pb += cchPrintable;
|
---|
501 | cb -= cchPrintable;
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | if (cchOnLine)
|
---|
506 | {
|
---|
507 | fRc = outputPrintf(", 0%02xh", *pb);
|
---|
508 | cchOnLine += 6;
|
---|
509 | }
|
---|
510 | else
|
---|
511 | {
|
---|
512 | fRc = outputPrintf("0%02xh", *pb);
|
---|
513 | cchOnLine += 4;
|
---|
514 | }
|
---|
515 | pb++;
|
---|
516 | cb--;
|
---|
517 | }
|
---|
518 | if (!fRc)
|
---|
519 | return false;
|
---|
520 | }
|
---|
521 | return outputPrintf("\n");
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * For dumping a portion of a string table.
|
---|
527 | *
|
---|
528 | * @returns @c true on success, @c false on failure.
|
---|
529 | * @param uFlatAddr The start address.
|
---|
530 | * @param cb The size of the string table.
|
---|
531 | */
|
---|
532 | static bool disStringsData(uint32_t uFlatAddr, uint32_t cb)
|
---|
533 | {
|
---|
534 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
535 | uint32_t cchOnLine = 0;
|
---|
536 | uint8_t bPrev = 255;
|
---|
537 | while (cb > 0)
|
---|
538 | {
|
---|
539 | /* Line endings and beginnings. */
|
---|
540 | if ( cchOnLine >= 72
|
---|
541 | || (bPrev == '\0' && *pb != '\0'))
|
---|
542 | {
|
---|
543 | if (!outputPrintf("\n"))
|
---|
544 | return false;
|
---|
545 | cchOnLine = 0;
|
---|
546 | }
|
---|
547 | if ( !cchOnLine
|
---|
548 | && !outputPrintf(" db "))
|
---|
549 | return false;
|
---|
550 |
|
---|
551 | /* See how many printable character we've got. */
|
---|
552 | uint32_t cchPrintable = 0;
|
---|
553 | while ( cchPrintable < cb
|
---|
554 | && RT_C_IS_PRINT(pb[cchPrintable])
|
---|
555 | && pb[cchPrintable] != '\'')
|
---|
556 | cchPrintable++;
|
---|
557 |
|
---|
558 | bool fRc = true;
|
---|
559 | if (cchPrintable)
|
---|
560 | {
|
---|
561 | if (cchPrintable + cchOnLine > 72)
|
---|
562 | cchPrintable = 72 - cchOnLine;
|
---|
563 | if (cchOnLine)
|
---|
564 | {
|
---|
565 | fRc = outputPrintf(", '%.*s'", cchPrintable, pb);
|
---|
566 | cchOnLine += 4 + cchPrintable;
|
---|
567 | }
|
---|
568 | else
|
---|
569 | {
|
---|
570 | fRc = outputPrintf("'%.*s'", cchPrintable, pb);
|
---|
571 | cchOnLine += 2 + cchPrintable;
|
---|
572 | }
|
---|
573 | pb += cchPrintable;
|
---|
574 | cb -= cchPrintable;
|
---|
575 | }
|
---|
576 | else
|
---|
577 | {
|
---|
578 | if (cchOnLine)
|
---|
579 | {
|
---|
580 | fRc = outputPrintf(", 0%02xh", *pb);
|
---|
581 | cchOnLine += 6;
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | fRc = outputPrintf("0%02xh", *pb);
|
---|
586 | cchOnLine += 4;
|
---|
587 | }
|
---|
588 | pb++;
|
---|
589 | cb--;
|
---|
590 | }
|
---|
591 | if (!fRc)
|
---|
592 | return false;
|
---|
593 | bPrev = pb[-1];
|
---|
594 | }
|
---|
595 | return outputPrintf("\n");
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Minds the gap between two segments.
|
---|
601 | *
|
---|
602 | * Gaps should generally be zero filled.
|
---|
603 | *
|
---|
604 | * @returns @c true on success, @c false on failure.
|
---|
605 | * @param uFlatAddr The address of the gap.
|
---|
606 | * @param cbPadding The size of the gap.
|
---|
607 | */
|
---|
608 | static bool disCopySegmentGap(uint32_t uFlatAddr, uint32_t cbPadding)
|
---|
609 | {
|
---|
610 | if (g_cVerbose > 0)
|
---|
611 | outputPrintf("\n"
|
---|
612 | " ; Padding %#x bytes at %#x\n", cbPadding, uFlatAddr);
|
---|
613 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
614 | if (!ASMMemIsAll8(pb, cbPadding, 0))
|
---|
615 | return outputPrintf(" times %u db 0\n", cbPadding);
|
---|
616 |
|
---|
617 | return disByteData(uFlatAddr, cbPadding);
|
---|
618 | }
|
---|
619 |
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Worker for disGetNextSymbol that only does the looking up, no RTDBSYMBOL::cb
|
---|
623 | * calc.
|
---|
624 | *
|
---|
625 | * @param uFlatAddr The address to start searching at.
|
---|
626 | * @param cbMax The size of the search range.
|
---|
627 | * @param poff Where to return the offset between the symbol
|
---|
628 | * and @a uFlatAddr.
|
---|
629 | * @param pSym Where to return the symbol data.
|
---|
630 | */
|
---|
631 | static void disGetNextSymbolWorker(uint32_t uFlatAddr, uint32_t cbMax, uint32_t *poff, PRTDBGSYMBOL pSym)
|
---|
632 | {
|
---|
633 | RTINTPTR off = 0;
|
---|
634 | int rc = RTDbgModSymbolByAddr(g_hMapMod, RTDBGSEGIDX_RVA, uFlatAddr, RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL, &off, pSym);
|
---|
635 | if (RT_SUCCESS(rc))
|
---|
636 | {
|
---|
637 | /* negative offset, indicates beyond. */
|
---|
638 | if (off <= 0)
|
---|
639 | {
|
---|
640 | *poff = (uint32_t)-off;
|
---|
641 | return;
|
---|
642 | }
|
---|
643 |
|
---|
644 | outputPrintf(" ; !! RTDbgModSymbolByAddr(,,%#x,,) -> off=%RTptr cb=%RTptr uValue=%RTptr '%s'\n",
|
---|
645 | uFlatAddr, off, pSym->cb, pSym->Value, pSym->szName);
|
---|
646 | }
|
---|
647 | else if (rc != VERR_SYMBOL_NOT_FOUND)
|
---|
648 | outputPrintf(" ; !! RTDbgModSymbolByAddr(,,%#x,,) -> %Rrc\n", uFlatAddr, rc);
|
---|
649 |
|
---|
650 | RTStrPrintf(pSym->szName, sizeof(pSym->szName), "_dummy_addr_%#x", uFlatAddr + cbMax);
|
---|
651 | pSym->Value = uFlatAddr + cbMax;
|
---|
652 | pSym->cb = 0;
|
---|
653 | pSym->offSeg = uFlatAddr + cbMax;
|
---|
654 | pSym->iSeg = RTDBGSEGIDX_RVA;
|
---|
655 | pSym->iOrdinal = 0;
|
---|
656 | pSym->fFlags = 0;
|
---|
657 | *poff = cbMax;
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Gets the symbol at or after the given address.
|
---|
663 | *
|
---|
664 | * If there are no symbols in the specified range, @a pSym and @a poff will be
|
---|
665 | * set up to indicate a symbol at the first byte after the range.
|
---|
666 | *
|
---|
667 | * @param uFlatAddr The address to start searching at.
|
---|
668 | * @param cbMax The size of the search range.
|
---|
669 | * @param poff Where to return the offset between the symbol
|
---|
670 | * and @a uFlatAddr.
|
---|
671 | * @param pSym Where to return the symbol data.
|
---|
672 | */
|
---|
673 | static void disGetNextSymbol(uint32_t uFlatAddr, uint32_t cbMax, uint32_t *poff, PRTDBGSYMBOL pSym)
|
---|
674 | {
|
---|
675 | disGetNextSymbolWorker(uFlatAddr, cbMax, poff, pSym);
|
---|
676 | if ( *poff < cbMax
|
---|
677 | && pSym->cb == 0)
|
---|
678 | {
|
---|
679 | if (*poff + 1 < cbMax)
|
---|
680 | {
|
---|
681 | uint32_t off2;
|
---|
682 | RTDBGSYMBOL Sym2;
|
---|
683 | disGetNextSymbolWorker(uFlatAddr + *poff + 1, cbMax - *poff - 1, &off2, &Sym2);
|
---|
684 | pSym->cb = off2 + 1;
|
---|
685 | }
|
---|
686 | else
|
---|
687 | pSym->cb = 1;
|
---|
688 | }
|
---|
689 | if (pSym->cb > cbMax - *poff)
|
---|
690 | pSym->cb = cbMax - *poff;
|
---|
691 |
|
---|
692 | if (g_cVerbose > 1)
|
---|
693 | outputPrintf(" ; disGetNextSymbol %#x LB %#x -> off=%#x cb=%RTptr uValue=%RTptr '%s'\n",
|
---|
694 | uFlatAddr, cbMax, *poff, pSym->cb, pSym->Value, pSym->szName);
|
---|
695 |
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * For dealing with the const segment (string constants).
|
---|
701 | *
|
---|
702 | * @returns @c true on success, @c false on failure.
|
---|
703 | * @param iSeg The segment.
|
---|
704 | */
|
---|
705 | static bool disConstSegment(uint32_t iSeg)
|
---|
706 | {
|
---|
707 | uint32_t uFlatAddr = g_aSegs[iSeg].uFlatAddr;
|
---|
708 | uint32_t cb = g_aSegs[iSeg].cb;
|
---|
709 |
|
---|
710 | while (cb > 0)
|
---|
711 | {
|
---|
712 | uint32_t off;
|
---|
713 | RTDBGSYMBOL Sym;
|
---|
714 | disGetNextSymbol(uFlatAddr, cb, &off, &Sym);
|
---|
715 |
|
---|
716 | if (off > 0)
|
---|
717 | {
|
---|
718 | if (!disStringsData(uFlatAddr, off))
|
---|
719 | return false;
|
---|
720 | cb -= off;
|
---|
721 | uFlatAddr += off;
|
---|
722 | off = 0;
|
---|
723 | if (!cb)
|
---|
724 | break;
|
---|
725 | }
|
---|
726 |
|
---|
727 | bool fRc;
|
---|
728 | if (off == 0)
|
---|
729 | {
|
---|
730 | size_t cchName = strlen(Sym.szName);
|
---|
731 | fRc = outputPrintf("%s: %*s; %#x LB %#x\n", Sym.szName, cchName < 41 - 2 ? cchName - 41 - 2 : 0, "", uFlatAddr, Sym.cb);
|
---|
732 | if (!fRc)
|
---|
733 | return false;
|
---|
734 | fRc = disStringsData(uFlatAddr, Sym.cb);
|
---|
735 | uFlatAddr += Sym.cb;
|
---|
736 | cb -= Sym.cb;
|
---|
737 | }
|
---|
738 | else
|
---|
739 | {
|
---|
740 | fRc = disStringsData(uFlatAddr, Sym.cb);
|
---|
741 | uFlatAddr += cb;
|
---|
742 | cb = 0;
|
---|
743 | }
|
---|
744 | if (!fRc)
|
---|
745 | return false;
|
---|
746 | }
|
---|
747 |
|
---|
748 | return true;
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 |
|
---|
753 | static bool disDataSegment(uint32_t iSeg)
|
---|
754 | {
|
---|
755 | uint32_t uFlatAddr = g_aSegs[iSeg].uFlatAddr;
|
---|
756 | uint32_t cb = g_aSegs[iSeg].cb;
|
---|
757 |
|
---|
758 | while (cb > 0)
|
---|
759 | {
|
---|
760 | uint32_t off;
|
---|
761 | RTDBGSYMBOL Sym;
|
---|
762 | disGetNextSymbol(uFlatAddr, cb, &off, &Sym);
|
---|
763 |
|
---|
764 | if (off > 0)
|
---|
765 | {
|
---|
766 | if (!disByteData(uFlatAddr, off))
|
---|
767 | return false;
|
---|
768 | cb -= off;
|
---|
769 | uFlatAddr += off;
|
---|
770 | off = 0;
|
---|
771 | if (!cb)
|
---|
772 | break;
|
---|
773 | }
|
---|
774 |
|
---|
775 | bool fRc;
|
---|
776 | if (off == 0)
|
---|
777 | {
|
---|
778 | size_t cchName = strlen(Sym.szName);
|
---|
779 | fRc = outputPrintf("%s: %*s; %#x LB %#x\n", Sym.szName, cchName < 41 - 2 ? cchName - 41 - 2 : 0, "", uFlatAddr, Sym.cb);
|
---|
780 | if (!fRc)
|
---|
781 | return false;
|
---|
782 |
|
---|
783 | if (Sym.cb == 2)
|
---|
784 | fRc = disWordData(uFlatAddr, 2);
|
---|
785 | //else if (Sym.cb == 4 && disIsFarBiosAddr(uFlatAddr))
|
---|
786 | // fRc = disDWordData(uFlatAddr, 4);
|
---|
787 | else if (Sym.cb == 4)
|
---|
788 | fRc = disDWordData(uFlatAddr, 4);
|
---|
789 | else if (disIsString(uFlatAddr, Sym.cb))
|
---|
790 | fRc = disStringData(uFlatAddr, Sym.cb);
|
---|
791 | else
|
---|
792 | fRc = disByteData(uFlatAddr, Sym.cb);
|
---|
793 |
|
---|
794 | uFlatAddr += Sym.cb;
|
---|
795 | cb -= Sym.cb;
|
---|
796 | }
|
---|
797 | else
|
---|
798 | {
|
---|
799 | fRc = disByteData(uFlatAddr, cb);
|
---|
800 | uFlatAddr += cb;
|
---|
801 | cb = 0;
|
---|
802 | }
|
---|
803 | if (!fRc)
|
---|
804 | return false;
|
---|
805 | }
|
---|
806 |
|
---|
807 | return true;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 | static bool disIsCodeAndAdjustSize(uint32_t uFlatAddr, PRTDBGSYMBOL pSym, PBIOSSEG pSeg)
|
---|
812 | {
|
---|
813 | switch (g_enmBiosType)
|
---|
814 | {
|
---|
815 | /*
|
---|
816 | * This is for the PC BIOS.
|
---|
817 | */
|
---|
818 | case kBiosType_System:
|
---|
819 | if (!strcmp(pSeg->szName, "BIOSSEG"))
|
---|
820 | {
|
---|
821 | if ( !strcmp(pSym->szName, "rom_fdpt")
|
---|
822 | || !strcmp(pSym->szName, "pmbios_gdt")
|
---|
823 | || !strcmp(pSym->szName, "pmbios_gdt_desc")
|
---|
824 | || !strcmp(pSym->szName, "_pmode_IDT")
|
---|
825 | || !strcmp(pSym->szName, "_rmode_IDT")
|
---|
826 | || !strncmp(pSym->szName, RT_STR_TUPLE("font"))
|
---|
827 | || !strcmp(pSym->szName, "bios_string")
|
---|
828 | || !strcmp(pSym->szName, "vector_table")
|
---|
829 | || !strcmp(pSym->szName, "pci_routing_table_structure")
|
---|
830 | || !strcmp(pSym->szName, "_pci_routing_table")
|
---|
831 | )
|
---|
832 | return false;
|
---|
833 | }
|
---|
834 |
|
---|
835 | if (!strcmp(pSym->szName, "cpu_reset"))
|
---|
836 | pSym->cb = RT_MIN(pSym->cb, 5);
|
---|
837 | else if (!strcmp(pSym->szName, "pci_init_end"))
|
---|
838 | pSym->cb = RT_MIN(pSym->cb, 3);
|
---|
839 | break;
|
---|
840 |
|
---|
841 | /*
|
---|
842 | * This is for the VGA BIOS.
|
---|
843 | */
|
---|
844 | case kBiosType_Vga:
|
---|
845 | break;
|
---|
846 | }
|
---|
847 |
|
---|
848 | return true;
|
---|
849 | }
|
---|
850 |
|
---|
851 |
|
---|
852 | static bool disIs16BitCode(const char *pszSymbol)
|
---|
853 | {
|
---|
854 | return true;
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Deals with instructions that YASM will assemble differently than WASM/WCC.
|
---|
860 | */
|
---|
861 | static size_t disHandleYasmDifferences(PDISCPUSTATE pCpuState, uint32_t uFlatAddr, uint32_t cbInstr,
|
---|
862 | char *pszBuf, size_t cbBuf, size_t cchUsed)
|
---|
863 | {
|
---|
864 | bool fDifferent = DISFormatYasmIsOddEncoding(pCpuState);
|
---|
865 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
866 |
|
---|
867 | /*
|
---|
868 | * Disassembler bugs.
|
---|
869 | */
|
---|
870 | /** @todo Group 1a and 11 seems to be disassembled incorrectly when
|
---|
871 | * modrm.reg != 0. Those encodings should be invalid AFAICT. */
|
---|
872 |
|
---|
873 | if ( ( pCpuState->bOpCode == 0x8f /* group 1a */
|
---|
874 | || pCpuState->bOpCode == 0xc7 /* group 11 */
|
---|
875 | || pCpuState->bOpCode == 0xc6 /* group 11 - not verified */
|
---|
876 | )
|
---|
877 | && pCpuState->ModRM.Bits.Reg != 0)
|
---|
878 | fDifferent = true;
|
---|
879 | /*
|
---|
880 | * Check these out and consider adding them to DISFormatYasmIsOddEncoding.
|
---|
881 | */
|
---|
882 | else if ( pb[0] == 0xf3
|
---|
883 | && pb[1] == 0x66
|
---|
884 | && pb[2] == 0x6d)
|
---|
885 | fDifferent = true; /* rep insd - prefix switched. */
|
---|
886 | else if ( pb[0] == 0xc6
|
---|
887 | && pb[1] == 0xc5
|
---|
888 | && pb[2] == 0xba)
|
---|
889 | fDifferent = true; /* mov ch, 0bah - yasm uses a short sequence: 0xb5 0xba. */
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * 32-bit retf.
|
---|
893 | */
|
---|
894 | else if ( pb[0] == 0x66
|
---|
895 | && pb[1] == 0xcb)
|
---|
896 | fDifferent = true;
|
---|
897 |
|
---|
898 | /*
|
---|
899 | * Handle different stuff.
|
---|
900 | */
|
---|
901 | if (fDifferent)
|
---|
902 | {
|
---|
903 | disByteData(uFlatAddr, cbInstr); /* lazy bird. */
|
---|
904 |
|
---|
905 | if (cchUsed + 2 < cbBuf)
|
---|
906 | {
|
---|
907 | memmove(pszBuf + 2, pszBuf, cchUsed + 1); /* include terminating \0 */
|
---|
908 | cchUsed += 2;
|
---|
909 | }
|
---|
910 |
|
---|
911 | pszBuf[0] = ';';
|
---|
912 | pszBuf[1] = ' ';
|
---|
913 | }
|
---|
914 |
|
---|
915 | return cchUsed;
|
---|
916 | }
|
---|
917 |
|
---|
918 |
|
---|
919 | /**
|
---|
920 | * @callback_method_impl{FNDISREADBYTES}
|
---|
921 | *
|
---|
922 | * @remarks @a uSrcAddr is the flat address.
|
---|
923 | */
|
---|
924 | static DECLCALLBACK(int) disReadOpcodeBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
925 | {
|
---|
926 | RTUINTPTR offBios = pDis->uInstrAddr + offInstr - g_uBiosFlatBase;
|
---|
927 | size_t cbToRead = cbMaxRead;
|
---|
928 | if (offBios + cbToRead > g_cbImg)
|
---|
929 | {
|
---|
930 | if (offBios >= g_cbImg)
|
---|
931 | cbToRead = 0;
|
---|
932 | else
|
---|
933 | cbToRead = g_cbImg - offBios;
|
---|
934 | }
|
---|
935 | memcpy(&pDis->abInstr[offInstr], &g_pbImg[offBios], cbToRead);
|
---|
936 | pDis->cbCachedInstr = (uint8_t)(offInstr + cbToRead);
|
---|
937 | return VINF_SUCCESS;
|
---|
938 | }
|
---|
939 |
|
---|
940 |
|
---|
941 | /**
|
---|
942 | * Disassembles code.
|
---|
943 | *
|
---|
944 | * @returns @c true on success, @c false on failure.
|
---|
945 | * @param uFlatAddr The address where the code starts.
|
---|
946 | * @param cb The amount of code to disassemble.
|
---|
947 | * @param fIs16Bit Is is 16-bit (@c true) or 32-bit (@c false).
|
---|
948 | */
|
---|
949 | static bool disCode(uint32_t uFlatAddr, uint32_t cb, bool fIs16Bit)
|
---|
950 | {
|
---|
951 | uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
|
---|
952 |
|
---|
953 | while (cb > 0)
|
---|
954 | {
|
---|
955 | /* Trailing zero padding detection. */
|
---|
956 | if ( *pb == '\0'
|
---|
957 | && ASMMemIsAll8(pb, RT_MIN(cb, 8), 0) == NULL)
|
---|
958 | {
|
---|
959 | void *pv = ASMMemIsAll8(pb, cb, 0);
|
---|
960 | uint32_t cbZeros = pv ? (uint32_t)((uint8_t const *)pv - pb) : cb;
|
---|
961 | if (!outputPrintf(" times %#x db 0\n", cbZeros))
|
---|
962 | return false;
|
---|
963 | cb -= cbZeros;
|
---|
964 | pb += cbZeros;
|
---|
965 | uFlatAddr += cbZeros;
|
---|
966 | if ( cb == 2
|
---|
967 | && pb[0] == 'X'
|
---|
968 | && pb[1] == 'M')
|
---|
969 | return disStringData(uFlatAddr, cb);
|
---|
970 | }
|
---|
971 | /* Work arounds for switch tables and such (disas assertions). */
|
---|
972 | else if ( 0
|
---|
973 | #if 0
|
---|
974 | || ( pb[0] == 0x11 /* int13_cdemu switch */
|
---|
975 | && pb[1] == 0xda
|
---|
976 | && pb[2] == 0x05
|
---|
977 | && pb[3] == 0xff
|
---|
978 | && pb[4] == 0xff
|
---|
979 | )
|
---|
980 | #endif
|
---|
981 | || ( pb[0] == 0xb0
|
---|
982 | && pb[1] == 0x58
|
---|
983 | && pb[2] == 0xc8
|
---|
984 | && pb[3] == 0x58
|
---|
985 | && pb[4] == 0xc8
|
---|
986 | && pb[5] == 0x58
|
---|
987 | )
|
---|
988 | || ( pb[0] == 0x50
|
---|
989 | && pb[1] == 0x4e
|
---|
990 | && pb[2] == 0x49
|
---|
991 | && pb[3] == 0x48
|
---|
992 | && pb[4] == 0x47
|
---|
993 | && pb[5] == 0x46
|
---|
994 | )
|
---|
995 | || ( pb[0] == 0x29
|
---|
996 | && pb[1] == 0x65
|
---|
997 | && pb[2] == 0x4b
|
---|
998 | && pb[3] == 0x65
|
---|
999 | && pb[4] == 0x6e
|
---|
1000 | && pb[5] == 0x65
|
---|
1001 | )
|
---|
1002 | || ( pb[0] == 0xc9 /* _pci16_function switch */
|
---|
1003 | && pb[1] == 0x8d
|
---|
1004 | && pb[2] == 0xe3
|
---|
1005 | && pb[3] == 0x8d
|
---|
1006 | && pb[4] == 0xf6
|
---|
1007 | && pb[5] == 0x8d
|
---|
1008 | )
|
---|
1009 | || ( pb[0] == 0xa3 /* _int1a_function switch */
|
---|
1010 | && pb[1] == 0x67
|
---|
1011 | && pb[2] == 0xca
|
---|
1012 | && pb[3] == 0x67
|
---|
1013 | && pb[4] == 0xef
|
---|
1014 | && pb[5] == 0x67
|
---|
1015 | )
|
---|
1016 | || ( pb[0] == 0x0b /* _ahci_init byte table */
|
---|
1017 | && pb[1] == 0x05
|
---|
1018 | && pb[2] == 0x04
|
---|
1019 | && pb[3] == 0x03
|
---|
1020 | && pb[4] == 0x02
|
---|
1021 | && pb[5] == 0x01
|
---|
1022 | )
|
---|
1023 | || ( pb[0] == 0x8c /* bytes after apm_out_str_ */
|
---|
1024 | && pb[1] == 0x2f
|
---|
1025 | && pb[2] == 0x8d
|
---|
1026 | && pb[3] == 0xbb
|
---|
1027 | && pb[4] == 0x8c
|
---|
1028 | && pb[5] == 0x2f)
|
---|
1029 | || ( pb[0] == 0x66
|
---|
1030 | && pb[1] == 0x9e
|
---|
1031 | && pb[2] == 0x66
|
---|
1032 | && pb[3] == 0xca
|
---|
1033 | && pb[4] == 0x66
|
---|
1034 | && pb[5] == 0x55)
|
---|
1035 | || 0
|
---|
1036 | )
|
---|
1037 | return disByteData(uFlatAddr, cb);
|
---|
1038 | else
|
---|
1039 | {
|
---|
1040 | unsigned cbInstr;
|
---|
1041 | DISCPUSTATE CpuState;
|
---|
1042 | int rc = DISInstrWithReader(uFlatAddr, fIs16Bit ? DISCPUMODE_16BIT : DISCPUMODE_32BIT,
|
---|
1043 | disReadOpcodeBytes, NULL, &CpuState, &cbInstr);
|
---|
1044 | if ( RT_SUCCESS(rc)
|
---|
1045 | && cbInstr <= cb
|
---|
1046 | && CpuState.pCurInstr
|
---|
1047 | && CpuState.pCurInstr->uOpcode != OP_INVALID)
|
---|
1048 | {
|
---|
1049 | char szTmp[4096];
|
---|
1050 | size_t cch = DISFormatYasmEx(&CpuState, szTmp, sizeof(szTmp),
|
---|
1051 | DIS_FMT_FLAGS_STRICT
|
---|
1052 | | DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_COMMENT | DIS_FMT_FLAGS_BYTES_SPACED,
|
---|
1053 | NULL, NULL);
|
---|
1054 | cch = disHandleYasmDifferences(&CpuState, uFlatAddr, cbInstr, szTmp, sizeof(szTmp), cch);
|
---|
1055 | Assert(cch < sizeof(szTmp));
|
---|
1056 |
|
---|
1057 | if (g_cVerbose > 1)
|
---|
1058 | {
|
---|
1059 | while (cch < 72)
|
---|
1060 | szTmp[cch++] = ' ';
|
---|
1061 | RTStrPrintf(&szTmp[cch], sizeof(szTmp) - cch, "; %#x", uFlatAddr);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | if (!outputPrintf(" %s\n", szTmp))
|
---|
1065 | return false;
|
---|
1066 | cb -= cbInstr;
|
---|
1067 | pb += cbInstr;
|
---|
1068 | uFlatAddr += cbInstr;
|
---|
1069 | }
|
---|
1070 | else
|
---|
1071 | {
|
---|
1072 | if (!disByteData(uFlatAddr, 1))
|
---|
1073 | return false;
|
---|
1074 | cb--;
|
---|
1075 | pb++;
|
---|
1076 | uFlatAddr++;
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 | return true;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 |
|
---|
1084 | static bool disCodeSegment(uint32_t iSeg)
|
---|
1085 | {
|
---|
1086 | uint32_t uFlatAddr = g_aSegs[iSeg].uFlatAddr;
|
---|
1087 | uint32_t cb = g_aSegs[iSeg].cb;
|
---|
1088 |
|
---|
1089 | while (cb > 0)
|
---|
1090 | {
|
---|
1091 | uint32_t off;
|
---|
1092 | RTDBGSYMBOL Sym;
|
---|
1093 | disGetNextSymbol(uFlatAddr, cb, &off, &Sym);
|
---|
1094 |
|
---|
1095 | if (off > 0)
|
---|
1096 | {
|
---|
1097 | if (!disByteData(uFlatAddr, off))
|
---|
1098 | return false;
|
---|
1099 | cb -= off;
|
---|
1100 | uFlatAddr += off;
|
---|
1101 | off = 0;
|
---|
1102 | if (!cb)
|
---|
1103 | break;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | bool fRc;
|
---|
1107 | if (off == 0)
|
---|
1108 | {
|
---|
1109 | size_t cchName = strlen(Sym.szName);
|
---|
1110 | fRc = outputPrintf("%s: %*s; %#x LB %#x\n", Sym.szName, cchName < 41 - 2 ? cchName - 41 - 2 : 0, "", uFlatAddr, Sym.cb);
|
---|
1111 | if (!fRc)
|
---|
1112 | return false;
|
---|
1113 |
|
---|
1114 | if (disIsCodeAndAdjustSize(uFlatAddr, &Sym, &g_aSegs[iSeg]))
|
---|
1115 | fRc = disCode(uFlatAddr, Sym.cb, disIs16BitCode(Sym.szName));
|
---|
1116 | else
|
---|
1117 | fRc = disByteData(uFlatAddr, Sym.cb);
|
---|
1118 |
|
---|
1119 | uFlatAddr += Sym.cb;
|
---|
1120 | cb -= Sym.cb;
|
---|
1121 | }
|
---|
1122 | else
|
---|
1123 | {
|
---|
1124 | fRc = disByteData(uFlatAddr, cb);
|
---|
1125 | uFlatAddr += cb;
|
---|
1126 | cb = 0;
|
---|
1127 | }
|
---|
1128 | if (!fRc)
|
---|
1129 | return false;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | return true;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 |
|
---|
1136 | static RTEXITCODE DisassembleBiosImage(void)
|
---|
1137 | {
|
---|
1138 | if (!disFileHeader())
|
---|
1139 | return RTEXITCODE_FAILURE;
|
---|
1140 |
|
---|
1141 | /*
|
---|
1142 | * Work the image segment by segment.
|
---|
1143 | */
|
---|
1144 | bool fRc = true;
|
---|
1145 | uint32_t uFlatAddr = g_uBiosFlatBase;
|
---|
1146 | for (uint32_t iSeg = 0; iSeg < g_cSegs && fRc; iSeg++)
|
---|
1147 | {
|
---|
1148 | /* Is there a gap between the segments? */
|
---|
1149 | if (uFlatAddr < g_aSegs[iSeg].uFlatAddr)
|
---|
1150 | {
|
---|
1151 | fRc = disCopySegmentGap(uFlatAddr, g_aSegs[iSeg].uFlatAddr - uFlatAddr);
|
---|
1152 | if (!fRc)
|
---|
1153 | break;
|
---|
1154 | uFlatAddr = g_aSegs[iSeg].uFlatAddr;
|
---|
1155 | }
|
---|
1156 | else if (uFlatAddr > g_aSegs[iSeg].uFlatAddr)
|
---|
1157 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Overlapping segments: %u and %u; uFlatAddr=%#x\n", iSeg - 1, iSeg, uFlatAddr);
|
---|
1158 |
|
---|
1159 | /* Disassemble the segment. */
|
---|
1160 | fRc = outputPrintf("\n"
|
---|
1161 | "section %s progbits vstart=%#x align=1 ; size=%#x class=%s group=%s\n",
|
---|
1162 | g_aSegs[iSeg].szName, g_aSegs[iSeg].uFlatAddr - g_uBiosFlatBase,
|
---|
1163 | g_aSegs[iSeg].cb, g_aSegs[iSeg].szClass, g_aSegs[iSeg].szGroup);
|
---|
1164 | if (!fRc)
|
---|
1165 | return RTEXITCODE_FAILURE;
|
---|
1166 | if (!strcmp(g_aSegs[iSeg].szName, "CONST"))
|
---|
1167 | fRc = disConstSegment(iSeg);
|
---|
1168 | else if (!strcmp(g_aSegs[iSeg].szClass, "DATA"))
|
---|
1169 | fRc = disDataSegment(iSeg);
|
---|
1170 | else
|
---|
1171 | fRc = disCodeSegment(iSeg);
|
---|
1172 |
|
---|
1173 | /* Advance. */
|
---|
1174 | uFlatAddr += g_aSegs[iSeg].cb;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | /* Final gap. */
|
---|
1178 | if (uFlatAddr < g_uBiosFlatBase + g_cbImg)
|
---|
1179 | fRc = disCopySegmentGap(uFlatAddr, (uint32_t)(g_uBiosFlatBase + g_cbImg - uFlatAddr));
|
---|
1180 | else if (uFlatAddr > g_uBiosFlatBase + g_cbImg)
|
---|
1181 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Last segment spills beyond 1MB; uFlatAddr=%#x\n", uFlatAddr);
|
---|
1182 |
|
---|
1183 | if (!fRc)
|
---|
1184 | return RTEXITCODE_FAILURE;
|
---|
1185 | return RTEXITCODE_SUCCESS;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 |
|
---|
1189 |
|
---|
1190 | /**
|
---|
1191 | * Parses the symbol file for the BIOS.
|
---|
1192 | *
|
---|
1193 | * This is in ELF/DWARF format.
|
---|
1194 | *
|
---|
1195 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
|
---|
1196 | * @param pszBiosSym Path to the sym file.
|
---|
1197 | */
|
---|
1198 | static RTEXITCODE ParseSymFile(const char *pszBiosSym)
|
---|
1199 | {
|
---|
1200 | #if 1
|
---|
1201 | /** @todo use RTDbg* later. (Just checking for existance currently.) */
|
---|
1202 | PRTSTREAM hStrm;
|
---|
1203 | int rc = RTStrmOpen(pszBiosSym, "rb", &hStrm);
|
---|
1204 | if (RT_FAILURE(rc))
|
---|
1205 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening '%s': %Rrc", pszBiosSym, rc);
|
---|
1206 | RTStrmClose(hStrm);
|
---|
1207 | #else
|
---|
1208 | RTDBGMOD hDbgMod;
|
---|
1209 | int rc = RTDbgModCreateFromImage(&hDbgMod, pszBiosSym, "VBoxBios", 0 /*fFlags*/);
|
---|
1210 | RTMsgInfo("RTDbgModCreateFromImage -> %Rrc\n", rc);
|
---|
1211 | #endif
|
---|
1212 | return RTEXITCODE_SUCCESS;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 |
|
---|
1216 | /**
|
---|
1217 | * Display an error with the mapfile name and current line, return false.
|
---|
1218 | *
|
---|
1219 | * @returns @c false.
|
---|
1220 | * @param pMap The map file handle.
|
---|
1221 | * @param pszFormat The format string.
|
---|
1222 | * @param ... Format arguments.
|
---|
1223 | */
|
---|
1224 | static bool mapError(PBIOSMAP pMap, const char *pszFormat, ...)
|
---|
1225 | {
|
---|
1226 | va_list va;
|
---|
1227 | va_start(va, pszFormat);
|
---|
1228 | RTMsgError("%s:%d: %N", pMap->pszMapFile, pMap->iLine, pszFormat, va);
|
---|
1229 | va_end(va);
|
---|
1230 | return false;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 |
|
---|
1234 | /**
|
---|
1235 | * Reads a line from the file.
|
---|
1236 | *
|
---|
1237 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1238 | * @param pMap The map file handle.
|
---|
1239 | */
|
---|
1240 | static bool mapReadLine(PBIOSMAP pMap)
|
---|
1241 | {
|
---|
1242 | int rc = RTStrmGetLine(pMap->hStrm, pMap->szLine, sizeof(pMap->szLine));
|
---|
1243 | if (RT_FAILURE(rc))
|
---|
1244 | {
|
---|
1245 | if (rc == VERR_EOF)
|
---|
1246 | {
|
---|
1247 | pMap->fEof = true;
|
---|
1248 | pMap->cch = 0;
|
---|
1249 | pMap->offNW = 0;
|
---|
1250 | pMap->szLine[0] = '\0';
|
---|
1251 | }
|
---|
1252 | else
|
---|
1253 | RTMsgError("%s:%d: Read error %Rrc", pMap->pszMapFile, pMap->iLine + 1, rc);
|
---|
1254 | return false;
|
---|
1255 | }
|
---|
1256 | pMap->iLine++;
|
---|
1257 | pMap->cch = (uint32_t)strlen(pMap->szLine);
|
---|
1258 |
|
---|
1259 | /* Check out leading white space. */
|
---|
1260 | if (!RT_C_IS_SPACE(pMap->szLine[0]))
|
---|
1261 | pMap->offNW = 0;
|
---|
1262 | else
|
---|
1263 | {
|
---|
1264 | uint32_t off = 1;
|
---|
1265 | while (RT_C_IS_SPACE(pMap->szLine[off]))
|
---|
1266 | off++;
|
---|
1267 | pMap->offNW = off;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | return true;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * Checks if it is an empty line.
|
---|
1276 | * @returns @c true if empty, @c false if not.
|
---|
1277 | * @param pMap The map file handle.
|
---|
1278 | */
|
---|
1279 | static bool mapIsEmptyLine(PBIOSMAP pMap)
|
---|
1280 | {
|
---|
1281 | Assert(pMap->offNW <= pMap->cch);
|
---|
1282 | return pMap->offNW == pMap->cch;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 |
|
---|
1286 | /**
|
---|
1287 | * Reads ahead in the map file until a non-empty line or EOF is encountered.
|
---|
1288 | *
|
---|
1289 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1290 | * @param pMap The map file handle.
|
---|
1291 | */
|
---|
1292 | static bool mapSkipEmptyLines(PBIOSMAP pMap)
|
---|
1293 | {
|
---|
1294 | for (;;)
|
---|
1295 | {
|
---|
1296 | if (!mapReadLine(pMap))
|
---|
1297 | return false;
|
---|
1298 | if (pMap->offNW < pMap->cch)
|
---|
1299 | return true;
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | * Reads ahead in the map file until an empty line or EOF is encountered.
|
---|
1306 | *
|
---|
1307 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1308 | * @param pMap The map file handle.
|
---|
1309 | */
|
---|
1310 | static bool mapSkipNonEmptyLines(PBIOSMAP pMap)
|
---|
1311 | {
|
---|
1312 | for (;;)
|
---|
1313 | {
|
---|
1314 | if (!mapReadLine(pMap))
|
---|
1315 | return false;
|
---|
1316 | if (pMap->offNW == pMap->cch)
|
---|
1317 | return true;
|
---|
1318 | }
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | /**
|
---|
1323 | * Strips the current line.
|
---|
1324 | *
|
---|
1325 | * The string length may change.
|
---|
1326 | *
|
---|
1327 | * @returns Pointer to the first non-space character.
|
---|
1328 | * @param pMap The map file handle.
|
---|
1329 | * @param pcch Where to return the length of the unstripped
|
---|
1330 | * part. Optional.
|
---|
1331 | */
|
---|
1332 | static char *mapStripCurrentLine(PBIOSMAP pMap, size_t *pcch)
|
---|
1333 | {
|
---|
1334 | char *psz = &pMap->szLine[pMap->offNW];
|
---|
1335 | char *pszEnd = &pMap->szLine[pMap->cch];
|
---|
1336 | while ( (uintptr_t)pszEnd > (uintptr_t)psz
|
---|
1337 | && RT_C_IS_SPACE(pszEnd[-1]))
|
---|
1338 | {
|
---|
1339 | *--pszEnd = '\0';
|
---|
1340 | pMap->cch--;
|
---|
1341 | }
|
---|
1342 | if (pcch)
|
---|
1343 | *pcch = pszEnd - psz;
|
---|
1344 | return psz;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 |
|
---|
1348 | /**
|
---|
1349 | * Reads a line from the file and right strips it.
|
---|
1350 | *
|
---|
1351 | * @returns Pointer to szLine on success, @c NULL + msg on failure, @c NULL on
|
---|
1352 | * EOF.
|
---|
1353 | * @param pMap The map file handle.
|
---|
1354 | * @param pcch Where to return the length of the unstripped
|
---|
1355 | * part. Optional.
|
---|
1356 | */
|
---|
1357 | static char *mapReadLineStripRight(PBIOSMAP pMap, size_t *pcch)
|
---|
1358 | {
|
---|
1359 | if (!mapReadLine(pMap))
|
---|
1360 | return NULL;
|
---|
1361 | mapStripCurrentLine(pMap, NULL);
|
---|
1362 | if (pcch)
|
---|
1363 | *pcch = pMap->cch;
|
---|
1364 | return pMap->szLine;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 |
|
---|
1368 | /**
|
---|
1369 | * mapReadLine() + mapStripCurrentLine().
|
---|
1370 | *
|
---|
1371 | * @returns Pointer to the first non-space character in the new line. NULL on
|
---|
1372 | * read error (bitched already) or end of file.
|
---|
1373 | * @param pMap The map file handle.
|
---|
1374 | * @param pcch Where to return the length of the unstripped
|
---|
1375 | * part. Optional.
|
---|
1376 | */
|
---|
1377 | static char *mapReadLineStrip(PBIOSMAP pMap, size_t *pcch)
|
---|
1378 | {
|
---|
1379 | if (!mapReadLine(pMap))
|
---|
1380 | return NULL;
|
---|
1381 | return mapStripCurrentLine(pMap, pcch);
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 |
|
---|
1385 | /**
|
---|
1386 | * Parses a word, copying it into the supplied buffer, and skipping any spaces
|
---|
1387 | * following it.
|
---|
1388 | *
|
---|
1389 | * @returns @c true on success, @c false on failure.
|
---|
1390 | * @param ppszCursor Pointer to the cursor variable.
|
---|
1391 | * @param pszBuf The output buffer.
|
---|
1392 | * @param cbBuf The size of the output buffer.
|
---|
1393 | */
|
---|
1394 | static bool mapParseWord(char **ppszCursor, char *pszBuf, size_t cbBuf)
|
---|
1395 | {
|
---|
1396 | /* Check that we start on a non-blank. */
|
---|
1397 | char *pszStart = *ppszCursor;
|
---|
1398 | if (!*pszStart || RT_C_IS_SPACE(*pszStart))
|
---|
1399 | return false;
|
---|
1400 |
|
---|
1401 | /* Find the end of the word. */
|
---|
1402 | char *psz = pszStart + 1;
|
---|
1403 | while (*psz && !RT_C_IS_SPACE(*psz))
|
---|
1404 | psz++;
|
---|
1405 |
|
---|
1406 | /* Copy it. */
|
---|
1407 | size_t cchWord = (uintptr_t)psz - (uintptr_t)pszStart;
|
---|
1408 | if (cchWord >= cbBuf)
|
---|
1409 | return false;
|
---|
1410 | memcpy(pszBuf, pszStart, cchWord);
|
---|
1411 | pszBuf[cchWord] = '\0';
|
---|
1412 |
|
---|
1413 | /* Skip blanks following it. */
|
---|
1414 | while (RT_C_IS_SPACE(*psz))
|
---|
1415 | psz++;
|
---|
1416 | *ppszCursor = psz;
|
---|
1417 | return true;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | /**
|
---|
1422 | * Parses an 16:16 address.
|
---|
1423 | *
|
---|
1424 | * @returns @c true on success, @c false on failure.
|
---|
1425 | * @param ppszCursor Pointer to the cursor variable.
|
---|
1426 | * @param pAddr Where to return the address.
|
---|
1427 | */
|
---|
1428 | static bool mapParseAddress(char **ppszCursor, PRTFAR16 pAddr)
|
---|
1429 | {
|
---|
1430 | char szWord[32];
|
---|
1431 | if (!mapParseWord(ppszCursor, szWord, sizeof(szWord)))
|
---|
1432 | return false;
|
---|
1433 | size_t cchWord = strlen(szWord);
|
---|
1434 |
|
---|
1435 | /* An address is at least 16:16 format. It may be 16:32. It may also be flagged. */
|
---|
1436 | size_t cchAddr = 4 + 1 + 4;
|
---|
1437 | if (cchWord < cchAddr)
|
---|
1438 | return false;
|
---|
1439 | if ( !RT_C_IS_XDIGIT(szWord[0])
|
---|
1440 | || !RT_C_IS_XDIGIT(szWord[1])
|
---|
1441 | || !RT_C_IS_XDIGIT(szWord[2])
|
---|
1442 | || !RT_C_IS_XDIGIT(szWord[3])
|
---|
1443 | || szWord[4] != ':'
|
---|
1444 | || !RT_C_IS_XDIGIT(szWord[5])
|
---|
1445 | || !RT_C_IS_XDIGIT(szWord[6])
|
---|
1446 | || !RT_C_IS_XDIGIT(szWord[7])
|
---|
1447 | || !RT_C_IS_XDIGIT(szWord[8])
|
---|
1448 | )
|
---|
1449 | return false;
|
---|
1450 | if ( cchWord > cchAddr
|
---|
1451 | && RT_C_IS_XDIGIT(szWord[9])
|
---|
1452 | && RT_C_IS_XDIGIT(szWord[10])
|
---|
1453 | && RT_C_IS_XDIGIT(szWord[11])
|
---|
1454 | && RT_C_IS_XDIGIT(szWord[12]))
|
---|
1455 | cchAddr += 4;
|
---|
1456 |
|
---|
1457 | /* Drop flag if present. */
|
---|
1458 | if (cchWord > cchAddr)
|
---|
1459 | {
|
---|
1460 | if (RT_C_IS_XDIGIT(szWord[cchAddr]))
|
---|
1461 | return false;
|
---|
1462 | szWord[cchAddr] = '\0';
|
---|
1463 | cchWord = cchAddr;
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | /* Convert it. */
|
---|
1467 | szWord[4] = '\0';
|
---|
1468 | int rc1 = RTStrToUInt16Full(szWord, 16, &pAddr->sel);
|
---|
1469 | if (rc1 != VINF_SUCCESS)
|
---|
1470 | return false;
|
---|
1471 |
|
---|
1472 | int rc2 = RTStrToUInt16Full(szWord + 5, 16, &pAddr->off);
|
---|
1473 | if (rc2 != VINF_SUCCESS)
|
---|
1474 | return false;
|
---|
1475 | return true;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 |
|
---|
1479 | /**
|
---|
1480 | * Parses a size.
|
---|
1481 | *
|
---|
1482 | * @returns @c true on success, @c false on failure.
|
---|
1483 | * @param ppszCursor Pointer to the cursor variable.
|
---|
1484 | * @param pcb Where to return the size.
|
---|
1485 | */
|
---|
1486 | static bool mapParseSize(char **ppszCursor, uint32_t *pcb)
|
---|
1487 | {
|
---|
1488 | char szWord[32];
|
---|
1489 | if (!mapParseWord(ppszCursor, szWord, sizeof(szWord)))
|
---|
1490 | return false;
|
---|
1491 | size_t cchWord = strlen(szWord);
|
---|
1492 | if (cchWord != 8)
|
---|
1493 | return false;
|
---|
1494 |
|
---|
1495 | int rc = RTStrToUInt32Full(szWord, 16, pcb);
|
---|
1496 | if (rc != VINF_SUCCESS)
|
---|
1497 | return false;
|
---|
1498 | return true;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 |
|
---|
1502 | /**
|
---|
1503 | * Parses a section box and the following column header.
|
---|
1504 | *
|
---|
1505 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1506 | * @param pMap Map file handle.
|
---|
1507 | * @param pszSectionNm The expected section name.
|
---|
1508 | * @param cColumns The number of columns.
|
---|
1509 | * @param ... The column names.
|
---|
1510 | */
|
---|
1511 | static bool mapSkipThruColumnHeadings(PBIOSMAP pMap, const char *pszSectionNm, uint32_t cColumns, ...)
|
---|
1512 | {
|
---|
1513 | if ( mapIsEmptyLine(pMap)
|
---|
1514 | && !mapSkipEmptyLines(pMap))
|
---|
1515 | return false;
|
---|
1516 |
|
---|
1517 | /* +------------+ */
|
---|
1518 | size_t cch;
|
---|
1519 | char *psz = mapStripCurrentLine(pMap, &cch);
|
---|
1520 | if (!psz)
|
---|
1521 | return false;
|
---|
1522 |
|
---|
1523 | if ( psz[0] != '+'
|
---|
1524 | || psz[1] != '-'
|
---|
1525 | || psz[2] != '-'
|
---|
1526 | || psz[3] != '-'
|
---|
1527 | || psz[cch - 4] != '-'
|
---|
1528 | || psz[cch - 3] != '-'
|
---|
1529 | || psz[cch - 2] != '-'
|
---|
1530 | || psz[cch - 1] != '+'
|
---|
1531 | )
|
---|
1532 | {
|
---|
1533 | RTMsgError("%s:%d: Expected section box: +-----...", pMap->pszMapFile, pMap->iLine);
|
---|
1534 | return false;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | /* | pszSectionNm | */
|
---|
1538 | psz = mapReadLineStrip(pMap, &cch);
|
---|
1539 | if (!psz)
|
---|
1540 | return false;
|
---|
1541 |
|
---|
1542 | size_t cchSectionNm = strlen(pszSectionNm);
|
---|
1543 | if ( psz[0] != '|'
|
---|
1544 | || psz[1] != ' '
|
---|
1545 | || psz[2] != ' '
|
---|
1546 | || psz[3] != ' '
|
---|
1547 | || psz[cch - 4] != ' '
|
---|
1548 | || psz[cch - 3] != ' '
|
---|
1549 | || psz[cch - 2] != ' '
|
---|
1550 | || psz[cch - 1] != '|'
|
---|
1551 | || cch != 1 + 3 + cchSectionNm + 3 + 1
|
---|
1552 | || strncmp(&psz[4], pszSectionNm, cchSectionNm)
|
---|
1553 | )
|
---|
1554 | {
|
---|
1555 | RTMsgError("%s:%d: Expected section box: | %s |", pMap->pszMapFile, pMap->iLine, pszSectionNm);
|
---|
1556 | return false;
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | /* +------------+ */
|
---|
1560 | psz = mapReadLineStrip(pMap, &cch);
|
---|
1561 | if (!psz)
|
---|
1562 | return false;
|
---|
1563 | if ( psz[0] != '+'
|
---|
1564 | || psz[1] != '-'
|
---|
1565 | || psz[2] != '-'
|
---|
1566 | || psz[3] != '-'
|
---|
1567 | || psz[cch - 4] != '-'
|
---|
1568 | || psz[cch - 3] != '-'
|
---|
1569 | || psz[cch - 2] != '-'
|
---|
1570 | || psz[cch - 1] != '+'
|
---|
1571 | )
|
---|
1572 | {
|
---|
1573 | RTMsgError("%s:%d: Expected section box: +-----...", pMap->pszMapFile, pMap->iLine);
|
---|
1574 | return false;
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | /* There may be a few lines describing the table notation now, surrounded by blank lines. */
|
---|
1578 | do
|
---|
1579 | {
|
---|
1580 | psz = mapReadLineStripRight(pMap, &cch);
|
---|
1581 | if (!psz)
|
---|
1582 | return false;
|
---|
1583 | } while ( *psz == '\0'
|
---|
1584 | || ( !RT_C_IS_SPACE(psz[0])
|
---|
1585 | && RT_C_IS_SPACE(psz[1])
|
---|
1586 | && psz[2] == '='
|
---|
1587 | && RT_C_IS_SPACE(psz[3]))
|
---|
1588 | );
|
---|
1589 |
|
---|
1590 | /* Should have the column heading now. */
|
---|
1591 | va_list va;
|
---|
1592 | va_start(va, cColumns);
|
---|
1593 | for (uint32_t i = 0; i < cColumns; i++)
|
---|
1594 | {
|
---|
1595 | const char *pszColumn = va_arg(va, const char *);
|
---|
1596 | size_t cchColumn = strlen(pszColumn);
|
---|
1597 | if ( strncmp(psz, pszColumn, cchColumn)
|
---|
1598 | || ( psz[cchColumn] != '\0'
|
---|
1599 | && !RT_C_IS_SPACE(psz[cchColumn])))
|
---|
1600 | {
|
---|
1601 | va_end(va);
|
---|
1602 | RTMsgError("%s:%d: Expected column '%s' found '%s'", pMap->pszMapFile, pMap->iLine, pszColumn, psz);
|
---|
1603 | return false;
|
---|
1604 | }
|
---|
1605 | psz += cchColumn;
|
---|
1606 | while (RT_C_IS_SPACE(*psz))
|
---|
1607 | psz++;
|
---|
1608 | }
|
---|
1609 | va_end(va);
|
---|
1610 |
|
---|
1611 | /* The next line is the underlining. */
|
---|
1612 | psz = mapReadLineStripRight(pMap, &cch);
|
---|
1613 | if (!psz)
|
---|
1614 | return false;
|
---|
1615 | if (*psz != '=' || psz[cch - 1] != '=')
|
---|
1616 | {
|
---|
1617 | RTMsgError("%s:%d: Expected column header underlining", pMap->pszMapFile, pMap->iLine);
|
---|
1618 | return false;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | /* Skip one blank line. */
|
---|
1622 | psz = mapReadLineStripRight(pMap, &cch);
|
---|
1623 | if (!psz)
|
---|
1624 | return false;
|
---|
1625 | if (*psz)
|
---|
1626 | {
|
---|
1627 | RTMsgError("%s:%d: Expected blank line beneath the column headers", pMap->pszMapFile, pMap->iLine);
|
---|
1628 | return false;
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | return true;
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 |
|
---|
1635 | /**
|
---|
1636 | * Parses a segment list.
|
---|
1637 | *
|
---|
1638 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1639 | * @param pMap The map file handle.
|
---|
1640 | */
|
---|
1641 | static bool mapParseSegments(PBIOSMAP pMap)
|
---|
1642 | {
|
---|
1643 | for (;;)
|
---|
1644 | {
|
---|
1645 | if (!mapReadLineStripRight(pMap, NULL))
|
---|
1646 | return false;
|
---|
1647 |
|
---|
1648 | /* The end? The line should be empty. Expectes segment name to not
|
---|
1649 | start with a space. */
|
---|
1650 | if (!pMap->szLine[0] || RT_C_IS_SPACE(pMap->szLine[0]))
|
---|
1651 | {
|
---|
1652 | if (!pMap->szLine[0])
|
---|
1653 | return true;
|
---|
1654 | RTMsgError("%s:%u: Malformed segment line", pMap->pszMapFile, pMap->iLine);
|
---|
1655 | return false;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | /* Parse the segment line. */
|
---|
1659 | uint32_t iSeg = g_cSegs;
|
---|
1660 | if (iSeg >= RT_ELEMENTS(g_aSegs))
|
---|
1661 | {
|
---|
1662 | RTMsgError("%s:%u: Too many segments", pMap->pszMapFile, pMap->iLine);
|
---|
1663 | return false;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | char *psz = pMap->szLine;
|
---|
1667 | if (!mapParseWord(&psz, g_aSegs[iSeg].szName, sizeof(g_aSegs[iSeg].szName)))
|
---|
1668 | RTMsgError("%s:%u: Segment name parser error", pMap->pszMapFile, pMap->iLine);
|
---|
1669 | else if (!mapParseWord(&psz, g_aSegs[iSeg].szClass, sizeof(g_aSegs[iSeg].szClass)))
|
---|
1670 | RTMsgError("%s:%u: Segment class parser error", pMap->pszMapFile, pMap->iLine);
|
---|
1671 | else if (!mapParseWord(&psz, g_aSegs[iSeg].szGroup, sizeof(g_aSegs[iSeg].szGroup)))
|
---|
1672 | RTMsgError("%s:%u: Segment group parser error", pMap->pszMapFile, pMap->iLine);
|
---|
1673 | else if (!mapParseAddress(&psz, &g_aSegs[iSeg].Address))
|
---|
1674 | RTMsgError("%s:%u: Segment address parser error", pMap->pszMapFile, pMap->iLine);
|
---|
1675 | else if (!mapParseSize(&psz, &g_aSegs[iSeg].cb))
|
---|
1676 | RTMsgError("%s:%u: Segment size parser error", pMap->pszMapFile, pMap->iLine);
|
---|
1677 | else
|
---|
1678 | {
|
---|
1679 | g_aSegs[iSeg].uFlatAddr = ((uint32_t)g_aSegs[iSeg].Address.sel << 4) + g_aSegs[iSeg].Address.off;
|
---|
1680 | g_cSegs++;
|
---|
1681 | if (g_cVerbose > 2)
|
---|
1682 | RTStrmPrintf(g_pStdErr, "read segment at %08x / %04x:%04x LB %04x %s / %s / %s\n",
|
---|
1683 | g_aSegs[iSeg].uFlatAddr,
|
---|
1684 | g_aSegs[iSeg].Address.sel,
|
---|
1685 | g_aSegs[iSeg].Address.off,
|
---|
1686 | g_aSegs[iSeg].cb,
|
---|
1687 | g_aSegs[iSeg].szName,
|
---|
1688 | g_aSegs[iSeg].szClass,
|
---|
1689 | g_aSegs[iSeg].szGroup);
|
---|
1690 |
|
---|
1691 | while (RT_C_IS_SPACE(*psz))
|
---|
1692 | psz++;
|
---|
1693 | if (!*psz)
|
---|
1694 | continue;
|
---|
1695 | RTMsgError("%s:%u: Junk at end of line", pMap->pszMapFile, pMap->iLine);
|
---|
1696 | }
|
---|
1697 | return false;
|
---|
1698 | }
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 |
|
---|
1702 | /**
|
---|
1703 | * Sorts the segment array by flat address and adds them to the debug module.
|
---|
1704 | *
|
---|
1705 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1706 | */
|
---|
1707 | static bool mapSortAndAddSegments(void)
|
---|
1708 | {
|
---|
1709 | for (uint32_t i = 0; i < g_cSegs; i++)
|
---|
1710 | {
|
---|
1711 | for (uint32_t j = i + 1; j < g_cSegs; j++)
|
---|
1712 | if (g_aSegs[j].uFlatAddr < g_aSegs[i].uFlatAddr)
|
---|
1713 | {
|
---|
1714 | BIOSSEG Tmp = g_aSegs[i];
|
---|
1715 | g_aSegs[i] = g_aSegs[j];
|
---|
1716 | g_aSegs[j] = Tmp;
|
---|
1717 | }
|
---|
1718 | if (g_cVerbose > 0)
|
---|
1719 | RTStrmPrintf(g_pStdErr, "segment at %08x / %04x:%04x LB %04x %s / %s / %s\n",
|
---|
1720 | g_aSegs[i].uFlatAddr,
|
---|
1721 | g_aSegs[i].Address.sel,
|
---|
1722 | g_aSegs[i].Address.off,
|
---|
1723 | g_aSegs[i].cb,
|
---|
1724 | g_aSegs[i].szName,
|
---|
1725 | g_aSegs[i].szClass,
|
---|
1726 | g_aSegs[i].szGroup);
|
---|
1727 |
|
---|
1728 | RTDBGSEGIDX idx = i;
|
---|
1729 | int rc = RTDbgModSegmentAdd(g_hMapMod, g_aSegs[i].uFlatAddr, g_aSegs[i].cb, g_aSegs[i].szName, 0 /*fFlags*/, &idx);
|
---|
1730 | if (RT_FAILURE(rc))
|
---|
1731 | {
|
---|
1732 | RTMsgError("RTDbgModSegmentAdd failed on %s: %Rrc", g_aSegs[i].szName);
|
---|
1733 | return false;
|
---|
1734 | }
|
---|
1735 | }
|
---|
1736 | return true;
|
---|
1737 | }
|
---|
1738 |
|
---|
1739 |
|
---|
1740 | /**
|
---|
1741 | * Parses a segment list.
|
---|
1742 | *
|
---|
1743 | * @returns @c true on success, @c false + msg on failure, @c false on eof.
|
---|
1744 | * @param pMap The map file handle.
|
---|
1745 | */
|
---|
1746 | static bool mapParseSymbols(PBIOSMAP pMap)
|
---|
1747 | {
|
---|
1748 | for (;;)
|
---|
1749 | {
|
---|
1750 | if (!mapReadLineStripRight(pMap, NULL))
|
---|
1751 | return false;
|
---|
1752 |
|
---|
1753 | /* The end? The line should be empty. Expectes segment name to not
|
---|
1754 | start with a space. */
|
---|
1755 | if (!pMap->szLine[0] || RT_C_IS_SPACE(pMap->szLine[0]))
|
---|
1756 | {
|
---|
1757 | if (!pMap->szLine[0])
|
---|
1758 | return true;
|
---|
1759 | return mapError(pMap, "Malformed symbol line");
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | if (!strncmp(pMap->szLine, RT_STR_TUPLE("Module: ")))
|
---|
1763 | {
|
---|
1764 | /* Parse the module line. */
|
---|
1765 | size_t offObj = sizeof("Module: ") - 1;
|
---|
1766 | while (RT_C_IS_SPACE(pMap->szLine[offObj]))
|
---|
1767 | offObj++;
|
---|
1768 | size_t offSrc = offObj;
|
---|
1769 | char ch;
|
---|
1770 | while ((ch = pMap->szLine[offSrc]) != '(' && ch != '\0')
|
---|
1771 | offSrc++;
|
---|
1772 | size_t cchObj = offSrc - offObj;
|
---|
1773 |
|
---|
1774 | offSrc++;
|
---|
1775 | size_t cchSrc = offSrc;
|
---|
1776 | while ((ch = pMap->szLine[cchSrc]) != ')' && ch != '\0')
|
---|
1777 | cchSrc++;
|
---|
1778 | cchSrc -= offSrc;
|
---|
1779 | if (ch != ')')
|
---|
1780 | return mapError(pMap, "Symbol/Module line parse error");
|
---|
1781 |
|
---|
1782 | PBIOSOBJFILE pObjFile = (PBIOSOBJFILE)RTMemAllocZ(sizeof(*pObjFile) + cchSrc + cchObj + 2);
|
---|
1783 | if (!pObjFile)
|
---|
1784 | return mapError(pMap, "Out of memory");
|
---|
1785 | char *psz = (char *)(pObjFile + 1);
|
---|
1786 | pObjFile->pszObject = psz;
|
---|
1787 | memcpy(psz, &pMap->szLine[offObj], cchObj);
|
---|
1788 | psz += cchObj;
|
---|
1789 | *psz++ = '\0';
|
---|
1790 | pObjFile->pszSource = psz;
|
---|
1791 | memcpy(psz, &pMap->szLine[offSrc], cchSrc);
|
---|
1792 | psz[cchSrc] = '\0';
|
---|
1793 | RTListAppend(&g_ObjList, &pObjFile->Node);
|
---|
1794 | }
|
---|
1795 | else
|
---|
1796 | {
|
---|
1797 | /* Parse the segment line. */
|
---|
1798 | RTFAR16 Addr;
|
---|
1799 | char *psz = pMap->szLine;
|
---|
1800 | if (!mapParseAddress(&psz, &Addr))
|
---|
1801 | return mapError(pMap, "Symbol address parser error");
|
---|
1802 |
|
---|
1803 | char szName[4096];
|
---|
1804 | if (!mapParseWord(&psz, szName, sizeof(szName)))
|
---|
1805 | return mapError(pMap, "Symbol name parser error");
|
---|
1806 |
|
---|
1807 | uint32_t uFlatAddr = ((uint32_t)Addr.sel << 4) + Addr.off;
|
---|
1808 | if (uFlatAddr != 0)
|
---|
1809 | {
|
---|
1810 | int rc = RTDbgModSymbolAdd(g_hMapMod, szName, RTDBGSEGIDX_RVA, uFlatAddr, 0 /*cb*/, 0 /*fFlags*/, NULL);
|
---|
1811 | if (RT_FAILURE(rc) && rc != VERR_DBG_ADDRESS_CONFLICT)
|
---|
1812 | {
|
---|
1813 | /* HACK ALERT! For dealing with lables at segment size. */ /** @todo fix end labels. */
|
---|
1814 | rc = RTDbgModSymbolAdd(g_hMapMod, szName, RTDBGSEGIDX_RVA, uFlatAddr - 1, 0 /*cb*/, 0 /*fFlags*/, NULL);
|
---|
1815 | if (RT_FAILURE(rc) && rc != VERR_DBG_ADDRESS_CONFLICT)
|
---|
1816 | return mapError(pMap, "RTDbgModSymbolAdd failed: %Rrc", rc);
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | if (g_cVerbose > 2)
|
---|
1820 | RTStrmPrintf(g_pStdErr, "read symbol - %08x %s\n", uFlatAddr, szName);
|
---|
1821 | while (RT_C_IS_SPACE(*psz))
|
---|
1822 | psz++;
|
---|
1823 | if (*psz)
|
---|
1824 | return mapError(pMap, "Junk at end of line");
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | }
|
---|
1828 | }
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 |
|
---|
1832 | /**
|
---|
1833 | * Parses the given map file.
|
---|
1834 | *
|
---|
1835 | * @returns RTEXITCODE_SUCCESS and lots of globals, or RTEXITCODE_FAILURE and a
|
---|
1836 | * error message.
|
---|
1837 | * @param pMap The map file handle.
|
---|
1838 | */
|
---|
1839 | static RTEXITCODE mapParseFile(PBIOSMAP pMap)
|
---|
1840 | {
|
---|
1841 | int rc = RTDbgModCreate(&g_hMapMod, "VBoxBios", 0 /*cbSeg*/, 0 /*fFlags*/);
|
---|
1842 | if (RT_FAILURE(rc))
|
---|
1843 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTDbgModCreate failed: %Rrc", rc);
|
---|
1844 |
|
---|
1845 | /*
|
---|
1846 | * Read the header.
|
---|
1847 | */
|
---|
1848 | if (!mapReadLine(pMap))
|
---|
1849 | return RTEXITCODE_FAILURE;
|
---|
1850 | if (strncmp(pMap->szLine, RT_STR_TUPLE("Open Watcom Linker Version")))
|
---|
1851 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unexpected map-file header: '%s'", pMap->szLine);
|
---|
1852 | if ( !mapSkipNonEmptyLines(pMap)
|
---|
1853 | || !mapSkipEmptyLines(pMap))
|
---|
1854 | return RTEXITCODE_FAILURE;
|
---|
1855 |
|
---|
1856 | /*
|
---|
1857 | * Skip groups.
|
---|
1858 | */
|
---|
1859 | if (!mapSkipThruColumnHeadings(pMap, "Groups", 3, "Group", "Address", "Size", NULL))
|
---|
1860 | return RTEXITCODE_FAILURE;
|
---|
1861 | if (!mapSkipNonEmptyLines(pMap))
|
---|
1862 | return RTEXITCODE_FAILURE;
|
---|
1863 |
|
---|
1864 | /*
|
---|
1865 | * Parse segments.
|
---|
1866 | */
|
---|
1867 | if (!mapSkipThruColumnHeadings(pMap, "Segments", 5, "Segment", "Class", "Group", "Address", "Size"))
|
---|
1868 | return RTEXITCODE_FAILURE;
|
---|
1869 | if (!mapParseSegments(pMap))
|
---|
1870 | return RTEXITCODE_FAILURE;
|
---|
1871 | if (!mapSortAndAddSegments())
|
---|
1872 | return RTEXITCODE_FAILURE;
|
---|
1873 |
|
---|
1874 | /*
|
---|
1875 | * Parse symbols.
|
---|
1876 | */
|
---|
1877 | if (!mapSkipThruColumnHeadings(pMap, "Memory Map", 2, "Address", "Symbol"))
|
---|
1878 | return RTEXITCODE_FAILURE;
|
---|
1879 | if (!mapParseSymbols(pMap))
|
---|
1880 | return RTEXITCODE_FAILURE;
|
---|
1881 |
|
---|
1882 | /* Ignore the rest of the file. */
|
---|
1883 | return RTEXITCODE_SUCCESS;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 |
|
---|
1887 | /**
|
---|
1888 | * Parses the linker map file for the BIOS.
|
---|
1889 | *
|
---|
1890 | * This is generated by the Watcom linker.
|
---|
1891 | *
|
---|
1892 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
|
---|
1893 | * @param pszBiosMap Path to the map file.
|
---|
1894 | */
|
---|
1895 | static RTEXITCODE ParseMapFile(const char *pszBiosMap)
|
---|
1896 | {
|
---|
1897 | BIOSMAP Map;
|
---|
1898 | Map.pszMapFile = pszBiosMap;
|
---|
1899 | Map.hStrm = NULL;
|
---|
1900 | Map.iLine = 0;
|
---|
1901 | Map.fEof = false;
|
---|
1902 | Map.cch = 0;
|
---|
1903 | Map.offNW = 0;
|
---|
1904 | int rc = RTStrmOpen(pszBiosMap, "r", &Map.hStrm);
|
---|
1905 | if (RT_FAILURE(rc))
|
---|
1906 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening '%s': %Rrc", pszBiosMap, rc);
|
---|
1907 | RTEXITCODE rcExit = mapParseFile(&Map);
|
---|
1908 | RTStrmClose(Map.hStrm);
|
---|
1909 | return rcExit;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 |
|
---|
1913 | /**
|
---|
1914 | * Reads the BIOS image into memory (g_pbImg and g_cbImg).
|
---|
1915 | *
|
---|
1916 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
|
---|
1917 | * @param pszBiosImg Path to the image file.
|
---|
1918 | */
|
---|
1919 | static RTEXITCODE ReadBiosImage(const char *pszBiosImg)
|
---|
1920 | {
|
---|
1921 | void *pvImg;
|
---|
1922 | size_t cbImg;
|
---|
1923 | int rc = RTFileReadAll(pszBiosImg, &pvImg, &cbImg);
|
---|
1924 | if (RT_FAILURE(rc))
|
---|
1925 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error reading '%s': %Rrc", pszBiosImg, rc);
|
---|
1926 |
|
---|
1927 | size_t cbImgExpect;
|
---|
1928 | switch (g_enmBiosType)
|
---|
1929 | {
|
---|
1930 | case kBiosType_System: cbImgExpect = _64K; break;
|
---|
1931 | case kBiosType_Vga: cbImgExpect = _32K; break;
|
---|
1932 | default: cbImgExpect = 0; break;
|
---|
1933 | }
|
---|
1934 | if (cbImg != cbImgExpect)
|
---|
1935 | {
|
---|
1936 | RTFileReadAllFree(pvImg, cbImg);
|
---|
1937 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The BIOS image %u bytes intead of %u bytes", cbImg, cbImgExpect);
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | g_pbImg = (uint8_t *)pvImg;
|
---|
1941 | g_cbImg = cbImg;
|
---|
1942 | return RTEXITCODE_SUCCESS;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 |
|
---|
1946 | int main(int argc, char **argv)
|
---|
1947 | {
|
---|
1948 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
1949 | if (RT_FAILURE(rc))
|
---|
1950 | return RTMsgInitFailure(rc);
|
---|
1951 |
|
---|
1952 | RTListInit(&g_ObjList);
|
---|
1953 |
|
---|
1954 | /*
|
---|
1955 | * Option config.
|
---|
1956 | */
|
---|
1957 | static RTGETOPTDEF const s_aOpts[] =
|
---|
1958 | {
|
---|
1959 | { "--bios-image", 'i', RTGETOPT_REQ_STRING },
|
---|
1960 | { "--bios-map", 'm', RTGETOPT_REQ_STRING },
|
---|
1961 | { "--bios-sym", 's', RTGETOPT_REQ_STRING },
|
---|
1962 | { "--bios-type", 't', RTGETOPT_REQ_STRING },
|
---|
1963 | { "--output", 'o', RTGETOPT_REQ_STRING },
|
---|
1964 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
1965 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
1966 | };
|
---|
1967 |
|
---|
1968 | const char *pszBiosMap = NULL;
|
---|
1969 | const char *pszBiosSym = NULL;
|
---|
1970 | const char *pszBiosImg = NULL;
|
---|
1971 | const char *pszOutput = NULL;
|
---|
1972 |
|
---|
1973 | RTGETOPTUNION ValueUnion;
|
---|
1974 | RTGETOPTSTATE GetOptState;
|
---|
1975 | rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1976 | AssertReleaseRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
1977 |
|
---|
1978 | /*
|
---|
1979 | * Process the options.
|
---|
1980 | */
|
---|
1981 | while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
|
---|
1982 | {
|
---|
1983 | switch (rc)
|
---|
1984 | {
|
---|
1985 | case 'i':
|
---|
1986 | if (pszBiosImg)
|
---|
1987 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-image is given more than once");
|
---|
1988 | pszBiosImg = ValueUnion.psz;
|
---|
1989 | break;
|
---|
1990 |
|
---|
1991 | case 'm':
|
---|
1992 | if (pszBiosMap)
|
---|
1993 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-map is given more than once");
|
---|
1994 | pszBiosMap = ValueUnion.psz;
|
---|
1995 | break;
|
---|
1996 |
|
---|
1997 | case 's':
|
---|
1998 | if (pszBiosSym)
|
---|
1999 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-sym is given more than once");
|
---|
2000 | pszBiosSym = ValueUnion.psz;
|
---|
2001 | break;
|
---|
2002 |
|
---|
2003 | case 'o':
|
---|
2004 | if (pszOutput)
|
---|
2005 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--output is given more than once");
|
---|
2006 | pszOutput = ValueUnion.psz;
|
---|
2007 | break;
|
---|
2008 |
|
---|
2009 | case 't':
|
---|
2010 | if (!strcmp(ValueUnion.psz, "system"))
|
---|
2011 | {
|
---|
2012 | g_enmBiosType = kBiosType_System;
|
---|
2013 | g_uBiosFlatBase = 0xf0000;
|
---|
2014 | }
|
---|
2015 | else if (!strcmp(ValueUnion.psz, "vga"))
|
---|
2016 | {
|
---|
2017 | g_enmBiosType = kBiosType_Vga;
|
---|
2018 | g_uBiosFlatBase = 0xc0000;
|
---|
2019 | }
|
---|
2020 | else
|
---|
2021 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown bios type '%s'", ValueUnion.psz);
|
---|
2022 | break;
|
---|
2023 |
|
---|
2024 | case 'v':
|
---|
2025 | g_cVerbose++;
|
---|
2026 | break;
|
---|
2027 |
|
---|
2028 | case 'q':
|
---|
2029 | g_cVerbose = 0;
|
---|
2030 | break;
|
---|
2031 |
|
---|
2032 | case 'H':
|
---|
2033 | RTPrintf("usage: %Rbn --bios-image <file.img> --bios-map <file.map> [--output <file.asm>]\n",
|
---|
2034 | argv[0]);
|
---|
2035 | return RTEXITCODE_SUCCESS;
|
---|
2036 |
|
---|
2037 | case 'V':
|
---|
2038 | {
|
---|
2039 | /* The following is assuming that svn does it's job here. */
|
---|
2040 | RTPrintf("r%u\n", RTBldCfgRevision());
|
---|
2041 | return RTEXITCODE_SUCCESS;
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | default:
|
---|
2045 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
2046 | }
|
---|
2047 | }
|
---|
2048 |
|
---|
2049 | /*
|
---|
2050 | * Got it all?
|
---|
2051 | */
|
---|
2052 | if (!pszBiosImg)
|
---|
2053 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-image is required");
|
---|
2054 | if (!pszBiosMap)
|
---|
2055 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-map is required");
|
---|
2056 | if (!pszBiosSym)
|
---|
2057 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-sym is required");
|
---|
2058 |
|
---|
2059 | /*
|
---|
2060 | * Do the job.
|
---|
2061 | */
|
---|
2062 | RTEXITCODE rcExit;
|
---|
2063 | rcExit = ReadBiosImage(pszBiosImg);
|
---|
2064 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
2065 | rcExit = ParseMapFile(pszBiosMap);
|
---|
2066 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
2067 | rcExit = ParseSymFile(pszBiosSym);
|
---|
2068 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
2069 | rcExit = OpenOutputFile(pszOutput);
|
---|
2070 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
2071 | rcExit = DisassembleBiosImage();
|
---|
2072 |
|
---|
2073 | return rcExit;
|
---|
2074 | }
|
---|
2075 |
|
---|