1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
7 #include <linux/types.h>
8 #include <api_public.h>
12 static int valid_sig(struct api_signature *sig)
15 struct api_signature s;
20 * Clear the checksum field (in the local copy) so as to calculate the
21 * CRC with the same initial contents as at the time when the sig was
27 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
29 if (checksum != sig->checksum)
36 * Searches for the U-Boot API signature
38 * returns 1/0 depending on found/not found result
40 int api_search_sig(struct api_signature **sig)
43 uint32_t search_start = 0;
44 uint32_t search_end = 0;
50 search_hint = 255 * 1024 * 1024;
52 search_start = search_hint & ~0x000fffff;
53 search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
55 sp = (unsigned char *)search_start;
56 while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
57 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
58 *sig = (struct api_signature *)sp;
69 /****************************************
73 ****************************************/
79 if (!syscall(API_GETC, NULL, &c))
89 if (!syscall(API_TSTC, NULL, &t))
97 syscall(API_PUTC, NULL, &c);
100 void ub_puts(const char *s)
102 syscall(API_PUTS, NULL, s);
105 /****************************************
109 ****************************************/
113 syscall(API_RESET, NULL);
116 static struct mem_region mr[UB_MAX_MR];
117 static struct sys_info si;
119 struct sys_info * ub_get_sys_info(void)
123 memset(&si, 0, sizeof(struct sys_info));
125 si.mr_no = UB_MAX_MR;
126 memset(&mr, 0, sizeof(mr));
128 if (!syscall(API_GET_SYS_INFO, &err, &si))
131 return ((err) ? NULL : &si);
134 /****************************************
138 ****************************************/
140 void ub_udelay(unsigned long usec)
142 syscall(API_UDELAY, NULL, &usec);
145 unsigned long ub_get_timer(unsigned long base)
149 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
156 /****************************************************************************
160 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
162 ***************************************************************************/
164 static struct device_info devices[UB_MAX_DEV];
166 struct device_info * ub_dev_get(int i)
168 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
172 * Enumerates the devices: fills out device_info elements in the devices[]
175 * returns: number of devices found
177 int ub_dev_enum(void)
179 struct device_info *di;
182 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
185 if (!syscall(API_DEV_ENUM, NULL, di))
188 while (di->cookie != NULL) {
190 if (++n >= UB_MAX_DEV)
193 /* take another device_info */
196 /* pass on the previous cookie */
197 di->cookie = devices[n - 1].cookie;
199 if (!syscall(API_DEV_ENUM, NULL, di))
207 * handle: 0-based id of the device
209 * returns: 0 when OK, err otherwise
211 int ub_dev_open(int handle)
213 struct device_info *di;
216 if (handle < 0 || handle >= UB_MAX_DEV)
219 di = &devices[handle];
221 if (!syscall(API_DEV_OPEN, &err, di))
227 int ub_dev_close(int handle)
229 struct device_info *di;
231 if (handle < 0 || handle >= UB_MAX_DEV)
234 di = &devices[handle];
235 if (!syscall(API_DEV_CLOSE, NULL, di))
243 * Validates device for read/write, it has to:
248 * returns: 0/1 accordingly
250 static int dev_valid(int handle)
252 if (handle < 0 || handle >= UB_MAX_DEV)
255 if (devices[handle].state != DEV_STA_OPEN)
261 static int dev_stor_valid(int handle)
263 if (!dev_valid(handle))
266 if (!(devices[handle].type & DEV_TYP_STOR))
272 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
275 struct device_info *di;
279 if (!dev_stor_valid(handle))
282 di = &devices[handle];
283 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
292 static int dev_net_valid(int handle)
294 if (!dev_valid(handle))
297 if (devices[handle].type != DEV_TYP_NET)
303 int ub_dev_recv(int handle, void *buf, int len, int *rlen)
305 struct device_info *di;
306 int err = 0, act_len;
308 if (!dev_net_valid(handle))
311 di = &devices[handle];
312 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
321 int ub_dev_send(int handle, void *buf, int len)
323 struct device_info *di;
326 if (!dev_net_valid(handle))
329 di = &devices[handle];
330 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
336 /****************************************
340 ****************************************/
342 char * ub_env_get(const char *name)
346 if (!syscall(API_ENV_GET, NULL, name, &value))
352 void ub_env_set(const char *name, char *value)
354 syscall(API_ENV_SET, NULL, name, value);
357 static char env_name[256];
359 const char * ub_env_enum(const char *last)
361 const char *env, *str;
367 * It's OK to pass only the name piece as last (and not the whole
368 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
369 * internally, which handles such case
371 if (!syscall(API_ENV_ENUM, NULL, last, &env))
375 /* no more env. variables to enumerate */
378 /* next enumerated env var */
379 memset(env_name, 0, 256);
380 for (i = 0, str = env; *str != '=' && *str != '\0';)
381 env_name[i++] = *str++;
388 /****************************************
392 ****************************************/
394 int ub_display_get_info(int type, struct display_info *di)
398 if (!syscall(API_DISPLAY_GET_INFO, &err, type, di))
404 int ub_display_draw_bitmap(ulong bitmap, int x, int y)
408 if (!syscall(API_DISPLAY_DRAW_BITMAP, &err, bitmap, x, y))
414 void ub_display_clear(void)
416 syscall(API_DISPLAY_CLEAR, NULL);
419 __weak void *memcpy(void *dest, const void *src, size_t size)
421 unsigned char *dptr = dest;
422 const unsigned char *ptr = src;
423 const unsigned char *end = src + size;