uboot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_ptr) (int, int *, ...);
  40. extern int grub_uboot_syscall (int, int *, ...);
  41. extern grub_addr_t grub_uboot_search_hint;
  42. static struct sys_info uboot_sys_info;
  43. static struct mem_region uboot_mem_info[5];
  44. static struct device_info * devices;
  45. static int num_devices;
  46. int
  47. grub_uboot_api_init (void)
  48. {
  49. struct api_signature *start, *end;
  50. struct api_signature *p;
  51. if (grub_uboot_search_hint)
  52. {
  53. /* Extended search range to work around Trim Slice U-Boot issue */
  54. start = (struct api_signature *) ((grub_uboot_search_hint & ~0x000fffff)
  55. - 0x00500000);
  56. end =
  57. (struct api_signature *) ((grub_addr_t) start + UBOOT_API_SEARCH_LEN -
  58. API_SIG_MAGLEN + 0x00500000);
  59. }
  60. else
  61. {
  62. start = 0;
  63. end = (struct api_signature *) (256 * 1024 * 1024);
  64. }
  65. /* Structure alignment is (at least) 8 bytes */
  66. for (p = start; p < end; p = (void *) ((grub_addr_t) p + 8))
  67. {
  68. if (grub_memcmp (&(p->magic), API_SIG_MAGIC, API_SIG_MAGLEN) == 0)
  69. {
  70. grub_uboot_syscall_ptr = p->syscall;
  71. return p->version;
  72. }
  73. }
  74. return 0;
  75. }
  76. /*
  77. * All functions below are wrappers around the grub_uboot_syscall() function
  78. */
  79. int
  80. grub_uboot_getc (void)
  81. {
  82. int c;
  83. if (!grub_uboot_syscall (API_GETC, NULL, &c))
  84. return -1;
  85. return c;
  86. }
  87. int
  88. grub_uboot_tstc (void)
  89. {
  90. int c;
  91. if (!grub_uboot_syscall (API_TSTC, NULL, &c))
  92. return -1;
  93. return c;
  94. }
  95. void
  96. grub_uboot_putc (int c)
  97. {
  98. grub_uboot_syscall (API_PUTC, NULL, &c);
  99. }
  100. void
  101. grub_uboot_puts (const char *s)
  102. {
  103. grub_uboot_syscall (API_PUTS, NULL, s);
  104. }
  105. void
  106. grub_uboot_reset (void)
  107. {
  108. grub_uboot_syscall (API_RESET, NULL, 0);
  109. }
  110. struct sys_info *
  111. grub_uboot_get_sys_info (void)
  112. {
  113. int retval;
  114. grub_memset (&uboot_sys_info, 0, sizeof (uboot_sys_info));
  115. grub_memset (&uboot_mem_info, 0, sizeof (uboot_mem_info));
  116. uboot_sys_info.mr = uboot_mem_info;
  117. uboot_sys_info.mr_no = sizeof (uboot_mem_info) / sizeof (struct mem_region);
  118. if (grub_uboot_syscall (API_GET_SYS_INFO, &retval, &uboot_sys_info))
  119. if (retval == 0)
  120. return &uboot_sys_info;
  121. return NULL;
  122. }
  123. void
  124. grub_uboot_udelay (grub_uint32_t usec)
  125. {
  126. grub_uboot_syscall (API_UDELAY, NULL, &usec);
  127. }
  128. grub_uint32_t
  129. grub_uboot_get_timer (grub_uint32_t base)
  130. {
  131. grub_uint32_t current;
  132. if (!grub_uboot_syscall (API_GET_TIMER, NULL, &current, &base))
  133. return 0;
  134. return current;
  135. }
  136. int
  137. grub_uboot_dev_enum (void)
  138. {
  139. struct device_info * enum_devices;
  140. int num_enum_devices, max_devices;
  141. if (num_devices)
  142. return num_devices;
  143. max_devices = 2;
  144. enum_devices = grub_malloc (sizeof(struct device_info) * max_devices);
  145. if (!enum_devices)
  146. return 0;
  147. /*
  148. * The API_DEV_ENUM call starts a fresh enumeration when passed a
  149. * struct device_info with a NULL cookie, and then depends on having
  150. * the previously enumerated device cookie "seeded" into the target
  151. * structure.
  152. */
  153. enum_devices[0].cookie = NULL;
  154. num_enum_devices = 0;
  155. if (grub_uboot_syscall (API_DEV_ENUM, NULL,
  156. &enum_devices[num_enum_devices]) == 0)
  157. goto error;
  158. num_enum_devices++;
  159. while (enum_devices[num_enum_devices - 1].cookie != NULL)
  160. {
  161. if (num_enum_devices == max_devices)
  162. {
  163. struct device_info *tmp;
  164. int new_max;
  165. new_max = max_devices * 2;
  166. tmp = grub_realloc (enum_devices,
  167. sizeof (struct device_info) * new_max);
  168. if (!tmp)
  169. {
  170. /* Failed to realloc, so return what we have */
  171. break;
  172. }
  173. enum_devices = tmp;
  174. max_devices = new_max;
  175. }
  176. enum_devices[num_enum_devices].cookie =
  177. enum_devices[num_enum_devices - 1].cookie;
  178. if (grub_uboot_syscall (API_DEV_ENUM, NULL,
  179. &enum_devices[num_enum_devices]) == 0)
  180. goto error;
  181. if (enum_devices[num_enum_devices].cookie == NULL)
  182. break;
  183. num_enum_devices++;
  184. }
  185. devices = enum_devices;
  186. return num_devices = num_enum_devices;
  187. error:
  188. grub_free (enum_devices);
  189. return 0;
  190. }
  191. #define VALID_DEV(x) (((x) < num_devices) && ((x) >= 0))
  192. #define OPEN_DEV(x) ((x->state == DEV_STA_OPEN))
  193. struct device_info *
  194. grub_uboot_dev_get (int index)
  195. {
  196. if (VALID_DEV (index))
  197. return &devices[index];
  198. return NULL;
  199. }
  200. int
  201. grub_uboot_dev_open (struct device_info *dev)
  202. {
  203. int retval;
  204. if (!grub_uboot_syscall (API_DEV_OPEN, &retval, dev))
  205. return -1;
  206. return retval;
  207. }
  208. int
  209. grub_uboot_dev_close (struct device_info *dev)
  210. {
  211. int retval;
  212. if (!grub_uboot_syscall (API_DEV_CLOSE, &retval, dev))
  213. return -1;
  214. return retval;
  215. }
  216. int
  217. grub_uboot_dev_read (struct device_info *dev, void *buf, grub_size_t blocks,
  218. grub_uint32_t start, grub_size_t * real_blocks)
  219. {
  220. int retval;
  221. if (!OPEN_DEV (dev))
  222. return -1;
  223. if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf,
  224. &blocks, &start, real_blocks))
  225. return -1;
  226. return retval;
  227. }
  228. int
  229. grub_uboot_dev_recv (struct device_info *dev, void *buf,
  230. int size, int *real_size)
  231. {
  232. int retval;
  233. if (!OPEN_DEV (dev))
  234. return -1;
  235. if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf, &size, real_size))
  236. return -1;
  237. return retval;
  238. }
  239. int
  240. grub_uboot_dev_send (struct device_info *dev, void *buf, int size)
  241. {
  242. int retval;
  243. if (!OPEN_DEV (dev))
  244. return -1;
  245. if (!grub_uboot_syscall (API_DEV_WRITE, &retval, dev, buf, &size))
  246. return -1;
  247. return retval;
  248. }
  249. char *
  250. grub_uboot_env_get (const char *name)
  251. {
  252. char *value;
  253. if (!grub_uboot_syscall (API_ENV_GET, NULL, name, &value))
  254. return NULL;
  255. return value;
  256. }
  257. void
  258. grub_uboot_env_set (const char *name, const char *value)
  259. {
  260. grub_uboot_syscall (API_ENV_SET, NULL, name, value);
  261. }