1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* $OpenBSD: disk.c,v 1.68 2021/07/19 14:30:08 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h> /* DEV_BSIZE */
#include <sys/ioctl.h>
#include <sys/dkio.h>
#include <sys/stat.h>
#include <sys/disklabel.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "part.h"
#include "disk.h"
#include "misc.h"
struct disk disk;
struct disklabel dl;
void
DISK_open(const char *name, const int oflags)
{
struct stat st;
uint64_t ns, bs, sz, spc;
disk.dk_name = strdup(name);
if (disk.dk_name == NULL)
err(1, "DISK_Open('%s')", name);
disk.dk_fd = opendev(disk.dk_name, oflags, OPENDEV_PART, NULL);
if (disk.dk_fd == -1)
err(1, "%s", disk.dk_name);
if (fstat(disk.dk_fd, &st) == -1)
err(1, "%s", disk.dk_name);
if (!S_ISCHR(st.st_mode))
errx(1, "%s is not a character device", disk.dk_name);
if (ioctl(disk.dk_fd, DIOCGPDINFO, &dl) == -1)
err(1, "DIOCGPDINFO");
unit_types[SECTORS].ut_conversion = dl.d_secsize;
/* Set geometry to use in MBR partitions. */
if (disk.dk_size > 0) {
/* -l has set disk size. */
sz = disk.dk_size;
disk.dk_heads = 1;
disk.dk_sectors = 64;
disk.dk_size = DL_BLKTOSEC(&dl, sz);
disk.dk_cylinders = disk.dk_size / disk.dk_sectors;
} else if (disk.dk_cylinders > 0) {
/* -c/-h/-s has set disk geometry & therefore size. */
sz = disk.dk_cylinders * disk.dk_heads * disk.dk_sectors;
disk.dk_size = DL_BLKTOSEC(&dl, sz);
disk.dk_sectors = DL_BLKTOSEC(&dl, disk.dk_sectors);
} else {
disk.dk_sectors = dl.d_nsectors;
disk.dk_cylinders = dl.d_ncylinders;
disk.dk_heads = dl.d_ntracks;
disk.dk_sectors = dl.d_nsectors;
/* MBR handles only first UINT32_MAX sectors. */
spc = (uint64_t)disk.dk_heads * disk.dk_sectors;
sz = DL_GETDSIZE(&dl);
if (sz > UINT32_MAX) {
disk.dk_cylinders = UINT32_MAX / spc;
disk.dk_size = disk.dk_cylinders * spc;
} else
disk.dk_size = sz;
}
if (disk.dk_size == 0)
errx(1, "disk size is 0");
if (disk.dk_bootprt.prt_ns > 0) {
ns = disk.dk_bootprt.prt_ns + DL_BLKSPERSEC(&dl) - 1;
bs = disk.dk_bootprt.prt_bs + DL_BLKSPERSEC(&dl) - 1;
disk.dk_bootprt.prt_ns = DL_BLKTOSEC(&dl, ns);
disk.dk_bootprt.prt_bs = DL_BLKTOSEC(&dl, bs);
}
}
void
DISK_printgeometry(const char *units)
{
const int secsize = unit_types[SECTORS].ut_conversion;
double size;
int i;
i = unit_lookup(units);
size = ((double)disk.dk_size * secsize) / unit_types[i].ut_conversion;
printf("Disk: %s\t", disk.dk_name);
if (disk.dk_size) {
printf("geometry: %d/%d/%d [%.0f ", disk.dk_cylinders,
disk.dk_heads, disk.dk_sectors, size);
if (i == SECTORS && secsize != sizeof(struct dos_mbr))
printf("%d-byte ", secsize);
printf("%s]\n", unit_types[i].ut_lname);
} else
printf("geometry: <none>\n");
}
/*
* The caller must free() the returned memory!
*/
char *
DISK_readsector(const uint64_t sector)
{
char *secbuf;
ssize_t len;
off_t off, where;
int secsize;
secsize = dl.d_secsize;
where = sector * secsize;
off = lseek(disk.dk_fd, where, SEEK_SET);
if (off != where)
return NULL;
secbuf = calloc(1, secsize);
if (secbuf == NULL)
return NULL;
len = read(disk.dk_fd, secbuf, secsize);
if (len == -1 || len != secsize) {
free(secbuf);
return NULL;
}
return secbuf;
}
int
DISK_writesector(const char *secbuf, const uint64_t sector)
{
int secsize;
ssize_t len;
off_t off, where;
len = -1;
secsize = dl.d_secsize;
where = secsize * sector;
off = lseek(disk.dk_fd, where, SEEK_SET);
if (off == where)
len = write(disk.dk_fd, secbuf, secsize);
if (len == -1 || len != secsize) {
errno = EIO;
return -1;
}
return 0;
}
|