cmd_fdt.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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') && (argv[1][1] == 'o') &&
  372. (argv[1][2] == 'a'))
  373. ft_board_setup(fdt, gd->bd);
  374. #endif
  375. /* Create a chosen node */
  376. else if (argv[1][0] == 'c')
  377. fdt_chosen(fdt, 0, 0, 1);
  378. else {
  379. /* Unrecognized command */
  380. printf ("Usage:\n%s\n", cmdtp->usage);
  381. return 1;
  382. }
  383. return 0;
  384. }
  385. /****************************************************************************/
  386. static int fdt_valid(void)
  387. {
  388. int err;
  389. if (fdt == NULL) {
  390. printf ("The address of the fdt is invalid (NULL).\n");
  391. return 0;
  392. }
  393. err = fdt_check_header(fdt);
  394. if (err == 0)
  395. return 1; /* valid */
  396. if (err < 0) {
  397. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  398. /*
  399. * Be more informative on bad version.
  400. */
  401. if (err == -FDT_ERR_BADVERSION) {
  402. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
  403. printf (" - too old, fdt $d < %d",
  404. fdt_version(fdt),
  405. FDT_FIRST_SUPPORTED_VERSION);
  406. fdt = NULL;
  407. }
  408. if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
  409. printf (" - too new, fdt $d > %d",
  410. fdt_version(fdt),
  411. FDT_LAST_SUPPORTED_VERSION);
  412. fdt = NULL;
  413. }
  414. return 0;
  415. }
  416. printf("\n");
  417. return 0;
  418. }
  419. return 1;
  420. }
  421. /****************************************************************************/
  422. /*
  423. * Parse the user's input, partially heuristic. Valid formats:
  424. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  425. * C conventions.
  426. * [00 11 22 .. nn] - byte stream
  427. * "string" - If the the value doesn't start with "<" or "[", it is
  428. * treated as a string. Note that the quotes are
  429. * stripped by the parser before we get the string.
  430. * newval: An array of strings containing the new property as specified
  431. * on the command line
  432. * count: The number of strings in the array
  433. * data: A bytestream to be placed in the property
  434. * len: The length of the resulting bytestream
  435. */
  436. static int fdt_parse_prop(char **newval, int count, char *data, int *len)
  437. {
  438. char *cp; /* temporary char pointer */
  439. char *newp; /* temporary newval char pointer */
  440. unsigned long tmp; /* holds converted values */
  441. int stridx = 0;
  442. *len = 0;
  443. newp = newval[0];
  444. /* An array of cells */
  445. if (*newp == '<') {
  446. newp++;
  447. while ((*newp != '>') && (stridx < count)) {
  448. /*
  449. * Keep searching until we find that last ">"
  450. * That way users don't have to escape the spaces
  451. */
  452. if (*newp == '\0') {
  453. newp = newval[++stridx];
  454. continue;
  455. }
  456. cp = newp;
  457. tmp = simple_strtoul(cp, &newp, 0);
  458. *(uint32_t *)data = __cpu_to_be32(tmp);
  459. data += 4;
  460. *len += 4;
  461. /* If the ptr didn't advance, something went wrong */
  462. if ((newp - cp) <= 0) {
  463. printf("Sorry, I could not convert \"%s\"\n",
  464. cp);
  465. return 1;
  466. }
  467. while (*newp == ' ')
  468. newp++;
  469. }
  470. if (*newp != '>') {
  471. printf("Unexpected character '%c'\n", *newp);
  472. return 1;
  473. }
  474. } else if (*newp == '[') {
  475. /*
  476. * Byte stream. Convert the values.
  477. */
  478. newp++;
  479. while ((*newp != ']') && (stridx < count)) {
  480. tmp = simple_strtoul(newp, &newp, 16);
  481. *data++ = tmp & 0xFF;
  482. *len = *len + 1;
  483. while (*newp == ' ')
  484. newp++;
  485. if (*newp != '\0')
  486. newp = newval[++stridx];
  487. }
  488. if (*newp != ']') {
  489. printf("Unexpected character '%c'\n", *newval);
  490. return 1;
  491. }
  492. } else {
  493. /*
  494. * Assume it is a string. Copy it into our data area for
  495. * convenience (including the terminating '\0').
  496. */
  497. while (stridx < count) {
  498. *len = strlen(newp) + 1;
  499. strcpy(data, newp);
  500. newp = newval[++stridx];
  501. }
  502. }
  503. return 0;
  504. }
  505. /****************************************************************************/
  506. /*
  507. * Heuristic to guess if this is a string or concatenated strings.
  508. */
  509. static int is_printable_string(const void *data, int len)
  510. {
  511. const char *s = data;
  512. /* zero length is not */
  513. if (len == 0)
  514. return 0;
  515. /* must terminate with zero */
  516. if (s[len - 1] != '\0')
  517. return 0;
  518. /* printable or a null byte (concatenated strings) */
  519. while (((*s == '\0') || isprint(*s)) && (len > 0)) {
  520. /*
  521. * If we see a null, there are three possibilities:
  522. * 1) If len == 1, it is the end of the string, printable
  523. * 2) Next character also a null, not printable.
  524. * 3) Next character not a null, continue to check.
  525. */
  526. if (s[0] == '\0') {
  527. if (len == 1)
  528. return 1;
  529. if (s[1] == '\0')
  530. return 0;
  531. }
  532. s++;
  533. len--;
  534. }
  535. /* Not the null termination, or not done yet: not printable */
  536. if (*s != '\0' || (len != 0))
  537. return 0;
  538. return 1;
  539. }
  540. /*
  541. * Print the property in the best format, a heuristic guess. Print as
  542. * a string, concatenated strings, a byte, word, double word, or (if all
  543. * else fails) it is printed as a stream of bytes.
  544. */
  545. static void print_data(const void *data, int len)
  546. {
  547. int j;
  548. /* no data, don't print */
  549. if (len == 0)
  550. return;
  551. /*
  552. * It is a string, but it may have multiple strings (embedded '\0's).
  553. */
  554. if (is_printable_string(data, len)) {
  555. puts("\"");
  556. j = 0;
  557. while (j < len) {
  558. if (j > 0)
  559. puts("\", \"");
  560. puts(data);
  561. j += strlen(data) + 1;
  562. data += strlen(data) + 1;
  563. }
  564. puts("\"");
  565. return;
  566. }
  567. if ((len %4) == 0) {
  568. const u32 *p;
  569. printf("<");
  570. for (j = 0, p = data; j < len/4; j ++)
  571. printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
  572. printf(">");
  573. } else { /* anything else... hexdump */
  574. const u8 *s;
  575. printf("[");
  576. for (j = 0, s = data; j < len; j++)
  577. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  578. printf("]");
  579. }
  580. }
  581. /****************************************************************************/
  582. /*
  583. * Recursively print (a portion of) the fdt. The depth parameter
  584. * determines how deeply nested the fdt is printed.
  585. */
  586. static int fdt_print(const char *pathp, char *prop, int depth)
  587. {
  588. static char tabs[MAX_LEVEL+1] =
  589. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  590. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  591. const void *nodep; /* property node pointer */
  592. int nodeoffset; /* node offset from libfdt */
  593. int nextoffset; /* next node offset from libfdt */
  594. uint32_t tag; /* tag */
  595. int len; /* length of the property */
  596. int level = 0; /* keep track of nesting level */
  597. const struct fdt_property *fdt_prop;
  598. nodeoffset = fdt_path_offset (fdt, pathp);
  599. if (nodeoffset < 0) {
  600. /*
  601. * Not found or something else bad happened.
  602. */
  603. printf ("libfdt fdt_path_offset() returned %s\n",
  604. fdt_strerror(nodeoffset));
  605. return 1;
  606. }
  607. /*
  608. * The user passed in a property as well as node path.
  609. * Print only the given property and then return.
  610. */
  611. if (prop) {
  612. nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
  613. if (len == 0) {
  614. /* no property value */
  615. printf("%s %s\n", pathp, prop);
  616. return 0;
  617. } else if (len > 0) {
  618. printf("%s = ", prop);
  619. print_data (nodep, len);
  620. printf("\n");
  621. return 0;
  622. } else {
  623. printf ("libfdt fdt_getprop(): %s\n",
  624. fdt_strerror(len));
  625. return 1;
  626. }
  627. }
  628. /*
  629. * The user passed in a node path and no property,
  630. * print the node and all subnodes.
  631. */
  632. while(level >= 0) {
  633. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
  634. switch(tag) {
  635. case FDT_BEGIN_NODE:
  636. pathp = fdt_get_name(fdt, nodeoffset, NULL);
  637. if (level <= depth) {
  638. if (pathp == NULL)
  639. pathp = "/* NULL pointer error */";
  640. if (*pathp == '\0')
  641. pathp = "/"; /* root is nameless */
  642. printf("%s%s {\n",
  643. &tabs[MAX_LEVEL - level], pathp);
  644. }
  645. level++;
  646. if (level >= MAX_LEVEL) {
  647. printf("Nested too deep, aborting.\n");
  648. return 1;
  649. }
  650. break;
  651. case FDT_END_NODE:
  652. level--;
  653. if (level <= depth)
  654. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  655. if (level == 0) {
  656. level = -1; /* exit the loop */
  657. }
  658. break;
  659. case FDT_PROP:
  660. fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
  661. sizeof(*fdt_prop));
  662. pathp = fdt_string(fdt,
  663. fdt32_to_cpu(fdt_prop->nameoff));
  664. len = fdt32_to_cpu(fdt_prop->len);
  665. nodep = fdt_prop->data;
  666. if (len < 0) {
  667. printf ("libfdt fdt_getprop(): %s\n",
  668. fdt_strerror(len));
  669. return 1;
  670. } else if (len == 0) {
  671. /* the property has no value */
  672. if (level <= depth)
  673. printf("%s%s;\n",
  674. &tabs[MAX_LEVEL - level],
  675. pathp);
  676. } else {
  677. if (level <= depth) {
  678. printf("%s%s = ",
  679. &tabs[MAX_LEVEL - level],
  680. pathp);
  681. print_data (nodep, len);
  682. printf(";\n");
  683. }
  684. }
  685. break;
  686. case FDT_NOP:
  687. printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
  688. break;
  689. case FDT_END:
  690. return 1;
  691. default:
  692. if (level <= depth)
  693. printf("Unknown tag 0x%08X\n", tag);
  694. return 1;
  695. }
  696. nodeoffset = nextoffset;
  697. }
  698. return 0;
  699. }
  700. /********************************************************************/
  701. U_BOOT_CMD(
  702. fdt, 255, 0, do_fdt,
  703. "fdt - flattened device tree utility commands\n",
  704. "addr <addr> [<length>] - Set the fdt location to <addr>\n"
  705. #ifdef CONFIG_OF_BOARD_SETUP
  706. "fdt boardsetup - Do board-specific set up\n"
  707. #endif
  708. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  709. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  710. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  711. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  712. "fdt mknode <path> <node> - Create a new node after <path>\n"
  713. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  714. "fdt header - Display header info\n"
  715. "fdt bootcpu <id> - Set boot cpuid\n"
  716. "fdt memory <addr> <size> - Add/Update memory node\n"
  717. "fdt rsvmem print - Show current mem reserves\n"
  718. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  719. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  720. "fdt chosen - Add/update the /chosen branch in the tree\n"
  721. "NOTE: If the path or property you are setting/printing has a '#' character\n"
  722. " or spaces, you MUST escape it with a \\ character or quote it with \".\n"
  723. );