1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/** @file
*
* Copyright (c) 2012-2014, ARM Limited. All rights reserved.
*
* This program and the accompanying materials
* are licensed and made available under the terms and conditions of the BSD License
* which accompanies this distribution. The full text of the license may be found at
* http://opensource.org/licenses/bsd-license.php
*
* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*
**/
#include <Protocol/SimpleFileSystem.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include "BootMonFsInternal.h"
/**
Read data from an open file.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to read data from.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
amount of data returned in Buffer. In both cases,
the size is measured in bytes.
@param[out] Buffer The buffer into which the data is read.
@retval EFI_SUCCESS The data was read.
@retval EFI_DEVICE_ERROR On entry, the current file position is
beyond the end of the file, or the device
reported an error while performing the read
operation.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFIAPI
EFI_STATUS
BootMonFsReadFile (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
BOOTMON_FS_INSTANCE *Instance;
BOOTMON_FS_FILE *File;
EFI_DISK_IO_PROTOCOL *DiskIo;
EFI_BLOCK_IO_MEDIA *Media;
UINT64 FileStart;
EFI_STATUS Status;
UINTN RemainingFileSize;
if ((This == NULL) ||
(BufferSize == NULL) ||
(Buffer == NULL) ) {
return EFI_INVALID_PARAMETER;
}
File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
if (File->Info == NULL) {
return EFI_INVALID_PARAMETER;
}
// Ensure the file has been written in Flash before reading it.
// This keeps the code simple and avoids having to manage a non-flushed file.
BootMonFsFlushFile (This);
Instance = File->Instance;
DiskIo = Instance->DiskIo;
Media = Instance->Media;
FileStart = (Media->LowestAlignedLba + File->HwDescription.BlockStart) * Media->BlockSize;
if (File->Position >= File->Info->FileSize) {
// The entire file has been read or the position has been
// set past the end of the file.
*BufferSize = 0;
if (File->Position > File->Info->FileSize) {
return EFI_DEVICE_ERROR;
} else {
return EFI_SUCCESS;
}
}
// This driver assumes that the entire file is in region 0.
RemainingFileSize = File->Info->FileSize - File->Position;
// If read would go past end of file, truncate the read
if (*BufferSize > RemainingFileSize) {
*BufferSize = RemainingFileSize;
}
Status = DiskIo->ReadDisk (
DiskIo,
Media->MediaId,
FileStart + File->Position,
*BufferSize,
Buffer
);
if (EFI_ERROR (Status)) {
*BufferSize = 0;
}
File->Position += *BufferSize;
return Status;
}
/**
Write data to an open file.
The data is not written to the flash yet. It will be written when the file
will be either read, closed or flushed.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that
is the file handle to write data to.
@param[in out] BufferSize On input, the size of the Buffer. On output, the
size of the data actually written. In both cases,
the size is measured in bytes.
@param[in] Buffer The buffer of data to write.
@retval EFI_SUCCESS The data was written.
@retval EFI_ACCESS_DENIED The file was opened read only.
@retval EFI_OUT_OF_RESOURCES Unable to allocate the buffer to store the
data to write.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFIAPI
EFI_STATUS
BootMonFsWriteFile (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
IN VOID *Buffer
)
{
BOOTMON_FS_FILE *File;
BOOTMON_FS_FILE_REGION *Region;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
if (File->Info == NULL) {
return EFI_INVALID_PARAMETER;
}
if (File->OpenMode == EFI_FILE_MODE_READ) {
return EFI_ACCESS_DENIED;
}
// Allocate and initialize the memory region
Region = (BOOTMON_FS_FILE_REGION*)AllocateZeroPool (sizeof (BOOTMON_FS_FILE_REGION));
if (Region == NULL) {
*BufferSize = 0;
return EFI_OUT_OF_RESOURCES;
}
Region->Buffer = AllocateCopyPool (*BufferSize, Buffer);
if (Region->Buffer == NULL) {
*BufferSize = 0;
FreePool (Region);
return EFI_OUT_OF_RESOURCES;
}
Region->Size = *BufferSize;
Region->Offset = File->Position;
InsertTailList (&File->RegionToFlushLink, &Region->Link);
File->Position += *BufferSize;
if (File->Position > File->Info->FileSize) {
File->Info->FileSize = File->Position;
}
return EFI_SUCCESS;
}
/**
Set a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to set the requested position on.
@param[in] Position The byte position from the start of the file to set.
@retval EFI_SUCCESS The position was set.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFIAPI
EFI_STATUS
BootMonFsSetPosition (
IN EFI_FILE_PROTOCOL *This,
IN UINT64 Position
)
{
BOOTMON_FS_FILE *File;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
if (File->Info == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// UEFI Spec section 12.5:
// "Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to
// be set to the end of the file."
//
if (Position == 0xFFFFFFFFFFFFFFFF) {
Position = File->Info->FileSize;
}
File->Position = Position;
return EFI_SUCCESS;
}
/**
Return a file's current position.
@param[in] This A pointer to the EFI_FILE_PROTOCOL instance that is
the file handle to get the current position on.
@param[out] Position The address to return the file's current position value.
@retval EFI_SUCCESS The position was returned.
@retval EFI_INVALID_PARAMETER At least one of the parameters is invalid.
**/
EFIAPI
EFI_STATUS
BootMonFsGetPosition (
IN EFI_FILE_PROTOCOL *This,
OUT UINT64 *Position
)
{
BOOTMON_FS_FILE *File;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
if (File->Info == NULL) {
return EFI_INVALID_PARAMETER;
}
if (Position == NULL) {
return EFI_INVALID_PARAMETER;
}
*Position = File->Position;
return EFI_SUCCESS;
}
|