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