serial.c 6.5 KB

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