TianoCore EDK2 master
Loading...
Searching...
No Matches
VirtNorFlashDxe.c
1
11#include <Library/HobLib.h>
13#include <Library/PcdLib.h>
15#include <Library/UefiLib.h>
16
17#include "VirtNorFlash.h"
18
19STATIC EFI_EVENT mNorFlashVirtualAddrChangeEvent;
20
21//
22// Global variable declarations
23//
24NOR_FLASH_INSTANCE **mNorFlashInstances;
25UINT32 mNorFlashDeviceCount;
26UINTN mFlashNvStorageVariableBase;
27EFI_EVENT mFvbVirtualAddrChangeEvent;
28
29NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {
30 NOR_FLASH_SIGNATURE, // Signature
31 NULL, // Handle ... NEED TO BE FILLED
32
33 0, // DeviceBaseAddress ... NEED TO BE FILLED
34 0, // RegionBaseAddress ... NEED TO BE FILLED
35 0, // Size ... NEED TO BE FILLED
36 0, // StartLba
37 0, // LastBlock
38 0, // BlockSize
39
40 {
41 FvbGetAttributes, // GetAttributes
42 FvbSetAttributes, // SetAttributes
43 FvbGetPhysicalAddress, // GetPhysicalAddress
44 FvbGetBlockSize, // GetBlockSize
45 FvbRead, // Read
46 FvbWrite, // Write
47 FvbEraseBlocks, // EraseBlocks
48 NULL, // ParentHandle
49 }, // FvbProtoccol;
50 NULL, // ShadowBuffer
51 {
52 {
53 {
56 {
57 (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)),
58 (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8)
59 }
60 },
61 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
62 }, // GUID ... NEED TO BE FILLED
63 },
64 0, // Index
65 {
66 END_DEVICE_PATH_TYPE,
67 END_ENTIRE_DEVICE_PATH_SUBTYPE,
68 { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
69 }
70 } // DevicePath
71};
72
74NorFlashCreateInstance (
75 IN UINTN NorFlashDeviceBase,
76 IN UINTN NorFlashRegionBase,
77 IN UINTN NorFlashSize,
78 IN UINT32 Index,
79 IN UINT32 BlockSize,
80 IN BOOLEAN SupportFvb,
81 OUT NOR_FLASH_INSTANCE **NorFlashInstance
82 )
83{
84 EFI_STATUS Status;
85 NOR_FLASH_INSTANCE *Instance;
86
87 ASSERT (NorFlashInstance != NULL);
88
89 Instance = AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INSTANCE), &mNorFlashInstanceTemplate);
90 if (Instance == NULL) {
91 return EFI_OUT_OF_RESOURCES;
92 }
93
94 Instance->DeviceBaseAddress = NorFlashDeviceBase;
95 Instance->RegionBaseAddress = NorFlashRegionBase;
96 Instance->Size = NorFlashSize;
97 Instance->BlockSize = BlockSize;
98 Instance->LastBlock = (NorFlashSize / BlockSize) - 1;
99
100 CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);
101 Instance->DevicePath.Index = (UINT8)Index;
102
103 Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
104 if (Instance->ShadowBuffer == NULL) {
105 return EFI_OUT_OF_RESOURCES;
106 }
107
108 if (SupportFvb) {
109 NorFlashFvbInitialize (Instance);
110
111 Status = gBS->InstallMultipleProtocolInterfaces (
112 &Instance->Handle,
113 &gEfiDevicePathProtocolGuid,
114 &Instance->DevicePath,
115 &gEfiFirmwareVolumeBlockProtocolGuid,
116 &Instance->FvbProtocol,
117 NULL
118 );
119 if (EFI_ERROR (Status)) {
120 FreePool (Instance);
121 return Status;
122 }
123 } else {
124 Status = gBS->InstallMultipleProtocolInterfaces (
125 &Instance->Handle,
126 &gEfiDevicePathProtocolGuid,
127 &Instance->DevicePath,
128 NULL
129 );
130 if (EFI_ERROR (Status)) {
131 FreePool (Instance);
132 return Status;
133 }
134 }
135
136 *NorFlashInstance = Instance;
137 return Status;
138}
139
144NorFlashUnlockAndEraseSingleBlock (
145 IN NOR_FLASH_INSTANCE *Instance,
146 IN UINTN BlockAddress
147 )
148{
149 EFI_STATUS Status;
150 UINTN Index;
151 EFI_TPL OriginalTPL;
152
153 if (!EfiAtRuntime ()) {
154 // Raise TPL to TPL_HIGH to stop anyone from interrupting us.
155 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
156 } else {
157 // This initialization is only to prevent the compiler to complain about the
158 // use of uninitialized variables
159 OriginalTPL = TPL_HIGH_LEVEL;
160 }
161
162 Index = 0;
163 // The block erase might fail a first time (SW bug ?). Retry it ...
164 do {
165 // Unlock the block if we have to
166 Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);
167 if (EFI_ERROR (Status)) {
168 break;
169 }
170
171 Status = NorFlashEraseSingleBlock (Instance, BlockAddress);
172 Index++;
173 } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));
174
175 if (Index == NOR_FLASH_ERASE_RETRY) {
176 DEBUG ((DEBUG_ERROR, "EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress, Index));
177 }
178
179 if (!EfiAtRuntime ()) {
180 // Interruptions can resume.
181 gBS->RestoreTPL (OriginalTPL);
182 }
183
184 return Status;
185}
186
188NorFlashWriteFullBlock (
189 IN NOR_FLASH_INSTANCE *Instance,
190 IN EFI_LBA Lba,
191 IN UINT32 *DataBuffer,
192 IN UINT32 BlockSizeInWords
193 )
194{
195 EFI_STATUS Status;
196 UINTN WordAddress;
197 UINT32 WordIndex;
198 UINTN BufferIndex;
199 UINTN BlockAddress;
200 UINTN BuffersInBlock;
201 UINTN RemainingWords;
202 EFI_TPL OriginalTPL;
203 UINTN Cnt;
204
205 Status = EFI_SUCCESS;
206
207 // Get the physical address of the block
208 BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4);
209
210 // Start writing from the first address at the start of the block
211 WordAddress = BlockAddress;
212
213 if (!EfiAtRuntime ()) {
214 // Raise TPL to TPL_HIGH to stop anyone from interrupting us.
215 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
216 } else {
217 // This initialization is only to prevent the compiler to complain about the
218 // use of uninitialized variables
219 OriginalTPL = TPL_HIGH_LEVEL;
220 }
221
222 Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);
223 if (EFI_ERROR (Status)) {
224 DEBUG ((DEBUG_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress));
225 goto EXIT;
226 }
227
228 // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.
229
230 // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero
231 if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) {
232 // First, break the entire block into buffer-sized chunks.
233 BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;
234
235 // Then feed each buffer chunk to the NOR Flash
236 // If a buffer does not contain any data, don't write it.
237 for (BufferIndex = 0;
238 BufferIndex < BuffersInBlock;
239 BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS
240 )
241 {
242 // Check the buffer to see if it contains any data (not set all 1s).
243 for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {
244 if (~DataBuffer[Cnt] != 0 ) {
245 // Some data found, write the buffer.
246 Status = NorFlashWriteBuffer (
247 Instance,
248 WordAddress,
249 P30_MAX_BUFFER_SIZE_IN_BYTES,
250 DataBuffer
251 );
252 if (EFI_ERROR (Status)) {
253 goto EXIT;
254 }
255
256 break;
257 }
258 }
259 }
260
261 // Finally, finish off any remaining words that are less than the maximum size of the buffer
262 RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;
263
264 if (RemainingWords != 0) {
265 Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer);
266 if (EFI_ERROR (Status)) {
267 goto EXIT;
268 }
269 }
270 } else {
271 // For now, use the single word programming algorithm
272 // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,
273 // i.e. which ends in the range 0x......01 - 0x......7F.
274 for (WordIndex = 0; WordIndex < BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) {
275 Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer);
276 if (EFI_ERROR (Status)) {
277 goto EXIT;
278 }
279 }
280 }
281
282EXIT:
283 // Put device back into Read Array mode
284 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);
285
286 if (!EfiAtRuntime ()) {
287 // Interruptions can resume.
288 gBS->RestoreTPL (OriginalTPL);
289 }
290
291 if (EFI_ERROR (Status)) {
292 DEBUG ((DEBUG_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status));
293 }
294
295 return Status;
296}
297
299EFIAPI
300NorFlashInitialise (
301 IN EFI_HANDLE ImageHandle,
302 IN EFI_SYSTEM_TABLE *SystemTable
303 )
304{
305 EFI_STATUS Status;
306 UINT32 Index;
307 VIRT_NOR_FLASH_DESCRIPTION *NorFlashDevices;
308 BOOLEAN ContainVariableStorage;
309
311 if (EFI_ERROR (Status)) {
312 DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to initialize Nor Flash devices\n"));
313 return Status;
314 }
315
316 Status = VirtNorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount);
317 if (EFI_ERROR (Status)) {
318 DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to get Nor Flash devices\n"));
319 return Status;
320 }
321
322 mNorFlashInstances = AllocateRuntimePool (sizeof (NOR_FLASH_INSTANCE *) * mNorFlashDeviceCount);
323
324 for (Index = 0; Index < mNorFlashDeviceCount; Index++) {
325 // Check if this NOR Flash device contain the variable storage region
326
327 if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {
328 ContainVariableStorage =
329 (NorFlashDevices[Index].RegionBaseAddress <= PcdGet64 (PcdFlashNvStorageVariableBase64)) &&
330 (PcdGet64 (PcdFlashNvStorageVariableBase64) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
331 NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
332 } else {
333 ContainVariableStorage =
334 (NorFlashDevices[Index].RegionBaseAddress <= PcdGet32 (PcdFlashNvStorageVariableBase)) &&
335 (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
336 NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
337 }
338
339 Status = NorFlashCreateInstance (
340 NorFlashDevices[Index].DeviceBaseAddress,
341 NorFlashDevices[Index].RegionBaseAddress,
342 NorFlashDevices[Index].Size,
343 Index,
344 NorFlashDevices[Index].BlockSize,
345 ContainVariableStorage,
346 &mNorFlashInstances[Index]
347 );
348 if (EFI_ERROR (Status)) {
349 DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to create instance for NorFlash[%d]\n", Index));
350 }
351 }
352
353 //
354 // Register for the virtual address change event
355 //
356 Status = gBS->CreateEventEx (
357 EVT_NOTIFY_SIGNAL,
358 TPL_NOTIFY,
359 NorFlashVirtualNotifyEvent,
360 NULL,
361 &gEfiEventVirtualAddressChangeGuid,
362 &mNorFlashVirtualAddrChangeEvent
363 );
364 ASSERT_EFI_ERROR (Status);
365
366 return Status;
367}
368
370EFIAPI
371NorFlashFvbInitialize (
372 IN NOR_FLASH_INSTANCE *Instance
373 )
374{
375 EFI_STATUS Status;
376 UINT32 FvbNumLba;
377 EFI_BOOT_MODE BootMode;
378 UINTN RuntimeMmioRegionSize;
379
380 DEBUG ((DEBUG_BLKIO, "NorFlashFvbInitialize\n"));
381 ASSERT ((Instance != NULL));
382
383 //
384 // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
385 //
386
387 // Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
388 // even if we only use the small block region at the top of the NOR Flash.
389 // The reason is when the NOR Flash memory is set into program mode, the command
390 // is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
391 RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;
392
393 Status = gDS->AddMemorySpace (
395 Instance->DeviceBaseAddress,
396 RuntimeMmioRegionSize,
397 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
398 );
399 ASSERT_EFI_ERROR (Status);
400
401 Status = gDS->SetMemorySpaceAttributes (
402 Instance->DeviceBaseAddress,
403 RuntimeMmioRegionSize,
404 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
405 );
406 ASSERT_EFI_ERROR (Status);
407
408 mFlashNvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
409 PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
410
411 // Set the index of the first LBA for the FVB
412 Instance->StartLba = (mFlashNvStorageVariableBase - Instance->RegionBaseAddress) / Instance->BlockSize;
413
414 BootMode = GetBootModeHob ();
415 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
416 Status = EFI_INVALID_PARAMETER;
417 } else {
418 // Determine if there is a valid header at the beginning of the NorFlash
419 Status = ValidateFvHeader (Instance);
420 }
421
422 // Install the Default FVB header if required
423 if (EFI_ERROR (Status)) {
424 // There is no valid header, so time to install one.
425 DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __func__));
426 DEBUG ((
427 DEBUG_INFO,
428 "%a: Installing a correct one for this volume.\n",
429 __func__
430 ));
431
432 // Erase all the NorFlash that is reserved for variable storage
433 FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
434
435 Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
436 if (EFI_ERROR (Status)) {
437 return Status;
438 }
439
440 // Install all appropriate headers
441 Status = InitializeFvAndVariableStoreHeaders (Instance);
442 if (EFI_ERROR (Status)) {
443 return Status;
444 }
445 }
446
447 //
448 // The driver implementing the variable read service can now be dispatched;
449 // the varstore headers are in place.
450 //
451 Status = gBS->InstallProtocolInterface (
453 &gEdkiiNvVarStoreFormattedGuid,
455 NULL
456 );
457 ASSERT_EFI_ERROR (Status);
458
459 //
460 // Register for the virtual address change event
461 //
462 Status = gBS->CreateEventEx (
463 EVT_NOTIFY_SIGNAL,
464 TPL_NOTIFY,
465 FvbVirtualNotifyEvent,
466 NULL,
467 &gEfiEventVirtualAddressChangeGuid,
468 &mFvbVirtualAddrChangeEvent
469 );
470 ASSERT_EFI_ERROR (Status);
471
472 return Status;
473}
UINT64 UINTN
EFI_BOOT_MODE EFIAPI GetBootModeHob(VOID)
Definition: HobLib.c:240
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
#define HARDWARE_DEVICE_PATH
Definition: DevicePath.h:68
#define HW_VENDOR_DP
Definition: DevicePath.h:133
EFI_DXE_SERVICES * gDS
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateRuntimePool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateRuntimeCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define EFI_LBA_LIST_TERMINATOR
VOID InitializeFvAndVariableStoreHeaders(IN VOID *Ptr)
Definition: Fvb.c:606
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define OFFSET_OF(TYPE, Field)
Definition: Base.h:758
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
EFI_STATUS VirtNorFlashPlatformInitialization(VOID)
EFI_STATUS VirtNorFlashPlatformGetDevices(OUT VIRT_NOR_FLASH_DESCRIPTION **NorFlashDescriptions, OUT UINT32 *Count)
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
UINT32 EFI_BOOT_MODE
Definition: PiBootMode.h:18
@ EfiGcdMemoryTypeMemoryMappedIo
Definition: PiDxeCis.h:44
UINT64 EFI_LBA
Definition: UefiBaseType.h:45
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_EVENT
Definition: UefiBaseType.h:37
UINTN EFI_TPL
Definition: UefiBaseType.h:41
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
BOOLEAN EFIAPI EfiAtRuntime(VOID)
Definition: RuntimeLib.c:167
@ EFI_NATIVE_INTERFACE
Definition: UefiSpec.h:1193