VirtualBox

source: vbox/trunk/src/VBox/Devices/BiosCommonCode/MakeAlternativeSource.cpp@ 43394

Last change on this file since 43394 was 43077, checked in by vboxsync, 12 years ago

MakeAlternativeSource.cpp: fix for retf == 0x66 0xcb

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette