VirtualBox

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

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

warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.7 KB
Line 
1/* $Id: MakeAlternativeSource.cpp 42548 2012-08-02 15:30:49Z 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 42548 2012-08-02 15:30:49Z 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 /*
873 * Handle different stuff.
874 */
875 if (fDifferent)
876 {
877 disByteData(uFlatAddr, cbInstr); /* lazy bird. */
878
879 if (cchUsed + 2 < cbBuf)
880 {
881 memmove(pszBuf + 2, pszBuf, cchUsed + 2);
882 cchUsed += 2;
883 }
884
885 pszBuf[0] = ';';
886 pszBuf[1] = ' ';
887 }
888
889 return cchUsed;
890}
891
892
893/**
894 * @callback_method_impl{FNDISREADBYTES}
895 *
896 * @remarks @a uSrcAddr is the flat address.
897 */
898static DECLCALLBACK(int) disReadOpcodeBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
899{
900 RTUINTPTR offBios = pDis->uInstrAddr + offInstr - g_uBiosFlatBase;
901 size_t cbToRead = cbMaxRead;
902 if (offBios + cbToRead > g_cbImg)
903 {
904 if (offBios >= g_cbImg)
905 cbToRead = 0;
906 else
907 cbToRead = g_cbImg - offBios;
908 }
909 memcpy(&pDis->abInstr[offInstr], &g_pbImg[offBios], cbToRead);
910 pDis->cbCachedInstr = (uint8_t)(offInstr + cbToRead);
911 return VINF_SUCCESS;
912}
913
914
915/**
916 * Disassembles code.
917 *
918 * @returns @c true on success, @c false on failure.
919 * @param uFlatAddr The address where the code starts.
920 * @param cb The amount of code to disassemble.
921 * @param fIs16Bit Is is 16-bit (@c true) or 32-bit (@c false).
922 */
923static bool disCode(uint32_t uFlatAddr, uint32_t cb, bool fIs16Bit)
924{
925 uint8_t const *pb = &g_pbImg[uFlatAddr - g_uBiosFlatBase];
926
927 while (cb > 0)
928 {
929 /* Trailing zero padding detection. */
930 if ( *pb == '\0'
931 && ASMMemIsAll8(pb, RT_MIN(cb, 8), 0) == NULL)
932 {
933 void *pv = ASMMemIsAll8(pb, cb, 0);
934 uint32_t cbZeros = pv ? (uint32_t)((uint8_t const *)pv - pb) : cb;
935 if (!outputPrintf(" times %#x db 0\n", cbZeros))
936 return false;
937 cb -= cbZeros;
938 pb += cbZeros;
939 uFlatAddr += cbZeros;
940 if ( cb == 2
941 && pb[0] == 'X'
942 && pb[1] == 'M')
943 return disStringData(uFlatAddr, cb);
944 }
945 /* Work arounds for switch tables and such (disas assertions). */
946 else if ( ( pb[0] == 0x11 /* int13_cdemu switch */
947 && pb[1] == 0xda
948 && pb[2] == 0x05
949 && pb[3] == 0xff
950 && pb[4] == 0xff
951 )
952 || 0
953 )
954 return disByteData(uFlatAddr, cb);
955 else
956 {
957 unsigned cbInstr;
958 DISCPUSTATE CpuState;
959 int rc = DISInstrWithReader(uFlatAddr, fIs16Bit ? DISCPUMODE_16BIT : DISCPUMODE_32BIT,
960 disReadOpcodeBytes, NULL, &CpuState, &cbInstr);
961 if ( RT_SUCCESS(rc)
962 && cbInstr <= cb
963 && CpuState.pCurInstr
964 && CpuState.pCurInstr->uOpcode != OP_INVALID)
965 {
966 char szTmp[4096];
967 size_t cch = DISFormatYasmEx(&CpuState, szTmp, sizeof(szTmp),
968 DIS_FMT_FLAGS_STRICT
969 | DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_COMMENT | DIS_FMT_FLAGS_BYTES_SPACED,
970 NULL, NULL);
971 cch = disHandleYasmDifferences(&CpuState, uFlatAddr, cbInstr, szTmp, sizeof(szTmp), cch);
972 Assert(cch < sizeof(szTmp));
973
974 if (g_cVerbose > 1)
975 {
976 while (cch < 72)
977 szTmp[cch++] = ' ';
978 RTStrPrintf(&szTmp[cch], sizeof(szTmp) - cch, "; %#x", uFlatAddr);
979 }
980
981 if (!outputPrintf(" %s\n", szTmp))
982 return false;
983 cb -= cbInstr;
984 pb += cbInstr;
985 uFlatAddr += cbInstr;
986 }
987 else
988 {
989 if (!disByteData(uFlatAddr, 1))
990 return false;
991 cb--;
992 pb++;
993 uFlatAddr++;
994 }
995 }
996 }
997 return true;
998}
999
1000
1001static bool disCodeSegment(uint32_t iSeg)
1002{
1003 uint32_t uFlatAddr = g_aSegs[iSeg].uFlatAddr;
1004 uint32_t cb = g_aSegs[iSeg].cb;
1005
1006 while (cb > 0)
1007 {
1008 uint32_t off;
1009 RTDBGSYMBOL Sym;
1010 disGetNextSymbol(uFlatAddr, cb, &off, &Sym);
1011
1012 if (off > 0)
1013 {
1014 if (!disByteData(uFlatAddr, off))
1015 return false;
1016 cb -= off;
1017 uFlatAddr += off;
1018 off = 0;
1019 if (!cb)
1020 break;
1021 }
1022
1023 bool fRc;
1024 if (off == 0)
1025 {
1026 size_t cchName = strlen(Sym.szName);
1027 fRc = outputPrintf("%s: %*s; %#x LB %#x\n", Sym.szName, cchName < 41 - 2 ? cchName - 41 - 2 : 0, "", uFlatAddr, Sym.cb);
1028 if (!fRc)
1029 return false;
1030
1031 if (disIsCodeAndAdjustSize(uFlatAddr, &Sym, &g_aSegs[iSeg]))
1032 fRc = disCode(uFlatAddr, Sym.cb, disIs16BitCode(Sym.szName));
1033 else
1034 fRc = disByteData(uFlatAddr, Sym.cb);
1035
1036 uFlatAddr += Sym.cb;
1037 cb -= Sym.cb;
1038 }
1039 else
1040 {
1041 fRc = disByteData(uFlatAddr, cb);
1042 uFlatAddr += cb;
1043 cb = 0;
1044 }
1045 if (!fRc)
1046 return false;
1047 }
1048
1049 return true;
1050}
1051
1052
1053static RTEXITCODE DisassembleBiosImage(void)
1054{
1055 if (!disFileHeader())
1056 return RTEXITCODE_FAILURE;
1057
1058 /*
1059 * Work the image segment by segment.
1060 */
1061 bool fRc = true;
1062 uint32_t uFlatAddr = g_uBiosFlatBase;
1063 for (uint32_t iSeg = 0; iSeg < g_cSegs && fRc; iSeg++)
1064 {
1065 /* Is there a gap between the segments? */
1066 if (uFlatAddr < g_aSegs[iSeg].uFlatAddr)
1067 {
1068 fRc = disCopySegmentGap(uFlatAddr, g_aSegs[iSeg].uFlatAddr - uFlatAddr);
1069 if (!fRc)
1070 break;
1071 uFlatAddr = g_aSegs[iSeg].uFlatAddr;
1072 }
1073 else if (uFlatAddr > g_aSegs[iSeg].uFlatAddr)
1074 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Overlapping segments: %u and %u; uFlatAddr=%#x\n", iSeg - 1, iSeg, uFlatAddr);
1075
1076 /* Disassemble the segment. */
1077 fRc = outputPrintf("\n"
1078 "section %s progbits vstart=%#x align=1 ; size=%#x class=%s group=%s\n",
1079 g_aSegs[iSeg].szName, g_aSegs[iSeg].uFlatAddr - g_uBiosFlatBase,
1080 g_aSegs[iSeg].cb, g_aSegs[iSeg].szClass, g_aSegs[iSeg].szGroup);
1081 if (!fRc)
1082 return RTEXITCODE_FAILURE;
1083 if (!strcmp(g_aSegs[iSeg].szName, "CONST"))
1084 fRc = disConstSegment(iSeg);
1085 else if (!strcmp(g_aSegs[iSeg].szClass, "DATA"))
1086 fRc = disDataSegment(iSeg);
1087 else
1088 fRc = disCodeSegment(iSeg);
1089
1090 /* Advance. */
1091 uFlatAddr += g_aSegs[iSeg].cb;
1092 }
1093
1094 /* Final gap. */
1095 if (uFlatAddr < g_uBiosFlatBase + g_cbImg)
1096 fRc = disCopySegmentGap(uFlatAddr, (uint32_t)(g_uBiosFlatBase + g_cbImg - uFlatAddr));
1097 else if (uFlatAddr > g_uBiosFlatBase + g_cbImg)
1098 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Last segment spills beyond 1MB; uFlatAddr=%#x\n", uFlatAddr);
1099
1100 if (!fRc)
1101 return RTEXITCODE_FAILURE;
1102 return RTEXITCODE_SUCCESS;
1103}
1104
1105
1106
1107/**
1108 * Parses the symbol file for the BIOS.
1109 *
1110 * This is in ELF/DWARF format.
1111 *
1112 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
1113 * @param pszBiosSym Path to the sym file.
1114 */
1115static RTEXITCODE ParseSymFile(const char *pszBiosSym)
1116{
1117#if 1
1118 /** @todo use RTDbg* later. (Just checking for existance currently.) */
1119 PRTSTREAM hStrm;
1120 int rc = RTStrmOpen(pszBiosSym, "rb", &hStrm);
1121 if (RT_FAILURE(rc))
1122 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening '%s': %Rrc", pszBiosSym, rc);
1123 RTStrmClose(hStrm);
1124#else
1125 RTDBGMOD hDbgMod;
1126 int rc = RTDbgModCreateFromImage(&hDbgMod, pszBiosSym, "VBoxBios", 0 /*fFlags*/);
1127 RTMsgInfo("RTDbgModCreateFromImage -> %Rrc\n", rc);
1128#endif
1129 return RTEXITCODE_SUCCESS;
1130}
1131
1132
1133/**
1134 * Display an error with the mapfile name and current line, return false.
1135 *
1136 * @returns @c false.
1137 * @param pMap The map file handle.
1138 * @param pszFormat The format string.
1139 * @param ... Format arguments.
1140 */
1141static bool mapError(PBIOSMAP pMap, const char *pszFormat, ...)
1142{
1143 va_list va;
1144 va_start(va, pszFormat);
1145 RTMsgError("%s:%d: %N", pMap->pszMapFile, pMap->iLine, pszFormat, va);
1146 va_end(va);
1147 return false;
1148}
1149
1150
1151/**
1152 * Reads a line from the file.
1153 *
1154 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1155 * @param pMap The map file handle.
1156 */
1157static bool mapReadLine(PBIOSMAP pMap)
1158{
1159 int rc = RTStrmGetLine(pMap->hStrm, pMap->szLine, sizeof(pMap->szLine));
1160 if (RT_FAILURE(rc))
1161 {
1162 if (rc == VERR_EOF)
1163 {
1164 pMap->fEof = true;
1165 pMap->cch = 0;
1166 pMap->offNW = 0;
1167 pMap->szLine[0] = '\0';
1168 }
1169 else
1170 RTMsgError("%s:%d: Read error %Rrc", pMap->pszMapFile, pMap->iLine + 1, rc);
1171 return false;
1172 }
1173 pMap->iLine++;
1174 pMap->cch = (uint32_t)strlen(pMap->szLine);
1175
1176 /* Check out leading white space. */
1177 if (!RT_C_IS_SPACE(pMap->szLine[0]))
1178 pMap->offNW = 0;
1179 else
1180 {
1181 uint32_t off = 1;
1182 while (RT_C_IS_SPACE(pMap->szLine[off]))
1183 off++;
1184 pMap->offNW = off;
1185 }
1186
1187 return true;
1188}
1189
1190
1191/**
1192 * Checks if it is an empty line.
1193 * @returns @c true if empty, @c false if not.
1194 * @param pMap The map file handle.
1195 */
1196static bool mapIsEmptyLine(PBIOSMAP pMap)
1197{
1198 Assert(pMap->offNW <= pMap->cch);
1199 return pMap->offNW == pMap->cch;
1200}
1201
1202
1203/**
1204 * Reads ahead in the map file until a non-empty line or EOF is encountered.
1205 *
1206 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1207 * @param pMap The map file handle.
1208 */
1209static bool mapSkipEmptyLines(PBIOSMAP pMap)
1210{
1211 for (;;)
1212 {
1213 if (!mapReadLine(pMap))
1214 return false;
1215 if (pMap->offNW < pMap->cch)
1216 return true;
1217 }
1218}
1219
1220
1221/**
1222 * Reads ahead in the map file until an empty line or EOF is encountered.
1223 *
1224 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1225 * @param pMap The map file handle.
1226 */
1227static bool mapSkipNonEmptyLines(PBIOSMAP pMap)
1228{
1229 for (;;)
1230 {
1231 if (!mapReadLine(pMap))
1232 return false;
1233 if (pMap->offNW == pMap->cch)
1234 return true;
1235 }
1236}
1237
1238
1239/**
1240 * Strips the current line.
1241 *
1242 * The string length may change.
1243 *
1244 * @returns Pointer to the first non-space character.
1245 * @param pMap The map file handle.
1246 * @param pcch Where to return the length of the unstripped
1247 * part. Optional.
1248 */
1249static char *mapStripCurrentLine(PBIOSMAP pMap, size_t *pcch)
1250{
1251 char *psz = &pMap->szLine[pMap->offNW];
1252 char *pszEnd = &pMap->szLine[pMap->cch];
1253 while ( (uintptr_t)pszEnd > (uintptr_t)psz
1254 && RT_C_IS_SPACE(pszEnd[-1]))
1255 {
1256 *--pszEnd = '\0';
1257 pMap->cch--;
1258 }
1259 if (pcch)
1260 *pcch = pszEnd - psz;
1261 return psz;
1262}
1263
1264
1265/**
1266 * Reads a line from the file and right strips it.
1267 *
1268 * @returns Pointer to szLine on success, @c NULL + msg on failure, @c NULL on
1269 * EOF.
1270 * @param pMap The map file handle.
1271 * @param pcch Where to return the length of the unstripped
1272 * part. Optional.
1273 */
1274static char *mapReadLineStripRight(PBIOSMAP pMap, size_t *pcch)
1275{
1276 if (!mapReadLine(pMap))
1277 return NULL;
1278 mapStripCurrentLine(pMap, NULL);
1279 if (pcch)
1280 *pcch = pMap->cch;
1281 return pMap->szLine;
1282}
1283
1284
1285/**
1286 * mapReadLine() + mapStripCurrentLine().
1287 *
1288 * @returns Pointer to the first non-space character in the new line. NULL on
1289 * read error (bitched already) or end of file.
1290 * @param pMap The map file handle.
1291 * @param pcch Where to return the length of the unstripped
1292 * part. Optional.
1293 */
1294static char *mapReadLineStrip(PBIOSMAP pMap, size_t *pcch)
1295{
1296 if (!mapReadLine(pMap))
1297 return NULL;
1298 return mapStripCurrentLine(pMap, pcch);
1299}
1300
1301
1302/**
1303 * Parses a word, copying it into the supplied buffer, and skipping any spaces
1304 * following it.
1305 *
1306 * @returns @c true on success, @c false on failure.
1307 * @param ppszCursor Pointer to the cursor variable.
1308 * @param pszBuf The output buffer.
1309 * @param cbBuf The size of the output buffer.
1310 */
1311static bool mapParseWord(char **ppszCursor, char *pszBuf, size_t cbBuf)
1312{
1313 /* Check that we start on a non-blank. */
1314 char *pszStart = *ppszCursor;
1315 if (!*pszStart || RT_C_IS_SPACE(*pszStart))
1316 return false;
1317
1318 /* Find the end of the word. */
1319 char *psz = pszStart + 1;
1320 while (*psz && !RT_C_IS_SPACE(*psz))
1321 psz++;
1322
1323 /* Copy it. */
1324 size_t cchWord = (uintptr_t)psz - (uintptr_t)pszStart;
1325 if (cchWord >= cbBuf)
1326 return false;
1327 memcpy(pszBuf, pszStart, cchWord);
1328 pszBuf[cchWord] = '\0';
1329
1330 /* Skip blanks following it. */
1331 while (RT_C_IS_SPACE(*psz))
1332 psz++;
1333 *ppszCursor = psz;
1334 return true;
1335}
1336
1337
1338/**
1339 * Parses an 16:16 address.
1340 *
1341 * @returns @c true on success, @c false on failure.
1342 * @param ppszCursor Pointer to the cursor variable.
1343 * @param pAddr Where to return the address.
1344 */
1345static bool mapParseAddress(char **ppszCursor, PRTFAR16 pAddr)
1346{
1347 char szWord[32];
1348 if (!mapParseWord(ppszCursor, szWord, sizeof(szWord)))
1349 return false;
1350 size_t cchWord = strlen(szWord);
1351
1352 /* An address is at least 16:16 format. It may be 16:32. It may also be flagged. */
1353 size_t cchAddr = 4 + 1 + 4;
1354 if (cchWord < cchAddr)
1355 return false;
1356 if ( !RT_C_IS_XDIGIT(szWord[0])
1357 || !RT_C_IS_XDIGIT(szWord[1])
1358 || !RT_C_IS_XDIGIT(szWord[2])
1359 || !RT_C_IS_XDIGIT(szWord[3])
1360 || szWord[4] != ':'
1361 || !RT_C_IS_XDIGIT(szWord[5])
1362 || !RT_C_IS_XDIGIT(szWord[6])
1363 || !RT_C_IS_XDIGIT(szWord[7])
1364 || !RT_C_IS_XDIGIT(szWord[8])
1365 )
1366 return false;
1367 if ( cchWord > cchAddr
1368 && RT_C_IS_XDIGIT(szWord[9])
1369 && RT_C_IS_XDIGIT(szWord[10])
1370 && RT_C_IS_XDIGIT(szWord[11])
1371 && RT_C_IS_XDIGIT(szWord[12]))
1372 cchAddr += 4;
1373
1374 /* Drop flag if present. */
1375 if (cchWord > cchAddr)
1376 {
1377 if (RT_C_IS_XDIGIT(szWord[cchAddr]))
1378 return false;
1379 szWord[cchAddr] = '\0';
1380 cchWord = cchAddr;
1381 }
1382
1383 /* Convert it. */
1384 szWord[4] = '\0';
1385 int rc1 = RTStrToUInt16Full(szWord, 16, &pAddr->sel);
1386 if (rc1 != VINF_SUCCESS)
1387 return false;
1388
1389 int rc2 = RTStrToUInt16Full(szWord + 5, 16, &pAddr->off);
1390 if (rc2 != VINF_SUCCESS)
1391 return false;
1392 return true;
1393}
1394
1395
1396/**
1397 * Parses a size.
1398 *
1399 * @returns @c true on success, @c false on failure.
1400 * @param ppszCursor Pointer to the cursor variable.
1401 * @param pcb Where to return the size.
1402 */
1403static bool mapParseSize(char **ppszCursor, uint32_t *pcb)
1404{
1405 char szWord[32];
1406 if (!mapParseWord(ppszCursor, szWord, sizeof(szWord)))
1407 return false;
1408 size_t cchWord = strlen(szWord);
1409 if (cchWord != 8)
1410 return false;
1411
1412 int rc = RTStrToUInt32Full(szWord, 16, pcb);
1413 if (rc != VINF_SUCCESS)
1414 return false;
1415 return true;
1416}
1417
1418
1419/**
1420 * Parses a section box and the following column header.
1421 *
1422 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1423 * @param pMap Map file handle.
1424 * @param pszSectionNm The expected section name.
1425 * @param cColumns The number of columns.
1426 * @param ... The column names.
1427 */
1428static bool mapSkipThruColumnHeadings(PBIOSMAP pMap, const char *pszSectionNm, uint32_t cColumns, ...)
1429{
1430 if ( mapIsEmptyLine(pMap)
1431 && !mapSkipEmptyLines(pMap))
1432 return false;
1433
1434 /* +------------+ */
1435 size_t cch;
1436 char *psz = mapStripCurrentLine(pMap, &cch);
1437 if (!psz)
1438 return false;
1439
1440 if ( psz[0] != '+'
1441 || psz[1] != '-'
1442 || psz[2] != '-'
1443 || psz[3] != '-'
1444 || psz[cch - 4] != '-'
1445 || psz[cch - 3] != '-'
1446 || psz[cch - 2] != '-'
1447 || psz[cch - 1] != '+'
1448 )
1449 {
1450 RTMsgError("%s:%d: Expected section box: +-----...", pMap->pszMapFile, pMap->iLine);
1451 return false;
1452 }
1453
1454 /* | pszSectionNm | */
1455 psz = mapReadLineStrip(pMap, &cch);
1456 if (!psz)
1457 return false;
1458
1459 size_t cchSectionNm = strlen(pszSectionNm);
1460 if ( psz[0] != '|'
1461 || psz[1] != ' '
1462 || psz[2] != ' '
1463 || psz[3] != ' '
1464 || psz[cch - 4] != ' '
1465 || psz[cch - 3] != ' '
1466 || psz[cch - 2] != ' '
1467 || psz[cch - 1] != '|'
1468 || cch != 1 + 3 + cchSectionNm + 3 + 1
1469 || strncmp(&psz[4], pszSectionNm, cchSectionNm)
1470 )
1471 {
1472 RTMsgError("%s:%d: Expected section box: | %s |", pMap->pszMapFile, pMap->iLine, pszSectionNm);
1473 return false;
1474 }
1475
1476 /* +------------+ */
1477 psz = mapReadLineStrip(pMap, &cch);
1478 if (!psz)
1479 return false;
1480 if ( psz[0] != '+'
1481 || psz[1] != '-'
1482 || psz[2] != '-'
1483 || psz[3] != '-'
1484 || psz[cch - 4] != '-'
1485 || psz[cch - 3] != '-'
1486 || psz[cch - 2] != '-'
1487 || psz[cch - 1] != '+'
1488 )
1489 {
1490 RTMsgError("%s:%d: Expected section box: +-----...", pMap->pszMapFile, pMap->iLine);
1491 return false;
1492 }
1493
1494 /* There may be a few lines describing the table notation now, surrounded by blank lines. */
1495 do
1496 {
1497 psz = mapReadLineStripRight(pMap, &cch);
1498 if (!psz)
1499 return false;
1500 } while ( *psz == '\0'
1501 || ( !RT_C_IS_SPACE(psz[0])
1502 && RT_C_IS_SPACE(psz[1])
1503 && psz[2] == '='
1504 && RT_C_IS_SPACE(psz[3]))
1505 );
1506
1507 /* Should have the column heading now. */
1508 va_list va;
1509 va_start(va, cColumns);
1510 for (uint32_t i = 0; i < cColumns; i++)
1511 {
1512 const char *pszColumn = va_arg(va, const char *);
1513 size_t cchColumn = strlen(pszColumn);
1514 if ( strncmp(psz, pszColumn, cchColumn)
1515 || ( psz[cchColumn] != '\0'
1516 && !RT_C_IS_SPACE(psz[cchColumn])))
1517 {
1518 va_end(va);
1519 RTMsgError("%s:%d: Expected column '%s' found '%s'", pMap->pszMapFile, pMap->iLine, pszColumn, psz);
1520 return false;
1521 }
1522 psz += cchColumn;
1523 while (RT_C_IS_SPACE(*psz))
1524 psz++;
1525 }
1526 va_end(va);
1527
1528 /* The next line is the underlining. */
1529 psz = mapReadLineStripRight(pMap, &cch);
1530 if (!psz)
1531 return false;
1532 if (*psz != '=' || psz[cch - 1] != '=')
1533 {
1534 RTMsgError("%s:%d: Expected column header underlining", pMap->pszMapFile, pMap->iLine);
1535 return false;
1536 }
1537
1538 /* Skip one blank line. */
1539 psz = mapReadLineStripRight(pMap, &cch);
1540 if (!psz)
1541 return false;
1542 if (*psz)
1543 {
1544 RTMsgError("%s:%d: Expected blank line beneath the column headers", pMap->pszMapFile, pMap->iLine);
1545 return false;
1546 }
1547
1548 return true;
1549}
1550
1551
1552/**
1553 * Parses a segment list.
1554 *
1555 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1556 * @param pMap The map file handle.
1557 */
1558static bool mapParseSegments(PBIOSMAP pMap)
1559{
1560 for (;;)
1561 {
1562 if (!mapReadLineStripRight(pMap, NULL))
1563 return false;
1564
1565 /* The end? The line should be empty. Expectes segment name to not
1566 start with a space. */
1567 if (!pMap->szLine[0] || RT_C_IS_SPACE(pMap->szLine[0]))
1568 {
1569 if (!pMap->szLine[0])
1570 return true;
1571 RTMsgError("%s:%u: Malformed segment line", pMap->pszMapFile, pMap->iLine);
1572 return false;
1573 }
1574
1575 /* Parse the segment line. */
1576 uint32_t iSeg = g_cSegs;
1577 if (iSeg >= RT_ELEMENTS(g_aSegs))
1578 {
1579 RTMsgError("%s:%u: Too many segments", pMap->pszMapFile, pMap->iLine);
1580 return false;
1581 }
1582
1583 char *psz = pMap->szLine;
1584 if (!mapParseWord(&psz, g_aSegs[iSeg].szName, sizeof(g_aSegs[iSeg].szName)))
1585 RTMsgError("%s:%u: Segment name parser error", pMap->pszMapFile, pMap->iLine);
1586 else if (!mapParseWord(&psz, g_aSegs[iSeg].szClass, sizeof(g_aSegs[iSeg].szClass)))
1587 RTMsgError("%s:%u: Segment class parser error", pMap->pszMapFile, pMap->iLine);
1588 else if (!mapParseWord(&psz, g_aSegs[iSeg].szGroup, sizeof(g_aSegs[iSeg].szGroup)))
1589 RTMsgError("%s:%u: Segment group parser error", pMap->pszMapFile, pMap->iLine);
1590 else if (!mapParseAddress(&psz, &g_aSegs[iSeg].Address))
1591 RTMsgError("%s:%u: Segment address parser error", pMap->pszMapFile, pMap->iLine);
1592 else if (!mapParseSize(&psz, &g_aSegs[iSeg].cb))
1593 RTMsgError("%s:%u: Segment size parser error", pMap->pszMapFile, pMap->iLine);
1594 else
1595 {
1596 g_aSegs[iSeg].uFlatAddr = ((uint32_t)g_aSegs[iSeg].Address.sel << 4) + g_aSegs[iSeg].Address.off;
1597 g_cSegs++;
1598 if (g_cVerbose > 2)
1599 RTStrmPrintf(g_pStdErr, "read segment at %08x / %04x:%04x LB %04x %s / %s / %s\n",
1600 g_aSegs[iSeg].uFlatAddr,
1601 g_aSegs[iSeg].Address.sel,
1602 g_aSegs[iSeg].Address.off,
1603 g_aSegs[iSeg].cb,
1604 g_aSegs[iSeg].szName,
1605 g_aSegs[iSeg].szClass,
1606 g_aSegs[iSeg].szGroup);
1607
1608 while (RT_C_IS_SPACE(*psz))
1609 psz++;
1610 if (!*psz)
1611 continue;
1612 RTMsgError("%s:%u: Junk at end of line", pMap->pszMapFile, pMap->iLine);
1613 }
1614 return false;
1615 }
1616}
1617
1618
1619/**
1620 * Sorts the segment array by flat address and adds them to the debug module.
1621 *
1622 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1623 */
1624static bool mapSortAndAddSegments(void)
1625{
1626 for (uint32_t i = 0; i < g_cSegs; i++)
1627 {
1628 for (uint32_t j = i + 1; j < g_cSegs; j++)
1629 if (g_aSegs[j].uFlatAddr < g_aSegs[i].uFlatAddr)
1630 {
1631 BIOSSEG Tmp = g_aSegs[i];
1632 g_aSegs[i] = g_aSegs[j];
1633 g_aSegs[j] = Tmp;
1634 }
1635 if (g_cVerbose > 0)
1636 RTStrmPrintf(g_pStdErr, "segment at %08x / %04x:%04x LB %04x %s / %s / %s\n",
1637 g_aSegs[i].uFlatAddr,
1638 g_aSegs[i].Address.sel,
1639 g_aSegs[i].Address.off,
1640 g_aSegs[i].cb,
1641 g_aSegs[i].szName,
1642 g_aSegs[i].szClass,
1643 g_aSegs[i].szGroup);
1644
1645 RTDBGSEGIDX idx = i;
1646 int rc = RTDbgModSegmentAdd(g_hMapMod, g_aSegs[i].uFlatAddr, g_aSegs[i].cb, g_aSegs[i].szName, 0 /*fFlags*/, &idx);
1647 if (RT_FAILURE(rc))
1648 {
1649 RTMsgError("RTDbgModSegmentAdd failed on %s: %Rrc", g_aSegs[i].szName);
1650 return false;
1651 }
1652 }
1653 return true;
1654}
1655
1656
1657/**
1658 * Parses a segment list.
1659 *
1660 * @returns @c true on success, @c false + msg on failure, @c false on eof.
1661 * @param pMap The map file handle.
1662 */
1663static bool mapParseSymbols(PBIOSMAP pMap)
1664{
1665 for (;;)
1666 {
1667 if (!mapReadLineStripRight(pMap, NULL))
1668 return false;
1669
1670 /* The end? The line should be empty. Expectes segment name to not
1671 start with a space. */
1672 if (!pMap->szLine[0] || RT_C_IS_SPACE(pMap->szLine[0]))
1673 {
1674 if (!pMap->szLine[0])
1675 return true;
1676 return mapError(pMap, "Malformed symbol line");
1677 }
1678
1679 if (!strncmp(pMap->szLine, RT_STR_TUPLE("Module: ")))
1680 {
1681 /* Parse the module line. */
1682 size_t offObj = sizeof("Module: ") - 1;
1683 while (RT_C_IS_SPACE(pMap->szLine[offObj]))
1684 offObj++;
1685 size_t offSrc = offObj;
1686 char ch;
1687 while ((ch = pMap->szLine[offSrc]) != '(' && ch != '\0')
1688 offSrc++;
1689 size_t cchObj = offSrc - offObj;
1690
1691 offSrc++;
1692 size_t cchSrc = offSrc;
1693 while ((ch = pMap->szLine[cchSrc]) != ')' && ch != '\0')
1694 cchSrc++;
1695 cchSrc -= offSrc;
1696 if (ch != ')')
1697 return mapError(pMap, "Symbol/Module line parse error");
1698
1699 PBIOSOBJFILE pObjFile = (PBIOSOBJFILE)RTMemAllocZ(sizeof(*pObjFile) + cchSrc + cchObj + 2);
1700 if (!pObjFile)
1701 return mapError(pMap, "Out of memory");
1702 char *psz = (char *)(pObjFile + 1);
1703 pObjFile->pszObject = psz;
1704 memcpy(psz, &pMap->szLine[offObj], cchObj);
1705 psz += cchObj;
1706 *psz++ = '\0';
1707 pObjFile->pszSource = psz;
1708 memcpy(psz, &pMap->szLine[offSrc], cchSrc);
1709 psz[cchSrc] = '\0';
1710 RTListAppend(&g_ObjList, &pObjFile->Node);
1711 }
1712 else
1713 {
1714 /* Parse the segment line. */
1715 RTFAR16 Addr;
1716 char *psz = pMap->szLine;
1717 if (!mapParseAddress(&psz, &Addr))
1718 return mapError(pMap, "Symbol address parser error");
1719
1720 char szName[4096];
1721 if (!mapParseWord(&psz, szName, sizeof(szName)))
1722 return mapError(pMap, "Symbol name parser error");
1723
1724 uint32_t uFlatAddr = ((uint32_t)Addr.sel << 4) + Addr.off;
1725 if (uFlatAddr != 0)
1726 {
1727 int rc = RTDbgModSymbolAdd(g_hMapMod, szName, RTDBGSEGIDX_RVA, uFlatAddr, 0 /*cb*/, 0 /*fFlags*/, NULL);
1728 if (RT_FAILURE(rc) && rc != VERR_DBG_ADDRESS_CONFLICT)
1729 {
1730 /* HACK ALERT! For dealing with lables at segment size. */ /** @todo fix end labels. */
1731 rc = RTDbgModSymbolAdd(g_hMapMod, szName, RTDBGSEGIDX_RVA, uFlatAddr - 1, 0 /*cb*/, 0 /*fFlags*/, NULL);
1732 if (RT_FAILURE(rc) && rc != VERR_DBG_ADDRESS_CONFLICT)
1733 return mapError(pMap, "RTDbgModSymbolAdd failed: %Rrc", rc);
1734 }
1735
1736 if (g_cVerbose > 2)
1737 RTStrmPrintf(g_pStdErr, "read symbol - %08x %s\n", uFlatAddr, szName);
1738 while (RT_C_IS_SPACE(*psz))
1739 psz++;
1740 if (*psz)
1741 return mapError(pMap, "Junk at end of line");
1742 }
1743
1744 }
1745 }
1746}
1747
1748
1749/**
1750 * Parses the given map file.
1751 *
1752 * @returns RTEXITCODE_SUCCESS and lots of globals, or RTEXITCODE_FAILURE and a
1753 * error message.
1754 * @param pMap The map file handle.
1755 */
1756static RTEXITCODE mapParseFile(PBIOSMAP pMap)
1757{
1758 int rc = RTDbgModCreate(&g_hMapMod, "VBoxBios", 0 /*cbSeg*/, 0 /*fFlags*/);
1759 if (RT_FAILURE(rc))
1760 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTDbgModCreate failed: %Rrc", rc);
1761
1762 /*
1763 * Read the header.
1764 */
1765 if (!mapReadLine(pMap))
1766 return RTEXITCODE_FAILURE;
1767 if (strncmp(pMap->szLine, RT_STR_TUPLE("Open Watcom Linker Version")))
1768 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unexpected map-file header: '%s'", pMap->szLine);
1769 if ( !mapSkipNonEmptyLines(pMap)
1770 || !mapSkipEmptyLines(pMap))
1771 return RTEXITCODE_FAILURE;
1772
1773 /*
1774 * Skip groups.
1775 */
1776 if (!mapSkipThruColumnHeadings(pMap, "Groups", 3, "Group", "Address", "Size", NULL))
1777 return RTEXITCODE_FAILURE;
1778 if (!mapSkipNonEmptyLines(pMap))
1779 return RTEXITCODE_FAILURE;
1780
1781 /*
1782 * Parse segments.
1783 */
1784 if (!mapSkipThruColumnHeadings(pMap, "Segments", 5, "Segment", "Class", "Group", "Address", "Size"))
1785 return RTEXITCODE_FAILURE;
1786 if (!mapParseSegments(pMap))
1787 return RTEXITCODE_FAILURE;
1788 if (!mapSortAndAddSegments())
1789 return RTEXITCODE_FAILURE;
1790
1791 /*
1792 * Parse symbols.
1793 */
1794 if (!mapSkipThruColumnHeadings(pMap, "Memory Map", 2, "Address", "Symbol"))
1795 return RTEXITCODE_FAILURE;
1796 if (!mapParseSymbols(pMap))
1797 return RTEXITCODE_FAILURE;
1798
1799 /* Ignore the rest of the file. */
1800 return RTEXITCODE_SUCCESS;
1801}
1802
1803
1804/**
1805 * Parses the linker map file for the BIOS.
1806 *
1807 * This is generated by the Watcom linker.
1808 *
1809 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
1810 * @param pszBiosMap Path to the map file.
1811 */
1812static RTEXITCODE ParseMapFile(const char *pszBiosMap)
1813{
1814 BIOSMAP Map;
1815 Map.pszMapFile = pszBiosMap;
1816 Map.hStrm = NULL;
1817 Map.iLine = 0;
1818 Map.fEof = false;
1819 Map.cch = 0;
1820 Map.offNW = 0;
1821 int rc = RTStrmOpen(pszBiosMap, "r", &Map.hStrm);
1822 if (RT_FAILURE(rc))
1823 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening '%s': %Rrc", pszBiosMap, rc);
1824 RTEXITCODE rcExit = mapParseFile(&Map);
1825 RTStrmClose(Map.hStrm);
1826 return rcExit;
1827}
1828
1829
1830/**
1831 * Reads the BIOS image into memory (g_pbImg and g_cbImg).
1832 *
1833 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
1834 * @param pszBiosImg Path to the image file.
1835 */
1836static RTEXITCODE ReadBiosImage(const char *pszBiosImg)
1837{
1838 void *pvImg;
1839 size_t cbImg;
1840 int rc = RTFileReadAll(pszBiosImg, &pvImg, &cbImg);
1841 if (RT_FAILURE(rc))
1842 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error reading '%s': %Rrc", pszBiosImg, rc);
1843
1844 size_t cbImgExpect;
1845 switch (g_enmBiosType)
1846 {
1847 case kBiosType_System: cbImgExpect = _64K; break;
1848 case kBiosType_Vga: cbImgExpect = _32K; break;
1849 default: cbImgExpect = 0; break;
1850 }
1851 if (cbImg != cbImgExpect)
1852 {
1853 RTFileReadAllFree(pvImg, cbImg);
1854 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The BIOS image %u bytes intead of %u bytes", cbImg, cbImgExpect);
1855 }
1856
1857 g_pbImg = (uint8_t *)pvImg;
1858 g_cbImg = cbImg;
1859 return RTEXITCODE_SUCCESS;
1860}
1861
1862
1863int main(int argc, char **argv)
1864{
1865 int rc = RTR3InitExe(argc, &argv, 0);
1866 if (RT_FAILURE(rc))
1867 return RTMsgInitFailure(rc);
1868
1869 RTListInit(&g_ObjList);
1870
1871 /*
1872 * Option config.
1873 */
1874 static RTGETOPTDEF const s_aOpts[] =
1875 {
1876 { "--bios-image", 'i', RTGETOPT_REQ_STRING },
1877 { "--bios-map", 'm', RTGETOPT_REQ_STRING },
1878 { "--bios-sym", 's', RTGETOPT_REQ_STRING },
1879 { "--bios-type", 't', RTGETOPT_REQ_STRING },
1880 { "--output", 'o', RTGETOPT_REQ_STRING },
1881 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
1882 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
1883 };
1884
1885 const char *pszBiosMap = NULL;
1886 const char *pszBiosSym = NULL;
1887 const char *pszBiosImg = NULL;
1888 const char *pszOutput = NULL;
1889
1890 RTGETOPTUNION ValueUnion;
1891 RTGETOPTSTATE GetOptState;
1892 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1893 AssertReleaseRCReturn(rc, RTEXITCODE_FAILURE);
1894
1895 /*
1896 * Process the options.
1897 */
1898 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1899 {
1900 switch (rc)
1901 {
1902 case 'i':
1903 if (pszBiosImg)
1904 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-image is given more than once");
1905 pszBiosImg = ValueUnion.psz;
1906 break;
1907
1908 case 'm':
1909 if (pszBiosMap)
1910 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-map is given more than once");
1911 pszBiosMap = ValueUnion.psz;
1912 break;
1913
1914 case 's':
1915 if (pszBiosSym)
1916 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-sym is given more than once");
1917 pszBiosSym = ValueUnion.psz;
1918 break;
1919
1920 case 'o':
1921 if (pszOutput)
1922 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--output is given more than once");
1923 pszOutput = ValueUnion.psz;
1924 break;
1925
1926 case 't':
1927 if (!strcmp(ValueUnion.psz, "system"))
1928 {
1929 g_enmBiosType = kBiosType_System;
1930 g_uBiosFlatBase = 0xf0000;
1931 }
1932 else if (!strcmp(ValueUnion.psz, "vga"))
1933 {
1934 g_enmBiosType = kBiosType_Vga;
1935 g_uBiosFlatBase = 0xc0000;
1936 }
1937 else
1938 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown bios type '%s'", ValueUnion.psz);
1939 break;
1940
1941 case 'v':
1942 g_cVerbose++;
1943 break;
1944
1945 case 'q':
1946 g_cVerbose = 0;
1947 break;
1948
1949 case 'H':
1950 RTPrintf("usage: %Rbn --bios-image <file.img> --bios-map <file.map> [--output <file.asm>]\n",
1951 argv[0]);
1952 return RTEXITCODE_SUCCESS;
1953
1954 case 'V':
1955 {
1956 /* The following is assuming that svn does it's job here. */
1957 RTPrintf("r%u\n", RTBldCfgRevision());
1958 return RTEXITCODE_SUCCESS;
1959 }
1960
1961 default:
1962 return RTGetOptPrintError(rc, &ValueUnion);
1963 }
1964 }
1965
1966 /*
1967 * Got it all?
1968 */
1969 if (!pszBiosImg)
1970 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-image is required");
1971 if (!pszBiosMap)
1972 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-map is required");
1973 if (!pszBiosSym)
1974 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--bios-sym is required");
1975
1976 /*
1977 * Do the job.
1978 */
1979 RTEXITCODE rcExit;
1980 rcExit = ReadBiosImage(pszBiosImg);
1981 if (rcExit == RTEXITCODE_SUCCESS)
1982 rcExit = ParseMapFile(pszBiosMap);
1983 if (rcExit == RTEXITCODE_SUCCESS)
1984 rcExit = ParseSymFile(pszBiosSym);
1985 if (rcExit == RTEXITCODE_SUCCESS)
1986 rcExit = OpenOutputFile(pszOutput);
1987 if (rcExit == RTEXITCODE_SUCCESS)
1988 rcExit = DisassembleBiosImage();
1989
1990 return rcExit;
1991}
1992
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