summaryrefslogtreecommitdiff
path: root/sys/arch/alpha/stand/standtest/test.c
blob: d26c9d9a3bf808bbb0f9e12e9fe907c564c0608d (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* $NetBSD: test.c,v 1.7 2016/06/11 06:26:50 dholland Exp $ */

/*
 * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Christopher G. Demetriou
 *	for the NetBSD Project.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>
#include <machine/autoconf.h>
#include <machine/rpb.h>

#include "../common/common.h"

struct cmdtab {
	const char *cmd;
	void (*fn)(const char *buf);
};

int		done;
unsigned long	arg_pfn, arg_ptb, arg_bim, arg_bip, arg_biv;

const char *advance_past_space(const char *buf);
const char *cvt_number(const char *buf, u_int64_t *nump);
int	dispatch_cmd(const char *buf, const struct cmdtab *cmds);
#define		DISPATCH_CMD_NOCMD	0
#define		DISPATCH_CMD_MATCHED	1
#define		DISPATCH_CMD_NOMATCH	2
#define		DISPATCH_CMD_AMBIGUOUS	3
void	print_cmds(const struct cmdtab *cmds, const char *match,
	    size_t matchlen);
void	print_stringarray(const char *s, size_t maxlen);

void	toplevel_dpb(const char *buf);
void	toplevel_dpl(const char *buf);
void	toplevel_dpq(const char *buf);
void	toplevel_dpw(const char *buf);
void	toplevel_dvb(const char *buf);
void	toplevel_dvl(const char *buf);
void	toplevel_dvq(const char *buf);
void	toplevel_dvw(const char *buf);
void	toplevel_halt(const char *buf);
void	toplevel_help(const char *buf);
void	toplevel_show(const char *buf);

void	show_args(const char *buf);
void	show_bootinfo(const char *buf);
void	show_pt(const char *buf);
void	show_rpb(const char *buf);

void
main(unsigned long pfn, unsigned long ptb, unsigned long bim, unsigned long bip, unsigned long biv)
	/* pfn:	 first free PFN number */
	/* ptb:	 PFN of current level 1 page table */
	/* bim:	 bootinfo magic */
	/* bip:	 bootinfo pointer */
	/* biv:	 bootinfo version */
{
	char input_buf[512];
	static const struct cmdtab toplevel_cmds[] = {
	    {	"?",		toplevel_help,	},
#if 0 /* XXX notyet */
	    {	"dpb",		toplevel_dpb,	},
	    {	"dpl",		toplevel_dpl,	},
	    {	"dpq",		toplevel_dpq,	},
	    {	"dpw",		toplevel_dpw,	},
	    {	"dvb",		toplevel_dvb,	},
	    {	"dvl",		toplevel_dvl,	},
	    {	"dvq",		toplevel_dvq,	},
	    {	"dvw",		toplevel_dvw,	},
#endif
	    {	"quit",		toplevel_halt,	},
	    {	"show",		toplevel_show,	},
	    {	NULL,				},
	};

	printf("\n");
	printf("NetBSD/alpha " NETBSD_VERS
	    " Standalone Test Program, Revision %s\n", bootprog_rev);
	printf("\n");

	arg_pfn = pfn;
	arg_ptb = ptb;
	arg_bim = bim;
	arg_bip = bip;
	arg_biv = biv;

	printf("Enter '?' for help.\n");
	printf("\n");

	do {
		printf("test> ");
		kgets(input_buf, sizeof(input_buf));

		dispatch_cmd(input_buf, toplevel_cmds);
	} while (!done);

	printf("\n");
	printf("halting...\n");
	halt();
}

const char *
advance_past_space(const char *buf)
{

	/* advance past white space. */
	while (isspace(*buf))
		buf++;

	if (*buf == '\0')
		return NULL;
	return buf;
}

const char *
cvt_number(const char *buf, u_int64_t *nump)
{
	int base;
	unsigned char c;

	base = 10;
	*nump = 0;

	c = *buf;
	if (c == '0') {
		c = *(++buf);

		if (c == 'x' || c == 'X') {
			base = 16;
			buf++;
		} else {
			base = 8;
		}
	}

	for (c = *buf; c != '\0' && !isspace(c); c = *(++buf)) {
		switch (base) {
		case 10:
			if (c < '0' || c > '9')
				goto done;
		}
	}
done:

}

int
dispatch_cmd(const char *buf, const struct cmdtab *cmds)
{
	const struct cmdtab *try, *winner;
	size_t nonwhitespace, i;
	unsigned int nmatches;
	const char *pre, *post;
	int rv;

	/* advance past white space. */
	buf = advance_past_space(buf);
	if (buf == NULL)
		return (DISPATCH_CMD_NOCMD);

	/* find how much non-white space there is. */
	nonwhitespace = 0;
	while ((buf[nonwhitespace] != '\0') && !isspace(buf[nonwhitespace]))
		nonwhitespace++;

	/* at this point, nonwhitespace should always be non-zero */
	if (nonwhitespace == 0) {
		printf("assertion failed: dispatch_cmd: nonwhitespace == 0\n");
		halt();
	}

	/* see how many matches there were. */
	for (nmatches = 0, try = cmds;
	    try != NULL && try->cmd != NULL;
	    try++) {
		if (strncmp(buf, try->cmd, nonwhitespace) == 0) {
			winner = try;
			nmatches++;
		}
	}

	if (nmatches == 1) {
		(*winner->fn)(buf + nonwhitespace);
		return (DISPATCH_CMD_MATCHED);
	} else if (nmatches == 0) {
		pre = "invalid command word";
		post = "allowed words";
		rv = DISPATCH_CMD_NOMATCH;
	} else {
		pre = "ambiguous command word";
		post = "matches";
		rv = DISPATCH_CMD_AMBIGUOUS;
	}

	printf("%s \"", pre);
	print_stringarray(buf, nonwhitespace);
	printf("\", %s:\n", post);

	/* print commands.  if no match, print all commands. */
	print_cmds(cmds, buf, rv == DISPATCH_CMD_NOMATCH ? 0 : nonwhitespace);
	return (rv);
}

void
print_cmds(const struct cmdtab *cmds, const char *match, size_t matchlen)
{
	const struct cmdtab *try;

	printf("    ");
	for (try = cmds; try != NULL && try->cmd != NULL; try++) {
		if (strncmp(match, try->cmd, matchlen) == 0)
			printf("%s%s", try != cmds ? ", " : "", try->cmd);
	}
	printf("\n");
}

void
print_stringarray(const char *s, size_t maxlen)
{
	size_t i;

	for (i = 0; (i < maxlen) && (*s != '\0'); i++, s++)
		putchar(*s);
}

void
warn_ignored_args(const char *buf, const char *cmd)
{

	if (advance_past_space(buf) != NULL)
		printf("WARNING: extra arguments to \"%s\" command ignored\n",
		    cmd);
}


/*
 * Top-level Commands
 */

void
toplevel_dpb(const char *buf)
{
	u_int64_t startaddr, count = 1;

	buf = advance_past_space(buf);
	if (buf == NULL) {
		printf("\"dpb\" must be given starting address\n");
		return;
	}
	buf = cvt_number(buf, &startaddr);
	if (*buf != '\0' && !isspace(*buf)) {
		printf("bad character '%c' in starting address\n", *buf);
		return;
	}

	buf = advance_past_space(buf);
	if (buf != NULL) {
		buf = cvt_number(buf, &count);
		if (*buf != '\0' && !isspace(*buf)) {
			printf("bad character '%c' in count\n", *buf);
			return;
		}
		buf = advance_past_space(buf);
		if (buf != NULL) {
			printf("extra args at end of \"dpb\" command\n");
			return;
		}
	}

	printf("startaddr = 0x%lx, count = 0x%lx\n", startaddr, count);
	printf("\"dpb\" not yet implemented\n");
}

void
toplevel_dpl(const char *buf)
{

	printf("\"dpl\" not yet implemented\n");
}

void
toplevel_dpq(const char *buf)
{

	printf("\"dpq\" not yet implemented\n");
}

void
toplevel_dpw(const char *buf)
{

	printf("\"dpw\" not yet implemented\n");
}

void
toplevel_dvb(const char *buf)
{

	printf("\"dvb\" not yet implemented\n");
}

void
toplevel_dvl(const char *buf)
{

	printf("\"dvl\" not yet implemented\n");
}

void
toplevel_dvq(const char *buf)
{

	printf("\"dvq\" not yet implemented\n");
}

void
toplevel_dvw(const char *buf)
{

	printf("\"dvw\" not yet implemented\n");
}

void
toplevel_halt(const char *buf)
{

	warn_ignored_args(buf, "halt");

	done = 1;
}

void
toplevel_help(const char *buf)
{

	warn_ignored_args(buf, "?");

	printf("Standalone Test Program Commands:\n");
	printf("    ?                       print help\n");
	printf("    quit                    return to console\n");
#if 0 /* XXX notyet */
	printf("    dpb startaddr [count]   display physical memory "
	    "(8-bit units)\n");
	printf("    dpw startaddr [count]   display physical memory "
	    "(16-bit units)\n");
	printf("    dpl startaddr [count]   display physical memory "
	    "(32-bit units)\n");
	printf("    dpq startaddr [count]   display physical memory "
	    "(64-bit units)\n");
	printf("    dvb startaddr [count]   display virtual memory "
	    "(8-bit units)\n");
	printf("    dvw startaddr [count]   display virtual memory "
	    "(16-bit units)\n");
	printf("    dvl startaddr [count]   display virtual memory "
	    "(32-bit units)\n");
	printf("    dvq startaddr [count]   display virtual memory "
	    "(64-bit units)\n");
#endif
	printf("    show args               show test program arguments\n");
	printf("    show bootinfo           show bootstrap bootinfo\n");
#if 0 /* XXX notyet */
	printf("    show pt [startaddr [endaddr]]\n");
	printf("                            show page tables\n");
	printf("    show rpb                show the HWRPB\n");
	printf("\n");
	printf("If optional \"count\" argument is omitted, 1 is used.\n");
	printf("If optional \"startaddr\" argument is omitted, "
	    "0x0 is used.\n");
	printf("If optional \"endaddr\" argument is omitted, "
	    "0xffffffffffffffff is used.\n");
#endif
}

void
toplevel_show(const char *buf)
{
	static const struct cmdtab show_cmds[] = {
	    {	"args",		show_args,	},
	    {	"bootinfo",	show_bootinfo,	},
#if 0 /* XXX notyet */
	    {	"pt",		show_pt,	},
	    {	"rpb",		show_rpb,	},
#endif
	    {	NULL,				},
	};

	if (dispatch_cmd(buf, show_cmds) == DISPATCH_CMD_NOCMD) {
		printf("no subcommand given.  allowed subcommands:\n");
		print_cmds(show_cmds, NULL, 0);
	}
}


/*
 * Show Commands
 */

void
show_args(const char *buf)
{

	warn_ignored_args(buf, "show args");

	printf("first free page frame number:       0x%lx\n", arg_pfn);
	printf("page table base page frame number:  0x%lx\n", arg_ptb);
	printf("bootinfo magic number:              0x%lx\n", arg_bim);
	printf("bootinfo pointer:                   0x%lx\n", arg_bip);
	printf("bootinfo version:                   0x%lx\n", arg_biv);
}

void
show_bootinfo(const char *buf)
{
	u_long biv, bip;

	warn_ignored_args(buf, "show bootinfo");

	if (arg_bim != BOOTINFO_MAGIC) {
		printf("bootinfo magic number not present; no bootinfo\n");
		return;
	}

	bip = arg_bip;
	biv = arg_biv;
	if (biv == 0) {
		biv = *(u_long *)bip;
		bip += 8;
	}

	printf("bootinfo version: %d\n", biv);
	printf("bootinfo pointer: %p\n", (void *)bip);
	printf("bootinfo data:\n");

	switch (biv) {
	case 1: {
		const struct bootinfo_v1 *v1p;
		int i;

		v1p = (const struct bootinfo_v1 *)bip;
		printf("    ssym:          0x%lx\n", v1p->ssym);
		printf("    esym:          0x%lx\n", v1p->esym);
		printf("    boot flags:    \"");
		print_stringarray(v1p->boot_flags, sizeof v1p->boot_flags);
		printf("\"\n");
		printf("    booted kernel: \"", v1p->esym);
		print_stringarray(v1p->booted_kernel,
		    sizeof v1p->booted_kernel);
		printf("\"\n");
		printf("    hwrpb:         %p\n", v1p->hwrpb);
		printf("    hwrpbsize:     0x%lx\n", v1p->hwrpbsize);
		printf("    cngetc:        %p\n", v1p->cngetc);
		printf("    cnputc:        %p\n", v1p->cnputc);
		printf("    cnpollc:       %p\n", v1p->cnpollc);
		for (i = 0; i < (sizeof v1p->pad / sizeof v1p->pad[0]); i++) {
			printf("    pad[%d]:        0x%lx\n", i, v1p->pad[i]);
		}
		break;
	}
	default:
		printf("    unknown bootinfo version, cannot print data\n");
		break;
	}
}

void
show_pt(const char *buf)
{

	/* has additional args! */
	printf("\"show pt\" not yet implemented\n");
}

void
show_rpb(const char *buf)
{

	warn_ignored_args(buf, "show pt");

	printf("\"show rpb\" not yet implemented\n");
}