]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c
MdeModulePkg: Fix Memory Attributes table type issue
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / PropertiesTable.c
1 /** @file
2 UEFI PropertiesTable support
3
4 Copyright (c) 2015 - 2016, 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
9
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.
12
13 **/
14
15 #include <PiDxe.h>
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>
24
25 #include <Guid/EventGroup.h>
26 #include <Protocol/DxeSmmReadyToLock.h>
27
28 #include <Library/PeCoffLib.h>
29 #include <Library/PeCoffGetEntryPointLib.h>
30 #include <Protocol/Runtime.h>
31
32 #include <Guid/PropertiesTable.h>
33
34 #include "DxeMain.h"
35
36 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
37 ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
38
39 #define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I','P','R','C')
40
41 typedef struct {
42 UINT32 Signature;
43 LIST_ENTRY Link;
44 EFI_PHYSICAL_ADDRESS CodeSegmentBase;
45 UINT64 CodeSegmentSize;
46 } IMAGE_PROPERTIES_RECORD_CODE_SECTION;
47
48 #define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D')
49
50 typedef struct {
51 UINT32 Signature;
52 LIST_ENTRY Link;
53 EFI_PHYSICAL_ADDRESS ImageBase;
54 UINT64 ImageSize;
55 UINTN CodeSegmentCount;
56 LIST_ENTRY CodeSegmentList;
57 } IMAGE_PROPERTIES_RECORD;
58
59 #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')
60
61 typedef struct {
62 UINT32 Signature;
63 UINTN ImageRecordCount;
64 UINTN CodeSegmentCountMax;
65 LIST_ENTRY ImageRecordList;
66 } IMAGE_PROPERTIES_PRIVATE_DATA;
67
68 IMAGE_PROPERTIES_PRIVATE_DATA mImagePropertiesPrivateData = {
69 IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE,
70 0,
71 0,
72 INITIALIZE_LIST_HEAD_VARIABLE (mImagePropertiesPrivateData.ImageRecordList)
73 };
74
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
79 };
80
81 EFI_LOCK mPropertiesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
82
83 BOOLEAN mPropertiesTableEnable;
84
85 //
86 // Below functions are for MemoryMap
87 //
88
89 /**
90 Converts a number of EFI_PAGEs to a size in bytes.
91
92 NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.
93
94 @param Pages The number of EFI_PAGES.
95
96 @return The number of bytes associated with the number of EFI_PAGEs specified
97 by Pages.
98 **/
99 STATIC
100 UINT64
101 EfiPagesToSize (
102 IN UINT64 Pages
103 )
104 {
105 return LShiftU64 (Pages, EFI_PAGE_SHIFT);
106 }
107
108 /**
109 Converts a size, in bytes, to a number of EFI_PAGESs.
110
111 NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only.
112
113 @param Size A size in bytes.
114
115 @return The number of EFI_PAGESs associated with the number of bytes specified
116 by Size.
117
118 **/
119 STATIC
120 UINT64
121 EfiSizeToPages (
122 IN UINT64 Size
123 )
124 {
125 return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MASK) ? 1 : 0);
126 }
127
128 /**
129 Acquire memory lock on mPropertiesTableLock.
130 **/
131 STATIC
132 VOID
133 CoreAcquirePropertiesTableLock (
134 VOID
135 )
136 {
137 CoreAcquireLock (&mPropertiesTableLock);
138 }
139
140 /**
141 Release memory lock on mPropertiesTableLock.
142 **/
143 STATIC
144 VOID
145 CoreReleasePropertiesTableLock (
146 VOID
147 )
148 {
149 CoreReleaseLock (&mPropertiesTableLock);
150 }
151
152 /**
153 Sort memory map entries based upon PhysicalStart, from low to high.
154
155 @param MemoryMap A pointer to the buffer in which firmware places
156 the current memory map.
157 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
158 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
159 **/
160 STATIC
161 VOID
162 SortMemoryMap (
163 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
164 IN UINTN MemoryMapSize,
165 IN UINTN DescriptorSize
166 )
167 {
168 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
169 EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;
170 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
171 EFI_MEMORY_DESCRIPTOR TempMemoryMap;
172
173 MemoryMapEntry = MemoryMap;
174 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
175 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);
176 while (MemoryMapEntry < MemoryMapEnd) {
177 while (NextMemoryMapEntry < MemoryMapEnd) {
178 if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStart) {
179 CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));
180 CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));
181 CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof(EFI_MEMORY_DESCRIPTOR));
182 }
183
184 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
185 }
186
187 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
188 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
189 }
190
191 return ;
192 }
193
194 /**
195 Merge continous memory map entries whose have same attributes.
196
197 @param MemoryMap A pointer to the buffer in which firmware places
198 the current memory map.
199 @param MemoryMapSize A pointer to the size, in bytes, of the
200 MemoryMap buffer. On input, this is the size of
201 the current memory map. On output,
202 it is the size of new memory map after merge.
203 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
204 **/
205 STATIC
206 VOID
207 MergeMemoryMap (
208 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
209 IN OUT UINTN *MemoryMapSize,
210 IN UINTN DescriptorSize
211 )
212 {
213 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
214 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
215 UINT64 MemoryBlockLength;
216 EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry;
217 EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;
218
219 MemoryMapEntry = MemoryMap;
220 NewMemoryMapEntry = MemoryMap;
221 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + *MemoryMapSize);
222 while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
223 CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));
224 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
225
226 do {
227 MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));
228 if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&
229 (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&
230 (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&
231 ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {
232 MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;
233 if (NewMemoryMapEntry != MemoryMapEntry) {
234 NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;
235 }
236
237 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
238 continue;
239 } else {
240 MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);
241 break;
242 }
243 } while (TRUE);
244
245 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
246 NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);
247 }
248
249 *MemoryMapSize = (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap;
250
251 return ;
252 }
253
254 /**
255 Enforce memory map attributes.
256 This function will set EfiRuntimeServicesData/EfiMemoryMappedIO/EfiMemoryMappedIOPortSpace to be EFI_MEMORY_XP.
257
258 @param MemoryMap A pointer to the buffer in which firmware places
259 the current memory map.
260 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.
261 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
262 **/
263 STATIC
264 VOID
265 EnforceMemoryMapAttribute (
266 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
267 IN UINTN MemoryMapSize,
268 IN UINTN DescriptorSize
269 )
270 {
271 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;
272 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;
273
274 MemoryMapEntry = MemoryMap;
275 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);
276 while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {
277 switch (MemoryMapEntry->Type) {
278 case EfiRuntimeServicesCode:
279 // do nothing
280 break;
281 case EfiRuntimeServicesData:
282 case EfiMemoryMappedIO:
283 case EfiMemoryMappedIOPortSpace:
284 MemoryMapEntry->Attribute |= EFI_MEMORY_XP;
285 break;
286 case EfiReservedMemoryType:
287 case EfiACPIMemoryNVS:
288 break;
289 }
290
291 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);
292 }
293
294 return ;
295 }
296
297 /**
298 Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].
299
300 @param Buffer Start Address
301 @param Length Address length
302
303 @return first image record covered by [buffer, length]
304 **/
305 STATIC
306 IMAGE_PROPERTIES_RECORD *
307 GetImageRecordByAddress (
308 IN EFI_PHYSICAL_ADDRESS Buffer,
309 IN UINT64 Length
310 )
311 {
312 IMAGE_PROPERTIES_RECORD *ImageRecord;
313 LIST_ENTRY *ImageRecordLink;
314 LIST_ENTRY *ImageRecordList;
315
316 ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
317
318 for (ImageRecordLink = ImageRecordList->ForwardLink;
319 ImageRecordLink != ImageRecordList;
320 ImageRecordLink = ImageRecordLink->ForwardLink) {
321 ImageRecord = CR (
322 ImageRecordLink,
323 IMAGE_PROPERTIES_RECORD,
324 Link,
325 IMAGE_PROPERTIES_RECORD_SIGNATURE
326 );
327
328 if ((Buffer <= ImageRecord->ImageBase) &&
329 (Buffer + Length >= ImageRecord->ImageBase + ImageRecord->ImageSize)) {
330 return ImageRecord;
331 }
332 }
333
334 return NULL;
335 }
336
337 /**
338 Set the memory map to new entries, according to one old entry,
339 based upon PE code section and data section in image record
340
341 @param ImageRecord An image record whose [ImageBase, ImageSize] covered
342 by old memory map entry.
343 @param NewRecord A pointer to several new memory map entries.
344 The caller gurantee the buffer size be 1 +
345 (SplitRecordCount * DescriptorSize) calculated
346 below.
347 @param OldRecord A pointer to one old memory map entry.
348 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
349 **/
350 STATIC
351 UINTN
352 SetNewRecord (
353 IN IMAGE_PROPERTIES_RECORD *ImageRecord,
354 IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord,
355 IN EFI_MEMORY_DESCRIPTOR *OldRecord,
356 IN UINTN DescriptorSize
357 )
358 {
359 EFI_MEMORY_DESCRIPTOR TempRecord;
360 IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;
361 LIST_ENTRY *ImageRecordCodeSectionLink;
362 LIST_ENTRY *ImageRecordCodeSectionEndLink;
363 LIST_ENTRY *ImageRecordCodeSectionList;
364 UINTN NewRecordCount;
365 UINT64 PhysicalEnd;
366 UINT64 ImageEnd;
367
368 CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR));
369 PhysicalEnd = TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.NumberOfPages);
370 NewRecordCount = 0;
371
372 ImageRecordCodeSectionList = &ImageRecord->CodeSegmentList;
373
374 ImageRecordCodeSectionLink = ImageRecordCodeSectionList->ForwardLink;
375 ImageRecordCodeSectionEndLink = ImageRecordCodeSectionList;
376 while (ImageRecordCodeSectionLink != ImageRecordCodeSectionEndLink) {
377 ImageRecordCodeSection = CR (
378 ImageRecordCodeSectionLink,
379 IMAGE_PROPERTIES_RECORD_CODE_SECTION,
380 Link,
381 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
382 );
383 ImageRecordCodeSectionLink = ImageRecordCodeSectionLink->ForwardLink;
384
385 if (TempRecord.PhysicalStart <= ImageRecordCodeSection->CodeSegmentBase) {
386 //
387 // DATA
388 //
389 if (!mPropertiesTableEnable) {
390 NewRecord->Type = TempRecord.Type;
391 } else {
392 NewRecord->Type = EfiRuntimeServicesData;
393 }
394 NewRecord->PhysicalStart = TempRecord.PhysicalStart;
395 NewRecord->VirtualStart = 0;
396 NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentBase - NewRecord->PhysicalStart);
397 NewRecord->Attribute = TempRecord.Attribute | EFI_MEMORY_XP;
398 if (NewRecord->NumberOfPages != 0) {
399 NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);
400 NewRecordCount ++;
401 }
402
403 //
404 // CODE
405 //
406 if (!mPropertiesTableEnable) {
407 NewRecord->Type = TempRecord.Type;
408 } else {
409 NewRecord->Type = EfiRuntimeServicesCode;
410 }
411 NewRecord->PhysicalStart = ImageRecordCodeSection->CodeSegmentBase;
412 NewRecord->VirtualStart = 0;
413 NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize);
414 NewRecord->Attribute = (TempRecord.Attribute & (~EFI_MEMORY_XP)) | EFI_MEMORY_RO;
415 if (NewRecord->NumberOfPages != 0) {
416 NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);
417 NewRecordCount ++;
418 }
419
420 TempRecord.PhysicalStart = ImageRecordCodeSection->CodeSegmentBase + EfiPagesToSize (EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize));
421 TempRecord.NumberOfPages = EfiSizeToPages(PhysicalEnd - TempRecord.PhysicalStart);
422 if (TempRecord.NumberOfPages == 0) {
423 break;
424 }
425 }
426 }
427
428 ImageEnd = ImageRecord->ImageBase + ImageRecord->ImageSize;
429
430 //
431 // Final DATA
432 //
433 if (TempRecord.PhysicalStart < ImageEnd) {
434 if (!mPropertiesTableEnable) {
435 NewRecord->Type = TempRecord.Type;
436 } else {
437 NewRecord->Type = EfiRuntimeServicesData;
438 }
439 NewRecord->PhysicalStart = TempRecord.PhysicalStart;
440 NewRecord->VirtualStart = 0;
441 NewRecord->NumberOfPages = EfiSizeToPages (ImageEnd - TempRecord.PhysicalStart);
442 NewRecord->Attribute = TempRecord.Attribute | EFI_MEMORY_XP;
443 NewRecordCount ++;
444 }
445
446 return NewRecordCount;
447 }
448
449 /**
450 Return the max number of new splitted entries, according to one old entry,
451 based upon PE code section and data section.
452
453 @param OldRecord A pointer to one old memory map entry.
454
455 @retval 0 no entry need to be splitted.
456 @return the max number of new splitted entries
457 **/
458 STATIC
459 UINTN
460 GetMaxSplitRecordCount (
461 IN EFI_MEMORY_DESCRIPTOR *OldRecord
462 )
463 {
464 IMAGE_PROPERTIES_RECORD *ImageRecord;
465 UINTN SplitRecordCount;
466 UINT64 PhysicalStart;
467 UINT64 PhysicalEnd;
468
469 SplitRecordCount = 0;
470 PhysicalStart = OldRecord->PhysicalStart;
471 PhysicalEnd = OldRecord->PhysicalStart + EfiPagesToSize(OldRecord->NumberOfPages);
472
473 do {
474 ImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart);
475 if (ImageRecord == NULL) {
476 break;
477 }
478 SplitRecordCount += (2 * ImageRecord->CodeSegmentCount + 1);
479 PhysicalStart = ImageRecord->ImageBase + ImageRecord->ImageSize;
480 } while ((ImageRecord != NULL) && (PhysicalStart < PhysicalEnd));
481
482 if (SplitRecordCount != 0) {
483 SplitRecordCount--;
484 }
485
486 return SplitRecordCount;
487 }
488
489 /**
490 Split the memory map to new entries, according to one old entry,
491 based upon PE code section and data section.
492
493 @param OldRecord A pointer to one old memory map entry.
494 @param NewRecord A pointer to several new memory map entries.
495 The caller gurantee the buffer size be 1 +
496 (SplitRecordCount * DescriptorSize) calculated
497 below.
498 @param MaxSplitRecordCount The max number of splitted entries
499 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
500
501 @retval 0 no entry is splitted.
502 @return the real number of splitted record.
503 **/
504 STATIC
505 UINTN
506 SplitRecord (
507 IN EFI_MEMORY_DESCRIPTOR *OldRecord,
508 IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord,
509 IN UINTN MaxSplitRecordCount,
510 IN UINTN DescriptorSize
511 )
512 {
513 EFI_MEMORY_DESCRIPTOR TempRecord;
514 IMAGE_PROPERTIES_RECORD *ImageRecord;
515 IMAGE_PROPERTIES_RECORD *NewImageRecord;
516 UINT64 PhysicalStart;
517 UINT64 PhysicalEnd;
518 UINTN NewRecordCount;
519 UINTN TotalNewRecordCount;
520 BOOLEAN IsLastRecordData;
521
522 if (MaxSplitRecordCount == 0) {
523 CopyMem (NewRecord, OldRecord, DescriptorSize);
524 return 0;
525 }
526
527 TotalNewRecordCount = 0;
528
529 //
530 // Override previous record
531 //
532 CopyMem (&TempRecord, OldRecord, sizeof(EFI_MEMORY_DESCRIPTOR));
533 PhysicalStart = TempRecord.PhysicalStart;
534 PhysicalEnd = TempRecord.PhysicalStart + EfiPagesToSize(TempRecord.NumberOfPages);
535
536 ImageRecord = NULL;
537 do {
538 NewImageRecord = GetImageRecordByAddress (PhysicalStart, PhysicalEnd - PhysicalStart);
539 if (NewImageRecord == NULL) {
540 //
541 // No more image covered by this range, stop
542 //
543 if ((PhysicalEnd > PhysicalStart) && (ImageRecord != NULL)) {
544 //
545 // If this is still address in this record, need record.
546 //
547 NewRecord = PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);
548 IsLastRecordData = FALSE;
549 if (!mPropertiesTableEnable) {
550 if ((NewRecord->Attribute & EFI_MEMORY_XP) != 0) {
551 IsLastRecordData = TRUE;
552 }
553 } else {
554 if (NewRecord->Type == EfiRuntimeServicesData) {
555 IsLastRecordData = TRUE;
556 }
557 }
558 if (IsLastRecordData) {
559 //
560 // Last record is DATA, just merge it.
561 //
562 NewRecord->NumberOfPages = EfiSizeToPages(PhysicalEnd - NewRecord->PhysicalStart);
563 } else {
564 //
565 // Last record is CODE, create a new DATA entry.
566 //
567 NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);
568 if (!mPropertiesTableEnable) {
569 NewRecord->Type = TempRecord.Type;
570 } else {
571 NewRecord->Type = EfiRuntimeServicesData;
572 }
573 NewRecord->PhysicalStart = TempRecord.PhysicalStart;
574 NewRecord->VirtualStart = 0;
575 NewRecord->NumberOfPages = TempRecord.NumberOfPages;
576 NewRecord->Attribute = TempRecord.Attribute | EFI_MEMORY_XP;
577 TotalNewRecordCount ++;
578 }
579 }
580 break;
581 }
582 ImageRecord = NewImageRecord;
583
584 //
585 // Set new record
586 //
587 NewRecordCount = SetNewRecord (ImageRecord, NewRecord, &TempRecord, DescriptorSize);
588 TotalNewRecordCount += NewRecordCount;
589 NewRecord = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + NewRecordCount * DescriptorSize);
590
591 //
592 // Update PhysicalStart, in order to exclude the image buffer already splitted.
593 //
594 PhysicalStart = ImageRecord->ImageBase + ImageRecord->ImageSize;
595 TempRecord.PhysicalStart = PhysicalStart;
596 TempRecord.NumberOfPages = EfiSizeToPages (PhysicalEnd - PhysicalStart);
597 } while ((ImageRecord != NULL) && (PhysicalStart < PhysicalEnd));
598
599 return TotalNewRecordCount - 1;
600 }
601
602 /**
603 Split the original memory map, and add more entries to describe PE code section and data section.
604 This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP.
605 This function will merge entries with same attributes finally.
606
607 NOTE: It assumes PE code/data section are page aligned.
608 NOTE: It assumes enough entry is prepared for new memory map.
609
610 Split table:
611 +---------------+
612 | Record X |
613 +---------------+
614 | Record RtCode |
615 +---------------+
616 | Record Y |
617 +---------------+
618 ==>
619 +---------------+
620 | Record X |
621 +---------------+ ----
622 | Record RtData | |
623 +---------------+ |
624 | Record RtCode | |-> PE/COFF1
625 +---------------+ |
626 | Record RtData | |
627 +---------------+ ----
628 | Record RtData | |
629 +---------------+ |
630 | Record RtCode | |-> PE/COFF2
631 +---------------+ |
632 | Record RtData | |
633 +---------------+ ----
634 | Record Y |
635 +---------------+
636
637 @param MemoryMapSize A pointer to the size, in bytes, of the
638 MemoryMap buffer. On input, this is the size of
639 old MemoryMap before split. The actual buffer
640 size of MemoryMap is MemoryMapSize +
641 (AdditionalRecordCount * DescriptorSize) calculated
642 below. On output, it is the size of new MemoryMap
643 after split.
644 @param MemoryMap A pointer to the buffer in which firmware places
645 the current memory map.
646 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
647 **/
648 STATIC
649 VOID
650 SplitTable (
651 IN OUT UINTN *MemoryMapSize,
652 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
653 IN UINTN DescriptorSize
654 )
655 {
656 INTN IndexOld;
657 INTN IndexNew;
658 UINTN MaxSplitRecordCount;
659 UINTN RealSplitRecordCount;
660 UINTN TotalSplitRecordCount;
661 UINTN AdditionalRecordCount;
662
663 AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 1) * mImagePropertiesPrivateData.ImageRecordCount;
664
665 TotalSplitRecordCount = 0;
666 //
667 // Let old record point to end of valid MemoryMap buffer.
668 //
669 IndexOld = ((*MemoryMapSize) / DescriptorSize) - 1;
670 //
671 // Let new record point to end of full MemoryMap buffer.
672 //
673 IndexNew = ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordCount;
674 for (; IndexOld >= 0; IndexOld--) {
675 MaxSplitRecordCount = GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize));
676 //
677 // Split this MemoryMap record
678 //
679 IndexNew -= MaxSplitRecordCount;
680 RealSplitRecordCount = SplitRecord (
681 (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize),
682 (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + IndexNew * DescriptorSize),
683 MaxSplitRecordCount,
684 DescriptorSize
685 );
686 //
687 // Adjust IndexNew according to real split.
688 //
689 CopyMem (
690 ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRecordCount) * DescriptorSize),
691 ((UINT8 *)MemoryMap + IndexNew * DescriptorSize),
692 RealSplitRecordCount * DescriptorSize
693 );
694 IndexNew = IndexNew + MaxSplitRecordCount - RealSplitRecordCount;
695 TotalSplitRecordCount += RealSplitRecordCount;
696 IndexNew --;
697 }
698 //
699 // Move all records to the beginning.
700 //
701 CopyMem (
702 MemoryMap,
703 (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) * DescriptorSize,
704 (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize
705 );
706
707 *MemoryMapSize = (*MemoryMapSize) + DescriptorSize * TotalSplitRecordCount;
708
709 //
710 // Sort from low to high (Just in case)
711 //
712 SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize);
713
714 //
715 // Set RuntimeData to XP
716 //
717 EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, DescriptorSize);
718
719 //
720 // Merge same type to save entry size
721 //
722 MergeMemoryMap (MemoryMap, MemoryMapSize, DescriptorSize);
723
724 return ;
725 }
726
727 /**
728 This function for GetMemoryMap() with properties table capability.
729
730 It calls original GetMemoryMap() to get the original memory map information. Then
731 plus the additional memory map entries for PE Code/Data seperation.
732
733 @param MemoryMapSize A pointer to the size, in bytes, of the
734 MemoryMap buffer. On input, this is the size of
735 the buffer allocated by the caller. On output,
736 it is the size of the buffer returned by the
737 firmware if the buffer was large enough, or the
738 size of the buffer needed to contain the map if
739 the buffer was too small.
740 @param MemoryMap A pointer to the buffer in which firmware places
741 the current memory map.
742 @param MapKey A pointer to the location in which firmware
743 returns the key for the current memory map.
744 @param DescriptorSize A pointer to the location in which firmware
745 returns the size, in bytes, of an individual
746 EFI_MEMORY_DESCRIPTOR.
747 @param DescriptorVersion A pointer to the location in which firmware
748 returns the version number associated with the
749 EFI_MEMORY_DESCRIPTOR.
750
751 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
752 buffer.
753 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
754 buffer size needed to hold the memory map is
755 returned in MemoryMapSize.
756 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
757
758 **/
759 EFI_STATUS
760 EFIAPI
761 CoreGetMemoryMapPropertiesTable (
762 IN OUT UINTN *MemoryMapSize,
763 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
764 OUT UINTN *MapKey,
765 OUT UINTN *DescriptorSize,
766 OUT UINT32 *DescriptorVersion
767 )
768 {
769 EFI_STATUS Status;
770 UINTN OldMemoryMapSize;
771 UINTN AdditionalRecordCount;
772
773 //
774 // If PE code/data is not aligned, just return.
775 //
776 if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {
777 return CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
778 }
779
780 if (MemoryMapSize == NULL) {
781 return EFI_INVALID_PARAMETER;
782 }
783
784 CoreAcquirePropertiesTableLock ();
785
786 AdditionalRecordCount = (2 * mImagePropertiesPrivateData.CodeSegmentCountMax + 1) * mImagePropertiesPrivateData.ImageRecordCount;
787
788 OldMemoryMapSize = *MemoryMapSize;
789 Status = CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey, DescriptorSize, DescriptorVersion);
790 if (Status == EFI_BUFFER_TOO_SMALL) {
791 *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
792 } else if (Status == EFI_SUCCESS) {
793 if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {
794 *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;
795 //
796 // Need update status to buffer too small
797 //
798 Status = EFI_BUFFER_TOO_SMALL;
799 } else {
800 //
801 // Split PE code/data
802 //
803 SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize);
804 }
805 }
806
807 CoreReleasePropertiesTableLock ();
808 return Status;
809 }
810
811 //
812 // Below functions are for ImageRecord
813 //
814
815 /**
816 Set PropertiesTable according to PE/COFF image section alignment.
817
818 @param SectionAlignment PE/COFF section alignment
819 **/
820 STATIC
821 VOID
822 SetPropertiesTableSectionAlignment (
823 IN UINT32 SectionAlignment
824 )
825 {
826 if (((SectionAlignment & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) &&
827 ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) != 0)) {
828 DEBUG ((EFI_D_VERBOSE, "SetPropertiesTableSectionAlignment - Clear\n"));
829 mPropertiesTable.MemoryProtectionAttribute &= ~((UINT64)EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
830 gBS->GetMemoryMap = CoreGetMemoryMap;
831 gBS->Hdr.CRC32 = 0;
832 gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);
833 }
834 }
835
836 /**
837 Swap two code sections in image record.
838
839 @param FirstImageRecordCodeSection first code section in image record
840 @param SecondImageRecordCodeSection second code section in image record
841 **/
842 STATIC
843 VOID
844 SwapImageRecordCodeSection (
845 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection,
846 IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection
847 )
848 {
849 IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection;
850
851 TempImageRecordCodeSection.CodeSegmentBase = FirstImageRecordCodeSection->CodeSegmentBase;
852 TempImageRecordCodeSection.CodeSegmentSize = FirstImageRecordCodeSection->CodeSegmentSize;
853
854 FirstImageRecordCodeSection->CodeSegmentBase = SecondImageRecordCodeSection->CodeSegmentBase;
855 FirstImageRecordCodeSection->CodeSegmentSize = SecondImageRecordCodeSection->CodeSegmentSize;
856
857 SecondImageRecordCodeSection->CodeSegmentBase = TempImageRecordCodeSection.CodeSegmentBase;
858 SecondImageRecordCodeSection->CodeSegmentSize = TempImageRecordCodeSection.CodeSegmentSize;
859 }
860
861 /**
862 Sort code section in image record, based upon CodeSegmentBase from low to high.
863
864 @param ImageRecord image record to be sorted
865 **/
866 STATIC
867 VOID
868 SortImageRecordCodeSection (
869 IN IMAGE_PROPERTIES_RECORD *ImageRecord
870 )
871 {
872 IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;
873 IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection;
874 LIST_ENTRY *ImageRecordCodeSectionLink;
875 LIST_ENTRY *NextImageRecordCodeSectionLink;
876 LIST_ENTRY *ImageRecordCodeSectionEndLink;
877 LIST_ENTRY *ImageRecordCodeSectionList;
878
879 ImageRecordCodeSectionList = &ImageRecord->CodeSegmentList;
880
881 ImageRecordCodeSectionLink = ImageRecordCodeSectionList->ForwardLink;
882 NextImageRecordCodeSectionLink = ImageRecordCodeSectionLink->ForwardLink;
883 ImageRecordCodeSectionEndLink = ImageRecordCodeSectionList;
884 while (ImageRecordCodeSectionLink != ImageRecordCodeSectionEndLink) {
885 ImageRecordCodeSection = CR (
886 ImageRecordCodeSectionLink,
887 IMAGE_PROPERTIES_RECORD_CODE_SECTION,
888 Link,
889 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
890 );
891 while (NextImageRecordCodeSectionLink != ImageRecordCodeSectionEndLink) {
892 NextImageRecordCodeSection = CR (
893 NextImageRecordCodeSectionLink,
894 IMAGE_PROPERTIES_RECORD_CODE_SECTION,
895 Link,
896 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
897 );
898 if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSection->CodeSegmentBase) {
899 SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecordCodeSection);
900 }
901 NextImageRecordCodeSectionLink = NextImageRecordCodeSectionLink->ForwardLink;
902 }
903
904 ImageRecordCodeSectionLink = ImageRecordCodeSectionLink->ForwardLink;
905 NextImageRecordCodeSectionLink = ImageRecordCodeSectionLink->ForwardLink;
906 }
907 }
908
909 /**
910 Check if code section in image record is valid.
911
912 @param ImageRecord image record to be checked
913
914 @retval TRUE image record is valid
915 @retval FALSE image record is invalid
916 **/
917 STATIC
918 BOOLEAN
919 IsImageRecordCodeSectionValid (
920 IN IMAGE_PROPERTIES_RECORD *ImageRecord
921 )
922 {
923 IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;
924 IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection;
925 LIST_ENTRY *ImageRecordCodeSectionLink;
926 LIST_ENTRY *ImageRecordCodeSectionEndLink;
927 LIST_ENTRY *ImageRecordCodeSectionList;
928
929 DEBUG ((EFI_D_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->CodeSegmentCount));
930
931 ImageRecordCodeSectionList = &ImageRecord->CodeSegmentList;
932
933 ImageRecordCodeSectionLink = ImageRecordCodeSectionList->ForwardLink;
934 ImageRecordCodeSectionEndLink = ImageRecordCodeSectionList;
935 LastImageRecordCodeSection = NULL;
936 while (ImageRecordCodeSectionLink != ImageRecordCodeSectionEndLink) {
937 ImageRecordCodeSection = CR (
938 ImageRecordCodeSectionLink,
939 IMAGE_PROPERTIES_RECORD_CODE_SECTION,
940 Link,
941 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
942 );
943 if (ImageRecordCodeSection->CodeSegmentSize == 0) {
944 return FALSE;
945 }
946 if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) {
947 return FALSE;
948 }
949 if (ImageRecordCodeSection->CodeSegmentBase >= MAX_ADDRESS - ImageRecordCodeSection->CodeSegmentSize) {
950 return FALSE;
951 }
952 if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection->CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) {
953 return FALSE;
954 }
955 if (LastImageRecordCodeSection != NULL) {
956 if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCodeSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) {
957 return FALSE;
958 }
959 }
960
961 LastImageRecordCodeSection = ImageRecordCodeSection;
962 ImageRecordCodeSectionLink = ImageRecordCodeSectionLink->ForwardLink;
963 }
964
965 return TRUE;
966 }
967
968 /**
969 Swap two image records.
970
971 @param FirstImageRecord first image record.
972 @param SecondImageRecord second image record.
973 **/
974 STATIC
975 VOID
976 SwapImageRecord (
977 IN IMAGE_PROPERTIES_RECORD *FirstImageRecord,
978 IN IMAGE_PROPERTIES_RECORD *SecondImageRecord
979 )
980 {
981 IMAGE_PROPERTIES_RECORD TempImageRecord;
982
983 TempImageRecord.ImageBase = FirstImageRecord->ImageBase;
984 TempImageRecord.ImageSize = FirstImageRecord->ImageSize;
985 TempImageRecord.CodeSegmentCount = FirstImageRecord->CodeSegmentCount;
986
987 FirstImageRecord->ImageBase = SecondImageRecord->ImageBase;
988 FirstImageRecord->ImageSize = SecondImageRecord->ImageSize;
989 FirstImageRecord->CodeSegmentCount = SecondImageRecord->CodeSegmentCount;
990
991 SecondImageRecord->ImageBase = TempImageRecord.ImageBase;
992 SecondImageRecord->ImageSize = TempImageRecord.ImageSize;
993 SecondImageRecord->CodeSegmentCount = TempImageRecord.CodeSegmentCount;
994
995 SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord->CodeSegmentList);
996 }
997
998 /**
999 Sort image record based upon the ImageBase from low to high.
1000 **/
1001 STATIC
1002 VOID
1003 SortImageRecord (
1004 VOID
1005 )
1006 {
1007 IMAGE_PROPERTIES_RECORD *ImageRecord;
1008 IMAGE_PROPERTIES_RECORD *NextImageRecord;
1009 LIST_ENTRY *ImageRecordLink;
1010 LIST_ENTRY *NextImageRecordLink;
1011 LIST_ENTRY *ImageRecordEndLink;
1012 LIST_ENTRY *ImageRecordList;
1013
1014 ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
1015
1016 ImageRecordLink = ImageRecordList->ForwardLink;
1017 NextImageRecordLink = ImageRecordLink->ForwardLink;
1018 ImageRecordEndLink = ImageRecordList;
1019 while (ImageRecordLink != ImageRecordEndLink) {
1020 ImageRecord = CR (
1021 ImageRecordLink,
1022 IMAGE_PROPERTIES_RECORD,
1023 Link,
1024 IMAGE_PROPERTIES_RECORD_SIGNATURE
1025 );
1026 while (NextImageRecordLink != ImageRecordEndLink) {
1027 NextImageRecord = CR (
1028 NextImageRecordLink,
1029 IMAGE_PROPERTIES_RECORD,
1030 Link,
1031 IMAGE_PROPERTIES_RECORD_SIGNATURE
1032 );
1033 if (ImageRecord->ImageBase > NextImageRecord->ImageBase) {
1034 SwapImageRecord (ImageRecord, NextImageRecord);
1035 }
1036 NextImageRecordLink = NextImageRecordLink->ForwardLink;
1037 }
1038
1039 ImageRecordLink = ImageRecordLink->ForwardLink;
1040 NextImageRecordLink = ImageRecordLink->ForwardLink;
1041 }
1042 }
1043
1044 /**
1045 Dump image record.
1046 **/
1047 STATIC
1048 VOID
1049 DumpImageRecord (
1050 VOID
1051 )
1052 {
1053 IMAGE_PROPERTIES_RECORD *ImageRecord;
1054 LIST_ENTRY *ImageRecordLink;
1055 LIST_ENTRY *ImageRecordList;
1056 UINTN Index;
1057
1058 ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
1059
1060 for (ImageRecordLink = ImageRecordList->ForwardLink, Index= 0;
1061 ImageRecordLink != ImageRecordList;
1062 ImageRecordLink = ImageRecordLink->ForwardLink, Index++) {
1063 ImageRecord = CR (
1064 ImageRecordLink,
1065 IMAGE_PROPERTIES_RECORD,
1066 Link,
1067 IMAGE_PROPERTIES_RECORD_SIGNATURE
1068 );
1069 DEBUG ((EFI_D_VERBOSE, " Image[%d]: 0x%016lx - 0x%016lx\n", Index, ImageRecord->ImageBase, ImageRecord->ImageSize));
1070 }
1071 }
1072
1073 /**
1074 Insert image record.
1075
1076 @param RuntimeImage Runtime image information
1077 **/
1078 VOID
1079 InsertImageRecord (
1080 IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
1081 )
1082 {
1083 VOID *ImageAddress;
1084 EFI_IMAGE_DOS_HEADER *DosHdr;
1085 UINT32 PeCoffHeaderOffset;
1086 UINT32 SectionAlignment;
1087 EFI_IMAGE_SECTION_HEADER *Section;
1088 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1089 UINT8 *Name;
1090 UINTN Index;
1091 IMAGE_PROPERTIES_RECORD *ImageRecord;
1092 CHAR8 *PdbPointer;
1093 IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;
1094 UINT16 Magic;
1095
1096 DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage));
1097 DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));
1098
1099 ImageRecord = AllocatePool (sizeof(*ImageRecord));
1100 if (ImageRecord == NULL) {
1101 return ;
1102 }
1103 ImageRecord->Signature = IMAGE_PROPERTIES_RECORD_SIGNATURE;
1104
1105 DEBUG ((EFI_D_VERBOSE, "ImageRecordCount - 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));
1106
1107 //
1108 // Step 1: record whole region
1109 //
1110 ImageRecord->ImageBase = (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase;
1111 ImageRecord->ImageSize = RuntimeImage->ImageSize;
1112
1113 ImageAddress = RuntimeImage->ImageBase;
1114
1115 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);
1116 if (PdbPointer != NULL) {
1117 DEBUG ((EFI_D_VERBOSE, " Image - %a\n", PdbPointer));
1118 }
1119
1120 //
1121 // Check PE/COFF image
1122 //
1123 DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;
1124 PeCoffHeaderOffset = 0;
1125 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1126 PeCoffHeaderOffset = DosHdr->e_lfanew;
1127 }
1128
1129 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress + PeCoffHeaderOffset);
1130 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1131 DEBUG ((EFI_D_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe32->Signature));
1132 // It might be image in SMM.
1133 goto Finish;
1134 }
1135
1136 //
1137 // Get SectionAlignment
1138 //
1139 if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1140 //
1141 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
1142 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
1143 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
1144 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
1145 //
1146 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1147 } else {
1148 //
1149 // Get the magic value from the PE/COFF Optional Header
1150 //
1151 Magic = Hdr.Pe32->OptionalHeader.Magic;
1152 }
1153 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1154 SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment;
1155 } else {
1156 SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;
1157 }
1158
1159 SetPropertiesTableSectionAlignment (SectionAlignment);
1160 if ((SectionAlignment & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {
1161 DEBUG ((EFI_D_WARN, "!!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %dK !!!!!!!!\n",
1162 SectionAlignment, EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT >> 10));
1163 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);
1164 if (PdbPointer != NULL) {
1165 DEBUG ((EFI_D_WARN, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
1166 }
1167 goto Finish;
1168 }
1169
1170 Section = (EFI_IMAGE_SECTION_HEADER *) (
1171 (UINT8 *) (UINTN) ImageAddress +
1172 PeCoffHeaderOffset +
1173 sizeof(UINT32) +
1174 sizeof(EFI_IMAGE_FILE_HEADER) +
1175 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
1176 );
1177 ImageRecord->CodeSegmentCount = 0;
1178 InitializeListHead (&ImageRecord->CodeSegmentList);
1179 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
1180 Name = Section[Index].Name;
1181 DEBUG ((
1182 EFI_D_VERBOSE,
1183 " Section - '%c%c%c%c%c%c%c%c'\n",
1184 Name[0],
1185 Name[1],
1186 Name[2],
1187 Name[3],
1188 Name[4],
1189 Name[5],
1190 Name[6],
1191 Name[7]
1192 ));
1193
1194 if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) != 0) {
1195 DEBUG ((EFI_D_VERBOSE, " VirtualSize - 0x%08x\n", Section[Index].Misc.VirtualSize));
1196 DEBUG ((EFI_D_VERBOSE, " VirtualAddress - 0x%08x\n", Section[Index].VirtualAddress));
1197 DEBUG ((EFI_D_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[Index].SizeOfRawData));
1198 DEBUG ((EFI_D_VERBOSE, " PointerToRawData - 0x%08x\n", Section[Index].PointerToRawData));
1199 DEBUG ((EFI_D_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[Index].PointerToRelocations));
1200 DEBUG ((EFI_D_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[Index].PointerToLinenumbers));
1201 DEBUG ((EFI_D_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[Index].NumberOfRelocations));
1202 DEBUG ((EFI_D_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[Index].NumberOfLinenumbers));
1203 DEBUG ((EFI_D_VERBOSE, " Characteristics - 0x%08x\n", Section[Index].Characteristics));
1204
1205 //
1206 // Step 2: record code section
1207 //
1208 ImageRecordCodeSection = AllocatePool (sizeof(*ImageRecordCodeSection));
1209 if (ImageRecordCodeSection == NULL) {
1210 return ;
1211 }
1212 ImageRecordCodeSection->Signature = IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE;
1213
1214 ImageRecordCodeSection->CodeSegmentBase = (UINTN)ImageAddress + Section[Index].VirtualAddress;
1215 ImageRecordCodeSection->CodeSegmentSize = Section[Index].SizeOfRawData;
1216
1217 DEBUG ((EFI_D_VERBOSE, "ImageCode: 0x%016lx - 0x%016lx\n", ImageRecordCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize));
1218
1219 InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSection->Link);
1220 ImageRecord->CodeSegmentCount++;
1221 }
1222 }
1223
1224 if (ImageRecord->CodeSegmentCount == 0) {
1225 SetPropertiesTableSectionAlignment (1);
1226 DEBUG ((EFI_D_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount is 0 !!!!!!!!\n"));
1227 PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);
1228 if (PdbPointer != NULL) {
1229 DEBUG ((EFI_D_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer));
1230 }
1231 goto Finish;
1232 }
1233
1234 //
1235 // Final
1236 //
1237 SortImageRecordCodeSection (ImageRecord);
1238 //
1239 // Check overlap all section in ImageBase/Size
1240 //
1241 if (!IsImageRecordCodeSectionValid (ImageRecord)) {
1242 DEBUG ((EFI_D_ERROR, "IsImageRecordCodeSectionValid - FAIL\n"));
1243 goto Finish;
1244 }
1245
1246 InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageRecord->Link);
1247 mImagePropertiesPrivateData.ImageRecordCount++;
1248
1249 SortImageRecord ();
1250
1251 if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeSegmentCount) {
1252 mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;
1253 }
1254
1255 Finish:
1256 return ;
1257 }
1258
1259 /**
1260 Find image record according to image base and size.
1261
1262 @param ImageBase Base of PE image
1263 @param ImageSize Size of PE image
1264
1265 @return image record
1266 **/
1267 STATIC
1268 IMAGE_PROPERTIES_RECORD *
1269 FindImageRecord (
1270 IN EFI_PHYSICAL_ADDRESS ImageBase,
1271 IN UINT64 ImageSize
1272 )
1273 {
1274 IMAGE_PROPERTIES_RECORD *ImageRecord;
1275 LIST_ENTRY *ImageRecordLink;
1276 LIST_ENTRY *ImageRecordList;
1277
1278 ImageRecordList = &mImagePropertiesPrivateData.ImageRecordList;
1279
1280 for (ImageRecordLink = ImageRecordList->ForwardLink;
1281 ImageRecordLink != ImageRecordList;
1282 ImageRecordLink = ImageRecordLink->ForwardLink) {
1283 ImageRecord = CR (
1284 ImageRecordLink,
1285 IMAGE_PROPERTIES_RECORD,
1286 Link,
1287 IMAGE_PROPERTIES_RECORD_SIGNATURE
1288 );
1289
1290 if ((ImageBase == ImageRecord->ImageBase) &&
1291 (ImageSize == ImageRecord->ImageSize)) {
1292 return ImageRecord;
1293 }
1294 }
1295
1296 return NULL;
1297 }
1298
1299 /**
1300 Remove Image record.
1301
1302 @param RuntimeImage Runtime image information
1303 **/
1304 VOID
1305 RemoveImageRecord (
1306 IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage
1307 )
1308 {
1309 IMAGE_PROPERTIES_RECORD *ImageRecord;
1310 LIST_ENTRY *CodeSegmentListHead;
1311 IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;
1312
1313 DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage));
1314 DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));
1315
1316 ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize);
1317 if (ImageRecord == NULL) {
1318 DEBUG ((EFI_D_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));
1319 return ;
1320 }
1321
1322 CodeSegmentListHead = &ImageRecord->CodeSegmentList;
1323 while (!IsListEmpty (CodeSegmentListHead)) {
1324 ImageRecordCodeSection = CR (
1325 CodeSegmentListHead->ForwardLink,
1326 IMAGE_PROPERTIES_RECORD_CODE_SECTION,
1327 Link,
1328 IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE
1329 );
1330 RemoveEntryList (&ImageRecordCodeSection->Link);
1331 FreePool (ImageRecordCodeSection);
1332 }
1333
1334 RemoveEntryList (&ImageRecord->Link);
1335 FreePool (ImageRecord);
1336 mImagePropertiesPrivateData.ImageRecordCount--;
1337 }
1338
1339
1340 /**
1341 Install PropertiesTable.
1342
1343 @param[in] Event The Event this notify function registered to.
1344 @param[in] Context Pointer to the context data registered to the Event.
1345 **/
1346 VOID
1347 EFIAPI
1348 InstallPropertiesTable (
1349 EFI_EVENT Event,
1350 VOID *Context
1351 )
1352 {
1353 if (PcdGetBool (PcdPropertiesTableEnable)) {
1354 EFI_STATUS Status;
1355
1356 Status = gBS->InstallConfigurationTable (&gEfiPropertiesTableGuid, &mPropertiesTable);
1357 ASSERT_EFI_ERROR (Status);
1358
1359 DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", mPropertiesTable.MemoryProtectionAttribute));
1360 if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {
1361 DEBUG ((EFI_D_ERROR, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));
1362 DEBUG ((EFI_D_ERROR, "because Runtime Driver Section Alignment is not %dK.\n", EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT >> 10));
1363 return ;
1364 }
1365
1366 gBS->GetMemoryMap = CoreGetMemoryMapPropertiesTable;
1367 gBS->Hdr.CRC32 = 0;
1368 gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);
1369
1370 DEBUG ((EFI_D_VERBOSE, "Total Image Count - 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));
1371 DEBUG ((EFI_D_VERBOSE, "Dump ImageRecord:\n"));
1372 DumpImageRecord ();
1373
1374 mPropertiesTableEnable = TRUE;
1375 }
1376 }
1377
1378 /**
1379 Initialize PropertiesTable support.
1380 **/
1381 VOID
1382 EFIAPI
1383 CoreInitializePropertiesTable (
1384 VOID
1385 )
1386 {
1387 EFI_STATUS Status;
1388 EFI_EVENT EndOfDxeEvent;
1389
1390 Status = gBS->CreateEventEx (
1391 EVT_NOTIFY_SIGNAL,
1392 TPL_NOTIFY,
1393 InstallPropertiesTable,
1394 NULL,
1395 &gEfiEndOfDxeEventGroupGuid,
1396 &EndOfDxeEvent
1397 );
1398 ASSERT_EFI_ERROR (Status);
1399 return ;
1400 }