1 /*-
2 * Copyright (c) 2007 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_bsd.c 230569 2012-01-26 09:14:51Z ae $");
29
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/disklabel.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 BOOT1_SIZE 512
49 #define LABEL_SIZE 512
50 #define BOOT2_OFF (BOOT1_SIZE + LABEL_SIZE)
51 #define BOOT2_SIZE (BBSIZE - BOOT2_OFF)
52
53 struct g_part_bsd_table {
54 struct g_part_table base;
55 u_char *bbarea;
56 uint32_t offset;
57 };
58
59 struct g_part_bsd_entry {
60 struct g_part_entry base;
61 struct partition part;
62 };
63
64 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
65 struct g_part_parms *);
66 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
67 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
68 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
69 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
70 struct sbuf *, const char *);
71 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
72 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
73 struct g_part_parms *);
74 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
75 char *, size_t);
76 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
77 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
78 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
79 char *, size_t);
80 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
81 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
82 struct g_part_parms *);
83
84 static kobj_method_t g_part_bsd_methods[] = {
85 KOBJMETHOD(g_part_add, g_part_bsd_add),
86 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
87 KOBJMETHOD(g_part_create, g_part_bsd_create),
88 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
89 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
90 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
91 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
92 KOBJMETHOD(g_part_resize, g_part_bsd_resize),
93 KOBJMETHOD(g_part_name, g_part_bsd_name),
94 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
95 KOBJMETHOD(g_part_read, g_part_bsd_read),
96 KOBJMETHOD(g_part_type, g_part_bsd_type),
97 KOBJMETHOD(g_part_write, g_part_bsd_write),
98 { 0, 0 }
99 };
100
101 static struct g_part_scheme g_part_bsd_scheme = {
102 "BSD",
103 g_part_bsd_methods,
104 sizeof(struct g_part_bsd_table),
105 .gps_entrysz = sizeof(struct g_part_bsd_entry),
106 .gps_minent = 8,
107 .gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */
108 .gps_bootcodesz = BBSIZE,
109 };
110 G_PART_SCHEME_DECLARE(g_part_bsd);
111
112 static int
113 bsd_parse_type(const char *type, uint8_t *fstype)
114 {
115 const char *alias;
116 char *endp;
117 long lt;
118
119 if (type[0] == '!') {
120 lt = strtol(type + 1, &endp, 0);
121 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
122 return (EINVAL);
123 *fstype = (u_int)lt;
124 return (0);
125 }
126 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
127 if (!strcasecmp(type, alias)) {
128 *fstype = FS_SWAP;
129 return (0);
130 }
131 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
132 if (!strcasecmp(type, alias)) {
133 *fstype = FS_BSDFFS;
134 return (0);
135 }
136 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
137 if (!strcasecmp(type, alias)) {
138 *fstype = FS_VINUM;
139 return (0);
140 }
141 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
142 if (!strcasecmp(type, alias)) {
143 *fstype = FS_ZFS;
144 return (0);
145 }
146 return (EINVAL);
147 }
148
149 static int
150 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
151 struct g_part_parms *gpp)
152 {
153 struct g_part_bsd_entry *entry;
154 struct g_part_bsd_table *table;
155
156 if (gpp->gpp_parms & G_PART_PARM_LABEL)
157 return (EINVAL);
158
159 entry = (struct g_part_bsd_entry *)baseentry;
160 table = (struct g_part_bsd_table *)basetable;
161
162 entry->part.p_size = gpp->gpp_size;
163 entry->part.p_offset = gpp->gpp_start + table->offset;
164 entry->part.p_fsize = 0;
165 entry->part.p_frag = 0;
166 entry->part.p_cpg = 0;
167 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
168 }
169
170 static int
171 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
172 {
173 struct g_part_bsd_table *table;
174 const u_char *codeptr;
175
176 if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
177 return (ENODEV);
178
179 table = (struct g_part_bsd_table *)basetable;
180 codeptr = gpp->gpp_codeptr;
181 bcopy(codeptr, table->bbarea, BOOT1_SIZE);
182 if (gpp->gpp_codesize == BBSIZE)
183 bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
184 BOOT2_SIZE);
185 return (0);
186 }
187
188 static int
189 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
190 {
191 struct g_provider *pp;
192 struct g_part_entry *baseentry;
193 struct g_part_bsd_entry *entry;
194 struct g_part_bsd_table *table;
195 u_char *ptr;
196 uint32_t msize, ncyls, secpercyl;
197
198 pp = gpp->gpp_provider;
199
200 if (pp->sectorsize < sizeof(struct disklabel))
201 return (ENOSPC);
202 if (BBSIZE % pp->sectorsize)
203 return (ENOTBLK);
204
205 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
206 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
207 ncyls = msize / secpercyl;
208
209 table = (struct g_part_bsd_table *)basetable;
210 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
211 ptr = table->bbarea + pp->sectorsize;
212
213 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
214 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
215 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
216 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
217 le32enc(ptr + 52, ncyls); /* d_ncylinders */
218 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
219 le32enc(ptr + 60, msize); /* d_secperunit */
220 le16enc(ptr + 72, 3600); /* d_rpm */
221 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
222 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
223 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
224
225 basetable->gpt_first = 0;
226 basetable->gpt_last = msize - 1;
227 basetable->gpt_isleaf = 1;
228
229 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
230 basetable->gpt_first, basetable->gpt_last);
231 baseentry->gpe_internal = 1;
232 entry = (struct g_part_bsd_entry *)baseentry;
233 entry->part.p_size = basetable->gpt_last + 1;
234 entry->part.p_offset = table->offset;
235
236 return (0);
237 }
238
239 static int
240 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
241 {
242 struct g_part_bsd_table *table;
243
244 table = (struct g_part_bsd_table *)basetable;
245 if (table->bbarea != NULL)
246 g_free(table->bbarea);
247 table->bbarea = NULL;
248
249 /* Wipe the second sector to clear the partitioning. */
250 basetable->gpt_smhead |= 2;
251 return (0);
252 }
253
254 static void
255 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
256 struct sbuf *sb, const char *indent)
257 {
258 struct g_part_bsd_entry *entry;
259
260 entry = (struct g_part_bsd_entry *)baseentry;
261 if (indent == NULL) {
262 /* conftxt: libdisk compatibility */
263 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
264 } else if (entry != NULL) {
265 /* confxml: partition entry information */
266 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
267 entry->part.p_fstype);
268 } else {
269 /* confxml: scheme information */
270 }
271 }
272
273 static int
274 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
275 {
276 struct g_part_bsd_entry *entry;
277
278 /* Allow dumping to a swap partition or an unused partition. */
279 entry = (struct g_part_bsd_entry *)baseentry;
280 return ((entry->part.p_fstype == FS_UNUSED ||
281 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
282 }
283
284 static int
285 g_part_bsd_modify(struct g_part_table *basetable,
286 struct g_part_entry *baseentry, struct g_part_parms *gpp)
287 {
288 struct g_part_bsd_entry *entry;
289
290 if (gpp->gpp_parms & G_PART_PARM_LABEL)
291 return (EINVAL);
292
293 entry = (struct g_part_bsd_entry *)baseentry;
294 if (gpp->gpp_parms & G_PART_PARM_TYPE)
295 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
296 return (0);
297 }
298
299 static int
300 g_part_bsd_resize(struct g_part_table *basetable,
301 struct g_part_entry *baseentry, struct g_part_parms *gpp)
302 {
303 struct g_part_bsd_entry *entry;
304
305 entry = (struct g_part_bsd_entry *)baseentry;
306 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
307 entry->part.p_size = gpp->gpp_size;
308
309 return (0);
310 }
311
312 static const char *
313 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
314 char *buf, size_t bufsz)
315 {
316
317 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
318 return (buf);
319 }
320
321 static int
322 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
323 {
324 struct g_provider *pp;
325 u_char *buf;
326 uint32_t magic1, magic2;
327 int error;
328
329 pp = cp->provider;
330
331 /* Sanity-check the provider. */
332 if (pp->sectorsize < sizeof(struct disklabel) ||
333 pp->mediasize < BBSIZE)
334 return (ENOSPC);
335 if (BBSIZE % pp->sectorsize)
336 return (ENOTBLK);
337
338 /* Check that there's a disklabel. */
339 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
340 if (buf == NULL)
341 return (error);
342 magic1 = le32dec(buf + 0);
343 magic2 = le32dec(buf + 132);
344 g_free(buf);
345 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
346 ? G_PART_PROBE_PRI_HIGH : ENXIO);
347 }
348
349 static int
350 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
351 {
352 struct g_provider *pp;
353 struct g_part_bsd_table *table;
354 struct g_part_entry *baseentry;
355 struct g_part_bsd_entry *entry;
356 struct partition part;
357 u_char *buf, *p;
358 off_t chs, msize;
359 u_int sectors, heads;
360 int error, index;
361
362 pp = cp->provider;
363 table = (struct g_part_bsd_table *)basetable;
364 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
365
366 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
367 if (table->bbarea == NULL)
368 return (error);
369
370 buf = table->bbarea + pp->sectorsize;
371
372 if (le32dec(buf + 40) != pp->sectorsize)
373 goto invalid_label;
374 sectors = le32dec(buf + 44);
375 if (sectors < 1 || sectors > 255)
376 goto invalid_label;
377 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
378 g_part_geometry_heads(msize, sectors, &chs, &heads);
379 if (chs != 0) {
380 basetable->gpt_sectors = sectors;
381 basetable->gpt_heads = heads;
382 }
383 }
384 heads = le32dec(buf + 48);
385 if (heads < 1 || heads > 255)
386 goto invalid_label;
387 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
388 basetable->gpt_heads = heads;
389 if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
390 printf("GEOM: %s: geometry does not match label"
391 " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
392 basetable->gpt_heads, basetable->gpt_sectors);
393
394 chs = le32dec(buf + 60);
395 if (chs < 1)
396 goto invalid_label;
397 /* Fix-up a sysinstall bug. */
398 if (chs > msize) {
399 chs = msize;
400 le32enc(buf + 60, msize);
401 }
402 if (chs != msize)
403 printf("GEOM: %s: media size does not match label.\n",
404 pp->name);
405
406 basetable->gpt_first = 0;
407 basetable->gpt_last = msize - 1;
408 basetable->gpt_isleaf = 1;
409
410 basetable->gpt_entries = le16dec(buf + 138);
411 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
412 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
413 goto invalid_label;
414
415 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
416 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
417 p = buf + 148 + index * 16;
418 part.p_size = le32dec(p + 0);
419 part.p_offset = le32dec(p + 4);
420 part.p_fsize = le32dec(p + 8);
421 part.p_fstype = p[12];
422 part.p_frag = p[13];
423 part.p_cpg = le16dec(p + 14);
424 if (part.p_size == 0)
425 continue;
426 if (part.p_offset < table->offset)
427 continue;
428 if (part.p_offset - table->offset > basetable->gpt_last)
429 goto invalid_label;
430 baseentry = g_part_new_entry(basetable, index + 1,
431 part.p_offset - table->offset,
432 part.p_offset - table->offset + part.p_size - 1);
433 entry = (struct g_part_bsd_entry *)baseentry;
434 entry->part = part;
435 if (index == RAW_PART)
436 baseentry->gpe_internal = 1;
437 }
438
439 return (0);
440
441 invalid_label:
442 printf("GEOM: %s: invalid disklabel.\n", pp->name);
443 g_free(table->bbarea);
444 table->bbarea = NULL;
445 return (EINVAL);
446 }
447
448 static const char *
449 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
450 char *buf, size_t bufsz)
451 {
452 struct g_part_bsd_entry *entry;
453 int type;
454
455 entry = (struct g_part_bsd_entry *)baseentry;
456 type = entry->part.p_fstype;
457 if (type == FS_SWAP)
458 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
459 if (type == FS_BSDFFS)
460 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
461 if (type == FS_VINUM)
462 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
463 if (type == FS_ZFS)
464 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
465 snprintf(buf, bufsz, "!%d", type);
466 return (buf);
467 }
468
469 static int
470 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
471 {
472 struct g_provider *pp;
473 struct g_part_entry *baseentry;
474 struct g_part_bsd_entry *entry;
475 struct g_part_bsd_table *table;
476 uint16_t sum;
477 u_char *label, *p, *pe;
478 int error, index;
479
480 pp = cp->provider;
481 table = (struct g_part_bsd_table *)basetable;
482 baseentry = LIST_FIRST(&basetable->gpt_entry);
483 label = table->bbarea + pp->sectorsize;
484 for (index = 1; index <= basetable->gpt_entries; index++) {
485 p = label + 148 + (index - 1) * 16;
486 entry = (baseentry != NULL && index == baseentry->gpe_index)
487 ? (struct g_part_bsd_entry *)baseentry : NULL;
488 if (entry != NULL && !baseentry->gpe_deleted) {
489 le32enc(p + 0, entry->part.p_size);
490 le32enc(p + 4, entry->part.p_offset);
491 le32enc(p + 8, entry->part.p_fsize);
492 p[12] = entry->part.p_fstype;
493 p[13] = entry->part.p_frag;
494 le16enc(p + 14, entry->part.p_cpg);
495 } else
496 bzero(p, 16);
497
498 if (entry != NULL)
499 baseentry = LIST_NEXT(baseentry, gpe_entry);
500 }
501
502 /* Calculate checksum. */
503 le16enc(label + 136, 0);
504 pe = label + 148 + basetable->gpt_entries * 16;
505 sum = 0;
506 for (p = label; p < pe; p += 2)
507 sum ^= le16dec(p);
508 le16enc(label + 136, sum);
509
510 error = g_write_data(cp, 0, table->bbarea, BBSIZE);
511 return (error);
512 }
Cache object: a5db0a30c3223606685b2430f50d58f5
|