]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/GenFdImage/GenFdImageLib.c
39698d9abefefe24f57a76a379078a0cf82e5d4f
3 Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
19 This file contains the functions required to generate
25 // Coded to EFI 2.0 Coding Standards
28 // Include file in build
30 #include "GenFdImage.h"
33 // Global declarations
35 UINTN ValidLineNum
= 0;
38 static UINT64 LastAddress
= 0;
47 FVINFO
**OrgFvInfoPtr
;
61 This function is used by qsort to sort the Fv list based on FvBaseAddress
73 // GC_TODO: Arg1 - add argument and description to function comment
74 // GC_TODO: Arg2 - add argument and description to function comment
76 if ((*(FVINFO
**) Arg1
)->FvBaseAddress
> (*(FVINFO
**) Arg2
)->FvBaseAddress
) {
78 } else if ((*(FVINFO
**) Arg1
)->FvBaseAddress
< (*(FVINFO
**) Arg2
)->FvBaseAddress
) {
93 This function builds the token list in an array which will be parsed later
105 strcpy (*TokenStr
, Token
);
117 This function cleans up the line by removing all whitespace and
130 CHAR8 TmpLine
[FILE_NAME_SIZE
];
138 // Change '#' to '//' for Comment style
140 // if((Ptr0=strchr(Line, '#')) != NULL) {
142 if ((Ptr0
= strstr (Line
, "//")) != NULL
) {
143 Line
[Ptr0
- Line
] = 0;
148 while ((c
= Line
[i
]) != 0) {
149 if ((c
!= ' ') && (c
!= '\t') && (c
!= '\n')) {
157 strcpy (Line
, TmpLine
);
168 This function calculates number of valid lines in a input file.
172 Fp Pointer to a file handle which has been opened.
180 CHAR8 Buff
[FILE_NAME_SIZE
];
182 while (fgets (Buff
, sizeof (Buff
), Fp
)) {
200 This function parses the input file and tokenizes the string
204 Fp Pointer to a file handle which has been opened.
213 CHAR8 Buff
[FILE_NAME_SIZE
];
214 CHAR8 OrgLine
[FILE_NAME_SIZE
];
215 CHAR8 Str
[FILE_NAME_SIZE
];
216 CHAR8 Delimit
[] = "=";
218 while (fgets (Buff
, sizeof (Buff
), Fp
) != NULL
) {
219 strcpy (OrgLine
, Buff
);
226 Token
= strtok (Buff
, Delimit
);
228 while (Token
!= NULL
) {
230 BuildTokenList (Str
);
231 Token
= strtok (NULL
, Delimit
);
244 This function intializes the relevant global variable
245 used to store the information retrieved from the INF file.
256 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
257 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
258 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
259 // GC_TODO: EFI_SUCCESS - add return value to function comment
263 FdInfo
= malloc (sizeof (FDINFO
));
265 if (FdInfo
== NULL
) {
266 printf ("ERROR: allocating memory (struct FDINFO) in"" function InitializeComps.\n");
267 return EFI_OUT_OF_RESOURCES
;
270 OrgFdInfoPtr
= FdInfo
;
272 FvInfo
= malloc (sizeof (INTN
) * NumFvFiles
);
274 if (FvInfo
== NULL
) {
275 printf ("ERROR: allocating memory (INTN * NumFvFiles) in"" function InitializeComps.\n");
276 return EFI_OUT_OF_RESOURCES
;
279 OrgFvInfoPtr
= FvInfo
;
281 for (Index
= 0; Index
< NumFvFiles
; Index
++) {
282 *FvInfo
= malloc (sizeof (FVINFO
));
284 if (*FvInfo
== NULL
) {
285 printf ("ERROR: allocating memory (FVINFO) in"" function InitializeComps.\n");
286 return EFI_OUT_OF_RESOURCES
;
289 memset (*FvInfo
, 0, sizeof (FVINFO
));
293 FvInfo
= OrgFvInfoPtr
;
299 InitializeInFileInfo (
306 This function intializes the relevant global variable
307 used to store the information retrieved from the INF file.
324 TokenStr
= OrgStrTokPtr
;
326 while (*TokenStr
!= NULL
) {
327 if (stricmp (*TokenStr
, "[options]") == 0) {
332 if (stricmp (*TokenStr
, "EFI_FV_BASE_ADDRESS") == 0) {
334 if (AsciiStringToUint64 (
339 printf ("\nERROR: Cannot determine the FV base address.");
342 (*FvInfo
)->FvBaseAddress
= StringValue
;
343 } else if (stricmp (*TokenStr
, "EFI_FV_FILE_NAME") == 0) {
345 strcpy ((*FvInfo
)->FvFile
, *TokenStr
);
354 GetFvRelatedInfoFromInfFile (
361 This function reads the input file, parses it and create a list of tokens
362 which are parsed and used, to intialize the data related to the Firmware
367 FileName FileName which needed to be read to parse data
374 // GC_TODO: EFI_ABORTED - add return value to function comment
375 // GC_TODO: EFI_ABORTED - add return value to function comment
376 // GC_TODO: EFI_SUCCESS - add return value to function comment
382 Fp
= fopen (FileName
, "r");
385 printf ("Error in opening %s file\n", FileName
);
391 if (ValidLineNum
== 0) {
392 printf ("\nFile doesn't contain any valid informations");
396 TokenStr
= (CHAR8
**) malloc (sizeof (UINTN
) * (2 * ValidLineNum
));
397 memset (TokenStr
, 0, sizeof (UINTN
) * (2 * ValidLineNum
));
398 OrgStrTokPtr
= TokenStr
;
400 for (Index
= 0; Index
< (2 * ValidLineNum
); Index
++) {
401 *TokenStr
= (CHAR8
*) malloc (sizeof (CHAR8
) * FILE_NAME_SIZE
);
402 memset (*TokenStr
, 0, FILE_NAME_SIZE
);
407 TokenStr
= OrgStrTokPtr
;
408 fseek (Fp
, 0L, SEEK_SET
);
411 InitializeInFileInfo ();
423 IN UINT64 StartAddress
,
431 This function reads the input file, parses it and creates a list of tokens
432 which are parsed and used to intialize the data related to the Firmware
437 FileName FileName which needed to be read to parse data
438 StartAddress This will set the file position.
439 Size Size in bytes needed to be written
440 Buffer Buffer needed to e written
447 // GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment
448 // GC_TODO: EFI_ABORTED - add return value to function comment
449 // GC_TODO: EFI_SUCCESS - add return value to function comment
455 Fp
= fopen (FileName
, "a+b");
458 printf ("\nERROR:Error in opening file %s ", FileName
);
459 return EFI_INVALID_PARAMETER
;
462 fseek (Fp
, (UINTN
) StartAddress
, SEEK_SET
);
463 NumByte
= fwrite ((VOID
*) Buffer
, sizeof (UINT8
), (UINTN
) Size
, Fp
);
466 // Check to ensure that buffer has been copied successfully
468 if (NumByte
!= Size
) {
469 printf ("\nERROR: Error in copying the buffer into file");
481 BuildFirmwareDeviceBinaryFromFwVolumes (
482 IN UINT64 FvBaseAddress
,
483 IN CHAR8
*FvFileName
,
490 This function reads the input file, parses it and creates a list of tokens
491 which are parsed and used to intialize the data related to the Firmware
496 FvBaseAddress Base Address. This info is retrieved from INF file
497 FvFileName InputFileName
498 FdFileName Output File Name
505 // GC_TODO: EFI_ABORTED - add return value to function comment
506 // GC_TODO: EFI_ABORTED - add return value to function comment
507 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
508 // GC_TODO: EFI_ABORTED - add return value to function comment
509 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
510 // GC_TODO: EFI_SUCCESS - add return value to function comment
525 static UINT64 StartAddress
= 0;
527 Fp
= fopen (FvFileName
, "r+b");
530 printf ("\nERROR:Error in opening file %s", FvFileName
);
534 BaseAddress
= FdInfo
->FdBaseAddress
;
537 // Check if Base Address of Firmware Volume falls below the Base Address
538 // Firmware Device, if yes, then abort this process.
540 if (FvBaseAddress
< BaseAddress
) {
541 printf ("\nERROR: Firmware Volume Base Address falls below Firmware ""Device Address.\n");
545 // Check if there are any holes between two Firmware Volumes. If any holes
546 // exist, fill the hole with PadByted data.
548 if (FvBaseAddress
> LastAddress
) {
549 PadByteSize
= (FvBaseAddress
- LastAddress
);
550 Buffer
= malloc ((UINTN
) PadByteSize
);
551 if (Buffer
== NULL
) {
552 printf ("ERROR: allocating (Buffer) memory in"" function BuildFirmwareDeviceBinaryFromFwVolumes.\n");
553 return EFI_OUT_OF_RESOURCES
;
556 for (Index
= 0; Index
< PadByteSize
; Index
++) {
557 *Buffer
= FdInfo
->PadValue
;
561 Buffer
-= PadByteSize
;
562 Status
= WriteFwBinary (
565 (UINT64
) PadByteSize
,
573 if (Status
!= EFI_SUCCESS
) {
574 printf ("\nERROR: Error in writing the binary image to file");
578 StartAddress
+= PadByteSize
;
579 LastAddress
+= PadByteSize
;
582 // Proceed with next Firmware Volume updates
584 FileSize
= _filelength (fileno (Fp
));
586 if ((FvBaseAddress
+ FileSize
) > (FdInfo
->FdBaseAddress
+ FdInfo
->FdSize
)) {
588 "\nERROR:Unable to update Firmware Device. File %s is larger than \
599 Buffer
= malloc ((UINTN
) FileSize
);
601 if (Buffer
== NULL
) {
602 printf ("Error in allocating buffer to read specific file\n");
603 return EFI_OUT_OF_RESOURCES
;
606 NumByteRead
= fread ((VOID
*) Buffer
, sizeof (UINT8
), (UINTN
) FileSize
, Fp
);
608 Status
= WriteFwBinary (FdFileName
, StartAddress
, FileSize
, Buffer
);
611 free ((VOID
*) Buffer
);
618 if (Status
!= EFI_SUCCESS
) {
619 printf ("\nERROR: Error in writing the binary image to file");
623 StartAddress
+= NumByteRead
;
624 LastAddress
+= FileSize
;
637 This function cleans up any allocated buffer
655 FvInfo
= OrgFvInfoPtr
;
658 for (Index
= 0; Index
< NumFvFiles
; Index
++) {
666 FvInfo
= OrgFvInfoPtr
;
673 IN UINT64 BaseAddress
,
683 This function reads the input file, parses it and creates a list of tokens
684 which are parsed and used to intialize the data related to the Firmware
689 BaseAddress Base Address for this Firmware Device
690 Size, Total Size of the Firmware Device
691 PadByte Pad byte data
692 OutFile Output File Name
693 FileList File List pointer to INF file names.
700 // GC_TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
701 // GC_TODO: EFI_ABORTED - add return value to function comment
702 // GC_TODO: EFI_ABORTED - add return value to function comment
703 // GC_TODO: EFI_SUCCESS - add return value to function comment
719 // If any previous Firmware Device existed,
720 // make it to 0 bytes
722 if ((Fp
= fopen (OutFile
, "w")) != NULL
) {
728 while (*InFile
!= NULL
) {
736 // Restore the orginal pointers
738 FvInfo
= OrgFvInfoPtr
;
741 while (*InFile
!= NULL
) {
742 strcpy ((*FvInfo
)->FvInfoFile
, *InFile
);
743 Status
= GetFvRelatedInfoFromInfFile (*InFile
);
745 if (Status
!= EFI_SUCCESS
) {
746 printf ("\nERROR: Error occurred in processsing INF file");
755 FdInfo
->FdSize
= Size
;
756 FdInfo
->FdBaseAddress
= BaseAddress
;
757 FdInfo
->PadValue
= PadByte
;
758 FvInfo
= OrgFvInfoPtr
;
759 strcpy (FdInfo
->OutFileName
, OutFile
);
761 for (Index
= 0; Index
< NumFvFiles
; Index
++) {
762 Status
= GenerateFvImage ((*FvInfo
)->FvInfoFile
);
764 if (Status
!= EFI_SUCCESS
) {
772 FvInfo
= OrgFvInfoPtr
;
775 // Sort the Firmware Volume information. Firmware Volume with lower
776 // base addresses will be processed first and higher base address one
777 // will be processed later.
779 qsort ((VOID
*) FvInfo
, NumFvFiles
, sizeof (FVINFO
*), CompareItems
);
781 LastAddress
= (*FvInfo
)->FvBaseAddress
;
783 for (Index
= 0; Index
< NumFvFiles
; Index
++) {
784 Status
= BuildFirmwareDeviceBinaryFromFwVolumes (
785 (*FvInfo
)->FvBaseAddress
,
789 if (Status
!= EFI_SUCCESS
) {
797 // Check if any space left after copying data from all Firmware Volumes
798 // If yes, then fill those location with PadValue.
800 if ((FdInfo
->FdBaseAddress
+ Size
) > LastAddress
) {
802 PadSize
= (UINTN
) ((FdInfo
->FdBaseAddress
+ FdInfo
->FdSize
) - LastAddress
);
803 Buffer
= malloc (PadSize
);
805 if (Buffer
== NULL
) {
806 printf ("\nERROR: allocating PadSize memory in function GenerateFdImage.\n");
808 return EFI_OUT_OF_RESOURCES
;
811 for (Index
= 0; Index
< PadSize
; Index
++) {
812 *Buffer
= FdInfo
->PadValue
;
818 Fp
= fopen (OutFile
, "a+b");
821 printf ("\nERROR:Opening file %s", OutFile
);
826 FileSize
= _filelength (fileno (Fp
));
827 fseek (Fp
, FileSize
, SEEK_SET
);
828 NumByte
= fwrite (Buffer
, sizeof (UINT8
), PadSize
, Fp
);
836 if (NumByte
!= (sizeof (UINT8
) * PadSize
)) {
837 printf ("\nERROR: Copying data from buffer to File %s ", OutFile
);
843 // Clean up all the memory which has been allocated so far.