summaryrefslogtreecommitdiff
path: root/EmbeddedPkg/Library/FdtLoadLib/FdtConfigurationTable.c
blob: a95e24958bd4eaa02051ef8a9a0a46bbfffbd8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/** @file
*
*  Copyright (c) 2014, ARM Limited. All rights reserved.
*
*  This program and the accompanying materials
*  are licensed and made available under the terms and conditions of the BSD License
*  which accompanies this distribution.  The full text of the license may be found at
*  http://opensource.org/licenses/bsd-license.php
*
*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*
**/

#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/FdtLoadLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Protocol/DevicePath.h>
#include <Protocol/FirmwareVolume2.h>
#include <Protocol/SimpleFileSystem.h>

#include <Guid/Fdt.h>
#include <Guid/FileInfo.h>

#include <libfdt.h>

//
// Device path for SemiHosting
//
STATIC CONST struct {
  VENDOR_DEVICE_PATH        Guid;
  EFI_DEVICE_PATH_PROTOCOL  End;
} mSemihostingDevicePath = {
  {
    { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0 } },
    { 0xC5B9C74A, 0x6D72, 0x4719, { 0x99, 0xAB, 0xC5, 0x9F, 0x19, 0x90, 0x91, 0xEB } }
  },
  { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }
};


/**
  This function declares the passed FDT into the UEFI Configuration Table

  @param FdtBlob    Base address of the Fdt Blob in System Memory
  @param FdtSize    Size of the Fdt Blob in System Memory

  @return EFI_SUCCESS           Fdt Blob was successfully installed into the configuration table
  @return !EFI_SUCCESS          Error returned by BS.InstallConfigurationTable()

**/
STATIC
EFI_STATUS
InstallFdtIntoConfigurationTable (
  IN VOID* FdtBlob,
  IN UINTN FdtSize
  )
{
  EFI_STATUS Status;

  // Check the FDT header is valid. We only make this check in DEBUG mode in case the FDT header change on
  // production device and this ASSERT() becomes not valid.
  ASSERT (fdt_check_header (FdtBlob) == 0);

  // Ensure the Size of the Device Tree is smaller than the size of the read file
  ASSERT ((UINTN)fdt_totalsize (FdtBlob) <= FdtSize);

  // Install the FDT into the Configuration Table
  Status = gBS->InstallConfigurationTable (&gFdtTableGuid, FdtBlob);

  return Status;
}


/**
  Load and Install FDT from Semihosting

  @param Filename   Name of the file to load from semihosting

  @return EFI_SUCCESS           Fdt Blob was successfully installed into the configuration table
                                from semihosting
  @return EFI_NOT_FOUND         Fail to locate the file in semihosting
  @return EFI_OUT_OF_RESOURCES  Fail to allocate memory to contain the blob
**/
EFI_STATUS
InstallFdtFromSemihosting (
  IN  CONST CHAR16*   FileName
  )
{
  EFI_STATUS                       Status;
  EFI_DEVICE_PATH*                 Remaining;
  EFI_HANDLE                       Handle;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SemihostingFs;
  EFI_FILE_PROTOCOL               *Fs;
  EFI_FILE_PROTOCOL               *File;
  EFI_PHYSICAL_ADDRESS             FdtBase;
  EFI_FILE_INFO                   *FileInfo;
  UINTN                            FdtSize;
  UINTN                            FileInfoSize;

  // Ensure the Semihosting driver is initialized
  Remaining = (EFI_DEVICE_PATH*)&mSemihostingDevicePath;
  // The LocateDevicePath() function locates all devices on DevicePath that support Protocol and returns
  // the handle to the device that is closest to DevicePath. On output, the device path pointer is modified
  // to point to the remaining part of the device path
  Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &Remaining, &Handle);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  // Recursive = FALSE: We do not want to start the whole device tree
  Status = gBS->ConnectController (Handle, NULL, Remaining, FALSE);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  // Locate the FileSystem
  Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&SemihostingFs);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  // Try to Open the volume and get root directory
  Status = SemihostingFs->OpenVolume (SemihostingFs, &Fs);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_WARN, "Warning: Fail to open semihosting filesystem that should contain FDT file.\n"));
    return Status;
  }

  File = NULL;
  Status = Fs->Open (Fs, &File, (CHAR16*)FileName, EFI_FILE_MODE_READ, 0);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_WARN, "Warning: Fail to load FDT file '%s'.\n", FileName));
    Fs->Close (Fs);
    return Status;
  }

  FileInfoSize = 0;
  File->GetInfo (File, &gEfiFileInfoGuid, &FileInfoSize, NULL);
  FileInfo = AllocatePool (FileInfoSize);
  if (FileInfo == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto CLOSE_FILES;
  }
  Status = File->GetInfo (File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
  if (EFI_ERROR (Status)) {
    FreePool (FileInfo);
    goto CLOSE_FILES;
  }

  // Get the file size
  FdtSize = FileInfo->FileSize;
  FreePool (FileInfo);

  // The FDT blob is attached to the Configuration Table. It is recommended to load it as Runtime Service Data
  // to prevent the kernel to overwrite its data
  Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (FdtSize), &FdtBase);
  if (!EFI_ERROR (Status)) {
    Status = File->Read (File, &FdtSize, (VOID*)(UINTN)(FdtBase));
    if (EFI_ERROR (Status)) {
      gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
    } else {
      // Install the FDT as part of the UEFI Configuration Table
      Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)FdtBase, FdtSize);
      if (EFI_ERROR (Status)) {
        gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
      }
    }
  }

