]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/EfiRom/EfiRom.c
d95864abc919f4538e968368f75e959a53d775bb
[mirror_edk2.git] / BaseTools / Source / C / EfiRom / EfiRom.c
1 /** @file
2 Utility program to create an EFI option ROM image from binary and EFI PE32 files.
3
4 Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "EfiUtilityMsgs.h"
16 #include "ParseInf.h"
17 #include "EfiRom.h"
18
19 UINT64 DebugLevel = 0;
20
21 int
22 main (
23 int Argc,
24 char *Argv[]
25 )
26 /*++
27
28 Routine Description:
29
30 Given an EFI image filename, create a ROM-able image by creating an option
31 ROM header and PCI data structure, filling them in, and then writing the
32 option ROM header + PCI data structure + EFI image out to the output file.
33
34 Arguments:
35
36 Argc - standard C main() argument count
37
38 Argv - standard C main() argument list
39
40 Returns:
41
42 0 success
43 non-zero otherwise
44
45 --*/
46 {
47 CHAR8 *Ext;
48 FILE *FptrOut;
49 UINT32 Status;
50 FILE_LIST *FList;
51 UINT32 TotalSize;
52 UINT32 Size;
53 CHAR8 *Ptr0;
54
55 SetUtilityName(UTILITY_NAME);
56
57 Status = STATUS_SUCCESS;
58 FptrOut = NULL;
59
60 //
61 // Parse the command line arguments
62 //
63 if (ParseCommandLine (Argc, Argv, &mOptions)) {
64 return STATUS_ERROR;
65 }
66
67 if (mOptions.Quiet) {
68 SetPrintLevel(40);
69 } else if (mOptions.Verbose) {
70 SetPrintLevel(15);
71 } else if (mOptions.Debug) {
72 SetPrintLevel(DebugLevel);
73 }
74
75 if (mOptions.Verbose) {
76 VerboseMsg("%s tool start.\n", UTILITY_NAME);
77 }
78
79 //
80 // If dumping an image, then do that and quit
81 //
82 if (mOptions.DumpOption == 1) {
83 if (mOptions.FileList != NULL) {
84 if ((Ptr0 = strstr ((CONST CHAR8 *) mOptions.FileList->FileName, DEFAULT_OUTPUT_EXTENSION)) != NULL) {
85 DumpImage (mOptions.FileList);
86 goto BailOut;
87 } else {
88 Error (NULL, 0, 1002, "No PciRom input file", "No *.rom input file");
89 goto BailOut;
90 }
91 }
92 }
93 //
94 // Determine the output filename. Either what they specified on
95 // the command line, or the first input filename with a different extension.
96 //
97 if (!mOptions.OutFileName[0]) {
98 if (mOptions.FileList != NULL) {
99 strcpy (mOptions.OutFileName, mOptions.FileList->FileName);
100 //
101 // Find the last . on the line and replace the filename extension with
102 // the default
103 //
104 for (Ext = mOptions.OutFileName + strlen (mOptions.OutFileName) - 1;
105 (Ext >= mOptions.OutFileName) && (*Ext != '.') && (*Ext != '\\');
106 Ext--
107 )
108 ;
109 //
110 // If dot here, then insert extension here, otherwise append
111 //
112 if (*Ext != '.') {
113 Ext = mOptions.OutFileName + strlen (mOptions.OutFileName);
114 }
115
116 strcpy (Ext, DEFAULT_OUTPUT_EXTENSION);
117 }
118 }
119 //
120 // Make sure we don't have the same filename for input and output files
121 //
122 for (FList = mOptions.FileList; FList != NULL; FList = FList->Next) {
123 if (stricmp (mOptions.OutFileName, FList->FileName) == 0) {
124 Status = STATUS_ERROR;
125 Error (NULL, 0, 1002, "Invalid input parameter", "Input and output file names must be different - %s = %s.", FList->FileName, mOptions.OutFileName);
126 goto BailOut;
127 }
128 }
129 //
130 // Now open our output file
131 //
132 if ((FptrOut = fopen (LongFilePath (mOptions.OutFileName), "wb")) == NULL) {
133 Error (NULL, 0, 0001, "Error opening file", "Error opening file %s", mOptions.OutFileName);
134 goto BailOut;
135 }
136 //
137 // Process all our files
138 //
139 TotalSize = 0;
140 for (FList = mOptions.FileList; FList != NULL; FList = FList->Next) {
141 Size = 0;
142 if ((FList->FileFlags & FILE_FLAG_EFI) != 0) {
143 if (mOptions.Verbose) {
144 VerboseMsg("Processing EFI file %s\n", FList->FileName);
145 }
146
147 Status = ProcessEfiFile (FptrOut, FList, mOptions.VendId, mOptions.DevId, &Size);
148 } else if ((FList->FileFlags & FILE_FLAG_BINARY) !=0 ) {
149 if (mOptions.Verbose) {
150 VerboseMsg("Processing binary file %s\n", FList->FileName);
151 }
152
153 Status = ProcessBinFile (FptrOut, FList, &Size);
154 } else {
155 Error (NULL, 0, 2000, "Invalid parameter", "File type not specified, it must be either an EFI or binary file: %s.", FList->FileName);
156 Status = STATUS_ERROR;
157 }
158
159 if (mOptions.Verbose) {
160 VerboseMsg(" Output size = 0x%X\n", (unsigned) Size);
161 }
162
163 if (Status != STATUS_SUCCESS) {
164 break;
165 }
166
167 TotalSize += Size;
168 }
169 //
170 // Check total size
171 //
172 if (TotalSize > MAX_OPTION_ROM_SIZE) {
173 Error (NULL, 0, 2000, "Invalid parameter", "Option ROM image size exceeds limit of 0x%X bytes.", MAX_OPTION_ROM_SIZE);
174 Status = STATUS_ERROR;
175 }
176
177 BailOut:
178 if (Status == STATUS_SUCCESS) {
179 if (FptrOut != NULL) {
180 fclose (FptrOut);
181 }
182 //
183 // Clean up our file list
184 //
185 while (mOptions.FileList != NULL) {
186 FList = mOptions.FileList->Next;
187 free (mOptions.FileList);
188 mOptions.FileList = FList;
189 }
190 }
191
192 if (mOptions.Verbose) {
193 VerboseMsg("%s tool done with return code is 0x%x.\n", UTILITY_NAME, GetUtilityStatus ());
194 }
195
196 return GetUtilityStatus ();
197 }
198
199 static
200 int
201 ProcessBinFile (
202 FILE *OutFptr,
203 FILE_LIST *InFile,
204 UINT32 *Size
205 )
206 /*++
207
208 Routine Description:
209
210 Process a binary input file.
211
212 Arguments:
213
214 OutFptr - file pointer to output binary ROM image file we're creating
215 InFile - structure contains information on the binary file to process
216 Size - pointer to where to return the size added to the output file
217
218 Returns:
219
220 0 - successful
221
222 --*/
223 {
224 FILE *InFptr;
225 UINT32 TotalSize;
226 UINT32 FileSize;
227 UINT8 *Buffer;
228 UINT32 Status;
229 PCI_EXPANSION_ROM_HEADER *RomHdr;
230 PCI_DATA_STRUCTURE *PciDs23;
231 PCI_3_0_DATA_STRUCTURE *PciDs30;
232 UINT32 Index;
233 UINT8 ByteCheckSum;
234 UINT16 CodeType;
235
236 PciDs23 = NULL;
237 PciDs30 = NULL;
238 Status = STATUS_SUCCESS;
239
240 //
241 // Try to open the input file
242 //
243 if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {
244 Error (NULL, 0, 0001, "Error opening file", InFile->FileName);
245 return STATUS_ERROR;
246 }
247 //
248 // Seek to the end of the input file and get the file size. Then allocate
249 // a buffer to read it in to.
250 //
251 fseek (InFptr, 0, SEEK_END);
252 FileSize = ftell (InFptr);
253 if (mOptions.Verbose) {
254 VerboseMsg(" File size = 0x%X\n", (unsigned) FileSize);
255 }
256
257 fseek (InFptr, 0, SEEK_SET);
258 Buffer = (UINT8 *) malloc (FileSize);
259 if (Buffer == NULL) {
260 Error (NULL, 0, 4003, "Resource", "memory cannot be allocated!");
261 Status = STATUS_ERROR;
262 goto BailOut;
263 }
264
265 if (fread (Buffer, FileSize, 1, InFptr) != 1) {
266 Error (NULL, 0, 2000, "Invalid", "Failed to read all bytes from input file.");
267 Status = STATUS_ERROR;
268 goto BailOut;
269 }
270 //
271 // Total size must be an even multiple of 512 bytes, and can't exceed
272 // the option ROM image size.
273 //
274 TotalSize = FileSize;
275 if (TotalSize & 0x1FF) {
276 TotalSize = (TotalSize + 0x200) &~0x1ff;
277 }
278
279 if (TotalSize > MAX_OPTION_ROM_SIZE) {
280 Error (NULL, 0, 3001, "Invalid", "Option ROM image %s size exceeds limit of 0x%X bytes.", InFile->FileName, MAX_OPTION_ROM_SIZE);
281 Status = STATUS_ERROR;
282 goto BailOut;
283 }
284 //
285 // Return the size to the caller so they can keep track of the running total.
286 //
287 *Size = TotalSize;
288
289 //
290 // Crude check to make sure it's a legitimate ROM image
291 //
292 RomHdr = (PCI_EXPANSION_ROM_HEADER *) Buffer;
293 if (RomHdr->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {
294 Error (NULL, 0, 2000, "Invalid parameter", "ROM image file has an invalid ROM signature.");
295 Status = STATUS_ERROR;
296 goto BailOut;
297 }
298 //
299 // Make sure the pointer to the PCI data structure is within the size of the image.
300 // Then check it for valid signature.
301 //
302 if ((RomHdr->PcirOffset > FileSize) || (RomHdr->PcirOffset == 0)) {
303 Error (NULL, 0, 2000, "Invalid parameter", "Invalid PCI data structure offset.");
304 Status = STATUS_ERROR;
305 goto BailOut;
306 }
307
308 //
309 // Check the header is conform to PCI2.3 or PCI3.0
310 //
311 if (mOptions.Pci23 == 1) {
312 PciDs23 = (PCI_DATA_STRUCTURE *) (Buffer + RomHdr->PcirOffset);
313 if (PciDs23->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {
314 Error (NULL, 0, 2000, "Invalid parameter", "PCI data structure has an invalid signature.");
315 Status = STATUS_ERROR;
316 goto BailOut;
317 }
318 } else {
319 //
320 // Default setting is PCI3.0 header
321 //
322 PciDs30 = (PCI_3_0_DATA_STRUCTURE *)(Buffer + RomHdr->PcirOffset);
323 if (PciDs30->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {
324 Error (NULL, 0, 2000, "Invalid parameter", "PCI data structure has an invalid signature.");
325 Status = STATUS_ERROR;
326 goto BailOut;
327 }
328 }
329
330 //
331 // ReSet Option Rom size
332 //
333 if (mOptions.Pci23 == 1) {
334 PciDs23->ImageLength = (UINT16) (TotalSize / 512);
335 CodeType = PciDs23->CodeType;
336 } else {
337 PciDs30->ImageLength = (UINT16) (TotalSize / 512);
338 CodeType = PciDs30->CodeType;
339 }
340
341 //
342 // If this is the last image, then set the LAST bit unless requested not
343 // to via the command-line -n argument. Otherwise, make sure you clear it.
344 //
345 if ((InFile->Next == NULL) && (mOptions.NoLast == 0)) {
346 if (mOptions.Pci23 == 1) {
347 PciDs23->Indicator = INDICATOR_LAST;
348 } else {
349 PciDs30->Indicator = INDICATOR_LAST;
350 }
351 } else {
352 if (mOptions.Pci23 == 1) {
353 PciDs23->Indicator = 0;
354 } else {
355 PciDs30->Indicator = 0;
356 }
357 }
358
359 if (CodeType != PCI_CODE_TYPE_EFI_IMAGE) {
360 ByteCheckSum = 0;
361 for (Index = 0; Index < FileSize - 1; Index++) {
362 ByteCheckSum = (UINT8) (ByteCheckSum + Buffer[Index]);
363 }
364
365 Buffer[FileSize - 1] = (UINT8) ((~ByteCheckSum) + 1);
366 if (mOptions.Verbose) {
367 VerboseMsg(" Checksum = %02x\n\n", Buffer[FileSize - 1]);
368 }
369 }
370
371 //
372 // Now copy the input file contents out to the output file
373 //
374 if (fwrite (Buffer, FileSize, 1, OutFptr) != 1) {
375 Error (NULL, 0, 0005, "Failed to write all file bytes to output file.", NULL);
376 Status = STATUS_ERROR;
377 goto BailOut;
378 }
379
380 TotalSize -= FileSize;
381 //
382 // Pad the rest of the image to make it a multiple of 512 bytes
383 //
384 while (TotalSize > 0) {
385 putc (~0, OutFptr);
386 TotalSize--;
387 }
388
389 BailOut:
390 if (InFptr != NULL) {
391 fclose (InFptr);
392 }
393
394 if (Buffer != NULL) {
395 free (Buffer);
396 }
397 //
398 // Print the file name if errors occurred
399 //
400 if (Status != STATUS_SUCCESS) {
401 Error (NULL, 0, 0003, "Error", "Error parsing file: %s", InFile->FileName);
402 }
403
404 return Status;
405 }
406
407 static
408 int
409 ProcessEfiFile (
410 FILE *OutFptr,
411 FILE_LIST *InFile,
412 UINT16 VendId,
413 UINT16 DevId,
414 UINT32 *Size
415 )
416 /*++
417
418 Routine Description:
419
420 Process a PE32 EFI file.
421
422 Arguments:
423
424 OutFptr - file pointer to output binary ROM image file we're creating
425 InFile - structure contains information on the PE32 file to process
426 VendId - vendor ID as required in the option ROM header
427 DevId - device ID as required in the option ROM header
428 Size - pointer to where to return the size added to the output file
429
430 Returns:
431
432 0 - successful
433
434 --*/
435 {
436 UINT32 Status;
437 FILE *InFptr;
438 EFI_PCI_EXPANSION_ROM_HEADER RomHdr;
439 PCI_DATA_STRUCTURE PciDs23;
440 PCI_3_0_DATA_STRUCTURE PciDs30;
441 UINT32 FileSize;
442 UINT32 CompressedFileSize;
443 UINT8 *Buffer;
444 UINT8 *CompressedBuffer;
445 UINT8 *TempBufferPtr;
446 UINT32 TotalSize;
447 UINT32 HeaderSize;
448 UINT16 MachineType;
449 UINT16 SubSystem;
450 UINT32 HeaderPadBytes;
451 UINT32 PadBytesBeforeImage;
452 UINT32 PadBytesAfterImage;
453
454 //
455 // Try to open the input file
456 //
457 if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {
458 Error (NULL, 0, 0001, "Open file error", "Error opening file: %s", InFile->FileName);
459 return STATUS_ERROR;
460 }
461 //
462 // Initialize our buffer pointers to null.
463 //
464 Buffer = NULL;
465 CompressedBuffer = NULL;
466
467 //
468 // Double-check the file to make sure it's what we expect it to be
469 //
470 Status = CheckPE32File (InFptr, &MachineType, &SubSystem);
471 if (Status != STATUS_SUCCESS) {
472 goto BailOut;
473 }
474 //
475 // Seek to the end of the input file and get the file size
476 //
477 fseek (InFptr, 0, SEEK_END);
478 FileSize = ftell (InFptr);
479
480 //
481 // Get the size of the headers we're going to put in front of the image. The
482 // EFI header must be aligned on a 4-byte boundary, so pad accordingly.
483 //
484 if (sizeof (RomHdr) & 0x03) {
485 HeaderPadBytes = 4 - (sizeof (RomHdr) & 0x03);
486 } else {
487 HeaderPadBytes = 0;
488 }
489
490 //
491 // For Pci3.0 to use the different data structure.
492 //
493 if (mOptions.Pci23 == 1) {
494 HeaderSize = sizeof (PCI_DATA_STRUCTURE) + HeaderPadBytes + sizeof (EFI_PCI_EXPANSION_ROM_HEADER);
495 } else {
496 HeaderSize = sizeof (PCI_3_0_DATA_STRUCTURE) + HeaderPadBytes + sizeof (EFI_PCI_EXPANSION_ROM_HEADER);
497 }
498
499 if (mOptions.Verbose) {
500 VerboseMsg(" File size = 0x%X\n", (unsigned) FileSize);
501 }
502 //
503 // Allocate memory for the entire file (in case we have to compress), then
504 // seek back to the beginning of the file and read it into our buffer.
505 //
506 Buffer = (UINT8 *) malloc (FileSize);
507 if (Buffer == NULL) {
508 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
509 Status = STATUS_ERROR;
510 goto BailOut;
511 }
512
513 fseek (InFptr, 0, SEEK_SET);
514 if (fread (Buffer, FileSize, 1, InFptr) != 1) {
515 Error (NULL, 0, 0004, "Error reading file", "File %s", InFile->FileName);
516 Status = STATUS_ERROR;
517 goto BailOut;
518 }
519 //
520 // Now determine the size of the final output file. It's either the header size
521 // plus the file's size, or the header size plus the compressed file size.
522 //
523 if ((InFile->FileFlags & FILE_FLAG_COMPRESS) != 0) {
524 //
525 // Allocate a buffer into which we can compress the image, compress it,
526 // and use that size as the new size.
527 //
528 CompressedBuffer = (UINT8 *) malloc (FileSize);
529 if (CompressedBuffer == NULL) {
530 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
531 Status = STATUS_ERROR;
532 goto BailOut;
533 }
534
535 CompressedFileSize = FileSize;
536 Status = EfiCompress (Buffer, FileSize, CompressedBuffer, &CompressedFileSize);
537 if (Status != STATUS_SUCCESS) {
538 Error (NULL, 0, 0007, "Error compressing file!", NULL);
539 goto BailOut;
540 }
541 //
542 // Now compute the size, then swap buffer pointers.
543 //
544 if (mOptions.Verbose) {
545 VerboseMsg(" Comp size = 0x%X\n", (unsigned) CompressedFileSize);
546 }
547
548 TotalSize = CompressedFileSize + HeaderSize;
549 FileSize = CompressedFileSize;
550 TempBufferPtr = Buffer;
551 Buffer = CompressedBuffer;
552 CompressedBuffer = TempBufferPtr;
553 } else {
554 TotalSize = FileSize + HeaderSize;
555 }
556 //
557 // Total size must be an even multiple of 512 bytes
558 //
559 if (TotalSize & 0x1FF) {
560 TotalSize = (TotalSize + 0x200) &~0x1ff;
561 }
562 //
563 // Workaround:
564 // If compressed, put the pad bytes after the image,
565 // else put the pad bytes before the image.
566 //
567 if ((InFile->FileFlags & FILE_FLAG_COMPRESS) != 0) {
568 PadBytesBeforeImage = 0;
569 PadBytesAfterImage = TotalSize - (FileSize + HeaderSize);
570 } else {
571 PadBytesBeforeImage = TotalSize - (FileSize + HeaderSize);
572 PadBytesAfterImage = 0;
573 }
574 //
575 // Check size
576 //
577 if (TotalSize > MAX_OPTION_ROM_SIZE) {
578 Error (NULL, 0, 2000, "Invalid", "Option ROM image %s size exceeds limit of 0x%X bytes.", InFile->FileName, MAX_OPTION_ROM_SIZE);
579 Status = STATUS_ERROR;
580 goto BailOut;
581 }
582 //
583 // Return the size to the caller so they can keep track of the running total.
584 //
585 *Size = TotalSize;
586
587 //
588 // Now fill in the ROM header. These values come from chapter 18 of the
589 // EFI 1.02 specification.
590 //
591 memset (&RomHdr, 0, sizeof (RomHdr));
592 RomHdr.Signature = PCI_EXPANSION_ROM_HEADER_SIGNATURE;
593 RomHdr.InitializationSize = (UINT16) (TotalSize / 512);
594 RomHdr.EfiSignature = EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE;
595 RomHdr.EfiSubsystem = SubSystem;
596 RomHdr.EfiMachineType = MachineType;
597 RomHdr.EfiImageHeaderOffset = (UINT16) (HeaderSize + PadBytesBeforeImage);
598 RomHdr.PcirOffset = (UINT16) (sizeof (RomHdr) + HeaderPadBytes);
599 //
600 // Set image as compressed or not
601 //
602 if (InFile->FileFlags & FILE_FLAG_COMPRESS) {
603 RomHdr.CompressionType = EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED;
604 }
605 //
606 // Fill in the PCI data structure
607 //
608 if (mOptions.Pci23 == 1) {
609 memset (&PciDs23, 0, sizeof (PCI_DATA_STRUCTURE));
610 } else {
611 memset (&PciDs30, 0, sizeof (PCI_3_0_DATA_STRUCTURE));
612 }
613
614 if (mOptions.Pci23 == 1) {
615 PciDs23.Signature = PCI_DATA_STRUCTURE_SIGNATURE;
616 PciDs23.VendorId = VendId;
617 PciDs23.DeviceId = DevId;
618 PciDs23.Length = (UINT16) sizeof (PCI_DATA_STRUCTURE);
619 PciDs23.Revision = 0;
620 //
621 // Class code and code revision from the command line (optional)
622 //
623 PciDs23.ClassCode[0] = (UINT8) InFile->ClassCode;
624 PciDs23.ClassCode[1] = (UINT8) (InFile->ClassCode >> 8);
625 PciDs23.ClassCode[2] = (UINT8) (InFile->ClassCode >> 16);
626 PciDs23.ImageLength = RomHdr.InitializationSize;
627 PciDs23.CodeRevision = InFile->CodeRevision;
628 PciDs23.CodeType = PCI_CODE_TYPE_EFI_IMAGE;
629 } else {
630 PciDs30.Signature = PCI_DATA_STRUCTURE_SIGNATURE;
631 PciDs30.VendorId = VendId;
632 PciDs30.DeviceId = DevId;
633 PciDs30.DeviceListOffset = 0; // to be fixed
634 PciDs30.Length = (UINT16) sizeof (PCI_3_0_DATA_STRUCTURE);
635 PciDs30.Revision = 0x3;
636 //
637 // Class code and code revision from the command line (optional)
638 //
639 PciDs30.ClassCode[0] = (UINT8) InFile->ClassCode;
640 PciDs30.ClassCode[1] = (UINT8) (InFile->ClassCode >> 8);
641 PciDs30.ClassCode[2] = (UINT8) (InFile->ClassCode >> 16);
642 PciDs30.ImageLength = RomHdr.InitializationSize;
643 PciDs30.CodeRevision = InFile->CodeRevision;
644 PciDs30.CodeType = PCI_CODE_TYPE_EFI_IMAGE;
645 PciDs30.MaxRuntimeImageLength = 0; // to be fixed
646 PciDs30.ConfigUtilityCodeHeaderOffset = 0; // to be fixed
647 PciDs30.DMTFCLPEntryPointOffset = 0; // to be fixed
648 }
649 //
650 // If this is the last image, then set the LAST bit unless requested not
651 // to via the command-line -n argument.
652 //
653 if ((InFile->Next == NULL) && (mOptions.NoLast == 0)) {
654 if (mOptions.Pci23 == 1) {
655 PciDs23.Indicator = INDICATOR_LAST;
656 } else {
657 PciDs30.Indicator = INDICATOR_LAST;}
658 } else {
659 if (mOptions.Pci23 == 1) {
660 PciDs23.Indicator = 0;
661 } else {
662 PciDs30.Indicator = 0;
663 }
664 }
665 //
666 // Write the ROM header to the output file
667 //
668 if (fwrite (&RomHdr, sizeof (RomHdr), 1, OutFptr) != 1) {
669 Error (NULL, 0, 0002, "Failed to write ROM header to output file!", NULL);
670 Status = STATUS_ERROR;
671 goto BailOut;
672 }
673
674 //
675 // Write pad bytes to align the PciDs
676 //
677 while (HeaderPadBytes > 0) {
678 if (putc (0, OutFptr) == EOF) {
679 Error (NULL, 0, 0002, "Failed to write ROM header pad bytes to output file!", NULL);
680 Status = STATUS_ERROR;
681 goto BailOut;
682 }
683
684 HeaderPadBytes--;
685 }
686 //
687 // Write the PCI data structure header to the output file
688 //
689 if (mOptions.Pci23 == 1) {
690 if (fwrite (&PciDs23, sizeof (PciDs23), 1, OutFptr) != 1) {
691 Error (NULL, 0, 0002, "Failed to write PCI ROM header to output file!", NULL);
692 Status = STATUS_ERROR;
693 goto BailOut;
694 }
695 } else {
696 if (fwrite (&PciDs30, sizeof (PciDs30), 1, OutFptr) != 1) {
697 Error (NULL, 0, 0002, "Failed to write PCI ROM header to output file!", NULL);
698 Status = STATUS_ERROR;
699 goto BailOut;
700 }
701 }
702
703 //
704 // Pad head to make it a multiple of 512 bytes
705 //
706 while (PadBytesBeforeImage > 0) {
707 if (putc (~0, OutFptr) == EOF) {
708 Error (NULL, 0, 2000, "Failed to write trailing pad bytes output file!", NULL);
709 Status = STATUS_ERROR;
710 goto BailOut;
711 }
712 PadBytesBeforeImage--;
713 }
714 //
715 // Now dump the input file's contents to the output file
716 //
717 if (fwrite (Buffer, FileSize, 1, OutFptr) != 1) {
718 Error (NULL, 0, 0002, "Failed to write all file bytes to output file!", NULL);
719 Status = STATUS_ERROR;
720 goto BailOut;
721 }
722
723 //
724 // Pad the rest of the image to make it a multiple of 512 bytes
725 //
726 while (PadBytesAfterImage > 0) {
727 if (putc (~0, OutFptr) == EOF) {
728 Error (NULL, 0, 2000, "Failed to write trailing pad bytes output file!", NULL);
729 Status = STATUS_ERROR;
730 goto BailOut;
731 }
732
733 PadBytesAfterImage--;
734 }
735
736 BailOut:
737 if (InFptr != NULL) {
738 fclose (InFptr);
739 }
740 //
741 // Free up our buffers
742 //
743 if (Buffer != NULL) {
744 free (Buffer);
745 }
746
747 if (CompressedBuffer != NULL) {
748 free (CompressedBuffer);
749 }
750 //
751 // Print the file name if errors occurred
752 //
753 if (Status != STATUS_SUCCESS) {
754 Error (NULL, 0, 0003, "Error parsing", "Error parsing file: %s", InFile->FileName);
755 }
756
757 return Status;
758 }
759
760 static
761 int
762 CheckPE32File (
763 FILE *Fptr,
764 UINT16 *MachineType,
765 UINT16 *SubSystem
766 )
767 /*++
768
769 Routine Description:
770
771 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
772 PE32 image file, and then return the machine type in the supplied pointer.
773
774 Arguments:
775
776 Fptr File pointer to the already-opened PE32 file
777 MachineType Location to stuff the machine type of the PE32 file. This is needed
778 because the image may be Itanium-based, IA32, or EBC.
779
780 Returns:
781
782 0 success
783 non-zero otherwise
784
785 --*/
786 {
787 EFI_IMAGE_DOS_HEADER DosHeader;
788 EFI_IMAGE_OPTIONAL_HEADER_UNION PeHdr;
789
790 //
791 // Position to the start of the file
792 //
793 fseek (Fptr, 0, SEEK_SET);
794
795 //
796 // Read the DOS header
797 //
798 if (fread (&DosHeader, sizeof (DosHeader), 1, Fptr) != 1) {
799 Error (NULL, 0, 0004, "Failed to read the DOS stub from the input file!", NULL);
800 return STATUS_ERROR;
801 }
802 //
803 // Check the magic number (0x5A4D)
804 //
805 if (DosHeader.e_magic != EFI_IMAGE_DOS_SIGNATURE) {
806 Error (NULL, 0, 2000, "Invalid parameter", "Input file does not appear to be a PE32 image (magic number)!");
807 return STATUS_ERROR;
808 }
809 //
810 // Position into the file and check the PE signature
811 //
812 fseek (Fptr, (long) DosHeader.e_lfanew, SEEK_SET);
813
814 //
815 // Read PE headers
816 //
817 if (fread (&PeHdr, sizeof (PeHdr), 1, Fptr) != 1) {
818 Error (NULL, 0, 0004, "Failed to read PE/COFF headers from input file!", NULL);
819 return STATUS_ERROR;
820 }
821
822
823 //
824 // Check the PE signature in the header "PE\0\0"
825 //
826 if (PeHdr.Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {
827 Error (NULL, 0, 2000, "Invalid parameter", "Input file does not appear to be a PE32 image (signature)!");
828 return STATUS_ERROR;
829 }
830
831 memcpy ((char *) MachineType, &PeHdr.Pe32.FileHeader.Machine, 2);
832
833 if (PeHdr.Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
834 *SubSystem = PeHdr.Pe32.OptionalHeader.Subsystem;
835 } else if (PeHdr.Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
836 *SubSystem = PeHdr.Pe32Plus.OptionalHeader.Subsystem;
837 } else {
838 Error (NULL, 0, 2000, "Invalid parameter", "Unable to find subsystem type!");
839 return STATUS_ERROR;
840 }
841
842 if (mOptions.Verbose) {
843 VerboseMsg(" Got subsystem = 0x%X from image\n", *SubSystem);
844 }
845
846 //
847 // File was successfully identified as a PE32
848 //
849 return STATUS_SUCCESS;
850 }
851
852 static
853 int
854 ParseCommandLine (
855 int Argc,
856 char *Argv[],
857 OPTIONS *Options
858 )
859 /*++
860
861 Routine Description:
862
863 Given the Argc/Argv program arguments, and a pointer to an options structure,
864 parse the command-line options and check their validity.
865
866
867 Arguments:
868
869 Argc - standard C main() argument count
870 Argv[] - standard C main() argument list
871 Options - pointer to a structure to store the options in
872
873 Returns:
874
875 STATUS_SUCCESS success
876 non-zero otherwise
877
878 --*/
879 {
880 FILE_LIST *FileList;
881 FILE_LIST *PrevFileList;
882 UINT32 FileFlags;
883 UINT32 ClassCode;
884 UINT32 CodeRevision;
885 EFI_STATUS Status;
886 BOOLEAN EfiRomFlag;
887 UINT64 TempValue;
888
889 FileFlags = 0;
890 EfiRomFlag = FALSE;
891
892 //
893 // Clear out the options
894 //
895 memset ((char *) Options, 0, sizeof (OPTIONS));
896
897 //
898 // To avoid compile warnings
899 //
900 FileList = PrevFileList = NULL;
901
902 ClassCode = 0;
903 CodeRevision = 0;
904 //
905 // Skip over the program name
906 //
907 Argc--;
908 Argv++;
909
910 //
911 // If no arguments, assume they want usage info
912 //
913 if (Argc == 0) {
914 Usage ();
915 return STATUS_ERROR;
916 }
917
918 if ((stricmp(Argv[0], "-h") == 0) || (stricmp(Argv[0], "--help") == 0)) {
919 Usage();
920 return STATUS_ERROR;
921 }
922
923 if ((stricmp(Argv[0], "--version") == 0)) {
924 Version();
925 return STATUS_ERROR;
926 }
927
928 //
929 // Process until no more arguments
930 //
931 while (Argc > 0) {
932 if (Argv[0][0] == '-') {
933 //
934 // Vendor ID specified with -f
935 //
936 if (stricmp (Argv[0], "-f") == 0) {
937 //
938 // Make sure there's another parameter
939 //
940 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
941 if (EFI_ERROR (Status)) {
942 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
943 return 1;
944 }
945 if (TempValue >= 0x10000) {
946 Error (NULL, 0, 2000, "Invalid option value", "Vendor Id %s out of range!", Argv[1]);
947 return 1;
948 }
949 Options->VendId = (UINT16) TempValue;
950 Options->VendIdValid = 1;
951
952 Argv++;
953 Argc--;
954 } else if (stricmp (Argv[0], "-i") == 0) {
955 //
956 // Device ID specified with -i
957 // Make sure there's another parameter
958 //
959 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
960 if (EFI_ERROR (Status)) {
961 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
962 return 1;
963 }
964 if (TempValue >= 0x10000) {
965 Error (NULL, 0, 2000, "Invalid option value", "Device Id %s out of range!", Argv[1]);
966 return 1;
967 }
968 Options->DevId = (UINT16) TempValue;
969 Options->DevIdValid = 1;
970
971 Argv++;
972 Argc--;
973 } else if ((stricmp (Argv[0], "-o") == 0) || (stricmp (Argv[0], "--output") == 0)) {
974 //
975 // Output filename specified with -o
976 // Make sure there's another parameter
977 //
978 if (Argv[1] == NULL || Argv[1][0] == '-') {
979 Error (NULL, 0, 2000, "Invalid parameter", "Missing output file name with %s option!", Argv[0]);
980 return STATUS_ERROR;
981 }
982 if (strlen (Argv[1]) > MAX_PATH - 1) {
983 Error (NULL, 0, 2000, "Invalid parameter", "Output file name %s is too long!", Argv[1]);
984 return STATUS_ERROR;
985 }
986 strncpy (Options->OutFileName, Argv[1], MAX_PATH - 1);
987 Options->OutFileName[MAX_PATH - 1] = 0;
988
989 Argv++;
990 Argc--;
991 } else if ((stricmp (Argv[0], "-h") == 0) || (stricmp (Argv[0], "--help") == 0)) {
992 //
993 // Help option
994 //
995 Usage ();
996 return STATUS_ERROR;
997 } else if (stricmp (Argv[0], "-b") == 0) {
998 //
999 // Specify binary files with -b
1000 //
1001 FileFlags = FILE_FLAG_BINARY;
1002 } else if ((stricmp (Argv[0], "-e") == 0) || (stricmp (Argv[0], "-ec") == 0)) {
1003 //
1004 // Specify EFI files with -e. Specify EFI-compressed with -c.
1005 //
1006 FileFlags = FILE_FLAG_EFI;
1007 if ((Argv[0][2] == 'c') || (Argv[0][2] == 'C')) {
1008 FileFlags |= FILE_FLAG_COMPRESS;
1009 }
1010 //
1011 // Specify not to set the LAST bit in the last file with -n
1012 //
1013 } else if (stricmp (Argv[0], "-n") == 0) {
1014 Options->NoLast = 1;
1015 } else if (((stricmp (Argv[0], "-v") == 0)) || ((stricmp (Argv[0], "--verbose") == 0))) {
1016 //
1017 // -v for verbose
1018 //
1019 Options->Verbose = 1;
1020 } else if (stricmp (Argv[0], "--debug") == 0) {
1021 Status = AsciiStringToUint64(Argv[1], FALSE, &DebugLevel);
1022 if (EFI_ERROR (Status)) {
1023 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1024 return 1;
1025 }
1026 if (DebugLevel > 9) {
1027 Error (NULL, 0, 2000, "Invalid option value", "Debug Level range is 0-9, current input level is %d", Argv[1]);
1028 return 1;
1029 }
1030 if (DebugLevel>=5 && DebugLevel<=9) {
1031 Options->Debug = TRUE;
1032 } else {
1033 Options->Debug = FALSE;
1034 }
1035 Argv++;
1036 Argc--;
1037 } else if ((stricmp (Argv[0], "--quiet") == 0) || (stricmp (Argv[0], "-q") == 0)) {
1038 Options->Quiet = TRUE;
1039 } else if ((stricmp (Argv[0], "--dump") == 0) || (stricmp (Argv[0], "-d") == 0)) {
1040 //
1041 // -dump for dumping a ROM image. In this case, say that the device id
1042 // and vendor id are valid so we don't have to specify bogus ones on the
1043 // command line.
1044 //
1045 Options->DumpOption = 1;
1046
1047 Options->VendIdValid = 1;
1048 Options->DevIdValid = 1;
1049 FileFlags = FILE_FLAG_BINARY;
1050 } else if ((stricmp (Argv[0], "-l") == 0) || (stricmp (Argv[0], "--class-code") == 0)) {
1051 //
1052 // Class code value for the next file in the list.
1053 // Make sure there's another parameter
1054 //
1055 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
1056 if (EFI_ERROR (Status)) {
1057 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1058 return 1;
1059 }
1060 ClassCode = (UINT32) TempValue;
1061 if (ClassCode & 0xFF000000) {
1062 Error (NULL, 0, 2000, "Invalid parameter", "Class code %s out of range!", Argv[1]);
1063 return STATUS_ERROR;
1064 }
1065 if (FileList != NULL && FileList->ClassCode == 0) {
1066 FileList->ClassCode = ClassCode;
1067 }
1068 Argv++;
1069 Argc--;
1070 } else if ((stricmp (Argv[0], "-r") == 0) || (stricmp (Argv[0], "--Revision") == 0)) {
1071 //
1072 // Code revision in the PCI data structure. The value is for the next
1073 // file in the list.
1074 // Make sure there's another parameter
1075 //
1076 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
1077 if (EFI_ERROR (Status)) {
1078 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1079 return 1;
1080 }
1081 CodeRevision = (UINT32) TempValue;
1082 if (CodeRevision & 0xFFFF0000) {
1083 Error (NULL, 0, 2000, "Invalid parameter", "Code revision %s out of range!", Argv[1]);
1084 return STATUS_ERROR;
1085 }
1086 if (FileList != NULL && FileList->CodeRevision == 0) {
1087 FileList->CodeRevision = (UINT16) CodeRevision;
1088 }
1089 Argv++;
1090 Argc--;
1091 } else if ((stricmp (Argv[0], "-p") == 0) || (stricmp (Argv[0], "--pci23") == 0)) {
1092 //
1093 // Default layout meets PCI 3.0 specifications, specifying this flag will for a PCI 2.3 layout.
1094 //
1095 mOptions.Pci23 = 1;
1096 } else {
1097 Error (NULL, 0, 2000, "Invalid parameter", "Invalid option specified: %s", Argv[0]);
1098 return STATUS_ERROR;
1099 }
1100 } else {
1101 //
1102 // Not a slash-option argument. Must be a file name. Make sure they've specified
1103 // -e or -b already.
1104 //
1105 if ((FileFlags & (FILE_FLAG_BINARY | FILE_FLAG_EFI)) == 0) {
1106 Error (NULL, 0, 2000, "Invalid parameter", "Missing -e or -b with input file %s!", Argv[0]);
1107 return STATUS_ERROR;
1108 }
1109 //
1110 // Check Efi Option RomImage
1111 //
1112 if ((FileFlags & FILE_FLAG_EFI) == FILE_FLAG_EFI) {
1113 EfiRomFlag = TRUE;
1114 }
1115 //
1116 // Create a new file structure
1117 //
1118 FileList = (FILE_LIST *) malloc (sizeof (FILE_LIST));
1119 if (FileList == NULL) {
1120 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!", NULL);
1121 return STATUS_ERROR;
1122 }
1123
1124 //
1125 // set flag and class code for this image.
1126 //
1127 memset ((char *) FileList, 0, sizeof (FILE_LIST));
1128 FileList->FileName = Argv[0];
1129 FileList->FileFlags = FileFlags;
1130 FileList->ClassCode = ClassCode;
1131 FileList->CodeRevision = (UINT16) CodeRevision;
1132 ClassCode = 0;
1133 CodeRevision = 0;
1134
1135 if (Options->FileList == NULL) {
1136 Options->FileList = FileList;
1137 } else {
1138 if (PrevFileList == NULL) {
1139 PrevFileList = FileList;
1140 } else {
1141 PrevFileList->Next = FileList;
1142 }
1143 }
1144
1145 PrevFileList = FileList;
1146 }
1147 //
1148 // Next argument
1149 //
1150 Argv++;
1151 Argc--;
1152 }
1153
1154 //
1155 // Must have specified some files
1156 //
1157 if (Options->FileList == NULL) {
1158 Error (NULL, 0, 2000, "Invalid parameter", "Missing input file name!");
1159 return STATUS_ERROR;
1160 }
1161
1162 //
1163 // For EFI OptionRom image, Make sure a device ID and vendor ID are both specified.
1164 //
1165 if (EfiRomFlag) {
1166 if (!Options->VendIdValid) {
1167 Error (NULL, 0, 2000, "Missing Vendor ID in command line", NULL);
1168 return STATUS_ERROR;
1169 }
1170
1171 if (!Options->DevIdValid) {
1172 Error (NULL, 0, 2000, "Missing Device ID in command line", NULL);
1173 return STATUS_ERROR;
1174 }
1175 }
1176
1177 return 0;
1178 }
1179
1180 static
1181 void
1182 Version (
1183 VOID
1184 )
1185 /*++
1186
1187 Routine Description:
1188
1189 Print version information for this utility.
1190
1191 Arguments:
1192
1193 None.
1194
1195 Returns:
1196
1197 Nothing.
1198 --*/
1199 {
1200 fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
1201 }
1202
1203 static
1204 void
1205 Usage (
1206 VOID
1207 )
1208 /*++
1209
1210 Routine Description:
1211
1212 Print usage information for this utility.
1213
1214 Arguments:
1215
1216 None.
1217
1218 Returns:
1219
1220 Nothing.
1221
1222 --*/
1223 {
1224 //
1225 // Summary usage
1226 //
1227 fprintf (stdout, "Usage: %s -f VendorId -i DeviceId [options] [file name<s>] \n\n", UTILITY_NAME);
1228
1229 //
1230 // Copyright declaration
1231 //
1232 fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");
1233
1234 //
1235 // Details Option
1236 //
1237 fprintf (stdout, "Options:\n");
1238 fprintf (stdout, " -o FileName, --output FileName\n\
1239 File will be created to store the output content.\n");
1240 fprintf (stdout, " -e EfiFileName\n\
1241 EFI PE32 image files.\n");
1242 fprintf (stdout, " -ec EfiFileName\n\
1243 EFI PE32 image files and will be compressed.\n");
1244 fprintf (stdout, " -b BinFileName\n\
1245 Legacy binary files.\n");
1246 fprintf (stdout, " -l ClassCode\n\
1247 Hex ClassCode in the PCI data structure header.\n");
1248 fprintf (stdout, " -r Rev Hex Revision in the PCI data structure header.\n");
1249 fprintf (stdout, " -n Not to automatically set the LAST bit in the last file.\n");
1250 fprintf (stdout, " -f VendorId\n\
1251 Hex PCI Vendor ID for the device OpROM, must be specified\n");
1252 fprintf (stdout, " -i DeviceId\n\
1253 Hex PCI Device ID for the device OpROM, must be specified\n");
1254 fprintf (stdout, " -p, --pci23\n\
1255 Default layout meets PCI 3.0 specifications\n\
1256 specifying this flag will for a PCI 2.3 layout.\n");
1257 fprintf (stdout, " -d, --dump\n\
1258 Dump the headers of an existing option ROM image.\n");
1259 fprintf (stdout, " -v, --verbose\n\
1260 Turn on verbose output with informational messages.\n");
1261 fprintf (stdout, " --version Show program's version number and exit.\n");
1262 fprintf (stdout, " -h, --help\n\
1263 Show this help message and exit.\n");
1264 fprintf (stdout, " -q, --quiet\n\
1265 Disable all messages except FATAL ERRORS.\n");
1266 fprintf (stdout, " --debug [#,0-9]\n\
1267 Enable debug messages at level #.\n");
1268 }
1269
1270 static
1271 void
1272 DumpImage (
1273 FILE_LIST *InFile
1274 )
1275 /*++
1276
1277 Routine Description:
1278
1279 Dump the headers of an existing option ROM image
1280
1281 Arguments:
1282
1283 InFile - the file name of an existing option ROM image
1284
1285 Returns:
1286
1287 none
1288
1289 --*/
1290 {
1291 PCI_EXPANSION_ROM_HEADER PciRomHdr;
1292 FILE *InFptr;
1293 UINT32 ImageStart;
1294 UINT32 ImageCount;
1295 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHdr;
1296 PCI_DATA_STRUCTURE PciDs23;
1297 PCI_3_0_DATA_STRUCTURE PciDs30;
1298
1299 //
1300 // Open the input file
1301 //
1302 if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {
1303 Error (NULL, 0, 0001, "Error opening file", InFile->FileName);
1304 return ;
1305 }
1306 //
1307 // Go through the image and dump the header stuff for each
1308 //
1309 ImageCount = 0;
1310 for (;;) {
1311 //
1312 // Save our postition in the file, since offsets in the headers
1313 // are relative to the particular image.
1314 //
1315 ImageStart = ftell (InFptr);
1316 ImageCount++;
1317
1318 //
1319 // Read the option ROM header. Have to assume a raw binary image for now.
1320 //
1321 if (fread (&PciRomHdr, sizeof (PciRomHdr), 1, InFptr) != 1) {
1322 Error (NULL, 0, 3001, "Not supported", "Failed to read PCI ROM header from file!");
1323 goto BailOut;
1324 }
1325
1326 //
1327 // Dump the contents of the header
1328 //
1329 fprintf (stdout, "Image %u -- Offset 0x%X\n", (unsigned) ImageCount, (unsigned) ImageStart);
1330 fprintf (stdout, " ROM header contents\n");
1331 fprintf (stdout, " Signature 0x%04X\n", PciRomHdr.Signature);
1332 fprintf (stdout, " PCIR offset 0x%04X\n", PciRomHdr.PcirOffset);
1333 //
1334 // Find PCI data structure
1335 //
1336 if (fseek (InFptr, ImageStart + PciRomHdr.PcirOffset, SEEK_SET)) {
1337 Error (NULL, 0, 3001, "Not supported", "Failed to seek to PCI data structure!");
1338 goto BailOut;
1339 }
1340 //
1341 // Read and dump the PCI data structure
1342 //
1343 memset (&PciDs23, 0, sizeof (PciDs23));
1344 memset (&PciDs30, 0, sizeof (PciDs30));
1345 if (mOptions.Pci23 == 1) {
1346 if (fread (&PciDs23, sizeof (PciDs23), 1, InFptr) != 1) {
1347 Error (NULL, 0, 3001, "Not supported", "Failed to read PCI data structure from file %s!", InFile->FileName);
1348 goto BailOut;
1349 }
1350 } else {
1351 if (fread (&PciDs30, sizeof (PciDs30), 1, InFptr) != 1) {
1352 Error (NULL, 0, 3001, "Not supported", "Failed to read PCI data structure from file %s!", InFile->FileName);
1353 goto BailOut;
1354 }
1355 }
1356 if (mOptions.Verbose) {
1357 VerboseMsg("Read PCI data structure from file %s", InFile->FileName);
1358 }
1359
1360 //fprintf (stdout, " PCI Data Structure\n");
1361 if (mOptions.Pci23 == 1) {
1362 fprintf (
1363 stdout,
1364 " Signature %c%c%c%c\n",
1365 (char) PciDs23.Signature,
1366 (char) (PciDs23.Signature >> 8),
1367 (char) (PciDs23.Signature >> 16),
1368 (char) (PciDs23.Signature >> 24)
1369 );
1370 fprintf (stdout, " Vendor ID 0x%04X\n", PciDs23.VendorId);
1371 fprintf (stdout, " Device ID 0x%04X\n", PciDs23.DeviceId);
1372 fprintf (stdout, " Length 0x%04X\n", PciDs23.Length);
1373 fprintf (stdout, " Revision 0x%04X\n", PciDs23.Revision);
1374 fprintf (
1375 stdout,
1376 " Class Code 0x%06X\n",
1377 (unsigned) (PciDs23.ClassCode[0] | (PciDs23.ClassCode[1] << 8) | (PciDs23.ClassCode[2] << 16))
1378 );
1379 fprintf (stdout, " Image size 0x%X\n", (unsigned) PciDs23.ImageLength * 512);
1380 fprintf (stdout, " Code revision: 0x%04X\n", PciDs23.CodeRevision);
1381 fprintf (stdout, " Indicator 0x%02X", PciDs23.Indicator);
1382 } else {
1383 fprintf (
1384 stdout,
1385 " Signature %c%c%c%c\n",
1386 (char) PciDs30.Signature,
1387 (char) (PciDs30.Signature >> 8),
1388 (char) (PciDs30.Signature >> 16),
1389 (char) (PciDs30.Signature >> 24)
1390 );
1391 fprintf (stdout, " Vendor ID 0x%04X\n", PciDs30.VendorId);
1392 fprintf (stdout, " Device ID 0x%04X\n", PciDs30.DeviceId);
1393 fprintf (stdout, " Length 0x%04X\n", PciDs30.Length);
1394 fprintf (stdout, " Revision 0x%04X\n", PciDs30.Revision);
1395 fprintf (stdout, " DeviceListOffset 0x%02X\n", PciDs30.DeviceListOffset);
1396 fprintf (
1397 stdout,
1398 " Class Code 0x%06X\n",
1399 (unsigned) (PciDs30.ClassCode[0] | (PciDs30.ClassCode[1] << 8) | (PciDs30.ClassCode[2] << 16))
1400 );
1401 fprintf (stdout, " Image size 0x%X\n", (unsigned) PciDs30.ImageLength * 512);
1402 fprintf (stdout, " Code revision: 0x%04X\n", PciDs30.CodeRevision);
1403 fprintf (stdout, " MaxRuntimeImageLength 0x%02X\n", PciDs30.MaxRuntimeImageLength);
1404 fprintf (stdout, " ConfigUtilityCodeHeaderOffset 0x%02X\n", PciDs30.ConfigUtilityCodeHeaderOffset);
1405 fprintf (stdout, " DMTFCLPEntryPointOffset 0x%02X\n", PciDs30.DMTFCLPEntryPointOffset);
1406 fprintf (stdout, " Indicator 0x%02X", PciDs30.Indicator);
1407 }
1408 //
1409 // Print the indicator, used to flag the last image
1410 //
1411 if (PciDs23.Indicator == INDICATOR_LAST || PciDs30.Indicator == INDICATOR_LAST) {
1412 fprintf (stdout, " (last image)\n");
1413 } else {
1414 fprintf (stdout, "\n");
1415 }
1416 //
1417 // Print the code type. If EFI code, then we can provide more info.
1418 //
1419 if (mOptions.Pci23 == 1) {
1420 fprintf (stdout, " Code type 0x%02X", PciDs23.CodeType);
1421 } else {
1422 fprintf (stdout, " Code type 0x%02X", PciDs30.CodeType);
1423 }
1424 if (PciDs23.CodeType == PCI_CODE_TYPE_EFI_IMAGE || PciDs30.CodeType == PCI_CODE_TYPE_EFI_IMAGE) {
1425 fprintf (stdout, " (EFI image)\n");
1426 //
1427 // Re-read the header as an EFI ROM header, then dump more info
1428 //
1429 fprintf (stdout, " EFI ROM header contents\n");
1430 if (fseek (InFptr, ImageStart, SEEK_SET)) {
1431 Error (NULL, 0, 5001, "Failed to re-seek to ROM header structure!", NULL);
1432 goto BailOut;
1433 }
1434
1435 if (fread (&EfiRomHdr, sizeof (EfiRomHdr), 1, InFptr) != 1) {
1436 Error (NULL, 0, 5001, "Failed to read EFI PCI ROM header from file!", NULL);
1437 goto BailOut;
1438 }
1439 //
1440 // Now dump more info
1441 //
1442 fprintf (stdout, " EFI Signature 0x%04X\n", (unsigned) EfiRomHdr.EfiSignature);
1443 fprintf (
1444 stdout,
1445 " Compression Type 0x%04X ",
1446 EfiRomHdr.CompressionType
1447 );
1448 if (EfiRomHdr.CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
1449 fprintf (stdout, "(compressed)\n");
1450 } else {
1451 fprintf (stdout, "(not compressed)\n");
1452 }
1453
1454 fprintf (
1455 stdout,
1456 " Machine type 0x%04X (%s)\n",
1457 EfiRomHdr.EfiMachineType,
1458 GetMachineTypeStr (EfiRomHdr.EfiMachineType)
1459 );
1460 fprintf (
1461 stdout,
1462 " Subsystem 0x%04X (%s)\n",
1463 EfiRomHdr.EfiSubsystem,
1464 GetSubsystemTypeStr (EfiRomHdr.EfiSubsystem)
1465 );
1466 fprintf (
1467 stdout,
1468 " EFI image offset 0x%04X (@0x%X)\n",
1469 EfiRomHdr.EfiImageHeaderOffset,
1470 EfiRomHdr.EfiImageHeaderOffset + (unsigned) ImageStart
1471 );
1472
1473 } else {
1474 //
1475 // Not an EFI image
1476 //
1477 fprintf (stdout, "\n");
1478 }
1479 //
1480 // If code type is EFI image, then dump it as well?
1481 //
1482 // if (PciDs.CodeType == PCI_CODE_TYPE_EFI_IMAGE) {
1483 // }
1484 //
1485 // If last image, then we're done
1486 //
1487 if (PciDs23.Indicator == INDICATOR_LAST || PciDs30.Indicator == INDICATOR_LAST) {
1488 goto BailOut;
1489 }
1490 //
1491 // Seek to the start of the next image
1492 //
1493 if (mOptions.Pci23 == 1) {
1494 if (fseek (InFptr, ImageStart + (PciDs23.ImageLength * 512), SEEK_SET)) {
1495 Error (NULL, 0, 3001, "Not supported", "Failed to seek to next image!");
1496 goto BailOut;
1497 }
1498 } else {
1499 if (fseek (InFptr, ImageStart + (PciDs30.ImageLength * 512), SEEK_SET)) {
1500 Error (NULL, 0, 3001, "Not supported", "Failed to seek to next image!");
1501 goto BailOut;
1502 }
1503 }
1504 }
1505
1506 BailOut:
1507 fclose (InFptr);
1508 }
1509
1510 char *
1511 GetMachineTypeStr (
1512 UINT16 MachineType
1513 )
1514 /*++
1515
1516 Routine Description:
1517
1518 GC_TODO: Add function description
1519
1520 Arguments:
1521
1522 MachineType - GC_TODO: add argument description
1523
1524 Returns:
1525
1526 GC_TODO: add return values
1527
1528 --*/
1529 {
1530 int Index;
1531
1532 for (Index = 0; mMachineTypes[Index].Name != NULL; Index++) {
1533 if (mMachineTypes[Index].Value == MachineType) {
1534 return mMachineTypes[Index].Name;
1535 }
1536 }
1537
1538 return "unknown";
1539 }
1540
1541 static
1542 char *
1543 GetSubsystemTypeStr (
1544 UINT16 SubsystemType
1545 )
1546 /*++
1547
1548 Routine Description:
1549
1550 GC_TODO: Add function description
1551
1552 Arguments:
1553
1554 SubsystemType - GC_TODO: add argument description
1555
1556 Returns:
1557
1558 GC_TODO: add return values
1559
1560 --*/
1561 {
1562 int Index;
1563
1564 for (Index = 0; mSubsystemTypes[Index].Name != NULL; Index++) {
1565 if (mSubsystemTypes[Index].Value == SubsystemType) {
1566 return mSubsystemTypes[Index].Name;
1567 }
1568 }
1569
1570 return "unknown";
1571 }