fdt.c 27 KB

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