Home | History | Annotate | Line # | Download | only in boot
monitor.c revision 1.1
      1 /*	$NetBSD: monitor.c,v 1.1 2006/09/01 21:26:18 uwe 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 #if defined(DBMONITOR)
     40 
     41 #include <lib/libsa/stand.h>
     42 #include <lib/libkern/libkern.h>
     43 
     44 #include "boot.h"
     45 
     46 #ifndef	NULL
     47 #define NULL	(void *)0
     48 #endif
     49 
     50 static void db_cmd_dump(int, char **);
     51 static void db_cmd_get(int, char **);
     52 static void db_cmd_put(int, char **);
     53 static void db_cmd_help(int, char **);
     54 
     55 static int db_atob(char *);
     56 
     57 static const struct db_cmd {
     58 	char *name;
     59 	void (*fcn)(int, char **);
     60 } db_cmd[] = {
     61 	{ "dump",	db_cmd_dump },
     62 	{ "get",	db_cmd_get },
     63 	{ "put",	db_cmd_put },
     64 	{ "help",	db_cmd_help },
     65 	{ NULL,		NULL },
     66 };
     67 
     68 int
     69 db_monitor(void)
     70 {
     71 	char line[1024];
     72 	char *p, *argv[16];
     73 	int argc, flag;
     74 	int tmp;
     75 
     76 	for (;;) {
     77 		printf("db> ");
     78 		gets(line);
     79 
     80 		flag = 0;
     81 		argc = 0;
     82 		for (p = line; *p != '\0'; p++) {
     83 			if (*p != ' ' && *p != '\t') {
     84 				if (!flag) {
     85 					flag++;
     86 					argv[argc++] = p;
     87 				}
     88 			} else {
     89 				if (flag) {
     90 					*p = '\0';
     91 					flag = 0;
     92 				}
     93 			}
     94 		}
     95 		if (argc == 0)
     96 			continue;
     97 
     98 		tmp = 0;
     99 		while (db_cmd[tmp].name != NULL) {
    100 			if (strcmp("continue", argv[0]) == 0)
    101 				return 0;
    102 			if (strcmp(db_cmd[tmp].name, argv[0]) == 0) {
    103 				(db_cmd[tmp].fcn)(argc, argv);
    104 				break;
    105 			}
    106 			tmp++;
    107 		}
    108 		if (db_cmd[tmp].name == NULL)
    109 			db_cmd_help(argc, argv);
    110 	}
    111 
    112 	return 0;
    113 }
    114 
    115 static int
    116 db_atob(p)
    117 	char *p;
    118 {
    119 	int b = 0, width, tmp, exp, x = 0;
    120 
    121 	if (p[1] == 'x') {
    122 		p += 2;
    123 		x = 1;
    124 	}
    125 
    126 	width = strlen(p);
    127 	while (width--) {
    128 		exp = 1;
    129 		for (tmp = 1; tmp <= width; tmp++)
    130 			exp *= (x ? 16 : 10);
    131 		if (*p >= '0' && *p <= '9') {
    132 			tmp = *p - '0';
    133 		} else {
    134 			tmp = *p - 'a' + 10;
    135 		}
    136 		b += tmp * exp;
    137 		p++;
    138 	}
    139 	return b;
    140 }
    141 
    142 static void
    143 db_cmd_dump(argc, argv)
    144 	int argc;
    145 	char **argv;
    146 {
    147 	char *p, *r, *pp;
    148 	int mode, add, size, i;
    149 
    150 	switch (argc) {
    151 	case 4:
    152 		r = argv[1];
    153 		switch (r[1]) {
    154 		case 'b':
    155 			mode = 1;
    156 			break;
    157 		case 'h':
    158 			mode = 2;
    159 			break;
    160 		case 'w':
    161 			mode = 4;
    162 			break;
    163 		default:
    164 			goto out;
    165 		}
    166 		p = argv[2];
    167 		pp = argv[3];
    168 		break;
    169 	case 3:
    170 		mode = 4;
    171 		p = argv[1];
    172 		pp = argv[2];
    173 		break;
    174 	default:
    175 		goto out;
    176 	}
    177 
    178 	add = db_atob(p);
    179 	size = db_atob(pp);
    180 	i = 0;
    181 	for (; size > 0;) {
    182 		if (i == 0) {
    183 			printf("\n0x%x:", add);
    184 		}
    185 		switch (mode) {
    186 		case 1:
    187 			printf(" ");
    188 			puthex(*(unsigned char *)add, 1);
    189 			add += 1;
    190 			size -= 1;
    191 			if (++i == 16)
    192 				i = 0;
    193 			break;
    194 		case 2:
    195 			printf(" ");
    196 			puthex(*(unsigned short *)add, 2);
    197 			add += 2;
    198 			size -= 2;
    199 			if (++i == 8)
    200 				i = 0;
    201 			break;
    202 		case 4:
    203 			printf(" ");
    204 			puthex(*(unsigned int *)add, 4);
    205 			add += 4;
    206 			size -= 4;
    207 			if (++i == 4)
    208 				i = 0;
    209 			break;
    210 		}
    211 	}
    212 	printf("\n");
    213 	return;
    214 
    215 out:
    216 	printf("dump [-b][-h][-w] address size\n");
    217 	return;
    218 }
    219 
    220 static void
    221 db_cmd_get(argc, argv)
    222 	int argc;
    223 	char **argv;
    224 {
    225 	char *p, *r;
    226 	int mode, add;
    227 	int val;
    228 
    229 	switch (argc) {
    230 	case 3:
    231 		r = argv[1];
    232 		switch (r[1]) {
    233 		case 'b':
    234 			mode = 1;
    235 			break;
    236 		case 'h':
    237 			mode = 2;
    238 			break;
    239 		case 'w':
    240 			mode = 4;
    241 			break;
    242 		default:
    243 			goto out;
    244 		}
    245 		p = argv[2];
    246 		break;
    247 	case 2:
    248 		mode = 4;
    249 		p = argv[1];
    250 		break;
    251 	default:
    252 		goto out;
    253 	}
    254 
    255 	add = db_atob(p);
    256 	printf("0x%x: 0x", add);
    257 	switch (mode) {
    258 	case 1:
    259 		val = *(char *)add;
    260 		break;
    261 	case 2:
    262 		val = *(short *)add;
    263 		break;
    264 	case 4:
    265 		val = *(int *)add;
    266 		break;
    267 	default:
    268 		val = 0;
    269 		break;
    270 	}
    271 	puthex(val, mode);
    272 	printf("\n");
    273 	return;
    274 
    275 out:
    276 	printf("get [-b][-h][-w] address\n");
    277 	return;
    278 }
    279 
    280 static void
    281 db_cmd_put(argc, argv)
    282 	int argc;
    283 	char **argv;
    284 {
    285 	char *p, *r, *pp;
    286 	int mode, add, data;
    287 
    288 	switch (argc) {
    289 	case 4:
    290 		r = argv[1];
    291 		switch (r[1]) {
    292 		case 'b':
    293 			mode = 1;
    294 			break;
    295 		case 'h':
    296 			mode = 2;
    297 			break;
    298 		case 'w':
    299 			mode = 4;
    300 			break;
    301 		default:
    302 			goto out;
    303 		}
    304 		p = argv[2];
    305 		pp = argv[3];
    306 		break;
    307 	case 3:
    308 		mode = 4;
    309 		p = argv[1];
    310 		pp = argv[2];
    311 		break;
    312 	default:
    313 		goto out;
    314 	}
    315 
    316 	add = db_atob(p);
    317 	data = db_atob(pp);
    318 	printf("0x%x: 0x", add);
    319 	puthex(data, mode);
    320 	switch (mode) {
    321 	case 1:
    322 		*(char *)add = data;
    323 		break;
    324 	case 2:
    325 		*(short *)add = data;
    326 		break;
    327 	case 4:
    328 		*(int *)add = data;
    329 		break;
    330 	}
    331 	printf("\n");
    332 	return;
    333 
    334 out:
    335 	printf("put [-b][-h][-w] address data\n");
    336 	return;
    337 }
    338 
    339 static void
    340 db_cmd_help(argc, argv)
    341 	int argc;
    342 	char **argv;
    343 {
    344 	int i = 0;
    345 
    346 	while (db_cmd[i].name != NULL)
    347 		printf("%s, ", db_cmd[i++].name);
    348 	printf("continue\n");
    349 }
    350 
    351 #endif	/* DBMONITOR */
    352