xref: /freebsd/stand/common/md.c (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
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 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/queue.h>
34 #include <machine/stdarg.h>
35 
36 #include "bootstrap.h"
37 
38 #define	MD_BLOCK_SIZE	512
39 
40 #ifndef MD_IMAGE_SIZE
41 #error Must be compiled with MD_IMAGE_SIZE defined
42 #endif
43 #if (MD_IMAGE_SIZE == 0 || MD_IMAGE_SIZE % MD_BLOCK_SIZE)
44 #error Image size must be a multiple of 512.
45 #endif
46 
47 /*
48  * Preloaded image gets put here.
49  * Applications that patch the object with the image can determine
50  * the size looking at the start and end markers (strings),
51  * so we want them contiguous.
52  */
53 static struct {
54 	u_char start[MD_IMAGE_SIZE];
55 	u_char end[128];
56 } md_image = {
57 	.start = "MFS Filesystem goes here",
58 	.end = "MFS Filesystem had better STOP here",
59 };
60 
61 /* devsw I/F */
62 static int md_init(void);
63 static int md_strategy(void *, int, daddr_t, size_t, char *, size_t *);
64 static int md_open(struct open_file *, ...);
65 static int md_close(struct open_file *);
66 static int md_print(int);
67 
68 struct devsw md_dev = {
69 	.dv_name = "md",
70 	.dv_type = DEVT_DISK,
71 	.dv_init = md_init,
72 	.dv_strategy = md_strategy,
73 	.dv_open = md_open,
74 	.dv_close = md_close,
75 	.dv_ioctl = noioctl,
76 	.dv_print = md_print,
77 	.dv_cleanup = nullsys,
78 	.dv_fmtdev = disk_fmtdev,
79 };
80 
81 static int
82 md_init(void)
83 {
84 
85 	return (0);
86 }
87 
88 static int
89 md_strategy(void *devdata, int rw, daddr_t blk, size_t size,
90     char *buf, size_t *rsize)
91 {
92 	struct devdesc *dev = (struct devdesc *)devdata;
93 	size_t ofs;
94 
95 	if (dev->d_unit != 0)
96 		return (ENXIO);
97 
98 	if (blk < 0 || blk >= (MD_IMAGE_SIZE / MD_BLOCK_SIZE))
99 		return (EIO);
100 
101 	if (size % MD_BLOCK_SIZE)
102 		return (EIO);
103 
104 	ofs = blk * MD_BLOCK_SIZE;
105 	if ((ofs + size) > MD_IMAGE_SIZE)
106 		size = MD_IMAGE_SIZE - ofs;
107 
108 	if (rsize != NULL)
109 		*rsize = size;
110 
111 	switch (rw & F_MASK) {
112 	case F_READ:
113 		bcopy(md_image.start + ofs, buf, size);
114 		return (0);
115 	case F_WRITE:
116 		bcopy(buf, md_image.start + ofs, size);
117 		return (0);
118 	}
119 
120 	return (ENODEV);
121 }
122 
123 static int
124 md_open(struct open_file *f, ...)
125 {
126 	va_list ap;
127 	struct devdesc *dev;
128 
129 	va_start(ap, f);
130 	dev = va_arg(ap, struct devdesc *);
131 	va_end(ap);
132 
133 	if (dev->d_unit != 0)
134 		return (ENXIO);
135 
136 	return (0);
137 }
138 
139 static int
140 md_close(struct open_file *f)
141 {
142 	struct devdesc *dev;
143 
144 	dev = (struct devdesc *)(f->f_devdata);
145 	return ((dev->d_unit != 0) ? ENXIO : 0);
146 }
147 
148 static int
149 md_print(int verbose)
150 {
151 
152 	printf("%s devices:", md_dev.dv_name);
153 	if (pager_output("\n") != 0)
154 		return (1);
155 
156 	printf("MD (%u bytes)", MD_IMAGE_SIZE);
157 	return (pager_output("\n"));
158 }
159