[MINI2440] Updated config file
[u-boot-openmoko/mini2440.git] / api_examples / glue.c
blob2bf47ae3d212f9f383c27683a23a4bc00f0d98b8
1 /*
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
7 * project.
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,
22 * MA 02111-1307 USA
26 #include <common.h>
27 #include <linux/types.h>
28 #include <api_public.h>
30 #include "glue.h"
32 static int valid_sig(struct api_signature *sig)
34 uint32_t checksum;
35 struct api_signature s;
37 if (sig == NULL)
38 return 0;
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
42 * produced
44 s = *sig;
45 s.checksum = 0;
47 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
49 if (checksum != sig->checksum)
50 return 0;
52 return 1;
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) {
62 unsigned char *sp;
64 if (sig == NULL)
65 return 0;
67 sp = (unsigned char *)API_SEARCH_START;
69 while ((sp + (int)API_SIG_MAGLEN) < (unsigned char *)API_SEARCH_END) {
70 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
71 *sig = (struct api_signature *)sp;
72 if (valid_sig(*sig))
73 return 1;
75 sp += API_SIG_MAGLEN;
78 *sig = NULL;
79 return 0;
82 /****************************************
84 * console
86 ****************************************/
88 int ub_getc(void)
90 int c;
92 if (!syscall(API_GETC, NULL, (uint32_t)&c))
93 return -1;
95 return c;
98 int ub_tstc(void)
100 int t;
102 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
103 return -1;
105 return t;
108 void ub_putc(char c)
110 syscall(API_PUTC, NULL, (uint32_t)&c);
113 void ub_puts(const char *s)
115 syscall(API_PUTS, NULL, (uint32_t)s);
118 /****************************************
120 * system
122 ****************************************/
124 void ub_reset(void)
126 syscall(API_RESET, NULL);
129 #define MR_MAX 5
130 static struct mem_region mr[MR_MAX];
131 static struct sys_info si;
133 struct sys_info * ub_get_sys_info(void)
135 int err = 0;
137 memset(&si, 0, sizeof(struct sys_info));
138 si.mr = mr;
139 si.mr_no = MR_MAX;
140 memset(&mr, 0, sizeof(mr));
142 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
143 return NULL;
145 return ((err) ? NULL : &si);
148 /****************************************
150 * timing
152 ****************************************/
154 void ub_udelay(unsigned long usec)
156 syscall(API_UDELAY, NULL, &usec);
159 unsigned long ub_get_timer(unsigned long base)
161 unsigned long cur;
163 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
164 return 0;
166 return cur;
170 /****************************************************************************
172 * devices
174 * Devices are identified by handles: numbers 0, 1, 2, ..., MAX_DEVS-1
176 ***************************************************************************/
178 #define MAX_DEVS 6
180 static struct device_info devices[MAX_DEVS];
182 struct device_info * ub_dev_get(int i)
184 return ((i < 0 || i >= MAX_DEVS) ? NULL : &devices[i]);
188 * Enumerates the devices: fills out device_info elements in the devices[]
189 * array.
191 * returns: number of devices found
193 int ub_dev_enum(void)
195 struct device_info *di;
196 int n = 0;
198 memset(&devices, 0, sizeof(struct device_info) * MAX_DEVS);
199 di = &devices[0];
201 if (!syscall(API_DEV_ENUM, NULL, di))
202 return 0;
204 while (di->cookie != NULL) {
206 if (++n >= MAX_DEVS)
207 break;
209 /* take another device_info */
210 di++;
212 /* pass on the previous cookie */
213 di->cookie = devices[n - 1].cookie;
215 if (!syscall(API_DEV_ENUM, NULL, di))
216 return 0;
219 return n;
223 * handle: 0-based id of the device
225 * returns: 0 when OK, err otherwise
227 int ub_dev_open(int handle)
229 struct device_info *di;
230 int err = 0;
232 if (handle < 0 || handle >= MAX_DEVS)
233 return API_EINVAL;
235 di = &devices[handle];
237 if (!syscall(API_DEV_OPEN, &err, di))
238 return -1;
240 return err;
243 int ub_dev_close(int handle)
245 struct device_info *di;
247 if (handle < 0 || handle >= MAX_DEVS)
248 return API_EINVAL;
250 di = &devices[handle];
251 if (!syscall(API_DEV_CLOSE, NULL, di))
252 return -1;
254 return 0;
259 * Validates device for read/write, it has to:
261 * - have sane handle
262 * - be opened
264 * returns: 0/1 accordingly
266 static int dev_valid(int handle)
268 if (handle < 0 || handle >= MAX_DEVS)
269 return 0;
271 if (devices[handle].state != DEV_STA_OPEN)
272 return 0;
274 return 1;
277 static int dev_stor_valid(int handle)
279 if (!dev_valid(handle))
280 return 0;
282 if (!(devices[handle].type & DEV_TYP_STOR))
283 return 0;
285 return 1;
288 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start)
290 struct device_info *di;
291 lbasize_t act_len;
292 int err = 0;
294 if (!dev_stor_valid(handle))
295 return API_ENODEV;
297 di = &devices[handle];
298 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
299 return -1;
301 if (err)
302 return err;
304 if (act_len != len)
305 return API_EIO;
307 return 0;
310 static int dev_net_valid(int handle)
312 if (!dev_valid(handle))
313 return 0;
315 if (devices[handle].type != DEV_TYP_NET)
316 return 0;
318 return 1;
321 int ub_dev_recv(int handle, void *buf, int len)
323 struct device_info *di;
324 int err = 0, act_len;
326 if (!dev_net_valid(handle))
327 return API_ENODEV;
329 di = &devices[handle];
330 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
331 return -1;
333 if (err)
334 return -1;
336 return act_len;
339 int ub_dev_send(int handle, void *buf, int len)
341 struct device_info *di;
342 int err = 0;
344 if (!dev_net_valid(handle))
345 return API_ENODEV;
347 di = &devices[handle];
348 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
349 return -1;
351 return err;
354 /****************************************
356 * env vars
358 ****************************************/
360 char * ub_env_get(const char *name)
362 char *value;
364 if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
365 return NULL;
367 return value;
370 void ub_env_set(const char *name, char *value)
372 syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
376 static char env_name[256];
378 const char * ub_env_enum(const char *last)
380 const char *env, *str;
381 int i;
383 env = NULL;
386 * It's OK to pass only the name piece as last (and not the whole
387 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
388 * internally, which handles such case
390 if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
391 return NULL;
393 if (!env)
394 /* no more env. variables to enumerate */
395 return NULL;
397 /* next enumerated env var */
398 memset(env_name, 0, 256);
399 for (i = 0, str = env; *str != '=' && *str != '\0';)
400 env_name[i++] = *str++;
402 env_name[i] = '\0';
404 return env_name;