]>
git.ipfire.org Git - people/ms/u-boot.git/blob - examples/api/glue.c
2 * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/types.h>
9 #include <api_public.h>
13 static int valid_sig(struct api_signature
*sig
)
16 struct api_signature s
;
21 * Clear the checksum field (in the local copy) so as to calculate the
22 * CRC with the same initial contents as at the time when the sig was
28 checksum
= crc32(0, (unsigned char *)&s
, sizeof(struct api_signature
));
30 if (checksum
!= sig
->checksum
)
37 * Searches for the U-Boot API signature
39 * returns 1/0 depending on found/not found result
41 int api_search_sig(struct api_signature
**sig
)
44 uint32_t search_start
= 0;
45 uint32_t search_end
= 0;
51 search_hint
= 255 * 1024 * 1024;
53 search_start
= search_hint
& ~0x000fffff;
54 search_end
= search_start
+ API_SEARCH_LEN
- API_SIG_MAGLEN
;
56 sp
= (unsigned char *)search_start
;
57 while ((sp
+ API_SIG_MAGLEN
) < (unsigned char *)search_end
) {
58 if (!memcmp(sp
, API_SIG_MAGIC
, API_SIG_MAGLEN
)) {
59 *sig
= (struct api_signature
*)sp
;
70 /****************************************
74 ****************************************/
80 if (!syscall(API_GETC
, NULL
, &c
))
90 if (!syscall(API_TSTC
, NULL
, &t
))
98 syscall(API_PUTC
, NULL
, &c
);
101 void ub_puts(const char *s
)
103 syscall(API_PUTS
, NULL
, s
);
106 /****************************************
110 ****************************************/
114 syscall(API_RESET
, NULL
);
117 static struct mem_region mr
[UB_MAX_MR
];
118 static struct sys_info si
;
120 struct sys_info
* ub_get_sys_info(void)
124 memset(&si
, 0, sizeof(struct sys_info
));
126 si
.mr_no
= UB_MAX_MR
;
127 memset(&mr
, 0, sizeof(mr
));
129 if (!syscall(API_GET_SYS_INFO
, &err
, &si
))
132 return ((err
) ? NULL
: &si
);
135 /****************************************
139 ****************************************/
141 void ub_udelay(unsigned long usec
)
143 syscall(API_UDELAY
, NULL
, &usec
);
146 unsigned long ub_get_timer(unsigned long base
)
150 if (!syscall(API_GET_TIMER
, NULL
, &cur
, &base
))
157 /****************************************************************************
161 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
163 ***************************************************************************/
165 static struct device_info devices
[UB_MAX_DEV
];
167 struct device_info
* ub_dev_get(int i
)
169 return ((i
< 0 || i
>= UB_MAX_DEV
) ? NULL
: &devices
[i
]);
173 * Enumerates the devices: fills out device_info elements in the devices[]
176 * returns: number of devices found
178 int ub_dev_enum(void)
180 struct device_info
*di
;
183 memset(&devices
, 0, sizeof(struct device_info
) * UB_MAX_DEV
);
186 if (!syscall(API_DEV_ENUM
, NULL
, di
))
189 while (di
->cookie
!= NULL
) {
191 if (++n
>= UB_MAX_DEV
)
194 /* take another device_info */
197 /* pass on the previous cookie */
198 di
->cookie
= devices
[n
- 1].cookie
;
200 if (!syscall(API_DEV_ENUM
, NULL
, di
))
208 * handle: 0-based id of the device
210 * returns: 0 when OK, err otherwise
212 int ub_dev_open(int handle
)
214 struct device_info
*di
;
217 if (handle
< 0 || handle
>= UB_MAX_DEV
)
220 di
= &devices
[handle
];
222 if (!syscall(API_DEV_OPEN
, &err
, di
))
228 int ub_dev_close(int handle
)
230 struct device_info
*di
;
232 if (handle
< 0 || handle
>= UB_MAX_DEV
)
235 di
= &devices
[handle
];
236 if (!syscall(API_DEV_CLOSE
, NULL
, di
))
244 * Validates device for read/write, it has to:
249 * returns: 0/1 accordingly
251 static int dev_valid(int handle
)
253 if (handle
< 0 || handle
>= UB_MAX_DEV
)
256 if (devices
[handle
].state
!= DEV_STA_OPEN
)
262 static int dev_stor_valid(int handle
)
264 if (!dev_valid(handle
))
267 if (!(devices
[handle
].type
& DEV_TYP_STOR
))
273 int ub_dev_read(int handle
, void *buf
, lbasize_t len
, lbastart_t start
,
276 struct device_info
*di
;
280 if (!dev_stor_valid(handle
))
283 di
= &devices
[handle
];
284 if (!syscall(API_DEV_READ
, &err
, di
, buf
, &len
, &start
, &act_len
))
293 static int dev_net_valid(int handle
)
295 if (!dev_valid(handle
))
298 if (devices
[handle
].type
!= DEV_TYP_NET
)
304 int ub_dev_recv(int handle
, void *buf
, int len
, int *rlen
)
306 struct device_info
*di
;
307 int err
= 0, act_len
;
309 if (!dev_net_valid(handle
))
312 di
= &devices
[handle
];
313 if (!syscall(API_DEV_READ
, &err
, di
, buf
, &len
, &act_len
))
322 int ub_dev_send(int handle
, void *buf
, int len
)
324 struct device_info
*di
;
327 if (!dev_net_valid(handle
))
330 di
= &devices
[handle
];
331 if (!syscall(API_DEV_WRITE
, &err
, di
, buf
, &len
))
337 /****************************************
341 ****************************************/
343 char * ub_env_get(const char *name
)
347 if (!syscall(API_ENV_GET
, NULL
, name
, &value
))
353 void ub_env_set(const char *name
, char *value
)
355 syscall(API_ENV_SET
, NULL
, name
, value
);
358 static char env_name
[256];
360 const char * ub_env_enum(const char *last
)
362 const char *env
, *str
;
368 * It's OK to pass only the name piece as last (and not the whole
369 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
370 * internally, which handles such case
372 if (!syscall(API_ENV_ENUM
, NULL
, last
, &env
))
376 /* no more env. variables to enumerate */
379 /* next enumerated env var */
380 memset(env_name
, 0, 256);
381 for (i
= 0, str
= env
; *str
!= '=' && *str
!= '\0';)
382 env_name
[i
++] = *str
++;
389 /****************************************
393 ****************************************/
395 int ub_display_get_info(int type
, struct display_info
*di
)
399 if (!syscall(API_DISPLAY_GET_INFO
, &err
, type
, di
))
405 int ub_display_draw_bitmap(ulong bitmap
, int x
, int y
)
409 if (!syscall(API_DISPLAY_DRAW_BITMAP
, &err
, bitmap
, x
, y
))
415 void ub_display_clear(void)
417 syscall(API_DISPLAY_CLEAR
, NULL
);
420 __weak
void *memcpy(void *dest
, const void *src
, size_t size
)
422 unsigned char *dptr
= dest
;
423 const unsigned char *ptr
= src
;
424 const unsigned char *end
= src
+ size
;