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