1 /*-
2 * Copyright (c) 1998 Michael Smith <[email protected]>
3 * Copyright (c) 2012 Andrey V. Elsukov <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/disk.h>
32 #include <sys/queue.h>
33 #include <stand.h>
34 #include <stdarg.h>
35 #include <bootstrap.h>
36 #include <part.h>
37
38 #include "disk.h"
39
40 #ifdef DISK_DEBUG
41 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
42 #else
43 # define DEBUG(fmt, args...)
44 #endif
45
46 struct open_disk {
47 struct ptable *table;
48 uint64_t mediasize;
49 uint64_t entrysize;
50 u_int sectorsize;
51 };
52
53 struct print_args {
54 struct disk_devdesc *dev;
55 const char *prefix;
56 int verbose;
57 };
58
59 /* Convert size to a human-readable number. */
60 static char *
display_size(uint64_t size,u_int sectorsize)61 display_size(uint64_t size, u_int sectorsize)
62 {
63 static char buf[80];
64 char unit;
65
66 size = size * sectorsize / 1024;
67 unit = 'K';
68 if (size >= 10485760000LL) {
69 size /= 1073741824;
70 unit = 'T';
71 } else if (size >= 10240000) {
72 size /= 1048576;
73 unit = 'G';
74 } else if (size >= 10000) {
75 size /= 1024;
76 unit = 'M';
77 }
78 sprintf(buf, "%4ld%cB", (long)size, unit);
79 return (buf);
80 }
81
82 int
ptblread(void * d,void * buf,size_t blocks,uint64_t offset)83 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84 {
85 struct disk_devdesc *dev;
86 struct open_disk *od;
87
88 dev = (struct disk_devdesc *)d;
89 od = (struct open_disk *)dev->dd.d_opendata;
90
91 /*
92 * The strategy function assumes the offset is in units of 512 byte
93 * sectors. For larger sector sizes, we need to adjust the offset to
94 * match the actual sector size.
95 */
96 offset *= (od->sectorsize / 512);
97 /*
98 * As the GPT backup partition is located at the end of the disk,
99 * to avoid reading past disk end, flag bcache not to use RA.
100 */
101 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
102 blocks * od->sectorsize, (char *)buf, NULL));
103 }
104
105 static int
ptable_print(void * arg,const char * pname,const struct ptable_entry * part)106 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
107 {
108 struct disk_devdesc dev;
109 struct print_args *pa, bsd;
110 struct open_disk *od;
111 struct ptable *table;
112 char line[80];
113 int res;
114 u_int sectsize;
115 uint64_t partsize;
116
117 pa = (struct print_args *)arg;
118 od = (struct open_disk *)pa->dev->dd.d_opendata;
119 sectsize = od->sectorsize;
120 partsize = part->end - part->start + 1;
121 sprintf(line, " %s%s: %s\t%s\n", pa->prefix, pname,
122 parttype2str(part->type),
123 pa->verbose ? display_size(partsize, sectsize) : "");
124 if (pager_output(line))
125 return 1;
126 res = 0;
127 if (part->type == PART_FREEBSD) {
128 /* Open slice with BSD label */
129 dev.dd.d_dev = pa->dev->dd.d_dev;
130 dev.dd.d_unit = pa->dev->dd.d_unit;
131 dev.d_slice = part->index;
132 dev.d_partition = -1;
133 if (disk_open(&dev, partsize, sectsize) == 0) {
134 /*
135 * disk_open() for partition -1 on a bsd slice assumes
136 * you want the first bsd partition. Reset things so
137 * that we're looking at the start of the raw slice.
138 */
139 dev.d_partition = -1;
140 dev.d_offset = part->start;
141 table = ptable_open(&dev, partsize, sectsize, ptblread);
142 if (table != NULL) {
143 sprintf(line, " %s%s", pa->prefix, pname);
144 bsd.dev = pa->dev;
145 bsd.prefix = line;
146 bsd.verbose = pa->verbose;
147 res = ptable_iterate(table, &bsd, ptable_print);
148 ptable_close(table);
149 }
150 disk_close(&dev);
151 }
152 }
153
154 return (res);
155 }
156
157 int
disk_print(struct disk_devdesc * dev,char * prefix,int verbose)158 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
159 {
160 struct open_disk *od;
161 struct print_args pa;
162
163 /* Disk should be opened */
164 od = (struct open_disk *)dev->dd.d_opendata;
165 pa.dev = dev;
166 pa.prefix = prefix;
167 pa.verbose = verbose;
168 return (ptable_iterate(od->table, &pa, ptable_print));
169 }
170
171 int
disk_read(struct disk_devdesc * dev,void * buf,uint64_t offset,u_int blocks)172 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
173 {
174 struct open_disk *od;
175 int ret;
176
177 od = (struct open_disk *)dev->dd.d_opendata;
178 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
179 blocks * od->sectorsize, buf, NULL);
180
181 return (ret);
182 }
183
184 int
disk_write(struct disk_devdesc * dev,void * buf,uint64_t offset,u_int blocks)185 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
186 {
187 struct open_disk *od;
188 int ret;
189
190 od = (struct open_disk *)dev->dd.d_opendata;
191 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
192 blocks * od->sectorsize, buf, NULL);
193
194 return (ret);
195 }
196
197 int
disk_ioctl(struct disk_devdesc * dev,u_long cmd,void * data)198 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
199 {
200 struct open_disk *od = dev->dd.d_opendata;
201
202 if (od == NULL)
203 return (ENOTTY);
204
205 switch (cmd) {
206 case DIOCGSECTORSIZE:
207 *(u_int *)data = od->sectorsize;
208 break;
209 case DIOCGMEDIASIZE:
210 if (dev->d_offset == 0)
211 *(uint64_t *)data = od->mediasize;
212 else
213 *(uint64_t *)data = od->entrysize * od->sectorsize;
214 break;
215 default:
216 return (ENOTTY);
217 }
218
219 return (0);
220 }
221
222 int
disk_open(struct disk_devdesc * dev,uint64_t mediasize,u_int sectorsize)223 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
224 {
225 struct disk_devdesc partdev;
226 struct open_disk *od;
227 struct ptable *table;
228 struct ptable_entry part;
229 int rc, slice, partition;
230
231 if (sectorsize == 0) {
232 DEBUG("unknown sector size");
233 return (ENXIO);
234 }
235 rc = 0;
236 od = (struct open_disk *)malloc(sizeof(struct open_disk));
237 if (od == NULL) {
238 DEBUG("no memory");
239 return (ENOMEM);
240 }
241 dev->dd.d_opendata = od;
242 od->entrysize = 0;
243 od->mediasize = mediasize;
244 od->sectorsize = sectorsize;
245 /*
246 * While we are reading disk metadata, make sure we do it relative
247 * to the start of the disk
248 */
249 memcpy(&partdev, dev, sizeof(partdev));
250 partdev.d_offset = 0;
251 partdev.d_slice = -1;
252 partdev.d_partition = -1;
253
254 dev->d_offset = 0;
255 table = NULL;
256 slice = dev->d_slice;
257 partition = dev->d_partition;
258
259 DEBUG("%s unit %d, slice %d, partition %d => %p",
260 disk_fmtdev(dev), dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
261
262 /* Determine disk layout. */
263 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
264 ptblread);
265 if (od->table == NULL) {
266 DEBUG("Can't read partition table");
267 rc = ENXIO;
268 goto out;
269 }
270
271 if (ptable_getsize(od->table, &mediasize) != 0) {
272 rc = ENXIO;
273 goto out;
274 }
275 od->mediasize = mediasize;
276
277 if (ptable_gettype(od->table) == PTABLE_BSD &&
278 partition >= 0) {
279 /* It doesn't matter what value has d_slice */
280 rc = ptable_getpart(od->table, &part, partition);
281 if (rc == 0) {
282 dev->d_offset = part.start;
283 od->entrysize = part.end - part.start + 1;
284 }
285 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
286 dev->d_offset = 0;
287 od->entrysize = mediasize;
288 } else if (slice >= 0) {
289 /* Try to get information about partition */
290 if (slice == 0)
291 rc = ptable_getbestpart(od->table, &part);
292 else
293 rc = ptable_getpart(od->table, &part, slice);
294 if (rc != 0) /* Partition doesn't exist */
295 goto out;
296 dev->d_offset = part.start;
297 od->entrysize = part.end - part.start + 1;
298 slice = part.index;
299 if (ptable_gettype(od->table) == PTABLE_GPT) {
300 partition = 255;
301 goto out; /* Nothing more to do */
302 } else if (partition == 255) {
303 /*
304 * When we try to open GPT partition, but partition
305 * table isn't GPT, reset d_partition value to -1
306 * and try to autodetect appropriate value.
307 */
308 partition = -1;
309 }
310 /*
311 * If d_partition < 0 and we are looking at a BSD slice,
312 * then try to read BSD label, otherwise return the
313 * whole MBR slice.
314 */
315 if (partition == -1 &&
316 part.type != PART_FREEBSD)
317 goto out;
318 /* Try to read BSD label */
319 table = ptable_open(dev, part.end - part.start + 1,
320 od->sectorsize, ptblread);
321 if (table == NULL) {
322 DEBUG("Can't read BSD label");
323 rc = ENXIO;
324 goto out;
325 }
326 /*
327 * If slice contains BSD label and d_partition < 0, then
328 * assume the 'a' partition. Otherwise just return the
329 * whole MBR slice, because it can contain ZFS.
330 */
331 if (partition < 0) {
332 if (ptable_gettype(table) != PTABLE_BSD)
333 goto out;
334 partition = 0;
335 }
336 rc = ptable_getpart(table, &part, partition);
337 if (rc != 0)
338 goto out;
339 dev->d_offset += part.start;
340 od->entrysize = part.end - part.start + 1;
341 }
342 out:
343 if (table != NULL)
344 ptable_close(table);
345
346 if (rc != 0) {
347 if (od->table != NULL)
348 ptable_close(od->table);
349 free(od);
350 DEBUG("%s could not open", disk_fmtdev(dev));
351 } else {
352 /* Save the slice and partition number to the dev */
353 dev->d_slice = slice;
354 dev->d_partition = partition;
355 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
356 (long long)dev->d_offset, od);
357 }
358 return (rc);
359 }
360
361 int
disk_close(struct disk_devdesc * dev)362 disk_close(struct disk_devdesc *dev)
363 {
364 struct open_disk *od;
365
366 od = (struct open_disk *)dev->dd.d_opendata;
367 DEBUG("%s closed => %p", disk_fmtdev(dev), od);
368 ptable_close(od->table);
369 free(od);
370 return (0);
371 }
372
373 char*
disk_fmtdev(struct disk_devdesc * dev)374 disk_fmtdev(struct disk_devdesc *dev)
375 {
376 static char buf[128];
377 char *cp;
378
379 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
380 if (dev->d_slice >= 0) {
381 #ifdef LOADER_GPT_SUPPORT
382 if (dev->d_partition == 255) {
383 sprintf(cp, "p%d:", dev->d_slice);
384 return (buf);
385 } else
386 #endif
387 #ifdef LOADER_MBR_SUPPORT
388 cp += sprintf(cp, "s%d", dev->d_slice);
389 #endif
390 }
391 if (dev->d_partition >= 0)
392 cp += sprintf(cp, "%c", dev->d_partition + 'a');
393 strcat(cp, ":");
394 return (buf);
395 }
396
397 int
disk_parsedev(struct disk_devdesc * dev,const char * devspec,const char ** path)398 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
399 {
400 int unit, slice, partition;
401 const char *np;
402 char *cp;
403
404 np = devspec;
405 unit = slice = partition = -1;
406 if (*np != '\0' && *np != ':') {
407 unit = strtol(np, &cp, 10);
408 if (cp == np)
409 return (EUNIT);
410 #ifdef LOADER_GPT_SUPPORT
411 if (*cp == 'p') {
412 np = cp + 1;
413 slice = strtol(np, &cp, 10);
414 if (np == cp)
415 return (ESLICE);
416 /* we don't support nested partitions on GPT */
417 if (*cp != '\0' && *cp != ':')
418 return (EINVAL);
419 partition = 255;
420 } else
421 #endif
422 #ifdef LOADER_MBR_SUPPORT
423 if (*cp == 's') {
424 np = cp + 1;
425 slice = strtol(np, &cp, 10);
426 if (np == cp)
427 return (ESLICE);
428 }
429 #endif
430 if (*cp != '\0' && *cp != ':') {
431 partition = *cp - 'a';
432 if (partition < 0)
433 return (EPART);
434 cp++;
435 }
436 } else
437 return (EINVAL);
438
439 if (*cp != '\0' && *cp != ':')
440 return (EINVAL);
441 dev->dd.d_unit = unit;
442 dev->d_slice = slice;
443 dev->d_partition = partition;
444 if (path != NULL)
445 *path = (*cp == '\0') ? cp: cp + 1;
446 return (0);
447 }
448