unixdev.c revision 1.2.6.1 1 /* $NetBSD: unixdev.c,v 1.2.6.1 2012/02/18 07:33:52 mrg Exp $ */
2 /* $OpenBSD: unixdev.c,v 1.6 2007/06/16 00:26:33 deraadt Exp $ */
3
4 /*
5 * Copyright (c) 1996-1998 Michael Shalayeff
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33
34 #include "boot.h"
35 #include "bootinfo.h"
36 #include "disk.h"
37 #include "unixdev.h"
38 #include "compat_linux.h"
39
40 static struct btinfo_bootdisk bi_disk;
41
42 int
43 unixstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
44 size_t *rsize)
45 {
46 off_t off;
47 int fd = (int)devdata;
48 int rc = 0;
49
50 #ifdef UNIX_DEBUG
51 printf("unixstrategy: %s %d bytes @ %d\n",
52 ((rw == F_READ) ? "reading" : "writing"), (int)size, (int)blk);
53 #endif
54
55 off = (off_t)blk * DEV_BSIZE;
56 if ((rc = ulseek(fd, off, SEEK_SET)) >= 0)
57 rc = (rw == F_READ) ? uread(fd, buf, size) :
58 uwrite(fd, buf, size);
59
60 if (rc >= 0) {
61 *rsize = (size_t)rc;
62 rc = 0;
63 } else
64 rc = errno;
65 return rc;
66 }
67
68 int
69 unixopen(struct open_file *f, ...)
70 {
71 va_list ap;
72 char path[PATH_MAX];
73 struct diskinfo *dip;
74 char *devname;
75 const char *fname;
76 u_int unit, partition;
77 int dospart;
78
79 va_start(ap, f);
80 devname = va_arg(ap, char *);
81 unit = va_arg(ap, u_int);
82 partition = va_arg(ap, u_int);
83 fname = va_arg(ap, char *);
84 va_end(ap);
85
86 #ifdef UNIX_DEBUG
87 printf("%s: devname=%s, unit=%d, partition=%d, fname=%s\n",
88 __func__, devname, unit, partition, fname);
89 #endif
90
91 f->f_devdata = NULL;
92
93 /* Find device. */
94 dip = dkdevice(devname, unit);
95 if (dip == NULL)
96 return ENOENT;
97
98 /* Try for disklabel again (might be removable media). */
99 if (dip->bios_info.flags & BDI_BADLABEL) {
100 const char *st = bios_getdisklabel(&dip->bios_info,
101 &dip->disklabel);
102 #ifdef UNIX_DEBUG
103 if (debug && st)
104 printf("%s\n", st);
105 #endif
106 if (!st) {
107 dip->bios_info.flags &= ~BDI_BADLABEL;
108 dip->bios_info.flags |= BDI_GOODLABEL;
109 } else
110 return ERDLAB;
111 }
112
113 dospart = bios_getdospart(&dip->bios_info);
114 bios_devpath(dip->bios_info.bios_number, dospart, path);
115 f->f_devdata = (void *)uopen(path, LINUX_O_RDONLY);
116 if ((int)f->f_devdata == -1)
117 return errno;
118
119 bi_disk.biosdev = dip->bios_info.bios_number;
120 bi_disk.partition = partition;
121 bi_disk.labelsector =
122 dip->disklabel.d_partitions[partition].p_offset + LABELSECTOR;
123 bi_disk.label.type = dip->disklabel.d_type;
124 memcpy(bi_disk.label.packname, dip->disklabel.d_packname, 16);
125 bi_disk.label.checksum = dip->disklabel.d_checksum;
126 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
127
128 return 0;
129 }
130
131 int
132 unixpathopen(struct open_file *f, ...)
133 {
134 va_list ap;
135 char *devname;
136 const char *fname;
137 u_int unit, partition;
138
139 va_start(ap, f);
140 devname = va_arg(ap, char *);
141 unit = va_arg(ap, u_int);
142 partition = va_arg(ap, u_int);
143 fname = va_arg(ap, char *);
144 va_end(ap);
145
146 #ifdef UNIX_DEBUG
147 printf("%s: devname=%s, unit=%d, partition=%d, fname=%s\n",
148 __func__, devname, unit, partition, fname);
149 #endif
150
151 if (fname == NULL || fname[0] == '\0')
152 return EINVAL;
153
154 f->f_devdata = (void *)uopen(fname, LINUX_O_RDONLY);
155 if ((int)f->f_devdata == -1)
156 return errno;
157
158 bi_del(BTINFO_BOOTDISK);
159
160 return 0;
161 }
162
163 int
164 unixclose(struct open_file *f)
165 {
166
167 #ifdef UNIX_DEBUG
168 printf("%s\n", __func__);
169 #endif
170
171 return uclose((int)f->f_devdata);
172 }
173
174 int
175 unixioctl(struct open_file *f, u_long cmd, void *data)
176 {
177
178 #ifdef UNIX_DEBUG
179 printf("%s: cmd=0x%08lx\n", __func__, cmd);
180 #endif
181
182 return uioctl((int)f->f_devdata, cmd, data);
183 }
184
185 off_t
186 ulseek(int fd, off_t off, int wh)
187 {
188 extern long ulseek32(int, long, int);
189 off_t r;
190
191 /* XXX only SEEK_SET is used, so anything else can fail for now. */
192
193 if (wh == SEEK_SET) {
194 if (ulseek32(fd, 0, SEEK_SET) != 0)
195 return -1;
196 while (off > OFFT_OFFSET_MAX) {
197 off -= OFFT_OFFSET_MAX;
198 if (ulseek32(fd, OFFT_OFFSET_MAX, SEEK_CUR) < 0 &&
199 errno != LINUX_EOVERFLOW)
200 return -1;
201 }
202 r = ulseek32(fd, (long)off, SEEK_CUR);
203 if (r == -1 && errno == LINUX_EOVERFLOW)
204 r = off;
205 } else
206 r = ulseek32(fd, (long)off, wh);
207
208 return r;
209 }
210