]>
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 |
f4dfad05 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
1e57a462 | 6 | \r |
7 | --*/\r | |
8 | \r | |
9 | #include <PiDxe.h>\r | |
10 | \r | |
11 | #include <Library/PcdLib.h>\r | |
12 | #include <Library/BaseLib.h>\r | |
13 | #include <Library/HobLib.h>\r | |
14 | #include <Library/UefiLib.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | #include <Library/MemoryAllocationLib.h>\r | |
1dbbfc17 | 17 | #include <Library/DxeServicesTableLib.h>\r |
1e57a462 | 18 | #include <Library/UefiBootServicesTableLib.h>\r |
19 | \r | |
20 | #include <Guid/VariableFormat.h>\r | |
21 | #include <Guid/SystemNvDataGuid.h>\r | |
6281a2ed | 22 | #include <Guid/NvVarStoreFormatted.h>\r |
1e57a462 | 23 | \r |
24 | #include "NorFlashDxe.h"\r | |
25 | \r | |
1dbbfc17 OM |
26 | STATIC EFI_EVENT mFvbVirtualAddrChangeEvent;\r |
27 | STATIC UINTN mFlashNvStorageVariableBase;\r | |
1e57a462 | 28 | \r |
29 | ///\r | |
30 | /// The Firmware Volume Block Protocol is the low-level interface\r | |
31 | /// to a firmware volume. File-level access to a firmware volume\r | |
32 | /// should not be done using the Firmware Volume Block Protocol.\r | |
33 | /// Normal access to a firmware volume must use the Firmware\r | |
34 | /// Volume Protocol. Typically, only the file system driver that\r | |
35 | /// produces the Firmware Volume Protocol will bind to the\r | |
36 | /// Firmware Volume Block Protocol.\r | |
37 | ///\r | |
38 | \r | |
39 | /**\r | |
40 | Initialises the FV Header and Variable Store Header\r | |
41 | to support variable operations.\r | |
42 | \r | |
43 | @param[in] Ptr - Location to initialise the headers\r | |
44 | \r | |
45 | **/\r | |
46 | EFI_STATUS\r | |
47 | InitializeFvAndVariableStoreHeaders (\r | |
48 | IN NOR_FLASH_INSTANCE *Instance\r | |
49 | )\r | |
50 | {\r | |
51 | EFI_STATUS Status;\r | |
52 | VOID* Headers;\r | |
53 | UINTN HeadersLength;\r | |
54 | EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader;\r | |
55 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
56 | \r | |
1e57a462 | 57 | HeadersLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(VARIABLE_STORE_HEADER);\r |
58 | Headers = AllocateZeroPool(HeadersLength);\r | |
59 | \r | |
60 | // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.\r | |
61 | ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) + PcdGet32(PcdFlashNvStorageVariableSize) == PcdGet32(PcdFlashNvStorageFtwWorkingBase));\r | |
62 | ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) == PcdGet32(PcdFlashNvStorageFtwSpareBase));\r | |
63 | \r | |
64 | // Check if the size of the area is at least one block size\r | |
65 | ASSERT((PcdGet32(PcdFlashNvStorageVariableSize) > 0) && (PcdGet32(PcdFlashNvStorageVariableSize) / Instance->Media.BlockSize > 0));\r | |
66 | ASSERT((PcdGet32(PcdFlashNvStorageFtwWorkingSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwWorkingSize) / Instance->Media.BlockSize > 0));\r | |
67 | ASSERT((PcdGet32(PcdFlashNvStorageFtwSpareSize) > 0) && (PcdGet32(PcdFlashNvStorageFtwSpareSize) / Instance->Media.BlockSize > 0));\r | |
68 | \r | |
69 | // Ensure the Variable area Base Addresses are aligned on a block size boundaries\r | |
70 | ASSERT(PcdGet32(PcdFlashNvStorageVariableBase) % Instance->Media.BlockSize == 0);\r | |
71 | ASSERT(PcdGet32(PcdFlashNvStorageFtwWorkingBase) % Instance->Media.BlockSize == 0);\r | |
72 | ASSERT(PcdGet32(PcdFlashNvStorageFtwSpareBase) % Instance->Media.BlockSize == 0);\r | |
73 | \r | |
74 | //\r | |
75 | // EFI_FIRMWARE_VOLUME_HEADER\r | |
76 | //\r | |
77 | FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Headers;\r | |
78 | CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);\r | |
79 | FirmwareVolumeHeader->FvLength =\r | |
80 | PcdGet32(PcdFlashNvStorageVariableSize) +\r | |
81 | PcdGet32(PcdFlashNvStorageFtwWorkingSize) +\r | |
82 | PcdGet32(PcdFlashNvStorageFtwSpareSize);\r | |
83 | FirmwareVolumeHeader->Signature = EFI_FVH_SIGNATURE;\r | |
84 | FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2) (\r | |
85 | EFI_FVB2_READ_ENABLED_CAP | // Reads may be enabled\r | |
86 | EFI_FVB2_READ_STATUS | // Reads are currently enabled\r | |
87 | EFI_FVB2_STICKY_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY\r | |
88 | EFI_FVB2_MEMORY_MAPPED | // It is memory mapped\r | |
89 | EFI_FVB2_ERASE_POLARITY | // After erasure all bits take this value (i.e. '1')\r | |
90 | EFI_FVB2_WRITE_STATUS | // Writes are currently enabled\r | |
91 | EFI_FVB2_WRITE_ENABLED_CAP // Writes may be enabled\r | |
92 | );\r | |
93 | FirmwareVolumeHeader->HeaderLength = sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY);\r | |
94 | FirmwareVolumeHeader->Revision = EFI_FVH_REVISION;\r | |
95 | FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->Media.LastBlock + 1;\r | |
96 | FirmwareVolumeHeader->BlockMap[0].Length = Instance->Media.BlockSize;\r | |
97 | FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;\r | |
98 | FirmwareVolumeHeader->BlockMap[1].Length = 0;\r | |
99 | FirmwareVolumeHeader->Checksum = CalculateCheckSum16 ((UINT16*)FirmwareVolumeHeader,FirmwareVolumeHeader->HeaderLength);\r | |
100 | \r | |
101 | //\r | |
102 | // VARIABLE_STORE_HEADER\r | |
103 | //\r | |
d5cd447b | 104 | VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);\r |
8753858f | 105 | CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid);\r |
1e57a462 | 106 | VariableStoreHeader->Size = PcdGet32(PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;\r |
107 | VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;\r | |
108 | VariableStoreHeader->State = VARIABLE_STORE_HEALTHY;\r | |
109 | \r | |
110 | // Install the combined super-header in the NorFlash\r | |
111 | Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);\r | |
112 | \r | |
113 | FreePool (Headers);\r | |
114 | return Status;\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | Check the integrity of firmware volume header.\r | |
119 | \r | |
120 | @param[in] FwVolHeader - A pointer to a firmware volume header\r | |
121 | \r | |
122 | @retval EFI_SUCCESS - The firmware volume is consistent\r | |
123 | @retval EFI_NOT_FOUND - The firmware volume has been corrupted.\r | |
124 | \r | |
125 | **/\r | |
126 | EFI_STATUS\r | |
127 | ValidateFvHeader (\r | |
128 | IN NOR_FLASH_INSTANCE *Instance\r | |
129 | )\r | |
130 | {\r | |
131 | UINT16 Checksum;\r | |
132 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
133 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
134 | UINTN VariableStoreLength;\r | |
91c38d4e | 135 | UINTN FvLength;\r |
1e57a462 | 136 | \r |
137 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Instance->RegionBaseAddress;\r | |
138 | \r | |
139 | FvLength = PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) +\r | |
140 | PcdGet32(PcdFlashNvStorageFtwSpareSize);\r | |
141 | \r | |
142 | //\r | |
143 | // Verify the header revision, header signature, length\r | |
144 | // Length of FvBlock cannot be 2**64-1\r | |
145 | // HeaderLength cannot be an odd number\r | |
146 | //\r | |
147 | if ( (FwVolHeader->Revision != EFI_FVH_REVISION)\r | |
148 | || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)\r | |
149 | || (FwVolHeader->FvLength != FvLength)\r | |
150 | )\r | |
151 | {\r | |
582085eb LE |
152 | DEBUG ((EFI_D_INFO, "%a: No Firmware Volume header present\n",\r |
153 | __FUNCTION__));\r | |
1e57a462 | 154 | return EFI_NOT_FOUND;\r |
155 | }\r | |
156 | \r | |
157 | // Check the Firmware Volume Guid\r | |
158 | if( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {\r | |
582085eb LE |
159 | DEBUG ((EFI_D_INFO, "%a: Firmware Volume Guid non-compatible\n",\r |
160 | __FUNCTION__));\r | |
1e57a462 | 161 | return EFI_NOT_FOUND;\r |
162 | }\r | |
163 | \r | |
164 | // Verify the header checksum\r | |
165 | Checksum = CalculateSum16((UINT16*)FwVolHeader, FwVolHeader->HeaderLength);\r | |
166 | if (Checksum != 0) {\r | |
582085eb LE |
167 | DEBUG ((EFI_D_INFO, "%a: FV checksum is invalid (Checksum:0x%X)\n",\r |
168 | __FUNCTION__, Checksum));\r | |
1e57a462 | 169 | return EFI_NOT_FOUND;\r |
170 | }\r | |
171 | \r | |
d5cd447b | 172 | VariableStoreHeader = (VARIABLE_STORE_HEADER*)((UINTN)FwVolHeader + FwVolHeader->HeaderLength);\r |
1e57a462 | 173 | \r |
174 | // Check the Variable Store Guid\r | |
8753858f AB |
175 | if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&\r |
176 | !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid)) {\r | |
582085eb LE |
177 | DEBUG ((EFI_D_INFO, "%a: Variable Store Guid non-compatible\n",\r |
178 | __FUNCTION__));\r | |
1e57a462 | 179 | return EFI_NOT_FOUND;\r |
180 | }\r | |
181 | \r | |
182 | VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;\r | |
183 | if (VariableStoreHeader->Size != VariableStoreLength) {\r | |
582085eb LE |
184 | DEBUG ((EFI_D_INFO, "%a: Variable Store Length does not match\n",\r |
185 | __FUNCTION__));\r | |
1e57a462 | 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 | |
1dbbfc17 | 304 | *Address = mFlashNvStorageVariableBase;\r |
1e57a462 | 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 | |
1e57a462 | 417 | EFI_STATUS TempStatus;\r |
418 | UINTN BlockSize;\r | |
1e57a462 | 419 | NOR_FLASH_INSTANCE *Instance;\r |
420 | \r | |
421 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
422 | \r | |
423 | DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Instance->StartLba + Lba, Offset, *NumBytes, Buffer));\r | |
424 | \r | |
518c243d | 425 | TempStatus = EFI_SUCCESS;\r |
1e57a462 | 426 | \r |
427 | // Cache the block size to avoid de-referencing pointers all the time\r | |
428 | BlockSize = Instance->Media.BlockSize;\r | |
429 | \r | |
430 | DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=0x%x + NumBytes=0x%x) <= BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
431 | \r | |
432 | // The read must not span block boundaries.\r | |
433 | // We need to check each variable individually because adding two large values together overflows.\r | |
434 | if ((Offset >= BlockSize) ||\r | |
435 | (*NumBytes > BlockSize) ||\r | |
436 | ((Offset + *NumBytes) > BlockSize)) {\r | |
437 | DEBUG ((EFI_D_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r | |
438 | return EFI_BAD_BUFFER_SIZE;\r | |
439 | }\r | |
440 | \r | |
441 | // We must have some bytes to read\r | |
442 | if (*NumBytes == 0) {\r | |
443 | return EFI_BAD_BUFFER_SIZE;\r | |
444 | }\r | |
445 | \r | |
518c243d HL |
446 | // Decide if we are doing full block reads or not.\r |
447 | if (*NumBytes % BlockSize != 0) {\r | |
448 | TempStatus = NorFlashRead (Instance, Instance->StartLba + Lba, Offset, *NumBytes, Buffer);\r | |
449 | if (EFI_ERROR (TempStatus)) {\r | |
450 | return EFI_DEVICE_ERROR;\r | |
451 | }\r | |
452 | } else {\r | |
453 | // Read NOR Flash data into shadow buffer\r | |
454 | TempStatus = NorFlashReadBlocks (Instance, Instance->StartLba + Lba, BlockSize, Buffer);\r | |
455 | if (EFI_ERROR (TempStatus)) {\r | |
456 | // Return one of the pre-approved error statuses\r | |
457 | return EFI_DEVICE_ERROR;\r | |
458 | }\r | |
1e57a462 | 459 | }\r |
518c243d | 460 | return EFI_SUCCESS;\r |
1e57a462 | 461 | }\r |
462 | \r | |
463 | /**\r | |
464 | Writes the specified number of bytes from the input buffer to the block.\r | |
465 | \r | |
466 | The Write() function writes the specified number of bytes from\r | |
467 | the provided buffer to the specified block and offset. If the\r | |
468 | firmware volume is sticky write, the caller must ensure that\r | |
469 | all the bits of the specified range to write are in the\r | |
470 | EFI_FVB_ERASE_POLARITY state before calling the Write()\r | |
471 | function, or else the result will be unpredictable. This\r | |
472 | unpredictability arises because, for a sticky-write firmware\r | |
473 | volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY\r | |
474 | state but cannot flip it back again. Before calling the\r | |
475 | Write() function, it is recommended for the caller to first call\r | |
476 | the EraseBlocks() function to erase the specified block to\r | |
477 | write. A block erase cycle will transition bits from the\r | |
478 | (NOT)EFI_FVB_ERASE_POLARITY state back to the\r | |
479 | EFI_FVB_ERASE_POLARITY state. Implementations should be\r | |
480 | mindful that the firmware volume might be in the WriteDisabled\r | |
481 | state. If it is in this state, the Write() function must\r | |
482 | return the status code EFI_ACCESS_DENIED without modifying the\r | |
483 | contents of the firmware volume. The Write() function must\r | |
484 | also prevent spanning block boundaries. If a write is\r | |
485 | requested that spans a block boundary, the write must store up\r | |
486 | to the boundary but not beyond. The output parameter NumBytes\r | |
487 | must be set to correctly indicate the number of bytes actually\r | |
488 | written. The caller must be aware that a write may be\r | |
489 | partially completed. All writes, partial or otherwise, must be\r | |
490 | fully flushed to the hardware before the Write() service\r | |
491 | returns.\r | |
492 | \r | |
493 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.\r | |
494 | \r | |
495 | @param Lba The starting logical block index to write to.\r | |
496 | \r | |
497 | @param Offset Offset into the block at which to begin writing.\r | |
498 | \r | |
499 | @param NumBytes The pointer to a UINTN.\r | |
500 | At entry, *NumBytes contains the total size of the buffer.\r | |
501 | At exit, *NumBytes contains the total number of bytes actually written.\r | |
502 | \r | |
503 | @param Buffer The pointer to a caller-allocated buffer that contains the source for the write.\r | |
504 | \r | |
505 | @retval EFI_SUCCESS The firmware volume was written successfully.\r | |
506 | \r | |
507 | @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.\r | |
508 | On output, NumBytes contains the total number of bytes\r | |
509 | actually written.\r | |
510 | \r | |
511 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.\r | |
512 | \r | |
513 | @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not be written.\r | |
514 | \r | |
515 | \r | |
516 | **/\r | |
517 | EFI_STATUS\r | |
518 | EFIAPI\r | |
519 | FvbWrite (\r | |
520 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
521 | IN EFI_LBA Lba,\r | |
522 | IN UINTN Offset,\r | |
523 | IN OUT UINTN *NumBytes,\r | |
524 | IN UINT8 *Buffer\r | |
525 | )\r | |
526 | {\r | |
1e57a462 | 527 | NOR_FLASH_INSTANCE *Instance;\r |
518c243d | 528 | \r |
452a9ee1 | 529 | Instance = INSTANCE_FROM_FVB_THIS (This);\r |
1e57a462 | 530 | \r |
36d66acf | 531 | return NorFlashWriteSingleBlock (Instance, Instance->StartLba + Lba, Offset, NumBytes, Buffer);\r |
1e57a462 | 532 | }\r |
533 | \r | |
534 | /**\r | |
535 | Erases and initialises a firmware volume block.\r | |
536 | \r | |
537 | The EraseBlocks() function erases one or more blocks as denoted\r | |
538 | by the variable argument list. The entire parameter list of\r | |
539 | blocks must be verified before erasing any blocks. If a block is\r | |
540 | requested that does not exist within the associated firmware\r | |
541 | volume (it has a larger index than the last block of the\r | |
542 | firmware volume), the EraseBlocks() function must return the\r | |
543 | status code EFI_INVALID_PARAMETER without modifying the contents\r | |
544 | of the firmware volume. Implementations should be mindful that\r | |
545 | the firmware volume might be in the WriteDisabled state. If it\r | |
546 | is in this state, the EraseBlocks() function must return the\r | |
547 | status code EFI_ACCESS_DENIED without modifying the contents of\r | |
548 | the firmware volume. All calls to EraseBlocks() must be fully\r | |
549 | flushed to the hardware before the EraseBlocks() service\r | |
550 | returns.\r | |
551 | \r | |
552 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL\r | |
553 | instance.\r | |
554 | \r | |
555 | @param ... The variable argument list is a list of tuples.\r | |
556 | Each tuple describes a range of LBAs to erase\r | |
557 | and consists of the following:\r | |
558 | - An EFI_LBA that indicates the starting LBA\r | |
559 | - A UINTN that indicates the number of blocks to erase.\r | |
560 | \r | |
561 | The list is terminated with an EFI_LBA_LIST_TERMINATOR.\r | |
562 | For example, the following indicates that two ranges of blocks\r | |
563 | (5-7 and 10-11) are to be erased:\r | |
564 | EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);\r | |
565 | \r | |
566 | @retval EFI_SUCCESS The erase request successfully completed.\r | |
567 | \r | |
568 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.\r | |
569 | \r | |
570 | @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written.\r | |
571 | The firmware device may have been partially erased.\r | |
572 | \r | |
573 | @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do\r | |
574 | not exist in the firmware volume.\r | |
575 | \r | |
576 | **/\r | |
577 | EFI_STATUS\r | |
578 | EFIAPI\r | |
579 | FvbEraseBlocks (\r | |
580 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This,\r | |
581 | ...\r | |
582 | )\r | |
583 | {\r | |
584 | EFI_STATUS Status;\r | |
585 | VA_LIST Args;\r | |
586 | UINTN BlockAddress; // Physical address of Lba to erase\r | |
587 | EFI_LBA StartingLba; // Lba from which we start erasing\r | |
588 | UINTN NumOfLba; // Number of Lba blocks to erase\r | |
589 | NOR_FLASH_INSTANCE *Instance;\r | |
590 | \r | |
591 | Instance = INSTANCE_FROM_FVB_THIS(This);\r | |
592 | \r | |
593 | DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n"));\r | |
594 | \r | |
595 | Status = EFI_SUCCESS;\r | |
596 | \r | |
597 | // Detect WriteDisabled state\r | |
598 | if (Instance->Media.ReadOnly == TRUE) {\r | |
599 | // Firmware volume is in WriteDisabled state\r | |
600 | DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n"));\r | |
601 | return EFI_ACCESS_DENIED;\r | |
602 | }\r | |
603 | \r | |
604 | // Before erasing, check the entire list of parameters to ensure all specified blocks are valid\r | |
605 | \r | |
606 | VA_START (Args, This);\r | |
607 | do {\r | |
608 | // Get the Lba from which we start erasing\r | |
609 | StartingLba = VA_ARG (Args, EFI_LBA);\r | |
610 | \r | |
611 | // Have we reached the end of the list?\r | |
612 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
613 | //Exit the while loop\r | |
614 | break;\r | |
615 | }\r | |
616 | \r | |
617 | // How many Lba blocks are we requested to erase?\r | |
ce69cc77 | 618 | NumOfLba = VA_ARG (Args, UINTN);\r |
1e57a462 | 619 | \r |
620 | // All blocks must be within range\r | |
ce69cc77 LE |
621 | DEBUG ((\r |
622 | DEBUG_BLKIO,\r | |
623 | "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",\r | |
624 | Instance->StartLba + StartingLba,\r | |
625 | (UINT64)NumOfLba,\r | |
626 | Instance->Media.LastBlock\r | |
627 | ));\r | |
1e57a462 | 628 | if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) {\r |
629 | VA_END (Args);\r | |
630 | DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));\r | |
631 | Status = EFI_INVALID_PARAMETER;\r | |
632 | goto EXIT;\r | |
633 | }\r | |
634 | } while (TRUE);\r | |
635 | VA_END (Args);\r | |
636 | \r | |
637 | //\r | |
638 | // To get here, all must be ok, so start erasing\r | |
639 | //\r | |
640 | VA_START (Args, This);\r | |
641 | do {\r | |
642 | // Get the Lba from which we start erasing\r | |
643 | StartingLba = VA_ARG (Args, EFI_LBA);\r | |
644 | \r | |
645 | // Have we reached the end of the list?\r | |
646 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
647 | // Exit the while loop\r | |
648 | break;\r | |
649 | }\r | |
650 | \r | |
651 | // How many Lba blocks are we requested to erase?\r | |
ce69cc77 | 652 | NumOfLba = VA_ARG (Args, UINTN);\r |
1e57a462 | 653 | \r |
654 | // Go through each one and erase it\r | |
655 | while (NumOfLba > 0) {\r | |
656 | \r | |
657 | // Get the physical address of Lba to erase\r | |
658 | BlockAddress = GET_NOR_BLOCK_ADDRESS (\r | |
659 | Instance->RegionBaseAddress,\r | |
660 | Instance->StartLba + StartingLba,\r | |
661 | Instance->Media.BlockSize\r | |
662 | );\r | |
663 | \r | |
664 | // Erase it\r | |
665 | DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress));\r | |
666 | Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);\r | |
667 | if (EFI_ERROR(Status)) {\r | |
668 | VA_END (Args);\r | |
669 | Status = EFI_DEVICE_ERROR;\r | |
670 | goto EXIT;\r | |
671 | }\r | |
672 | \r | |
673 | // Move to the next Lba\r | |
674 | StartingLba++;\r | |
675 | NumOfLba--;\r | |
676 | }\r | |
677 | } while (TRUE);\r | |
678 | VA_END (Args);\r | |
679 | \r | |
680 | EXIT:\r | |
681 | return Status;\r | |
682 | }\r | |
683 | \r | |
1dbbfc17 OM |
684 | /**\r |
685 | Fixup internal data so that EFI can be call in virtual mode.\r | |
686 | Call the passed in Child Notify event and convert any pointers in\r | |
687 | lib to virtual mode.\r | |
688 | \r | |
689 | @param[in] Event The Event that is being processed\r | |
690 | @param[in] Context Event Context\r | |
691 | **/\r | |
692 | VOID\r | |
693 | EFIAPI\r | |
694 | FvbVirtualNotifyEvent (\r | |
695 | IN EFI_EVENT Event,\r | |
696 | IN VOID *Context\r | |
697 | )\r | |
698 | {\r | |
699 | EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);\r | |
700 | return;\r | |
701 | }\r | |
702 | \r | |
1e57a462 | 703 | EFI_STATUS\r |
704 | EFIAPI\r | |
705 | NorFlashFvbInitialize (\r | |
706 | IN NOR_FLASH_INSTANCE* Instance\r | |
707 | )\r | |
708 | {\r | |
709 | EFI_STATUS Status;\r | |
710 | UINT32 FvbNumLba;\r | |
711 | EFI_BOOT_MODE BootMode;\r | |
1dbbfc17 | 712 | UINTN RuntimeMmioRegionSize;\r |
1e57a462 | 713 | \r |
714 | DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n"));\r | |
90d1f671 AG |
715 | ASSERT((Instance != NULL));\r |
716 | \r | |
717 | //\r | |
718 | // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME\r | |
719 | //\r | |
720 | \r | |
721 | // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;\r | |
722 | // even if we only use the small block region at the top of the NOR Flash.\r | |
723 | // The reason is when the NOR Flash memory is set into program mode, the command\r | |
724 | // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)\r | |
725 | RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;\r | |
726 | \r | |
727 | Status = gDS->AddMemorySpace (\r | |
728 | EfiGcdMemoryTypeMemoryMappedIo,\r | |
729 | Instance->DeviceBaseAddress, RuntimeMmioRegionSize,\r | |
730 | EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r | |
731 | );\r | |
732 | ASSERT_EFI_ERROR (Status);\r | |
733 | \r | |
734 | Status = gDS->SetMemorySpaceAttributes (\r | |
735 | Instance->DeviceBaseAddress, RuntimeMmioRegionSize,\r | |
736 | EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);\r | |
737 | ASSERT_EFI_ERROR (Status);\r | |
1e57a462 | 738 | \r |
1dbbfc17 | 739 | mFlashNvStorageVariableBase = FixedPcdGet32 (PcdFlashNvStorageVariableBase);\r |
1e57a462 | 740 | \r |
741 | // Set the index of the first LBA for the FVB\r | |
742 | Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.BlockSize;\r | |
743 | \r | |
744 | BootMode = GetBootModeHob ();\r | |
745 | if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r | |
746 | Status = EFI_INVALID_PARAMETER;\r | |
747 | } else {\r | |
748 | // Determine if there is a valid header at the beginning of the NorFlash\r | |
749 | Status = ValidateFvHeader (Instance);\r | |
750 | }\r | |
751 | \r | |
3402aac7 | 752 | // Install the Default FVB header if required\r |
1e57a462 | 753 | if (EFI_ERROR(Status)) {\r |
754 | // There is no valid header, so time to install one.\r | |
582085eb LE |
755 | DEBUG ((EFI_D_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));\r |
756 | DEBUG ((EFI_D_INFO, "%a: Installing a correct one for this volume.\n",\r | |
757 | __FUNCTION__));\r | |
1e57a462 | 758 | \r |
759 | // Erase all the NorFlash that is reserved for variable storage\r | |
760 | FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.BlockSize;\r | |
761 | \r | |
762 | Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);\r | |
763 | if (EFI_ERROR(Status)) {\r | |
764 | return Status;\r | |
765 | }\r | |
766 | \r | |
767 | // Install all appropriate headers\r | |
768 | Status = InitializeFvAndVariableStoreHeaders (Instance);\r | |
769 | if (EFI_ERROR(Status)) {\r | |
770 | return Status;\r | |
771 | }\r | |
772 | }\r | |
1dbbfc17 | 773 | \r |
6281a2ed LE |
774 | //\r |
775 | // The driver implementing the variable read service can now be dispatched;\r | |
776 | // the varstore headers are in place.\r | |
777 | //\r | |
778 | Status = gBS->InstallProtocolInterface (\r | |
779 | &gImageHandle,\r | |
780 | &gEdkiiNvVarStoreFormattedGuid,\r | |
781 | EFI_NATIVE_INTERFACE,\r | |
782 | NULL\r | |
783 | );\r | |
784 | ASSERT_EFI_ERROR (Status);\r | |
785 | \r | |
1dbbfc17 OM |
786 | //\r |
787 | // Register for the virtual address change event\r | |
788 | //\r | |
789 | Status = gBS->CreateEventEx (\r | |
790 | EVT_NOTIFY_SIGNAL,\r | |
791 | TPL_NOTIFY,\r | |
792 | FvbVirtualNotifyEvent,\r | |
793 | NULL,\r | |
794 | &gEfiEventVirtualAddressChangeGuid,\r | |
795 | &mFvbVirtualAddrChangeEvent\r | |
796 | );\r | |
797 | ASSERT_EFI_ERROR (Status);\r | |
798 | \r | |
1e57a462 | 799 | return Status;\r |
800 | }\r |