unixdev.c revision 1.2 1 /* $NetBSD: unixdev.c,v 1.2 2011/07/17 20:54:50 joerg 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 int dev;
77 u_int unit, partition;
78 int dospart;
79
80 va_start(ap, f);
81 dev = va_arg(ap, int);
82 devname = va_arg(ap, char *);
83 unit = va_arg(ap, u_int);
84 partition = va_arg(ap, u_int);
85 fname = va_arg(ap, char *);
86 va_end(ap);
87
88 f->f_devdata = NULL;
89
90 /* Find device. */
91 dip = dkdevice(devname, unit);
92 if (dip == NULL)
93 return ENOENT;
94
95 /* Try for disklabel again (might be removable media). */
96 if (dip->bios_info.flags & BDI_BADLABEL) {
97 const char *st = bios_getdisklabel(&dip->bios_info,
98 &dip->disklabel);
99 #ifdef UNIX_DEBUG
100 if (debug && st)
101 printf("%s\n", st);
102 #endif
103 if (!st) {
104 dip->bios_info.flags &= ~BDI_BADLABEL;
105 dip->bios_info.flags |= BDI_GOODLABEL;
106 } else
107 return ERDLAB;
108 }
109
110 dospart = bios_getdospart(&dip->bios_info);
111 bios_devpath(dip->bios_info.bios_number, dospart, path);
112 f->f_devdata = (void *)uopen(path, LINUX_O_RDONLY);
113 if ((int)f->f_devdata == -1)
114 return errno;
115
116 bi_disk.biosdev = dip->bios_info.bios_number;
117 bi_disk.partition = partition;
118 bi_disk.labelsector =
119 dip->disklabel.d_partitions[partition].p_offset + LABELSECTOR;
120 bi_disk.label.type = dip->disklabel.d_type;
121 memcpy(bi_disk.label.packname, dip->disklabel.d_packname, 16);
122 bi_disk.label.checksum = dip->disklabel.d_checksum;
123 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
124
125 return 0;
126 }
127
128 int
129 unixclose(struct open_file *f)
130 {
131
132 return uclose((int)f->f_devdata);
133 }
134
135 int
136 unixioctl(struct open_file *f, u_long cmd, void *data)
137 {
138
139 return uioctl((int)f->f_devdata, cmd, data);
140 }
141
142 off_t
143 ulseek(int fd, off_t off, int wh)
144 {
145 extern long ulseek32(int, long, int);
146 off_t r;
147
148 /* XXX only SEEK_SET is used, so anything else can fail for now. */
149
150 if (wh == SEEK_SET) {
151 if (ulseek32(fd, 0, SEEK_SET) != 0)
152 return -1;
153 while (off > OFFT_OFFSET_MAX) {
154 off -= OFFT_OFFSET_MAX;
155 if (ulseek32(fd, OFFT_OFFSET_MAX, SEEK_CUR) < 0 &&
156 errno != LINUX_EOVERFLOW)
157 return -1;
158 }
159 r = ulseek32(fd, (long)off, SEEK_CUR);
160 if (r == -1 && errno == LINUX_EOVERFLOW)
161 r = off;
162 } else
163 r = ulseek32(fd, (long)off, wh);
164
165 return r;
166 }
167