CLOSE_FILES:
  File->Close (File);
  Fs->Close (Fs);
  return Status;
}

/**
  Load and Install FDT from Firmware Volume

  @param Filename   Guid of the FDT blob to load from firmware volume

  @return EFI_SUCCESS           Fdt Blob was successfully installed into the configuration table
                                from firmware volume
  @return EFI_NOT_FOUND         Fail to locate the file in firmware volume
  @return EFI_OUT_OF_RESOURCES  Fail to allocate memory to contain the blob
**/
EFI_STATUS
InstallFdtFromFv (
  IN  CONST EFI_GUID *FileName
  )
{
  EFI_STATUS                    Status;
  EFI_HANDLE                    *HandleBuffer;
  UINTN                         NumberOfHandles;
  UINT32                        FvStatus;
  UINTN                         Index;
  EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;
  INTN                          SectionInstance;
  UINTN                         FdtSize;
  VOID*                         FdtBlob;
  EFI_PHYSICAL_ADDRESS          FdtBase;

  FvStatus        = 0;
  SectionInstance = 0;

  // Locate all the Firmware Volume protocols.
  Status = gBS->LocateHandleBuffer (
                   ByProtocol,
                   &gEfiFirmwareVolume2ProtocolGuid,
                   NULL,
                   &NumberOfHandles,
                   &HandleBuffer
                   );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  // Looking for FV that contains the FDT blob
  for (Index = 0; Index < NumberOfHandles; Index++) {
    //
    // Get the protocol on this handle
    // This should not fail because of LocateHandleBuffer
    //
    Status = gBS->HandleProtocol (
                     HandleBuffer[Index],
                     &gEfiFirmwareVolume2ProtocolGuid,
                     (VOID**) &FvInstance
                     );
    if (EFI_ERROR (Status)) {
      goto FREE_HANDLE_BUFFER;
    }

    while (Status == EFI_SUCCESS) {
      // FdtBlob must be allocated by ReadSection
      FdtBlob = NULL;

      // See if it contains the FDT file
      Status = FvInstance->ReadSection (
                        FvInstance,
                        FileName,
                        EFI_SECTION_RAW,
                        SectionInstance,
                        &FdtBlob,
                        &FdtSize,
                        &FvStatus
                        );
      if (!EFI_ERROR (Status)) {
        // When the FDT blob is attached to the Configuration Table it is recommended to load it as Runtime Service Data
        // to prevent the kernel to overwrite its data
        Status = gBS->AllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (FdtSize), &FdtBase);
        if (EFI_ERROR (Status)) {
          goto FREE_HANDLE_BUFFER;
        }

        // Copy the FDT to the Runtime memory
        gBS->CopyMem ((VOID*)(UINTN)FdtBase, FdtBlob, FdtSize);
        // Free the buffer allocated by FvInstance->ReadSection()
        gBS->FreePool (FdtBlob);

        // Install the FDT as part of the UEFI Configuration Table
        Status = InstallFdtIntoConfigurationTable ((VOID*)(UINTN)FdtBase, FdtSize);
        if (EFI_ERROR (Status)) {
          gBS->FreePages (FdtBase, EFI_SIZE_TO_PAGES (FdtSize));
        }
        break;
      }
    }
  }

FREE_HANDLE_BUFFER:
  // Free any allocated buffers
  gBS->FreePool (HandleBuffer);

  return Status;
}