1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
8 #include <linux/types.h>
9 #include <api_public.h>
10 #include <u-boot/crc.h>
14 static int valid_sig(struct api_signature *sig)
17 struct api_signature s;
22 * Clear the checksum field (in the local copy) so as to calculate the
23 * CRC with the same initial contents as at the time when the sig was
29 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
31 if (checksum != sig->checksum)
38 * Searches for the U-Boot API signature
40 * returns 1/0 depending on found/not found result
42 int api_search_sig(struct api_signature **sig)
45 uint32_t search_start = 0;
46 uint32_t search_end = 0;
52 search_hint = 255 * 1024 * 1024;
54 search_start = search_hint & ~0x000fffff;
55 search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
57 sp = (unsigned char *)search_start;
58 while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
59 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
60 *sig = (struct api_signature *)sp;
71 /****************************************
75 ****************************************/
81 if (!syscall(API_GETC, NULL, &c))
91 if (!syscall(API_TSTC, NULL, &t))
99 syscall(API_PUTC, NULL, &c);
102 void ub_puts(const char *s)
104 syscall(API_PUTS, NULL, s);
107 /****************************************
111 ****************************************/
115 syscall(API_RESET, NULL);
118 static struct mem_region mr[UB_MAX_MR];
119 static struct sys_info si;
121 struct sys_info * ub_get_sys_info(void)
125 memset(&si, 0, sizeof(struct sys_info));
127 si.mr_no = UB_MAX_MR;
128 memset(&mr, 0, sizeof(mr));
130 if (!syscall(API_GET_SYS_INFO, &err, &si))
133 return ((err) ? NULL : &si);
136 /****************************************
140 ****************************************/
142 void ub_udelay(unsigned long usec)
144 syscall(API_UDELAY, NULL, &usec);
147 unsigned long ub_get_timer(unsigned long base)
151 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
158 /****************************************************************************
162 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
164 ***************************************************************************/
166 static struct device_info devices[UB_MAX_DEV];
168 struct device_info * ub_dev_get(int i)
170 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
174 * Enumerates the devices: fills out device_info elements in the devices[]
177 * returns: number of devices found
179 int ub_dev_enum(void)
181 struct device_info *di;
184 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
187 if (!syscall(API_DEV_ENUM, NULL, di))
190 while (di->cookie != NULL) {
192 if (++n >= UB_MAX_DEV)
195 /* take another device_info */
198 /* pass on the previous cookie */
199 di->cookie = devices[n - 1].cookie;
201 if (!syscall(API_DEV_ENUM, NULL, di))
209 * handle: 0-based id of the device
211 * returns: 0 when OK, err otherwise
213 int ub_dev_open(int handle)
215 struct device_info *di;
218 if (handle < 0 || handle >= UB_MAX_DEV)
221 di = &devices[handle];
223 if (!syscall(API_DEV_OPEN, &err, di))
229 int ub_dev_close(int handle)
231 struct device_info *di;
233 if (handle < 0 || handle >= UB_MAX_DEV)
236 di = &devices[handle];
237 if (!syscall(API_DEV_CLOSE, NULL, di))
245 * Validates device for read/write, it has to:
250 * returns: 0/1 accordingly
252 static int dev_valid(int handle)
254 if (handle < 0 || handle >= UB_MAX_DEV)
257 if (devices[handle].state != DEV_STA_OPEN)
263 static int dev_stor_valid(int handle)
265 if (!dev_valid(handle))
268 if (!(devices[handle].type & DEV_TYP_STOR))
274 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
277 struct device_info *di;
281 if (!dev_stor_valid(handle))
284 di = &devices[handle];
285 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
294 static int dev_net_valid(int handle)
296 if (!dev_valid(handle))
299 if (devices[handle].type != DEV_TYP_NET)
305 int ub_dev_recv(int handle, void *buf, int len, int *rlen)
307 struct device_info *di;
308 int err = 0, act_len;
310 if (!dev_net_valid(handle))
313 di = &devices[handle];
314 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
323 int ub_dev_send(int handle, void *buf, int len)
325 struct device_info *di;
328 if (!dev_net_valid(handle))
331 di = &devices[handle];
332 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
338 /****************************************
342 ****************************************/
344 char * ub_env_get(const char *name)
348 if (!syscall(API_ENV_GET, NULL, name, &value))
354 void ub_env_set(const char *name, char *value)
356 syscall(API_ENV_SET, NULL, name, value);
359 static char env_name[256];
361 const char * ub_env_enum(const char *last)
363 const char *env, *str;
369 * It's OK to pass only the name piece as last (and not the whole
370 * 'name=val' string), since the API_ENUM_ENV call uses env_match()
371 * internally, which handles such case
373 if (!syscall(API_ENV_ENUM, NULL, last, &env))
377 /* no more env. variables to enumerate */
380 /* next enumerated env var */
381 memset(env_name, 0, 256);
382 for (i = 0, str = env; *str != '=' && *str != '\0';)
383 env_name[i++] = *str++;
390 /****************************************
394 ****************************************/
396 int ub_display_get_info(int type, struct display_info *di)
400 if (!syscall(API_DISPLAY_GET_INFO, &err, type, di))
406 int ub_display_draw_bitmap(ulong bitmap, int x, int y)
410 if (!syscall(API_DISPLAY_DRAW_BITMAP, &err, bitmap, x, y))
416 void ub_display_clear(void)
418 syscall(API_DISPLAY_CLEAR, NULL);
421 __weak void *memcpy(void *dest, const void *src, size_t size)
423 unsigned char *dptr = dest;
424 const unsigned char *ptr = src;
425 const unsigned char *end = src + size;