2 * Copyright (c) 2003 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
49 #include <sys/types.h>
51 #include <sys/queue.h>
56 LIST_ENTRY(sector) sectors;
61 struct simdisk_softc {
65 LIST_HEAD(,sector) sectors;
74 g_simdisk_insertsector(struct simdisk_softc *sc, struct sector *dsp)
76 struct sector *dsp2, *dsp3;
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);
85 LIST_FOREACH(dsp2, &sc->sectors, sectors) {
87 if (dsp2->offset > dsp->offset) {
88 LIST_INSERT_BEFORE(dsp2, dsp, sectors);
92 LIST_INSERT_AFTER(dsp3, dsp, sectors);
96 startElement(void *userData, const char *name, const char **atts __unused)
98 struct simdisk_softc *sc;
101 if (!strcasecmp(name, "sector")) {
102 sc->sp = calloc(1, sizeof(*sc->sp) + sc->sectorsize);
103 sc->sp->data = (u_char *)(sc->sp + 1);
105 sbuf_clear(sc->sbuf);
109 endElement(void *userData, const char *name)
111 struct simdisk_softc *sc;
119 if (!strcasecmp(name, "comment")) {
120 sbuf_clear(sc->sbuf);
123 sbuf_finish(sc->sbuf);
124 if (!strcasecmp(name, "sectorsize")) {
125 sc->sectorsize = strtoul(sbuf_data(sc->sbuf), &p, 0);
127 errx(1, "strtoul croaked on sectorsize");
128 } else if (!strcasecmp(name, "mediasize")) {
129 o = strtoull(sbuf_data(sc->sbuf), &p, 0);
131 errx(1, "strtoul croaked on mediasize");
134 } else if (!strcasecmp(name, "fwsectors")) {
135 sc->fwsectors = strtoul(sbuf_data(sc->sbuf), &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);
141 errx(1, "strtoul croaked on fwheads");
142 } else if (!strcasecmp(name, "fwcylinders")) {
143 sc->fwcylinders = strtoul(sbuf_data(sc->sbuf), &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);
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")) {
155 p = sbuf_data(sc->sbuf);
156 for (i = 0; i < sc->sectorsize; i++) {
158 errx(1, "I croaked on hexdata %d:(%02x)", i, *p);
162 j = (tolower(*p) - 'a' + 10) << 4;
165 errx(1, "I croaked on hexdata %d:(%02x)", i, *p);
169 j |= tolower(*p) - 'a' + 10;
173 } else if (!strcasecmp(name, "sector")) {
174 g_simdisk_insertsector(sc, sc->sp);
176 } else if (!strcasecmp(name, "diskimage")) {
177 } else if (!strcasecmp(name, "FreeBSD")) {
179 printf("<%s>[[%s]]\n", name, sbuf_data(sc->sbuf));
181 sbuf_clear(sc->sbuf);
185 characterData(void *userData, const XML_Char *s, int len)
188 struct simdisk_softc *sc;
193 while (isspace(*b) && b < e)
195 while (isspace(*e) && e > b)
197 if (e != b || !isspace(*b))
198 sbuf_bcat(sc->sbuf, b, e - b + 1);
201 static struct simdisk_softc *
202 g_simdisk_xml_load(const char *file)
204 XML_Parser parser = XML_ParserCreate(NULL);
207 struct simdisk_softc *sc;
210 sc = calloc(1, sizeof *sc);
211 sc->sbuf = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
212 LIST_INIT(&sc->sectors);
213 XML_SetUserData(parser, sc);
214 XML_SetElementHandler(parser, startElement, endElement);
215 XML_SetCharacterDataHandler(parser, characterData);
217 fd = open(file, O_RDONLY);
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);
224 errx(1, "XML_Parse complains: return %d", i);
225 munmap(p, st.st_size);
227 XML_ParserFree(parser);
232 main(int argc, char **argv)
234 struct simdisk_softc *sc;
240 errx(1, "Usage: %s mddevice xmlfile", argv[0]);
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]);
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);
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");