blob: d907e3f2875baef3da2a2c9ad3778e7de2564d36 [file] [log] [blame]
Rafal Jaworowskia19be782008-01-09 19:39:36 +01001/*
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +01002 * (C) Copyright 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
Rafal Jaworowskia19be782008-01-09 19:39:36 +01003 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
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.
11 *
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.
16 *
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,
20 * MA 02111-1307 USA
21 *
22 */
23
24#include <common.h>
25#include <linux/types.h>
26#include <api_public.h>
27
28#include "glue.h"
29
30static int valid_sig(struct api_signature *sig)
31{
32 uint32_t checksum;
33 struct api_signature s;
34
35 if (sig == NULL)
36 return 0;
37 /*
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
40 * produced
41 */
42 s = *sig;
43 s.checksum = 0;
44
45 checksum = crc32(0, (unsigned char *)&s, sizeof(struct api_signature));
46
47 if (checksum != sig->checksum)
48 return 0;
49
50 return 1;
51}
52
53/*
54 * Searches for the U-Boot API signature
55 *
56 * returns 1/0 depending on found/not found result
57 */
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +010058int api_search_sig(struct api_signature **sig)
59{
Rafal Jaworowskia19be782008-01-09 19:39:36 +010060 unsigned char *sp;
Rafal Jaworowski3d25ba52009-01-23 13:27:15 +010061 uint32_t search_start = 0;
62 uint32_t search_end = 0;
Rafal Jaworowskia19be782008-01-09 19:39:36 +010063
64 if (sig == NULL)
65 return 0;
66
Rafal Jaworowski3d25ba52009-01-23 13:27:15 +010067 if (search_hint == 0)
68 search_hint = 255 * 1024 * 1024;
69
70 search_start = search_hint & ~0x000fffff;
71 search_end = search_start + API_SEARCH_LEN - API_SIG_MAGLEN;
Rafal Jaworowskia19be782008-01-09 19:39:36 +010072
Rafal Jaworowski3d25ba52009-01-23 13:27:15 +010073 sp = (unsigned char *)search_start;
74 while ((sp + API_SIG_MAGLEN) < (unsigned char *)search_end) {
Rafal Jaworowskia19be782008-01-09 19:39:36 +010075 if (!memcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
76 *sig = (struct api_signature *)sp;
77 if (valid_sig(*sig))
78 return 1;
79 }
80 sp += API_SIG_MAGLEN;
81 }
82
83 *sig = NULL;
84 return 0;
85}
86
87/****************************************
88 *
89 * console
90 *
91 ****************************************/
92
93int ub_getc(void)
94{
95 int c;
96
97 if (!syscall(API_GETC, NULL, (uint32_t)&c))
98 return -1;
99
100 return c;
101}
102
103int ub_tstc(void)
104{
105 int t;
106
107 if (!syscall(API_TSTC, NULL, (uint32_t)&t))
108 return -1;
109
110 return t;
111}
112
113void ub_putc(char c)
114{
115 syscall(API_PUTC, NULL, (uint32_t)&c);
116}
117
118void ub_puts(const char *s)
119{
120 syscall(API_PUTS, NULL, (uint32_t)s);
121}
122
123/****************************************
124 *
125 * system
126 *
127 ****************************************/
128
129void ub_reset(void)
130{
131 syscall(API_RESET, NULL);
132}
133
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100134static struct mem_region mr[UB_MAX_MR];
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100135static struct sys_info si;
136
137struct sys_info * ub_get_sys_info(void)
138{
139 int err = 0;
140
141 memset(&si, 0, sizeof(struct sys_info));
142 si.mr = mr;
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100143 si.mr_no = UB_MAX_MR;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100144 memset(&mr, 0, sizeof(mr));
145
146 if (!syscall(API_GET_SYS_INFO, &err, (u_int32_t)&si))
147 return NULL;
148
149 return ((err) ? NULL : &si);
150}
151
152/****************************************
153 *
154 * timing
155 *
156 ****************************************/
Wolfgang Denk545b06a2008-01-10 00:55:14 +0100157
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100158void ub_udelay(unsigned long usec)
159{
160 syscall(API_UDELAY, NULL, &usec);
161}
162
163unsigned long ub_get_timer(unsigned long base)
164{
165 unsigned long cur;
166
167 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
168 return 0;
169
170 return cur;
171}
172
173
174/****************************************************************************
175 *
176 * devices
177 *
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100178 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100179 *
180 ***************************************************************************/
181
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100182static struct device_info devices[UB_MAX_DEV];
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100183
184struct device_info * ub_dev_get(int i)
185{
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100186 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100187}
188
189/*
190 * Enumerates the devices: fills out device_info elements in the devices[]
191 * array.
192 *
193 * returns: number of devices found
194 */
195int ub_dev_enum(void)
196{
197 struct device_info *di;
198 int n = 0;
199
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100200 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100201 di = &devices[0];
202
203 if (!syscall(API_DEV_ENUM, NULL, di))
204 return 0;
205
206 while (di->cookie != NULL) {
207
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100208 if (++n >= UB_MAX_DEV)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100209 break;
210
211 /* take another device_info */
212 di++;
213
214 /* pass on the previous cookie */
215 di->cookie = devices[n - 1].cookie;
216
217 if (!syscall(API_DEV_ENUM, NULL, di))
218 return 0;
219 }
220
221 return n;
222}
223
224/*
225 * handle: 0-based id of the device
226 *
227 * returns: 0 when OK, err otherwise
228 */
229int ub_dev_open(int handle)
230{
231 struct device_info *di;
232 int err = 0;
233
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100234 if (handle < 0 || handle >= UB_MAX_DEV)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100235 return API_EINVAL;
236
237 di = &devices[handle];
238
239 if (!syscall(API_DEV_OPEN, &err, di))
240 return -1;
241
242 return err;
243}
244
245int ub_dev_close(int handle)
246{
247 struct device_info *di;
248
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100249 if (handle < 0 || handle >= UB_MAX_DEV)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100250 return API_EINVAL;
251
252 di = &devices[handle];
253 if (!syscall(API_DEV_CLOSE, NULL, di))
254 return -1;
255
256 return 0;
257}
258
259/*
260 *
261 * Validates device for read/write, it has to:
262 *
263 * - have sane handle
264 * - be opened
265 *
266 * returns: 0/1 accordingly
267 */
268static int dev_valid(int handle)
269{
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100270 if (handle < 0 || handle >= UB_MAX_DEV)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100271 return 0;
272
273 if (devices[handle].state != DEV_STA_OPEN)
274 return 0;
275
276 return 1;
277}
278
279static int dev_stor_valid(int handle)
280{
281 if (!dev_valid(handle))
282 return 0;
283
284 if (!(devices[handle].type & DEV_TYP_STOR))
285 return 0;
286
287 return 1;
288}
289
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100290int ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
291 lbasize_t *rlen)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100292{
293 struct device_info *di;
294 lbasize_t act_len;
295 int err = 0;
296
297 if (!dev_stor_valid(handle))
298 return API_ENODEV;
299
300 di = &devices[handle];
301 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100302 return API_ESYSC;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100303
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100304 if (!err && rlen)
305 *rlen = act_len;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100306
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100307 return err;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100308}
309
310static int dev_net_valid(int handle)
311{
312 if (!dev_valid(handle))
313 return 0;
314
315 if (devices[handle].type != DEV_TYP_NET)
316 return 0;
317
318 return 1;
319}
320
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100321int ub_dev_recv(int handle, void *buf, int len, int *rlen)
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100322{
323 struct device_info *di;
324 int err = 0, act_len;
325
326 if (!dev_net_valid(handle))
327 return API_ENODEV;
328
329 di = &devices[handle];
330 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100331 return API_ESYSC;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100332
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100333 if (!err && rlen)
334 *rlen = act_len;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100335
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100336 return (err);
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100337}
338
339int ub_dev_send(int handle, void *buf, int len)
340{
341 struct device_info *di;
342 int err = 0;
343
344 if (!dev_net_valid(handle))
345 return API_ENODEV;
346
347 di = &devices[handle];
348 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
Rafal Jaworowski6feb82d2009-01-23 13:27:18 +0100349 return API_ESYSC;
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100350
351 return err;
352}
353
354/****************************************
355 *
356 * env vars
357 *
358 ****************************************/
359
360char * ub_env_get(const char *name)
361{
362 char *value;
363
364 if (!syscall(API_ENV_GET, NULL, (uint32_t)name, (uint32_t)&value))
365 return NULL;
366
367 return value;
368}
369
370void ub_env_set(const char *name, char *value)
371{
372 syscall(API_ENV_SET, NULL, (uint32_t)name, (uint32_t)value);
373}
374
Rafal Jaworowskia19be782008-01-09 19:39:36 +0100375static char env_name[256];
376
377const char * ub_env_enum(const char *last)
378{
379 const char *env, *str;
380 int i;
381
382 env = NULL;
383
384 /*
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
388 */
389 if (!syscall(API_ENV_ENUM, NULL, (uint32_t)last, (uint32_t)&env))
390 return NULL;
391
392 if (!env)
393 /* no more env. variables to enumerate */
394 return NULL;
395
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++;
400
401 env_name[i] = '\0';
402
403 return env_name;
404}
Che-Liang Chiou0d460412011-10-20 23:04:22 +0000405
406/****************************************
407 *
408 * display
409 *
410 ****************************************/
411
412int ub_display_get_info(int type, struct display_info *di)
413{
414 int err = 0;
415
416 if (!syscall(API_DISPLAY_GET_INFO, &err, (uint32_t)type, (uint32_t)di))
417 return API_ESYSC;
418
419 return err;
420}
421
422int ub_display_draw_bitmap(ulong bitmap, int x, int y)
423{
424 int err = 0;
425
426 if (!syscall(API_DISPLAY_DRAW_BITMAP, &err, bitmap, x, y))
427 return API_ESYSC;
428
429 return err;
430}
431
432void ub_display_clear(void)
433{
434 syscall(API_DISPLAY_CLEAR, NULL);
435}