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