fdt.c 27 KB

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