1 /*-
2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include <err.h>
31 #include <fcntl.h>
32 #include <libgeom.h>
33 #include <disk.h>
34 #include <part.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 static int disk_strategy(void *devdata, int rw, daddr_t blk,
40 size_t size, char *buf, size_t *rsize);
41
42 /* stub struct devsw */
43 struct devsw {
44 const char dv_name[8];
45 int dv_type;
46 void *dv_init;
47 int (*dv_strategy)(void *devdata, int rw, daddr_t blk,
48 size_t size, char *buf, size_t *rsize);
49 void *dv_open;
50 void *dv_close;
51 void *dv_ioctl;
52 void *dv_print;
53 void *dv_cleanup;
54 } udisk = {
55 .dv_name = "disk",
56 .dv_strategy = disk_strategy
57 };
58
59 struct disk {
60 uint64_t mediasize;
61 uint16_t sectorsize;
62
63 int fd;
64 int file;
65 } disk;
66
67 static int
disk_strategy(void * devdata,int rw,daddr_t blk,size_t size,char * buf,size_t * rsize)68 disk_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
69 size_t *rsize)
70 {
71 struct disk_devdesc *dev = devdata;
72 int ret;
73
74 if (rw != 1 /* F_READ */)
75 return (-1);
76 if (rsize)
77 *rsize = 0;
78 printf("read %zu bytes from the block %lld [+%lld]\n", size,
79 (long long)blk, (long long)dev->d_offset);
80 ret = pread(disk.fd, buf, size,
81 (blk + dev->d_offset) * disk.sectorsize);
82 if (ret != size)
83 return (-1);
84 return (0);
85 }
86
87 int
main(int argc,char ** argv)88 main(int argc, char **argv)
89 {
90 struct disk_devdesc dev;
91 struct stat sb;
92 const char *p;
93
94 if (argc < 2)
95 errx(1, "Usage: %s <GEOM provider name> | "
96 "<disk image file name>", argv[0]);
97 memset(&disk, 0, sizeof(disk));
98 memset(&dev, 0, sizeof(dev));
99 dev.d_dev = &udisk;
100 dev.d_slice = -1;
101 dev.d_partition = -1;
102 if (stat(argv[1], &sb) == 0 && S_ISREG(sb.st_mode)) {
103 disk.fd = open(argv[1], O_RDONLY);
104 if (disk.fd < 0)
105 err(1, "open %s", argv[1]);
106 disk.mediasize = sb.st_size;
107 disk.sectorsize = 512;
108 disk.file = 1;
109 } else {
110 disk.fd = g_open(argv[1], 0);
111 if (disk.fd < 0)
112 err(1, "g_open %s", argv[1]);
113 disk.mediasize = g_mediasize(disk.fd);
114 disk.sectorsize = g_sectorsize(disk.fd);
115 p = strpbrk(argv[1], "0123456789");
116 if (p != NULL)
117 disk_parsedev(&dev, p, NULL);
118 }
119 printf("%s \"%s\" opened\n", disk.file ? "Disk image": "GEOM provider",
120 argv[1]);
121 printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n",
122 disk.mediasize, disk.mediasize / disk.sectorsize, disk.sectorsize);
123
124 if (disk_open(&dev, disk.mediasize, disk.sectorsize) != 0)
125 errx(1, "disk_open failed");
126 printf("\tdisk0:\n");
127 disk_print(&dev, "\tdisk0", 1);
128 disk_close(&dev);
129
130 if (disk.file)
131 close(disk.fd);
132 else
133 g_close(disk.fd);
134 return (0);
135 }
136