1 | |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2013 Jerry Hoemann <jerry.hoemann@hp.com> |
---|
5 | * |
---|
6 | * Application to allocate memory at EFI. Syntax of command |
---|
7 | * mimics the EFI Boot Service "FreePages." |
---|
8 | * |
---|
9 | * See UEFI spec 2.3, Section 6.2. |
---|
10 | * |
---|
11 | |
---|
12 | Example freeing a 5 page BS_Code setment at address: 0000000020000000 (hex) |
---|
13 | |
---|
14 | |
---|
15 | FS1:\> memmap |
---|
16 | Type Start End #pages Attributes |
---|
17 | BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F |
---|
18 | Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F |
---|
19 | Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F |
---|
20 | Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F |
---|
21 | Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F |
---|
22 | BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F |
---|
23 | Available 0000000010062000-000000001FFFFFFF 000000000000FF9E 000000000000000F |
---|
24 | BS_Code 0000000020000000-0000000020004FFF 0000000000000005 000000000000000F |
---|
25 | Available 0000000020005000-000000005DDFFFFF 000000000003DDFB 000000000000000F |
---|
26 | BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F |
---|
27 | Available 000000005E000000-000000006DE7CFFF 000000000000FE7D 000000000000000F |
---|
28 | ACPI_NVS 000000006DE7D000-000000006EE7CFFF 0000000000001000 000000000000000F |
---|
29 | BS_Data 000000006EE7D000-00000000709FBFFF 0000000000001B7F 000000000000000F |
---|
30 | Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F |
---|
31 | |
---|
32 | |
---|
33 | FS1:\> FreePages 0000000020000000 5 |
---|
34 | FreePages: __PhysAddr__ __PgCnt__ |
---|
35 | __PhysAddr__ 0... 3FFFFFFFFFFF |
---|
36 | __PgCnt__ [0..F000000] |
---|
37 | All numbers hex w/ no leading 0x |
---|
38 | |
---|
39 | FreePages(20000000,5) |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | FS1:\> memmap |
---|
44 | Type Start End #pages Attributes |
---|
45 | BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F |
---|
46 | Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F |
---|
47 | Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F |
---|
48 | Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F |
---|
49 | Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F |
---|
50 | BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F |
---|
51 | Available 0000000010062000-000000005DDFFFFF 000000000004DD9E 000000000000000F |
---|
52 | BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F |
---|
53 | Available 000000005E000000-000000006DE7CFFF 000000000000FE7D 000000000000000F |
---|
54 | ACPI_NVS 000000006DE7D000-000000006EE7CFFF 0000000000001000 000000000000000F |
---|
55 | BS_Data 000000006EE7D000-00000000709FBFFF 0000000000001B7F 000000000000000F |
---|
56 | Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F |
---|
57 | |
---|
58 | |
---|
59 | */ |
---|
60 | |
---|
61 | #include <efi.h> |
---|
62 | #include <efilib.h> |
---|
63 | #include <argify.h> |
---|
64 | |
---|
65 | /* |
---|
66 | * FreePages: __PhysAddr__ __PgCnt__ |
---|
67 | * |
---|
68 | */ |
---|
69 | |
---|
70 | #define MAX_NUM_PAGES 0x000000000F000000 |
---|
71 | |
---|
72 | #define MAX_ADDR ((1ULL << 46) - 1) |
---|
73 | |
---|
74 | #ifdef DEBUG |
---|
75 | #undef DEBUG |
---|
76 | #endif |
---|
77 | #define DEBUG 0 |
---|
78 | |
---|
79 | |
---|
80 | EFI_STATUS |
---|
81 | efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) |
---|
82 | { |
---|
83 | |
---|
84 | EFI_STATUS efi_status; |
---|
85 | EFI_GUID LoadedImageProtocol = LOADED_IMAGE_PROTOCOL; |
---|
86 | EFI_LOADED_IMAGE *info; |
---|
87 | |
---|
88 | CHAR16 arglist[MAX_ARGS+1] = {0}; |
---|
89 | CHAR16 *argv[MAX_ARGS]; |
---|
90 | INTN argc = 0; |
---|
91 | INTN err = 0; |
---|
92 | |
---|
93 | INTN PgCnt = -1; |
---|
94 | UINTN PhysAddr = 0; |
---|
95 | |
---|
96 | InitializeLib(image, systab); |
---|
97 | |
---|
98 | efi_status = uefi_call_wrapper( BS->HandleProtocol, 3, image, |
---|
99 | &LoadedImageProtocol, &info); |
---|
100 | |
---|
101 | |
---|
102 | Print(L"FreePages: __PhysAddr__ __PgCnt__\n"); |
---|
103 | Print(L"__PhysAddr__ 0... %llx\n", MAX_ADDR); |
---|
104 | Print(L"__PgCnt__ [0..%lx]\n", MAX_NUM_PAGES); |
---|
105 | Print(L"All numbers hex w/ no leading 0x\n"); |
---|
106 | Print(L"\n"); |
---|
107 | |
---|
108 | #if DEBUG |
---|
109 | Print(L"%s\n", info->LoadOptions); |
---|
110 | #endif |
---|
111 | |
---|
112 | |
---|
113 | #if DEBUG |
---|
114 | Print(L"Set up arglist\n"); |
---|
115 | #endif |
---|
116 | CopyMem(arglist, info->LoadOptions, info->LoadOptionsSize); |
---|
117 | #if DEBUG |
---|
118 | Print(L"arglist = <%s>\n", arglist); |
---|
119 | #endif |
---|
120 | |
---|
121 | #if DEBUG |
---|
122 | Print(L"Now try argify\n"); |
---|
123 | #endif |
---|
124 | argc = argify(arglist, info->LoadOptionsSize, argv); |
---|
125 | #if DEBUG |
---|
126 | Print(L"argc = %d\n", argc); |
---|
127 | #endif |
---|
128 | |
---|
129 | #if DEBUG |
---|
130 | for (c = 0; c < argc; c++ ) { |
---|
131 | Print(L"argv[%d] = <%s>\n", c, argv[c]); |
---|
132 | } |
---|
133 | #endif |
---|
134 | if (argc != 3) { |
---|
135 | Print(L"Invalid argument count\n"); |
---|
136 | return EFI_SUCCESS; |
---|
137 | } |
---|
138 | |
---|
139 | PhysAddr = xtoi(argv[1]); |
---|
140 | PgCnt = xtoi(argv[2]); |
---|
141 | |
---|
142 | if ( (PgCnt < 0) || (PgCnt > MAX_NUM_PAGES) ) { |
---|
143 | Print(L"Inavlid PgCnt\n"); |
---|
144 | err++; |
---|
145 | } |
---|
146 | if ( PhysAddr > MAX_ADDR ) { |
---|
147 | Print(L"Inavlid Address\n"); |
---|
148 | err++; |
---|
149 | } |
---|
150 | if ( err ) { |
---|
151 | return EFI_SUCCESS; |
---|
152 | } |
---|
153 | |
---|
154 | Print(L"FreePages(%lx,%d)\n", PhysAddr, PgCnt); |
---|
155 | |
---|
156 | efi_status = uefi_call_wrapper(BS->FreePages, 2, PhysAddr, PgCnt); |
---|
157 | |
---|
158 | if ( EFI_ERROR(efi_status) ) { |
---|
159 | Print(L"Free Pages Failed: %d\n", efi_status); |
---|
160 | return efi_status; |
---|
161 | } |
---|
162 | |
---|
163 | return EFI_SUCCESS; |
---|
164 | } |
---|