1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/disklabel.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/sbuf.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <geom/geom.h>
47 #include <geom/part/g_part.h>
48
49 #include "g_part_if.h"
50
51 #define BOOT1_SIZE 512
52 #define LABEL_SIZE 512
53 #define BOOT2_OFF (BOOT1_SIZE + LABEL_SIZE)
54 #define BOOT2_SIZE (BBSIZE - BOOT2_OFF)
55
56 FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
57
58 struct g_part_bsd_table {
59 struct g_part_table base;
60 u_char *bbarea;
61 uint32_t offset;
62 };
63
64 struct g_part_bsd_entry {
65 struct g_part_entry base;
66 struct partition part;
67 };
68
69 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
70 struct g_part_parms *);
71 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
72 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
73 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
74 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
75 struct sbuf *, const char *);
76 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
77 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
78 struct g_part_parms *);
79 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
80 char *, size_t);
81 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
82 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
83 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
84 char *, size_t);
85 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
86 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
87 struct g_part_parms *);
88
89 static kobj_method_t g_part_bsd_methods[] = {
90 KOBJMETHOD(g_part_add, g_part_bsd_add),
91 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode),
92 KOBJMETHOD(g_part_create, g_part_bsd_create),
93 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy),
94 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf),
95 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto),
96 KOBJMETHOD(g_part_modify, g_part_bsd_modify),
97 KOBJMETHOD(g_part_resize, g_part_bsd_resize),
98 KOBJMETHOD(g_part_name, g_part_bsd_name),
99 KOBJMETHOD(g_part_probe, g_part_bsd_probe),
100 KOBJMETHOD(g_part_read, g_part_bsd_read),
101 KOBJMETHOD(g_part_type, g_part_bsd_type),
102 KOBJMETHOD(g_part_write, g_part_bsd_write),
103 { 0, 0 }
104 };
105
106 static struct g_part_scheme g_part_bsd_scheme = {
107 "BSD",
108 g_part_bsd_methods,
109 sizeof(struct g_part_bsd_table),
110 .gps_entrysz = sizeof(struct g_part_bsd_entry),
111 .gps_minent = 8,
112 .gps_maxent = 20, /* Only 22 entries fit in 512 byte sectors */
113 .gps_bootcodesz = BBSIZE,
114 };
115 G_PART_SCHEME_DECLARE(g_part_bsd);
116 MODULE_VERSION(geom_part_bsd, 0);
117
118 static struct g_part_bsd_alias {
119 uint8_t type;
120 int alias;
121 } bsd_alias_match[] = {
122 { FS_BSDFFS, G_PART_ALIAS_FREEBSD_UFS },
123 { FS_SWAP, G_PART_ALIAS_FREEBSD_SWAP },
124 { FS_ZFS, G_PART_ALIAS_FREEBSD_ZFS },
125 { FS_VINUM, G_PART_ALIAS_FREEBSD_VINUM },
126 { FS_NANDFS, G_PART_ALIAS_FREEBSD_NANDFS },
127 { FS_HAMMER, G_PART_ALIAS_DFBSD_HAMMER },
128 { FS_HAMMER2, G_PART_ALIAS_DFBSD_HAMMER2 },
129 };
130
131 static int
132 bsd_parse_type(const char *type, uint8_t *fstype)
133 {
134 const char *alias;
135 char *endp;
136 long lt;
137 int i;
138
139 if (type[0] == '!') {
140 lt = strtol(type + 1, &endp, 0);
141 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
142 return (EINVAL);
143 *fstype = (u_int)lt;
144 return (0);
145 }
146 for (i = 0; i < nitems(bsd_alias_match); i++) {
147 alias = g_part_alias_name(bsd_alias_match[i].alias);
148 if (strcasecmp(type, alias) == 0) {
149 *fstype = bsd_alias_match[i].type;
150 return (0);
151 }
152 }
153 return (EINVAL);
154 }
155
156 static int
157 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
158 struct g_part_parms *gpp)
159 {
160 struct g_part_bsd_entry *entry;
161 struct g_part_bsd_table *table;
162
163 if (gpp->gpp_parms & G_PART_PARM_LABEL)
164 return (EINVAL);
165
166 entry = (struct g_part_bsd_entry *)baseentry;
167 table = (struct g_part_bsd_table *)basetable;
168
169 entry->part.p_size = gpp->gpp_size;
170 entry->part.p_offset = gpp->gpp_start + table->offset;
171 entry->part.p_fsize = 0;
172 entry->part.p_frag = 0;
173 entry->part.p_cpg = 0;
174 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
175 }
176
177 static int
178 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
179 {
180 struct g_part_bsd_table *table;
181 const u_char *codeptr;
182
183 if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
184 return (ENODEV);
185
186 table = (struct g_part_bsd_table *)basetable;
187 codeptr = gpp->gpp_codeptr;
188 bcopy(codeptr, table->bbarea, BOOT1_SIZE);
189 if (gpp->gpp_codesize == BBSIZE)
190 bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
191 BOOT2_SIZE);
192 return (0);
193 }
194
195 static int
196 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
197 {
198 struct g_provider *pp;
199 struct g_part_entry *baseentry;
200 struct g_part_bsd_entry *entry;
201 struct g_part_bsd_table *table;
202 u_char *ptr;
203 uint32_t msize, ncyls, secpercyl;
204
205 pp = gpp->gpp_provider;
206
207 if (pp->sectorsize < sizeof(struct disklabel))
208 return (ENOSPC);
209 if (BBSIZE % pp->sectorsize)
210 return (ENOTBLK);
211
212 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
213 secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
214 ncyls = msize / secpercyl;
215
216 table = (struct g_part_bsd_table *)basetable;
217 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
218 ptr = table->bbarea + pp->sectorsize;
219
220 le32enc(ptr + 0, DISKMAGIC); /* d_magic */
221 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */
222 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */
223 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */
224 le32enc(ptr + 52, ncyls); /* d_ncylinders */
225 le32enc(ptr + 56, secpercyl); /* d_secpercyl */
226 le32enc(ptr + 60, msize); /* d_secperunit */
227 le16enc(ptr + 72, 3600); /* d_rpm */
228 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */
229 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */
230 le32enc(ptr + 140, BBSIZE); /* d_bbsize */
231
232 basetable->gpt_first = 0;
233 basetable->gpt_last = msize - 1;
234 basetable->gpt_isleaf = 1;
235
236 baseentry = g_part_new_entry(basetable, RAW_PART + 1,
237 basetable->gpt_first, basetable->gpt_last);
238 baseentry->gpe_internal = 1;
239 entry = (struct g_part_bsd_entry *)baseentry;
240 entry->part.p_size = basetable->gpt_last + 1;
241 entry->part.p_offset = table->offset;
242
243 return (0);
244 }
245
246 static int
247 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
248 {
249 struct g_part_bsd_table *table;
250
251 table = (struct g_part_bsd_table *)basetable;
252 g_free(table->bbarea);
253 table->bbarea = NULL;
254
255 /* Wipe the second sector to clear the partitioning. */
256 basetable->gpt_smhead |= 2;
257 return (0);
258 }
259
260 static void
261 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
262 struct sbuf *sb, const char *indent)
263 {
264 struct g_part_bsd_entry *entry;
265
266 entry = (struct g_part_bsd_entry *)baseentry;
267 if (indent == NULL) {
268 /* conftxt: libdisk compatibility */
269 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
270 } else if (entry != NULL) {
271 /* confxml: partition entry information */
272 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
273 entry->part.p_fstype);
274 } else {
275 /* confxml: scheme information */
276 }
277 }
278
279 static int
280 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
281 {
282 struct g_part_bsd_entry *entry;
283
284 /* Allow dumping to a swap partition or an unused partition. */
285 entry = (struct g_part_bsd_entry *)baseentry;
286 return ((entry->part.p_fstype == FS_UNUSED ||
287 entry->part.p_fstype == FS_SWAP) ? 1 : 0);
288 }
289
290 static int
291 g_part_bsd_modify(struct g_part_table *basetable,
292 struct g_part_entry *baseentry, struct g_part_parms *gpp)
293 {
294 struct g_part_bsd_entry *entry;
295
296 if (gpp->gpp_parms & G_PART_PARM_LABEL)
297 return (EINVAL);
298
299 entry = (struct g_part_bsd_entry *)baseentry;
300 if (gpp->gpp_parms & G_PART_PARM_TYPE)
301 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
302 return (0);
303 }
304
305 static void
306 bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
307 {
308 struct g_part_bsd_table *table;
309 struct g_part_bsd_entry *entry;
310 struct g_part_entry *baseentry;
311 uint32_t msize;
312
313 table = (struct g_part_bsd_table *)basetable;
314 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
315 le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
316 basetable->gpt_last = msize - 1;
317 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
318 if (baseentry->gpe_index != RAW_PART + 1)
319 continue;
320 baseentry->gpe_end = basetable->gpt_last;
321 entry = (struct g_part_bsd_entry *)baseentry;
322 entry->part.p_size = msize;
323 return;
324 }
325 }
326
327 static int
328 g_part_bsd_resize(struct g_part_table *basetable,
329 struct g_part_entry *baseentry, struct g_part_parms *gpp)
330 {
331 struct g_part_bsd_entry *entry;
332 struct g_provider *pp;
333
334 if (baseentry == NULL) {
335 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
336 bsd_set_rawsize(basetable, pp);
337 return (0);
338 }
339 entry = (struct g_part_bsd_entry *)baseentry;
340 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
341 entry->part.p_size = gpp->gpp_size;
342
343 return (0);
344 }
345
346 static const char *
347 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
348 char *buf, size_t bufsz)
349 {
350
351 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
352 return (buf);
353 }
354
355 static int
356 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
357 {
358 struct g_provider *pp;
359 u_char *buf;
360 uint32_t magic1, magic2;
361 int error;
362
363 pp = cp->provider;
364
365 /* Sanity-check the provider. */
366 if (pp->sectorsize < sizeof(struct disklabel) ||
367 pp->mediasize < BBSIZE)
368 return (ENOSPC);
369 if (BBSIZE % pp->sectorsize)
370 return (ENOTBLK);
371
372 /* Check that there's a disklabel. */
373 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
374 if (buf == NULL)
375 return (error);
376 magic1 = le32dec(buf + 0);
377 magic2 = le32dec(buf + 132);
378 g_free(buf);
379 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
380 ? G_PART_PROBE_PRI_HIGH : ENXIO);
381 }
382
383 static int
384 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
385 {
386 struct g_provider *pp;
387 struct g_part_bsd_table *table;
388 struct g_part_entry *baseentry;
389 struct g_part_bsd_entry *entry;
390 struct partition part;
391 u_char *buf, *p;
392 off_t chs, msize;
393 u_int sectors, heads;
394 int error, index;
395
396 pp = cp->provider;
397 table = (struct g_part_bsd_table *)basetable;
398 msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
399
400 table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
401 if (table->bbarea == NULL)
402 return (error);
403
404 buf = table->bbarea + pp->sectorsize;
405
406 if (le32dec(buf + 40) != pp->sectorsize)
407 goto invalid_label;
408 sectors = le32dec(buf + 44);
409 if (sectors < 1 || sectors > 255)
410 goto invalid_label;
411 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
412 g_part_geometry_heads(msize, sectors, &chs, &heads);
413 if (chs != 0) {
414 basetable->gpt_sectors = sectors;
415 basetable->gpt_heads = heads;
416 }
417 }
418 heads = le32dec(buf + 48);
419 if (heads < 1 || heads > 255)
420 goto invalid_label;
421 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
422 basetable->gpt_heads = heads;
423
424 chs = le32dec(buf + 60);
425 if (chs < 1)
426 goto invalid_label;
427 /* Fix-up a sysinstall bug. */
428 if (chs > msize) {
429 chs = msize;
430 le32enc(buf + 60, msize);
431 }
432
433 basetable->gpt_first = 0;
434 basetable->gpt_last = msize - 1;
435 basetable->gpt_isleaf = 1;
436
437 basetable->gpt_entries = le16dec(buf + 138);
438 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
439 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
440 goto invalid_label;
441
442 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
443 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
444 p = buf + 148 + index * 16;
445 part.p_size = le32dec(p + 0);
446 part.p_offset = le32dec(p + 4);
447 part.p_fsize = le32dec(p + 8);
448 part.p_fstype = p[12];
449 part.p_frag = p[13];
450 part.p_cpg = le16dec(p + 14);
451 if (part.p_size == 0)
452 continue;
453 if (part.p_offset < table->offset)
454 continue;
455 if (part.p_offset - table->offset > basetable->gpt_last)
456 goto invalid_label;
457 baseentry = g_part_new_entry(basetable, index + 1,
458 part.p_offset - table->offset,
459 part.p_offset - table->offset + part.p_size - 1);
460 entry = (struct g_part_bsd_entry *)baseentry;
461 entry->part = part;
462 if (index == RAW_PART)
463 baseentry->gpe_internal = 1;
464 }
465
466 return (0);
467
468 invalid_label:
469 printf("GEOM: %s: invalid disklabel.\n", pp->name);
470 g_free(table->bbarea);
471 table->bbarea = NULL;
472 return (EINVAL);
473 }
474
475 static const char *
476 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
477 char *buf, size_t bufsz)
478 {
479 struct g_part_bsd_entry *entry;
480 int type;
481
482 entry = (struct g_part_bsd_entry *)baseentry;
483 type = entry->part.p_fstype;
484 if (type == FS_NANDFS)
485 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
486 if (type == FS_SWAP)
487 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
488 if (type == FS_BSDFFS)
489 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
490 if (type == FS_VINUM)
491 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
492 if (type == FS_ZFS)
493 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
494 snprintf(buf, bufsz, "!%d", type);
495 return (buf);
496 }
497
498 static int
499 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
500 {
501 struct g_provider *pp;
502 struct g_part_entry *baseentry;
503 struct g_part_bsd_entry *entry;
504 struct g_part_bsd_table *table;
505 uint16_t sum;
506 u_char *label, *p, *pe;
507 int error, index;
508
509 pp = cp->provider;
510 table = (struct g_part_bsd_table *)basetable;
511 baseentry = LIST_FIRST(&basetable->gpt_entry);
512 label = table->bbarea + pp->sectorsize;
513 for (index = 1; index <= basetable->gpt_entries; index++) {
514 p = label + 148 + (index - 1) * 16;
515 entry = (baseentry != NULL && index == baseentry->gpe_index)
516 ? (struct g_part_bsd_entry *)baseentry : NULL;
517 if (entry != NULL && !baseentry->gpe_deleted) {
518 le32enc(p + 0, entry->part.p_size);
519 le32enc(p + 4, entry->part.p_offset);
520 le32enc(p + 8, entry->part.p_fsize);
521 p[12] = entry->part.p_fstype;
522 p[13] = entry->part.p_frag;
523 le16enc(p + 14, entry->part.p_cpg);
524 } else
525 bzero(p, 16);
526
527 if (entry != NULL)
528 baseentry = LIST_NEXT(baseentry, gpe_entry);
529 }
530
531 /* Calculate checksum. */
532 le16enc(label + 136, 0);
533 pe = label + 148 + basetable->gpt_entries * 16;
534 sum = 0;
535 for (p = label; p < pe; p += 2)
536 sum ^= le16dec(p);
537 le16enc(label + 136, sum);
538
539 error = g_write_data(cp, 0, table->bbarea, BBSIZE);
540 return (error);
541 }
Cache object: 745c33d6958c98f7fd89b366a9c497fe
|