fdt.c 27 KB

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