]> git.proxmox.com Git - mirror_edk2.git/blob - PrmPkg/Samples/PrmSampleMemoryAllocationModule/PrmSampleMemoryAllocationModule.c
PrmPkg: Add initial PrmSampleMemoryAllocationModule
[mirror_edk2.git] / PrmPkg / Samples / PrmSampleMemoryAllocationModule / PrmSampleMemoryAllocationModule.c
1 /** @file
2
3 A sample PRM Module implementation. This PRM Module provides 3 PRM handlers that simply take a DEBUG print
4 function from the OS and invoke it with a debug message internal the PRM handler.
5
6 Copyright (c) Microsoft Corporation
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PrmModule.h>
12
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/PrintLib.h>
16 #include <Library/UefiLib.h>
17
18 //
19 // PRM Handler GUIDs
20 //
21
22 // {149a5cb3-6a9c-403f-940a-156abf63938a}
23 #define PRM_HANDLER_1_GUID {0x149a5cb3, 0x6a9c, 0x403f, {0x94, 0x0a, 0x15, 0x6a, 0xbf, 0x63, 0x93, 0x8a}}
24
25 // Note: If the signature size is modified, the PRM Handler test code in this module needs to be updated.
26 #define MEMORY_ALLOCATION_TEST_DATA_SIGNATURE SIGNATURE_32('T','E','S','T')
27 #define MEMORY_ALLOCATION_TEST_DATA_SIZE sizeof(UINT32)
28 #define MEMORY_ALLOCATION_TEST_DATA_BUFFER_SIZE 256
29
30 /**
31 A sample Platform Runtime Mechanism (PRM) handler.
32
33 This sample handler currently uses the OS_SERVICES to write a debug message
34 indicating this is PRM handler 1.
35
36 @param[in] ParameterBuffer A pointer to the PRM handler parameter buffer
37 @param[in] ContextBuffer A pointer to the PRM handler context buffer
38
39 @retval EFI_STATUS The PRM handler executed successfully.
40 @retval Others An error occurred in the PRM handler.
41
42 **/
43 EFI_STATUS
44 PRM_EXPORT_API
45 EFIAPI
46 PrmHandler1 (
47 IN VOID *ParameterBuffer,
48 IN PRM_CONTEXT_BUFFER *ContextBUffer
49 )
50 {
51 EFI_STATUS Status;
52 UINTN Index;
53 VOID *NonPagedPool;
54 CHAR8 DebugMessage[256];
55
56 if (OsServices == NULL || OsServices->DebugPrint == NULL || OsServices->AllocateMemory == NULL) {
57 return EFI_INVALID_PARAMETER;
58 }
59
60 OsServices->DebugPrint ("Memory Allocation PrmHandler1 entry.\n");
61 OsServices->DebugPrint (" Requesting allocation of a 256 byte non-paged pool...\n");
62
63 NonPagedPool = NULL;
64 NonPagedPool = OsServices->AllocateMemory (MEMORY_ALLOCATION_TEST_DATA_BUFFER_SIZE, FALSE);
65 if (NonPagedPool == NULL) {
66 OsServices->DebugPrint (" NULL was returned from AllocateMemory()...\n");
67 return EFI_OUT_OF_RESOURCES;
68 }
69
70 AsciiSPrint (
71 &DebugMessage[0],
72 ARRAY_SIZE (DebugMessage),
73 " Buffer address returned from AllocateMemory() = 0x%016lx.\n",
74 (UINTN) NonPagedPool
75 );
76 OsServices->DebugPrint (&DebugMessage[0]);
77
78 // Write the test data
79 OsServices->DebugPrint (" Beginning memory buffer write and read back test...\n");
80 SetMem32 (NonPagedPool, MEMORY_ALLOCATION_TEST_DATA_BUFFER_SIZE, MEMORY_ALLOCATION_TEST_DATA_SIGNATURE);
81
82 // Read back and verify the test data is valid
83 for (Index = 0, Status = EFI_SUCCESS; Index < (MEMORY_ALLOCATION_TEST_DATA_BUFFER_SIZE / MEMORY_ALLOCATION_TEST_DATA_SIZE); Index++) {
84 if (((UINT32 *) NonPagedPool)[Index] != MEMORY_ALLOCATION_TEST_DATA_SIGNATURE) {
85 Status = EFI_DEVICE_ERROR;
86 break;
87 }
88 }
89 if (EFI_ERROR (Status)) {
90 OsServices->DebugPrint (" Memory write & read test failed.\n");
91 } else {
92 OsServices->DebugPrint (" Memory write & read test passed.\n");
93 }
94
95 OsServices->DebugPrint ("Memory Allocation PrmHandler1 exit.\n");
96
97 return EFI_SUCCESS;
98 }
99
100 //
101 // Register the PRM export information for this PRM Module
102 //
103 PRM_MODULE_EXPORT (
104 PRM_HANDLER_EXPORT_ENTRY (PRM_HANDLER_1_GUID, PrmHandler1)
105 );
106
107 EFI_STATUS
108 EFIAPI
109 PrmSampleMemoryAllocationModuleInit (
110 IN EFI_HANDLE ImageHandle,
111 IN EFI_SYSTEM_TABLE *SystemTable
112 )
113 {
114 return EFI_SUCCESS;
115 }