Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1 | #if ENABLE_FEATURE_OSF_LABEL |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 2 | /* |
| 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 Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 46 | #if defined(i386) || defined(__sparc__) || defined(__arm__) \ |
| 47 | || defined(__m68k__) || defined(__mips__) || defined(__s390__) \ |
| 48 | || defined(__sh__) || defined(__x86_64__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 49 | #define BSD_LABELSECTOR 1 |
| 50 | #define BSD_LABELOFFSET 0 |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 51 | #elif defined(__alpha__) || defined(__powerpc__) || defined(__ia64__) \ |
| 52 | || defined(__hppa__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 53 | #define BSD_LABELSECTOR 0 |
| 54 | #define BSD_LABELOFFSET 64 |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 55 | #elif defined(__s390__) || defined(__s390x__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 56 | #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 | |
| 65 | struct 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 Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 152 | static const char *const xbsd_dktypenames[] = { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 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 | }; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 166 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 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 Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 198 | static 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 Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 207 | #ifdef __alpha__ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 208 | "\x08" "ext2", /* BSD_FS_EXT2 */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 209 | #else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 210 | "\x08" "MS-DOS", /* BSD_FS_MSDOS */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 211 | #endif |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 212 | "\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 Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 221 | }; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 222 | |
| 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 Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 243 | #define FREEBSD_PARTITION 0xa5 |
| 244 | #define NETBSD_PARTITION 0xa9 |
| 245 | |
| 246 | static void xbsd_delete_part(void); |
| 247 | static void xbsd_new_part(void); |
| 248 | static void xbsd_write_disklabel(void); |
| 249 | static int xbsd_create_disklabel(void); |
| 250 | static void xbsd_edit_disklabel(void); |
| 251 | static void xbsd_write_bootstrap(void); |
| 252 | static void xbsd_change_fstype(void); |
| 253 | static int xbsd_get_part_index(int max); |
| 254 | static int xbsd_check_new_partition(int *i); |
| 255 | static void xbsd_list_types(void); |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 256 | static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 257 | static int xbsd_initlabel(struct partition *p); |
| 258 | static int xbsd_readlabel(struct partition *p); |
| 259 | static int xbsd_writelabel(struct partition *p); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 261 | #if defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 262 | static void alpha_bootblock_checksum(char *boot); |
| 263 | #endif |
| 264 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 265 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 266 | static int xbsd_translate_fstype(int linux_type); |
| 267 | static void xbsd_link_part(void); |
| 268 | static struct partition *xbsd_part; |
| 269 | static int xbsd_part_index; |
| 270 | #endif |
| 271 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 272 | |
| 273 | /* Group big globals data and allocate it in one go */ |
| 274 | struct bsd_globals { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 275 | /* We access this through a uint64_t * when checksumming */ |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 276 | /* hopefully xmalloc gives us required alignment */ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 277 | char disklabelbuffer[BSD_BBSIZE]; |
| 278 | struct xbsd_disklabel xbsd_dlabel; |
| 279 | }; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 281 | static struct bsd_globals *bsd_globals_ptr; |
| 282 | |
| 283 | #define disklabelbuffer (bsd_globals_ptr->disklabelbuffer) |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 284 | #define xbsd_dlabel (bsd_globals_ptr->xbsd_dlabel) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 285 | |
| 286 | |
| 287 | /* Code */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 288 | |
| 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 | */ |
| 298 | static int |
| 299 | check_osf_label(void) |
| 300 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 301 | if (xbsd_readlabel(NULL) == 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | static int |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 307 | bsd_trydev(const char * dev) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 308 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 309 | if (xbsd_readlabel(NULL) == 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 310 | return -1; |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 311 | printf("\nBSD label for device: %s\n", dev); |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 312 | xbsd_print_disklabel(0); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 317 | bsd_menu(void) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 318 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 319 | 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 Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 332 | #if !defined(__alpha__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 333 | puts("x\tlink BSD partition to non-BSD partition"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 334 | #endif |
| 335 | } |
| 336 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 337 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 338 | static int |
| 339 | hidden(int type) |
| 340 | { |
| 341 | return type ^ 0x10; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | is_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 | |
| 354 | static void |
Denis Vlasenko | efeed5e | 2006-10-14 16:16:03 +0000 | [diff] [blame] | 355 | bsd_select(void) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 356 | { |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 357 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 358 | 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 Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 368 | printf("Partition %s has invalid starting sector 0\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 369 | partname(disk_device, t+1, 0)); |
| 370 | return; |
| 371 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 372 | printf("Reading disklabel of %s at sector %d\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 373 | partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 374 | if (xbsd_readlabel(xbsd_part) == 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 375 | if (xbsd_create_disklabel() == 0) |
| 376 | return; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (t == 4) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 382 | printf("There is no *BSD partition on %s\n", disk_device); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 386 | #elif defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 388 | if (xbsd_readlabel(NULL) == 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 389 | if (xbsd_create_disklabel() == 0) |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 390 | exit(EXIT_SUCCESS); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 391 | |
| 392 | #endif |
| 393 | |
| 394 | while (1) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 395 | bb_putchar('\n'); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 396 | switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 397 | 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': |
| 416 | close(fd); |
| 417 | exit(EXIT_SUCCESS); |
| 418 | case 'r': |
| 419 | return; |
| 420 | case 's': |
| 421 | xbsd_print_disklabel(1); |
| 422 | break; |
| 423 | case 't': |
| 424 | xbsd_change_fstype(); |
| 425 | break; |
| 426 | case 'u': |
| 427 | change_units(); |
| 428 | break; |
| 429 | case 'w': |
| 430 | xbsd_write_disklabel(); |
| 431 | break; |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 432 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 433 | case 'x': |
| 434 | xbsd_link_part(); |
| 435 | break; |
| 436 | #endif |
| 437 | default: |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 438 | bsd_menu(); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | static void |
| 445 | xbsd_delete_part(void) |
| 446 | { |
| 447 | int i; |
| 448 | |
| 449 | i = xbsd_get_part_index(xbsd_dlabel.d_npartitions); |
| 450 | xbsd_dlabel.d_partitions[i].p_size = 0; |
| 451 | xbsd_dlabel.d_partitions[i].p_offset = 0; |
| 452 | xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; |
| 453 | if (xbsd_dlabel.d_npartitions == i + 1) |
| 454 | while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0) |
| 455 | xbsd_dlabel.d_npartitions--; |
| 456 | } |
| 457 | |
| 458 | static void |
| 459 | xbsd_new_part(void) |
| 460 | { |
| 461 | off_t begin, end; |
| 462 | char mesg[256]; |
| 463 | int i; |
| 464 | |
| 465 | if (!xbsd_check_new_partition(&i)) |
| 466 | return; |
| 467 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 468 | #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 469 | begin = get_start_sect(xbsd_part); |
| 470 | end = begin + get_nr_sects(xbsd_part) - 1; |
| 471 | #else |
| 472 | begin = 0; |
| 473 | end = xbsd_dlabel.d_secperunit - 1; |
| 474 | #endif |
| 475 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 476 | snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 477 | begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end), |
| 478 | 0, mesg); |
| 479 | |
| 480 | if (display_in_cyl_units) |
| 481 | begin = (begin - 1) * xbsd_dlabel.d_secpercyl; |
| 482 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 483 | snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 484 | str_units(SINGULAR)); |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 485 | end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end), |
| 486 | bsd_cround(begin), mesg); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 487 | |
| 488 | if (display_in_cyl_units) |
| 489 | end = end * xbsd_dlabel.d_secpercyl - 1; |
| 490 | |
| 491 | xbsd_dlabel.d_partitions[i].p_size = end - begin + 1; |
| 492 | xbsd_dlabel.d_partitions[i].p_offset = begin; |
| 493 | xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | xbsd_print_disklabel(int show_all) |
| 498 | { |
| 499 | struct xbsd_disklabel *lp = &xbsd_dlabel; |
| 500 | struct xbsd_partition *pp; |
| 501 | int i, j; |
| 502 | |
| 503 | if (show_all) { |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 504 | #if defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 505 | printf("# %s:\n", disk_device); |
| 506 | #else |
| 507 | printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0)); |
| 508 | #endif |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 509 | if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 510 | printf("type: %s\n", xbsd_dktypenames[lp->d_type]); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 511 | else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 512 | printf("type: %d\n", lp->d_type); |
| 513 | printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename); |
| 514 | printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname); |
| 515 | printf("flags:"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 516 | if (lp->d_flags & BSD_D_REMOVABLE) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 517 | printf(" removable"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 518 | if (lp->d_flags & BSD_D_ECC) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 519 | printf(" ecc"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 520 | if (lp->d_flags & BSD_D_BADSECT) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 521 | printf(" badsect"); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 522 | bb_putchar('\n'); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 523 | /* On various machines the fields of *lp are short/int/long */ |
| 524 | /* In order to avoid problems, we cast them all to long. */ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 525 | printf("bytes/sector: %ld\n", (long) lp->d_secsize); |
| 526 | printf("sectors/track: %ld\n", (long) lp->d_nsectors); |
| 527 | printf("tracks/cylinder: %ld\n", (long) lp->d_ntracks); |
| 528 | printf("sectors/cylinder: %ld\n", (long) lp->d_secpercyl); |
| 529 | printf("cylinders: %ld\n", (long) lp->d_ncylinders); |
| 530 | printf("rpm: %d\n", lp->d_rpm); |
| 531 | printf("interleave: %d\n", lp->d_interleave); |
| 532 | printf("trackskew: %d\n", lp->d_trackskew); |
| 533 | printf("cylinderskew: %d\n", lp->d_cylskew); |
| 534 | printf("headswitch: %ld\t\t# milliseconds\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 535 | (long) lp->d_headswitch); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 536 | printf("track-to-track seek: %ld\t# milliseconds\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 537 | (long) lp->d_trkseek); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 538 | printf("drivedata: "); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 539 | for (i = NDDATA - 1; i >= 0; i--) |
| 540 | if (lp->d_drivedata[i]) |
| 541 | break; |
| 542 | if (i < 0) |
| 543 | i = 0; |
| 544 | for (j = 0; j <= i; j++) |
| 545 | printf("%ld ", (long) lp->d_drivedata[j]); |
| 546 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 547 | printf("\n%d partitions:\n", lp->d_npartitions); |
| 548 | printf("# start end size fstype [fsize bsize cpg]\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 549 | pp = lp->d_partitions; |
| 550 | for (i = 0; i < lp->d_npartitions; i++, pp++) { |
| 551 | if (pp->p_size) { |
| 552 | if (display_in_cyl_units && lp->d_secpercyl) { |
| 553 | printf(" %c: %8ld%c %8ld%c %8ld%c ", |
| 554 | 'a' + i, |
| 555 | (long) pp->p_offset / lp->d_secpercyl + 1, |
| 556 | (pp->p_offset % lp->d_secpercyl) ? '*' : ' ', |
| 557 | (long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl, |
| 558 | ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ', |
| 559 | (long) pp->p_size / lp->d_secpercyl, |
| 560 | (pp->p_size % lp->d_secpercyl) ? '*' : ' ' |
| 561 | ); |
| 562 | } else { |
| 563 | printf(" %c: %8ld %8ld %8ld ", |
| 564 | 'a' + i, |
| 565 | (long) pp->p_offset, |
| 566 | (long) pp->p_offset + pp->p_size - 1, |
| 567 | (long) pp->p_size |
| 568 | ); |
| 569 | } |
| 570 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 571 | if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 572 | printf("%8.8s", xbsd_fstypes[pp->p_fstype]); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 573 | else |
| 574 | printf("%8x", pp->p_fstype); |
| 575 | |
| 576 | switch (pp->p_fstype) { |
| 577 | case BSD_FS_UNUSED: |
| 578 | printf(" %5ld %5ld %5.5s ", |
| 579 | (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, ""); |
| 580 | break; |
| 581 | case BSD_FS_BSDFFS: |
| 582 | printf(" %5ld %5ld %5d ", |
| 583 | (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg); |
| 584 | break; |
| 585 | default: |
| 586 | printf("%22.22s", ""); |
| 587 | break; |
| 588 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 589 | bb_putchar('\n'); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | xbsd_write_disklabel(void) |
| 596 | { |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 597 | #if defined(__alpha__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 598 | printf("Writing disklabel to %s\n", disk_device); |
| 599 | xbsd_writelabel(NULL); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 600 | #else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 601 | printf("Writing disklabel to %s\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 602 | partname(disk_device, xbsd_part_index + 1, 0)); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 603 | xbsd_writelabel(xbsd_part); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 604 | #endif |
| 605 | reread_partition_table(0); /* no exit yet */ |
| 606 | } |
| 607 | |
| 608 | static int |
| 609 | xbsd_create_disklabel(void) |
| 610 | { |
| 611 | char c; |
| 612 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 613 | #if defined(__alpha__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 614 | printf("%s contains no disklabel\n", disk_device); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 615 | #else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 616 | printf("%s contains no disklabel\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 617 | partname(disk_device, xbsd_part_index + 1, 0)); |
| 618 | #endif |
| 619 | |
| 620 | while (1) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 621 | c = read_nonempty("Do you want to create a disklabel? (y/n) "); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 622 | if (c == 'y' || c == 'Y') { |
| 623 | if (xbsd_initlabel( |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 624 | #if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \ |
| 625 | defined(__s390__) || defined(__s390x__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 626 | NULL |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 627 | #else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 628 | xbsd_part |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 629 | #endif |
| 630 | ) == 1) { |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 631 | xbsd_print_disklabel(1); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 632 | return 1; |
| 633 | } else |
| 634 | return 0; |
| 635 | } else if (c == 'n') |
| 636 | return 0; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static int |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 641 | edit_int(int def, const char *mesg) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 642 | { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 643 | mesg = xasprintf("%s (%d): ", mesg, def); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 644 | do { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 645 | if (!read_line(mesg)) |
| 646 | goto ret; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 647 | } while (!isdigit(*line_ptr)); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 648 | def = atoi(line_ptr); |
| 649 | ret: |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 650 | free((char*)mesg); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 651 | return def; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static void |
| 655 | xbsd_edit_disklabel(void) |
| 656 | { |
| 657 | struct xbsd_disklabel *d; |
| 658 | |
| 659 | d = &xbsd_dlabel; |
| 660 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 661 | #if defined(__alpha__) || defined(__ia64__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 662 | d->d_secsize = edit_int(d->d_secsize , "bytes/sector"); |
| 663 | d->d_nsectors = edit_int(d->d_nsectors , "sectors/track"); |
| 664 | d->d_ntracks = edit_int(d->d_ntracks , "tracks/cylinder"); |
| 665 | d->d_ncylinders = edit_int(d->d_ncylinders , "cylinders"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 668 | /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 669 | while (1) { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 670 | d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks, |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 671 | "sectors/cylinder"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 672 | if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks) |
| 673 | break; |
| 674 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 675 | printf("Must be <= sectors/track * tracks/cylinder (default)\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 676 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 677 | d->d_rpm = edit_int(d->d_rpm , "rpm"); |
| 678 | d->d_interleave = edit_int(d->d_interleave, "interleave"); |
| 679 | d->d_trackskew = edit_int(d->d_trackskew , "trackskew"); |
| 680 | d->d_cylskew = edit_int(d->d_cylskew , "cylinderskew"); |
| 681 | d->d_headswitch = edit_int(d->d_headswitch, "headswitch"); |
| 682 | d->d_trkseek = edit_int(d->d_trkseek , "track-to-track seek"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 683 | |
| 684 | d->d_secperunit = d->d_secpercyl * d->d_ncylinders; |
| 685 | } |
| 686 | |
| 687 | static int |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 688 | xbsd_get_bootstrap(char *path, void *ptr, int size) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 689 | { |
| 690 | int fdb; |
| 691 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 692 | fdb = open(path, O_RDONLY); |
| 693 | if (fdb < 0) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 694 | perror(path); |
| 695 | return 0; |
| 696 | } |
| 697 | if (read(fdb, ptr, size) < 0) { |
| 698 | perror(path); |
| 699 | close(fdb); |
| 700 | return 0; |
| 701 | } |
| 702 | printf(" ... %s\n", path); |
| 703 | close(fdb); |
| 704 | return 1; |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | sync_disks(void) |
| 709 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 710 | printf("Syncing disks\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 711 | sync(); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 712 | /* sleep(4); What? */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void |
| 716 | xbsd_write_bootstrap(void) |
| 717 | { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 718 | char path[MAXPATHLEN]; |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 719 | const char *bootdir = BSD_LINUX_BOOTDIR; |
| 720 | const char *dkbasename; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 721 | struct xbsd_disklabel dl; |
| 722 | char *d, *p, *e; |
| 723 | int sector; |
| 724 | |
| 725 | if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI) |
| 726 | dkbasename = "sd"; |
| 727 | else |
| 728 | dkbasename = "wd"; |
| 729 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 730 | snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 731 | dkbasename, dkbasename, dkbasename); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 732 | if (read_line(path)) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 733 | dkbasename = line_ptr; |
| 734 | } |
| 735 | snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename); |
| 736 | if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize)) |
| 737 | return; |
| 738 | |
| 739 | /* We need a backup of the disklabel (xbsd_dlabel might have changed). */ |
| 740 | d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE]; |
| 741 | memmove(&dl, d, sizeof(struct xbsd_disklabel)); |
| 742 | |
| 743 | /* The disklabel will be overwritten by 0's from bootxx anyway */ |
| 744 | memset(d, 0, sizeof(struct xbsd_disklabel)); |
| 745 | |
| 746 | snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename); |
Denis Vlasenko | 93f6aa6 | 2006-11-30 21:11:01 +0000 | [diff] [blame] | 747 | if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize], |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 748 | (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize)) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 749 | return; |
| 750 | |
| 751 | e = d + sizeof(struct xbsd_disklabel); |
| 752 | for (p = d; p < e; p++) |
| 753 | if (*p) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 754 | printf("Bootstrap overlaps with disk label!\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 755 | exit(EXIT_FAILURE); |
| 756 | } |
| 757 | |
| 758 | memmove(d, &dl, sizeof(struct xbsd_disklabel)); |
| 759 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 760 | #if defined(__powerpc__) || defined(__hppa__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 761 | sector = 0; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 762 | #elif defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 763 | sector = 0; |
| 764 | alpha_bootblock_checksum(disklabelbuffer); |
| 765 | #else |
| 766 | sector = get_start_sect(xbsd_part); |
| 767 | #endif |
| 768 | |
| 769 | if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1) |
| 770 | fdisk_fatal(unable_to_seek); |
| 771 | if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE)) |
| 772 | fdisk_fatal(unable_to_write); |
| 773 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 774 | #if defined(__alpha__) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 775 | printf("Bootstrap installed on %s\n", disk_device); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 776 | #else |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 777 | printf("Bootstrap installed on %s\n", |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 778 | partname(disk_device, xbsd_part_index+1, 0)); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 779 | #endif |
| 780 | |
| 781 | sync_disks(); |
| 782 | } |
| 783 | |
| 784 | static void |
| 785 | xbsd_change_fstype(void) |
| 786 | { |
| 787 | int i; |
| 788 | |
| 789 | i = xbsd_get_part_index(xbsd_dlabel.d_npartitions); |
| 790 | xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes); |
| 791 | } |
| 792 | |
| 793 | static int |
| 794 | xbsd_get_part_index(int max) |
| 795 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 796 | char prompt[sizeof("Partition (a-%c): ") + 16]; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 797 | char l; |
| 798 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 799 | snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 800 | do |
| 801 | l = tolower(read_nonempty(prompt)); |
| 802 | while (l < 'a' || l > 'a' + max - 1); |
| 803 | return l - 'a'; |
| 804 | } |
| 805 | |
| 806 | static int |
| 807 | xbsd_check_new_partition(int *i) |
| 808 | { |
| 809 | /* room for more? various BSD flavours have different maxima */ |
| 810 | if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) { |
| 811 | int t; |
| 812 | |
| 813 | for (t = 0; t < BSD_MAXPARTITIONS; t++) |
| 814 | if (xbsd_dlabel.d_partitions[t].p_size == 0) |
| 815 | break; |
| 816 | |
| 817 | if (t == BSD_MAXPARTITIONS) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 818 | printf("The maximum number of partitions has been created\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | } |
| 822 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 823 | *i = xbsd_get_part_index(BSD_MAXPARTITIONS); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 824 | |
| 825 | if (*i >= xbsd_dlabel.d_npartitions) |
| 826 | xbsd_dlabel.d_npartitions = (*i) + 1; |
| 827 | |
| 828 | if (xbsd_dlabel.d_partitions[*i].p_size != 0) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 829 | printf("This partition already exists\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | static void |
| 837 | xbsd_list_types(void) |
| 838 | { |
| 839 | list_types(xbsd_fstypes); |
| 840 | } |
| 841 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 842 | static uint16_t |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 843 | xbsd_dkcksum(struct xbsd_disklabel *lp) |
| 844 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 845 | uint16_t *start, *end; |
| 846 | uint16_t sum = 0; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 847 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 848 | start = (uint16_t *) lp; |
| 849 | end = (uint16_t *) &lp->d_partitions[lp->d_npartitions]; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 850 | while (start < end) |
| 851 | sum ^= *start++; |
| 852 | return sum; |
| 853 | } |
| 854 | |
| 855 | static int |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 856 | xbsd_initlabel(struct partition *p) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 857 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 858 | struct xbsd_disklabel *d = &xbsd_dlabel; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 859 | struct xbsd_partition *pp; |
| 860 | |
| 861 | get_geometry(); |
| 862 | memset(d, 0, sizeof(struct xbsd_disklabel)); |
| 863 | |
| 864 | d->d_magic = BSD_DISKMAGIC; |
| 865 | |
| 866 | if (strncmp(disk_device, "/dev/sd", 7) == 0) |
| 867 | d->d_type = BSD_DTYPE_SCSI; |
| 868 | else |
| 869 | d->d_type = BSD_DTYPE_ST506; |
| 870 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 871 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 872 | d->d_flags = BSD_D_DOSPART; |
| 873 | #else |
| 874 | d->d_flags = 0; |
| 875 | #endif |
| 876 | d->d_secsize = SECTOR_SIZE; /* bytes/sector */ |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 877 | d->d_nsectors = g_sectors; /* sectors/track */ |
| 878 | d->d_ntracks = g_heads; /* tracks/cylinder (heads) */ |
| 879 | d->d_ncylinders = g_cylinders; |
| 880 | d->d_secpercyl = g_sectors * g_heads;/* sectors/cylinder */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 881 | if (d->d_secpercyl == 0) |
| 882 | d->d_secpercyl = 1; /* avoid segfaults */ |
| 883 | d->d_secperunit = d->d_secpercyl * d->d_ncylinders; |
| 884 | |
| 885 | d->d_rpm = 3600; |
| 886 | d->d_interleave = 1; |
| 887 | d->d_trackskew = 0; |
| 888 | d->d_cylskew = 0; |
| 889 | d->d_headswitch = 0; |
| 890 | d->d_trkseek = 0; |
| 891 | |
| 892 | d->d_magic2 = BSD_DISKMAGIC; |
| 893 | d->d_bbsize = BSD_BBSIZE; |
| 894 | d->d_sbsize = BSD_SBSIZE; |
| 895 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 896 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 897 | d->d_npartitions = 4; |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 898 | pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */ |
| 899 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 900 | pp->p_offset = get_start_sect(p); |
| 901 | pp->p_size = get_nr_sects(p); |
| 902 | pp->p_fstype = BSD_FS_UNUSED; |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 903 | pp = &d->d_partitions[3]; /* Partition D should be whole disk */ |
| 904 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 905 | pp->p_offset = 0; |
| 906 | pp->p_size = d->d_secperunit; |
| 907 | pp->p_fstype = BSD_FS_UNUSED; |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 908 | #else |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 909 | d->d_npartitions = 3; |
| 910 | pp = &d->d_partitions[2]; /* Partition C should be |
| 911 | the whole disk */ |
| 912 | pp->p_offset = 0; |
| 913 | pp->p_size = d->d_secperunit; |
| 914 | pp->p_fstype = BSD_FS_UNUSED; |
| 915 | #endif |
| 916 | |
| 917 | return 1; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Read a xbsd_disklabel from sector 0 or from the starting sector of p. |
| 922 | * If it has the right magic, return 1. |
| 923 | */ |
| 924 | static int |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 925 | xbsd_readlabel(struct partition *p) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 926 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 927 | struct xbsd_disklabel *d; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 928 | int t, sector; |
| 929 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 930 | if (!bsd_globals_ptr) |
| 931 | bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr)); |
| 932 | |
| 933 | d = &xbsd_dlabel; |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 934 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 935 | /* p is used only to get the starting sector */ |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 936 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 937 | sector = (p ? get_start_sect(p) : 0); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 938 | #else |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 939 | sector = 0; |
| 940 | #endif |
| 941 | |
| 942 | if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1) |
| 943 | fdisk_fatal(unable_to_seek); |
| 944 | if (BSD_BBSIZE != read(fd, disklabelbuffer, BSD_BBSIZE)) |
| 945 | fdisk_fatal(unable_to_read); |
| 946 | |
| 947 | memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], |
| 948 | sizeof(struct xbsd_disklabel)); |
| 949 | |
| 950 | if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC) |
| 951 | return 0; |
| 952 | |
| 953 | for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) { |
| 954 | d->d_partitions[t].p_size = 0; |
| 955 | d->d_partitions[t].p_offset = 0; |
| 956 | d->d_partitions[t].p_fstype = BSD_FS_UNUSED; |
| 957 | } |
| 958 | |
| 959 | if (d->d_npartitions > BSD_MAXPARTITIONS) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 960 | printf("Warning: too many partitions (%d, maximum is %d)\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 961 | d->d_npartitions, BSD_MAXPARTITIONS); |
| 962 | return 1; |
| 963 | } |
| 964 | |
| 965 | static int |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 966 | xbsd_writelabel(struct partition *p) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 967 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 968 | struct xbsd_disklabel *d = &xbsd_dlabel; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 969 | unsigned int sector; |
| 970 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 971 | #if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 972 | sector = get_start_sect(p) + BSD_LABELSECTOR; |
| 973 | #else |
| 974 | sector = BSD_LABELSECTOR; |
| 975 | #endif |
| 976 | |
| 977 | d->d_checksum = 0; |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 978 | d->d_checksum = xbsd_dkcksum(d); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 979 | |
| 980 | /* This is necessary if we want to write the bootstrap later, |
| 981 | otherwise we'd write the old disklabel with the bootstrap. |
| 982 | */ |
| 983 | memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], |
| 984 | d, sizeof(struct xbsd_disklabel)); |
| 985 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 986 | #if defined(__alpha__) && BSD_LABELSECTOR == 0 |
Denis Vlasenko | 93f6aa6 | 2006-11-30 21:11:01 +0000 | [diff] [blame] | 987 | alpha_bootblock_checksum(disklabelbuffer); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 988 | if (lseek(fd, 0, SEEK_SET) == -1) |
| 989 | fdisk_fatal(unable_to_seek); |
| 990 | if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE)) |
| 991 | fdisk_fatal(unable_to_write); |
| 992 | #else |
| 993 | if (lseek(fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1) |
| 994 | fdisk_fatal(unable_to_seek); |
| 995 | if (sizeof(struct xbsd_disklabel) != write(fd, d, sizeof(struct xbsd_disklabel))) |
| 996 | fdisk_fatal(unable_to_write); |
| 997 | #endif |
| 998 | sync_disks(); |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
| 1002 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 1003 | #if !defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 1004 | static int |
| 1005 | xbsd_translate_fstype(int linux_type) |
| 1006 | { |
| 1007 | switch (linux_type) { |
| 1008 | case 0x01: /* DOS 12-bit FAT */ |
| 1009 | case 0x04: /* DOS 16-bit <32M */ |
| 1010 | case 0x06: /* DOS 16-bit >=32M */ |
| 1011 | case 0xe1: /* DOS access */ |
| 1012 | case 0xe3: /* DOS R/O */ |
| 1013 | case 0xf2: /* DOS secondary */ |
| 1014 | return BSD_FS_MSDOS; |
| 1015 | case 0x07: /* OS/2 HPFS */ |
| 1016 | return BSD_FS_HPFS; |
| 1017 | default: |
| 1018 | return BSD_FS_OTHER; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static void |
| 1023 | xbsd_link_part(void) |
| 1024 | { |
| 1025 | int k, i; |
| 1026 | struct partition *p; |
| 1027 | |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 1028 | k = get_partition(1, g_partitions); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (!xbsd_check_new_partition(&i)) |
| 1031 | return; |
| 1032 | |
| 1033 | p = get_part_table(k); |
| 1034 | |
| 1035 | xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p); |
| 1036 | xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p); |
| 1037 | xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind); |
| 1038 | } |
| 1039 | #endif |
| 1040 | |
Denis Vlasenko | 2a85676 | 2007-01-02 16:45:05 +0000 | [diff] [blame] | 1041 | #if defined(__alpha__) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 1042 | static void |
| 1043 | alpha_bootblock_checksum(char *boot) |
| 1044 | { |
| 1045 | uint64_t *dp, sum; |
| 1046 | int i; |
| 1047 | |
| 1048 | dp = (uint64_t *)boot; |
| 1049 | sum = 0; |
| 1050 | for (i = 0; i < 63; i++) |
| 1051 | sum += dp[i]; |
| 1052 | dp[63] = sum; |
| 1053 | } |
| 1054 | #endif /* __alpha__ */ |
| 1055 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 1056 | /* Undefine 'global' tricks */ |
| 1057 | #undef disklabelbuffer |
| 1058 | #undef xbsd_dlabel |
| 1059 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 1060 | #endif /* OSF_LABEL */ |