partutil.c revision 1.1 1 /* $NetBSD: partutil.c,v 1.1 2006/08/26 21:54:05 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: partutil.c,v 1.1 2006/08/26 21:54:05 christos Exp $");
41
42 #include <sys/types.h>
43 #include <sys/disklabel.h>
44 #include <sys/disk.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47
48 #include <disktab.h>
49 #include <fcntl.h>
50 #include <util.h>
51 #include <unistd.h>
52 #include <err.h>
53 #include <string.h>
54 #include <errno.h>
55
56 #include "partutil.h"
57
58 static void
59 label2geom(struct disk_geom *geo, const struct disklabel *lp)
60 {
61 geo->dg_secperunit = lp->d_secperunit;
62 geo->dg_secsize = lp->d_secsize;
63 geo->dg_nsectors = lp->d_nsectors;
64 geo->dg_ntracks = lp->d_ntracks;
65 geo->dg_ncylinders = lp->d_ncylinders;
66 geo->dg_secpercyl = lp->d_secpercyl;
67 geo->dg_pcylinders = lp->d_ncylinders;
68 geo->dg_sparespertrack = lp->d_sparespertrack;
69 geo->dg_sparespercyl = lp->d_sparespercyl;
70 geo->dg_acylinders = lp->d_acylinders;
71 }
72
73 static void
74 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
75 {
76 struct stat sb;
77 struct partition *pp;
78 int ptn;
79
80 (void)memset(dkw, 0, sizeof(*dkw));
81 if (stat(s, &sb) == -1)
82 return;
83
84 ptn = strchr(s, '\0')[-1] - 'a';
85 if (ptn >= lp->d_npartitions || ptn != DISKPART(sb.st_rdev))
86 return;
87
88 pp = &lp->d_partitions[ptn];
89 dkw->dkw_offset = pp->p_offset;
90 dkw->dkw_size = pp->p_size;
91 dkw->dkw_parent[0] = '*';
92 switch (pp->p_fstype) {
93 default:
94 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
95 break;
96 case FS_UNUSED:
97 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
98 break;
99 case FS_SWAP:
100 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
101 break;
102 case FS_BSDFFS:
103 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
104 break;
105 case FS_BSDLFS:
106 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
107 break;
108 case FS_EX2FS:
109 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
110 break;
111 case FS_ISO9660:
112 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
113 break;
114 case FS_ADOS:
115 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
116 break;
117 case FS_HFS:
118 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
119 break;
120 case FS_MSDOS:
121 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
122 break;
123 case FS_FILECORE:
124 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
125 break;
126 case FS_APPLEUFS:
127 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
128 break;
129 case FS_NTFS:
130 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
131 break;
132 }
133 }
134
135 int
136 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
137 struct dkwedge_info *dkw)
138 {
139 struct disklabel lab;
140 struct disklabel *lp = &lab;
141 char parent[1024];
142
143 if (dt) {
144 lp = getdiskbyname(dt);
145 if (lp == NULL)
146 errx(1, "%s: unknown disk type", dt);
147 goto part;
148 }
149
150 if (ioctl(fd, DIOCGDINFO, lp) == -1) {
151 if (errno == ENOTTY) {
152 int pfd;
153 if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
154 warn("ioctl (DIOCGWEDGEINFO)");
155 goto bad;
156 }
157 pfd = opendisk(dkw->dkw_parent, O_RDONLY,
158 parent, sizeof(parent), 0);
159 if (pfd == -1) {
160 warn("Cannot open `%s'", dkw->dkw_parent);
161 goto bad;
162 }
163 if (ioctl(pfd, DIOCGDINFO, lp) != -1) {
164 (void)close(pfd);
165 goto label;
166 } else {
167 int serrno = errno;
168 (void)close(pfd);
169 errno = serrno;
170 }
171 }
172 warn("ioctl (DIOCGDINFO)");
173 goto bad;
174 }
175 part:
176 part2wedge(dkw, lp, s);
177 label:
178 label2geom(geo, lp);
179 return 0;
180 bad:
181 return -1;
182 }
183