fdt.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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]; /* 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 *node_name;
  321. node_name = fdt_get_name(working_fdt,
  322. nextNodeOffset,
  323. NULL);
  324. env_set(var, node_name);
  325. return 0;
  326. }
  327. nextNodeOffset = fdt_next_node(
  328. working_fdt, nextNodeOffset, &curDepth);
  329. if (nextNodeOffset < 0)
  330. break;
  331. }
  332. if (subcmd[0] == 's') {
  333. /* get the num nodes at this level */
  334. env_set_ulong(var, curIndex + 1);
  335. } else {
  336. /* node index not found */
  337. printf("libfdt node not found\n");
  338. return 1;
  339. }
  340. } else {
  341. nodep = fdt_getprop(
  342. working_fdt, nodeoffset, prop, &len);
  343. if (len == 0) {
  344. /* no property value */
  345. env_set(var, "");
  346. return 0;
  347. } else if (nodep && len > 0) {
  348. if (subcmd[0] == 'v') {
  349. int ret;
  350. ret = fdt_value_env_set(nodep, len,
  351. var);
  352. if (ret != 0)
  353. return ret;
  354. } else if (subcmd[0] == 'a') {
  355. /* Get address */
  356. char buf[11];
  357. sprintf(buf, "0x%p", nodep);
  358. env_set(var, buf);
  359. } else if (subcmd[0] == 's') {
  360. /* Get size */
  361. char buf[11];
  362. sprintf(buf, "0x%08X", len);
  363. env_set(var, buf);
  364. } else
  365. return CMD_RET_USAGE;
  366. return 0;
  367. } else {
  368. printf("libfdt fdt_getprop(): %s\n",
  369. fdt_strerror(len));
  370. return 1;
  371. }
  372. }
  373. /*
  374. * Print (recursive) / List (single level)
  375. */
  376. } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
  377. int depth = MAX_LEVEL; /* how deep to print */
  378. char *pathp; /* path */
  379. char *prop; /* property */
  380. int ret; /* return value */
  381. static char root[2] = "/";
  382. /*
  383. * list is an alias for print, but limited to 1 level
  384. */
  385. if (argv[1][0] == 'l') {
  386. depth = 1;
  387. }
  388. /*
  389. * Get the starting path. The root node is an oddball,
  390. * the offset is zero and has no name.
  391. */
  392. if (argc == 2)
  393. pathp = root;
  394. else
  395. pathp = argv[2];
  396. if (argc > 3)
  397. prop = argv[3];
  398. else
  399. prop = NULL;
  400. ret = fdt_print(pathp, prop, depth);
  401. if (ret != 0)
  402. return ret;
  403. /*
  404. * Remove a property/node
  405. */
  406. } else if (strncmp(argv[1], "rm", 2) == 0) {
  407. int nodeoffset; /* node offset from libfdt */
  408. int err;
  409. /*
  410. * Get the path. The root node is an oddball, the offset
  411. * is zero and has no name.
  412. */
  413. nodeoffset = fdt_path_offset (working_fdt, argv[2]);
  414. if (nodeoffset < 0) {
  415. /*
  416. * Not found or something else bad happened.
  417. */
  418. printf ("libfdt fdt_path_offset() returned %s\n",
  419. fdt_strerror(nodeoffset));
  420. return 1;
  421. }
  422. /*
  423. * Do the delete. A fourth parameter means delete a property,
  424. * otherwise delete the node.
  425. */
  426. if (argc > 3) {
  427. err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
  428. if (err < 0) {
  429. printf("libfdt fdt_delprop(): %s\n",
  430. fdt_strerror(err));
  431. return err;
  432. }
  433. } else {
  434. err = fdt_del_node(working_fdt, nodeoffset);
  435. if (err < 0) {
  436. printf("libfdt fdt_del_node(): %s\n",
  437. fdt_strerror(err));
  438. return err;
  439. }
  440. }
  441. /*
  442. * Display header info
  443. */
  444. } else if (argv[1][0] == 'h') {
  445. u32 version = fdt_version(working_fdt);
  446. printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
  447. printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
  448. fdt_totalsize(working_fdt));
  449. printf("off_dt_struct:\t\t0x%x\n",
  450. fdt_off_dt_struct(working_fdt));
  451. printf("off_dt_strings:\t\t0x%x\n",
  452. fdt_off_dt_strings(working_fdt));
  453. printf("off_mem_rsvmap:\t\t0x%x\n",
  454. fdt_off_mem_rsvmap(working_fdt));
  455. printf("version:\t\t%d\n", version);
  456. printf("last_comp_version:\t%d\n",
  457. fdt_last_comp_version(working_fdt));
  458. if (version >= 2)
  459. printf("boot_cpuid_phys:\t0x%x\n",
  460. fdt_boot_cpuid_phys(working_fdt));
  461. if (version >= 3)
  462. printf("size_dt_strings:\t0x%x\n",
  463. fdt_size_dt_strings(working_fdt));
  464. if (version >= 17)
  465. printf("size_dt_struct:\t\t0x%x\n",
  466. fdt_size_dt_struct(working_fdt));
  467. printf("number mem_rsv:\t\t0x%x\n",
  468. fdt_num_mem_rsv(working_fdt));
  469. printf("\n");
  470. /*
  471. * Set boot cpu id
  472. */
  473. } else if (strncmp(argv[1], "boo", 3) == 0) {
  474. unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
  475. fdt_set_boot_cpuid_phys(working_fdt, tmp);
  476. /*
  477. * memory command
  478. */
  479. } else if (strncmp(argv[1], "me", 2) == 0) {
  480. uint64_t addr, size;
  481. int err;
  482. addr = simple_strtoull(argv[2], NULL, 16);
  483. size = simple_strtoull(argv[3], NULL, 16);
  484. err = fdt_fixup_memory(working_fdt, addr, size);
  485. if (err < 0)
  486. return err;
  487. /*
  488. * mem reserve commands
  489. */
  490. } else if (strncmp(argv[1], "rs", 2) == 0) {
  491. if (argv[2][0] == 'p') {
  492. uint64_t addr, size;
  493. int total = fdt_num_mem_rsv(working_fdt);
  494. int j, err;
  495. printf("index\t\t start\t\t size\n");
  496. printf("-------------------------------"
  497. "-----------------\n");
  498. for (j = 0; j < total; j++) {
  499. err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
  500. if (err < 0) {
  501. printf("libfdt fdt_get_mem_rsv(): %s\n",
  502. fdt_strerror(err));
  503. return err;
  504. }
  505. printf(" %x\t%08x%08x\t%08x%08x\n", j,
  506. (u32)(addr >> 32),
  507. (u32)(addr & 0xffffffff),
  508. (u32)(size >> 32),
  509. (u32)(size & 0xffffffff));
  510. }
  511. } else if (argv[2][0] == 'a') {
  512. uint64_t addr, size;
  513. int err;
  514. addr = simple_strtoull(argv[3], NULL, 16);
  515. size = simple_strtoull(argv[4], NULL, 16);
  516. err = fdt_add_mem_rsv(working_fdt, addr, size);
  517. if (err < 0) {
  518. printf("libfdt fdt_add_mem_rsv(): %s\n",
  519. fdt_strerror(err));
  520. return err;
  521. }
  522. } else if (argv[2][0] == 'd') {
  523. unsigned long idx = simple_strtoul(argv[3], NULL, 16);
  524. int err = fdt_del_mem_rsv(working_fdt, idx);
  525. if (err < 0) {
  526. printf("libfdt fdt_del_mem_rsv(): %s\n",
  527. fdt_strerror(err));
  528. return err;
  529. }
  530. } else {
  531. /* Unrecognized command */
  532. return CMD_RET_USAGE;
  533. }
  534. }
  535. #ifdef CONFIG_OF_BOARD_SETUP
  536. /* Call the board-specific fixup routine */
  537. else if (strncmp(argv[1], "boa", 3) == 0) {
  538. int err = ft_board_setup(working_fdt, gd->bd);
  539. if (err) {
  540. printf("Failed to update board information in FDT: %s\n",
  541. fdt_strerror(err));
  542. return CMD_RET_FAILURE;
  543. }
  544. }
  545. #endif
  546. /* Create a chosen node */
  547. else if (strncmp(argv[1], "cho", 3) == 0) {
  548. unsigned long initrd_start = 0, initrd_end = 0;
  549. if ((argc != 2) && (argc != 4))
  550. return CMD_RET_USAGE;
  551. if (argc == 4) {
  552. initrd_start = simple_strtoul(argv[2], NULL, 16);
  553. initrd_end = simple_strtoul(argv[3], NULL, 16);
  554. }
  555. fdt_chosen(working_fdt);
  556. fdt_initrd(working_fdt, initrd_start, initrd_end);
  557. #if defined(CONFIG_FIT_SIGNATURE)
  558. } else if (strncmp(argv[1], "che", 3) == 0) {
  559. int cfg_noffset;
  560. int ret;
  561. unsigned long addr;
  562. struct fdt_header *blob;
  563. if (!working_fdt)
  564. return CMD_RET_FAILURE;
  565. if (argc > 2) {
  566. addr = simple_strtoul(argv[2], NULL, 16);
  567. blob = map_sysmem(addr, 0);
  568. } else {
  569. blob = (struct fdt_header *)gd->fdt_blob;
  570. }
  571. if (!fdt_valid(&blob))
  572. return 1;
  573. gd->fdt_blob = blob;
  574. cfg_noffset = fit_conf_get_node(working_fdt, NULL);
  575. if (!cfg_noffset) {
  576. printf("Could not find configuration node: %s\n",
  577. fdt_strerror(cfg_noffset));
  578. return CMD_RET_FAILURE;
  579. }
  580. ret = fit_config_verify(working_fdt, cfg_noffset);
  581. if (ret == 0)
  582. return CMD_RET_SUCCESS;
  583. else
  584. return CMD_RET_FAILURE;
  585. #endif
  586. }
  587. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  588. /* apply an overlay */
  589. else if (strncmp(argv[1], "ap", 2) == 0) {
  590. unsigned long addr;
  591. struct fdt_header *blob;
  592. int ret;
  593. if (argc != 3)
  594. return CMD_RET_USAGE;
  595. if (!working_fdt)
  596. return CMD_RET_FAILURE;
  597. addr = simple_strtoul(argv[2], NULL, 16);
  598. blob = map_sysmem(addr, 0);
  599. if (!fdt_valid(&blob))
  600. return CMD_RET_FAILURE;
  601. ret = fdt_overlay_apply(working_fdt, blob);
  602. if (ret) {
  603. printf("fdt_overlay_apply(): %s\n", fdt_strerror(ret));
  604. return CMD_RET_FAILURE;
  605. }
  606. }
  607. #endif
  608. /* resize the fdt */
  609. else if (strncmp(argv[1], "re", 2) == 0) {
  610. uint extrasize;
  611. if (argc > 2)
  612. extrasize = simple_strtoul(argv[2], NULL, 16);
  613. else
  614. extrasize = 0;
  615. fdt_shrink_to_minimum(working_fdt, extrasize);
  616. }
  617. else {
  618. /* Unrecognized command */
  619. return CMD_RET_USAGE;
  620. }
  621. return 0;
  622. }
  623. /****************************************************************************/
  624. /**
  625. * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
  626. *
  627. * @blobp: Pointer to FDT pointer
  628. * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
  629. */
  630. static int fdt_valid(struct fdt_header **blobp)
  631. {
  632. const void *blob = *blobp;
  633. int err;
  634. if (blob == NULL) {
  635. printf ("The address of the fdt is invalid (NULL).\n");
  636. return 0;
  637. }
  638. err = fdt_check_header(blob);
  639. if (err == 0)
  640. return 1; /* valid */
  641. if (err < 0) {
  642. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  643. /*
  644. * Be more informative on bad version.
  645. */
  646. if (err == -FDT_ERR_BADVERSION) {
  647. if (fdt_version(blob) <
  648. FDT_FIRST_SUPPORTED_VERSION) {
  649. printf (" - too old, fdt %d < %d",
  650. fdt_version(blob),
  651. FDT_FIRST_SUPPORTED_VERSION);
  652. }
  653. if (fdt_last_comp_version(blob) >
  654. FDT_LAST_SUPPORTED_VERSION) {
  655. printf (" - too new, fdt %d > %d",
  656. fdt_version(blob),
  657. FDT_LAST_SUPPORTED_VERSION);
  658. }
  659. }
  660. printf("\n");
  661. *blobp = NULL;
  662. return 0;
  663. }
  664. return 1;
  665. }
  666. /****************************************************************************/
  667. /*
  668. * Parse the user's input, partially heuristic. Valid formats:
  669. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  670. * C conventions.
  671. * [00 11 22 .. nn] - byte stream
  672. * "string" - If the the value doesn't start with "<" or "[", it is
  673. * treated as a string. Note that the quotes are
  674. * stripped by the parser before we get the string.
  675. * newval: An array of strings containing the new property as specified
  676. * on the command line
  677. * count: The number of strings in the array
  678. * data: A bytestream to be placed in the property
  679. * len: The length of the resulting bytestream
  680. */
  681. static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
  682. {
  683. char *cp; /* temporary char pointer */
  684. char *newp; /* temporary newval char pointer */
  685. unsigned long tmp; /* holds converted values */
  686. int stridx = 0;
  687. *len = 0;
  688. newp = newval[0];
  689. /* An array of cells */
  690. if (*newp == '<') {
  691. newp++;
  692. while ((*newp != '>') && (stridx < count)) {
  693. /*
  694. * Keep searching until we find that last ">"
  695. * That way users don't have to escape the spaces
  696. */
  697. if (*newp == '\0') {
  698. newp = newval[++stridx];
  699. continue;
  700. }
  701. cp = newp;
  702. tmp = simple_strtoul(cp, &newp, 0);
  703. if (*cp != '?')
  704. *(fdt32_t *)data = cpu_to_fdt32(tmp);
  705. else
  706. newp++;
  707. data += 4;
  708. *len += 4;
  709. /* If the ptr didn't advance, something went wrong */
  710. if ((newp - cp) <= 0) {
  711. printf("Sorry, I could not convert \"%s\"\n",
  712. cp);
  713. return 1;
  714. }
  715. while (*newp == ' ')
  716. newp++;
  717. }
  718. if (*newp != '>') {
  719. printf("Unexpected character '%c'\n", *newp);
  720. return 1;
  721. }
  722. } else if (*newp == '[') {
  723. /*
  724. * Byte stream. Convert the values.
  725. */
  726. newp++;
  727. while ((stridx < count) && (*newp != ']')) {
  728. while (*newp == ' ')
  729. newp++;
  730. if (*newp == '\0') {
  731. newp = newval[++stridx];
  732. continue;
  733. }
  734. if (!isxdigit(*newp))
  735. break;
  736. tmp = simple_strtoul(newp, &newp, 16);
  737. *data++ = tmp & 0xFF;
  738. *len = *len + 1;
  739. }
  740. if (*newp != ']') {
  741. printf("Unexpected character '%c'\n", *newp);
  742. return 1;
  743. }
  744. } else {
  745. /*
  746. * Assume it is one or more strings. Copy it into our
  747. * data area for convenience (including the
  748. * terminating '\0's).
  749. */
  750. while (stridx < count) {
  751. size_t length = strlen(newp) + 1;
  752. strcpy(data, newp);
  753. data += length;
  754. *len += length;
  755. newp = newval[++stridx];
  756. }
  757. }
  758. return 0;
  759. }
  760. /****************************************************************************/
  761. /*
  762. * Heuristic to guess if this is a string or concatenated strings.
  763. */
  764. static int is_printable_string(const void *data, int len)
  765. {
  766. const char *s = data;
  767. /* zero length is not */
  768. if (len == 0)
  769. return 0;
  770. /* must terminate with zero or '\n' */
  771. if (s[len - 1] != '\0' && s[len - 1] != '\n')
  772. return 0;
  773. /* printable or a null byte (concatenated strings) */
  774. while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
  775. /*
  776. * If we see a null, there are three possibilities:
  777. * 1) If len == 1, it is the end of the string, printable
  778. * 2) Next character also a null, not printable.
  779. * 3) Next character not a null, continue to check.
  780. */
  781. if (s[0] == '\0') {
  782. if (len == 1)
  783. return 1;
  784. if (s[1] == '\0')
  785. return 0;
  786. }
  787. s++;
  788. len--;
  789. }
  790. /* Not the null termination, or not done yet: not printable */
  791. if (*s != '\0' || (len != 0))
  792. return 0;
  793. return 1;
  794. }
  795. /*
  796. * Print the property in the best format, a heuristic guess. Print as
  797. * a string, concatenated strings, a byte, word, double word, or (if all
  798. * else fails) it is printed as a stream of bytes.
  799. */
  800. static void print_data(const void *data, int len)
  801. {
  802. int j;
  803. /* no data, don't print */
  804. if (len == 0)
  805. return;
  806. /*
  807. * It is a string, but it may have multiple strings (embedded '\0's).
  808. */
  809. if (is_printable_string(data, len)) {
  810. puts("\"");
  811. j = 0;
  812. while (j < len) {
  813. if (j > 0)
  814. puts("\", \"");
  815. puts(data);
  816. j += strlen(data) + 1;
  817. data += strlen(data) + 1;
  818. }
  819. puts("\"");
  820. return;
  821. }
  822. if ((len %4) == 0) {
  823. if (len > CMD_FDT_MAX_DUMP)
  824. printf("* 0x%p [0x%08x]", data, len);
  825. else {
  826. const __be32 *p;
  827. printf("<");
  828. for (j = 0, p = data; j < len/4; j++)
  829. printf("0x%08x%s", fdt32_to_cpu(p[j]),
  830. j < (len/4 - 1) ? " " : "");
  831. printf(">");
  832. }
  833. } else { /* anything else... hexdump */
  834. if (len > CMD_FDT_MAX_DUMP)
  835. printf("* 0x%p [0x%08x]", data, len);
  836. else {
  837. const u8 *s;
  838. printf("[");
  839. for (j = 0, s = data; j < len; j++)
  840. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  841. printf("]");
  842. }
  843. }
  844. }
  845. /****************************************************************************/
  846. /*
  847. * Recursively print (a portion of) the working_fdt. The depth parameter
  848. * determines how deeply nested the fdt is printed.
  849. */
  850. static int fdt_print(const char *pathp, char *prop, int depth)
  851. {
  852. static char tabs[MAX_LEVEL+1] =
  853. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  854. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  855. const void *nodep; /* property node pointer */
  856. int nodeoffset; /* node offset from libfdt */
  857. int nextoffset; /* next node offset from libfdt */
  858. uint32_t tag; /* tag */
  859. int len; /* length of the property */
  860. int level = 0; /* keep track of nesting level */
  861. const struct fdt_property *fdt_prop;
  862. nodeoffset = fdt_path_offset (working_fdt, pathp);
  863. if (nodeoffset < 0) {
  864. /*
  865. * Not found or something else bad happened.
  866. */
  867. printf ("libfdt fdt_path_offset() returned %s\n",
  868. fdt_strerror(nodeoffset));
  869. return 1;
  870. }
  871. /*
  872. * The user passed in a property as well as node path.
  873. * Print only the given property and then return.
  874. */
  875. if (prop) {
  876. nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
  877. if (len == 0) {
  878. /* no property value */
  879. printf("%s %s\n", pathp, prop);
  880. return 0;
  881. } else if (nodep && len > 0) {
  882. printf("%s = ", prop);
  883. print_data (nodep, len);
  884. printf("\n");
  885. return 0;
  886. } else {
  887. printf ("libfdt fdt_getprop(): %s\n",
  888. fdt_strerror(len));
  889. return 1;
  890. }
  891. }
  892. /*
  893. * The user passed in a node path and no property,
  894. * print the node and all subnodes.
  895. */
  896. while(level >= 0) {
  897. tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
  898. switch(tag) {
  899. case FDT_BEGIN_NODE:
  900. pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
  901. if (level <= depth) {
  902. if (pathp == NULL)
  903. pathp = "/* NULL pointer error */";
  904. if (*pathp == '\0')
  905. pathp = "/"; /* root is nameless */
  906. printf("%s%s {\n",
  907. &tabs[MAX_LEVEL - level], pathp);
  908. }
  909. level++;
  910. if (level >= MAX_LEVEL) {
  911. printf("Nested too deep, aborting.\n");
  912. return 1;
  913. }
  914. break;
  915. case FDT_END_NODE:
  916. level--;
  917. if (level <= depth)
  918. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  919. if (level == 0) {
  920. level = -1; /* exit the loop */
  921. }
  922. break;
  923. case FDT_PROP:
  924. fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
  925. sizeof(*fdt_prop));
  926. pathp = fdt_string(working_fdt,
  927. fdt32_to_cpu(fdt_prop->nameoff));
  928. len = fdt32_to_cpu(fdt_prop->len);
  929. nodep = fdt_prop->data;
  930. if (len < 0) {
  931. printf ("libfdt fdt_getprop(): %s\n",
  932. fdt_strerror(len));
  933. return 1;
  934. } else if (len == 0) {
  935. /* the property has no value */
  936. if (level <= depth)
  937. printf("%s%s;\n",
  938. &tabs[MAX_LEVEL - level],
  939. pathp);
  940. } else {
  941. if (level <= depth) {
  942. printf("%s%s = ",
  943. &tabs[MAX_LEVEL - level],
  944. pathp);
  945. print_data (nodep, len);
  946. printf(";\n");
  947. }
  948. }
  949. break;
  950. case FDT_NOP:
  951. printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
  952. break;
  953. case FDT_END:
  954. return 1;
  955. default:
  956. if (level <= depth)
  957. printf("Unknown tag 0x%08X\n", tag);
  958. return 1;
  959. }
  960. nodeoffset = nextoffset;
  961. }
  962. return 0;
  963. }
  964. /********************************************************************/
  965. #ifdef CONFIG_SYS_LONGHELP
  966. static char fdt_help_text[] =
  967. "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
  968. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  969. "fdt apply <addr> - Apply overlay to the DT\n"
  970. #endif
  971. #ifdef CONFIG_OF_BOARD_SETUP
  972. "fdt boardsetup - Do board-specific set up\n"
  973. #endif
  974. #ifdef CONFIG_OF_SYSTEM_SETUP
  975. "fdt systemsetup - Do system-specific set up\n"
  976. #endif
  977. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  978. "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
  979. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  980. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  981. "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
  982. "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
  983. "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
  984. "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
  985. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  986. "fdt mknode <path> <node> - Create a new node after <path>\n"
  987. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  988. "fdt header - Display header info\n"
  989. "fdt bootcpu <id> - Set boot cpuid\n"
  990. "fdt memory <addr> <size> - Add/Update memory node\n"
  991. "fdt rsvmem print - Show current mem reserves\n"
  992. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  993. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  994. "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
  995. " <start>/<end> - initrd start/end addr\n"
  996. #if defined(CONFIG_FIT_SIGNATURE)
  997. "fdt checksign [<addr>] - check FIT signature\n"
  998. " <start> - addr of key blob\n"
  999. " default gd->fdt_blob\n"
  1000. #endif
  1001. "NOTE: Dereference aliases by omitting the leading '/', "
  1002. "e.g. fdt print ethernet0.";
  1003. #endif
  1004. U_BOOT_CMD(
  1005. fdt, 255, 0, do_fdt,
  1006. "flattened device tree utility commands", fdt_help_text
  1007. );