2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <linux/types.h>
28 #include <api_public.h>
32 static int valid_sig(struct api_signature *sig)
35 struct api_signature s;
40 * Clear the checksum field (in the local copy) so as to calculate the
41 * CRC with the same initial contents as at the time when the sig was
47 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
49 if (checksum != sig->checksum)
56 * Searches for the U-Boot API signature
58 * returns 1/0 depending on found/not found result
60 int api_search_sig(struct api_signature **sig) {
63 uint32_t search_start = 0;
64 uint32_t search_end = 0;
70 search_hint = 255 * 1024 * 1024;
72 search_start = search_hint & ~0x000fffff;
73 search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
75 sp = (unsigned char *)search_start;
76 while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
77 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
78 *sig = (struct api_signature *)sp;
89 /****************************************
93 ****************************************/
99 if (!syscall(API_GETC, NULL, (uint32_t)&c))
109 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
117 syscall(API_PUTC, NULL, (uint32_t)&c);
120 void ub_puts(const char *s)
122 syscall(API_PUTS, NULL, (uint32_t)s);
125 /****************************************
129 ****************************************/
133 syscall(API_RESET, NULL);
137 static struct mem_region mr[MR_MAX];
138 static struct sys_info si;
140 struct sys_info * ub_get_sys_info(void)
144 memset(&si, 0, sizeof(struct sys_info));
147 memset(&mr, 0, sizeof(mr));
149 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
152 return ((err) ? NULL : &si);
155 /****************************************
159 ****************************************/
161 void ub_udelay(unsigned long usec)
163 syscall(API_UDELAY, NULL, &usec);
166 unsigned long ub_get_timer(unsigned long base)
170 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
177 /****************************************************************************
181 * Devices are identified by handles: numbers 0, 1, 2, ..., MAX_DEVS-1
183 ***************************************************************************/
187 static struct device_info devices[MAX_DEVS];
189 struct device_info * ub_dev_get(int i)
191 return ((i < 0 || i >= MAX_DEVS) ? NULL : &devices[i]);
195 * Enumerates the devices: fills out device_info elements in the devices[]
198 * returns: number of devices found
200 int ub_dev_enum(void)
202 struct device_info *di;
205 memset(&devices, 0, sizeof(struct device_info) * MAX_DEVS);
208 if (!syscall(API_DEV_ENUM, NULL, di))
211 while (di->cookie != NULL) {
216 /* take another device_info */
219 /* pass on the previous cookie */
220 di->cookie = devices[n - 1].cookie;
222 if (!syscall(API_DEV_ENUM, NULL, di))
230 * handle: 0-based id of the device
232 * returns: 0 when OK, err otherwise
234 int ub_dev_open(int handle)
236 struct device_info *di;
239 if (handle < 0 || handle >= MAX_DEVS)
242 di = &devices[handle];
244 if (!syscall(API_DEV_OPEN, &err, di))
250 int ub_dev_close(int handle)
252 struct device_info *di;
254 if (handle < 0 || handle >= MAX_DEVS)
257 di = &devices[handle];
258 if (!syscall(API_DEV_CLOSE, NULL, di))
266 * Validates device for read/write, it has to:
271 * returns: 0/1 accordingly
273 static int dev_valid(int handle)
275 if (handle < 0 || handle >= MAX_DEVS)
278 if (devices[handle].state != DEV_STA_OPEN)
284 static int dev_stor_valid(int handle)
286 if (!dev_valid(handle))
289 if (!(devices[handle].type & DEV_TYP_STOR))
295 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start)
297 struct device_info *di;
301 if (!dev_stor_valid(handle))
304 di = &devices[handle];
305 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
317 static int dev_net_valid(int handle)
319 if (!dev_valid(handle))
322 if (devices[handle].type != DEV_TYP_NET)
328 int ub_dev_recv(int handle, void *buf, int len)
330 struct device_info *di;
331 int err = 0, act_len;
333 if (!dev_net_valid(handle))
336 di = &devices[handle];
337 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
346 int ub_dev_send(int handle, void *buf, int len)
348 struct device_info *di;
351 if (!dev_net_valid(handle))
354 di = &devices[handle];
355 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
361 /****************************************
365 ****************************************/
367 char * ub_env_get(const char *name)
371 if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
377 void ub_env_set(const char *name, char *value)
379 syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
383 static char env_name[256];
385 const char * ub_env_enum(const char *last)
387 const char *env, *str;
393 * It's OK to pass only the name piece as last (and not the whole
394 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
395 * internally, which handles such case
397 if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
401 /* no more env. variables to enumerate */
404 /* next enumerated env var */
405 memset(env_name, 0, 256);
406 for (i = 0, str = env; *str != '=' && *str != '\0';)
407 env_name[i++] = *str++;