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