glue.c 7.1 KB

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