1bfda055 |
1 | /** @file |
2 | * |
995d9676 |
3 | * Copyright (c) 2011-2012, ARM Limited. All rights reserved. |
1bfda055 |
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 "BdsInternal.h" |
b34e4db3 |
16 | #include "BdsLinuxLoader.h" |
1bfda055 |
17 | |
a355a365 |
18 | #define ALIGN32_BELOW(addr) ALIGN_POINTER(addr - 32,32) |
1bfda055 |
19 | |
1bfda055 |
20 | STATIC |
21 | EFI_STATUS |
a355a365 |
22 | PreparePlatformHardware ( |
23 | VOID |
24 | ) |
1bfda055 |
25 | { |
26 | //Note: Interrupts will be disabled by the GIC driver when ExitBootServices() will be called. |
27 | |
76d17c31 |
28 | // Clean, invalidate, disable data cache |
1bfda055 |
29 | ArmDisableDataCache(); |
ed71a22c |
30 | ArmCleanInvalidateDataCache(); |
1bfda055 |
31 | |
32 | // Invalidate and disable the Instruction cache |
1bfda055 |
33 | ArmDisableInstructionCache (); |
ed71a22c |
34 | ArmInvalidateInstructionCache (); |
1bfda055 |
35 | |
76d17c31 |
36 | // Turn off MMU |
1bfda055 |
37 | ArmDisableMmu(); |
38 | |
39 | return EFI_SUCCESS; |
40 | } |
41 | |
76d17c31 |
42 | STATIC |
a355a365 |
43 | EFI_STATUS |
76d17c31 |
44 | StartLinux ( |
45 | IN EFI_PHYSICAL_ADDRESS LinuxImage, |
46 | IN UINTN LinuxImageSize, |
47 | IN EFI_PHYSICAL_ADDRESS KernelParamsAddress, |
48 | IN UINTN KernelParamsSize, |
49 | IN UINT32 MachineType |
a355a365 |
50 | ) |
51 | { |
52 | EFI_STATUS Status; |
a355a365 |
53 | LINUX_KERNEL LinuxKernel; |
1bfda055 |
54 | |
a355a365 |
55 | // Shut down UEFI boot services. ExitBootServices() will notify every driver that created an event on |
56 | // ExitBootServices event. Example the Interrupt DXE driver will disable the interrupts on this event. |
57 | Status = ShutdownUefiBootServices (); |
58 | if(EFI_ERROR(Status)) { |
59 | DEBUG((EFI_D_ERROR,"ERROR: Can not shutdown UEFI boot services. Status=0x%X\n", Status)); |
60 | goto Exit; |
61 | } |
1bfda055 |
62 | |
a355a365 |
63 | // Move the kernel parameters to any address inside the first 1MB. |
64 | // This is necessary because the ARM Linux kernel requires |
65 | // the FTD / ATAG List to reside entirely inside the first 1MB of |
66 | // physical memory. |
67 | if ((UINTN)KernelParamsAddress > LINUX_ATAG_MAX_OFFSET) { |
68 | //Note: There is no requirement on the alignment |
1c1e70fa |
69 | KernelParamsAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)CopyMem (ALIGN32_BELOW(LINUX_ATAG_MAX_OFFSET - KernelParamsSize), (VOID*)(UINTN)KernelParamsAddress, KernelParamsSize); |
a355a365 |
70 | } |
1bfda055 |
71 | |
a355a365 |
72 | if ((UINTN)LinuxImage > LINUX_KERNEL_MAX_OFFSET) { |
73 | //Note: There is no requirement on the alignment |
74 | LinuxKernel = (LINUX_KERNEL)CopyMem (ALIGN32_BELOW(LINUX_KERNEL_MAX_OFFSET - LinuxImageSize), (VOID*)(UINTN)LinuxImage, LinuxImageSize); |
76d17c31 |
75 | } else { |
76 | LinuxKernel = (LINUX_KERNEL)(UINTN)LinuxImage; |
a355a365 |
77 | } |
1bfda055 |
78 | |
995d9676 |
79 | // Check if the Linux Image is a uImage |
80 | if (*(UINT32*)LinuxKernel == LINUX_UIMAGE_SIGNATURE) { |
81 | // Assume the Image Entry Point is just after the uImage header (64-byte size) |
82 | LinuxKernel = (LINUX_KERNEL)((UINTN)LinuxKernel + 64); |
1093e307 |
83 | LinuxImageSize -= 64; |
995d9676 |
84 | } |
85 | |
a355a365 |
86 | //TODO: Check there is no overlapping between kernel and Atag |
1bfda055 |
87 | |
a355a365 |
88 | // |
89 | // Switch off interrupts, caches, mmu, etc |
90 | // |
91 | Status = PreparePlatformHardware (); |
92 | ASSERT_EFI_ERROR(Status); |
1bfda055 |
93 | |
a355a365 |
94 | // Register and print out performance information |
95 | PERF_END (NULL, "BDS", NULL, 0); |
96 | if (PerformanceMeasurementEnabled ()) { |
97 | PrintPerformance (); |
98 | } |
1bfda055 |
99 | |
a355a365 |
100 | // |
101 | // Start the Linux Kernel |
102 | // |
1bfda055 |
103 | |
a355a365 |
104 | // Outside BootServices, so can't use Print(); |
105 | DEBUG((EFI_D_ERROR, "\nStarting the kernel:\n\n")); |
1bfda055 |
106 | |
995d9676 |
107 | // Jump to kernel with register set |
76d17c31 |
108 | LinuxKernel ((UINTN)0, MachineType, (UINTN)KernelParamsAddress); |
1bfda055 |
109 | |
a355a365 |
110 | // Kernel should never exit |
111 | // After Life services are not provided |
112 | ASSERT(FALSE); |
1bfda055 |
113 | |
114 | Exit: |
a355a365 |
115 | // Only be here if we fail to start Linux |
116 | Print (L"ERROR : Can not start the kernel. Status=0x%X\n", Status); |
117 | |
118 | // Free Runtimee Memory (kernel and FDT) |
119 | return Status; |
1bfda055 |
120 | } |
76d17c31 |
121 | |
122 | /** |
123 | Start a Linux kernel from a Device Path |
124 | |
125 | @param LinuxKernel Device Path to the Linux Kernel |
b34e4db3 |
126 | @param Parameters Linux kernel arguments |
76d17c31 |
127 | @param Fdt Device Path to the Flat Device Tree |
128 | |
129 | @retval EFI_SUCCESS All drivers have been connected |
130 | @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found |
131 | @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results. |
132 | |
133 | **/ |
134 | EFI_STATUS |
135 | BdsBootLinuxAtag ( |
136 | IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath, |
137 | IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath, |
138 | IN CONST CHAR8* Arguments |
139 | ) |
140 | { |
141 | EFI_STATUS Status; |
142 | UINT32 LinuxImageSize; |
143 | UINT32 InitrdImageSize = 0; |
144 | UINT32 KernelParamsSize; |
145 | EFI_PHYSICAL_ADDRESS KernelParamsAddress; |
146 | EFI_PHYSICAL_ADDRESS LinuxImage; |
147 | EFI_PHYSICAL_ADDRESS InitrdImage; |
148 | |
149 | PERF_START (NULL, "BDS", NULL, 0); |
150 | |
151 | // Load the Linux kernel from a device path |
152 | LinuxImage = LINUX_KERNEL_MAX_OFFSET; |
153 | Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize); |
154 | if (EFI_ERROR(Status)) { |
155 | Print (L"ERROR: Did not find Linux kernel.\n"); |
156 | return Status; |
157 | } |
158 | |
159 | if (InitrdDevicePath) { |
4671d15d |
160 | // Load the initrd near to the Linux kernel |
161 | InitrdImage = LINUX_KERNEL_MAX_OFFSET; |
162 | Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImage, &InitrdImageSize); |
163 | if (Status == EFI_OUT_OF_RESOURCES) { |
164 | Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImage, &InitrdImageSize); |
165 | } |
76d17c31 |
166 | if (EFI_ERROR(Status)) { |
167 | Print (L"ERROR: Did not find initrd image.\n"); |
168 | return Status; |
169 | } |
c0a1f777 |
170 | |
171 | // Check if the initrd is a uInitrd |
172 | if (*(UINT32*)((UINTN)InitrdImage) == LINUX_UIMAGE_SIGNATURE) { |
173 | // Skip the 64-byte image header |
174 | InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImage + 64); |
175 | InitrdImageSize -= 64; |
176 | } |
76d17c31 |
177 | } |
178 | |
179 | // |
180 | // Setup the Linux Kernel Parameters |
181 | // |
182 | |
183 | // By setting address=0 we leave the memory allocation to the function |
19a7404a |
184 | Status = PrepareAtagList (Arguments, InitrdImage, InitrdImageSize, &KernelParamsAddress, &KernelParamsSize); |
76d17c31 |
185 | if (EFI_ERROR(Status)) { |
186 | Print(L"ERROR: Can not prepare ATAG list. Status=0x%X\n", Status); |
187 | return Status; |
188 | } |
189 | |
190 | return StartLinux (LinuxImage, LinuxImageSize, KernelParamsAddress, KernelParamsSize, PcdGet32(PcdArmMachineType)); |
191 | } |
192 | |
193 | /** |
194 | Start a Linux kernel from a Device Path |
195 | |
196 | @param LinuxKernel Device Path to the Linux Kernel |
b34e4db3 |
197 | @param Parameters Linux kernel arguments |
76d17c31 |
198 | @param Fdt Device Path to the Flat Device Tree |
199 | |
200 | @retval EFI_SUCCESS All drivers have been connected |
201 | @retval EFI_NOT_FOUND The Linux kernel Device Path has not been found |
202 | @retval EFI_OUT_OF_RESOURCES There is not enough resource memory to store the matching results. |
203 | |
204 | **/ |
205 | EFI_STATUS |
206 | BdsBootLinuxFdt ( |
207 | IN EFI_DEVICE_PATH_PROTOCOL* LinuxKernelDevicePath, |
208 | IN EFI_DEVICE_PATH_PROTOCOL* InitrdDevicePath, |
209 | IN CONST CHAR8* Arguments, |
210 | IN EFI_DEVICE_PATH_PROTOCOL* FdtDevicePath |
211 | ) |
212 | { |
213 | EFI_STATUS Status; |
214 | UINT32 LinuxImageSize; |
215 | UINT32 InitrdImageSize = 0; |
216 | UINT32 KernelParamsSize; |
217 | EFI_PHYSICAL_ADDRESS KernelParamsAddress; |
218 | UINT32 FdtMachineType; |
219 | EFI_PHYSICAL_ADDRESS LinuxImage; |
220 | EFI_PHYSICAL_ADDRESS InitrdImage; |
221 | |
222 | FdtMachineType = 0xFFFFFFFF; |
223 | |
224 | PERF_START (NULL, "BDS", NULL, 0); |
225 | |
226 | // Load the Linux kernel from a device path |
227 | LinuxImage = LINUX_KERNEL_MAX_OFFSET; |
228 | Status = BdsLoadImage (LinuxKernelDevicePath, AllocateMaxAddress, &LinuxImage, &LinuxImageSize); |
229 | if (EFI_ERROR(Status)) { |
230 | Print (L"ERROR: Did not find Linux kernel.\n"); |
231 | return Status; |
232 | } |
233 | |
234 | if (InitrdDevicePath) { |
4671d15d |
235 | InitrdImage = LINUX_KERNEL_MAX_OFFSET; |
236 | Status = BdsLoadImage (InitrdDevicePath, AllocateMaxAddress, &InitrdImage, &InitrdImageSize); |
237 | if (Status == EFI_OUT_OF_RESOURCES) { |
238 | Status = BdsLoadImage (InitrdDevicePath, AllocateAnyPages, &InitrdImage, &InitrdImageSize); |
239 | } |
76d17c31 |
240 | if (EFI_ERROR(Status)) { |
241 | Print (L"ERROR: Did not find initrd image.\n"); |
242 | return Status; |
243 | } |
c0a1f777 |
244 | |
245 | // Check if the initrd is a uInitrd |
246 | if (*(UINT32*)((UINTN)InitrdImage) == LINUX_UIMAGE_SIGNATURE) { |
247 | // Skip the 64-byte image header |
248 | InitrdImage = (EFI_PHYSICAL_ADDRESS)((UINTN)InitrdImage + 64); |
249 | InitrdImageSize -= 64; |
250 | } |
76d17c31 |
251 | } |
252 | |
253 | // Load the FDT binary from a device path |
254 | KernelParamsAddress = LINUX_ATAG_MAX_OFFSET; |
255 | Status = BdsLoadImage (FdtDevicePath, AllocateMaxAddress, &KernelParamsAddress, &KernelParamsSize); |
256 | if (EFI_ERROR(Status)) { |
257 | Print (L"ERROR: Did not find Device Tree blob.\n"); |
258 | return Status; |
259 | } |
260 | return StartLinux (LinuxImage, LinuxImageSize, KernelParamsAddress, KernelParamsSize, FdtMachineType); |
261 | } |
262 | |