xref: /freebsd/stand/uboot/glue.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
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 #include <sys/types.h>
29 
30 #include <zlib.h>
31 #include <stand.h>
32 #include "api_public.h"
33 #include "glue.h"
34 
35 #ifdef DEBUG
36 #define	debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
37 #else
38 #define	debugf(fmt, args...)
39 #endif
40 
41 /* Some random address used by U-Boot. */
42 extern long uboot_address;
43 
44 static int
45 valid_sig(struct api_signature *sig)
46 {
47 	uint32_t checksum;
48 	struct api_signature s;
49 
50 	if (sig == NULL)
51 		return (0);
52 	/*
53 	 * Clear the checksum field (in the local copy) so as to calculate the
54 	 * CRC with the same initial contents as at the time when the sig was
55 	 * produced
56 	 */
57 	s = *sig;
58 	s.checksum = crc32(0, Z_NULL, 0);
59 
60 	checksum = crc32(s.checksum, (void *)&s, sizeof(struct api_signature));
61 
62 	if (checksum != sig->checksum)
63 		return (0);
64 
65 	return (1);
66 }
67 
68 /*
69  * Checks to see if API signature's address was given to us as a command line
70  * argument by U-Boot.
71  *
72  * returns 1/0 depending on found/not found result
73  */
74 int
75 api_parse_cmdline_sig(int argc, char **argv, struct api_signature **sig)
76 {
77 	unsigned long api_address;
78 	int c;
79 
80 	api_address = 0;
81 	opterr = 0;
82 	optreset = 1;
83 	optind = 1;
84 
85 	while ((c = getopt (argc, argv, "a:")) != -1)
86 		switch (c) {
87 		case 'a':
88 			api_address = strtoul(optarg, NULL, 16);
89 			break;
90 		default:
91 			break;
92 		}
93 
94 	if (api_address != 0) {
95 		*sig = (struct api_signature *)api_address;
96 		if (valid_sig(*sig))
97 			return (1);
98 	}
99 
100 	return (0);
101 }
102 
103 /*
104  * Searches for the U-Boot API signature
105  *
106  * returns 1/0 depending on found/not found result
107  */
108 int
109 api_search_sig(struct api_signature **sig)
110 {
111 	unsigned char *sp, *spend;
112 
113 	if (sig == NULL)
114 		return (0);
115 
116 	if (uboot_address == 0)
117 		uboot_address = 255 * 1024 * 1024;
118 
119 	sp = (void *)(uboot_address & API_SIG_SEARCH_MASK);
120 	spend = sp + API_SIG_SEARCH_LEN - API_SIG_MAGLEN;
121 
122 	while (sp < spend) {
123 		if (!bcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
124 			*sig = (struct api_signature *)sp;
125 			if (valid_sig(*sig))
126 				return (1);
127 		}
128 		sp += API_SIG_MAGLEN;
129 	}
130 
131 	*sig = NULL;
132 	return (0);
133 }
134 
135 /****************************************
136  *
137  * console
138  *
139  ****************************************/
140 
141 int
142 ub_getc(void)
143 {
144 	int c;
145 
146 	if (!syscall(API_GETC, NULL, &c))
147 		return (-1);
148 
149 	return (c);
150 }
151 
152 int
153 ub_tstc(void)
154 {
155 	int t;
156 
157 	if (!syscall(API_TSTC, NULL, &t))
158 		return (-1);
159 
160 	return (t);
161 }
162 
163 void
164 ub_putc(const char c)
165 {
166 
167 	syscall(API_PUTC, NULL, &c);
168 }
169 
170 void
171 ub_puts(const char *s)
172 {
173 
174 	syscall(API_PUTS, NULL, s);
175 }
176 
177 /****************************************
178  *
179  * system
180  *
181  ****************************************/
182 
183 void
184 ub_reset(void)
185 {
186 
187 	syscall(API_RESET, NULL);
188 	while (1);	/* fallback if API_RESET failed */
189 	__unreachable();
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 }
565