glue.c 7.2 KB

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