]>
Commit | Line | Data |
---|---|---|
91447636 A |
1 | /* |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
55e303ae A |
22 | /* |
23 | * Copyright (c) 1988 University of Utah. | |
24 | * Copyright (c) 1990, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * | |
27 | * This code is derived from software contributed to Berkeley by | |
28 | * the Systems Programming Group of the University of Utah Computer | |
29 | * Science Department. | |
30 | * | |
31 | * Redistribution and use in source and binary forms, with or without | |
32 | * modification, are permitted provided that the following conditions | |
33 | * are met: | |
34 | * 1. Redistributions of source code must retain the above copyright | |
35 | * notice, this list of conditions and the following disclaimer. | |
36 | * 2. Redistributions in binary form must reproduce the above copyright | |
37 | * notice, this list of conditions and the following disclaimer in the | |
38 | * documentation and/or other materials provided with the distribution. | |
39 | * 3. All advertising materials mentioning features or use of this software | |
40 | * must display the following acknowledgement: | |
41 | * This product includes software developed by the University of | |
42 | * California, Berkeley and its contributors. | |
43 | * 4. Neither the name of the University nor the names of its contributors | |
44 | * may be used to endorse or promote products derived from this software | |
45 | * without specific prior written permission. | |
46 | * | |
47 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
57 | * SUCH DAMAGE. | |
58 | * | |
59 | * from: Utah Hdr: vn.c 1.13 94/04/02 | |
60 | * | |
61 | * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 | |
62 | * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $ | |
63 | */ | |
64 | ||
65 | /* | |
66 | * RAM disk driver. | |
67 | * | |
68 | * Block interface to a ramdisk. | |
69 | * | |
70 | */ | |
71 | ||
55e303ae | 72 | #include <sys/param.h> |
55e303ae A |
73 | #include <sys/kernel.h> |
74 | #include <sys/mount.h> | |
75 | #include <sys/namei.h> | |
76 | #include <sys/proc.h> | |
77 | #include <sys/buf.h> | |
78 | #include <sys/malloc.h> | |
79 | #include <sys/mount.h> | |
55e303ae A |
80 | #include <sys/fcntl.h> |
81 | #include <sys/conf.h> | |
82 | #include <sys/disk.h> | |
83 | #include <sys/stat.h> | |
55e303ae | 84 | #include <sys/vm.h> |
91447636 A |
85 | #include <sys/uio_internal.h> |
86 | #include <libkern/libkern.h> | |
55e303ae | 87 | |
91447636 | 88 | #include <vm/pmap.h> |
55e303ae | 89 | #include <vm/vm_pager.h> |
55e303ae A |
90 | #include <mach/memory_object_types.h> |
91 | ||
92 | #include <miscfs/devfs/devfs.h> | |
93 | ||
91447636 A |
94 | |
95 | void mdevinit(int the_cnt); | |
96 | ||
97 | static open_close_fcn_t mdevopen; | |
98 | static open_close_fcn_t mdevclose; | |
55e303ae | 99 | static psize_fcn_t mdevsize; |
91447636 A |
100 | static strategy_fcn_t mdevstrategy; |
101 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
102 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
103 | static int mdevrw(dev_t dev, struct uio *uio, int ioflag); | |
104 | static char * nonspace(char *pos, char *end); | |
105 | static char * getspace(char *pos, char *end); | |
106 | static char * cvtnum(char *pos, char *end, unsigned int *num); | |
107 | ||
108 | extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes); | |
109 | extern void mapping_set_mod(ppnum_t pn); | |
110 | extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va); | |
111 | ||
55e303ae A |
112 | |
113 | /* | |
114 | * cdevsw | |
115 | * D_DISK we want to look like a disk | |
116 | * D_CANFREE We support B_FREEBUF | |
117 | */ | |
118 | ||
119 | static struct bdevsw mdevbdevsw = { | |
120 | /* open */ mdevopen, | |
121 | /* close */ mdevclose, | |
122 | /* strategy */ mdevstrategy, | |
123 | /* ioctl */ mdevbioctl, | |
124 | /* dump */ eno_dump, | |
125 | /* psize */ mdevsize, | |
126 | /* flags */ D_DISK, | |
127 | }; | |
128 | ||
129 | static struct cdevsw mdevcdevsw = { | |
130 | /* open */ mdevopen, | |
131 | /* close */ mdevclose, | |
132 | /* read */ mdevrw, | |
133 | /* write */ mdevrw, | |
134 | /* ioctl */ mdevcioctl, | |
135 | /* stop */ eno_stop, | |
136 | /* reset */ eno_reset, | |
137 | /* ttys */ 0, | |
138 | /* select */ eno_select, | |
139 | /* mmap */ eno_mmap, | |
140 | /* strategy */ eno_strat, | |
141 | /* getc */ eno_getc, | |
142 | /* putc */ eno_putc, | |
143 | /* flags */ D_DISK, | |
144 | }; | |
145 | ||
146 | struct mdev { | |
147 | vm_offset_t mdBase; /* file size in bytes */ | |
148 | uint32_t mdSize; /* file size in bytes */ | |
149 | int mdFlags; /* flags */ | |
150 | int mdSecsize; /* sector size */ | |
151 | int mdBDev; /* Block device number */ | |
152 | int mdCDev; /* Character device number */ | |
153 | void * mdbdevb; | |
154 | void * mdcdevb; | |
155 | } mdev[16]; | |
156 | ||
157 | /* mdFlags */ | |
158 | #define mdInited 0x01 /* This device defined */ | |
159 | #define mdRO 0x02 /* This device is read-only */ | |
160 | #define mdPhys 0x04 /* This device is in physical memory */ | |
161 | ||
162 | int mdevBMajor = -1; | |
163 | int mdevCMajor = -1; | |
164 | ||
165 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char); | |
166 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys); | |
167 | dev_t mdevlookup(int devid); | |
168 | ||
91447636 A |
169 | static int mdevclose(__unused dev_t dev, __unused int flags, |
170 | __unused int devtype, __unused struct proc *p) { | |
171 | ||
55e303ae A |
172 | return (0); |
173 | } | |
174 | ||
91447636 | 175 | static int mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p) { |
55e303ae A |
176 | |
177 | int devid; | |
178 | ||
179 | devid = minor(dev); /* Get minor device number */ | |
180 | ||
181 | if (devid > 16) return (ENXIO); /* Not valid */ | |
182 | ||
183 | if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */ | |
184 | ||
185 | return(0); | |
186 | } | |
187 | ||
91447636 | 188 | static int mdevrw(dev_t dev, struct uio *uio, __unused int ioflag) { |
55e303ae | 189 | int status; |
55e303ae | 190 | addr64_t mdata; |
91447636 A |
191 | int devid; |
192 | enum uio_seg saveflag; | |
55e303ae A |
193 | |
194 | devid = minor(dev); /* Get minor device number */ | |
195 | ||
196 | if (devid > 16) return (ENXIO); /* Not valid */ | |
197 | if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */ | |
198 | ||
199 | mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */ | |
200 | ||
201 | saveflag = uio->uio_segflg; /* Remember what the request is */ | |
91447636 A |
202 | #if LP64_DEBUG |
203 | if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) { | |
204 | panic("mdevrw - invalid uio_segflg\n"); | |
205 | } | |
206 | #endif /* LP64_DEBUG */ | |
207 | /* Make sure we are moving from physical ram if physical device */ | |
208 | if (mdev[devid].mdFlags & mdPhys) { | |
209 | if (uio->uio_segflg == UIO_USERSPACE64) | |
210 | uio->uio_segflg = UIO_PHYS_USERSPACE64; | |
211 | else if (uio->uio_segflg == UIO_USERSPACE32) | |
212 | uio->uio_segflg = UIO_PHYS_USERSPACE32; | |
213 | else | |
214 | uio->uio_segflg = UIO_PHYS_USERSPACE; | |
215 | } | |
216 | status = uiomove64(mdata, uio_resid(uio), uio); /* Move the data */ | |
55e303ae A |
217 | uio->uio_segflg = saveflag; /* Restore the flag */ |
218 | ||
219 | return (status); | |
220 | } | |
221 | ||
222 | static void mdevstrategy(struct buf *bp) { | |
91447636 | 223 | unsigned int left, lop, csize; |
55e303ae | 224 | vm_offset_t vaddr, blkoff; |
55e303ae A |
225 | int devid; |
226 | addr64_t paddr, fvaddr; | |
227 | ppnum_t pp; | |
228 | ||
91447636 | 229 | devid = minor(buf_device(bp)); /* Get minor device number */ |
55e303ae A |
230 | |
231 | if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */ | |
91447636 A |
232 | buf_seterror(bp, ENXIO); |
233 | buf_biodone(bp); | |
55e303ae A |
234 | return; |
235 | } | |
236 | ||
91447636 | 237 | buf_setresid(bp, buf_count(bp)); /* Set byte count */ |
55e303ae | 238 | |
91447636 | 239 | blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */ |
55e303ae A |
240 | |
241 | /* | |
242 | * Note that reading past end is an error, but reading at end is an EOF. For these | |
91447636 | 243 | * we just return with resid == count. |
55e303ae A |
244 | */ |
245 | ||
246 | if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */ | |
247 | if(blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */ | |
91447636 | 248 | buf_seterror(bp, EINVAL); /* Yeah, this is an error */ |
55e303ae | 249 | } |
91447636 | 250 | buf_biodone(bp); /* Return */ |
55e303ae A |
251 | return; |
252 | } | |
253 | ||
91447636 A |
254 | if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */ |
255 | buf_setcount(bp, ((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */ | |
55e303ae | 256 | } |
91447636 A |
257 | /* |
258 | * make sure the buffer's data area is | |
259 | * accessible | |
260 | */ | |
261 | if (buf_map(bp, (caddr_t *)&vaddr)) | |
262 | panic("ramstrategy: buf_map failed\n"); | |
55e303ae | 263 | |
55e303ae A |
264 | fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */ |
265 | ||
91447636 | 266 | if (buf_flags(bp) & B_READ) { /* Is this a read? */ |
55e303ae A |
267 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ |
268 | bcopy((void *)((uintptr_t)fvaddr), | |
91447636 | 269 | (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */ |
55e303ae A |
270 | } |
271 | else { | |
91447636 | 272 | left = buf_count(bp); /* Init the amount left to copy */ |
55e303ae A |
273 | while(left) { /* Go until it is all copied */ |
274 | ||
275 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
276 | csize = min(lop, left); /* Don't move more than we need to */ | |
277 | ||
278 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the sink physical address */ | |
279 | if(!pp) { /* Not found, what gives? */ | |
280 | panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
281 | } | |
282 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
283 | bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */ | |
284 | mapping_set_mod(paddr >> 12); /* Make sure we know that it is modified */ | |
285 | ||
286 | left = left - csize; /* Calculate what is left */ | |
287 | vaddr = vaddr + csize; /* Move to next sink address */ | |
288 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
289 | } | |
290 | } | |
291 | } | |
292 | else { /* This is a write */ | |
293 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ | |
294 | bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr), | |
91447636 | 295 | (size_t)buf_count(bp)); /* This is virtual, just put the data */ |
55e303ae A |
296 | } |
297 | else { | |
91447636 | 298 | left = buf_count(bp); /* Init the amount left to copy */ |
55e303ae A |
299 | while(left) { /* Go until it is all copied */ |
300 | ||
301 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
302 | csize = min(lop, left); /* Don't move more than we need to */ | |
303 | ||
304 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the source physical address */ | |
305 | if(!pp) { /* Not found, what gives? */ | |
306 | panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
307 | } | |
308 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
309 | ||
310 | bcopy_phys(paddr, fvaddr, csize); /* Move this on out */ | |
311 | ||
312 | left = left - csize; /* Calculate what is left */ | |
313 | vaddr = vaddr + csize; /* Move to next sink address */ | |
314 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
315 | } | |
316 | } | |
317 | } | |
91447636 A |
318 | /* |
319 | * buf_unmap takes care of all the cases | |
320 | * it will unmap the buffer from kernel | |
321 | * virtual space if that was the state | |
322 | * when we mapped it. | |
323 | */ | |
324 | buf_unmap(bp); | |
325 | ||
326 | buf_setresid(bp, 0); /* Nothing more to do */ | |
327 | buf_biodone(bp); /* Say we've finished */ | |
55e303ae A |
328 | } |
329 | ||
330 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
331 | return (mdevioctl(dev, cmd, data, flag, p, 0)); | |
332 | } | |
333 | ||
334 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
335 | return (mdevioctl(dev, cmd, data, flag, p, 1)); | |
336 | } | |
337 | ||
91447636 A |
338 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag, |
339 | struct proc *p, int is_char) { | |
55e303ae A |
340 | int error; |
341 | u_long *f; | |
342 | u_int64_t *o; | |
343 | int devid; | |
344 | ||
345 | devid = minor(dev); /* Get minor device number */ | |
346 | ||
347 | if (devid > 16) return (ENXIO); /* Not valid */ | |
348 | ||
91447636 | 349 | error = proc_suser(p); /* Are we superman? */ |
55e303ae A |
350 | if (error) return (error); /* Nope... */ |
351 | ||
352 | f = (u_long*)data; | |
353 | o = (u_int64_t *)data; | |
354 | ||
355 | switch (cmd) { | |
356 | ||
357 | case DKIOCGETMAXBLOCKCOUNTREAD: | |
358 | *o = 32; | |
359 | break; | |
360 | ||
361 | case DKIOCGETMAXBLOCKCOUNTWRITE: | |
362 | *o = 32; | |
363 | break; | |
364 | ||
365 | case DKIOCGETMAXSEGMENTCOUNTREAD: | |
366 | *o = 32; | |
367 | break; | |
368 | ||
369 | case DKIOCGETMAXSEGMENTCOUNTWRITE: | |
370 | *o = 32; | |
371 | break; | |
372 | ||
373 | case DKIOCGETBLOCKSIZE: | |
374 | *f = mdev[devid].mdSecsize; | |
375 | break; | |
376 | ||
377 | case DKIOCSETBLOCKSIZE: | |
378 | if (is_char) return (ENODEV); /* We can only do this for a block */ | |
379 | ||
380 | if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */ | |
381 | ||
382 | mdev[devid].mdSecsize = *f; /* set the new block size */ | |
383 | break; | |
384 | ||
385 | case DKIOCISWRITABLE: | |
386 | *f = 1; | |
387 | break; | |
388 | ||
389 | case DKIOCGETBLOCKCOUNT32: | |
390 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
391 | *f = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
392 | break; | |
393 | ||
394 | case DKIOCGETBLOCKCOUNT: | |
395 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
396 | *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
397 | break; | |
398 | ||
399 | default: | |
400 | error = ENOTTY; | |
401 | break; | |
402 | } | |
403 | return(error); | |
404 | } | |
405 | ||
406 | ||
407 | static int mdevsize(dev_t dev) { | |
408 | ||
409 | int devid; | |
410 | ||
411 | devid = minor(dev); /* Get minor device number */ | |
412 | if (devid > 16) return (ENXIO); /* Not valid */ | |
413 | ||
414 | if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */ | |
415 | ||
416 | return(mdev[devid].mdSecsize); | |
417 | } | |
418 | ||
419 | #include <pexpert/pexpert.h> | |
420 | ||
91447636 | 421 | void mdevinit(__unused int the_cnt) { |
55e303ae A |
422 | |
423 | int devid, phys; | |
424 | ppnum_t base; | |
425 | unsigned int size; | |
426 | char *ba, *lp; | |
427 | dev_t dev; | |
428 | ||
429 | ||
430 | ba = PE_boot_args(); /* Get the boot arguments */ | |
431 | lp = ba + 256; /* Point to the end */ | |
432 | ||
433 | while(1) { /* Step through, looking for our keywords */ | |
434 | phys = 0; /* Assume virtual memory device */ | |
435 | ba = nonspace(ba, lp); /* Find non-space */ | |
436 | if(ba >= lp) return; /* We are done if no more... */ | |
437 | if(((ba[0] != 'v') && (ba[0] != 'p')) | |
438 | || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=') | |
439 | || (ba[3] < '0') || (ba[3] > 'f') | |
440 | || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */ | |
441 | ||
442 | ba = getspace(ba, lp); /* Find next white space or end */ | |
443 | continue; /* Start looking for the next one */ | |
444 | } | |
445 | ||
446 | if(ba[0] == 'p') phys = 1; /* Set physical memory disk */ | |
447 | ||
448 | devid = ba[3] & 0xF; /* Assume digit */ | |
449 | if(ba[3] > '9') devid += 9; /* Adjust for hex digits */ | |
450 | ||
451 | ba = &ba[5]; /* Step past keyword */ | |
452 | ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */ | |
453 | if(ba >= lp) return; /* Malformed one at the end, leave */ | |
454 | if(ba[0] != '.') continue; /* If not length separater, try next... */ | |
455 | if(base & 0xFFF) continue; /* Only allow page aligned stuff */ | |
456 | ||
457 | ba++; /* Step past '.' */ | |
458 | ba = cvtnum(ba, lp, &size); /* Try to convert it */ | |
459 | if(!size || (size & 0xFFF)) continue; /* Allow only non-zer page size multiples */ | |
460 | if(ba < lp) { /* If we are not at end, check end character */ | |
461 | if((ba[0] != ' ') && (ba[0] != 0)) continue; /* End must be null or space */ | |
462 | } | |
463 | ||
464 | dev = mdevadd(devid, base >> 12, size >> 12, phys); /* Go add the device */ | |
465 | } | |
466 | ||
467 | return; | |
468 | ||
469 | } | |
470 | ||
471 | char *nonspace(char *pos, char *end) { /* Find next non-space in string */ | |
472 | ||
473 | if(pos >= end) return end; /* Don't go past end */ | |
474 | if(pos[0] == 0) return end; /* If at null, make end */ | |
475 | ||
476 | while(1) { /* Keep going */ | |
477 | if(pos[0] != ' ') return pos; /* Leave if we found one */ | |
478 | pos++; /* Stop */ | |
479 | if(pos >= end) return end; /* Quit if we run off end */ | |
480 | } | |
481 | } | |
482 | ||
483 | char *getspace(char *pos, char *end) { /* Find next non-space in string */ | |
484 | ||
485 | while(1) { /* Keep going */ | |
486 | if(pos >= end) return end; /* Don't go past end */ | |
487 | if(pos[0] == 0) return end; /* Leave if we hit null */ | |
488 | if(pos[0] == ' ') return pos; /* Leave if we found one */ | |
489 | pos++; /* Stop */ | |
490 | } | |
491 | } | |
492 | ||
493 | char *cvtnum(char *pos, char *end, unsigned int *num) { /* Convert to a number */ | |
494 | ||
495 | int rad, dig; | |
496 | ||
497 | *num = 0; /* Set answer to 0 to start */ | |
498 | rad = 10; | |
499 | ||
500 | if(pos >= end) return end; /* Don't go past end */ | |
501 | if(pos[0] == 0) return end; /* If at null, make end */ | |
502 | ||
503 | if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */ | |
504 | rad = 16; | |
505 | pos += 2; /* Point to the number */ | |
506 | } | |
507 | ||
508 | while(1) { /* Convert it */ | |
509 | ||
510 | if(pos >= end) return end; /* Don't go past end */ | |
511 | if(pos[0] == 0) return end; /* If at null, make end */ | |
512 | if(pos[0] < '0') return pos; /* Leave if non-digit */ | |
513 | dig = pos[0] & 0xF; /* Extract digit */ | |
514 | if(pos[0] > '9') { /* Is it bigger than 9? */ | |
515 | if(rad == 10) return pos; /* Leave if not base 10 */ | |
516 | if(!(((pos[0] >= 'A') && (pos[0] <= 'F')) | |
517 | || ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos; /* Leave if bogus char */ | |
518 | dig = dig + 9; /* Adjust for character */ | |
519 | } | |
520 | *num = (*num * rad) + dig; /* Accumulate the number */ | |
521 | pos++; /* Step on */ | |
522 | } | |
523 | } | |
524 | ||
525 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys) { | |
526 | ||
527 | int i; | |
528 | ||
529 | if(devid < 0) { | |
530 | ||
531 | devid = -1; | |
532 | for(i = 0; i < 16; i++) { /* Search all known memory devices */ | |
533 | if(!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */ | |
534 | if(devid < 0)devid = i; /* Remember first free one */ | |
535 | continue; /* Skip check */ | |
536 | } | |
537 | if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */ | |
538 | panic("mdevadd: attempt to add overlapping memory device at %08X-%08X\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1); | |
539 | } | |
540 | } | |
541 | if(devid < 0) { /* Do we have free slots? */ | |
542 | panic("mdevadd: attempt to add more than 16 memory devices\n"); | |
543 | } | |
544 | } | |
545 | else { | |
546 | if(devid >= 16) { /* Giving us something bogus? */ | |
547 | panic("mdevadd: attempt to explicitly add a bogus memory device: &08X\n", devid); | |
548 | } | |
549 | if(mdev[devid].mdFlags &mdInited) { /* Already there? */ | |
550 | panic("mdevadd: attempt to explicitly add a previously defined memory device: &08X\n", devid); | |
551 | } | |
552 | } | |
553 | ||
554 | if(mdevBMajor < 0) { /* Have we gotten a major number yet? */ | |
555 | mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */ | |
556 | if (mdevBMajor < 0) { | |
557 | printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor); | |
558 | return -1; | |
559 | } | |
560 | } | |
561 | ||
562 | if(mdevCMajor < 0) { /* Have we gotten a major number yet? */ | |
563 | mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */ | |
564 | if (mdevCMajor < 0) { | |
565 | printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor); | |
566 | return -1; | |
567 | } | |
568 | } | |
569 | ||
570 | mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */ | |
571 | mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */ | |
572 | UID_ROOT, GID_OPERATOR, | |
573 | 0600, "md%d", devid); | |
574 | if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */ | |
575 | printf("mdevadd: devfs_make_node for block failed!\n"); | |
576 | return -1; /* Nope... */ | |
577 | } | |
578 | ||
579 | mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */ | |
580 | mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */ | |
581 | UID_ROOT, GID_OPERATOR, | |
582 | 0600, "rmd%d", devid); | |
583 | if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */ | |
584 | printf("mdevadd: devfs_make_node for character failed!\n"); | |
585 | return -1; /* Nope... */ | |
586 | } | |
587 | ||
588 | mdev[devid].mdBase = base; /* Set the base address of ram disk */ | |
589 | mdev[devid].mdSize = size; /* Set the length of the ram disk */ | |
590 | mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */ | |
591 | if(phys) mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */ | |
592 | mdev[devid].mdFlags |= mdInited; /* Show we are all set up */ | |
593 | printf("Added memory device md%x/rmd%x (%08X/%08X) at %08X for %08X\n", | |
594 | devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, size << 12); | |
595 | return mdev[devid].mdBDev; | |
596 | } | |
597 | ||
598 | ||
599 | dev_t mdevlookup(int devid) { | |
600 | ||
601 | if((devid < 0) || (devid > 15)) return -1; /* Filter any bogus requests */ | |
602 | if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */ | |
603 | return mdev[devid].mdBDev; /* Return the device number */ | |
604 | } |