1/*	$NetBSD: mbrlabel.c,v 1.26 2005/12/28 06:03:15 christos Exp $	*/
2
3/*
4 * Copyright (C) 1998 Wolfgang Solfrank.
5 * Copyright (C) 1998 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: mbrlabel.c,v 1.26 2005/12/28 06:03:15 christos Exp $");
37#endif /* not lint */
38
39#include <stdio.h>
40#include <err.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <limits.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <util.h>
48
49#include <sys/param.h>
50#define FSTYPENAMES
51#include <sys/disklabel.h>
52#include <sys/bootblock.h>
53#include <sys/ioctl.h>
54
55#include "dkcksum.h"
56#include "extern.h"
57
58__dead static void	usage(void);
59static void	getlabel(int);
60static void	setlabel(int, int);
61static int	getparts(int, u_int32_t, u_int32_t, int);
62static u_int16_t	getshort(void *);
63static u_int32_t	getlong(void *);
64
65struct disklabel label;
66
67static void
68getlabel(int sd)
69{
70
71	if (ioctl(sd, DIOCGDINFO, &label) < 0) {
72		perror("get label");
73		exit(1);
74	}
75	/*
76	 * Some ports seem to not set the number of partitions
77	 * correctly, albeit they seem to set the raw partition ok!
78	 */
79	if (label.d_npartitions <= getrawpartition())
80		label.d_npartitions = getrawpartition() + 1;
81}
82
83static void
84setlabel(int sd, int doraw)
85{
86	int one = 1;
87
88	label.d_checksum = 0;
89	label.d_checksum = dkcksum(&label);
90	if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
91		perror("set label");
92		exit(1);
93	}
94	if (!doraw)
95		/* If we haven't written to the disk, don't discard on close */
96		ioctl(sd, DIOCKLABEL, &one);
97
98}
99
100static u_int16_t
101getshort(void *p)
102{
103	unsigned char *cp = p;
104
105	return (cp[0] | (cp[1] << 8));
106}
107
108static u_int32_t
109getlong(void *p)
110{
111	unsigned char *cp = p;
112
113	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
114}
115
116static int
117getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
118{
119	unsigned char		buf[DEV_BSIZE];
120	struct mbr_partition	parts[MBR_PART_COUNT];
121	struct partition	npe;
122	off_t			loff;
123	int			i, j, unused, changed;
124
125	changed = 0;
126	loff = (off_t)off * DEV_BSIZE;
127
128	if (lseek(sd, loff, SEEK_SET) != loff) {
129		perror("seek label");
130		exit(1);
131	}
132	if (read(sd, buf, sizeof buf) != DEV_BSIZE) {
133		if (off != MBR_BBSECTOR)
134			perror("read label (sector is possibly out of "
135			    "range)");
136		else
137			perror("read label");
138		exit(1);
139	}
140	if (getshort(buf + MBR_MAGIC_OFFSET) != MBR_MAGIC)
141		return (changed);
142	memcpy(parts, buf + MBR_PART_OFFSET, sizeof parts);
143
144				/* scan partition table */
145	for (i = 0; i < MBR_PART_COUNT; i++) {
146		if (parts[i].mbrp_type == 0 ||
147				/* extended partitions are handled below */
148		    MBR_IS_EXTENDED(parts[i].mbrp_type))
149			continue;
150
151		memset((void *)&npe, 0, sizeof(npe));
152		npe.p_size = getlong(&parts[i].mbrp_size);
153		npe.p_offset = getlong(&parts[i].mbrp_start) + off;
154		npe.p_fstype = xlat_mbr_fstype(parts[i].mbrp_type);
155
156				/* find existing entry, or first free slot */
157		unused = -1;	/* flag as no free slot */
158		if (verbose)
159			printf(
160			    "Found %s partition; size %u (%u MB), offset %u\n",
161			    fstypenames[npe.p_fstype],
162			    npe.p_size, npe.p_size / 2048, npe.p_offset);
163		for (j = 0; j < label.d_npartitions; j++) {
164			struct partition *lpe;
165
166			if (j == RAW_PART)
167				continue;
168			lpe = &label.d_partitions[j];
169			if (lpe->p_size == npe.p_size &&
170			    lpe->p_offset == npe.p_offset
171#ifdef notyet
172			    && (lpe->p_fstype == npe.p_fstype ||
173			     lpe->p_fstype == FS_UNUSED) */
174#endif
175			     ) {
176				if (verbose)
177					printf(
178			    "  skipping existing %s partition at slot %c.\n",
179					    fstypenames[lpe->p_fstype],
180					    j + 'a');
181				unused = -2;	/* flag as existing */
182				break;
183			}
184			if (unused == -1 && lpe->p_size == 0 &&
185			    lpe->p_fstype == FS_UNUSED)
186				unused = j;
187		}
188		if (unused == -2)
189			continue;	/* entry exists, skip... */
190		if (unused == -1) {
191			if (label.d_npartitions < MAXPARTITIONS) {
192				unused = label.d_npartitions;
193				label.d_npartitions++;
194			} else {
195				printf(
196				"  WARNING: no slots free for %s partition.\n",
197				    fstypenames[npe.p_fstype]);
198				continue;
199			}
200		}
201
202		if (verbose)
203			printf("  adding %s partition to slot %c.\n",
204			    fstypenames[npe.p_fstype], unused + 'a');
205		switch (npe.p_fstype) {
206		case FS_BSDFFS:
207		case FS_APPLEUFS:
208			npe.p_size = 16384;	/* XXX */
209			npe.p_fsize = 1024;
210			npe.p_frag = 8;
211			npe.p_cpg = 16;
212			break;
213#ifdef	__does_not_happen__
214		case FS_BSDLFS:
215			npe.p_size = 16384;	/* XXX */
216			npe.p_fsize = 1024;
217			npe.p_frag = 8;
218			npe.p_sgs = XXX;
219			break;
220#endif
221		}
222		changed++;
223		label.d_partitions[unused] = npe;
224	}
225
226				/* recursively scan extended partitions */
227	for (i = 0; i < MBR_PART_COUNT; i++) {
228		u_int32_t poff;
229
230		if (MBR_IS_EXTENDED(parts[i].mbrp_type)) {
231			poff = getlong(&parts[i].mbrp_start) + extoff;
232			changed += getparts(sd, poff,
233			    extoff ? extoff : poff, verbose);
234		}
235	}
236	return (changed);
237}
238
239static void
240usage(void)
241{
242	fprintf(stderr, "usage: %s [-fqrw] [-s sector] rawdisk\n",
243	    getprogname());
244	exit(1);
245}
246
247
248int
249main(int argc, char **argv)
250{
251	int	sd, ch, changed;
252	char	*ep, name[MAXPATHLEN];
253	int	force;			/* force label update */
254	int	raw;			/* update on-disk label as well */
255	int	verbose;		/* verbose output */
256	int	write_it;		/* update in-core label if changed */
257	uint32_t sector;		/* sector that contains the MBR */
258	ulong	ulsector;
259
260	force = 0;
261	raw = 0;
262	verbose = 1;
263	write_it = 0;
264	sector = MBR_BBSECTOR;
265	while ((ch = getopt(argc, argv, "fqrs:w")) != -1) {
266		switch (ch) {
267		case 'f':
268			force = 1;
269			break;
270		case 'q':
271			verbose = 0;
272			break;
273		case 'r':
274			raw = 1;
275			break;
276		case 's':
277			errno = 0;
278			ulsector = strtoul(optarg, &ep, 10);
279			if (optarg[0] == '\0' || *ep != '\0')
280				errx(EXIT_FAILURE,
281				    "sector number (%s) incorrectly specified",
282				    optarg);
283			if ((errno == ERANGE && ulsector == ULONG_MAX) ||
284			    ulsector > UINT32_MAX)
285			    	errx(EXIT_FAILURE,
286				    "sector number (%s) out of range",
287				    optarg);
288			sector = (uint32_t)ulsector;
289			break;
290		case 'w':
291			write_it = 1;
292			break;
293		default:
294			usage();
295		}
296	}
297	argc -= optind;
298	argv += optind;
299	if (argc != 1)
300		usage();
301
302	if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
303	    (size_t)MAXPATHLEN, 0)) < 0) {
304		perror(argv[0]);
305		exit(1);
306	}
307	getlabel(sd);
308	changed = getparts(sd, sector, 0, verbose);
309
310	if (verbose) {
311		putchar('\n');
312		showpartitions(stdout, &label, 0);
313		putchar('\n');
314	}
315	if (write_it) {
316		if (! changed && ! force)
317			printf("No change; not updating disk label.\n");
318		else {
319			if (verbose)
320				printf("Updating in-core %sdisk label.\n",
321				    raw ? "and on-disk " : "");
322			setlabel(sd, raw);
323		}
324	} else {
325		printf("Not updating disk label.\n");
326	}
327	close(sd);
328	return (0);
329}
330