]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
ArmPlatformPkg: Fixed memory leak after calling GetEnvironmentVariable()
[mirror_edk2.git] / EmbeddedPkg / Application / AndroidFastboot / Arm / BootAndroidBootImg.c
1 /** @file
2
3 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
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 "AndroidFastbootApp.h"
16
17 #include <Protocol/DevicePath.h>
18
19 #include <Library/BdsLib.h>
20 #include <Library/DevicePathLib.h>
21
22 #include <Guid/ArmGlobalVariableHob.h>
23
24 // Device Path representing an image in memory
25 #pragma pack(1)
26 typedef struct {
27 MEMMAP_DEVICE_PATH Node1;
28 EFI_DEVICE_PATH_PROTOCOL End;
29 } MEMORY_DEVICE_PATH;
30 #pragma pack()
31
32 STATIC CONST MEMORY_DEVICE_PATH MemoryDevicePathTemplate =
33 {
34 {
35 {
36 HARDWARE_DEVICE_PATH,
37 HW_MEMMAP_DP,
38 {
39 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
40 (UINT8)((sizeof (MEMMAP_DEVICE_PATH)) >> 8),
41 },
42 }, // Header
43 0, // StartingAddress (set at runtime)
44 0 // EndingAddress (set at runtime)
45 }, // Node1
46 {
47 END_DEVICE_PATH_TYPE,
48 END_ENTIRE_DEVICE_PATH_SUBTYPE,
49 sizeof (EFI_DEVICE_PATH_PROTOCOL),
50 0
51 } // End
52 };
53
54 EFI_STATUS
55 BootAndroidBootImg (
56 IN UINTN BufferSize,
57 IN VOID *Buffer
58 )
59 {
60 EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;
61 EFI_STATUS Status;
62 CHAR8 KernelArgs[BOOTIMG_KERNEL_ARGS_SIZE];
63 VOID *Kernel;
64 UINTN KernelSize;
65 VOID *Ramdisk;
66 UINTN RamdiskSize;
67 MEMORY_DEVICE_PATH KernelDevicePath;
68 MEMORY_DEVICE_PATH* RamdiskDevicePath;
69
70 Status = ParseAndroidBootImg (
71 Buffer,
72 &Kernel,
73 &KernelSize,
74 &Ramdisk,
75 &RamdiskSize,
76 KernelArgs
77 );
78 if (EFI_ERROR (Status)) {
79 return Status;
80 }
81
82 KernelDevicePath = MemoryDevicePathTemplate;
83
84 // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to
85 // appease GCC.
86 KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel;
87 KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize;
88
89 RamdiskDevicePath = NULL;
90 if (RamdiskSize != 0) {
91 RamdiskDevicePath = (MEMORY_DEVICE_PATH*)DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL*) &MemoryDevicePathTemplate);
92
93 RamdiskDevicePath->Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk;
94 RamdiskDevicePath->Node1.EndingAddress = ((EFI_PHYSICAL_ADDRESS)(UINTN) Ramdisk) + RamdiskSize;
95 }
96
97 // Get the default FDT device path
98 Status = GetEnvironmentVariable ((CHAR16 *)L"Fdt", &gArmGlobalVariableGuid,
99 NULL, 0, (VOID **)&FdtDevicePath);
100 if (Status == EFI_NOT_FOUND) {
101 DEBUG ((EFI_D_ERROR, "Error: Please update FDT path in boot manager\n"));
102 return EFI_DEVICE_ERROR;
103 }
104 ASSERT_EFI_ERROR (Status);
105
106 Status = BdsBootLinuxFdt (
107 (EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath,
108 (EFI_DEVICE_PATH_PROTOCOL *) RamdiskDevicePath,
109 KernelArgs,
110 FdtDevicePath
111 );
112 if (EFI_ERROR (Status)) {
113 DEBUG ((EFI_D_ERROR, "Couldn't Boot Linux: %d\n", Status));
114 return EFI_DEVICE_ERROR;
115 }
116
117 if (RamdiskDevicePath) {
118 FreePool (RamdiskDevicePath);
119 }
120
121 FreePool (FdtDevicePath);
122
123 // If we got here we do a confused face because BootLinuxFdt returned,
124 // reporting success.
125 DEBUG ((EFI_D_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n"));
126 return EFI_SUCCESS;
127 }