Debian Codegraph @

libfdisk/src/bsd.c

raw
1 /*
2 * Copyright (C) 2007-2013 Karel Zak <[email protected]>
3 *
4 * Based on the original code from fdisk
5 * written by Bernhard Fastenrath ([email protected])
6 * with code from the NetBSD disklabel command.
7 *
8 * Arnaldo Carvalho de Melo <[email protected]>, March 1999
9 * David Huggins-Daines <[email protected]>, January 2000
10 */
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <sys/param.h>
18
19 #include "blkdev.h"
20 #include "fdiskP.h"
21 #include "pt-mbr.h"
22 #include "pt-bsd.h"
23 #include "all-io.h"
24
25
26 /**
27 * SECTION: bsd
28 * @title: BSD
29 * @short_description: disk label specific functions
30 *
31 */
32
33 static const char * const bsd_dktypenames[] = {
34 "unknown",
35 "SMD",
36 "MSCP",
37 "old DEC",
38 "SCSI",
39 "ESDI",
40 "ST506",
41 "HP-IB",
42 "HP-FL",
43 "type 9",
44 "floppy",
45 NULL
46 };
47 #define BSD_DKMAXTYPES (ARRAY_SIZE(bsd_dktypenames) - 1)
48
49 static const struct fdisk_parttype bsd_fstypes[] = {
50 {BSD_FS_UNUSED, "unused"},
51 {BSD_FS_SWAP, "swap"},
52 {BSD_FS_V6, "Version 6"},
53 {BSD_FS_V7, "Version 7"},
54 {BSD_FS_SYSV, "System V"},
55 {BSD_FS_V71K, "4.1BSD"},
56 {BSD_FS_V8, "Eighth Edition"},
57 {BSD_FS_BSDFFS, "4.2BSD"},
58 #ifdef __alpha__
59 {BSD_FS_EXT2, "ext2"},
60 #else
61 {BSD_FS_MSDOS, "MS-DOS"},
62 #endif
63 {BSD_FS_BSDLFS, "4.4LFS"},
64 {BSD_FS_OTHER, "unknown"},
65 {BSD_FS_HPFS, "HPFS"},
66 {BSD_FS_ISO9660,"ISO-9660"},
67 {BSD_FS_BOOT, "boot"},
68 {BSD_FS_ADOS, "ADOS"},
69 {BSD_FS_HFS, "HFS"},
70 {BSD_FS_ADVFS, "AdvFS"},
71 { 0, NULL }
72 };
73 #define BSD_FSMAXTYPES (ARRAY_SIZE(bsd_fstypes)-1)
74
75 /*
76 * in-memory fdisk BSD stuff
77 */
78 struct fdisk_bsd_label {
79 struct fdisk_label head; /* generic part */
80
81 struct dos_partition *dos_part; /* parent */
82 struct bsd_disklabel bsd; /* on disk label */
83 #if defined (__alpha__)
84 /* We access this through a u_int64_t * when checksumming */
85 char bsdbuffer[BSD_BBSIZE] __attribute__((aligned(8)));
86 #else
87 char bsdbuffer[BSD_BBSIZE];
88 #endif
89 };
90
91 static int bsd_initlabel(struct fdisk_context *cxt);
92 static int bsd_readlabel(struct fdisk_context *cxt);
93 static void sync_disks(struct fdisk_context *cxt);
94
95 static inline struct fdisk_bsd_label *self_label(struct fdisk_context *cxt)
96 {
97 assert(cxt);
98 assert(cxt->label);
99 assert(fdisk_is_label(cxt, BSD));
100
101 return (struct fdisk_bsd_label *) cxt->label;
102 }
103
104 static inline struct bsd_disklabel *self_disklabel(struct fdisk_context *cxt)
105 {
106 assert(cxt);
107 assert(cxt->label);
108 assert(fdisk_is_label(cxt, BSD));
109
110 return &((struct fdisk_bsd_label *) cxt->label)->bsd;
111 }
112
113 static struct fdisk_parttype *bsd_partition_parttype(
114 struct fdisk_context *cxt,
115 struct bsd_partition *p)
116 {
117 struct fdisk_parttype *t
118 = fdisk_label_get_parttype_from_code(cxt->label, p->p_fstype);
119 return t ? : fdisk_new_unknown_parttype(p->p_fstype, NULL);
120 }
121
122
123 #if defined (__alpha__)
124 static void alpha_bootblock_checksum (char *boot)
125 {
126 uint64_t *dp = (uint64_t *) boot, sum = 0;
127 int i;
128
129 for (i = 0; i < 63; i++)
130 sum += dp[i];
131 dp[63] = sum;
132 }
133 #endif /* __alpha__ */
134
135 #define HIDDEN_MASK 0x10
136
137 static int is_bsd_partition_type(int type)
138 {
139 return (type == MBR_FREEBSD_PARTITION ||
140 type == (MBR_FREEBSD_PARTITION ^ HIDDEN_MASK) ||
141 type == MBR_NETBSD_PARTITION ||
142 type == (MBR_NETBSD_PARTITION ^ HIDDEN_MASK) ||
143 type == MBR_OPENBSD_PARTITION ||
144 type == (MBR_OPENBSD_PARTITION ^ HIDDEN_MASK));
145 }
146
147 /*
148 * look for DOS partition usable for nested BSD partition table
149 */
150 static int bsd_assign_dos_partition(struct fdisk_context *cxt)
151 {
152 struct fdisk_bsd_label *l = self_label(cxt);
153 size_t i;
154
155 for (i = 0; i < 4; i++) {
156 fdisk_sector_t ss;
157
158 l->dos_part = fdisk_dos_get_partition(cxt->parent, i);
159
160 if (!l->dos_part || !is_bsd_partition_type(l->dos_part->sys_ind))
161 continue;
162
163 ss = dos_partition_get_start(l->dos_part);
164 if (!ss) {
165 fdisk_warnx(cxt, _("Partition %zd: has invalid starting "
166 "sector 0."), i + 1);
167 return -1;
168 }
169
170 if (cxt->parent->dev_path) {
171 free(cxt->dev_path);
172 cxt->dev_path = fdisk_partname(
173 cxt->parent->dev_path, i + 1);
174 }
175
176 DBG(LABEL, ul_debug("partition %zu assigned to BSD", i + 1));
177 return 0;
178 }
179
180 fdisk_warnx(cxt, _("There is no *BSD partition on %s."),
181 cxt->parent->dev_path);
182 free(cxt->dev_path);
183 cxt->dev_path = NULL;
184 l->dos_part = NULL;
185 return 1;
186 }
187
188 static int bsd_probe_label(struct fdisk_context *cxt)
189 {
190 int rc = 0;
191
192 if (cxt->parent)
193 rc = bsd_assign_dos_partition(cxt); /* nested BSD partition table */
194 if (!rc)
195 rc = bsd_readlabel(cxt);
196 if (!rc)
197 return 1; /* found BSD */
198 return 0; /* not found */
199 }
200
201 static int set_parttype(
202 struct fdisk_context *cxt,
203 size_t partnum,
204 struct fdisk_parttype *t)
205 {
206 struct bsd_partition *p;
207 struct bsd_disklabel *d = self_disklabel(cxt);
208
209 if (partnum >= d->d_npartitions || !t || t->code > UINT8_MAX)
210 return -EINVAL;
211
212 p = &d->d_partitions[partnum];
213 if (t->code == p->p_fstype)
214 return 0;
215
216 p->p_fstype = t->code;
217 fdisk_label_set_changed(cxt->label, 1);
218 return 0;
219 }
220
221 static int bsd_add_partition(struct fdisk_context *cxt,
222 struct fdisk_partition *pa,
223 size_t *partno)
224 {
225 struct fdisk_bsd_label *l = self_label(cxt);
226 struct bsd_disklabel *d = self_disklabel(cxt);
227 size_t i;
228 unsigned int begin = 0, end;
229 int rc = 0;
230
231 rc = fdisk_partition_next_partno(pa, cxt, &i);
232 if (rc)
233 return rc;
234 if (i >= BSD_MAXPARTITIONS)
235 return -ERANGE;
236 if (l->dos_part) {
237 begin = dos_partition_get_start(l->dos_part);
238 end = begin + dos_partition_get_size(l->dos_part) - 1;
239 } else
240 end = d->d_secperunit - 1;
241
242 /*
243 * First sector
244 */
245 if (pa && pa->start_follow_default)
246 ;
247 else if (pa && fdisk_partition_has_start(pa)) {
248 if (pa->start < begin || pa->start > end)
249 return -ERANGE;
250 begin = pa->start;
251 } else {
252 struct fdisk_ask *ask = fdisk_new_ask();
253
254 if (!ask)
255 return -ENOMEM;
256 fdisk_ask_set_query(ask,
257 fdisk_use_cylinders(cxt) ?
258 _("First cylinder") : _("First sector"));
259 fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER);
260 fdisk_ask_number_set_low(ask, fdisk_cround(cxt, begin));
261 fdisk_ask_number_set_default(ask, fdisk_cround(cxt, begin));
262 fdisk_ask_number_set_high(ask, fdisk_cround(cxt, end));
263
264 rc = fdisk_do_ask(cxt, ask);
265 begin = fdisk_ask_number_get_result(ask);
266 fdisk_unref_ask(ask);
267 if (rc)
268 return rc;
269 if (fdisk_use_cylinders(cxt))
270 begin = (begin - 1) * d->d_secpercyl;
271 }
272
273 /*
274 * Last sector
275 */
276 if (pa && pa->end_follow_default)
277 ;
278 else if (pa && fdisk_partition_has_size(pa)) {
279 if (begin + pa->size > end)
280 return -ERANGE;
281 end = begin + pa->size - 1ULL;
282 } else {
283 /* ask user by dialog */
284 struct fdisk_ask *ask = fdisk_new_ask();
285
286 if (!ask)
287 return -ENOMEM;
288 fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET);
289
290 if (fdisk_use_cylinders(cxt)) {
291 fdisk_ask_set_query(ask, _("Last cylinder, +/-cylinders or +/-size{K,M,G,T,P}"));
292 fdisk_ask_number_set_unit(ask,
293 cxt->sector_size *
294 fdisk_get_units_per_sector(cxt));
295 } else {
296 fdisk_ask_set_query(ask, _("Last sector, +/-sectors or +/-size{K,M,G,T,P}"));
297 fdisk_ask_number_set_unit(ask,cxt->sector_size);
298 }
299
300 fdisk_ask_number_set_low(ask, fdisk_cround(cxt, begin));
301 fdisk_ask_number_set_default(ask, fdisk_cround(cxt, end));
302 fdisk_ask_number_set_high(ask, fdisk_cround(cxt, end));
303 fdisk_ask_number_set_base(ask, fdisk_cround(cxt, begin));
304 fdisk_ask_number_set_wrap_negative(ask, 1); /* wrap negative around high */
305
306 rc = fdisk_do_ask(cxt, ask);
307 end = fdisk_ask_number_get_result(ask);
308 fdisk_unref_ask(ask);
309 if (rc)
310 return rc;
311 if (fdisk_use_cylinders(cxt))
312 end = end * d->d_secpercyl - 1;
313 }
314
315 d->d_partitions[i].p_size = end - begin + 1;
316 d->d_partitions[i].p_offset = begin;
317 d->d_partitions[i].p_fstype = BSD_FS_UNUSED;
318
319 if (i >= d->d_npartitions)
320 d->d_npartitions = i + 1;
321 cxt->label->nparts_cur = d->d_npartitions;
322
323 if (pa && pa->type)
324 set_parttype(cxt, i, pa->type);
325
326 fdisk_label_set_changed(cxt->label, 1);
327 if (partno)
328 *partno = i;
329 return 0;
330 }
331
332 static int bsd_set_partition(struct fdisk_context *cxt, size_t n,
333 struct fdisk_partition *pa)
334 {
335 struct bsd_partition *p;
336 struct fdisk_bsd_label *l = self_label(cxt);
337 struct bsd_disklabel *d = self_disklabel(cxt);
338
339 if (n >= d->d_npartitions)
340 return -EINVAL;
341
342 p = &d->d_partitions[n];
343
344 /* we have to stay within parental DOS partition */
345 if (l->dos_part && (fdisk_partition_has_start(pa) ||
346 fdisk_partition_has_size(pa))) {
347
348 fdisk_sector_t dosbegin = dos_partition_get_start(l->dos_part);
349 fdisk_sector_t dosend = dosbegin + dos_partition_get_size(l->dos_part) - 1;
350 fdisk_sector_t begin = fdisk_partition_has_start(pa) ? pa->start : p->p_offset;
351 fdisk_sector_t end = begin + (fdisk_partition_has_size(pa) ? pa->size : p->p_size) - 1;
352
353 if (begin < dosbegin || begin > dosend)
354 return -ERANGE;
355 if (end < dosbegin || end > dosend)
356 return -ERANGE;
357 }
358
359 if (pa->type) {
360 int rc = set_parttype(cxt, n, pa->type);
361 if (rc)
362 return rc;
363 }
364
365 if (fdisk_partition_has_start(pa))
366 d->d_partitions[n].p_offset = pa->start;
367 if (fdisk_partition_has_size(pa))
368 d->d_partitions[n].p_size = pa->size;
369
370 fdisk_label_set_changed(cxt->label, 1);
371 return 0;
372 }
373
374
375 /* Returns 0 on success, < 0 on error. */
376 static int bsd_create_disklabel(struct fdisk_context *cxt)
377 {
378 int rc, yes = 0;
379 struct bsd_disklabel *d = self_disklabel(cxt);
380
381 fdisk_info(cxt, _("The device %s does not contain BSD disklabel."), cxt->dev_path);
382 rc = fdisk_ask_yesno(cxt,
383 _("Do you want to create a BSD disklabel?"),
384 &yes);
385 if (rc)
386 return rc;
387 if (!yes)
388 return 1;
389 if (cxt->parent) {
390 rc = bsd_assign_dos_partition(cxt);
391 if (rc == 1)
392 /* not found DOS partition usable for BSD label */
393 rc = -EINVAL;
394 }
395 if (rc)
396 return rc;
397
398 rc = bsd_initlabel(cxt);
399 if (!rc) {
400 cxt->label->nparts_cur = d->d_npartitions;
401 cxt->label->nparts_max = BSD_MAXPARTITIONS;
402 }
403
404 return rc;
405 }
406
407 static int bsd_delete_part(
408 struct fdisk_context *cxt,
409 size_t partnum)
410 {
411 struct bsd_disklabel *d = self_disklabel(cxt);
412
413 d->d_partitions[partnum].p_size = 0;
414 d->d_partitions[partnum].p_offset = 0;
415 d->d_partitions[partnum].p_fstype = BSD_FS_UNUSED;
416
417 if (d->d_npartitions == partnum + 1)
418 while (!d->d_partitions[d->d_npartitions - 1].p_size)
419 d->d_npartitions--;
420
421 cxt->label->nparts_cur = d->d_npartitions;
422 fdisk_label_set_changed(cxt->label, 1);
423 return 0;
424 }
425
426 static int bsd_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labelitem *item)
427 {
428 struct bsd_disklabel *d;
429 int rc = 0;
430
431 assert(cxt);
432 assert(cxt->label);
433 assert(fdisk_is_label(cxt, BSD));
434
435 d = self_disklabel(cxt);
436
437 switch (item->id) {
438 case BSD_LABELITEM_TYPE:
439 item->name = _("Type");
440 item->type = 's';
441 if ((unsigned) d->d_type < BSD_DKMAXTYPES) {
442 item->data.str = strdup(bsd_dktypenames[d->d_type]);
443 if (!item->data.str)
444 rc = -ENOMEM;
445 } else if (asprintf(&item->data.str, "%d", d->d_type) < 0)
446 rc = -ENOMEM;
447 break;
448 case BSD_LABELITEM_DISK:
449 item->name = _("Disk");
450 item->type = 's';
451 item->data.str = strndup(d->d_typename, sizeof(d->d_typename));
452 if (!item->data.str)
453 rc = -ENOMEM;
454 break;
455 case BSD_LABELITEM_PACKNAME:
456 item->name = _("Packname");
457 item->type = 's';
458 item->data.str = strndup(d->d_packname, sizeof(d->d_packname));
459 if (!item->data.str)
460 rc = -ENOMEM;
461 break;
462 case BSD_LABELITEM_FLAGS:
463 item->name = _("Flags");
464 item->type = 's';
465 item->data.str = strdup(
466 d->d_flags & BSD_D_REMOVABLE ? _(" removable") :
467 d->d_flags & BSD_D_ECC ? _(" ecc") :
468 d->d_flags & BSD_D_BADSECT ? _(" badsect") : "");
469 if (!item->data.str)
470 rc = -ENOMEM;
471 break;
472
473 /* On various machines the fields of *lp are short/int/long */
474 /* In order to avoid problems, we cast them all uint64. */
475 case BSD_LABELITEM_SECSIZE:
476 item->name = _("Bytes/Sector");
477 item->type = 'j';
478 item->data.num64 = (uint64_t) d->d_secsize;
479 break;
480 case BSD_LABELITEM_NTRACKS:
481 item->name = _("Tracks/Cylinder");
482 item->type = 'j';
483 item->data.num64 = (uint64_t) d->d_ntracks;
484 break;
485 case BSD_LABELITEM_SECPERCYL:
486 item->name = _("Sectors/Cylinder");
487 item->data.num64 = (uint64_t) d->d_secpercyl;
488 item->type = 'j';
489 break;
490 case BSD_LABELITEM_CYLINDERS:
491 item->name = _("Cylinders");
492 item->data.num64 = (uint64_t) d->d_ncylinders;
493 item->type = 'j';
494 break;
495 case BSD_LABELITEM_RPM:
496 item->name = _("Rpm");
497 item->data.num64 = (uint64_t) d->d_rpm;
498 item->type = 'j';
499 break;
500 case BSD_LABELITEM_INTERLEAVE:
501 item->name = _("Interleave");
502 item->data.num64 = (uint64_t) d->d_interleave;
503 item->type = 'j';
504 break;
505 case BSD_LABELITEM_TRACKSKEW:
506 item->name = _("Trackskew");
507 item->data.num64 = (uint64_t) d->d_trackskew;
508 item->type = 'j';
509 break;
510 case BSD_LABELITEM_CYLINDERSKEW:
511 item->name = _("Cylinderskew");
512 item->data.num64 = (uint64_t) d->d_cylskew;
513 item->type = 'j';
514 break;
515 case BSD_LABELITEM_HEADSWITCH:
516 item->name = _("Headswitch");
517 item->data.num64 = (uint64_t) d->d_headswitch;
518 item->type = 'j';
519 break;
520 case BSD_LABELITEM_TRKSEEK:
521 item->name = _("Track-to-track seek");
522 item->data.num64 = (uint64_t) d->d_trkseek;
523 item->type = 'j';
524 break;
525 default:
526 if (item->id < __FDISK_NLABELITEMS)
527 rc = 1; /* unsupported generic item */
528 else
529 rc = 2; /* out of range */
530 break;
531 }
532
533 return rc;
534 }
535
536 static int bsd_get_partition(struct fdisk_context *cxt, size_t n,
537 struct fdisk_partition *pa)
538 {
539 struct bsd_partition *p;
540 struct bsd_disklabel *d = self_disklabel(cxt);
541
542 assert(cxt);
543 assert(cxt->label);
544 assert(fdisk_is_label(cxt, BSD));
545
546 if (n >= d->d_npartitions)
547 return -EINVAL;
548
549 p = &d->d_partitions[n];
550
551 pa->used = p->p_size ? 1 : 0;
552 if (!pa->used)
553 return 0;
554
555 if (fdisk_use_cylinders(cxt) && d->d_secpercyl) {
556 pa->start_post = p->p_offset % d->d_secpercyl ? '*' : ' ';
557 pa->end_post = (p->p_offset + p->p_size) % d->d_secpercyl ? '*' : ' ';
558 }
559
560 pa->start = p->p_offset;
561 pa->size = p->p_size;
562 pa->type = bsd_partition_parttype(cxt, p);
563
564 if (p->p_fstype == BSD_FS_UNUSED || p->p_fstype == BSD_FS_BSDFFS) {
565 pa->fsize = p->p_fsize;
566 pa->bsize = p->p_fsize * p->p_frag;
567 }
568 if (p->p_fstype == BSD_FS_BSDFFS)
569 pa->cpg = p->p_cpg;
570
571 return 0;
572 }
573
574 static uint32_t ask_uint32(struct fdisk_context *cxt,
575 uint32_t dflt, char *mesg)
576 {
577 uintmax_t res;
578
579 if (fdisk_ask_number(cxt, min(dflt, (uint32_t) 1), dflt,
580 UINT32_MAX, mesg, &res) == 0)
581 return res;
582 return dflt;
583 }
584
585 static uint16_t ask_uint16(struct fdisk_context *cxt,
586 uint16_t dflt, char *mesg)
587 {
588 uintmax_t res;
589
590 if (fdisk_ask_number(cxt, min(dflt, (uint16_t) 1),
591 dflt, UINT16_MAX, mesg, &res) == 0)
592 return res;
593 return dflt;
594 }
595
596 /**
597 * fdisk_bsd_edit_disklabel:
598 * @cxt: context
599 *
600 * Edits fields in BSD disk label.
601 *
602 * Returns: 0 on success, <0 on error
603 */
604 int fdisk_bsd_edit_disklabel(struct fdisk_context *cxt)
605 {
606 struct bsd_disklabel *d = self_disklabel(cxt);
607 uintmax_t res;
608
609 #if defined (__alpha__) || defined (__ia64__)
610 if (fdisk_ask_number(cxt, DEFAULT_SECTOR_SIZE, d->d_secsize,
611 UINT32_MAX, _("bytes/sector"), &res) == 0)
612 d->d_secsize = res;
613
614 d->d_nsectors = ask_uint32(cxt, d->d_nsectors, _("sectors/track"));
615 d->d_ntracks = ask_uint32(cxt, d->d_ntracks, _("tracks/cylinder"));
616 d->d_ncylinders = ask_uint32(cxt, d->d_ncylinders ,_("cylinders"));
617 #endif
618 if (fdisk_ask_number(cxt, 1,
619 (uintmax_t) d->d_nsectors * d->d_ntracks,
620 (uintmax_t) d->d_nsectors * d->d_ntracks,
621 _("sectors/cylinder"), &res) == 0)
622 d->d_secpercyl = res;
623
624 d->d_rpm = ask_uint16(cxt, d->d_rpm, _("rpm"));
625 d->d_interleave = ask_uint16(cxt, d->d_interleave, _("interleave"));
626 d->d_trackskew = ask_uint16(cxt, d->d_trackskew, _("trackskew"));
627 d->d_cylskew = ask_uint16(cxt, d->d_cylskew, _("cylinderskew"));
628
629 d->d_headswitch = ask_uint32(cxt, d->d_headswitch, _("headswitch"));
630 d->d_trkseek = ask_uint32(cxt, d->d_trkseek, _("track-to-track seek"));
631
632 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
633 return 0;
634 }
635
636 static int bsd_get_bootstrap(struct fdisk_context *cxt,
637 char *path, void *ptr, int size)
638 {
639 int fd;
640
641 if ((fd = open(path, O_RDONLY)) < 0) {
642 fdisk_warn(cxt, _("cannot open %s"), path);
643 return -errno;
644 }
645
646 if (read_all(fd, ptr, size) != size) {
647 fdisk_warn(cxt, _("cannot read %s"), path);
648 close(fd);
649 return -errno;
650 }
651
652 fdisk_info(cxt, _("The bootstrap file %s successfully loaded."), path);
653 close (fd);
654 return 0;
655 }
656
657 /**
658 * fdisk_bsd_write_bootstrap:
659 * @cxt: context
660 *
661 * Install bootstrap file to the BSD device
662 */
663 int fdisk_bsd_write_bootstrap(struct fdisk_context *cxt)
664 {
665 struct bsd_disklabel dl, *d = self_disklabel(cxt);
666 struct fdisk_bsd_label *l = self_label(cxt);
667 char *name = d->d_type == BSD_DTYPE_SCSI ? "sd" : "wd";
668 char buf[BUFSIZ];
669 char *res, *dp, *p;
670 int rc;
671 fdisk_sector_t sector;
672
673 snprintf(buf, sizeof(buf),
674 _("Bootstrap: %1$sboot -> boot%1$s (default %1$s)"),
675 name);
676 rc = fdisk_ask_string(cxt, buf, &res);
677 if (rc)
678 goto done;
679 if (res && *res)
680 name = res;
681
682 snprintf(buf, sizeof(buf), "%s/%sboot", BSD_LINUX_BOOTDIR, name);
683 rc = bsd_get_bootstrap(cxt, buf, l->bsdbuffer, (int) d->d_secsize);
684 if (rc)
685 goto done;
686
687 /* We need a backup of the disklabel (might have changed). */
688 dp = &l->bsdbuffer[BSD_LABELSECTOR * DEFAULT_SECTOR_SIZE];
689 memmove(&dl, dp, sizeof(struct bsd_disklabel));
690
691 /* The disklabel will be overwritten by 0's from bootxx anyway */
692 memset(dp, 0, sizeof(struct bsd_disklabel));
693
694 snprintf(buf, sizeof(buf), "%s/boot%s", BSD_LINUX_BOOTDIR, name);
695 rc = bsd_get_bootstrap(cxt, buf,
696 &l->bsdbuffer[d->d_secsize],
697 (int) d->d_bbsize - d->d_secsize);
698 if (rc)
699 goto done;
700
701 /* check end of the bootstrap */
702 for (p = dp; p < dp + sizeof(struct bsd_disklabel); p++) {
703 if (!*p)
704 continue;
705 fdisk_warnx(cxt, _("Bootstrap overlaps with disklabel!"));
706 return -EINVAL;
707 }
708
709 /* move disklabel back */
710 memmove(dp, &dl, sizeof(struct bsd_disklabel));
711
712 sector = 0;
713 if (l->dos_part)
714 sector = dos_partition_get_start(l->dos_part);
715 #if defined (__alpha__)
716 alpha_bootblock_checksum(l->bsdbuffer);
717 #endif
718 if (lseek(cxt->dev_fd, (off_t) sector * DEFAULT_SECTOR_SIZE, SEEK_SET) == -1) {
719 fdisk_warn(cxt, _("seek on %s failed"), cxt->dev_path);
720 rc = -errno;
721 goto done;
722 }
723 if (write_all(cxt->dev_fd, l->bsdbuffer, BSD_BBSIZE)) {
724 fdisk_warn(cxt, _("cannot write %s"), cxt->dev_path);
725 rc = -errno;
726 goto done;
727 }
728
729 fdisk_info(cxt, _("Bootstrap installed on %s."), cxt->dev_path);
730 sync_disks(cxt);
731
732 rc = 0;
733 done:
734 free(res);
735 return rc;
736 }
737
738 static unsigned short bsd_dkcksum (struct bsd_disklabel *lp)
739 {
740 unsigned char *ptr, *end;
741 unsigned short sum = 0;
742
743 ptr = (unsigned char *) lp;
744 end = (unsigned char *) &lp->d_partitions[lp->d_npartitions];
745
746 while (ptr < end) {
747 unsigned short val;
748
749 memcpy(&val, ptr, sizeof(unsigned short));
750 sum ^= val;
751
752 ptr += sizeof(unsigned short);
753 }
754 return sum;
755 }
756
757 static int bsd_initlabel (struct fdisk_context *cxt)
758 {
759 struct fdisk_bsd_label *l = self_label(cxt);
760 struct bsd_disklabel *d = self_disklabel(cxt);
761 struct bsd_partition *pp;
762
763 memset (d, 0, sizeof (struct bsd_disklabel));
764
765 d -> d_magic = BSD_DISKMAGIC;
766
767 if (strncmp (cxt->dev_path, "/dev/sd", 7) == 0)
768 d -> d_type = BSD_DTYPE_SCSI;
769 else
770 d -> d_type = BSD_DTYPE_ST506;
771
772 #if !defined (__alpha__)
773 d -> d_flags = BSD_D_DOSPART;
774 #else
775 d -> d_flags = 0;
776 #endif
777 d -> d_secsize = DEFAULT_SECTOR_SIZE; /* bytes/sector */
778 d -> d_nsectors = cxt->geom.sectors; /* sectors/track */
779 d -> d_ntracks = cxt->geom.heads; /* tracks/cylinder (heads) */
780 d -> d_ncylinders = cxt->geom.cylinders;
781 d -> d_secpercyl = cxt->geom.sectors * cxt->geom.heads;/* sectors/cylinder */
782 if (d -> d_secpercyl == 0)
783 d -> d_secpercyl = 1; /* avoid segfaults */
784 d -> d_secperunit = d -> d_secpercyl * d -> d_ncylinders;
785
786 d -> d_rpm = 3600;
787 d -> d_interleave = 1;
788 d -> d_trackskew = 0;
789 d -> d_cylskew = 0;
790 d -> d_headswitch = 0;
791 d -> d_trkseek = 0;
792
793 d -> d_magic2 = BSD_DISKMAGIC;
794 d -> d_bbsize = BSD_BBSIZE;
795 d -> d_sbsize = BSD_SBSIZE;
796
797 if (l->dos_part) {
798 d->d_npartitions = 4;
799
800 pp = &d->d_partitions[2]; /* Partition C should be the NetBSD partition */
801 pp->p_offset = dos_partition_get_start(l->dos_part);
802 pp->p_size = dos_partition_get_size(l->dos_part);
803 pp->p_fstype = BSD_FS_UNUSED;
804
805 pp = &d -> d_partitions[3]; /* Partition D should be the whole disk */
806 pp->p_offset = 0;
807 pp->p_size = d->d_secperunit;
808 pp->p_fstype = BSD_FS_UNUSED;
809 } else {
810 d->d_npartitions = 3;
811
812 pp = &d->d_partitions[2]; /* Partition C should be the whole disk */
813 pp->p_offset = 0;
814 pp->p_size = d->d_secperunit;
815 pp->p_fstype = BSD_FS_UNUSED;
816 }
817
818 return 0;
819 }
820
821 /*
822 * Read a bsd_disklabel from sector 0 or from the starting sector of p.
823 * If it has the right magic, return 0.
824 */
825 static int bsd_readlabel(struct fdisk_context *cxt)
826 {
827 struct fdisk_bsd_label *l;
828 struct bsd_disklabel *d;
829 int t;
830 off_t offset = 0;
831
832 l = self_label(cxt);
833 d = self_disklabel(cxt);
834
835 if (l->dos_part)
836 /* BSD is nested within DOS partition, get the begin of the
837 * partition. Note that DOS uses native sector size. */
838 offset = dos_partition_get_start(l->dos_part) * cxt->sector_size;
839
840 if (lseek(cxt->dev_fd, offset, SEEK_SET) == -1)
841 return -1;
842 if (read_all(cxt->dev_fd, l->bsdbuffer, sizeof(l->bsdbuffer)) < 0)
843 return errno ? -errno : -1;
844
845 /* The offset to begin of the disk label. Note that BSD uses
846 * 512-byte (default) sectors. */
847 memmove(d, &l->bsdbuffer[BSD_LABELSECTOR * DEFAULT_SECTOR_SIZE
848 + BSD_LABELOFFSET], sizeof(*d));
849
850 if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC) {
851 DBG(LABEL, ul_debug("not found magic"));
852 return -1;
853 }
854
855 for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
856 d->d_partitions[t].p_size = 0;
857 d->d_partitions[t].p_offset = 0;
858 d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
859 }
860
861 if (d->d_npartitions > BSD_MAXPARTITIONS)
862 fdisk_warnx(cxt, _("Too many partitions (%d, maximum is %d)."),
863 d->d_npartitions, BSD_MAXPARTITIONS);
864
865 /* let's follow in-PT geometry */
866 cxt->geom.sectors = d->d_nsectors;
867 cxt->geom.heads = d->d_ntracks;
868 cxt->geom.cylinders = d->d_ncylinders;
869
870 if (fdisk_has_user_device_geometry(cxt))
871 fdisk_apply_user_device_properties(cxt);
872
873 cxt->label->nparts_cur = d->d_npartitions;
874 cxt->label->nparts_max = BSD_MAXPARTITIONS;
875 DBG(LABEL, ul_debug("read BSD label"));
876 return 0;
877 }
878
879 static int bsd_write_disklabel(struct fdisk_context *cxt)
880 {
881 off_t offset = 0;
882 struct fdisk_bsd_label *l = self_label(cxt);
883 struct bsd_disklabel *d = self_disklabel(cxt);
884
885
886 if (l->dos_part)
887 offset = dos_partition_get_start(l->dos_part) * cxt->sector_size;
888
889 d->d_checksum = 0;
890 d->d_checksum = bsd_dkcksum(d);
891
892 /* Update label within boot block. */
893 memmove(&l->bsdbuffer[BSD_LABELSECTOR * DEFAULT_SECTOR_SIZE
894 + BSD_LABELOFFSET], d, sizeof(*d));
895
896 #if defined (__alpha__) && BSD_LABELSECTOR == 0
897 /* Write the checksum to the end of the first sector. */
898 alpha_bootblock_checksum(l->bsdbuffer);
899 #endif
900 if (lseek(cxt->dev_fd, offset, SEEK_SET) == -1) {
901 fdisk_warn(cxt, _("seek on %s failed"), cxt->dev_path);
902 return -errno;
903 }
904 if (write_all(cxt->dev_fd, l->bsdbuffer, sizeof(l->bsdbuffer))) {
905 fdisk_warn(cxt, _("cannot write %s"), cxt->dev_path);
906 return -errno;
907 }
908 sync_disks(cxt);
909
910 if (cxt->parent && fdisk_label_is_changed(cxt->parent->label))
911 fdisk_info(cxt, _("Disklabel written to %s. (Don't forget to write the %s disklabel too.)"),
912 cxt->dev_path, cxt->parent->dev_path);
913 else
914 fdisk_info(cxt, _("Disklabel written to %s."), cxt->dev_path);
915 return 0;
916 }
917
918 static void sync_disks(struct fdisk_context *cxt)
919 {
920 fdisk_info(cxt, _("Syncing disks."));
921 sync();
922 }
923
924 static int bsd_translate_fstype (int linux_type)
925 {
926 switch (linux_type) {
927 case 0x01: /* DOS 12-bit FAT */
928 case 0x04: /* DOS 16-bit <32M */
929 case 0x06: /* DOS 16-bit >=32M */
930 case 0xe1: /* DOS access */
931 case 0xe3: /* DOS R/O */
932 #if !defined (__alpha__)
933 case 0xf2: /* DOS secondary */
934 return BSD_FS_MSDOS;
935 #endif
936 case 0x07: /* OS/2 HPFS */
937 return BSD_FS_HPFS;
938 default:
939 break;
940 }
941
942 return BSD_FS_OTHER;
943 }
944
945 /**
946 * fdisk_bsd_link_partition:
947 * @cxt: context
948 *
949 * Links partition from parent (DOS) to nested BSD partition table.
950 *
951 * Returns: 0 on success, <0 on error
952 */
953 int fdisk_bsd_link_partition(struct fdisk_context *cxt)
954 {
955 size_t k, i;
956 int rc;
957 struct dos_partition *p;
958 struct bsd_disklabel *d = self_disklabel(cxt);
959
960 if (!cxt->parent || !fdisk_is_label(cxt->parent, DOS)) {
961 fdisk_warnx(cxt, _("BSD label is not nested within a DOS partition."));
962 return -EINVAL;
963 }
964
965 /* ask for DOS partition */
966 rc = fdisk_ask_partnum(cxt->parent, &k, FALSE);
967 if (rc)
968 return rc;
969 /* ask for BSD partition */
970 rc = fdisk_ask_partnum(cxt, &i, TRUE);
971 if (rc)
972 return rc;
973
974 if (i >= BSD_MAXPARTITIONS)
975 return -EINVAL;
976
977 p = fdisk_dos_get_partition(cxt->parent, k);
978
979 d->d_partitions[i].p_size = dos_partition_get_size(p);
980 d->d_partitions[i].p_offset = dos_partition_get_start(p);
981 d->d_partitions[i].p_fstype = bsd_translate_fstype(p->sys_ind);
982
983 if (i >= d->d_npartitions)
984 d->d_npartitions = i + 1;
985
986 cxt->label->nparts_cur = d->d_npartitions;
987 fdisk_label_set_changed(cxt->label, 1);
988
989 fdisk_info(cxt, _("BSD partition '%c' linked to DOS partition %zu."),
990 'a' + (int) i, k + 1);
991 return 0;
992 }
993
994
995 static int bsd_partition_is_used(
996 struct fdisk_context *cxt,
997 size_t partnum)
998 {
999 struct bsd_disklabel *d = self_disklabel(cxt);
1000
1001 if (partnum >= BSD_MAXPARTITIONS)
1002 return 0;
1003
1004 return d->d_partitions[partnum].p_size ? 1 : 0;
1005 }
1006
1007
1008 static const struct fdisk_label_operations bsd_operations =
1009 {
1010 .probe = bsd_probe_label,
1011 .get_item = bsd_get_disklabel_item,
1012 .write = bsd_write_disklabel,
1013 .create = bsd_create_disklabel,
1014
1015 .del_part = bsd_delete_part,
1016 .get_part = bsd_get_partition,
1017 .set_part = bsd_set_partition,
1018 .add_part = bsd_add_partition,
1019
1020 .part_is_used = bsd_partition_is_used,
1021 };
1022
1023 static const struct fdisk_field bsd_fields[] =
1024 {
1025 { FDISK_FIELD_DEVICE, N_("Slice"), 1, 0 },
1026 { FDISK_FIELD_START, N_("Start"), 5, FDISK_FIELDFL_NUMBER },
1027 { FDISK_FIELD_END, N_("End"), 5, FDISK_FIELDFL_NUMBER },
1028 { FDISK_FIELD_SECTORS, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER },
1029 { FDISK_FIELD_CYLINDERS,N_("Cylinders"), 5, FDISK_FIELDFL_NUMBER },
1030 { FDISK_FIELD_SIZE, N_("Size"), 5, FDISK_FIELDFL_NUMBER },
1031 { FDISK_FIELD_TYPE, N_("Type"), 8, 0 },
1032 { FDISK_FIELD_FSIZE, N_("Fsize"), 5, FDISK_FIELDFL_NUMBER },
1033 { FDISK_FIELD_BSIZE, N_("Bsize"), 5, FDISK_FIELDFL_NUMBER },
1034 { FDISK_FIELD_CPG, N_("Cpg"), 5, FDISK_FIELDFL_NUMBER }
1035 };
1036
1037 /*
1038 * allocates BSD label driver
1039 */
1040 struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt __attribute__ ((__unused__)))
1041 {
1042 struct fdisk_label *lb;
1043 struct fdisk_bsd_label *bsd;
1044
1045 bsd = calloc(1, sizeof(*bsd));
1046 if (!bsd)
1047 return NULL;
1048
1049 /* initialize generic part of the driver */
1050 lb = (struct fdisk_label *) bsd;
1051 lb->name = "bsd";
1052 lb->id = FDISK_DISKLABEL_BSD;
1053 lb->op = &bsd_operations;
1054 lb->parttypes = bsd_fstypes;
1055 lb->nparttypes = ARRAY_SIZE(bsd_fstypes) - 1;
1056
1057 lb->fields = bsd_fields;
1058 lb->nfields = ARRAY_SIZE(bsd_fields);
1059
1060 lb->flags |= FDISK_LABEL_FL_INCHARS_PARTNO;
1061 lb->flags |= FDISK_LABEL_FL_REQUIRE_GEOMETRY;
1062
1063 /* return calloc() result to keep static anaylizers happy */
1064 return (struct fdisk_label *) bsd;
1065 }