07d0ea7b4120e735d61fb47b47c862dba77d9576
[mirror_edk2.git] / ArmPkg / Application / LinuxLoader / LinuxFdtLoader.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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 "LinuxInternal.h"
16
17 #include <Library/PrintLib.h>
18 #include <Library/UefiApplicationEntryPoint.h>
19
20 /**
21 The user Entry Point for Application. The user code starts with this function
22 as the real entry point for the application.
23
24 @param[in] ImageHandle The firmware allocated handle for the EFI image.
25 @param[in] SystemTable A pointer to the EFI System Table.
26
27 @retval EFI_SUCCESS The entry point is executed successfully.
28 @retval other Some error occurs when executing this entry point.
29
30 **/
31 EFI_STATUS
32 EFIAPI
33 UefiMain (
34 IN EFI_HANDLE ImageHandle,
35 IN EFI_SYSTEM_TABLE *SystemTable
36 )
37 {
38 EFI_STATUS Status;
39 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
40 LINUX_LOADER_OPTIONAL_DATA* LinuxOptionalData;
41 EFI_DEVICE_PATH* DevicePathKernel;
42 EFI_DEVICE_PATH* DevicePathFdt;
43 EFI_DEVICE_PATH* InitrdDevicePath;
44 CHAR16* OptionalDataInitrd;
45 CHAR8* OptionalDataArguments;
46 CHAR16* Initrd;
47
48 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&LoadedImage);
49 ASSERT_EFI_ERROR (Status);
50
51 if (LoadedImage->LoadOptionsSize == 0) {
52 Status = LinuxLoaderConfig (LoadedImage);
53 } else {
54 // Ensure the signature is correct
55 LinuxOptionalData = (LINUX_LOADER_OPTIONAL_DATA*)LoadedImage->LoadOptions;
56 if (LinuxOptionalData->Signature != LINUX_LOADER_SIGNATURE) {
57 return EFI_UNSUPPORTED;
58 }
59
60 // Generate the File Path Node for the Linux Kernel & Device Tree blob
61 DevicePathKernel = FileDevicePath (LoadedImage->DeviceHandle, LINUX_KERNEL_NAME);
62 DevicePathFdt = FileDevicePath (LoadedImage->DeviceHandle, FDT_NAME);
63
64 if (LinuxOptionalData->CmdLineLength > 0) {
65 OptionalDataArguments = (CHAR8*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA);
66 } else {
67 OptionalDataArguments = NULL;
68 }
69
70 if (LinuxOptionalData->InitrdPathListLength > 0) {
71 if (OptionalDataArguments != NULL) {
72 OptionalDataInitrd = (CHAR16*)(OptionalDataArguments + LinuxOptionalData->CmdLineLength);
73 } else {
74 OptionalDataInitrd = (CHAR16*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA);
75 }
76
77 // If pointer not aligned
78 if ((UINTN)OptionalDataInitrd & 0x1) {
79 Initrd = (CHAR16*)AllocateCopyPool (LinuxOptionalData->InitrdPathListLength, OptionalDataInitrd);
80 } else {
81 Initrd = OptionalDataInitrd;
82 }
83
84 InitrdDevicePath = FileDevicePath (LoadedImage->DeviceHandle, Initrd);
85 } else {
86 OptionalDataInitrd = NULL;
87 InitrdDevicePath = NULL;
88 Initrd = NULL;
89 }
90
91 // Load and Start the Linux Kernel (we should never return)
92 Status = BdsBootLinuxFdt (DevicePathKernel, InitrdDevicePath, OptionalDataArguments, DevicePathFdt);
93
94 if ((UINTN)OptionalDataInitrd & 0x1) {
95 FreePool (Initrd);
96 }
97
98 FreePool (DevicePathKernel);
99 if (InitrdDevicePath) {
100 FreePool (InitrdDevicePath);
101 }
102 }
103
104 return Status;
105 }