1 /** @file
2   MDE DXE Services Library provides functions that simplify the development of DXE Drivers.
3   These functions help access data from sections of FFS files or from file path.
4 
5 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials are licensed and made available under
8 the terms and conditions of the BSD License that accompanies this distribution.
9 The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef __DXE_SERVICES_LIB_H__
18 #define __DXE_SERVICES_LIB_H__
19 
20 /**
21   Searches all the available firmware volumes and returns the first matching FFS section.
22 
23   This function searches all the firmware volumes for FFS files with FV file type specified by FileType
24   The order that the firmware volumes is searched is not deterministic. For each available FV a search
25   is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,
26   the FileInstance instance will be the matched FFS file. For each FFS file found a search
27   is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
28   of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
29   Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
30   It is the caller's responsibility to use FreePool() to free the allocated buffer.
31   See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
32   are retrieved from an FFS file based on SectionType and SectionInstance.
33 
34   If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
35   the search will be retried with a section type of EFI_SECTION_PE32.
36   This function must be called with a TPL <= TPL_NOTIFY.
37 
38   If Buffer is NULL, then ASSERT().
39   If Size is NULL, then ASSERT().
40 
41   @param  FileType             Indicates the FV file type to search for within all available FVs.
42   @param  FileInstance         Indicates which file instance within all available FVs specified by FileType.
43                                FileInstance starts from zero.
44   @param  SectionType          Indicates the FFS section type to search for within the FFS file
45                                specified by FileType with FileInstance.
46   @param  SectionInstance      Indicates which section instance within the FFS file
47                                specified by FileType with FileInstance to retrieve. SectionInstance starts from zero.
48   @param  Buffer               On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
49                                Is it the caller's responsibility to free this buffer using FreePool().
50   @param  Size                 On output, a pointer to the size, in bytes, of Buffer.
51 
52   @retval  EFI_SUCCESS          The specified FFS section was returned.
53   @retval  EFI_NOT_FOUND        The specified FFS section could not be found.
54   @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
55   @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a device error.
56   @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the firmware volume that
57                                 contains the matching FFS section does not allow reads.
58 **/
59 EFI_STATUS
60 EFIAPI
61 GetSectionFromAnyFvByFileType  (
62   IN  EFI_FV_FILETYPE               FileType,
63   IN  UINTN                         FileInstance,
64   IN  EFI_SECTION_TYPE              SectionType,
65   IN  UINTN                         SectionInstance,
66   OUT VOID                          **Buffer,
67   OUT UINTN                         *Size
68   );
69 
70 /**
71   Searches all the available firmware volumes and returns the first matching FFS section.
72 
73   This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
74   The order in which the firmware volumes are searched is not deterministic. For each FFS file found, a search
75   is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
76   of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
77   Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
78   It is the caller's responsibility to use FreePool() to free the allocated buffer.
79   See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
80   are retrieved from an FFS file based on SectionType and SectionInstance.
81 
82   If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
83   the search will be retried with a section type of EFI_SECTION_PE32.
84   This function must be called with a TPL <= TPL_NOTIFY.
85 
86   If NameGuid is NULL, then ASSERT().
87   If Buffer is NULL, then ASSERT().
88   If Size is NULL, then ASSERT().
89 
90 
91   @param  NameGuid             A pointer to to the FFS filename GUID to search for
92                                within any of the firmware volumes in the platform.
93   @param  SectionType          Indicates the FFS section type to search for within
94                                the FFS file specified by NameGuid.
95   @param  SectionInstance      Indicates which section instance within the FFS file
96                                specified by NameGuid to retrieve.
97   @param  Buffer               On output, a pointer to a callee-allocated buffer
98                                containing the FFS file section that was found.
99                                It is the caller's responsibility to free this
100                                buffer using FreePool().
101   @param  Size                 On output, a pointer to the size, in bytes, of Buffer.
102 
103   @retval  EFI_SUCCESS          The specified FFS section was returned.
104   @retval  EFI_NOT_FOUND        The specified FFS section could not be found.
105   @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
106                                 the matching FFS section.
107   @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a
108                                 device error.
109   @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the
110                                 firmware volume that contains the matching FFS
111                                 section does not allow reads.
112 **/
113 EFI_STATUS
114 EFIAPI
115 GetSectionFromAnyFv  (
116   IN  CONST EFI_GUID                *NameGuid,
117   IN  EFI_SECTION_TYPE              SectionType,
118   IN  UINTN                         SectionInstance,
119   OUT VOID                          **Buffer,
120   OUT UINTN                         *Size
121   );
122 
123 /**
124   Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
125 
126   This function searches the firmware volume that the currently executing module was loaded
127   from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found, a search
128   is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
129   instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
130   Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
131   It is the caller's responsibility to use FreePool() to free the allocated buffer.
132   See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
133   an FFS file based on SectionType and SectionInstance.
134 
135   If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
136   If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
137   the search will be retried with a section type of EFI_SECTION_PE32.
138 
139   This function must be called with a TPL <= TPL_NOTIFY.
140   If NameGuid is NULL, then ASSERT().
141   If Buffer is NULL, then ASSERT().
142   If Size is NULL, then ASSERT().
143 
144   @param  NameGuid             A pointer to to the FFS filename GUID to search for
145                                within the firmware volumes that the currently
146                                executing module was loaded from.
147   @param  SectionType          Indicates the FFS section type to search for within
148                                the FFS file specified by NameGuid.
149   @param  SectionInstance      Indicates which section instance within the FFS
150                                file specified by NameGuid to retrieve.
151   @param  Buffer               On output, a pointer to a callee allocated buffer
152                                containing the FFS file section that was found.
153                                It is the caller's responsibility to free this buffer
154                                using FreePool().
155   @param  Size                 On output, a pointer to the size, in bytes, of Buffer.
156 
157 
158   @retval  EFI_SUCCESS          The specified FFS section was returned.
159   @retval  EFI_NOT_FOUND        The specified FFS section could not be found.
160   @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
161                                 the matching FFS section.
162   @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a
163                                 device error.
164   @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the
165                                 firmware volume that contains the matching FFS
166                                 section does not allow reads.
167 **/
168 EFI_STATUS
169 EFIAPI
170 GetSectionFromFv (
171   IN  CONST EFI_GUID                *NameGuid,
172   IN  EFI_SECTION_TYPE              SectionType,
173   IN  UINTN                         SectionInstance,
174   OUT VOID                          **Buffer,
175   OUT UINTN                         *Size
176   );
177 
178 
179 /**
180   Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
181 
182   This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
183   If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
184   then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
185   and the size of the allocated buffer is returned in Size. It is the caller's responsibility
186   to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
187   details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
188 
189   If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
190   If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
191   the search will be retried with a section type of EFI_SECTION_PE32.
192   This function must be called with a TPL <= TPL_NOTIFY.
193 
194   If Buffer is NULL, then ASSERT().
195   If Size is NULL, then ASSERT().
196 
197 
198   @param  SectionType          Indicates the FFS section type to search for within
199                                the FFS file that the currently executing module
200                                was loaded from.
201   @param  SectionInstance      Indicates which section instance to retrieve within
202                                the FFS file that the currently executing module
203                                was loaded from.
204   @param  Buffer               On output, a pointer to a callee allocated buffer
205                                containing the FFS file section that was found.
206                                It is the caller's responsibility to free this buffer
207                                using FreePool().
208   @param  Size                 On output, a pointer to the size, in bytes, of Buffer.
209 
210   @retval  EFI_SUCCESS          The specified FFS section was returned.
211   @retval  EFI_NOT_FOUND        The specified FFS section could not be found.
212   @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
213                                 the matching FFS section.
214   @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a
215                                 device error.
216   @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the
217                                 firmware volume that contains the matching FFS
218                                 section does not allow reads.
219 
220 **/
221 EFI_STATUS
222 EFIAPI
223 GetSectionFromFfs (
224   IN  EFI_SECTION_TYPE              SectionType,
225   IN  UINTN                         SectionInstance,
226   OUT VOID                          **Buffer,
227   OUT UINTN                         *Size
228   );
229 
230 
231 /**
232   Get the image file buffer data and buffer size by its device path.
233 
234   Access the file either from a firmware volume, from a file system interface,
235   or from the load file interface.
236 
237   Allocate memory to store the found image. The caller is responsible to free memory.
238 
239   If FilePath is NULL, then NULL is returned.
240   If FileSize is NULL, then NULL is returned.
241   If AuthenticationStatus is NULL, then NULL is returned.
242 
243   @param[in]       BootPolicy           The policy for Open Image File.If TRUE,
244                                         indicates that the request originates from
245                                         the boot manager, and that the boot manager is
246                                         attempting to load FilePath as a boot selection.
247                                         If FALSE, then FilePath must match an exact
248                                         file to be loaded.
249   @param[in]       FilePath             Pointer to the device path of the file that is abstracted to
250                                         the file buffer.
251   @param[out]      FileSize             Pointer to the size of the abstracted file buffer.
252   @param[out]      AuthenticationStatus Pointer to the authentication status.
253 
254   @retval NULL   FilePath is NULL, or FileSize is NULL, or AuthenticationStatus is NULL, or the file can't be found.
255   @retval other  The abstracted file buffer. The caller is responsible to free memory.
256 **/
257 VOID *
258 EFIAPI
259 GetFileBufferByFilePath (
260   IN BOOLEAN                           BootPolicy,
261   IN CONST EFI_DEVICE_PATH_PROTOCOL    *FilePath,
262   OUT      UINTN                       *FileSize,
263   OUT UINT32                           *AuthenticationStatus
264   );
265 
266 /**
267   Searches all the available firmware volumes and returns the file device path of first matching
268   FFS section.
269 
270   This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
271   The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
272   is made for FFS sections of type SectionType.
273 
274   If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
275   the search will be retried with a section type of EFI_SECTION_PE32.
276   This function must be called with a TPL <= TPL_NOTIFY.
277 
278   If NameGuid is NULL, then ASSERT().
279 
280    @param  NameGuid             A pointer to to the FFS filename GUID to search for
281                                 within any of the firmware volumes in the platform.
282    @param  SectionType          Indicates the FFS section type to search for within
283                                 the FFS file specified by NameGuid.
284    @param  SectionInstance      Indicates which section instance within the FFS file
285                                 specified by NameGuid to retrieve.
286    @param  FvFileDevicePath     Device path for the target FFS
287                                 file.
288 
289    @retval  EFI_SUCCESS           The specified file device path of FFS section was returned.
290    @retval  EFI_NOT_FOUND         The specified file device path of FFS section could not be found.
291    @retval  EFI_DEVICE_ERROR      The FFS section could not be retrieves due to a
292                                   device error.
293    @retval  EFI_ACCESS_DENIED     The FFS section could not be retrieves because the
294                                   firmware volume that contains the matching FFS section does not
295                                   allow reads.
296    @retval  EFI_INVALID_PARAMETER FvFileDevicePath is NULL.
297 
298 **/
299 EFI_STATUS
300 EFIAPI
301 GetFileDevicePathFromAnyFv (
302   IN CONST  EFI_GUID                  *NameGuid,
303   IN        EFI_SECTION_TYPE          SectionType,
304   IN        UINTN                     SectionInstance,
305   OUT       EFI_DEVICE_PATH_PROTOCOL  **FvFileDevicePath
306   );
307 
308 #endif
309 
310