2 * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <linux/types.h>
26 #include <api_public.h>
30 static int valid_sig(struct api_signature *sig)
33 struct api_signature s;
38 * Clear the checksum field (in the local copy) so as to calculate the
39 * CRC with the same initial contents as at the time when the sig was
45 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
47 if (checksum != sig->checksum)
54 * Searches for the U-Boot API signature
56 * returns 1/0 depending on found/not found result
58 int api_search_sig(struct api_signature **sig)
61 uint32_t search_start = 0;
62 uint32_t search_end = 0;
68 search_hint = 255 * 1024 * 1024;
70 search_start = search_hint & ~0x000fffff;
71 search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
73 sp = (unsigned char *)search_start;
74 while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
75 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
76 *sig = (struct api_signature *)sp;
87 /****************************************
91 ****************************************/
97 if (!syscall(API_GETC, NULL, (uint32_t)&c))
107 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
115 syscall(API_PUTC, NULL, (uint32_t)&c);
118 void ub_puts(const char *s)
120 syscall(API_PUTS, NULL, (uint32_t)s);
123 /****************************************
127 ****************************************/
131 syscall(API_RESET, NULL);
134 static struct mem_region mr[UB_MAX_MR];
135 static struct sys_info si;
137 struct sys_info * ub_get_sys_info(void)
141 memset(&si, 0, sizeof(struct sys_info));
143 si.mr_no = UB_MAX_MR;
144 memset(&mr, 0, sizeof(mr));
146 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
149 return ((err) ? NULL : &si);
152 /****************************************
156 ****************************************/
158 void ub_udelay(unsigned long usec)
160 syscall(API_UDELAY, NULL, &usec);
163 unsigned long ub_get_timer(unsigned long base)
167 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
174 /****************************************************************************
178 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
180 ***************************************************************************/
182 static struct device_info devices[UB_MAX_DEV];
184 struct device_info * ub_dev_get(int i)
186 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
190 * Enumerates the devices: fills out device_info elements in the devices[]
193 * returns: number of devices found
195 int ub_dev_enum(void)
197 struct device_info *di;
200 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
203 if (!syscall(API_DEV_ENUM, NULL, di))
206 while (di->cookie != NULL) {
208 if (++n >= UB_MAX_DEV)
211 /* take another device_info */
214 /* pass on the previous cookie */
215 di->cookie = devices[n - 1].cookie;
217 if (!syscall(API_DEV_ENUM, NULL, di))
225 * handle: 0-based id of the device
227 * returns: 0 when OK, err otherwise
229 int ub_dev_open(int handle)
231 struct device_info *di;
234 if (handle < 0 || handle >= UB_MAX_DEV)
237 di = &devices[handle];
239 if (!syscall(API_DEV_OPEN, &err, di))
245 int ub_dev_close(int handle)
247 struct device_info *di;
249 if (handle < 0 || handle >= UB_MAX_DEV)
252 di = &devices[handle];
253 if (!syscall(API_DEV_CLOSE, NULL, di))
261 * Validates device for read/write, it has to:
266 * returns: 0/1 accordingly
268 static int dev_valid(int handle)
270 if (handle < 0 || handle >= UB_MAX_DEV)
273 if (devices[handle].state != DEV_STA_OPEN)
279 static int dev_stor_valid(int handle)
281 if (!dev_valid(handle))
284 if (!(devices[handle].type & DEV_TYP_STOR))
290 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
293 struct device_info *di;
297 if (!dev_stor_valid(handle))
300 di = &devices[handle];
301 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
310 static int dev_net_valid(int handle)
312 if (!dev_valid(handle))
315 if (devices[handle].type != DEV_TYP_NET)
321 int ub_dev_recv(int handle, void *buf, int len, int *rlen)
323 struct device_info *di;
324 int err = 0, act_len;
326 if (!dev_net_valid(handle))
329 di = &devices[handle];
330 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
339 int ub_dev_send(int handle, void *buf, int len)
341 struct device_info *di;
344 if (!dev_net_valid(handle))
347 di = &devices[handle];
348 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
354 /****************************************
358 ****************************************/
360 char * ub_env_get(const char *name)
364 if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
370 void ub_env_set(const char *name, char *value)
372 syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
375 static char env_name[256];
377 const char * ub_env_enum(const char *last)
379 const char *env, *str;
385 * It's OK to pass only the name piece as last (and not the whole
386 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
387 * internally, which handles such case
389 if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
393 /* no more env. variables to enumerate */
396 /* next enumerated env var */
397 memset(env_name, 0, 256);
398 for (i = 0, str = env; *str != '=' && *str != '\0';)
399 env_name[i++] = *str++;