cmd_fdt.c 24 KB

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