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