]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
91447636 | 5 | * |
8f6c56a5 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. | |
14 | * | |
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 | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
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 A |
96 | #include <mach/memory_object_types.h> |
97 | ||
98 | #include <miscfs/devfs/devfs.h> | |
99 | ||
91447636 A |
100 | |
101 | void mdevinit(int the_cnt); | |
102 | ||
103 | static open_close_fcn_t mdevopen; | |
104 | static open_close_fcn_t mdevclose; | |
55e303ae | 105 | static psize_fcn_t mdevsize; |
91447636 A |
106 | static strategy_fcn_t mdevstrategy; |
107 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
108 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p); | |
109 | static int mdevrw(dev_t dev, struct uio *uio, int ioflag); | |
110 | static char * nonspace(char *pos, char *end); | |
111 | static char * getspace(char *pos, char *end); | |
112 | static char * cvtnum(char *pos, char *end, unsigned int *num); | |
113 | ||
114 | extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes); | |
115 | extern void mapping_set_mod(ppnum_t pn); | |
116 | extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va); | |
117 | ||
55e303ae A |
118 | |
119 | /* | |
120 | * cdevsw | |
121 | * D_DISK we want to look like a disk | |
122 | * D_CANFREE We support B_FREEBUF | |
123 | */ | |
124 | ||
125 | static struct bdevsw mdevbdevsw = { | |
126 | /* open */ mdevopen, | |
127 | /* close */ mdevclose, | |
128 | /* strategy */ mdevstrategy, | |
129 | /* ioctl */ mdevbioctl, | |
130 | /* dump */ eno_dump, | |
131 | /* psize */ mdevsize, | |
132 | /* flags */ D_DISK, | |
133 | }; | |
134 | ||
135 | static struct cdevsw mdevcdevsw = { | |
136 | /* open */ mdevopen, | |
137 | /* close */ mdevclose, | |
138 | /* read */ mdevrw, | |
139 | /* write */ mdevrw, | |
140 | /* ioctl */ mdevcioctl, | |
141 | /* stop */ eno_stop, | |
142 | /* reset */ eno_reset, | |
143 | /* ttys */ 0, | |
144 | /* select */ eno_select, | |
145 | /* mmap */ eno_mmap, | |
146 | /* strategy */ eno_strat, | |
147 | /* getc */ eno_getc, | |
148 | /* putc */ eno_putc, | |
149 | /* flags */ D_DISK, | |
150 | }; | |
151 | ||
152 | struct mdev { | |
153 | vm_offset_t mdBase; /* file size in bytes */ | |
154 | uint32_t mdSize; /* file size in bytes */ | |
155 | int mdFlags; /* flags */ | |
156 | int mdSecsize; /* sector size */ | |
157 | int mdBDev; /* Block device number */ | |
158 | int mdCDev; /* Character device number */ | |
159 | void * mdbdevb; | |
160 | void * mdcdevb; | |
161 | } mdev[16]; | |
162 | ||
163 | /* mdFlags */ | |
164 | #define mdInited 0x01 /* This device defined */ | |
165 | #define mdRO 0x02 /* This device is read-only */ | |
166 | #define mdPhys 0x04 /* This device is in physical memory */ | |
167 | ||
168 | int mdevBMajor = -1; | |
169 | int mdevCMajor = -1; | |
170 | ||
171 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p, int is_char); | |
172 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys); | |
173 | dev_t mdevlookup(int devid); | |
174 | ||
91447636 A |
175 | static int mdevclose(__unused dev_t dev, __unused int flags, |
176 | __unused int devtype, __unused struct proc *p) { | |
177 | ||
55e303ae A |
178 | return (0); |
179 | } | |
180 | ||
91447636 | 181 | static int mdevopen(dev_t dev, int flags, __unused int devtype, __unused struct proc *p) { |
55e303ae A |
182 | |
183 | int devid; | |
184 | ||
185 | devid = minor(dev); /* Get minor device number */ | |
186 | ||
187 | if (devid > 16) return (ENXIO); /* Not valid */ | |
188 | ||
189 | if ((flags & FWRITE) && (mdev[devid].mdFlags & mdRO)) return (EACCES); /* Currently mounted RO */ | |
190 | ||
191 | return(0); | |
192 | } | |
193 | ||
91447636 | 194 | static int mdevrw(dev_t dev, struct uio *uio, __unused int ioflag) { |
55e303ae | 195 | int status; |
55e303ae | 196 | addr64_t mdata; |
91447636 A |
197 | int devid; |
198 | enum uio_seg saveflag; | |
55e303ae A |
199 | |
200 | devid = minor(dev); /* Get minor device number */ | |
201 | ||
202 | if (devid > 16) return (ENXIO); /* Not valid */ | |
203 | if (!(mdev[devid].mdFlags & mdInited)) return (ENXIO); /* Have we actually been defined yet? */ | |
204 | ||
205 | mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */ | |
206 | ||
207 | saveflag = uio->uio_segflg; /* Remember what the request is */ | |
91447636 A |
208 | #if LP64_DEBUG |
209 | if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) { | |
210 | panic("mdevrw - invalid uio_segflg\n"); | |
211 | } | |
212 | #endif /* LP64_DEBUG */ | |
213 | /* Make sure we are moving from physical ram if physical device */ | |
214 | if (mdev[devid].mdFlags & mdPhys) { | |
215 | if (uio->uio_segflg == UIO_USERSPACE64) | |
216 | uio->uio_segflg = UIO_PHYS_USERSPACE64; | |
217 | else if (uio->uio_segflg == UIO_USERSPACE32) | |
218 | uio->uio_segflg = UIO_PHYS_USERSPACE32; | |
219 | else | |
220 | uio->uio_segflg = UIO_PHYS_USERSPACE; | |
221 | } | |
222 | status = uiomove64(mdata, uio_resid(uio), uio); /* Move the data */ | |
55e303ae A |
223 | uio->uio_segflg = saveflag; /* Restore the flag */ |
224 | ||
225 | return (status); | |
226 | } | |
227 | ||
228 | static void mdevstrategy(struct buf *bp) { | |
91447636 | 229 | unsigned int left, lop, csize; |
55e303ae | 230 | vm_offset_t vaddr, blkoff; |
55e303ae A |
231 | int devid; |
232 | addr64_t paddr, fvaddr; | |
233 | ppnum_t pp; | |
234 | ||
91447636 | 235 | devid = minor(buf_device(bp)); /* Get minor device number */ |
55e303ae A |
236 | |
237 | if ((mdev[devid].mdFlags & mdInited) == 0) { /* Have we actually been defined yet? */ | |
91447636 A |
238 | buf_seterror(bp, ENXIO); |
239 | buf_biodone(bp); | |
55e303ae A |
240 | return; |
241 | } | |
242 | ||
91447636 | 243 | buf_setresid(bp, buf_count(bp)); /* Set byte count */ |
55e303ae | 244 | |
91447636 | 245 | blkoff = buf_blkno(bp) * mdev[devid].mdSecsize; /* Get offset into file */ |
55e303ae A |
246 | |
247 | /* | |
248 | * Note that reading past end is an error, but reading at end is an EOF. For these | |
91447636 | 249 | * we just return with resid == count. |
55e303ae A |
250 | */ |
251 | ||
252 | if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */ | |
253 | if(blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */ | |
91447636 | 254 | buf_seterror(bp, EINVAL); /* Yeah, this is an error */ |
55e303ae | 255 | } |
91447636 | 256 | buf_biodone(bp); /* Return */ |
55e303ae A |
257 | return; |
258 | } | |
259 | ||
91447636 A |
260 | if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */ |
261 | buf_setcount(bp, ((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */ | |
55e303ae | 262 | } |
91447636 A |
263 | /* |
264 | * make sure the buffer's data area is | |
265 | * accessible | |
266 | */ | |
267 | if (buf_map(bp, (caddr_t *)&vaddr)) | |
268 | panic("ramstrategy: buf_map failed\n"); | |
55e303ae | 269 | |
55e303ae A |
270 | fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */ |
271 | ||
91447636 | 272 | if (buf_flags(bp) & B_READ) { /* Is this a read? */ |
55e303ae A |
273 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ |
274 | bcopy((void *)((uintptr_t)fvaddr), | |
91447636 | 275 | (void *)vaddr, (size_t)buf_count(bp)); /* This is virtual, just get the data */ |
55e303ae A |
276 | } |
277 | else { | |
91447636 | 278 | left = buf_count(bp); /* Init the amount left to copy */ |
55e303ae A |
279 | while(left) { /* Go until it is all copied */ |
280 | ||
281 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
282 | csize = min(lop, left); /* Don't move more than we need to */ | |
283 | ||
284 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the sink physical address */ | |
285 | if(!pp) { /* Not found, what gives? */ | |
286 | panic("mdevstrategy: sink address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
287 | } | |
288 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
289 | bcopy_phys(fvaddr, paddr, csize); /* Copy this on in */ | |
290 | mapping_set_mod(paddr >> 12); /* Make sure we know that it is modified */ | |
291 | ||
292 | left = left - csize; /* Calculate what is left */ | |
293 | vaddr = vaddr + csize; /* Move to next sink address */ | |
294 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
295 | } | |
296 | } | |
297 | } | |
298 | else { /* This is a write */ | |
299 | if(!(mdev[devid].mdFlags & mdPhys)) { /* Physical mapped disk? */ | |
300 | bcopy((void *)vaddr, (void *)((uintptr_t)fvaddr), | |
91447636 | 301 | (size_t)buf_count(bp)); /* This is virtual, just put the data */ |
55e303ae A |
302 | } |
303 | else { | |
91447636 | 304 | left = buf_count(bp); /* Init the amount left to copy */ |
55e303ae A |
305 | while(left) { /* Go until it is all copied */ |
306 | ||
307 | lop = min((4096 - (vaddr & 4095)), (4096 - (fvaddr & 4095))); /* Get smallest amount left on sink and source */ | |
308 | csize = min(lop, left); /* Don't move more than we need to */ | |
309 | ||
310 | pp = pmap_find_phys(kernel_pmap, (addr64_t)((unsigned int)vaddr)); /* Get the source physical address */ | |
311 | if(!pp) { /* Not found, what gives? */ | |
312 | panic("mdevstrategy: source address %016llX not mapped\n", (addr64_t)((unsigned int)vaddr)); | |
313 | } | |
314 | paddr = (addr64_t)(((addr64_t)pp << 12) | (addr64_t)(vaddr & 4095)); /* Get actual address */ | |
315 | ||
316 | bcopy_phys(paddr, fvaddr, csize); /* Move this on out */ | |
317 | ||
318 | left = left - csize; /* Calculate what is left */ | |
319 | vaddr = vaddr + csize; /* Move to next sink address */ | |
320 | fvaddr = fvaddr + csize; /* Bump to next physical address */ | |
321 | } | |
322 | } | |
323 | } | |
91447636 A |
324 | /* |
325 | * buf_unmap takes care of all the cases | |
326 | * it will unmap the buffer from kernel | |
327 | * virtual space if that was the state | |
328 | * when we mapped it. | |
329 | */ | |
330 | buf_unmap(bp); | |
331 | ||
332 | buf_setresid(bp, 0); /* Nothing more to do */ | |
333 | buf_biodone(bp); /* Say we've finished */ | |
55e303ae A |
334 | } |
335 | ||
336 | static int mdevbioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
337 | return (mdevioctl(dev, cmd, data, flag, p, 0)); | |
338 | } | |
339 | ||
340 | static int mdevcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { | |
341 | return (mdevioctl(dev, cmd, data, flag, p, 1)); | |
342 | } | |
343 | ||
91447636 A |
344 | static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag, |
345 | struct proc *p, int is_char) { | |
55e303ae A |
346 | int error; |
347 | u_long *f; | |
348 | u_int64_t *o; | |
349 | int devid; | |
350 | ||
351 | devid = minor(dev); /* Get minor device number */ | |
352 | ||
353 | if (devid > 16) return (ENXIO); /* Not valid */ | |
354 | ||
91447636 | 355 | error = proc_suser(p); /* Are we superman? */ |
55e303ae A |
356 | if (error) return (error); /* Nope... */ |
357 | ||
358 | f = (u_long*)data; | |
359 | o = (u_int64_t *)data; | |
360 | ||
361 | switch (cmd) { | |
362 | ||
363 | case DKIOCGETMAXBLOCKCOUNTREAD: | |
364 | *o = 32; | |
365 | break; | |
366 | ||
367 | case DKIOCGETMAXBLOCKCOUNTWRITE: | |
368 | *o = 32; | |
369 | break; | |
370 | ||
371 | case DKIOCGETMAXSEGMENTCOUNTREAD: | |
372 | *o = 32; | |
373 | break; | |
374 | ||
375 | case DKIOCGETMAXSEGMENTCOUNTWRITE: | |
376 | *o = 32; | |
377 | break; | |
378 | ||
379 | case DKIOCGETBLOCKSIZE: | |
380 | *f = mdev[devid].mdSecsize; | |
381 | break; | |
382 | ||
383 | case DKIOCSETBLOCKSIZE: | |
384 | if (is_char) return (ENODEV); /* We can only do this for a block */ | |
385 | ||
386 | if (*f < DEV_BSIZE) return (EINVAL); /* Too short? */ | |
387 | ||
388 | mdev[devid].mdSecsize = *f; /* set the new block size */ | |
389 | break; | |
390 | ||
391 | case DKIOCISWRITABLE: | |
392 | *f = 1; | |
393 | break; | |
394 | ||
395 | case DKIOCGETBLOCKCOUNT32: | |
396 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
397 | *f = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
398 | break; | |
399 | ||
400 | case DKIOCGETBLOCKCOUNT: | |
401 | if(!(mdev[devid].mdFlags & mdInited)) return (ENXIO); | |
402 | *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize; | |
403 | break; | |
404 | ||
405 | default: | |
406 | error = ENOTTY; | |
407 | break; | |
408 | } | |
409 | return(error); | |
410 | } | |
411 | ||
412 | ||
413 | static int mdevsize(dev_t dev) { | |
414 | ||
415 | int devid; | |
416 | ||
417 | devid = minor(dev); /* Get minor device number */ | |
418 | if (devid > 16) return (ENXIO); /* Not valid */ | |
419 | ||
420 | if ((mdev[devid].mdFlags & mdInited) == 0) return(-1); /* Not inited yet */ | |
421 | ||
422 | return(mdev[devid].mdSecsize); | |
423 | } | |
424 | ||
425 | #include <pexpert/pexpert.h> | |
426 | ||
91447636 | 427 | void mdevinit(__unused int the_cnt) { |
55e303ae A |
428 | |
429 | int devid, phys; | |
430 | ppnum_t base; | |
431 | unsigned int size; | |
432 | char *ba, *lp; | |
433 | dev_t dev; | |
434 | ||
435 | ||
436 | ba = PE_boot_args(); /* Get the boot arguments */ | |
437 | lp = ba + 256; /* Point to the end */ | |
438 | ||
439 | while(1) { /* Step through, looking for our keywords */ | |
440 | phys = 0; /* Assume virtual memory device */ | |
441 | ba = nonspace(ba, lp); /* Find non-space */ | |
442 | if(ba >= lp) return; /* We are done if no more... */ | |
443 | if(((ba[0] != 'v') && (ba[0] != 'p')) | |
444 | || (ba[1] != 'm') || (ba[2] != 'd') || (ba[4] != '=') | |
445 | || (ba[3] < '0') || (ba[3] > 'f') | |
446 | || ((ba[3] > '9') && (ba[3] < 'a'))) { /* Is this of form "vmdx=" or "pmdx=" where x is hex digit? */ | |
447 | ||
448 | ba = getspace(ba, lp); /* Find next white space or end */ | |
449 | continue; /* Start looking for the next one */ | |
450 | } | |
451 | ||
452 | if(ba[0] == 'p') phys = 1; /* Set physical memory disk */ | |
453 | ||
454 | devid = ba[3] & 0xF; /* Assume digit */ | |
455 | if(ba[3] > '9') devid += 9; /* Adjust for hex digits */ | |
456 | ||
457 | ba = &ba[5]; /* Step past keyword */ | |
458 | ba = cvtnum(ba, lp, &base); /* Convert base of memory disk */ | |
459 | if(ba >= lp) return; /* Malformed one at the end, leave */ | |
460 | if(ba[0] != '.') continue; /* If not length separater, try next... */ | |
461 | if(base & 0xFFF) continue; /* Only allow page aligned stuff */ | |
462 | ||
463 | ba++; /* Step past '.' */ | |
464 | ba = cvtnum(ba, lp, &size); /* Try to convert it */ | |
465 | if(!size || (size & 0xFFF)) continue; /* Allow only non-zer page size multiples */ | |
466 | if(ba < lp) { /* If we are not at end, check end character */ | |
467 | if((ba[0] != ' ') && (ba[0] != 0)) continue; /* End must be null or space */ | |
468 | } | |
469 | ||
470 | dev = mdevadd(devid, base >> 12, size >> 12, phys); /* Go add the device */ | |
471 | } | |
472 | ||
473 | return; | |
474 | ||
475 | } | |
476 | ||
477 | char *nonspace(char *pos, char *end) { /* Find next non-space in string */ | |
478 | ||
479 | if(pos >= end) return end; /* Don't go past end */ | |
480 | if(pos[0] == 0) return end; /* If at null, make end */ | |
481 | ||
482 | while(1) { /* Keep going */ | |
483 | if(pos[0] != ' ') return pos; /* Leave if we found one */ | |
484 | pos++; /* Stop */ | |
485 | if(pos >= end) return end; /* Quit if we run off end */ | |
486 | } | |
487 | } | |
488 | ||
489 | char *getspace(char *pos, char *end) { /* Find next non-space in string */ | |
490 | ||
491 | while(1) { /* Keep going */ | |
492 | if(pos >= end) return end; /* Don't go past end */ | |
493 | if(pos[0] == 0) return end; /* Leave if we hit null */ | |
494 | if(pos[0] == ' ') return pos; /* Leave if we found one */ | |
495 | pos++; /* Stop */ | |
496 | } | |
497 | } | |
498 | ||
499 | char *cvtnum(char *pos, char *end, unsigned int *num) { /* Convert to a number */ | |
500 | ||
501 | int rad, dig; | |
502 | ||
503 | *num = 0; /* Set answer to 0 to start */ | |
504 | rad = 10; | |
505 | ||
506 | if(pos >= end) return end; /* Don't go past end */ | |
507 | if(pos[0] == 0) return end; /* If at null, make end */ | |
508 | ||
509 | if(pos[0] == '0' && ((pos[1] == 'x') || (pos[1] == 'x'))) { /* A hex constant? */ | |
510 | rad = 16; | |
511 | pos += 2; /* Point to the number */ | |
512 | } | |
513 | ||
514 | while(1) { /* Convert it */ | |
515 | ||
516 | if(pos >= end) return end; /* Don't go past end */ | |
517 | if(pos[0] == 0) return end; /* If at null, make end */ | |
518 | if(pos[0] < '0') return pos; /* Leave if non-digit */ | |
519 | dig = pos[0] & 0xF; /* Extract digit */ | |
520 | if(pos[0] > '9') { /* Is it bigger than 9? */ | |
521 | if(rad == 10) return pos; /* Leave if not base 10 */ | |
522 | if(!(((pos[0] >= 'A') && (pos[0] <= 'F')) | |
523 | || ((pos[0] >= 'a') && (pos[0] <= 'f')))) return pos; /* Leave if bogus char */ | |
524 | dig = dig + 9; /* Adjust for character */ | |
525 | } | |
526 | *num = (*num * rad) + dig; /* Accumulate the number */ | |
527 | pos++; /* Step on */ | |
528 | } | |
529 | } | |
530 | ||
531 | dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys) { | |
532 | ||
533 | int i; | |
534 | ||
535 | if(devid < 0) { | |
536 | ||
537 | devid = -1; | |
538 | for(i = 0; i < 16; i++) { /* Search all known memory devices */ | |
539 | if(!(mdev[i].mdFlags & mdInited)) { /* Is this a free one? */ | |
540 | if(devid < 0)devid = i; /* Remember first free one */ | |
541 | continue; /* Skip check */ | |
542 | } | |
543 | if(!(((base + size -1 ) < mdev[i].mdBase) || ((mdev[i].mdBase + mdev[i].mdSize - 1) < base))) { /* Is there any overlap? */ | |
544 | panic("mdevadd: attempt to add overlapping memory device at %08X-%08X\n", mdev[i].mdBase, mdev[i].mdBase + mdev[i].mdSize - 1); | |
545 | } | |
546 | } | |
547 | if(devid < 0) { /* Do we have free slots? */ | |
548 | panic("mdevadd: attempt to add more than 16 memory devices\n"); | |
549 | } | |
550 | } | |
551 | else { | |
552 | if(devid >= 16) { /* Giving us something bogus? */ | |
553 | panic("mdevadd: attempt to explicitly add a bogus memory device: &08X\n", devid); | |
554 | } | |
555 | if(mdev[devid].mdFlags &mdInited) { /* Already there? */ | |
556 | panic("mdevadd: attempt to explicitly add a previously defined memory device: &08X\n", devid); | |
557 | } | |
558 | } | |
559 | ||
560 | if(mdevBMajor < 0) { /* Have we gotten a major number yet? */ | |
561 | mdevBMajor = bdevsw_add(-1, &mdevbdevsw); /* Add to the table and figure out a major number */ | |
562 | if (mdevBMajor < 0) { | |
563 | printf("mdevadd: error - bdevsw_add() returned %d\n", mdevBMajor); | |
564 | return -1; | |
565 | } | |
566 | } | |
567 | ||
568 | if(mdevCMajor < 0) { /* Have we gotten a major number yet? */ | |
569 | mdevCMajor = cdevsw_add_with_bdev(-1, &mdevcdevsw, mdevBMajor); /* Add to the table and figure out a major number */ | |
570 | if (mdevCMajor < 0) { | |
571 | printf("ramdevice_init: error - cdevsw_add() returned %d\n", mdevCMajor); | |
572 | return -1; | |
573 | } | |
574 | } | |
575 | ||
576 | mdev[devid].mdBDev = makedev(mdevBMajor, devid); /* Get the device number */ | |
577 | mdev[devid].mdbdevb = devfs_make_node(mdev[devid].mdBDev, DEVFS_BLOCK, /* Make the node */ | |
578 | UID_ROOT, GID_OPERATOR, | |
579 | 0600, "md%d", devid); | |
580 | if (mdev[devid].mdbdevb == NULL) { /* Did we make one? */ | |
581 | printf("mdevadd: devfs_make_node for block failed!\n"); | |
582 | return -1; /* Nope... */ | |
583 | } | |
584 | ||
585 | mdev[devid].mdCDev = makedev(mdevCMajor, devid); /* Get the device number */ | |
586 | mdev[devid].mdcdevb = devfs_make_node(mdev[devid].mdCDev, DEVFS_CHAR, /* Make the node */ | |
587 | UID_ROOT, GID_OPERATOR, | |
588 | 0600, "rmd%d", devid); | |
589 | if (mdev[devid].mdcdevb == NULL) { /* Did we make one? */ | |
590 | printf("mdevadd: devfs_make_node for character failed!\n"); | |
591 | return -1; /* Nope... */ | |
592 | } | |
593 | ||
594 | mdev[devid].mdBase = base; /* Set the base address of ram disk */ | |
595 | mdev[devid].mdSize = size; /* Set the length of the ram disk */ | |
596 | mdev[devid].mdSecsize = DEV_BSIZE; /* Set starting block size */ | |
597 | if(phys) mdev[devid].mdFlags |= mdPhys; /* Show that we are in physical memory */ | |
598 | mdev[devid].mdFlags |= mdInited; /* Show we are all set up */ | |
599 | printf("Added memory device md%x/rmd%x (%08X/%08X) at %08X for %08X\n", | |
600 | devid, devid, mdev[devid].mdBDev, mdev[devid].mdCDev, base << 12, size << 12); | |
601 | return mdev[devid].mdBDev; | |
602 | } | |
603 | ||
604 | ||
605 | dev_t mdevlookup(int devid) { | |
606 | ||
607 | if((devid < 0) || (devid > 15)) return -1; /* Filter any bogus requests */ | |
608 | if(!(mdev[devid].mdFlags & mdInited)) return -1; /* This one hasn't been defined */ | |
609 | return mdev[devid].mdBDev; /* Return the device number */ | |
610 | } |