Remove last remnants of the wd(4) driver which was moved to the Attic some
[dragonfly.git] / lib / libdisk / tst01.c
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  * $FreeBSD: src/lib/libdisk/tst01.c,v 1.23.2.4 2000/08/07 08:55:19 ps Exp $
10  * $DragonFly: src/lib/libdisk/Attic/tst01.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <err.h>
20 #ifdef READLINE
21 #include <readline/readline.h>
22 #include <readline/history.h>
23 #endif
24 #include <sys/types.h>
25 #include <paths.h>
26 #include "libdisk.h"
27
28 u_char mbr[] = {
29 250,51,192,142,208,188,0,124,139,244,80,7,80,31,251,252,191,0,6,185,0,1,
30 242,165,234,29,6,0,0,190,190,7,179,4,128,60,128,116,14,128,60,0,117,28,
31 131,198,16,254,203,117,239,205,24,139,20,139,76,2,139,238,131,198,16,254,
32 203,116,26,128,60,0,116,244,190,139,6,172,60,0,116,11,86,187,7,0,180,14,
33 205,16,94,235,240,235,254,191,5,0,187,0,124,184,1,2,87,205,19,95,115,12,
34 51,192,205,19,79,117,237,190,163,6,235,211,190,194,6,191,254,125,129,61,
35 85,170,117,199,139,245,234,0,124,0,0,73,110,118,97,108,105,100,32,112,97,
36 114,116,105,116,105,111,110,32,116,97,98,108,101,0,69,114,114,111,114,32,
37 108,111,97,100,105,110,103,32,111,112,101,114,97,116,105,110,103,32,115,
38 121,115,116,101,109,0,77,105,115,115,105,110,103,32,111,112,101,114,97,
39 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,
40 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,
41 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,
42 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,
43 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,
44 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,
45 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,
46 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,
47 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,
48 0,0,0,0,0,0,0,0,85,170
49 };
50
51 u_char bteasy17[] = {
52 51,192,142,192,142,216,142,208,188,0,124,252,139,244,191,0,6,185,0,1,242,
53 165,234,96,6,0,0,139,213,88,162,72,7,60,53,116,28,180,16,246,228,5,174,
54 4,150,246,68,4,255,116,62,198,4,128,232,218,0,138,116,1,139,76,2,235,8,
55 232,207,0,185,1,0,50,209,187,0,124,184,1,2,205,19,114,30,129,191,254,1,
56 85,170,117,22,234,0,124,0,0,128,250,129,116,2,178,128,139,234,66,128,242,
57 179,136,22,58,7,191,190,7,185,4,0,198,6,45,7,49,50,246,136,45,138,69,4,
58 60,0,116,35,60,5,116,31,254,198,190,42,7,232,113,0,190,72,7,70,70,139,28,
59 10,255,116,5,50,125,4,117,243,141,183,114,7,232,90,0,131,199,16,254,6,45,
60 7,226,203,128,62,117,4,2,116,11,190,59,7,10,246,117,10,205,24,235,172,190,
61 42,7,232,57,0,232,54,0,50,228,205,26,139,218,131,195,96,180,1,205,22,180,
62 0,117,11,205,26,59,211,114,242,160,72,7,235,10,205,22,138,196,60,28,116,
63 243,4,246,60,49,114,214,60,53,119,210,80,190,40,7,187,27,6,83,252,172,80,
64 36,127,180,14,205,16,88,168,128,116,242,195,86,184,1,3,187,0,6,185,1,0,
65 50,246,205,19,94,198,6,72,7,63,195,13,138,13,10,70,48,32,46,32,46,32,46,
66 160,100,105,115,107,32,49,13,10,10,68,101,102,97,117,108,116,58,32,70,63,
67 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,
68 36,165,39,159,43,117,47,82,47,219,50,64,55,242,61,0,100,111,243,72,80,70,
69 211,79,115,178,85,110,105,248,78,111,118,101,108,236,77,105,110,105,248,
70 76,105,110,117,248,65,109,111,101,98,225,66,83,196,66,83,68,233,80,67,73,
71 216,67,80,205,86,101,110,105,248,68,111,115,115,101,227,63,191,0,0,0,0,
72 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,
73 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,
74 0,85,170
75 };
76
77 int
78 scan_block(int fd, daddr_t block)
79 {
80         u_char foo[512];
81
82         if (-1 == lseek(fd, (off_t)block * 512, SEEK_SET))
83                 err(1, "lseek");
84         if (512 != read(fd,foo, 512))
85                 return 1;
86         return 0;
87 }
88
89 void
90 Scan_Disk(struct disk *d)
91 {
92         char device[64];
93         u_long l;
94         int i,j,fd;
95
96         strcpy(device,_PATH_DEV);
97         strcat(device,d->name);
98
99         fd = open(device,O_RDWR);
100         if (fd < 0) {
101                 warn("open(%s) failed",device);
102                 return;
103         }
104         for(i=-1,l=0;;l++) {
105                 j = scan_block(fd,l);
106                 if (j != i) {
107                         if (i == -1) {
108                                 printf("%c: %lu.",j ? 'B' : 'G', l);
109                                 fflush(stdout);
110                         } else if (i == 0) {
111                                 printf(".%lu\nB: %lu.",l-1,l);
112                                 fflush(stdout);
113                         } else {
114                                 printf(".%lu\nG: %lu.",l-1,l);
115                                 fflush(stdout);
116                         }
117                         i = j;
118                 }
119         }
120         close(fd);
121 }
122
123 int
124 main(int argc, char **argv)
125 {
126         struct disk *d,*db;
127         char myprompt[BUFSIZ];
128 #ifndef READLINE
129         char input[BUFSIZ];
130 #endif
131         char *p,*q=0;
132         char **cp,*cmds[200];
133         int ncmd,i;
134
135         if (argc < 2) {
136                 fprintf(stderr,"Usage:\n\t%s diskname\n",argv[0]);
137                 exit(1);
138         }
139         d = Open_Disk(argv[1]);
140         if (!d)
141                 err(1,"Couldn't open disk %s",argv[1]);
142
143         sprintf(myprompt,"%s %s> ",argv[0],argv[1]);
144         while(1) {
145                 printf("--==##==--\n");
146                 p = CheckRules(d);
147                 Debug_Disk(d);
148                 if (p) {
149                         printf("%s",p);
150                         free(p);
151                 }
152 #ifdef READLINE
153                 if (q)
154                         free(q);
155                 q = p = readline(myprompt);
156 #else
157                 printf("%s", myprompt);
158                 fflush(stdout);
159                 q = p = fgets(input,sizeof(input),stdin);
160 #endif
161                 if(!p)
162                         break;
163                 for(cp = cmds; (*cp = strsep(&p, " \t\n")) != NULL;)
164                         if (**cp != '\0')
165                                 cp++;
166                 ncmd = cp - cmds;
167                 if(!ncmd)
168                         continue;
169                 if (!strcasecmp(*cmds,"quit")) { break; }
170                 if (!strcasecmp(*cmds,"exit")) { break; }
171                 if (!strcasecmp(*cmds,"q")) { break; }
172                 if (!strcasecmp(*cmds,"x")) { break; }
173                 if (!strcasecmp(*cmds,"dwim") && ncmd == 6) {
174                         printf("dwim = %p\n",
175                                 Create_Chunk_DWIM(d,
176                                         (struct chunk *)strtol(cmds[1],0,0),
177                                         strtol(cmds[2],0,0),
178                                         strtol(cmds[3],0,0),
179                                         strtol(cmds[4],0,0),
180                                         strtol(cmds[5],0,0)));
181                         continue;
182                 }
183                 if (!strcasecmp(*cmds,"mknod")) {
184                         MakeDevDisk(d,"/tmp");
185                         continue;
186                 }
187                 if (!strcasecmp(*cmds,"delete") && ncmd == 2) {
188                         printf("delete = %d\n",
189                                 Delete_Chunk(d,
190                                         (struct chunk *)strtol(cmds[1],0,0)));
191                         continue;
192                 }
193                 if (!strcasecmp(*cmds,"allfreebsd")) {
194                         All_FreeBSD(d, 0);
195                         continue;
196                 }
197                 if (!strcasecmp(*cmds,"dedicate")) {
198                         All_FreeBSD(d, 1);
199                         continue;
200                 }
201                 if (!strcasecmp(*cmds,"sanitize")) {
202                         Sanitize_Bios_Geom(d);
203                         continue;
204                 }
205                 if (!strcasecmp(*cmds,"bios") && ncmd == 4) {
206                         Set_Bios_Geom(d,
207                                 strtol(cmds[1],0,0),
208                                 strtol(cmds[2],0,0),
209                                 strtol(cmds[3],0,0));
210                         continue;
211                 }
212                 if (!strcasecmp(*cmds,"list")) {
213                         cp = Disk_Names();
214                         printf("Disks:");
215                         for(i=0;cp[i];i++) {
216                                 printf(" %s",cp[i]);
217                                 free(cp[i]);
218                         }
219                         free(cp);
220                         continue;
221                 }
222 #ifdef PC98
223                 if (!strcasecmp(*cmds,"create") && ncmd == 7) {
224 #else
225                 if (!strcasecmp(*cmds,"create") && ncmd == 6) {
226 #endif
227
228                         printf("Create=%d\n",
229                                 Create_Chunk(d,
230                                         strtol(cmds[1],0,0),
231                                         strtol(cmds[2],0,0),
232                                         strtol(cmds[3],0,0),
233                                         strtol(cmds[4],0,0),
234 #ifdef PC98
235                                         strtol(cmds[5],0,0),
236                                         cmds[6]));
237 #else
238                                         strtol(cmds[5],0,0)));
239 #endif
240                         continue;
241                 }
242                 if (!strcasecmp(*cmds,"read")) {
243                         db = d;
244                         if (ncmd > 1)
245                                 d = Open_Disk(cmds[1]);
246                         else
247                                 d = Open_Disk(argv[1]);
248                         if (d)
249                                 Free_Disk(db);
250                         else
251                                 d = db;
252                         continue;
253                 }
254                 if (!strcasecmp(*cmds,"scan")) {
255                         Scan_Disk(d);
256                         continue;
257                 }
258                 if (!strcasecmp(*cmds,"bteasy")) {
259                         Set_Boot_Mgr(d,bteasy17);
260                         continue;
261                 }
262                 if (!strcasecmp(*cmds,"mbr")) {
263                         Set_Boot_Mgr(d,mbr);
264                         continue;
265                 }
266 #ifndef __alpha__       /* don't think this compiles on i386 either */
267                 if (!strcasecmp(*cmds,"boot")) {
268                         Set_Boot_Blocks(d,boot1,boot2);
269                         continue;
270                 }
271 #endif
272                 if (!strcasecmp(*cmds,"write")) {
273                         printf("Write=%d\n",
274                                 Write_Disk(d));
275                         Free_Disk(d);
276                         d = Open_Disk(d->name);
277                         continue;
278                 }
279                 if (strcasecmp(*cmds,"help"))
280                         printf("\007ERROR\n");
281                 printf("CMDS:\n");
282                 printf("\tallfreebsd\n");
283                 printf("\tdedicate\n");
284                 printf("\tbios cyl hd sect\n");
285                 printf("\tboot\n");
286                 printf("\tbteasy17\n");
287 #if 0
288                 printf("\tcollapse [pointer]\n");
289 #endif
290 #ifdef PC98
291                 printf("\tcreate offset size enum subtype flags name\n");
292 #else
293                 printf("\tcreate offset size enum subtype flags\n");
294 #endif
295                 printf("\t\tsubtype(part): swap=1, ffs=7\n");
296                 printf("\tdelete pointer\n");
297                 printf("\tlist\n");
298                 printf("\tmbr\n");
299 #if 0
300                 printf("\tphys cyl hd sect\n");
301 #endif
302                 printf("\tquit\n");
303                 printf("\tread [disk]\n");
304                 printf("\tscan\n");
305                 printf("\twrite\n");
306                 printf("\nENUM:\n\t");
307                 for(i=0;chunk_n[i];i++)
308                         printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : "  ");
309                 printf("\n");
310
311         }
312         exit (0);
313 }