2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
42 static int disk_strategy(void *devdata, int rw, daddr_t blk,
43 size_t size, char *buf, size_t *rsize);
45 /* stub struct devsw */
47 const char dv_name[8];
50 int (*dv_strategy)(void *devdata, int rw, daddr_t blk,
51 size_t size, char *buf, size_t *rsize);
59 .dv_strategy = disk_strategy
71 disk_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
74 struct disk_devdesc *dev = devdata;
77 if (rw != 1 /* F_READ */)
81 printf("read %zu bytes from the block %lld [+%lld]\n", size,
82 (long long)blk, (long long)dev->d_offset);
83 ret = pread(disk.fd, buf, size,
84 (blk + dev->d_offset) * disk.sectorsize);
91 main(int argc, char **argv)
93 struct disk_devdesc dev;
98 errx(1, "Usage: %s <GEOM provider name> | "
99 "<disk image file name>", argv[0]);
100 memset(&disk, 0, sizeof(disk));
101 memset(&dev, 0, sizeof(dev));
104 dev.d_partition = -1;
105 if (stat(argv[1], &sb) == 0 && S_ISREG(sb.st_mode)) {
106 disk.fd = open(argv[1], O_RDONLY);
108 err(1, "open %s", argv[1]);
109 disk.mediasize = sb.st_size;
110 disk.sectorsize = 512;
113 disk.fd = g_open(argv[1], 0);
115 err(1, "g_open %s", argv[1]);
116 disk.mediasize = g_mediasize(disk.fd);
117 disk.sectorsize = g_sectorsize(disk.fd);
118 p = strpbrk(argv[1], "0123456789");
120 disk_parsedev(&dev, p, NULL);
122 printf("%s \"%s\" opened\n", disk.file ? "Disk image": "GEOM provider",
124 printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n",
125 disk.mediasize, disk.mediasize / disk.sectorsize, disk.sectorsize);
127 if (disk_open(&dev, disk.mediasize, disk.sectorsize, 0) != 0)
128 errx(1, "disk_open failed");
129 printf("\tdisk0:\n");
130 disk_print(&dev, "\tdisk0", 1);