ofw.c 13 KB

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