1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <config.h>
27#include <command.h>
28#include <common.h>
29#include <malloc.h>
30#include <environment.h>
31#include <linux/types.h>
32#include <api_public.h>
33
34#include "api_private.h"
35
36#define DEBUG
37#undef DEBUG
38
39
40extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#ifdef DEBUG
57#define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
58#else
59#define debugf(fmt, args...)
60#endif
61
62typedef int (*cfp_t)(va_list argp);
63
64static int calls_no;
65
66
67
68
69
70
71static int API_getc(va_list ap)
72{
73 int *c;
74
75 if ((c = (int *)va_arg(ap, u_int32_t)) == NULL)
76 return API_EINVAL;
77
78 *c = getc();
79 return 0;
80}
81
82
83
84
85
86
87static int API_tstc(va_list ap)
88{
89 int *t;
90
91 if ((t = (int *)va_arg(ap, u_int32_t)) == NULL)
92 return API_EINVAL;
93
94 *t = tstc();
95 return 0;
96}
97
98
99
100
101
102
103static int API_putc(va_list ap)
104{
105 char *c;
106
107 if ((c = (char *)va_arg(ap, u_int32_t)) == NULL)
108 return API_EINVAL;
109
110 putc(*c);
111 return 0;
112}
113
114
115
116
117
118
119static int API_puts(va_list ap)
120{
121 char *s;
122
123 if ((s = (char *)va_arg(ap, u_int32_t)) == NULL)
124 return API_EINVAL;
125
126 puts(s);
127 return 0;
128}
129
130
131
132
133
134
135static int API_reset(va_list ap)
136{
137 do_reset(NULL, 0, 0, NULL);
138
139
140 return 0;
141}
142
143
144
145
146
147
148
149
150
151static int API_get_sys_info(va_list ap)
152{
153 struct sys_info *si;
154
155 si = (struct sys_info *)va_arg(ap, u_int32_t);
156 if (si == NULL)
157 return API_ENOMEM;
158
159 return (platform_sys_info(si)) ? 0 : API_ENODEV;
160}
161
162
163
164
165
166
167static int API_udelay(va_list ap)
168{
169 unsigned long *d;
170
171 if ((d = (unsigned long *)va_arg(ap, u_int32_t)) == NULL)
172 return API_EINVAL;
173
174 udelay(*d);
175 return 0;
176}
177
178
179
180
181
182
183static int API_get_timer(va_list ap)
184{
185 unsigned long *base, *cur;
186
187 cur = (unsigned long *)va_arg(ap, u_int32_t);
188 if (cur == NULL)
189 return API_EINVAL;
190
191 base = (unsigned long *)va_arg(ap, u_int32_t);
192 if (base == NULL)
193 return API_EINVAL;
194
195 *cur = get_timer(*base);
196 return 0;
197}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217static int API_dev_enum(va_list ap)
218{
219 struct device_info *di;
220
221
222 di = (struct device_info *)va_arg(ap, u_int32_t);
223 if (di == NULL)
224 return API_EINVAL;
225
226 if (di->cookie == NULL) {
227
228 dev_enum_reset();
229 debugf("RESTART ENUM\n");
230
231
232 if (dev_enum_net(di))
233 return 0;
234 }
235
236
237
238
239
240
241
242 if (!dev_enum_storage(di))
243
244 di->cookie = NULL;
245
246 return 0;
247}
248
249
250static int API_dev_open(va_list ap)
251{
252 struct device_info *di;
253 int err = 0;
254
255
256 di = (struct device_info *)va_arg(ap, u_int32_t);
257 if (di == NULL)
258 return API_EINVAL;
259
260
261 if (di->state == DEV_STA_OPEN)
262 return API_EBUSY;
263
264 if (di->cookie == NULL)
265 return API_ENODEV;
266
267 if (di->type & DEV_TYP_STOR)
268 err = dev_open_stor(di->cookie);
269
270 else if (di->type & DEV_TYP_NET)
271 err = dev_open_net(di->cookie);
272 else
273 err = API_ENODEV;
274
275 if (!err)
276 di->state = DEV_STA_OPEN;
277
278 return err;
279}
280
281
282static int API_dev_close(va_list ap)
283{
284 struct device_info *di;
285 int err = 0;
286
287
288 di = (struct device_info *)va_arg(ap, u_int32_t);
289 if (di == NULL)
290 return API_EINVAL;
291
292 if (di->state == DEV_STA_CLOSED)
293 return 0;
294
295 if (di->cookie == NULL)
296 return API_ENODEV;
297
298 if (di->type & DEV_TYP_STOR)
299 err = dev_close_stor(di->cookie);
300
301 else if (di->type & DEV_TYP_NET)
302 err = dev_close_net(di->cookie);
303 else
304
305
306
307
308 err = API_ENODEV;
309
310 if (!err)
311 di->state = DEV_STA_CLOSED;
312
313 return err;
314}
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334static int API_dev_write(va_list ap)
335{
336 struct device_info *di;
337 void *buf;
338 int *len;
339 int err = 0;
340
341
342 di = (struct device_info *)va_arg(ap, u_int32_t);
343 if (di == NULL)
344 return API_EINVAL;
345
346
347
348 if (di->cookie == NULL)
349 return API_ENODEV;
350
351
352 buf = (void *)va_arg(ap, u_int32_t);
353 if (buf == NULL)
354 return API_EINVAL;
355
356
357 len = (int *)va_arg(ap, u_int32_t);
358 if (len == NULL)
359 return API_EINVAL;
360 if (*len <= 0)
361 return API_EINVAL;
362
363 if (di->type & DEV_TYP_STOR)
364
365
366
367
368 return API_ENODEV;
369
370 else if (di->type & DEV_TYP_NET)
371 err = dev_write_net(di->cookie, buf, *len);
372 else
373 err = API_ENODEV;
374
375 return err;
376}
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401static int API_dev_read(va_list ap)
402{
403 struct device_info *di;
404 void *buf;
405 lbasize_t *len_stor, *act_len_stor;
406 lbastart_t *start;
407 int *len_net, *act_len_net;
408
409
410 di = (struct device_info *)va_arg(ap, u_int32_t);
411 if (di == NULL)
412 return API_EINVAL;
413
414
415
416 if (di->cookie == NULL)
417 return API_ENODEV;
418
419
420 buf = (void *)va_arg(ap, u_int32_t);
421 if (buf == NULL)
422 return API_EINVAL;
423
424 if (di->type & DEV_TYP_STOR) {
425
426 len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
427 if (!len_stor)
428 return API_EINVAL;
429 if (*len_stor <= 0)
430 return API_EINVAL;
431
432
433 start = (lbastart_t *)va_arg(ap, u_int32_t);
434
435
436 act_len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
437 if (!act_len_stor)
438 return API_EINVAL;
439
440 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
441
442 } else if (di->type & DEV_TYP_NET) {
443
444
445 len_net = (int *)va_arg(ap, u_int32_t);
446 if (!len_net)
447 return API_EINVAL;
448 if (*len_net <= 0)
449 return API_EINVAL;
450
451
452 act_len_net = (int *)va_arg(ap, u_int32_t);
453 if (!act_len_net)
454 return API_EINVAL;
455
456 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
457
458 } else
459 return API_ENODEV;
460
461 return 0;
462}
463
464
465
466
467
468
469
470
471
472static int API_env_get(va_list ap)
473{
474 char *name, **value;
475
476 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
477 return API_EINVAL;
478 if ((value = (char **)va_arg(ap, u_int32_t)) == NULL)
479 return API_EINVAL;
480
481 *value = getenv(name);
482
483 return 0;
484}
485
486
487
488
489
490
491
492
493
494
495static int API_env_set(va_list ap)
496{
497 char *name, *value;
498
499 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
500 return API_EINVAL;
501 if ((value = (char *)va_arg(ap, u_int32_t)) == NULL)
502 return API_EINVAL;
503
504 setenv(name, value);
505
506 return 0;
507}
508
509
510
511
512
513
514
515
516static int API_env_enum(va_list ap)
517{
518 int i, n;
519 char *last, **next;
520
521 last = (char *)va_arg(ap, u_int32_t);
522
523 if ((next = (char **)va_arg(ap, u_int32_t)) == NULL)
524 return API_EINVAL;
525
526 if (last == NULL)
527
528 *next = ((char *)env_get_addr(0));
529 else {
530 *next = last;
531
532 for (i = 0; env_get_char(i) != '\0'; i = n + 1) {
533 for (n = i; env_get_char(n) != '\0'; ++n) {
534 if (n >= CONFIG_ENV_SIZE) {
535
536 return 0;
537 }
538 }
539
540 if (envmatch((uchar *)last, i) < 0)
541 continue;
542
543
544 i = n + 1;
545 if (env_get_char(i) == '\0') {
546
547 *next = NULL;
548 return 0;
549 }
550
551 *next = ((char *)env_get_addr(i));
552 return 0;
553 }
554 }
555
556 return 0;
557}
558
559static cfp_t calls_table[API_MAXCALL] = { NULL, };
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577int syscall(int call, int *retval, ...)
578{
579 va_list ap;
580 int rv;
581
582 if (call < 0 || call >= calls_no) {
583 debugf("invalid call #%d\n", call);
584 return 0;
585 }
586
587 if (calls_table[call] == NULL) {
588 debugf("syscall #%d does not have a handler\n", call);
589 return 0;
590 }
591
592 va_start(ap, retval);
593 rv = calls_table[call](ap);
594 if (retval != NULL)
595 *retval = rv;
596
597 return 1;
598}
599
600void api_init(void)
601{
602 struct api_signature *sig = NULL;
603
604
605 calls_table[API_RSVD] = NULL;
606 calls_table[API_GETC] = &API_getc;
607 calls_table[API_PUTC] = &API_putc;
608 calls_table[API_TSTC] = &API_tstc;
609 calls_table[API_PUTS] = &API_puts;
610 calls_table[API_RESET] = &API_reset;
611 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
612 calls_table[API_UDELAY] = &API_udelay;
613 calls_table[API_GET_TIMER] = &API_get_timer;
614 calls_table[API_DEV_ENUM] = &API_dev_enum;
615 calls_table[API_DEV_OPEN] = &API_dev_open;
616 calls_table[API_DEV_CLOSE] = &API_dev_close;
617 calls_table[API_DEV_READ] = &API_dev_read;
618 calls_table[API_DEV_WRITE] = &API_dev_write;
619 calls_table[API_ENV_GET] = &API_env_get;
620 calls_table[API_ENV_SET] = &API_env_set;
621 calls_table[API_ENV_ENUM] = &API_env_enum;
622 calls_no = API_MAXCALL;
623
624 debugf("API initialized with %d calls\n", calls_no);
625
626 dev_stor_init();
627
628
629
630
631 sig = malloc(sizeof(struct api_signature));
632 if (sig == NULL) {
633 printf("API: could not allocate memory for the signature!\n");
634 return;
635 }
636
637 debugf("API sig @ 0x%08x\n", sig);
638 memcpy(sig->magic, API_SIG_MAGIC, 8);
639 sig->version = API_SIG_VERSION;
640 sig->syscall = &syscall;
641 sig->checksum = 0;
642 sig->checksum = crc32(0, (unsigned char *)sig,
643 sizeof(struct api_signature));
644 debugf("syscall entry: 0x%08x\n", sig->syscall);
645}
646
647void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
648 int flags)
649{
650 int i;
651
652 if (!si->mr || !size || (flags == 0))
653 return;
654
655
656 for (i = 0; i < si->mr_no; i++)
657 if (si->mr[i].flags == 0) {
658
659 si->mr[i].start = start;
660 si->mr[i].size = size;
661 si->mr[i].flags = flags;
662 return;
663 }
664}
665