summaryrefslogtreecommitdiff
path: root/ArmPkg/Library/BdsLib/BdsAppLoader.c
blob: 1f208f8dd796c3c3f01a842cf736258d5fa6cbdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/** @file
*
*  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
*
*  This program and the accompanying materials
*  are licensed and made available under the terms and conditions of the BSD License
*  which accompanies this distribution.  The full text of the license may be found at
*  http://opensource.org/licenses/bsd-license.php
*
*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*
**/

#include "BdsInternal.h"

/**
  Locate an EFI application in a the Firmware Volumes by its Name

  @param  EfiAppGuid            Guid of the EFI Application into the Firmware Volume
  @param  DevicePath            EFI Device Path of the EFI application

  @return EFI_SUCCESS           The function completed successfully.
  @return EFI_NOT_FOUND         The protocol could not be located.
  @return EFI_OUT_OF_RESOURCES  There are not enough resources to find the protocol.

**/
EFI_STATUS
LocateEfiApplicationInFvByName (
  IN  CONST CHAR16*             EfiAppName,
  OUT EFI_DEVICE_PATH           **DevicePath
  )
{
  VOID                          *Key;
  EFI_STATUS                    Status, FileStatus;
  EFI_GUID                      NameGuid;
  EFI_FV_FILETYPE               FileType;
  EFI_FV_FILE_ATTRIBUTES        Attributes;
  UINTN                         Size;
  UINTN                         UiStringLen;
  CHAR16                        *UiSection;
  UINT32                        Authentication;
  EFI_DEVICE_PATH               *FvDevicePath;
  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH    FileDevicePath;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         NumberOfHandles;
  UINTN                         Index;
  EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;

  ASSERT (DevicePath != NULL);

  // Length of FilePath
  UiStringLen = StrLen (EfiAppName);

  // Locate all the Firmware Volume protocols.
  Status = gBS->LocateHandleBuffer (
                   ByProtocol,
                   &gEfiFirmwareVolume2ProtocolGuid,
                   NULL,
                   &NumberOfHandles,
                   &HandleBuffer
                   );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  *DevicePath = NULL;

  // Looking for FV with ACPI storage file
  for (Index = 0; Index < NumberOfHandles; Index++) {
    //
    // Get the protocol on this handle
    // This should not fail because of LocateHandleBuffer
    //
    Status = gBS->HandleProtocol (
                     HandleBuffer[Index],
                     &gEfiFirmwareVolume2ProtocolGuid,
                     (VOID**) &FvInstance
                     );
    if (EFI_ERROR (Status)) {
      goto FREE_HANDLE_BUFFER;
    }

    // Allocate Key
    Key = AllocatePool (FvInstance->KeySize);
    ASSERT (Key != NULL);
    ZeroMem (Key, FvInstance->KeySize);

    do {
      // Search in all files
      FileType = EFI_FV_FILETYPE_ALL;

      Status = FvInstance->GetNextFile (FvInstance, Key, &FileType, &NameGuid, &Attributes, &Size);
      if (!EFI_ERROR (Status)) {
        UiSection = NULL;
        FileStatus = FvInstance->ReadSection (
                      FvInstance,
                      &NameGuid,
                      EFI_SECTION_USER_INTERFACE,
                      0,
                      (VOID **)&UiSection,
                      &Size,
                      &Authentication
                      );
        if (!EFI_ERROR (FileStatus)) {
          if (StrnCmp (EfiAppName, UiSection, UiStringLen) == 0) {
            //
            // We found a UiString match.
            //
            Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);

            // Generate the Device Path for the file
            EfiInitializeFwVolDevicepathNode (&FileDevicePath, &NameGuid);
            *DevicePath = AppendDevicePathNode (FvDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&FileDevicePath);
            ASSERT (*DevicePath != NULL);

            FreePool (Key);
            FreePool (UiSection);
            FreePool (HandleBuffer);
            return FileStatus;
          }
          FreePool (UiSection);
        }
      }
    } while (!EFI_ERROR (Status));

    FreePool (Key);
  }

FREE_HANDLE_BUFFER:
  FreePool (HandleBuffer);
  return EFI_NOT_FOUND;
}

/**
  Locate an EFI application in a the Firmware Volumes by its GUID

  @param  EfiAppGuid            Guid of the EFI Application into the Firmware Volume
  @param  DevicePath            EFI Device Path of the EFI application

  @return EFI_SUCCESS           The function completed successfully.
  @return EFI_NOT_FOUND         The protocol could not be located.
  @return EFI_OUT_OF_RESOURCES  There are not enough resources to find the protocol.

**/
EFI_STATUS
LocateEfiApplicationInFvByGuid (
  IN  CONST EFI_GUID            *EfiAppGuid,
  OUT EFI_DEVICE_PATH           **DevicePath
  )
{
  EFI_STATUS                    Status;
  EFI_DEVICE_PATH               *FvDevicePath;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         NumberOfHandles;
  UINTN                         Index;
  EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;
  EFI_FV_FILE_ATTRIBUTES        Attributes;
  UINT32                        AuthenticationStatus;
  EFI_FV_FILETYPE               Type;
  UINTN                         Size;
  CHAR16                        *UiSection;
  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileDevicePath;

  ASSERT (DevicePath != NULL);

  // Locate all the Firmware Volume protocols.
  Status = gBS->LocateHandleBuffer (
                   ByProtocol,
                   &gEfiFirmwareVolume2ProtocolGuid,
                   NULL,
                   &NumberOfHandles,
                   &HandleBuffer
                   );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  *DevicePath = NULL;

  // Looking for FV with ACPI storage file
  for (Index = 0; Index < NumberOfHandles; Index++) {
    //
    // Get the protocol on this handle
    // This should not fail because of LocateHandleBuffer
    //
    Status = gBS->HandleProtocol (
                     HandleBuffer[Index],
                     &gEfiFirmwareVolume2ProtocolGuid,
                     (VOID**) &FvInstance
                     );
    if (EFI_ERROR (Status)) {
      goto FREE_HANDLE_BUFFER;
    }

    Status = FvInstance->ReadFile (
                  FvInstance,
                  EfiAppGuid,
                  NULL,
                  &Size,
                  &Type,
                  &Attributes,
                  &AuthenticationStatus
                  );
    if (EFI_ERROR (Status)) {
      //
      // Skip if no EFI application file in the FV
      //
      continue;
    } else {
      UiSection = NULL;
      Status = FvInstance->ReadSection (
                    FvInstance,
                    EfiAppGuid,
                    EFI_SECTION_USER_INTERFACE,
                    0,
                    (VOID **)&UiSection,
                    &Size,
                    &AuthenticationStatus
                    );
      if (!EFI_ERROR (Status)) {
        //
        // Create the EFI Device Path for the application using the Filename of the application
        //
        *DevicePath = FileDevicePath (HandleBuffer[Index], UiSection);
      } else {
        Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID**)&FvDevicePath);
        ASSERT_EFI_ERROR (Status);

        //
        // Create the EFI Device Path for the application using the EFI GUID of the application
        //
        EfiInitializeFwVolDevicepathNode (&FvFileDevicePath, EfiAppGuid);

        *DevicePath = AppendDevicePathNode (FvDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&FvFileDevicePath);
        ASSERT (*DevicePath != NULL);
      }
      break;
    }
  }

FREE_HANDLE_BUFFER:
  //
  // Free any allocated buffers
  //
  FreePool (HandleBuffer);

  if (*DevicePath == NULL) {
    return EFI_NOT_FOUND;
  } else {
    return EFI_SUCCESS;
  }
}