EFI_STATUS NorFlashReadBlocks ( IN NOR_FLASH_INSTANCE *Instance, IN EFI_LBA Lba, IN UINTN BufferSizeInBytes, OUT VOID *Buffer ) { UINT32 NumBlocks; UINTN StartAddress; // The buffer must be valid if (Buffer == NULL) { return EFI_INVALID_PARAMETER; } // We must have some bytes to read DEBUG((DEBUG_BLKIO, "NorFlashReadBlocks: BufferSize=0x%x bytes.\n", BufferSizeInBytes)); if(BufferSizeInBytes == 0) { return EFI_BAD_BUFFER_SIZE; } // The size of the buffer must be a multiple of the block size DEBUG((DEBUG_BLKIO, "NorFlashReadBlocks: BlockSize=0x%x bytes.\n", Instance->Media.BlockSize)); if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) { return EFI_BAD_BUFFER_SIZE; } // All blocks must be within the device NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ; DEBUG((DEBUG_BLKIO, "NorFlashReadBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%ld\n", NumBlocks, Instance->Media.LastBlock, Lba)); if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) { DEBUG((EFI_D_ERROR, "NorFlashReadBlocks: ERROR - Read will exceed last block\n")); return EFI_INVALID_PARAMETER; } // Get the address to start reading from StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, Instance->Media.BlockSize ); // Put the device into Read Array mode SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY); // Readout the data CopyMem(Buffer, (UINTN *)StartAddress, BufferSizeInBytes); return EFI_SUCCESS; }
EFI_STATUS NorFlashRead ( IN NOR_FLASH_INSTANCE *Instance, IN EFI_LBA Lba, IN UINTN Offset, IN UINTN BufferSizeInBytes, OUT VOID *Buffer ) { UINT32 NumBlocks; UINTN StartAddress; // The buffer must be valid if (Buffer == NULL) { return EFI_INVALID_PARAMETER; } // Return if we have not any byte to read if (BufferSizeInBytes == 0) { return EFI_SUCCESS; } // All blocks must be within the device NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ; if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) { DEBUG ((EFI_D_ERROR, "NorFlashRead: ERROR - Read will exceed last block\n")); return EFI_INVALID_PARAMETER; } if (Offset + BufferSizeInBytes >= Instance->Size) { DEBUG ((EFI_D_ERROR, "NorFlashRead: ERROR - Read will exceed device size.\n")); return EFI_INVALID_PARAMETER; } // Get the address to start reading from StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, Instance->Media.BlockSize ); // Put the device into Read Array mode SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY); // Readout the data CopyMem (Buffer, (UINTN *)(StartAddress + Offset), BufferSizeInBytes); return EFI_SUCCESS; }
/* Write a full or portion of a block. It must not span block boundaries; that is, Offset + *NumBytes <= Instance->Media.BlockSize. */ EFI_STATUS NorFlashWriteSingleBlock ( IN NOR_FLASH_INSTANCE *Instance, IN EFI_LBA Lba, IN UINTN Offset, IN OUT UINTN *NumBytes, IN UINT8 *Buffer ) { EFI_STATUS TempStatus; UINT32 Tmp; UINT32 TmpBuf; UINT32 WordToWrite; UINT32 Mask; BOOLEAN DoErase; UINTN BytesToWrite; UINTN CurOffset; UINTN WordAddr; UINTN BlockSize; UINTN BlockAddress; UINTN PrevBlockAddress; PrevBlockAddress = 0; if (!Instance->Initialized && Instance->Initialize) { Instance->Initialize(Instance); } DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer)); // Detect WriteDisabled state if (Instance->Media.ReadOnly == TRUE) { DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - Can not write: Device is in WriteDisabled state.\n")); // It is in WriteDisabled state, return an error right away return EFI_ACCESS_DENIED; } // Cache the block size to avoid de-referencing pointers all the time BlockSize = Instance->Media.BlockSize; // The write must not span block boundaries. // We need to check each variable individually because adding two large values together overflows. if ( ( Offset >= BlockSize ) || ( *NumBytes > BlockSize ) || ( (Offset + *NumBytes) > BlockSize ) ) { DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize )); return EFI_BAD_BUFFER_SIZE; } // We must have some bytes to write if (*NumBytes == 0) { DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize )); return EFI_BAD_BUFFER_SIZE; } // Pick 128bytes as a good start for word operations as opposed to erasing the // block and writing the data regardless if an erase is really needed. // It looks like most individual NV variable writes are smaller than 128bytes. if (*NumBytes <= 128) { // Check to see if we need to erase before programming the data into NOR. // If the destination bits are only changing from 1s to 0s we can just write. // After a block is erased all bits in the block is set to 1. // If any byte requires us to erase we just give up and rewrite all of it. DoErase = FALSE; BytesToWrite = *NumBytes; CurOffset = Offset; while (BytesToWrite > 0) { // Read full word from NOR, splice as required. A word is the smallest // unit we can write. TempStatus = NorFlashRead (Instance, Lba, CurOffset & ~(0x3), sizeof(Tmp), &Tmp); if (EFI_ERROR (TempStatus)) { return EFI_DEVICE_ERROR; } // Physical address of word in NOR to write. WordAddr = (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize); // The word of data that is to be written. TmpBuf = *((UINT32*)(Buffer + (*NumBytes - BytesToWrite))); // First do word aligned chunks. if ((CurOffset & 0x3) == 0) { if (BytesToWrite >= 4) { // Is the destination still in 'erased' state? if (~Tmp != 0) { // Check to see if we are only changing bits to zero. if ((Tmp ^ TmpBuf) & TmpBuf) { DoErase = TRUE; break; } } // Write this word to NOR WordToWrite = TmpBuf; CurOffset += sizeof(TmpBuf); BytesToWrite -= sizeof(TmpBuf); } else { // BytesToWrite < 4. Do small writes and left-overs Mask = ~((~0) << (BytesToWrite * 8)); // Mask out the bytes we want. TmpBuf &= Mask; // Is the destination still in 'erased' state? if ((Tmp & Mask) != Mask) { // Check to see if we are only changing bits to zero. if ((Tmp ^ TmpBuf) & TmpBuf) { DoErase = TRUE; break; } } // Merge old and new data. Write merged word to NOR WordToWrite = (Tmp & ~Mask) | TmpBuf; CurOffset += BytesToWrite; BytesToWrite = 0; } } else { // Do multiple words, but starting unaligned. if (BytesToWrite > (4 - (CurOffset & 0x3))) { Mask = ((~0) << ((CurOffset & 0x3) * 8)); // Mask out the bytes we want. TmpBuf &= Mask; // Is the destination still in 'erased' state? if ((Tmp & Mask) != Mask) { // Check to see if we are only changing bits to zero. if ((Tmp ^ TmpBuf) & TmpBuf) { DoErase = TRUE; break; } } // Merge old and new data. Write merged word to NOR WordToWrite = (Tmp & ~Mask) | TmpBuf; BytesToWrite -= (4 - (CurOffset & 0x3)); CurOffset += (4 - (CurOffset & 0x3)); } else { // Unaligned and fits in one word. Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8); // Mask out the bytes we want. TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask; // Is the destination still in 'erased' state? if ((Tmp & Mask) != Mask) { // Check to see if we are only changing bits to zero. if ((Tmp ^ TmpBuf) & TmpBuf) { DoErase = TRUE; break; } } // Merge old and new data. Write merged word to NOR WordToWrite = (Tmp & ~Mask) | TmpBuf; CurOffset += BytesToWrite; BytesToWrite = 0; } } // // Write the word to NOR. // BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize); if (BlockAddress != PrevBlockAddress) { TempStatus = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress); if (EFI_ERROR (TempStatus)) { return EFI_DEVICE_ERROR; } PrevBlockAddress = BlockAddress; } TempStatus = NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite); if (EFI_ERROR (TempStatus)) { return EFI_DEVICE_ERROR; } } // Exit if we got here and could write all the data. Otherwise do the // Erase-Write cycle. if (!DoErase) { return EFI_SUCCESS; } } // Check we did get some memory. Buffer is BlockSize. if (Instance->ShadowBuffer == NULL) { DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Buffer not ready\n")); return EFI_DEVICE_ERROR; } // Read NOR Flash data into shadow buffer TempStatus = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); if (EFI_ERROR (TempStatus)) { // Return one of the pre-approved error statuses return EFI_DEVICE_ERROR; } // Put the data at the appropriate location inside the buffer area CopyMem ((VOID*)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumBytes); // Write the modified buffer back to the NorFlash TempStatus = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); if (EFI_ERROR (TempStatus)) { // Return one of the pre-approved error statuses return EFI_DEVICE_ERROR; } return EFI_SUCCESS; }
STATIC EFI_STATUS NorFlashWriteFullBlock ( IN NOR_FLASH_INSTANCE *Instance, IN EFI_LBA Lba, IN UINT32 *DataBuffer, IN UINT32 BlockSizeInWords ) { EFI_STATUS Status; UINTN WordAddress; UINT32 WordIndex; UINTN BufferIndex; UINTN BlockAddress; UINTN BuffersInBlock; UINTN RemainingWords; EFI_TPL OriginalTPL; UINTN Cnt; Status = EFI_SUCCESS; // Get the physical address of the block BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4); // Start writing from the first address at the start of the block WordAddress = BlockAddress; if (!EfiAtRuntime ()) { // Raise TPL to TPL_HIGH to stop anyone from interrupting us. OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL); } else { // This initialization is only to prevent the compiler to complain about the // use of uninitialized variables OriginalTPL = TPL_HIGH_LEVEL; } Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress); if (EFI_ERROR(Status)) { DEBUG((EFI_D_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress)); goto EXIT; } // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method. // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) { // First, break the entire block into buffer-sized chunks. BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES; // Then feed each buffer chunk to the NOR Flash // If a buffer does not contain any data, don't write it. for(BufferIndex=0; BufferIndex < BuffersInBlock; BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS ) { // Check the buffer to see if it contains any data (not set all 1s). for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) { if (~DataBuffer[Cnt] != 0 ) { // Some data found, write the buffer. Status = NorFlashWriteBuffer (Instance, WordAddress, P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer); if (EFI_ERROR(Status)) { goto EXIT; } break; } } } // Finally, finish off any remaining words that are less than the maximum size of the buffer RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS; if(RemainingWords != 0) { Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer); if (EFI_ERROR(Status)) { goto EXIT; } } } else { // For now, use the single word programming algorithm // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range, // i.e. which ends in the range 0x......01 - 0x......7F. for(WordIndex=0; WordIndex<BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) { Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer); if (EFI_ERROR(Status)) { goto EXIT; } } } EXIT: if (!EfiAtRuntime ()) { // Interruptions can resume. gBS->RestoreTPL (OriginalTPL); } if (EFI_ERROR(Status)) { DEBUG((EFI_D_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status)); } return Status; }
/** Erases and initialises a firmware volume block. The EraseBlocks() function erases one or more blocks as denoted by the variable argument list. The entire parameter list of blocks must be verified before erasing any blocks. If a block is requested that does not exist within the associated firmware volume (it has a larger index than the last block of the firmware volume), the EraseBlocks() function must return the status code EFI_INVALID_PARAMETER without modifying the contents of the firmware volume. Implementations should be mindful that the firmware volume might be in the WriteDisabled state. If it is in this state, the EraseBlocks() function must return the status code EFI_ACCESS_DENIED without modifying the contents of the firmware volume. All calls to EraseBlocks() must be fully flushed to the hardware before the EraseBlocks() service returns. @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance. @param ... The variable argument list is a list of tuples. Each tuple describes a range of LBAs to erase and consists of the following: - An EFI_LBA that indicates the starting LBA - A UINTN that indicates the number of blocks to erase. The list is terminated with an EFI_LBA_LIST_TERMINATOR. For example, the following indicates that two ranges of blocks (5-7 and 10-11) are to be erased: EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR); @retval EFI_SUCCESS The erase request successfully completed. @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state. @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be written. The firmware device may have been partially erased. @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable argument list do not exist in the firmware volume. **/ EFI_STATUS EFIAPI FvbEraseBlocks ( IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, ... ) { EFI_STATUS Status; VA_LIST Args; UINTN BlockAddress; // Physical address of Lba to erase EFI_LBA StartingLba; // Lba from which we start erasing UINTN NumOfLba; // Number of Lba blocks to erase NOR_FLASH_INSTANCE *Instance; Instance = INSTANCE_FROM_FVB_THIS(This); DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n")); Status = EFI_SUCCESS; // Detect WriteDisabled state if (Instance->Media.ReadOnly == TRUE) { // Firmware volume is in WriteDisabled state DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabled state.\n")); return EFI_ACCESS_DENIED; } // Before erasing, check the entire list of parameters to ensure all specified blocks are valid VA_START (Args, This); do { // Get the Lba from which we start erasing StartingLba = VA_ARG (Args, EFI_LBA); // Have we reached the end of the list? if (StartingLba == EFI_LBA_LIST_TERMINATOR) { //Exit the while loop break; } // How many Lba blocks are we requested to erase? NumOfLba = VA_ARG (Args, UINT32); // All blocks must be within range DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%d - 1 ) > LastBlock=%ld.\n", Instance->StartLba + StartingLba, NumOfLba, Instance->Media.LastBlock)); if ((NumOfLba == 0) || ((Instance->StartLba + StartingLba + NumOfLba - 1) > Instance->Media.LastBlock)) { VA_END (Args); DEBUG ((EFI_D_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n")); Status = EFI_INVALID_PARAMETER; goto EXIT; } } while (TRUE); VA_END (Args); // // To get here, all must be ok, so start erasing // VA_START (Args, This); do { // Get the Lba from which we start erasing StartingLba = VA_ARG (Args, EFI_LBA); // Have we reached the end of the list? if (StartingLba == EFI_LBA_LIST_TERMINATOR) { // Exit the while loop break; } // How many Lba blocks are we requested to erase? NumOfLba = VA_ARG (Args, UINT32); // Go through each one and erase it while (NumOfLba > 0) { // Get the physical address of Lba to erase BlockAddress = GET_NOR_BLOCK_ADDRESS ( Instance->RegionBaseAddress, Instance->StartLba + StartingLba, Instance->Media.BlockSize ); // Erase it DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld @ 0x%08x.\n", Instance->StartLba + StartingLba, BlockAddress)); Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress); if (EFI_ERROR(Status)) { VA_END (Args); Status = EFI_DEVICE_ERROR; goto EXIT; } // Move to the next Lba StartingLba++; NumOfLba--; } } while (TRUE); VA_END (Args); EXIT: return Status; }