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