2 UEFI PropertiesTable support
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 #include <Library/DxeServicesTableLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/UefiLib.h>
17 #include <Library/PcdLib.h>
19 #include <Guid/EventGroup.h>
20 #include <Protocol/DxeSmmReadyToLock.h>
22 #include <Library/PeCoffLib.h>
23 #include <Library/PeCoffGetEntryPointLib.h>
24 #include <Protocol/Runtime.h>
26 #include <Guid/PropertiesTable.h>
29 #include "HeapGuard.h"
31 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
32 ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
34 #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
38 UINTN ImageRecordCount
;
39 UINTN CodeSegmentCountMax
;
40 LIST_ENTRY ImageRecordList
;
41 } IMAGE_PROPERTIES_PRIVATE_DATA
;
43 IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData
= {
44 IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE
,
47 INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData
.ImageRecordList
)
50 EFI_PROPERTIES_TABLE mPropertiesTable
= {
51 EFI_PROPERTIES_TABLE_VERSION
,
52 sizeof(EFI_PROPERTIES_TABLE
),
53 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
56 EFI_LOCK mPropertiesTableLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
58 BOOLEAN mPropertiesTableEnable
;
60 BOOLEAN mPropertiesTableEndOfDxe
= FALSE
;
63 // Below functions are for MemoryMap
67 Converts a number of EFI_PAGEs to a size in bytes.
69 NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.
71 @param Pages The number of EFI_PAGES.
73 @return The number of bytes associated with the number of EFI_PAGEs specified
82 return LShiftU64 (Pages
, EFI_PAGE_SHIFT
);
86 Converts a size, in bytes, to a number of EFI_PAGESs.
88 NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only.
90 @param Size A size in bytes.
92 @return The number of EFI_PAGESs associated with the number of bytes specified
102 return RShiftU64 (Size
, EFI_PAGE_SHIFT
) + ((((UINTN
)Size
) & EFI_PAGE_MASK
) ? 1 : 0);
106 Acquire memory lock on mPropertiesTableLock.
110 CoreAcquirePropertiesTableLock (
114 CoreAcquireLock (&mPropertiesTableLock
);
118 Release memory lock on mPropertiesTableLock.
122 CoreReleasePropertiesTableLock (
126 CoreReleaseLock (&mPropertiesTableLock
);
130 Sort memory map entries based upon PhysicalStart, from low to high.
132 @param MemoryMap A pointer to the buffer in which firmware places
133 the current memory map.
134 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
135 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
140 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
141 IN UINTN MemoryMapSize
,
142 IN UINTN DescriptorSize
145 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
146 EFI_MEMORY_DESCRIPTOR
*NextMemoryMapEntry
;
147 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
148 EFI_MEMORY_DESCRIPTOR TempMemoryMap
;
150 MemoryMapEntry
= MemoryMap
;
151 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
152 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
153 while (MemoryMapEntry
< MemoryMapEnd
) {
154 while (NextMemoryMapEntry
< MemoryMapEnd
) {
155 if (MemoryMapEntry
->PhysicalStart
> NextMemoryMapEntry
->PhysicalStart
) {
156 CopyMem (&TempMemoryMap
, MemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
157 CopyMem (MemoryMapEntry
, NextMemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
158 CopyMem (NextMemoryMapEntry
, &TempMemoryMap
, sizeof(EFI_MEMORY_DESCRIPTOR
));
161 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry
, DescriptorSize
);
164 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
165 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
172 Merge continous memory map entries whose have same attributes.
174 @param MemoryMap A pointer to the buffer in which firmware places
175 the current memory map.
176 @param MemoryMapSize A pointer to the size, in bytes, of the
177 MemoryMap buffer. On input, this is the size of
178 the current memory map. On output,
179 it is the size of new memory map after merge.
180 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
184 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
185 IN OUT UINTN
*MemoryMapSize
,
186 IN UINTN DescriptorSize
189 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
190 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
191 UINT64 MemoryBlockLength
;
192 EFI_MEMORY_DESCRIPTOR
*NewMemoryMapEntry
;
193 EFI_MEMORY_DESCRIPTOR
*NextMemoryMapEntry
;
195 MemoryMapEntry
= MemoryMap
;
196 NewMemoryMapEntry
= MemoryMap
;
197 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ *MemoryMapSize
);
198 while ((UINTN
)MemoryMapEntry
< (UINTN
)MemoryMapEnd
) {
199 CopyMem (NewMemoryMapEntry
, MemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
200 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
203 MergeGuardPages (NewMemoryMapEntry
, NextMemoryMapEntry
->PhysicalStart
);
204 MemoryBlockLength
= (UINT64
) (EfiPagesToSize (NewMemoryMapEntry
->NumberOfPages
));
205 if (((UINTN
)NextMemoryMapEntry
< (UINTN
)MemoryMapEnd
) &&
206 (NewMemoryMapEntry
->Type
== NextMemoryMapEntry
->Type
) &&
207 (NewMemoryMapEntry
->Attribute
== NextMemoryMapEntry
->Attribute
) &&
208 ((NewMemoryMapEntry
->PhysicalStart
+ MemoryBlockLength
) == NextMemoryMapEntry
->PhysicalStart
)) {
209 NewMemoryMapEntry
->NumberOfPages
+= NextMemoryMapEntry
->NumberOfPages
;
210 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry
, DescriptorSize
);
213 MemoryMapEntry
= PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry
, DescriptorSize
);
218 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
219 NewMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry
, DescriptorSize
);
222 *MemoryMapSize
= (UINTN
)NewMemoryMapEntry
- (UINTN
)MemoryMap
;
228 Enforce memory map attributes.
229 This function will set EfiRuntimeServicesData/EfiMemoryMappedIO/EfiMemoryMappedIOPortSpace to be EFI_MEMORY_XP.
231 @param MemoryMap A pointer to the buffer in which firmware places
232 the current memory map.
233 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
234 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
238 EnforceMemoryMapAttribute (
239 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
240 IN UINTN MemoryMapSize
,
241 IN UINTN DescriptorSize
244 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
245 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
247 MemoryMapEntry
= MemoryMap
;
248 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
249 while ((UINTN
)MemoryMapEntry
< (UINTN
)MemoryMapEnd
) {
250 switch (MemoryMapEntry
->Type
) {
251 case EfiRuntimeServicesCode
:
254 case EfiRuntimeServicesData
:
255 case EfiMemoryMappedIO
:
256 case EfiMemoryMappedIOPortSpace
:
257 MemoryMapEntry
->Attribute
|= EFI_MEMORY_XP
;
259 case EfiReservedMemoryType
:
260 case EfiACPIMemoryNVS
:
264 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
271 Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].
273 @param Buffer Start Address
274 @param Length Address length
276 @return first image record covered by [buffer, length]
279 IMAGE_PROPERTIES_RECORD
*
280 GetImageRecordByAddress (
281 IN EFI_PHYSICAL_ADDRESS Buffer
,
285 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
286 LIST_ENTRY
*ImageRecordLink
;
287 LIST_ENTRY
*ImageRecordList
;
289 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
291 for (ImageRecordLink
= ImageRecordList
->ForwardLink
;
292 ImageRecordLink
!= ImageRecordList
;
293 ImageRecordLink
= ImageRecordLink
->ForwardLink
) {
296 IMAGE_PROPERTIES_RECORD
,
298 IMAGE_PROPERTIES_RECORD_SIGNATURE
301 if ((Buffer
<= ImageRecord
->ImageBase
) &&
302 (Buffer
+ Length
>= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
)) {
311 Set the memory map to new entries, according to one old entry,
312 based upon PE code section and data section in image record
314 @param ImageRecord An image record whose [ImageBase, ImageSize] covered
315 by old memory map entry.
316 @param NewRecord A pointer to several new memory map entries.
317 The caller gurantee the buffer size be 1 +
318 (SplitRecordCount * DescriptorSize) calculated
320 @param OldRecord A pointer to one old memory map entry.
321 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
326 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
,
327 IN OUT EFI_MEMORY_DESCRIPTOR
*NewRecord
,
328 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
,
329 IN UINTN DescriptorSize
332 EFI_MEMORY_DESCRIPTOR TempRecord
;
333 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
334 LIST_ENTRY
*ImageRecordCodeSectionLink
;
335 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
336 LIST_ENTRY
*ImageRecordCodeSectionList
;
337 UINTN NewRecordCount
;
341 CopyMem (&TempRecord
, OldRecord
, sizeof(EFI_MEMORY_DESCRIPTOR
));
342 PhysicalEnd
= TempRecord
.PhysicalStart
+ EfiPagesToSize(TempRecord
.NumberOfPages
);
345 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
347 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
348 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
349 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
350 ImageRecordCodeSection
= CR (
351 ImageRecordCodeSectionLink
,
352 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
354 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
356 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
358 if (TempRecord
.PhysicalStart
<= ImageRecordCodeSection
->CodeSegmentBase
) {
362 if (!mPropertiesTableEnable
) {
363 NewRecord
->Type
= TempRecord
.Type
;
365 NewRecord
->Type
= EfiRuntimeServicesData
;
367 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
368 NewRecord
->VirtualStart
= 0;
369 NewRecord
->NumberOfPages
= EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentBase
- NewRecord
->PhysicalStart
);
370 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
371 if (NewRecord
->NumberOfPages
!= 0) {
372 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
379 if (!mPropertiesTableEnable
) {
380 NewRecord
->Type
= TempRecord
.Type
;
382 NewRecord
->Type
= EfiRuntimeServicesCode
;
384 NewRecord
->PhysicalStart
= ImageRecordCodeSection
->CodeSegmentBase
;
385 NewRecord
->VirtualStart
= 0;
386 NewRecord
->NumberOfPages
= EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentSize
);
387 NewRecord
->Attribute
= (TempRecord
.Attribute
& (~EFI_MEMORY_XP
)) | EFI_MEMORY_RO
;
388 if (NewRecord
->NumberOfPages
!= 0) {
389 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
393 TempRecord
.PhysicalStart
= ImageRecordCodeSection
->CodeSegmentBase
+ EfiPagesToSize (EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentSize
));
394 TempRecord
.NumberOfPages
= EfiSizeToPages(PhysicalEnd
- TempRecord
.PhysicalStart
);
395 if (TempRecord
.NumberOfPages
== 0) {
401 ImageEnd
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
406 if (TempRecord
.PhysicalStart
< ImageEnd
) {
407 if (!mPropertiesTableEnable
) {
408 NewRecord
->Type
= TempRecord
.Type
;
410 NewRecord
->Type
= EfiRuntimeServicesData
;
412 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
413 NewRecord
->VirtualStart
= 0;
414 NewRecord
->NumberOfPages
= EfiSizeToPages (ImageEnd
- TempRecord
.PhysicalStart
);
415 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
419 return NewRecordCount
;
423 Return the max number of new splitted entries, according to one old entry,
424 based upon PE code section and data section.
426 @param OldRecord A pointer to one old memory map entry.
428 @retval 0 no entry need to be splitted.
429 @return the max number of new splitted entries
433 GetMaxSplitRecordCount (
434 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
437 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
438 UINTN SplitRecordCount
;
439 UINT64 PhysicalStart
;
442 SplitRecordCount
= 0;
443 PhysicalStart
= OldRecord
->PhysicalStart
;
444 PhysicalEnd
= OldRecord
->PhysicalStart
+ EfiPagesToSize(OldRecord
->NumberOfPages
);
447 ImageRecord
= GetImageRecordByAddress (PhysicalStart
, PhysicalEnd
- PhysicalStart
);
448 if (ImageRecord
== NULL
) {
451 SplitRecordCount
+= (2 * ImageRecord
->CodeSegmentCount
+ 1);
452 PhysicalStart
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
453 } while ((ImageRecord
!= NULL
) && (PhysicalStart
< PhysicalEnd
));
455 if (SplitRecordCount
!= 0) {
459 return SplitRecordCount
;
463 Split the memory map to new entries, according to one old entry,
464 based upon PE code section and data section.
466 @param OldRecord A pointer to one old memory map entry.
467 @param NewRecord A pointer to several new memory map entries.
468 The caller gurantee the buffer size be 1 +
469 (SplitRecordCount * DescriptorSize) calculated
471 @param MaxSplitRecordCount The max number of splitted entries
472 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
474 @retval 0 no entry is splitted.
475 @return the real number of splitted record.
480 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
,
481 IN OUT EFI_MEMORY_DESCRIPTOR
*NewRecord
,
482 IN UINTN MaxSplitRecordCount
,
483 IN UINTN DescriptorSize
486 EFI_MEMORY_DESCRIPTOR TempRecord
;
487 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
488 IMAGE_PROPERTIES_RECORD
*NewImageRecord
;
489 UINT64 PhysicalStart
;
491 UINTN NewRecordCount
;
492 UINTN TotalNewRecordCount
;
493 BOOLEAN IsLastRecordData
;
495 if (MaxSplitRecordCount
== 0) {
496 CopyMem (NewRecord
, OldRecord
, DescriptorSize
);
500 TotalNewRecordCount
= 0;
503 // Override previous record
505 CopyMem (&TempRecord
, OldRecord
, sizeof(EFI_MEMORY_DESCRIPTOR
));
506 PhysicalStart
= TempRecord
.PhysicalStart
;
507 PhysicalEnd
= TempRecord
.PhysicalStart
+ EfiPagesToSize(TempRecord
.NumberOfPages
);
511 NewImageRecord
= GetImageRecordByAddress (PhysicalStart
, PhysicalEnd
- PhysicalStart
);
512 if (NewImageRecord
== NULL
) {
514 // No more image covered by this range, stop
516 if ((PhysicalEnd
> PhysicalStart
) && (ImageRecord
!= NULL
)) {
518 // If this is still address in this record, need record.
520 NewRecord
= PREVIOUS_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
521 IsLastRecordData
= FALSE
;
522 if (!mPropertiesTableEnable
) {
523 if ((NewRecord
->Attribute
& EFI_MEMORY_XP
) != 0) {
524 IsLastRecordData
= TRUE
;
527 if (NewRecord
->Type
== EfiRuntimeServicesData
) {
528 IsLastRecordData
= TRUE
;
531 if (IsLastRecordData
) {
533 // Last record is DATA, just merge it.
535 NewRecord
->NumberOfPages
= EfiSizeToPages(PhysicalEnd
- NewRecord
->PhysicalStart
);
538 // Last record is CODE, create a new DATA entry.
540 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
541 if (!mPropertiesTableEnable
) {
542 NewRecord
->Type
= TempRecord
.Type
;
544 NewRecord
->Type
= EfiRuntimeServicesData
;
546 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
547 NewRecord
->VirtualStart
= 0;
548 NewRecord
->NumberOfPages
= TempRecord
.NumberOfPages
;
549 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
550 TotalNewRecordCount
++;
555 ImageRecord
= NewImageRecord
;
560 NewRecordCount
= SetNewRecord (ImageRecord
, NewRecord
, &TempRecord
, DescriptorSize
);
561 TotalNewRecordCount
+= NewRecordCount
;
562 NewRecord
= (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)NewRecord
+ NewRecordCount
* DescriptorSize
);
565 // Update PhysicalStart, in order to exclude the image buffer already splitted.
567 PhysicalStart
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
568 TempRecord
.PhysicalStart
= PhysicalStart
;
569 TempRecord
.NumberOfPages
= EfiSizeToPages (PhysicalEnd
- PhysicalStart
);
570 } while ((ImageRecord
!= NULL
) && (PhysicalStart
< PhysicalEnd
));
573 // The logic in function SplitTable() ensures that TotalNewRecordCount will not be zero if the
574 // code reaches here.
576 ASSERT (TotalNewRecordCount
!= 0);
577 return TotalNewRecordCount
- 1;
581 Split the original memory map, and add more entries to describe PE code section and data section.
582 This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP.
583 This function will merge entries with same attributes finally.
585 NOTE: It assumes PE code/data section are page aligned.
586 NOTE: It assumes enough entry is prepared for new memory map.
599 +---------------+ ----
602 | Record RtCode | |-> PE/COFF1
605 +---------------+ ----
608 | Record RtCode | |-> PE/COFF2
611 +---------------+ ----
615 @param MemoryMapSize A pointer to the size, in bytes, of the
616 MemoryMap buffer. On input, this is the size of
617 old MemoryMap before split. The actual buffer
618 size of MemoryMap is MemoryMapSize +
619 (AdditionalRecordCount * DescriptorSize) calculated
620 below. On output, it is the size of new MemoryMap
622 @param MemoryMap A pointer to the buffer in which firmware places
623 the current memory map.
624 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
629 IN OUT UINTN
*MemoryMapSize
,
630 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
631 IN UINTN DescriptorSize
636 UINTN MaxSplitRecordCount
;
637 UINTN RealSplitRecordCount
;
638 UINTN TotalSplitRecordCount
;
639 UINTN AdditionalRecordCount
;
641 AdditionalRecordCount
= (2 * mImagePropertiesPrivateData
.CodeSegmentCountMax
+ 1) * mImagePropertiesPrivateData
.ImageRecordCount
;
643 TotalSplitRecordCount
= 0;
645 // Let old record point to end of valid MemoryMap buffer.
647 IndexOld
= ((*MemoryMapSize
) / DescriptorSize
) - 1;
649 // Let new record point to end of full MemoryMap buffer.
651 IndexNew
= ((*MemoryMapSize
) / DescriptorSize
) - 1 + AdditionalRecordCount
;
652 for (; IndexOld
>= 0; IndexOld
--) {
653 MaxSplitRecordCount
= GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexOld
* DescriptorSize
));
655 // Split this MemoryMap record
657 IndexNew
-= MaxSplitRecordCount
;
658 RealSplitRecordCount
= SplitRecord (
659 (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexOld
* DescriptorSize
),
660 (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexNew
* DescriptorSize
),
665 // Adjust IndexNew according to real split.
668 ((UINT8
*)MemoryMap
+ (IndexNew
+ MaxSplitRecordCount
- RealSplitRecordCount
) * DescriptorSize
),
669 ((UINT8
*)MemoryMap
+ IndexNew
* DescriptorSize
),
670 RealSplitRecordCount
* DescriptorSize
672 IndexNew
= IndexNew
+ MaxSplitRecordCount
- RealSplitRecordCount
;
673 TotalSplitRecordCount
+= RealSplitRecordCount
;
677 // Move all records to the beginning.
681 (UINT8
*)MemoryMap
+ (AdditionalRecordCount
- TotalSplitRecordCount
) * DescriptorSize
,
682 (*MemoryMapSize
) + TotalSplitRecordCount
* DescriptorSize
685 *MemoryMapSize
= (*MemoryMapSize
) + DescriptorSize
* TotalSplitRecordCount
;
688 // Sort from low to high (Just in case)
690 SortMemoryMap (MemoryMap
, *MemoryMapSize
, DescriptorSize
);
693 // Set RuntimeData to XP
695 EnforceMemoryMapAttribute (MemoryMap
, *MemoryMapSize
, DescriptorSize
);
698 // Merge same type to save entry size
700 MergeMemoryMap (MemoryMap
, MemoryMapSize
, DescriptorSize
);
706 This function for GetMemoryMap() with properties table capability.
708 It calls original GetMemoryMap() to get the original memory map information. Then
709 plus the additional memory map entries for PE Code/Data seperation.
711 @param MemoryMapSize A pointer to the size, in bytes, of the
712 MemoryMap buffer. On input, this is the size of
713 the buffer allocated by the caller. On output,
714 it is the size of the buffer returned by the
715 firmware if the buffer was large enough, or the
716 size of the buffer needed to contain the map if
717 the buffer was too small.
718 @param MemoryMap A pointer to the buffer in which firmware places
719 the current memory map.
720 @param MapKey A pointer to the location in which firmware
721 returns the key for the current memory map.
722 @param DescriptorSize A pointer to the location in which firmware
723 returns the size, in bytes, of an individual
724 EFI_MEMORY_DESCRIPTOR.
725 @param DescriptorVersion A pointer to the location in which firmware
726 returns the version number associated with the
727 EFI_MEMORY_DESCRIPTOR.
729 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
731 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
732 buffer size needed to hold the memory map is
733 returned in MemoryMapSize.
734 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
739 CoreGetMemoryMapWithSeparatedImageSection (
740 IN OUT UINTN
*MemoryMapSize
,
741 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
743 OUT UINTN
*DescriptorSize
,
744 OUT UINT32
*DescriptorVersion
748 UINTN OldMemoryMapSize
;
749 UINTN AdditionalRecordCount
;
752 // If PE code/data is not aligned, just return.
754 if ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) == 0) {
755 return CoreGetMemoryMap (MemoryMapSize
, MemoryMap
, MapKey
, DescriptorSize
, DescriptorVersion
);
758 if (MemoryMapSize
== NULL
) {
759 return EFI_INVALID_PARAMETER
;
762 CoreAcquirePropertiesTableLock ();
764 AdditionalRecordCount
= (2 * mImagePropertiesPrivateData
.CodeSegmentCountMax
+ 1) * mImagePropertiesPrivateData
.ImageRecordCount
;
766 OldMemoryMapSize
= *MemoryMapSize
;
767 Status
= CoreGetMemoryMap (MemoryMapSize
, MemoryMap
, MapKey
, DescriptorSize
, DescriptorVersion
);
768 if (Status
== EFI_BUFFER_TOO_SMALL
) {
769 *MemoryMapSize
= *MemoryMapSize
+ (*DescriptorSize
) * AdditionalRecordCount
;
770 } else if (Status
== EFI_SUCCESS
) {
771 ASSERT (MemoryMap
!= NULL
);
772 if (OldMemoryMapSize
- *MemoryMapSize
< (*DescriptorSize
) * AdditionalRecordCount
) {
773 *MemoryMapSize
= *MemoryMapSize
+ (*DescriptorSize
) * AdditionalRecordCount
;
775 // Need update status to buffer too small
777 Status
= EFI_BUFFER_TOO_SMALL
;
780 // Split PE code/data
782 SplitTable (MemoryMapSize
, MemoryMap
, *DescriptorSize
);
786 CoreReleasePropertiesTableLock ();
791 // Below functions are for ImageRecord
795 Set PropertiesTable according to PE/COFF image section alignment.
797 @param SectionAlignment PE/COFF section alignment
801 SetPropertiesTableSectionAlignment (
802 IN UINT32 SectionAlignment
805 if (((SectionAlignment
& (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) &&
806 ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) != 0)) {
807 DEBUG ((EFI_D_VERBOSE
, "SetPropertiesTableSectionAlignment - Clear\n"));
808 mPropertiesTable
.MemoryProtectionAttribute
&= ~((UINT64
)EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
);
809 gBS
->GetMemoryMap
= CoreGetMemoryMap
;
811 gBS
->CalculateCrc32 ((UINT8
*)gBS
, gBS
->Hdr
.HeaderSize
, &gBS
->Hdr
.CRC32
);
816 Swap two code sections in image record.
818 @param FirstImageRecordCodeSection first code section in image record
819 @param SecondImageRecordCodeSection second code section in image record
823 SwapImageRecordCodeSection (
824 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION
*FirstImageRecordCodeSection
,
825 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION
*SecondImageRecordCodeSection
828 IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection
;
830 TempImageRecordCodeSection
.CodeSegmentBase
= FirstImageRecordCodeSection
->CodeSegmentBase
;
831 TempImageRecordCodeSection
.CodeSegmentSize
= FirstImageRecordCodeSection
->CodeSegmentSize
;
833 FirstImageRecordCodeSection
->CodeSegmentBase
= SecondImageRecordCodeSection
->CodeSegmentBase
;
834 FirstImageRecordCodeSection
->CodeSegmentSize
= SecondImageRecordCodeSection
->CodeSegmentSize
;
836 SecondImageRecordCodeSection
->CodeSegmentBase
= TempImageRecordCodeSection
.CodeSegmentBase
;
837 SecondImageRecordCodeSection
->CodeSegmentSize
= TempImageRecordCodeSection
.CodeSegmentSize
;
841 Sort code section in image record, based upon CodeSegmentBase from low to high.
843 @param ImageRecord image record to be sorted
846 SortImageRecordCodeSection (
847 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
850 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
851 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*NextImageRecordCodeSection
;
852 LIST_ENTRY
*ImageRecordCodeSectionLink
;
853 LIST_ENTRY
*NextImageRecordCodeSectionLink
;
854 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
855 LIST_ENTRY
*ImageRecordCodeSectionList
;
857 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
859 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
860 NextImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
861 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
862 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
863 ImageRecordCodeSection
= CR (
864 ImageRecordCodeSectionLink
,
865 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
867 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
869 while (NextImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
870 NextImageRecordCodeSection
= CR (
871 NextImageRecordCodeSectionLink
,
872 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
874 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
876 if (ImageRecordCodeSection
->CodeSegmentBase
> NextImageRecordCodeSection
->CodeSegmentBase
) {
877 SwapImageRecordCodeSection (ImageRecordCodeSection
, NextImageRecordCodeSection
);
879 NextImageRecordCodeSectionLink
= NextImageRecordCodeSectionLink
->ForwardLink
;
882 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
883 NextImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
888 Check if code section in image record is valid.
890 @param ImageRecord image record to be checked
892 @retval TRUE image record is valid
893 @retval FALSE image record is invalid
896 IsImageRecordCodeSectionValid (
897 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
900 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
901 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*LastImageRecordCodeSection
;
902 LIST_ENTRY
*ImageRecordCodeSectionLink
;
903 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
904 LIST_ENTRY
*ImageRecordCodeSectionList
;
906 DEBUG ((EFI_D_VERBOSE
, "ImageCode SegmentCount - 0x%x\n", ImageRecord
->CodeSegmentCount
));
908 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
910 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
911 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
912 LastImageRecordCodeSection
= NULL
;
913 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
914 ImageRecordCodeSection
= CR (
915 ImageRecordCodeSectionLink
,
916 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
918 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
920 if (ImageRecordCodeSection
->CodeSegmentSize
== 0) {
923 if (ImageRecordCodeSection
->CodeSegmentBase
< ImageRecord
->ImageBase
) {
926 if (ImageRecordCodeSection
->CodeSegmentBase
>= MAX_ADDRESS
- ImageRecordCodeSection
->CodeSegmentSize
) {
929 if ((ImageRecordCodeSection
->CodeSegmentBase
+ ImageRecordCodeSection
->CodeSegmentSize
) > (ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
)) {
932 if (LastImageRecordCodeSection
!= NULL
) {
933 if ((LastImageRecordCodeSection
->CodeSegmentBase
+ LastImageRecordCodeSection
->CodeSegmentSize
) > ImageRecordCodeSection
->CodeSegmentBase
) {
938 LastImageRecordCodeSection
= ImageRecordCodeSection
;
939 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
946 Swap two image records.
948 @param FirstImageRecord first image record.
949 @param SecondImageRecord second image record.
954 IN IMAGE_PROPERTIES_RECORD
*FirstImageRecord
,
955 IN IMAGE_PROPERTIES_RECORD
*SecondImageRecord
958 IMAGE_PROPERTIES_RECORD TempImageRecord
;
960 TempImageRecord
.ImageBase
= FirstImageRecord
->ImageBase
;
961 TempImageRecord
.ImageSize
= FirstImageRecord
->ImageSize
;
962 TempImageRecord
.CodeSegmentCount
= FirstImageRecord
->CodeSegmentCount
;
964 FirstImageRecord
->ImageBase
= SecondImageRecord
->ImageBase
;
965 FirstImageRecord
->ImageSize
= SecondImageRecord
->ImageSize
;
966 FirstImageRecord
->CodeSegmentCount
= SecondImageRecord
->CodeSegmentCount
;
968 SecondImageRecord
->ImageBase
= TempImageRecord
.ImageBase
;
969 SecondImageRecord
->ImageSize
= TempImageRecord
.ImageSize
;
970 SecondImageRecord
->CodeSegmentCount
= TempImageRecord
.CodeSegmentCount
;
972 SwapListEntries (&FirstImageRecord
->CodeSegmentList
, &SecondImageRecord
->CodeSegmentList
);
976 Sort image record based upon the ImageBase from low to high.
984 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
985 IMAGE_PROPERTIES_RECORD
*NextImageRecord
;
986 LIST_ENTRY
*ImageRecordLink
;
987 LIST_ENTRY
*NextImageRecordLink
;
988 LIST_ENTRY
*ImageRecordEndLink
;
989 LIST_ENTRY
*ImageRecordList
;
991 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
993 ImageRecordLink
= ImageRecordList
->ForwardLink
;
994 NextImageRecordLink
= ImageRecordLink
->ForwardLink
;
995 ImageRecordEndLink
= ImageRecordList
;
996 while (ImageRecordLink
!= ImageRecordEndLink
) {
999 IMAGE_PROPERTIES_RECORD
,
1001 IMAGE_PROPERTIES_RECORD_SIGNATURE
1003 while (NextImageRecordLink
!= ImageRecordEndLink
) {
1004 NextImageRecord
= CR (
1005 NextImageRecordLink
,
1006 IMAGE_PROPERTIES_RECORD
,
1008 IMAGE_PROPERTIES_RECORD_SIGNATURE
1010 if (ImageRecord
->ImageBase
> NextImageRecord
->ImageBase
) {
1011 SwapImageRecord (ImageRecord
, NextImageRecord
);
1013 NextImageRecordLink
= NextImageRecordLink
->ForwardLink
;
1016 ImageRecordLink
= ImageRecordLink
->ForwardLink
;
1017 NextImageRecordLink
= ImageRecordLink
->ForwardLink
;
1030 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1031 LIST_ENTRY
*ImageRecordLink
;
1032 LIST_ENTRY
*ImageRecordList
;
1035 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
1037 for (ImageRecordLink
= ImageRecordList
->ForwardLink
, Index
= 0;
1038 ImageRecordLink
!= ImageRecordList
;
1039 ImageRecordLink
= ImageRecordLink
->ForwardLink
, Index
++) {
1042 IMAGE_PROPERTIES_RECORD
,
1044 IMAGE_PROPERTIES_RECORD_SIGNATURE
1046 DEBUG ((EFI_D_VERBOSE
, " Image[%d]: 0x%016lx - 0x%016lx\n", Index
, ImageRecord
->ImageBase
, ImageRecord
->ImageSize
));
1051 Insert image record.
1053 @param RuntimeImage Runtime image information
1057 IN EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
1061 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1062 UINT32 PeCoffHeaderOffset
;
1063 UINT32 SectionAlignment
;
1064 EFI_IMAGE_SECTION_HEADER
*Section
;
1065 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1068 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1070 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
1072 DEBUG ((EFI_D_VERBOSE
, "InsertImageRecord - 0x%x\n", RuntimeImage
));
1073 DEBUG ((EFI_D_VERBOSE
, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
));
1075 if (mPropertiesTableEndOfDxe
) {
1076 DEBUG ((DEBUG_INFO
, "Do not insert runtime image record after EndOfDxe\n"));
1080 ImageRecord
= AllocatePool (sizeof(*ImageRecord
));
1081 if (ImageRecord
== NULL
) {
1084 ImageRecord
->Signature
= IMAGE_PROPERTIES_RECORD_SIGNATURE
;
1086 DEBUG ((EFI_D_VERBOSE
, "ImageRecordCount - 0x%x\n", mImagePropertiesPrivateData
.ImageRecordCount
));
1089 // Step 1: record whole region
1091 ImageRecord
->ImageBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
;
1092 ImageRecord
->ImageSize
= RuntimeImage
->ImageSize
;
1094 ImageAddress
= RuntimeImage
->ImageBase
;
1096 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1097 if (PdbPointer
!= NULL
) {
1098 DEBUG ((EFI_D_VERBOSE
, " Image - %a\n", PdbPointer
));
1102 // Check PE/COFF image
1104 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
1105 PeCoffHeaderOffset
= 0;
1106 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1107 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
1110 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
1111 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1112 DEBUG ((EFI_D_VERBOSE
, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr
.Pe32
->Signature
));
1113 // It might be image in SMM.
1118 // Get SectionAlignment
1120 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1121 SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
1123 SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
1126 SetPropertiesTableSectionAlignment (SectionAlignment
);
1127 if ((SectionAlignment
& (RUNTIME_PAGE_ALLOCATION_GRANULARITY
- 1)) != 0) {
1128 DEBUG ((EFI_D_WARN
, "!!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %dK !!!!!!!!\n",
1129 SectionAlignment
, RUNTIME_PAGE_ALLOCATION_GRANULARITY
>> 10));
1130 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1131 if (PdbPointer
!= NULL
) {
1132 DEBUG ((EFI_D_WARN
, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer
));
1137 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1138 (UINT8
*) (UINTN
) ImageAddress
+
1139 PeCoffHeaderOffset
+
1141 sizeof(EFI_IMAGE_FILE_HEADER
) +
1142 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
1144 ImageRecord
->CodeSegmentCount
= 0;
1145 InitializeListHead (&ImageRecord
->CodeSegmentList
);
1146 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1147 Name
= Section
[Index
].Name
;
1150 " Section - '%c%c%c%c%c%c%c%c'\n",
1161 if ((Section
[Index
].Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) != 0) {
1162 DEBUG ((EFI_D_VERBOSE
, " VirtualSize - 0x%08x\n", Section
[Index
].Misc
.VirtualSize
));
1163 DEBUG ((EFI_D_VERBOSE
, " VirtualAddress - 0x%08x\n", Section
[Index
].VirtualAddress
));
1164 DEBUG ((EFI_D_VERBOSE
, " SizeOfRawData - 0x%08x\n", Section
[Index
].SizeOfRawData
));
1165 DEBUG ((EFI_D_VERBOSE
, " PointerToRawData - 0x%08x\n", Section
[Index
].PointerToRawData
));
1166 DEBUG ((EFI_D_VERBOSE
, " PointerToRelocations - 0x%08x\n", Section
[Index
].PointerToRelocations
));
1167 DEBUG ((EFI_D_VERBOSE
, " PointerToLinenumbers - 0x%08x\n", Section
[Index
].PointerToLinenumbers
));
1168 DEBUG ((EFI_D_VERBOSE
, " NumberOfRelocations - 0x%08x\n", Section
[Index
].NumberOfRelocations
));
1169 DEBUG ((EFI_D_VERBOSE
, " NumberOfLinenumbers - 0x%08x\n", Section
[Index
].NumberOfLinenumbers
));
1170 DEBUG ((EFI_D_VERBOSE
, " Characteristics - 0x%08x\n", Section
[Index
].Characteristics
));
1173 // Step 2: record code section
1175 ImageRecordCodeSection
= AllocatePool (sizeof(*ImageRecordCodeSection
));
1176 if (ImageRecordCodeSection
== NULL
) {
1179 ImageRecordCodeSection
->Signature
= IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
;
1181 ImageRecordCodeSection
->CodeSegmentBase
= (UINTN
)ImageAddress
+ Section
[Index
].VirtualAddress
;
1182 ImageRecordCodeSection
->CodeSegmentSize
= Section
[Index
].SizeOfRawData
;
1184 DEBUG ((EFI_D_VERBOSE
, "ImageCode: 0x%016lx - 0x%016lx\n", ImageRecordCodeSection
->CodeSegmentBase
, ImageRecordCodeSection
->CodeSegmentSize
));
1186 InsertTailList (&ImageRecord
->CodeSegmentList
, &ImageRecordCodeSection
->Link
);
1187 ImageRecord
->CodeSegmentCount
++;
1191 if (ImageRecord
->CodeSegmentCount
== 0) {
1192 SetPropertiesTableSectionAlignment (1);
1193 DEBUG ((EFI_D_ERROR
, "!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
1194 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1195 if (PdbPointer
!= NULL
) {
1196 DEBUG ((EFI_D_ERROR
, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer
));
1204 SortImageRecordCodeSection (ImageRecord
);
1206 // Check overlap all section in ImageBase/Size
1208 if (!IsImageRecordCodeSectionValid (ImageRecord
)) {
1209 DEBUG ((EFI_D_ERROR
, "IsImageRecordCodeSectionValid - FAIL\n"));
1213 InsertTailList (&mImagePropertiesPrivateData
.ImageRecordList
, &ImageRecord
->Link
);
1214 mImagePropertiesPrivateData
.ImageRecordCount
++;
1216 if (mImagePropertiesPrivateData
.CodeSegmentCountMax
< ImageRecord
->CodeSegmentCount
) {
1217 mImagePropertiesPrivateData
.CodeSegmentCountMax
= ImageRecord
->CodeSegmentCount
;
1227 Find image record according to image base and size.
1229 @param ImageBase Base of PE image
1230 @param ImageSize Size of PE image
1232 @return image record
1235 IMAGE_PROPERTIES_RECORD
*
1237 IN EFI_PHYSICAL_ADDRESS ImageBase
,
1241 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1242 LIST_ENTRY
*ImageRecordLink
;
1243 LIST_ENTRY
*ImageRecordList
;
1245 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
1247 for (ImageRecordLink
= ImageRecordList
->ForwardLink
;
1248 ImageRecordLink
!= ImageRecordList
;
1249 ImageRecordLink
= ImageRecordLink
->ForwardLink
) {
1252 IMAGE_PROPERTIES_RECORD
,
1254 IMAGE_PROPERTIES_RECORD_SIGNATURE
1257 if ((ImageBase
== ImageRecord
->ImageBase
) &&
1258 (ImageSize
== ImageRecord
->ImageSize
)) {
1267 Remove Image record.
1269 @param RuntimeImage Runtime image information
1273 IN EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
1276 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1277 LIST_ENTRY
*CodeSegmentListHead
;
1278 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
1280 DEBUG ((EFI_D_VERBOSE
, "RemoveImageRecord - 0x%x\n", RuntimeImage
));
1281 DEBUG ((EFI_D_VERBOSE
, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
));
1283 if (mPropertiesTableEndOfDxe
) {
1284 DEBUG ((DEBUG_INFO
, "Do not remove runtime image record after EndOfDxe\n"));
1288 ImageRecord
= FindImageRecord ((EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
);
1289 if (ImageRecord
== NULL
) {
1290 DEBUG ((EFI_D_ERROR
, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
1294 CodeSegmentListHead
= &ImageRecord
->CodeSegmentList
;
1295 while (!IsListEmpty (CodeSegmentListHead
)) {
1296 ImageRecordCodeSection
= CR (
1297 CodeSegmentListHead
->ForwardLink
,
1298 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
1300 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
1302 RemoveEntryList (&ImageRecordCodeSection
->Link
);
1303 FreePool (ImageRecordCodeSection
);
1306 RemoveEntryList (&ImageRecord
->Link
);
1307 FreePool (ImageRecord
);
1308 mImagePropertiesPrivateData
.ImageRecordCount
--;
1313 Install PropertiesTable.
1315 @param[in] Event The Event this notify function registered to.
1316 @param[in] Context Pointer to the context data registered to the Event.
1320 InstallPropertiesTable (
1325 mPropertiesTableEndOfDxe
= TRUE
;
1326 if (PcdGetBool (PcdPropertiesTableEnable
)) {
1329 Status
= gBS
->InstallConfigurationTable (&gEfiPropertiesTableGuid
, &mPropertiesTable
);
1330 ASSERT_EFI_ERROR (Status
);
1332 DEBUG ((EFI_D_INFO
, "MemoryProtectionAttribute - 0x%016lx\n", mPropertiesTable
.MemoryProtectionAttribute
));
1333 if ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) == 0) {
1334 DEBUG ((EFI_D_ERROR
, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));
1335 DEBUG ((EFI_D_ERROR
, "because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY
>> 10));
1339 gBS
->GetMemoryMap
= CoreGetMemoryMapWithSeparatedImageSection
;
1341 gBS
->CalculateCrc32 ((UINT8
*)gBS
, gBS
->Hdr
.HeaderSize
, &gBS
->Hdr
.CRC32
);
1343 DEBUG ((EFI_D_VERBOSE
, "Total Image Count - 0x%x\n", mImagePropertiesPrivateData
.ImageRecordCount
));
1344 DEBUG ((EFI_D_VERBOSE
, "Dump ImageRecord:\n"));
1347 mPropertiesTableEnable
= TRUE
;
1352 Initialize PropertiesTable support.
1356 CoreInitializePropertiesTable (
1361 EFI_EVENT EndOfDxeEvent
;
1363 Status
= gBS
->CreateEventEx (
1366 InstallPropertiesTable
,
1368 &gEfiEndOfDxeEventGroupGuid
,
1371 ASSERT_EFI_ERROR (Status
);