cmd_fdt.c 25 KB

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