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