source: MondoRescue/branches/3.0/mindi-busybox/util-linux/fdisk_osf.c@ 3085

Last change on this file since 3085 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
  • Property svn:eol-style set to native
File size: 29.5 KB
Line 
1/*
2 * Copyright (c) 1987, 1988 Regents of the University of California.
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if ENABLE_FEATURE_OSF_LABEL
35
36#ifndef BSD_DISKMAGIC
37#define BSD_DISKMAGIC ((uint32_t) 0x82564557)
38#endif
39
40#ifndef BSD_MAXPARTITIONS
41#define BSD_MAXPARTITIONS 16
42#endif
43
44#define BSD_LINUX_BOOTDIR "/usr/ucb/mdec"
45
46#if defined(i386) || defined(__sparc__) || defined(__arm__) \
47 || defined(__m68k__) || defined(__mips__) || defined(__s390__) \
48 || defined(__s390__) || defined(__s390x__) \
49 || defined(__sh__) || defined(__x86_64__) || defined(__avr32__) \
50 || defined(__nds32__)
51# define BSD_LABELSECTOR 1
52# define BSD_LABELOFFSET 0
53#elif defined(__alpha__) || defined(__powerpc__) || defined(__ia64__) \
54 || defined(__hppa__)
55# define BSD_LABELSECTOR 0
56# define BSD_LABELOFFSET 64
57#else
58# error unknown architecture
59#endif
60
61#define BSD_BBSIZE 8192 /* size of boot area, with label */
62#define BSD_SBSIZE 8192 /* max size of fs superblock */
63
64struct xbsd_disklabel {
65 uint32_t d_magic; /* the magic number */
66 int16_t d_type; /* drive type */
67 int16_t d_subtype; /* controller/d_type specific */
68 char d_typename[16]; /* type name, e.g. "eagle" */
69 char d_packname[16]; /* pack identifier */
70 /* disk geometry: */
71 uint32_t d_secsize; /* # of bytes per sector */
72 uint32_t d_nsectors; /* # of data sectors per track */
73 uint32_t d_ntracks; /* # of tracks per cylinder */
74 uint32_t d_ncylinders; /* # of data cylinders per unit */
75 uint32_t d_secpercyl; /* # of data sectors per cylinder */
76 uint32_t d_secperunit; /* # of data sectors per unit */
77 /*
78 * Spares (bad sector replacements) below
79 * are not counted in d_nsectors or d_secpercyl.
80 * Spare sectors are assumed to be physical sectors
81 * which occupy space at the end of each track and/or cylinder.
82 */
83 uint16_t d_sparespertrack; /* # of spare sectors per track */
84 uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
85 /*
86 * Alternate cylinders include maintenance, replacement,
87 * configuration description areas, etc.
88 */
89 uint32_t d_acylinders; /* # of alt. cylinders per unit */
90
91 /* hardware characteristics: */
92 /*
93 * d_interleave, d_trackskew and d_cylskew describe perturbations
94 * in the media format used to compensate for a slow controller.
95 * Interleave is physical sector interleave, set up by the formatter
96 * or controller when formatting. When interleaving is in use,
97 * logically adjacent sectors are not physically contiguous,
98 * but instead are separated by some number of sectors.
99 * It is specified as the ratio of physical sectors traversed
100 * per logical sector. Thus an interleave of 1:1 implies contiguous
101 * layout, while 2:1 implies that logical sector 0 is separated
102 * by one sector from logical sector 1.
103 * d_trackskew is the offset of sector 0 on track N
104 * relative to sector 0 on track N-1 on the same cylinder.
105 * Finally, d_cylskew is the offset of sector 0 on cylinder N
106 * relative to sector 0 on cylinder N-1.
107 */
108 uint16_t d_rpm; /* rotational speed */
109 uint16_t d_interleave; /* hardware sector interleave */
110 uint16_t d_trackskew; /* sector 0 skew, per track */
111 uint16_t d_cylskew; /* sector 0 skew, per cylinder */
112 uint32_t d_headswitch; /* head switch time, usec */
113 uint32_t d_trkseek; /* track-to-track seek, usec */
114 uint32_t d_flags; /* generic flags */
115#define NDDATA 5
116 uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
117#define NSPARE 5
118 uint32_t d_spare[NSPARE]; /* reserved for future use */
119 uint32_t d_magic2; /* the magic number (again) */
120 uint16_t d_checksum; /* xor of data incl. partitions */
121 /* filesystem and partition information: */
122 uint16_t d_npartitions; /* number of partitions in following */
123 uint32_t d_bbsize; /* size of boot area at sn0, bytes */
124 uint32_t d_sbsize; /* max size of fs superblock, bytes */
125 struct xbsd_partition { /* the partition table */
126 uint32_t p_size; /* number of sectors in partition */
127 uint32_t p_offset; /* starting sector */
128 uint32_t p_fsize; /* filesystem basic fragment size */
129 uint8_t p_fstype; /* filesystem type, see below */
130 uint8_t p_frag; /* filesystem fragments per block */
131 uint16_t p_cpg; /* filesystem cylinders per group */
132 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
133};
134
135/* d_type values: */
136#define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
137#define BSD_DTYPE_MSCP 2 /* MSCP */
138#define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */
139#define BSD_DTYPE_SCSI 4 /* SCSI */
140#define BSD_DTYPE_ESDI 5 /* ESDI interface */
141#define BSD_DTYPE_ST506 6 /* ST506 etc. */
142#define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */
143#define BSD_DTYPE_HPFL 8 /* HP Fiber-link */
144#define BSD_DTYPE_FLOPPY 10 /* floppy */
145
146/* d_subtype values: */
147#define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */
148#define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */
149#define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */
150
151static const char *const xbsd_dktypenames[] = {
152 "unknown",
153 "SMD",
154 "MSCP",
155 "old DEC",
156 "SCSI",
157 "ESDI",
158 "ST506",
159 "HP-IB",
160 "HP-FL",
161 "type 9",
162 "floppy",
163 0
164};
165
166
167/*
168 * Filesystem type and version.
169 * Used to interpret other filesystem-specific
170 * per-partition information.
171 */
172#define BSD_FS_UNUSED 0 /* unused */
173#define BSD_FS_SWAP 1 /* swap */
174#define BSD_FS_V6 2 /* Sixth Edition */
175#define BSD_FS_V7 3 /* Seventh Edition */
176#define BSD_FS_SYSV 4 /* System V */
177#define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
178#define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */
179#define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */
180#define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */
181#define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */
182#define BSD_FS_HPFS 11 /* OS/2 high-performance file system */
183#define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */
184#define BSD_FS_ISOFS BSD_FS_ISO9660
185#define BSD_FS_BOOT 13 /* partition contains bootstrap */
186#define BSD_FS_ADOS 14 /* AmigaDOS fast file system */
187#define BSD_FS_HFS 15 /* Macintosh HFS */
188#define BSD_FS_ADVFS 16 /* Digital Unix AdvFS */
189
190/* this is annoying, but it's also the way it is :-( */
191#ifdef __alpha__
192#define BSD_FS_EXT2 8 /* ext2 file system */
193#else
194#define BSD_FS_MSDOS 8 /* MS-DOS file system */
195#endif
196
197static const char *const xbsd_fstypes[] = {
198 "\x00" "unused", /* BSD_FS_UNUSED */
199 "\x01" "swap", /* BSD_FS_SWAP */
200 "\x02" "Version 6", /* BSD_FS_V6 */
201 "\x03" "Version 7", /* BSD_FS_V7 */
202 "\x04" "System V", /* BSD_FS_SYSV */
203 "\x05" "4.1BSD", /* BSD_FS_V71K */
204 "\x06" "Eighth Edition", /* BSD_FS_V8 */
205 "\x07" "4.2BSD", /* BSD_FS_BSDFFS */
206#ifdef __alpha__
207 "\x08" "ext2", /* BSD_FS_EXT2 */
208#else
209 "\x08" "MS-DOS", /* BSD_FS_MSDOS */
210#endif
211 "\x09" "4.4LFS", /* BSD_FS_BSDLFS */
212 "\x0a" "unknown", /* BSD_FS_OTHER */
213 "\x0b" "HPFS", /* BSD_FS_HPFS */
214 "\x0c" "ISO-9660", /* BSD_FS_ISO9660 */
215 "\x0d" "boot", /* BSD_FS_BOOT */
216 "\x0e" "ADOS", /* BSD_FS_ADOS */
217 "\x0f" "HFS", /* BSD_FS_HFS */
218 "\x10" "AdvFS", /* BSD_FS_ADVFS */
219 NULL
220};
221
222
223/*
224 * flags shared by various drives:
225 */
226#define BSD_D_REMOVABLE 0x01 /* removable media */
227#define BSD_D_ECC 0x02 /* supports ECC */
228#define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
229#define BSD_D_RAMDISK 0x08 /* disk emulator */
230#define BSD_D_CHAIN 0x10 /* can do back-back transfers */
231#define BSD_D_DOSPART 0x20 /* within MSDOS partition */
232
233/*
234 Changes:
235 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
236
237 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
238 support for OSF/1 disklabels on Alpha.
239 Also fixed unaligned accesses in alpha_bootblock_checksum()
240*/
241
242#define FREEBSD_PARTITION 0xa5
243#define NETBSD_PARTITION 0xa9
244
245static void xbsd_delete_part(void);
246static void xbsd_new_part(void);
247static void xbsd_write_disklabel(void);
248static int xbsd_create_disklabel(void);
249static void xbsd_edit_disklabel(void);
250static void xbsd_write_bootstrap(void);
251static void xbsd_change_fstype(void);
252static int xbsd_get_part_index(int max);
253static int xbsd_check_new_partition(int *i);
254static void xbsd_list_types(void);
255static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
256static int xbsd_initlabel(struct partition *p);
257static int xbsd_readlabel(struct partition *p);
258static int xbsd_writelabel(struct partition *p);
259
260#if defined(__alpha__)
261static void alpha_bootblock_checksum(char *boot);
262#endif
263
264#if !defined(__alpha__)
265static int xbsd_translate_fstype(int linux_type);
266static void xbsd_link_part(void);
267static struct partition *xbsd_part;
268static int xbsd_part_index;
269#endif
270
271
272/* Group big globals data and allocate it in one go */
273struct bsd_globals {
274/* We access this through a uint64_t * when checksumming */
275/* hopefully xmalloc gives us required alignment */
276 char disklabelbuffer[BSD_BBSIZE];
277 struct xbsd_disklabel xbsd_dlabel;
278};
279
280static struct bsd_globals *bsd_globals_ptr;
281
282#define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
283#define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel)
284
285
286/* Code */
287
288#define bsd_cround(n) \
289 (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
290
291/*
292 * Test whether the whole disk has BSD disk label magic.
293 *
294 * Note: often reformatting with DOS-type label leaves the BSD magic,
295 * so this does not mean that there is a BSD disk label.
296 */
297static int
298check_osf_label(void)
299{
300 if (xbsd_readlabel(NULL) == 0)
301 return 0;
302 return 1;
303}
304
305static int
306bsd_trydev(const char * dev)
307{
308 if (xbsd_readlabel(NULL) == 0)
309 return -1;
310 printf("\nBSD label for device: %s\n", dev);
311 xbsd_print_disklabel(0);
312 return 0;
313}
314
315static void
316bsd_menu(void)
317{
318 puts("Command Action");
319 puts("d\tdelete a BSD partition");
320 puts("e\tedit drive data");
321 puts("i\tinstall bootstrap");
322 puts("l\tlist known filesystem types");
323 puts("n\tadd a new BSD partition");
324 puts("p\tprint BSD partition table");
325 puts("q\tquit without saving changes");
326 puts("r\treturn to main menu");
327 puts("s\tshow complete disklabel");
328 puts("t\tchange a partition's filesystem id");
329 puts("u\tchange units (cylinders/sectors)");
330 puts("w\twrite disklabel to disk");
331#if !defined(__alpha__)
332 puts("x\tlink BSD partition to non-BSD partition");
333#endif
334}
335
336#if !defined(__alpha__)
337static int
338hidden(int type)
339{
340 return type ^ 0x10;
341}
342
343static int
344is_bsd_partition_type(int type)
345{
346 return (type == FREEBSD_PARTITION ||
347 type == hidden(FREEBSD_PARTITION) ||
348 type == NETBSD_PARTITION ||
349 type == hidden(NETBSD_PARTITION));
350}
351#endif
352
353static void
354bsd_select(void)
355{
356#if !defined(__alpha__)
357 int t, ss;
358 struct partition *p;
359
360 for (t = 0; t < 4; t++) {
361 p = get_part_table(t);
362 if (p && is_bsd_partition_type(p->sys_ind)) {
363 xbsd_part = p;
364 xbsd_part_index = t;
365 ss = get_start_sect(xbsd_part);
366 if (ss == 0) {
367 printf("Partition %s has invalid starting sector 0\n",
368 partname(disk_device, t+1, 0));
369 return;
370 }
371 printf("Reading disklabel of %s at sector %u\n",
372 partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
373 if (xbsd_readlabel(xbsd_part) == 0)
374 if (xbsd_create_disklabel() == 0)
375 return;
376 break;
377 }
378 }
379
380 if (t == 4) {
381 printf("There is no *BSD partition on %s\n", disk_device);
382 return;
383 }
384
385#elif defined(__alpha__)
386
387 if (xbsd_readlabel(NULL) == 0)
388 if (xbsd_create_disklabel() == 0)
389 exit(EXIT_SUCCESS);
390
391#endif
392
393 while (1) {
394 bb_putchar('\n');
395 switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
396 case 'd':
397 xbsd_delete_part();
398 break;
399 case 'e':
400 xbsd_edit_disklabel();
401 break;
402 case 'i':
403 xbsd_write_bootstrap();
404 break;
405 case 'l':
406 xbsd_list_types();
407 break;
408 case 'n':
409 xbsd_new_part();
410 break;
411 case 'p':
412 xbsd_print_disklabel(0);
413 break;
414 case 'q':
415 if (ENABLE_FEATURE_CLEAN_UP)
416 close_dev_fd();
417 exit(EXIT_SUCCESS);
418 case 'r':
419 return;
420 case 's':
421 xbsd_print_disklabel(1);
422 break;
423 case 't':
424 xbsd_change_fstype();
425 break;
426 case 'u':
427 change_units();
428 break;
429 case 'w':
430 xbsd_write_disklabel();
431 break;
432#if !defined(__alpha__)
433 case 'x':
434 xbsd_link_part();
435 break;
436#endif
437 default:
438 bsd_menu();
439 break;
440 }
441 }
442}
443
444static void
445xbsd_delete_part(void)
446{
447 int i;
448
449 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
450 xbsd_dlabel.d_partitions[i].p_size = 0;
451 xbsd_dlabel.d_partitions[i].p_offset = 0;
452 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
453 if (xbsd_dlabel.d_npartitions == i + 1)
454 while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
455 xbsd_dlabel.d_npartitions--;
456}
457
458static void
459xbsd_new_part(void)
460{
461 off_t begin, end;
462 char mesg[256];
463 int i;
464
465 if (!xbsd_check_new_partition(&i))
466 return;
467
468#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
469 begin = get_start_sect(xbsd_part);
470 end = begin + get_nr_sects(xbsd_part) - 1;
471#else
472 begin = 0;
473 end = xbsd_dlabel.d_secperunit - 1;
474#endif
475
476 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
477 begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
478 0, mesg);
479
480 if (display_in_cyl_units)
481 begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
482
483 snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
484 str_units(SINGULAR));
485 end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
486 bsd_cround(begin), mesg);
487
488 if (display_in_cyl_units)
489 end = end * xbsd_dlabel.d_secpercyl - 1;
490
491 xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
492 xbsd_dlabel.d_partitions[i].p_offset = begin;
493 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
494}
495
496static void
497xbsd_print_disklabel(int show_all)
498{
499 struct xbsd_disklabel *lp = &xbsd_dlabel;
500 struct xbsd_partition *pp;
501 int i, j;
502
503 if (show_all) {
504 static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
505
506#if defined(__alpha__)
507 printf("# %s:\n", disk_device);
508#else
509 printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
510#endif
511 if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
512 printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
513 else
514 printf("type: %u\n", lp->d_type);
515 printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
516 printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
517 printf("flags: ");
518 print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
519 bb_putchar('\n');
520 /* On various machines the fields of *lp are short/int/long */
521 /* In order to avoid problems, we cast them all to long. */
522 printf("bytes/sector: %lu\n", (long) lp->d_secsize);
523 printf("sectors/track: %lu\n", (long) lp->d_nsectors);
524 printf("tracks/cylinder: %lu\n", (long) lp->d_ntracks);
525 printf("sectors/cylinder: %lu\n", (long) lp->d_secpercyl);
526 printf("cylinders: %lu\n", (long) lp->d_ncylinders);
527 printf("rpm: %u\n", lp->d_rpm);
528 printf("interleave: %u\n", lp->d_interleave);
529 printf("trackskew: %u\n", lp->d_trackskew);
530 printf("cylinderskew: %u\n", lp->d_cylskew);
531 printf("headswitch: %lu\t\t# milliseconds\n",
532 (long) lp->d_headswitch);
533 printf("track-to-track seek: %lu\t# milliseconds\n",
534 (long) lp->d_trkseek);
535 printf("drivedata: ");
536 for (i = NDDATA - 1; i >= 0; i--)
537 if (lp->d_drivedata[i])
538 break;
539 if (i < 0)
540 i = 0;
541 for (j = 0; j <= i; j++)
542 printf("%lu ", (long) lp->d_drivedata[j]);
543 }
544 printf("\n%u partitions:\n", lp->d_npartitions);
545 printf("# start end size fstype [fsize bsize cpg]\n");
546 pp = lp->d_partitions;
547 for (i = 0; i < lp->d_npartitions; i++, pp++) {
548 if (pp->p_size) {
549 if (display_in_cyl_units && lp->d_secpercyl) {
550 printf(" %c: %8lu%c %8lu%c %8lu%c ",
551 'a' + i,
552 (unsigned long) pp->p_offset / lp->d_secpercyl + 1,
553 (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
554 (unsigned long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
555 ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
556 (long) pp->p_size / lp->d_secpercyl,
557 (pp->p_size % lp->d_secpercyl) ? '*' : ' '
558 );
559 } else {
560 printf(" %c: %8lu %8lu %8lu ",
561 'a' + i,
562 (long) pp->p_offset,
563 (long) pp->p_offset + pp->p_size - 1,
564 (long) pp->p_size
565 );
566 }
567
568 if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
569 printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
570 else
571 printf("%8x", pp->p_fstype);
572
573 switch (pp->p_fstype) {
574 case BSD_FS_UNUSED:
575 printf(" %5lu %5lu %5.5s ",
576 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
577 break;
578 case BSD_FS_BSDFFS:
579 printf(" %5lu %5lu %5u ",
580 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
581 break;
582 default:
583 printf("%22.22s", "");
584 break;
585 }
586 bb_putchar('\n');
587 }
588 }
589}
590
591static void
592xbsd_write_disklabel(void)
593{
594#if defined(__alpha__)
595 printf("Writing disklabel to %s\n", disk_device);
596 xbsd_writelabel(NULL);
597#else
598 printf("Writing disklabel to %s\n",
599 partname(disk_device, xbsd_part_index + 1, 0));
600 xbsd_writelabel(xbsd_part);
601#endif
602 reread_partition_table(0); /* no exit yet */
603}
604
605static int
606xbsd_create_disklabel(void)
607{
608 char c;
609
610#if defined(__alpha__)
611 printf("%s contains no disklabel\n", disk_device);
612#else
613 printf("%s contains no disklabel\n",
614 partname(disk_device, xbsd_part_index + 1, 0));
615#endif
616
617 while (1) {
618 c = read_nonempty("Do you want to create a disklabel? (y/n) ");
619 if ((c|0x20) == 'y') {
620 if (xbsd_initlabel(
621#if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
622 defined(__s390__) || defined(__s390x__)
623 NULL
624#else
625 xbsd_part
626#endif
627 ) == 1) {
628 xbsd_print_disklabel(1);
629 return 1;
630 }
631 return 0;
632 }
633 if ((c|0x20) == 'n')
634 return 0;
635 }
636}
637
638static int
639edit_int(int def, const char *mesg)
640{
641 mesg = xasprintf("%s (%u): ", mesg, def);
642 do {
643 if (!read_line(mesg))
644 goto ret;
645 } while (!isdigit(*line_ptr));
646 def = atoi(line_ptr);
647 ret:
648 free((char*)mesg);
649 return def;
650}
651
652static void
653xbsd_edit_disklabel(void)
654{
655 struct xbsd_disklabel *d;
656
657 d = &xbsd_dlabel;
658
659#if defined(__alpha__) || defined(__ia64__)
660 d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
661 d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
662 d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
663 d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
664#endif
665
666 /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
667 while (1) {
668 d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
669 "sectors/cylinder");
670 if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
671 break;
672
673 printf("Must be <= sectors/track * tracks/cylinder (default)\n");
674 }
675 d->d_rpm = edit_int(d->d_rpm , "rpm");
676 d->d_interleave = edit_int(d->d_interleave, "interleave");
677 d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
678 d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
679 d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
680 d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
681
682 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
683}
684
685static int
686xbsd_get_bootstrap(char *path, void *ptr, int size)
687{
688 int fdb;
689
690 fdb = open_or_warn(path, O_RDONLY);
691 if (fdb < 0) {
692 return 0;
693 }
694 if (full_read(fdb, ptr, size) < 0) {
695 bb_simple_perror_msg(path);
696 close(fdb);
697 return 0;
698 }
699 printf(" ... %s\n", path);
700 close(fdb);
701 return 1;
702}
703
704static void
705sync_disks(void)
706{
707 printf("Syncing disks\n");
708 sync();
709 /* sleep(4); What? */
710}
711
712static void
713xbsd_write_bootstrap(void)
714{
715 char path[MAXPATHLEN];
716 const char *bootdir = BSD_LINUX_BOOTDIR;
717 const char *dkbasename;
718 struct xbsd_disklabel dl;
719 char *d, *p, *e;
720 int sector;
721
722 if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
723 dkbasename = "sd";
724 else
725 dkbasename = "wd";
726
727 snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
728 dkbasename, dkbasename, dkbasename);
729 if (read_line(path)) {
730 dkbasename = line_ptr;
731 }
732 snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
733 if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
734 return;
735
736/* We need a backup of the disklabel (xbsd_dlabel might have changed). */
737 d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
738 memmove(&dl, d, sizeof(struct xbsd_disklabel));
739
740/* The disklabel will be overwritten by 0's from bootxx anyway */
741 memset(d, 0, sizeof(struct xbsd_disklabel));
742
743 snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
744 if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
745 (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
746 return;
747
748 e = d + sizeof(struct xbsd_disklabel);
749 for (p = d; p < e; p++)
750 if (*p) {
751 printf("Bootstrap overlaps with disk label!\n");
752 exit(EXIT_FAILURE);
753 }
754
755 memmove(d, &dl, sizeof(struct xbsd_disklabel));
756
757#if defined(__powerpc__) || defined(__hppa__)
758 sector = 0;
759#elif defined(__alpha__)
760 sector = 0;
761 alpha_bootblock_checksum(disklabelbuffer);
762#else
763 sector = get_start_sect(xbsd_part);
764#endif
765
766 seek_sector(sector);
767 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
768
769#if defined(__alpha__)
770 printf("Bootstrap installed on %s\n", disk_device);
771#else
772 printf("Bootstrap installed on %s\n",
773 partname(disk_device, xbsd_part_index+1, 0));
774#endif
775
776 sync_disks();
777}
778
779static void
780xbsd_change_fstype(void)
781{
782 int i;
783
784 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
785 xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
786}
787
788static int
789xbsd_get_part_index(int max)
790{
791 char prompt[sizeof("Partition (a-%c): ") + 16];
792 char l;
793
794 snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
795 do
796 l = tolower(read_nonempty(prompt));
797 while (l < 'a' || l > 'a' + max - 1);
798 return l - 'a';
799}
800
801static int
802xbsd_check_new_partition(int *i)
803{
804 /* room for more? various BSD flavours have different maxima */
805 if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
806 int t;
807
808 for (t = 0; t < BSD_MAXPARTITIONS; t++)
809 if (xbsd_dlabel.d_partitions[t].p_size == 0)
810 break;
811
812 if (t == BSD_MAXPARTITIONS) {
813 printf("The maximum number of partitions has been created\n");
814 return 0;
815 }
816 }
817
818 *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
819
820 if (*i >= xbsd_dlabel.d_npartitions)
821 xbsd_dlabel.d_npartitions = (*i) + 1;
822
823 if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
824 printf("This partition already exists\n");
825 return 0;
826 }
827
828 return 1;
829}
830
831static void
832xbsd_list_types(void)
833{
834 list_types(xbsd_fstypes);
835}
836
837static uint16_t
838xbsd_dkcksum(struct xbsd_disklabel *lp)
839{
840 uint16_t *start, *end;
841 uint16_t sum = 0;
842
843 start = (uint16_t *) lp;
844 end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
845 while (start < end)
846 sum ^= *start++;
847 return sum;
848}
849
850static int
851xbsd_initlabel(struct partition *p)
852{
853 struct xbsd_disklabel *d = &xbsd_dlabel;
854 struct xbsd_partition *pp;
855
856 get_geometry();
857 memset(d, 0, sizeof(struct xbsd_disklabel));
858
859 d->d_magic = BSD_DISKMAGIC;
860
861 if (strncmp(disk_device, "/dev/sd", 7) == 0)
862 d->d_type = BSD_DTYPE_SCSI;
863 else
864 d->d_type = BSD_DTYPE_ST506;
865
866#if !defined(__alpha__)
867 d->d_flags = BSD_D_DOSPART;
868#else
869 d->d_flags = 0;
870#endif
871 d->d_secsize = SECTOR_SIZE; /* bytes/sector */
872 d->d_nsectors = g_sectors; /* sectors/track */
873 d->d_ntracks = g_heads; /* tracks/cylinder (heads) */
874 d->d_ncylinders = g_cylinders;
875 d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */
876 if (d->d_secpercyl == 0)
877 d->d_secpercyl = 1; /* avoid segfaults */
878 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
879
880 d->d_rpm = 3600;
881 d->d_interleave = 1;
882 d->d_trackskew = 0;
883 d->d_cylskew = 0;
884 d->d_headswitch = 0;
885 d->d_trkseek = 0;
886
887 d->d_magic2 = BSD_DISKMAGIC;
888 d->d_bbsize = BSD_BBSIZE;
889 d->d_sbsize = BSD_SBSIZE;
890
891#if !defined(__alpha__)
892 d->d_npartitions = 4;
893 pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
894
895 pp->p_offset = get_start_sect(p);
896 pp->p_size = get_nr_sects(p);
897 pp->p_fstype = BSD_FS_UNUSED;
898 pp = &d->d_partitions[3]; /* Partition D should be whole disk */
899
900 pp->p_offset = 0;
901 pp->p_size = d->d_secperunit;
902 pp->p_fstype = BSD_FS_UNUSED;
903#else
904 d->d_npartitions = 3;
905 pp = &d->d_partitions[2]; /* Partition C should be
906 the whole disk */
907 pp->p_offset = 0;
908 pp->p_size = d->d_secperunit;
909 pp->p_fstype = BSD_FS_UNUSED;
910#endif
911
912 return 1;
913}
914
915/*
916 * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
917 * If it has the right magic, return 1.
918 */
919static int
920xbsd_readlabel(struct partition *p)
921{
922 struct xbsd_disklabel *d;
923 int t, sector;
924
925 if (!bsd_globals_ptr)
926 bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
927
928 d = &xbsd_dlabel;
929
930 /* p is used only to get the starting sector */
931#if !defined(__alpha__)
932 sector = (p ? get_start_sect(p) : 0);
933#else
934 sector = 0;
935#endif
936
937 seek_sector(sector);
938 if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
939 fdisk_fatal(unable_to_read);
940
941 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
942 sizeof(struct xbsd_disklabel));
943
944 if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
945 return 0;
946
947 for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
948 d->d_partitions[t].p_size = 0;
949 d->d_partitions[t].p_offset = 0;
950 d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
951 }
952
953 if (d->d_npartitions > BSD_MAXPARTITIONS)
954 printf("Warning: too many partitions (%u, maximum is %u)\n",
955 d->d_npartitions, BSD_MAXPARTITIONS);
956 return 1;
957}
958
959static int
960xbsd_writelabel(struct partition *p)
961{
962 struct xbsd_disklabel *d = &xbsd_dlabel;
963 unsigned int sector;
964
965#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
966 sector = get_start_sect(p) + BSD_LABELSECTOR;
967#else
968 (void)p; /* silence warning */
969 sector = BSD_LABELSECTOR;
970#endif
971
972 d->d_checksum = 0;
973 d->d_checksum = xbsd_dkcksum(d);
974
975 /* This is necessary if we want to write the bootstrap later,
976 otherwise we'd write the old disklabel with the bootstrap.
977 */
978 memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
979 d, sizeof(struct xbsd_disklabel));
980
981#if defined(__alpha__) && BSD_LABELSECTOR == 0
982 alpha_bootblock_checksum(disklabelbuffer);
983 seek_sector(0);
984 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
985#else
986 seek_sector(sector);
987 lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
988 xwrite(dev_fd, d, sizeof(*d));
989#endif
990 sync_disks();
991 return 1;
992}
993
994
995#if !defined(__alpha__)
996static int
997xbsd_translate_fstype(int linux_type)
998{
999 switch (linux_type) {
1000 case 0x01: /* DOS 12-bit FAT */
1001 case 0x04: /* DOS 16-bit <32M */
1002 case 0x06: /* DOS 16-bit >=32M */
1003 case 0xe1: /* DOS access */
1004 case 0xe3: /* DOS R/O */
1005 case 0xf2: /* DOS secondary */
1006 return BSD_FS_MSDOS;
1007 case 0x07: /* OS/2 HPFS */
1008 return BSD_FS_HPFS;
1009 default:
1010 return BSD_FS_OTHER;
1011 }
1012}
1013
1014static void
1015xbsd_link_part(void)
1016{
1017 int k, i;
1018 struct partition *p;
1019
1020 k = get_partition(1, g_partitions);
1021
1022 if (!xbsd_check_new_partition(&i))
1023 return;
1024
1025 p = get_part_table(k);
1026
1027 xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
1028 xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
1029 xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
1030}
1031#endif
1032
1033#if defined(__alpha__)
1034static void
1035alpha_bootblock_checksum(char *boot)
1036{
1037 uint64_t *dp, sum;
1038 int i;
1039
1040 dp = (uint64_t *)boot;
1041 sum = 0;
1042 for (i = 0; i < 63; i++)
1043 sum += dp[i];
1044 dp[63] = sum;
1045}
1046#endif /* __alpha__ */
1047
1048/* Undefine 'global' tricks */
1049#undef disklabelbuffer
1050#undef xbsd_dlabel
1051
1052#endif /* OSF_LABEL */
Note: See TracBrowser for help on using the repository browser.