cmd_fdt.c 20 KB

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