geom.c revision 1.3 1 1.3 christos /* $NetBSD: geom.c,v 1.3 2019/06/21 21:54:39 christos 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.3 christos #include <stdint.h>
43 1.1 dholland #include <errno.h>
44 1.2 martin #include "partutil.h"
45 1.1 dholland
46 1.1 dholland #include "defs.h"
47 1.1 dholland
48 1.3 christos bool
49 1.3 christos 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.3 christos if (fd == -1)
58 1.3 christos return false;
59 1.1 dholland
60 1.3 christos 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.3 christos return false;
65 1.1 dholland }
66 1.1 dholland (void)close(fd);
67 1.3 christos return true;
68 1.1 dholland }
69 1.1 dholland
70 1.3 christos bool
71 1.3 christos get_wedge_list(const char *disk, struct dkwedge_list *dkwl)
72 1.3 christos {
73 1.3 christos struct dkwedge_info *dkw;
74 1.3 christos memset(dkwl, 0, sizeof(*dkwl));
75 1.3 christos
76 1.3 christos for (;;) {
77 1.3 christos if (!disk_ioctl(disk, DIOCLWEDGES, dkwl))
78 1.3 christos goto out;
79 1.3 christos if (dkwl->dkwl_nwedges == dkwl->dkwl_ncopied)
80 1.3 christos return true;
81 1.3 christos dkwl->dkwl_bufsize = dkwl->dkwl_nwedges * sizeof(*dkw);
82 1.3 christos dkw = realloc(dkwl->dkwl_buf, dkwl->dkwl_bufsize);
83 1.3 christos if (dkw == NULL)
84 1.3 christos goto out;
85 1.3 christos dkwl->dkwl_buf = dkw;
86 1.3 christos }
87 1.3 christos out:
88 1.3 christos free(dkwl->dkwl_buf);
89 1.3 christos return false;
90 1.3 christos }
91 1.3 christos
92 1.3 christos bool
93 1.3 christos get_wedge_info(const char *disk, struct dkwedge_info *dkw)
94 1.3 christos {
95 1.3 christos
96 1.3 christos return disk_ioctl(disk, DIOCGWEDGEINFO, dkw);
97 1.3 christos }
98 1.3 christos
99 1.3 christos bool
100 1.2 martin get_disk_geom(const char *disk, struct disk_geom *d)
101 1.1 dholland {
102 1.2 martin char buf[MAXPATHLEN];
103 1.2 martin int fd, error;
104 1.2 martin
105 1.2 martin if ((fd = opendisk(disk, O_RDONLY, buf, sizeof(buf), 0)) == -1)
106 1.3 christos return false;
107 1.2 martin
108 1.2 martin error = getdiskinfo(disk, fd, NULL, d, NULL);
109 1.2 martin close(fd);
110 1.2 martin if (error < 0) {
111 1.2 martin errno = error;
112 1.3 christos return false;
113 1.2 martin }
114 1.3 christos return true;
115 1.1 dholland }
116 1.1 dholland
117 1.3 christos bool
118 1.2 martin get_label_geom(const char *disk, struct disklabel *l)
119 1.1 dholland {
120 1.1 dholland
121 1.3 christos return disk_ioctl(disk, DIOCGDINFO, l);
122 1.1 dholland }
123