fdt.c 27 KB

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