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