Home | History | Annotate | Line # | Download | only in boot
monitor.c revision 1.1
      1 /*	$NetBSD: monitor.c,v 1.1 2000/02/29 15:21:50 nonaka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Kazuki Sakamoto.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <lib/libsa/stand.h>
     40 #include "boot.h"
     41 
     42 #define NULL	0
     43 
     44 extern int errno;
     45 extern char *name;
     46 
     47 void db_cmd_dump __P((int, char **));
     48 void db_cmd_get __P((int, char **));
     49 void db_cmd_mf __P((int, char **));
     50 void db_cmd_mt __P((int, char **));
     51 void db_cmd_put __P((int, char **));
     52 void db_cmd_help __P((int, char **));
     53 
     54 unsigned int mfmsr __P((void));
     55 void mtmsr __P((unsigned int));
     56 
     57 int db_atob __P((char *));
     58 
     59 struct {
     60 	char *name;
     61 	void (*fcn)(int, char **);
     62 } db_cmd[] = {
     63 	{ "dump",	db_cmd_dump },
     64 	{ "get",	db_cmd_get },
     65 	{ "mf",		db_cmd_mf },
     66 	{ "mt",		db_cmd_mt },
     67 	{ "put",	db_cmd_put },
     68 	{ "help",	db_cmd_help },
     69 	{ NULL,		NULL },
     70 };
     71 
     72 int
     73 db_monitor()
     74 {
     75 	int tmp;
     76 	int argc, flag;
     77 	char *p, *argv[16];
     78 	char line[1024];
     79 
     80 	while(1) {
     81 		printf("db> ");
     82 		gets(line);
     83 
     84 		flag = 0;
     85 		for(p = line, argc = 0; *p != '\0'; p++) {
     86 			if (*p != ' ' && *p != '\t') {
     87 				if (!flag) {
     88 					flag++;
     89 					argv[argc++] = p;
     90 				}
     91 			} else {
     92 				if (flag) {
     93 					*p = '\0';
     94 					flag = 0;
     95 				}
     96 			}
     97 		}
     98 
     99 		if (argc == 0)
    100 			continue;
    101 
    102 		tmp = 0;
    103 		while (db_cmd[tmp].name != NULL) {
    104 			if (!strcmp("continue", argv[0]))
    105 				return 0;
    106 			if (!strcmp(db_cmd[tmp].name, argv[0])) {
    107 				(db_cmd[tmp].fcn)(argc, argv);
    108 				break;
    109 			}
    110 			tmp++;
    111 		}
    112 		if (db_cmd[tmp].name == NULL)
    113 			db_cmd_help(argc, argv);
    114 	}
    115 	return 0;
    116 }
    117 
    118 int
    119 db_atob(p)
    120 	char *p;
    121 {
    122 	int b = 0, width, tmp, exp, x = 0;
    123 
    124 	if (p[1] == 'x') {
    125 		p += 2;
    126 		x = 1;
    127 	}
    128 	width = strlen(p);
    129 	while(width--) {
    130 		exp = 1;
    131 		for (tmp = 1; tmp <= width; tmp++)
    132 			exp *= (x ? 16 : 10);
    133 		if (*p >= '0' && *p <= '9') {
    134 			tmp = *p - '0';
    135 		} else {
    136 			tmp = *p - 'a' + 10;
    137 		}
    138 		b += tmp * exp;
    139 		p++;
    140 	}
    141 	return b;
    142 }
    143 
    144 void
    145 db_cmd_dump(argc, argv)
    146 	int argc;
    147 	char **argv;
    148 {
    149 	char *p, *r, *pp;
    150 	int mode, add, size, i;
    151 
    152 	switch (argc) {
    153 	case 4:
    154 		r = argv[1];
    155 		switch (r[1]) {
    156 		case 'b':
    157 			mode = 1;
    158 			break;
    159 		case 'h':
    160 			mode = 2;
    161 			break;
    162 		case 'w':
    163 			mode = 4;
    164 			break;
    165 		default:
    166 			goto out;
    167 		}
    168 		p = argv[2];
    169 		pp = argv[3];
    170 		break;
    171 	case 3:
    172 		mode = 4;
    173 		p = argv[1];
    174 		pp = argv[2];
    175 		break;
    176 	default:
    177 		goto out;
    178 	}
    179 
    180 	add = db_atob(p);
    181 	size = db_atob(pp);
    182 	i = 0;
    183 	for (; size > 0;) {
    184 		if (!i)
    185 			printf("\n0x%x:", add);
    186 		switch (mode) {
    187 		case 1:
    188 			printf(" %x", *(unsigned char *)add);
    189 			add += 1;
    190 			size -= 1;
    191 			if (++i == 16)
    192 				i = 0;
    193 			break;
    194 		case 2:
    195 			printf(" %x", *(unsigned short *)add);
    196 			add += 2;
    197 			size -= 2;
    198 			if (++i == 8)
    199 				i = 0;
    200 			break;
    201 		case 4:
    202 			printf(" %x", *(unsigned int *)add);
    203 			add += 4;
    204 			size -= 4;
    205 			if (++i == 4)
    206 				i = 0;
    207 			break;
    208 		}
    209 	}
    210 	printf("\n");
    211 	return;
    212 
    213 out:
    214 	printf("dump [-b][-h][-w] address size\n");
    215 	return;
    216 }
    217 
    218 void
    219 db_cmd_get(argc, argv)
    220 	int argc;
    221 	char **argv;
    222 {
    223 	char *p, *r;
    224 	int mode, add;
    225 
    226 	switch (argc) {
    227 	case 3:
    228 		r = argv[1];
    229 		switch (r[1]) {
    230 		case 'b':
    231 			mode = 1;
    232 			break;
    233 		case 'h':
    234 			mode = 2;
    235 			break;
    236 		case 'w':
    237 			mode = 4;
    238 			break;
    239 		default:
    240 			goto out;
    241 		}
    242 		p = argv[2];
    243 		break;
    244 	case 2:
    245 		mode = 4;
    246 		p = argv[1];
    247 		break;
    248 	default:
    249 		goto out;
    250 	}
    251 
    252 	add = db_atob(p);
    253 	printf("0x%x: ", add);
    254 	switch (mode) {
    255 	case 1:
    256 		printf("0x%x", *(char *)add);
    257 		break;
    258 	case 2:
    259 		printf("0x%x", *(short *)add);
    260 		break;
    261 	case 4:
    262 		printf("0x%x", *(int *)add);
    263 		break;
    264 	}
    265 	printf("\n");
    266 	return;
    267 
    268 out:
    269 	printf("get [-b][-h][-w] address\n");
    270 	return;
    271 }
    272 
    273 void
    274 db_cmd_put(argc, argv)
    275 	int argc;
    276 	char **argv;
    277 {
    278 	char *p, *r, *pp;
    279 	int mode, add, data;
    280 
    281 	switch (argc) {
    282 	case 4:
    283 		r = argv[1];
    284 		switch (r[1]) {
    285 		case 'b':
    286 			mode = 1;
    287 			break;
    288 		case 'h':
    289 			mode = 2;
    290 			break;
    291 		case 'w':
    292 			mode = 4;
    293 			break;
    294 		default:
    295 			goto out;
    296 		}
    297 		p = argv[2];
    298 		pp = argv[3];
    299 		break;
    300 	case 3:
    301 		mode = 4;
    302 		p = argv[1];
    303 		pp = argv[2];
    304 		break;
    305 	default:
    306 		goto out;
    307 	}
    308 
    309 	add = db_atob(p);
    310 	data = db_atob(pp);
    311 	printf("0x%x: 0x%x", add, data);
    312 	switch (mode) {
    313 	case 1:
    314 		*(char *)add = data;
    315 		break;
    316 	case 2:
    317 		*(short *)add = data;
    318 		break;
    319 	case 4:
    320 		*(int *)add = data;
    321 		break;
    322 	}
    323 	printf("\n");
    324 	return;
    325 
    326 out:
    327 	printf("put [-b][-h][-w] address data\n");
    328 	return;
    329 }
    330 
    331 #define STR(x) #x
    332 
    333 #define	FUNC(x) \
    334 unsigned int mf ## x() { \
    335 	unsigned int tmp; \
    336 	asm volatile (STR(mf ## x %0) : STR(=r)(tmp)); \
    337 	return (tmp); \
    338 } \
    339 void mt ## x(data) \
    340 unsigned int data; \
    341 { \
    342 	asm volatile (STR(mt ## x %0) :: STR(r)(data)); \
    343 } \
    344 
    345 #define DEF(x) \
    346 	{ #x, mf ## x, mt ## x }
    347 
    348 FUNC(msr);
    349 
    350 struct {
    351 	char *op;
    352 	unsigned int (*mf)(void);
    353 	void (*mt)(unsigned int);
    354 } mreg [] = {
    355 	DEF(msr),
    356 	{ NULL, NULL, NULL },
    357 };
    358 
    359 void
    360 db_cmd_mf(argc, argv)
    361 	int argc;
    362 	char **argv;
    363 {
    364 	int i = 0;
    365 
    366 	if (argc != 2) {
    367 		printf("mf register\nregister:");
    368 		while (mreg[i].op != NULL)
    369 			printf(" %s", mreg[i++].op);
    370 		printf("\n");
    371 		return;
    372 	}
    373 
    374 	while (mreg[i].op != NULL) {
    375 		if (!strcmp(mreg[i].op, argv[1])) {
    376 			printf(" 0x%x\n", (mreg[i].mf)());
    377 			break;
    378 		}
    379 		i++;
    380 	}
    381 }
    382 
    383 void
    384 db_cmd_mt(argc, argv)
    385 	int argc;
    386 	char **argv;
    387 {
    388 	int i = 0;
    389 
    390 	if (argc != 3) {
    391 		printf("mt register data\nregister:");
    392 		while (mreg[i].op != NULL)
    393 			printf(" %s", mreg[i++].op);
    394 		printf("\n");
    395 		return;
    396 	}
    397 
    398 	while (mreg[i].op != NULL) {
    399 		if (!strcmp(mreg[i].op, argv[1])) {
    400 			(mreg[i].mt)((unsigned int)db_atob(argv[2]));
    401 			printf(" 0x%x\n", db_atob(argv[2]));
    402 			break;
    403 		}
    404 		i++;
    405 	}
    406 }
    407 
    408 void
    409 db_cmd_help(argc, argv)
    410 	int argc;
    411 	char **argv;
    412 {
    413 	int i = 0;
    414 
    415 	while (db_cmd[i].name != NULL)
    416 		printf("%s, ", db_cmd[i++].name);
    417 	printf("continue\n");
    418 }
    419