aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Aladyshev <aladyshev22@gmail.com>2022-08-08 14:53:25 +0300
committerKonstantin Aladyshev <aladyshev22@gmail.com>2022-08-08 14:53:25 +0300
commit0cda4601cf92ba1679ef8fbe00e3e4a77e185d2e (patch)
treef830b1275ee8e2bd75a5515069334ab895a369dc
parentb3e465c18f383f5415862ad7680ab2290b24f773 (diff)
downloadUEFI-Lessons-0cda4601cf92ba1679ef8fbe00e3e4a77e185d2e.tar.gz
UEFI-Lessons-0cda4601cf92ba1679ef8fbe00e3e4a77e185d2e.tar.bz2
UEFI-Lessons-0cda4601cf92ba1679ef8fbe00e3e4a77e185d2e.zip
Add PcdToken guard for Dynamic PCD
Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
-rw-r--r--Lessons/Lesson_25/README.md19
-rw-r--r--Lessons/Lesson_25/UefiLessonsPkg/PCDLesson/PCDLesson.c10
-rw-r--r--UefiLessonsPkg/PCDLesson/PCDLesson.c10
3 files changed, 33 insertions, 6 deletions
diff --git a/Lessons/Lesson_25/README.md b/Lessons/Lesson_25/README.md
index 15b0b85..ccbcc90 100644
--- a/Lessons/Lesson_25/README.md
+++ b/Lessons/Lesson_25/README.md
@@ -144,6 +144,25 @@ PcdDynamicInt32=0xCAFECAFE
PcdDynamicInt32=0xBEEFBEEF
```
+As we don't always use our application as a part of Ovmf package it would be better to guard the `PcdDynamicInt32` access for cases when local tokan is unassigned.
+
+We can do it with a help of a `PcdToken` macro:
+```
+#define PcdToken(TokenName) _PCD_TOKEN_##TokenName
+```
+
+Code usage as simple as this:
+```
+if (PcdToken(PcdDynamicInt32)) {
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ PcdSet32S(PcdDynamicInt32, 0xBEEFBEEF);
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+} else {
+ Print(L"PcdDynamicInt32 token is unassigned\n");
+}
+```
+At least now if you accidently copy `Build/UefiLessonsPkg/RELEASE_GCC5/X64/PCDLesson.efi` instead of `Build/OvmfX64/RELEASE_GCC5/X64/PCDLesson.efi` the application won't break the OVMF.
+
# Dynamic Ex PCD
Suppose we already have the necessary PCD in the PCD Database. And we want to access it via application from the UEFI Shell.
diff --git a/Lessons/Lesson_25/UefiLessonsPkg/PCDLesson/PCDLesson.c b/Lessons/Lesson_25/UefiLessonsPkg/PCDLesson/PCDLesson.c
index 904404f..a3ecfeb 100644
--- a/Lessons/Lesson_25/UefiLessonsPkg/PCDLesson/PCDLesson.c
+++ b/Lessons/Lesson_25/UefiLessonsPkg/PCDLesson/PCDLesson.c
@@ -57,9 +57,13 @@ UefiMain (
Print(L"Status=%r\n", Status);
Print(L"PcdPatchableInt32=%d\n", PatchPcdGet32(PcdPatchableInt32));
//-------
- Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
- PcdSet32S(PcdDynamicInt32, 0xBEEFBEEF);
- Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ if (PcdToken(PcdDynamicInt32)) {
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ PcdSet32S(PcdDynamicInt32, 0xBEEFBEEF);
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ } else {
+ Print(L"PcdDynamicInt32 token is unassigned\n");
+ }
Print(L"PcdDynamicExInt32=%x\n", PcdGetEx32(&gUefiLessonsPkgTokenSpaceGuid, PcdDynamicExInt32));
PcdSetEx32S(&gUefiLessonsPkgTokenSpaceGuid, PcdDynamicExInt32, 0x77777777);
diff --git a/UefiLessonsPkg/PCDLesson/PCDLesson.c b/UefiLessonsPkg/PCDLesson/PCDLesson.c
index 904404f..a3ecfeb 100644
--- a/UefiLessonsPkg/PCDLesson/PCDLesson.c
+++ b/UefiLessonsPkg/PCDLesson/PCDLesson.c
@@ -57,9 +57,13 @@ UefiMain (
Print(L"Status=%r\n", Status);
Print(L"PcdPatchableInt32=%d\n", PatchPcdGet32(PcdPatchableInt32));
//-------
- Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
- PcdSet32S(PcdDynamicInt32, 0xBEEFBEEF);
- Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ if (PcdToken(PcdDynamicInt32)) {
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ PcdSet32S(PcdDynamicInt32, 0xBEEFBEEF);
+ Print(L"PcdDynamicInt32=0x%x\n", PcdGet32(PcdDynamicInt32));
+ } else {
+ Print(L"PcdDynamicInt32 token is unassigned\n");
+ }
Print(L"PcdDynamicExInt32=%x\n", PcdGetEx32(&gUefiLessonsPkgTokenSpaceGuid, PcdDynamicExInt32));
PcdSetEx32S(&gUefiLessonsPkgTokenSpaceGuid, PcdDynamicExInt32, 0x77777777);