partutil.c revision 1.5 1 1.5 haad /* $NetBSD: partutil.c,v 1.5 2009/06/05 21:52:31 haad Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.5 haad __RCSID("$NetBSD: partutil.c,v 1.5 2009/06/05 21:52:31 haad Exp $");
34 1.1 christos
35 1.1 christos #include <sys/types.h>
36 1.1 christos #include <sys/disklabel.h>
37 1.1 christos #include <sys/disk.h>
38 1.1 christos #include <sys/ioctl.h>
39 1.1 christos #include <sys/stat.h>
40 1.1 christos
41 1.5 haad
42 1.1 christos #include <disktab.h>
43 1.5 haad #include <err.h>
44 1.5 haad #include <errno.h>
45 1.1 christos #include <fcntl.h>
46 1.1 christos #include <util.h>
47 1.1 christos #include <unistd.h>
48 1.5 haad #include <stdlib.h>
49 1.1 christos #include <string.h>
50 1.5 haad
51 1.5 haad #include <prop/proplib.h>
52 1.1 christos
53 1.1 christos #include "partutil.h"
54 1.1 christos
55 1.5 haad
56 1.5 haad /*
57 1.5 haad * Set what we need to know about disk geometry.
58 1.5 haad */
59 1.1 christos static void
60 1.5 haad dict2geom(struct disk_geom *geo, prop_dictionary_t dict)
61 1.1 christos {
62 1.5 haad memset(geo, 0, sizeof(struct disk_geom));
63 1.5 haad prop_dictionary_get_int64(dict, "sectors-per-unit", &geo->dg_secperunit);
64 1.5 haad prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize);
65 1.5 haad prop_dictionary_get_uint32(dict, "sectors-per-track", &geo->dg_nsectors);
66 1.5 haad prop_dictionary_get_uint32(dict, "tracks-per-cylinder", &geo->dg_ntracks);
67 1.5 haad prop_dictionary_get_uint32(dict, "cylinders-per-unit", &geo->dg_ncylinders);
68 1.1 christos }
69 1.1 christos
70 1.5 haad
71 1.1 christos static void
72 1.1 christos part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
73 1.1 christos {
74 1.1 christos struct stat sb;
75 1.2 christos const struct partition *pp;
76 1.1 christos int ptn;
77 1.1 christos
78 1.1 christos (void)memset(dkw, 0, sizeof(*dkw));
79 1.1 christos if (stat(s, &sb) == -1)
80 1.1 christos return;
81 1.1 christos
82 1.1 christos ptn = strchr(s, '\0')[-1] - 'a';
83 1.4 lukem if ((unsigned)ptn >= lp->d_npartitions ||
84 1.4 lukem (devminor_t)ptn != DISKPART(sb.st_rdev))
85 1.1 christos return;
86 1.1 christos
87 1.1 christos pp = &lp->d_partitions[ptn];
88 1.1 christos dkw->dkw_offset = pp->p_offset;
89 1.1 christos dkw->dkw_size = pp->p_size;
90 1.1 christos dkw->dkw_parent[0] = '*';
91 1.1 christos switch (pp->p_fstype) {
92 1.1 christos default:
93 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
94 1.1 christos break;
95 1.1 christos case FS_UNUSED:
96 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
97 1.1 christos break;
98 1.1 christos case FS_SWAP:
99 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
100 1.1 christos break;
101 1.1 christos case FS_BSDFFS:
102 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
103 1.1 christos break;
104 1.1 christos case FS_BSDLFS:
105 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
106 1.1 christos break;
107 1.1 christos case FS_EX2FS:
108 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
109 1.1 christos break;
110 1.1 christos case FS_ISO9660:
111 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
112 1.1 christos break;
113 1.1 christos case FS_ADOS:
114 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
115 1.1 christos break;
116 1.1 christos case FS_HFS:
117 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
118 1.1 christos break;
119 1.1 christos case FS_MSDOS:
120 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
121 1.1 christos break;
122 1.1 christos case FS_FILECORE:
123 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
124 1.1 christos break;
125 1.1 christos case FS_APPLEUFS:
126 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
127 1.1 christos break;
128 1.1 christos case FS_NTFS:
129 1.1 christos (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
130 1.1 christos break;
131 1.1 christos }
132 1.1 christos }
133 1.1 christos
134 1.1 christos int
135 1.1 christos getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
136 1.1 christos struct dkwedge_info *dkw)
137 1.1 christos {
138 1.1 christos struct disklabel lab;
139 1.1 christos struct disklabel *lp = &lab;
140 1.5 haad prop_dictionary_t disk_dict, geom_dict;
141 1.1 christos
142 1.1 christos if (dt) {
143 1.1 christos lp = getdiskbyname(dt);
144 1.1 christos if (lp == NULL)
145 1.1 christos errx(1, "%s: unknown disk type", dt);
146 1.1 christos }
147 1.1 christos
148 1.5 haad /* Get disk description dictionary */
149 1.5 haad if (prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict) != 0) {
150 1.5 haad warn("Please implement DIOCGDISKINFO for %s\n disk driver\n", s);
151 1.5 haad return (errno);
152 1.1 christos }
153 1.5 haad
154 1.5 haad geom_dict = prop_dictionary_get(disk_dict, "geometry");
155 1.5 haad dict2geom(geo, geom_dict);
156 1.5 haad
157 1.5 haad /* Get info about partition/wedge */
158 1.5 haad if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
159 1.5 haad warn("ioctl (DIOCGWEDGEINFO)");
160 1.5 haad printf("Using old disklabel method\n");
161 1.5 haad if (ioctl(fd, DIOCGDINFO, lp) == -1)
162 1.5 haad errx(errno, "Please implement DIOCGWEDGEINFO or DIOCGDINFO for disk device %s\n", s);
163 1.5 haad
164 1.5 haad part2wedge(dkw, lp, s);
165 1.5 haad }
166 1.5 haad
167 1.1 christos return 0;
168 1.1 christos }
169