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