3 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>
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.
15 #include "FdtPlatform.h"
17 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
18 #define PALIGN(p, a) ((void *)(ALIGN ((unsigned long)(p), (a))))
19 #define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
28 CONST CHAR8
*s
= data
;
36 // Must terminate with zero
37 if (s
[len
- 1] != '\0') {
42 while (*s
/* && isprint (*s)*/) {
46 // Not zero, or not done yet
47 if (*s
!= '\0' || (s
+ 1 - ss
) < len
) {
62 CONST CHAR8
*p
= data
;
64 // No data, don't print
68 if (IsPrintableString (data
, len
)) {
69 Print (L
" = \"%a\"", (const char *)data
);
70 } else if ((len
% 4) == 0) {
72 for (i
= 0; i
< len
; i
+= 4) {
73 Print (L
"0x%08x%a", fdt32_to_cpu (GET_CELL (p
)), i
< (len
- 4) ? " " : "");
78 for (i
= 0; i
< len
; i
++)
79 Print (L
"%02x%a", *p
++, i
< len
- 1 ? " " : "");
90 struct fdt_header
*bph
;
93 CONST CHAR8
* p_struct
;
94 CONST CHAR8
* p_strings
;
105 // Can 'memreserve' be printed by below code?
106 INTN num
= fdt_num_mem_rsv (FdtBlob
);
108 UINT64 addr
= 0, size
= 0;
110 for (i
= 0; i
< num
; i
++) {
111 err
= fdt_get_mem_rsv (FdtBlob
, i
, &addr
, &size
);
113 DEBUG ((EFI_D_ERROR
, "Error (%d) : Cannot get memreserve section (%d)\n", err
, i
));
116 Print (L
"/memreserve/ \t0x%lx \t0x%lx;\n", addr
, size
);
125 off_dt
= fdt32_to_cpu (bph
->off_dt_struct
);
126 off_str
= fdt32_to_cpu (bph
->off_dt_strings
);
127 p_struct
= (CONST CHAR8
*)FdtBlob
+ off_dt
;
128 p_strings
= (CONST CHAR8
*)FdtBlob
+ off_str
;
129 version
= fdt32_to_cpu (bph
->version
);
132 while ((tag
= fdt32_to_cpu (GET_CELL (p
))) != FDT_END
) {
133 if (tag
== FDT_BEGIN_NODE
) {
135 p
= PALIGN (p
+ AsciiStrLen (s
) + 1, 4);
140 Print (L
"%*s%a {\n", depth
* shift
, L
" ", s
);
146 if (tag
== FDT_END_NODE
) {
149 Print (L
"%*s};\n", depth
* shift
, L
" ");
153 if (tag
== FDT_NOP
) {
154 Print (L
"%*s// [NOP]\n", depth
* shift
, L
" ");
158 if (tag
!= FDT_PROP
) {
159 Print (L
"%*s ** Unknown tag 0x%08x\n", depth
* shift
, L
" ", tag
);
162 sz
= fdt32_to_cpu (GET_CELL (p
));
163 s
= p_strings
+ fdt32_to_cpu (GET_CELL (p
));
164 if (version
< 16 && sz
>= 8)
168 p
= PALIGN (p
+ sz
, 4);
170 Print (L
"%*s%a", depth
* shift
, L
" ", s
);
177 This is the shell command "dumpfdt" handler function. This function handles
178 the command when it is invoked in the shell.
180 @param[in] This The instance of the
181 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.
182 @param[in] SystemTable The pointer to the UEFI system table.
183 @param[in] ShellParameters The parameters associated with the command.
184 @param[in] Shell The instance of the shell protocol used in the
185 context of processing this command.
187 @return SHELL_SUCCESS The operation was successful.
188 @return SHELL_ABORTED Operation aborted due to internal error.
189 @return SHELL_NOT_FOUND Failed to locate the Device Tree into the EFI Configuration Table
190 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
195 ShellDynCmdDumpFdtHandler (
196 IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*This
,
197 IN EFI_SYSTEM_TABLE
*SystemTable
,
198 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ShellParameters
,
199 IN EFI_SHELL_PROTOCOL
*Shell
202 SHELL_STATUS ShellStatus
;
206 ShellStatus
= SHELL_SUCCESS
;
209 // Install the Shell and Shell Parameters Protocols on the driver
210 // image. This is necessary for the initialisation of the Shell
211 // Library to succeed in the next step.
213 Status
= gBS
->InstallMultipleProtocolInterfaces (
215 &gEfiShellProtocolGuid
, Shell
,
216 &gEfiShellParametersProtocolGuid
, ShellParameters
,
219 if (EFI_ERROR (Status
)) {
220 return SHELL_ABORTED
;
224 // Initialise the Shell Library as we are going to use it.
225 // Assert that the return code is EFI_SUCCESS as it should.
226 // To anticipate any change is the codes returned by
227 // ShellInitialize(), leave in case of error.
229 Status
= ShellInitialize ();
230 if (EFI_ERROR (Status
)) {
231 ASSERT_EFI_ERROR (Status
);
232 return SHELL_ABORTED
;
235 Status
= EfiGetSystemConfigurationTable (&gFdtTableGuid
, &FdtBlob
);
236 if (EFI_ERROR (Status
)) {
237 Print (L
"ERROR: Did not find the Fdt Blob.\n");
238 return EfiCodeToShellCode (Status
);
243 gBS
->UninstallMultipleProtocolInterfaces (
245 &gEfiShellProtocolGuid
, Shell
,
246 &gEfiShellParametersProtocolGuid
, ShellParameters
,
254 This is the shell command "dumpfdt" help handler function. This
255 function returns the formatted help for the "dumpfdt" command.
256 The format matchs that in Appendix B of the revision 2.1 of the
257 UEFI Shell Specification.
259 @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.
260 @param[in] Language The pointer to the language string to use.
262 @return CHAR16* Pool allocated help string, must be freed by caller.
266 ShellDynCmdDumpFdtGetHelp (
267 IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*This
,
268 IN CONST CHAR8
*Language
272 // This allocates memory. The caller has to free the allocated memory.
274 return HiiGetString (
275 mFdtPlatformDxeHiiHandle
,
276 STRING_TOKEN (STR_GET_HELP_DUMPFDT
),