]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/uboot/lib/glue.c
Re-sync loader.mk and ficl.mk to where they should be
[FreeBSD/FreeBSD.git] / stand / uboot / lib / glue.c
1 /*-
2  * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31
32 #include <crc32.h>
33 #include <stand.h>
34 #include "api_public.h"
35 #include "glue.h"
36
37 #ifdef DEBUG
38 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
39 #else
40 #define debugf(fmt, args...)
41 #endif
42
43 /* Some random address used by U-Boot. */
44 extern long uboot_address;
45
46 static int
47 valid_sig(struct api_signature *sig)
48 {
49         uint32_t checksum;
50         struct api_signature s;
51
52         if (sig == NULL)
53                 return (0);
54         /*
55          * Clear the checksum field (in the local copy) so as to calculate the
56          * CRC with the same initial contents as at the time when the sig was
57          * produced
58          */
59         s = *sig;
60         s.checksum = 0;
61
62         checksum = crc32((void *)&s, sizeof(struct api_signature));
63
64         if (checksum != sig->checksum)
65                 return (0);
66
67         return (1);
68 }
69
70 /*
71  * Checks to see if API signature's address was given to us as a command line
72  * argument by U-Boot.
73  *
74  * returns 1/0 depending on found/not found result
75  */
76 int
77 api_parse_cmdline_sig(int argc, char **argv, struct api_signature **sig)
78 {
79         unsigned long api_address;
80         int c;
81
82         api_address = 0;
83         opterr = 0;
84         optreset = 1;
85         optind = 1;
86
87         while ((c = getopt (argc, argv, "a:")) != -1)
88                 switch (c) {
89                 case 'a':
90                         api_address = strtoul(optarg, NULL, 16);
91                         break;
92                 default:
93                         break;
94                 }
95
96         if (api_address != 0) {
97                 *sig = (struct api_signature *)api_address;
98                 if (valid_sig(*sig))
99                         return (1);
100         }
101
102         return (0);
103 }
104
105 /*
106  * Searches for the U-Boot API signature
107  *
108  * returns 1/0 depending on found/not found result
109  */
110 int
111 api_search_sig(struct api_signature **sig)
112 {
113         unsigned char *sp, *spend;
114
115         if (sig == NULL)
116                 return (0);
117
118         if (uboot_address == 0)
119                 uboot_address = 255 * 1024 * 1024;
120
121         sp = (void *)(uboot_address & API_SIG_SEARCH_MASK);
122         spend = sp + API_SIG_SEARCH_LEN - API_SIG_MAGLEN;
123
124         while (sp < spend) {
125                 if (!bcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
126                         *sig = (struct api_signature *)sp;
127                         if (valid_sig(*sig))
128                                 return (1);
129                 }
130                 sp += API_SIG_MAGLEN;
131         }
132
133         *sig = NULL;
134         return (0);
135 }
136
137 /****************************************
138  *
139  * console
140  *
141  ****************************************/
142
143 int
144 ub_getc(void)
145 {
146         int c;
147
148         if (!syscall(API_GETC, NULL, &c))
149                 return (-1);
150
151         return (c);
152 }
153
154 int
155 ub_tstc(void)
156 {
157         int t;
158
159         if (!syscall(API_TSTC, NULL, &t))
160                 return (-1);
161
162         return (t);
163 }
164
165 void
166 ub_putc(const char c)
167 {
168
169         syscall(API_PUTC, NULL, &c);
170 }
171
172 void
173 ub_puts(const char *s)
174 {
175
176         syscall(API_PUTS, NULL, s);
177 }
178
179 /****************************************
180  *
181  * system
182  *
183  ****************************************/
184
185 void
186 ub_reset(void)
187 {
188
189         syscall(API_RESET, NULL);
190 }
191
192 static struct mem_region mr[UB_MAX_MR];
193 static struct sys_info si;
194
195 struct sys_info *
196 ub_get_sys_info(void)
197 {
198         int err = 0;
199
200         memset(&si, 0, sizeof(struct sys_info));
201         si.mr = mr;
202         si.mr_no = UB_MAX_MR;
203         memset(&mr, 0, sizeof(mr));
204
205         if (!syscall(API_GET_SYS_INFO, &err, &si))
206                 return (NULL);
207
208         return ((err) ? NULL : &si);
209 }
210
211 /****************************************
212  *
213  * timing
214  *
215  ****************************************/
216
217 void
218 ub_udelay(unsigned long usec)
219 {
220
221         syscall(API_UDELAY, NULL, &usec);
222 }
223
224 unsigned long
225 ub_get_timer(unsigned long base)
226 {
227         unsigned long cur;
228
229         if (!syscall(API_GET_TIMER, NULL, &cur, &base))
230                 return (0);
231
232         return (cur);
233 }
234
235 /****************************************************************************
236  *
237  * devices
238  *
239  * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
240  *
241  ***************************************************************************/
242
243 static struct device_info devices[UB_MAX_DEV];
244
245 struct device_info *
246 ub_dev_get(int i)
247 {
248
249         return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
250 }
251
252 /*
253  * Enumerates the devices: fills out device_info elements in the devices[]
254  * array.
255  *
256  * returns:             number of devices found
257  */
258 int
259 ub_dev_enum(void)
260 {
261         struct device_info *di;
262         int n = 0;
263
264         memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
265         di = &devices[0];
266
267         if (!syscall(API_DEV_ENUM, NULL, di))
268                 return (0);
269
270         while (di->cookie != NULL) {
271
272                 if (++n >= UB_MAX_DEV)
273                         break;
274
275                 /* take another device_info */
276                 di++;
277
278                 /* pass on the previous cookie */
279                 di->cookie = devices[n - 1].cookie;
280
281                 if (!syscall(API_DEV_ENUM, NULL, di))
282                         return (0);
283         }
284
285         return (n);
286 }
287
288 /*
289  * handle:      0-based id of the device
290  *
291  * returns:     0 when OK, err otherwise
292  */
293 int
294 ub_dev_open(int handle)
295 {
296         struct device_info *di;
297         int err = 0;
298
299         if (handle < 0 || handle >= UB_MAX_DEV)
300                 return (API_EINVAL);
301
302         di = &devices[handle];
303         if (!syscall(API_DEV_OPEN, &err, di))
304                 return (-1);
305
306         return (err);
307 }
308
309 int
310 ub_dev_close(int handle)
311 {
312         struct device_info *di;
313
314         if (handle < 0 || handle >= UB_MAX_DEV)
315                 return (API_EINVAL);
316
317         di = &devices[handle];
318         if (!syscall(API_DEV_CLOSE, NULL, di))
319                 return (-1);
320
321         return (0);
322 }
323
324 /*
325  * Validates device for read/write, it has to:
326  *
327  * - have sane handle
328  * - be opened
329  *
330  * returns:     0/1 accordingly
331  */
332 static int
333 dev_valid(int handle)
334 {
335
336         if (handle < 0 || handle >= UB_MAX_DEV)
337                 return (0);
338
339         if (devices[handle].state != DEV_STA_OPEN)
340                 return (0);
341
342         return (1);
343 }
344
345 static int
346 dev_stor_valid(int handle)
347 {
348
349         if (!dev_valid(handle))
350                 return (0);
351
352         if (!(devices[handle].type & DEV_TYP_STOR))
353                 return (0);
354
355         return (1);
356 }
357
358 int
359 ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
360     lbasize_t *rlen)
361 {
362         struct device_info *di;
363         lbasize_t act_len;
364         int err = 0;
365
366         if (!dev_stor_valid(handle))
367                 return (API_ENODEV);
368
369         di = &devices[handle];
370         if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
371                 return (API_ESYSC);
372
373         if (!err && rlen)
374                 *rlen = act_len;
375
376         return (err);
377 }
378
379 static int
380 dev_net_valid(int handle)
381 {
382
383         if (!dev_valid(handle))
384                 return (0);
385
386         if (devices[handle].type != DEV_TYP_NET)
387                 return (0);
388
389         return (1);
390 }
391
392 int
393 ub_dev_recv(int handle, void *buf, int len, int *rlen)
394 {
395         struct device_info *di;
396         int err = 0, act_len;
397
398         if (!dev_net_valid(handle))
399                 return (API_ENODEV);
400
401         di = &devices[handle];
402         if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
403                 return (API_ESYSC);
404
405         if (!err)
406                 *rlen = act_len;
407
408         return (err);
409 }
410
411 int
412 ub_dev_send(int handle, void *buf, int len)
413 {
414         struct device_info *di;
415         int err = 0;
416
417         if (!dev_net_valid(handle))
418                 return (API_ENODEV);
419
420         di = &devices[handle];
421         if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
422                 return (API_ESYSC);
423
424         return (err);
425 }
426
427 char *
428 ub_stor_type(int type)
429 {
430
431         if (type & DT_STOR_IDE)
432                 return ("IDE");
433
434         if (type & DT_STOR_SCSI)
435                 return ("SCSI");
436
437         if (type & DT_STOR_USB)
438                 return ("USB");
439
440         if (type & DT_STOR_MMC)
441                 return ("MMC");
442
443         if (type & DT_STOR_SATA)
444                 return ("SATA");
445
446         return ("Unknown");
447 }
448
449 char *
450 ub_mem_type(int flags)
451 {
452
453         switch (flags & 0x000F) {
454         case MR_ATTR_FLASH:
455                 return ("FLASH");
456         case MR_ATTR_DRAM:
457                 return ("DRAM");
458         case MR_ATTR_SRAM:
459                 return ("SRAM");
460         default:
461                 return ("Unknown");
462         }
463 }
464
465 void
466 ub_dump_di(int handle)
467 {
468         struct device_info *di = ub_dev_get(handle);
469         int i;
470
471         printf("device info (%d):\n", handle);
472         printf("  cookie\t= %p\n", di->cookie);
473         printf("  type\t\t= 0x%08x\n", di->type);
474
475         if (di->type == DEV_TYP_NET) {
476                 printf("  hwaddr\t= ");
477                 for (i = 0; i < 6; i++)
478                         printf("%02x ", di->di_net.hwaddr[i]);
479
480                 printf("\n");
481
482         } else if (di->type & DEV_TYP_STOR) {
483                 printf("  type\t\t= %s\n", ub_stor_type(di->type));
484                 printf("  blk size\t\t= %ld\n", di->di_stor.block_size);
485                 printf("  blk count\t\t= %ld\n", di->di_stor.block_count);
486         }
487 }
488
489 void
490 ub_dump_si(struct sys_info *si)
491 {
492         int i;
493
494         printf("sys info:\n");
495         printf("  clkbus\t= %ld MHz\n", si->clk_bus / 1000 / 1000);
496         printf("  clkcpu\t= %ld MHz\n", si->clk_cpu / 1000 / 1000);
497         printf("  bar\t\t= 0x%08lx\n", si->bar);
498
499         printf("---\n");
500         for (i = 0; i < si->mr_no; i++) {
501                 if (si->mr[i].flags == 0)
502                         break;
503
504                 printf("  start\t= 0x%08lx\n", si->mr[i].start);
505                 printf("  size\t= 0x%08lx\n", si->mr[i].size);
506                 printf("  type\t= %s\n", ub_mem_type(si->mr[i].flags));
507                 printf("---\n");
508         }
509 }
510
511 /****************************************
512  *
513  * env vars
514  *
515  ****************************************/
516
517 char *
518 ub_env_get(const char *name)
519 {
520         char *value;
521
522         if (!syscall(API_ENV_GET, NULL, name, &value))
523                 return (NULL);
524
525         return (value);
526 }
527
528 void
529 ub_env_set(const char *name, char *value)
530 {
531
532         syscall(API_ENV_SET, NULL, name, value);
533 }
534
535 static char env_name[256];
536
537 const char *
538 ub_env_enum(const char *last)
539 {
540         const char *env, *str;
541         int i;
542
543         /*
544          * It's OK to pass only the name piece as last (and not the whole
545          * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
546          * internally, which handles such case
547          */
548         env = NULL;
549         if (!syscall(API_ENV_ENUM, NULL, last, &env))
550                 return (NULL);
551
552         if (env == NULL || last == env)
553                 /* no more env. variables to enumerate */
554                 return (NULL);
555
556         /* next enumerated env var */
557         memset(env_name, 0, 256);
558         for (i = 0, str = env; *str != '=' && *str != '\0';)
559                 env_name[i++] = *str++;
560
561         env_name[i] = '\0';
562
563         return (env_name);
564 }