diff options
author | Konstantin Aladyshev <aladyshev22@gmail.com> | 2021-06-28 16:42:14 +0300 |
---|---|---|
committer | Konstantin Aladyshev <aladyshev22@gmail.com> | 2021-06-28 16:49:08 +0300 |
commit | 52f83512d1ea8e43d7590f7e761302db13932a76 (patch) | |
tree | 927d38fe8eb534a429501d97ae9e571fb4b61b8e /Lesson_20/README.md | |
parent | 5669003262cd685d3efc240426d263aa8fbb38bd (diff) | |
download | UEFI-Lessons-52f83512d1ea8e43d7590f7e761302db13932a76.tar.gz UEFI-Lessons-52f83512d1ea8e43d7590f7e761302db13932a76.tar.bz2 UEFI-Lessons-52f83512d1ea8e43d7590f7e761302db13932a76.zip |
Add lesson 20
Diffstat (limited to 'Lesson_20/README.md')
-rw-r--r-- | Lesson_20/README.md | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/Lesson_20/README.md b/Lesson_20/README.md new file mode 100644 index 0000000..7cce893 --- /dev/null +++ b/Lesson_20/README.md @@ -0,0 +1,87 @@ +The Platform Configuration Database (PCD) is a database that contains a variety of current platform settings or directives that can be accessed by a driver or application. + +You can checkout edk2 specification https://edk2-docs.gitbook.io/edk-ii-pcd-specification/ or https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/edkii-platform-config-database-entries-paper.pdf for more explanation on PCD. + +PCD entry is also called PCD, so we will use this term further. + +The PCD entry is defined in a DEC file in a format: +``` +<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token> +``` +`<TokenSpaceGuidCName>` is a GUID value, `<Token>` is a 32-bit value. Together they are used to uniqely identify PCD. + +First let's declare a Token Space that would contain all our PCDs. +Usually in is defined as a `g<PackageName>TokenSpaceGuid`, so add this to our `UefiLessonsPkg/UefiLessonsPkg.dec`: +``` +[Guids] + ... + gUefiLessonsPkgTokenSpaceGuid = {0x150cab53, 0xad47, 0x4385, {0xb5, 0xdd, 0xbc, 0xfc, 0x76, 0xba, 0xca, 0xf0}} +``` + +Now we can define our PCDs in the same *.dec file. Let's start with `UINT32 PcdMyVar32 = 42`: +``` +[PcdsFixedAtBuild] + gEfiUefiLessonsPkgTokenSpaceGuid.PcdMyVar32|42|UINT32|0x00000001 +``` + +Now create an app `PCDLesson` with the following code in its entry point function: +``` +Print(L"PcdMyVar32=%d\n", FixedPcdGet32(PcdMyVar32)); +``` +To use `FixedPcdGet32` in our code we need to add the necessary include: +``` +#include <Library/PcdLib.h> +``` +If you check out this file you'll see that `FixedPcdGet32` is simply a define statement: +``` +#define FixedPcdGet32(TokenName) _PCD_VALUE_##TokenName +``` + +If we try to build our app now, build will fail, as we don't have such define in our app: +``` +/home/kostr/tiano/edk2/MdePkg/Include/Library/PcdLib.h:97:45: error: ‘_PCD_VALUE_PcdMyVar32’ undeclared (first use in this function) + 97 | #define FixedPcdGet32(TokenName) _PCD_VALUE_##TokenName + | ^~~~~~~~~~~ +``` + +To fix this we need to add this PCD to our app `*.inf` file: +``` +[FixedPcd] + gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32 +``` + +Also we need to include "dec" file that defines this PCD: +``` +[Packages] + ... + UefiLessonsPkg/UefiLessonsPkg.dec +``` + +Now compilation would succeed. + +If you check out the content of autogenerated files `AutoGen.h`/`AutoGen.c`, you'll see, that our PCD is there. + +Build/UefiLessonsPkg/RELEASE_GCC5/X64/UefiLessonsPkg/PCDLesson/PCDLesson/DEBUG/AutoGen.h +``` +// Definition of PCDs used in this module + +#define _PCD_TOKEN_PcdMyVar32 0U +#define _PCD_SIZE_PcdMyVar32 4 +#define _PCD_GET_MODE_SIZE_PcdMyVar32 _PCD_SIZE_PcdMyVar32 +#define _PCD_VALUE_PcdMyVar32 42U +extern const UINT32 _gPcd_FixedAtBuild_PcdMyVar32; +#define _PCD_GET_MODE_32_PcdMyVar32 _gPcd_FixedAtBuild_PcdMyVar32 +//#define _PCD_SET_MODE_32_PcdMyVar32 ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD +``` + +Build/UefiLessonsPkg/RELEASE_GCC5/X64/UefiLessonsPkg/PCDLesson/PCDLesson/DEBUG/AutoGen.c +``` +// Definition of PCDs used in this module +GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPcd_FixedAtBuild_PcdMyVar32 = _PCD_VALUE_PcdMyVar32; +``` + +If you execute app code under OVMF: +``` +FS0:\> PCDLesson.efi +PcdMyVar32=42 +``` |