ArmPlatformPkg/Bds: Remove any use of the "Fdt" UEFI variable
[mirror_edk2.git] / ArmPlatformPkg / Library / EblCmdLib / EblCmdFdt.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 <Base.h>
16 #include <Uefi.h>
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>
26
27 #include <Protocol/DevicePathFromText.h>
28
29 #include <Guid/ArmGlobalVariableHob.h>
30 #include <Guid/Fdt.h>
31
32 #include <libfdt.h>
33
34 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
35 #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
36 #define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
37
38 STATIC
39 UINTN
40 IsPrintableString (
41 IN CONST VOID* data,
42 IN UINTN len
43 )
44 {
45 CONST CHAR8 *s = data;
46 CONST CHAR8 *ss;
47
48 // Zero length is not
49 if (len == 0) {
50 return 0;
51 }
52
53 // Must terminate with zero
54 if (s[len - 1] != '\0') {
55 return 0;
56 }
57
58 ss = s;
59 while (*s/* && isprint(*s)*/) {
60 s++;
61 }
62
63 // Not zero, or not done yet
64 if (*s != '\0' || (s + 1 - ss) < len) {
65 return 0;
66 }
67
68 return 1;
69 }
70
71 STATIC
72 VOID
73 PrintData (
74 IN CONST CHAR8* data,
75 IN UINTN len
76 )
77 {
78 UINTN i;
79 CONST CHAR8 *p = data;
80
81 // No data, don't print
82 if (len == 0)
83 return;
84
85 if (IsPrintableString (data, len)) {
86 Print(L" = \"%a\"", (const char *)data);
87 } else if ((len % 4) == 0) {
88 Print(L" = <");
89 for (i = 0; i < len; i += 4) {
90 Print(L"0x%08x%a", fdt32_to_cpu(GET_CELL(p)),i < (len - 4) ? " " : "");
91 }
92 Print(L">");
93 } else {
94 Print(L" = [");
95 for (i = 0; i < len; i++)
96 Print(L"%02x%a", *p++, i < len - 1 ? " " : "");
97 Print(L"]");
98 }
99 }
100
101 VOID
102 DumpFdt (
103 IN VOID* FdtBlob
104 )
105 {
106 struct fdt_header *bph;
107 UINT32 off_dt;
108 UINT32 off_str;
109 CONST CHAR8* p_struct;
110 CONST CHAR8* p_strings;
111 CONST CHAR8* p;
112 CONST CHAR8* s;
113 CONST CHAR8* t;
114 UINT32 tag;
115 UINTN sz;
116 UINTN depth;
117 UINTN shift;
118 UINT32 version;
119
120 depth = 0;
121 shift = 4;
122
123 bph = FdtBlob;
124 off_dt = fdt32_to_cpu(bph->off_dt_struct);
125 off_str = fdt32_to_cpu(bph->off_dt_strings);
126 p_struct = (CONST CHAR8*)FdtBlob + off_dt;
127 p_strings = (CONST CHAR8*)FdtBlob + off_str;
128 version = fdt32_to_cpu(bph->version);
129
130 p = p_struct;
131 while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
132
133 if (tag == FDT_BEGIN_NODE) {
134 s = p;
135 p = PALIGN(p + strlen(s) + 1, 4);
136
137 if (*s == '\0')
138 s = "/";
139
140 Print(L"%*s%a {\n", depth * shift, L" ", s);
141
142 depth++;
143 continue;
144 }
145
146 if (tag == FDT_END_NODE) {
147 depth--;
148
149 Print(L"%*s};\n", depth * shift, L" ");
150 continue;
151 }
152
153 if (tag == FDT_NOP) {
154 Print(L"%*s// [NOP]\n", depth * shift, L" ");
155 continue;
156 }
157
158 if (tag != FDT_PROP) {
159 Print(L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag);
160 break;
161 }
162 sz = fdt32_to_cpu(GET_CELL(p));
163 s = p_strings + fdt32_to_cpu(GET_CELL(p));
164 if (version < 16 && sz >= 8)
165 p = PALIGN(p, 8);
166 t = p;
167
168 p = PALIGN(p + sz, 4);
169
170 Print(L"%*s%a", depth * shift, L" ", s);
171 PrintData(t, sz);
172 Print(L";\n");
173 }
174 }
175
176 EFI_STATUS
177 EblDumpFdt (
178 IN UINTN Argc,
179 IN CHAR8 **Argv
180 )
181 {
182 EFI_STATUS Status;
183 VOID *FdtBlob;
184 UINTN Ret;
185
186 // If no FDT file is passed to the argument then get the one from the platform
187 if (Argc < 2) {
188 Status = EfiGetSystemConfigurationTable (&gFdtTableGuid, &FdtBlob);
189 if (EFI_ERROR (Status)) {
190 Print (L"ERROR: Did not find the Fdt Blob.\n");
191 return Status;
192 }
193 } else {
194 return EFI_NOT_FOUND;
195 }
196
197 Ret = fdt_check_header (FdtBlob);
198 if (Ret != 0) {
199 Print (L"ERROR: Device Tree header not valid (err:%d)\n", Ret);
200 return EFI_INVALID_PARAMETER;
201 }
202
203 DumpFdt (FdtBlob);
204
205 return EFI_SUCCESS;
206 }