cmd_fdt.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * (C) Copyright 2007
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. * Based on code written by:
  5. * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
  6. * Matthew McClintock <msm@freescale.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <linux/ctype.h>
  29. #include <linux/types.h>
  30. #include <asm/global_data.h>
  31. #include <fdt.h>
  32. #include <libfdt.h>
  33. #include <fdt_support.h>
  34. #define MAX_LEVEL 32 /* how deeply nested we will go */
  35. #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
  36. /*
  37. * Global data (for the gd->bd)
  38. */
  39. DECLARE_GLOBAL_DATA_PTR;
  40. static int fdt_valid(void);
  41. static int fdt_parse_prop(char **newval, int count, char *data, int *len);
  42. static int fdt_print(const char *pathp, char *prop, int depth);
  43. /*
  44. * Flattened Device Tree command, see the help for parameter definitions.
  45. */
  46. int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  47. {
  48. if (argc < 2) {
  49. printf ("Usage:\n%s\n", cmdtp->usage);
  50. return 1;
  51. }
  52. /********************************************************************
  53. * Set the address of the fdt
  54. ********************************************************************/
  55. if (argv[1][0] == 'a') {
  56. /*
  57. * Set the address [and length] of the fdt.
  58. */
  59. fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
  60. if (!fdt_valid()) {
  61. return 1;
  62. }
  63. if (argc >= 4) {
  64. int len;
  65. int err;
  66. /*
  67. * Optional new length
  68. */
  69. len = simple_strtoul(argv[3], NULL, 16);
  70. if (len < fdt_totalsize(fdt)) {
  71. printf ("New length %d < existing length %d, "
  72. "ignoring.\n",
  73. len, fdt_totalsize(fdt));
  74. } else {
  75. /*
  76. * Open in place with a new length.
  77. */
  78. err = fdt_open_into(fdt, fdt, len);
  79. if (err != 0) {
  80. printf ("libfdt fdt_open_into(): %s\n",
  81. fdt_strerror(err));
  82. }
  83. }
  84. }
  85. /********************************************************************
  86. * Move the fdt
  87. ********************************************************************/
  88. } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
  89. struct fdt_header *newaddr;
  90. int len;
  91. int err;
  92. if (argc < 4) {
  93. printf ("Usage:\n%s\n", cmdtp->usage);
  94. return 1;
  95. }
  96. /*
  97. * Set the address and length of the fdt.
  98. */
  99. fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
  100. if (!fdt_valid()) {
  101. return 1;
  102. }
  103. newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
  104. /*
  105. * If the user specifies a length, use that. Otherwise use the
  106. * current length.
  107. */
  108. if (argc <= 4) {
  109. len = fdt_totalsize(fdt);
  110. } else {
  111. len = simple_strtoul(argv[4], NULL, 16);
  112. if (len < fdt_totalsize(fdt)) {
  113. printf ("New length 0x%X < existing length "
  114. "0x%X, aborting.\n",
  115. len, fdt_totalsize(fdt));
  116. return 1;
  117. }
  118. }
  119. /*
  120. * Copy to the new location.
  121. */
  122. err = fdt_open_into(fdt, newaddr, len);
  123. if (err != 0) {
  124. printf ("libfdt fdt_open_into(): %s\n",
  125. fdt_strerror(err));
  126. return 1;
  127. }
  128. fdt = newaddr;
  129. /********************************************************************
  130. * Make a new node
  131. ********************************************************************/
  132. } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
  133. char *pathp; /* path */
  134. char *nodep; /* new node to add */
  135. int nodeoffset; /* node offset from libfdt */
  136. int err;
  137. /*
  138. * Parameters: Node path, new node to be appended to the path.
  139. */
  140. if (argc < 4) {
  141. printf ("Usage:\n%s\n", cmdtp->usage);
  142. return 1;
  143. }
  144. pathp = argv[2];
  145. nodep = argv[3];
  146. nodeoffset = fdt_path_offset (fdt, pathp);
  147. if (nodeoffset < 0) {
  148. /*
  149. * Not found or something else bad happened.
  150. */
  151. printf ("libfdt fdt_path_offset() returned %s\n",
  152. fdt_strerror(nodeoffset));
  153. return 1;
  154. }
  155. err = fdt_add_subnode(fdt, nodeoffset, nodep);
  156. if (err < 0) {
  157. printf ("libfdt fdt_add_subnode(): %s\n",
  158. fdt_strerror(err));
  159. return 1;
  160. }
  161. /********************************************************************
  162. * Set the value of a property in the fdt.
  163. ********************************************************************/
  164. } else if (argv[1][0] == 's') {
  165. char *pathp; /* path */
  166. char *prop; /* property */
  167. int nodeoffset; /* node offset from libfdt */
  168. static char data[SCRATCHPAD]; /* storage for the property */
  169. int len; /* new length of the property */
  170. int ret; /* return value */
  171. /*
  172. * Parameters: Node path, property, optional value.
  173. */
  174. if (argc < 4) {
  175. printf ("Usage:\n%s\n", cmdtp->usage);
  176. return 1;
  177. }
  178. pathp = argv[2];
  179. prop = argv[3];
  180. if (argc == 4) {
  181. len = 0;
  182. } else {
  183. ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
  184. if (ret != 0)
  185. return ret;
  186. }
  187. nodeoffset = fdt_path_offset (fdt, pathp);
  188. if (nodeoffset < 0) {
  189. /*
  190. * Not found or something else bad happened.
  191. */
  192. printf ("libfdt fdt_path_offset() returned %s\n",
  193. fdt_strerror(nodeoffset));
  194. return 1;
  195. }
  196. ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
  197. if (ret < 0) {
  198. printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
  199. return 1;
  200. }
  201. /********************************************************************
  202. * Print (recursive) / List (single level)
  203. ********************************************************************/
  204. } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
  205. int depth = MAX_LEVEL; /* how deep to print */
  206. char *pathp; /* path */
  207. char *prop; /* property */
  208. int ret; /* return value */
  209. static char root[2] = "/";
  210. /*
  211. * list is an alias for print, but limited to 1 level
  212. */
  213. if (argv[1][0] == 'l') {
  214. depth = 1;
  215. }
  216. /*
  217. * Get the starting path. The root node is an oddball,
  218. * the offset is zero and has no name.
  219. */
  220. if (argc == 2)
  221. pathp = root;
  222. else
  223. pathp = argv[2];
  224. if (argc > 3)
  225. prop = argv[3];
  226. else
  227. prop = NULL;
  228. ret = fdt_print(pathp, prop, depth);
  229. if (ret != 0)
  230. return ret;
  231. /********************************************************************
  232. * Remove a property/node
  233. ********************************************************************/
  234. } else if ((argv[1][0] == 'r') && (argv[1][1] == 'm')) {
  235. int nodeoffset; /* node offset from libfdt */
  236. int err;
  237. /*
  238. * Get the path. The root node is an oddball, the offset
  239. * is zero and has no name.
  240. */
  241. nodeoffset = fdt_path_offset (fdt, argv[2]);
  242. if (nodeoffset < 0) {
  243. /*
  244. * Not found or something else bad happened.
  245. */
  246. printf ("libfdt fdt_path_offset() returned %s\n",
  247. fdt_strerror(nodeoffset));
  248. return 1;
  249. }
  250. /*
  251. * Do the delete. A fourth parameter means delete a property,
  252. * otherwise delete the node.
  253. */
  254. if (argc > 3) {
  255. err = fdt_delprop(fdt, nodeoffset, argv[3]);
  256. if (err < 0) {
  257. printf("libfdt fdt_delprop(): %s\n",
  258. fdt_strerror(err));
  259. return err;
  260. }
  261. } else {
  262. err = fdt_del_node(fdt, nodeoffset);
  263. if (err < 0) {
  264. printf("libfdt fdt_del_node(): %s\n",
  265. fdt_strerror(err));
  266. return err;
  267. }
  268. }
  269. /********************************************************************
  270. * Display header info
  271. ********************************************************************/
  272. } else if (argv[1][0] == 'h') {
  273. u32 version = fdt_version(fdt);
  274. printf("magic:\t\t\t0x%x\n", fdt_magic(fdt));
  275. printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(fdt), fdt_totalsize(fdt));
  276. printf("off_dt_struct:\t\t0x%x\n", fdt_off_dt_struct(fdt));
  277. printf("off_dt_strings:\t\t0x%x\n", fdt_off_dt_strings(fdt));
  278. printf("off_mem_rsvmap:\t\t0x%x\n", fdt_off_mem_rsvmap(fdt));
  279. printf("version:\t\t%d\n", version);
  280. printf("last_comp_version:\t%d\n", fdt_last_comp_version(fdt));
  281. if (version >= 2)
  282. printf("boot_cpuid_phys:\t0x%x\n",
  283. fdt_boot_cpuid_phys(fdt));
  284. if (version >= 3)
  285. printf("size_dt_strings:\t0x%x\n",
  286. fdt_size_dt_strings(fdt));
  287. if (version >= 17)
  288. printf("size_dt_struct:\t\t0x%x\n",
  289. fdt_size_dt_struct(fdt));
  290. printf("number mem_rsv:\t\t0x%x\n", fdt_num_mem_rsv(fdt));
  291. printf("\n");
  292. /********************************************************************
  293. * Set boot cpu id
  294. ********************************************************************/
  295. } else if ((argv[1][0] == 'b') && (argv[1][1] == 'o') &&
  296. (argv[1][2] == 'o')) {
  297. unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
  298. fdt_set_boot_cpuid_phys(fdt, tmp);
  299. /********************************************************************
  300. * memory command
  301. ********************************************************************/
  302. } else if ((argv[1][0] == 'm') && (argv[1][1] == 'e')) {
  303. uint64_t addr, size;
  304. int err;
  305. #ifdef CFG_64BIT_STRTOUL
  306. addr = simple_strtoull(argv[2], NULL, 16);
  307. size = simple_strtoull(argv[3], NULL, 16);
  308. #else
  309. addr = simple_strtoul(argv[2], NULL, 16);
  310. size = simple_strtoul(argv[3], NULL, 16);
  311. #endif
  312. err = fdt_fixup_memory(fdt, addr, size);
  313. if (err < 0)
  314. return err;
  315. /********************************************************************
  316. * mem reserve commands
  317. ********************************************************************/
  318. } else if ((argv[1][0] == 'r') && (argv[1][1] == 's')) {
  319. if (argv[2][0] == 'p') {
  320. uint64_t addr, size;
  321. int total = fdt_num_mem_rsv(fdt);
  322. int j, err;
  323. printf("index\t\t start\t\t size\n");
  324. printf("-------------------------------"
  325. "-----------------\n");
  326. for (j = 0; j < total; j++) {
  327. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  328. if (err < 0) {
  329. printf("libfdt fdt_get_mem_rsv(): %s\n",
  330. fdt_strerror(err));
  331. return err;
  332. }
  333. printf(" %x\t%08x%08x\t%08x%08x\n", j,
  334. (u32)(addr >> 32),
  335. (u32)(addr & 0xffffffff),
  336. (u32)(size >> 32),
  337. (u32)(size & 0xffffffff));
  338. }
  339. } else if (argv[2][0] == 'a') {
  340. uint64_t addr, size;
  341. int err;
  342. #ifdef CFG_64BIT_STRTOUL
  343. addr = simple_strtoull(argv[3], NULL, 16);
  344. size = simple_strtoull(argv[4], NULL, 16);
  345. #else
  346. addr = simple_strtoul(argv[3], NULL, 16);
  347. size = simple_strtoul(argv[4], NULL, 16);
  348. #endif
  349. err = fdt_add_mem_rsv(fdt, addr, size);
  350. if (err < 0) {
  351. printf("libfdt fdt_add_mem_rsv(): %s\n",
  352. fdt_strerror(err));
  353. return err;
  354. }
  355. } else if (argv[2][0] == 'd') {
  356. unsigned long idx = simple_strtoul(argv[3], NULL, 16);
  357. int err = fdt_del_mem_rsv(fdt, idx);
  358. if (err < 0) {
  359. printf("libfdt fdt_del_mem_rsv(): %s\n",
  360. fdt_strerror(err));
  361. return err;
  362. }
  363. } else {
  364. /* Unrecognized command */
  365. printf ("Usage:\n%s\n", cmdtp->usage);
  366. return 1;
  367. }
  368. }
  369. #ifdef CONFIG_OF_BOARD_SETUP
  370. /* Call the board-specific fixup routine */
  371. else if (argv[1][0] == 'b')
  372. ft_board_setup(fdt, gd->bd);
  373. #endif
  374. /* Create a chosen node */
  375. else if (argv[1][0] == 'c')
  376. fdt_chosen(fdt, 0, 0, 1);
  377. else {
  378. /* Unrecognized command */
  379. printf ("Usage:\n%s\n", cmdtp->usage);
  380. return 1;
  381. }
  382. return 0;
  383. }
  384. /****************************************************************************/
  385. static int fdt_valid(void)
  386. {
  387. int err;
  388. if (fdt == NULL) {
  389. printf ("The address of the fdt is invalid (NULL).\n");
  390. return 0;
  391. }
  392. err = fdt_check_header(fdt);
  393. if (err == 0)
  394. return 1; /* valid */
  395. if (err < 0) {
  396. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  397. /*
  398. * Be more informative on bad version.
  399. */
  400. if (err == -FDT_ERR_BADVERSION) {
  401. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
  402. printf (" - too old, fdt $d < %d",
  403. fdt_version(fdt),
  404. FDT_FIRST_SUPPORTED_VERSION);
  405. fdt = NULL;
  406. }
  407. if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
  408. printf (" - too new, fdt $d > %d",
  409. fdt_version(fdt),
  410. FDT_LAST_SUPPORTED_VERSION);
  411. fdt = NULL;
  412. }
  413. return 0;
  414. }
  415. printf("\n");
  416. return 0;
  417. }
  418. return 1;
  419. }
  420. /****************************************************************************/
  421. /*
  422. * Parse the user's input, partially heuristic. Valid formats:
  423. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  424. * C conventions.
  425. * [00 11 22 .. nn] - byte stream
  426. * "string" - If the the value doesn't start with "<" or "[", it is
  427. * treated as a string. Note that the quotes are
  428. * stripped by the parser before we get the string.
  429. * newval: An array of strings containing the new property as specified
  430. * on the command line
  431. * count: The number of strings in the array
  432. * data: A bytestream to be placed in the property
  433. * len: The length of the resulting bytestream
  434. */
  435. static int fdt_parse_prop(char **newval, int count, char *data, int *len)
  436. {
  437. char *cp; /* temporary char pointer */
  438. char *newp; /* temporary newval char pointer */
  439. unsigned long tmp; /* holds converted values */
  440. int stridx = 0;
  441. *len = 0;
  442. newp = newval[0];
  443. /* An array of cells */
  444. if (*newp == '<') {
  445. newp++;
  446. while ((*newp != '>') && (stridx < count)) {
  447. /*
  448. * Keep searching until we find that last ">"
  449. * That way users don't have to escape the spaces
  450. */
  451. if (*newp == '\0') {
  452. newp = newval[++stridx];
  453. continue;
  454. }
  455. cp = newp;
  456. tmp = simple_strtoul(cp, &newp, 0);
  457. *(uint32_t *)data = __cpu_to_be32(tmp);
  458. data += 4;
  459. *len += 4;
  460. /* If the ptr didn't advance, something went wrong */
  461. if ((newp - cp) <= 0) {
  462. printf("Sorry, I could not convert \"%s\"\n",
  463. cp);
  464. return 1;
  465. }
  466. while (*newp == ' ')
  467. newp++;
  468. }
  469. if (*newp != '>') {
  470. printf("Unexpected character '%c'\n", *newp);
  471. return 1;
  472. }
  473. } else if (*newp == '[') {
  474. /*
  475. * Byte stream. Convert the values.
  476. */
  477. newp++;
  478. while ((*newp != ']') && (stridx < count)) {
  479. tmp = simple_strtoul(newp, &newp, 16);
  480. *data++ = tmp & 0xFF;
  481. *len = *len + 1;
  482. while (*newp == ' ')
  483. newp++;
  484. if (*newp != '\0')
  485. newp = newval[++stridx];
  486. }
  487. if (*newp != ']') {
  488. printf("Unexpected character '%c'\n", *newval);
  489. return 1;
  490. }
  491. } else {
  492. /*
  493. * Assume it is a string. Copy it into our data area for
  494. * convenience (including the terminating '\0').
  495. */
  496. while (stridx < count) {
  497. *len = strlen(newp) + 1;
  498. strcpy(data, newp);
  499. newp = newval[++stridx];
  500. }
  501. }
  502. return 0;
  503. }
  504. /****************************************************************************/
  505. /*
  506. * Heuristic to guess if this is a string or concatenated strings.
  507. */
  508. static int is_printable_string(const void *data, int len)
  509. {
  510. const char *s = data;
  511. /* zero length is not */
  512. if (len == 0)
  513. return 0;
  514. /* must terminate with zero */
  515. if (s[len - 1] != '\0')
  516. return 0;
  517. /* printable or a null byte (concatenated strings) */
  518. while (((*s == '\0') || isprint(*s)) && (len > 0)) {
  519. /*
  520. * If we see a null, there are three possibilities:
  521. * 1) If len == 1, it is the end of the string, printable
  522. * 2) Next character also a null, not printable.
  523. * 3) Next character not a null, continue to check.
  524. */
  525. if (s[0] == '\0') {
  526. if (len == 1)
  527. return 1;
  528. if (s[1] == '\0')
  529. return 0;
  530. }
  531. s++;
  532. len--;
  533. }
  534. /* Not the null termination, or not done yet: not printable */
  535. if (*s != '\0' || (len != 0))
  536. return 0;
  537. return 1;
  538. }
  539. /*
  540. * Print the property in the best format, a heuristic guess. Print as
  541. * a string, concatenated strings, a byte, word, double word, or (if all
  542. * else fails) it is printed as a stream of bytes.
  543. */
  544. static void print_data(const void *data, int len)
  545. {
  546. int j;
  547. /* no data, don't print */
  548. if (len == 0)
  549. return;
  550. /*
  551. * It is a string, but it may have multiple strings (embedded '\0's).
  552. */
  553. if (is_printable_string(data, len)) {
  554. puts("\"");
  555. j = 0;
  556. while (j < len) {
  557. if (j > 0)
  558. puts("\", \"");
  559. puts(data);
  560. j += strlen(data) + 1;
  561. data += strlen(data) + 1;
  562. }
  563. puts("\"");
  564. return;
  565. }
  566. if ((len %4) == 0) {
  567. const u32 *p;
  568. printf("<");
  569. for (j = 0, p = data; j < len/4; j ++)
  570. printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
  571. printf(">");
  572. } else { /* anything else... hexdump */
  573. const u8 *s;
  574. printf("[");
  575. for (j = 0, s = data; j < len; j++)
  576. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  577. printf("]");
  578. }
  579. }
  580. /****************************************************************************/
  581. /*
  582. * Recursively print (a portion of) the fdt. The depth parameter
  583. * determines how deeply nested the fdt is printed.
  584. */
  585. static int fdt_print(const char *pathp, char *prop, int depth)
  586. {
  587. static char tabs[MAX_LEVEL+1] =
  588. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  589. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  590. const void *nodep; /* property node pointer */
  591. int nodeoffset; /* node offset from libfdt */
  592. int nextoffset; /* next node offset from libfdt */
  593. uint32_t tag; /* tag */
  594. int len; /* length of the property */
  595. int level = 0; /* keep track of nesting level */
  596. const struct fdt_property *fdt_prop;
  597. nodeoffset = fdt_path_offset (fdt, pathp);
  598. if (nodeoffset < 0) {
  599. /*
  600. * Not found or something else bad happened.
  601. */
  602. printf ("libfdt fdt_path_offset() returned %s\n",
  603. fdt_strerror(nodeoffset));
  604. return 1;
  605. }
  606. /*
  607. * The user passed in a property as well as node path.
  608. * Print only the given property and then return.
  609. */
  610. if (prop) {
  611. nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
  612. if (len == 0) {
  613. /* no property value */
  614. printf("%s %s\n", pathp, prop);
  615. return 0;
  616. } else if (len > 0) {
  617. printf("%s = ", prop);
  618. print_data (nodep, len);
  619. printf("\n");
  620. return 0;
  621. } else {
  622. printf ("libfdt fdt_getprop(): %s\n",
  623. fdt_strerror(len));
  624. return 1;
  625. }
  626. }
  627. /*
  628. * The user passed in a node path and no property,
  629. * print the node and all subnodes.
  630. */
  631. while(level >= 0) {
  632. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
  633. switch(tag) {
  634. case FDT_BEGIN_NODE:
  635. pathp = fdt_get_name(fdt, nodeoffset, NULL);
  636. if (level <= depth) {
  637. if (pathp == NULL)
  638. pathp = "/* NULL pointer error */";
  639. if (*pathp == '\0')
  640. pathp = "/"; /* root is nameless */
  641. printf("%s%s {\n",
  642. &tabs[MAX_LEVEL - level], pathp);
  643. }
  644. level++;
  645. if (level >= MAX_LEVEL) {
  646. printf("Nested too deep, aborting.\n");
  647. return 1;
  648. }
  649. break;
  650. case FDT_END_NODE:
  651. level--;
  652. if (level <= depth)
  653. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  654. if (level == 0) {
  655. level = -1; /* exit the loop */
  656. }
  657. break;
  658. case FDT_PROP:
  659. fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
  660. sizeof(*fdt_prop));
  661. pathp = fdt_string(fdt,
  662. fdt32_to_cpu(fdt_prop->nameoff));
  663. len = fdt32_to_cpu(fdt_prop->len);
  664. nodep = fdt_prop->data;
  665. if (len < 0) {
  666. printf ("libfdt fdt_getprop(): %s\n",
  667. fdt_strerror(len));
  668. return 1;
  669. } else if (len == 0) {
  670. /* the property has no value */
  671. if (level <= depth)
  672. printf("%s%s;\n",
  673. &tabs[MAX_LEVEL - level],
  674. pathp);
  675. } else {
  676. if (level <= depth) {
  677. printf("%s%s = ",
  678. &tabs[MAX_LEVEL - level],
  679. pathp);
  680. print_data (nodep, len);
  681. printf(";\n");
  682. }
  683. }
  684. break;
  685. case FDT_NOP:
  686. printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
  687. break;
  688. case FDT_END:
  689. return 1;
  690. default:
  691. if (level <= depth)
  692. printf("Unknown tag 0x%08X\n", tag);
  693. return 1;
  694. }
  695. nodeoffset = nextoffset;
  696. }
  697. return 0;
  698. }
  699. /********************************************************************/
  700. U_BOOT_CMD(
  701. fdt, 255, 0, do_fdt,
  702. "fdt - flattened device tree utility commands\n",
  703. "addr <addr> [<length>] - Set the fdt location to <addr>\n"
  704. #ifdef CONFIG_OF_BOARD_SETUP
  705. "fdt boardsetup - Do board-specific set up\n"
  706. #endif
  707. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  708. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  709. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  710. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  711. "fdt mknode <path> <node> - Create a new node after <path>\n"
  712. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  713. "fdt header - Display header info\n"
  714. "fdt bootcpu <id> - Set boot cpuid\n"
  715. "fdt memory <addr> <size> - Add/Update memory node\n"
  716. "fdt rsvmem print - Show current mem reserves\n"
  717. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  718. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  719. "fdt chosen - Add/update the /chosen branch in the tree\n"
  720. "NOTE: If the path or property you are setting/printing has a '#' character\n"
  721. " or spaces, you MUST escape it with a \\ character or quote it with \".\n"
  722. );