IN UINT8 *Buffer\r
)\r
{\r
- EFI_STATUS TempStatus;\r
- UINT32 Tmp;\r
- UINT32 TmpBuf;\r
- UINT32 WordToWrite;\r
- UINT32 Mask;\r
- BOOLEAN DoErase;\r
- UINTN BytesToWrite;\r
+ EFI_STATUS Status;\r
UINTN CurOffset;\r
- UINTN WordAddr;\r
UINTN BlockSize;\r
UINTN BlockAddress;\r
- UINTN PrevBlockAddress;\r
-\r
- PrevBlockAddress = 0;\r
+ UINT8 *OrigData;\r
\r
DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));\r
\r
+ // Check we did get some memory. Buffer is BlockSize.\r
+ if (Instance->ShadowBuffer == NULL) {\r
+ DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
// Cache the block size to avoid de-referencing pointers all the time\r
BlockSize = Instance->BlockSize;\r
\r
return EFI_BAD_BUFFER_SIZE;\r
}\r
\r
- // Pick 128bytes as a good start for word operations as opposed to erasing the\r
- // block and writing the data regardless if an erase is really needed.\r
- // It looks like most individual NV variable writes are smaller than 128bytes.\r
- if (*NumBytes <= 128) {\r
+ // Pick P30_MAX_BUFFER_SIZE_IN_BYTES (== 128 bytes) as a good start for word\r
+ // operations as opposed to erasing the block and writing the data regardless\r
+ // if an erase is really needed. It looks like most individual NV variable\r
+ // writes are smaller than 128 bytes.\r
+ // To avoid pathological cases were a 2 byte write is disregarded because it\r
+ // occurs right at a 128 byte buffered write alignment boundary, permit up to\r
+ // twice the max buffer size, and perform two writes if needed.\r
+ if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) <= (2 * P30_MAX_BUFFER_SIZE_IN_BYTES)) {\r
// Check to see if we need to erase before programming the data into NOR.\r
// If the destination bits are only changing from 1s to 0s we can just write.\r
// After a block is erased all bits in the block is set to 1.\r
// If any byte requires us to erase we just give up and rewrite all of it.\r
- DoErase = FALSE;\r
- BytesToWrite = *NumBytes;\r
- CurOffset = Offset;\r
-\r
- while (BytesToWrite > 0) {\r
- // Read full word from NOR, splice as required. A word is the smallest\r
- // unit we can write.\r
- TempStatus = NorFlashRead (Instance, Lba, CurOffset & ~(0x3), sizeof (Tmp), &Tmp);\r
- if (EFI_ERROR (TempStatus)) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
\r
- // Physical address of word in NOR to write.\r
- WordAddr = (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (\r
- Instance->RegionBaseAddress,\r
- Lba,\r
- BlockSize\r
- );\r
- // The word of data that is to be written.\r
- TmpBuf = *((UINT32 *)(Buffer + (*NumBytes - BytesToWrite)));\r
-\r
- // First do word aligned chunks.\r
- if ((CurOffset & 0x3) == 0) {\r
- if (BytesToWrite >= 4) {\r
- // Is the destination still in 'erased' state?\r
- if (~Tmp != 0) {\r
- // Check to see if we are only changing bits to zero.\r
- if ((Tmp ^ TmpBuf) & TmpBuf) {\r
- DoErase = TRUE;\r
- break;\r
- }\r
- }\r
-\r
- // Write this word to NOR\r
- WordToWrite = TmpBuf;\r
- CurOffset += sizeof (TmpBuf);\r
- BytesToWrite -= sizeof (TmpBuf);\r
- } else {\r
- // BytesToWrite < 4. Do small writes and left-overs\r
- Mask = ~((~0) << (BytesToWrite * 8));\r
- // Mask out the bytes we want.\r
- TmpBuf &= Mask;\r
- // Is the destination still in 'erased' state?\r
- if ((Tmp & Mask) != Mask) {\r
- // Check to see if we are only changing bits to zero.\r
- if ((Tmp ^ TmpBuf) & TmpBuf) {\r
- DoErase = TRUE;\r
- break;\r
- }\r
- }\r
-\r
- // Merge old and new data. Write merged word to NOR\r
- WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
- CurOffset += BytesToWrite;\r
- BytesToWrite = 0;\r
- }\r
- } else {\r
- // Do multiple words, but starting unaligned.\r
- if (BytesToWrite > (4 - (CurOffset & 0x3))) {\r
- Mask = ((~0) << ((CurOffset & 0x3) * 8));\r
- // Mask out the bytes we want.\r
- TmpBuf &= Mask;\r
- // Is the destination still in 'erased' state?\r
- if ((Tmp & Mask) != Mask) {\r
- // Check to see if we are only changing bits to zero.\r
- if ((Tmp ^ TmpBuf) & TmpBuf) {\r
- DoErase = TRUE;\r
- break;\r
- }\r
- }\r
-\r
- // Merge old and new data. Write merged word to NOR\r
- WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
- BytesToWrite -= (4 - (CurOffset & 0x3));\r
- CurOffset += (4 - (CurOffset & 0x3));\r
- } else {\r
- // Unaligned and fits in one word.\r
- Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8);\r
- // Mask out the bytes we want.\r
- TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;\r
- // Is the destination still in 'erased' state?\r
- if ((Tmp & Mask) != Mask) {\r
- // Check to see if we are only changing bits to zero.\r
- if ((Tmp ^ TmpBuf) & TmpBuf) {\r
- DoErase = TRUE;\r
- break;\r
- }\r
- }\r
-\r
- // Merge old and new data. Write merged word to NOR\r
- WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
- CurOffset += BytesToWrite;\r
- BytesToWrite = 0;\r
- }\r
+ // Read the old version of the data into the shadow buffer\r
+ Status = NorFlashRead (\r
+ Instance,\r
+ Lba,\r
+ Offset & ~BOUNDARY_OF_32_WORDS,\r
+ (*NumBytes | BOUNDARY_OF_32_WORDS) + 1,\r
+ Instance->ShadowBuffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ // Make OrigData point to the start of the old version of the data inside\r
+ // the word aligned buffer\r
+ OrigData = Instance->ShadowBuffer + (Offset & BOUNDARY_OF_32_WORDS);\r
+\r
+ // Update the buffer containing the old version of the data with the new\r
+ // contents, while checking whether the old version had any bits cleared\r
+ // that we want to set. In that case, we will need to erase the block first.\r
+ for (CurOffset = 0; CurOffset < *NumBytes; CurOffset++) {\r
+ if (~OrigData[CurOffset] & Buffer[CurOffset]) {\r
+ goto DoErase;\r
}\r
\r
- //\r
- // Write the word to NOR.\r
- //\r
+ OrigData[CurOffset] = Buffer[CurOffset];\r
+ }\r
\r
- BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize);\r
- if (BlockAddress != PrevBlockAddress) {\r
- TempStatus = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r
- if (EFI_ERROR (TempStatus)) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
+ //\r
+ // Write the updated buffer to NOR.\r
+ //\r
+ BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize);\r
\r
- PrevBlockAddress = BlockAddress;\r
- }\r
+ // Unlock the block if we have to\r
+ Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Exit;\r
+ }\r
\r
- TempStatus = NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite);\r
- // Put device back into Read Array mode\r
- SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
- if (EFI_ERROR (TempStatus)) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
+ Status = NorFlashWriteBuffer (\r
+ Instance,\r
+ BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS),\r
+ P30_MAX_BUFFER_SIZE_IN_BYTES,\r
+ Instance->ShadowBuffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Exit;\r
}\r
\r
- // Exit if we got here and could write all the data. Otherwise do the\r
- // Erase-Write cycle.\r
- if (!DoErase) {\r
- return EFI_SUCCESS;\r
+ if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) > P30_MAX_BUFFER_SIZE_IN_BYTES) {\r
+ BlockAddress += P30_MAX_BUFFER_SIZE_IN_BYTES;\r
+\r
+ Status = NorFlashWriteBuffer (\r
+ Instance,\r
+ BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS),\r
+ P30_MAX_BUFFER_SIZE_IN_BYTES,\r
+ Instance->ShadowBuffer + P30_MAX_BUFFER_SIZE_IN_BYTES\r
+ );\r
}\r
- }\r
\r
- // Check we did get some memory. Buffer is BlockSize.\r
- if (Instance->ShadowBuffer == NULL) {\r
- DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r
- return EFI_DEVICE_ERROR;\r
+Exit:\r
+ // Put device back into Read Array mode\r
+ SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
+\r
+ return Status;\r
}\r
\r
+DoErase:\r
// Read NOR Flash data into shadow buffer\r
- TempStatus = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
- if (EFI_ERROR (TempStatus)) {\r
+ Status = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
+ if (EFI_ERROR (Status)) {\r
// Return one of the pre-approved error statuses\r
return EFI_DEVICE_ERROR;\r
}\r
CopyMem ((VOID *)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumBytes);\r
\r
// Write the modified buffer back to the NorFlash\r
- TempStatus = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
- if (EFI_ERROR (TempStatus)) {\r
+ Status = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
+ if (EFI_ERROR (Status)) {\r
// Return one of the pre-approved error statuses\r
return EFI_DEVICE_ERROR;\r
}\r