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
64 /*
65 * FreePages: __PhysAddr__ __PgCnt__
66 *
67 */
68
69 #define MAX_NUM_PAGES 0x000000000F000000
70
71 #define MAX_ADDR ((1ULL << 46) - 1)
72
73 #ifdef DEBUG
74 #undef DEBUG
75 #endif
76 #define DEBUG 0
77
78
79 EFI_STATUS
efi_main(EFI_HANDLE image,EFI_SYSTEM_TABLE * systab)80 efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
81 {
82
83 EFI_STATUS efi_status;
84 CHAR16 **argv;
85 INTN argc = 0;
86 #if DEBUG
87 INTN c = 0;
88 #endif
89 INTN err = 0;
90
91 INTN PgCnt = -1;
92 EFI_PHYSICAL_ADDRESS PhysAddr = 0;
93
94 InitializeLib(image, systab);
95
96 Print(L"FreePages: __PhysAddr__ __PgCnt__\n");
97 Print(L"__PhysAddr__ 0... %llx\n", MAX_ADDR);
98 Print(L"__PgCnt__ [0..%lx]\n", MAX_NUM_PAGES);
99 Print(L"All numbers hex w/ no leading 0x\n");
100 Print(L"\n");
101
102 #if DEBUG
103 Print(L"Now parse argc/argv\n");
104 #endif
105 argc = GetShellArgcArgv(image, &argv);
106 #if DEBUG
107 Print(L"argc = %d\n", argc);
108 #endif
109
110 #if DEBUG
111 for (c = 0; c < argc; c++ ) {
112 Print(L"argv[%d] = <%s>\n", c, argv[c]);
113 }
114 #endif
115 if (argc != 3) {
116 Print(L"Invalid argument count\n");
117 return EFI_SUCCESS;
118 }
119
120 PhysAddr = xtoi(argv[1]);
121 PgCnt = xtoi(argv[2]);
122
123 if ( (PgCnt < 0) || (PgCnt > MAX_NUM_PAGES) ) {
124 Print(L"Inavlid PgCnt\n");
125 err++;
126 }
127 if ( PhysAddr > MAX_ADDR ) {
128 Print(L"Inavlid Address\n");
129 err++;
130 }
131 if ( err ) {
132 return EFI_SUCCESS;
133 }
134
135 Print(L"FreePages(%lx,%d)\n", PhysAddr, PgCnt);
136
137 efi_status = uefi_call_wrapper(BS->FreePages, 2, PhysAddr, PgCnt);
138
139 if ( EFI_ERROR(efi_status) ) {
140 Print(L"Free Pages Failed: %d\n", efi_status);
141 return efi_status;
142 }
143
144 return EFI_SUCCESS;
145 }
146