Annotation of sys/arch/zaurus/stand/zboot/unixdev.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: unixdev.c,v 1.6 2007/06/16 00:26:33 deraadt Exp $ */
2:
3: /*
4: * Copyright (c) 1996-1998 Michael Shalayeff
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: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26: * SUCH DAMAGE.
27: *
28: */
29:
30: #include <sys/param.h>
31: #include <sys/reboot.h>
32: #include <sys/disklabel.h>
33: #include <sys/syscall.h>
34: #include <sys/time.h>
35: #define open uopen
36: #include <sys/fcntl.h>
37: #include <dev/cons.h>
38: #undef open
39: #include "disk.h"
40: #include "libsa.h"
41: #include <lib/libsa/unixdev.h>
42:
43: int
44: unixstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
45: size_t *rsize)
46: {
47: int rc = 0;
48: off_t off;
49:
50: #ifdef UNIX_DEBUG
51: printf("unixstrategy: %s %d bytes @ %d\n",
52: (rw==F_READ?"reading":"writing"), size, blk);
53: #endif
54:
55: off = (off_t)blk * DEV_BSIZE;
56: if ((rc = ulseek((int)devdata, off, SEEK_SET)) >= 0)
57: rc = (rw==F_READ) ? uread((int)devdata, buf, size) :
58: uwrite((int)devdata, buf, size);
59:
60: if (rc >= 0) {
61: *rsize = (size_t)rc;
62: rc = 0;
63: } else
64: rc = errno;
65:
66: return rc;
67: }
68:
69: int
70: unixopen(struct open_file *f, ...)
71: {
72: va_list ap;
73: char path[PATH_MAX];
74: char *cp, **file;
75: dev_t maj, unit, part, bsd_dev;
76: struct diskinfo *dip;
77:
78: va_start(ap, f);
79: cp = *(file = va_arg(ap, char **));
80: va_end(ap);
81:
82: #ifdef UNIX_DEBUG
83: if (debug)
84: printf("unixopen: %s\n", cp);
85: #endif
86:
87: f->f_devdata = NULL;
88: /* Search for device specification. */
89: if (strlen(cp) < 4)
90: return ENOENT;
91: cp += 2;
92: if (cp[2] != ':') {
93: if (cp[3] != ':')
94: return ENOENT;
95: else
96: cp++;
97: }
98:
99: for (maj = 0;
100: maj < nbdevs && strncmp(*file, bdevs[maj], cp - *file) != 0;
101: maj++)
102: ;
103: if (maj >= nbdevs) {
104: printf("Unknown device: ");
105: for (cp = *file; *cp != ':'; cp++)
106: putchar(*cp);
107: putchar('\n');
108: return EADAPT;
109: }
110:
111: /* Get unit. */
112: if ('0' <= *cp && *cp <= '9')
113: unit = *cp++ - '0';
114: else {
115: printf("Bad unit number\n");
116: return EUNIT;
117: }
118:
119: /* Get partition. */
120: if ('a' <= *cp && *cp <= 'p')
121: part = *cp++ - 'a';
122: else {
123: printf("Bad partition id\n");
124: return EPART;
125: }
126:
127: cp++; /* skip ':' */
128: if (*cp != 0)
129: *file = cp;
130: else
131: f->f_flags |= F_RAW;
132:
133: /* Find device. */
134: dip = dkdevice(maj, unit);
135: if (dip == (struct diskinfo *)NULL)
136: return ENOENT;
137:
138: /* Fix up bootdev. */
139: bsd_dev = dip->bios_info.bsd_dev;
140: dip->bsddev = MAKEBOOTDEV(B_TYPE(bsd_dev), B_ADAPTOR(bsd_dev),
141: B_CONTROLLER(bsd_dev), unit, part);
142: dip->bootdev = MAKEBOOTDEV(B_TYPE(bsd_dev), B_ADAPTOR(bsd_dev),
143: B_CONTROLLER(bsd_dev), B_UNIT(bsd_dev), part);
144:
145: /* Try for disklabel again (might be removable media). */
146: if (dip->bios_info.flags & BDI_BADLABEL) {
147: const char *st = bios_getdisklabel(&dip->bios_info,
148: &dip->disklabel);
149: #ifdef UNIX_DEBUG
150: if (debug && st)
151: printf("%s\n", st);
152: #endif
153: if (!st) {
154: dip->bios_info.flags &= ~BDI_BADLABEL;
155: dip->bios_info.flags |= BDI_GOODLABEL;
156: } else
157: return ERDLAB;
158: }
159:
160: part = bios_getdospart(&dip->bios_info);
161: bios_devpath(dip->bios_info.bios_number, part, path);
162: f->f_devdata = (void *)uopen(path, O_RDONLY);
163: if ((int)f->f_devdata == -1)
164: return errno;
165:
166: return 0;
167: }
168:
169: int
170: unixclose(struct open_file *f)
171: {
172: return uclose((int)f->f_devdata);
173: }
174:
175: int
176: unixioctl(struct open_file *f, u_long cmd, void *data)
177: {
178: return uioctl((int)f->f_devdata, cmd, data);
179: }
180:
181: off_t
182: ulseek(int fd, off_t off, int wh)
183: {
184: extern long ulseek32(int, long, int);
185: off_t r;
186:
187: /* XXX only SEEK_SET is used, so anything else can fail for now. */
188:
189: if (wh == SEEK_SET) {
190: if (ulseek32(fd, 0, SEEK_SET) != 0)
191: return -1;
192: while (off > OFFT_OFFSET_MAX) {
193: off -= OFFT_OFFSET_MAX;
194: if (ulseek32(fd, OFFT_OFFSET_MAX, SEEK_CUR) < 0 &&
195: errno != LINUX_EOVERFLOW)
196: return -1;
197: }
198: r = ulseek32(fd, (long)off, SEEK_CUR);
199: if (r == -1 && errno == LINUX_EOVERFLOW)
200: r = off;
201: } else
202: r = ulseek32(fd, (long)off, wh);
203:
204: return r;
205: }
206:
207: time_t
208: getsecs(void)
209: {
210: return (time_t)syscall(__NR_time, NULL);
211: }
212:
213: unsigned int
214: sleep(unsigned int seconds)
215: {
216: unsigned int start;
217:
218: start = getsecs();
219: while (getsecs() - start < seconds)
220: ;
221:
222: return (0);
223: }
CVSweb