cmd_fdt.c 25 KB

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