xref: /openbsd/sys/arch/i386/stand/libsa/cmd_i386.c (revision 133306f0)
1 /*	$OpenBSD: cmd_i386.c,v 1.22 1999/08/25 00:54:19 mickey Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-1999 Michael Shalayeff
5  * Copyright (c) 1997 Tobias Weingartner
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Michael Shalayeff.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/param.h>
37 #include <sys/reboot.h>
38 #include <machine/biosvar.h>
39 #include <sys/disklabel.h>
40 #include "disk.h"
41 #include "debug.h"
42 #include "biosdev.h"
43 #include "libsa.h"
44 #include <cmd.h>
45 
46 
47 extern const char version[];
48 
49 int Xboot __P((void));
50 int Xdiskinfo __P((void));
51 int Xmemory __P((void));
52 int Xregs __P((void));
53 
54 /* From gidt.S */
55 int bootbuf __P((void*, int));
56 
57 const struct cmd_table cmd_machine[] = {
58 	{ "boot",     CMDT_CMD, Xboot },
59 	{ "diskinfo", CMDT_CMD, Xdiskinfo },
60 	{ "memory",   CMDT_CMD, Xmemory },
61 #ifdef DEBUG
62 	{ "regs",     CMDT_CMD, Xregs },
63 #endif
64 	{ NULL, 0 }
65 };
66 
67 int
68 Xdiskinfo()
69 {
70 #ifndef _TEST
71 	dump_diskinfo();
72 #endif
73 	return 0;
74 }
75 
76 #ifdef DEBUG
77 int
78 Xregs()
79 {
80 	DUMP_REGS;
81 	return 0;
82 }
83 #endif
84 
85 int
86 Xboot()
87 {
88 #ifndef _TEST
89 	int dev, part, st;
90 	char buf[DEV_BSIZE], *dest = (void*)BOOTBIOS_ADDR;
91 
92 	if(cmd.argc != 2) {
93 		printf("machine boot {fd,hd}<0123>[abcd]\n");
94 		printf("Where [0123] is the disk number,"
95 			" and [abcd] is the partition.\n");
96 		return 0;
97 	}
98 
99 	/* Check arg */
100 	if(cmd.argv[1][0] != 'f' && cmd.argv[1][0] != 'h')
101 		goto bad;
102 	if(cmd.argv[1][1] != 'd')
103 		goto bad;
104 	if(cmd.argv[1][2] < '0' || cmd.argv[1][2] > '3')
105 		goto bad;
106 	if((cmd.argv[1][3] < 'a' || cmd.argv[1][3] > 'd') && cmd.argv[1][3] != '\0')
107 		goto bad;
108 
109 	printf("Booting from %s ", cmd.argv[1]);
110 
111 	dev = (cmd.argv[1][0] == 'h')?0x80:0;
112 	dev += (cmd.argv[1][2] - '0');
113 	part = (cmd.argv[1][3] - 'a');
114 
115 	if (part > 0)
116 		printf("[%x,%d]\n", dev, part);
117 	else
118 		printf("[%x]\n", dev);
119 
120 	/* Read boot sector from device */
121 	st = biosd_io(F_READ, dev, 0, 0, 0, 1, buf);
122 	if(st) goto bad;
123 
124 	/* Frob boot flag in buffer from HD */
125 	if((dev & 0x80) && (part > 0)){
126 		int i, j;
127 
128 		for(i = 0, j = DOSPARTOFF; i < 4; i++, j += 16)
129 			if(part == i)
130 				buf[j] |= 0x80;
131 			else
132 				buf[j] &= ~0x80;
133 	}
134 
135 	/* Load %dl, ljmp */
136 	bcopy(buf, dest, DEV_BSIZE);
137 	bootbuf(dest, dev);
138 
139 bad:
140 	printf("Invalid device!\n");
141 #endif
142 	return 0;
143 }
144 
145 int
146 Xmemory()
147 {
148 	if (cmd.argc >= 2) {
149 		int i;
150 		/* parse the memory specs */
151 
152 		for (i = 1; i < cmd.argc; i++) {
153 			char *p;
154 			long addr, size;
155 
156 			p = cmd.argv[i];
157 
158 			size = strtol(p + 1, &p, 0);
159 			if (*p && *p == '@')
160 				addr = strtol(p + 1, NULL, 0);
161 			else
162 				addr = 0;
163 			if (addr == 0 && (*p != '@' || size == 0)) {
164 				printf ("bad language\n");
165 				return 0;
166 			} else {
167 				switch (cmd.argv[i][0]) {
168 				case '-':
169 					mem_delete(addr, addr + size);
170 					break;
171 				case '+':
172 					mem_add(addr, addr + size);
173 					break;
174 				default :
175 					printf ("bad OP\n");
176 					return 0;
177 				}
178 			}
179 		}
180 	}
181 
182 	dump_biosmem(NULL);
183 
184 	return 0;
185 }
186