]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - api_examples/demo.c
a4aeef183b53039edd94229aec5a62dfa7952390
[karo-tx-uboot.git] / api_examples / demo.c
1 /*
2  * (C) Copyright 2007 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 void    test_dump_si(struct sys_info *);
35 void    test_dump_di(int);
36 void    test_dump_sig(struct api_signature *);
37
38 char buf[2048];
39
40 #define WAIT_SECS 5
41
42 int main(int argc, char *argv[])
43 {
44         int rv = 0;
45         int h, i, j;
46         int devs_no;
47         struct api_signature *sig = NULL;
48         ulong start, now;
49         struct device_info *di;
50
51         if (!api_search_sig(&sig))
52                 return -1;
53
54         syscall_ptr = sig->syscall;
55         if (syscall_ptr == NULL)
56                 return -2;
57
58         if (sig->version > API_SIG_VERSION)
59                 return -3;
60
61         printf("API signature found @%x\n", sig);
62         test_dump_sig(sig);
63
64         printf("\n*** Consumer API test ***\n");
65         printf("syscall ptr 0x%08x@%08x\n", syscall_ptr, &syscall_ptr);
66
67         /* console activities */
68         ub_putc('B');
69
70         printf("*** Press any key to continue ***\n");
71         printf("got char 0x%x\n", ub_getc());
72
73         /* system info */
74         test_dump_si(ub_get_sys_info());
75
76         /* timing */
77         printf("\n*** Timing - wait a couple of secs ***\n");
78         start = ub_get_timer(0);
79         printf("\ntime: start %lu\n\n", start);
80         for (i = 0; i < WAIT_SECS; i++)
81                 for (j = 0; j < 1000; j++)
82                         ub_udelay(1000);        /* wait 1 ms */
83
84         /* this is the number of milliseconds that passed from ub_get_timer(0) */
85         now = ub_get_timer(start);
86         printf("\ntime: now %lu\n\n", now);
87
88         /* enumerate devices */
89         printf("\n*** Enumerate devices ***\n");
90         devs_no = ub_dev_enum();
91         
92         printf("Number of devices found: %d\n", devs_no);
93         if (devs_no == 0)
94                 return -1;
95
96
97         printf("\n*** Show devices ***\n");
98         for (i = 0; i < devs_no; i++) {
99                 test_dump_di(i);
100                 printf("\n");
101         }
102
103         printf("\n*** Operations on devices ***\n");
104
105         /* test opening a device already opened */
106         h = 0;
107         if ((rv = ub_dev_open(h)) != 0) {
108                 errf("open device %d error %d\n", h, rv);
109                 return -1;
110         }
111         if ((rv = ub_dev_open(h)) != 0)
112                 errf("open device %d error %d\n", h, rv);
113
114         ub_dev_close(h);
115
116         /* test storage */
117         printf("Trying storage devices...\n");
118         for (i = 0; i < devs_no; i++) {
119                 di = ub_dev_get(i);
120
121                 if (di->type & DEV_TYP_STOR)
122                         break;
123
124         }
125         if (i == devs_no)
126                 printf("No storage devices available\n");
127         else {
128                 if ((rv = ub_dev_open(i)) != 0)
129                         errf("open device %d error %d\n", i, rv);
130                 else if ((rv = ub_dev_read(i, &buf, 200, 20)) != 0)
131                         errf("could not read from device %d, error %d\n", i, rv);
132
133                 ub_dev_close(i);
134         }
135
136         /* test networking */
137         printf("Trying network devices...\n");
138         for (i = 0; i < devs_no; i++) {
139                 di = ub_dev_get(i);
140
141                 if (di->type == DEV_TYP_NET)
142                         break;
143
144         }
145         if (i == devs_no)
146                 printf("No network devices available\n");
147         else {
148                 if ((rv = ub_dev_open(i)) != 0)
149                         errf("open device %d error %d\n", i, rv);
150                 else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
151                         errf("could not send to device %d, error %d\n", i, rv);
152
153                 ub_dev_close(i);
154         }
155
156         if (ub_dev_close(h) != 0)
157                 errf("could not close device %d\n", h);
158
159         printf("\n*** Env vars ***\n");
160
161         printf("ethact = %s\n", ub_env_get("ethact"));
162         printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
163         ub_env_set("fileaddr", "deadbeef");
164         printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
165
166         const char *env = NULL;
167
168         while ((env = ub_env_enum(env)) != NULL)
169                 printf("%s = %s\n", env, ub_env_get(env));
170
171         /* reset */
172         ub_reset();
173         printf("\nHmm, reset returned...?!\n");
174
175         return rv;
176 }
177
178 void test_dump_sig(struct api_signature *sig)
179 {
180         printf("signature:\n");
181         printf("  version\t= %d\n", sig->version);
182         printf("  checksum\t= 0x%08x\n", sig->checksum);
183         printf("  sc entry\t= 0x%08x\n", sig->syscall);
184 }
185
186 void test_dump_si(struct sys_info *si)
187 {
188         int i;
189
190         printf("sys info:\n");
191         printf("  clkbus\t= 0x%08x\n", si->clk_bus);
192         printf("  clkcpu\t= 0x%08x\n", si->clk_cpu);
193         printf("  bar\t\t= 0x%08x\n", si->bar);
194
195         printf("---\n");
196         for (i = 0; i < si->mr_no; i++) {
197                 if (si->mr[i].flags == 0)
198                         break;
199
200                 printf("  start\t= 0x%08lx\n", si->mr[i].start);
201                 printf("  size\t= 0x%08lx\n", si->mr[i].size);
202
203                 switch(si->mr[i].flags & 0x000F) {
204                         case MR_ATTR_FLASH:
205                                 printf("  type FLASH\n");
206                                 break;
207                         case MR_ATTR_DRAM:
208                                 printf("  type DRAM\n");
209                                 break;
210                         case MR_ATTR_SRAM:
211                                 printf("  type SRAM\n");
212                                 break;
213                         default:
214                                 printf("  type UNKNOWN\n");
215                 }
216                 printf("---\n");
217         }
218 }
219
220 static char * test_stor_typ(int type)
221 {
222         if (type & DT_STOR_IDE)
223                 return "IDE";
224
225         if (type & DT_STOR_SCSI)
226                 return "SCSI";
227
228         if (type & DT_STOR_USB)
229                 return "USB";
230
231         if (type & DT_STOR_MMC);
232                 return "MMC";
233
234         return "Unknown";
235 }
236
237 void test_dump_di(int handle)
238 {
239         int i;
240         struct device_info *di = ub_dev_get(handle);
241
242         printf("device info (%d):\n", handle);
243         printf("  cookie\t= 0x%08x\n", (uint32_t)di->cookie);
244         printf("  type\t\t= 0x%08x\n", di->type);
245
246         if (di->type == DEV_TYP_NET) {
247                 printf("  hwaddr\t= ");
248                 for (i = 0; i < 6; i++)
249                         printf("%02x ", di->di_net.hwaddr[i]);
250
251                 printf("\n");
252
253         } else if (di->type & DEV_TYP_STOR) {
254                 printf("  type\t\t= %s\n", test_stor_typ(di->type));
255                 printf("  blk size\t\t= %d\n", di->di_stor.block_size);
256                 printf("  blk count\t\t= %d\n", di->di_stor.block_count);
257         }
258 }