]>
Commit | Line | Data |
---|---|---|
1e57a462 | 1 | /*++ @file NorFlashFvbDxe.c\r |
2 | \r | |
2dff0c1a | 3 | Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>\r |
1e57a462 | 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 <PiDxe.h>\r | |
16 | \r | |
17 | #include <Library/PcdLib.h>\r | |
18 | #include <Library/BaseLib.h>\r | |
19 | #include <Library/HobLib.h>\r | |
20 | #include <Library/UefiLib.h>\r | |
21 | #include <Library/BaseMemoryLib.h>\r | |
22 | #include <Library/MemoryAllocationLib.h>\r | |
23 | #include <Library/UefiBootServicesTableLib.h>\r | |
24 | \r | |
25 | #include <Guid/VariableFormat.h>\r | |
26 | #include <Guid/SystemNvDataGuid.h>\r | |
27 | \r | |
28 | #include "NorFlashDxe.h"\r | |
29 | \r | |
30 | \r | |
31 | ///\r | |
32 | /// The Firmware Volume Block Protocol is the low-level interface\r | |
33 | /// to a firmware volume. File-level access to a firmware volume\r | |
34 | /// should not be done using the Firmware Volume Block Protocol.\r | |
35 | /// Normal access to a firmware volume must use the Firmware\r | |
36 | /// Volume Protocol. Typically, only the file system driver that\r | |
37 | /// produces the Firmware Volume Protocol will bind to the\r | |
38 | /// Firmware Volume Block Protocol.\r | |
39 | ///\r | |
40 | \r | |
41 | /**\r | |
42 | Initialises the FV Header and Variable Store Header\r | |
43 | to support variable operations.\r | |
44 | \r | |
45 | @param[in] Ptr - Location to initialise the headers\r | |
46 | \r | |
47 | **/\r | |
48 | EFI_STATUS\r | |
49 | InitializeFvAndVariableStoreHeaders (\r | |
50 | IN NOR_FLASH_INSTANCE *Instance\r | |
51 | )\r | |
52 | {\r | |
53 | EFI_STATUS Status;\r | |
54 | VOID* Headers;\r | |
55 | UINTN HeadersLength;\r | |
56 | EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;\r | |
57 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
58 | \r | |
59 | if (!Instance->Initialized && Instance->Initialize) {\r | |
60 | Instance->Initialize (Instance);\r | |
61 | }\r | |
62 | \r | |
63 | HeadersLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(VARIABLE_STORE_HEADER);\r | |
64 | Headers = AllocateZeroPool(HeadersLength);\r | |
65 | \r | |
66 | // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.\r | |
67 | ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) + PcdGet32(PcdFlashNvStorageVariableSize) == PcdGet32(PcdFlashNvStorageFtwWorkingBase));\r | |
68 | ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) == PcdGet32(PcdFlashNvStorageFtwSpareBase));\r | |
69 | \r | |
70 | // Check if the size of the area is at least one block size\r | |
71 | ASSERT((PcdGet32(PcdFlashNvStorageVariableSize) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize) / Instance->Media.BlockSize > 0));\r | |
72 | ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize) / Instance->Media.BlockSize > 0));\r | |
73 | ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize) / Instance->Media.BlockSize > 0));\r | |
74 | \r | |
75 | // Ensure the Variable area Base Addresses are aligned on a block size boundaries\r | |
76 | ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) % Instance->Media.BlockSize == 0);\r | |
77 | ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) % Instance->Media.BlockSize == 0);\r | |
78 | ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase) % Instance->Media.BlockSize == 0);\r | |
79 | \r | |
80 | //\r | |
81 | // EFI_FIRMWARE_VOLUME_HEADER\r | |
82 | //\r | |
83 | FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Headers;\r | |
84 | CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);\r | |
85 | FirmwareVolumeHeader->FvLength =\r | |
86 | PcdGet32(PcdFlashNvStorageVariableSize) +\r | |
87 | PcdGet32(PcdFlashNvStorageFtwWorkingSize) +\r | |
88 | PcdGet32(PcdFlashNvStorageFtwSpareSize);\r | |
89 | FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;\r | |
90 | FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2) (\r | |
91 | EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled\r | |
92 | EFI_FVB2_READ_STATUS | // Reads are currently enabled\r | |
93 | EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY\r | |
94 | EFI_FVB2_MEMORY_MAPPED | // It is memory mapped\r | |
95 | EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')\r | |
96 | EFI_FVB2_WRITE_STATUS | // Writes are currently enabled\r | |
97 | EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled\r | |
98 | );\r | |
99 | FirmwareVolumeHeader->HeaderLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY);\r | |
100 | FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;\r | |
101 | FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->Media.LastBlock + 1;\r | |
102 | FirmwareVolumeHeader->BlockMap[0].Length = Instance->Media.BlockSize;\r | |
103 | FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;\r | |
104 | FirmwareVolumeHeader->BlockMap[1].Length = 0;\r | |
105 | FirmwareVolumeHeader->Checksum = CalculateCheckSum16 ((UINT16*)FirmwareVolumeHeader,FirmwareVolumeHeader->HeaderLength);\r | |
106 | \r | |
107 | //\r | |
108 | // VARIABLE_STORE_HEADER\r | |
109 | //\r | |
d5cd447b | 110 | VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);\r |
1e57a462 | 111 | CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);\r |
112 | VariableStoreHeader->Size = PcdGet32(PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;\r | |
113 | VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;\r | |
114 | VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;\r | |
115 | \r | |
116 | // Install the combined super-header in the NorFlash\r | |
117 | Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);\r | |
118 | \r | |
119 | FreePool (Headers);\r | |
120 | return Status;\r | |
121 | }\r | |
122 | \r | |
123 | /**\r | |
124 | Check the integrity of firmware volume header.\r | |
125 | \r | |
126 | @param[in] FwVolHeader - A pointer to a firmware volume header\r | |
127 | \r | |
128 | @retval EFI_SUCCESS - The firmware volume is consistent\r | |
129 | @retval EFI_NOT_FOUND - The firmware volume has been corrupted.\r | |
130 | \r | |
131 | **/\r | |
132 | EFI_STATUS\r | |
133 | ValidateFvHeader (\r | |
134 | IN NOR_FLASH_INSTANCE *Instance\r | |
135 | )\r | |
136 | {\r | |
137 | UINT16 Checksum;\r | |
138 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
139 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
140 | UINTN VariableStoreLength;\r | |
141 | UINTN FvLength;\r | |
142 | \r | |
143 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Instance->RegionBaseAddress;\r | |
144 | \r | |
145 | FvLength = PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +\r | |
146 | PcdGet32(PcdFlashNvStorageFtwSpareSize);\r | |
147 | \r | |
148 | //\r | |
149 | // Verify the header revision, header signature, length\r | |
150 | // Length of FvBlock cannot be 2**64-1\r | |
151 | // HeaderLength cannot be an odd number\r | |
152 | //\r | |
153 | if ( (FwVolHeader->Revision != EFI_FVH_REVISION)\r | |
154 | || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)\r | |
155 | || (FwVolHeader->FvLength != FvLength)\r | |
156 | )\r | |
157 | {\r | |
158 | DEBUG ((EFI_D_ERROR, "ValidateFvHeader: No Firmware Volume header present\n"));\r | |
159 | return EFI_NOT_FOUND;\r | |
160 | }\r | |
161 | \r | |
162 | // Check the Firmware Volume Guid\r | |
163 | if( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {\r | |
164 | DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Firmware Volume Guid non-compatible\n"));\r | |
165 | return EFI_NOT_FOUND;\r | |
166 | }\r | |
167 | \r | |
168 | // Verify the header checksum\r | |
169 | Checksum = CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLength);\r | |
170 | if (Checksum != 0) {\r | |
171 | DEBUG ((EFI_D_ERROR, "ValidateFvHeader: FV checksum is invalid (Checksum:0x%X)\n",Checksum));\r | |
172 | return EFI_NOT_FOUND;\r | |
173 | }\r | |
174 | \r | |
d5cd447b | 175 | VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);\r |
1e57a462 | 176 | \r |
177 | // Check the Variable Store Guid\r | |
178 | if( CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) == FALSE ) {\r | |
179 | DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Variable Store Guid non-compatible\n"));\r | |
180 | return EFI_NOT_FOUND;\r | |
181 | }\r | |
182 | \r | |
183 | VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;\r | |
184 | if (VariableStoreHeader->Size != VariableStoreLength) {\r | |
185 | DEBUG ((EFI_D_ERROR, "ValidateFvHeader: Variable Store Length does not match\n"));\r | |
186 | return EFI_NOT_FOUND;\r | |
187 | }\r | |
188 | \r | |
189 | return EFI_SUCCESS;\r | |
190 | }\r | |
191 | \r | |
192 | /**\r | |
193 | The GetAttributes() function retrieves the attributes and\r | |
194 | current settings of the block.\r | |
195 | \r | |
196 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
197 | \r | |
198 | @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and\r | |
199 | current settings are returned.\r | |
200 | Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.\r | |
201 | \r | |
202 | @retval EFI_SUCCESS The firmware volume attributes were returned.\r | |
203 | \r | |
204 | **/\r | |
205 | EFI_STATUS\r | |
206 | EFIAPI\r | |
207 | FvbGetAttributes(\r | |
208 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
209 | OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r | |
210 | )\r | |
211 | {\r | |
212 | EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;\r | |
213 | NOR_FLASH_INSTANCE *Instance;\r | |
214 | \r | |
215 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
216 | \r | |
217 | FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2) (\r | |
218 | \r | |
219 | EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled\r | |
220 | EFI_FVB2_READ_STATUS | // Reads are currently enabled\r | |
221 | EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY\r | |
222 | EFI_FVB2_MEMORY_MAPPED | // It is memory mapped\r | |
223 | EFI_FVB2_ERASE_POLARITY // After erasure all bits take this value (i.e. '1')\r | |
224 | \r | |
225 | );\r | |
226 | \r | |
227 | // Check if it is write protected\r | |
228 | if (Instance->Media.ReadOnly != TRUE) {\r | |
229 | \r | |
230 | FlashFvbAttributes = FlashFvbAttributes |\r | |
231 | EFI_FVB2_WRITE_STATUS | // Writes are currently enabled\r | |
232 | EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be enabled\r | |
233 | }\r | |
234 | \r | |
235 | *Attributes = FlashFvbAttributes;\r | |
236 | \r | |
237 | DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));\r | |
238 | \r | |
239 | return EFI_SUCCESS;\r | |
240 | }\r | |
241 | \r | |
242 | /**\r | |
243 | The SetAttributes() function sets configurable firmware volume attributes\r | |
244 | and returns the new settings of the firmware volume.\r | |
245 | \r | |
246 | \r | |
247 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
248 | \r | |
249 | @param Attributes On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2\r | |
250 | that contains the desired firmware volume settings.\r | |
251 | On successful return, it contains the new settings of\r | |
252 | the firmware volume.\r | |
253 | Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.\r | |
254 | \r | |
255 | @retval EFI_SUCCESS The firmware volume attributes were returned.\r | |
256 | \r | |
257 | @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with the capabilities\r | |
258 | as declared in the firmware volume header.\r | |
259 | \r | |
260 | **/\r | |
261 | EFI_STATUS\r | |
262 | EFIAPI\r | |
263 | FvbSetAttributes(\r | |
264 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
265 | IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r | |
266 | )\r | |
267 | {\r | |
268 | DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n",*Attributes));\r | |
269 | return EFI_UNSUPPORTED;\r | |
270 | }\r | |
271 | \r | |
272 | /**\r | |
273 | The GetPhysicalAddress() function retrieves the base address of\r | |
274 | a memory-mapped firmware volume. This function should be called\r | |
275 | only for memory-mapped firmware volumes.\r | |
276 | \r | |
277 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
278 | \r | |
279 | @param Address Pointer to a caller-allocated\r | |
280 | EFI_PHYSICAL_ADDRESS that, on successful\r | |
281 | return from GetPhysicalAddress(), contains the\r | |
282 | base address of the firmware volume.\r | |
283 | \r | |
284 | @retval EFI_SUCCESS The firmware volume base address was returned.\r | |
285 | \r | |
286 | @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.\r | |
287 | \r | |
288 | **/\r | |
289 | EFI_STATUS\r | |
290 | EFIAPI\r | |
291 | FvbGetPhysicalAddress (\r | |
292 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
293 | OUT EFI_PHYSICAL_ADDRESS *Address\r | |
294 | )\r | |
295 | {\r | |
296 | NOR_FLASH_INSTANCE *Instance;\r | |
297 | \r | |
298 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
299 | \r | |
300 | DEBUG ((DEBUG_BLKIO, "FvbGetPhysicalAddress(BaseAddress=0x%08x)\n", Instance->RegionBaseAddress));\r | |
301 | \r | |
302 | ASSERT(Address != NULL);\r | |
303 | \r | |
304 | *Address = PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
305 | return EFI_SUCCESS;\r | |
306 | }\r | |
307 | \r | |
308 | /**\r | |
309 | The GetBlockSize() function retrieves the size of the requested\r | |
310 | block. It also returns the number of additional blocks with\r | |
311 | the identical size. The GetBlockSize() function is used to\r | |
312 | retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).\r | |
313 | \r | |
314 | \r | |
315 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
316 | \r | |
317 | @param Lba Indicates the block for which to return the size.\r | |
318 | \r | |
319 | @param BlockSize Pointer to a caller-allocated UINTN in which\r | |
320 | the size of the block is returned.\r | |
321 | \r | |
322 | @param NumberOfBlocks Pointer to a caller-allocated UINTN in\r | |
323 | which the number of consecutive blocks,\r | |
324 | starting with Lba, is returned. All\r | |
325 | blocks in this range have a size of\r | |
326 | BlockSize.\r | |
327 | \r | |
328 | \r | |
329 | @retval EFI_SUCCESS The firmware volume base address was returned.\r | |
330 | \r | |
331 | @retval EFI_INVALID_PARAMETER The requested LBA is out of range.\r | |
332 | \r | |
333 | **/\r | |
334 | EFI_STATUS\r | |
335 | EFIAPI\r | |
336 | FvbGetBlockSize (\r | |
337 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
338 | IN EFI_LBA Lba,\r | |
339 | OUT UINTN *BlockSize,\r | |
340 | OUT UINTN *NumberOfBlocks\r | |
341 | )\r | |
342 | {\r | |
343 | EFI_STATUS Status;\r | |
344 | NOR_FLASH_INSTANCE *Instance;\r | |
345 | \r | |
346 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
347 | \r | |
348 | DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock));\r | |
349 | \r | |
350 | if (Lba > Instance->Media.LastBlock) {\r | |
351 | DEBUG ((EFI_D_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->Media.LastBlock));\r | |
352 | Status = EFI_INVALID_PARAMETER;\r | |
353 | } else {\r | |
354 | // This is easy because in this platform each NorFlash device has equal sized blocks.\r | |
355 | *BlockSize = (UINTN) Instance->Media.BlockSize;\r | |
356 | *NumberOfBlocks = (UINTN) (Instance->Media.LastBlock - Lba + 1);\r | |
357 | \r | |
358 | DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));\r | |
359 | \r | |
360 | Status = EFI_SUCCESS;\r | |
361 | }\r | |
362 | \r | |
363 | return Status;\r | |
364 | }\r | |
365 | \r | |
366 | /**\r | |
367 | Reads the specified number of bytes into a buffer from the specified block.\r | |
368 | \r | |
369 | The Read() function reads the requested number of bytes from the\r | |
370 | requested block and stores them in the provided buffer.\r | |
371 | Implementations should be mindful that the firmware volume\r | |
372 | might be in the ReadDisabled state. If it is in this state,\r | |
373 | the Read() function must return the status code\r | |
374 | EFI_ACCESS_DENIED without modifying the contents of the\r | |
375 | buffer. The Read() function must also prevent spanning block\r | |
376 | boundaries. If a read is requested that would span a block\r | |
377 | boundary, the read must read up to the boundary but not\r | |
378 | beyond. The output parameter NumBytes must be set to correctly\r | |
379 | indicate the number of bytes actually read. The caller must be\r | |
380 | aware that a read may be partially completed.\r | |
381 | \r | |
382 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
383 | \r | |
384 | @param Lba The starting logical block index from which to read.\r | |
385 | \r | |
386 | @param Offset Offset into the block at which to begin reading.\r | |
387 | \r | |
388 | @param NumBytes Pointer to a UINTN.\r | |
389 | At entry, *NumBytes contains the total size of the buffer.\r | |
390 | At exit, *NumBytes contains the total number of bytes read.\r | |
391 | \r | |
392 | @param Buffer Pointer to a caller-allocated buffer that will be used\r | |
393 | to hold the data that is read.\r | |
394 | \r | |
395 | @retval EFI_SUCCESS The firmware volume was read successfully, and contents are\r | |
396 | in Buffer.\r | |
397 | \r | |
398 | @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.\r | |
399 | On output, NumBytes contains the total number of bytes\r | |
400 | returned in Buffer.\r | |
401 | \r | |
402 | @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.\r | |
403 | \r | |
404 | @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be read.\r | |
405 | \r | |
406 | **/\r | |
407 | EFI_STATUS\r | |
408 | EFIAPI\r | |
409 | FvbRead (\r | |
410 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
411 | IN EFI_LBA Lba,\r | |
412 | IN UINTN Offset,\r | |
413 | IN OUT UINTN *NumBytes,\r | |
414 | IN OUT UINT8 *Buffer\r | |
415 | )\r | |
416 | {\r | |
417 | EFI_STATUS Status;\r | |
418 | EFI_STATUS TempStatus;\r | |
419 | UINTN BlockSize;\r | |
1e57a462 | 420 | NOR_FLASH_INSTANCE *Instance;\r |
421 | \r | |
422 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
423 | \r | |
424 | DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));\r | |
425 | \r | |
426 | if (!Instance->Initialized && Instance->Initialize) {\r | |
427 | Instance->Initialize(Instance);\r | |
428 | }\r | |
429 | \r | |
430 | Status = EFI_SUCCESS;\r | |
431 | TempStatus = Status;\r | |
432 | \r | |
433 | // Cache the block size to avoid de-referencing pointers all the time\r | |
434 | BlockSize = Instance->Media.BlockSize;\r | |
435 | \r | |
436 | DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
437 | \r | |
438 | // The read must not span block boundaries.\r | |
439 | // We need to check each variable individually because adding two large values together overflows.\r | |
440 | if ((Offset >= BlockSize) ||\r | |
441 | (*NumBytes > BlockSize) ||\r | |
442 | ((Offset + *NumBytes) > BlockSize)) {\r | |
443 | DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
444 | return EFI_BAD_BUFFER_SIZE;\r | |
445 | }\r | |
446 | \r | |
447 | // We must have some bytes to read\r | |
448 | if (*NumBytes == 0) {\r | |
449 | return EFI_BAD_BUFFER_SIZE;\r | |
450 | }\r | |
451 | \r | |
2dff0c1a OM |
452 | // Check we did get some memory\r |
453 | if (Instance->FvbBuffer == NULL) {\r | |
454 | DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - Buffer not ready\n"));\r | |
1e57a462 | 455 | return EFI_DEVICE_ERROR;\r |
456 | }\r | |
457 | \r | |
458 | // Read NOR Flash data into shadow buffer\r | |
2dff0c1a | 459 | TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r |
1e57a462 | 460 | if (EFI_ERROR (TempStatus)) {\r |
461 | // Return one of the pre-approved error statuses\r | |
2dff0c1a | 462 | return EFI_DEVICE_ERROR;\r |
1e57a462 | 463 | }\r |
464 | \r | |
465 | // Put the data at the appropriate location inside the buffer area\r | |
2dff0c1a | 466 | DEBUG ((DEBUG_BLKIO, "FvbRead: CopyMem( Dst=0x%08x, Src=0x%08x, Size=0x%x ).\n", Buffer, (UINTN)Instance->FvbBuffer + Offset, *NumBytes));\r |
1e57a462 | 467 | \r |
2dff0c1a | 468 | CopyMem (Buffer, (VOID*)((UINTN)Instance->FvbBuffer + Offset), *NumBytes);\r |
1e57a462 | 469 | \r |
1e57a462 | 470 | return Status;\r |
471 | }\r | |
472 | \r | |
473 | /**\r | |
474 | Writes the specified number of bytes from the input buffer to the block.\r | |
475 | \r | |
476 | The Write() function writes the specified number of bytes from\r | |
477 | the provided buffer to the specified block and offset. If the\r | |
478 | firmware volume is sticky write, the caller must ensure that\r | |
479 | all the bits of the specified range to write are in the\r | |
480 | EFI_FVB_ERASE_POLARITY state before calling the Write()\r | |
481 | function, or else the result will be unpredictable. This\r | |
482 | unpredictability arises because, for a sticky-write firmware\r | |
483 | volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY\r | |
484 | state but cannot flip it back again. Before calling the\r | |
485 | Write() function, it is recommended for the caller to first call\r | |
486 | the EraseBlocks() function to erase the specified block to\r | |
487 | write. A block erase cycle will transition bits from the\r | |
488 | (NOT)EFI_FVB_ERASE_POLARITY state back to the\r | |
489 | EFI_FVB_ERASE_POLARITY state. Implementations should be\r | |
490 | mindful that the firmware volume might be in the WriteDisabled\r | |
491 | state. If it is in this state, the Write() function must\r | |
492 | return the status code EFI_ACCESS_DENIED without modifying the\r | |
493 | contents of the firmware volume. The Write() function must\r | |
494 | also prevent spanning block boundaries. If a write is\r | |
495 | requested that spans a block boundary, the write must store up\r | |
496 | to the boundary but not beyond. The output parameter NumBytes\r | |
497 | must be set to correctly indicate the number of bytes actually\r | |
498 | written. The caller must be aware that a write may be\r | |
499 | partially completed. All writes, partial or otherwise, must be\r | |
500 | fully flushed to the hardware before the Write() service\r | |
501 | returns.\r | |
502 | \r | |
503 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
504 | \r | |
505 | @param Lba The starting logical block index to write to.\r | |
506 | \r | |
507 | @param Offset Offset into the block at which to begin writing.\r | |
508 | \r | |
509 | @param NumBytes The pointer to a UINTN.\r | |
510 | At entry, *NumBytes contains the total size of the buffer.\r | |
511 | At exit, *NumBytes contains the total number of bytes actually written.\r | |
512 | \r | |
513 | @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.\r | |
514 | \r | |
515 | @retval EFI_SUCCESS The firmware volume was written successfully.\r | |
516 | \r | |
517 | @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.\r | |
518 | On output, NumBytes contains the total number of bytes\r | |
519 | actually written.\r | |
520 | \r | |
521 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.\r | |
522 | \r | |
523 | @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.\r | |
524 | \r | |
525 | \r | |
526 | **/\r | |
527 | EFI_STATUS\r | |
528 | EFIAPI\r | |
529 | FvbWrite (\r | |
530 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
531 | IN EFI_LBA Lba,\r | |
532 | IN UINTN Offset,\r | |
533 | IN OUT UINTN *NumBytes,\r | |
534 | IN UINT8 *Buffer\r | |
535 | )\r | |
536 | {\r | |
537 | EFI_STATUS Status;\r | |
538 | EFI_STATUS TempStatus;\r | |
539 | UINTN BlockSize;\r | |
1e57a462 | 540 | NOR_FLASH_INSTANCE *Instance;\r |
541 | \r | |
542 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
543 | \r | |
544 | if (!Instance->Initialized && Instance->Initialize) {\r | |
545 | Instance->Initialize(Instance);\r | |
546 | }\r | |
547 | \r | |
548 | DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));\r | |
549 | \r | |
550 | Status = EFI_SUCCESS;\r | |
551 | TempStatus = Status;\r | |
552 | \r | |
553 | // Detect WriteDisabled state\r | |
554 | if (Instance->Media.ReadOnly == TRUE) {\r | |
555 | DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Can not write: Device is in WriteDisabled state.\n"));\r | |
556 | // It is in WriteDisabled state, return an error right away\r | |
557 | return EFI_ACCESS_DENIED;\r | |
558 | }\r | |
559 | \r | |
560 | // Cache the block size to avoid de-referencing pointers all the time\r | |
561 | BlockSize = Instance->Media.BlockSize;\r | |
562 | \r | |
563 | // The write must not span block boundaries.\r | |
564 | // We need to check each variable individually because adding two large values together overflows.\r | |
565 | if ( ( Offset >= BlockSize ) ||\r | |
566 | ( *NumBytes > BlockSize ) ||\r | |
567 | ( (Offset + *NumBytes) > BlockSize ) ) {\r | |
568 | DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
569 | return EFI_BAD_BUFFER_SIZE;\r | |
570 | }\r | |
571 | \r | |
572 | // We must have some bytes to write\r | |
573 | if (*NumBytes == 0) {\r | |
574 | DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
575 | return EFI_BAD_BUFFER_SIZE;\r | |
576 | }\r | |
577 | \r | |
1e57a462 | 578 | // Check we did get some memory\r |
2dff0c1a OM |
579 | if (Instance->FvbBuffer == NULL) {\r |
580 | DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r | |
1e57a462 | 581 | return EFI_DEVICE_ERROR;\r |
582 | }\r | |
583 | \r | |
584 | // Read NOR Flash data into shadow buffer\r | |
2dff0c1a | 585 | TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r |
1e57a462 | 586 | if (EFI_ERROR (TempStatus)) {\r |
587 | // Return one of the pre-approved error statuses\r | |
2dff0c1a | 588 | return EFI_DEVICE_ERROR;\r |
1e57a462 | 589 | }\r |
590 | \r | |
591 | // Put the data at the appropriate location inside the buffer area\r | |
2dff0c1a | 592 | CopyMem ((VOID*)((UINTN)Instance->FvbBuffer + Offset), Buffer, *NumBytes);\r |
1e57a462 | 593 | \r |
594 | // Write the modified buffer back to the NorFlash\r | |
2dff0c1a | 595 | TempStatus = NorFlashWriteBlocks (Instance, Instance->StartLba + Lba, BlockSize, Instance->FvbBuffer);\r |
1e57a462 | 596 | if (EFI_ERROR (TempStatus)) {\r |
597 | // Return one of the pre-approved error statuses\r | |
2dff0c1a | 598 | return EFI_DEVICE_ERROR;\r |
1e57a462 | 599 | }\r |
600 | \r | |
1e57a462 | 601 | return Status;\r |
602 | }\r | |
603 | \r | |
604 | /**\r | |
605 | Erases and initialises a firmware volume block.\r | |
606 | \r | |
607 | The EraseBlocks() function erases one or more blocks as denoted\r | |
608 | by the variable argument list. The entire parameter list of\r | |
609 | blocks must be verified before erasing any blocks. If a block is\r | |
610 | requested that does not exist within the associated firmware\r | |
611 | volume (it has a larger index than the last block of the\r | |
612 | firmware volume), the EraseBlocks() function must return the\r | |
613 | status code EFI_INVALID_PARAMETER without modifying the contents\r | |
614 | of the firmware volume. Implementations should be mindful that\r | |
615 | the firmware volume might be in the WriteDisabled state. If it\r | |
616 | is in this state, the EraseBlocks() function must return the\r | |
617 | status code EFI_ACCESS_DENIED without modifying the contents of\r | |
618 | the firmware volume. All calls to EraseBlocks() must be fully\r | |
619 | flushed to the hardware before the EraseBlocks() service\r | |
620 | returns.\r | |
621 | \r | |
622 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL\r | |
623 | instance.\r | |
624 | \r | |
625 | @param ... The variable argument list is a list of tuples.\r | |
626 | Each tuple describes a range of LBAs to erase\r | |
627 | and consists of the following:\r | |
628 | - An EFI_LBA that indicates the starting LBA\r | |
629 | - A UINTN that indicates the number of blocks to erase.\r | |
630 | \r | |
631 | The list is terminated with an EFI_LBA_LIST_TERMINATOR.\r | |
632 | For example, the following indicates that two ranges of blocks\r | |
633 | (5-7 and 10-11) are to be erased:\r | |
634 | EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);\r | |
635 | \r | |
636 | @retval EFI_SUCCESS The erase request successfully completed.\r | |
637 | \r | |
638 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.\r | |
639 | \r | |
640 | @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.\r | |
641 | The firmware device may have been partially erased.\r | |
642 | \r | |
643 | @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do\r | |
644 | not exist in the firmware volume.\r | |
645 | \r | |
646 | **/\r | |
647 | EFI_STATUS\r | |
648 | EFIAPI\r | |
649 | FvbEraseBlocks (\r | |
650 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
651 | ...\r | |
652 | )\r | |
653 | {\r | |
654 | EFI_STATUS Status;\r | |
655 | VA_LIST Args;\r | |
656 | UINTN BlockAddress; // Physical address of Lba to erase\r | |
657 | EFI_LBA StartingLba; // Lba from which we start erasing\r | |
658 | UINTN NumOfLba; // Number of Lba blocks to erase\r | |
659 | NOR_FLASH_INSTANCE *Instance;\r | |
660 | \r | |
661 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
662 | \r | |
663 | DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));\r | |
664 | \r | |
665 | Status = EFI_SUCCESS;\r | |
666 | \r | |
667 | // Detect WriteDisabled state\r | |
668 | if (Instance->Media.ReadOnly == TRUE) {\r | |
669 | // Firmware volume is in WriteDisabled state\r | |
670 | DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));\r | |
671 | return EFI_ACCESS_DENIED;\r | |
672 | }\r | |
673 | \r | |
674 | // Before erasing, check the entire list of parameters to ensure all specified blocks are valid\r | |
675 | \r | |
676 | VA_START (Args, This);\r | |
677 | do {\r | |
678 | // Get the Lba from which we start erasing\r | |
679 | StartingLba = VA_ARG (Args, EFI_LBA);\r | |
680 | \r | |
681 | // Have we reached the end of the list?\r | |
682 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
683 | //Exit the while loop\r | |
684 | break;\r | |
685 | }\r | |
686 | \r | |
687 | // How many Lba blocks are we requested to erase?\r | |
688 | NumOfLba = VA_ARG (Args, UINT32);\r | |
689 | \r | |
690 | // All blocks must be within range\r | |
691 | DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", Instance->StartLba + StartingLba, NumOfLba, Instance->Media.LastBlock));\r | |
692 | if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) {\r | |
693 | VA_END (Args);\r | |
694 | DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));\r | |
695 | Status = EFI_INVALID_PARAMETER;\r | |
696 | goto EXIT;\r | |
697 | }\r | |
698 | } while (TRUE);\r | |
699 | VA_END (Args);\r | |
700 | \r | |
701 | //\r | |
702 | // To get here, all must be ok, so start erasing\r | |
703 | //\r | |
704 | VA_START (Args, This);\r | |
705 | do {\r | |
706 | // Get the Lba from which we start erasing\r | |
707 | StartingLba = VA_ARG (Args, EFI_LBA);\r | |
708 | \r | |
709 | // Have we reached the end of the list?\r | |
710 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
711 | // Exit the while loop\r | |
712 | break;\r | |
713 | }\r | |
714 | \r | |
715 | // How many Lba blocks are we requested to erase?\r | |
716 | NumOfLba = VA_ARG (Args, UINT32);\r | |
717 | \r | |
718 | // Go through each one and erase it\r | |
719 | while (NumOfLba > 0) {\r | |
720 | \r | |
721 | // Get the physical address of Lba to erase\r | |
722 | BlockAddress = GET_NOR_BLOCK_ADDRESS (\r | |
723 | Instance->RegionBaseAddress,\r | |
724 | Instance->StartLba + StartingLba,\r | |
725 | Instance->Media.BlockSize\r | |
726 | );\r | |
727 | \r | |
728 | // Erase it\r | |
729 | DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress));\r | |
730 | Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);\r | |
731 | if (EFI_ERROR(Status)) {\r | |
732 | VA_END (Args);\r | |
733 | Status = EFI_DEVICE_ERROR;\r | |
734 | goto EXIT;\r | |
735 | }\r | |
736 | \r | |
737 | // Move to the next Lba\r | |
738 | StartingLba++;\r | |
739 | NumOfLba--;\r | |
740 | }\r | |
741 | } while (TRUE);\r | |
742 | VA_END (Args);\r | |
743 | \r | |
744 | EXIT:\r | |
745 | return Status;\r | |
746 | }\r | |
747 | \r | |
748 | EFI_STATUS\r | |
749 | EFIAPI\r | |
750 | NorFlashFvbInitialize (\r | |
751 | IN NOR_FLASH_INSTANCE* Instance\r | |
752 | )\r | |
753 | {\r | |
754 | EFI_STATUS Status;\r | |
755 | UINT32 FvbNumLba;\r | |
756 | EFI_BOOT_MODE BootMode;\r | |
757 | \r | |
758 | DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n"));\r | |
759 | \r | |
760 | Instance->Initialized = TRUE;\r | |
761 | \r | |
762 | // Set the index of the first LBA for the FVB\r | |
763 | Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;\r | |
764 | \r | |
765 | BootMode = GetBootModeHob ();\r | |
766 | if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r | |
767 | Status = EFI_INVALID_PARAMETER;\r | |
768 | } else {\r | |
769 | // Determine if there is a valid header at the beginning of the NorFlash\r | |
770 | Status = ValidateFvHeader (Instance);\r | |
771 | }\r | |
772 | \r | |
773 | // Install the Default FVB header if required \r | |
774 | if (EFI_ERROR(Status)) {\r | |
775 | // There is no valid header, so time to install one.\r | |
776 | DEBUG((EFI_D_ERROR,"NorFlashFvbInitialize: ERROR - The FVB Header is not valid. Installing a correct one for this volume.\n"));\r | |
777 | \r | |
778 | // Erase all the NorFlash that is reserved for variable storage\r | |
779 | FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;\r | |
780 | \r | |
781 | Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);\r | |
782 | if (EFI_ERROR(Status)) {\r | |
783 | return Status;\r | |
784 | }\r | |
785 | \r | |
786 | // Install all appropriate headers\r | |
787 | Status = InitializeFvAndVariableStoreHeaders (Instance);\r | |
788 | if (EFI_ERROR(Status)) {\r | |
789 | return Status;\r | |
790 | }\r | |
791 | }\r | |
792 | return Status;\r | |
793 | }\r |