wizard.c revision 8881
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $Id: wizard.c,v 1.4 1995/05/20 10:33:14 jkh Exp $
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <err.h>
19#include <sys/types.h>
20#include "sysinstall.h"
21
22u_char mbr[] = {
23    250,51,192,142,208,188,0,124,139,244,80,7,80,31,251,252,191,0,6,185,0,1,
24    242,165,234,29,6,0,0,190,190,7,179,4,128,60,128,116,14,128,60,0,117,28,
25    131,198,16,254,203,117,239,205,24,139,20,139,76,2,139,238,131,198,16,254,
26    203,116,26,128,60,0,116,244,190,139,6,172,60,0,116,11,86,187,7,0,180,14,
27    205,16,94,235,240,235,254,191,5,0,187,0,124,184,1,2,87,205,19,95,115,12,
28    51,192,205,19,79,117,237,190,163,6,235,211,190,194,6,191,254,125,129,61,
29    85,170,117,199,139,245,234,0,124,0,0,73,110,118,97,108,105,100,32,112,97,
30    114,116,105,116,105,111,110,32,116,97,98,108,101,0,69,114,114,111,114,32,
31    108,111,97,100,105,110,103,32,111,112,101,114,97,116,105,110,103,32,115,
32    121,115,116,101,109,0,77,105,115,115,105,110,103,32,111,112,101,114,97,
33    116,105,110,103,32,115,121,115,116,101,109,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
35    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
36    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
37    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
39    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,
40    1,1,0,4,15,63,60,63,0,0,0,241,239,0,0,0,0,1,61,5,15,63,243,48,240,0,0,144,
41    208,2,0,0,0,1,244,165,15,63,170,192,192,3,0,144,208,2,0,0,0,0,0,0,0,0,0,
42    0,0,0,0,0,0,0,0,85,170
43};
44
45u_char bteasy17[] = {
46    51,192,142,192,142,216,142,208,188,0,124,252,139,244,191,0,6,185,0,1,242,
47    165,234,96,6,0,0,139,213,88,162,72,7,60,53,116,28,180,16,246,228,5,174,
48    4,150,246,68,4,255,116,62,198,4,128,232,218,0,138,116,1,139,76,2,235,8,
49    232,207,0,185,1,0,50,209,187,0,124,184,1,2,205,19,114,30,129,191,254,1,
50    85,170,117,22,234,0,124,0,0,128,250,129,116,2,178,128,139,234,66,128,242,
51    179,136,22,58,7,191,190,7,185,4,0,198,6,45,7,49,50,246,136,45,138,69,4,
52    60,0,116,35,60,5,116,31,254,198,190,42,7,232,113,0,190,72,7,70,70,139,28,
53    10,255,116,5,50,125,4,117,243,141,183,114,7,232,90,0,131,199,16,254,6,45,
54    7,226,203,128,62,117,4,2,116,11,190,59,7,10,246,117,10,205,24,235,172,190,
55    42,7,232,57,0,232,54,0,50,228,205,26,139,218,131,195,96,180,1,205,22,180,
56    0,117,11,205,26,59,211,114,242,160,72,7,235,10,205,22,138,196,60,28,116,
57    243,4,246,60,49,114,214,60,53,119,210,80,190,40,7,187,27,6,83,252,172,80,
58    36,127,180,14,205,16,88,168,128,116,242,195,86,184,1,3,187,0,6,185,1,0,
59    50,246,205,19,94,198,6,72,7,63,195,13,138,13,10,70,48,32,46,32,46,32,46,
60    160,100,105,115,107,32,49,13,10,10,68,101,102,97,117,108,116,58,32,70,63,
61    160,0,1,0,4,0,6,3,7,7,10,10,99,14,100,14,101,20,128,20,129,25,130,30,147,
62    36,165,39,159,43,117,47,82,47,219,50,64,55,242,61,0,100,111,243,72,80,70,
63    211,79,115,178,85,110,105,248,78,111,118,101,108,236,77,105,110,105,248,
64    76,105,110,117,248,65,109,111,101,98,225,66,83,196,66,83,68,233,80,67,73,
65    216,67,80,205,86,101,110,105,248,68,111,115,115,101,227,63,191,0,0,0,0,
66    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
67    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
68    0,85,170
69};
70
71int
72scan_block(int fd, daddr_t block)
73{
74    u_char foo[512];
75
76    if (-1 == lseek(fd,block * 512,SEEK_SET))
77	err(1,"lseek");
78    if (512 != read(fd,foo, 512))
79	return 1;
80    return 0;
81}
82
83void
84Scan_Disk(Disk *d)
85{
86    char device[64];
87    u_long l;
88    int i,j,fd;
89
90    strcpy(device,"/dev/r");
91    strcat(device,d->name);
92
93    fd = open(device,O_RDWR);
94    if (fd < 0) {
95	msgWarn("open(%s) failed", device);
96	return;
97    }
98    for(i=-1,l=0;;l++) {
99	j = scan_block(fd,l);
100	if (j != i) {
101	    if (i == -1) {
102		printf("%c: %lu.",j ? 'B' : 'G', l);
103		fflush(stdout);
104	    } else if (i == 0) {
105		printf(".%lu\nB: %lu.",l-1,l);
106		fflush(stdout);
107	    } else {
108		printf(".%lu\nG: %lu.",l-1,l);
109		fflush(stdout);
110	    }
111	    i = j;
112	}
113    }
114    close(fd);
115}
116
117void
118slice_wizard(Disk *d)
119{
120    Disk *db;
121    char myprompt[BUFSIZ];
122    char input[BUFSIZ];
123    char *p,*q=0;
124    char **cp,*cmds[200];
125    int ncmd,i;
126
127    sprintf(myprompt,"%s> ", d->name);
128    while(1) {
129	printf("--==##==--\n");
130	Debug_Disk(d);
131	p = CheckRules(d);
132	if (p) {
133	    printf("%s",p);
134	    free(p);
135	}
136	printf(myprompt);
137	fflush(stdout);
138	q = p = fgets(input,sizeof(input),stdin);
139	if(!p)
140	    break;
141	for(cp = cmds; (*cp = strsep(&p, " \t\n")) != NULL;)
142	    if (**cp != '\0')
143		cp++;
144	ncmd = cp - cmds;
145	if(!ncmd)
146	    continue;
147	if (!strcasecmp(*cmds,"quit")) { break; }
148	if (!strcasecmp(*cmds,"exit")) { break; }
149	if (!strcasecmp(*cmds,"q")) { break; }
150	if (!strcasecmp(*cmds,"x")) { break; }
151	if (!strcasecmp(*cmds,"delete") && ncmd == 2) {
152	    printf("delete = %d\n",
153		   Delete_Chunk(d,
154				(struct chunk *)strtol(cmds[1],0,0)));
155	    continue;
156	}
157	if (!strcasecmp(*cmds,"allfreebsd")) {
158	    All_FreeBSD(d);
159	    continue;
160	}
161	if (!strcasecmp(*cmds,"bios") && ncmd == 4) {
162	    Set_Bios_Geom(d,
163			  strtol(cmds[1],0,0),
164			  strtol(cmds[2],0,0),
165			  strtol(cmds[3],0,0));
166	    continue;
167	}
168	if (!strcasecmp(*cmds,"phys") && ncmd == 4) {
169	    d = Set_Phys_Geom(d,
170			      strtol(cmds[1],0,0),
171			      strtol(cmds[2],0,0),
172			      strtol(cmds[3],0,0));
173	    continue;
174	}
175	if (!strcasecmp(*cmds,"collapse")) {
176	    if (cmds[1])
177		while (Collapse_Chunk(d,
178				      (struct chunk *)strtol(cmds[1],0,0)))
179		    ;
180	    else
181		Collapse_Disk(d);
182	    continue;
183	}
184	if (!strcasecmp(*cmds,"list")) {
185	    cp = Disk_Names();
186	    printf("Disks:");
187	    for(i=0;cp[i];i++) {
188		printf(" %s",cp[i]);
189		free(cp[i]);
190	    }
191	    free(cp);
192	    continue;
193	}
194	if (!strcasecmp(*cmds,"create") && ncmd == 6) {
195
196	    printf("Create=%d\n",
197		   Create_Chunk(d,
198				strtol(cmds[1],0,0),
199				strtol(cmds[2],0,0),
200				strtol(cmds[3],0,0),
201				strtol(cmds[4],0,0),
202				strtol(cmds[5],0,0)));
203	    continue;
204	}
205	if (!strcasecmp(*cmds,"read")) {
206	    db = d;
207	    if (ncmd > 1)
208		d = Open_Disk(cmds[1]);
209	    else
210		d = Open_Disk(d->name);
211	    if (d)
212		Free_Disk(db);
213	    else
214		d = db;
215	    continue;
216	}
217	if (!strcasecmp(*cmds,"scan")) {
218	    Scan_Disk(d);
219	    continue;
220	}
221	if (!strcasecmp(*cmds,"bteasy")) {
222	    Set_Boot_Mgr(d,bteasy17);
223	    continue;
224	}
225	if (!strcasecmp(*cmds,"mbr")) {
226	    Set_Boot_Mgr(d,mbr);
227	    continue;
228	}
229	if (!strcasecmp(*cmds,"boot")) {
230	    extern u_char boot1[],boot2[];
231	    Set_Boot_Blocks(d,boot1,boot2);
232	    continue;
233	}
234	if (!strcasecmp(*cmds,"write")) {
235	    printf("Write=%d\n",
236		   Write_Disk(d));
237	    Free_Disk(d);
238	    d = Open_Disk(d->name);
239	    continue;
240	}
241	if (strcasecmp(*cmds,"help"))
242	    printf("\007ERROR\n");
243	printf("CMDS:\n");
244	printf("allfreebsd\t\t");
245	printf("bios cyl hd sect\n");
246	printf("boot\t\t");
247	printf("bteasy17\n");
248	printf("collapse [pointer]\t\t");
249	printf("create offset size enum subtype flags\n");
250	printf("subtype(part): swap=1, ffs=7\t\t");
251	printf("delete pointer\n");
252	printf("list\t\t");
253	printf("mbr\n");
254	printf("phys cyl hd sect\t\t");
255	printf("quit\n");
256	printf("read [disk]\t\t");
257	printf("scan\n");
258	printf("write\t\t");
259	printf("ENUM:\n\t");
260	for(i=0;chunk_n[i];i++)
261	    printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : "  ");
262	printf("\n");
263
264    }
265}
266