serial.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2012 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/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/i18n.h>
  25. #include <grub/ieee1275/console.h>
  26. struct ofserial_hash_ent
  27. {
  28. char *devpath;
  29. /* Pointer to shortest available name on nodes representing canonical names,
  30. otherwise NULL. */
  31. const char *shortest;
  32. struct ofserial_hash_ent *next;
  33. };
  34. static void
  35. do_real_config (struct grub_serial_port *port)
  36. {
  37. if (port->configured)
  38. return;
  39. if (grub_ieee1275_open (port->elem->devpath, &port->handle)
  40. || port->handle == (grub_ieee1275_ihandle_t) -1)
  41. port->handle = GRUB_IEEE1275_IHANDLE_INVALID;
  42. port->configured = 1;
  43. }
  44. /* Fetch a key. */
  45. static int
  46. serial_hw_fetch (struct grub_serial_port *port)
  47. {
  48. grub_ssize_t actual;
  49. char c;
  50. do_real_config (port);
  51. if (port->handle == GRUB_IEEE1275_IHANDLE_INVALID)
  52. return -1;
  53. grub_ieee1275_read (port->handle, &c, 1, &actual);
  54. if (actual <= 0)
  55. return -1;
  56. return c;
  57. }
  58. /* Put a character. */
  59. static void
  60. serial_hw_put (struct grub_serial_port *port, const int c)
  61. {
  62. grub_ssize_t actual;
  63. char c0 = c;
  64. do_real_config (port);
  65. if (port->handle == GRUB_IEEE1275_IHANDLE_INVALID)
  66. return;
  67. grub_ieee1275_write (port->handle, &c0, 1, &actual);
  68. }
  69. /* Initialize a serial device. PORT is the port number for a serial device.
  70. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  71. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  72. for the device. Likewise, PARITY is the type of the parity and
  73. STOP_BIT_LEN is the length of the stop bit. The possible values for
  74. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  75. macros. */
  76. static grub_err_t
  77. serial_hw_configure (struct grub_serial_port *port __attribute__ ((unused)),
  78. struct grub_serial_config *config __attribute__ ((unused)))
  79. {
  80. /* FIXME: no IEEE1275 serial config available. */
  81. return GRUB_ERR_NONE;
  82. }
  83. struct grub_serial_driver grub_ofserial_driver =
  84. {
  85. .configure = serial_hw_configure,
  86. .fetch = serial_hw_fetch,
  87. .put = serial_hw_put
  88. };
  89. #define OFSERIAL_HASH_SZ 8
  90. static struct ofserial_hash_ent *ofserial_hash[OFSERIAL_HASH_SZ];
  91. static int
  92. ofserial_hash_fn (const char *devpath)
  93. {
  94. int hash = 0;
  95. while (*devpath)
  96. hash ^= *devpath++;
  97. return (hash & (OFSERIAL_HASH_SZ - 1));
  98. }
  99. static struct ofserial_hash_ent *
  100. ofserial_hash_find (const char *devpath)
  101. {
  102. struct ofserial_hash_ent *p = ofserial_hash[ofserial_hash_fn(devpath)];
  103. while (p)
  104. {
  105. if (!grub_strcmp (p->devpath, devpath))
  106. break;
  107. p = p->next;
  108. }
  109. return p;
  110. }
  111. static struct ofserial_hash_ent *
  112. ofserial_hash_add_real (char *devpath)
  113. {
  114. struct ofserial_hash_ent *p;
  115. struct ofserial_hash_ent **head = &ofserial_hash[ofserial_hash_fn(devpath)];
  116. p = grub_malloc(sizeof (*p));
  117. if (!p)
  118. return NULL;
  119. p->devpath = devpath;
  120. p->next = *head;
  121. p->shortest = 0;
  122. *head = p;
  123. return p;
  124. }
  125. static struct ofserial_hash_ent *
  126. ofserial_hash_add (char *devpath, char *curcan)
  127. {
  128. struct ofserial_hash_ent *p, *pcan;
  129. p = ofserial_hash_add_real (devpath);
  130. grub_dprintf ("serial", "devpath = %s, canonical = %s\n", devpath, curcan);
  131. if (!curcan)
  132. {
  133. p->shortest = devpath;
  134. return p;
  135. }
  136. pcan = ofserial_hash_find (curcan);
  137. if (!pcan)
  138. pcan = ofserial_hash_add_real (curcan);
  139. else
  140. grub_free (curcan);
  141. if (!pcan)
  142. grub_errno = GRUB_ERR_NONE;
  143. else
  144. {
  145. if (!pcan->shortest
  146. || grub_strlen (pcan->shortest) > grub_strlen (devpath))
  147. pcan->shortest = devpath;
  148. }
  149. return p;
  150. }
  151. static void
  152. dev_iterate_real (struct grub_ieee1275_devalias *alias,
  153. int use_name)
  154. {
  155. struct ofserial_hash_ent *op;
  156. if (grub_strcmp (alias->type, "serial") != 0)
  157. return;
  158. grub_dprintf ("serial", "serial name = %s, path = %s\n", alias->name,
  159. alias->path);
  160. op = ofserial_hash_find (alias->path);
  161. if (!op)
  162. {
  163. char *name = grub_strdup (use_name ? alias->name : alias->path);
  164. char *can = grub_strdup (alias->path);
  165. if (!name || !can)
  166. {
  167. grub_errno = GRUB_ERR_NONE;
  168. grub_free (name);
  169. grub_free (can);
  170. return;
  171. }
  172. op = ofserial_hash_add (name, can);
  173. }
  174. return;
  175. }
  176. static int
  177. dev_iterate (struct grub_ieee1275_devalias *alias)
  178. {
  179. dev_iterate_real (alias, 0);
  180. return 0;
  181. }
  182. static struct grub_serial_port *
  183. add_port (struct ofserial_hash_ent *ent)
  184. {
  185. struct grub_serial_port *port;
  186. char *ptr;
  187. grub_err_t err;
  188. if (!ent->shortest)
  189. return NULL;
  190. FOR_SERIAL_PORTS (port)
  191. if (port->elem == ent)
  192. return port;
  193. port = grub_zalloc (sizeof (*port));
  194. if (!port)
  195. return NULL;
  196. port->name = grub_malloc (sizeof ("ieee1275/")
  197. + grub_strlen (ent->shortest));
  198. port->elem = ent;
  199. if (!port->name)
  200. return NULL;
  201. ptr = grub_stpcpy (port->name, "ieee1275/");
  202. grub_strcpy (ptr, ent->shortest);
  203. port->driver = &grub_ofserial_driver;
  204. err = grub_serial_config_defaults (port);
  205. if (err)
  206. grub_print_error ();
  207. grub_serial_register (port);
  208. return port;
  209. }
  210. struct grub_serial_port *
  211. grub_ofserial_add_port (const char *path)
  212. {
  213. struct ofserial_hash_ent *ent;
  214. char *name = grub_strdup (path);
  215. char *can = grub_strdup (path);
  216. if (!name || ! can)
  217. {
  218. grub_free (name);
  219. grub_free (can);
  220. return NULL;
  221. }
  222. ent = ofserial_hash_add (name, can);
  223. return add_port (ent);
  224. }
  225. void
  226. grub_ofserial_init (void)
  227. {
  228. unsigned i;
  229. struct grub_ieee1275_devalias alias;
  230. FOR_IEEE1275_DEVALIASES(alias)
  231. dev_iterate_real (&alias, 1);
  232. grub_ieee1275_devices_iterate (dev_iterate);
  233. for (i = 0; i < ARRAY_SIZE (ofserial_hash); i++)
  234. {
  235. struct ofserial_hash_ent *ent;
  236. for (ent = ofserial_hash[i]; ent; ent = ent->next)
  237. add_port (ent);
  238. }
  239. }