]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - api_examples/glue.c
200b1637396cee3e9620406ad907bec67cfa8044
[karo-tx-uboot.git] / api_examples / glue.c
1 /*
2  * (C) Copyright 2007 Semihalf
3  *
4  * Written by: Rafal Jaworowski <raj@semihalf.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
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.
13  *
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.
18  *
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
23  *
24  */
25
26 #include <common.h>
27 #include <linux/types.h>
28 #include <api_public.h>
29
30 #include "glue.h"
31
32 static int valid_sig(struct api_signature *sig)
33 {
34         uint32_t checksum;
35         struct api_signature s;
36
37         if (sig == NULL)
38                 return 0;
39         /*
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
43          */
44         s = *sig;
45         s.checksum = 0;
46
47         checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
48
49         if (checksum != sig->checksum)
50                 return 0;
51
52         return 1;
53 }
54
55 /*
56  * Searches for the U-Boot API signature
57  *
58  * returns 1/0 depending on found/not found result
59  */
60 int api_search_sig(struct api_signature **sig) {
61
62         unsigned char *sp;
63         uint32_t search_start = 0;
64         uint32_t search_end = 0;
65
66         if (sig == NULL)
67                 return 0;
68
69         if (search_hint == 0)
70                 search_hint = 255 * 1024 * 1024;
71
72         search_start = search_hint & ~0x000fffff;
73         search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
74
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;
79                         if (valid_sig(*sig))
80                                 return 1;
81                 }
82                 sp += API_SIG_MAGLEN;
83         }
84
85         *sig = NULL;
86         return 0;
87 }
88
89 /****************************************
90  *
91  * console
92  *
93  ****************************************/
94
95 int ub_getc(void)
96 {
97         int c;
98
99         if (!syscall(API_GETC, NULL, (uint32_t)&c))
100                 return -1;
101
102         return c;
103 }
104
105 int ub_tstc(void)
106 {
107         int t;
108
109         if (!syscall(API_TSTC, NULL, (uint32_t)&t))
110                 return -1;
111
112         return t;
113 }
114
115 void ub_putc(char c)
116 {
117         syscall(API_PUTC, NULL, (uint32_t)&c);
118 }
119
120 void ub_puts(const char *s)
121 {
122         syscall(API_PUTS, NULL, (uint32_t)s);
123 }
124
125 /****************************************
126  *
127  * system
128  *
129  ****************************************/
130
131 void ub_reset(void)
132 {
133         syscall(API_RESET, NULL);
134 }
135
136 #define MR_MAX 5
137 static struct mem_region mr[MR_MAX];
138 static struct sys_info si;
139
140 struct sys_info * ub_get_sys_info(void)
141 {
142         int err = 0;
143
144         memset(&si, 0, sizeof(struct sys_info));
145         si.mr = mr;
146         si.mr_no = MR_MAX;
147         memset(&mr, 0, sizeof(mr));
148
149         if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
150                 return NULL;
151
152         return ((err) ? NULL : &si);
153 }
154
155 /****************************************
156  *
157  * timing
158  *
159  ****************************************/
160
161 void ub_udelay(unsigned long usec)
162 {
163         syscall(API_UDELAY, NULL, &usec);
164 }
165
166 unsigned long ub_get_timer(unsigned long base)
167 {
168         unsigned long cur;
169
170         if (!syscall(API_GET_TIMER, NULL, &cur, &base))
171                 return 0;
172
173         return cur;
174 }
175
176
177 /****************************************************************************
178  *
179  * devices
180  *
181  * Devices are identified by handles: numbers 0, 1, 2, ..., MAX_DEVS-1
182  *
183  ***************************************************************************/
184
185 #define MAX_DEVS 6
186
187 static struct device_info devices[MAX_DEVS];
188
189 struct device_info * ub_dev_get(int i)
190 {
191         return ((i < 0 || i >= MAX_DEVS) ? NULL : &devices[i]);
192 }
193
194 /*
195  * Enumerates the devices: fills out device_info elements in the devices[]
196  * array.
197  *
198  * returns:             number of devices found
199  */
200 int ub_dev_enum(void)
201 {
202         struct device_info *di;
203         int n = 0;
204
205         memset(&devices, 0, sizeof(struct device_info) * MAX_DEVS);
206         di = &devices[0];
207
208         if (!syscall(API_DEV_ENUM, NULL, di))
209                 return 0;
210
211         while (di->cookie != NULL) {
212
213                 if (++n >= MAX_DEVS)
214                         break;
215
216                 /* take another device_info */
217                 di++;
218
219                 /* pass on the previous cookie */
220                 di->cookie = devices[n - 1].cookie;
221
222                 if (!syscall(API_DEV_ENUM, NULL, di))
223                         return 0;
224         }
225
226         return n;
227 }
228
229 /*
230  * handle:      0-based id of the device
231  *
232  * returns:     0 when OK, err otherwise
233  */
234 int ub_dev_open(int handle)
235 {
236         struct device_info *di;
237         int err = 0;
238
239         if (handle < 0 || handle >= MAX_DEVS)
240                 return API_EINVAL;
241
242         di = &devices[handle];
243
244         if (!syscall(API_DEV_OPEN, &err, di))
245                 return -1;
246
247         return err;
248 }
249
250 int ub_dev_close(int handle)
251 {
252         struct device_info *di;
253
254         if (handle < 0 || handle >= MAX_DEVS)
255                 return API_EINVAL;
256
257         di = &devices[handle];
258         if (!syscall(API_DEV_CLOSE, NULL, di))
259                 return -1;
260
261         return 0;
262 }
263
264 /*
265  *
266  * Validates device for read/write, it has to:
267  *
268  * - have sane handle
269  * - be opened
270  *
271  * returns:     0/1 accordingly
272  */
273 static int dev_valid(int handle)
274 {
275         if (handle < 0 || handle >= MAX_DEVS)
276                 return 0;
277
278         if (devices[handle].state != DEV_STA_OPEN)
279                 return 0;
280
281         return 1;
282 }
283
284 static int dev_stor_valid(int handle)
285 {
286         if (!dev_valid(handle))
287                 return 0;
288
289         if (!(devices[handle].type & DEV_TYP_STOR))
290                 return 0;
291
292         return 1;
293 }
294
295 int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start)
296 {
297         struct device_info *di;
298         lbasize_t act_len;
299         int err = 0;
300
301         if (!dev_stor_valid(handle))
302                 return API_ENODEV;
303
304         di = &devices[handle];
305         if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
306                 return -1;
307
308         if (err)
309                 return err;
310
311         if (act_len != len)
312                 return API_EIO;
313
314         return 0;
315 }
316
317 static int dev_net_valid(int handle)
318 {
319         if (!dev_valid(handle))
320                 return 0;
321
322         if (devices[handle].type != DEV_TYP_NET)
323                 return 0;
324
325         return 1;
326 }
327
328 int ub_dev_recv(int handle, void *buf, int len)
329 {
330         struct device_info *di;
331         int err = 0, act_len;
332
333         if (!dev_net_valid(handle))
334                 return API_ENODEV;
335
336         di = &devices[handle];
337         if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
338                 return -1;
339
340         if (err)
341                 return -1;
342
343         return act_len;
344 }
345
346 int ub_dev_send(int handle, void *buf, int len)
347 {
348         struct device_info *di;
349         int err = 0;
350
351         if (!dev_net_valid(handle))
352                 return API_ENODEV;
353
354         di = &devices[handle];
355         if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
356                 return -1;
357
358         return err;
359 }
360
361 /****************************************
362  *
363  * env vars
364  *
365  ****************************************/
366
367 char * ub_env_get(const char *name)
368 {
369         char *value;
370
371         if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
372                 return NULL;
373
374         return value;
375 }
376
377 void ub_env_set(const char *name, char *value)
378 {
379         syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
380 }
381
382
383 static char env_name[256];
384
385 const char * ub_env_enum(const char *last)
386 {
387         const char *env, *str;
388         int i;
389
390         env = NULL;
391
392         /*
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
396          */
397         if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
398                 return NULL;
399
400         if (!env)
401                 /* no more env. variables to enumerate */
402                 return NULL;
403
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++;
408
409         env_name[i] = '\0';
410
411         return env_name;
412 }