cmd_fdt.c 22 KB

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