1 /* $NetBSD: partutil.c,v 1.15 2015/06/03 17:53:23 martin Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: partutil.c,v 1.15 2015/06/03 17:53:23 martin Exp $");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/disklabel.h>
39 #include <sys/ioctl.h>
52 #include <prop/proplib.h>
57 * Convert disklabel geometry info to disk_geom.
60 label2geom(struct disk_geom
*geo
, const struct disklabel
*lp
)
62 geo
->dg_secperunit
= lp
->d_secperunit
;
63 geo
->dg_secsize
= lp
->d_secsize
;
64 geo
->dg_nsectors
= lp
->d_nsectors
;
65 geo
->dg_ntracks
= lp
->d_ntracks
;
66 geo
->dg_ncylinders
= lp
->d_ncylinders
;
67 geo
->dg_secpercyl
= lp
->d_secpercyl
;
68 geo
->dg_pcylinders
= lp
->d_ncylinders
;
69 geo
->dg_sparespertrack
= lp
->d_sparespertrack
;
70 geo
->dg_sparespercyl
= lp
->d_sparespercyl
;
71 geo
->dg_acylinders
= lp
->d_acylinders
;
75 * Set what we need to know about disk geometry.
78 dict2geom(struct disk_geom
*geo
, prop_dictionary_t dict
)
80 (void)memset(geo
, 0, sizeof(struct disk_geom
));
81 prop_dictionary_get_int64(dict
, "sectors-per-unit",
83 prop_dictionary_get_uint32(dict
, "sector-size", &geo
->dg_secsize
);
84 prop_dictionary_get_uint32(dict
, "sectors-per-track",
86 prop_dictionary_get_uint32(dict
, "tracks-per-cylinder",
88 prop_dictionary_get_uint32(dict
, "cylinders-per-unit",
94 getdiskinfo(const char *s
, int fd
, const char *dt
, struct disk_geom
*geo
,
95 struct dkwedge_info
*dkw
)
98 struct disklabel
*lp
= &lab
;
99 prop_dictionary_t disk_dict
, geom_dict
;
100 #if !defined(__minix)
102 const struct partition
*pp
;
104 #endif /* defined(__minix) */
108 errx(1, "minix doesn't know about disk types (%s)", dt
);
110 lp
= getdiskbyname(dt
);
112 errx(1, "unknown disk type `%s'", dt
);
113 #endif /* defined(__minix) */
116 /* Get disk description dictionary */
117 #if !defined(__minix)
118 error
= prop_dictionary_recv_ioctl(fd
, DIOCGDISKINFO
, &disk_dict
);
120 /* fail quickly if the device does not exist at all */
127 #endif /* !defined(__minix) */
129 * Ask for disklabel if DIOCGDISKINFO failed. This is
130 * compatibility call and can be removed when all devices
131 * will support DIOCGDISKINFO.
132 * cgd, ccd pseudo disk drives doesn't support DIOCGDDISKINFO
134 if (ioctl(fd
, DIOCGDINFO
, lp
) == -1) {
136 warn("DIOCGDINFO on %s failed", s
);
141 geom_dict
= prop_dictionary_get(disk_dict
, "geometry");
142 dict2geom(geo
, geom_dict
);
148 /* Get info about partition/wedge */
149 if (ioctl(fd
, DIOCGWEDGEINFO
, dkw
) != -1) {
150 /* DIOCGWEDGEINFO didn't fail, we're done */
154 if (ioctl(fd
, DIOCGDINFO
, lp
) == -1) {
155 err(1, "Please implement DIOCGWEDGEINFO or "
156 "DIOCGDINFO for disk device %s", s
);
159 #if !defined(__minix)
160 /* DIOCGDINFO didn't fail */
161 (void)memset(dkw
, 0, sizeof(*dkw
));
163 if (stat(s
, &sb
) == -1)
166 ptn
= strchr(s
, '\0')[-1] - 'a';
167 if ((unsigned)ptn
>= lp
->d_npartitions
||
168 (devminor_t
)ptn
!= DISKPART(sb
.st_rdev
))
171 pp
= &lp
->d_partitions
[ptn
];
172 if (ptn
!= getrawpartition()) {
173 dkw
->dkw_offset
= pp
->p_offset
;
174 dkw
->dkw_size
= pp
->p_size
;
177 dkw
->dkw_size
= geo
->dg_secperunit
;
179 dkw
->dkw_parent
[0] = '*';
180 strlcpy(dkw
->dkw_ptype
, getfstypename(pp
->p_fstype
),
181 sizeof(dkw
->dkw_ptype
));
182 #endif /* !defined(__minix) */
188 getdisksize(const char *name
, u_int
*secsize
, off_t
*mediasize
)
190 char buf
[MAXPATHLEN
];
191 struct disk_geom geo
;
194 if ((fd
= opendisk(name
, O_RDONLY
, buf
, sizeof(buf
), 0)) == -1)
197 error
= getdiskinfo(name
, fd
, NULL
, &geo
, NULL
);
202 *secsize
= geo
.dg_secsize
;
203 *mediasize
= geo
.dg_secsize
* geo
.dg_secperunit
;