VirtualBox

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

Last change on this file since 47937 was 47937, checked in by vboxsync, 11 years ago

4.3 Beta 1

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