serial.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/serial.h>
  19. #include <grub/term.h>
  20. #include <grub/types.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/terminfo.h>
  24. #if !defined (GRUB_MACHINE_EMU) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
  25. #include <grub/cpu/io.h>
  26. #endif
  27. #include <grub/extcmd.h>
  28. #include <grub/i18n.h>
  29. #include <grub/list.h>
  30. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  31. #include <grub/machine/kernel.h>
  32. #endif
  33. #ifdef GRUB_MACHINE_IEEE1275
  34. #include <grub/ieee1275/console.h>
  35. #endif
  36. GRUB_MOD_LICENSE ("GPLv3+");
  37. enum
  38. {
  39. OPTION_UNIT,
  40. OPTION_PORT,
  41. OPTION_SPEED,
  42. OPTION_WORD,
  43. OPTION_PARITY,
  44. OPTION_STOP,
  45. OPTION_BASE_CLOCK,
  46. OPTION_RTSCTS
  47. };
  48. /* Argument options. */
  49. static const struct grub_arg_option options[] =
  50. {
  51. {"unit", 'u', 0, N_("Set the serial unit."), 0, ARG_TYPE_INT},
  52. {"port", 'p', 0, N_("Set the serial port address."), 0, ARG_TYPE_STRING},
  53. {"speed", 's', 0, N_("Set the serial port speed."), 0, ARG_TYPE_INT},
  54. {"word", 'w', 0, N_("Set the serial port word length."), 0, ARG_TYPE_INT},
  55. {"parity", 'r', 0, N_("Set the serial port parity."), 0, ARG_TYPE_STRING},
  56. {"stop", 't', 0, N_("Set the serial port stop bits."), 0, ARG_TYPE_INT},
  57. {"base-clock", 'b', 0, N_("Set the base frequency."), 0, ARG_TYPE_STRING},
  58. {"rtscts", 'f', 0, N_("Enable/disable RTS/CTS."), "on|off", ARG_TYPE_STRING},
  59. {0, 0, 0, 0, 0, 0}
  60. };
  61. struct grub_serial_port *grub_serial_ports;
  62. struct grub_serial_output_state
  63. {
  64. struct grub_terminfo_output_state tinfo;
  65. struct grub_serial_port *port;
  66. };
  67. struct grub_serial_input_state
  68. {
  69. struct grub_terminfo_input_state tinfo;
  70. struct grub_serial_port *port;
  71. };
  72. static void
  73. serial_put (grub_term_output_t term, const int c)
  74. {
  75. struct grub_serial_output_state *data = term->data;
  76. data->port->driver->put (data->port, c);
  77. }
  78. static int
  79. serial_fetch (grub_term_input_t term)
  80. {
  81. struct grub_serial_input_state *data = term->data;
  82. return data->port->driver->fetch (data->port);
  83. }
  84. static const struct grub_serial_input_state grub_serial_terminfo_input_template =
  85. {
  86. .tinfo =
  87. {
  88. .readkey = serial_fetch
  89. }
  90. };
  91. static const struct grub_serial_output_state grub_serial_terminfo_output_template =
  92. {
  93. .tinfo =
  94. {
  95. .put = serial_put,
  96. .size = { 80, 24 }
  97. }
  98. };
  99. static struct grub_serial_input_state grub_serial_terminfo_input;
  100. static struct grub_serial_output_state grub_serial_terminfo_output;
  101. static int registered = 0;
  102. static struct grub_term_input grub_serial_term_input =
  103. {
  104. .name = "serial",
  105. .init = grub_terminfo_input_init,
  106. .getkey = grub_terminfo_getkey,
  107. .data = &grub_serial_terminfo_input
  108. };
  109. static struct grub_term_output grub_serial_term_output =
  110. {
  111. .name = "serial",
  112. .init = grub_terminfo_output_init,
  113. .putchar = grub_terminfo_putchar,
  114. .getwh = grub_terminfo_getwh,
  115. .getxy = grub_terminfo_getxy,
  116. .gotoxy = grub_terminfo_gotoxy,
  117. .cls = grub_terminfo_cls,
  118. .setcolorstate = grub_terminfo_setcolorstate,
  119. .setcursor = grub_terminfo_setcursor,
  120. .flags = GRUB_TERM_CODE_TYPE_ASCII,
  121. .data = &grub_serial_terminfo_output,
  122. .progress_update_divisor = GRUB_PROGRESS_SLOW
  123. };
  124. struct grub_serial_port *
  125. grub_serial_find (const char *name)
  126. {
  127. struct grub_serial_port *port;
  128. /*
  129. * First look for an exact match by name, this will take care of
  130. * things like "com0" which have already been created and that
  131. * this function cannot re-create.
  132. */
  133. FOR_SERIAL_PORTS (port)
  134. if (grub_strcmp (port->name, name) == 0)
  135. return port;
  136. #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC)
  137. if (grub_strncmp (name, "port", sizeof ("port") - 1) == 0
  138. && grub_isxdigit (name [sizeof ("port") - 1]))
  139. {
  140. port = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
  141. 0, 16), NULL);
  142. if (port != NULL)
  143. return port;
  144. }
  145. if (grub_strncmp (name, "mmio,", sizeof ("mmio,") - 1) == 0
  146. && grub_isxdigit (name [sizeof ("mmio,") - 1]))
  147. {
  148. const char *p1, *p = &name[sizeof ("mmio,") - 1];
  149. grub_addr_t addr = grub_strtoul (p, &p1, 16);
  150. unsigned int acc_size = 1;
  151. /*
  152. * If we reach here, we know there's a digit after "mmio,", so
  153. * all we need to check is the validity of the character following
  154. * the number, which should be a termination, or a dot followed by
  155. * an access size.
  156. */
  157. if (p1[0] != '\0' && p1[0] != '.')
  158. {
  159. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("incorrect MMIO address syntax"));
  160. return NULL;
  161. }
  162. if (p1[0] == '.')
  163. switch(p1[1])
  164. {
  165. case 'w':
  166. acc_size = 2;
  167. break;
  168. case 'l':
  169. acc_size = 3;
  170. break;
  171. case 'q':
  172. acc_size = 4;
  173. break;
  174. case 'b':
  175. acc_size = 1;
  176. break;
  177. default:
  178. /*
  179. * Should we abort for an unknown size? Let's just default
  180. * to 1 byte, it would increase the chance that the user who
  181. * did a typo can actually see the console.
  182. */
  183. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("incorrect MMIO access size"));
  184. }
  185. port = grub_serial_ns8250_add_mmio (addr, acc_size, NULL);
  186. if (port != NULL)
  187. return port;
  188. }
  189. #if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
  190. if (grub_strcmp (name, "auto") == 0)
  191. {
  192. /* Look for an SPCR if any. If not, default to com0. */
  193. port = grub_ns8250_spcr_init ();
  194. if (port != NULL)
  195. return port;
  196. FOR_SERIAL_PORTS (port)
  197. if (grub_strcmp (port->name, "com0") == 0)
  198. return port;
  199. }
  200. #endif
  201. #endif
  202. #ifdef GRUB_MACHINE_IEEE1275
  203. if (grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0)
  204. {
  205. port = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
  206. if (port != NULL)
  207. return port;
  208. }
  209. #endif
  210. return NULL;
  211. }
  212. static grub_err_t
  213. grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
  214. {
  215. struct grub_arg_list *state = ctxt->state;
  216. char pname[40];
  217. const char *name = NULL;
  218. struct grub_serial_port *port;
  219. struct grub_serial_config config;
  220. grub_err_t err;
  221. if (state[OPTION_UNIT].set)
  222. {
  223. grub_snprintf (pname, sizeof (pname), "com%ld",
  224. grub_strtoul (state[0].arg, 0, 0));
  225. name = pname;
  226. }
  227. if (state[OPTION_PORT].set)
  228. {
  229. if (grub_strncmp (state[OPTION_PORT].arg, "mmio,", sizeof ("mmio,") - 1) == 0 ||
  230. grub_strncmp (state[OPTION_PORT].arg, "pci,", sizeof ("pci,") - 1) == 0)
  231. {
  232. grub_strncpy (pname, state[1].arg, sizeof (pname));
  233. pname[sizeof (pname) - 1] = '\0';
  234. }
  235. else
  236. grub_snprintf (pname, sizeof (pname), "port%lx",
  237. grub_strtoul (state[1].arg, 0, 0));
  238. name = pname;
  239. }
  240. if (argc >= 1)
  241. name = args[0];
  242. if (!name)
  243. name = "auto";
  244. port = grub_serial_find (name);
  245. if (!port)
  246. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  247. N_("serial port `%s' isn't found"),
  248. name);
  249. config = port->config;
  250. if (state[OPTION_SPEED].set) {
  251. config.speed = grub_strtoul (state[OPTION_SPEED].arg, 0, 0);
  252. if (config.speed == 0)
  253. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  254. N_("unsupported serial port parity"));
  255. }
  256. if (state[OPTION_WORD].set)
  257. config.word_len = grub_strtoul (state[OPTION_WORD].arg, 0, 0);
  258. if (state[OPTION_PARITY].set)
  259. {
  260. if (! grub_strcmp (state[OPTION_PARITY].arg, "no"))
  261. config.parity = GRUB_SERIAL_PARITY_NONE;
  262. else if (! grub_strcmp (state[OPTION_PARITY].arg, "odd"))
  263. config.parity = GRUB_SERIAL_PARITY_ODD;
  264. else if (! grub_strcmp (state[OPTION_PARITY].arg, "even"))
  265. config.parity = GRUB_SERIAL_PARITY_EVEN;
  266. else
  267. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  268. N_("unsupported serial port parity"));
  269. }
  270. if (state[OPTION_RTSCTS].set)
  271. {
  272. if (grub_strcmp (state[OPTION_RTSCTS].arg, "on") == 0)
  273. config.rtscts = 1;
  274. else if (grub_strcmp (state[OPTION_RTSCTS].arg, "off") == 0)
  275. config.rtscts = 0;
  276. else
  277. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  278. N_("unsupported serial port flow control"));
  279. }
  280. if (state[OPTION_STOP].set)
  281. {
  282. if (! grub_strcmp (state[OPTION_STOP].arg, "1"))
  283. config.stop_bits = GRUB_SERIAL_STOP_BITS_1;
  284. else if (! grub_strcmp (state[OPTION_STOP].arg, "2"))
  285. config.stop_bits = GRUB_SERIAL_STOP_BITS_2;
  286. else if (! grub_strcmp (state[OPTION_STOP].arg, "1.5"))
  287. config.stop_bits = GRUB_SERIAL_STOP_BITS_1_5;
  288. else
  289. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  290. N_("unsupported serial port stop bits number"));
  291. }
  292. if (state[OPTION_BASE_CLOCK].set)
  293. {
  294. const char *ptr;
  295. config.base_clock = grub_strtoull (state[OPTION_BASE_CLOCK].arg, &ptr, 0);
  296. if (grub_errno)
  297. return grub_errno;
  298. if (ptr && *ptr == 'M')
  299. config.base_clock *= 1000000;
  300. if (ptr && (*ptr == 'k' || *ptr == 'K'))
  301. config.base_clock *= 1000;
  302. }
  303. if (config.speed == 0)
  304. config.speed = 9600;
  305. /* Initialize with new settings. */
  306. err = port->driver->configure (port, &config);
  307. if (err)
  308. return err;
  309. #if !defined (GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
  310. /* Compatibility kludge. */
  311. if (port->driver == &grub_ns8250_driver)
  312. {
  313. if (!registered)
  314. {
  315. grub_terminfo_output_register (&grub_serial_term_output, "vt100");
  316. grub_term_register_input ("serial", &grub_serial_term_input);
  317. grub_term_register_output ("serial", &grub_serial_term_output);
  318. }
  319. grub_serial_terminfo_output.port = port;
  320. grub_serial_terminfo_input.port = port;
  321. registered = 1;
  322. }
  323. #endif
  324. return GRUB_ERR_NONE;
  325. }
  326. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  327. const char loongson_defserial[][6] =
  328. {
  329. [GRUB_ARCH_MACHINE_YEELOONG] = "com0",
  330. [GRUB_ARCH_MACHINE_FULOONG2F] = "com2",
  331. [GRUB_ARCH_MACHINE_FULOONG2E] = "com1"
  332. };
  333. #endif
  334. grub_err_t
  335. grub_serial_register (struct grub_serial_port *port)
  336. {
  337. struct grub_term_input *in;
  338. struct grub_term_output *out;
  339. struct grub_serial_input_state *indata;
  340. struct grub_serial_output_state *outdata;
  341. in = grub_malloc (sizeof (*in));
  342. if (!in)
  343. return grub_errno;
  344. indata = grub_malloc (sizeof (*indata));
  345. if (!indata)
  346. {
  347. grub_free (in);
  348. return grub_errno;
  349. }
  350. grub_memcpy (in, &grub_serial_term_input, sizeof (*in));
  351. in->data = indata;
  352. in->name = grub_xasprintf ("serial_%s", port->name);
  353. grub_memcpy (indata, &grub_serial_terminfo_input, sizeof (*indata));
  354. if (!in->name)
  355. {
  356. grub_free (in);
  357. grub_free (indata);
  358. return grub_errno;
  359. }
  360. out = grub_zalloc (sizeof (*out));
  361. if (!out)
  362. {
  363. grub_free (indata);
  364. grub_free ((char *) in->name);
  365. grub_free (in);
  366. return grub_errno;
  367. }
  368. outdata = grub_malloc (sizeof (*outdata));
  369. if (!outdata)
  370. {
  371. grub_free (indata);
  372. grub_free ((char *) in->name);
  373. grub_free (out);
  374. grub_free (in);
  375. return grub_errno;
  376. }
  377. grub_memcpy (out, &grub_serial_term_output, sizeof (*out));
  378. out->data = outdata;
  379. out->name = in->name;
  380. grub_memcpy (outdata, &grub_serial_terminfo_output, sizeof (*outdata));
  381. grub_list_push (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
  382. ((struct grub_serial_input_state *) in->data)->port = port;
  383. ((struct grub_serial_output_state *) out->data)->port = port;
  384. port->term_in = in;
  385. port->term_out = out;
  386. grub_terminfo_output_register (out, "vt100");
  387. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  388. if (grub_strcmp (port->name, loongson_defserial[grub_arch_machine]) == 0)
  389. {
  390. grub_term_register_input_active ("serial_*", in);
  391. grub_term_register_output_active ("serial_*", out);
  392. }
  393. else
  394. {
  395. grub_term_register_input_inactive ("serial_*", in);
  396. grub_term_register_output_inactive ("serial_*", out);
  397. }
  398. #else
  399. grub_term_register_input ("serial_*", in);
  400. grub_term_register_output ("serial_*", out);
  401. #endif
  402. return GRUB_ERR_NONE;
  403. }
  404. void
  405. grub_serial_unregister (struct grub_serial_port *port)
  406. {
  407. if (port->driver->fini)
  408. port->driver->fini (port);
  409. if (port->term_in)
  410. grub_term_unregister_input (port->term_in);
  411. if (port->term_out)
  412. grub_term_unregister_output (port->term_out);
  413. grub_list_remove (GRUB_AS_LIST (port));
  414. }
  415. void
  416. grub_serial_unregister_driver (struct grub_serial_driver *driver)
  417. {
  418. struct grub_serial_port *port, *next;
  419. for (port = grub_serial_ports; port; port = next)
  420. {
  421. next = port->next;
  422. if (port->driver == driver)
  423. grub_serial_unregister (port);
  424. }
  425. }
  426. static grub_extcmd_t cmd;
  427. GRUB_MOD_INIT(serial)
  428. {
  429. cmd = grub_register_extcmd ("serial", grub_cmd_serial, 0,
  430. N_("[OPTIONS...]"),
  431. N_("Configure serial port."), options);
  432. grub_memcpy (&grub_serial_terminfo_output,
  433. &grub_serial_terminfo_output_template,
  434. sizeof (grub_serial_terminfo_output));
  435. grub_memcpy (&grub_serial_terminfo_input,
  436. &grub_serial_terminfo_input_template,
  437. sizeof (grub_serial_terminfo_input));
  438. #if !defined (GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
  439. grub_ns8250_init ();
  440. #endif
  441. #ifdef GRUB_MACHINE_IEEE1275
  442. grub_ofserial_init ();
  443. #endif
  444. #ifdef GRUB_MACHINE_EFI
  445. grub_efiserial_init ();
  446. #endif
  447. #ifdef GRUB_MACHINE_ARC
  448. grub_arcserial_init ();
  449. #endif
  450. #if defined(__i386__) || defined(__x86_64__)
  451. grub_pciserial_init ();
  452. #endif
  453. }
  454. GRUB_MOD_FINI(serial)
  455. {
  456. while (grub_serial_ports)
  457. grub_serial_unregister (grub_serial_ports);
  458. if (registered)
  459. {
  460. grub_term_unregister_input (&grub_serial_term_input);
  461. grub_term_unregister_output (&grub_serial_term_output);
  462. }
  463. grub_unregister_extcmd (cmd);
  464. }