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