Vishal Bhoj | 82c8071 | 2015-12-15 21:13:33 +0530 | [diff] [blame^] | 1 | /** @file
|
| 2 | *
|
| 3 | * Copyright (c) 2014, ARM Limited. All rights reserved.
|
| 4 | *
|
| 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 | #include <Library/DebugLib.h>
|
| 17 | #include <Library/FdtLoadLib.h>
|
| 18 | #include <Library/MemoryAllocationLib.h>
|
| 19 | #include <Library/UefiBootServicesTableLib.h>
|
| 20 |
|
| 21 | #include <Protocol/DevicePath.h>
|
| 22 | #include <Protocol/FirmwareVolume2.h>
|
| 23 | #include <Protocol/SimpleFileSystem.h>
|
| 24 |
|
| 25 | #include <Guid/Fdt.h>
|
| 26 | #include <Guid/FileInfo.h>
|
| 27 |
|
| 28 | #include <libfdt.h>
|
| 29 |
|
| 30 | //
|
| 31 | // Device path for SemiHosting
|
| 32 | //
|
| 33 | STATIC CONST struct {
|
| 34 | VENDOR_DEVICE_PATH Guid;
|
| 35 | EFI_DEVICE_PATH_PROTOCOL End;
|
| 36 | } mSemihostingDevicePath = {
|
| 37 | {
|
| 38 | { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0 } },
|
| 39 | { 0xC5B9C74A, 0x6D72, 0x4719, { 0x99, 0xAB, 0xC5, 0x9F, 0x19, 0x90, 0x91, 0xEB } }
|
| 40 | },
|
| 41 | { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }
|
| 42 | };
|
| 43 |
|
| 44 |
|
| 45 | /**
|
| 46 | This function declares the passed FDT into the UEFI Configuration Table
|
| 47 |
|
| 48 | @param FdtBlob Base address of the Fdt Blob in System Memory
|
| 49 | @param FdtSize Size of the Fdt Blob in System Memory
|
| 50 |
|
| 51 | @return EFI_SUCCESS Fdt Blob was successfully installed into the configuration table
|
| 52 | @return !EFI_SUCCESS Error returned by BS.InstallConfigurationTable()
|
| 53 |
|
| 54 | **/
|
| 55 | STATIC
|
| 56 | EFI_STATUS
|
| 57 | InstallFdtIntoConfigurationTable (
|
| 58 | IN VOID* FdtBlob,
|
| 59 | IN UINTN FdtSize
|
| 60 | )
|
| 61 | {
|
| 62 | EFI_STATUS Status;
|
| 63 |
|
| 64 | // Check the FDT header is valid. We only make this check in DEBUG mode in case the FDT header change on
|
| 65 | // production device and this ASSERT() becomes not valid.
|
| 66 | ASSERT (fdt_check_header (FdtBlob) == 0);
|
| 67 |
|
| 68 | // Ensure the Size of the Device Tree is smaller than the size of the read file
|
| 69 | ASSERT ((UINTN)fdt_totalsize (FdtBlob) <= FdtSize);
|
| 70 |
|
| 71 | // Install the FDT into the Configuration Table
|
| 72 | Status = gBS->InstallConfigurationTable (&gFdtTableGuid, FdtBlob);
|
| 73 |
|
| 74 | return Status;
|
| 75 | }
|
| 76 |
|
| 77 |
|
| 78 | /**
|
| 79 | Load and Install FDT from Semihosting
|
| 80 |
|
| 81 | @param Filename Name of the file to load from semihosting
|
| 82 |
|
| 83 | @return EFI_SUCCESS Fdt Blob was successfully installed into the configuration table
|
| 84 | from semihosting
|
| 85 | @return EFI_NOT_FOUND Fail to locate the file in semihosting
|
| 86 | @return EFI_OUT_OF_RESOURCES Fail to allocate memory to contain the blob
|
| 87 | **/
|
| 88 | EFI_STATUS
|
| 89 | InstallFdtFromSemihosting (
|
| 90 | IN CONST CHAR16* FileName
|
| 91 | )
|
| 92 | {
|
| 93 | EFI_STATUS Status;
|
| 94 | EFI_DEVICE_PATH* Remaining;
|
| 95 | EFI_HANDLE Handle;
|
| 96 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SemihostingFs;
|
| 97 | EFI_FILE_PROTOCOL *Fs;
|
| 98 | EFI_FILE_PROTOCOL *File;
|
| 99 | EFI_PHYSICAL_ADDRESS FdtBase;
|
| 100 | EFI_FILE_INFO *FileInfo;
|
| 101 | UINTN FdtSize;
|
| 102 | UINTN FileInfoSize;
|
| 103 |
|
| 104 | // Ensure the Semihosting driver is initialized
|
| 105 | Remaining = (EFI_DEVICE_PATH*)&mSemihostingDevicePath;
|
| 106 | // The LocateDevicePath() function locates all devices on DevicePath that support Protocol and returns
|
| 107 | // the handle to the device that is closest to DevicePath. On output, the device path pointer is modified
|
| 108 | // to point to the remaining part of the device path
|
| 109 | Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &Remaining, &Handle);
|
| 110 | if (EFI_ERROR (Status)) {
|
| 111 | ASSERT_EFI_ERROR (Status);
|
| 112 | return Status;
|
| 113 | }
|
| 114 |
|
| 115 | // Recursive = FALSE: We do not want to start the whole device tree
|
| 116 | Status = gBS->ConnectController (Handle, NULL, Remaining, FALSE);
|
| 117 | if (EFI_ERROR (Status)) {
|
| 118 | return Status;
|
| 119 | }
|
| 120 |
|
| 121 | // Locate the FileSystem
|
| 122 | Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&SemihostingFs);
|
| 123 | if (EFI_ERROR (Status)) {
|
| 124 | ASSERT_EFI_ERROR (Status);
|
| 125 | return Status;
|
| 126 | }
|
| 127 |
|
| 128 | // Try to Open the volume and get root directory
|
| 129 | Status = SemihostingFs->OpenVolume (SemihostingFs, &Fs);
|
| 130 | if (EFI_ERROR (Status)) {
|
| 131 | DEBUG ((EFI_D_WARN, "Warning: Fail to open semihosting filesystem that should contain FDT file.\n"));
|
| 132 | return Status;
|
| 133 | }
|
| 134 |
|
| 135 | File = NULL;
|
| 136 | Status = Fs->Open (Fs, &File, (CHAR16*)FileName, EFI_FILE_MODE_READ, 0);
|
| 137 | if (EFI_ERROR (Status)) {
|
| 138 | DEBUG ((EFI_D_WARN, "Warning: Fail to load FDT file '%s'.\n", FileName));
|
| 139 | Fs->Close (Fs);
|
| 140 | return Status;
|
| 141 | }
|
| 142 |
|
| 143 | FileInfoSize = 0;
|
| 144 | File->GetInfo (File, &gEfiFileInfoGuid, &FileInfoSize, NULL);
|
| 145 | FileInfo = AllocatePool (FileInfoSize);
|
| 146 | if (FileInfo == NULL) {
|
| 147 | Status = EFI_OUT_OF_RESOURCES;
|
| 148 | goto CLOSE_FILES;
|
| 149 | }
|
| 150 | Status = File->GetInfo (File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
|
| 151 | if (EFI_ERROR (Status)) {
|
| 152 | FreePool (FileInfo);
|
| 153 | goto CLOSE_FILES;
|
| 154 | }
|
| 155 |
|
| 156 | // Get the file size
|
| 157 | FdtSize = FileInfo->FileSize;
|
| 158 | FreePool (FileInfo);
|
| 159 |
|
| 160 | // The FDT blob is attached to the Configuration Table. It is recommended to load it as Runtime Service Data
|
| 161 | // to prevent the kernel to overwrite its data
|
| 162 | Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (FdtSize), &FdtBase);
|
| 163 | if (!EFI_ERROR (Status)) {
|
| 164 | Status = File->Read (File, &FdtSize, (VOID*)(UINTN)(FdtBase));
|
| 165 | if (EFI_ERROR (Status)) {
|
| 166 | gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
|
| 167 | } else {
|
| 168 | // Install the FDT as part of the UEFI Configuration Table
|
| 169 | Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)FdtBase, FdtSize);
|
| 170 | if (EFI_ERROR (Status)) {
|
| 171 | gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | CLOSE_FILES:
|
| 177 | File->Close (File);
|
| 178 | Fs->Close (Fs);
|
| 179 | return Status;
|
| 180 | }
|
| 181 |
|
| 182 | /**
|
| 183 | Load and Install FDT from Firmware Volume
|
| 184 |
|
| 185 | @param Filename Guid of the FDT blob to load from firmware volume
|
| 186 |
|
| 187 | @return EFI_SUCCESS Fdt Blob was successfully installed into the configuration table
|
| 188 | from firmware volume
|
| 189 | @return EFI_NOT_FOUND Fail to locate the file in firmware volume
|
| 190 | @return EFI_OUT_OF_RESOURCES Fail to allocate memory to contain the blob
|
| 191 | **/
|
| 192 | EFI_STATUS
|
| 193 | InstallFdtFromFv (
|
| 194 | IN CONST EFI_GUID *FileName
|
| 195 | )
|
| 196 | {
|
| 197 | EFI_STATUS Status;
|
| 198 | EFI_HANDLE *HandleBuffer;
|
| 199 | UINTN NumberOfHandles;
|
| 200 | UINT32 FvStatus;
|
| 201 | UINTN Index;
|
| 202 | EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;
|
| 203 | INTN SectionInstance;
|
| 204 | UINTN FdtSize;
|
| 205 | VOID* FdtBlob;
|
| 206 | EFI_PHYSICAL_ADDRESS FdtBase;
|
| 207 |
|
| 208 | FvStatus = 0;
|
| 209 | SectionInstance = 0;
|
| 210 |
|
| 211 | // Locate all the Firmware Volume protocols.
|
| 212 | Status = gBS->LocateHandleBuffer (
|
| 213 | ByProtocol,
|
| 214 | &gEfiFirmwareVolume2ProtocolGuid,
|
| 215 | NULL,
|
| 216 | &NumberOfHandles,
|
| 217 | &HandleBuffer
|
| 218 | );
|
| 219 | if (EFI_ERROR (Status)) {
|
| 220 | return Status;
|
| 221 | }
|
| 222 |
|
| 223 | // Looking for FV that contains the FDT blob
|
| 224 | for (Index = 0; Index < NumberOfHandles; Index++) {
|
| 225 | //
|
| 226 | // Get the protocol on this handle
|
| 227 | // This should not fail because of LocateHandleBuffer
|
| 228 | //
|
| 229 | Status = gBS->HandleProtocol (
|
| 230 | HandleBuffer[Index],
|
| 231 | &gEfiFirmwareVolume2ProtocolGuid,
|
| 232 | (VOID**) &FvInstance
|
| 233 | );
|
| 234 | if (EFI_ERROR (Status)) {
|
| 235 | goto FREE_HANDLE_BUFFER;
|
| 236 | }
|
| 237 |
|
| 238 | while (Status == EFI_SUCCESS) {
|
| 239 | // FdtBlob must be allocated by ReadSection
|
| 240 | FdtBlob = NULL;
|
| 241 |
|
| 242 | // See if it contains the FDT file
|
| 243 | Status = FvInstance->ReadSection (
|
| 244 | FvInstance,
|
| 245 | FileName,
|
| 246 | EFI_SECTION_RAW,
|
| 247 | SectionInstance,
|
| 248 | &FdtBlob,
|
| 249 | &FdtSize,
|
| 250 | &FvStatus
|
| 251 | );
|
| 252 | if (!EFI_ERROR (Status)) {
|
| 253 | // When the FDT blob is attached to the Configuration Table it is recommended to load it as Runtime Service Data
|
| 254 | // to prevent the kernel to overwrite its data
|
| 255 | Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (FdtSize), &FdtBase);
|
| 256 | if (EFI_ERROR (Status)) {
|
| 257 | goto FREE_HANDLE_BUFFER;
|
| 258 | }
|
| 259 |
|
| 260 | // Copy the FDT to the Runtime memory
|
| 261 | gBS->CopyMem ((VOID*)(UINTN)FdtBase, FdtBlob, FdtSize);
|
| 262 | // Free the buffer allocated by FvInstance->ReadSection()
|
| 263 | gBS->FreePool (FdtBlob);
|
| 264 |
|
| 265 | // Install the FDT as part of the UEFI Configuration Table
|
| 266 | Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)FdtBase, FdtSize);
|
| 267 | if (EFI_ERROR (Status)) {
|
| 268 | gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
|
| 269 | }
|
| 270 | break;
|
| 271 | }
|
| 272 | }
|
| 273 | }
|
| 274 |
|
| 275 | FREE_HANDLE_BUFFER:
|
| 276 | // Free any allocated buffers
|
| 277 | gBS->FreePool (HandleBuffer);
|
| 278 |
|
| 279 | return Status;
|
| 280 | }
|