]>
git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsReadWrite.c
3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Protocol/SimpleFileSystem.h>
16 #include <Library/UefiLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/DebugLib.h>
21 #include "BootMonFsInternal.h"
24 Read data from an open file.
26 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
27 is the file handle to read data from.
28 @param[in out] BufferSize On input, the size of the Buffer. On output, the
29 amount of data returned in Buffer. In both cases,
30 the size is measured in bytes.
31 @param[out] Buffer The buffer into which the data is read.
33 @retval EFI_SUCCESS The data was read.
34 @retval EFI_DEVICE_ERROR On entry, the current file position is
35 beyond the end of the file, or the device
36 reported an error while performing the read
38 @retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
43 IN EFI_FILE_PROTOCOL
*This
,
44 IN OUT UINTN
*BufferSize
,
48 BOOTMON_FS_INSTANCE
*Instance
;
49 BOOTMON_FS_FILE
*File
;
50 EFI_DISK_IO_PROTOCOL
*DiskIo
;
51 EFI_BLOCK_IO_MEDIA
*Media
;
54 UINTN RemainingFileSize
;
56 // Ensure the file has been written in Flash before reading it.
57 // This keeps the code simple and avoids having to manage a non-flushed file.
58 BootMonFsFlushFile (This
);
60 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
62 return EFI_INVALID_PARAMETER
;
65 Instance
= File
->Instance
;
66 DiskIo
= Instance
->DiskIo
;
67 Media
= Instance
->Media
;
68 FileStart
= (Media
->LowestAlignedLba
+ File
->HwDescription
.BlockStart
) * Media
->BlockSize
;
70 if (File
->Position
>= File
->HwDescription
.Region
[0].Size
) {
71 // The entire file has been read or the position has been
72 // set past the end of the file.
74 if (File
->Position
> File
->HwDescription
.Region
[0].Size
) {
75 return EFI_DEVICE_ERROR
;
81 // This driver assumes that the entire file is in region 0.
82 RemainingFileSize
= File
->HwDescription
.Region
[0].Size
- File
->Position
;
84 // If read would go past end of file, truncate the read
85 if (*BufferSize
> RemainingFileSize
) {
86 *BufferSize
= RemainingFileSize
;
89 Status
= DiskIo
->ReadDisk (
92 FileStart
+ File
->Position
,
96 if (EFI_ERROR (Status
)) {
100 File
->Position
+= *BufferSize
;
105 // Inserts an entry into the write chain
109 IN EFI_FILE_PROTOCOL
*This
,
110 IN OUT UINTN
*BufferSize
,
114 BOOTMON_FS_FILE
*File
;
115 BOOTMON_FS_FILE_REGION
*Region
;
117 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
119 return EFI_INVALID_PARAMETER
;
122 if (!(File
->OpenMode
& EFI_FILE_MODE_WRITE
)) {
123 return EFI_ACCESS_DENIED
;
126 // Allocate and initialize the memory region
127 Region
= (BOOTMON_FS_FILE_REGION
*)AllocateZeroPool (sizeof (BOOTMON_FS_FILE_REGION
));
128 if (Region
== NULL
) {
129 return EFI_OUT_OF_RESOURCES
;
132 Region
->Buffer
= AllocateCopyPool (*BufferSize
, Buffer
);
133 if (Region
->Buffer
== NULL
) {
135 return EFI_OUT_OF_RESOURCES
;
138 Region
->Size
= *BufferSize
;
140 Region
->Offset
= File
->Position
;
142 InsertTailList (&File
->RegionToFlushLink
, &Region
->Link
);
144 File
->Position
+= *BufferSize
;
151 BootMonFsSetPosition (
152 IN EFI_FILE_PROTOCOL
*This
,
156 BOOTMON_FS_FILE
*File
;
158 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
160 // UEFI Spec section 12.5:
161 // "Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to
162 // be set to the end of the file."
163 if (Position
== 0xFFFFFFFFFFFFFFFF) {
164 File
->Position
= BootMonFsGetImageLength (File
);
166 // NB: Seeking past the end of the file is valid.
167 File
->Position
= Position
;
175 BootMonFsGetPosition (
176 IN EFI_FILE_PROTOCOL
*This
,
179 BOOTMON_FS_FILE
*File
;
181 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
183 *Position
= File
->Position
;