fdt.c 27 KB

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