3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
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
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.
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/BdsLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/UefiLib.h>
23 #include <Library/UefiApplicationEntryPoint.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/UefiRuntimeServicesTableLib.h>
27 #include <Protocol/DevicePathFromText.h>
29 #include <Guid/ArmGlobalVariableHob.h>
33 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
34 #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
35 #define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
44 CONST CHAR8
*s
= data
;
52 // Must terminate with zero
53 if (s
[len
- 1] != '\0') {
58 while (*s
/* && isprint(*s)*/) {
62 // Not zero, or not done yet
63 if (*s
!= '\0' || (s
+ 1 - ss
) < len
) {
78 CONST CHAR8
*p
= data
;
80 // No data, don't print
84 if (IsPrintableString (data
, len
)) {
85 Print(L
" = \"%a\"", (const char *)data
);
86 } else if ((len
% 4) == 0) {
88 for (i
= 0; i
< len
; i
+= 4) {
89 Print(L
"0x%08x%a", fdt32_to_cpu(GET_CELL(p
)),i
< (len
- 4) ? " " : "");
94 for (i
= 0; i
< len
; i
++)
95 Print(L
"%02x%a", *p
++, i
< len
- 1 ? " " : "");
105 struct fdt_header
*bph
;
108 CONST CHAR8
* p_struct
;
109 CONST CHAR8
* p_strings
;
123 off_dt
= fdt32_to_cpu(bph
->off_dt_struct
);
124 off_str
= fdt32_to_cpu(bph
->off_dt_strings
);
125 p_struct
= (CONST CHAR8
*)FdtBlob
+ off_dt
;
126 p_strings
= (CONST CHAR8
*)FdtBlob
+ off_str
;
127 version
= fdt32_to_cpu(bph
->version
);
130 while ((tag
= fdt32_to_cpu(GET_CELL(p
))) != FDT_END
) {
132 if (tag
== FDT_BEGIN_NODE
) {
134 p
= PALIGN(p
+ strlen(s
) + 1, 4);
139 Print(L
"%*s%a {\n", depth
* shift
, L
" ", s
);
145 if (tag
== FDT_END_NODE
) {
148 Print(L
"%*s};\n", depth
* shift
, L
" ");
152 if (tag
== FDT_NOP
) {
153 Print(L
"%*s// [NOP]\n", depth
* shift
, L
" ");
157 if (tag
!= FDT_PROP
) {
158 Print(L
"%*s ** Unknown tag 0x%08x\n", depth
* shift
, L
" ", tag
);
161 sz
= fdt32_to_cpu(GET_CELL(p
));
162 s
= p_strings
+ fdt32_to_cpu(GET_CELL(p
));
163 if (version
< 16 && sz
>= 8)
167 p
= PALIGN(p
+ sz
, 4);
169 Print(L
"%*s%a", depth
* shift
, L
" ", s
);
182 EFI_DEVICE_PATH
* FdtDevicePath
;
183 EFI_PHYSICAL_ADDRESS FdtBlob
;
186 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
*EfiDevicePathFromTextProtocol
;
188 // If no FDT file is passed to the argument then get the one from the platform
190 Status
= GetEnvironmentVariable (L
"Fdt", &gArmGlobalVariableGuid
, NULL
, NULL
, (VOID
**)&FdtDevicePath
);
191 if (Status
== EFI_NOT_FOUND
) {
192 // No set yet, get the Default Device Path
193 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
194 ASSERT_EFI_ERROR(Status
);
195 FdtDevicePath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdFdtDevicePath
));
198 return EFI_NOT_FOUND
;
201 Status
= BdsLoadImage (FdtDevicePath
, AllocateAnyPages
, &FdtBlob
, &FdtBlobSize
);
202 if (EFI_ERROR(Status
)) {
203 Print (L
"ERROR: Did not find the Fdt Blob.\n");
207 Ret
= fdt_check_header((CONST VOID
*)(UINTN
)FdtBlob
);
209 Print (L
"ERROR: Device Tree header not valid (err:%d)\n",Ret
);
213 DumpFdt ((VOID
*)(UINTN
)FdtBlob
);
215 FreePool (FdtDevicePath
);