cmd_fdt.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 (strncmp(argv[1], "cho", 3) == 0) {
  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);
  526. fdt_initrd(working_fdt, initrd_start, initrd_end);
  527. #if defined(CONFIG_FIT_SIGNATURE)
  528. } else if (strncmp(argv[1], "che", 3) == 0) {
  529. int cfg_noffset;
  530. int ret;
  531. unsigned long addr;
  532. struct fdt_header *blob;
  533. if (!working_fdt)
  534. return CMD_RET_FAILURE;
  535. if (argc > 2) {
  536. addr = simple_strtoul(argv[2], NULL, 16);
  537. blob = map_sysmem(addr, 0);
  538. } else {
  539. blob = (struct fdt_header *)gd->fdt_blob;
  540. }
  541. if (!fdt_valid(&blob))
  542. return 1;
  543. gd->fdt_blob = blob;
  544. cfg_noffset = fit_conf_get_node(working_fdt, NULL);
  545. if (!cfg_noffset) {
  546. printf("Could not find configuration node: %s\n",
  547. fdt_strerror(cfg_noffset));
  548. return CMD_RET_FAILURE;
  549. }
  550. ret = fit_config_verify(working_fdt, cfg_noffset);
  551. if (ret == 0)
  552. return CMD_RET_SUCCESS;
  553. else
  554. return CMD_RET_FAILURE;
  555. #endif
  556. }
  557. /* resize the fdt */
  558. else if (strncmp(argv[1], "re", 2) == 0) {
  559. fdt_shrink_to_minimum(working_fdt);
  560. }
  561. else {
  562. /* Unrecognized command */
  563. return CMD_RET_USAGE;
  564. }
  565. return 0;
  566. }
  567. /****************************************************************************/
  568. /**
  569. * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
  570. *
  571. * @blobp: Pointer to FDT pointer
  572. * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
  573. */
  574. static int fdt_valid(struct fdt_header **blobp)
  575. {
  576. const void *blob = *blobp;
  577. int err;
  578. if (blob == NULL) {
  579. printf ("The address of the fdt is invalid (NULL).\n");
  580. return 0;
  581. }
  582. err = fdt_check_header(blob);
  583. if (err == 0)
  584. return 1; /* valid */
  585. if (err < 0) {
  586. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  587. /*
  588. * Be more informative on bad version.
  589. */
  590. if (err == -FDT_ERR_BADVERSION) {
  591. if (fdt_version(blob) <
  592. FDT_FIRST_SUPPORTED_VERSION) {
  593. printf (" - too old, fdt %d < %d",
  594. fdt_version(blob),
  595. FDT_FIRST_SUPPORTED_VERSION);
  596. }
  597. if (fdt_last_comp_version(blob) >
  598. FDT_LAST_SUPPORTED_VERSION) {
  599. printf (" - too new, fdt %d > %d",
  600. fdt_version(blob),
  601. FDT_LAST_SUPPORTED_VERSION);
  602. }
  603. }
  604. printf("\n");
  605. *blobp = NULL;
  606. return 0;
  607. }
  608. return 1;
  609. }
  610. /****************************************************************************/
  611. /*
  612. * Parse the user's input, partially heuristic. Valid formats:
  613. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  614. * C conventions.
  615. * [00 11 22 .. nn] - byte stream
  616. * "string" - If the the value doesn't start with "<" or "[", it is
  617. * treated as a string. Note that the quotes are
  618. * stripped by the parser before we get the string.
  619. * newval: An array of strings containing the new property as specified
  620. * on the command line
  621. * count: The number of strings in the array
  622. * data: A bytestream to be placed in the property
  623. * len: The length of the resulting bytestream
  624. */
  625. static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
  626. {
  627. char *cp; /* temporary char pointer */
  628. char *newp; /* temporary newval char pointer */
  629. unsigned long tmp; /* holds converted values */
  630. int stridx = 0;
  631. *len = 0;
  632. newp = newval[0];
  633. /* An array of cells */
  634. if (*newp == '<') {
  635. newp++;
  636. while ((*newp != '>') && (stridx < count)) {
  637. /*
  638. * Keep searching until we find that last ">"
  639. * That way users don't have to escape the spaces
  640. */
  641. if (*newp == '\0') {
  642. newp = newval[++stridx];
  643. continue;
  644. }
  645. cp = newp;
  646. tmp = simple_strtoul(cp, &newp, 0);
  647. *(__be32 *)data = __cpu_to_be32(tmp);
  648. data += 4;
  649. *len += 4;
  650. /* If the ptr didn't advance, something went wrong */
  651. if ((newp - cp) <= 0) {
  652. printf("Sorry, I could not convert \"%s\"\n",
  653. cp);
  654. return 1;
  655. }
  656. while (*newp == ' ')
  657. newp++;
  658. }
  659. if (*newp != '>') {
  660. printf("Unexpected character '%c'\n", *newp);
  661. return 1;
  662. }
  663. } else if (*newp == '[') {
  664. /*
  665. * Byte stream. Convert the values.
  666. */
  667. newp++;
  668. while ((stridx < count) && (*newp != ']')) {
  669. while (*newp == ' ')
  670. newp++;
  671. if (*newp == '\0') {
  672. newp = newval[++stridx];
  673. continue;
  674. }
  675. if (!isxdigit(*newp))
  676. break;
  677. tmp = simple_strtoul(newp, &newp, 16);
  678. *data++ = tmp & 0xFF;
  679. *len = *len + 1;
  680. }
  681. if (*newp != ']') {
  682. printf("Unexpected character '%c'\n", *newp);
  683. return 1;
  684. }
  685. } else {
  686. /*
  687. * Assume it is one or more strings. Copy it into our
  688. * data area for convenience (including the
  689. * terminating '\0's).
  690. */
  691. while (stridx < count) {
  692. size_t length = strlen(newp) + 1;
  693. strcpy(data, newp);
  694. data += length;
  695. *len += length;
  696. newp = newval[++stridx];
  697. }
  698. }
  699. return 0;
  700. }
  701. /****************************************************************************/
  702. /*
  703. * Heuristic to guess if this is a string or concatenated strings.
  704. */
  705. static int is_printable_string(const void *data, int len)
  706. {
  707. const char *s = data;
  708. /* zero length is not */
  709. if (len == 0)
  710. return 0;
  711. /* must terminate with zero or '\n' */
  712. if (s[len - 1] != '\0' && s[len - 1] != '\n')
  713. return 0;
  714. /* printable or a null byte (concatenated strings) */
  715. while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
  716. /*
  717. * If we see a null, there are three possibilities:
  718. * 1) If len == 1, it is the end of the string, printable
  719. * 2) Next character also a null, not printable.
  720. * 3) Next character not a null, continue to check.
  721. */
  722. if (s[0] == '\0') {
  723. if (len == 1)
  724. return 1;
  725. if (s[1] == '\0')
  726. return 0;
  727. }
  728. s++;
  729. len--;
  730. }
  731. /* Not the null termination, or not done yet: not printable */
  732. if (*s != '\0' || (len != 0))
  733. return 0;
  734. return 1;
  735. }
  736. /*
  737. * Print the property in the best format, a heuristic guess. Print as
  738. * a string, concatenated strings, a byte, word, double word, or (if all
  739. * else fails) it is printed as a stream of bytes.
  740. */
  741. static void print_data(const void *data, int len)
  742. {
  743. int j;
  744. /* no data, don't print */
  745. if (len == 0)
  746. return;
  747. /*
  748. * It is a string, but it may have multiple strings (embedded '\0's).
  749. */
  750. if (is_printable_string(data, len)) {
  751. puts("\"");
  752. j = 0;
  753. while (j < len) {
  754. if (j > 0)
  755. puts("\", \"");
  756. puts(data);
  757. j += strlen(data) + 1;
  758. data += strlen(data) + 1;
  759. }
  760. puts("\"");
  761. return;
  762. }
  763. if ((len %4) == 0) {
  764. if (len > CONFIG_CMD_FDT_MAX_DUMP)
  765. printf("* 0x%p [0x%08x]", data, len);
  766. else {
  767. const __be32 *p;
  768. printf("<");
  769. for (j = 0, p = data; j < len/4; j++)
  770. printf("0x%08x%s", fdt32_to_cpu(p[j]),
  771. j < (len/4 - 1) ? " " : "");
  772. printf(">");
  773. }
  774. } else { /* anything else... hexdump */
  775. if (len > CONFIG_CMD_FDT_MAX_DUMP)
  776. printf("* 0x%p [0x%08x]", data, len);
  777. else {
  778. const u8 *s;
  779. printf("[");
  780. for (j = 0, s = data; j < len; j++)
  781. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  782. printf("]");
  783. }
  784. }
  785. }
  786. /****************************************************************************/
  787. /*
  788. * Recursively print (a portion of) the working_fdt. The depth parameter
  789. * determines how deeply nested the fdt is printed.
  790. */
  791. static int fdt_print(const char *pathp, char *prop, int depth)
  792. {
  793. static char tabs[MAX_LEVEL+1] =
  794. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  795. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  796. const void *nodep; /* property node pointer */
  797. int nodeoffset; /* node offset from libfdt */
  798. int nextoffset; /* next node offset from libfdt */
  799. uint32_t tag; /* tag */
  800. int len; /* length of the property */
  801. int level = 0; /* keep track of nesting level */
  802. const struct fdt_property *fdt_prop;
  803. nodeoffset = fdt_path_offset (working_fdt, pathp);
  804. if (nodeoffset < 0) {
  805. /*
  806. * Not found or something else bad happened.
  807. */
  808. printf ("libfdt fdt_path_offset() returned %s\n",
  809. fdt_strerror(nodeoffset));
  810. return 1;
  811. }
  812. /*
  813. * The user passed in a property as well as node path.
  814. * Print only the given property and then return.
  815. */
  816. if (prop) {
  817. nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
  818. if (len == 0) {
  819. /* no property value */
  820. printf("%s %s\n", pathp, prop);
  821. return 0;
  822. } else if (len > 0) {
  823. printf("%s = ", prop);
  824. print_data (nodep, len);
  825. printf("\n");
  826. return 0;
  827. } else {
  828. printf ("libfdt fdt_getprop(): %s\n",
  829. fdt_strerror(len));
  830. return 1;
  831. }
  832. }
  833. /*
  834. * The user passed in a node path and no property,
  835. * print the node and all subnodes.
  836. */
  837. while(level >= 0) {
  838. tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
  839. switch(tag) {
  840. case FDT_BEGIN_NODE:
  841. pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
  842. if (level <= depth) {
  843. if (pathp == NULL)
  844. pathp = "/* NULL pointer error */";
  845. if (*pathp == '\0')
  846. pathp = "/"; /* root is nameless */
  847. printf("%s%s {\n",
  848. &tabs[MAX_LEVEL - level], pathp);
  849. }
  850. level++;
  851. if (level >= MAX_LEVEL) {
  852. printf("Nested too deep, aborting.\n");
  853. return 1;
  854. }
  855. break;
  856. case FDT_END_NODE:
  857. level--;
  858. if (level <= depth)
  859. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  860. if (level == 0) {
  861. level = -1; /* exit the loop */
  862. }
  863. break;
  864. case FDT_PROP:
  865. fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
  866. sizeof(*fdt_prop));
  867. pathp = fdt_string(working_fdt,
  868. fdt32_to_cpu(fdt_prop->nameoff));
  869. len = fdt32_to_cpu(fdt_prop->len);
  870. nodep = fdt_prop->data;
  871. if (len < 0) {
  872. printf ("libfdt fdt_getprop(): %s\n",
  873. fdt_strerror(len));
  874. return 1;
  875. } else if (len == 0) {
  876. /* the property has no value */
  877. if (level <= depth)
  878. printf("%s%s;\n",
  879. &tabs[MAX_LEVEL - level],
  880. pathp);
  881. } else {
  882. if (level <= depth) {
  883. printf("%s%s = ",
  884. &tabs[MAX_LEVEL - level],
  885. pathp);
  886. print_data (nodep, len);
  887. printf(";\n");
  888. }
  889. }
  890. break;
  891. case FDT_NOP:
  892. printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
  893. break;
  894. case FDT_END:
  895. return 1;
  896. default:
  897. if (level <= depth)
  898. printf("Unknown tag 0x%08X\n", tag);
  899. return 1;
  900. }
  901. nodeoffset = nextoffset;
  902. }
  903. return 0;
  904. }
  905. /********************************************************************/
  906. #ifdef CONFIG_SYS_LONGHELP
  907. static char fdt_help_text[] =
  908. "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
  909. #ifdef CONFIG_OF_BOARD_SETUP
  910. "fdt boardsetup - Do board-specific set up\n"
  911. #endif
  912. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  913. "fdt resize - Resize fdt to size + padding to 4k addr\n"
  914. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  915. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  916. "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
  917. "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
  918. "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
  919. "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
  920. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  921. "fdt mknode <path> <node> - Create a new node after <path>\n"
  922. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  923. "fdt header - Display header info\n"
  924. "fdt bootcpu <id> - Set boot cpuid\n"
  925. "fdt memory <addr> <size> - Add/Update memory node\n"
  926. "fdt rsvmem print - Show current mem reserves\n"
  927. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  928. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  929. "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
  930. " <start>/<end> - initrd start/end addr\n"
  931. #if defined(CONFIG_FIT_SIGNATURE)
  932. "fdt checksign [<addr>] - check FIT signature\n"
  933. " <start> - addr of key blob\n"
  934. " default gd->fdt_blob\n"
  935. #endif
  936. "NOTE: Dereference aliases by omiting the leading '/', "
  937. "e.g. fdt print ethernet0.";
  938. #endif
  939. U_BOOT_CMD(
  940. fdt, 255, 0, do_fdt,
  941. "flattened device tree utility commands", fdt_help_text
  942. );