From 43a33dd6676acebded9e8879f1210cca5d974564 Mon Sep 17 00:00:00 2001 From: Konstantin Aladyshev Date: Fri, 22 Oct 2021 11:52:24 +0300 Subject: Add sources for lessons 45/46/47 Signed-off-by: Konstantin Aladyshev --- Lessons/Lesson_47/HIIStringsC/HIIStringsC.c | 111 ++++++++++++++++++++++++++ Lessons/Lesson_47/HIIStringsC/HIIStringsC.inf | 23 ++++++ Lessons/Lesson_47/UefiLessonsPkg.dec | 36 +++++++++ Lessons/Lesson_47/UefiLessonsPkg.dsc | 66 +++++++++++++++ UefiLessonsPkg/HIIStringsC/HIIStringsC.c | 111 ++++++++++++++++++++++++++ UefiLessonsPkg/HIIStringsC/HIIStringsC.inf | 23 ++++++ UefiLessonsPkg/UefiLessonsPkg.dec | 1 + UefiLessonsPkg/UefiLessonsPkg.dsc | 1 + 8 files changed, 372 insertions(+) create mode 100644 Lessons/Lesson_47/HIIStringsC/HIIStringsC.c create mode 100644 Lessons/Lesson_47/HIIStringsC/HIIStringsC.inf create mode 100644 Lessons/Lesson_47/UefiLessonsPkg.dec create mode 100644 Lessons/Lesson_47/UefiLessonsPkg.dsc create mode 100644 UefiLessonsPkg/HIIStringsC/HIIStringsC.c create mode 100644 UefiLessonsPkg/HIIStringsC/HIIStringsC.inf diff --git a/Lessons/Lesson_47/HIIStringsC/HIIStringsC.c b/Lessons/Lesson_47/HIIStringsC/HIIStringsC.c new file mode 100644 index 0000000..307351f --- /dev/null +++ b/Lessons/Lesson_47/HIIStringsC/HIIStringsC.c @@ -0,0 +1,111 @@ +#include +#include + +#include +#include + + +EFI_STATUS PrintStringFromHiiHandle(EFI_HII_HANDLE* Handle, CHAR8* Language, UINTN StringId) +{ + EFI_STRING String = NULL; + UINTN StringSize = 0; + EFI_STATUS Status = gHiiString->GetString(gHiiString, Language, *Handle, StringId, String, &StringSize, NULL); + + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + String = AllocateZeroPool (StringSize); + if (String == NULL) { + return Status; + } + + Status = gHiiString->GetString(gHiiString, Language, *Handle, StringId, String, &StringSize, NULL); + + Print(L"Status = %r, %s\n", Status, String); + + FreePool(String); + + return EFI_SUCCESS; +} + + +UINT32 InitStringPackage(CHAR8* Ptr, CHAR8* Language, CHAR16** Strings, UINTN StringsLen) +{ + UINT32 Size = 0; + EFI_HII_STRING_PACKAGE_HDR* HIIStringPackageHdr = (EFI_HII_STRING_PACKAGE_HDR*)&Ptr[0]; + HIIStringPackageHdr->Header.Type = EFI_HII_PACKAGE_STRINGS; + UINT32 HeaderSize = (UINT32) (AsciiStrSize(Language) - 1 + sizeof (EFI_HII_STRING_PACKAGE_HDR)); + HIIStringPackageHdr->HdrSize = HeaderSize; + HIIStringPackageHdr->StringInfoOffset = HeaderSize; + HIIStringPackageHdr->LanguageName = 1; + AsciiStrCpyS (HIIStringPackageHdr->Language, + (HeaderSize - OFFSET_OF(EFI_HII_STRING_PACKAGE_HDR,Language)) / sizeof (CHAR8), + (CHAR8 *) Language); + Size += HeaderSize; + + for (UINTN i=0; iHeader.BlockType = EFI_HII_SIBT_STRING_UCS2; + StrCpyS(StrBlock->StringText, StrLen(Strings[i])+1, Strings[i]); + Size += sizeof(EFI_HII_SIBT_STRING_UCS2_BLOCK) + StrLen(Strings[i])*2; + } + + EFI_HII_SIBT_END_BLOCK* EndBlock = (EFI_HII_SIBT_END_BLOCK*)&Ptr[Size]; + EndBlock->Header.BlockType = EFI_HII_SIBT_END; + Size += sizeof(EFI_HII_SIBT_END_BLOCK); + + HIIStringPackageHdr->Header.Length = Size; + + return Size; +} + + +EFI_STATUS +EFIAPI +UefiMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + CHAR8* Data = (CHAR8*) AllocateZeroPool(200); // CHEAT! NEEDS CORRECTION FOR YOUR OWN PACKAGES! + UINT32 offset = 0; + EFI_HII_PACKAGE_LIST_HEADER* PackageListHdr = (EFI_HII_PACKAGE_LIST_HEADER*)&Data[offset]; + PackageListHdr->PackageListGuid = gHIIStringsCGuid; + offset += sizeof(EFI_HII_PACKAGE_LIST_HEADER); + + CHAR16* EnStrings[] = { + L"English", + L"Hello", + }; + offset += InitStringPackage(&Data[offset], "en-US", EnStrings, sizeof(EnStrings)/sizeof(EnStrings[0])); + + CHAR16* FrStrings[] = { + L"French", + L"Bonjour", + }; + offset += InitStringPackage(&Data[offset], "fr-FR", FrStrings, sizeof(FrStrings)/sizeof(FrStrings[0])); + + EFI_HII_PACKAGE_HEADER* HIIEndPackageHdr = (EFI_HII_PACKAGE_HEADER*)&Data[offset]; + HIIEndPackageHdr->Type = EFI_HII_PACKAGE_END; + HIIEndPackageHdr->Length = sizeof(EFI_HII_PACKAGE_HEADER); + offset += sizeof(EFI_HII_PACKAGE_HEADER); + + PackageListHdr->PackageLength = offset; + + EFI_HII_HANDLE Handle; + EFI_STATUS Status = gHiiDatabase->NewPackageList(gHiiDatabase, PackageListHdr, NULL, &Handle); + if (EFI_ERROR(Status)) + { + Print(L"Can't register HII Package list %g, status = %r\n", gHIIStringsCGuid, Status); + } + FreePool(Data); + + PrintStringFromHiiHandle(&Handle, "en-US", 1); + PrintStringFromHiiHandle(&Handle, "en-US", 2); + PrintStringFromHiiHandle(&Handle, "fr-FR", 1); + PrintStringFromHiiHandle(&Handle, "fr-FR", 2); + + return EFI_SUCCESS; +} diff --git a/Lessons/Lesson_47/HIIStringsC/HIIStringsC.inf b/Lessons/Lesson_47/HIIStringsC/HIIStringsC.inf new file mode 100644 index 0000000..51f4b48 --- /dev/null +++ b/Lessons/Lesson_47/HIIStringsC/HIIStringsC.inf @@ -0,0 +1,23 @@ +[Defines] + INF_VERSION = 1.25 + BASE_NAME = HIIStringsC + FILE_GUID = 98f85c26-f491-40bc-8fd0-60b57a2383f6 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = UefiMain + +[Sources] + HIIStringsC.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiLessonsPkg/UefiLessonsPkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + UefiHiiServicesLib + +[Guids] + gHIIStringsCGuid diff --git a/Lessons/Lesson_47/UefiLessonsPkg.dec b/Lessons/Lesson_47/UefiLessonsPkg.dec new file mode 100644 index 0000000..8a6ff4b --- /dev/null +++ b/Lessons/Lesson_47/UefiLessonsPkg.dec @@ -0,0 +1,36 @@ +[Defines] + DEC_SPECIFICATION = 0x00010005 + PACKAGE_NAME = UefiLessonsPkg + PACKAGE_GUID = 7e7edbba-ca2c-4177-a3f0-d3371358773a + PACKAGE_VERSION = 1.01 + +[Includes] + Include + +[Guids] + # FILE_GUID as defined in UefiLessonsPkg/HelloWorld/HelloWorld.inf + gHelloWorldFileGuid = {0x2e55fa38, 0xf148, 0x42d3, {0xaf, 0x90, 0x1b, 0xe2, 0x47, 0x32, 0x3e, 0x30}} + gUefiLessonsPkgTokenSpaceGuid = {0x150cab53, 0xad47, 0x4385, {0xb5, 0xdd, 0xbc, 0xfc, 0x76, 0xba, 0xca, 0xf0}} + gHIIStringsCGuid = { 0x8e0b8ed3, 0x14f7, 0x499d, { 0xa2, 0x24, 0xae, 0xe8, 0x9d, 0xc9, 0x7f, 0xa3 }} + +[Protocols] + gSimpleClassProtocolGuid = { 0xb5510eea, 0x6f11, 0x4e4b, { 0xad, 0x0f, 0x35, 0xce, 0x17, 0xbd, 0x7a, 0x67 }} + +[PcdsFixedAtBuild] + gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32|42|UINT32|0x00000001 + gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_1|42|UINT32|0x00000002 + gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_2|42|UINT32|0x00000003 + gUefiLessonsPkgTokenSpaceGuid.PcdMyVarBool|FALSE|BOOLEAN|0x00000004 + +[PcdsPatchableInModule] + gUefiLessonsPkgTokenSpaceGuid.PcdMyPatchableVar32|0x31313131|UINT32|0x10000001 + +[PcdsFeatureFlag] + gUefiLessonsPkgTokenSpaceGuid.PcdMyFeatureFlagVar|FALSE|BOOLEAN|0x20000001 + +[PcdsDynamic] + gUefiLessonsPkgTokenSpaceGuid.PcdMyDynamicVar32|0x38323232|UINT32|0x30000001 + gUefiLessonsPkgTokenSpaceGuid.PcdMyDynamicVar32_1|42|UINT32|0x30000002 + +[PcdsDynamicEx] + gUefiLessonsPkgTokenSpaceGuid.PcdMyDynamicExVar32|0x38333333|UINT32|0x40000001 diff --git a/Lessons/Lesson_47/UefiLessonsPkg.dsc b/Lessons/Lesson_47/UefiLessonsPkg.dsc new file mode 100644 index 0000000..075cc8e --- /dev/null +++ b/Lessons/Lesson_47/UefiLessonsPkg.dsc @@ -0,0 +1,66 @@ +[Defines] + DSC_SPECIFICATION = 0x0001001C + PLATFORM_GUID = 3db7270f-ffac-4139-90a4-0ae68f3f8167 + PLATFORM_VERSION = 0.01 + PLATFORM_NAME = UefiLessonsPkg + SKUID_IDENTIFIER = DEFAULT + SUPPORTED_ARCHITECTURES = X64 + BUILD_TARGETS = RELEASE + + +[LibraryClasses] + UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf + UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf + DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf + BaseLib|MdePkg/Library/BaseLib/BaseLib.inf + #PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf + PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf + BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf + RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf + PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf + DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf + UefiLib|MdePkg/Library/UefiLib/UefiLib.inf + MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf + DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf + UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf + ShellCEntryLib|ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.inf + ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf + FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf + HiiLib|MdeModulePkg/Library/UefiHiiLib/UefiHiiLib.inf + SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf + UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf + UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf + #SimpleLibrary|UefiLessonsPkg/Library/SimpleLibrary/SimpleLibrary.inf + #SimpleLibrary|UefiLessonsPkg/Library/SimpleLibraryWithConstructor/SimpleLibraryWithConstructor.inf + SimpleLibrary|UefiLessonsPkg/Library/SimpleLibraryWithConstructorAndDestructor/SimpleLibraryWithConstructorAndDestructor.inf + +[Components] + UefiLessonsPkg/SimplestApp/SimplestApp.inf + UefiLessonsPkg/HelloWorld/HelloWorld.inf + UefiLessonsPkg/ImageHandle/ImageHandle.inf + UefiLessonsPkg/ImageInfo/ImageInfo.inf + UefiLessonsPkg/MemoryInfo/MemoryInfo.inf + UefiLessonsPkg/SimpleShellApp/SimpleShellApp.inf + UefiLessonsPkg/ListVariables/ListVariables.inf + UefiLessonsPkg/ShowBootVariables/ShowBootVariables.inf + UefiLessonsPkg/InteractiveApp/InteractiveApp.inf + UefiLessonsPkg/PCDLesson/PCDLesson.inf + UefiLessonsPkg/SmbiosInfo/SmbiosInfo.inf + UefiLessonsPkg/ShowTables/ShowTables.inf + UefiLessonsPkg/AcpiInfo/AcpiInfo.inf + UefiLessonsPkg/SaveBGRT/SaveBGRT.inf + UefiLessonsPkg/ListPCI/ListPCI.inf + UefiLessonsPkg/SimpleDriver/SimpleDriver.inf + UefiLessonsPkg/PCIRomInfo/PCIRomInfo.inf + UefiLessonsPkg/Library/SimpleLibrary/SimpleLibrary.inf + UefiLessonsPkg/Library/SimpleLibraryWithConstructor/SimpleLibraryWithConstructor.inf + UefiLessonsPkg/SimpleLibraryUser/SimpleLibraryUser.inf + UefiLessonsPkg/SimpleClassProtocol/SimpleClassProtocol.inf + UefiLessonsPkg/SimpleClassUser/SimpleClassUser.inf + UefiLessonsPkg/HotKeyDriver/HotKeyDriver.inf + UefiLessonsPkg/ShowHII/ShowHII.inf + UefiLessonsPkg/HIIStringsC/HIIStringsC.inf + +[PcdsFixedAtBuild] + gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_2|44 + diff --git a/UefiLessonsPkg/HIIStringsC/HIIStringsC.c b/UefiLessonsPkg/HIIStringsC/HIIStringsC.c new file mode 100644 index 0000000..307351f --- /dev/null +++ b/UefiLessonsPkg/HIIStringsC/HIIStringsC.c @@ -0,0 +1,111 @@ +#include +#include + +#include +#include + + +EFI_STATUS PrintStringFromHiiHandle(EFI_HII_HANDLE* Handle, CHAR8* Language, UINTN StringId) +{ + EFI_STRING String = NULL; + UINTN StringSize = 0; + EFI_STATUS Status = gHiiString->GetString(gHiiString, Language, *Handle, StringId, String, &StringSize, NULL); + + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + String = AllocateZeroPool (StringSize); + if (String == NULL) { + return Status; + } + + Status = gHiiString->GetString(gHiiString, Language, *Handle, StringId, String, &StringSize, NULL); + + Print(L"Status = %r, %s\n", Status, String); + + FreePool(String); + + return EFI_SUCCESS; +} + + +UINT32 InitStringPackage(CHAR8* Ptr, CHAR8* Language, CHAR16** Strings, UINTN StringsLen) +{ + UINT32 Size = 0; + EFI_HII_STRING_PACKAGE_HDR* HIIStringPackageHdr = (EFI_HII_STRING_PACKAGE_HDR*)&Ptr[0]; + HIIStringPackageHdr->Header.Type = EFI_HII_PACKAGE_STRINGS; + UINT32 HeaderSize = (UINT32) (AsciiStrSize(Language) - 1 + sizeof (EFI_HII_STRING_PACKAGE_HDR)); + HIIStringPackageHdr->HdrSize = HeaderSize; + HIIStringPackageHdr->StringInfoOffset = HeaderSize; + HIIStringPackageHdr->LanguageName = 1; + AsciiStrCpyS (HIIStringPackageHdr->Language, + (HeaderSize - OFFSET_OF(EFI_HII_STRING_PACKAGE_HDR,Language)) / sizeof (CHAR8), + (CHAR8 *) Language); + Size += HeaderSize; + + for (UINTN i=0; iHeader.BlockType = EFI_HII_SIBT_STRING_UCS2; + StrCpyS(StrBlock->StringText, StrLen(Strings[i])+1, Strings[i]); + Size += sizeof(EFI_HII_SIBT_STRING_UCS2_BLOCK) + StrLen(Strings[i])*2; + } + + EFI_HII_SIBT_END_BLOCK* EndBlock = (EFI_HII_SIBT_END_BLOCK*)&Ptr[Size]; + EndBlock->Header.BlockType = EFI_HII_SIBT_END; + Size += sizeof(EFI_HII_SIBT_END_BLOCK); + + HIIStringPackageHdr->Header.Length = Size; + + return Size; +} + + +EFI_STATUS +EFIAPI +UefiMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + CHAR8* Data = (CHAR8*) AllocateZeroPool(200); // CHEAT! NEEDS CORRECTION FOR YOUR OWN PACKAGES! + UINT32 offset = 0; + EFI_HII_PACKAGE_LIST_HEADER* PackageListHdr = (EFI_HII_PACKAGE_LIST_HEADER*)&Data[offset]; + PackageListHdr->PackageListGuid = gHIIStringsCGuid; + offset += sizeof(EFI_HII_PACKAGE_LIST_HEADER); + + CHAR16* EnStrings[] = { + L"English", + L"Hello", + }; + offset += InitStringPackage(&Data[offset], "en-US", EnStrings, sizeof(EnStrings)/sizeof(EnStrings[0])); + + CHAR16* FrStrings[] = { + L"French", + L"Bonjour", + }; + offset += InitStringPackage(&Data[offset], "fr-FR", FrStrings, sizeof(FrStrings)/sizeof(FrStrings[0])); + + EFI_HII_PACKAGE_HEADER* HIIEndPackageHdr = (EFI_HII_PACKAGE_HEADER*)&Data[offset]; + HIIEndPackageHdr->Type = EFI_HII_PACKAGE_END; + HIIEndPackageHdr->Length = sizeof(EFI_HII_PACKAGE_HEADER); + offset += sizeof(EFI_HII_PACKAGE_HEADER); + + PackageListHdr->PackageLength = offset; + + EFI_HII_HANDLE Handle; + EFI_STATUS Status = gHiiDatabase->NewPackageList(gHiiDatabase, PackageListHdr, NULL, &Handle); + if (EFI_ERROR(Status)) + { + Print(L"Can't register HII Package list %g, status = %r\n", gHIIStringsCGuid, Status); + } + FreePool(Data); + + PrintStringFromHiiHandle(&Handle, "en-US", 1); + PrintStringFromHiiHandle(&Handle, "en-US", 2); + PrintStringFromHiiHandle(&Handle, "fr-FR", 1); + PrintStringFromHiiHandle(&Handle, "fr-FR", 2); + + return EFI_SUCCESS; +} diff --git a/UefiLessonsPkg/HIIStringsC/HIIStringsC.inf b/UefiLessonsPkg/HIIStringsC/HIIStringsC.inf new file mode 100644 index 0000000..51f4b48 --- /dev/null +++ b/UefiLessonsPkg/HIIStringsC/HIIStringsC.inf @@ -0,0 +1,23 @@ +[Defines] + INF_VERSION = 1.25 + BASE_NAME = HIIStringsC + FILE_GUID = 98f85c26-f491-40bc-8fd0-60b57a2383f6 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = UefiMain + +[Sources] + HIIStringsC.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiLessonsPkg/UefiLessonsPkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + UefiHiiServicesLib + +[Guids] + gHIIStringsCGuid diff --git a/UefiLessonsPkg/UefiLessonsPkg.dec b/UefiLessonsPkg/UefiLessonsPkg.dec index 8d14eec..8a6ff4b 100644 --- a/UefiLessonsPkg/UefiLessonsPkg.dec +++ b/UefiLessonsPkg/UefiLessonsPkg.dec @@ -11,6 +11,7 @@ # FILE_GUID as defined in UefiLessonsPkg/HelloWorld/HelloWorld.inf gHelloWorldFileGuid = {0x2e55fa38, 0xf148, 0x42d3, {0xaf, 0x90, 0x1b, 0xe2, 0x47, 0x32, 0x3e, 0x30}} gUefiLessonsPkgTokenSpaceGuid = {0x150cab53, 0xad47, 0x4385, {0xb5, 0xdd, 0xbc, 0xfc, 0x76, 0xba, 0xca, 0xf0}} + gHIIStringsCGuid = { 0x8e0b8ed3, 0x14f7, 0x499d, { 0xa2, 0x24, 0xae, 0xe8, 0x9d, 0xc9, 0x7f, 0xa3 }} [Protocols] gSimpleClassProtocolGuid = { 0xb5510eea, 0x6f11, 0x4e4b, { 0xad, 0x0f, 0x35, 0xce, 0x17, 0xbd, 0x7a, 0x67 }} diff --git a/UefiLessonsPkg/UefiLessonsPkg.dsc b/UefiLessonsPkg/UefiLessonsPkg.dsc index 7912ad7..075cc8e 100644 --- a/UefiLessonsPkg/UefiLessonsPkg.dsc +++ b/UefiLessonsPkg/UefiLessonsPkg.dsc @@ -59,6 +59,7 @@ UefiLessonsPkg/SimpleClassUser/SimpleClassUser.inf UefiLessonsPkg/HotKeyDriver/HotKeyDriver.inf UefiLessonsPkg/ShowHII/ShowHII.inf + UefiLessonsPkg/HIIStringsC/HIIStringsC.inf [PcdsFixedAtBuild] gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_2|44 -- cgit v1.2.3-18-g5258