cmd_fdt.c 25 KB

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