]>
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.
44 IN EFI_FILE_PROTOCOL
*This
,
45 IN OUT UINTN
*BufferSize
,
49 BOOTMON_FS_INSTANCE
*Instance
;
50 BOOTMON_FS_FILE
*File
;
51 EFI_DISK_IO_PROTOCOL
*DiskIo
;
52 EFI_BLOCK_IO_MEDIA
*Media
;
55 UINTN RemainingFileSize
;
58 (BufferSize
== NULL
) ||
60 return EFI_INVALID_PARAMETER
;
62 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
63 if (File
->Info
== NULL
) {
64 return EFI_INVALID_PARAMETER
;
67 // Ensure the file has been written in Flash before reading it.
68 // This keeps the code simple and avoids having to manage a non-flushed file.
69 BootMonFsFlushFile (This
);
71 Instance
= File
->Instance
;
72 DiskIo
= Instance
->DiskIo
;
73 Media
= Instance
->Media
;
74 FileStart
= (Media
->LowestAlignedLba
+ File
->HwDescription
.BlockStart
) * Media
->BlockSize
;
76 if (File
->Position
>= File
->Info
->FileSize
) {
77 // The entire file has been read or the position has been
78 // set past the end of the file.
80 if (File
->Position
> File
->Info
->FileSize
) {
81 return EFI_DEVICE_ERROR
;
87 // This driver assumes that the entire file is in region 0.
88 RemainingFileSize
= File
->Info
->FileSize
- File
->Position
;
90 // If read would go past end of file, truncate the read
91 if (*BufferSize
> RemainingFileSize
) {
92 *BufferSize
= RemainingFileSize
;
95 Status
= DiskIo
->ReadDisk (
98 FileStart
+ File
->Position
,
102 if (EFI_ERROR (Status
)) {
106 File
->Position
+= *BufferSize
;
112 Write data to an open file.
114 The data is not written to the flash yet. It will be written when the file
115 will be either read, closed or flushed.
117 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
118 is the file handle to write data to.
119 @param[in out] BufferSize On input, the size of the Buffer. On output, the
120 size of the data actually written. In both cases,
121 the size is measured in bytes.
122 @param[in] Buffer The buffer of data to write.
124 @retval EFI_SUCCESS The data was written.
125 @retval EFI_ACCESS_DENIED The file was opened read only.
126 @retval EFI_OUT_OF_RESOURCES Unable to allocate the buffer to store the
128 @retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
134 IN EFI_FILE_PROTOCOL
*This
,
135 IN OUT UINTN
*BufferSize
,
139 BOOTMON_FS_FILE
*File
;
140 BOOTMON_FS_FILE_REGION
*Region
;
143 return EFI_INVALID_PARAMETER
;
145 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
146 if (File
->Info
== NULL
) {
147 return EFI_INVALID_PARAMETER
;
150 if (File
->OpenMode
== EFI_FILE_MODE_READ
) {
151 return EFI_ACCESS_DENIED
;
154 // Allocate and initialize the memory region
155 Region
= (BOOTMON_FS_FILE_REGION
*)AllocateZeroPool (sizeof (BOOTMON_FS_FILE_REGION
));
156 if (Region
== NULL
) {
158 return EFI_OUT_OF_RESOURCES
;
161 Region
->Buffer
= AllocateCopyPool (*BufferSize
, Buffer
);
162 if (Region
->Buffer
== NULL
) {
165 return EFI_OUT_OF_RESOURCES
;
168 Region
->Size
= *BufferSize
;
169 Region
->Offset
= File
->Position
;
171 InsertTailList (&File
->RegionToFlushLink
, &Region
->Link
);
173 File
->Position
+= *BufferSize
;
175 if (File
->Position
> File
->Info
->FileSize
) {
176 File
->Info
->FileSize
= File
->Position
;
183 Set a file's current position.
185 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
186 the file handle to set the requested position on.
187 @param[in] Position The byte position from the start of the file to set.
189 @retval EFI_SUCCESS The position was set.
190 @retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
195 BootMonFsSetPosition (
196 IN EFI_FILE_PROTOCOL
*This
,
200 BOOTMON_FS_FILE
*File
;
203 return EFI_INVALID_PARAMETER
;
205 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
206 if (File
->Info
== NULL
) {
207 return EFI_INVALID_PARAMETER
;
211 // UEFI Spec section 12.5:
212 // "Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to
213 // be set to the end of the file."
215 if (Position
== 0xFFFFFFFFFFFFFFFF) {
216 Position
= File
->Info
->FileSize
;
219 File
->Position
= Position
;
225 Return a file's current position.
227 @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
228 the file handle to get the current position on.
229 @param[out] Position The address to return the file's current position value.
231 @retval EFI_SUCCESS The position was returned.
232 @retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
237 BootMonFsGetPosition (
238 IN EFI_FILE_PROTOCOL
*This
,
242 BOOTMON_FS_FILE
*File
;
245 return EFI_INVALID_PARAMETER
;
247 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
248 if (File
->Info
== NULL
) {
249 return EFI_INVALID_PARAMETER
;
252 if (Position
== NULL
) {
253 return EFI_INVALID_PARAMETER
;
256 *Position
= File
->Position
;