VirtualBox

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

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

update alternative BIOS sources

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