]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/bootparttest/bootparttest.c
Update ACPICA to 20181003.
[FreeBSD/FreeBSD.git] / tools / tools / bootparttest / bootparttest.c
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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <libgeom.h>
36 #include <disk.h>
37 #include <part.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 static int disk_strategy(void *devdata, int rw, daddr_t blk,
43     size_t size, char *buf, size_t *rsize);
44
45 /* stub struct devsw */
46 struct devsw {
47         const char      dv_name[8];
48         int             dv_type;
49         void            *dv_init;
50         int             (*dv_strategy)(void *devdata, int rw, daddr_t blk,
51                             size_t size, char *buf, size_t *rsize);
52         void            *dv_open;
53         void            *dv_close;
54         void            *dv_ioctl;
55         void            *dv_print;
56         void            *dv_cleanup;
57 } udisk = {
58         .dv_name = "disk",
59         .dv_strategy = disk_strategy
60 };
61
62 struct disk {
63         uint64_t        mediasize;
64         uint16_t        sectorsize;
65
66         int             fd;
67         int             file;
68 } disk;
69
70 static int
71 disk_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
72     size_t *rsize)
73 {
74         struct disk_devdesc *dev = devdata;
75         int ret;
76
77         if (rw != 1 /* F_READ */)
78                 return (-1);
79         if (rsize)
80                 *rsize = 0;
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);
85         if (ret != size)
86                 return (-1);
87         return (0);
88 }
89
90 int
91 main(int argc, char **argv)
92 {
93         struct disk_devdesc dev;
94         struct stat sb;
95         const char *p;
96
97         if (argc < 2)
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));
102         dev.d_dev = &udisk;
103         dev.d_slice = -1;
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);
107                 if (disk.fd < 0)
108                         err(1, "open %s", argv[1]);
109                 disk.mediasize = sb.st_size;
110                 disk.sectorsize = 512;
111                 disk.file = 1;
112         } else {
113                 disk.fd = g_open(argv[1], 0);
114                 if (disk.fd < 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");
119                 if (p != NULL)
120                         disk_parsedev(&dev, p, NULL);
121         }
122         printf("%s \"%s\" opened\n", disk.file ? "Disk image": "GEOM provider",
123             argv[1]);
124         printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n",
125             disk.mediasize, disk.mediasize / disk.sectorsize, disk.sectorsize);
126
127         if (disk_open(&dev, disk.mediasize, disk.sectorsize) != 0)
128                 errx(1, "disk_open failed");
129         printf("\tdisk0:\n");
130         disk_print(&dev, "\tdisk0", 1);
131         disk_close(&dev);
132
133         if (disk.file)
134                 close(disk.fd);
135         else
136                 g_close(disk.fd);
137         return (0);
138 }