xref: /freebsd/tools/regression/geom/MdLoad/MdLoad.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1 /*-
2  * Copyright (c) 2003 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <paths.h>
46 #include <fcntl.h>
47 #include <err.h>
48 #include <bsdxml.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/queue.h>
52 #include <sys/sbuf.h>
53 #include <sys/mman.h>
54 
55 struct sector {
56 	LIST_ENTRY(sector)	sectors;
57 	off_t			offset;
58 	unsigned char		*data;
59 };
60 
61 struct simdisk_softc {
62 	int			sectorsize;
63 	off_t			mediasize;
64 	off_t			lastsector;
65 	LIST_HEAD(,sector)	sectors;
66 	struct sbuf		*sbuf;
67 	struct sector		*sp;
68 	u_int			fwsectors;
69 	u_int			fwheads;
70 	u_int			fwcylinders;
71 };
72 
73 static void
74 g_simdisk_insertsector(struct simdisk_softc *sc, struct sector *dsp)
75 {
76 	struct sector *dsp2, *dsp3;
77 
78 	if (sc->lastsector < dsp->offset)
79 		sc->lastsector = dsp->offset;
80 	if (LIST_EMPTY(&sc->sectors)) {
81 		LIST_INSERT_HEAD(&sc->sectors, dsp, sectors);
82 		return;
83 	}
84 	dsp3 = NULL;
85 	LIST_FOREACH(dsp2, &sc->sectors, sectors) {
86 		dsp3 = dsp2;
87 		if (dsp2->offset > dsp->offset) {
88 			LIST_INSERT_BEFORE(dsp2, dsp, sectors);
89 			return;
90 		}
91 	}
92 	LIST_INSERT_AFTER(dsp3, dsp, sectors);
93 }
94 
95 static void
96 startElement(void *userData, const char *name, const char **atts __unused)
97 {
98 	struct simdisk_softc *sc;
99 
100 	sc = userData;
101 	if (!strcasecmp(name, "sector")) {
102 		sc->sp = calloc(1, sizeof(*sc->sp) + sc->sectorsize);
103 		sc->sp->data = (u_char *)(sc->sp + 1);
104 	}
105 	sbuf_clear(sc->sbuf);
106 }
107 
108 static void
109 endElement(void *userData, const char *name)
110 {
111 	struct simdisk_softc *sc;
112 	char *p;
113 	u_char *q;
114 	int i, j;
115 	off_t o;
116 
117 	sc = userData;
118 
119 	if (!strcasecmp(name, "comment")) {
120 		sbuf_clear(sc->sbuf);
121 		return;
122 	}
123 	sbuf_finish(sc->sbuf);
124 	if (!strcasecmp(name, "sectorsize")) {
125 		sc->sectorsize = strtoul(sbuf_data(sc->sbuf), &p, 0);
126 		if (*p != '\0')
127 			errx(1, "strtoul croaked on sectorsize");
128 	} else if (!strcasecmp(name, "mediasize")) {
129 		o = strtoull(sbuf_data(sc->sbuf), &p, 0);
130 		if (*p != '\0')
131 			errx(1, "strtoul croaked on mediasize");
132 		if (o > 0)
133 			sc->mediasize = o;
134 	} else if (!strcasecmp(name, "fwsectors")) {
135 		sc->fwsectors = strtoul(sbuf_data(sc->sbuf), &p, 0);
136 		if (*p != '\0')
137 			errx(1, "strtoul croaked on fwsectors");
138 	} else if (!strcasecmp(name, "fwheads")) {
139 		sc->fwheads = strtoul(sbuf_data(sc->sbuf), &p, 0);
140 		if (*p != '\0')
141 			errx(1, "strtoul croaked on fwheads");
142 	} else if (!strcasecmp(name, "fwcylinders")) {
143 		sc->fwcylinders = strtoul(sbuf_data(sc->sbuf), &p, 0);
144 		if (*p != '\0')
145 			errx(1, "strtoul croaked on fwcylinders");
146 	} else if (!strcasecmp(name, "offset")) {
147 		sc->sp->offset= strtoull(sbuf_data(sc->sbuf), &p, 0);
148 		if (*p != '\0')
149 			errx(1, "strtoul croaked on offset");
150 	} else if (!strcasecmp(name, "fill")) {
151 		j = strtoul(sbuf_data(sc->sbuf), NULL, 16);
152 		memset(sc->sp->data, j, sc->sectorsize);
153 	} else if (!strcasecmp(name, "hexdata")) {
154 		q = sc->sp->data;
155 		p = sbuf_data(sc->sbuf);
156 		for (i = 0; i < sc->sectorsize; i++) {
157 			if (!isxdigit(*p))
158 				errx(1, "I croaked on hexdata %d:(%02x)", i, *p);
159 			if (isdigit(*p))
160 				j = (*p - '0') << 4;
161 			else
162 				j = (tolower(*p) - 'a' + 10) << 4;
163 			p++;
164 			if (!isxdigit(*p))
165 				errx(1, "I croaked on hexdata %d:(%02x)", i, *p);
166 			if (isdigit(*p))
167 				j |= *p - '0';
168 			else
169 				j |= tolower(*p) - 'a' + 10;
170 			p++;
171 			*q++ = j;
172 		}
173 	} else if (!strcasecmp(name, "sector")) {
174 		g_simdisk_insertsector(sc, sc->sp);
175 		sc->sp = NULL;
176 	} else if (!strcasecmp(name, "diskimage")) {
177 	} else if (!strcasecmp(name, "FreeBSD")) {
178 	} else {
179 		printf("<%s>[[%s]]\n", name, sbuf_data(sc->sbuf));
180 	}
181 	sbuf_clear(sc->sbuf);
182 }
183 
184 static void
185 characterData(void *userData, const XML_Char *s, int len)
186 {
187 	const char *b, *e;
188 	struct simdisk_softc *sc;
189 
190 	sc = userData;
191 	b = s;
192 	e = s + len - 1;
193 	while (isspace(*b) && b < e)
194 		b++;
195 	while (isspace(*e) && e > b)
196 		e--;
197 	if (e != b || !isspace(*b))
198 		sbuf_bcat(sc->sbuf, b, e - b + 1);
199 }
200 
201 static struct simdisk_softc *
202 g_simdisk_xml_load(const char *file)
203 {
204 	XML_Parser parser = XML_ParserCreate(NULL);
205 	struct stat st;
206 	char *p;
207 	struct simdisk_softc *sc;
208 	int fd, i;
209 
210 	sc = calloc(1, sizeof *sc);
211 	sc->sbuf = sbuf_new_auto();
212 	LIST_INIT(&sc->sectors);
213 	XML_SetUserData(parser, sc);
214 	XML_SetElementHandler(parser, startElement, endElement);
215 	XML_SetCharacterDataHandler(parser, characterData);
216 
217 	fd = open(file, O_RDONLY);
218 	if (fd < 0)
219 		err(1, file);
220 	fstat(fd, &st);
221 	p = mmap(NULL, st.st_size, PROT_READ, MAP_NOCORE|MAP_PRIVATE, fd, 0);
222 	i = XML_Parse(parser, p, st.st_size, 1);
223 	if (i != 1)
224 		errx(1, "XML_Parse complains: return %d", i);
225 	munmap(p, st.st_size);
226 	close(fd);
227 	XML_ParserFree(parser);
228 	return (sc);
229 }
230 
231 int
232 main(int argc, char **argv)
233 {
234 	struct simdisk_softc *sc;
235 	char buf[BUFSIZ];
236 	int error, fd;
237 	struct sector *dsp;
238 
239 	if (argc != 3)
240 		errx(1, "Usage: %s mddevice xmlfile", argv[0]);
241 
242 	sc = g_simdisk_xml_load(argv[2]);
243 	if (sc->mediasize == 0)
244 		sc->mediasize = sc->lastsector + sc->sectorsize * 10;
245 	if (sc->sectorsize == 0)
246 		sc->sectorsize = 512;
247 	sprintf(buf, "mdconfig -a -t malloc -s %jd -S %d",
248 	    (intmax_t)sc->mediasize / sc->sectorsize, sc->sectorsize);
249 	if (sc->fwsectors && sc->fwheads)
250 		sprintf(buf + strlen(buf), " -x %d -y %d",
251 		    sc->fwsectors, sc->fwheads);
252 	sprintf(buf + strlen(buf), " -u %s", argv[1]);
253 	error = system(buf);
254 	if (error)
255 		return (error);
256 	fd = open(argv[1], O_RDWR);
257 	if (fd < 0 && errno == ENOENT) {
258 		sprintf(buf, "%s%s", _PATH_DEV, argv[1]);
259 		fd = open(buf, O_RDWR);
260 	}
261 	if (fd < 0)
262 		err(1, "Could not open %s", argv[1]);
263 	LIST_FOREACH(dsp, &sc->sectors, sectors) {
264 		lseek(fd, dsp->offset, SEEK_SET);
265 		error = write(fd, dsp->data, sc->sectorsize);
266 		if (error != sc->sectorsize)
267 			err(1, "write sectordata failed");
268 	}
269 	close(fd);
270 	exit (0);
271 }
272