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