geom.c revision 1.1.28.1 1 1.1.28.1 martin /* $NetBSD: geom.c,v 1.1.28.1 2020/04/13 08:06:00 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright (c) 1995, 1997 Jason R. Thorpe.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Redistribution and use in source and binary forms, with or without
8 1.1 dholland * modification, are permitted provided that the following conditions
9 1.1 dholland * are met:
10 1.1 dholland * 1. Redistributions of source code must retain the above copyright
11 1.1 dholland * notice, this list of conditions and the following disclaimer.
12 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer in the
14 1.1 dholland * documentation and/or other materials provided with the distribution.
15 1.1 dholland * 3. All advertising materials mentioning features or use of this software
16 1.1 dholland * must display the following acknowledgement:
17 1.1 dholland * This product includes software developed for the NetBSD Project
18 1.1 dholland * by Jason R. Thorpe.
19 1.1 dholland * 4. The name of the author may not be used to endorse or promote products
20 1.1 dholland * derived from this software without specific prior written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 dholland * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 dholland * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 dholland * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 dholland * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 dholland * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 dholland * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 dholland * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 dholland * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 dholland * SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* Modified by Philip A. Nelson for use in sysinst. */
36 1.1 dholland
37 1.1 dholland #include <sys/param.h>
38 1.1 dholland #include <sys/ioctl.h>
39 1.1 dholland #include <fcntl.h>
40 1.1 dholland #include <unistd.h>
41 1.1 dholland #include <util.h>
42 1.1.28.1 martin #include <stdint.h>
43 1.1 dholland #include <errno.h>
44 1.1.28.1 martin #include "partutil.h"
45 1.1 dholland
46 1.1 dholland #include "defs.h"
47 1.1 dholland
48 1.1.28.1 martin bool
49 1.1.28.1 martin disk_ioctl(const char *disk, unsigned long cmd, void *d)
50 1.1 dholland {
51 1.1 dholland char diskpath[MAXPATHLEN];
52 1.1 dholland int fd;
53 1.1 dholland int sv_errno;
54 1.1 dholland
55 1.1 dholland /* Open the disk. */
56 1.1 dholland fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
57 1.1.28.1 martin if (fd == -1)
58 1.1.28.1 martin return false;
59 1.1 dholland
60 1.1.28.1 martin if (ioctl(fd, cmd, d) == -1) {
61 1.1 dholland sv_errno = errno;
62 1.1 dholland (void)close(fd);
63 1.1 dholland errno = sv_errno;
64 1.1.28.1 martin return false;
65 1.1 dholland }
66 1.1 dholland (void)close(fd);
67 1.1.28.1 martin return true;
68 1.1 dholland }
69 1.1 dholland
70 1.1.28.1 martin bool
71 1.1.28.1 martin get_wedge_list(const char *disk, struct dkwedge_list *dkwl)
72 1.1 dholland {
73 1.1.28.1 martin struct dkwedge_info *dkw;
74 1.1.28.1 martin memset(dkwl, 0, sizeof(*dkwl));
75 1.1 dholland
76 1.1.28.1 martin for (;;) {
77 1.1.28.1 martin if (!disk_ioctl(disk, DIOCLWEDGES, dkwl))
78 1.1.28.1 martin goto out;
79 1.1.28.1 martin if (dkwl->dkwl_nwedges == dkwl->dkwl_ncopied)
80 1.1.28.1 martin return true;
81 1.1.28.1 martin dkwl->dkwl_bufsize = dkwl->dkwl_nwedges * sizeof(*dkw);
82 1.1.28.1 martin dkw = realloc(dkwl->dkwl_buf, dkwl->dkwl_bufsize);
83 1.1.28.1 martin if (dkw == NULL)
84 1.1.28.1 martin goto out;
85 1.1.28.1 martin dkwl->dkwl_buf = dkw;
86 1.1.28.1 martin }
87 1.1.28.1 martin out:
88 1.1.28.1 martin free(dkwl->dkwl_buf);
89 1.1.28.1 martin return false;
90 1.1.28.1 martin }
91 1.1.28.1 martin
92 1.1.28.1 martin bool
93 1.1.28.1 martin get_wedge_info(const char *disk, struct dkwedge_info *dkw)
94 1.1.28.1 martin {
95 1.1.28.1 martin
96 1.1.28.1 martin return disk_ioctl(disk, DIOCGWEDGEINFO, dkw);
97 1.1.28.1 martin }
98 1.1.28.1 martin
99 1.1.28.1 martin bool
100 1.1.28.1 martin get_disk_geom(const char *disk, struct disk_geom *d)
101 1.1.28.1 martin {
102 1.1.28.1 martin char buf[MAXPATHLEN];
103 1.1.28.1 martin int fd, error;
104 1.1.28.1 martin
105 1.1.28.1 martin if ((fd = opendisk(disk, O_RDONLY, buf, sizeof(buf), 0)) == -1)
106 1.1.28.1 martin return false;
107 1.1.28.1 martin
108 1.1.28.1 martin error = getdiskinfo(disk, fd, NULL, d, NULL);
109 1.1.28.1 martin close(fd);
110 1.1.28.1 martin if (error < 0) {
111 1.1.28.1 martin errno = error;
112 1.1.28.1 martin return false;
113 1.1.28.1 martin }
114 1.1.28.1 martin return true;
115 1.1 dholland }
116 1.1 dholland
117 1.1.28.1 martin bool
118 1.1.28.1 martin get_label_geom(const char *disk, struct disklabel *l)
119 1.1 dholland {
120 1.1 dholland
121 1.1.28.1 martin return disk_ioctl(disk, DIOCGDINFO, l);
122 1.1 dholland }
123