fdt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 (argv[1][0] == 'a') {
  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. /* resize the fdt */
  576. else if (strncmp(argv[1], "re", 2) == 0) {
  577. fdt_shrink_to_minimum(working_fdt);
  578. }
  579. else {
  580. /* Unrecognized command */
  581. return CMD_RET_USAGE;
  582. }
  583. return 0;
  584. }
  585. /****************************************************************************/
  586. /**
  587. * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
  588. *
  589. * @blobp: Pointer to FDT pointer
  590. * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
  591. */
  592. static int fdt_valid(struct fdt_header **blobp)
  593. {
  594. const void *blob = *blobp;
  595. int err;
  596. if (blob == NULL) {
  597. printf ("The address of the fdt is invalid (NULL).\n");
  598. return 0;
  599. }
  600. err = fdt_check_header(blob);
  601. if (err == 0)
  602. return 1; /* valid */
  603. if (err < 0) {
  604. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  605. /*
  606. * Be more informative on bad version.
  607. */
  608. if (err == -FDT_ERR_BADVERSION) {
  609. if (fdt_version(blob) <
  610. FDT_FIRST_SUPPORTED_VERSION) {
  611. printf (" - too old, fdt %d < %d",
  612. fdt_version(blob),
  613. FDT_FIRST_SUPPORTED_VERSION);
  614. }
  615. if (fdt_last_comp_version(blob) >
  616. FDT_LAST_SUPPORTED_VERSION) {
  617. printf (" - too new, fdt %d > %d",
  618. fdt_version(blob),
  619. FDT_LAST_SUPPORTED_VERSION);
  620. }
  621. }
  622. printf("\n");
  623. *blobp = NULL;
  624. return 0;
  625. }
  626. return 1;
  627. }
  628. /****************************************************************************/
  629. /*
  630. * Parse the user's input, partially heuristic. Valid formats:
  631. * <0x00112233 4 05> - an array of cells. Numbers follow standard
  632. * C conventions.
  633. * [00 11 22 .. nn] - byte stream
  634. * "string" - If the the value doesn't start with "<" or "[", it is
  635. * treated as a string. Note that the quotes are
  636. * stripped by the parser before we get the string.
  637. * newval: An array of strings containing the new property as specified
  638. * on the command line
  639. * count: The number of strings in the array
  640. * data: A bytestream to be placed in the property
  641. * len: The length of the resulting bytestream
  642. */
  643. static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
  644. {
  645. char *cp; /* temporary char pointer */
  646. char *newp; /* temporary newval char pointer */
  647. unsigned long tmp; /* holds converted values */
  648. int stridx = 0;
  649. *len = 0;
  650. newp = newval[0];
  651. /* An array of cells */
  652. if (*newp == '<') {
  653. newp++;
  654. while ((*newp != '>') && (stridx < count)) {
  655. /*
  656. * Keep searching until we find that last ">"
  657. * That way users don't have to escape the spaces
  658. */
  659. if (*newp == '\0') {
  660. newp = newval[++stridx];
  661. continue;
  662. }
  663. cp = newp;
  664. tmp = simple_strtoul(cp, &newp, 0);
  665. *(__be32 *)data = __cpu_to_be32(tmp);
  666. data += 4;
  667. *len += 4;
  668. /* If the ptr didn't advance, something went wrong */
  669. if ((newp - cp) <= 0) {
  670. printf("Sorry, I could not convert \"%s\"\n",
  671. cp);
  672. return 1;
  673. }
  674. while (*newp == ' ')
  675. newp++;
  676. }
  677. if (*newp != '>') {
  678. printf("Unexpected character '%c'\n", *newp);
  679. return 1;
  680. }
  681. } else if (*newp == '[') {
  682. /*
  683. * Byte stream. Convert the values.
  684. */
  685. newp++;
  686. while ((stridx < count) && (*newp != ']')) {
  687. while (*newp == ' ')
  688. newp++;
  689. if (*newp == '\0') {
  690. newp = newval[++stridx];
  691. continue;
  692. }
  693. if (!isxdigit(*newp))
  694. break;
  695. tmp = simple_strtoul(newp, &newp, 16);
  696. *data++ = tmp & 0xFF;
  697. *len = *len + 1;
  698. }
  699. if (*newp != ']') {
  700. printf("Unexpected character '%c'\n", *newp);
  701. return 1;
  702. }
  703. } else {
  704. /*
  705. * Assume it is one or more strings. Copy it into our
  706. * data area for convenience (including the
  707. * terminating '\0's).
  708. */
  709. while (stridx < count) {
  710. size_t length = strlen(newp) + 1;
  711. strcpy(data, newp);
  712. data += length;
  713. *len += length;
  714. newp = newval[++stridx];
  715. }
  716. }
  717. return 0;
  718. }
  719. /****************************************************************************/
  720. /*
  721. * Heuristic to guess if this is a string or concatenated strings.
  722. */
  723. static int is_printable_string(const void *data, int len)
  724. {
  725. const char *s = data;
  726. /* zero length is not */
  727. if (len == 0)
  728. return 0;
  729. /* must terminate with zero or '\n' */
  730. if (s[len - 1] != '\0' && s[len - 1] != '\n')
  731. return 0;
  732. /* printable or a null byte (concatenated strings) */
  733. while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
  734. /*
  735. * If we see a null, there are three possibilities:
  736. * 1) If len == 1, it is the end of the string, printable
  737. * 2) Next character also a null, not printable.
  738. * 3) Next character not a null, continue to check.
  739. */
  740. if (s[0] == '\0') {
  741. if (len == 1)
  742. return 1;
  743. if (s[1] == '\0')
  744. return 0;
  745. }
  746. s++;
  747. len--;
  748. }
  749. /* Not the null termination, or not done yet: not printable */
  750. if (*s != '\0' || (len != 0))
  751. return 0;
  752. return 1;
  753. }
  754. /*
  755. * Print the property in the best format, a heuristic guess. Print as
  756. * a string, concatenated strings, a byte, word, double word, or (if all
  757. * else fails) it is printed as a stream of bytes.
  758. */
  759. static void print_data(const void *data, int len)
  760. {
  761. int j;
  762. /* no data, don't print */
  763. if (len == 0)
  764. return;
  765. /*
  766. * It is a string, but it may have multiple strings (embedded '\0's).
  767. */
  768. if (is_printable_string(data, len)) {
  769. puts("\"");
  770. j = 0;
  771. while (j < len) {
  772. if (j > 0)
  773. puts("\", \"");
  774. puts(data);
  775. j += strlen(data) + 1;
  776. data += strlen(data) + 1;
  777. }
  778. puts("\"");
  779. return;
  780. }
  781. if ((len %4) == 0) {
  782. if (len > CONFIG_CMD_FDT_MAX_DUMP)
  783. printf("* 0x%p [0x%08x]", data, len);
  784. else {
  785. const __be32 *p;
  786. printf("<");
  787. for (j = 0, p = data; j < len/4; j++)
  788. printf("0x%08x%s", fdt32_to_cpu(p[j]),
  789. j < (len/4 - 1) ? " " : "");
  790. printf(">");
  791. }
  792. } else { /* anything else... hexdump */
  793. if (len > CONFIG_CMD_FDT_MAX_DUMP)
  794. printf("* 0x%p [0x%08x]", data, len);
  795. else {
  796. const u8 *s;
  797. printf("[");
  798. for (j = 0, s = data; j < len; j++)
  799. printf("%02x%s", s[j], j < len - 1 ? " " : "");
  800. printf("]");
  801. }
  802. }
  803. }
  804. /****************************************************************************/
  805. /*
  806. * Recursively print (a portion of) the working_fdt. The depth parameter
  807. * determines how deeply nested the fdt is printed.
  808. */
  809. static int fdt_print(const char *pathp, char *prop, int depth)
  810. {
  811. static char tabs[MAX_LEVEL+1] =
  812. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
  813. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  814. const void *nodep; /* property node pointer */
  815. int nodeoffset; /* node offset from libfdt */
  816. int nextoffset; /* next node offset from libfdt */
  817. uint32_t tag; /* tag */
  818. int len; /* length of the property */
  819. int level = 0; /* keep track of nesting level */
  820. const struct fdt_property *fdt_prop;
  821. nodeoffset = fdt_path_offset (working_fdt, pathp);
  822. if (nodeoffset < 0) {
  823. /*
  824. * Not found or something else bad happened.
  825. */
  826. printf ("libfdt fdt_path_offset() returned %s\n",
  827. fdt_strerror(nodeoffset));
  828. return 1;
  829. }
  830. /*
  831. * The user passed in a property as well as node path.
  832. * Print only the given property and then return.
  833. */
  834. if (prop) {
  835. nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
  836. if (len == 0) {
  837. /* no property value */
  838. printf("%s %s\n", pathp, prop);
  839. return 0;
  840. } else if (len > 0) {
  841. printf("%s = ", prop);
  842. print_data (nodep, len);
  843. printf("\n");
  844. return 0;
  845. } else {
  846. printf ("libfdt fdt_getprop(): %s\n",
  847. fdt_strerror(len));
  848. return 1;
  849. }
  850. }
  851. /*
  852. * The user passed in a node path and no property,
  853. * print the node and all subnodes.
  854. */
  855. while(level >= 0) {
  856. tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
  857. switch(tag) {
  858. case FDT_BEGIN_NODE:
  859. pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
  860. if (level <= depth) {
  861. if (pathp == NULL)
  862. pathp = "/* NULL pointer error */";
  863. if (*pathp == '\0')
  864. pathp = "/"; /* root is nameless */
  865. printf("%s%s {\n",
  866. &tabs[MAX_LEVEL - level], pathp);
  867. }
  868. level++;
  869. if (level >= MAX_LEVEL) {
  870. printf("Nested too deep, aborting.\n");
  871. return 1;
  872. }
  873. break;
  874. case FDT_END_NODE:
  875. level--;
  876. if (level <= depth)
  877. printf("%s};\n", &tabs[MAX_LEVEL - level]);
  878. if (level == 0) {
  879. level = -1; /* exit the loop */
  880. }
  881. break;
  882. case FDT_PROP:
  883. fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
  884. sizeof(*fdt_prop));
  885. pathp = fdt_string(working_fdt,
  886. fdt32_to_cpu(fdt_prop->nameoff));
  887. len = fdt32_to_cpu(fdt_prop->len);
  888. nodep = fdt_prop->data;
  889. if (len < 0) {
  890. printf ("libfdt fdt_getprop(): %s\n",
  891. fdt_strerror(len));
  892. return 1;
  893. } else if (len == 0) {
  894. /* the property has no value */
  895. if (level <= depth)
  896. printf("%s%s;\n",
  897. &tabs[MAX_LEVEL - level],
  898. pathp);
  899. } else {
  900. if (level <= depth) {
  901. printf("%s%s = ",
  902. &tabs[MAX_LEVEL - level],
  903. pathp);
  904. print_data (nodep, len);
  905. printf(";\n");
  906. }
  907. }
  908. break;
  909. case FDT_NOP:
  910. printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
  911. break;
  912. case FDT_END:
  913. return 1;
  914. default:
  915. if (level <= depth)
  916. printf("Unknown tag 0x%08X\n", tag);
  917. return 1;
  918. }
  919. nodeoffset = nextoffset;
  920. }
  921. return 0;
  922. }
  923. /********************************************************************/
  924. #ifdef CONFIG_SYS_LONGHELP
  925. static char fdt_help_text[] =
  926. "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
  927. #ifdef CONFIG_OF_BOARD_SETUP
  928. "fdt boardsetup - Do board-specific set up\n"
  929. #endif
  930. #ifdef CONFIG_OF_SYSTEM_SETUP
  931. "fdt systemsetup - Do system-specific set up\n"
  932. #endif
  933. "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
  934. "fdt resize - Resize fdt to size + padding to 4k addr\n"
  935. "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
  936. "fdt list <path> [<prop>] - Print one level starting at <path>\n"
  937. "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
  938. "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
  939. "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
  940. "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
  941. "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
  942. "fdt mknode <path> <node> - Create a new node after <path>\n"
  943. "fdt rm <path> [<prop>] - Delete the node or <property>\n"
  944. "fdt header - Display header info\n"
  945. "fdt bootcpu <id> - Set boot cpuid\n"
  946. "fdt memory <addr> <size> - Add/Update memory node\n"
  947. "fdt rsvmem print - Show current mem reserves\n"
  948. "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
  949. "fdt rsvmem delete <index> - Delete a mem reserves\n"
  950. "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
  951. " <start>/<end> - initrd start/end addr\n"
  952. #if defined(CONFIG_FIT_SIGNATURE)
  953. "fdt checksign [<addr>] - check FIT signature\n"
  954. " <start> - addr of key blob\n"
  955. " default gd->fdt_blob\n"
  956. #endif
  957. "NOTE: Dereference aliases by omitting the leading '/', "
  958. "e.g. fdt print ethernet0.";
  959. #endif
  960. U_BOOT_CMD(
  961. fdt, 255, 0, do_fdt,
  962. "flattened device tree utility commands", fdt_help_text
  963. );