From owner-svn-src-user@FreeBSD.ORG Fri Jul 13 09:43:45 2012 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ACCB81065785; Fri, 13 Jul 2012 09:43:45 +0000 (UTC) (envelope-from ae@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 96E3E8FC16; Fri, 13 Jul 2012 09:43:45 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q6D9hj44060260; Fri, 13 Jul 2012 09:43:45 GMT (envelope-from ae@svn.freebsd.org) Received: (from ae@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q6D9hjwA060257; Fri, 13 Jul 2012 09:43:45 GMT (envelope-from ae@svn.freebsd.org) Message-Id: <201207130943.q6D9hjwA060257@svn.freebsd.org> From: "Andrey V. Elsukov" Date: Fri, 13 Jul 2012 09:43:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r238420 - user/ae/bootcode/tools/tools/bootparttest X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Jul 2012 09:43:45 -0000 Author: ae Date: Fri Jul 13 09:43:45 2012 New Revision: 238420 URL: http://svn.freebsd.org/changeset/base/238420 Log: Add simple test program that uses the partition tables handling code. It is useful to test and debug how boot loader handles partition tables metadata. Added: user/ae/bootcode/tools/tools/bootparttest/ user/ae/bootcode/tools/tools/bootparttest/Makefile (contents, props changed) user/ae/bootcode/tools/tools/bootparttest/bootparttest.c (contents, props changed) user/ae/bootcode/tools/tools/bootparttest/malloc.c (contents, props changed) Added: user/ae/bootcode/tools/tools/bootparttest/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/ae/bootcode/tools/tools/bootparttest/Makefile Fri Jul 13 09:43:45 2012 (r238420) @@ -0,0 +1,20 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../../sys/boot/common + +BINDIR?= /usr/bin +SCRIPTSDIR?= /usr/bin + +PROG= bootparttest +NO_MAN= + +SRCS= bootparttest.c crc32.c malloc.c part.c + +CFLAGS= -I${.CURDIR}/../../../sys/boot/common -I. \ + -DLOADER_GPT_SUPPORT -DLOADER_MBR_SUPPORT -DPART_DEBUG + +DPADD+= ${LIBGEOM} ${LIBUTIL} +LDADD+= ${LIBGEOM} ${LIBUTIL} +LDFLAGS+= -lgeom -lutil + +.include Added: user/ae/bootcode/tools/tools/bootparttest/bootparttest.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/ae/bootcode/tools/tools/bootparttest/bootparttest.c Fri Jul 13 09:43:45 2012 (r238420) @@ -0,0 +1,172 @@ +/*- + * Copyright (c) 2012 Andrey V. Elsukov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +struct disk { + const char *name; + uint64_t mediasize; + uint16_t sectorsize; + + int fd; + int file; + off_t offset; +}; + +static int +diskread(void *arg, void *buf, size_t blocks, off_t offset) +{ + struct disk *dp; + + dp = (struct disk *)arg; + printf("%s: read %d blocks from the offset %jd\n", dp->name, + blocks, offset); + if (offset >= dp->mediasize / dp->sectorsize) + return (-1); + + return (pread(dp->fd, buf, blocks * dp->sectorsize, + (offset + dp->offset) * dp->sectorsize) != blocks * dp->sectorsize); +} + +static const char* +ptable_type2str(const struct ptable *table) +{ + + switch (ptable_gettype(table)) { + case PTABLE_NONE: + return ("None"); + case PTABLE_BSD: + return ("BSD"); + case PTABLE_MBR: + return ("MBR"); + case PTABLE_GPT: + return ("GPT"); + case PTABLE_VTOC8: + return ("VTOC8"); + }; + return ("Unknown"); +} + +#define PWIDTH 35 +static void +ptable_print(void *arg, const char *pname, const struct ptable_entry *part) +{ + struct ptable *table; + struct disk *dp, bsd; + char line[80], size[6]; + + dp = (struct disk *)arg; + sprintf(line, " %s%s: %s", dp->file ? "disk0": dp->name, pname, + parttype2str(part->type)); + humanize_number(size, sizeof(size), + (part->end - part->start + 1) * dp->sectorsize, "", + HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); + printf("%-*s%s\n", PWIDTH, line, size); + if (part->type == PART_FREEBSD) { + sprintf(line, "%s%s", dp->file ? "disk0": dp->name, pname); + bsd.name = line; + bsd.fd = dp->fd; + bsd.file = 0; /* to use dp->name in the next sprintf */ + bsd.offset = dp->offset + part->start; + bsd.sectorsize = dp->sectorsize; + bsd.mediasize = (part->end - part->start + 1) * dp->sectorsize; + table = ptable_open(&bsd, bsd.mediasize / bsd.sectorsize, + bsd.sectorsize, diskread); + if (table == NULL) + return; + ptable_iterate(table, &bsd, ptable_print); + ptable_close(table); + } +} +#undef PWIDTH + +static void +inspect_disk(struct disk *dp) +{ + struct ptable *table; + + table = ptable_open(dp, dp->mediasize / dp->sectorsize, + dp->sectorsize, diskread); + if (table == NULL) { + printf("ptable_open failed\n"); + return; + } + printf("Partition table detected: %s\n", ptable_type2str(table)); + ptable_iterate(table, dp, ptable_print); + ptable_close(table); +} + +int +main(int argc, char **argv) +{ + struct stat sb; + struct disk d; + + if (argc < 2) + errx(1, "Usage: %s | " + "", argv[0]); + d.name = argv[1]; + if (stat(d.name, &sb) == 0 && S_ISREG(sb.st_mode)) { + d.fd = open(d.name, O_RDONLY); + if (d.fd < 0) + err(1, "open %s", d.name); + d.mediasize = sb.st_size; + d.sectorsize = 512; + d.file = 1; + } else { + d.fd = g_open(d.name, 0); + if (d.fd < 0) + err(1, "g_open %s", d.name); + d.mediasize = g_mediasize(d.fd); + d.sectorsize = g_sectorsize(d.fd); + d.file = 0; + } + d.offset = 0; + printf("%s \"%s\" opened\n", d.file ? "Disk image": "GEOM provider", + d.name); + printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n", + d.mediasize, d.mediasize / d.sectorsize, d.sectorsize); + + inspect_disk(&d); + + if (d.file) + close(d.fd); + else + g_close(d.fd); + return (0); +} Added: user/ae/bootcode/tools/tools/bootparttest/malloc.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/ae/bootcode/tools/tools/bootparttest/malloc.c Fri Jul 13 09:43:45 2012 (r238420) @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2012 Andrey V. Elsukov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +void* +Malloc(size_t size, const char *file, int line) +{ + return (malloc(size)); +} + +void +Free(void *ptr, const char *file, int line) +{ + return (free(ptr)); +}