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 
10   Copyright (c) 2009, Apple Inc. All rights reserved.<BR>
11 
12   This program and the accompanying materials
13   are licensed and made available under the terms and conditions of the BSD License
14   which accompanies this distribution.  The full text of the license may be found at
15   http://opensource.org/licenses/bsd-license.php
16 
17   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 
20 **/
21 
22 #include "BdsEntry.h"
23 
24 
25 EFI_STATUS
FindApplicationMatchingUiSection(IN CHAR16 * UiString,OUT EFI_HANDLE * FvHandle,OUT EFI_GUID * NameGuid)26 FindApplicationMatchingUiSection (
27   IN  CHAR16      *UiString,
28   OUT EFI_HANDLE  *FvHandle,
29   OUT EFI_GUID    *NameGuid
30   )
31 {
32   EFI_STATUS                    Status;
33   EFI_STATUS                    NextStatus;
34   UINTN                         NoHandles;
35   EFI_HANDLE                    *Buffer;
36   UINTN                         Index;
37   EFI_FV_FILETYPE               FileType;
38   EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
39   VOID                          *Key;
40   EFI_FV_FILE_ATTRIBUTES        Attributes;
41   UINTN                         Size;
42   UINTN                         UiStringLen;
43   CHAR16                        *UiSection;
44   UINT32                        Authentication;
45 
46 
47   UiStringLen = 0;
48   if (UiString != NULL) {
49     DEBUG ((DEBUG_ERROR, "UiString %s\n", UiString));
50     UiStringLen = StrLen (UiString);
51   }
52 
53   Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiFirmwareVolume2ProtocolGuid, NULL, &NoHandles, &Buffer);
54   if (!EFI_ERROR (Status)) {
55     for (Index = 0; Index < NoHandles; Index++) {
56       Status = gBS->HandleProtocol (Buffer[Index], &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
57       if (!EFI_ERROR (Status)) {
58         Key = AllocatePool (Fv->KeySize);
59         ASSERT (Key != NULL);
60         ZeroMem (Key, Fv->KeySize);
61 
62         FileType = EFI_FV_FILETYPE_APPLICATION;
63 
64         do {
65           NextStatus = Fv->GetNextFile (Fv, Key, &FileType, NameGuid, &Attributes, &Size);
66           if (!EFI_ERROR (NextStatus)) {
67             if (UiString == NULL) {
68               //
69               // If UiString is NULL match first application we find.
70               //
71               *FvHandle = Buffer[Index];
72               FreePool (Key);
73               return Status;
74             }
75 
76             UiSection = NULL;
77             Status = Fv->ReadSection (
78                           Fv,
79                           NameGuid,
80                           EFI_SECTION_USER_INTERFACE,
81                           0,
82                           (VOID **)&UiSection,
83                           &Size,
84                           &Authentication
85                           );
86             if (!EFI_ERROR (Status)) {
87               if (StrnCmp (UiString, UiSection, UiStringLen) == 0) {
88                 //
89                 // We found a UiString match.
90                 //
91                 *FvHandle = Buffer[Index];
92                 FreePool (Key);
93                 FreePool (UiSection);
94                 return Status;
95               }
96               FreePool (UiSection);
97             }
98           }
99         } while (!EFI_ERROR (NextStatus));
100 
101         FreePool (Key);
102       }
103     }
104 
105     FreePool (Buffer);
106    }
107 
108   return EFI_NOT_FOUND;
109 }
110 
111 
112 EFI_DEVICE_PATH *
FvFileDevicePath(IN EFI_HANDLE FvHandle,IN EFI_GUID * NameGuid)113 FvFileDevicePath (
114   IN  EFI_HANDLE   FvHandle,
115   IN  EFI_GUID     *NameGuid
116   )
117 {
118   EFI_DEVICE_PATH_PROTOCOL          *DevicePath;
119   MEDIA_FW_VOL_FILEPATH_DEVICE_PATH NewNode;
120 
121   DevicePath = DevicePathFromHandle (FvHandle);
122 
123   EfiInitializeFwVolDevicepathNode (&NewNode, NameGuid);
124 
125   return AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&NewNode);
126 }
127 
128 
129 
130 EFI_STATUS
LoadPeCoffSectionFromFv(IN EFI_HANDLE FvHandle,IN EFI_GUID * NameGuid)131 LoadPeCoffSectionFromFv (
132  IN  EFI_HANDLE   FvHandle,
133  IN  EFI_GUID     *NameGuid
134  )
135 {
136   EFI_STATUS                    Status;
137   EFI_DEVICE_PATH_PROTOCOL      *DevicePath;
138   EFI_HANDLE                    ImageHandle;
139 
140   DevicePath = FvFileDevicePath (FvHandle, NameGuid);
141 
142   Status = gBS->LoadImage (TRUE, gImageHandle, DevicePath, NULL, 0, &ImageHandle);
143   if (!EFI_ERROR (Status)) {
144     PERF_END (NULL, "BDS", NULL, 0);
145     Status = gBS->StartImage (ImageHandle, NULL, NULL);
146   }
147 
148   return Status;
149 }
150 
151