fdt.c 28 KB

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