glue.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <linux/types.h>
  8. #include <api_public.h>
  9. #include "glue.h"
  10. static int valid_sig(struct api_signature *sig)
  11. {
  12. uint32_t checksum;
  13. struct api_signature s;
  14. if (sig == NULL)
  15. return 0;
  16. /*
  17. * Clear the checksum field (in the local copy) so as to calculate the
  18. * CRC with the same initial contents as at the time when the sig was
  19. * produced
  20. */
  21. s = *sig;
  22. s.checksum = 0;
  23. checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
  24. if (checksum != sig->checksum)
  25. return 0;
  26. return 1;
  27. }
  28. /*
  29. * Searches for the U-Boot API signature
  30. *
  31. * returns 1/0 depending on found/not found result
  32. */
  33. int api_search_sig(struct api_signature **sig)
  34. {
  35. unsigned char *sp;
  36. uint32_t search_start = 0;
  37. uint32_t search_end = 0;
  38. if (sig == NULL)
  39. return 0;
  40. if (search_hint == 0)
  41. search_hint = 255 * 1024 * 1024;
  42. search_start = search_hint & ~0x000fffff;
  43. search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
  44. sp = (unsigned char *)search_start;
  45. while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
  46. if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
  47. *sig = (struct api_signature *)sp;
  48. if (valid_sig(*sig))
  49. return 1;
  50. }
  51. sp += API_SIG_MAGLEN;
  52. }
  53. *sig = NULL;
  54. return 0;
  55. }
  56. /****************************************
  57. *
  58. * console
  59. *
  60. ****************************************/
  61. int ub_getc(void)
  62. {
  63. int c;
  64. if (!syscall(API_GETC, NULL, &c))
  65. return -1;
  66. return c;
  67. }
  68. int ub_tstc(void)
  69. {
  70. int t;
  71. if (!syscall(API_TSTC, NULL, &t))
  72. return -1;
  73. return t;
  74. }
  75. void ub_putc(char c)
  76. {
  77. syscall(API_PUTC, NULL, &c);
  78. }
  79. void ub_puts(const char *s)
  80. {
  81. syscall(API_PUTS, NULL, s);
  82. }
  83. /****************************************
  84. *
  85. * system
  86. *
  87. ****************************************/
  88. void ub_reset(void)
  89. {
  90. syscall(API_RESET, NULL);
  91. }
  92. static struct mem_region mr[UB_MAX_MR];
  93. static struct sys_info si;
  94. struct sys_info * ub_get_sys_info(void)
  95. {
  96. int err = 0;
  97. memset(&si, 0, sizeof(struct sys_info));
  98. si.mr = mr;
  99. si.mr_no = UB_MAX_MR;
  100. memset(&mr, 0, sizeof(mr));
  101. if (!syscall(API_GET_SYS_INFO, &err, &si))
  102. return NULL;
  103. return ((err) ? NULL : &si);
  104. }
  105. /****************************************
  106. *
  107. * timing
  108. *
  109. ****************************************/
  110. void ub_udelay(unsigned long usec)
  111. {
  112. syscall(API_UDELAY, NULL, &usec);
  113. }
  114. unsigned long ub_get_timer(unsigned long base)
  115. {
  116. unsigned long cur;
  117. if (!syscall(API_GET_TIMER, NULL, &cur, &base))
  118. return 0;
  119. return cur;
  120. }
  121. /****************************************************************************
  122. *
  123. * devices
  124. *
  125. * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
  126. *
  127. ***************************************************************************/
  128. static struct device_info devices[UB_MAX_DEV];
  129. struct device_info * ub_dev_get(int i)
  130. {
  131. return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
  132. }
  133. /*
  134. * Enumerates the devices: fills out device_info elements in the devices[]
  135. * array.
  136. *
  137. * returns: number of devices found
  138. */
  139. int ub_dev_enum(void)
  140. {
  141. struct device_info *di;
  142. int n = 0;
  143. memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
  144. di = &devices[0];
  145. if (!syscall(API_DEV_ENUM, NULL, di))
  146. return 0;
  147. while (di->cookie != NULL) {
  148. if (++n >= UB_MAX_DEV)
  149. break;
  150. /* take another device_info */
  151. di++;
  152. /* pass on the previous cookie */
  153. di->cookie = devices[n - 1].cookie;
  154. if (!syscall(API_DEV_ENUM, NULL, di))
  155. return 0;
  156. }
  157. return n;
  158. }
  159. /*
  160. * handle: 0-based id of the device
  161. *
  162. * returns: 0 when OK, err otherwise
  163. */
  164. int ub_dev_open(int handle)
  165. {
  166. struct device_info *di;
  167. int err = 0;
  168. if (handle < 0 || handle >= UB_MAX_DEV)
  169. return API_EINVAL;
  170. di = &devices[handle];
  171. if (!syscall(API_DEV_OPEN, &err, di))
  172. return -1;
  173. return err;
  174. }
  175. int ub_dev_close(int handle)
  176. {
  177. struct device_info *di;
  178. if (handle < 0 || handle >= UB_MAX_DEV)
  179. return API_EINVAL;
  180. di = &devices[handle];
  181. if (!syscall(API_DEV_CLOSE, NULL, di))
  182. return -1;
  183. return 0;
  184. }
  185. /*
  186. *
  187. * Validates device for read/write, it has to:
  188. *
  189. * - have sane handle
  190. * - be opened
  191. *
  192. * returns: 0/1 accordingly
  193. */
  194. static int dev_valid(int handle)
  195. {
  196. if (handle < 0 || handle >= UB_MAX_DEV)
  197. return 0;
  198. if (devices[handle].state != DEV_STA_OPEN)
  199. return 0;
  200. return 1;
  201. }
  202. static int dev_stor_valid(int handle)
  203. {
  204. if (!dev_valid(handle))
  205. return 0;
  206. if (!(devices[handle].type & DEV_TYP_STOR))
  207. return 0;
  208. return 1;
  209. }
  210. int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
  211. lbasize_t *rlen)
  212. {
  213. struct device_info *di;
  214. lbasize_t act_len;
  215. int err = 0;
  216. if (!dev_stor_valid(handle))
  217. return API_ENODEV;
  218. di = &devices[handle];
  219. if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
  220. return API_ESYSC;
  221. if (!err && rlen)
  222. *rlen = act_len;
  223. return err;
  224. }
  225. static int dev_net_valid(int handle)
  226. {
  227. if (!dev_valid(handle))
  228. return 0;
  229. if (devices[handle].type != DEV_TYP_NET)
  230. return 0;
  231. return 1;
  232. }
  233. int ub_dev_recv(int handle, void *buf, int len, int *rlen)
  234. {
  235. struct device_info *di;
  236. int err = 0, act_len;
  237. if (!dev_net_valid(handle))
  238. return API_ENODEV;
  239. di = &devices[handle];
  240. if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
  241. return API_ESYSC;
  242. if (!err && rlen)
  243. *rlen = act_len;
  244. return (err);
  245. }
  246. int ub_dev_send(int handle, void *buf, int len)
  247. {
  248. struct device_info *di;
  249. int err = 0;
  250. if (!dev_net_valid(handle))
  251. return API_ENODEV;
  252. di = &devices[handle];
  253. if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
  254. return API_ESYSC;
  255. return err;
  256. }
  257. /****************************************
  258. *
  259. * env vars
  260. *
  261. ****************************************/
  262. char * ub_env_get(const char *name)
  263. {
  264. char *value;
  265. if (!syscall(API_ENV_GET, NULL, name, &value))
  266. return NULL;
  267. return value;
  268. }
  269. void ub_env_set(const char *name, char *value)
  270. {
  271. syscall(API_ENV_SET, NULL, name, value);
  272. }
  273. static char env_name[256];
  274. const char * ub_env_enum(const char *last)
  275. {
  276. const char *env, *str;
  277. int i;
  278. env = NULL;
  279. /*
  280. * It's OK to pass only the name piece as last (and not the whole
  281. * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
  282. * internally, which handles such case
  283. */
  284. if (!syscall(API_ENV_ENUM, NULL, last, &env))
  285. return NULL;
  286. if (!env)
  287. /* no more env. variables to enumerate */
  288. return NULL;
  289. /* next enumerated env var */
  290. memset(env_name, 0, 256);
  291. for (i = 0, str = env; *str != '=' && *str != '\0';)
  292. env_name[i++] = *str++;
  293. env_name[i] = '\0';
  294. return env_name;
  295. }
  296. /****************************************
  297. *
  298. * display
  299. *
  300. ****************************************/
  301. int ub_display_get_info(int type, struct display_info *di)
  302. {
  303. int err = 0;
  304. if (!syscall(API_DISPLAY_GET_INFO, &err, type, di))
  305. return API_ESYSC;
  306. return err;
  307. }
  308. int ub_display_draw_bitmap(ulong bitmap, int x, int y)
  309. {
  310. int err = 0;
  311. if (!syscall(API_DISPLAY_DRAW_BITMAP, &err, bitmap, x, y))
  312. return API_ESYSC;
  313. return err;
  314. }
  315. void ub_display_clear(void)
  316. {
  317. syscall(API_DISPLAY_CLEAR, NULL);
  318. }