]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/disk.c
Upgrade Unbound to 1.7.1.
[FreeBSD/FreeBSD.git] / stand / common / disk.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
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 *
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, "%ld%cB", (long)size, unit);
79         return (buf);
80 }
81
82 int
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 #define PWIDTH  35
106 static int
107 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
108 {
109         struct disk_devdesc dev;
110         struct print_args *pa, bsd;
111         struct open_disk *od;
112         struct ptable *table;
113         char line[80];
114         int res;
115
116         pa = (struct print_args *)arg;
117         od = (struct open_disk *)pa->dev->dd.d_opendata;
118         sprintf(line, "  %s%s: %s", pa->prefix, pname,
119             parttype2str(part->type));
120         if (pa->verbose)
121                 sprintf(line, "%-*s%s", PWIDTH, line,
122                     display_size(part->end - part->start + 1,
123                     od->sectorsize));
124         strcat(line, "\n");
125         if (pager_output(line))
126                 return 1;
127         res = 0;
128         if (part->type == PART_FREEBSD) {
129                 /* Open slice with BSD label */
130                 dev.dd.d_dev = pa->dev->dd.d_dev;
131                 dev.dd.d_unit = pa->dev->dd.d_unit;
132                 dev.d_slice = part->index;
133                 dev.d_partition = -1;
134                 if (disk_open(&dev, part->end - part->start + 1,
135                     od->sectorsize) == 0) {
136                         table = ptable_open(&dev, part->end - part->start + 1,
137                             od->sectorsize, ptblread);
138                         if (table != NULL) {
139                                 sprintf(line, "  %s%s", pa->prefix, pname);
140                                 bsd.dev = pa->dev;
141                                 bsd.prefix = line;
142                                 bsd.verbose = pa->verbose;
143                                 res = ptable_iterate(table, &bsd, ptable_print);
144                                 ptable_close(table);
145                         }
146                         disk_close(&dev);
147                 }
148         }
149
150         return (res);
151 }
152 #undef PWIDTH
153
154 int
155 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
156 {
157         struct open_disk *od;
158         struct print_args pa;
159
160         /* Disk should be opened */
161         od = (struct open_disk *)dev->dd.d_opendata;
162         pa.dev = dev;
163         pa.prefix = prefix;
164         pa.verbose = verbose;
165         return (ptable_iterate(od->table, &pa, ptable_print));
166 }
167
168 int
169 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
170 {
171         struct open_disk *od;
172         int ret;
173
174         od = (struct open_disk *)dev->dd.d_opendata;
175         ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
176             blocks * od->sectorsize, buf, NULL);
177
178         return (ret);
179 }
180
181 int
182 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
183 {
184         struct open_disk *od;
185         int ret;
186
187         od = (struct open_disk *)dev->dd.d_opendata;
188         ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
189             blocks * od->sectorsize, buf, NULL);
190
191         return (ret);
192 }
193
194 int
195 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
196 {
197         struct open_disk *od = dev->dd.d_opendata;
198
199         if (od == NULL)
200                 return (ENOTTY);
201
202         switch (cmd) {
203         case DIOCGSECTORSIZE:
204                 *(u_int *)data = od->sectorsize;
205                 break;
206         case DIOCGMEDIASIZE:
207                 if (dev->d_offset == 0)
208                         *(uint64_t *)data = od->mediasize;
209                 else
210                         *(uint64_t *)data = od->entrysize * od->sectorsize;
211                 break;
212         default:
213                 return (ENOTTY);
214         }
215
216         return (0);
217 }
218
219 int
220 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
221 {
222         struct open_disk *od;
223         struct ptable *table;
224         struct ptable_entry part;
225         int rc, slice, partition;
226
227         rc = 0;
228         /*
229          * While we are reading disk metadata, make sure we do it relative
230          * to the start of the disk
231          */
232         dev->d_offset = 0;
233         table = NULL;
234         slice = dev->d_slice;
235         partition = dev->d_partition;
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         DEBUG("%s unit %d, slice %d, partition %d => %p",
246             disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
247
248         /* Determine disk layout. */
249         od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
250             ptblread);
251         if (od->table == NULL) {
252                 DEBUG("Can't read partition table");
253                 rc = ENXIO;
254                 goto out;
255         }
256
257         if (ptable_getsize(od->table, &mediasize) != 0) {
258                 rc = ENXIO;
259                 goto out;
260         }
261         if (mediasize > od->mediasize) {
262                 od->mediasize = mediasize;
263         }
264
265         if (ptable_gettype(od->table) == PTABLE_BSD &&
266             partition >= 0) {
267                 /* It doesn't matter what value has d_slice */
268                 rc = ptable_getpart(od->table, &part, partition);
269                 if (rc == 0) {
270                         dev->d_offset = part.start;
271                         od->entrysize = part.end - part.start + 1;
272                 }
273         } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
274                 dev->d_offset = 0;
275                 od->entrysize = mediasize;
276         } else if (slice >= 0) {
277                 /* Try to get information about partition */
278                 if (slice == 0)
279                         rc = ptable_getbestpart(od->table, &part);
280                 else
281                         rc = ptable_getpart(od->table, &part, slice);
282                 if (rc != 0) /* Partition doesn't exist */
283                         goto out;
284                 dev->d_offset = part.start;
285                 od->entrysize = part.end - part.start + 1;
286                 slice = part.index;
287                 if (ptable_gettype(od->table) == PTABLE_GPT) {
288                         partition = 255;
289                         goto out; /* Nothing more to do */
290                 } else if (partition == 255) {
291                         /*
292                          * When we try to open GPT partition, but partition
293                          * table isn't GPT, reset d_partition value to -1
294                          * and try to autodetect appropriate value.
295                          */
296                         partition = -1;
297                 }
298                 /*
299                  * If d_partition < 0 and we are looking at a BSD slice,
300                  * then try to read BSD label, otherwise return the
301                  * whole MBR slice.
302                  */
303                 if (partition == -1 &&
304                     part.type != PART_FREEBSD)
305                         goto out;
306                 /* Try to read BSD label */
307                 table = ptable_open(dev, part.end - part.start + 1,
308                     od->sectorsize, ptblread);
309                 if (table == NULL) {
310                         DEBUG("Can't read BSD label");
311                         rc = ENXIO;
312                         goto out;
313                 }
314                 /*
315                  * If slice contains BSD label and d_partition < 0, then
316                  * assume the 'a' partition. Otherwise just return the
317                  * whole MBR slice, because it can contain ZFS.
318                  */
319                 if (partition < 0) {
320                         if (ptable_gettype(table) != PTABLE_BSD)
321                                 goto out;
322                         partition = 0;
323                 }
324                 rc = ptable_getpart(table, &part, partition);
325                 if (rc != 0)
326                         goto out;
327                 dev->d_offset += part.start;
328                 od->entrysize = part.end - part.start + 1;
329         }
330 out:
331         if (table != NULL)
332                 ptable_close(table);
333
334         if (rc != 0) {
335                 if (od->table != NULL)
336                         ptable_close(od->table);
337                 free(od);
338                 DEBUG("%s could not open", disk_fmtdev(dev));
339         } else {
340                 /* Save the slice and partition number to the dev */
341                 dev->d_slice = slice;
342                 dev->d_partition = partition;
343                 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
344                     (long long)dev->d_offset, od);
345         }
346         return (rc);
347 }
348
349 int
350 disk_close(struct disk_devdesc *dev)
351 {
352         struct open_disk *od;
353
354         od = (struct open_disk *)dev->dd.d_opendata;
355         DEBUG("%s closed => %p", disk_fmtdev(dev), od);
356         ptable_close(od->table);
357         free(od);
358         return (0);
359 }
360
361 char*
362 disk_fmtdev(struct disk_devdesc *dev)
363 {
364         static char buf[128];
365         char *cp;
366
367         cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
368         if (dev->d_slice >= 0) {
369 #ifdef LOADER_GPT_SUPPORT
370                 if (dev->d_partition == 255) {
371                         sprintf(cp, "p%d:", dev->d_slice);
372                         return (buf);
373                 } else
374 #endif
375 #ifdef LOADER_MBR_SUPPORT
376                         cp += sprintf(cp, "s%d", dev->d_slice);
377 #endif
378         }
379         if (dev->d_partition >= 0)
380                 cp += sprintf(cp, "%c", dev->d_partition + 'a');
381         strcat(cp, ":");
382         return (buf);
383 }
384
385 int
386 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
387 {
388         int unit, slice, partition;
389         const char *np;
390         char *cp;
391
392         np = devspec;
393         unit = slice = partition = -1;
394         if (*np != '\0' && *np != ':') {
395                 unit = strtol(np, &cp, 10);
396                 if (cp == np)
397                         return (EUNIT);
398 #ifdef LOADER_GPT_SUPPORT
399                 if (*cp == 'p') {
400                         np = cp + 1;
401                         slice = strtol(np, &cp, 10);
402                         if (np == cp)
403                                 return (ESLICE);
404                         /* we don't support nested partitions on GPT */
405                         if (*cp != '\0' && *cp != ':')
406                                 return (EINVAL);
407                         partition = 255;
408                 } else
409 #endif
410 #ifdef LOADER_MBR_SUPPORT
411                 if (*cp == 's') {
412                         np = cp + 1;
413                         slice = strtol(np, &cp, 10);
414                         if (np == cp)
415                                 return (ESLICE);
416                 }
417 #endif
418                 if (*cp != '\0' && *cp != ':') {
419                         partition = *cp - 'a';
420                         if (partition < 0)
421                                 return (EPART);
422                         cp++;
423                 }
424         } else
425                 return (EINVAL);
426
427         if (*cp != '\0' && *cp != ':')
428                 return (EINVAL);
429         dev->dd.d_unit = unit;
430         dev->d_slice = slice;
431         dev->d_partition = partition;
432         if (path != NULL)
433                 *path = (*cp == '\0') ? cp: cp + 1;
434         return (0);
435 }