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. env_set_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_env_set(const void *nodep, int len, const char *var)
  45. {
  46. if (is_printable_string(nodep, len))
  47. env_set(var, (void *)nodep);
  48. else if (len == 4) {
  49. char buf[11];
  50. sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
  51. env_set(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. env_set(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. env_get_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] __aligned(4);/* property storage */
  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 (len > SCRATCHPAD) {
  257. printf("prop (%d) doesn't fit in scratchpad!\n",
  258. len);
  259. return 1;
  260. }
  261. if (ptmp != NULL)
  262. memcpy(data, ptmp, len);
  263. ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
  264. if (ret != 0)
  265. return ret;
  266. }
  267. ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
  268. if (ret < 0) {
  269. printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
  270. return 1;
  271. }
  272. /********************************************************************
  273. * Get the value of a property in the working_fdt.
  274. ********************************************************************/
  275. } else if (argv[1][0] == 'g') {
  276. char *subcmd; /* sub-command */
  277. char *pathp; /* path */
  278. char *prop; /* property */
  279. char *var; /* variable to store result */
  280. int nodeoffset; /* node offset from libfdt */
  281. const void *nodep; /* property node pointer */
  282. int len = 0; /* new length of the property */
  283. /*
  284. * Parameters: Node path, property, optional value.
  285. */
  286. if (argc < 5)
  287. return CMD_RET_USAGE;
  288. subcmd = argv[2];
  289. if (argc < 6 && subcmd[0] != 's')
  290. return CMD_RET_USAGE;
  291. var = argv[3];
  292. pathp = argv[4];
  293. prop = argv[5];
  294. nodeoffset = fdt_path_offset(working_fdt, pathp);
  295. if (nodeoffset < 0) {
  296. /*
  297. * Not found or something else bad happened.
  298. */
  299. printf("libfdt fdt_path_offset() returned %s\n",
  300. fdt_strerror(nodeoffset));
  301. return 1;
  302. }
  303. if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
  304. int reqIndex = -1;
  305. int startDepth = fdt_node_depth(
  306. working_fdt, nodeoffset);
  307. int curDepth = startDepth;
  308. int curIndex = -1;
  309. int nextNodeOffset = fdt_next_node(
  310. working_fdt, nodeoffset, &curDepth);
  311. if (subcmd[0] == 'n')
  312. reqIndex = simple_strtoul(argv[5], NULL, 16);
  313. while (curDepth > startDepth) {
  314. if (curDepth == startDepth + 1)
  315. curIndex++;
  316. if (subcmd[0] == 'n' && curIndex == reqIndex) {
  317. const char *node_name;
  318. node_name = fdt_get_name(working_fdt,
  319. nextNodeOffset,
  320. NULL);
  321. env_set(var, node_name);
  322. return 0;
  323. }
  324. nextNodeOffset = fdt_next_node(
  325. working_fdt, nextNodeOffset, &curDepth);
  326. if (nextNodeOffset < 0)
  327. break;
  328. }
  329. if (subcmd[0] == 's') {
  330. /* get the num nodes at this level */
  331. env_set_ulong(var, curIndex + 1);
  332. } else {
  333. /* node index not found */
  334. printf("libfdt node not found\n");
  335. return 1;
  336. }
  337. } else {
  338. nodep = fdt_getprop(
  339. working_fdt, nodeoffset, prop, &len);
  340. if (len == 0) {
  341. /* no property value */
  342. env_set(var, "");
  343. return 0;
  344. } else if (nodep && len > 0) {
  345. if (subcmd[0] == 'v') {
  346. int ret;
  347. ret = fdt_value_env_set(nodep, len,
  348. 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. env_set(var, buf);
  356. } else if (subcmd[0] == 's') {
  357. /* Get size */
  358. char buf[11];
  359. sprintf(buf, "0x%08X", len);
  360. env_set(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. /* apply method prints messages on error */
  599. ret = fdt_overlay_apply_verbose(working_fdt, blob);
  600. if (ret)
  601. return CMD_RET_FAILURE;
  602. }
  603. #endif
  604. /* resize the fdt */
  605. else if (strncmp(argv[1], "re", 2) == 0) {
  606. uint extrasize;
  607. if (argc > 2)
  608. extrasize = simple_strtoul(argv[2], NULL, 16);
  609. else
  610. extrasize = 0;
  611. fdt_shrink_to_minimum(working_fdt, extrasize);
  612. }
  613. else {
  614. /* Unrecognized command */
  615. return CMD_RET_USAGE;
  616. }
  617. return 0;
  618. }
  619. /****************************************************************************/
  620. /**
  621. * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
  622. *
  623. * @blobp: Pointer to FDT pointer
  624. * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
  625. */
  626. static int fdt_valid(struct fdt_header **blobp)
  627. {
  628. const void *blob = *blobp;
  629. int err;
  630. if (blob == NULL) {
  631. printf ("The address of the fdt is invalid (NULL).\n");
  632. return 0;
  633. }
  634. err = fdt_check_header(blob);
  635. if (err == 0)
  636. return 1; /* valid */
  637. if (err < 0) {
  638. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  639. /*
  640. * Be more informative on bad version.
  641. */
  642. if (err == -FDT_ERR_BADVERSION) {
  643. if (fdt_version(blob) <
  644. FDT_FIRST_SUPPORTED_VERSION) {
  645. printf (" - too old, fdt %d < %d",
  646. fdt_version(blob),
  647. FDT_FIRST_SUPPORTED_VERSION);
  648. }
  649. if (fdt_last_comp_version(blob) >
  650. FDT_LAST_SUPPORTED_VERSION) {
  651. printf (" - too new, fdt %d > %d",
  652. fdt_version(blob),
  653. FDT_LAST_SUPPORTED_VERSION);
  654. }
  655. }
  656. printf("\n");
  657. *blobp = NULL;
  658. return 0;
  659. }
  660. return 1;
  661. }
  662. /****************************************************************************/
  663. /*
  664. * Parse the user's input, partially heuristic. Valid formats:
  665. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  666. * C conventions.
  667. * [00 11 22 .. nn] - byte stream
  668. * "string" - If the the value doesn't start with "<" or "[", it is
  669. * treated as a string. Note that the quotes are
  670. * stripped by the parser before we get the string.
  671. * newval: An array of strings containing the new property as specified
  672. * on the command line
  673. * count: The number of strings in the array
  674. * data: A bytestream to be placed in the property
  675. * len: The length of the resulting bytestream
  676. */
  677. static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
  678. {
  679. char *cp; /* temporary char pointer */
  680. char *newp; /* temporary newval char pointer */
  681. unsigned long tmp; /* holds converted values */
  682. int stridx = 0;
  683. *len = 0;
  684. newp = newval[0];
  685. /* An array of cells */
  686. if (*newp == '<') {
  687. newp++;
  688. while ((*newp != '>') && (stridx < count)) {
  689. /*
  690. * Keep searching until we find that last ">"
  691. * That way users don't have to escape the spaces
  692. */
  693. if (*newp == '\0') {
  694. newp = newval[++stridx];
  695. continue;
  696. }
  697. cp = newp;
  698. tmp = simple_strtoul(cp, &newp, 0);
  699. if (*cp != '?')
  700. *(fdt32_t *)data = cpu_to_fdt32(tmp);
  701. else
  702. newp++;
  703. data += 4;
  704. *len += 4;
  705. /* If the ptr didn't advance, something went wrong */
  706. if ((newp - cp) <= 0) {
  707. printf("Sorry, I could not convert \"%s\"\n",
  708. cp);
  709. return 1;
  710. }
  711. while (*newp == ' ')
  712. newp++;
  713. }
  714. if (*newp != '>') {
  715. printf("Unexpected character '%c'\n", *newp);
  716. return 1;
  717. }
  718. } else if (*newp == '[') {
  719. /*
  720. * Byte stream. Convert the values.
  721. */
  722. newp++;
  723. while ((stridx < count) && (*newp != ']')) {
  724. while (*newp == ' ')
  725. newp++;
  726. if (*newp == '\0') {
  727. newp = newval[++stridx];
  728. continue;
  729. }
  730. if (!isxdigit(*newp))
  731. break;
  732. tmp = simple_strtoul(newp, &newp, 16);
  733. *data++ = tmp & 0xFF;
  734. *len = *len + 1;
  735. }
  736. if (*newp != ']') {
  737. printf("Unexpected character '%c'\n", *newp);
  738. return 1;
  739. }
  740. } else {
  741. /*
  742. * Assume it is one or more strings. Copy it into our
  743. * data area for convenience (including the
  744. * terminating '\0's).
  745. */
  746. while (stridx < count) {
  747. size_t length = strlen(newp) + 1;
  748. strcpy(data, newp);
  749. data += length;
  750. *len += length;
  751. newp = newval[++stridx];
  752. }
  753. }
  754. return 0;
  755. }
  756. /****************************************************************************/
  757. /*
  758. * Heuristic to guess if this is a string or concatenated strings.
  759. */
  760. static int is_printable_string(const void *data, int len)
  761. {
  762. const char *s = data;
  763. /* zero length is not */
  764. if (len == 0)
  765. return 0;
  766. /* must terminate with zero or '\n' */
  767. if (s[len - 1] != '\0' && s[len - 1] != '\n')
  768. return 0;
  769. /* printable or a null byte (concatenated strings) */
  770. while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
  771. /*
  772. * If we see a null, there are three possibilities:
  773. * 1) If len == 1, it is the end of the string, printable
  774. * 2) Next character also a null, not printable.
  775. * 3) Next character not a null, continue to check.
  776. */
  777. if (s[0] == '\0') {
  778. if (len == 1)
  779. return 1;
  780. if (s[1] == '\0')
  781. return 0;
  782. }
  783. s++;
  784. len--;
  785. }
  786. /* Not the null termination, or not done yet: not printable */
  787. if (*s != '\0' || (len != 0))
  788. return 0;
  789. return 1;
  790. }
  791. /*
  792. * Print the property in the best format, a heuristic guess. Print as
  793. * a string, concatenated strings, a byte, word, double word, or (if all
  794. * else fails) it is printed as a stream of bytes.
  795. */
  796. static void print_data(const void *data, int len)
  797. {
  798. int j;
  799. /* no data, don't print */
  800. if (len == 0)
  801. return;
  802. /*
  803. * It is a string, but it may have multiple strings (embedded '\0's).
  804. */
  805. if (is_printable_string(data, len)) {
  806. puts("\"");
  807. j = 0;
  808. while (j < len) {
  809. if (j > 0)
  810. puts("\", \"");
  811. puts(data);
  812. j += strlen(data) + 1;
  813. data += strlen(data) + 1;
  814. }
  815. puts("\"");
  816. return;
  817. }
  818. if ((len %4) == 0) {
  819. if (len > CMD_FDT_MAX_DUMP)
  820. printf("* 0x%p [0x%08x]", data, len);
  821. else {
  822. const __be32 *p;
  823. printf("<");
  824. for (j = 0, p = data; j < len/4; j++)
  825. printf("0x%08x%s", fdt32_to_cpu(p[j]),
  826. j < (len/4 - 1) ? " " : "");
  827. printf(">");
  828. }
  829. } else { /* anything else... hexdump */
  830. if (len > CMD_FDT_MAX_DUMP)
  831. printf("* 0x%p [0x%08x]", data, len);
  832. else {
  833. const u8 *s;
  834. printf("[");
  835. for (j = 0, s = data; j < len; j++)
  836. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  837. printf("]");
  838. }
  839. }
  840. }
  841. /****************************************************************************/
  842. /*
  843. * Recursively print (a portion of) the working_fdt. The depth parameter
  844. * determines how deeply nested the fdt is printed.
  845. */
  846. static int fdt_print(const char *pathp, char *prop, int depth)
  847. {
  848. static char tabs[MAX_LEVEL+1] =
  849. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  850. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  851. const void *nodep; /* property node pointer */
  852. int nodeoffset; /* node offset from libfdt */
  853. int nextoffset; /* next node offset from libfdt */
  854. uint32_t tag; /* tag */
  855. int len; /* length of the property */
  856. int level = 0; /* keep track of nesting level */
  857. const struct fdt_property *fdt_prop;
  858. nodeoffset = fdt_path_offset (working_fdt, pathp);
  859. if (nodeoffset < 0) {
  860. /*
  861. * Not found or something else bad happened.
  862. */
  863. printf ("libfdt fdt_path_offset() returned %s\n",
  864. fdt_strerror(nodeoffset));
  865. return 1;
  866. }
  867. /*
  868. * The user passed in a property as well as node path.
  869. * Print only the given property and then return.
  870. */
  871. if (prop) {
  872. nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
  873. if (len == 0) {
  874. /* no property value */
  875. printf("%s %s\n", pathp, prop);
  876. return 0;
  877. } else if (nodep && len > 0) {
  878. printf("%s = ", prop);
  879. print_data (nodep, len);
  880. printf("\n");
  881. return 0;
  882. } else {
  883. printf ("libfdt fdt_getprop(): %s\n",
  884. fdt_strerror(len));
  885. return 1;
  886. }
  887. }
  888. /*
  889. * The user passed in a node path and no property,
  890. * print the node and all subnodes.
  891. */
  892. while(level >= 0) {
  893. tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
  894. switch(tag) {
  895. case FDT_BEGIN_NODE:
  896. pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
  897. if (level <= depth) {
  898. if (pathp == NULL)
  899. pathp = "/* NULL pointer error */";
  900. if (*pathp == '\0')
  901. pathp = "/"; /* root is nameless */
  902. printf("%s%s {\n",
  903. &tabs[MAX_LEVEL - level], pathp);
  904. }
  905. level++;
  906. if (level >= MAX_LEVEL) {
  907. printf("Nested too deep, aborting.\n");
  908. return 1;
  909. }
  910. break;
  911. case FDT_END_NODE:
  912. level--;
  913. if (level <= depth)
  914. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  915. if (level == 0) {
  916. level = -1; /* exit the loop */
  917. }
  918. break;
  919. case FDT_PROP:
  920. fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
  921. sizeof(*fdt_prop));
  922. pathp = fdt_string(working_fdt,
  923. fdt32_to_cpu(fdt_prop->nameoff));
  924. len = fdt32_to_cpu(fdt_prop->len);
  925. nodep = fdt_prop->data;
  926. if (len < 0) {
  927. printf ("libfdt fdt_getprop(): %s\n",
  928. fdt_strerror(len));
  929. return 1;
  930. } else if (len == 0) {
  931. /* the property has no value */
  932. if (level <= depth)
  933. printf("%s%s;\n",
  934. &tabs[MAX_LEVEL - level],
  935. pathp);
  936. } else {
  937. if (level <= depth) {
  938. printf("%s%s = ",
  939. &tabs[MAX_LEVEL - level],
  940. pathp);
  941. print_data (nodep, len);
  942. printf(";\n");
  943. }
  944. }
  945. break;
  946. case FDT_NOP:
  947. printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
  948. break;
  949. case FDT_END:
  950. return 1;
  951. default:
  952. if (level <= depth)
  953. printf("Unknown tag 0x%08X\n", tag);
  954. return 1;
  955. }
  956. nodeoffset = nextoffset;
  957. }
  958. return 0;
  959. }
  960. /********************************************************************/
  961. #ifdef CONFIG_SYS_LONGHELP
  962. static char fdt_help_text[] =
  963. "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
  964. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  965. "fdt apply <addr> - Apply overlay to the DT\n"
  966. #endif
  967. #ifdef CONFIG_OF_BOARD_SETUP
  968. "fdt boardsetup - Do board-specific set up\n"
  969. #endif
  970. #ifdef CONFIG_OF_SYSTEM_SETUP
  971. "fdt systemsetup - Do system-specific set up\n"
  972. #endif
  973. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  974. "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
  975. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  976. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  977. "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
  978. "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
  979. "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
  980. "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
  981. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  982. "fdt mknode <path> <node> - Create a new node after <path>\n"
  983. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  984. "fdt header - Display header info\n"
  985. "fdt bootcpu <id> - Set boot cpuid\n"
  986. "fdt memory <addr> <size> - Add/Update memory node\n"
  987. "fdt rsvmem print - Show current mem reserves\n"
  988. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  989. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  990. "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
  991. " <start>/<end> - initrd start/end addr\n"
  992. #if defined(CONFIG_FIT_SIGNATURE)
  993. "fdt checksign [<addr>] - check FIT signature\n"
  994. " <start> - addr of key blob\n"
  995. " default gd->fdt_blob\n"
  996. #endif
  997. "NOTE: Dereference aliases by omitting the leading '/', "
  998. "e.g. fdt print ethernet0.";
  999. #endif
  1000. U_BOOT_CMD(
  1001. fdt, 255, 0, do_fdt,
  1002. "flattened device tree utility commands", fdt_help_text
  1003. );