ofw.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drivers/platform.h>
  13. #include <drivers/core/bus.h>
  14. #include <drivers/serial_dm.h>
  15. #define DBG_TAG "rtdm.ofw"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "ofw_internal.h"
  19. struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
  20. const struct rt_ofw_stub *stub_start, const struct rt_ofw_stub *stub_end)
  21. {
  22. const struct rt_ofw_stub *stub = RT_NULL;
  23. if (np && stub_start && stub_end &&
  24. !rt_ofw_node_test_flag(np, RT_OFW_F_READLY) &&
  25. !rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM))
  26. {
  27. struct rt_ofw_prop *compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  28. if (compat_prop)
  29. {
  30. rt_ofw_foreach_stub(stub, stub_start, stub_end)
  31. {
  32. struct rt_ofw_node_id *id;
  33. if (!stub->ids)
  34. {
  35. continue;
  36. }
  37. id = rt_ofw_prop_match(compat_prop, stub->ids);
  38. if (id)
  39. {
  40. if (!stub->handler(np, id))
  41. {
  42. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  43. }
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. return (struct rt_ofw_stub *)stub;
  50. }
  51. struct ofw_obj_cmp_list
  52. {
  53. const char *cells_name;
  54. const char *obj_name;
  55. rt_size_t obj_size;
  56. };
  57. static const struct ofw_obj_cmp_list ofw_obj_cmp_list[] =
  58. {
  59. { "#power-domain-cells", RT_POWER_DOMAIN_PROXY_OBJ_NAME, sizeof(struct rt_dm_power_domain_proxy) },
  60. { "#power-domain-cells", RT_POWER_DOMAIN_OBJ_NAME, sizeof(struct rt_dm_power_domain) },
  61. };
  62. static struct rt_object *ofw_parse_object(struct rt_ofw_node *np, const char *cells_name)
  63. {
  64. const struct ofw_obj_cmp_list *item;
  65. struct rt_object *obj = rt_ofw_data(np), *ret_obj = RT_NULL;
  66. RT_BITMAP_DECLARE(idx_mask, RT_ARRAY_SIZE(ofw_obj_cmp_list)) = {};
  67. for (int i = 0; i < RT_ARRAY_SIZE(ofw_obj_cmp_list); ++i)
  68. {
  69. item = &ofw_obj_cmp_list[i];
  70. if (!rt_ofw_prop_read_bool(np, item->cells_name))
  71. {
  72. rt_bitmap_set_bit(idx_mask, i);
  73. }
  74. }
  75. while (!ret_obj)
  76. {
  77. int i = 0;
  78. /* Is print ? */
  79. if (!((rt_uint32_t)(obj->name[0] - 0x20) < 0x5f))
  80. {
  81. break;
  82. }
  83. rt_bitmap_for_each_clear_bit(idx_mask, i, RT_ARRAY_SIZE(ofw_obj_cmp_list))
  84. {
  85. item = &ofw_obj_cmp_list[i];
  86. if (!rt_strcmp(item->cells_name, cells_name))
  87. {
  88. ret_obj = obj;
  89. break;
  90. }
  91. if (!rt_strncmp(item->obj_name, obj->name, RT_NAME_MAX))
  92. {
  93. obj = (struct rt_object *)((rt_ubase_t)obj + item->obj_size);
  94. break;
  95. }
  96. }
  97. if (i >= RT_ARRAY_SIZE(ofw_obj_cmp_list))
  98. {
  99. LOG_E("Invalid rt_object = %s", obj->name);
  100. break;
  101. }
  102. }
  103. return ret_obj;
  104. }
  105. struct rt_object *rt_ofw_parse_object(struct rt_ofw_node *np, const char *obj_name, const char *cells_name)
  106. {
  107. struct rt_object *obj = RT_NULL, *test_obj;
  108. if (np && (test_obj = rt_ofw_data(np)) && cells_name)
  109. {
  110. /* The composite object is rare, so we try to find this object as much as possible at once. */
  111. if (obj_name && rt_strcmp(test_obj->name, obj_name))
  112. {
  113. obj = test_obj;
  114. }
  115. else
  116. {
  117. obj = ofw_parse_object(np, cells_name);
  118. }
  119. }
  120. return obj;
  121. }
  122. static const char *ofw_console_serial_find(char *dst_con, struct rt_ofw_node *np)
  123. {
  124. rt_object_t rt_obj = RT_NULL;
  125. const char *ofw_name = RT_NULL;
  126. struct rt_serial_device *rt_serial = rt_ofw_data(np);
  127. if (rt_serial)
  128. {
  129. rt_obj = &rt_serial->parent.parent;
  130. }
  131. /*
  132. * This is a dangerous behavior because rt_data can be modified by other
  133. * user. But fortunately, we can check if the rt_data is a rt_device or
  134. * rt_serial_device.
  135. */
  136. if (rt_obj && rt_object_get_type(rt_obj) == RT_Object_Class_Device &&
  137. rt_serial->parent.type == RT_Device_Class_Char)
  138. {
  139. ofw_name = np->full_name;
  140. rt_strncpy(dst_con, rt_obj->name, RT_NAME_MAX);
  141. }
  142. return ofw_name;
  143. }
  144. static int tty_device_compare(rt_device_t dev, void *data)
  145. {
  146. rt_ubase_t *args_list;
  147. char *dst_con;
  148. const char *console, **ofw_name;
  149. const struct rt_ofw_node_id *id;
  150. struct rt_platform_device *pdev;
  151. int *tty_idx, tty_id, tty_name_len;
  152. pdev = rt_container_of(dev, struct rt_platform_device, parent);
  153. id = pdev->id;
  154. args_list = data;
  155. tty_idx = (int *)args_list[0];
  156. tty_id = args_list[1];
  157. console = (const char *)args_list[2];
  158. tty_name_len = args_list[3];
  159. dst_con = (char *)args_list[4];
  160. ofw_name = (const char **)args_list[5];
  161. if (id && id->type[0] && !strncmp(id->type, console, tty_name_len))
  162. {
  163. if (*tty_idx == tty_id)
  164. {
  165. *ofw_name = ofw_console_serial_find(dst_con, pdev->parent.ofw_node);
  166. return RT_EOK;
  167. }
  168. ++*tty_idx;
  169. }
  170. return -RT_EEMPTY;
  171. }
  172. static const char *ofw_console_tty_find(char *dst_con, const char *con)
  173. {
  174. const char *ofw_name = RT_NULL;
  175. static rt_bus_t platform_bus = RT_NULL;
  176. if (!platform_bus)
  177. {
  178. platform_bus = rt_bus_find_by_name("platform");
  179. }
  180. if (platform_bus)
  181. {
  182. rt_ubase_t args_list[6];
  183. const char *console = con;
  184. int tty_idx = 0, tty_id = 0, tty_name_len;
  185. con += sizeof("tty") - 1;
  186. while (*con && !(*con >= '0' && *con <= '9'))
  187. {
  188. ++con;
  189. }
  190. tty_name_len = con - console;
  191. while (*con && *con >= '0' && *con <= '9')
  192. {
  193. tty_id *= 10;
  194. tty_id += *con - '0';
  195. ++con;
  196. }
  197. args_list[0] = (rt_ubase_t)&tty_idx;
  198. args_list[1] = tty_id;
  199. args_list[2] = (rt_ubase_t)console;
  200. args_list[3] = tty_name_len;
  201. args_list[4] = (rt_ubase_t)dst_con;
  202. args_list[5] = (rt_ubase_t)&ofw_name;
  203. rt_bus_for_each_dev(platform_bus, &args_list, tty_device_compare);
  204. }
  205. return ofw_name;
  206. }
  207. rt_err_t rt_ofw_console_setup(void)
  208. {
  209. rt_err_t err = -RT_ENOSYS;
  210. char con_name[RT_NAME_MAX], *options = RT_NULL;
  211. const char *ofw_name = RT_NULL, *stdout_path, *con;
  212. /* chosen.console > chosen.stdout-path > RT_CONSOLE_DEVICE_NAME */
  213. con = rt_ofw_bootargs_select("console=", 0);
  214. for (int i = 1; con; ++i)
  215. {
  216. if (!rt_strncmp(con, "uart", sizeof("uart") - 1))
  217. {
  218. rt_strncpy(con_name, con, RT_NAME_MAX);
  219. err = RT_EOK;
  220. break;
  221. }
  222. else if (!rt_strncmp(con, "tty", sizeof("tty") - 1))
  223. {
  224. ofw_name = ofw_console_tty_find(con_name, con);
  225. if (ofw_name)
  226. {
  227. const char *ch = con;
  228. while (*ch && *ch != ' ')
  229. {
  230. if (*ch++ == ',')
  231. {
  232. options = (char *)ch;
  233. break;
  234. }
  235. }
  236. err = RT_EOK;
  237. break;
  238. }
  239. }
  240. con = rt_ofw_bootargs_select("console=", i);
  241. }
  242. if (err == -RT_ENOSYS && !rt_ofw_prop_read_string(ofw_node_chosen, "stdout-path", &stdout_path))
  243. {
  244. struct rt_ofw_node *stdout_np = rt_ofw_find_node_by_path(stdout_path);
  245. if (stdout_np && (ofw_name = ofw_console_serial_find(con_name, stdout_np)))
  246. {
  247. err = RT_EOK;
  248. }
  249. }
  250. if (err == -RT_ENOSYS)
  251. {
  252. rt_device_t serial = rt_device_find(RT_CONSOLE_DEVICE_NAME);
  253. if (serial)
  254. {
  255. ofw_name = rt_ofw_node_full_name(serial->ofw_node);
  256. con = RT_CONSOLE_DEVICE_NAME;
  257. }
  258. else
  259. {
  260. LOG_W("Console will probably fail to setup");
  261. }
  262. }
  263. else
  264. {
  265. con = con_name;
  266. }
  267. rt_console_set_device(con);
  268. if (options)
  269. {
  270. rt_device_t con_dev = rt_console_get_device();
  271. if (con_dev)
  272. {
  273. struct serial_configure con_conf = serial_cfg_from_args(options);
  274. rt_device_control(con_dev, RT_DEVICE_CTRL_CONFIG, &con_conf);
  275. }
  276. }
  277. rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED);
  278. LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>");
  279. return err;
  280. }
  281. const char *rt_ofw_bootargs_select(const char *key, int index)
  282. {
  283. const char *value = RT_NULL;
  284. if (key && index >= 0)
  285. {
  286. static char **values = RT_NULL;
  287. static rt_size_t bootargs_nr = 1;
  288. const char *bootargs = RT_NULL, *ch;
  289. if (bootargs_nr && !values &&
  290. (bootargs_nr = 0, !rt_ofw_prop_read_string(ofw_node_chosen, "bootargs", &bootargs)) &&
  291. bootargs && (bootargs = rt_strdup(bootargs)))
  292. {
  293. rt_bool_t quotes = RT_TRUE;
  294. rt_size_t length = rt_strlen(bootargs);
  295. const char *bootargs_end = bootargs + length;
  296. for (ch = bootargs; ch < bootargs_end; ++ch)
  297. {
  298. if (*ch == '"')
  299. {
  300. quotes = !quotes;
  301. continue;
  302. }
  303. if (*ch != ' ' || !quotes)
  304. {
  305. continue;
  306. }
  307. ++bootargs_nr;
  308. while (*ch == ' ' && ch < bootargs_end)
  309. {
  310. *(char *)ch++ = '\0';
  311. }
  312. --ch;
  313. }
  314. if (bootargs_nr)
  315. {
  316. /* last arg */
  317. ++bootargs_nr;
  318. values = rt_malloc(sizeof(char *) * bootargs_nr);
  319. }
  320. if (values)
  321. {
  322. int idx = 0;
  323. for (int i = 0; i < length; ++i)
  324. {
  325. for (; i < length && !bootargs[i]; ++i)
  326. {
  327. }
  328. if (i < length)
  329. {
  330. values[idx++] = (char *)&bootargs[i];
  331. }
  332. for (; i < length && bootargs[i]; ++i)
  333. {
  334. }
  335. }
  336. }
  337. else
  338. {
  339. rt_free((char *)bootargs);
  340. }
  341. }
  342. if (values)
  343. {
  344. int keylen = rt_strlen(key);
  345. for (int idx = 0, count = 0; idx < bootargs_nr; ++idx)
  346. {
  347. if (!rt_strncmp(values[idx], key, keylen))
  348. {
  349. if (count == index)
  350. {
  351. value = values[idx] + keylen;
  352. break;
  353. }
  354. ++count;
  355. }
  356. }
  357. }
  358. }
  359. return value;
  360. }
  361. #ifdef RT_USING_CONSOLE
  362. static void dts_put_depth(int depth)
  363. {
  364. while (depth --> 0)
  365. {
  366. rt_kputs(" ");
  367. }
  368. }
  369. static rt_bool_t dts_test_string_list(const void *value, int size)
  370. {
  371. const char *str, *str_start, *str_end;
  372. if (!size)
  373. {
  374. return RT_FALSE;
  375. }
  376. str = value;
  377. /* String end with '\0' */
  378. if (str[size - 1] != '\0')
  379. {
  380. return RT_FALSE;
  381. }
  382. /* Get string list end */
  383. str_end = str + size;
  384. while (str < str_end)
  385. {
  386. str_start = str;
  387. /* Before string list end, not '\0' and a printable characters */
  388. while (str < str_end && *str && ((unsigned char)*str >= ' ' && (unsigned char)*str <= '~'))
  389. {
  390. ++str;
  391. }
  392. /* Not zero, or not increased */
  393. if (*str != '\0' || str == str_start)
  394. {
  395. return RT_FALSE;
  396. }
  397. /* Next string */
  398. ++str;
  399. }
  400. return RT_TRUE;
  401. }
  402. static void ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  403. {
  404. int depth = 0;
  405. struct rt_ofw_prop *prop;
  406. struct rt_ofw_node *org_np = np;
  407. while (np)
  408. {
  409. dts_put_depth(depth);
  410. rt_kputs(np->full_name);
  411. rt_kputs(" {\n");
  412. prop = np->props;
  413. /* Print prop start */
  414. ++depth;
  415. while (prop)
  416. {
  417. dts_put_depth(depth);
  418. rt_kputs(prop->name);
  419. /* Have value? */
  420. if (prop->length > 0)
  421. {
  422. int length = prop->length;
  423. void *value = prop->value;
  424. rt_kputs(" = ");
  425. if (dts_test_string_list(value, length))
  426. {
  427. /* String list */
  428. char *str = value;
  429. do {
  430. rt_kputs("\"");
  431. rt_kputs(str);
  432. rt_kputs("\", ");
  433. str += rt_strlen(str) + 1;
  434. } while (str < (char *)value + length);
  435. rt_kputs("\b\b");
  436. }
  437. else if ((length % 4) == 0)
  438. {
  439. /* u32 data in <?> */
  440. fdt32_t *cell = value;
  441. rt_kputs("<");
  442. length /= 4;
  443. for (int i = 0; i < length; ++i)
  444. {
  445. rt_kprintf("0x%02x ", fdt32_to_cpu(cell[i]));
  446. }
  447. rt_kputs("\b>");
  448. }
  449. else
  450. {
  451. /* Byte data in [?] */
  452. rt_uint8_t *byte = value;
  453. rt_kputs("[");
  454. for (int i = 0; i < length; ++i)
  455. {
  456. rt_kprintf("%02x ", *byte++);
  457. }
  458. rt_kputs("\b]");
  459. }
  460. }
  461. rt_kputs(";\n");
  462. prop = prop->next;
  463. }
  464. --depth;
  465. /* Print prop end */
  466. if (np->child)
  467. {
  468. rt_kputs("\n");
  469. np = np->child;
  470. ++depth;
  471. }
  472. else
  473. {
  474. dts_put_depth(depth);
  475. rt_kputs("};\n");
  476. if (!sibling_too && org_np == np)
  477. {
  478. break;
  479. }
  480. while (np->parent && !np->sibling)
  481. {
  482. np = np->parent;
  483. --depth;
  484. if (depth >= 0)
  485. {
  486. dts_put_depth(depth);
  487. rt_kputs("};\n");
  488. }
  489. }
  490. if (!sibling_too && org_np == np)
  491. {
  492. break;
  493. }
  494. np = np->sibling;
  495. if (np)
  496. {
  497. rt_kputs("\n");
  498. }
  499. }
  500. }
  501. }
  502. void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  503. {
  504. if (np)
  505. {
  506. if (!rt_strcmp(np->name, "/"))
  507. {
  508. struct fdt_info *header = (struct fdt_info *)np->name;
  509. struct fdt_reserve_entry *rsvmap = header->rsvmap;
  510. /*
  511. * Shall be present to identify the file as a version 1 DTS
  512. * (dts files without this tag will be treated by dtc
  513. * as being in the obsolete version 0, which uses
  514. * a different format for integers in addition to
  515. * other small but incompatible changes).
  516. */
  517. rt_kprintf("/dts-v1/;\n\n");
  518. for (int i = header->rsvmap_nr - 1; i >= 0; --i)
  519. {
  520. rt_kprintf("/memreserve/\t%p %p;\n",
  521. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->address)),
  522. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->size)));
  523. ++rsvmap;
  524. }
  525. rt_kputs(np->name);
  526. }
  527. ofw_node_dump_dts(np, sibling_too);
  528. }
  529. }
  530. #ifdef RT_USING_MSH
  531. static void ofw_dts(int argc, char**argv)
  532. {
  533. if (ofw_node_root)
  534. {
  535. if (argc == 1)
  536. {
  537. rt_ofw_node_dump_dts(ofw_node_root, RT_TRUE);
  538. }
  539. else if (argv[1][0] == '/')
  540. {
  541. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[1]);
  542. if (np)
  543. {
  544. rt_ofw_node_dump_dts(np, RT_FALSE);
  545. }
  546. else
  547. {
  548. rt_kprintf("%s not found.\n", argv[1]);
  549. }
  550. }
  551. else if (argc >= 2 && !rt_strcmp(argv[1], "list") && argv[2][0] == '/')
  552. {
  553. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[2]);
  554. if (np)
  555. {
  556. const char *split = np == ofw_node_root ? "" : "/";
  557. np = np->child;
  558. while (np)
  559. {
  560. rt_kprintf("%s%s%s\n", argv[2], split, np->full_name);
  561. np = np->sibling;
  562. }
  563. }
  564. else
  565. {
  566. rt_kprintf("%s not found.\n", argv[2]);
  567. }
  568. }
  569. else
  570. {
  571. rt_kprintf("Usage: %s {list} {path}\n", __func__);
  572. }
  573. }
  574. else
  575. {
  576. rt_kprintf("\"/\" path not found.");
  577. }
  578. }
  579. MSH_CMD_EXPORT(ofw_dts, dump the dts or node for this platform);
  580. #endif /* RT_USING_MSH */
  581. #endif /* RT_USING_CONSOLE */