]>
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"
26 IN EFI_FILE_PROTOCOL
*This
,
27 IN OUT UINTN
*BufferSize
,
31 BOOTMON_FS_INSTANCE
*Instance
;
32 BOOTMON_FS_FILE
*File
;
33 EFI_DISK_IO_PROTOCOL
*DiskIo
;
34 EFI_BLOCK_IO_MEDIA
*Media
;
37 UINTN RemainingFileSize
;
39 // Ensure the file has been written in Flash before reading it.
40 // This keeps the code simple and avoids having to manage a non-flushed file.
41 BootMonFsFlushFile (This
);
43 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
45 return EFI_INVALID_PARAMETER
;
48 Instance
= File
->Instance
;
49 DiskIo
= Instance
->DiskIo
;
50 Media
= Instance
->Media
;
51 FileStart
= (Media
->LowestAlignedLba
+ File
->HwDescription
.BlockStart
) * Media
->BlockSize
;
53 if (File
->Position
>= File
->HwDescription
.Region
[0].Size
) {
54 // The entire file has been read
56 return EFI_DEVICE_ERROR
;
59 // This driver assumes that the entire file is in region 0.
60 RemainingFileSize
= File
->HwDescription
.Region
[0].Size
- File
->Position
;
62 // If read would go past end of file, truncate the read
63 if (*BufferSize
> RemainingFileSize
) {
64 *BufferSize
= RemainingFileSize
;
67 Status
= DiskIo
->ReadDisk (
70 FileStart
+ File
->Position
,
74 if (EFI_ERROR (Status
)) {
78 File
->Position
+= *BufferSize
;
83 // Inserts an entry into the write chain
87 IN EFI_FILE_PROTOCOL
*This
,
88 IN OUT UINTN
*BufferSize
,
92 BOOTMON_FS_FILE
*File
;
93 BOOTMON_FS_FILE_REGION
*Region
;
95 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
97 return EFI_INVALID_PARAMETER
;
100 if (!(File
->OpenMode
& EFI_FILE_MODE_WRITE
)) {
101 return EFI_ACCESS_DENIED
;
104 // Allocate and initialize the memory region
105 Region
= (BOOTMON_FS_FILE_REGION
*)AllocateZeroPool (sizeof (BOOTMON_FS_FILE_REGION
));
106 if (Region
== NULL
) {
107 return EFI_OUT_OF_RESOURCES
;
110 Region
->Buffer
= AllocateCopyPool (*BufferSize
, Buffer
);
111 if (Region
->Buffer
== NULL
) {
113 return EFI_OUT_OF_RESOURCES
;
116 Region
->Size
= *BufferSize
;
118 Region
->Offset
= File
->Position
;
120 InsertTailList (&File
->RegionToFlushLink
, &Region
->Link
);
122 File
->Position
+= *BufferSize
;
129 BootMonFsSetPosition (
130 IN EFI_FILE_PROTOCOL
*This
,
134 BOOTMON_FS_FILE
*File
;
136 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
138 // UEFI Spec section 12.5:
139 // "Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to
140 // be set to the end of the file."
141 if (Position
== 0xFFFFFFFFFFFFFFFF) {
142 File
->Position
= BootMonFsGetImageLength (File
);
144 // NB: Seeking past the end of the file is valid.
145 File
->Position
= Position
;
153 BootMonFsGetPosition (
154 IN EFI_FILE_PROTOCOL
*This
,
157 BOOTMON_FS_FILE
*File
;
159 File
= BOOTMON_FS_FILE_FROM_FILE_THIS (This
);
161 *Position
= File
->Position
;