cmd_fdt.c 26 KB

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