]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/geom/part/g_part_mbr.c
MFC r230064:
[FreeBSD/stable/8.git] / sys / geom / part / g_part_mbr.c
1 /*-
2  * Copyright (c) 2007, 2008 Marcel Moolenaar
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  *
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 ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <geom/geom.h>
44 #include <geom/part/g_part.h>
45
46 #include "g_part_if.h"
47
48 #define MBRSIZE         512
49
50 struct g_part_mbr_table {
51         struct g_part_table     base;
52         u_char          mbr[MBRSIZE];
53 };
54
55 struct g_part_mbr_entry {
56         struct g_part_entry     base;
57         struct dos_partition ent;
58 };
59
60 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
61     struct g_part_parms *);
62 static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
63 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
64 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
65 static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
66     struct sbuf *, const char *);
67 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
68 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,  
69     struct g_part_parms *);
70 static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
71     char *, size_t);
72 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
73 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
74 static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
75     const char *, unsigned int);
76 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
77     char *, size_t);
78 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
79 static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
80     struct g_part_parms *);
81
82 static kobj_method_t g_part_mbr_methods[] = {
83         KOBJMETHOD(g_part_add,          g_part_mbr_add),
84         KOBJMETHOD(g_part_bootcode,     g_part_mbr_bootcode),
85         KOBJMETHOD(g_part_create,       g_part_mbr_create),
86         KOBJMETHOD(g_part_destroy,      g_part_mbr_destroy),
87         KOBJMETHOD(g_part_dumpconf,     g_part_mbr_dumpconf),
88         KOBJMETHOD(g_part_dumpto,       g_part_mbr_dumpto),
89         KOBJMETHOD(g_part_modify,       g_part_mbr_modify),
90         KOBJMETHOD(g_part_resize,       g_part_mbr_resize),
91         KOBJMETHOD(g_part_name,         g_part_mbr_name),
92         KOBJMETHOD(g_part_probe,        g_part_mbr_probe),
93         KOBJMETHOD(g_part_read,         g_part_mbr_read),
94         KOBJMETHOD(g_part_setunset,     g_part_mbr_setunset),
95         KOBJMETHOD(g_part_type,         g_part_mbr_type),
96         KOBJMETHOD(g_part_write,        g_part_mbr_write),
97         { 0, 0 }
98 };
99
100 static struct g_part_scheme g_part_mbr_scheme = {
101         "MBR",
102         g_part_mbr_methods,
103         sizeof(struct g_part_mbr_table),
104         .gps_entrysz = sizeof(struct g_part_mbr_entry),
105         .gps_minent = NDOSPART,
106         .gps_maxent = NDOSPART,
107         .gps_bootcodesz = MBRSIZE,
108 };
109 G_PART_SCHEME_DECLARE(g_part_mbr);
110
111 static struct g_part_mbr_alias {
112         u_char          typ;
113         int             alias;
114 } mbr_alias_match[] = {
115         { DOSPTYP_386BSD,       G_PART_ALIAS_FREEBSD },
116         { DOSPTYP_EXT,          G_PART_ALIAS_EBR },
117         { DOSPTYP_NTFS,         G_PART_ALIAS_MS_NTFS },
118         { DOSPTYP_FAT32,        G_PART_ALIAS_MS_FAT32 },
119         { DOSPTYP_LINSWP,       G_PART_ALIAS_LINUX_SWAP },
120         { DOSPTYP_LINUX,        G_PART_ALIAS_LINUX_DATA },
121         { DOSPTYP_LINLVM,       G_PART_ALIAS_LINUX_LVM },
122         { DOSPTYP_LINRAID,      G_PART_ALIAS_LINUX_RAID },
123 };
124
125 static int
126 mbr_parse_type(const char *type, u_char *dp_typ)
127 {
128         const char *alias;
129         char *endp;
130         long lt;
131         int i;
132
133         if (type[0] == '!') {
134                 lt = strtol(type + 1, &endp, 0);
135                 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
136                         return (EINVAL);
137                 *dp_typ = (u_char)lt;
138                 return (0);
139         }
140         for (i = 0;
141             i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) {
142                 alias = g_part_alias_name(mbr_alias_match[i].alias);
143                 if (strcasecmp(type, alias) == 0) {
144                         *dp_typ = mbr_alias_match[i].typ;
145                         return (0);
146                 }
147         }
148         return (EINVAL);
149 }
150
151 static int
152 mbr_probe_bpb(u_char *bpb)
153 {
154         uint16_t secsz;
155         uint8_t clstsz;
156
157 #define PO2(x)  ((x & (x - 1)) == 0)
158         secsz = le16dec(bpb);
159         if (secsz < 512 || secsz > 4096 || !PO2(secsz))
160                 return (0);
161         clstsz = bpb[2];
162         if (clstsz < 1 || clstsz > 128 || !PO2(clstsz))
163                 return (0);
164 #undef PO2
165
166         return (1);
167 }
168
169 static void
170 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
171     u_char *secp)
172 {
173         uint32_t cyl, hd, sec;
174
175         sec = lba % table->gpt_sectors + 1;
176         lba /= table->gpt_sectors;
177         hd = lba % table->gpt_heads;
178         lba /= table->gpt_heads;
179         cyl = lba;
180         if (cyl > 1023)
181                 sec = hd = cyl = ~0;
182
183         *cylp = cyl & 0xff;
184         *hdp = hd & 0xff;
185         *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
186 }
187
188 static int
189 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
190     struct g_part_parms *gpp)
191 {
192         struct g_part_mbr_entry *entry;
193         struct g_part_mbr_table *table;
194         uint32_t start, size, sectors;
195
196         if (gpp->gpp_parms & G_PART_PARM_LABEL)
197                 return (EINVAL);
198
199         sectors = basetable->gpt_sectors;
200
201         entry = (struct g_part_mbr_entry *)baseentry;
202         table = (struct g_part_mbr_table *)basetable;
203
204         start = gpp->gpp_start;
205         size = gpp->gpp_size;
206         if (size < sectors)
207                 return (EINVAL);
208         if (start % sectors) {
209                 size = size - sectors + (start % sectors);
210                 start = start - (start % sectors) + sectors;
211         }
212         if (size % sectors)
213                 size = size - (size % sectors);
214         if (size < sectors)
215                 return (EINVAL);
216
217         if (baseentry->gpe_deleted)
218                 bzero(&entry->ent, sizeof(entry->ent));
219
220         KASSERT(baseentry->gpe_start <= start, (__func__));
221         KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
222         baseentry->gpe_start = start;
223         baseentry->gpe_end = start + size - 1;
224         entry->ent.dp_start = start;
225         entry->ent.dp_size = size;
226         mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
227             &entry->ent.dp_shd, &entry->ent.dp_ssect);
228         mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
229             &entry->ent.dp_ehd, &entry->ent.dp_esect);
230         return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
231 }
232
233 static int
234 g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
235 {
236         struct g_part_mbr_table *table;
237         uint32_t dsn;
238
239         if (gpp->gpp_codesize != MBRSIZE)
240                 return (ENODEV);
241
242         table = (struct g_part_mbr_table *)basetable;
243         dsn = *(uint32_t *)(table->mbr + DOSDSNOFF);
244         bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF);
245         if (dsn != 0)
246                 *(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
247         return (0);
248 }
249
250 static int
251 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
252 {
253         struct g_provider *pp;
254         struct g_part_mbr_table *table;
255
256         pp = gpp->gpp_provider;
257         if (pp->sectorsize < MBRSIZE)
258                 return (ENOSPC);
259
260         basetable->gpt_first = basetable->gpt_sectors;
261         basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
262             UINT32_MAX) - 1;
263
264         table = (struct g_part_mbr_table *)basetable;
265         le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
266         return (0);
267 }
268
269 static int
270 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
271 {
272
273         /* Wipe the first sector to clear the partitioning. */
274         basetable->gpt_smhead |= 1;
275         return (0);
276 }
277
278 static void
279 g_part_mbr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 
280     struct sbuf *sb, const char *indent)
281 {
282         struct g_part_mbr_entry *entry;
283  
284         entry = (struct g_part_mbr_entry *)baseentry;
285         if (indent == NULL) {
286                 /* conftxt: libdisk compatibility */
287                 sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
288         } else if (entry != NULL) {
289                 /* confxml: partition entry information */
290                 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
291                     entry->ent.dp_typ);
292                 if (entry->ent.dp_flag & 0x80)
293                         sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
294         } else {
295                 /* confxml: scheme information */
296         }
297 }
298
299 static int
300 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)  
301 {
302         struct g_part_mbr_entry *entry;
303
304         /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
305         entry = (struct g_part_mbr_entry *)baseentry;
306         return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
307             entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
308 }
309
310 static int
311 g_part_mbr_modify(struct g_part_table *basetable,
312     struct g_part_entry *baseentry, struct g_part_parms *gpp)
313 {
314         struct g_part_mbr_entry *entry;
315
316         if (gpp->gpp_parms & G_PART_PARM_LABEL)
317                 return (EINVAL);
318
319         entry = (struct g_part_mbr_entry *)baseentry;
320         if (gpp->gpp_parms & G_PART_PARM_TYPE)
321                 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
322         return (0);
323 }
324
325 static int
326 g_part_mbr_resize(struct g_part_table *basetable,
327     struct g_part_entry *baseentry, struct g_part_parms *gpp)
328 {
329         struct g_part_mbr_entry *entry;
330         uint32_t size, sectors;
331
332         sectors = basetable->gpt_sectors;
333         size = gpp->gpp_size;
334
335         if (size < sectors)
336                 return (EINVAL);
337         if (size % sectors)
338                 size = size - (size % sectors);
339         if (size < sectors)
340                 return (EINVAL);
341
342         entry = (struct g_part_mbr_entry *)baseentry;
343         baseentry->gpe_end = baseentry->gpe_start + size - 1;
344         entry->ent.dp_size = size;
345         mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
346             &entry->ent.dp_ehd, &entry->ent.dp_esect);
347         return (0);
348 }
349
350 static const char *
351 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
352     char *buf, size_t bufsz)
353 {
354
355         snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
356         return (buf);
357 }
358
359 static int
360 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
361 {
362         char psn[8];
363         struct g_provider *pp;
364         u_char *buf, *p;
365         int error, index, res, sum;
366         uint16_t magic;
367
368         pp = cp->provider;
369
370         /* Sanity-check the provider. */
371         if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
372                 return (ENOSPC);
373         if (pp->sectorsize > 4096)
374                 return (ENXIO);
375
376         /* We don't nest under an MBR (see EBR instead). */
377         error = g_getattr("PART::scheme", cp, &psn);
378         if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
379                 return (ELOOP);
380
381         /* Check that there's a MBR. */
382         buf = g_read_data(cp, 0L, pp->sectorsize, &error);
383         if (buf == NULL)
384                 return (error);
385
386         /* We goto out on mismatch. */
387         res = ENXIO;
388
389         magic = le16dec(buf + DOSMAGICOFFSET);
390         if (magic != DOSMAGIC)
391                 goto out;
392
393         for (index = 0; index < NDOSPART; index++) {
394                 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
395                 if (p[0] != 0 && p[0] != 0x80)
396                         goto out;
397         }
398
399         /*
400          * If the partition table does not consist of all zeroes,
401          * assume we have a MBR. If it's all zeroes, we could have
402          * a boot sector. For example, a boot sector that doesn't
403          * have boot code -- common on non-i386 hardware. In that
404          * case we check if we have a possible BPB. If so, then we
405          * assume we have a boot sector instead.
406          */
407         sum = 0;
408         for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
409                 sum += buf[DOSPARTOFF + index];
410         if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
411                 res = G_PART_PROBE_PRI_NORM;
412
413  out:
414         g_free(buf);
415         return (res);
416 }
417
418 static int
419 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
420 {
421         struct dos_partition ent;
422         struct g_provider *pp;
423         struct g_part_mbr_table *table;
424         struct g_part_mbr_entry *entry;
425         u_char *buf, *p;
426         off_t chs, msize, first;
427         u_int sectors, heads;
428         int error, index;
429
430         pp = cp->provider;
431         table = (struct g_part_mbr_table *)basetable;
432         first = basetable->gpt_sectors;
433         msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
434
435         buf = g_read_data(cp, 0L, pp->sectorsize, &error);
436         if (buf == NULL)
437                 return (error);
438
439         bcopy(buf, table->mbr, sizeof(table->mbr));
440         for (index = NDOSPART - 1; index >= 0; index--) {
441                 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
442                 ent.dp_flag = p[0];
443                 ent.dp_shd = p[1];
444                 ent.dp_ssect = p[2];
445                 ent.dp_scyl = p[3];
446                 ent.dp_typ = p[4];
447                 ent.dp_ehd = p[5];
448                 ent.dp_esect = p[6];
449                 ent.dp_ecyl = p[7];
450                 ent.dp_start = le32dec(p + 8);
451                 ent.dp_size = le32dec(p + 12);
452                 if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
453                         continue;
454                 if (ent.dp_start == 0 || ent.dp_size == 0)
455                         continue;
456                 sectors = ent.dp_esect & 0x3f;
457                 if (sectors > basetable->gpt_sectors &&
458                     !basetable->gpt_fixgeom) {
459                         g_part_geometry_heads(msize, sectors, &chs, &heads);
460                         if (chs != 0) {
461                                 basetable->gpt_sectors = sectors;
462                                 basetable->gpt_heads = heads;
463                         }
464                 }
465                 if (ent.dp_start < first)
466                         first = ent.dp_start;
467                 entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
468                     index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
469                 entry->ent = ent;
470         }
471
472         basetable->gpt_entries = NDOSPART;
473         basetable->gpt_first = basetable->gpt_sectors;
474         basetable->gpt_last = msize - 1;
475
476         if (first < basetable->gpt_first)
477                 basetable->gpt_first = 1;
478
479         g_free(buf);
480         return (0);
481 }
482
483 static int
484 g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
485     const char *attrib, unsigned int set)
486 {
487         struct g_part_entry *iter;
488         struct g_part_mbr_entry *entry;
489         int changed;
490
491         if (strcasecmp(attrib, "active") != 0)
492                 return (EINVAL);
493
494         /* Only one entry can have the active attribute. */
495         LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
496                 if (iter->gpe_deleted)
497                         continue;
498                 changed = 0;
499                 entry = (struct g_part_mbr_entry *)iter;
500                 if (iter == baseentry) {
501                         if (set && (entry->ent.dp_flag & 0x80) == 0) {
502                                 entry->ent.dp_flag |= 0x80;
503                                 changed = 1;
504                         } else if (!set && (entry->ent.dp_flag & 0x80)) {
505                                 entry->ent.dp_flag &= ~0x80;
506                                 changed = 1;
507                         }
508                 } else {
509                         if (set && (entry->ent.dp_flag & 0x80)) {
510                                 entry->ent.dp_flag &= ~0x80;
511                                 changed = 1;
512                         }
513                 }
514                 if (changed && !iter->gpe_created)
515                         iter->gpe_modified = 1;
516         }
517         return (0);
518 }
519
520 static const char *
521 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 
522     char *buf, size_t bufsz)
523 {
524         struct g_part_mbr_entry *entry;
525         int i;
526
527         entry = (struct g_part_mbr_entry *)baseentry;
528         for (i = 0;
529             i < sizeof(mbr_alias_match) / sizeof(mbr_alias_match[0]); i++) {
530                 if (mbr_alias_match[i].typ == entry->ent.dp_typ)
531                         return (g_part_alias_name(mbr_alias_match[i].alias));
532         }
533         snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
534         return (buf);
535 }
536
537 static int
538 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
539 {
540         struct g_part_entry *baseentry;
541         struct g_part_mbr_entry *entry;
542         struct g_part_mbr_table *table;
543         u_char *p;
544         int error, index;
545
546         table = (struct g_part_mbr_table *)basetable;
547         baseentry = LIST_FIRST(&basetable->gpt_entry);
548         for (index = 1; index <= basetable->gpt_entries; index++) {
549                 p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
550                 entry = (baseentry != NULL && index == baseentry->gpe_index)
551                     ? (struct g_part_mbr_entry *)baseentry : NULL;
552                 if (entry != NULL && !baseentry->gpe_deleted) {
553                         p[0] = entry->ent.dp_flag;
554                         p[1] = entry->ent.dp_shd;
555                         p[2] = entry->ent.dp_ssect;
556                         p[3] = entry->ent.dp_scyl;
557                         p[4] = entry->ent.dp_typ;
558                         p[5] = entry->ent.dp_ehd;
559                         p[6] = entry->ent.dp_esect;
560                         p[7] = entry->ent.dp_ecyl;
561                         le32enc(p + 8, entry->ent.dp_start);
562                         le32enc(p + 12, entry->ent.dp_size);
563                 } else
564                         bzero(p, DOSPARTSIZE);
565
566                 if (entry != NULL)
567                         baseentry = LIST_NEXT(baseentry, gpe_entry);
568         }
569
570         error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);
571         return (error);
572 }