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