ofw.c 13 KB

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