svhlabel.c revision 1.6 1 1.6 joerg /* $NetBSD: svhlabel.c,v 1.6 2011/08/27 18:55:58 joerg Exp $ */
2 1.1 rumble
3 1.1 rumble /*
4 1.1 rumble * Copyright (C) 2007 Stephen M. Rumble.
5 1.1 rumble * Copyright (C) 1998 Wolfgang Solfrank.
6 1.1 rumble * Copyright (C) 1998 TooLs GmbH.
7 1.1 rumble * All rights reserved.
8 1.1 rumble *
9 1.1 rumble * Redistribution and use in source and binary forms, with or without
10 1.1 rumble * modification, are permitted provided that the following conditions
11 1.1 rumble * are met:
12 1.1 rumble * 1. Redistributions of source code must retain the above copyright
13 1.1 rumble * notice, this list of conditions and the following disclaimer.
14 1.1 rumble * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 rumble * notice, this list of conditions and the following disclaimer in the
16 1.1 rumble * documentation and/or other materials provided with the distribution.
17 1.1 rumble * 3. All advertising materials mentioning features or use of this software
18 1.1 rumble * must display the following acknowledgement:
19 1.1 rumble * This product includes software developed by TooLs GmbH.
20 1.1 rumble * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1 rumble * derived from this software without specific prior written permission.
22 1.1 rumble *
23 1.1 rumble * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1 rumble * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 rumble * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 rumble * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1 rumble * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 rumble * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1 rumble * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 rumble * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1 rumble * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1 rumble * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 rumble */
34 1.1 rumble
35 1.1 rumble #include <sys/cdefs.h>
36 1.1 rumble #ifndef lint
37 1.6 joerg __RCSID("$NetBSD: svhlabel.c,v 1.6 2011/08/27 18:55:58 joerg Exp $");
38 1.1 rumble #endif /* not lint */
39 1.1 rumble
40 1.1 rumble #include <stdio.h>
41 1.1 rumble #include <err.h>
42 1.1 rumble #include <errno.h>
43 1.1 rumble #include <fcntl.h>
44 1.1 rumble #include <limits.h>
45 1.1 rumble #include <stdlib.h>
46 1.1 rumble #include <string.h>
47 1.1 rumble #include <unistd.h>
48 1.1 rumble #include <util.h>
49 1.1 rumble
50 1.1 rumble #include <sys/param.h>
51 1.1 rumble #define FSTYPENAMES
52 1.1 rumble #include <sys/disklabel.h>
53 1.1 rumble #include <sys/bootblock.h>
54 1.1 rumble #include <sys/ioctl.h>
55 1.1 rumble
56 1.4 rumble #include <fs/efs/efs.h>
57 1.3 rumble #include <fs/efs/efs_sb.h>
58 1.3 rumble
59 1.1 rumble #include "dkcksum.h"
60 1.1 rumble #include "extern.h"
61 1.1 rumble
62 1.6 joerg __dead static void usage(void);
63 1.1 rumble static void getlabel(int);
64 1.1 rumble static void setlabel(int, int);
65 1.1 rumble static int getparts(int, int);
66 1.1 rumble static int is_efs(int, uint32_t);
67 1.1 rumble static struct sgi_boot_block *convert_sgi_boot_block(unsigned char *);
68 1.1 rumble
69 1.1 rumble struct disklabel label;
70 1.1 rumble
71 1.1 rumble static void
72 1.1 rumble getlabel(int sd)
73 1.1 rumble {
74 1.1 rumble
75 1.1 rumble if (ioctl(sd, DIOCGDINFO, &label) < 0) {
76 1.1 rumble perror("get label");
77 1.1 rumble exit(1);
78 1.1 rumble }
79 1.1 rumble /*
80 1.1 rumble * Some ports seem to not set the number of partitions
81 1.1 rumble * correctly, albeit they seem to set the raw partition ok!
82 1.1 rumble */
83 1.1 rumble if (label.d_npartitions <= getrawpartition())
84 1.1 rumble label.d_npartitions = getrawpartition() + 1;
85 1.1 rumble }
86 1.1 rumble
87 1.1 rumble static void
88 1.1 rumble setlabel(int sd, int doraw)
89 1.1 rumble {
90 1.1 rumble int one = 1;
91 1.1 rumble
92 1.1 rumble label.d_checksum = 0;
93 1.1 rumble label.d_checksum = dkcksum(&label);
94 1.1 rumble if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
95 1.1 rumble perror("set label");
96 1.1 rumble exit(1);
97 1.1 rumble }
98 1.1 rumble if (!doraw)
99 1.1 rumble /* If we haven't written to the disk, don't discard on close */
100 1.1 rumble ioctl(sd, DIOCKLABEL, &one);
101 1.1 rumble
102 1.1 rumble }
103 1.1 rumble
104 1.1 rumble static int
105 1.1 rumble getparts(int sd, int verbose)
106 1.1 rumble {
107 1.1 rumble unsigned char buf[DEV_BSIZE];
108 1.1 rumble struct sgi_boot_block *vh;
109 1.1 rumble struct partition npe;
110 1.1 rumble int i, j, changed;
111 1.1 rumble
112 1.1 rumble changed = 0;
113 1.1 rumble
114 1.1 rumble if (lseek(sd, 0, SEEK_SET) == -1) {
115 1.1 rumble perror("seek vh");
116 1.1 rumble exit(1);
117 1.1 rumble }
118 1.1 rumble if ((i = read(sd, buf, sizeof(buf))) != DEV_BSIZE) {
119 1.1 rumble perror("read vh");
120 1.1 rumble exit(1);
121 1.1 rumble }
122 1.1 rumble vh = convert_sgi_boot_block(buf);
123 1.1 rumble
124 1.1 rumble if (vh->magic != SGI_BOOT_BLOCK_MAGIC)
125 1.1 rumble return (changed);
126 1.1 rumble
127 1.1 rumble if (label.d_secsize != SGI_BOOT_BLOCK_BLOCKSIZE)
128 1.1 rumble changed++;
129 1.1 rumble label.d_secsize = SGI_BOOT_BLOCK_BLOCKSIZE;
130 1.1 rumble
131 1.1 rumble for (i = j = 0; i < SGI_BOOT_BLOCK_MAXPARTITIONS; i++) {
132 1.1 rumble if (vh->partitions[i].blocks == 0)
133 1.1 rumble continue;
134 1.1 rumble
135 1.1 rumble if (j == MAXPARTITIONS)
136 1.1 rumble break;
137 1.1 rumble
138 1.1 rumble switch (vh->partitions[i].type) {
139 1.1 rumble case SGI_PTYPE_EFS:
140 1.1 rumble /*
141 1.1 rumble * For some reason, my IRIX CDs list EFS partitions as SYSV!?
142 1.1 rumble */
143 1.1 rumble case SGI_PTYPE_SYSV:
144 1.1 rumble if (is_efs(sd, vh->partitions[i].first)) {
145 1.1 rumble npe.p_fstype = FS_EFS;
146 1.1 rumble npe.p_size = vh->partitions[i].blocks;
147 1.1 rumble npe.p_offset = vh->partitions[i].first;
148 1.1 rumble npe.p_fsize = 0;
149 1.1 rumble npe.p_frag = 0;
150 1.1 rumble npe.p_cpg = 0;
151 1.1 rumble }
152 1.1 rumble break;
153 1.1 rumble
154 1.1 rumble case SGI_PTYPE_VOLUME:
155 1.5 lukem if (label.d_secperunit != (uint32_t)vh->partitions[i].blocks)
156 1.1 rumble changed++;
157 1.1 rumble label.d_secperunit = vh->partitions[i].blocks;
158 1.1 rumble continue;
159 1.1 rumble
160 1.1 rumble default:
161 1.1 rumble continue;
162 1.1 rumble }
163 1.1 rumble
164 1.1 rumble if (j >= label.d_npartitions)
165 1.1 rumble break;
166 1.1 rumble
167 1.1 rumble if (j == getrawpartition()) {
168 1.1 rumble if (++j >= label.d_npartitions)
169 1.1 rumble break;
170 1.1 rumble }
171 1.1 rumble
172 1.1 rumble if (memcmp(&label.d_partitions[j], &npe, sizeof(npe)) != 0) {
173 1.1 rumble label.d_partitions[j] = npe;
174 1.1 rumble changed++;
175 1.1 rumble }
176 1.1 rumble
177 1.1 rumble j++;
178 1.1 rumble }
179 1.1 rumble
180 1.1 rumble /* XXX - fudge */
181 1.1 rumble if (label.d_nsectors != 1 || label.d_ntracks != 1 ||
182 1.1 rumble label.d_secpercyl != 1 || label.d_ncylinders != label.d_secperunit)
183 1.1 rumble changed++;
184 1.1 rumble label.d_nsectors = 1;
185 1.1 rumble label.d_ntracks = 1;
186 1.1 rumble label.d_secpercyl = 1;
187 1.1 rumble label.d_ncylinders = label.d_secperunit;
188 1.1 rumble
189 1.1 rumble i = getrawpartition();
190 1.1 rumble if (label.d_partitions[i].p_fstype != FS_UNUSED ||
191 1.1 rumble label.d_partitions[i].p_offset != 0 ||
192 1.1 rumble label.d_partitions[i].p_size != label.d_secperunit) {
193 1.1 rumble label.d_partitions[i].p_fstype = FS_UNUSED;
194 1.1 rumble label.d_partitions[i].p_offset = 0;
195 1.1 rumble label.d_partitions[i].p_size = label.d_secperunit;
196 1.1 rumble changed++;
197 1.1 rumble }
198 1.1 rumble
199 1.1 rumble return (changed);
200 1.1 rumble }
201 1.1 rumble
202 1.1 rumble static int
203 1.1 rumble is_efs(int sd, uint32_t blkoff)
204 1.1 rumble {
205 1.4 rumble struct efs_sb sb;
206 1.1 rumble off_t oldoff;
207 1.1 rumble
208 1.1 rumble if ((oldoff = lseek(sd, 0, SEEK_CUR)) == -1) {
209 1.1 rumble perror("is_efs lseek 0");
210 1.1 rumble exit(1);
211 1.1 rumble }
212 1.1 rumble
213 1.4 rumble blkoff *= SGI_BOOT_BLOCK_BLOCKSIZE;
214 1.4 rumble if (lseek(sd, blkoff + (EFS_BB_SB * EFS_BB_SIZE), SEEK_SET) == -1) {
215 1.1 rumble perror("is_efs lseek 1");
216 1.1 rumble exit(1);
217 1.1 rumble }
218 1.1 rumble
219 1.4 rumble if (read(sd, &sb, sizeof(sb)) != sizeof(sb)) {
220 1.1 rumble perror("is_efs read");
221 1.1 rumble exit(1);
222 1.1 rumble }
223 1.1 rumble
224 1.1 rumble if (lseek(sd, oldoff, SEEK_SET) == -1) {
225 1.1 rumble perror("is_efs lseek 2");
226 1.1 rumble exit(1);
227 1.1 rumble }
228 1.1 rumble
229 1.4 rumble BE32TOH(sb.sb_magic);
230 1.1 rumble
231 1.4 rumble return (sb.sb_magic == EFS_SB_MAGIC || sb.sb_magic == EFS_SB_NEWMAGIC);
232 1.1 rumble }
233 1.1 rumble
234 1.1 rumble static struct sgi_boot_block *
235 1.1 rumble convert_sgi_boot_block(unsigned char *buf)
236 1.1 rumble {
237 1.1 rumble struct sgi_boot_block *vh;
238 1.1 rumble int i;
239 1.1 rumble
240 1.1 rumble vh = (struct sgi_boot_block *)buf;
241 1.1 rumble
242 1.1 rumble BE32TOH(vh->magic);
243 1.1 rumble BE16TOH(vh->root);
244 1.1 rumble BE16TOH(vh->swap);
245 1.1 rumble
246 1.1 rumble BE16TOH(vh->dp.dp_cyls);
247 1.1 rumble BE16TOH(vh->dp.dp_shd0);
248 1.1 rumble BE16TOH(vh->dp.dp_trks0);
249 1.1 rumble BE16TOH(vh->dp.dp_secs);
250 1.1 rumble BE16TOH(vh->dp.dp_secbytes);
251 1.1 rumble BE16TOH(vh->dp.dp_interleave);
252 1.1 rumble BE32TOH(vh->dp.dp_flags);
253 1.1 rumble BE32TOH(vh->dp.dp_datarate);
254 1.1 rumble BE32TOH(vh->dp.dp_nretries);
255 1.1 rumble BE32TOH(vh->dp.dp_mspw);
256 1.1 rumble BE16TOH(vh->dp.dp_xgap1);
257 1.1 rumble BE16TOH(vh->dp.dp_xsync);
258 1.1 rumble BE16TOH(vh->dp.dp_xrdly);
259 1.1 rumble BE16TOH(vh->dp.dp_xgap2);
260 1.1 rumble BE16TOH(vh->dp.dp_xrgate);
261 1.1 rumble BE16TOH(vh->dp.dp_xwcont);
262 1.1 rumble
263 1.1 rumble for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; i++) {
264 1.1 rumble BE32TOH(vh->voldir[i].block);
265 1.1 rumble BE32TOH(vh->voldir[i].bytes);
266 1.1 rumble }
267 1.1 rumble
268 1.1 rumble for (i = 0; i < SGI_BOOT_BLOCK_MAXPARTITIONS; i++) {
269 1.1 rumble BE32TOH(vh->partitions[i].blocks);
270 1.1 rumble BE32TOH(vh->partitions[i].first);
271 1.1 rumble BE32TOH(vh->partitions[i].type);
272 1.1 rumble }
273 1.1 rumble
274 1.1 rumble BE32TOH(vh->checksum);
275 1.1 rumble
276 1.1 rumble return (vh);
277 1.1 rumble }
278 1.1 rumble
279 1.1 rumble static void
280 1.1 rumble usage(void)
281 1.1 rumble {
282 1.1 rumble fprintf(stderr, "usage: %s [-fqrw] rawdisk\n",
283 1.1 rumble getprogname());
284 1.1 rumble exit(1);
285 1.1 rumble }
286 1.1 rumble
287 1.1 rumble
288 1.1 rumble int
289 1.1 rumble main(int argc, char **argv)
290 1.1 rumble {
291 1.1 rumble int sd, ch, changed;
292 1.1 rumble char name[MAXPATHLEN];
293 1.1 rumble int force; /* force label update */
294 1.1 rumble int raw; /* update on-disk label as well */
295 1.1 rumble int verbose; /* verbose output */
296 1.1 rumble int write_it; /* update in-core label if changed */
297 1.1 rumble
298 1.1 rumble force = 0;
299 1.1 rumble raw = 0;
300 1.1 rumble verbose = 1;
301 1.1 rumble write_it = 0;
302 1.1 rumble while ((ch = getopt(argc, argv, "fqrw")) != -1) {
303 1.1 rumble switch (ch) {
304 1.1 rumble case 'f':
305 1.1 rumble force = 1;
306 1.1 rumble break;
307 1.1 rumble case 'q':
308 1.1 rumble verbose = 0;
309 1.1 rumble break;
310 1.1 rumble case 'r':
311 1.1 rumble raw = 1;
312 1.1 rumble break;
313 1.1 rumble case 'w':
314 1.1 rumble write_it = 1;
315 1.1 rumble break;
316 1.1 rumble default:
317 1.1 rumble usage();
318 1.1 rumble }
319 1.1 rumble }
320 1.1 rumble argc -= optind;
321 1.1 rumble argv += optind;
322 1.1 rumble if (argc != 1)
323 1.1 rumble usage();
324 1.1 rumble
325 1.1 rumble if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
326 1.1 rumble (size_t)MAXPATHLEN, 1)) < 0) {
327 1.1 rumble perror(argv[0]);
328 1.1 rumble exit(1);
329 1.1 rumble }
330 1.1 rumble getlabel(sd);
331 1.1 rumble changed = getparts(sd, verbose);
332 1.1 rumble
333 1.1 rumble if (verbose) {
334 1.1 rumble putchar('\n');
335 1.1 rumble showpartitions(stdout, &label, 0);
336 1.1 rumble putchar('\n');
337 1.1 rumble }
338 1.1 rumble if (write_it) {
339 1.1 rumble if (! changed && ! force)
340 1.1 rumble printf("No change; not updating disk label.\n");
341 1.1 rumble else {
342 1.1 rumble if (verbose)
343 1.1 rumble printf("Updating in-core %sdisk label.\n",
344 1.1 rumble raw ? "and on-disk " : "");
345 1.1 rumble raw = 0; /* XXX */
346 1.1 rumble setlabel(sd, raw);
347 1.1 rumble }
348 1.1 rumble } else {
349 1.1 rumble printf("Not updating disk label.\n");
350 1.1 rumble }
351 1.1 rumble close(sd);
352 1.1 rumble return (0);
353 1.1 rumble }
354