1 /*- 2 * Copyright (c) 2009 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <stand.h> 29 #include <sys/param.h> 30 #include <sys/endian.h> 31 #include <sys/queue.h> 32 #include <machine/stdarg.h> 33 34 #include "bootstrap.h" 35 36 #define MD_BLOCK_SIZE 512 37 38 #ifndef MD_IMAGE_SIZE 39 #error Must be compiled with MD_IMAGE_SIZE defined 40 #endif 41 #if (MD_IMAGE_SIZE == 0 || MD_IMAGE_SIZE % MD_BLOCK_SIZE) 42 #error Image size must be a multiple of 512. 43 #endif 44 45 /* 46 * Preloaded image gets put here. 47 * Applications that patch the object with the image can determine 48 * the size looking at the start and end markers (strings), 49 * so we want them contiguous. 50 */ 51 static struct { 52 u_char start[MD_IMAGE_SIZE]; 53 u_char end[128]; 54 } md_image = { 55 .start = "MFS Filesystem goes here", 56 .end = "MFS Filesystem had better STOP here", 57 }; 58 59 /* devsw I/F */ 60 static int md_init(void); 61 static int md_strategy(void *, int, daddr_t, size_t, char *, size_t *); 62 static int md_open(struct open_file *, ...); 63 static int md_close(struct open_file *); 64 static int md_print(int); 65 66 struct devsw md_dev = { 67 .dv_name = "md", 68 .dv_type = DEVT_DISK, 69 .dv_init = md_init, 70 .dv_strategy = md_strategy, 71 .dv_open = md_open, 72 .dv_close = md_close, 73 .dv_ioctl = noioctl, 74 .dv_print = md_print, 75 .dv_cleanup = nullsys, 76 }; 77 78 static int 79 md_init(void) 80 { 81 82 return (0); 83 } 84 85 static int 86 md_strategy(void *devdata, int rw, daddr_t blk, size_t size, 87 char *buf, size_t *rsize) 88 { 89 struct devdesc *dev = (struct devdesc *)devdata; 90 size_t ofs; 91 92 if (dev->d_unit != 0) 93 return (ENXIO); 94 95 if (blk < 0 || blk >= (MD_IMAGE_SIZE / MD_BLOCK_SIZE)) 96 return (EIO); 97 98 if (size % MD_BLOCK_SIZE) 99 return (EIO); 100 101 ofs = blk * MD_BLOCK_SIZE; 102 if ((ofs + size) > MD_IMAGE_SIZE) 103 size = MD_IMAGE_SIZE - ofs; 104 105 if (rsize != NULL) 106 *rsize = size; 107 108 switch (rw & F_MASK) { 109 case F_READ: 110 bcopy(md_image.start + ofs, buf, size); 111 return (0); 112 case F_WRITE: 113 bcopy(buf, md_image.start + ofs, size); 114 return (0); 115 } 116 117 return (ENODEV); 118 } 119 120 static int 121 md_open(struct open_file *f, ...) 122 { 123 va_list ap; 124 struct devdesc *dev; 125 126 va_start(ap, f); 127 dev = va_arg(ap, struct devdesc *); 128 va_end(ap); 129 130 if (dev->d_unit != 0) 131 return (ENXIO); 132 133 return (0); 134 } 135 136 static int 137 md_close(struct open_file *f) 138 { 139 struct devdesc *dev; 140 141 dev = (struct devdesc *)(f->f_devdata); 142 return ((dev->d_unit != 0) ? ENXIO : 0); 143 } 144 145 static int 146 md_print(int verbose) 147 { 148 149 printf("%s devices:", md_dev.dv_name); 150 if (pager_output("\n") != 0) 151 return (1); 152 153 printf("MD (%u bytes)", MD_IMAGE_SIZE); 154 return (pager_output("\n")); 155 } 156