]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BdsLib/BdsLinuxFdt.c
ArmPkg/BdsLib: Support ignoring EfiReservedMemoryType when updating the FDT.
[mirror_edk2.git] / ArmPkg / Library / BdsLib / BdsLinuxFdt.c
1 /** @file
2 *
3 * Copyright (c) 2011-2013, 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 <Library/ArmSmcLib.h>
16 #include <Library/PcdLib.h>
17 #include <libfdt.h>
18
19 #include <IndustryStandard/ArmSmc.h>
20
21 #include "BdsInternal.h"
22 #include "BdsLinuxLoader.h"
23
24 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
25 #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
26 #define GET_CELL(p) (p += 4, *((const UINT32 *)(p-4)))
27
28 STATIC
29 UINTN
30 cpu_to_fdtn (UINTN x) {
31 if (sizeof (UINTN) == sizeof (UINT32)) {
32 return cpu_to_fdt32 (x);
33 } else {
34 return cpu_to_fdt64 (x);
35 }
36 }
37
38 typedef struct {
39 UINTN Base;
40 UINTN Size;
41 } FdtRegion;
42
43
44 STATIC
45 UINTN
46 IsPrintableString (
47 IN CONST VOID* data,
48 IN UINTN len
49 )
50 {
51 CONST CHAR8 *s = data;
52 CONST CHAR8 *ss;
53
54 // Zero length is not
55 if (len == 0) {
56 return 0;
57 }
58
59 // Must terminate with zero
60 if (s[len - 1] != '\0') {
61 return 0;
62 }
63
64 ss = s;
65 while (*s/* && isprint(*s)*/) {
66 s++;
67 }
68
69 // Not zero, or not done yet
70 if (*s != '\0' || (s + 1 - ss) < len) {
71 return 0;
72 }
73
74 return 1;
75 }
76
77 STATIC
78 VOID
79 PrintData (
80 IN CONST CHAR8* data,
81 IN UINTN len
82 )
83 {
84 UINTN i;
85 CONST CHAR8 *p = data;
86
87 // No data, don't print
88 if (len == 0)
89 return;
90
91 if (IsPrintableString (data, len)) {
92 Print(L" = \"%a\"", (const char *)data);
93 } else if ((len % 4) == 0) {
94 Print(L" = <");
95 for (i = 0; i < len; i += 4) {
96 Print(L"0x%08x%a", fdt32_to_cpu(GET_CELL(p)),i < (len - 4) ? " " : "");
97 }
98 Print(L">");
99 } else {
100 Print(L" = [");
101 for (i = 0; i < len; i++)
102 Print(L"%02x%a", *p++, i < len - 1 ? " " : "");
103 Print(L"]");
104 }
105 }
106
107 VOID
108 DebugDumpFdt (
109 IN VOID* FdtBlob
110 )
111 {
112 struct fdt_header *bph;
113 UINT32 off_dt;
114 UINT32 off_str;
115 CONST CHAR8* p_struct;
116 CONST CHAR8* p_strings;
117 CONST CHAR8* p;
118 CONST CHAR8* s;
119 CONST CHAR8* t;
120 UINT32 tag;
121 UINTN sz;
122 UINTN depth;
123 UINTN shift;
124 UINT32 version;
125
126 {
127 // Can 'memreserve' be printed by below code?
128 INTN num = fdt_num_mem_rsv(FdtBlob);
129 INTN i, err;
130 UINT64 addr = 0,size = 0;
131
132 for (i = 0; i < num; i++) {
133 err = fdt_get_mem_rsv(FdtBlob, i, &addr, &size);
134 if (err) {
135 DEBUG((EFI_D_ERROR, "Error (%d) : Cannot get memreserve section (%d)\n", err, i));
136 }
137 else {
138 Print(L"/memreserve/ \t0x%lx \t0x%lx;\n",addr,size);
139 }
140 }
141 }
142
143 depth = 0;
144 shift = 4;
145
146 bph = FdtBlob;
147 off_dt = fdt32_to_cpu(bph->off_dt_struct);
148 off_str = fdt32_to_cpu(bph->off_dt_strings);
149 p_struct = (CONST CHAR8*)FdtBlob + off_dt;
150 p_strings = (CONST CHAR8*)FdtBlob + off_str;
151 version = fdt32_to_cpu(bph->version);
152
153 p = p_struct;
154 while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
155 if (tag == FDT_BEGIN_NODE) {
156 s = p;
157 p = PALIGN(p + AsciiStrLen (s) + 1, 4);
158
159 if (*s == '\0')
160 s = "/";
161
162 Print(L"%*s%a {\n", depth * shift, L" ", s);
163
164 depth++;
165 continue;
166 }
167
168 if (tag == FDT_END_NODE) {
169 depth--;
170
171 Print(L"%*s};\n", depth * shift, L" ");
172 continue;
173 }
174
175 if (tag == FDT_NOP) {
176 Print(L"%*s// [NOP]\n", depth * shift, L" ");
177 continue;
178 }
179
180 if (tag != FDT_PROP) {
181 Print(L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag);
182 break;
183 }
184 sz = fdt32_to_cpu(GET_CELL(p));
185 s = p_strings + fdt32_to_cpu(GET_CELL(p));
186 if (version < 16 && sz >= 8)
187 p = PALIGN(p, 8);
188 t = p;
189
190 p = PALIGN(p + sz, 4);
191
192 Print(L"%*s%a", depth * shift, L" ", s);
193 PrintData(t, sz);
194 Print(L";\n");
195 }
196 }
197
198 STATIC
199 BOOLEAN
200 IsLinuxReservedRegion (
201 IN EFI_MEMORY_TYPE MemoryType
202 )
203 {
204 switch(MemoryType) {
205 case EfiRuntimeServicesCode:
206 case EfiRuntimeServicesData:
207 case EfiUnusableMemory:
208 case EfiACPIReclaimMemory:
209 case EfiACPIMemoryNVS:
210 case EfiReservedMemoryType:
211 return TRUE;
212 default:
213 return FALSE;
214 }
215 }
216
217
218 STATIC
219 BOOLEAN
220 IsPsciSmcSupported (
221 VOID
222 )
223 {
224 BOOLEAN PsciSmcSupported;
225 UINTN Rx;
226
227 PsciSmcSupported = FALSE;
228
229 // Check the SMC response to the Presence SMC
230 Rx = ARM_SMC_ID_PRESENCE;
231 ArmCallSmc (&Rx);
232 if (Rx == 1) {
233 // Check the SMC UID
234 Rx = ARM_SMC_ID_UID;
235 ArmCallSmc (&Rx);
236 if (Rx == ARM_TRUSTZONE_UID_4LETTERID) {
237 Rx = ARM_SMC_ID_UID + 1;
238 ArmCallSmc (&Rx);
239 if (Rx == ARM_TRUSTZONE_ARM_UID) {
240 PsciSmcSupported = TRUE;
241 }
242 }
243 }
244
245 return PsciSmcSupported;
246 }
247
248
249 /**
250 ** Relocate the FDT blob to a more appropriate location for the Linux kernel.
251 ** This function will allocate memory for the relocated FDT blob.
252 **
253 ** @retval EFI_SUCCESS on success.
254 ** @retval EFI_OUT_OF_RESOURCES or EFI_INVALID_PARAMETER on failure.
255 */
256 STATIC
257 EFI_STATUS
258 RelocateFdt (
259 EFI_PHYSICAL_ADDRESS OriginalFdt,
260 UINTN OriginalFdtSize,
261 EFI_PHYSICAL_ADDRESS *RelocatedFdt,
262 UINTN *RelocatedFdtSize,
263 EFI_PHYSICAL_ADDRESS *RelocatedFdtAlloc
264 )
265 {
266 EFI_STATUS Status;
267 INTN Error;
268 UINT64 FdtAlignment;
269
270 *RelocatedFdtSize = OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE;
271
272 // If FDT load address needs to be aligned, allocate more space.
273 FdtAlignment = PcdGet32 (PcdArmLinuxFdtAlignment);
274 if (FdtAlignment != 0) {
275 *RelocatedFdtSize += FdtAlignment;
276 }
277
278 // Try below a watermark address.
279 Status = EFI_NOT_FOUND;
280 if (PcdGet32 (PcdArmLinuxFdtMaxOffset) != 0) {
281 *RelocatedFdt = LINUX_FDT_MAX_OFFSET;
282 Status = gBS->AllocatePages (AllocateMaxAddress, EfiBootServicesData,
283 EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);
284 if (EFI_ERROR (Status)) {
285 DEBUG ((EFI_D_WARN, "Warning: Failed to load FDT below address 0x%lX (%r). Will try again at a random address anywhere.\n", *RelocatedFdt, Status));
286 }
287 }
288
289 // Try anywhere there is available space.
290 if (EFI_ERROR (Status)) {
291 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,
292 EFI_SIZE_TO_PAGES (*RelocatedFdtSize), RelocatedFdt);
293 if (EFI_ERROR (Status)) {
294 ASSERT_EFI_ERROR (Status);
295 return EFI_OUT_OF_RESOURCES;
296 } else {
297 DEBUG ((EFI_D_WARN, "WARNING: Loaded FDT at random address 0x%lX.\nWARNING: There is a risk of accidental overwriting by other code/data.\n", *RelocatedFdt));
298 }
299 }
300
301 *RelocatedFdtAlloc = *RelocatedFdt;
302 if (FdtAlignment != 0) {
303 *RelocatedFdt = ALIGN (*RelocatedFdt, FdtAlignment);
304 }
305
306 // Load the Original FDT tree into the new region
307 Error = fdt_open_into ((VOID*)(UINTN) OriginalFdt,
308 (VOID*)(UINTN)(*RelocatedFdt), *RelocatedFdtSize);
309 if (Error) {
310 DEBUG ((EFI_D_ERROR, "fdt_open_into(): %a\n", fdt_strerror (Error)));
311 gBS->FreePages (*RelocatedFdtAlloc, EFI_SIZE_TO_PAGES (*RelocatedFdtSize));
312 return EFI_INVALID_PARAMETER;
313 }
314
315 DEBUG_CODE_BEGIN();
316 //DebugDumpFdt (fdt);
317 DEBUG_CODE_END();
318
319 return EFI_SUCCESS;
320 }
321
322
323 EFI_STATUS
324 PrepareFdt (
325 IN CONST CHAR8* CommandLineArguments,
326 IN EFI_PHYSICAL_ADDRESS InitrdImage,
327 IN UINTN InitrdImageSize,
328 IN OUT EFI_PHYSICAL_ADDRESS *FdtBlobBase,
329 IN OUT UINTN *FdtBlobSize
330 )
331 {
332 EFI_STATUS Status;
333 EFI_PHYSICAL_ADDRESS NewFdtBlobBase;
334 EFI_PHYSICAL_ADDRESS NewFdtBlobAllocation;
335 UINTN NewFdtBlobSize;
336 VOID* fdt;
337 INTN err;
338 INTN node;
339 INTN cpu_node;
340 INT32 lenp;
341 CONST VOID* BootArg;
342 CONST VOID* Method;
343 EFI_PHYSICAL_ADDRESS InitrdImageStart;
344 EFI_PHYSICAL_ADDRESS InitrdImageEnd;
345 FdtRegion Region;
346 UINTN Index;
347 CHAR8 Name[10];
348 LIST_ENTRY ResourceList;
349 BDS_SYSTEM_MEMORY_RESOURCE *Resource;
350 ARM_PROCESSOR_TABLE *ArmProcessorTable;
351 ARM_CORE_INFO *ArmCoreInfoTable;
352 UINT32 MpId;
353 UINT32 ClusterId;
354 UINT32 CoreId;
355 UINT64 CpuReleaseAddr;
356 UINTN MemoryMapSize;
357 EFI_MEMORY_DESCRIPTOR *MemoryMap;
358 EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;
359 UINTN MapKey;
360 UINTN DescriptorSize;
361 UINT32 DescriptorVersion;
362 UINTN Pages;
363 BOOLEAN PsciSmcSupported;
364 UINTN OriginalFdtSize;
365 BOOLEAN CpusNodeExist;
366 UINTN CoreMpId;
367 UINTN Smc;
368
369 NewFdtBlobAllocation = 0;
370
371 //
372 // Sanity checks on the original FDT blob.
373 //
374 err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase));
375 if (err != 0) {
376 Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);
377 return EFI_INVALID_PARAMETER;
378 }
379
380 // The original FDT blob might have been loaded partially.
381 // Check that it is not the case.
382 OriginalFdtSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));
383 if (OriginalFdtSize > *FdtBlobSize) {
384 Print (L"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",
385 *FdtBlobSize, OriginalFdtSize);
386 return EFI_INVALID_PARAMETER;
387 }
388
389 //
390 // Relocate the FDT to its final location.
391 //
392 Status = RelocateFdt (*FdtBlobBase, OriginalFdtSize,
393 &NewFdtBlobBase, &NewFdtBlobSize, &NewFdtBlobAllocation);
394 if (EFI_ERROR (Status)) {
395 goto FAIL_RELOCATE_FDT;
396 }
397
398 //
399 // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported
400 //
401 PsciSmcSupported = FALSE;
402 if (FeaturePcdGet (PcdArmPsciSupport) == TRUE) {
403 PsciSmcSupported = IsPsciSmcSupported();
404 if (PsciSmcSupported == FALSE) {
405 DEBUG ((EFI_D_ERROR, "Warning: The Power State Coordination Interface (PSCI) is not supported by your platform Trusted Firmware.\n"));
406 }
407 }
408
409 fdt = (VOID*)(UINTN)NewFdtBlobBase;
410
411 node = fdt_subnode_offset (fdt, 0, "chosen");
412 if (node < 0) {
413 // The 'chosen' node does not exist, create it
414 node = fdt_add_subnode(fdt, 0, "chosen");
415 if (node < 0) {
416 DEBUG((EFI_D_ERROR,"Error on finding 'chosen' node\n"));
417 Status = EFI_INVALID_PARAMETER;
418 goto FAIL_COMPLETE_FDT;
419 }
420 }
421
422 DEBUG_CODE_BEGIN();
423 BootArg = fdt_getprop(fdt, node, "bootargs", &lenp);
424 if (BootArg != NULL) {
425 DEBUG((EFI_D_ERROR,"BootArg: %a\n",BootArg));
426 }
427 DEBUG_CODE_END();
428
429 //
430 // Set Linux CmdLine
431 //
432 if ((CommandLineArguments != NULL) && (AsciiStrLen (CommandLineArguments) > 0)) {
433 err = fdt_setprop(fdt, node, "bootargs", CommandLineArguments, AsciiStrSize(CommandLineArguments));
434 if (err) {
435 DEBUG((EFI_D_ERROR,"Fail to set new 'bootarg' (err:%d)\n",err));
436 }
437 }
438
439 //
440 // Set Linux Initrd
441 //
442 if (InitrdImageSize != 0) {
443 InitrdImageStart = cpu_to_fdt64 (InitrdImage);
444 err = fdt_setprop(fdt, node, "linux,initrd-start", &InitrdImageStart, sizeof(EFI_PHYSICAL_ADDRESS));
445 if (err) {
446 DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));
447 }
448 InitrdImageEnd = cpu_to_fdt64 (InitrdImage + InitrdImageSize);
449 err = fdt_setprop(fdt, node, "linux,initrd-end", &InitrdImageEnd, sizeof(EFI_PHYSICAL_ADDRESS));
450 if (err) {
451 DEBUG((EFI_D_ERROR,"Fail to set new 'linux,initrd-start' (err:%d)\n",err));
452 }
453 }
454
455 //
456 // Set Physical memory setup if does not exist
457 //
458 node = fdt_subnode_offset(fdt, 0, "memory");
459 if (node < 0) {
460 // The 'memory' node does not exist, create it
461 node = fdt_add_subnode(fdt, 0, "memory");
462 if (node >= 0) {
463 fdt_setprop_string(fdt, node, "name", "memory");
464 fdt_setprop_string(fdt, node, "device_type", "memory");
465
466 GetSystemMemoryResources (&ResourceList);
467 Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink;
468
469 Region.Base = cpu_to_fdtn ((UINTN)Resource->PhysicalStart);
470 Region.Size = cpu_to_fdtn ((UINTN)Resource->ResourceLength);
471
472 err = fdt_setprop(fdt, node, "reg", &Region, sizeof(Region));
473 if (err) {
474 DEBUG((EFI_D_ERROR,"Fail to set new 'memory region' (err:%d)\n",err));
475 }
476 }
477 }
478
479 //
480 // Add the memory regions reserved by the UEFI Firmware
481 //
482
483 // Retrieve the UEFI Memory Map
484 MemoryMap = NULL;
485 MemoryMapSize = 0;
486 Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
487 if (Status == EFI_BUFFER_TOO_SMALL) {
488 // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive
489 // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.
490 Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;
491 MemoryMap = AllocatePages (Pages);
492 if (MemoryMap == NULL) {
493 Status = EFI_OUT_OF_RESOURCES;
494 goto FAIL_COMPLETE_FDT;
495 }
496 Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
497 }
498
499 // Go through the list and add the reserved region to the Device Tree
500 if (!EFI_ERROR(Status)) {
501 MemoryMapPtr = MemoryMap;
502 for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {
503 if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE)MemoryMapPtr->Type)) {
504 DEBUG((DEBUG_VERBOSE, "Reserved region of type %d [0x%lX, 0x%lX]\n",
505 MemoryMapPtr->Type,
506 (UINTN)MemoryMapPtr->PhysicalStart,
507 (UINTN)(MemoryMapPtr->PhysicalStart + MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE)));
508 err = fdt_add_mem_rsv(fdt, MemoryMapPtr->PhysicalStart, MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE);
509 if (err != 0) {
510 Print(L"Warning: Fail to add 'memreserve' (err:%d)\n", err);
511 }
512 }
513 MemoryMapPtr = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + DescriptorSize);
514 }
515 }
516
517 //
518 // Setup Arm Mpcore Info if it is a multi-core or multi-cluster platforms.
519 //
520 // For 'cpus' and 'cpu' device tree nodes bindings, refer to this file
521 // in the kernel documentation:
522 // Documentation/devicetree/bindings/arm/cpus.txt
523 //
524 for (Index=0; Index < gST->NumberOfTableEntries; Index++) {
525 // Check for correct GUID type
526 if (CompareGuid (&gArmMpCoreInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
527 MpId = ArmReadMpidr ();
528 ClusterId = GET_CLUSTER_ID(MpId);
529 CoreId = GET_CORE_ID(MpId);
530
531 node = fdt_subnode_offset(fdt, 0, "cpus");
532 if (node < 0) {
533 // Create the /cpus node
534 node = fdt_add_subnode(fdt, 0, "cpus");
535 fdt_setprop_string(fdt, node, "name", "cpus");
536 fdt_setprop_cell (fdt, node, "#address-cells", sizeof (UINTN) / 4);
537 fdt_setprop_cell(fdt, node, "#size-cells", 0);
538 CpusNodeExist = FALSE;
539 } else {
540 CpusNodeExist = TRUE;
541 }
542
543 // Get pointer to ARM processor table
544 ArmProcessorTable = (ARM_PROCESSOR_TABLE *)gST->ConfigurationTable[Index].VendorTable;
545 ArmCoreInfoTable = ArmProcessorTable->ArmCpus;
546
547 for (Index = 0; Index < ArmProcessorTable->NumberOfEntries; Index++) {
548 CoreMpId = (UINTN) GET_MPID (ArmCoreInfoTable[Index].ClusterId,
549 ArmCoreInfoTable[Index].CoreId);
550 AsciiSPrint (Name, 10, "cpu@%x", CoreMpId);
551
552 // If the 'cpus' node did not exist then create all the 'cpu' nodes.
553 // In case 'cpus' node is provided in the original FDT then we do not add
554 // any 'cpu' node.
555 if (!CpusNodeExist) {
556 cpu_node = fdt_add_subnode (fdt, node, Name);
557 if (cpu_node < 0) {
558 DEBUG ((EFI_D_ERROR, "Error on creating '%s' node\n", Name));
559 Status = EFI_INVALID_PARAMETER;
560 goto FAIL_COMPLETE_FDT;
561 }
562
563 fdt_setprop_string (fdt, cpu_node, "device_type", "cpu");
564
565 CoreMpId = cpu_to_fdtn (CoreMpId);
566 fdt_setprop (fdt, cpu_node, "reg", &CoreMpId, sizeof (CoreMpId));
567 if (PsciSmcSupported) {
568 fdt_setprop_string (fdt, cpu_node, "enable-method", "psci");
569 }
570 } else {
571 cpu_node = fdt_subnode_offset(fdt, node, Name);
572 }
573
574 // If Power State Coordination Interface (PSCI) is not supported then it is expected the secondary
575 // cores are spinning waiting for the Operating System to release them
576 if ((PsciSmcSupported == FALSE) && (cpu_node >= 0)) {
577 // We as the bootloader are responsible for either creating or updating
578 // these entries. Do not trust the entries in the DT. We only know about
579 // 'spin-table' type. Do not try to update other types if defined.
580 Method = fdt_getprop(fdt, cpu_node, "enable-method", &lenp);
581 if ( (Method == NULL) || (!AsciiStrCmp((CHAR8 *)Method, "spin-table")) ) {
582 fdt_setprop_string(fdt, cpu_node, "enable-method", "spin-table");
583 CpuReleaseAddr = cpu_to_fdt64(ArmCoreInfoTable[Index].MailboxSetAddress);
584 fdt_setprop(fdt, cpu_node, "cpu-release-addr", &CpuReleaseAddr, sizeof(CpuReleaseAddr));
585
586 // If it is not the primary core than the cpu should be disabled
587 if (((ArmCoreInfoTable[Index].ClusterId != ClusterId) || (ArmCoreInfoTable[Index].CoreId != CoreId))) {
588 fdt_setprop_string(fdt, cpu_node, "status", "disabled");
589 }
590 } else {
591 Print(L"Warning: Unsupported enable-method type for CPU[%d] : %a\n", Index, (CHAR8 *)Method);
592 }
593 }
594 }
595 break;
596 }
597 }
598
599 // If the Power State Coordination Interface is supported then we signal it in the Device Tree
600 if (PsciSmcSupported == TRUE) {
601 // Before to create it we check if the node is not already defined in the Device Tree
602 node = fdt_subnode_offset(fdt, 0, "psci");
603 if (node < 0) {
604 // The 'psci' node does not exist, create it
605 node = fdt_add_subnode(fdt, 0, "psci");
606 if (node < 0) {
607 DEBUG((EFI_D_ERROR,"Error on creating 'psci' node\n"));
608 Status = EFI_INVALID_PARAMETER;
609 goto FAIL_COMPLETE_FDT;
610 } else {
611 fdt_setprop_string (fdt, node, "compatible", "arm,psci");
612 fdt_setprop_string (fdt, node, "method", "smc");
613
614 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_SUSPEND);
615 fdt_setprop (fdt, node, "cpu_suspend", &Smc, sizeof (Smc));
616
617 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_OFF);
618 fdt_setprop (fdt, node, "cpu_off", &Smc, sizeof (Smc));
619
620 Smc = cpu_to_fdtn (ARM_SMC_ARM_CPU_ON);
621 fdt_setprop (fdt, node, "cpu_on", &Smc, sizeof (Smc));
622
623 Smc = cpu_to_fdtn (ARM_SMC_ARM_MIGRATE);
624 fdt_setprop (fdt, node, "migrate", &Smc, sizeof (Smc));
625 }
626 }
627 }
628
629 DEBUG_CODE_BEGIN();
630 //DebugDumpFdt (fdt);
631 DEBUG_CODE_END();
632
633 // If we succeeded to generate the new Device Tree then free the old Device Tree
634 gBS->FreePages (*FdtBlobBase, EFI_SIZE_TO_PAGES (*FdtBlobSize));
635
636 *FdtBlobBase = NewFdtBlobBase;
637 *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(NewFdtBlobBase));
638 return EFI_SUCCESS;
639
640 FAIL_COMPLETE_FDT:
641 gBS->FreePages (NewFdtBlobAllocation, EFI_SIZE_TO_PAGES (NewFdtBlobSize));
642
643 FAIL_RELOCATE_FDT:
644 *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));
645 // Return success even if we failed to update the FDT blob.
646 // The original one is still valid.
647 return EFI_SUCCESS;
648 }