blob: 65e6bd7c4f5a2085aa5703ad630c3977eb57827f [file] [log] [blame]
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001/*
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
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000034#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000035
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
Denys Vlasenko625cfd82011-01-31 05:55:52 +010046#if defined(__alpha__) \
47 || defined(__powerpc__) \
48 || defined(__ia64__) \
Denis Vlasenko2a856762007-01-02 16:45:05 +000049 || defined(__hppa__)
Denys Vlasenkoe5317592009-08-10 00:08:35 +020050# define BSD_LABELSECTOR 0
51# define BSD_LABELOFFSET 64
Denis Vlasenko98ae2162006-10-12 19:30:44 +000052#else
Denys Vlasenko625cfd82011-01-31 05:55:52 +010053# define BSD_LABELSECTOR 1
54# define BSD_LABELOFFSET 0
Denis Vlasenko98ae2162006-10-12 19:30:44 +000055#endif
56
57#define BSD_BBSIZE 8192 /* size of boot area, with label */
58#define BSD_SBSIZE 8192 /* max size of fs superblock */
59
60struct xbsd_disklabel {
61 uint32_t d_magic; /* the magic number */
62 int16_t d_type; /* drive type */
63 int16_t d_subtype; /* controller/d_type specific */
64 char d_typename[16]; /* type name, e.g. "eagle" */
Denys Vlasenkoddf78502009-09-16 03:03:13 +020065 char d_packname[16]; /* pack identifier */
66 /* disk geometry: */
Denis Vlasenko98ae2162006-10-12 19:30:44 +000067 uint32_t d_secsize; /* # of bytes per sector */
68 uint32_t d_nsectors; /* # of data sectors per track */
69 uint32_t d_ntracks; /* # of tracks per cylinder */
70 uint32_t d_ncylinders; /* # of data cylinders per unit */
71 uint32_t d_secpercyl; /* # of data sectors per cylinder */
72 uint32_t d_secperunit; /* # of data sectors per unit */
73 /*
74 * Spares (bad sector replacements) below
75 * are not counted in d_nsectors or d_secpercyl.
76 * Spare sectors are assumed to be physical sectors
77 * which occupy space at the end of each track and/or cylinder.
78 */
79 uint16_t d_sparespertrack; /* # of spare sectors per track */
80 uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
81 /*
82 * Alternate cylinders include maintenance, replacement,
83 * configuration description areas, etc.
84 */
85 uint32_t d_acylinders; /* # of alt. cylinders per unit */
86
Denys Vlasenkoddf78502009-09-16 03:03:13 +020087 /* hardware characteristics: */
Denis Vlasenko98ae2162006-10-12 19:30:44 +000088 /*
89 * d_interleave, d_trackskew and d_cylskew describe perturbations
90 * in the media format used to compensate for a slow controller.
91 * Interleave is physical sector interleave, set up by the formatter
92 * or controller when formatting. When interleaving is in use,
93 * logically adjacent sectors are not physically contiguous,
94 * but instead are separated by some number of sectors.
95 * It is specified as the ratio of physical sectors traversed
96 * per logical sector. Thus an interleave of 1:1 implies contiguous
97 * layout, while 2:1 implies that logical sector 0 is separated
98 * by one sector from logical sector 1.
99 * d_trackskew is the offset of sector 0 on track N
100 * relative to sector 0 on track N-1 on the same cylinder.
101 * Finally, d_cylskew is the offset of sector 0 on cylinder N
102 * relative to sector 0 on cylinder N-1.
103 */
104 uint16_t d_rpm; /* rotational speed */
105 uint16_t d_interleave; /* hardware sector interleave */
106 uint16_t d_trackskew; /* sector 0 skew, per track */
107 uint16_t d_cylskew; /* sector 0 skew, per cylinder */
108 uint32_t d_headswitch; /* head switch time, usec */
109 uint32_t d_trkseek; /* track-to-track seek, usec */
110 uint32_t d_flags; /* generic flags */
111#define NDDATA 5
112 uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
113#define NSPARE 5
114 uint32_t d_spare[NSPARE]; /* reserved for future use */
115 uint32_t d_magic2; /* the magic number (again) */
116 uint16_t d_checksum; /* xor of data incl. partitions */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200117 /* filesystem and partition information: */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000118 uint16_t d_npartitions; /* number of partitions in following */
119 uint32_t d_bbsize; /* size of boot area at sn0, bytes */
120 uint32_t d_sbsize; /* max size of fs superblock, bytes */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200121 struct xbsd_partition { /* the partition table */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000122 uint32_t p_size; /* number of sectors in partition */
123 uint32_t p_offset; /* starting sector */
124 uint32_t p_fsize; /* filesystem basic fragment size */
125 uint8_t p_fstype; /* filesystem type, see below */
126 uint8_t p_frag; /* filesystem fragments per block */
127 uint16_t p_cpg; /* filesystem cylinders per group */
128 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
129};
130
131/* d_type values: */
132#define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */
133#define BSD_DTYPE_MSCP 2 /* MSCP */
134#define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */
135#define BSD_DTYPE_SCSI 4 /* SCSI */
136#define BSD_DTYPE_ESDI 5 /* ESDI interface */
137#define BSD_DTYPE_ST506 6 /* ST506 etc. */
138#define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */
139#define BSD_DTYPE_HPFL 8 /* HP Fiber-link */
140#define BSD_DTYPE_FLOPPY 10 /* floppy */
141
142/* d_subtype values: */
143#define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */
144#define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */
145#define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */
146
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000147static const char *const xbsd_dktypenames[] = {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000148 "unknown",
149 "SMD",
150 "MSCP",
151 "old DEC",
152 "SCSI",
153 "ESDI",
154 "ST506",
155 "HP-IB",
156 "HP-FL",
157 "type 9",
158 "floppy",
159 0
160};
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000161
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000162
163/*
164 * Filesystem type and version.
165 * Used to interpret other filesystem-specific
166 * per-partition information.
167 */
168#define BSD_FS_UNUSED 0 /* unused */
169#define BSD_FS_SWAP 1 /* swap */
170#define BSD_FS_V6 2 /* Sixth Edition */
171#define BSD_FS_V7 3 /* Seventh Edition */
172#define BSD_FS_SYSV 4 /* System V */
173#define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
174#define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */
175#define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */
176#define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */
177#define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */
178#define BSD_FS_HPFS 11 /* OS/2 high-performance file system */
179#define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */
180#define BSD_FS_ISOFS BSD_FS_ISO9660
181#define BSD_FS_BOOT 13 /* partition contains bootstrap */
182#define BSD_FS_ADOS 14 /* AmigaDOS fast file system */
183#define BSD_FS_HFS 15 /* Macintosh HFS */
184#define BSD_FS_ADVFS 16 /* Digital Unix AdvFS */
185
186/* this is annoying, but it's also the way it is :-( */
187#ifdef __alpha__
188#define BSD_FS_EXT2 8 /* ext2 file system */
189#else
190#define BSD_FS_MSDOS 8 /* MS-DOS file system */
191#endif
192
Denis Vlasenkobd852072007-03-19 14:43:38 +0000193static const char *const xbsd_fstypes[] = {
194 "\x00" "unused", /* BSD_FS_UNUSED */
195 "\x01" "swap", /* BSD_FS_SWAP */
196 "\x02" "Version 6", /* BSD_FS_V6 */
197 "\x03" "Version 7", /* BSD_FS_V7 */
198 "\x04" "System V", /* BSD_FS_SYSV */
199 "\x05" "4.1BSD", /* BSD_FS_V71K */
200 "\x06" "Eighth Edition", /* BSD_FS_V8 */
201 "\x07" "4.2BSD", /* BSD_FS_BSDFFS */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000202#ifdef __alpha__
Denis Vlasenkobd852072007-03-19 14:43:38 +0000203 "\x08" "ext2", /* BSD_FS_EXT2 */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000204#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000205 "\x08" "MS-DOS", /* BSD_FS_MSDOS */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000206#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000207 "\x09" "4.4LFS", /* BSD_FS_BSDLFS */
208 "\x0a" "unknown", /* BSD_FS_OTHER */
209 "\x0b" "HPFS", /* BSD_FS_HPFS */
210 "\x0c" "ISO-9660", /* BSD_FS_ISO9660 */
211 "\x0d" "boot", /* BSD_FS_BOOT */
212 "\x0e" "ADOS", /* BSD_FS_ADOS */
213 "\x0f" "HFS", /* BSD_FS_HFS */
214 "\x10" "AdvFS", /* BSD_FS_ADVFS */
215 NULL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000216};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000217
218
219/*
220 * flags shared by various drives:
221 */
222#define BSD_D_REMOVABLE 0x01 /* removable media */
223#define BSD_D_ECC 0x02 /* supports ECC */
224#define BSD_D_BADSECT 0x04 /* supports bad sector forw. */
225#define BSD_D_RAMDISK 0x08 /* disk emulator */
226#define BSD_D_CHAIN 0x10 /* can do back-back transfers */
227#define BSD_D_DOSPART 0x20 /* within MSDOS partition */
228
229/*
230 Changes:
231 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n/nls
232
233 20000101 - David Huggins-Daines <dhuggins@linuxcare.com> - Better
234 support for OSF/1 disklabels on Alpha.
235 Also fixed unaligned accesses in alpha_bootblock_checksum()
236*/
237
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000238#define FREEBSD_PARTITION 0xa5
239#define NETBSD_PARTITION 0xa9
240
241static void xbsd_delete_part(void);
242static void xbsd_new_part(void);
243static void xbsd_write_disklabel(void);
244static int xbsd_create_disklabel(void);
245static void xbsd_edit_disklabel(void);
246static void xbsd_write_bootstrap(void);
247static void xbsd_change_fstype(void);
248static int xbsd_get_part_index(int max);
249static int xbsd_check_new_partition(int *i);
250static void xbsd_list_types(void);
Denis Vlasenko28703012006-12-19 20:32:02 +0000251static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000252static int xbsd_initlabel(struct partition *p);
253static int xbsd_readlabel(struct partition *p);
254static int xbsd_writelabel(struct partition *p);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000255
Denis Vlasenko2a856762007-01-02 16:45:05 +0000256#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000257static void alpha_bootblock_checksum(char *boot);
258#endif
259
Denis Vlasenko2a856762007-01-02 16:45:05 +0000260#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000261static int xbsd_translate_fstype(int linux_type);
262static void xbsd_link_part(void);
263static struct partition *xbsd_part;
264static int xbsd_part_index;
265#endif
266
Denis Vlasenkobd852072007-03-19 14:43:38 +0000267
268/* Group big globals data and allocate it in one go */
269struct bsd_globals {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000270/* We access this through a uint64_t * when checksumming */
Denis Vlasenkod5470832007-01-03 02:58:54 +0000271/* hopefully xmalloc gives us required alignment */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000272 char disklabelbuffer[BSD_BBSIZE];
273 struct xbsd_disklabel xbsd_dlabel;
274};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000275
Denis Vlasenkobd852072007-03-19 14:43:38 +0000276static struct bsd_globals *bsd_globals_ptr;
277
278#define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000279#define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000280
281
282/* Code */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000283
284#define bsd_cround(n) \
285 (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
286
287/*
288 * Test whether the whole disk has BSD disk label magic.
289 *
290 * Note: often reformatting with DOS-type label leaves the BSD magic,
291 * so this does not mean that there is a BSD disk label.
292 */
293static int
294check_osf_label(void)
295{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000296 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000297 return 0;
298 return 1;
299}
300
301static int
Denis Vlasenkod5470832007-01-03 02:58:54 +0000302bsd_trydev(const char * dev)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000303{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000304 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000305 return -1;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000306 printf("\nBSD label for device: %s\n", dev);
Denis Vlasenko2a856762007-01-02 16:45:05 +0000307 xbsd_print_disklabel(0);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000308 return 0;
309}
310
311static void
Denis Vlasenkod5470832007-01-03 02:58:54 +0000312bsd_menu(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000313{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000314 puts("Command Action");
315 puts("d\tdelete a BSD partition");
316 puts("e\tedit drive data");
317 puts("i\tinstall bootstrap");
318 puts("l\tlist known filesystem types");
319 puts("n\tadd a new BSD partition");
320 puts("p\tprint BSD partition table");
321 puts("q\tquit without saving changes");
322 puts("r\treturn to main menu");
323 puts("s\tshow complete disklabel");
324 puts("t\tchange a partition's filesystem id");
325 puts("u\tchange units (cylinders/sectors)");
326 puts("w\twrite disklabel to disk");
Denis Vlasenko2a856762007-01-02 16:45:05 +0000327#if !defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000328 puts("x\tlink BSD partition to non-BSD partition");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000329#endif
330}
331
Denis Vlasenko2a856762007-01-02 16:45:05 +0000332#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000333static int
334hidden(int type)
335{
336 return type ^ 0x10;
337}
338
339static int
340is_bsd_partition_type(int type)
341{
342 return (type == FREEBSD_PARTITION ||
343 type == hidden(FREEBSD_PARTITION) ||
344 type == NETBSD_PARTITION ||
345 type == hidden(NETBSD_PARTITION));
346}
347#endif
348
349static void
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000350bsd_select(void)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000351{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000352#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000353 int t, ss;
354 struct partition *p;
355
356 for (t = 0; t < 4; t++) {
357 p = get_part_table(t);
358 if (p && is_bsd_partition_type(p->sys_ind)) {
359 xbsd_part = p;
360 xbsd_part_index = t;
361 ss = get_start_sect(xbsd_part);
362 if (ss == 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000363 printf("Partition %s has invalid starting sector 0\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000364 partname(disk_device, t+1, 0));
365 return;
366 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200367 printf("Reading disklabel of %s at sector %u\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000368 partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000369 if (xbsd_readlabel(xbsd_part) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000370 if (xbsd_create_disklabel() == 0)
371 return;
372 break;
373 }
374 }
375
376 if (t == 4) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000377 printf("There is no *BSD partition on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000378 return;
379 }
380
Denis Vlasenko2a856762007-01-02 16:45:05 +0000381#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000382
Denis Vlasenkobd852072007-03-19 14:43:38 +0000383 if (xbsd_readlabel(NULL) == 0)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000384 if (xbsd_create_disklabel() == 0)
Denis Vlasenko2a856762007-01-02 16:45:05 +0000385 exit(EXIT_SUCCESS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000386
387#endif
388
389 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000390 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +0000391 switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000392 case 'd':
393 xbsd_delete_part();
394 break;
395 case 'e':
396 xbsd_edit_disklabel();
397 break;
398 case 'i':
399 xbsd_write_bootstrap();
400 break;
401 case 'l':
402 xbsd_list_types();
403 break;
404 case 'n':
405 xbsd_new_part();
406 break;
407 case 'p':
408 xbsd_print_disklabel(0);
409 break;
410 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +0000411 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000412 close_dev_fd();
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000413 exit(EXIT_SUCCESS);
414 case 'r':
415 return;
416 case 's':
417 xbsd_print_disklabel(1);
418 break;
419 case 't':
420 xbsd_change_fstype();
421 break;
422 case 'u':
423 change_units();
424 break;
425 case 'w':
426 xbsd_write_disklabel();
427 break;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000428#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000429 case 'x':
430 xbsd_link_part();
431 break;
432#endif
433 default:
Denis Vlasenkod5470832007-01-03 02:58:54 +0000434 bsd_menu();
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000435 break;
436 }
437 }
438}
439
440static void
441xbsd_delete_part(void)
442{
443 int i;
444
445 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
446 xbsd_dlabel.d_partitions[i].p_size = 0;
447 xbsd_dlabel.d_partitions[i].p_offset = 0;
448 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
449 if (xbsd_dlabel.d_npartitions == i + 1)
450 while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
451 xbsd_dlabel.d_npartitions--;
452}
453
454static void
455xbsd_new_part(void)
456{
457 off_t begin, end;
458 char mesg[256];
459 int i;
460
461 if (!xbsd_check_new_partition(&i))
462 return;
463
Denis Vlasenko2a856762007-01-02 16:45:05 +0000464#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000465 begin = get_start_sect(xbsd_part);
466 end = begin + get_nr_sects(xbsd_part) - 1;
467#else
468 begin = 0;
469 end = xbsd_dlabel.d_secperunit - 1;
470#endif
471
Denis Vlasenkobd852072007-03-19 14:43:38 +0000472 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000473 begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
474 0, mesg);
475
476 if (display_in_cyl_units)
477 begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
478
Denis Vlasenkobd852072007-03-19 14:43:38 +0000479 snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000480 str_units(SINGULAR));
Denis Vlasenko2a856762007-01-02 16:45:05 +0000481 end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
482 bsd_cround(begin), mesg);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000483
484 if (display_in_cyl_units)
485 end = end * xbsd_dlabel.d_secpercyl - 1;
486
487 xbsd_dlabel.d_partitions[i].p_size = end - begin + 1;
488 xbsd_dlabel.d_partitions[i].p_offset = begin;
489 xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
490}
491
492static void
493xbsd_print_disklabel(int show_all)
494{
495 struct xbsd_disklabel *lp = &xbsd_dlabel;
496 struct xbsd_partition *pp;
497 int i, j;
498
499 if (show_all) {
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000500 static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
501
Denis Vlasenko2a856762007-01-02 16:45:05 +0000502#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000503 printf("# %s:\n", disk_device);
504#else
505 printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
506#endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000507 if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000508 printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000509 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200510 printf("type: %u\n", lp->d_type);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000511 printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
512 printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000513 printf("flags: ");
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000514 print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
Denis Vlasenko4daad902007-09-27 10:20:47 +0000515 bb_putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000516 /* On various machines the fields of *lp are short/int/long */
517 /* In order to avoid problems, we cast them all to long. */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200518 printf("bytes/sector: %lu\n", (long) lp->d_secsize);
519 printf("sectors/track: %lu\n", (long) lp->d_nsectors);
520 printf("tracks/cylinder: %lu\n", (long) lp->d_ntracks);
521 printf("sectors/cylinder: %lu\n", (long) lp->d_secpercyl);
522 printf("cylinders: %lu\n", (long) lp->d_ncylinders);
523 printf("rpm: %u\n", lp->d_rpm);
524 printf("interleave: %u\n", lp->d_interleave);
525 printf("trackskew: %u\n", lp->d_trackskew);
526 printf("cylinderskew: %u\n", lp->d_cylskew);
527 printf("headswitch: %lu\t\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000528 (long) lp->d_headswitch);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200529 printf("track-to-track seek: %lu\t# milliseconds\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000530 (long) lp->d_trkseek);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000531 printf("drivedata: ");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000532 for (i = NDDATA - 1; i >= 0; i--)
533 if (lp->d_drivedata[i])
534 break;
535 if (i < 0)
536 i = 0;
537 for (j = 0; j <= i; j++)
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200538 printf("%lu ", (long) lp->d_drivedata[j]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000539 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200540 printf("\n%u partitions:\n", lp->d_npartitions);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000541 printf("# start end size fstype [fsize bsize cpg]\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000542 pp = lp->d_partitions;
543 for (i = 0; i < lp->d_npartitions; i++, pp++) {
544 if (pp->p_size) {
545 if (display_in_cyl_units && lp->d_secpercyl) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200546 printf(" %c: %8lu%c %8lu%c %8lu%c ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000547 'a' + i,
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200548 (unsigned long) pp->p_offset / lp->d_secpercyl + 1,
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000549 (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200550 (unsigned long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000551 ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
552 (long) pp->p_size / lp->d_secpercyl,
553 (pp->p_size % lp->d_secpercyl) ? '*' : ' '
554 );
555 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200556 printf(" %c: %8lu %8lu %8lu ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000557 'a' + i,
558 (long) pp->p_offset,
559 (long) pp->p_offset + pp->p_size - 1,
560 (long) pp->p_size
561 );
562 }
563
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000564 if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000565 printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000566 else
567 printf("%8x", pp->p_fstype);
568
569 switch (pp->p_fstype) {
570 case BSD_FS_UNUSED:
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200571 printf(" %5lu %5lu %5.5s ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000572 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
573 break;
574 case BSD_FS_BSDFFS:
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200575 printf(" %5lu %5lu %5u ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000576 (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
577 break;
578 default:
579 printf("%22.22s", "");
580 break;
581 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000582 bb_putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000583 }
584 }
585}
586
587static void
588xbsd_write_disklabel(void)
589{
Denis Vlasenko2a856762007-01-02 16:45:05 +0000590#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000591 printf("Writing disklabel to %s\n", disk_device);
592 xbsd_writelabel(NULL);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000593#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000594 printf("Writing disklabel to %s\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000595 partname(disk_device, xbsd_part_index + 1, 0));
Denis Vlasenkobd852072007-03-19 14:43:38 +0000596 xbsd_writelabel(xbsd_part);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000597#endif
598 reread_partition_table(0); /* no exit yet */
599}
600
601static int
602xbsd_create_disklabel(void)
603{
604 char c;
605
Denis Vlasenko2a856762007-01-02 16:45:05 +0000606#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000607 printf("%s contains no disklabel\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000608#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000609 printf("%s contains no disklabel\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000610 partname(disk_device, xbsd_part_index + 1, 0));
611#endif
612
613 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000614 c = read_nonempty("Do you want to create a disklabel? (y/n) ");
Denys Vlasenko245a4f82009-11-07 01:31:14 +0100615 if ((c|0x20) == 'y') {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000616 if (xbsd_initlabel(
Denis Vlasenko2a856762007-01-02 16:45:05 +0000617#if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
618 defined(__s390__) || defined(__s390x__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000619 NULL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000620#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000621 xbsd_part
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000622#endif
Denis Vlasenko4437d192008-04-17 00:12:10 +0000623 ) == 1) {
Denis Vlasenko2a856762007-01-02 16:45:05 +0000624 xbsd_print_disklabel(1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000625 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +0000626 }
627 return 0;
628 }
Denys Vlasenko245a4f82009-11-07 01:31:14 +0100629 if ((c|0x20) == 'n')
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000630 return 0;
631 }
632}
633
634static int
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000635edit_int(int def, const char *mesg)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000636{
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200637 mesg = xasprintf("%s (%u): ", mesg, def);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000638 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000639 if (!read_line(mesg))
640 goto ret;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000641 } while (!isdigit(*line_ptr));
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000642 def = atoi(line_ptr);
643 ret:
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000644 free((char*)mesg);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000645 return def;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000646}
647
648static void
649xbsd_edit_disklabel(void)
650{
651 struct xbsd_disklabel *d;
652
653 d = &xbsd_dlabel;
654
Denis Vlasenko2a856762007-01-02 16:45:05 +0000655#if defined(__alpha__) || defined(__ia64__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000656 d->d_secsize = edit_int(d->d_secsize , "bytes/sector");
657 d->d_nsectors = edit_int(d->d_nsectors , "sectors/track");
658 d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder");
659 d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000660#endif
661
Denis Vlasenko28703012006-12-19 20:32:02 +0000662 /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000663 while (1) {
Denis Vlasenko28703012006-12-19 20:32:02 +0000664 d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
Denis Vlasenkobd852072007-03-19 14:43:38 +0000665 "sectors/cylinder");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000666 if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
667 break;
668
Denis Vlasenkobd852072007-03-19 14:43:38 +0000669 printf("Must be <= sectors/track * tracks/cylinder (default)\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000670 }
Denis Vlasenkobd852072007-03-19 14:43:38 +0000671 d->d_rpm = edit_int(d->d_rpm , "rpm");
672 d->d_interleave = edit_int(d->d_interleave, "interleave");
673 d->d_trackskew = edit_int(d->d_trackskew , "trackskew");
674 d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew");
675 d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
676 d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000677
678 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
679}
680
681static int
Denis Vlasenko28703012006-12-19 20:32:02 +0000682xbsd_get_bootstrap(char *path, void *ptr, int size)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000683{
684 int fdb;
685
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000686 fdb = open_or_warn(path, O_RDONLY);
Denis Vlasenko28703012006-12-19 20:32:02 +0000687 if (fdb < 0) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000688 return 0;
689 }
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000690 if (full_read(fdb, ptr, size) < 0) {
691 bb_simple_perror_msg(path);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000692 close(fdb);
693 return 0;
694 }
695 printf(" ... %s\n", path);
696 close(fdb);
697 return 1;
698}
699
700static void
701sync_disks(void)
702{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000703 printf("Syncing disks\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000704 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +0000705 /* sleep(4); What? */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000706}
707
708static void
709xbsd_write_bootstrap(void)
710{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000711 char path[MAXPATHLEN];
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000712 const char *bootdir = BSD_LINUX_BOOTDIR;
713 const char *dkbasename;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000714 struct xbsd_disklabel dl;
715 char *d, *p, *e;
716 int sector;
717
718 if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
719 dkbasename = "sd";
720 else
721 dkbasename = "wd";
722
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000723 snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000724 dkbasename, dkbasename, dkbasename);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000725 if (read_line(path)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000726 dkbasename = line_ptr;
727 }
728 snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
729 if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
730 return;
731
732/* We need a backup of the disklabel (xbsd_dlabel might have changed). */
733 d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
734 memmove(&dl, d, sizeof(struct xbsd_disklabel));
735
736/* The disklabel will be overwritten by 0's from bootxx anyway */
737 memset(d, 0, sizeof(struct xbsd_disklabel));
738
739 snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000740 if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
Denis Vlasenko28703012006-12-19 20:32:02 +0000741 (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000742 return;
743
744 e = d + sizeof(struct xbsd_disklabel);
745 for (p = d; p < e; p++)
746 if (*p) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000747 printf("Bootstrap overlaps with disk label!\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000748 exit(EXIT_FAILURE);
749 }
750
751 memmove(d, &dl, sizeof(struct xbsd_disklabel));
752
Denis Vlasenko28703012006-12-19 20:32:02 +0000753#if defined(__powerpc__) || defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000754 sector = 0;
Denis Vlasenko28703012006-12-19 20:32:02 +0000755#elif defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000756 sector = 0;
757 alpha_bootblock_checksum(disklabelbuffer);
758#else
759 sector = get_start_sect(xbsd_part);
760#endif
761
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000762 seek_sector(sector);
763 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000764
Denis Vlasenko28703012006-12-19 20:32:02 +0000765#if defined(__alpha__)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000766 printf("Bootstrap installed on %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000767#else
Denis Vlasenkobd852072007-03-19 14:43:38 +0000768 printf("Bootstrap installed on %s\n",
Denis Vlasenko28703012006-12-19 20:32:02 +0000769 partname(disk_device, xbsd_part_index+1, 0));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000770#endif
771
772 sync_disks();
773}
774
775static void
776xbsd_change_fstype(void)
777{
778 int i;
779
780 i = xbsd_get_part_index(xbsd_dlabel.d_npartitions);
781 xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes);
782}
783
784static int
785xbsd_get_part_index(int max)
786{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000787 char prompt[sizeof("Partition (a-%c): ") + 16];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000788 char l;
789
Denis Vlasenkobd852072007-03-19 14:43:38 +0000790 snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000791 do
792 l = tolower(read_nonempty(prompt));
793 while (l < 'a' || l > 'a' + max - 1);
794 return l - 'a';
795}
796
797static int
798xbsd_check_new_partition(int *i)
799{
800 /* room for more? various BSD flavours have different maxima */
801 if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {
802 int t;
803
804 for (t = 0; t < BSD_MAXPARTITIONS; t++)
805 if (xbsd_dlabel.d_partitions[t].p_size == 0)
806 break;
807
808 if (t == BSD_MAXPARTITIONS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000809 printf("The maximum number of partitions has been created\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000810 return 0;
811 }
812 }
813
Denis Vlasenko28703012006-12-19 20:32:02 +0000814 *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000815
816 if (*i >= xbsd_dlabel.d_npartitions)
817 xbsd_dlabel.d_npartitions = (*i) + 1;
818
819 if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000820 printf("This partition already exists\n");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000821 return 0;
822 }
823
824 return 1;
825}
826
827static void
828xbsd_list_types(void)
829{
830 list_types(xbsd_fstypes);
831}
832
Denis Vlasenko28703012006-12-19 20:32:02 +0000833static uint16_t
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000834xbsd_dkcksum(struct xbsd_disklabel *lp)
835{
Denis Vlasenko28703012006-12-19 20:32:02 +0000836 uint16_t *start, *end;
837 uint16_t sum = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000838
Denis Vlasenko28703012006-12-19 20:32:02 +0000839 start = (uint16_t *) lp;
840 end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000841 while (start < end)
842 sum ^= *start++;
843 return sum;
844}
845
846static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000847xbsd_initlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000848{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000849 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000850 struct xbsd_partition *pp;
851
852 get_geometry();
853 memset(d, 0, sizeof(struct xbsd_disklabel));
854
855 d->d_magic = BSD_DISKMAGIC;
856
857 if (strncmp(disk_device, "/dev/sd", 7) == 0)
858 d->d_type = BSD_DTYPE_SCSI;
859 else
860 d->d_type = BSD_DTYPE_ST506;
861
Denis Vlasenko2a856762007-01-02 16:45:05 +0000862#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000863 d->d_flags = BSD_D_DOSPART;
864#else
865 d->d_flags = 0;
866#endif
867 d->d_secsize = SECTOR_SIZE; /* bytes/sector */
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000868 d->d_nsectors = g_sectors; /* sectors/track */
869 d->d_ntracks = g_heads; /* tracks/cylinder (heads) */
870 d->d_ncylinders = g_cylinders;
871 d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000872 if (d->d_secpercyl == 0)
873 d->d_secpercyl = 1; /* avoid segfaults */
874 d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
875
876 d->d_rpm = 3600;
877 d->d_interleave = 1;
878 d->d_trackskew = 0;
879 d->d_cylskew = 0;
880 d->d_headswitch = 0;
881 d->d_trkseek = 0;
882
883 d->d_magic2 = BSD_DISKMAGIC;
884 d->d_bbsize = BSD_BBSIZE;
885 d->d_sbsize = BSD_SBSIZE;
886
Denis Vlasenko2a856762007-01-02 16:45:05 +0000887#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000888 d->d_npartitions = 4;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000889 pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
890
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000891 pp->p_offset = get_start_sect(p);
892 pp->p_size = get_nr_sects(p);
893 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000894 pp = &d->d_partitions[3]; /* Partition D should be whole disk */
895
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000896 pp->p_offset = 0;
897 pp->p_size = d->d_secperunit;
898 pp->p_fstype = BSD_FS_UNUSED;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000899#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000900 d->d_npartitions = 3;
901 pp = &d->d_partitions[2]; /* Partition C should be
902 the whole disk */
903 pp->p_offset = 0;
904 pp->p_size = d->d_secperunit;
905 pp->p_fstype = BSD_FS_UNUSED;
906#endif
907
908 return 1;
909}
910
911/*
912 * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
913 * If it has the right magic, return 1.
914 */
915static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000916xbsd_readlabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000917{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000918 struct xbsd_disklabel *d;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000919 int t, sector;
920
Denis Vlasenkobd852072007-03-19 14:43:38 +0000921 if (!bsd_globals_ptr)
922 bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
923
924 d = &xbsd_dlabel;
Denis Vlasenkod5470832007-01-03 02:58:54 +0000925
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000926 /* p is used only to get the starting sector */
Denis Vlasenko2a856762007-01-02 16:45:05 +0000927#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000928 sector = (p ? get_start_sect(p) : 0);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000929#else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000930 sector = 0;
931#endif
932
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000933 seek_sector(sector);
934 if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000935 fdisk_fatal(unable_to_read);
936
937 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
938 sizeof(struct xbsd_disklabel));
939
940 if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
941 return 0;
942
943 for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
944 d->d_partitions[t].p_size = 0;
945 d->d_partitions[t].p_offset = 0;
946 d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
947 }
948
949 if (d->d_npartitions > BSD_MAXPARTITIONS)
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200950 printf("Warning: too many partitions (%u, maximum is %u)\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000951 d->d_npartitions, BSD_MAXPARTITIONS);
952 return 1;
953}
954
955static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000956xbsd_writelabel(struct partition *p)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000957{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000958 struct xbsd_disklabel *d = &xbsd_dlabel;
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000959 unsigned int sector;
960
Denis Vlasenko2a856762007-01-02 16:45:05 +0000961#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000962 sector = get_start_sect(p) + BSD_LABELSECTOR;
963#else
Denys Vlasenko245a4f82009-11-07 01:31:14 +0100964 (void)p; /* silence warning */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000965 sector = BSD_LABELSECTOR;
966#endif
967
968 d->d_checksum = 0;
Denis Vlasenko2a856762007-01-02 16:45:05 +0000969 d->d_checksum = xbsd_dkcksum(d);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000970
971 /* This is necessary if we want to write the bootstrap later,
972 otherwise we'd write the old disklabel with the bootstrap.
973 */
974 memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
975 d, sizeof(struct xbsd_disklabel));
976
Denis Vlasenko2a856762007-01-02 16:45:05 +0000977#if defined(__alpha__) && BSD_LABELSECTOR == 0
Denis Vlasenko93f6aa62006-11-30 21:11:01 +0000978 alpha_bootblock_checksum(disklabelbuffer);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000979 seek_sector(0);
980 xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000981#else
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000982 seek_sector(sector);
983 lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
984 xwrite(dev_fd, d, sizeof(*d));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000985#endif
986 sync_disks();
987 return 1;
988}
989
990
Denis Vlasenko2a856762007-01-02 16:45:05 +0000991#if !defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000992static int
993xbsd_translate_fstype(int linux_type)
994{
995 switch (linux_type) {
996 case 0x01: /* DOS 12-bit FAT */
997 case 0x04: /* DOS 16-bit <32M */
998 case 0x06: /* DOS 16-bit >=32M */
999 case 0xe1: /* DOS access */
1000 case 0xe3: /* DOS R/O */
1001 case 0xf2: /* DOS secondary */
1002 return BSD_FS_MSDOS;
1003 case 0x07: /* OS/2 HPFS */
1004 return BSD_FS_HPFS;
1005 default:
1006 return BSD_FS_OTHER;
1007 }
1008}
1009
1010static void
1011xbsd_link_part(void)
1012{
1013 int k, i;
1014 struct partition *p;
1015
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001016 k = get_partition(1, g_partitions);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001017
1018 if (!xbsd_check_new_partition(&i))
1019 return;
1020
1021 p = get_part_table(k);
1022
1023 xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p);
1024 xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);
1025 xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);
1026}
1027#endif
1028
Denis Vlasenko2a856762007-01-02 16:45:05 +00001029#if defined(__alpha__)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001030static void
1031alpha_bootblock_checksum(char *boot)
1032{
1033 uint64_t *dp, sum;
1034 int i;
1035
1036 dp = (uint64_t *)boot;
1037 sum = 0;
1038 for (i = 0; i < 63; i++)
1039 sum += dp[i];
1040 dp[63] = sum;
1041}
1042#endif /* __alpha__ */
1043
Denis Vlasenkobd852072007-03-19 14:43:38 +00001044/* Undefine 'global' tricks */
1045#undef disklabelbuffer
1046#undef xbsd_dlabel
1047
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001048#endif /* OSF_LABEL */