uboot.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 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/misc.h>
  19. #include <grub/mm.h>
  20. #include <grub/uboot/api_public.h>
  21. #include <grub/uboot/uboot.h>
  22. /*
  23. * The main syscall entry point is not reentrant, only one call is
  24. * serviced until finished.
  25. *
  26. * int syscall(int call, int *retval, ...)
  27. * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
  28. *
  29. * call: syscall number
  30. *
  31. * retval: points to the return value placeholder, this is the place the
  32. * syscall puts its return value, if NULL the caller does not
  33. * expect a return value
  34. *
  35. * ... syscall arguments (variable number)
  36. *
  37. * returns: 0 if the call not found, 1 if serviced
  38. */
  39. extern int grub_uboot_syscall (int, int *, ...);
  40. static struct sys_info uboot_sys_info;
  41. static struct mem_region uboot_mem_info[5];
  42. static struct device_info * devices;
  43. static int num_devices;
  44. /*
  45. * All functions below are wrappers around the grub_uboot_syscall() function
  46. */
  47. int
  48. grub_uboot_getc (void)
  49. {
  50. int c;
  51. if (!grub_uboot_syscall (API_GETC, NULL, &c))
  52. return -1;
  53. return c;
  54. }
  55. int
  56. grub_uboot_tstc (void)
  57. {
  58. int c;
  59. if (!grub_uboot_syscall (API_TSTC, NULL, &c))
  60. return -1;
  61. return c;
  62. }
  63. void
  64. grub_uboot_putc (int c)
  65. {
  66. grub_uboot_syscall (API_PUTC, NULL, &c);
  67. }
  68. void
  69. grub_uboot_puts (const char *s)
  70. {
  71. grub_uboot_syscall (API_PUTS, NULL, s);
  72. }
  73. void
  74. grub_uboot_reset (void)
  75. {
  76. grub_uboot_syscall (API_RESET, NULL, 0);
  77. }
  78. struct sys_info *
  79. grub_uboot_get_sys_info (void)
  80. {
  81. int retval;
  82. grub_memset (&uboot_sys_info, 0, sizeof (uboot_sys_info));
  83. grub_memset (&uboot_mem_info, 0, sizeof (uboot_mem_info));
  84. uboot_sys_info.mr = uboot_mem_info;
  85. uboot_sys_info.mr_no = sizeof (uboot_mem_info) / sizeof (struct mem_region);
  86. if (grub_uboot_syscall (API_GET_SYS_INFO, &retval, &uboot_sys_info))
  87. if (retval == 0)
  88. return &uboot_sys_info;
  89. return NULL;
  90. }
  91. void
  92. grub_uboot_udelay (grub_uint32_t usec)
  93. {
  94. grub_uboot_syscall (API_UDELAY, NULL, &usec);
  95. }
  96. grub_uint32_t
  97. grub_uboot_get_timer (grub_uint32_t base)
  98. {
  99. grub_uint32_t current;
  100. if (!grub_uboot_syscall (API_GET_TIMER, NULL, &current, &base))
  101. return 0;
  102. return current;
  103. }
  104. int
  105. grub_uboot_dev_enum (void)
  106. {
  107. struct device_info * enum_devices;
  108. int num_enum_devices, max_devices;
  109. if (num_devices)
  110. return num_devices;
  111. max_devices = 2;
  112. enum_devices = grub_malloc (sizeof(struct device_info) * max_devices);
  113. if (!enum_devices)
  114. return 0;
  115. /*
  116. * The API_DEV_ENUM call starts a fresh enumeration when passed a
  117. * struct device_info with a NULL cookie, and then depends on having
  118. * the previously enumerated device cookie "seeded" into the target
  119. * structure.
  120. */
  121. enum_devices[0].cookie = NULL;
  122. num_enum_devices = 0;
  123. if (grub_uboot_syscall (API_DEV_ENUM, NULL,
  124. &enum_devices[num_enum_devices]) == 0)
  125. goto error;
  126. num_enum_devices++;
  127. while (enum_devices[num_enum_devices - 1].cookie != NULL)
  128. {
  129. if (num_enum_devices == max_devices)
  130. {
  131. struct device_info *tmp;
  132. int new_max;
  133. new_max = max_devices * 2;
  134. tmp = grub_realloc (enum_devices,
  135. sizeof (struct device_info) * new_max);
  136. if (!tmp)
  137. {
  138. /* Failed to realloc, so return what we have */
  139. break;
  140. }
  141. enum_devices = tmp;
  142. max_devices = new_max;
  143. }
  144. enum_devices[num_enum_devices].cookie =
  145. enum_devices[num_enum_devices - 1].cookie;
  146. if (grub_uboot_syscall (API_DEV_ENUM, NULL,
  147. &enum_devices[num_enum_devices]) == 0)
  148. goto error;
  149. if (enum_devices[num_enum_devices].cookie == NULL)
  150. break;
  151. num_enum_devices++;
  152. }
  153. devices = enum_devices;
  154. return num_devices = num_enum_devices;
  155. error:
  156. grub_free (enum_devices);
  157. return 0;
  158. }
  159. #define VALID_DEV(x) (((x) < num_devices) && ((x) >= 0))
  160. #define OPEN_DEV(x) ((x->state == DEV_STA_OPEN))
  161. struct device_info *
  162. grub_uboot_dev_get (int index)
  163. {
  164. if (VALID_DEV (index))
  165. return &devices[index];
  166. return NULL;
  167. }
  168. int
  169. grub_uboot_dev_open (struct device_info *dev)
  170. {
  171. int retval;
  172. if (!grub_uboot_syscall (API_DEV_OPEN, &retval, dev))
  173. return -1;
  174. return retval;
  175. }
  176. int
  177. grub_uboot_dev_close (struct device_info *dev)
  178. {
  179. int retval;
  180. if (!grub_uboot_syscall (API_DEV_CLOSE, &retval, dev))
  181. return -1;
  182. return retval;
  183. }
  184. int
  185. grub_uboot_dev_read (struct device_info *dev, void *buf, grub_size_t blocks,
  186. grub_uint32_t start, grub_size_t * real_blocks)
  187. {
  188. int retval;
  189. if (!OPEN_DEV (dev))
  190. return -1;
  191. if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf,
  192. &blocks, &start, real_blocks))
  193. return -1;
  194. return retval;
  195. }
  196. int
  197. grub_uboot_dev_recv (struct device_info *dev, void *buf,
  198. int size, int *real_size)
  199. {
  200. int retval;
  201. if (!OPEN_DEV (dev))
  202. return -1;
  203. if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf, &size, real_size))
  204. return -1;
  205. return retval;
  206. }
  207. int
  208. grub_uboot_dev_send (struct device_info *dev, void *buf, int size)
  209. {
  210. int retval;
  211. if (!OPEN_DEV (dev))
  212. return -1;
  213. if (!grub_uboot_syscall (API_DEV_WRITE, &retval, dev, buf, &size))
  214. return -1;
  215. return retval;
  216. }
  217. char *
  218. grub_uboot_env_get (const char *name)
  219. {
  220. char *value;
  221. if (!grub_uboot_syscall (API_ENV_GET, NULL, name, &value))
  222. return NULL;
  223. return value;
  224. }
  225. void
  226. grub_uboot_env_set (const char *name, const char *value)
  227. {
  228. grub_uboot_syscall (API_ENV_SET, NULL, name, value);
  229. }