]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/FatPei/FatLitePeim.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / FatPkg / FatPei / FatLitePeim.h
1 /** @file
2 Data structures for FAT recovery PEIM
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #ifndef _FAT_PEIM_H_
11 #define _FAT_PEIM_H_
12
13 #include <PiPei.h>
14
15 #include <Guid/RecoveryDevice.h>
16 #include <Ppi/BlockIo.h>
17 #include <Ppi/BlockIo2.h>
18 #include <Ppi/DeviceRecoveryModule.h>
19
20 #include <Library/DebugLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/PeimEntryPoint.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/PeiServicesTablePointerLib.h>
27 #include <Library/PeiServicesLib.h>
28
29 #include "FatLiteApi.h"
30 #include "FatLiteFmt.h"
31
32 //
33 // Definitions
34 //
35
36 #define PEI_FAT_CACHE_SIZE 4
37 #define PEI_FAT_MAX_BLOCK_SIZE 8192
38 #define FAT_MAX_FILE_NAME_LENGTH 128
39 #define PEI_FAT_MAX_BLOCK_DEVICE 64
40 #define PEI_FAT_MAX_BLOCK_IO_PPI 32
41 #define PEI_FAT_MAX_VOLUME 64
42
43 #define PEI_FAT_MEMORY_PAGE_SIZE 0x1000
44
45 //
46 // Data Structures
47 //
48 //
49 // The block device
50 //
51 typedef struct {
52 UINT32 BlockSize;
53 UINT64 LastBlock;
54 UINT32 IoAlign;
55 BOOLEAN Logical;
56 BOOLEAN PartitionChecked;
57
58 //
59 // Following fields only valid for logical device
60 //
61 CHAR8 PartitionFlag[8];
62 UINT64 StartingPos;
63 UINTN ParentDevNo;
64
65 //
66 // Following fields only valid for physical device
67 //
68 EFI_PEI_BLOCK_DEVICE_TYPE DevType;
69 UINT8 InterfaceType;
70 //
71 // EFI_PEI_READ_BLOCKS ReadFunc;
72 //
73 EFI_PEI_RECOVERY_BLOCK_IO_PPI *BlockIo;
74 EFI_PEI_RECOVERY_BLOCK_IO2_PPI *BlockIo2;
75 UINT8 PhysicalDevNo;
76 } PEI_FAT_BLOCK_DEVICE;
77
78 //
79 // the Volume structure
80 //
81 typedef struct {
82 UINTN BlockDeviceNo;
83 UINTN VolumeNo;
84 UINT64 VolumeSize;
85 UINTN MaxCluster;
86 CHAR16 VolumeLabel[FAT_MAX_FILE_NAME_LENGTH];
87 PEI_FAT_TYPE FatType;
88 UINT64 FatPos;
89 UINT32 SectorSize;
90 UINT32 ClusterSize;
91 UINT64 FirstClusterPos;
92 UINT64 RootDirPos;
93 UINT32 RootEntries;
94 UINT32 RootDirCluster;
95 } PEI_FAT_VOLUME;
96
97 //
98 // File instance
99 //
100 typedef struct {
101 PEI_FAT_VOLUME *Volume;
102 CHAR16 FileName[FAT_MAX_FILE_NAME_LENGTH];
103
104 BOOLEAN IsFixedRootDir;
105
106 UINT32 StartingCluster;
107 UINT32 CurrentPos;
108 UINT32 StraightReadAmount;
109 UINT32 CurrentCluster;
110
111 UINT8 Attributes;
112 UINT32 FileSize;
113 } PEI_FAT_FILE;
114
115 //
116 // Cache Buffer
117 //
118 typedef struct {
119 BOOLEAN Valid;
120 UINTN BlockDeviceNo;
121 UINT64 Lba;
122 UINT32 Lru;
123 UINT64 Buffer[PEI_FAT_MAX_BLOCK_SIZE / 8];
124 UINTN Size;
125 } PEI_FAT_CACHE_BUFFER;
126
127 //
128 // Private Data.
129 // This structure abstracts the whole memory usage in FAT PEIM.
130 // The entry point routine will get a chunk of memory (by whatever
131 // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
132 // in both 32 and 64 bit environment. The boundary of the memory chunk
133 // should be 64bit aligned.
134 //
135 #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
136
137 typedef struct {
138 UINTN Signature;
139 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi;
140 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor;
141 EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor[2];
142
143 UINT8 UnicodeCaseMap[0x300];
144 CHAR8 *EngUpperMap;
145 CHAR8 *EngLowerMap;
146 CHAR8 *EngInfoMap;
147
148 UINT64 BlockData[PEI_FAT_MAX_BLOCK_SIZE / 8];
149 UINTN BlockDeviceCount;
150 PEI_FAT_BLOCK_DEVICE BlockDevice[PEI_FAT_MAX_BLOCK_DEVICE];
151 UINTN VolumeCount;
152 PEI_FAT_VOLUME Volume[PEI_FAT_MAX_VOLUME];
153 PEI_FAT_FILE File;
154 PEI_FAT_CACHE_BUFFER CacheBuffer[PEI_FAT_CACHE_SIZE];
155 } PEI_FAT_PRIVATE_DATA;
156
157 #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
158 CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
159
160 //
161 // Extract INT32 from char array
162 //
163 #define UNPACK_INT32(a) \
164 (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
165
166 //
167 // Extract UINT32 from char array
168 //
169 #define UNPACK_UINT32(a) \
170 (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
171
172 //
173 // API functions
174 //
175
176 /**
177 Finds the recovery file on a FAT volume.
178 This function finds the recovery file named FileName on a specified FAT volume and returns
179 its FileHandle pointer.
180
181 @param PrivateData Global memory map for accessing global
182 variables.
183 @param VolumeIndex The index of the volume.
184 @param FileName The recovery file name to find.
185 @param Handle The output file handle.
186
187 @retval EFI_DEVICE_ERROR Some error occurred when operating the FAT
188 volume.
189 @retval EFI_NOT_FOUND The recovery file was not found.
190 @retval EFI_SUCCESS The recovery file was successfully found on the
191 FAT volume.
192
193 **/
194 EFI_STATUS
195 FindRecoveryFile (
196 IN PEI_FAT_PRIVATE_DATA *PrivateData,
197 IN UINTN VolumeIndex,
198 IN CHAR16 *FileName,
199 OUT PEI_FILE_HANDLE *Handle
200 );
201
202 /**
203 Returns the number of DXE capsules residing on the device.
204 This function, by whatever mechanism, searches for DXE capsules from the associated device and
205 returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
206 the highest load priority and entry N is assumed to be the lowest priority.
207
208 @param PeiServices General-purpose services that are available to
209 every PEIM.
210 @param This Indicates the
211 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
212 @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
213 *NumberRecoveryCapsules contains the number of
214 recovery capsule images available for retrieval
215 from this PEIM instance.
216
217 @retval EFI_SUCCESS The function completed successfully.
218
219 **/
220 EFI_STATUS
221 EFIAPI
222 GetNumberRecoveryCapsules (
223 IN EFI_PEI_SERVICES **PeiServices,
224 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
225 OUT UINTN *NumberRecoveryCapsules
226 );
227
228 /**
229 Returns the size and type of the requested recovery capsule.
230 This function returns the size and type of the capsule specified by CapsuleInstance.
231
232 @param PeiServices General-purpose services that are available to
233 every PEIM.
234 @param This Indicates the
235 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
236 @param CapsuleInstance Specifies for which capsule instance to
237 retrieve the information.T his parameter must
238 be between one and the value returned by
239 GetNumberRecoveryCapsules() in
240 NumberRecoveryCapsules.
241 @param Size A pointer to a caller-allocated UINTN in which
242 the size of the requested recovery module is
243 returned.
244 @param CapsuleType A pointer to a caller-allocated EFI_GUID in
245 which the type of the requested recovery
246 capsule is returned.T he semantic meaning of
247 the value returned is defined by the
248 implementation.
249
250 @retval EFI_SUCCESS The capsule type and size were retrieved.
251 @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
252 discovered recovery capsule.
253
254 **/
255 EFI_STATUS
256 EFIAPI
257 GetRecoveryCapsuleInfo (
258 IN EFI_PEI_SERVICES **PeiServices,
259 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
260 IN UINTN CapsuleInstance,
261 OUT UINTN *Size,
262 OUT EFI_GUID *CapsuleType
263 );
264
265 /**
266 Loads a DXE capsule from some media into memory.
267
268 This function, by whatever mechanism, retrieves a DXE capsule from some device
269 and loads it into memory. Note that the published interface is device neutral.
270
271 @param[in] PeiServices General-purpose services that are available
272 to every PEIM
273 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
274 instance.
275 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
276 @param[out] Buffer Specifies a caller-allocated buffer in which
277 the requested recovery capsule will be returned.
278
279 @retval EFI_SUCCESS The capsule was loaded correctly.
280 @retval EFI_DEVICE_ERROR A device error occurred.
281 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
282
283 **/
284 EFI_STATUS
285 EFIAPI
286 LoadRecoveryCapsule (
287 IN EFI_PEI_SERVICES **PeiServices,
288 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
289 IN UINTN CapsuleInstance,
290 OUT VOID *Buffer
291 );
292
293 /**
294 This version is different from the version in Unicode collation
295 protocol in that this version strips off trailing blanks.
296 Converts an 8.3 FAT file name using an OEM character set
297 to a Null-terminated Unicode string.
298 Here does not expand DBCS FAT chars.
299
300 @param FatSize The size of the string Fat in bytes.
301 @param Fat A pointer to a Null-terminated string that contains
302 an 8.3 file name using an OEM character set.
303 @param Str A pointer to a Null-terminated Unicode string. The
304 string must be allocated in advance to hold FatSize
305 Unicode characters
306
307 **/
308 VOID
309 EngFatToStr (
310 IN UINTN FatSize,
311 IN CHAR8 *Fat,
312 OUT CHAR16 *Str
313 );
314
315 /**
316 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
317
318 @param PrivateData Global memory map for accessing global variables
319 @param Str1 First string to perform case insensitive comparison.
320 @param Str2 Second string to perform case insensitive comparison.
321
322 **/
323 BOOLEAN
324 EngStriColl (
325 IN PEI_FAT_PRIVATE_DATA *PrivateData,
326 IN CHAR16 *Str1,
327 IN CHAR16 *Str2
328 );
329
330 /**
331 Reads a block of data from the block device by calling
332 underlying Block I/O service.
333
334 @param PrivateData Global memory map for accessing global variables
335 @param BlockDeviceNo The index for the block device number.
336 @param Lba The logic block address to read data from.
337 @param BufferSize The size of data in byte to read.
338 @param Buffer The buffer of the
339
340 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
341 device number.
342 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
343 of the block device.
344
345 **/
346 EFI_STATUS
347 FatReadBlock (
348 IN PEI_FAT_PRIVATE_DATA *PrivateData,
349 IN UINTN BlockDeviceNo,
350 IN EFI_PEI_LBA Lba,
351 IN UINTN BufferSize,
352 OUT VOID *Buffer
353 );
354
355 /**
356 Check if there is a valid FAT in the corresponding Block device
357 of the volume and if yes, fill in the relevant fields for the
358 volume structure. Note there should be a valid Block device number
359 already set.
360
361 @param PrivateData Global memory map for accessing global
362 variables.
363 @param Volume On input, the BlockDeviceNumber field of the
364 Volume should be a valid value. On successful
365 output, all fields except the VolumeNumber
366 field is initialized.
367
368 @retval EFI_SUCCESS A FAT is found and the volume structure is
369 initialized.
370 @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
371 @retval EFI_DEVICE_ERROR There is something error while accessing device.
372
373 **/
374 EFI_STATUS
375 FatGetBpbInfo (
376 IN PEI_FAT_PRIVATE_DATA *PrivateData,
377 IN OUT PEI_FAT_VOLUME *Volume
378 );
379
380 /**
381 Gets the next cluster in the cluster chain.
382
383 @param PrivateData Global memory map for accessing global variables
384 @param Volume The volume
385 @param Cluster The cluster
386 @param NextCluster The cluster number of the next cluster
387
388 @retval EFI_SUCCESS The address is got
389 @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
390 @retval EFI_DEVICE_ERROR Read disk error
391
392 **/
393 EFI_STATUS
394 FatGetNextCluster (
395 IN PEI_FAT_PRIVATE_DATA *PrivateData,
396 IN PEI_FAT_VOLUME *Volume,
397 IN UINT32 Cluster,
398 OUT UINT32 *NextCluster
399 );
400
401 /**
402 Disk reading.
403
404 @param PrivateData the global memory map;
405 @param BlockDeviceNo the block device to read;
406 @param StartingAddress the starting address.
407 @param Size the amount of data to read.
408 @param Buffer the buffer holding the data
409
410 @retval EFI_SUCCESS The function completed successfully.
411 @retval EFI_DEVICE_ERROR Something error.
412
413 **/
414 EFI_STATUS
415 FatReadDisk (
416 IN PEI_FAT_PRIVATE_DATA *PrivateData,
417 IN UINTN BlockDeviceNo,
418 IN UINT64 StartingAddress,
419 IN UINTN Size,
420 OUT VOID *Buffer
421 );
422
423 /**
424 Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
425
426 @param PrivateData the global memory map
427 @param File the file
428 @param Pos the Position which is offset from the file's
429 CurrentPos
430
431 @retval EFI_SUCCESS Success.
432 @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
433 @retval EFI_DEVICE_ERROR Something error while accessing media.
434
435 **/
436 EFI_STATUS
437 FatSetFilePos (
438 IN PEI_FAT_PRIVATE_DATA *PrivateData,
439 IN PEI_FAT_FILE *File,
440 IN UINT32 Pos
441 );
442
443 /**
444 Reads file data. Updates the file's CurrentPos.
445
446 @param PrivateData Global memory map for accessing global variables
447 @param File The file.
448 @param Size The amount of data to read.
449 @param Buffer The buffer storing the data.
450
451 @retval EFI_SUCCESS The data is read.
452 @retval EFI_INVALID_PARAMETER File is invalid.
453 @retval EFI_DEVICE_ERROR Something error while accessing media.
454
455 **/
456 EFI_STATUS
457 FatReadFile (
458 IN PEI_FAT_PRIVATE_DATA *PrivateData,
459 IN PEI_FAT_FILE *File,
460 IN UINTN Size,
461 OUT VOID *Buffer
462 );
463
464 /**
465 This function reads the next item in the parent directory and
466 initializes the output parameter SubFile (CurrentPos is initialized to 0).
467 The function updates the CurrentPos of the parent dir to after the item read.
468 If no more items were found, the function returns EFI_NOT_FOUND.
469
470 @param PrivateData Global memory map for accessing global variables
471 @param ParentDir The parent directory.
472 @param SubFile The File structure containing the sub file that
473 is caught.
474
475 @retval EFI_SUCCESS The next sub file is obtained.
476 @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
477 @retval EFI_NOT_FOUND No more sub file exists.
478 @retval EFI_DEVICE_ERROR Something error while accessing media.
479
480 **/
481 EFI_STATUS
482 FatReadNextDirectoryEntry (
483 IN PEI_FAT_PRIVATE_DATA *PrivateData,
484 IN PEI_FAT_FILE *ParentDir,
485 OUT PEI_FAT_FILE *SubFile
486 );
487
488 /**
489 This function finds partitions (logical devices) in physical block devices.
490
491 @param PrivateData Global memory map for accessing global variables.
492
493 **/
494 VOID
495 FatFindPartitions (
496 IN PEI_FAT_PRIVATE_DATA *PrivateData
497 );
498
499 #endif // _FAT_PEIM_H_