cmd_fdt.c 21 KB

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