xref: /original-bsd/sys/luna68k/stand/fsdump.c (revision 3705696b)
1 /*
2  * Copyright (c) 1992 OMRON Corporation.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * OMRON Corporation.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)fsdump.c	8.1 (Berkeley) 06/10/93
12  */
13 
14 /*
15  * fsdump.c -- dump and restore of File System
16  * by A.Fujita, APR-26-1992
17  */
18 
19 #include <sys/param.h>
20 #include <sys/stat.h>
21 #define DKTYPENAMES
22 #include <sys/disklabel.h>
23 #include <luna68k/stand/status.h>
24 #include <luna68k/stand/omron_disklabel.h>
25 
26 #define LABEL_SIZE 512
27 
28 #define BUF_BLOCK	(20 * 12 * 38)			/* 20 Cylinder */
29 #define BUF_BYTES	BUF_BLOCK << DEV_BSHIFT
30 
31 static u_char index[LABEL_SIZE];
32 
33 struct disklabel *lp  = (struct disklabel *)((struct scd_dk_label *) index)->dkl_pad;
34 
35 extern dev_t  rst0;
36 extern dev_t nrst0;
37 
38 static u_char *dump_buf = (u_char *) 0x100000;
39 
40 extern int scsi_device;
41 char cons_buf[100];
42 
43 
44 int
45 fsdump(argc, argv)
46 	int   argc;
47 	char *argv[];
48 {
49 	register int i, j, io;
50 	register char *p;
51 	register int status;
52 	register int block, bytes;
53 	int scsi_id, blk, nblks, size, mark;
54 	struct stat boot_stat;
55 	struct	partition *pp;
56 	scsi_id = scsi_device;
57 
58 	printf("Current SCSI device = ID %d\n", scsi_id);
59 	getline("Is it sure ? (y/n) ", cons_buf);
60 
61 	if ((cons_buf[0] != 'y') && (cons_buf[0] != 'Y'))
62 		return(ST_ERROR);
63 
64 	scsi_read_raw(scsi_id, 0, 1, index, LABEL_SIZE);
65 
66 	for (i = 0; i < MAXPARTITIONS; i++) {
67 		pp = &(lp->d_partitions[i]);
68 		if ((i != 0) &&
69 		    (i != 3) &&
70 		    (i != 4) &&
71 		    (i != 5)) {
72 			pp->p_size = 0;
73 		}
74 		if (i == 5 && argc > 1 && !strcmp(argv[1], "tailor"))
75 			pp->p_size = 0;
76 	}
77 
78 	st_rewind(rst0);
79 
80 	printf("Boot Program		");
81 	io = open("sd(0,0)boot", 0);
82 	if (io >= 0) {
83 		printf("read ... ");
84 		size = read(io, dump_buf, 1048576);
85 		close(io);
86 		printf("%d bytes ... ", size);
87 		if (size <= 0) {
88 			printf("failed\n");
89 			return(ST_ERROR);
90 		}
91 		boot_stat.st_size = size;
92 	}
93 
94 	printf("write ... ");
95 	status = stwrite(rst0, dump_buf, size);
96 	st_write_EOF(rst0);
97 
98 	if (status < size) {
99 		printf("failed\n");
100 		return(ST_ERROR);
101 	}
102 
103 	printf("done\n");
104 
105 	printf("disklabel (index)\t");
106 
107 	printf("write ... ");
108 	status = stwrite(rst0, index, LABEL_SIZE);
109 	st_write_EOF(rst0);
110 
111 	if (status < LABEL_SIZE) {
112 		printf("failed\n");
113 		return(ST_ERROR);
114 	}
115 
116 	printf("done\n\n");
117 
118 	for (i = 0; i < MAXPARTITIONS; i++) {
119 		pp = &(lp->d_partitions[i]);
120 		if (pp->p_size > 0) {
121 			printf("%c: ", i + 'A');
122 			printf("size = %d(0x%s), ", pp->p_size, hexstr(pp->p_size, 8));
123 			printf("offset = %d(0x%s)\n", pp->p_offset, hexstr(pp->p_offset, 8));
124 
125 			blk   = pp->p_offset;
126 			nblks = pp->p_size;
127 			size  = nblks << DEV_BSHIFT;
128 
129 			block = BUF_BLOCK;
130 			bytes = BUF_BYTES;
131 
132 			mark = nblks / block;
133 			if (nblks % block)
134 				mark++;
135 			for (j = 0; j < mark; j++)
136 				printf("-");
137 			for (j = 0; j < mark; j++)
138 				printf("%c", '\x08');
139 
140 			while (nblks > 0) {
141 				if (nblks < block) {
142 					block = nblks;
143 					bytes = nblks << DEV_BSHIFT;
144 				}
145 
146 				if (!scsi_read_raw(scsi_id, blk, block, dump_buf, bytes)) {
147 					printf("disk read failed !!!\n");
148 					return(ST_ERROR);
149 				}
150 
151 				if (stwrite(rst0, dump_buf, bytes) < bytes) {
152 					printf("tape write failed !!!\n");
153 					return(ST_ERROR);
154 				}
155 
156 				blk   += block;
157 				nblks -= block;
158 				size  -= bytes;
159 
160 				printf("#");
161 			}
162 
163 			st_write_EOF(rst0);
164 			printf("\n\n");
165 		}
166 	}
167 }
168 
169 int
170 fsrestore(argc, argv)
171 	int   argc;
172 	char *argv[];
173 {
174 	register int i, j, status;
175 	register int block, bytes;
176 	int blk, nblks, size, mark;
177 	struct	partition *pp;
178 
179 	printf("Current SCSI device = ID %d\n", scsi_device);
180 	getline("Is it sure ? (y/n) ", cons_buf);
181 
182 	if ((cons_buf[0] != 'y') && (cons_buf[0] != 'Y'))
183 		return(ST_ERROR);
184 
185 	st_rewind(rst0);
186 
187 	st_skip(rst0);
188 
189 	status = stread(rst0, index, LABEL_SIZE);
190 
191 	st_skip(rst0);
192 
193 	for (i = 0; i < MAXPARTITIONS; i++) {
194 		pp = &(lp->d_partitions[i]);
195 		if (pp->p_size > 0) {
196 			printf("%c: ", i + 'A');
197 			printf("size = %d(0x%s), ", pp->p_size, hexstr(pp->p_size, 8));
198 			printf("offset = %d(0x%s)\n", pp->p_offset, hexstr(pp->p_offset, 8));
199 
200 			blk   = pp->p_offset;
201 			nblks = pp->p_size;
202 			size  = nblks << DEV_BSHIFT;
203 
204 			block = BUF_BLOCK;
205 			bytes = BUF_BYTES;
206 
207 			mark = nblks / block;
208 			if (nblks % block)
209 				mark++;
210 			for (j = 0; j < mark; j++)
211 				printf("-");
212 			for (j = 0; j < mark; j++)
213 				printf("%c", '\x08');
214 
215 			while (nblks > 0) {
216 				if (nblks < block) {
217 					block = nblks;
218 					bytes = nblks << DEV_BSHIFT;
219 				}
220 
221 				if (stread(rst0, dump_buf, bytes) != bytes) {
222 					printf("tape read failed !!!\n");
223 					return(ST_ERROR);
224 				}
225 
226 				if (!scsi_write(blk, dump_buf, bytes)) {
227 					printf("disk write failed !!!\n");
228 					return(ST_ERROR);
229 				}
230 
231 				blk   += block;
232 				nblks -= block;
233 				size  -= bytes;
234 
235 				printf("#");
236 			}
237 			st_skip(rst0);
238 			printf("\n\n");
239 		}
240 	}
241 }
242