xref: /openbmc/u-boot/examples/api/demo.c (revision 849fc424)
1 /*
2  * (C) Copyright 2007-2008 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 #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
33 
34 #define BUF_SZ		2048
35 #define WAIT_SECS	5
36 
37 void	test_dump_buf(void *, int);
38 void	test_dump_di(int);
39 void	test_dump_si(struct sys_info *);
40 void	test_dump_sig(struct api_signature *);
41 
42 static char buf[BUF_SZ];
43 
44 int main(int argc, char * const argv[])
45 {
46 	int rv = 0, h, i, j, devs_no;
47 	struct api_signature *sig = NULL;
48 	ulong start, now;
49 	struct device_info *di;
50 	lbasize_t rlen;
51 	struct display_info disinfo;
52 
53 	if (!api_search_sig(&sig))
54 		return -1;
55 
56 	syscall_ptr = sig->syscall;
57 	if (syscall_ptr == NULL)
58 		return -2;
59 
60 	if (sig->version > API_SIG_VERSION)
61 		return -3;
62 
63 	printf("API signature found @%x\n", (unsigned int)sig);
64 	test_dump_sig(sig);
65 
66 	printf("\n*** Consumer API test ***\n");
67 	printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr,
68 		(unsigned int)&syscall_ptr);
69 
70 	/* console activities */
71 	ub_putc('B');
72 
73 	printf("*** Press any key to continue ***\n");
74 	printf("got char 0x%x\n", ub_getc());
75 
76 	/* system info */
77 	test_dump_si(ub_get_sys_info());
78 
79 	/* timing */
80 	printf("\n*** Timing - wait a couple of secs ***\n");
81 	start = ub_get_timer(0);
82 	printf("\ntime: start %lu\n\n", start);
83 	for (i = 0; i < WAIT_SECS; i++)
84 		for (j = 0; j < 1000; j++)
85 			ub_udelay(1000);	/* wait 1 ms */
86 
87 	/* this is the number of milliseconds that passed from ub_get_timer(0) */
88 	now = ub_get_timer(start);
89 	printf("\ntime: now %lu\n\n", now);
90 
91 	/* enumerate devices */
92 	printf("\n*** Enumerate devices ***\n");
93 	devs_no = ub_dev_enum();
94 
95 	printf("Number of devices found: %d\n", devs_no);
96 	if (devs_no == 0)
97 		return -1;
98 
99 	printf("\n*** Show devices ***\n");
100 	for (i = 0; i < devs_no; i++) {
101 		test_dump_di(i);
102 		printf("\n");
103 	}
104 
105 	printf("\n*** Operations on devices ***\n");
106 
107 	/* test opening a device already opened */
108 	h = 0;
109 	if ((rv = ub_dev_open(h)) != 0) {
110 		errf("open device %d error %d\n", h, rv);
111 		return -1;
112 	}
113 	if ((rv = ub_dev_open(h)) != 0)
114 		errf("open device %d error %d\n", h, rv);
115 
116 	ub_dev_close(h);
117 
118 	/* test storage */
119 	printf("Trying storage devices...\n");
120 	for (i = 0; i < devs_no; i++) {
121 		di = ub_dev_get(i);
122 
123 		if (di->type & DEV_TYP_STOR)
124 			break;
125 
126 	}
127 	if (i == devs_no)
128 		printf("No storage devices available\n");
129 	else {
130 		memset(buf, 0, BUF_SZ);
131 
132 		if ((rv = ub_dev_open(i)) != 0)
133 			errf("open device %d error %d\n", i, rv);
134 
135 		else if ((rv = ub_dev_read(i, buf, 1, 0, &rlen)) != 0)
136 			errf("could not read from device %d, error %d\n", i, rv);
137 		else {
138 			printf("Sector 0 dump (512B):\n");
139 			test_dump_buf(buf, 512);
140 		}
141 
142 		ub_dev_close(i);
143 	}
144 
145 	/* test networking */
146 	printf("Trying network devices...\n");
147 	for (i = 0; i < devs_no; i++) {
148 		di = ub_dev_get(i);
149 
150 		if (di->type == DEV_TYP_NET)
151 			break;
152 
153 	}
154 	if (i == devs_no)
155 		printf("No network devices available\n");
156 	else {
157 		if ((rv = ub_dev_open(i)) != 0)
158 			errf("open device %d error %d\n", i, rv);
159 		else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
160 			errf("could not send to device %d, error %d\n", i, rv);
161 
162 		ub_dev_close(i);
163 	}
164 
165 	if (ub_dev_close(h) != 0)
166 		errf("could not close device %d\n", h);
167 
168 	printf("\n*** Env vars ***\n");
169 
170 	printf("ethact = %s\n", ub_env_get("ethact"));
171 	printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
172 	ub_env_set("fileaddr", "deadbeef");
173 	printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
174 
175 	const char *env = NULL;
176 
177 	while ((env = ub_env_enum(env)) != NULL)
178 		printf("%s = %s\n", env, ub_env_get(env));
179 
180 	printf("\n*** Display ***\n");
181 
182 	if (ub_display_get_info(DISPLAY_TYPE_LCD, &disinfo)) {
183 		printf("LCD info: failed\n");
184 	} else {
185 		printf("LCD info:\n");
186 		printf("  pixel width:  %d\n", disinfo.pixel_width);
187 		printf("  pixel height: %d\n", disinfo.pixel_height);
188 		printf("  screen rows:  %d\n", disinfo.screen_rows);
189 		printf("  screen cols:  %d\n", disinfo.screen_cols);
190 	}
191 	if (ub_display_get_info(DISPLAY_TYPE_VIDEO, &disinfo)) {
192 		printf("video info: failed\n");
193 	} else {
194 		printf("video info:\n");
195 		printf("  pixel width:  %d\n", disinfo.pixel_width);
196 		printf("  pixel height: %d\n", disinfo.pixel_height);
197 		printf("  screen rows:  %d\n", disinfo.screen_rows);
198 		printf("  screen cols:  %d\n", disinfo.screen_cols);
199 	}
200 
201 	printf("*** Press any key to continue ***\n");
202 	printf("got char 0x%x\n", ub_getc());
203 
204 	/*
205 	 * This only clears messages on screen, not on serial port. It is
206 	 * equivalent to a no-op if no display is available.
207 	 */
208 	ub_display_clear();
209 
210 	/* reset */
211 	printf("\n*** Resetting board ***\n");
212 	ub_reset();
213 	printf("\nHmm, reset returned...?!\n");
214 
215 	return rv;
216 }
217 
218 void test_dump_sig(struct api_signature *sig)
219 {
220 	printf("signature:\n");
221 	printf("  version\t= %d\n", sig->version);
222 	printf("  checksum\t= 0x%08x\n", sig->checksum);
223 	printf("  sc entry\t= 0x%08x\n", (unsigned int)sig->syscall);
224 }
225 
226 void test_dump_si(struct sys_info *si)
227 {
228 	int i;
229 
230 	printf("sys info:\n");
231 	printf("  clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus);
232 	printf("  clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu);
233 	printf("  bar\t\t= 0x%08x\n", (unsigned int)si->bar);
234 
235 	printf("---\n");
236 	for (i = 0; i < si->mr_no; i++) {
237 		if (si->mr[i].flags == 0)
238 			break;
239 
240 		printf("  start\t= 0x%08lx\n", si->mr[i].start);
241 		printf("  size\t= 0x%08lx\n", si->mr[i].size);
242 
243 		switch(si->mr[i].flags & 0x000F) {
244 			case MR_ATTR_FLASH:
245 				printf("  type FLASH\n");
246 				break;
247 			case MR_ATTR_DRAM:
248 				printf("  type DRAM\n");
249 				break;
250 			case MR_ATTR_SRAM:
251 				printf("  type SRAM\n");
252 				break;
253 			default:
254 				printf("  type UNKNOWN\n");
255 		}
256 		printf("---\n");
257 	}
258 }
259 
260 static char *test_stor_typ(int type)
261 {
262 	if (type & DT_STOR_IDE)
263 		return "IDE";
264 
265 	if (type & DT_STOR_MMC)
266 		return "MMC";
267 
268 	if (type & DT_STOR_SATA)
269 		return "SATA";
270 
271 	if (type & DT_STOR_SCSI)
272 		return "SCSI";
273 
274 	if (type & DT_STOR_USB)
275 		return "USB";
276 
277 	return "Unknown";
278 }
279 
280 void test_dump_buf(void *buf, int len)
281 {
282 	int i;
283 	int line_counter = 0;
284 	int sep_flag = 0;
285 	int addr = 0;
286 
287 	printf("%07x:\t", addr);
288 
289 	for (i = 0; i < len; i++) {
290 		if (line_counter++ > 15) {
291 			line_counter = 0;
292 			sep_flag = 0;
293 			addr += 16;
294 			i--;
295 			printf("\n%07x:\t", addr);
296 			continue;
297 		}
298 
299 		if (sep_flag++ > 1) {
300 			sep_flag = 1;
301 			printf(" ");
302 		}
303 
304 		printf("%02x", *((char *)buf++));
305 	}
306 
307 	printf("\n");
308 }
309 
310 void test_dump_di(int handle)
311 {
312 	int i;
313 	struct device_info *di = ub_dev_get(handle);
314 
315 	printf("device info (%d):\n", handle);
316 	printf("  cookie\t= 0x%08x\n", (uint32_t)di->cookie);
317 	printf("  type\t\t= 0x%08x\n", di->type);
318 
319 	if (di->type == DEV_TYP_NET) {
320 		printf("  hwaddr\t= ");
321 		for (i = 0; i < 6; i++)
322 			printf("%02x ", di->di_net.hwaddr[i]);
323 
324 		printf("\n");
325 
326 	} else if (di->type & DEV_TYP_STOR) {
327 		printf("  type\t\t= %s\n", test_stor_typ(di->type));
328 		printf("  blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size);
329 		printf("  blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count);
330 	}
331 }
332