]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Application/AndroidBoot/AndroidBootApp.c
EmbeddedPkg: Apply uncrustify changes
[mirror_edk2.git] / EmbeddedPkg / Application / AndroidBoot / AndroidBootApp.c
1 /** @file
2
3 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
4 Copyright (c) 2017, Linaro. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Library/AndroidBootImgLib.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/DevicePathLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16
17 #include <Protocol/BlockIo.h>
18 #include <Protocol/DevicePathFromText.h>
19
20 /* Validate the node is media hard drive type */
21 EFI_STATUS
22 ValidateAndroidMediaDevicePath (
23 IN EFI_DEVICE_PATH *DevicePath
24 )
25 {
26 EFI_DEVICE_PATH_PROTOCOL *Node, *NextNode;
27
28 NextNode = DevicePath;
29 while (NextNode != NULL) {
30 Node = NextNode;
31 if ((Node->Type == MEDIA_DEVICE_PATH) &&
32 (Node->SubType == MEDIA_HARDDRIVE_DP))
33 {
34 return EFI_SUCCESS;
35 }
36
37 NextNode = NextDevicePathNode (Node);
38 }
39
40 return EFI_INVALID_PARAMETER;
41 }
42
43 EFI_STATUS
44 EFIAPI
45 AndroidBootAppEntryPoint (
46 IN EFI_HANDLE ImageHandle,
47 IN EFI_SYSTEM_TABLE *SystemTable
48 )
49 {
50 EFI_STATUS Status;
51 CHAR16 *BootPathStr;
52 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol;
53 EFI_DEVICE_PATH *DevicePath;
54 EFI_BLOCK_IO_PROTOCOL *BlockIo;
55 UINT32 MediaId, BlockSize;
56 VOID *Buffer;
57 EFI_HANDLE Handle;
58 UINTN BootImgSize;
59
60 BootPathStr = (CHAR16 *)PcdGetPtr (PcdAndroidBootDevicePath);
61 ASSERT (BootPathStr != NULL);
62 Status = gBS->LocateProtocol (
63 &gEfiDevicePathFromTextProtocolGuid,
64 NULL,
65 (VOID **)&EfiDevicePathFromTextProtocol
66 );
67 ASSERT_EFI_ERROR (Status);
68 DevicePath = (EFI_DEVICE_PATH *)EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (BootPathStr);
69 ASSERT (DevicePath != NULL);
70
71 Status = ValidateAndroidMediaDevicePath (DevicePath);
72 if (EFI_ERROR (Status)) {
73 return Status;
74 }
75
76 Status = gBS->LocateDevicePath (
77 &gEfiDevicePathProtocolGuid,
78 &DevicePath,
79 &Handle
80 );
81 if (EFI_ERROR (Status)) {
82 return Status;
83 }
84
85 Status = gBS->OpenProtocol (
86 Handle,
87 &gEfiBlockIoProtocolGuid,
88 (VOID **)&BlockIo,
89 gImageHandle,
90 NULL,
91 EFI_OPEN_PROTOCOL_GET_PROTOCOL
92 );
93 if (EFI_ERROR (Status)) {
94 DEBUG ((DEBUG_ERROR, "Failed to get BlockIo: %r\n", Status));
95 return Status;
96 }
97
98 MediaId = BlockIo->Media->MediaId;
99 BlockSize = BlockIo->Media->BlockSize;
100 Buffer = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (ANDROID_BOOTIMG_HEADER)));
101 if (Buffer == NULL) {
102 return EFI_BUFFER_TOO_SMALL;
103 }
104
105 /* Load header of boot.img */
106 Status = BlockIo->ReadBlocks (
107 BlockIo,
108 MediaId,
109 0,
110 BlockSize,
111 Buffer
112 );
113 Status = AndroidBootImgGetImgSize (Buffer, &BootImgSize);
114 if (EFI_ERROR (Status)) {
115 DEBUG ((DEBUG_ERROR, "Failed to get AndroidBootImg Size: %r\n", Status));
116 return Status;
117 }
118
119 BootImgSize = ALIGN_VALUE (BootImgSize, BlockSize);
120 FreePages (Buffer, EFI_SIZE_TO_PAGES (sizeof (ANDROID_BOOTIMG_HEADER)));
121
122 /* Both PartitionStart and PartitionSize are counted as block size. */
123 Buffer = AllocatePages (EFI_SIZE_TO_PAGES (BootImgSize));
124 if (Buffer == NULL) {
125 return EFI_BUFFER_TOO_SMALL;
126 }
127
128 /* Load header of boot.img */
129 Status = BlockIo->ReadBlocks (
130 BlockIo,
131 MediaId,
132 0,
133 BootImgSize,
134 Buffer
135 );
136 if (EFI_ERROR (Status)) {
137 DEBUG ((DEBUG_ERROR, "Failed to read blocks: %r\n", Status));
138 goto EXIT;
139 }
140
141 Status = AndroidBootImgBoot (Buffer, BootImgSize);
142
143 EXIT:
144 return Status;
145 }