]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Bds/BootOption.c
ArmPlatformPkg: Fixed memory leak after calling GetEnvironmentVariable()
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootOption.c
1 /** @file
2 *
3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 *
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 <Guid/ArmGlobalVariableHob.h>
16 #include "BdsInternal.h"
17
18 extern EFI_HANDLE mImageHandle;
19
20 EFI_STATUS
21 BootOptionStart (
22 IN BDS_LOAD_OPTION *BootOption
23 )
24 {
25 EFI_STATUS Status;
26 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL* EfiDevicePathFromTextProtocol;
27 UINT32 LoaderType;
28 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;
29 ARM_BDS_LINUX_ARGUMENTS* LinuxArguments;
30 EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath;
31 EFI_DEVICE_PATH_PROTOCOL* DefaultFdtDevicePath;
32 UINTN FdtDevicePathSize;
33 UINTN CmdLineSize;
34 UINTN InitrdSize;
35 EFI_DEVICE_PATH* Initrd;
36 UINT16 LoadOptionIndexSize;
37
38 if (IS_ARM_BDS_BOOTENTRY (BootOption)) {
39 Status = EFI_UNSUPPORTED;
40 OptionalData = BootOption->OptionalData;
41 LoaderType = ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);
42
43 if (LoaderType == BDS_LOADER_EFI_APPLICATION) {
44 // Need to connect every drivers to ensure no dependencies are missing for the application
45 BdsConnectAllDrivers();
46
47 Status = BdsStartEfiApplication (mImageHandle, BootOption->FilePathList, 0, NULL);
48 } else if (LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) {
49 LinuxArguments = &(OptionalData->Arguments.LinuxArguments);
50 CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);
51 InitrdSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->InitrdSize);
52
53 if (InitrdSize > 0) {
54 Initrd = GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize));
55 } else {
56 Initrd = NULL;
57 }
58
59 Status = BdsBootLinuxAtag (BootOption->FilePathList,
60 Initrd, // Initrd
61 (CHAR8*)(LinuxArguments + 1)); // CmdLine
62 } else if (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT) {
63 LinuxArguments = &(OptionalData->Arguments.LinuxArguments);
64 CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);
65 InitrdSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->InitrdSize);
66
67 if (InitrdSize > 0) {
68 Initrd = GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize));
69 } else {
70 Initrd = NULL;
71 }
72
73 // Get the default FDT device path
74 Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
75 ASSERT_EFI_ERROR(Status);
76 DefaultFdtDevicePath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdFdtDevicePath));
77
78 // Get the FDT device path
79 FdtDevicePathSize = GetDevicePathSize (DefaultFdtDevicePath);
80 Status = GetEnvironmentVariable ((CHAR16 *)L"Fdt", &gArmGlobalVariableGuid,
81 DefaultFdtDevicePath, &FdtDevicePathSize, (VOID **)&FdtDevicePath);
82 ASSERT_EFI_ERROR(Status);
83
84 Status = BdsBootLinuxFdt (BootOption->FilePathList,
85 Initrd, // Initrd
86 (CHAR8*)(LinuxArguments + 1),
87 FdtDevicePath);
88
89 FreePool (DefaultFdtDevicePath);
90 FreePool (FdtDevicePath);
91 }
92 } else {
93 // Set BootCurrent variable
94 LoadOptionIndexSize = sizeof(UINT16);
95 gRT->SetVariable (L"BootCurrent", &gEfiGlobalVariableGuid,
96 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
97 LoadOptionIndexSize, &(BootOption->LoadOptionIndex));
98
99 Status = BdsStartEfiApplication (mImageHandle, BootOption->FilePathList, BootOption->OptionalDataSize, BootOption->OptionalData);
100
101 // Clear BootCurrent variable
102 LoadOptionIndexSize = sizeof(UINT16);
103 gRT->SetVariable (L"BootCurrent", &gEfiGlobalVariableGuid,
104 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
105 0, NULL);
106 }
107
108 return Status;
109 }
110
111 EFI_STATUS
112 BootOptionList (
113 IN OUT LIST_ENTRY *BootOptionList
114 )
115 {
116 EFI_STATUS Status;
117 UINTN Index;
118 UINT16* BootOrder;
119 UINTN BootOrderSize;
120 BDS_LOAD_OPTION* BdsLoadOption;
121 BDS_LOAD_OPTION_ENTRY* BdsLoadOptionEntry;
122
123 InitializeListHead (BootOptionList);
124
125 // Get the Boot Option Order from the environment variable
126 Status = GetGlobalEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
127 if (EFI_ERROR(Status)) {
128 return Status;
129 }
130
131 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
132 Status = BootOptionFromLoadOptionIndex (BootOrder[Index], &BdsLoadOption);
133 if (!EFI_ERROR(Status)) {
134 BdsLoadOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool(sizeof(BDS_LOAD_OPTION_ENTRY));
135 BdsLoadOptionEntry->BdsLoadOption = BdsLoadOption;
136 InsertTailList (BootOptionList,&BdsLoadOptionEntry->Link);
137 }
138 }
139
140 FreePool (BootOrder);
141
142 return EFI_SUCCESS;
143 }
144
145 STATIC
146 EFI_STATUS
147 BootOptionSetFields (
148 IN BDS_LOAD_OPTION* BootOption,
149 IN UINT32 Attributes,
150 IN CHAR16* BootDescription,
151 IN EFI_DEVICE_PATH_PROTOCOL* DevicePath,
152 IN ARM_BDS_LOADER_TYPE BootType,
153 IN ARM_BDS_LOADER_ARGUMENTS* BootArguments
154 )
155 {
156 EFI_LOAD_OPTION EfiLoadOption;
157 UINTN EfiLoadOptionSize;
158 UINTN BootDescriptionSize;
159 UINTN BootOptionalDataSize;
160 UINT16 FilePathListLength;
161 UINT8* EfiLoadOptionPtr;
162 UINT8* InitrdPathListPtr;
163 UINTN OptionalDataSize;
164 ARM_BDS_LINUX_ARGUMENTS* DestLinuxArguments;
165 ARM_BDS_LINUX_ARGUMENTS* SrcLinuxArguments;
166
167 // If we are overwriting an existent Boot Option then we have to free previously allocated memory
168 if (BootOption->LoadOption) {
169 FreePool(BootOption->LoadOption);
170 }
171
172 BootDescriptionSize = StrSize (BootDescription);
173 BootOptionalDataSize = sizeof(ARM_BDS_LOADER_OPTIONAL_DATA_HEADER);
174 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
175 BootOptionalDataSize += sizeof(ARM_BDS_LINUX_ARGUMENTS) + BootArguments->LinuxArguments.CmdLineSize + BootArguments->LinuxArguments.InitrdSize;
176 }
177
178 // Compute the size of the FilePath list
179 FilePathListLength = GetUnalignedDevicePathSize (DevicePath);
180
181 // Allocate the memory for the EFI Load Option
182 EfiLoadOptionSize = sizeof(UINT32) + sizeof(UINT16) + BootDescriptionSize + FilePathListLength + BootOptionalDataSize;
183 EfiLoadOption = (EFI_LOAD_OPTION)AllocatePool(EfiLoadOptionSize);
184 EfiLoadOptionPtr = EfiLoadOption;
185
186 //
187 // Populate the EFI Load Option and BDS Boot Option structures
188 //
189
190 // Attributes fields
191 BootOption->Attributes = Attributes;
192 *(UINT32*)EfiLoadOptionPtr = Attributes;
193 EfiLoadOptionPtr += sizeof(UINT32);
194
195 // FilePath List fields
196 BootOption->FilePathListLength = FilePathListLength;
197 *(UINT16*)EfiLoadOptionPtr = FilePathListLength;
198 EfiLoadOptionPtr += sizeof(UINT16);
199
200 // Boot description fields
201 BootOption->Description = (CHAR16*)EfiLoadOptionPtr;
202 CopyMem (EfiLoadOptionPtr, BootDescription, BootDescriptionSize);
203 EfiLoadOptionPtr += BootDescriptionSize;
204
205 // File path fields
206 BootOption->FilePathList = (EFI_DEVICE_PATH_PROTOCOL*)EfiLoadOptionPtr;
207 CopyMem (EfiLoadOptionPtr, DevicePath, FilePathListLength);
208 EfiLoadOptionPtr += FilePathListLength;
209
210 // Optional Data fields, Do unaligned writes
211 BootOption->OptionalData = EfiLoadOptionPtr;
212 WriteUnaligned32 ((UINT32 *)EfiLoadOptionPtr, ARM_BDS_OPTIONAL_DATA_SIGNATURE);
213 WriteUnaligned32 ((UINT32 *)(EfiLoadOptionPtr + 4), BootType);
214
215 OptionalDataSize = sizeof(ARM_BDS_LOADER_OPTIONAL_DATA_HEADER);
216
217 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
218 SrcLinuxArguments = &(BootArguments->LinuxArguments);
219 DestLinuxArguments = &((ARM_BDS_LOADER_OPTIONAL_DATA*)EfiLoadOptionPtr)->Arguments.LinuxArguments;
220
221 WriteUnaligned16 ((UINT16 *)&(DestLinuxArguments->CmdLineSize), SrcLinuxArguments->CmdLineSize);
222 WriteUnaligned16 ((UINT16 *)&(DestLinuxArguments->InitrdSize), SrcLinuxArguments->InitrdSize);
223 OptionalDataSize += sizeof (ARM_BDS_LINUX_ARGUMENTS);
224
225 if (SrcLinuxArguments->CmdLineSize > 0) {
226 CopyMem ((VOID*)(DestLinuxArguments + 1), (VOID*)(SrcLinuxArguments + 1), SrcLinuxArguments->CmdLineSize);
227 OptionalDataSize += SrcLinuxArguments->CmdLineSize;
228 }
229
230 if (SrcLinuxArguments->InitrdSize > 0) {
231 InitrdPathListPtr = (UINT8*)((UINTN)(DestLinuxArguments + 1) + SrcLinuxArguments->CmdLineSize);
232 CopyMem (InitrdPathListPtr, (VOID*)((UINTN)(SrcLinuxArguments + 1) + SrcLinuxArguments->CmdLineSize), SrcLinuxArguments->InitrdSize);
233 }
234 }
235 BootOption->OptionalDataSize = OptionalDataSize;
236
237 // If this function is called at the creation of the Boot Device entry (not at the update) the
238 // BootOption->LoadOptionSize must be zero then we get a new BootIndex for this entry
239 if (BootOption->LoadOptionSize == 0) {
240 BootOption->LoadOptionIndex = BootOptionAllocateBootIndex ();
241 }
242
243 // Fill the EFI Load option fields
244 BootOption->LoadOption = EfiLoadOption;
245 BootOption->LoadOptionSize = EfiLoadOptionSize;
246
247 return EFI_SUCCESS;
248 }
249
250 EFI_STATUS
251 BootOptionCreate (
252 IN UINT32 Attributes,
253 IN CHAR16* BootDescription,
254 IN EFI_DEVICE_PATH_PROTOCOL* DevicePath,
255 IN ARM_BDS_LOADER_TYPE BootType,
256 IN ARM_BDS_LOADER_ARGUMENTS* BootArguments,
257 OUT BDS_LOAD_OPTION** BdsLoadOption
258 )
259 {
260 EFI_STATUS Status;
261 BDS_LOAD_OPTION_ENTRY* BootOptionEntry;
262 BDS_LOAD_OPTION* BootOption;
263 CHAR16 BootVariableName[9];
264 UINT16* BootOrder;
265 UINTN BootOrderSize;
266
267 //
268 // Allocate and fill the memory for the BDS Load Option structure
269 //
270 BootOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool (sizeof (BDS_LOAD_OPTION_ENTRY));
271 InitializeListHead (&BootOptionEntry->Link);
272 BootOptionEntry->BdsLoadOption = (BDS_LOAD_OPTION*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION));
273
274 BootOption = BootOptionEntry->BdsLoadOption;
275 BootOptionSetFields (BootOption, Attributes, BootDescription, DevicePath, BootType, BootArguments);
276
277 //
278 // Set the related environment variables
279 //
280
281 // Create Boot#### environment variable
282 UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", BootOption->LoadOptionIndex);
283 Status = gRT->SetVariable (
284 BootVariableName,
285 &gEfiGlobalVariableGuid,
286 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
287 BootOption->LoadOptionSize,
288 BootOption->LoadOption
289 );
290
291 // Add the new Boot Index to the list
292 Status = GetGlobalEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
293 if (!EFI_ERROR(Status)) {
294 BootOrder = ReallocatePool (BootOrderSize, BootOrderSize + sizeof(UINT16), BootOrder);
295 // Add the new index at the end
296 BootOrder[BootOrderSize / sizeof(UINT16)] = BootOption->LoadOptionIndex;
297 BootOrderSize += sizeof(UINT16);
298 } else {
299 // BootOrder does not exist. Create it
300 BootOrderSize = sizeof(UINT16);
301 BootOrder = &(BootOption->LoadOptionIndex);
302 }
303
304 // Update (or Create) the BootOrder environment variable
305 Status = gRT->SetVariable (
306 L"BootOrder",
307 &gEfiGlobalVariableGuid,
308 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
309 BootOrderSize,
310 BootOrder
311 );
312
313 // We only free it if the UEFI Variable 'BootOrder' was already existing
314 if (BootOrderSize > sizeof(UINT16)) {
315 FreePool (BootOrder);
316 }
317
318 *BdsLoadOption = BootOption;
319 return Status;
320 }
321
322 EFI_STATUS
323 BootOptionUpdate (
324 IN BDS_LOAD_OPTION* BdsLoadOption,
325 IN UINT32 Attributes,
326 IN CHAR16* BootDescription,
327 IN EFI_DEVICE_PATH_PROTOCOL* DevicePath,
328 IN ARM_BDS_LOADER_TYPE BootType,
329 IN ARM_BDS_LOADER_ARGUMENTS* BootArguments
330 )
331 {
332 EFI_STATUS Status;
333 CHAR16 BootVariableName[9];
334
335 // Update the BDS Load Option structure
336 BootOptionSetFields (BdsLoadOption, Attributes, BootDescription, DevicePath, BootType, BootArguments);
337
338 // Update the related environment variables
339 UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", BdsLoadOption->LoadOptionIndex);
340
341 Status = gRT->SetVariable (
342 BootVariableName,
343 &gEfiGlobalVariableGuid,
344 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
345 BdsLoadOption->LoadOptionSize,
346 BdsLoadOption->LoadOption
347 );
348
349 return Status;
350 }
351
352 EFI_STATUS
353 BootOptionDelete (
354 IN BDS_LOAD_OPTION *BootOption
355 )
356 {
357 UINTN Index;
358 UINTN BootOrderSize;
359 UINT16* BootOrder;
360 UINTN BootOrderCount;
361 EFI_STATUS Status;
362
363 // Remove the entry from the BootOrder environment variable
364 Status = GetGlobalEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
365 if (!EFI_ERROR(Status)) {
366 BootOrderCount = BootOrderSize / sizeof(UINT16);
367
368 // Find the index of the removed entry
369 for (Index = 0; Index < BootOrderCount; Index++) {
370 if (BootOrder[Index] == BootOption->LoadOptionIndex) {
371 // If it the last entry we do not need to rearrange the BootOrder list
372 if (Index + 1 != BootOrderCount) {
373 CopyMem (
374 &BootOrder[Index],
375 &BootOrder[Index + 1],
376 (BootOrderCount - (Index + 1)) * sizeof(UINT16)
377 );
378 }
379 break;
380 }
381 }
382
383 // Update the BootOrder environment variable
384 Status = gRT->SetVariable (
385 L"BootOrder",
386 &gEfiGlobalVariableGuid,
387 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
388 BootOrderSize - sizeof(UINT16),
389 BootOrder
390 );
391 }
392
393 FreePool (BootOrder);
394
395 return EFI_SUCCESS;
396 }