ofw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #define DBG_TAG "rtdm.ofw"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #include "ofw_internal.h"
  18. struct rt_ofw_stub *rt_ofw_stub_probe_range(struct rt_ofw_node *np,
  19. const struct rt_ofw_stub *stub_start, const struct rt_ofw_stub *stub_end)
  20. {
  21. const struct rt_ofw_stub *stub = RT_NULL;
  22. if (np && stub_start && stub_end &&
  23. !rt_ofw_node_test_flag(np, RT_OFW_F_READLY) &&
  24. !rt_ofw_node_test_flag(np, RT_OFW_F_SYSTEM))
  25. {
  26. struct rt_ofw_prop *compat_prop = rt_ofw_get_prop(np, "compatible", RT_NULL);
  27. if (compat_prop)
  28. {
  29. rt_ofw_foreach_stub(stub, stub_start, stub_end)
  30. {
  31. struct rt_ofw_node_id *id;
  32. if (!stub->ids)
  33. {
  34. continue;
  35. }
  36. id = rt_ofw_prop_match(compat_prop, stub->ids);
  37. if (id)
  38. {
  39. if (!stub->handler(np, id))
  40. {
  41. rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
  42. }
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. return (struct rt_ofw_stub *)stub;
  49. }
  50. static const char *ofw_console_serial_find(char *dst_con, struct rt_ofw_node *np)
  51. {
  52. rt_object_t rt_obj;
  53. const char *ofw_name = RT_NULL;
  54. struct rt_serial_device *rt_serial = rt_ofw_data(np);
  55. if (rt_serial)
  56. {
  57. rt_obj = &rt_serial->parent.parent;
  58. }
  59. /*
  60. * This is a dangerous behavior because rt_data can be modified by other
  61. * user. But fortunately, we can check if the rt_data is a rt_device or
  62. * rt_serial_device.
  63. */
  64. if (rt_obj && rt_object_get_type(rt_obj) == RT_Object_Class_Device &&
  65. rt_serial->parent.type == RT_Device_Class_Char)
  66. {
  67. ofw_name = np->full_name;
  68. rt_strncpy(dst_con, rt_obj->name, RT_NAME_MAX);
  69. }
  70. return ofw_name;
  71. }
  72. static int tty_device_compare(rt_device_t dev, void *data)
  73. {
  74. rt_ubase_t *args_list;
  75. char *dst_con;
  76. const char *console, **ofw_name;
  77. const struct rt_ofw_node_id *id;
  78. struct rt_platform_device *pdev;
  79. int *tty_idx, tty_id, tty_name_len;
  80. pdev = rt_container_of(dev, struct rt_platform_device, parent);
  81. id = pdev->id;
  82. args_list = data;
  83. tty_idx = (int *)args_list[0];
  84. tty_id = args_list[1];
  85. console = (const char *)args_list[2];
  86. tty_name_len = args_list[3];
  87. dst_con = (char *)args_list[4];
  88. ofw_name = (const char **)args_list[5];
  89. if (id && id->type[0] && !strncmp(id->type, console, tty_name_len))
  90. {
  91. if (*tty_idx == tty_id)
  92. {
  93. *ofw_name = ofw_console_serial_find(dst_con, pdev->parent.ofw_node);
  94. return RT_EOK;
  95. }
  96. ++*tty_idx;
  97. }
  98. return -RT_EEMPTY;
  99. }
  100. static const char *ofw_console_tty_find(char *dst_con, const char *con)
  101. {
  102. const char *ofw_name = RT_NULL;
  103. static rt_bus_t platform_bus = RT_NULL;
  104. if (!platform_bus)
  105. {
  106. platform_bus = rt_bus_find_by_name("platform");
  107. }
  108. if (platform_bus)
  109. {
  110. rt_ubase_t args_list[6];
  111. const char *console = con;
  112. int tty_idx = 0, tty_id = 0, tty_name_len;
  113. con += sizeof("tty") - 1;
  114. while (*con && !(*con >= '0' && *con <= '9'))
  115. {
  116. ++con;
  117. }
  118. tty_name_len = con - console;
  119. while (*con && *con >= '0' && *con <= '9')
  120. {
  121. tty_id *= 10;
  122. tty_id += *con - '0';
  123. ++con;
  124. }
  125. args_list[0] = (rt_ubase_t)&tty_idx;
  126. args_list[1] = tty_id;
  127. args_list[2] = (rt_ubase_t)console;
  128. args_list[3] = tty_name_len;
  129. args_list[4] = (rt_ubase_t)dst_con;
  130. args_list[5] = (rt_ubase_t)&ofw_name;
  131. rt_bus_for_each_dev(platform_bus, &args_list, tty_device_compare);
  132. }
  133. return ofw_name;
  134. }
  135. rt_err_t rt_ofw_console_setup(void)
  136. {
  137. rt_err_t err = -RT_ENOSYS;
  138. char con_name[RT_NAME_MAX];
  139. const char *ofw_name = RT_NULL, *stdout_path, *con;
  140. /* chosen.console > chosen.stdout-path > RT_CONSOLE_DEVICE_NAME */
  141. con = rt_ofw_bootargs_select("console=", 0);
  142. for (int i = 1; con; ++i)
  143. {
  144. if (!rt_strncmp(con, "uart", sizeof("uart") - 1))
  145. {
  146. rt_strncpy(con_name, con, RT_NAME_MAX);
  147. err = RT_EOK;
  148. break;
  149. }
  150. else if (!rt_strncmp(con, "tty", sizeof("tty") - 1))
  151. {
  152. ofw_name = ofw_console_tty_find(con_name, con);
  153. if (ofw_name)
  154. {
  155. err = RT_EOK;
  156. break;
  157. }
  158. }
  159. con = rt_ofw_bootargs_select("console=", i);
  160. }
  161. if (err == -RT_ENOSYS && !rt_ofw_prop_read_string(ofw_node_chosen, "stdout-path", &stdout_path))
  162. {
  163. struct rt_ofw_node *stdout_np = rt_ofw_find_node_by_path(stdout_path);
  164. if (stdout_np && (ofw_name = ofw_console_serial_find(con_name, stdout_np)))
  165. {
  166. err = RT_EOK;
  167. }
  168. }
  169. if (err == -RT_ENOSYS)
  170. {
  171. rt_device_t serial = rt_device_find(RT_CONSOLE_DEVICE_NAME);
  172. if (serial)
  173. {
  174. ofw_name = rt_ofw_node_full_name(serial->ofw_node);
  175. con = RT_CONSOLE_DEVICE_NAME;
  176. }
  177. else
  178. {
  179. LOG_W("Console will probably fail to setup");
  180. }
  181. }
  182. else
  183. {
  184. con = con_name;
  185. }
  186. rt_console_set_device(con);
  187. rt_fdt_earlycon_kick(FDT_EARLYCON_KICK_COMPLETED);
  188. LOG_I("Console: %s (%s)", con, ofw_name ? ofw_name : "<unknown>");
  189. return err;
  190. }
  191. const char *rt_ofw_bootargs_select(const char *key, int index)
  192. {
  193. const char *value = RT_NULL;
  194. if (key && index >= 0)
  195. {
  196. static char **values = RT_NULL;
  197. static rt_size_t bootargs_nr = 1;
  198. const char *bootargs = RT_NULL, *ch;
  199. if (bootargs_nr && !values &&
  200. (bootargs_nr = 0, !rt_ofw_prop_read_string(ofw_node_chosen, "bootargs", &bootargs)) &&
  201. bootargs && (bootargs = rt_strdup(bootargs)))
  202. {
  203. rt_bool_t quotes = RT_TRUE;
  204. rt_size_t length = rt_strlen(bootargs);
  205. const char *bootargs_end = bootargs + length;
  206. for (ch = bootargs; ch < bootargs_end; ++ch)
  207. {
  208. if (*ch == '"')
  209. {
  210. quotes = !quotes;
  211. continue;
  212. }
  213. if (*ch != ' ' || !quotes)
  214. {
  215. continue;
  216. }
  217. ++bootargs_nr;
  218. while (*ch == ' ' && ch < bootargs_end)
  219. {
  220. *(char *)ch++ = '\0';
  221. }
  222. --ch;
  223. }
  224. if (bootargs_nr)
  225. {
  226. /* last arg */
  227. ++bootargs_nr;
  228. values = rt_malloc(sizeof(char *) * bootargs_nr);
  229. }
  230. if (values)
  231. {
  232. int idx = 0;
  233. for (int i = 0; i < length; ++i)
  234. {
  235. for (; i < length && !bootargs[i]; ++i)
  236. {
  237. }
  238. if (i < length)
  239. {
  240. values[idx++] = (char *)&bootargs[i];
  241. }
  242. for (; i < length && bootargs[i]; ++i)
  243. {
  244. }
  245. }
  246. }
  247. else
  248. {
  249. rt_free((char *)bootargs);
  250. }
  251. }
  252. if (values)
  253. {
  254. int keylen = rt_strlen(key);
  255. for (int idx = 0, count = 0; idx < bootargs_nr; ++idx)
  256. {
  257. if (!rt_strncmp(values[idx], key, keylen))
  258. {
  259. if (count == index)
  260. {
  261. value = values[idx] + keylen;
  262. break;
  263. }
  264. ++count;
  265. }
  266. }
  267. }
  268. }
  269. return value;
  270. }
  271. #ifdef RT_USING_CONSOLE
  272. static void dts_put_depth(int depth)
  273. {
  274. while (depth --> 0)
  275. {
  276. rt_kputs(" ");
  277. }
  278. }
  279. static rt_bool_t dts_test_string_list(const void *value, int size)
  280. {
  281. const char *str, *str_start, *str_end;
  282. if (!size)
  283. {
  284. return RT_FALSE;
  285. }
  286. str = value;
  287. /* String end with '\0' */
  288. if (str[size - 1] != '\0')
  289. {
  290. return RT_FALSE;
  291. }
  292. /* Get string list end */
  293. str_end = str + size;
  294. while (str < str_end)
  295. {
  296. str_start = str;
  297. /* Before string list end, not '\0' and a printable characters */
  298. while (str < str_end && *str && ((unsigned char)*str >= ' ' && (unsigned char)*str <= '~'))
  299. {
  300. ++str;
  301. }
  302. /* Not zero, or not increased */
  303. if (*str != '\0' || str == str_start)
  304. {
  305. return RT_FALSE;
  306. }
  307. /* Next string */
  308. ++str;
  309. }
  310. return RT_TRUE;
  311. }
  312. static void ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  313. {
  314. int depth = 0;
  315. struct rt_ofw_prop *prop;
  316. struct rt_ofw_node *org_np = np;
  317. while (np)
  318. {
  319. dts_put_depth(depth);
  320. rt_kputs(np->full_name);
  321. rt_kputs(" {\n");
  322. prop = np->props;
  323. /* Print prop start */
  324. ++depth;
  325. while (prop)
  326. {
  327. dts_put_depth(depth);
  328. rt_kputs(prop->name);
  329. /* Have value? */
  330. if (prop->length > 0)
  331. {
  332. int length = prop->length;
  333. void *value = prop->value;
  334. rt_kputs(" = ");
  335. if (dts_test_string_list(value, length))
  336. {
  337. /* String list */
  338. char *str = value;
  339. do {
  340. rt_kputs("\"");
  341. rt_kputs(str);
  342. rt_kputs("\", ");
  343. str += rt_strlen(str) + 1;
  344. } while (str < (char *)value + length);
  345. rt_kputs("\b\b");
  346. }
  347. else if ((length % 4) == 0)
  348. {
  349. /* u32 data in <?> */
  350. fdt32_t *cell = value;
  351. rt_kputs("<");
  352. length /= 4;
  353. for (int i = 0; i < length; ++i)
  354. {
  355. rt_kprintf("0x%02x ", fdt32_to_cpu(cell[i]));
  356. }
  357. rt_kputs("\b>");
  358. }
  359. else
  360. {
  361. /* Byte data in [?] */
  362. rt_uint8_t *byte = value;
  363. rt_kputs("[");
  364. for (int i = 0; i < length; ++i)
  365. {
  366. rt_kprintf("%02x ", *byte++);
  367. }
  368. rt_kputs("\b]");
  369. }
  370. }
  371. rt_kputs(";\n");
  372. prop = prop->next;
  373. }
  374. --depth;
  375. /* Print prop end */
  376. if (np->child)
  377. {
  378. rt_kputs("\n");
  379. np = np->child;
  380. ++depth;
  381. }
  382. else
  383. {
  384. dts_put_depth(depth);
  385. rt_kputs("};\n");
  386. if (!sibling_too && org_np == np)
  387. {
  388. break;
  389. }
  390. while (np->parent && !np->sibling)
  391. {
  392. np = np->parent;
  393. --depth;
  394. if (depth >= 0)
  395. {
  396. dts_put_depth(depth);
  397. rt_kputs("};\n");
  398. }
  399. }
  400. if (!sibling_too && org_np == np)
  401. {
  402. break;
  403. }
  404. np = np->sibling;
  405. if (np)
  406. {
  407. rt_kputs("\n");
  408. }
  409. }
  410. }
  411. }
  412. void rt_ofw_node_dump_dts(struct rt_ofw_node *np, rt_bool_t sibling_too)
  413. {
  414. if (np)
  415. {
  416. if (!rt_strcmp(np->name, "/"))
  417. {
  418. struct fdt_info *header = (struct fdt_info *)np->name;
  419. struct fdt_reserve_entry *rsvmap = header->rsvmap;
  420. rt_kprintf("/dts-v%x/;\n\n", fdt_version(header->fdt));
  421. for (int i = header->rsvmap_nr - 1; i >= 0; --i)
  422. {
  423. rt_kprintf("/memreserve/\t%p %p;\n",
  424. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->address)),
  425. ofw_static_cast(rt_size_t, fdt64_to_cpu(rsvmap->size)));
  426. ++rsvmap;
  427. }
  428. rt_kputs(np->name);
  429. }
  430. ofw_node_dump_dts(np, sibling_too);
  431. }
  432. }
  433. #ifdef RT_USING_MSH
  434. static void ofw_dts(int argc, char**argv)
  435. {
  436. if (ofw_node_root)
  437. {
  438. if (argc == 1)
  439. {
  440. rt_ofw_node_dump_dts(ofw_node_root, RT_TRUE);
  441. }
  442. else if (argv[1][0] == '/')
  443. {
  444. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[1]);
  445. if (np)
  446. {
  447. rt_ofw_node_dump_dts(np, RT_FALSE);
  448. }
  449. else
  450. {
  451. rt_kprintf("%s not found.\n", argv[1]);
  452. }
  453. }
  454. else if (argc >= 2 && !rt_strcmp(argv[1], "list") && argv[2][0] == '/')
  455. {
  456. struct rt_ofw_node *np = rt_ofw_find_node_by_path(argv[2]);
  457. if (np)
  458. {
  459. const char *split = np == ofw_node_root ? "" : "/";
  460. np = np->child;
  461. while (np)
  462. {
  463. rt_kprintf("%s%s%s\n", argv[2], split, np->full_name);
  464. np = np->sibling;
  465. }
  466. }
  467. else
  468. {
  469. rt_kprintf("%s not found.\n", argv[2]);
  470. }
  471. }
  472. else
  473. {
  474. rt_kprintf("Usage: %s {list} {path}\n", __func__);
  475. }
  476. }
  477. else
  478. {
  479. rt_kprintf("\"/\" path not found.");
  480. }
  481. }
  482. MSH_CMD_EXPORT(ofw_dts, dump the dts or node for this platform);
  483. #endif /* RT_USING_MSH */
  484. #endif /* RT_USING_CONSOLE */