cmd_fdt.c 21 KB

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