]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/EfiRom/EfiRom.c
BaseTools/EfiRom: Avoid possible NULL pointer dereference
[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 strcpy (Options->OutFileName, Argv[1]);
983
984 Argv++;
985 Argc--;
986 } else if ((stricmp (Argv[0], "-h") == 0) || (stricmp (Argv[0], "--help") == 0)) {
987 //
988 // Help option
989 //
990 Usage ();
991 return STATUS_ERROR;
992 } else if (stricmp (Argv[0], "-b") == 0) {
993 //
994 // Specify binary files with -b
995 //
996 FileFlags = FILE_FLAG_BINARY;
997 } else if ((stricmp (Argv[0], "-e") == 0) || (stricmp (Argv[0], "-ec") == 0)) {
998 //
999 // Specify EFI files with -e. Specify EFI-compressed with -c.
1000 //
1001 FileFlags = FILE_FLAG_EFI;
1002 if ((Argv[0][2] == 'c') || (Argv[0][2] == 'C')) {
1003 FileFlags |= FILE_FLAG_COMPRESS;
1004 }
1005 //
1006 // Specify not to set the LAST bit in the last file with -n
1007 //
1008 } else if (stricmp (Argv[0], "-n") == 0) {
1009 Options->NoLast = 1;
1010 } else if (((stricmp (Argv[0], "-v") == 0)) || ((stricmp (Argv[0], "--verbose") == 0))) {
1011 //
1012 // -v for verbose
1013 //
1014 Options->Verbose = 1;
1015 } else if (stricmp (Argv[0], "--debug") == 0) {
1016 Status = AsciiStringToUint64(Argv[1], FALSE, &DebugLevel);
1017 if (EFI_ERROR (Status)) {
1018 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1019 return 1;
1020 }
1021 if (DebugLevel > 9) {
1022 Error (NULL, 0, 2000, "Invalid option value", "Debug Level range is 0-9, current input level is %d", Argv[1]);
1023 return 1;
1024 }
1025 if (DebugLevel>=5 && DebugLevel<=9) {
1026 Options->Debug = TRUE;
1027 } else {
1028 Options->Debug = FALSE;
1029 }
1030 Argv++;
1031 Argc--;
1032 } else if ((stricmp (Argv[0], "--quiet") == 0) || (stricmp (Argv[0], "-q") == 0)) {
1033 Options->Quiet = TRUE;
1034 } else if ((stricmp (Argv[0], "--dump") == 0) || (stricmp (Argv[0], "-d") == 0)) {
1035 //
1036 // -dump for dumping a ROM image. In this case, say that the device id
1037 // and vendor id are valid so we don't have to specify bogus ones on the
1038 // command line.
1039 //
1040 Options->DumpOption = 1;
1041
1042 Options->VendIdValid = 1;
1043 Options->DevIdValid = 1;
1044 FileFlags = FILE_FLAG_BINARY;
1045 } else if ((stricmp (Argv[0], "-l") == 0) || (stricmp (Argv[0], "--class-code") == 0)) {
1046 //
1047 // Class code value for the next file in the list.
1048 // Make sure there's another parameter
1049 //
1050 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
1051 if (EFI_ERROR (Status)) {
1052 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1053 return 1;
1054 }
1055 ClassCode = (UINT32) TempValue;
1056 if (ClassCode & 0xFF000000) {
1057 Error (NULL, 0, 2000, "Invalid parameter", "Class code %s out of range!", Argv[1]);
1058 return STATUS_ERROR;
1059 }
1060 if (FileList != NULL && FileList->ClassCode == 0) {
1061 FileList->ClassCode = ClassCode;
1062 }
1063 Argv++;
1064 Argc--;
1065 } else if ((stricmp (Argv[0], "-r") == 0) || (stricmp (Argv[0], "--Revision") == 0)) {
1066 //
1067 // Code revision in the PCI data structure. The value is for the next
1068 // file in the list.
1069 // Make sure there's another parameter
1070 //
1071 Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
1072 if (EFI_ERROR (Status)) {
1073 Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
1074 return 1;
1075 }
1076 CodeRevision = (UINT32) TempValue;
1077 if (CodeRevision & 0xFFFF0000) {
1078 Error (NULL, 0, 2000, "Invalid parameter", "Code revision %s out of range!", Argv[1]);
1079 return STATUS_ERROR;
1080 }
1081 if (FileList != NULL && FileList->CodeRevision == 0) {
1082 FileList->CodeRevision = (UINT16) CodeRevision;
1083 }
1084 Argv++;
1085 Argc--;
1086 } else if ((stricmp (Argv[0], "-p") == 0) || (stricmp (Argv[0], "--pci23") == 0)) {
1087 //
1088 // Default layout meets PCI 3.0 specifications, specifying this flag will for a PCI 2.3 layout.
1089 //
1090 mOptions.Pci23 = 1;
1091 } else {
1092 Error (NULL, 0, 2000, "Invalid parameter", "Invalid option specified: %s", Argv[0]);
1093 return STATUS_ERROR;
1094 }
1095 } else {
1096 //
1097 // Not a slash-option argument. Must be a file name. Make sure they've specified
1098 // -e or -b already.
1099 //
1100 if ((FileFlags & (FILE_FLAG_BINARY | FILE_FLAG_EFI)) == 0) {
1101 Error (NULL, 0, 2000, "Invalid parameter", "Missing -e or -b with input file %s!", Argv[0]);
1102 return STATUS_ERROR;
1103 }
1104 //
1105 // Check Efi Option RomImage
1106 //
1107 if ((FileFlags & FILE_FLAG_EFI) == FILE_FLAG_EFI) {
1108 EfiRomFlag = TRUE;
1109 }
1110 //
1111 // Create a new file structure
1112 //
1113 FileList = (FILE_LIST *) malloc (sizeof (FILE_LIST));
1114 if (FileList == NULL) {
1115 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!", NULL);
1116 return STATUS_ERROR;
1117 }
1118
1119 //
1120 // set flag and class code for this image.
1121 //
1122 memset ((char *) FileList, 0, sizeof (FILE_LIST));
1123 FileList->FileName = Argv[0];
1124 FileList->FileFlags = FileFlags;
1125 FileList->ClassCode = ClassCode;
1126 FileList->CodeRevision = (UINT16) CodeRevision;
1127 ClassCode = 0;
1128 CodeRevision = 0;
1129
1130 if (Options->FileList == NULL) {
1131 Options->FileList = FileList;
1132 } else {
1133 if (PrevFileList == NULL) {
1134 PrevFileList = FileList;
1135 } else {
1136 PrevFileList->Next = FileList;
1137 }
1138 }
1139
1140 PrevFileList = FileList;
1141 }
1142 //
1143 // Next argument
1144 //
1145 Argv++;
1146 Argc--;
1147 }
1148
1149 //
1150 // Must have specified some files
1151 //
1152 if (Options->FileList == NULL) {
1153 Error (NULL, 0, 2000, "Invalid parameter", "Missing input file name!");
1154 return STATUS_ERROR;
1155 }
1156
1157 //
1158 // For EFI OptionRom image, Make sure a device ID and vendor ID are both specified.
1159 //
1160 if (EfiRomFlag) {
1161 if (!Options->VendIdValid) {
1162 Error (NULL, 0, 2000, "Missing Vendor ID in command line", NULL);
1163 return STATUS_ERROR;
1164 }
1165
1166 if (!Options->DevIdValid) {
1167 Error (NULL, 0, 2000, "Missing Device ID in command line", NULL);
1168 return STATUS_ERROR;
1169 }
1170 }
1171
1172 return 0;
1173 }
1174
1175 static
1176 void
1177 Version (
1178 VOID
1179 )
1180 /*++
1181
1182 Routine Description:
1183
1184 Print version information for this utility.
1185
1186 Arguments:
1187
1188 None.
1189
1190 Returns:
1191
1192 Nothing.
1193 --*/
1194 {
1195 fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
1196 }
1197
1198 static
1199 void
1200 Usage (
1201 VOID
1202 )
1203 /*++
1204
1205 Routine Description:
1206
1207 Print usage information for this utility.
1208
1209 Arguments:
1210
1211 None.
1212
1213 Returns:
1214
1215 Nothing.
1216
1217 --*/
1218 {
1219 //
1220 // Summary usage
1221 //
1222 fprintf (stdout, "Usage: %s -f VendorId -i DeviceId [options] [file name<s>] \n\n", UTILITY_NAME);
1223
1224 //
1225 // Copyright declaration
1226 //
1227 fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");
1228
1229 //
1230 // Details Option
1231 //
1232 fprintf (stdout, "Options:\n");
1233 fprintf (stdout, " -o FileName, --output FileName\n\
1234 File will be created to store the output content.\n");
1235 fprintf (stdout, " -e EfiFileName\n\
1236 EFI PE32 image files.\n");
1237 fprintf (stdout, " -ec EfiFileName\n\
1238 EFI PE32 image files and will be compressed.\n");
1239 fprintf (stdout, " -b BinFileName\n\
1240 Legacy binary files.\n");
1241 fprintf (stdout, " -l ClassCode\n\
1242 Hex ClassCode in the PCI data structure header.\n");
1243 fprintf (stdout, " -r Rev Hex Revision in the PCI data structure header.\n");
1244 fprintf (stdout, " -n Not to automatically set the LAST bit in the last file.\n");
1245 fprintf (stdout, " -f VendorId\n\
1246 Hex PCI Vendor ID for the device OpROM, must be specified\n");
1247 fprintf (stdout, " -i DeviceId\n\
1248 Hex PCI Device ID for the device OpROM, must be specified\n");
1249 fprintf (stdout, " -p, --pci23\n\
1250 Default layout meets PCI 3.0 specifications\n\
1251 specifying this flag will for a PCI 2.3 layout.\n");
1252 fprintf (stdout, " -d, --dump\n\
1253 Dump the headers of an existing option ROM image.\n");
1254 fprintf (stdout, " -v, --verbose\n\
1255 Turn on verbose output with informational messages.\n");
1256 fprintf (stdout, " --version Show program's version number and exit.\n");
1257 fprintf (stdout, " -h, --help\n\
1258 Show this help message and exit.\n");
1259 fprintf (stdout, " -q, --quiet\n\
1260 Disable all messages except FATAL ERRORS.\n");
1261 fprintf (stdout, " --debug [#,0-9]\n\
1262 Enable debug messages at level #.\n");
1263 }
1264
1265 static
1266 void
1267 DumpImage (
1268 FILE_LIST *InFile
1269 )
1270 /*++
1271
1272 Routine Description:
1273
1274 Dump the headers of an existing option ROM image
1275
1276 Arguments:
1277
1278 InFile - the file name of an existing option ROM image
1279
1280 Returns:
1281
1282 none
1283
1284 --*/
1285 {
1286 PCI_EXPANSION_ROM_HEADER PciRomHdr;
1287 FILE *InFptr;
1288 UINT32 ImageStart;
1289 UINT32 ImageCount;
1290 EFI_PCI_EXPANSION_ROM_HEADER EfiRomHdr;
1291 PCI_DATA_STRUCTURE PciDs23;
1292 PCI_3_0_DATA_STRUCTURE PciDs30;
1293
1294 //
1295 // Open the input file
1296 //
1297 if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {
1298 Error (NULL, 0, 0001, "Error opening file", InFile->FileName);
1299 return ;
1300 }
1301 //
1302 // Go through the image and dump the header stuff for each
1303 //
1304 ImageCount = 0;
1305 for (;;) {
1306 //
1307 // Save our postition in the file, since offsets in the headers
1308 // are relative to the particular image.
1309 //
1310 ImageStart = ftell (InFptr);
1311 ImageCount++;
1312
1313 //
1314 // Read the option ROM header. Have to assume a raw binary image for now.
1315 //
1316 if (fread (&PciRomHdr, sizeof (PciRomHdr), 1, InFptr) != 1) {
1317 Error (NULL, 0, 3001, "Not supported", "Failed to read PCI ROM header from file!");
1318 goto BailOut;
1319 }
1320
1321 //
1322 // Dump the contents of the header
1323 //
1324 fprintf (stdout, "Image %u -- Offset 0x%X\n", (unsigned) ImageCount, (unsigned) ImageStart);
1325 fprintf (stdout, " ROM header contents\n");
1326 fprintf (stdout, " Signature 0x%04X\n", PciRomHdr.Signature);
1327 fprintf (stdout, " PCIR offset 0x%04X\n", PciRomHdr.PcirOffset);
1328 //
1329 // Find PCI data structure
1330 //
1331 if (fseek (InFptr, ImageStart + PciRomHdr.PcirOffset, SEEK_SET)) {
1332 Error (NULL, 0, 3001, "Not supported", "Failed to seek to PCI data structure!");
1333 goto BailOut;
1334 }
1335 //
1336 // Read and dump the PCI data structure
1337 //
1338 memset (&PciDs23, 0, sizeof (PciDs23));
1339 memset (&PciDs30, 0, sizeof (PciDs30));
1340 if (mOptions.Pci23 == 1) {
1341 if (fread (&PciDs23, sizeof (PciDs23), 1, InFptr) != 1) {
1342 Error (NULL, 0, 3001, "Not supported", "Failed to read PCI data structure from file %s!", InFile->FileName);
1343 goto BailOut;
1344 }
1345 } else {
1346 if (fread (&PciDs30, sizeof (PciDs30), 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 }
1351 if (mOptions.Verbose) {
1352 VerboseMsg("Read PCI data structure from file %s", InFile->FileName);
1353 }
1354
1355 //fprintf (stdout, " PCI Data Structure\n");
1356 if (mOptions.Pci23 == 1) {
1357 fprintf (
1358 stdout,
1359 " Signature %c%c%c%c\n",
1360 (char) PciDs23.Signature,
1361 (char) (PciDs23.Signature >> 8),
1362 (char) (PciDs23.Signature >> 16),
1363 (char) (PciDs23.Signature >> 24)
1364 );
1365 fprintf (stdout, " Vendor ID 0x%04X\n", PciDs23.VendorId);
1366 fprintf (stdout, " Device ID 0x%04X\n", PciDs23.DeviceId);
1367 fprintf (stdout, " Length 0x%04X\n", PciDs23.Length);
1368 fprintf (stdout, " Revision 0x%04X\n", PciDs23.Revision);
1369 fprintf (
1370 stdout,
1371 " Class Code 0x%06X\n",
1372 (unsigned) (PciDs23.ClassCode[0] | (PciDs23.ClassCode[1] << 8) | (PciDs23.ClassCode[2] << 16))
1373 );
1374 fprintf (stdout, " Image size 0x%X\n", (unsigned) PciDs23.ImageLength * 512);
1375 fprintf (stdout, " Code revision: 0x%04X\n", PciDs23.CodeRevision);
1376 fprintf (stdout, " Indicator 0x%02X", PciDs23.Indicator);
1377 } else {
1378 fprintf (
1379 stdout,
1380 " Signature %c%c%c%c\n",
1381 (char) PciDs30.Signature,
1382 (char) (PciDs30.Signature >> 8),
1383 (char) (PciDs30.Signature >> 16),
1384 (char) (PciDs30.Signature >> 24)
1385 );
1386 fprintf (stdout, " Vendor ID 0x%04X\n", PciDs30.VendorId);
1387 fprintf (stdout, " Device ID 0x%04X\n", PciDs30.DeviceId);
1388 fprintf (stdout, " Length 0x%04X\n", PciDs30.Length);
1389 fprintf (stdout, " Revision 0x%04X\n", PciDs30.Revision);
1390 fprintf (stdout, " DeviceListOffset 0x%02X\n", PciDs30.DeviceListOffset);
1391 fprintf (
1392 stdout,
1393 " Class Code 0x%06X\n",
1394 (unsigned) (PciDs30.ClassCode[0] | (PciDs30.ClassCode[1] << 8) | (PciDs30.ClassCode[2] << 16))
1395 );
1396 fprintf (stdout, " Image size 0x%X\n", (unsigned) PciDs30.ImageLength * 512);
1397 fprintf (stdout, " Code revision: 0x%04X\n", PciDs30.CodeRevision);
1398 fprintf (stdout, " MaxRuntimeImageLength 0x%02X\n", PciDs30.MaxRuntimeImageLength);
1399 fprintf (stdout, " ConfigUtilityCodeHeaderOffset 0x%02X\n", PciDs30.ConfigUtilityCodeHeaderOffset);
1400 fprintf (stdout, " DMTFCLPEntryPointOffset 0x%02X\n", PciDs30.DMTFCLPEntryPointOffset);
1401 fprintf (stdout, " Indicator 0x%02X", PciDs30.Indicator);
1402 }
1403 //
1404 // Print the indicator, used to flag the last image
1405 //
1406 if (PciDs23.Indicator == INDICATOR_LAST || PciDs30.Indicator == INDICATOR_LAST) {
1407 fprintf (stdout, " (last image)\n");
1408 } else {
1409 fprintf (stdout, "\n");
1410 }
1411 //
1412 // Print the code type. If EFI code, then we can provide more info.
1413 //
1414 if (mOptions.Pci23 == 1) {
1415 fprintf (stdout, " Code type 0x%02X", PciDs23.CodeType);
1416 } else {
1417 fprintf (stdout, " Code type 0x%02X", PciDs30.CodeType);
1418 }
1419 if (PciDs23.CodeType == PCI_CODE_TYPE_EFI_IMAGE || PciDs30.CodeType == PCI_CODE_TYPE_EFI_IMAGE) {
1420 fprintf (stdout, " (EFI image)\n");
1421 //
1422 // Re-read the header as an EFI ROM header, then dump more info
1423 //
1424 fprintf (stdout, " EFI ROM header contents\n");
1425 if (fseek (InFptr, ImageStart, SEEK_SET)) {
1426 Error (NULL, 0, 5001, "Failed to re-seek to ROM header structure!", NULL);
1427 goto BailOut;
1428 }
1429
1430 if (fread (&EfiRomHdr, sizeof (EfiRomHdr), 1, InFptr) != 1) {
1431 Error (NULL, 0, 5001, "Failed to read EFI PCI ROM header from file!", NULL);
1432 goto BailOut;
1433 }
1434 //
1435 // Now dump more info
1436 //
1437 fprintf (stdout, " EFI Signature 0x%04X\n", (unsigned) EfiRomHdr.EfiSignature);
1438 fprintf (
1439 stdout,
1440 " Compression Type 0x%04X ",
1441 EfiRomHdr.CompressionType
1442 );
1443 if (EfiRomHdr.CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {
1444 fprintf (stdout, "(compressed)\n");
1445 } else {
1446 fprintf (stdout, "(not compressed)\n");
1447 }
1448
1449 fprintf (
1450 stdout,
1451 " Machine type 0x%04X (%s)\n",
1452 EfiRomHdr.EfiMachineType,
1453 GetMachineTypeStr (EfiRomHdr.EfiMachineType)
1454 );
1455 fprintf (
1456 stdout,
1457 " Subsystem 0x%04X (%s)\n",
1458 EfiRomHdr.EfiSubsystem,
1459 GetSubsystemTypeStr (EfiRomHdr.EfiSubsystem)
1460 );
1461 fprintf (
1462 stdout,
1463 " EFI image offset 0x%04X (@0x%X)\n",
1464 EfiRomHdr.EfiImageHeaderOffset,
1465 EfiRomHdr.EfiImageHeaderOffset + (unsigned) ImageStart
1466 );
1467
1468 } else {
1469 //
1470 // Not an EFI image
1471 //
1472 fprintf (stdout, "\n");
1473 }
1474 //
1475 // If code type is EFI image, then dump it as well?
1476 //
1477 // if (PciDs.CodeType == PCI_CODE_TYPE_EFI_IMAGE) {
1478 // }
1479 //
1480 // If last image, then we're done
1481 //
1482 if (PciDs23.Indicator == INDICATOR_LAST || PciDs30.Indicator == INDICATOR_LAST) {
1483 goto BailOut;
1484 }
1485 //
1486 // Seek to the start of the next image
1487 //
1488 if (mOptions.Pci23 == 1) {
1489 if (fseek (InFptr, ImageStart + (PciDs23.ImageLength * 512), SEEK_SET)) {
1490 Error (NULL, 0, 3001, "Not supported", "Failed to seek to next image!");
1491 goto BailOut;
1492 }
1493 } else {
1494 if (fseek (InFptr, ImageStart + (PciDs30.ImageLength * 512), SEEK_SET)) {
1495 Error (NULL, 0, 3001, "Not supported", "Failed to seek to next image!");
1496 goto BailOut;
1497 }
1498 }
1499 }
1500
1501 BailOut:
1502 fclose (InFptr);
1503 }
1504
1505 char *
1506 GetMachineTypeStr (
1507 UINT16 MachineType
1508 )
1509 /*++
1510
1511 Routine Description:
1512
1513 GC_TODO: Add function description
1514
1515 Arguments:
1516
1517 MachineType - GC_TODO: add argument description
1518
1519 Returns:
1520
1521 GC_TODO: add return values
1522
1523 --*/
1524 {
1525 int Index;
1526
1527 for (Index = 0; mMachineTypes[Index].Name != NULL; Index++) {
1528 if (mMachineTypes[Index].Value == MachineType) {
1529 return mMachineTypes[Index].Name;
1530 }
1531 }
1532
1533 return "unknown";
1534 }
1535
1536 static
1537 char *
1538 GetSubsystemTypeStr (
1539 UINT16 SubsystemType
1540 )
1541 /*++
1542
1543 Routine Description:
1544
1545 GC_TODO: Add function description
1546
1547 Arguments:
1548
1549 SubsystemType - GC_TODO: add argument description
1550
1551 Returns:
1552
1553 GC_TODO: add return values
1554
1555 --*/
1556 {
1557 int Index;
1558
1559 for (Index = 0; mSubsystemTypes[Index].Name != NULL; Index++) {
1560 if (mSubsystemTypes[Index].Value == SubsystemType) {
1561 return mSubsystemTypes[Index].Name;
1562 }
1563 }
1564
1565 return "unknown";
1566 }