xref: /original-bsd/sys/luna68k/stand/fsdump.c (revision d4dfefc4)
1 /*
2  * Copyright (c) 1992 OMRON Corporation.
3  * Copyright (c) 1992 The Regents of the University of California.
4  * 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	7.1 (Berkeley) 12/13/92
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 
41 int
42 fsdump(argc, argv)
43 	int   argc;
44 	char *argv[];
45 {
46 	register int i, j, io;
47 	register char *p;
48 	register int status;
49 	register int block, bytes;
50 	int scsi_id, blk, nblks, size, mark;
51 	struct stat boot_stat;
52 	struct	partition *pp;
53 
54 	scsi_id = 6;
55 	scsi_read_raw(scsi_id, 0, 1, index, LABEL_SIZE);
56 
57 	for (i = 0; i < MAXPARTITIONS; i++) {
58 		pp = &(lp->d_partitions[i]);
59 /*
60 		if ((i != 0) &&
61 		    (i != 4) &&
62 		    (i != 5)) {
63  */
64 		if (i != 0) {
65 			pp->p_size = 0;
66 		}
67 	}
68 
69 	st_rewind(rst0);
70 
71 	printf("Boot Program		");
72 	io = open("sd(0,0)boot", 0);
73 	if (io >= 0) {
74 		printf("read ... ");
75 		size = read(io, dump_buf, 1048576);
76 		close(io);
77 		printf("%d bytes ... ", size);
78 		if (size <= 0) {
79 			printf("failed\n");
80 			return(ST_ERROR);
81 		}
82 		boot_stat.st_size = size;
83 	}
84 
85 	printf("write ... ");
86 	status = stwrite(rst0, dump_buf, size);
87 	st_write_EOF(rst0);
88 
89 	if (status < size) {
90 		printf("failed\n");
91 		return(ST_ERROR);
92 	}
93 
94 	printf("done\n");
95 
96 	printf("disklabel (index)\t");
97 
98 	printf("write ... ");
99 	status = stwrite(rst0, index, LABEL_SIZE);
100 	st_write_EOF(rst0);
101 
102 	if (status < LABEL_SIZE) {
103 		printf("failed\n");
104 		return(ST_ERROR);
105 	}
106 
107 	printf("done\n\n");
108 
109 	for (i = 0; i < MAXPARTITIONS; i++) {
110 		pp = &(lp->d_partitions[i]);
111 		if (pp->p_size > 0) {
112 			printf("%c: ", i + 'A');
113 			printf("size = %d(0x%s), ", pp->p_size, hexstr(pp->p_size, 8));
114 			printf("offset = %d(0x%s)\n", pp->p_offset, hexstr(pp->p_offset, 8));
115 
116 			blk   = pp->p_offset;
117 			nblks = pp->p_size;
118 			size  = nblks << DEV_BSHIFT;
119 
120 			block = BUF_BLOCK;
121 			bytes = BUF_BYTES;
122 
123 			mark = nblks / block;
124 			if (nblks % block)
125 				mark++;
126 			for (j = 0; j < mark; j++)
127 				printf("-");
128 			for (j = 0; j < mark; j++)
129 				printf("%c", '\x08');
130 
131 			while (nblks > 0) {
132 				if (nblks < block) {
133 					block = nblks;
134 					bytes = nblks << DEV_BSHIFT;
135 				}
136 
137 				if (!scsi_read_raw(scsi_id, blk, block, dump_buf, bytes)) {
138 					printf("disk read failed !!!\n");
139 					return(ST_ERROR);
140 				}
141 
142 				if (stwrite(rst0, dump_buf, bytes) < bytes) {
143 					printf("tape write failed !!!\n");
144 					return(ST_ERROR);
145 				}
146 
147 				blk   += block;
148 				nblks -= block;
149 				size  -= bytes;
150 
151 				printf("#");
152 			}
153 
154 			st_write_EOF(rst0);
155 			printf("\n\n");
156 		}
157 	}
158 }
159 
160 extern int scsi_device;
161 char cons_buf[100];
162 
163 int
164 fsrestore(argc, argv)
165 	int   argc;
166 	char *argv[];
167 {
168 	register int i, j, status;
169 	register int block, bytes;
170 	int blk, nblks, size, mark;
171 	struct	partition *pp;
172 
173 	printf("Current SCSI device = ID %d\n", scsi_device);
174 	getline("Is it sure ? (y/n) ", cons_buf);
175 
176 	if ((cons_buf[0] != 'y') && (cons_buf[0] != 'Y'))
177 		return(ST_ERROR);
178 
179 	st_rewind(rst0);
180 
181 	st_skip(rst0);
182 
183 	status = stread(rst0, index, LABEL_SIZE);
184 
185 	st_skip(rst0);
186 
187 	for (i = 0; i < MAXPARTITIONS; i++) {
188 		pp = &(lp->d_partitions[i]);
189 		if (pp->p_size > 0) {
190 			printf("%c: ", i + 'A');
191 			printf("size = %d(0x%s), ", pp->p_size, hexstr(pp->p_size, 8));
192 			printf("offset = %d(0x%s)\n", pp->p_offset, hexstr(pp->p_offset, 8));
193 
194 			blk   = pp->p_offset;
195 			nblks = pp->p_size;
196 			size  = nblks << DEV_BSHIFT;
197 
198 			block = BUF_BLOCK;
199 			bytes = BUF_BYTES;
200 
201 			mark = nblks / block;
202 			if (nblks % block)
203 				mark++;
204 			for (j = 0; j < mark; j++)
205 				printf("-");
206 			for (j = 0; j < mark; j++)
207 				printf("%c", '\x08');
208 
209 			while (nblks > 0) {
210 				if (nblks < block) {
211 					block = nblks;
212 					bytes = nblks << DEV_BSHIFT;
213 				}
214 
215 				if (stread(rst0, dump_buf, bytes) != bytes) {
216 					printf("tape read failed !!!\n");
217 					return(ST_ERROR);
218 				}
219 
220 				if (!scsi_write(blk, dump_buf, bytes)) {
221 					printf("disk write failed !!!\n");
222 					return(ST_ERROR);
223 				}
224 
225 				blk   += block;
226 				nblks -= block;
227 				size  -= bytes;
228 
229 				printf("#");
230 			}
231 			st_skip(rst0);
232 			printf("\n\n");
233 		}
234 	}
235 }
236