[PATCH] BaseTools/GenSec: Support EFI_SECTION_FREEFORM_SUBTYPE_GUID sections


Konstantin Aladyshev
 

Signed-off-by: Konstantin Aladyshev <aladyshev22@...>
---
BaseTools/Source/C/GenSec/GenSec.c | 171 ++++++++++++++++++++++++++++-
1 file changed, 169 insertions(+), 2 deletions(-)

diff --git a/BaseTools/Source/C/GenSec/GenSec.c b/BaseTools/Source/C/GenSec=
/GenSec.c
index 35a1ac64ea..d86cc197cc 100644
--- a/BaseTools/Source/C/GenSec/GenSec.c
+++ b/BaseTools/Source/C/GenSec/GenSec.c
@@ -988,6 +988,155 @@ Returns:
return EFI_SUCCESS;=0D
}=0D
=0D
+EFI_STATUS=0D
+GenSectionSubtypeGuidSection (=0D
+ CHAR8 **InputFileName,=0D
+ UINT32 *InputFileAlign,=0D
+ UINT32 InputFileNum,=0D
+ EFI_GUID *SubTypeGuid,=0D
+ UINT8 **OutFileBuffer=0D
+ )=0D
+/*++=0D
+=0D
+Routine Description:=0D
+=0D
+ Generate a section of type EFI_SECTION_FREEFORM_SUBTYPE_GUID=0D
+ The function won't validate the input file contents.=0D
+ The utility will add section header to the file.=0D
+=0D
+Arguments:=0D
+=0D
+ InputFileName - Name of the input file.=0D
+=0D
+ InputFileAlign - Alignment required by the input file data.=0D
+=0D
+ InputFileNum - Number of input files. Should be 1 for this section.=0D
+=0D
+ SubTypeGuid - Specify vendor guid value.=0D
+=0D
+ OutFileBuffer - Buffer pointer to Output file contents=0D
+=0D
+Returns:=0D
+=0D
+ EFI_SUCCESS on successful return=0D
+ EFI_INVALID_PARAMETER if InputFileNum is less than 1=0D
+ EFI_ABORTED if unable to open input file.=0D
+ EFI_OUT_OF_RESOURCES No resource to complete the operation.=0D
+=0D
+--*/=0D
+{=0D
+ UINT32 TotalLength;=0D
+ UINT32 InputLength;=0D
+ UINT32 Offset;=0D
+ UINT8 *FileBuffer;=0D
+ EFI_STATUS Status;=0D
+ EFI_FREEFORM_SUBTYPE_GUID_SECTION *SubtypeGuidSect;=0D
+ EFI_FREEFORM_SUBTYPE_GUID_SECTION2 *SubtypeGuidSect2;=0D
+=0D
+=0D
+ InputLength =3D 0;=0D
+ Offset =3D 0;=0D
+ FileBuffer =3D NULL;=0D
+ TotalLength =3D 0;=0D
+=0D
+ if (InputFileNum > 1) {=0D
+ Error (NULL, 0, 2000, "Invalid parameter", "more than one input file s=
pecified");=0D
+ return STATUS_ERROR;=0D
+ } else if (InputFileNum < 1) {=0D
+ Error (NULL, 0, 2000, "Invalid parameter", "no input file specified");=
=0D
+ return STATUS_ERROR;=0D
+ }=0D
+=0D
+ //=0D
+ // read all input file contents into a buffer=0D
+ // first get the size of all file contents=0D
+ //=0D
+ Status =3D GetSectionContents (=0D
+ InputFileName,=0D
+ InputFileAlign,=0D
+ InputFileNum,=0D
+ FileBuffer,=0D
+ &InputLength=0D
+ );=0D
+=0D
+ if (Status =3D=3D EFI_BUFFER_TOO_SMALL) {=0D
+ Offset =3D sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION);=0D
+ if (InputLength + Offset >=3D MAX_SECTION_SIZE) {=0D
+ Offset =3D sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION2);=0D
+ }=0D
+ TotalLength =3D InputLength + Offset;=0D
+=0D
+ FileBuffer =3D (UINT8 *) malloc (InputLength + Offset);=0D
+ if (FileBuffer =3D=3D NULL) {=0D
+ Error (NULL, 0, 4001, "Resource", "memory cannot be allocated");=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+ //=0D
+ // read all input file contents into a buffer=0D
+ //=0D
+ Status =3D GetSectionContents (=0D
+ InputFileName,=0D
+ InputFileAlign,=0D
+ InputFileNum,=0D
+ FileBuffer + Offset,=0D
+ &InputLength=0D
+ );=0D
+ }=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ if (FileBuffer !=3D NULL) {=0D
+ free (FileBuffer);=0D
+ }=0D
+ Error (NULL, 0, 0001, "Error opening file for reading", InputFileName[=
0]);=0D
+ return Status;=0D
+ }=0D
+=0D
+ if (InputLength =3D=3D 0) {=0D
+ if (FileBuffer !=3D NULL) {=0D
+ free (FileBuffer);=0D
+ }=0D
+ Error (NULL, 0, 2000, "Invalid parameter", "the size of input file %s =
can't be zero", InputFileName);=0D
+ return EFI_NOT_FOUND;=0D
+ }=0D
+=0D
+ //=0D
+ // InputLength !=3D 0, but FileBuffer =3D=3D NULL means out of resources=
.=0D
+ //=0D
+ if (FileBuffer =3D=3D NULL) {=0D
+ Error (NULL, 0, 4001, "Resource", "memory cannot be allocated");=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ //=0D
+ // Now data is in FileBuffer + Offset=0D
+ //=0D
+ if (TotalLength >=3D MAX_SECTION_SIZE) {=0D
+ SubtypeGuidSect2 =3D (EFI_FREEFORM_SUBTYPE_GUID_SECTION2 *) FileBuffer=
;=0D
+ SubtypeGuidSect2->CommonHeader.Type =3D EFI_SECTION_GUID_DEFINED;=
=0D
+ SubtypeGuidSect2->CommonHeader.Size[0] =3D (UINT8) 0xff;=0D
+ SubtypeGuidSect2->CommonHeader.Size[1] =3D (UINT8) 0xff;=0D
+ SubtypeGuidSect2->CommonHeader.Size[2] =3D (UINT8) 0xff;=0D
+ SubtypeGuidSect2->CommonHeader.ExtendedSize =3D InputLength + sizeof (=
EFI_FREEFORM_SUBTYPE_GUID_SECTION2);=0D
+ memcpy (&(SubtypeGuidSect2->SubTypeGuid), SubTypeGuid, sizeof (EFI_GUI=
D));=0D
+ } else {=0D
+ SubtypeGuidSect =3D (EFI_FREEFORM_SUBTYPE_GUID_SECTION *) FileBuffer;=
=0D
+ SubtypeGuidSect->CommonHeader.Type =3D EFI_SECTION_FREEFORM_SUBTYP=
E_GUID;=0D
+ SubtypeGuidSect->CommonHeader.Size[0] =3D (UINT8) (TotalLength & 0xff=
);=0D
+ SubtypeGuidSect->CommonHeader.Size[1] =3D (UINT8) ((TotalLength & 0xf=
f00) >> 8);=0D
+ SubtypeGuidSect->CommonHeader.Size[2] =3D (UINT8) ((TotalLength & 0xf=
f0000) >> 16);=0D
+ memcpy (&(SubtypeGuidSect->SubTypeGuid), SubTypeGuid, sizeof (EFI_GUID=
));=0D
+ }=0D
+=0D
+ VerboseMsg ("the size of the created section file is %u bytes", (unsigne=
d) TotalLength);=0D
+=0D
+ //=0D
+ // Set OutFileBuffer=0D
+ //=0D
+ *OutFileBuffer =3D FileBuffer;=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
EFI_STATUS=0D
FfsRebaseImageRead (=0D
IN VOID *FileHandle,=0D
@@ -1591,14 +1740,22 @@ Returns:
}=0D
=0D
//=0D
- // GuidValue is only required by Guided section.=0D
+ // GuidValue is only required by Guided section and SubtypeGuid section.=
=0D
//=0D
- if ((SectType !=3D EFI_SECTION_GUID_DEFINED) &&=0D
+ if ((SectType !=3D EFI_SECTION_GUID_DEFINED) && (SectType !=3D EFI_SECTI=
ON_FREEFORM_SUBTYPE_GUID) &&=0D
(SectionName !=3D NULL) &&=0D
(CompareGuid (&VendorGuid, &mZeroGuid) !=3D 0)) {=0D
fprintf (stdout, "Warning: the input guid value is not required for th=
is section type %s\n", SectionName);=0D
}=0D
=0D
+ //=0D
+ // Check whether there is GUID for the SubtypeGuid section=0D
+ //=0D
+ if ((SectType =3D=3D EFI_SECTION_FREEFORM_SUBTYPE_GUID) && (CompareGuid =
(&VendorGuid, &mZeroGuid) =3D=3D 0)) {=0D
+ Error (NULL, 0, 1001, "Missing options", "GUID");=0D
+ goto Finish;=0D
+ }=0D
+=0D
//=0D
// Check whether there is input file=0D
//=0D
@@ -1667,6 +1824,16 @@ Returns:
);=0D
break;=0D
=0D
+ case EFI_SECTION_FREEFORM_SUBTYPE_GUID:=0D
+ Status =3D GenSectionSubtypeGuidSection (=0D
+ InputFileName,=0D
+ InputFileAlign,=0D
+ InputFileNum,=0D
+ &VendorGuid,=0D
+ &OutFileBuffer=0D
+ );=0D
+ break;=0D
+=0D
case EFI_SECTION_VERSION:=0D
Index =3D sizeof (EFI_COMMON_SECTION_HEADER);=0D
//=0D
--=20
2.25.1

Join devel@edk2.groups.io to automatically receive all group messages.