fdt.c 28 KB

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