blob: c50ee9b1652111432c0814a8850f3b395a695b71 [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
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000152static const char *const xbsd_dktypenames[] = {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000153 "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};
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000166
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000167
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};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000222
223
224/*
225 * flags shared by various drives:
226 */
227#define BSD_D_REMOVABLE 0x01 /* removable media */
228#define BSD_D_ECC 0x02 /* supports ECC */
229#define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
230#define BSD_D_RAMDISK 0x08 /* disk emulator */
231#define BSD_D_CHAIN 0x10 /* can do back-back transfers */
232#define BSD_D_DOSPART 0x20 /* within MSDOS partition */
233
234/*
235 Changes:
236 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
237
238 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
239 support for OSF/1 disklabels on Alpha.
240 Also fixed unaligned accesses in alpha_bootblock_checksum()
241*/
242
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000243#define FREEBSD_PARTITION 0xa5
244#define NETBSD_PARTITION 0xa9
245
246static void xbsd_delete_part(void);
247static void xbsd_new_part(void);
248static void xbsd_write_disklabel(void);
249static int xbsd_create_disklabel(void);
250static void xbsd_edit_disklabel(void);
251static void xbsd_write_bootstrap(void);
252static void xbsd_change_fstype(void);
253static int xbsd_get_part_index(int max);
254static int xbsd_check_new_partition(int *i);
255static void xbsd_list_types(void);
Denis Vlasenko28703012006-12-19 20:32:02 +0000256static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000257static int xbsd_initlabel(struct partition *p);
258static int xbsd_readlabel(struct partition *p);
259static int xbsd_writelabel(struct partition *p);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000260
Denis Vlasenko2a856762007-01-02 16:45:05 +0000261#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000262static void alpha_bootblock_checksum(char *boot);
263#endif
264
Denis Vlasenko2a856762007-01-02 16:45:05 +0000265#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000266static int xbsd_translate_fstype(int linux_type);
267static void xbsd_link_part(void);
268static struct partition *xbsd_part;
269static int xbsd_part_index;
270#endif
271
Denis Vlasenkobd852072007-03-19 14:43:38 +0000272
273/* Group big globals data and allocate it in one go */
274struct bsd_globals {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000275/* We access this through a uint64_t * when checksumming */
Denis Vlasenkod5470832007-01-03 02:58:54 +0000276/* hopefully xmalloc gives us required alignment */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000277 char disklabelbuffer[BSD_BBSIZE];
278 struct xbsd_disklabel xbsd_dlabel;
279};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000280
Denis Vlasenkobd852072007-03-19 14:43:38 +0000281static struct bsd_globals *bsd_globals_ptr;
282
283#define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000284#define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000285
286
287/* Code */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000288
289#define bsd_cround(n) \
290 (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
291
292/*
293 * Test whether the whole disk has BSD disk label magic.
294 *
295 * Note: often reformatting with DOS-type label leaves the BSD magic,
296 * so this does not mean that there is a BSD disk label.
297 */
298static int
299check_osf_label(void)
300{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000301 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000302 return 0;
303 return 1;
304}
305
306static int
Denis Vlasenkod5470832007-01-03 02:58:54 +0000307bsd_trydev(const char * dev)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000308{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000309 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000310 return -1;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000311 printf("\nBSD label for device: %s\n", dev);
Denis Vlasenko2a856762007-01-02 16:45:05 +0000312 xbsd_print_disklabel(0);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000313 return 0;
314}
315
316static void
Denis Vlasenkod5470832007-01-03 02:58:54 +0000317bsd_menu(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000318{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000319 puts("Command Action");
320 puts("d\tdelete a BSD partition");
321 puts("e\tedit drive data");
322 puts("i\tinstall bootstrap");
323 puts("l\tlist known filesystem types");
324 puts("n\tadd a new BSD partition");
325 puts("p\tprint BSD partition table");
326 puts("q\tquit without saving changes");
327 puts("r\treturn to main menu");
328 puts("s\tshow complete disklabel");
329 puts("t\tchange a partition's filesystem id");
330 puts("u\tchange units (cylinders/sectors)");
331 puts("w\twrite disklabel to disk");
Denis Vlasenko2a856762007-01-02 16:45:05 +0000332#if !defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000333 puts("x\tlink BSD partition to non-BSD partition");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000334#endif
335}
336
Denis Vlasenko2a856762007-01-02 16:45:05 +0000337#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000338static int
339hidden(int type)
340{
341 return type ^ 0x10;
342}
343
344static int
345is_bsd_partition_type(int type)
346{
347 return (type == FREEBSD_PARTITION ||
348 type == hidden(FREEBSD_PARTITION) ||
349 type == NETBSD_PARTITION ||
350 type == hidden(NETBSD_PARTITION));
351}
352#endif
353
354static void
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000355bsd_select(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000356{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000357#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000358 int t, ss;
359 struct partition *p;
360
361 for (t = 0; t < 4; t++) {
362 p = get_part_table(t);
363 if (p && is_bsd_partition_type(p->sys_ind)) {
364 xbsd_part = p;
365 xbsd_part_index = t;
366 ss = get_start_sect(xbsd_part);
367 if (ss == 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000368 printf("Partition %s has invalid starting sector 0\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000369 partname(disk_device, t+1, 0));
370 return;
371 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000372 printf("Reading disklabel of %s at sector %d\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000373 partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000374 if (xbsd_readlabel(xbsd_part) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000375 if (xbsd_create_disklabel() == 0)
376 return;
377 break;
378 }
379 }
380
381 if (t == 4) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000382 printf("There is no *BSD partition on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000383 return;
384 }
385
Denis Vlasenko2a856762007-01-02 16:45:05 +0000386#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000387
Denis Vlasenkobd852072007-03-19 14:43:38 +0000388 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000389 if (xbsd_create_disklabel() == 0)
Denis Vlasenko2a856762007-01-02 16:45:05 +0000390 exit(EXIT_SUCCESS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000391
392#endif
393
394 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000395 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +0000396 switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000397 case 'd':
398 xbsd_delete_part();
399 break;
400 case 'e':
401 xbsd_edit_disklabel();
402 break;
403 case 'i':
404 xbsd_write_bootstrap();
405 break;
406 case 'l':
407 xbsd_list_types();
408 break;
409 case 'n':
410 xbsd_new_part();
411 break;
412 case 'p':
413 xbsd_print_disklabel(0);
414 break;
415 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +0000416 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000417 close_dev_fd();
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000418 exit(EXIT_SUCCESS);
419 case 'r':
420 return;
421 case 's':
422 xbsd_print_disklabel(1);
423 break;
424 case 't':
425 xbsd_change_fstype();
426 break;
427 case 'u':
428 change_units();
429 break;
430 case 'w':
431 xbsd_write_disklabel();
432 break;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000433#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000434 case 'x':
435 xbsd_link_part();
436 break;
437#endif
438 default:
Denis Vlasenkod5470832007-01-03 02:58:54 +0000439 bsd_menu();
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000440 break;
441 }
442 }
443}
444
445static void
446xbsd_delete_part(void)
447{
448 int i;
449
450 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
451 xbsd_dlabel.d_partitions[i].p_size = 0;
452 xbsd_dlabel.d_partitions[i].p_offset = 0;
453 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
454 if (xbsd_dlabel.d_npartitions == i + 1)
455 while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
456 xbsd_dlabel.d_npartitions--;
457}
458
459static void
460xbsd_new_part(void)
461{
462 off_t begin, end;
463 char mesg[256];
464 int i;
465
466 if (!xbsd_check_new_partition(&i))
467 return;
468
Denis Vlasenko2a856762007-01-02 16:45:05 +0000469#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000470 begin = get_start_sect(xbsd_part);
471 end = begin + get_nr_sects(xbsd_part) - 1;
472#else
473 begin = 0;
474 end = xbsd_dlabel.d_secperunit - 1;
475#endif
476
Denis Vlasenkobd852072007-03-19 14:43:38 +0000477 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000478 begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
479 0, mesg);
480
481 if (display_in_cyl_units)
482 begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
483
Denis Vlasenkobd852072007-03-19 14:43:38 +0000484 snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000485 str_units(SINGULAR));
Denis Vlasenko2a856762007-01-02 16:45:05 +0000486 end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
487 bsd_cround(begin), mesg);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000488
489 if (display_in_cyl_units)
490 end = end * xbsd_dlabel.d_secpercyl - 1;
491
492 xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
493 xbsd_dlabel.d_partitions[i].p_offset = begin;
494 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
495}
496
497static void
498xbsd_print_disklabel(int show_all)
499{
500 struct xbsd_disklabel *lp = &xbsd_dlabel;
501 struct xbsd_partition *pp;
502 int i, j;
503
504 if (show_all) {
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000505 static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
506
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
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000512 if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
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);
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000518 printf("flags: ");
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000519 print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
Denis Vlasenko4daad902007-09-27 10:20:47 +0000520 bb_putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000521 /* On various machines the fields of *lp are short/int/long */
522 /* In order to avoid problems, we cast them all to long. */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000523 printf("bytes/sector: %ld\n", (long) lp->d_secsize);
524 printf("sectors/track: %ld\n", (long) lp->d_nsectors);
525 printf("tracks/cylinder: %ld\n", (long) lp->d_ntracks);
526 printf("sectors/cylinder: %ld\n", (long) lp->d_secpercyl);
527 printf("cylinders: %ld\n", (long) lp->d_ncylinders);
528 printf("rpm: %d\n", lp->d_rpm);
529 printf("interleave: %d\n", lp->d_interleave);
530 printf("trackskew: %d\n", lp->d_trackskew);
531 printf("cylinderskew: %d\n", lp->d_cylskew);
532 printf("headswitch: %ld\t\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000533 (long) lp->d_headswitch);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000534 printf("track-to-track seek: %ld\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000535 (long) lp->d_trkseek);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000536 printf("drivedata: ");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000537 for (i = NDDATA - 1; i >= 0; i--)
538 if (lp->d_drivedata[i])
539 break;
540 if (i < 0)
541 i = 0;
542 for (j = 0; j <= i; j++)
543 printf("%ld ", (long) lp->d_drivedata[j]);
544 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000545 printf("\n%d partitions:\n", lp->d_npartitions);
546 printf("# start end size fstype [fsize bsize cpg]\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000547 pp = lp->d_partitions;
548 for (i = 0; i < lp->d_npartitions; i++, pp++) {
549 if (pp->p_size) {
550 if (display_in_cyl_units && lp->d_secpercyl) {
551 printf(" %c: %8ld%c %8ld%c %8ld%c ",
552 'a' + i,
553 (long) pp->p_offset / lp->d_secpercyl + 1,
554 (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
555 (long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
556 ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
557 (long) pp->p_size / lp->d_secpercyl,
558 (pp->p_size % lp->d_secpercyl) ? '*' : ' '
559 );
560 } else {
561 printf(" %c: %8ld %8ld %8ld ",
562 'a' + i,
563 (long) pp->p_offset,
564 (long) pp->p_offset + pp->p_size - 1,
565 (long) pp->p_size
566 );
567 }
568
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000569 if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000570 printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000571 else
572 printf("%8x", pp->p_fstype);
573
574 switch (pp->p_fstype) {
575 case BSD_FS_UNUSED:
576 printf(" %5ld %5ld %5.5s ",
577 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
578 break;
579 case BSD_FS_BSDFFS:
580 printf(" %5ld %5ld %5d ",
581 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
582 break;
583 default:
584 printf("%22.22s", "");
585 break;
586 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000587 bb_putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000588 }
589 }
590}
591
592static void
593xbsd_write_disklabel(void)
594{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000595#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000596 printf("Writing disklabel to %s\n", disk_device);
597 xbsd_writelabel(NULL);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000598#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000599 printf("Writing disklabel to %s\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000600 partname(disk_device, xbsd_part_index + 1, 0));
Denis Vlasenkobd852072007-03-19 14:43:38 +0000601 xbsd_writelabel(xbsd_part);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000602#endif
603 reread_partition_table(0); /* no exit yet */
604}
605
606static int
607xbsd_create_disklabel(void)
608{
609 char c;
610
Denis Vlasenko2a856762007-01-02 16:45:05 +0000611#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000612 printf("%s contains no disklabel\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000613#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000614 printf("%s contains no disklabel\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000615 partname(disk_device, xbsd_part_index + 1, 0));
616#endif
617
618 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000619 c = read_nonempty("Do you want to create a disklabel? (y/n) ");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000620 if (c == 'y' || c == 'Y') {
621 if (xbsd_initlabel(
Denis Vlasenko2a856762007-01-02 16:45:05 +0000622#if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
623 defined(__s390__) || defined(__s390x__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000624 NULL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000625#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000626 xbsd_part
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000627#endif
Denis Vlasenko4437d192008-04-17 00:12:10 +0000628 ) == 1) {
Denis Vlasenko2a856762007-01-02 16:45:05 +0000629 xbsd_print_disklabel(1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000630 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +0000631 }
632 return 0;
633 }
634 if (c == 'n')
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000635 return 0;
636 }
637}
638
639static int
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000640edit_int(int def, const char *mesg)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000641{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000642 mesg = xasprintf("%s (%d): ", mesg, def);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000643 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000644 if (!read_line(mesg))
645 goto ret;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000646 } while (!isdigit(*line_ptr));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000647 def = atoi(line_ptr);
648 ret:
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000649 free((char*)mesg);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000650 return def;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000651}
652
653static void
654xbsd_edit_disklabel(void)
655{
656 struct xbsd_disklabel *d;
657
658 d = &xbsd_dlabel;
659
Denis Vlasenko2a856762007-01-02 16:45:05 +0000660#if defined(__alpha__) || defined(__ia64__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000661 d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
662 d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
663 d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
664 d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000665#endif
666
Denis Vlasenko28703012006-12-19 20:32:02 +0000667 /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000668 while (1) {
Denis Vlasenko28703012006-12-19 20:32:02 +0000669 d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
Denis Vlasenkobd852072007-03-19 14:43:38 +0000670 "sectors/cylinder");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000671 if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
672 break;
673
Denis Vlasenkobd852072007-03-19 14:43:38 +0000674 printf("Must be <= sectors/track * tracks/cylinder (default)\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000675 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000676 d->d_rpm = edit_int(d->d_rpm , "rpm");
677 d->d_interleave = edit_int(d->d_interleave, "interleave");
678 d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
679 d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
680 d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
681 d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000682
683 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
684}
685
686static int
Denis Vlasenko28703012006-12-19 20:32:02 +0000687xbsd_get_bootstrap(char *path, void *ptr, int size)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000688{
689 int fdb;
690
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000691 fdb = open_or_warn(path, O_RDONLY);
Denis Vlasenko28703012006-12-19 20:32:02 +0000692 if (fdb < 0) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000693 return 0;
694 }
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000695 if (full_read(fdb, ptr, size) < 0) {
696 bb_simple_perror_msg(path);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000697 close(fdb);
698 return 0;
699 }
700 printf(" ... %s\n", path);
701 close(fdb);
702 return 1;
703}
704
705static void
706sync_disks(void)
707{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000708 printf("Syncing disks\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000709 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +0000710 /* sleep(4); What? */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000711}
712
713static void
714xbsd_write_bootstrap(void)
715{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000716 char path[MAXPATHLEN];
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000717 const char *bootdir = BSD_LINUX_BOOTDIR;
718 const char *dkbasename;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000719 struct xbsd_disklabel dl;
720 char *d, *p, *e;
721 int sector;
722
723 if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
724 dkbasename = "sd";
725 else
726 dkbasename = "wd";
727
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000728 snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000729 dkbasename, dkbasename, dkbasename);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000730 if (read_line(path)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000731 dkbasename = line_ptr;
732 }
733 snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
734 if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
735 return;
736
737/* We need a backup of the disklabel (xbsd_dlabel might have changed). */
738 d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
739 memmove(&dl, d, sizeof(struct xbsd_disklabel));
740
741/* The disklabel will be overwritten by 0's from bootxx anyway */
742 memset(d, 0, sizeof(struct xbsd_disklabel));
743
744 snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000745 if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
Denis Vlasenko28703012006-12-19 20:32:02 +0000746 (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000747 return;
748
749 e = d + sizeof(struct xbsd_disklabel);
750 for (p = d; p < e; p++)
751 if (*p) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000752 printf("Bootstrap overlaps with disk label!\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000753 exit(EXIT_FAILURE);
754 }
755
756 memmove(d, &dl, sizeof(struct xbsd_disklabel));
757
Denis Vlasenko28703012006-12-19 20:32:02 +0000758#if defined(__powerpc__) || defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000759 sector = 0;
Denis Vlasenko28703012006-12-19 20:32:02 +0000760#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000761 sector = 0;
762 alpha_bootblock_checksum(disklabelbuffer);
763#else
764 sector = get_start_sect(xbsd_part);
765#endif
766
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000767 seek_sector(sector);
768 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000769
Denis Vlasenko28703012006-12-19 20:32:02 +0000770#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000771 printf("Bootstrap installed on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000772#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000773 printf("Bootstrap installed on %s\n",
Denis Vlasenko28703012006-12-19 20:32:02 +0000774 partname(disk_device, xbsd_part_index+1, 0));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000775#endif
776
777 sync_disks();
778}
779
780static void
781xbsd_change_fstype(void)
782{
783 int i;
784
785 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
786 xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
787}
788
789static int
790xbsd_get_part_index(int max)
791{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000792 char prompt[sizeof("Partition (a-%c): ") + 16];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000793 char l;
794
Denis Vlasenkobd852072007-03-19 14:43:38 +0000795 snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000796 do
797 l = tolower(read_nonempty(prompt));
798 while (l < 'a' || l > 'a' + max - 1);
799 return l - 'a';
800}
801
802static int
803xbsd_check_new_partition(int *i)
804{
805 /* room for more? various BSD flavours have different maxima */
806 if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
807 int t;
808
809 for (t = 0; t < BSD_MAXPARTITIONS; t++)
810 if (xbsd_dlabel.d_partitions[t].p_size == 0)
811 break;
812
813 if (t == BSD_MAXPARTITIONS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000814 printf("The maximum number of partitions has been created\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000815 return 0;
816 }
817 }
818
Denis Vlasenko28703012006-12-19 20:32:02 +0000819 *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000820
821 if (*i >= xbsd_dlabel.d_npartitions)
822 xbsd_dlabel.d_npartitions = (*i) + 1;
823
824 if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000825 printf("This partition already exists\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000826 return 0;
827 }
828
829 return 1;
830}
831
832static void
833xbsd_list_types(void)
834{
835 list_types(xbsd_fstypes);
836}
837
Denis Vlasenko28703012006-12-19 20:32:02 +0000838static uint16_t
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000839xbsd_dkcksum(struct xbsd_disklabel *lp)
840{
Denis Vlasenko28703012006-12-19 20:32:02 +0000841 uint16_t *start, *end;
842 uint16_t sum = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000843
Denis Vlasenko28703012006-12-19 20:32:02 +0000844 start = (uint16_t *) lp;
845 end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000846 while (start < end)
847 sum ^= *start++;
848 return sum;
849}
850
851static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000852xbsd_initlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000853{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000854 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000855 struct xbsd_partition *pp;
856
857 get_geometry();
858 memset(d, 0, sizeof(struct xbsd_disklabel));
859
860 d->d_magic = BSD_DISKMAGIC;
861
862 if (strncmp(disk_device, "/dev/sd", 7) == 0)
863 d->d_type = BSD_DTYPE_SCSI;
864 else
865 d->d_type = BSD_DTYPE_ST506;
866
Denis Vlasenko2a856762007-01-02 16:45:05 +0000867#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000868 d->d_flags = BSD_D_DOSPART;
869#else
870 d->d_flags = 0;
871#endif
872 d->d_secsize = SECTOR_SIZE; /* bytes/sector */
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000873 d->d_nsectors = g_sectors; /* sectors/track */
874 d->d_ntracks = g_heads; /* tracks/cylinder (heads) */
875 d->d_ncylinders = g_cylinders;
876 d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000877 if (d->d_secpercyl == 0)
878 d->d_secpercyl = 1; /* avoid segfaults */
879 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
880
881 d->d_rpm = 3600;
882 d->d_interleave = 1;
883 d->d_trackskew = 0;
884 d->d_cylskew = 0;
885 d->d_headswitch = 0;
886 d->d_trkseek = 0;
887
888 d->d_magic2 = BSD_DISKMAGIC;
889 d->d_bbsize = BSD_BBSIZE;
890 d->d_sbsize = BSD_SBSIZE;
891
Denis Vlasenko2a856762007-01-02 16:45:05 +0000892#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000893 d->d_npartitions = 4;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000894 pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
895
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000896 pp->p_offset = get_start_sect(p);
897 pp->p_size = get_nr_sects(p);
898 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000899 pp = &d->d_partitions[3]; /* Partition D should be whole disk */
900
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000901 pp->p_offset = 0;
902 pp->p_size = d->d_secperunit;
903 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000904#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000905 d->d_npartitions = 3;
906 pp = &d->d_partitions[2]; /* Partition C should be
907 the whole disk */
908 pp->p_offset = 0;
909 pp->p_size = d->d_secperunit;
910 pp->p_fstype = BSD_FS_UNUSED;
911#endif
912
913 return 1;
914}
915
916/*
917 * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
918 * If it has the right magic, return 1.
919 */
920static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000921xbsd_readlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000922{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000923 struct xbsd_disklabel *d;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000924 int t, sector;
925
Denis Vlasenkobd852072007-03-19 14:43:38 +0000926 if (!bsd_globals_ptr)
927 bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
928
929 d = &xbsd_dlabel;
Denis Vlasenkod5470832007-01-03 02:58:54 +0000930
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000931 /* p is used only to get the starting sector */
Denis Vlasenko2a856762007-01-02 16:45:05 +0000932#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000933 sector = (p ? get_start_sect(p) : 0);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000934#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000935 sector = 0;
936#endif
937
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000938 seek_sector(sector);
939 if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000940 fdisk_fatal(unable_to_read);
941
942 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
943 sizeof(struct xbsd_disklabel));
944
945 if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
946 return 0;
947
948 for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
949 d->d_partitions[t].p_size = 0;
950 d->d_partitions[t].p_offset = 0;
951 d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
952 }
953
954 if (d->d_npartitions > BSD_MAXPARTITIONS)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000955 printf("Warning: too many partitions (%d, maximum is %d)\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000956 d->d_npartitions, BSD_MAXPARTITIONS);
957 return 1;
958}
959
960static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000961xbsd_writelabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000962{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000963 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000964 unsigned int sector;
965
Denis Vlasenko2a856762007-01-02 16:45:05 +0000966#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000967 sector = get_start_sect(p) + BSD_LABELSECTOR;
968#else
969 sector = BSD_LABELSECTOR;
970#endif
971
972 d->d_checksum = 0;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000973 d->d_checksum = xbsd_dkcksum(d);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000974
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
Denis Vlasenko2a856762007-01-02 16:45:05 +0000981#if defined(__alpha__) && BSD_LABELSECTOR == 0
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000982 alpha_bootblock_checksum(disklabelbuffer);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000983 seek_sector(0);
984 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000985#else
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000986 seek_sector(sector);
987 lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
988 xwrite(dev_fd, d, sizeof(*d));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000989#endif
990 sync_disks();
991 return 1;
992}
993
994
Denis Vlasenko2a856762007-01-02 16:45:05 +0000995#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000996static 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
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001020 k = get_partition(1, g_partitions);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001021
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
Denis Vlasenko2a856762007-01-02 16:45:05 +00001033#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001034static 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
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048/* Undefine 'global' tricks */
1049#undef disklabelbuffer
1050#undef xbsd_dlabel
1051
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001052#endif /* OSF_LABEL */