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