b6a2206a86e5039a5e182fe73769f0cd0826f6ce
[mirror_edk2.git] / EmbeddedPkg / TemplateBds / FirmwareVolume.c
1 /** @file
2 The entry of the embedded BDS. This BDS does not follow the Boot Manager requirements
3 of the UEFI specification as it is designed to implement an embedded systmes
4 propriatary boot scheme.
5
6 This template assume a DXE driver produces a SerialIo protocol not using the EFI
7 driver module and it will attempt to connect a console on top of this.
8
9 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
10
11 All rights reserved. This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include "BdsEntry.h"
22
23
24 EFI_STATUS
25 FindApplicationMatchingUiSection (
26 IN CHAR16 *UiString,
27 OUT EFI_HANDLE *FvHandle,
28 OUT EFI_GUID **NameGuid
29 )
30 {
31 EFI_STATUS Status;
32 EFI_STATUS NextStatus;
33 UINTN NoHandles;
34 EFI_HANDLE *Buffer;
35 UINTN Index;
36 EFI_FV_FILETYPE FileType;
37 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
38 VOID *Key;
39 EFI_FV_FILE_ATTRIBUTES Attributes;
40 UINTN Size;
41 UINTN UiStringLen;
42 CHAR16 *UiSection;
43 UINT32 Authentication;
44
45
46 UiStringLen = 0;
47 if (UiString != NULL) {
48 UiStringLen = StrLen (UiString);
49 }
50
51 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiFirmwareVolume2ProtocolGuid, NULL, &NoHandles, &Buffer);
52 if (!EFI_ERROR (Status)) {
53 for (Index = 0; Index < NoHandles; Index++) {
54 Status = gBS->HandleProtocol (Buffer[Index], &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
55 if (!EFI_ERROR (Status)) {
56 Key = AllocatePool (Fv->KeySize);
57 FileType = EFI_FV_FILETYPE_APPLICATION;
58
59 do {
60 NextStatus = Fv->GetNextFile (Fv, Key, &FileType, *NameGuid, &Attributes, &Size);
61 if (!EFI_ERROR (NextStatus)) {
62 if (UiString == NULL) {
63 //
64 // If UiString is NULL match first application we find.
65 //
66 *FvHandle = Buffer[Index];
67 FreePool (Key);
68 return Status;
69 }
70
71 UiSection = NULL;
72 Status = Fv->ReadSection (
73 Fv,
74 *NameGuid,
75 EFI_SECTION_USER_INTERFACE,
76 0,
77 (VOID **)&UiSection,
78 &Size,
79 &Authentication
80 );
81 if (!EFI_ERROR (Status)) {
82 if (StrnCmp (UiString, UiSection, UiStringLen)) {
83 //
84 // We found a UiString match.
85 //
86 *FvHandle = Buffer[Index];
87 FreePool (Key);
88 FreePool (UiSection);
89 return Status;
90 }
91 FreePool (UiSection);
92 }
93 }
94 } while (!EFI_ERROR (NextStatus));
95
96 FreePool (Key);
97 }
98 }
99
100 FreePool (Buffer);
101 }
102
103 return EFI_NOT_FOUND;
104 }
105
106
107 EFI_DEVICE_PATH *
108 FvFileDevicePath (
109 IN EFI_HANDLE FvHandle,
110 IN EFI_GUID *NameGuid
111 )
112 {
113 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH NewNode;
115
116 DevicePath = DevicePathFromHandle (FvHandle);
117
118 EfiInitializeFwVolDevicepathNode (&NewNode, NameGuid);
119
120 return AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&NewNode);
121 }
122
123
124
125 EFI_STATUS
126 LoadPeCoffSectionFromFv (
127 IN EFI_HANDLE FvHandle,
128 IN EFI_GUID *NameGuid
129 )
130 {
131 EFI_STATUS Status;
132 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
133 VOID *Buffer;
134 UINTN BufferSize;
135 UINT32 Authentication;
136 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
137 EFI_HANDLE ImageHandle;
138
139 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
140 if (EFI_ERROR (Status)) {
141 return Status;
142 }
143
144 Status = Fv->ReadSection (Fv, NameGuid, EFI_SECTION_PE32, 0, &Buffer, &BufferSize, &Authentication);
145 if (!EFI_ERROR (Status)) {
146 DevicePath = FvFileDevicePath (FvHandle, NameGuid);
147 Status = gBS->LoadImage (TRUE, gImageHandle, DevicePath, Buffer, BufferSize, &ImageHandle);
148 if (!EFI_ERROR (Status)) {
149 // ExitData is NULL so we need to pass in a size of zero
150 BufferSize = 0;
151 Status = gBS->StartImage (ImageHandle, &BufferSize, NULL);
152 }
153
154 FreePool (Buffer);
155 }
156
157
158 return Status;
159 }
160