blob: afb8459a218fcefca67bbde64b554d4b2b59ee2b [file] [log] [blame]
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002/*
3 * Copyright (c) 1987, 1988 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgment:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
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
Denis Vlasenko2a856762007-01-02 16:45:05 +000046#if defined(i386) || defined(__sparc__) || defined(__arm__) \
47 || defined(__m68k__) || defined(__mips__) || defined(__s390__) \
48 || defined(__sh__) || defined(__x86_64__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +000049#define BSD_LABELSECTOR 1
50#define BSD_LABELOFFSET 0
Denis Vlasenko2a856762007-01-02 16:45:05 +000051#elif defined(__alpha__) || defined(__powerpc__) || defined(__ia64__) \
52 || defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +000053#define BSD_LABELSECTOR 0
54#define BSD_LABELOFFSET 64
Denis Vlasenko2a856762007-01-02 16:45:05 +000055#elif defined(__s390__) || defined(__s390x__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +000056#define BSD_LABELSECTOR 1
57#define BSD_LABELOFFSET 0
58#else
59#error unknown architecture
60#endif
61
62#define BSD_BBSIZE 8192 /* size of boot area, with label */
63#define BSD_SBSIZE 8192 /* max size of fs superblock */
64
65struct xbsd_disklabel {
66 uint32_t d_magic; /* the magic number */
67 int16_t d_type; /* drive type */
68 int16_t d_subtype; /* controller/d_type specific */
69 char d_typename[16]; /* type name, e.g. "eagle" */
70 char d_packname[16]; /* pack identifier */
71 /* disk geometry: */
72 uint32_t d_secsize; /* # of bytes per sector */
73 uint32_t d_nsectors; /* # of data sectors per track */
74 uint32_t d_ntracks; /* # of tracks per cylinder */
75 uint32_t d_ncylinders; /* # of data cylinders per unit */
76 uint32_t d_secpercyl; /* # of data sectors per cylinder */
77 uint32_t d_secperunit; /* # of data sectors per unit */
78 /*
79 * Spares (bad sector replacements) below
80 * are not counted in d_nsectors or d_secpercyl.
81 * Spare sectors are assumed to be physical sectors
82 * which occupy space at the end of each track and/or cylinder.
83 */
84 uint16_t d_sparespertrack; /* # of spare sectors per track */
85 uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
86 /*
87 * Alternate cylinders include maintenance, replacement,
88 * configuration description areas, etc.
89 */
90 uint32_t d_acylinders; /* # of alt. cylinders per unit */
91
92 /* hardware characteristics: */
93 /*
94 * d_interleave, d_trackskew and d_cylskew describe perturbations
95 * in the media format used to compensate for a slow controller.
96 * Interleave is physical sector interleave, set up by the formatter
97 * or controller when formatting. When interleaving is in use,
98 * logically adjacent sectors are not physically contiguous,
99 * but instead are separated by some number of sectors.
100 * It is specified as the ratio of physical sectors traversed
101 * per logical sector. Thus an interleave of 1:1 implies contiguous
102 * layout, while 2:1 implies that logical sector 0 is separated
103 * by one sector from logical sector 1.
104 * d_trackskew is the offset of sector 0 on track N
105 * relative to sector 0 on track N-1 on the same cylinder.
106 * Finally, d_cylskew is the offset of sector 0 on cylinder N
107 * relative to sector 0 on cylinder N-1.
108 */
109 uint16_t d_rpm; /* rotational speed */
110 uint16_t d_interleave; /* hardware sector interleave */
111 uint16_t d_trackskew; /* sector 0 skew, per track */
112 uint16_t d_cylskew; /* sector 0 skew, per cylinder */
113 uint32_t d_headswitch; /* head switch time, usec */
114 uint32_t d_trkseek; /* track-to-track seek, usec */
115 uint32_t d_flags; /* generic flags */
116#define NDDATA 5
117 uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
118#define NSPARE 5
119 uint32_t d_spare[NSPARE]; /* reserved for future use */
120 uint32_t d_magic2; /* the magic number (again) */
121 uint16_t d_checksum; /* xor of data incl. partitions */
122 /* filesystem and partition information: */
123 uint16_t d_npartitions; /* number of partitions in following */
124 uint32_t d_bbsize; /* size of boot area at sn0, bytes */
125 uint32_t d_sbsize; /* max size of fs superblock, bytes */
126 struct xbsd_partition { /* the partition table */
127 uint32_t p_size; /* number of sectors in partition */
128 uint32_t p_offset; /* starting sector */
129 uint32_t p_fsize; /* filesystem basic fragment size */
130 uint8_t p_fstype; /* filesystem type, see below */
131 uint8_t p_frag; /* filesystem fragments per block */
132 uint16_t p_cpg; /* filesystem cylinders per group */
133 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
134};
135
136/* d_type values: */
137#define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
138#define BSD_DTYPE_MSCP 2 /* MSCP */
139#define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */
140#define BSD_DTYPE_SCSI 4 /* SCSI */
141#define BSD_DTYPE_ESDI 5 /* ESDI interface */
142#define BSD_DTYPE_ST506 6 /* ST506 etc. */
143#define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */
144#define BSD_DTYPE_HPFL 8 /* HP Fiber-link */
145#define BSD_DTYPE_FLOPPY 10 /* floppy */
146
147/* d_subtype values: */
148#define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */
149#define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */
150#define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */
151
152static const char * const xbsd_dktypenames[] = {
153 "unknown",
154 "SMD",
155 "MSCP",
156 "old DEC",
157 "SCSI",
158 "ESDI",
159 "ST506",
160 "HP-IB",
161 "HP-FL",
162 "type 9",
163 "floppy",
164 0
165};
166#define BSD_DKMAXTYPES (sizeof(xbsd_dktypenames) / sizeof(xbsd_dktypenames[0]) - 1)
167
168/*
169 * Filesystem type and version.
170 * Used to interpret other filesystem-specific
171 * per-partition information.
172 */
173#define BSD_FS_UNUSED 0 /* unused */
174#define BSD_FS_SWAP 1 /* swap */
175#define BSD_FS_V6 2 /* Sixth Edition */
176#define BSD_FS_V7 3 /* Seventh Edition */
177#define BSD_FS_SYSV 4 /* System V */
178#define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
179#define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */
180#define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */
181#define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */
182#define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */
183#define BSD_FS_HPFS 11 /* OS/2 high-performance file system */
184#define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */
185#define BSD_FS_ISOFS BSD_FS_ISO9660
186#define BSD_FS_BOOT 13 /* partition contains bootstrap */
187#define BSD_FS_ADOS 14 /* AmigaDOS fast file system */
188#define BSD_FS_HFS 15 /* Macintosh HFS */
189#define BSD_FS_ADVFS 16 /* Digital Unix AdvFS */
190
191/* this is annoying, but it's also the way it is :-( */
192#ifdef __alpha__
193#define BSD_FS_EXT2 8 /* ext2 file system */
194#else
195#define BSD_FS_MSDOS 8 /* MS-DOS file system */
196#endif
197
Denis Vlasenkobd852072007-03-19 14:43:38 +0000198static const char *const xbsd_fstypes[] = {
199 "\x00" "unused", /* BSD_FS_UNUSED */
200 "\x01" "swap", /* BSD_FS_SWAP */
201 "\x02" "Version 6", /* BSD_FS_V6 */
202 "\x03" "Version 7", /* BSD_FS_V7 */
203 "\x04" "System V", /* BSD_FS_SYSV */
204 "\x05" "4.1BSD", /* BSD_FS_V71K */
205 "\x06" "Eighth Edition", /* BSD_FS_V8 */
206 "\x07" "4.2BSD", /* BSD_FS_BSDFFS */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000207#ifdef __alpha__
Denis Vlasenkobd852072007-03-19 14:43:38 +0000208 "\x08" "ext2", /* BSD_FS_EXT2 */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000209#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000210 "\x08" "MS-DOS", /* BSD_FS_MSDOS */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000211#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000212 "\x09" "4.4LFS", /* BSD_FS_BSDLFS */
213 "\x0a" "unknown", /* BSD_FS_OTHER */
214 "\x0b" "HPFS", /* BSD_FS_HPFS */
215 "\x0c" "ISO-9660", /* BSD_FS_ISO9660 */
216 "\x0d" "boot", /* BSD_FS_BOOT */
217 "\x0e" "ADOS", /* BSD_FS_ADOS */
218 "\x0f" "HFS", /* BSD_FS_HFS */
219 "\x10" "AdvFS", /* BSD_FS_ADVFS */
220 NULL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000221};
222#define BSD_FSMAXTYPES (SIZE(xbsd_fstypes)-1)
223
224
225/*
226 * flags shared by various drives:
227 */
228#define BSD_D_REMOVABLE 0x01 /* removable media */
229#define BSD_D_ECC 0x02 /* supports ECC */
230#define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
231#define BSD_D_RAMDISK 0x08 /* disk emulator */
232#define BSD_D_CHAIN 0x10 /* can do back-back transfers */
233#define BSD_D_DOSPART 0x20 /* within MSDOS partition */
234
235/*
236 Changes:
237 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
238
239 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
240 support for OSF/1 disklabels on Alpha.
241 Also fixed unaligned accesses in alpha_bootblock_checksum()
242*/
243
244static int possibly_osf_label;
245
246#define FREEBSD_PARTITION 0xa5
247#define NETBSD_PARTITION 0xa9
248
249static void xbsd_delete_part(void);
250static void xbsd_new_part(void);
251static void xbsd_write_disklabel(void);
252static int xbsd_create_disklabel(void);
253static void xbsd_edit_disklabel(void);
254static void xbsd_write_bootstrap(void);
255static void xbsd_change_fstype(void);
256static int xbsd_get_part_index(int max);
257static int xbsd_check_new_partition(int *i);
258static void xbsd_list_types(void);
Denis Vlasenko28703012006-12-19 20:32:02 +0000259static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000260static int xbsd_initlabel(struct partition *p);
261static int xbsd_readlabel(struct partition *p);
262static int xbsd_writelabel(struct partition *p);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000263
Denis Vlasenko2a856762007-01-02 16:45:05 +0000264#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000265static void alpha_bootblock_checksum(char *boot);
266#endif
267
Denis Vlasenko2a856762007-01-02 16:45:05 +0000268#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000269static int xbsd_translate_fstype(int linux_type);
270static void xbsd_link_part(void);
271static struct partition *xbsd_part;
272static int xbsd_part_index;
273#endif
274
Denis Vlasenkobd852072007-03-19 14:43:38 +0000275
276/* Group big globals data and allocate it in one go */
277struct bsd_globals {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000278/* We access this through a uint64_t * when checksumming */
Denis Vlasenkod5470832007-01-03 02:58:54 +0000279/* hopefully xmalloc gives us required alignment */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000280 char disklabelbuffer[BSD_BBSIZE];
281 struct xbsd_disklabel xbsd_dlabel;
282};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000283
Denis Vlasenkobd852072007-03-19 14:43:38 +0000284static struct bsd_globals *bsd_globals_ptr;
285
286#define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000287#define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000288
289
290/* Code */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000291
292#define bsd_cround(n) \
293 (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
294
295/*
296 * Test whether the whole disk has BSD disk label magic.
297 *
298 * Note: often reformatting with DOS-type label leaves the BSD magic,
299 * so this does not mean that there is a BSD disk label.
300 */
301static int
302check_osf_label(void)
303{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000304 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000305 return 0;
306 return 1;
307}
308
309static int
Denis Vlasenkod5470832007-01-03 02:58:54 +0000310bsd_trydev(const char * dev)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000311{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000312 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000313 return -1;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000314 printf("\nBSD label for device: %s\n", dev);
Denis Vlasenko2a856762007-01-02 16:45:05 +0000315 xbsd_print_disklabel(0);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000316 return 0;
317}
318
319static void
Denis Vlasenkod5470832007-01-03 02:58:54 +0000320bsd_menu(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000321{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000322 puts("Command Action");
323 puts("d\tdelete a BSD partition");
324 puts("e\tedit drive data");
325 puts("i\tinstall bootstrap");
326 puts("l\tlist known filesystem types");
327 puts("n\tadd a new BSD partition");
328 puts("p\tprint BSD partition table");
329 puts("q\tquit without saving changes");
330 puts("r\treturn to main menu");
331 puts("s\tshow complete disklabel");
332 puts("t\tchange a partition's filesystem id");
333 puts("u\tchange units (cylinders/sectors)");
334 puts("w\twrite disklabel to disk");
Denis Vlasenko2a856762007-01-02 16:45:05 +0000335#if !defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000336 puts("x\tlink BSD partition to non-BSD partition");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000337#endif
338}
339
Denis Vlasenko2a856762007-01-02 16:45:05 +0000340#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000341static int
342hidden(int type)
343{
344 return type ^ 0x10;
345}
346
347static int
348is_bsd_partition_type(int type)
349{
350 return (type == FREEBSD_PARTITION ||
351 type == hidden(FREEBSD_PARTITION) ||
352 type == NETBSD_PARTITION ||
353 type == hidden(NETBSD_PARTITION));
354}
355#endif
356
357static void
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000358bsd_select(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000359{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000360#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000361 int t, ss;
362 struct partition *p;
363
364 for (t = 0; t < 4; t++) {
365 p = get_part_table(t);
366 if (p && is_bsd_partition_type(p->sys_ind)) {
367 xbsd_part = p;
368 xbsd_part_index = t;
369 ss = get_start_sect(xbsd_part);
370 if (ss == 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000371 printf("Partition %s has invalid starting sector 0\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000372 partname(disk_device, t+1, 0));
373 return;
374 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000375 printf("Reading disklabel of %s at sector %d\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000376 partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000377 if (xbsd_readlabel(xbsd_part) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000378 if (xbsd_create_disklabel() == 0)
379 return;
380 break;
381 }
382 }
383
384 if (t == 4) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000385 printf("There is no *BSD partition on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000386 return;
387 }
388
Denis Vlasenko2a856762007-01-02 16:45:05 +0000389#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000390
Denis Vlasenkobd852072007-03-19 14:43:38 +0000391 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000392 if (xbsd_create_disklabel() == 0)
Denis Vlasenko2a856762007-01-02 16:45:05 +0000393 exit(EXIT_SUCCESS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000394
395#endif
396
397 while (1) {
398 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +0000399 switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000400 case 'd':
401 xbsd_delete_part();
402 break;
403 case 'e':
404 xbsd_edit_disklabel();
405 break;
406 case 'i':
407 xbsd_write_bootstrap();
408 break;
409 case 'l':
410 xbsd_list_types();
411 break;
412 case 'n':
413 xbsd_new_part();
414 break;
415 case 'p':
416 xbsd_print_disklabel(0);
417 break;
418 case 'q':
419 close(fd);
420 exit(EXIT_SUCCESS);
421 case 'r':
422 return;
423 case 's':
424 xbsd_print_disklabel(1);
425 break;
426 case 't':
427 xbsd_change_fstype();
428 break;
429 case 'u':
430 change_units();
431 break;
432 case 'w':
433 xbsd_write_disklabel();
434 break;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000435#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000436 case 'x':
437 xbsd_link_part();
438 break;
439#endif
440 default:
Denis Vlasenkod5470832007-01-03 02:58:54 +0000441 bsd_menu();
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000442 break;
443 }
444 }
445}
446
447static void
448xbsd_delete_part(void)
449{
450 int i;
451
452 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
453 xbsd_dlabel.d_partitions[i].p_size = 0;
454 xbsd_dlabel.d_partitions[i].p_offset = 0;
455 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
456 if (xbsd_dlabel.d_npartitions == i + 1)
457 while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
458 xbsd_dlabel.d_npartitions--;
459}
460
461static void
462xbsd_new_part(void)
463{
464 off_t begin, end;
465 char mesg[256];
466 int i;
467
468 if (!xbsd_check_new_partition(&i))
469 return;
470
Denis Vlasenko2a856762007-01-02 16:45:05 +0000471#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000472 begin = get_start_sect(xbsd_part);
473 end = begin + get_nr_sects(xbsd_part) - 1;
474#else
475 begin = 0;
476 end = xbsd_dlabel.d_secperunit - 1;
477#endif
478
Denis Vlasenkobd852072007-03-19 14:43:38 +0000479 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000480 begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
481 0, mesg);
482
483 if (display_in_cyl_units)
484 begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
485
Denis Vlasenkobd852072007-03-19 14:43:38 +0000486 snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000487 str_units(SINGULAR));
Denis Vlasenko2a856762007-01-02 16:45:05 +0000488 end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
489 bsd_cround(begin), mesg);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000490
491 if (display_in_cyl_units)
492 end = end * xbsd_dlabel.d_secpercyl - 1;
493
494 xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
495 xbsd_dlabel.d_partitions[i].p_offset = begin;
496 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
497}
498
499static void
500xbsd_print_disklabel(int show_all)
501{
502 struct xbsd_disklabel *lp = &xbsd_dlabel;
503 struct xbsd_partition *pp;
504 int i, j;
505
506 if (show_all) {
Denis Vlasenko2a856762007-01-02 16:45:05 +0000507#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000508 printf("# %s:\n", disk_device);
509#else
510 printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
511#endif
512 if ((unsigned) lp->d_type < BSD_DKMAXTYPES)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000513 printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000514 else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000515 printf("type: %d\n", lp->d_type);
516 printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
517 printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
518 printf("flags:");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000519 if (lp->d_flags & BSD_D_REMOVABLE)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000520 printf(" removable");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000521 if (lp->d_flags & BSD_D_ECC)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000522 printf(" ecc");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000523 if (lp->d_flags & BSD_D_BADSECT)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000524 printf(" badsect");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000525 puts("");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000526 /* On various machines the fields of *lp are short/int/long */
527 /* In order to avoid problems, we cast them all to long. */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000528 printf("bytes/sector: %ld\n", (long) lp->d_secsize);
529 printf("sectors/track: %ld\n", (long) lp->d_nsectors);
530 printf("tracks/cylinder: %ld\n", (long) lp->d_ntracks);
531 printf("sectors/cylinder: %ld\n", (long) lp->d_secpercyl);
532 printf("cylinders: %ld\n", (long) lp->d_ncylinders);
533 printf("rpm: %d\n", lp->d_rpm);
534 printf("interleave: %d\n", lp->d_interleave);
535 printf("trackskew: %d\n", lp->d_trackskew);
536 printf("cylinderskew: %d\n", lp->d_cylskew);
537 printf("headswitch: %ld\t\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000538 (long) lp->d_headswitch);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000539 printf("track-to-track seek: %ld\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000540 (long) lp->d_trkseek);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000541 printf("drivedata: ");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000542 for (i = NDDATA - 1; i >= 0; i--)
543 if (lp->d_drivedata[i])
544 break;
545 if (i < 0)
546 i = 0;
547 for (j = 0; j <= i; j++)
548 printf("%ld ", (long) lp->d_drivedata[j]);
549 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000550 printf("\n%d partitions:\n", lp->d_npartitions);
551 printf("# start end size fstype [fsize bsize cpg]\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000552 pp = lp->d_partitions;
553 for (i = 0; i < lp->d_npartitions; i++, pp++) {
554 if (pp->p_size) {
555 if (display_in_cyl_units && lp->d_secpercyl) {
556 printf(" %c: %8ld%c %8ld%c %8ld%c ",
557 'a' + i,
558 (long) pp->p_offset / lp->d_secpercyl + 1,
559 (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
560 (long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
561 ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
562 (long) pp->p_size / lp->d_secpercyl,
563 (pp->p_size % lp->d_secpercyl) ? '*' : ' '
564 );
565 } else {
566 printf(" %c: %8ld %8ld %8ld ",
567 'a' + i,
568 (long) pp->p_offset,
569 (long) pp->p_offset + pp->p_size - 1,
570 (long) pp->p_size
571 );
572 }
573
574 if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000575 printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000576 else
577 printf("%8x", pp->p_fstype);
578
579 switch (pp->p_fstype) {
580 case BSD_FS_UNUSED:
581 printf(" %5ld %5ld %5.5s ",
582 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
583 break;
584 case BSD_FS_BSDFFS:
585 printf(" %5ld %5ld %5d ",
586 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
587 break;
588 default:
589 printf("%22.22s", "");
590 break;
591 }
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000592 puts("");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000593 }
594 }
595}
596
597static void
598xbsd_write_disklabel(void)
599{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000600#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000601 printf("Writing disklabel to %s\n", disk_device);
602 xbsd_writelabel(NULL);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000603#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000604 printf("Writing disklabel to %s\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000605 partname(disk_device, xbsd_part_index + 1, 0));
Denis Vlasenkobd852072007-03-19 14:43:38 +0000606 xbsd_writelabel(xbsd_part);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000607#endif
608 reread_partition_table(0); /* no exit yet */
609}
610
611static int
612xbsd_create_disklabel(void)
613{
614 char c;
615
Denis Vlasenko2a856762007-01-02 16:45:05 +0000616#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000617 printf("%s contains no disklabel\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000618#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000619 printf("%s contains no disklabel\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000620 partname(disk_device, xbsd_part_index + 1, 0));
621#endif
622
623 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000624 c = read_nonempty("Do you want to create a disklabel? (y/n) ");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000625 if (c == 'y' || c == 'Y') {
626 if (xbsd_initlabel(
Denis Vlasenko2a856762007-01-02 16:45:05 +0000627#if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
628 defined(__s390__) || defined(__s390x__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000629 NULL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000630#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000631 xbsd_part
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000632#endif
633 ) == 1) {
Denis Vlasenko2a856762007-01-02 16:45:05 +0000634 xbsd_print_disklabel(1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000635 return 1;
636 } else
637 return 0;
638 } else if (c == 'n')
639 return 0;
640 }
641}
642
643static int
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000644edit_int(int def, const char *mesg)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000645{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000646 mesg = xasprintf("%s (%d): ", mesg, def);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000647 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000648 if (!read_line(mesg))
649 goto ret;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000650 } while (!isdigit(*line_ptr));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000651 def = atoi(line_ptr);
652 ret:
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000653 free((char*)mesg);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000654 return def;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000655}
656
657static void
658xbsd_edit_disklabel(void)
659{
660 struct xbsd_disklabel *d;
661
662 d = &xbsd_dlabel;
663
Denis Vlasenko2a856762007-01-02 16:45:05 +0000664#if defined(__alpha__) || defined(__ia64__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000665 d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
666 d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
667 d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
668 d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000669#endif
670
Denis Vlasenko28703012006-12-19 20:32:02 +0000671 /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000672 while (1) {
Denis Vlasenko28703012006-12-19 20:32:02 +0000673 d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
Denis Vlasenkobd852072007-03-19 14:43:38 +0000674 "sectors/cylinder");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000675 if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
676 break;
677
Denis Vlasenkobd852072007-03-19 14:43:38 +0000678 printf("Must be <= sectors/track * tracks/cylinder (default)\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000679 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000680 d->d_rpm = edit_int(d->d_rpm , "rpm");
681 d->d_interleave = edit_int(d->d_interleave, "interleave");
682 d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
683 d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
684 d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
685 d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000686
687 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
688}
689
690static int
Denis Vlasenko28703012006-12-19 20:32:02 +0000691xbsd_get_bootstrap(char *path, void *ptr, int size)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000692{
693 int fdb;
694
Denis Vlasenko28703012006-12-19 20:32:02 +0000695 fdb = open(path, O_RDONLY);
696 if (fdb < 0) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000697 perror(path);
698 return 0;
699 }
700 if (read(fdb, ptr, size) < 0) {
701 perror(path);
702 close(fdb);
703 return 0;
704 }
705 printf(" ... %s\n", path);
706 close(fdb);
707 return 1;
708}
709
710static void
711sync_disks(void)
712{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000713 printf("Syncing disks\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000714 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +0000715 /* sleep(4); What? */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000716}
717
718static void
719xbsd_write_bootstrap(void)
720{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000721 char path[MAXPATHLEN];
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000722 const char *bootdir = BSD_LINUX_BOOTDIR;
723 const char *dkbasename;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000724 struct xbsd_disklabel dl;
725 char *d, *p, *e;
726 int sector;
727
728 if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
729 dkbasename = "sd";
730 else
731 dkbasename = "wd";
732
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000733 snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000734 dkbasename, dkbasename, dkbasename);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000735 if (read_line(path)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000736 dkbasename = line_ptr;
737 }
738 snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
739 if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
740 return;
741
742/* We need a backup of the disklabel (xbsd_dlabel might have changed). */
743 d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
744 memmove(&dl, d, sizeof(struct xbsd_disklabel));
745
746/* The disklabel will be overwritten by 0's from bootxx anyway */
747 memset(d, 0, sizeof(struct xbsd_disklabel));
748
749 snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000750 if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
Denis Vlasenko28703012006-12-19 20:32:02 +0000751 (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000752 return;
753
754 e = d + sizeof(struct xbsd_disklabel);
755 for (p = d; p < e; p++)
756 if (*p) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000757 printf("Bootstrap overlaps with disk label!\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000758 exit(EXIT_FAILURE);
759 }
760
761 memmove(d, &dl, sizeof(struct xbsd_disklabel));
762
Denis Vlasenko28703012006-12-19 20:32:02 +0000763#if defined(__powerpc__) || defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000764 sector = 0;
Denis Vlasenko28703012006-12-19 20:32:02 +0000765#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000766 sector = 0;
767 alpha_bootblock_checksum(disklabelbuffer);
768#else
769 sector = get_start_sect(xbsd_part);
770#endif
771
772 if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
773 fdisk_fatal(unable_to_seek);
774 if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
775 fdisk_fatal(unable_to_write);
776
Denis Vlasenko28703012006-12-19 20:32:02 +0000777#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000778 printf("Bootstrap installed on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000779#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000780 printf("Bootstrap installed on %s\n",
Denis Vlasenko28703012006-12-19 20:32:02 +0000781 partname(disk_device, xbsd_part_index+1, 0));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000782#endif
783
784 sync_disks();
785}
786
787static void
788xbsd_change_fstype(void)
789{
790 int i;
791
792 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
793 xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
794}
795
796static int
797xbsd_get_part_index(int max)
798{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000799 char prompt[sizeof("Partition (a-%c): ") + 16];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000800 char l;
801
Denis Vlasenkobd852072007-03-19 14:43:38 +0000802 snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000803 do
804 l = tolower(read_nonempty(prompt));
805 while (l < 'a' || l > 'a' + max - 1);
806 return l - 'a';
807}
808
809static int
810xbsd_check_new_partition(int *i)
811{
812 /* room for more? various BSD flavours have different maxima */
813 if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
814 int t;
815
816 for (t = 0; t < BSD_MAXPARTITIONS; t++)
817 if (xbsd_dlabel.d_partitions[t].p_size == 0)
818 break;
819
820 if (t == BSD_MAXPARTITIONS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000821 printf("The maximum number of partitions has been created\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000822 return 0;
823 }
824 }
825
Denis Vlasenko28703012006-12-19 20:32:02 +0000826 *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000827
828 if (*i >= xbsd_dlabel.d_npartitions)
829 xbsd_dlabel.d_npartitions = (*i) + 1;
830
831 if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832 printf("This partition already exists\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000833 return 0;
834 }
835
836 return 1;
837}
838
839static void
840xbsd_list_types(void)
841{
842 list_types(xbsd_fstypes);
843}
844
Denis Vlasenko28703012006-12-19 20:32:02 +0000845static uint16_t
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000846xbsd_dkcksum(struct xbsd_disklabel *lp)
847{
Denis Vlasenko28703012006-12-19 20:32:02 +0000848 uint16_t *start, *end;
849 uint16_t sum = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000850
Denis Vlasenko28703012006-12-19 20:32:02 +0000851 start = (uint16_t *) lp;
852 end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000853 while (start < end)
854 sum ^= *start++;
855 return sum;
856}
857
858static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000859xbsd_initlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000860{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000861 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000862 struct xbsd_partition *pp;
863
864 get_geometry();
865 memset(d, 0, sizeof(struct xbsd_disklabel));
866
867 d->d_magic = BSD_DISKMAGIC;
868
869 if (strncmp(disk_device, "/dev/sd", 7) == 0)
870 d->d_type = BSD_DTYPE_SCSI;
871 else
872 d->d_type = BSD_DTYPE_ST506;
873
Denis Vlasenko2a856762007-01-02 16:45:05 +0000874#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000875 d->d_flags = BSD_D_DOSPART;
876#else
877 d->d_flags = 0;
878#endif
879 d->d_secsize = SECTOR_SIZE; /* bytes/sector */
Denis Vlasenko2a856762007-01-02 16:45:05 +0000880 d->d_nsectors = sectors; /* sectors/track */
881 d->d_ntracks = heads; /* tracks/cylinder (heads) */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000882 d->d_ncylinders = cylinders;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000883 d->d_secpercyl = sectors * heads; /* sectors/cylinder */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000884 if (d->d_secpercyl == 0)
885 d->d_secpercyl = 1; /* avoid segfaults */
886 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
887
888 d->d_rpm = 3600;
889 d->d_interleave = 1;
890 d->d_trackskew = 0;
891 d->d_cylskew = 0;
892 d->d_headswitch = 0;
893 d->d_trkseek = 0;
894
895 d->d_magic2 = BSD_DISKMAGIC;
896 d->d_bbsize = BSD_BBSIZE;
897 d->d_sbsize = BSD_SBSIZE;
898
Denis Vlasenko2a856762007-01-02 16:45:05 +0000899#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000900 d->d_npartitions = 4;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000901 pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
902
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000903 pp->p_offset = get_start_sect(p);
904 pp->p_size = get_nr_sects(p);
905 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000906 pp = &d->d_partitions[3]; /* Partition D should be whole disk */
907
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000908 pp->p_offset = 0;
909 pp->p_size = d->d_secperunit;
910 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000911#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000912 d->d_npartitions = 3;
913 pp = &d->d_partitions[2]; /* Partition C should be
914 the whole disk */
915 pp->p_offset = 0;
916 pp->p_size = d->d_secperunit;
917 pp->p_fstype = BSD_FS_UNUSED;
918#endif
919
920 return 1;
921}
922
923/*
924 * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
925 * If it has the right magic, return 1.
926 */
927static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000928xbsd_readlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000929{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000930 struct xbsd_disklabel *d;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000931 int t, sector;
932
Denis Vlasenkobd852072007-03-19 14:43:38 +0000933 if (!bsd_globals_ptr)
934 bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
935
936 d = &xbsd_dlabel;
Denis Vlasenkod5470832007-01-03 02:58:54 +0000937
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000938 /* p is used only to get the starting sector */
Denis Vlasenko2a856762007-01-02 16:45:05 +0000939#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000940 sector = (p ? get_start_sect(p) : 0);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000941#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000942 sector = 0;
943#endif
944
945 if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
946 fdisk_fatal(unable_to_seek);
947 if (BSD_BBSIZE != read(fd, disklabelbuffer, BSD_BBSIZE))
948 fdisk_fatal(unable_to_read);
949
950 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
951 sizeof(struct xbsd_disklabel));
952
953 if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
954 return 0;
955
956 for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
957 d->d_partitions[t].p_size = 0;
958 d->d_partitions[t].p_offset = 0;
959 d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
960 }
961
962 if (d->d_npartitions > BSD_MAXPARTITIONS)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000963 printf("Warning: too many partitions (%d, maximum is %d)\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000964 d->d_npartitions, BSD_MAXPARTITIONS);
965 return 1;
966}
967
968static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000969xbsd_writelabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000970{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000971 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000972 unsigned int sector;
973
Denis Vlasenko2a856762007-01-02 16:45:05 +0000974#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000975 sector = get_start_sect(p) + BSD_LABELSECTOR;
976#else
977 sector = BSD_LABELSECTOR;
978#endif
979
980 d->d_checksum = 0;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000981 d->d_checksum = xbsd_dkcksum(d);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000982
983 /* This is necessary if we want to write the bootstrap later,
984 otherwise we'd write the old disklabel with the bootstrap.
985 */
986 memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
987 d, sizeof(struct xbsd_disklabel));
988
Denis Vlasenko2a856762007-01-02 16:45:05 +0000989#if defined(__alpha__) && BSD_LABELSECTOR == 0
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000990 alpha_bootblock_checksum(disklabelbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000991 if (lseek(fd, 0, SEEK_SET) == -1)
992 fdisk_fatal(unable_to_seek);
993 if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
994 fdisk_fatal(unable_to_write);
995#else
996 if (lseek(fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1)
997 fdisk_fatal(unable_to_seek);
998 if (sizeof(struct xbsd_disklabel) != write(fd, d, sizeof(struct xbsd_disklabel)))
999 fdisk_fatal(unable_to_write);
1000#endif
1001 sync_disks();
1002 return 1;
1003}
1004
1005
Denis Vlasenko2a856762007-01-02 16:45:05 +00001006#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001007static int
1008xbsd_translate_fstype(int linux_type)
1009{
1010 switch (linux_type) {
1011 case 0x01: /* DOS 12-bit FAT */
1012 case 0x04: /* DOS 16-bit <32M */
1013 case 0x06: /* DOS 16-bit >=32M */
1014 case 0xe1: /* DOS access */
1015 case 0xe3: /* DOS R/O */
1016 case 0xf2: /* DOS secondary */
1017 return BSD_FS_MSDOS;
1018 case 0x07: /* OS/2 HPFS */
1019 return BSD_FS_HPFS;
1020 default:
1021 return BSD_FS_OTHER;
1022 }
1023}
1024
1025static void
1026xbsd_link_part(void)
1027{
1028 int k, i;
1029 struct partition *p;
1030
1031 k = get_partition(1, partitions);
1032
1033 if (!xbsd_check_new_partition(&i))
1034 return;
1035
1036 p = get_part_table(k);
1037
1038 xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
1039 xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
1040 xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
1041}
1042#endif
1043
Denis Vlasenko2a856762007-01-02 16:45:05 +00001044#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001045static void
1046alpha_bootblock_checksum(char *boot)
1047{
1048 uint64_t *dp, sum;
1049 int i;
1050
1051 dp = (uint64_t *)boot;
1052 sum = 0;
1053 for (i = 0; i < 63; i++)
1054 sum += dp[i];
1055 dp[63] = sum;
1056}
1057#endif /* __alpha__ */
1058
Denis Vlasenkobd852072007-03-19 14:43:38 +00001059/* Undefine 'global' tricks */
1060#undef disklabelbuffer
1061#undef xbsd_dlabel
1062
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001063#endif /* OSF_LABEL */