2 UEFI PropertiesTable support
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/PcdLib.h>
25 #include <Guid/EventGroup.h>
26 #include <Protocol/DxeSmmReadyToLock.h>
28 #include <Library/PeCoffLib.h>
29 #include <Library/PeCoffGetEntryPointLib.h>
30 #include <Protocol/Runtime.h>
32 #include <Guid/PropertiesTable.h>
36 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
37 ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
39 #define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I','P','R','C')
44 EFI_PHYSICAL_ADDRESS CodeSegmentBase
;
45 UINT64 CodeSegmentSize
;
46 } IMAGE_PROPERTIES_RECORD_CODE_SECTION
;
48 #define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D')
53 EFI_PHYSICAL_ADDRESS ImageBase
;
55 UINTN CodeSegmentCount
;
56 LIST_ENTRY CodeSegmentList
;
57 } IMAGE_PROPERTIES_RECORD
;
59 #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
63 UINTN ImageRecordCount
;
64 UINTN CodeSegmentCountMax
;
65 LIST_ENTRY ImageRecordList
;
66 } IMAGE_PROPERTIES_PRIVATE_DATA
;
68 IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData
= {
69 IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE
,
72 INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData
.ImageRecordList
)
75 EFI_PROPERTIES_TABLE mPropertiesTable
= {
76 EFI_PROPERTIES_TABLE_VERSION
,
77 sizeof(EFI_PROPERTIES_TABLE
),
78 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
81 EFI_LOCK mPropertiesTableLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
84 // Below functions are for MemoryMap
88 Converts a number of EFI_PAGEs to a size in bytes.
90 NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.
92 @param Pages The number of EFI_PAGES.
94 @return The number of bytes associated with the number of EFI_PAGEs specified
102 return LShiftU64 (Pages
, EFI_PAGE_SHIFT
);
106 Converts a size, in bytes, to a number of EFI_PAGESs.
108 NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only.
110 @param Size A size in bytes.
112 @return The number of EFI_PAGESs associated with the number of bytes specified
121 return RShiftU64 (Size
, EFI_PAGE_SHIFT
) + ((((UINTN
)Size
) & EFI_PAGE_MASK
) ? 1 : 0);
125 Acquire memory lock on mPropertiesTableLock.
128 CoreAcquirePropertiesTableLock (
132 CoreAcquireLock (&mPropertiesTableLock
);
136 Release memory lock on mPropertiesTableLock.
139 CoreReleasePropertiesTableLock (
143 CoreReleaseLock (&mPropertiesTableLock
);
149 @param MemoryMap A pointer to the buffer in which firmware places
150 the current memory map.
151 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
152 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
156 IN EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
157 IN UINTN MemoryMapSize
,
158 IN UINTN DescriptorSize
161 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
162 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
163 UINT64 MemoryBlockLength
;
165 DEBUG ((EFI_D_VERBOSE
, " MemoryMap:\n"));
166 MemoryMapEntry
= MemoryMap
;
167 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
168 while (MemoryMapEntry
< MemoryMapEnd
) {
169 MemoryBlockLength
= (UINT64
) (EfiPagesToSize (MemoryMapEntry
->NumberOfPages
));
170 DEBUG ((EFI_D_VERBOSE
, " Entry(0x%02x) 0x%016lx - 0x%016lx (0x%016lx)\n", MemoryMapEntry
->Type
, MemoryMapEntry
->PhysicalStart
, MemoryMapEntry
->PhysicalStart
+ MemoryBlockLength
, MemoryMapEntry
->Attribute
));
171 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
176 Sort memory map entries based upon PhysicalStart, from low to high.
178 @param MemoryMap A pointer to the buffer in which firmware places
179 the current memory map.
180 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
181 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
185 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
186 IN UINTN MemoryMapSize
,
187 IN UINTN DescriptorSize
190 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
191 EFI_MEMORY_DESCRIPTOR
*NextMemoryMapEntry
;
192 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
193 EFI_MEMORY_DESCRIPTOR TempMemoryMap
;
195 MemoryMapEntry
= MemoryMap
;
196 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
197 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
198 while (MemoryMapEntry
< MemoryMapEnd
) {
199 while (NextMemoryMapEntry
< MemoryMapEnd
) {
200 if (MemoryMapEntry
->PhysicalStart
> NextMemoryMapEntry
->PhysicalStart
) {
201 CopyMem (&TempMemoryMap
, MemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
202 CopyMem (MemoryMapEntry
, NextMemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
203 CopyMem (NextMemoryMapEntry
, &TempMemoryMap
, sizeof(EFI_MEMORY_DESCRIPTOR
));
206 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry
, DescriptorSize
);
209 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
210 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
217 Merge continous memory map entries whose have same attributes.
219 @param MemoryMap A pointer to the buffer in which firmware places
220 the current memory map.
221 @param MemoryMapSize A pointer to the size, in bytes, of the
222 MemoryMap buffer. On input, this is the size of
223 the current memory map. On output,
224 it is the size of new memory map after merge.
225 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
229 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
230 IN OUT UINTN
*MemoryMapSize
,
231 IN UINTN DescriptorSize
234 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
235 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
236 UINT64 MemoryBlockLength
;
237 EFI_MEMORY_DESCRIPTOR
*NewMemoryMapEntry
;
238 EFI_MEMORY_DESCRIPTOR
*NextMemoryMapEntry
;
240 MemoryMapEntry
= MemoryMap
;
241 NewMemoryMapEntry
= MemoryMap
;
242 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ *MemoryMapSize
);
243 while ((UINTN
)MemoryMapEntry
< (UINTN
)MemoryMapEnd
) {
244 CopyMem (NewMemoryMapEntry
, MemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
245 NextMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
247 MemoryBlockLength
= (UINT64
) (EfiPagesToSize (MemoryMapEntry
->NumberOfPages
));
248 if (((UINTN
)NextMemoryMapEntry
< (UINTN
)MemoryMapEnd
) &&
249 (MemoryMapEntry
->Type
== NextMemoryMapEntry
->Type
) &&
250 (MemoryMapEntry
->Attribute
== NextMemoryMapEntry
->Attribute
) &&
251 ((MemoryMapEntry
->PhysicalStart
+ MemoryBlockLength
) == NextMemoryMapEntry
->PhysicalStart
)) {
252 NewMemoryMapEntry
->NumberOfPages
+= NextMemoryMapEntry
->NumberOfPages
;
253 MemoryMapEntry
= NextMemoryMapEntry
;
256 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
257 NewMemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry
, DescriptorSize
);
260 *MemoryMapSize
= (UINTN
)NewMemoryMapEntry
- (UINTN
)MemoryMap
;
266 Enforce memory map attributes.
267 This function will set EfiRuntimeServicesData/EfiMemoryMappedIO/EfiMemoryMappedIOPortSpace to be EFI_MEMORY_XP.
269 @param MemoryMap A pointer to the buffer in which firmware places
270 the current memory map.
271 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
272 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
275 EnforceMemoryMapAttribute (
276 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
277 IN UINTN MemoryMapSize
,
278 IN UINTN DescriptorSize
281 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
282 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
284 MemoryMapEntry
= MemoryMap
;
285 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
286 while ((UINTN
)MemoryMapEntry
< (UINTN
)MemoryMapEnd
) {
287 switch (MemoryMapEntry
->Type
) {
288 case EfiRuntimeServicesCode
:
291 case EfiRuntimeServicesData
:
292 case EfiMemoryMappedIO
:
293 case EfiMemoryMappedIOPortSpace
:
294 MemoryMapEntry
->Attribute
|= EFI_MEMORY_XP
;
296 case EfiReservedMemoryType
:
297 case EfiACPIMemoryNVS
:
301 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
308 Sort memory map entries whose type is EfiRuntimeServicesCode/EfiRuntimeServicesData,
310 This function assumes memory map is already from low to high, so it just reverts them.
312 @param MemoryMap A pointer to the buffer in which firmware places
313 the current memory map.
314 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
315 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
318 RevertRuntimeMemoryMap (
319 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
320 IN UINTN MemoryMapSize
,
321 IN UINTN DescriptorSize
324 EFI_MEMORY_DESCRIPTOR
*MemoryMapEntry
;
325 EFI_MEMORY_DESCRIPTOR
*MemoryMapEnd
;
326 EFI_MEMORY_DESCRIPTOR TempMemoryMap
;
328 EFI_MEMORY_DESCRIPTOR
*RuntimeMapEntryBegin
;
329 EFI_MEMORY_DESCRIPTOR
*RuntimeMapEntryEnd
;
331 MemoryMapEntry
= MemoryMap
;
332 RuntimeMapEntryBegin
= NULL
;
333 RuntimeMapEntryEnd
= NULL
;
334 MemoryMapEnd
= (EFI_MEMORY_DESCRIPTOR
*) ((UINT8
*) MemoryMap
+ MemoryMapSize
);
335 while ((UINTN
)MemoryMapEntry
< (UINTN
)MemoryMapEnd
) {
336 if ((MemoryMapEntry
->Type
== EfiRuntimeServicesCode
) ||
337 (MemoryMapEntry
->Type
== EfiRuntimeServicesData
)) {
338 if (RuntimeMapEntryBegin
== NULL
) {
339 RuntimeMapEntryBegin
= MemoryMapEntry
;
341 RuntimeMapEntryEnd
= MemoryMapEntry
;
343 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
346 MemoryMapEntry
= RuntimeMapEntryBegin
;
347 MemoryMapEnd
= RuntimeMapEntryEnd
;
348 while (MemoryMapEntry
< MemoryMapEnd
) {
349 CopyMem (&TempMemoryMap
, MemoryMapEntry
, sizeof(EFI_MEMORY_DESCRIPTOR
));
350 CopyMem (MemoryMapEntry
, MemoryMapEnd
, sizeof(EFI_MEMORY_DESCRIPTOR
));
351 CopyMem (MemoryMapEnd
, &TempMemoryMap
, sizeof(EFI_MEMORY_DESCRIPTOR
));
353 MemoryMapEntry
= NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry
, DescriptorSize
);
354 MemoryMapEnd
= PREVIOUS_MEMORY_DESCRIPTOR (MemoryMapEnd
, DescriptorSize
);
361 Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].
363 @param Buffer Start Address
364 @param Length Address length
366 @return first image record covered by [buffer, length]
368 IMAGE_PROPERTIES_RECORD
*
369 GetImageRecordByAddress (
370 IN EFI_PHYSICAL_ADDRESS Buffer
,
374 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
375 LIST_ENTRY
*ImageRecordLink
;
376 LIST_ENTRY
*ImageRecordList
;
378 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
380 for (ImageRecordLink
= ImageRecordList
->ForwardLink
;
381 ImageRecordLink
!= ImageRecordList
;
382 ImageRecordLink
= ImageRecordLink
->ForwardLink
) {
385 IMAGE_PROPERTIES_RECORD
,
387 IMAGE_PROPERTIES_RECORD_SIGNATURE
390 if ((Buffer
<= ImageRecord
->ImageBase
) &&
391 (Buffer
+ Length
>= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
)) {
400 Set the memory map to new entries, according to one old entry,
401 based upon PE code section and data section in image record
403 @param ImageRecord An image record whose [ImageBase, ImageSize] covered
404 by old memory map entry.
405 @param NewRecord A pointer to several new memory map entries.
406 The caller gurantee the buffer size be 1 +
407 (SplitRecordCount * DescriptorSize) calculated
409 @param OldRecord A pointer to one old memory map entry.
410 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
414 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
,
415 IN OUT EFI_MEMORY_DESCRIPTOR
*NewRecord
,
416 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
,
417 IN UINTN DescriptorSize
420 EFI_MEMORY_DESCRIPTOR TempRecord
;
421 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
422 LIST_ENTRY
*ImageRecordCodeSectionLink
;
423 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
424 LIST_ENTRY
*ImageRecordCodeSectionList
;
425 UINTN NewRecordCount
;
429 CopyMem (&TempRecord
, OldRecord
, sizeof(EFI_MEMORY_DESCRIPTOR
));
430 PhysicalEnd
= TempRecord
.PhysicalStart
+ EfiPagesToSize(TempRecord
.NumberOfPages
);
433 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
435 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
436 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
437 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
438 ImageRecordCodeSection
= CR (
439 ImageRecordCodeSectionLink
,
440 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
442 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
444 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
446 if (TempRecord
.PhysicalStart
<= ImageRecordCodeSection
->CodeSegmentBase
) {
450 NewRecord
->Type
= EfiRuntimeServicesData
;
451 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
452 NewRecord
->VirtualStart
= 0;
453 NewRecord
->NumberOfPages
= EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentBase
- NewRecord
->PhysicalStart
);
454 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
455 if (NewRecord
->NumberOfPages
!= 0) {
456 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
463 NewRecord
->Type
= EfiRuntimeServicesCode
;
464 NewRecord
->PhysicalStart
= ImageRecordCodeSection
->CodeSegmentBase
;
465 NewRecord
->VirtualStart
= 0;
466 NewRecord
->NumberOfPages
= EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentSize
);
467 NewRecord
->Attribute
= (TempRecord
.Attribute
& (~EFI_MEMORY_XP
)) | EFI_MEMORY_RO
;
468 if (NewRecord
->NumberOfPages
!= 0) {
469 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
473 TempRecord
.PhysicalStart
= ImageRecordCodeSection
->CodeSegmentBase
+ EfiPagesToSize (EfiSizeToPages(ImageRecordCodeSection
->CodeSegmentSize
));
474 TempRecord
.NumberOfPages
= EfiSizeToPages(PhysicalEnd
- TempRecord
.PhysicalStart
);
475 if (TempRecord
.NumberOfPages
== 0) {
481 ImageEnd
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
486 if (TempRecord
.PhysicalStart
< ImageEnd
) {
487 NewRecord
->Type
= EfiRuntimeServicesData
;
488 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
489 NewRecord
->VirtualStart
= 0;
490 NewRecord
->NumberOfPages
= EfiSizeToPages (ImageEnd
- TempRecord
.PhysicalStart
);
491 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
495 return NewRecordCount
;
499 Return the max number of new splitted entries, according to one old entry,
500 based upon PE code section and data section.
502 @param OldRecord A pointer to one old memory map entry.
504 @retval 0 no entry need to be splitted.
505 @return the max number of new splitted entries
508 GetMaxSplitRecordCount (
509 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
512 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
513 UINTN SplitRecordCount
;
514 UINT64 PhysicalStart
;
517 SplitRecordCount
= 0;
518 PhysicalStart
= OldRecord
->PhysicalStart
;
519 PhysicalEnd
= OldRecord
->PhysicalStart
+ EfiPagesToSize(OldRecord
->NumberOfPages
);
522 ImageRecord
= GetImageRecordByAddress (PhysicalStart
, PhysicalEnd
- PhysicalStart
);
523 if (ImageRecord
== NULL
) {
526 SplitRecordCount
+= (2 * ImageRecord
->CodeSegmentCount
+ 1);
527 PhysicalStart
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
528 } while ((ImageRecord
!= NULL
) && (PhysicalStart
< PhysicalEnd
));
530 if (SplitRecordCount
!= 0) {
534 return SplitRecordCount
;
538 Split the memory map to new entries, according to one old entry,
539 based upon PE code section and data section.
541 @param OldRecord A pointer to one old memory map entry.
542 @param NewRecord A pointer to several new memory map entries.
543 The caller gurantee the buffer size be 1 +
544 (SplitRecordCount * DescriptorSize) calculated
546 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
548 @retval 0 no entry is splitted.
549 @return the real number of splitted record.
553 IN EFI_MEMORY_DESCRIPTOR
*OldRecord
,
554 IN OUT EFI_MEMORY_DESCRIPTOR
*NewRecord
,
555 IN UINTN MaxSplitRecordCount
,
556 IN UINTN DescriptorSize
559 EFI_MEMORY_DESCRIPTOR TempRecord
;
560 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
561 IMAGE_PROPERTIES_RECORD
*NewImageRecord
;
562 UINT64 PhysicalStart
;
564 UINTN NewRecordCount
;
565 UINTN TotalNewRecordCount
;
567 if (MaxSplitRecordCount
== 0) {
568 CopyMem (NewRecord
, OldRecord
, DescriptorSize
);
572 TotalNewRecordCount
= 0;
575 // Override previous record
577 CopyMem (&TempRecord
, OldRecord
, sizeof(EFI_MEMORY_DESCRIPTOR
));
578 PhysicalStart
= TempRecord
.PhysicalStart
;
579 PhysicalEnd
= TempRecord
.PhysicalStart
+ EfiPagesToSize(TempRecord
.NumberOfPages
);
583 NewImageRecord
= GetImageRecordByAddress (PhysicalStart
, PhysicalEnd
- PhysicalStart
);
584 if (NewImageRecord
== NULL
) {
586 // No more image covered by this range, stop
588 if ((PhysicalEnd
> PhysicalStart
) && (ImageRecord
!= NULL
)) {
590 // If this is still address in this record, need record.
592 NewRecord
= PREVIOUS_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
593 if (NewRecord
->Type
== EfiRuntimeServicesData
) {
595 // Last record is DATA, just merge it.
597 NewRecord
->NumberOfPages
= EfiSizeToPages(PhysicalEnd
- NewRecord
->PhysicalStart
);
600 // Last record is CODE, create a new DATA entry.
602 NewRecord
= NEXT_MEMORY_DESCRIPTOR (NewRecord
, DescriptorSize
);
603 NewRecord
->Type
= EfiRuntimeServicesData
;
604 NewRecord
->PhysicalStart
= TempRecord
.PhysicalStart
;
605 NewRecord
->VirtualStart
= 0;
606 NewRecord
->NumberOfPages
= TempRecord
.NumberOfPages
;
607 NewRecord
->Attribute
= TempRecord
.Attribute
| EFI_MEMORY_XP
;
608 TotalNewRecordCount
++;
613 ImageRecord
= NewImageRecord
;
618 NewRecordCount
= SetNewRecord (ImageRecord
, NewRecord
, &TempRecord
, DescriptorSize
);
619 TotalNewRecordCount
+= NewRecordCount
;
620 NewRecord
= (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)NewRecord
+ NewRecordCount
* DescriptorSize
);
623 // Update PhysicalStart, in order to exclude the image buffer already splitted.
625 PhysicalStart
= ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
;
626 TempRecord
.PhysicalStart
= PhysicalStart
;
627 TempRecord
.NumberOfPages
= EfiSizeToPages (PhysicalEnd
- PhysicalStart
);
628 } while ((ImageRecord
!= NULL
) && (PhysicalStart
< PhysicalEnd
));
630 return TotalNewRecordCount
- 1;
634 Split the original memory map, and add more entries to describe PE code section and data section.
635 This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP.
636 This function will merge entries with same attributes finally.
638 NOTE: It assumes PE code/data section are page aligned.
639 NOTE: It assumes enough entry is prepared for new memory map.
652 +---------------+ ----
655 | Record RtCode | |-> PE/COFF1
658 +---------------+ ----
661 | Record RtCode | |-> PE/COFF2
664 +---------------+ ----
668 @param MemoryMapSize A pointer to the size, in bytes, of the
669 MemoryMap buffer. On input, this is the size of
670 old MemoryMap before split. The actual buffer
671 size of MemoryMap is MemoryMapSize +
672 (AdditionalRecordCount * DescriptorSize) calculated
673 below. On output, it is the size of new MemoryMap
675 @param MemoryMap A pointer to the buffer in which firmware places
676 the current memory map.
677 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
681 IN OUT UINTN
*MemoryMapSize
,
682 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
683 IN UINTN DescriptorSize
688 UINTN MaxSplitRecordCount
;
689 UINTN RealSplitRecordCount
;
690 UINTN TotalSplitRecordCount
;
691 UINTN AdditionalRecordCount
;
693 AdditionalRecordCount
= (2 * mImagePropertiesPrivateData
.CodeSegmentCountMax
+ 1) * mImagePropertiesPrivateData
.ImageRecordCount
;
695 TotalSplitRecordCount
= 0;
697 // Let old record point to end of valid MemoryMap buffer.
699 IndexOld
= ((*MemoryMapSize
) / DescriptorSize
) - 1;
701 // Let new record point to end of full MemoryMap buffer.
703 IndexNew
= ((*MemoryMapSize
) / DescriptorSize
) - 1 + AdditionalRecordCount
;
704 for (; IndexOld
>= 0; IndexOld
--) {
705 MaxSplitRecordCount
= GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexOld
* DescriptorSize
));
707 // Split this MemoryMap record
709 IndexNew
-= MaxSplitRecordCount
;
710 RealSplitRecordCount
= SplitRecord (
711 (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexOld
* DescriptorSize
),
712 (EFI_MEMORY_DESCRIPTOR
*)((UINT8
*)MemoryMap
+ IndexNew
* DescriptorSize
),
717 // Adjust IndexNew according to real split.
720 ((UINT8
*)MemoryMap
+ (IndexNew
+ MaxSplitRecordCount
- RealSplitRecordCount
) * DescriptorSize
),
721 ((UINT8
*)MemoryMap
+ IndexNew
* DescriptorSize
),
722 RealSplitRecordCount
* DescriptorSize
724 IndexNew
= IndexNew
+ MaxSplitRecordCount
- RealSplitRecordCount
;
725 TotalSplitRecordCount
+= RealSplitRecordCount
;
729 // Move all records to the beginning.
733 (UINT8
*)MemoryMap
+ (AdditionalRecordCount
- TotalSplitRecordCount
) * DescriptorSize
,
734 (*MemoryMapSize
) + TotalSplitRecordCount
* DescriptorSize
737 *MemoryMapSize
= (*MemoryMapSize
) + DescriptorSize
* TotalSplitRecordCount
;
740 // Sort from low to high (Just in case)
742 SortMemoryMap (MemoryMap
, *MemoryMapSize
, DescriptorSize
);
745 // Set RuntimeData to XP
747 EnforceMemoryMapAttribute (MemoryMap
, *MemoryMapSize
, DescriptorSize
);
750 // Merge same type to save entry size
752 MergeMemoryMap (MemoryMap
, MemoryMapSize
, DescriptorSize
);
758 This function for GetMemoryMap() with properties table.
760 It calls original GetMemoryMap() to get the original memory map information. Then
761 plus the additional memory map entries for PE Code/Data seperation.
763 @param MemoryMapSize A pointer to the size, in bytes, of the
764 MemoryMap buffer. On input, this is the size of
765 the buffer allocated by the caller. On output,
766 it is the size of the buffer returned by the
767 firmware if the buffer was large enough, or the
768 size of the buffer needed to contain the map if
769 the buffer was too small.
770 @param MemoryMap A pointer to the buffer in which firmware places
771 the current memory map.
772 @param MapKey A pointer to the location in which firmware
773 returns the key for the current memory map.
774 @param DescriptorSize A pointer to the location in which firmware
775 returns the size, in bytes, of an individual
776 EFI_MEMORY_DESCRIPTOR.
777 @param DescriptorVersion A pointer to the location in which firmware
778 returns the version number associated with the
779 EFI_MEMORY_DESCRIPTOR.
781 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
783 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
784 buffer size needed to hold the memory map is
785 returned in MemoryMapSize.
786 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
791 CoreGetMemoryMapPropertiesTable (
792 IN OUT UINTN
*MemoryMapSize
,
793 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
795 OUT UINTN
*DescriptorSize
,
796 OUT UINT32
*DescriptorVersion
800 UINTN OldMemoryMapSize
;
801 UINTN AdditionalRecordCount
;
804 // If PE code/data is not aligned, just return.
806 if ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) == 0) {
807 return CoreGetMemoryMap (MemoryMapSize
, MemoryMap
, MapKey
, DescriptorSize
, DescriptorVersion
);
810 if (MemoryMapSize
== NULL
) {
811 return EFI_INVALID_PARAMETER
;
814 CoreAcquirePropertiesTableLock ();
816 AdditionalRecordCount
= (2 * mImagePropertiesPrivateData
.CodeSegmentCountMax
+ 1) * mImagePropertiesPrivateData
.ImageRecordCount
;
818 OldMemoryMapSize
= *MemoryMapSize
;
819 Status
= CoreGetMemoryMap (MemoryMapSize
, MemoryMap
, MapKey
, DescriptorSize
, DescriptorVersion
);
820 if (Status
== EFI_BUFFER_TOO_SMALL
) {
821 *MemoryMapSize
= *MemoryMapSize
+ (*DescriptorSize
) * AdditionalRecordCount
;
822 } else if (Status
== EFI_SUCCESS
) {
823 if (OldMemoryMapSize
- *MemoryMapSize
< (*DescriptorSize
) * AdditionalRecordCount
) {
824 *MemoryMapSize
= *MemoryMapSize
+ (*DescriptorSize
) * AdditionalRecordCount
;
826 // Need update status to buffer too small
828 Status
= EFI_BUFFER_TOO_SMALL
;
831 // Split PE code/data
833 SplitTable (MemoryMapSize
, MemoryMap
, *DescriptorSize
);
837 CoreReleasePropertiesTableLock ();
842 // Below functions are for ImageRecord
846 Set PropertiesTable accroding to PE/COFF image section alignment.
848 @param SectionAlignment PE/COFF section alignment
851 SetPropertiesTableSectionAlignment (
852 IN UINT32 SectionAlignment
855 if (((SectionAlignment
& (SIZE_4KB
- 1)) != 0) &&
856 ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) != 0)) {
857 DEBUG ((EFI_D_VERBOSE
, "SetPropertiesTableSectionAlignment - Clear\n"));
858 mPropertiesTable
.MemoryProtectionAttribute
&= ~EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
;
859 gBS
->GetMemoryMap
= CoreGetMemoryMap
;
861 gBS
->CalculateCrc32 ((UINT8
*)gBS
, gBS
->Hdr
.HeaderSize
, &gBS
->Hdr
.CRC32
);
866 Swap two code sections in image record.
868 @param FirstImageRecordCodeSection first code section in image record
869 @param SecondImageRecordCodeSection second code section in image record
872 SwapImageRecordCodeSection (
873 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION
*FirstImageRecordCodeSection
,
874 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION
*SecondImageRecordCodeSection
877 IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection
;
879 TempImageRecordCodeSection
.CodeSegmentBase
= FirstImageRecordCodeSection
->CodeSegmentBase
;
880 TempImageRecordCodeSection
.CodeSegmentSize
= FirstImageRecordCodeSection
->CodeSegmentSize
;
882 FirstImageRecordCodeSection
->CodeSegmentBase
= SecondImageRecordCodeSection
->CodeSegmentBase
;
883 FirstImageRecordCodeSection
->CodeSegmentSize
= SecondImageRecordCodeSection
->CodeSegmentSize
;
885 SecondImageRecordCodeSection
->CodeSegmentBase
= TempImageRecordCodeSection
.CodeSegmentBase
;
886 SecondImageRecordCodeSection
->CodeSegmentSize
= TempImageRecordCodeSection
.CodeSegmentSize
;
890 Sort code section in image record, based upon CodeSegmentBase from low to high.
892 @param ImageRecord image record to be sorted
895 SortImageRecordCodeSection (
896 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
899 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
900 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*NextImageRecordCodeSection
;
901 LIST_ENTRY
*ImageRecordCodeSectionLink
;
902 LIST_ENTRY
*NextImageRecordCodeSectionLink
;
903 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
904 LIST_ENTRY
*ImageRecordCodeSectionList
;
906 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
908 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
909 NextImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
910 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
911 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
912 ImageRecordCodeSection
= CR (
913 ImageRecordCodeSectionLink
,
914 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
916 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
918 while (NextImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
919 NextImageRecordCodeSection
= CR (
920 NextImageRecordCodeSectionLink
,
921 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
923 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
925 if (ImageRecordCodeSection
->CodeSegmentBase
> NextImageRecordCodeSection
->CodeSegmentBase
) {
926 SwapImageRecordCodeSection (ImageRecordCodeSection
, NextImageRecordCodeSection
);
928 NextImageRecordCodeSectionLink
= NextImageRecordCodeSectionLink
->ForwardLink
;
931 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
932 NextImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
937 Check if code section in image record is valid
939 @param ImageRecord image record to be checked
941 @retval TRUE image record is valid
942 @retval FALSE image record is invalid
945 IsImageRecordCodeSectionValid (
946 IN IMAGE_PROPERTIES_RECORD
*ImageRecord
949 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
950 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*LastImageRecordCodeSection
;
951 LIST_ENTRY
*ImageRecordCodeSectionLink
;
952 LIST_ENTRY
*ImageRecordCodeSectionEndLink
;
953 LIST_ENTRY
*ImageRecordCodeSectionList
;
955 DEBUG ((EFI_D_VERBOSE
, "ImageCode SegmentCount - 0x%x\n", ImageRecord
->CodeSegmentCount
));
957 ImageRecordCodeSectionList
= &ImageRecord
->CodeSegmentList
;
959 ImageRecordCodeSectionLink
= ImageRecordCodeSectionList
->ForwardLink
;
960 ImageRecordCodeSectionEndLink
= ImageRecordCodeSectionList
;
961 LastImageRecordCodeSection
= NULL
;
962 while (ImageRecordCodeSectionLink
!= ImageRecordCodeSectionEndLink
) {
963 ImageRecordCodeSection
= CR (
964 ImageRecordCodeSectionLink
,
965 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
967 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
969 if (ImageRecordCodeSection
->CodeSegmentSize
== 0) {
972 if (ImageRecordCodeSection
->CodeSegmentBase
< ImageRecord
->ImageBase
) {
975 if (ImageRecordCodeSection
->CodeSegmentBase
>= MAX_ADDRESS
- ImageRecordCodeSection
->CodeSegmentSize
) {
978 if ((ImageRecordCodeSection
->CodeSegmentBase
+ ImageRecordCodeSection
->CodeSegmentSize
) > (ImageRecord
->ImageBase
+ ImageRecord
->ImageSize
)) {
981 if (LastImageRecordCodeSection
!= NULL
) {
982 if ((LastImageRecordCodeSection
->CodeSegmentBase
+ LastImageRecordCodeSection
->CodeSegmentSize
) > ImageRecordCodeSection
->CodeSegmentBase
) {
987 LastImageRecordCodeSection
= ImageRecordCodeSection
;
988 ImageRecordCodeSectionLink
= ImageRecordCodeSectionLink
->ForwardLink
;
995 Swap two image records.
997 @param FirstImageRecord first image record.
998 @param SecondImageRecord second image record.
1002 IN IMAGE_PROPERTIES_RECORD
*FirstImageRecord
,
1003 IN IMAGE_PROPERTIES_RECORD
*SecondImageRecord
1006 IMAGE_PROPERTIES_RECORD TempImageRecord
;
1008 TempImageRecord
.ImageBase
= FirstImageRecord
->ImageBase
;
1009 TempImageRecord
.ImageSize
= FirstImageRecord
->ImageSize
;
1010 TempImageRecord
.CodeSegmentCount
= FirstImageRecord
->CodeSegmentCount
;
1012 FirstImageRecord
->ImageBase
= SecondImageRecord
->ImageBase
;
1013 FirstImageRecord
->ImageSize
= SecondImageRecord
->ImageSize
;
1014 FirstImageRecord
->CodeSegmentCount
= SecondImageRecord
->CodeSegmentCount
;
1016 SecondImageRecord
->ImageBase
= TempImageRecord
.ImageBase
;
1017 SecondImageRecord
->ImageSize
= TempImageRecord
.ImageSize
;
1018 SecondImageRecord
->CodeSegmentCount
= TempImageRecord
.CodeSegmentCount
;
1020 SwapListEntries (&FirstImageRecord
->CodeSegmentList
, &SecondImageRecord
->CodeSegmentList
);
1024 Sort image record based upon the ImageBase from low to high.
1031 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1032 IMAGE_PROPERTIES_RECORD
*NextImageRecord
;
1033 LIST_ENTRY
*ImageRecordLink
;
1034 LIST_ENTRY
*NextImageRecordLink
;
1035 LIST_ENTRY
*ImageRecordEndLink
;
1036 LIST_ENTRY
*ImageRecordList
;
1038 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
1040 ImageRecordLink
= ImageRecordList
->ForwardLink
;
1041 NextImageRecordLink
= ImageRecordLink
->ForwardLink
;
1042 ImageRecordEndLink
= ImageRecordList
;
1043 while (ImageRecordLink
!= ImageRecordEndLink
) {
1046 IMAGE_PROPERTIES_RECORD
,
1048 IMAGE_PROPERTIES_RECORD_SIGNATURE
1050 while (NextImageRecordLink
!= ImageRecordEndLink
) {
1051 NextImageRecord
= CR (
1052 NextImageRecordLink
,
1053 IMAGE_PROPERTIES_RECORD
,
1055 IMAGE_PROPERTIES_RECORD_SIGNATURE
1057 if (ImageRecord
->ImageBase
> NextImageRecord
->ImageBase
) {
1058 SwapImageRecord (ImageRecord
, NextImageRecord
);
1060 NextImageRecordLink
= NextImageRecordLink
->ForwardLink
;
1063 ImageRecordLink
= ImageRecordLink
->ForwardLink
;
1064 NextImageRecordLink
= ImageRecordLink
->ForwardLink
;
1076 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1077 LIST_ENTRY
*ImageRecordLink
;
1078 LIST_ENTRY
*ImageRecordList
;
1081 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
1083 for (ImageRecordLink
= ImageRecordList
->ForwardLink
, Index
= 0;
1084 ImageRecordLink
!= ImageRecordList
;
1085 ImageRecordLink
= ImageRecordLink
->ForwardLink
, Index
++) {
1088 IMAGE_PROPERTIES_RECORD
,
1090 IMAGE_PROPERTIES_RECORD_SIGNATURE
1092 DEBUG ((EFI_D_VERBOSE
, " Image[%d]: 0x%016lx - 0x%016lx\n", Index
, ImageRecord
->ImageBase
, ImageRecord
->ImageSize
));
1097 Insert image record.
1099 @param RuntimeImage Runtime image information
1103 IN EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
1107 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1108 UINT32 PeCoffHeaderOffset
;
1109 UINT32 SectionAlignment
;
1110 EFI_IMAGE_SECTION_HEADER
*Section
;
1111 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1114 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1116 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
1118 DEBUG ((EFI_D_VERBOSE
, "InsertImageRecord - 0x%x\n", RuntimeImage
));
1119 DEBUG ((EFI_D_VERBOSE
, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
));
1121 ImageRecord
= AllocatePool (sizeof(*ImageRecord
));
1122 if (ImageRecord
== NULL
) {
1125 ImageRecord
->Signature
= IMAGE_PROPERTIES_RECORD_SIGNATURE
;
1127 DEBUG ((EFI_D_VERBOSE
, "ImageRecordCount - 0x%x\n", mImagePropertiesPrivateData
.ImageRecordCount
));
1130 // Step 1: record whole region
1132 ImageRecord
->ImageBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
;
1133 ImageRecord
->ImageSize
= RuntimeImage
->ImageSize
;
1135 ImageAddress
= RuntimeImage
->ImageBase
;
1137 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1138 if (PdbPointer
!= NULL
) {
1139 DEBUG ((EFI_D_VERBOSE
, " Image - %a\n", PdbPointer
));
1143 // Check PE/COFF image
1145 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
1146 PeCoffHeaderOffset
= 0;
1147 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1148 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
1151 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
1152 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1153 DEBUG ((EFI_D_VERBOSE
, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr
.Pe32
->Signature
));
1154 // It might be image in SMM.
1159 // Measuring PE/COFF Image Header;
1160 // But CheckSum field and SECURITY data directory (certificate) are excluded
1162 if (Hdr
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1164 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1165 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1166 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1167 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1169 SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
1172 // Get the magic value from the PE/COFF Optional Header
1174 SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
1177 SetPropertiesTableSectionAlignment (SectionAlignment
);
1178 if ((SectionAlignment
& (SIZE_4KB
- 1)) != 0) {
1179 DEBUG ((EFI_D_ERROR
, "!!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not 4K !!!!!!!!\n", SectionAlignment
));
1180 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1181 if (PdbPointer
!= NULL
) {
1182 DEBUG ((EFI_D_ERROR
, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer
));
1187 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
1188 (UINT8
*) (UINTN
) ImageAddress
+
1189 PeCoffHeaderOffset
+
1191 sizeof(EFI_IMAGE_FILE_HEADER
) +
1192 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
1194 ImageRecord
->CodeSegmentCount
= 0;
1195 InitializeListHead (&ImageRecord
->CodeSegmentList
);
1196 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
1197 Name
= Section
[Index
].Name
;
1200 " Section - '%c%c%c%c%c%c%c%c'\n",
1211 if ((Section
[Index
].Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) != 0) {
1212 DEBUG ((EFI_D_VERBOSE
, " VirtualSize - 0x%08x\n", Section
[Index
].Misc
.VirtualSize
));
1213 DEBUG ((EFI_D_VERBOSE
, " VirtualAddress - 0x%08x\n", Section
[Index
].VirtualAddress
));
1214 DEBUG ((EFI_D_VERBOSE
, " SizeOfRawData - 0x%08x\n", Section
[Index
].SizeOfRawData
));
1215 DEBUG ((EFI_D_VERBOSE
, " PointerToRawData - 0x%08x\n", Section
[Index
].PointerToRawData
));
1216 DEBUG ((EFI_D_VERBOSE
, " PointerToRelocations - 0x%08x\n", Section
[Index
].PointerToRelocations
));
1217 DEBUG ((EFI_D_VERBOSE
, " PointerToLinenumbers - 0x%08x\n", Section
[Index
].PointerToLinenumbers
));
1218 DEBUG ((EFI_D_VERBOSE
, " NumberOfRelocations - 0x%08x\n", Section
[Index
].NumberOfRelocations
));
1219 DEBUG ((EFI_D_VERBOSE
, " NumberOfLinenumbers - 0x%08x\n", Section
[Index
].NumberOfLinenumbers
));
1220 DEBUG ((EFI_D_VERBOSE
, " Characteristics - 0x%08x\n", Section
[Index
].Characteristics
));
1223 // Step 2: record code section
1225 ImageRecordCodeSection
= AllocatePool (sizeof(*ImageRecordCodeSection
));
1226 if (ImageRecordCodeSection
== NULL
) {
1229 ImageRecordCodeSection
->Signature
= IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
;
1231 ImageRecordCodeSection
->CodeSegmentBase
= (UINTN
)ImageAddress
+ Section
[Index
].VirtualAddress
;
1232 ImageRecordCodeSection
->CodeSegmentSize
= Section
[Index
].SizeOfRawData
;
1234 DEBUG ((EFI_D_VERBOSE
, "ImageCode: 0x%016lx - 0x%016lx\n", ImageRecordCodeSection
->CodeSegmentBase
, ImageRecordCodeSection
->CodeSegmentSize
));
1236 InsertTailList (&ImageRecord
->CodeSegmentList
, &ImageRecordCodeSection
->Link
);
1237 ImageRecord
->CodeSegmentCount
++;
1241 if (ImageRecord
->CodeSegmentCount
== 0) {
1242 SetPropertiesTableSectionAlignment (1);
1243 DEBUG ((EFI_D_ERROR
, "!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
1244 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) (UINTN
) ImageAddress
);
1245 if (PdbPointer
!= NULL
) {
1246 DEBUG ((EFI_D_ERROR
, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer
));
1254 SortImageRecordCodeSection (ImageRecord
);
1256 // Check overlap all section in ImageBase/Size
1258 if (!IsImageRecordCodeSectionValid (ImageRecord
)) {
1259 DEBUG ((EFI_D_ERROR
, "IsImageRecordCodeSectionValid - FAIL\n"));
1263 InsertTailList (&mImagePropertiesPrivateData
.ImageRecordList
, &ImageRecord
->Link
);
1264 mImagePropertiesPrivateData
.ImageRecordCount
++;
1268 if (mImagePropertiesPrivateData
.CodeSegmentCountMax
< ImageRecord
->CodeSegmentCount
) {
1269 mImagePropertiesPrivateData
.CodeSegmentCountMax
= ImageRecord
->CodeSegmentCount
;
1277 Find image record accroding to image base and size.
1279 @param ImageBase Base of PE image
1280 @param ImageSize Size of PE image
1282 @return image record
1284 IMAGE_PROPERTIES_RECORD
*
1286 IN EFI_PHYSICAL_ADDRESS ImageBase
,
1290 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1291 LIST_ENTRY
*ImageRecordLink
;
1292 LIST_ENTRY
*ImageRecordList
;
1294 ImageRecordList
= &mImagePropertiesPrivateData
.ImageRecordList
;
1296 for (ImageRecordLink
= ImageRecordList
->ForwardLink
;
1297 ImageRecordLink
!= ImageRecordList
;
1298 ImageRecordLink
= ImageRecordLink
->ForwardLink
) {
1301 IMAGE_PROPERTIES_RECORD
,
1303 IMAGE_PROPERTIES_RECORD_SIGNATURE
1306 if ((ImageBase
== ImageRecord
->ImageBase
) &&
1307 (ImageSize
== ImageRecord
->ImageSize
)) {
1316 Remove Image record.
1318 @param RuntimeImage Runtime image information
1322 IN EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
1325 IMAGE_PROPERTIES_RECORD
*ImageRecord
;
1326 LIST_ENTRY
*CodeSegmentListHead
;
1327 IMAGE_PROPERTIES_RECORD_CODE_SECTION
*ImageRecordCodeSection
;
1329 DEBUG ((EFI_D_VERBOSE
, "RemoveImageRecord - 0x%x\n", RuntimeImage
));
1330 DEBUG ((EFI_D_VERBOSE
, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
));
1332 ImageRecord
= FindImageRecord ((EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
, RuntimeImage
->ImageSize
);
1333 if (ImageRecord
== NULL
) {
1334 DEBUG ((EFI_D_ERROR
, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
1338 CodeSegmentListHead
= &ImageRecord
->CodeSegmentList
;
1339 while (!IsListEmpty (CodeSegmentListHead
)) {
1340 ImageRecordCodeSection
= CR (
1341 CodeSegmentListHead
->ForwardLink
,
1342 IMAGE_PROPERTIES_RECORD_CODE_SECTION
,
1344 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
1346 RemoveEntryList (&ImageRecordCodeSection
->Link
);
1347 FreePool (ImageRecordCodeSection
);
1350 RemoveEntryList (&ImageRecord
->Link
);
1351 FreePool (ImageRecord
);
1352 mImagePropertiesPrivateData
.ImageRecordCount
--;
1357 Install PropertiesTable.
1359 @param[in] Event The Event this notify function registered to.
1360 @param[in] Context Pointer to the context data registered to the Event.
1364 InstallPropertiesTable (
1369 if (PcdGetBool (PropertiesTableEnable
)) {
1372 Status
= gBS
->InstallConfigurationTable (&gEfiPropertiesTableGuid
, &mPropertiesTable
);
1373 ASSERT_EFI_ERROR (Status
);
1375 DEBUG ((EFI_D_INFO
, "MemoryProtectionAttribute - 0x%016lx\n", mPropertiesTable
.MemoryProtectionAttribute
));
1376 if ((mPropertiesTable
.MemoryProtectionAttribute
& EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA
) == 0) {
1380 gBS
->GetMemoryMap
= CoreGetMemoryMapPropertiesTable
;
1382 gBS
->CalculateCrc32 ((UINT8
*)gBS
, gBS
->Hdr
.HeaderSize
, &gBS
->Hdr
.CRC32
);
1384 DEBUG ((EFI_D_VERBOSE
, "Total Image Count - 0x%x\n", mImagePropertiesPrivateData
.ImageRecordCount
));
1385 DEBUG ((EFI_D_VERBOSE
, "Dump ImageRecord:\n"));
1391 Initialize PropertiesTable support.
1395 CoreInitializePropertiesTable (
1400 EFI_EVENT EndOfDxeEvent
;
1402 Status
= gBS
->CreateEventEx (
1405 InstallPropertiesTable
,
1407 &gEfiEndOfDxeEventGroupGuid
,
1410 ASSERT_EFI_ERROR (Status
);