Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Andreas Neuper, Sep 1998. |
| 3 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 7 | #if ENABLE_FEATURE_SGI_LABEL |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 8 | |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 9 | #define SGI_DEBUG 0 |
| 10 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 11 | #define SGI_VOLHDR 0x00 |
| 12 | /* 1 and 2 were used for drive types no longer supported by SGI */ |
| 13 | #define SGI_SWAP 0x03 |
| 14 | /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */ |
| 15 | #define SGI_VOLUME 0x06 |
| 16 | #define SGI_EFS 0x07 |
| 17 | #define SGI_LVOL 0x08 |
| 18 | #define SGI_RLVOL 0x09 |
| 19 | #define SGI_XFS 0x0a |
| 20 | #define SGI_XFSLOG 0x0b |
| 21 | #define SGI_XLV 0x0c |
| 22 | #define SGI_XVM 0x0d |
| 23 | #define SGI_ENTIRE_DISK SGI_VOLUME |
| 24 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 25 | struct device_parameter { /* 48 bytes */ |
| 26 | unsigned char skew; |
| 27 | unsigned char gap1; |
| 28 | unsigned char gap2; |
| 29 | unsigned char sparecyl; |
| 30 | unsigned short pcylcount; |
| 31 | unsigned short head_vol0; |
| 32 | unsigned short ntrks; /* tracks in cyl 0 or vol 0 */ |
| 33 | unsigned char cmd_tag_queue_depth; |
| 34 | unsigned char unused0; |
| 35 | unsigned short unused1; |
| 36 | unsigned short nsect; /* sectors/tracks in cyl 0 or vol 0 */ |
| 37 | unsigned short bytes; |
| 38 | unsigned short ilfact; |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 39 | unsigned int flags; /* controller flags */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 40 | unsigned int datarate; |
| 41 | unsigned int retries_on_error; |
| 42 | unsigned int ms_per_word; |
| 43 | unsigned short xylogics_gap1; |
| 44 | unsigned short xylogics_syncdelay; |
| 45 | unsigned short xylogics_readdelay; |
| 46 | unsigned short xylogics_gap2; |
| 47 | unsigned short xylogics_readgate; |
| 48 | unsigned short xylogics_writecont; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * controller flags |
| 53 | */ |
| 54 | #define SECTOR_SLIP 0x01 |
| 55 | #define SECTOR_FWD 0x02 |
| 56 | #define TRACK_FWD 0x04 |
| 57 | #define TRACK_MULTIVOL 0x08 |
| 58 | #define IGNORE_ERRORS 0x10 |
| 59 | #define RESEEK 0x20 |
| 60 | #define ENABLE_CMDTAGQ 0x40 |
| 61 | |
| 62 | typedef struct { |
| 63 | unsigned int magic; /* expect SGI_LABEL_MAGIC */ |
| 64 | unsigned short boot_part; /* active boot partition */ |
| 65 | unsigned short swap_part; /* active swap partition */ |
| 66 | unsigned char boot_file[16]; /* name of the bootfile */ |
| 67 | struct device_parameter devparam; /* 1 * 48 bytes */ |
| 68 | struct volume_directory { /* 15 * 16 bytes */ |
| 69 | unsigned char vol_file_name[8]; /* a character array */ |
| 70 | unsigned int vol_file_start; /* number of logical block */ |
| 71 | unsigned int vol_file_size; /* number of bytes */ |
| 72 | } directory[15]; |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 73 | struct sgi_partinfo { /* 16 * 12 bytes */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 74 | unsigned int num_sectors; /* number of blocks */ |
| 75 | unsigned int start_sector; /* must be cylinder aligned */ |
| 76 | unsigned int id; |
| 77 | } partitions[16]; |
| 78 | unsigned int csum; |
| 79 | unsigned int fillbytes; |
| 80 | } sgi_partition; |
| 81 | |
| 82 | typedef struct { |
| 83 | unsigned int magic; /* looks like a magic number */ |
| 84 | unsigned int a2; |
| 85 | unsigned int a3; |
| 86 | unsigned int a4; |
| 87 | unsigned int b1; |
| 88 | unsigned short b2; |
| 89 | unsigned short b3; |
| 90 | unsigned int c[16]; |
| 91 | unsigned short d[3]; |
| 92 | unsigned char scsi_string[50]; |
| 93 | unsigned char serial[137]; |
| 94 | unsigned short check1816; |
| 95 | unsigned char installer[225]; |
| 96 | } sgiinfo; |
| 97 | |
| 98 | #define SGI_LABEL_MAGIC 0x0be5a941 |
| 99 | #define SGI_LABEL_MAGIC_SWAPPED 0x41a9e50b |
| 100 | #define SGI_INFO_MAGIC 0x00072959 |
| 101 | #define SGI_INFO_MAGIC_SWAPPED 0x59290700 |
| 102 | |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 103 | #define SGI_SSWAP16(x) (sgi_other_endian ? fdisk_swap16(x) : (uint16_t)(x)) |
| 104 | #define SGI_SSWAP32(x) (sgi_other_endian ? fdisk_swap32(x) : (uint32_t)(x)) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 105 | |
| 106 | #define sgilabel ((sgi_partition *)MBRbuffer) |
| 107 | #define sgiparam (sgilabel->devparam) |
| 108 | |
| 109 | /* |
| 110 | * |
| 111 | * fdisksgilabel.c |
| 112 | * |
| 113 | * Copyright (C) Andreas Neuper, Sep 1998. |
| 114 | * This file may be modified and redistributed under |
| 115 | * the terms of the GNU Public License. |
| 116 | * |
| 117 | * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 118 | * Internationalization |
| 119 | */ |
| 120 | |
| 121 | |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 122 | static smallint sgi_other_endian; /* bool */ |
| 123 | static smallint sgi_volumes = 1; /* max 15 */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * only dealing with free blocks here |
| 127 | */ |
| 128 | |
| 129 | typedef struct { |
| 130 | unsigned int first; |
| 131 | unsigned int last; |
| 132 | } freeblocks; |
| 133 | static freeblocks freelist[17]; /* 16 partitions can produce 17 vacant slots */ |
| 134 | |
| 135 | static void |
| 136 | setfreelist(int i, unsigned int f, unsigned int l) |
| 137 | { |
| 138 | freelist[i].first = f; |
| 139 | freelist[i].last = l; |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | add2freelist(unsigned int f, unsigned int l) |
| 144 | { |
| 145 | int i; |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 146 | for (i = 0; i < 17; i++) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 147 | if (freelist[i].last == 0) |
| 148 | break; |
| 149 | setfreelist(i, f, l); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | clearfreelist(void) |
| 154 | { |
| 155 | int i; |
| 156 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 157 | for (i = 0; i < 17; i++) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 158 | setfreelist(i, 0, 0); |
| 159 | } |
| 160 | |
| 161 | static unsigned int |
| 162 | isinfreelist(unsigned int b) |
| 163 | { |
| 164 | int i; |
| 165 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 166 | for (i = 0; i < 17; i++) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 167 | if (freelist[i].first <= b && freelist[i].last >= b) |
| 168 | return freelist[i].last; |
| 169 | return 0; |
| 170 | } |
| 171 | /* return last vacant block of this stride (never 0). */ |
| 172 | /* the '>=' is not quite correct, but simplifies the code */ |
| 173 | /* |
| 174 | * end of free blocks section |
| 175 | */ |
| 176 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 177 | static const char *const sgi_sys_types[] = { |
| 178 | /* SGI_VOLHDR */ "\x00" "SGI volhdr" , |
| 179 | /* 0x01 */ "\x01" "SGI trkrepl" , |
| 180 | /* 0x02 */ "\x02" "SGI secrepl" , |
| 181 | /* SGI_SWAP */ "\x03" "SGI raw" , |
| 182 | /* 0x04 */ "\x04" "SGI bsd" , |
| 183 | /* 0x05 */ "\x05" "SGI sysv" , |
| 184 | /* SGI_ENTIRE_DISK */ "\x06" "SGI volume" , |
| 185 | /* SGI_EFS */ "\x07" "SGI efs" , |
| 186 | /* 0x08 */ "\x08" "SGI lvol" , |
| 187 | /* 0x09 */ "\x09" "SGI rlvol" , |
| 188 | /* SGI_XFS */ "\x0a" "SGI xfs" , |
| 189 | /* SGI_XFSLOG */ "\x0b" "SGI xfslog" , |
| 190 | /* SGI_XLV */ "\x0c" "SGI xlv" , |
| 191 | /* SGI_XVM */ "\x0d" "SGI xvm" , |
| 192 | /* LINUX_SWAP */ "\x82" "Linux swap" , |
| 193 | /* LINUX_NATIVE */ "\x83" "Linux native", |
| 194 | /* LINUX_LVM */ "\x8d" "Linux LVM" , |
| 195 | /* LINUX_RAID */ "\xfd" "Linux RAID" , |
| 196 | NULL |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | |
| 200 | static int |
| 201 | sgi_get_nsect(void) |
| 202 | { |
| 203 | return SGI_SSWAP16(sgilabel->devparam.nsect); |
| 204 | } |
| 205 | |
| 206 | static int |
| 207 | sgi_get_ntrks(void) |
| 208 | { |
| 209 | return SGI_SSWAP16(sgilabel->devparam.ntrks); |
| 210 | } |
| 211 | |
| 212 | static unsigned int |
| 213 | two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */) |
| 214 | { |
| 215 | int i = 0; |
| 216 | unsigned int sum = 0; |
| 217 | |
| 218 | size /= sizeof(unsigned int); |
| 219 | for (i = 0; i < size; i++) |
| 220 | sum -= SGI_SSWAP32(base[i]); |
| 221 | return sum; |
| 222 | } |
| 223 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 224 | void BUG_bad_sgi_partition_size(void); |
| 225 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 226 | static int |
| 227 | check_sgi_label(void) |
| 228 | { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 229 | if (sizeof(sgi_partition) > 512) { |
| 230 | /* According to MIPS Computer Systems, Inc the label |
| 231 | * must not contain more than 512 bytes */ |
| 232 | BUG_bad_sgi_partition_size(); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (sgilabel->magic != SGI_LABEL_MAGIC |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 236 | && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED |
| 237 | ) { |
Denis Vlasenko | 4437d19 | 2008-04-17 00:12:10 +0000 | [diff] [blame] | 238 | current_label_type = LABEL_DOS; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | sgi_other_endian = (sgilabel->magic == SGI_LABEL_MAGIC_SWAPPED); |
| 243 | /* |
| 244 | * test for correct checksum |
| 245 | */ |
| 246 | if (two_s_complement_32bit_sum((unsigned int*)sgilabel, |
| 247 | sizeof(*sgilabel))) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 248 | printf("Detected sgi disklabel with wrong checksum\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 249 | } |
| 250 | update_units(); |
Denis Vlasenko | 4437d19 | 2008-04-17 00:12:10 +0000 | [diff] [blame] | 251 | current_label_type = LABEL_SGI; |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 252 | g_partitions = 16; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 253 | sgi_volumes = 15; |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | static unsigned int |
| 258 | sgi_get_start_sector(int i) |
| 259 | { |
| 260 | return SGI_SSWAP32(sgilabel->partitions[i].start_sector); |
| 261 | } |
| 262 | |
| 263 | static unsigned int |
| 264 | sgi_get_num_sectors(int i) |
| 265 | { |
| 266 | return SGI_SSWAP32(sgilabel->partitions[i].num_sectors); |
| 267 | } |
| 268 | |
| 269 | static int |
| 270 | sgi_get_sysid(int i) |
| 271 | { |
| 272 | return SGI_SSWAP32(sgilabel->partitions[i].id); |
| 273 | } |
| 274 | |
| 275 | static int |
| 276 | sgi_get_bootpartition(void) |
| 277 | { |
| 278 | return SGI_SSWAP16(sgilabel->boot_part); |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | sgi_get_swappartition(void) |
| 283 | { |
| 284 | return SGI_SSWAP16(sgilabel->swap_part); |
| 285 | } |
| 286 | |
| 287 | static void |
| 288 | sgi_list_table(int xtra) |
| 289 | { |
| 290 | int i, w, wd; |
| 291 | int kpi = 0; /* kernel partition ID */ |
| 292 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 293 | if (xtra) { |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 294 | printf("\nDisk %s (SGI disk label): %u heads, %u sectors\n" |
| 295 | "%u cylinders, %u physical cylinders\n" |
| 296 | "%u extra sects/cyl, interleave %u:1\n" |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 297 | "%s\n" |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 298 | "Units = %ss of %u * 512 bytes\n\n", |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 299 | disk_device, g_heads, g_sectors, g_cylinders, |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 300 | SGI_SSWAP16(sgiparam.pcylcount), |
| 301 | SGI_SSWAP16(sgiparam.sparecyl), |
| 302 | SGI_SSWAP16(sgiparam.ilfact), |
| 303 | (char *)sgilabel, |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 304 | str_units(), units_per_sector); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 305 | } else { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 306 | printf("\nDisk %s (SGI disk label): " |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 307 | "%u heads, %u sectors, %u cylinders\n" |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 308 | "Units = %ss of %u * 512 bytes\n\n", |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 309 | disk_device, g_heads, g_sectors, g_cylinders, |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 310 | str_units(), units_per_sector ); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | w = strlen(disk_device); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 314 | wd = sizeof("Device") - 1; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 315 | if (w < wd) |
| 316 | w = wd; |
| 317 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 318 | printf("----- partitions -----\n" |
| 319 | "Pt# %*s Info Start End Sectors Id System\n", |
| 320 | w + 2, "Device"); |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 321 | for (i = 0; i < g_partitions; i++) { |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 322 | if (sgi_get_num_sectors(i) || SGI_DEBUG) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 323 | uint32_t start = sgi_get_start_sector(i); |
| 324 | uint32_t len = sgi_get_num_sectors(i); |
| 325 | kpi++; /* only count nonempty partitions */ |
| 326 | printf( |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 327 | "%2u: %s %4s %9lu %9lu %9lu %2x %s\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 328 | /* fdisk part number */ i+1, |
| 329 | /* device */ partname(disk_device, kpi, w+3), |
| 330 | /* flags */ (sgi_get_swappartition() == i) ? "swap" : |
| 331 | /* flags */ (sgi_get_bootpartition() == i) ? "boot" : " ", |
| 332 | /* start */ (long) scround(start), |
| 333 | /* end */ (long) scround(start+len)-1, |
| 334 | /* no odd flag on end */(long) len, |
| 335 | /* type id */ sgi_get_sysid(i), |
| 336 | /* type name */ partition_type(sgi_get_sysid(i))); |
| 337 | } |
| 338 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 339 | printf("----- Bootinfo -----\nBootfile: %s\n" |
| 340 | "----- Directory Entries -----\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 341 | sgilabel->boot_file); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 342 | for (i = 0; i < sgi_volumes; i++) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 343 | if (sgilabel->directory[i].vol_file_size) { |
| 344 | uint32_t start = SGI_SSWAP32(sgilabel->directory[i].vol_file_start); |
| 345 | uint32_t len = SGI_SSWAP32(sgilabel->directory[i].vol_file_size); |
| 346 | unsigned char *name = sgilabel->directory[i].vol_file_name; |
| 347 | |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 348 | printf("%2u: %-10s sector%5u size%8u\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 349 | i, (char*)name, (unsigned int) start, (unsigned int) len); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static void |
| 355 | sgi_set_bootpartition(int i) |
| 356 | { |
| 357 | sgilabel->boot_part = SGI_SSWAP16(((short)i)); |
| 358 | } |
| 359 | |
| 360 | static unsigned int |
| 361 | sgi_get_lastblock(void) |
| 362 | { |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 363 | return g_heads * g_sectors * g_cylinders; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void |
| 367 | sgi_set_swappartition(int i) |
| 368 | { |
| 369 | sgilabel->swap_part = SGI_SSWAP16(((short)i)); |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | sgi_check_bootfile(const char* aFile) |
| 374 | { |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 375 | if (strlen(aFile) < 3) /* "/a\n" is minimum */ { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 376 | printf("\nInvalid Bootfile!\n" |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 377 | "\tThe bootfile must be an absolute non-zero pathname,\n" |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 378 | "\te.g. \"/unix\" or \"/unix.save\".\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 379 | return 0; |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 380 | } |
| 381 | if (strlen(aFile) > 16) { |
| 382 | printf("\nName of Bootfile too long (>16 bytes)\n"); |
| 383 | return 0; |
| 384 | } |
| 385 | if (aFile[0] != '/') { |
| 386 | printf("\nBootfile must have a fully qualified pathname\n"); |
| 387 | return 0; |
| 388 | } |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 389 | if (strncmp(aFile, (char*)sgilabel->boot_file, 16)) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 390 | printf("\nBe aware, that the bootfile is not checked for existence.\n" |
| 391 | "\tSGI's default is \"/unix\" and for backup \"/unix.save\".\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 392 | /* filename is correct and did change */ |
| 393 | return 1; |
| 394 | } |
| 395 | return 0; /* filename did not change */ |
| 396 | } |
| 397 | |
| 398 | static const char * |
| 399 | sgi_get_bootfile(void) |
| 400 | { |
| 401 | return (char*)sgilabel->boot_file; |
| 402 | } |
| 403 | |
| 404 | static void |
| 405 | sgi_set_bootfile(const char* aFile) |
| 406 | { |
| 407 | int i = 0; |
| 408 | |
| 409 | if (sgi_check_bootfile(aFile)) { |
| 410 | while (i < 16) { |
| 411 | if ((aFile[i] != '\n') /* in principle caught again by next line */ |
| 412 | && (strlen(aFile) > i)) |
| 413 | sgilabel->boot_file[i] = aFile[i]; |
| 414 | else |
| 415 | sgilabel->boot_file[i] = 0; |
| 416 | i++; |
| 417 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 418 | printf("\n\tBootfile is changed to \"%s\"\n", sgilabel->boot_file); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | create_sgiinfo(void) |
| 424 | { |
| 425 | /* I keep SGI's habit to write the sgilabel to the second block */ |
| 426 | sgilabel->directory[0].vol_file_start = SGI_SSWAP32(2); |
| 427 | sgilabel->directory[0].vol_file_size = SGI_SSWAP32(sizeof(sgiinfo)); |
Denys Vlasenko | 8ef9eff | 2018-06-26 15:44:58 +0200 | [diff] [blame] | 428 | memcpy((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static sgiinfo *fill_sgiinfo(void); |
| 432 | |
| 433 | static void |
| 434 | sgi_write_table(void) |
| 435 | { |
| 436 | sgilabel->csum = 0; |
| 437 | sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum( |
| 438 | (unsigned int*)sgilabel, sizeof(*sgilabel))); |
| 439 | assert(two_s_complement_32bit_sum( |
| 440 | (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); |
| 441 | |
Denis Vlasenko | 6eaf0a9 | 2008-06-29 05:10:47 +0000 | [diff] [blame] | 442 | write_sector(0, sgilabel); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 443 | if (is_prefixed_with((char*)sgilabel->directory[0].vol_file_name, "sgilabel")) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 444 | /* |
| 445 | * keep this habit of first writing the "sgilabel". |
| 446 | * I never tested whether it works without (AN 981002). |
| 447 | */ |
| 448 | sgiinfo *info = fill_sgiinfo(); |
| 449 | int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start); |
Denis Vlasenko | 6eaf0a9 | 2008-06-29 05:10:47 +0000 | [diff] [blame] | 450 | write_sector(infostartblock, info); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 451 | free(info); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static int |
| 456 | compare_start(int *x, int *y) |
| 457 | { |
| 458 | /* |
| 459 | * sort according to start sectors |
| 460 | * and prefers largest partition: |
| 461 | * entry zero is entire disk entry |
| 462 | */ |
| 463 | unsigned int i = *x; |
| 464 | unsigned int j = *y; |
| 465 | unsigned int a = sgi_get_start_sector(i); |
| 466 | unsigned int b = sgi_get_start_sector(j); |
| 467 | unsigned int c = sgi_get_num_sectors(i); |
| 468 | unsigned int d = sgi_get_num_sectors(j); |
| 469 | |
| 470 | if (a == b) |
| 471 | return (d > c) ? 1 : (d == c) ? 0 : -1; |
| 472 | return (a > b) ? 1 : -1; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | static int |
| 477 | verify_sgi(int verbose) |
| 478 | { |
| 479 | int Index[16]; /* list of valid partitions */ |
| 480 | int sortcount = 0; /* number of used partitions, i.e. non-zero lengths */ |
| 481 | int entire = 0, i = 0; |
| 482 | unsigned int start = 0; |
| 483 | long long gap = 0; /* count unused blocks */ |
| 484 | unsigned int lastblock = sgi_get_lastblock(); |
| 485 | |
| 486 | clearfreelist(); |
| 487 | for (i = 0; i < 16; i++) { |
| 488 | if (sgi_get_num_sectors(i) != 0) { |
| 489 | Index[sortcount++] = i; |
| 490 | if (sgi_get_sysid(i) == SGI_ENTIRE_DISK) { |
| 491 | if (entire++ == 1) { |
| 492 | if (verbose) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 493 | printf("More than one entire disk entry present\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | if (sortcount == 0) { |
| 499 | if (verbose) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 500 | printf("No partitions defined\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 501 | return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1; |
| 502 | } |
| 503 | qsort(Index, sortcount, sizeof(Index[0]), (void*)compare_start); |
| 504 | if (sgi_get_sysid(Index[0]) == SGI_ENTIRE_DISK) { |
| 505 | if ((Index[0] != 10) && verbose) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 506 | printf("IRIX likes when Partition 11 covers the entire disk\n"); |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 507 | if ((sgi_get_start_sector(Index[0]) != 0) && verbose) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 508 | printf("The entire disk partition should start " |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 509 | "at block 0,\n" |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 510 | "not at diskblock %u\n", |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 511 | sgi_get_start_sector(Index[0])); |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 512 | } |
| 513 | if (SGI_DEBUG) { /* I do not understand how some disks fulfil it */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 514 | if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 515 | printf("The entire disk partition is only %u diskblock large,\n" |
| 516 | "but the disk is %u diskblocks long\n", |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 517 | sgi_get_num_sectors(Index[0]), lastblock); |
| 518 | lastblock = sgi_get_num_sectors(Index[0]); |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 519 | } |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 520 | } else { |
| 521 | if (verbose) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 522 | printf("One Partition (#11) should cover the entire disk\n"); |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 523 | if (SGI_DEBUG > 2) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 524 | printf("sysid=%u\tpartition=%u\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 525 | sgi_get_sysid(Index[0]), Index[0]+1); |
| 526 | } |
| 527 | for (i = 1, start = 0; i < sortcount; i++) { |
| 528 | int cylsize = sgi_get_nsect() * sgi_get_ntrks(); |
| 529 | |
| 530 | if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) { |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 531 | if (SGI_DEBUG) /* I do not understand how some disks fulfil it */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 532 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 533 | printf("Partition %u does not start on cylinder boundary\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 534 | Index[i]+1); |
| 535 | } |
| 536 | if (sgi_get_num_sectors(Index[i]) % cylsize != 0) { |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 537 | if (SGI_DEBUG) /* I do not understand how some disks fulfil it */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 538 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 539 | printf("Partition %u does not end on cylinder boundary\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 540 | Index[i]+1); |
| 541 | } |
| 542 | /* We cannot handle several "entire disk" entries. */ |
| 543 | if (sgi_get_sysid(Index[i]) == SGI_ENTIRE_DISK) continue; |
| 544 | if (start > sgi_get_start_sector(Index[i])) { |
| 545 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 546 | printf("Partitions %u and %u overlap by %u sectors\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 547 | Index[i-1]+1, Index[i]+1, |
| 548 | start - sgi_get_start_sector(Index[i])); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 549 | if (gap > 0) gap = -gap; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 550 | if (gap == 0) gap = -1; |
| 551 | } |
| 552 | if (start < sgi_get_start_sector(Index[i])) { |
| 553 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 554 | printf("Unused gap of %u sectors - sectors %u-%u\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 555 | sgi_get_start_sector(Index[i]) - start, |
| 556 | start, sgi_get_start_sector(Index[i])-1); |
| 557 | gap += sgi_get_start_sector(Index[i]) - start; |
| 558 | add2freelist(start, sgi_get_start_sector(Index[i])); |
| 559 | } |
| 560 | start = sgi_get_start_sector(Index[i]) |
| 561 | + sgi_get_num_sectors(Index[i]); |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 562 | if (SGI_DEBUG > 1) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 563 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 564 | printf("%2u:%12u\t%12u\t%12u\n", Index[i], |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 565 | sgi_get_start_sector(Index[i]), |
| 566 | sgi_get_num_sectors(Index[i]), |
| 567 | sgi_get_sysid(Index[i])); |
| 568 | } |
| 569 | } |
| 570 | if (start < lastblock) { |
| 571 | if (verbose) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 572 | printf("Unused gap of %u sectors - sectors %u-%u\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 573 | lastblock - start, start, lastblock-1); |
| 574 | gap += lastblock - start; |
| 575 | add2freelist(start, lastblock); |
| 576 | } |
| 577 | /* |
| 578 | * Done with arithmetics |
| 579 | * Go for details now |
| 580 | */ |
| 581 | if (verbose) { |
| 582 | if (!sgi_get_num_sectors(sgi_get_bootpartition())) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 583 | printf("\nThe boot partition does not exist\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 584 | } |
| 585 | if (!sgi_get_num_sectors(sgi_get_swappartition())) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 586 | printf("\nThe swap partition does not exist\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 587 | } else { |
| 588 | if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP) |
| 589 | && (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP)) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 590 | printf("\nThe swap partition has no swap type\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 591 | } |
| 592 | if (sgi_check_bootfile("/unix")) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 593 | printf("\tYou have chosen an unusual boot file name\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 594 | } |
| 595 | return (gap > 0) ? 1 : (gap == 0) ? 0 : -1; |
| 596 | } |
| 597 | |
| 598 | static int |
| 599 | sgi_gaps(void) |
| 600 | { |
| 601 | /* |
| 602 | * returned value is: |
| 603 | * = 0 : disk is properly filled to the rim |
| 604 | * < 0 : there is an overlap |
| 605 | * > 0 : there is still some vacant space |
| 606 | */ |
| 607 | return verify_sgi(0); |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | sgi_change_sysid(int i, int sys) |
| 612 | { |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 613 | if (sgi_get_num_sectors(i) == 0) { /* caught already before, ... */ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 614 | printf("Sorry you may change the Tag of non-empty partitions\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 615 | return; |
| 616 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 617 | if ((sys != SGI_ENTIRE_DISK) && (sys != SGI_VOLHDR) |
| 618 | && (sgi_get_start_sector(i) < 1) |
| 619 | ) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 620 | read_maybe_empty( |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 621 | "It is highly recommended that the partition at offset 0\n" |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 622 | "is of type \"SGI volhdr\", the IRIX system will rely on it to\n" |
| 623 | "retrieve from its directory standalone tools like sash and fx.\n" |
| 624 | "Only the \"SGI volume\" entire disk section may violate this.\n" |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 625 | "Type YES if you are sure about tagging this partition differently.\n"); |
Denys Vlasenko | c104549 | 2018-07-25 13:45:36 +0200 | [diff] [blame] | 626 | if (strcmp(line_ptr, "YES") != 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | sgilabel->partitions[i].id = SGI_SSWAP32(sys); |
| 630 | } |
| 631 | |
| 632 | /* returns partition index of first entry marked as entire disk */ |
| 633 | static int |
| 634 | sgi_entire(void) |
| 635 | { |
| 636 | int i; |
| 637 | |
| 638 | for (i = 0; i < 16; i++) |
| 639 | if (sgi_get_sysid(i) == SGI_VOLUME) |
| 640 | return i; |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | static void |
| 645 | sgi_set_partition(int i, unsigned int start, unsigned int length, int sys) |
| 646 | { |
| 647 | sgilabel->partitions[i].id = SGI_SSWAP32(sys); |
| 648 | sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length); |
| 649 | sgilabel->partitions[i].start_sector = SGI_SSWAP32(start); |
| 650 | set_changed(i); |
| 651 | if (sgi_gaps() < 0) /* rebuild freelist */ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 652 | printf("Partition overlap detected\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void |
| 656 | sgi_set_entire(void) |
| 657 | { |
| 658 | int n; |
| 659 | |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 660 | for (n = 10; n < g_partitions; n++) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 661 | if (!sgi_get_num_sectors(n) ) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 662 | sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME); |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void |
| 669 | sgi_set_volhdr(void) |
| 670 | { |
| 671 | int n; |
| 672 | |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 673 | for (n = 8; n < g_partitions; n++) { |
Denys Vlasenko | 015db58 | 2016-06-19 18:15:33 +0200 | [diff] [blame] | 674 | if (!sgi_get_num_sectors(n)) { |
| 675 | /* |
| 676 | * 5 cylinders is an arbitrary value I like |
| 677 | * IRIX 5.3 stored files in the volume header |
| 678 | * (like sash, symmon, fx, ide) with ca. 3200 |
| 679 | * sectors. |
| 680 | */ |
| 681 | if (g_heads * g_sectors * 5 < sgi_get_lastblock()) { |
| 682 | sgi_set_partition(n, 0, g_heads * g_sectors * 5, SGI_VOLHDR); |
| 683 | break; |
| 684 | } |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static void |
| 690 | sgi_delete_partition(int i) |
| 691 | { |
| 692 | sgi_set_partition(i, 0, 0, 0); |
| 693 | } |
| 694 | |
| 695 | static void |
| 696 | sgi_add_partition(int n, int sys) |
| 697 | { |
| 698 | char mesg[256]; |
| 699 | unsigned int first = 0, last = 0; |
| 700 | |
| 701 | if (n == 10) { |
| 702 | sys = SGI_VOLUME; |
| 703 | } else if (n == 8) { |
| 704 | sys = 0; |
| 705 | } |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 706 | if (sgi_get_num_sectors(n)) { |
| 707 | printf(msg_part_already_defined, n + 1); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 708 | return; |
| 709 | } |
| 710 | if ((sgi_entire() == -1) && (sys != SGI_VOLUME)) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 711 | printf("Attempting to generate entire disk entry automatically\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 712 | sgi_set_entire(); |
| 713 | sgi_set_volhdr(); |
| 714 | } |
| 715 | if ((sgi_gaps() == 0) && (sys != SGI_VOLUME)) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 716 | printf("The entire disk is already covered with partitions\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | if (sgi_gaps() < 0) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 720 | printf("You got a partition overlap on the disk. Fix it first!\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 723 | snprintf(mesg, sizeof(mesg), "First %s", str_units()); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 724 | while (1) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 725 | if (sys == SGI_VOLUME) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 726 | last = sgi_get_lastblock(); |
| 727 | first = read_int(0, 0, last-1, 0, mesg); |
| 728 | if (first != 0) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 729 | printf("It is highly recommended that eleventh partition\n" |
| 730 | "covers the entire disk and is of type 'SGI volume'\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 731 | } |
| 732 | } else { |
| 733 | first = freelist[0].first; |
| 734 | last = freelist[0].last; |
| 735 | first = read_int(scround(first), scround(first), scround(last)-1, |
| 736 | 0, mesg); |
| 737 | } |
| 738 | if (display_in_cyl_units) |
| 739 | first *= units_per_sector; |
| 740 | else |
| 741 | first = first; /* align to cylinder if you know how ... */ |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 742 | if (!last ) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 743 | last = isinfreelist(first); |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 744 | if (last != 0) |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 745 | break; |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 746 | printf("You will get a partition overlap on the disk. " |
| 747 | "Fix it first!\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 748 | } |
Denys Vlasenko | d8e4ce0 | 2019-10-04 16:45:04 +0200 | [diff] [blame^] | 749 | snprintf(mesg, sizeof(mesg), " Last %s", str_units()); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 750 | last = read_int(scround(first), scround(last)-1, scround(last)-1, |
| 751 | scround(first), mesg)+1; |
| 752 | if (display_in_cyl_units) |
| 753 | last *= units_per_sector; |
| 754 | else |
| 755 | last = last; /* align to cylinder if You know how ... */ |
| 756 | if ( (sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock() ) ) |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 757 | printf("It is highly recommended that eleventh partition\n" |
| 758 | "covers the entire disk and is of type 'SGI volume'\n"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 759 | sgi_set_partition(n, first, last-first, sys); |
| 760 | } |
| 761 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 762 | #if ENABLE_FEATURE_FDISK_ADVANCED |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 763 | static void |
| 764 | create_sgilabel(void) |
| 765 | { |
| 766 | struct hd_geometry geometry; |
| 767 | struct { |
| 768 | unsigned int start; |
| 769 | unsigned int nsect; |
| 770 | int sysid; |
| 771 | } old[4]; |
| 772 | int i = 0; |
| 773 | long longsectors; /* the number of sectors on the device */ |
| 774 | int res; /* the result from the ioctl */ |
| 775 | int sec_fac; /* the sector factor */ |
| 776 | |
| 777 | sec_fac = sector_size / 512; /* determine the sector factor */ |
| 778 | |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 779 | printf(msg_building_new_label, "SGI disklabel"); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 780 | |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 781 | sgi_other_endian = BB_LITTLE_ENDIAN; |
Denis Vlasenko | 4437d19 | 2008-04-17 00:12:10 +0000 | [diff] [blame] | 782 | res = ioctl(dev_fd, BLKGETSIZE, &longsectors); |
| 783 | if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) { |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 784 | g_heads = geometry.heads; |
| 785 | g_sectors = geometry.sectors; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 786 | if (res == 0) { |
| 787 | /* the get device size ioctl was successful */ |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 788 | g_cylinders = longsectors / (g_heads * g_sectors); |
| 789 | g_cylinders /= sec_fac; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 790 | } else { |
| 791 | /* otherwise print error and use truncated version */ |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 792 | g_cylinders = geometry.cylinders; |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 793 | printf( |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 794 | "Warning: BLKGETSIZE ioctl failed on %s. Using geometry cylinder value of %u.\n" |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 795 | "This value may be truncated for devices > 33.8 GB.\n", disk_device, g_cylinders); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | for (i = 0; i < 4; i++) { |
| 799 | old[i].sysid = 0; |
| 800 | if (valid_part_table_flag(MBRbuffer)) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 801 | if (get_part_table(i)->sys_ind) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 802 | old[i].sysid = get_part_table(i)->sys_ind; |
| 803 | old[i].start = get_start_sect(get_part_table(i)); |
| 804 | old[i].nsect = get_nr_sects(get_part_table(i)); |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 805 | printf("Trying to keep parameters of partition %u\n", i); |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 806 | if (SGI_DEBUG) |
Denys Vlasenko | ddf7850 | 2009-09-16 03:03:13 +0200 | [diff] [blame] | 807 | printf("ID=%02x\tSTART=%u\tLENGTH=%u\n", |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 808 | old[i].sysid, old[i].start, old[i].nsect); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | memset(MBRbuffer, 0, sizeof(MBRbuffer)); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 814 | /* fields with '//' are already zeroed out by memset above */ |
| 815 | |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 816 | sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 817 | //sgilabel->boot_part = SGI_SSWAP16(0); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 818 | sgilabel->swap_part = SGI_SSWAP16(1); |
| 819 | |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 820 | //memset(sgilabel->boot_file, 0, 16); |
| 821 | strcpy((char*)sgilabel->boot_file, "/unix"); /* sizeof(sgilabel->boot_file) == 16 > 6 */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 822 | |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 823 | //sgilabel->devparam.skew = (0); |
| 824 | //sgilabel->devparam.gap1 = (0); |
| 825 | //sgilabel->devparam.gap2 = (0); |
| 826 | //sgilabel->devparam.sparecyl = (0); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 827 | sgilabel->devparam.pcylcount = SGI_SSWAP16(geometry.cylinders); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 828 | //sgilabel->devparam.head_vol0 = SGI_SSWAP16(0); |
| 829 | /* tracks/cylinder (heads) */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 830 | sgilabel->devparam.ntrks = SGI_SSWAP16(geometry.heads); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 831 | //sgilabel->devparam.cmd_tag_queue_depth = (0); |
| 832 | //sgilabel->devparam.unused0 = (0); |
| 833 | //sgilabel->devparam.unused1 = SGI_SSWAP16(0); |
| 834 | /* sectors/track */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 835 | sgilabel->devparam.nsect = SGI_SSWAP16(geometry.sectors); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 836 | sgilabel->devparam.bytes = SGI_SSWAP16(512); |
| 837 | sgilabel->devparam.ilfact = SGI_SSWAP16(1); |
| 838 | sgilabel->devparam.flags = SGI_SSWAP32(TRACK_FWD| |
| 839 | IGNORE_ERRORS|RESEEK); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 840 | //sgilabel->devparam.datarate = SGI_SSWAP32(0); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 841 | sgilabel->devparam.retries_on_error = SGI_SSWAP32(1); |
Denis Vlasenko | d547083 | 2007-01-03 02:58:54 +0000 | [diff] [blame] | 842 | //sgilabel->devparam.ms_per_word = SGI_SSWAP32(0); |
| 843 | //sgilabel->devparam.xylogics_gap1 = SGI_SSWAP16(0); |
| 844 | //sgilabel->devparam.xylogics_syncdelay = SGI_SSWAP16(0); |
| 845 | //sgilabel->devparam.xylogics_readdelay = SGI_SSWAP16(0); |
| 846 | //sgilabel->devparam.xylogics_gap2 = SGI_SSWAP16(0); |
| 847 | //sgilabel->devparam.xylogics_readgate = SGI_SSWAP16(0); |
| 848 | //sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0); |
| 849 | //memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 ); |
| 850 | //memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partinfo)*16 ); |
Denis Vlasenko | 4437d19 | 2008-04-17 00:12:10 +0000 | [diff] [blame] | 851 | current_label_type = LABEL_SGI; |
Denis Vlasenko | f77f369 | 2007-12-16 17:22:33 +0000 | [diff] [blame] | 852 | g_partitions = 16; |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 853 | sgi_volumes = 15; |
| 854 | sgi_set_entire(); |
| 855 | sgi_set_volhdr(); |
| 856 | for (i = 0; i < 4; i++) { |
Denis Vlasenko | bd85207 | 2007-03-19 14:43:38 +0000 | [diff] [blame] | 857 | if (old[i].sysid) { |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 858 | sgi_set_partition(i, old[i].start, old[i].nsect, old[i].sysid); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | static void |
| 864 | sgi_set_xcyl(void) |
| 865 | { |
| 866 | /* do nothing in the beginning */ |
| 867 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 868 | #endif /* FEATURE_FDISK_ADVANCED */ |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 869 | |
| 870 | /* _____________________________________________________________ |
| 871 | */ |
| 872 | |
| 873 | static sgiinfo * |
| 874 | fill_sgiinfo(void) |
| 875 | { |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 876 | sgiinfo *info = xzalloc(sizeof(sgiinfo)); |
Denis Vlasenko | 98ae216 | 2006-10-12 19:30:44 +0000 | [diff] [blame] | 877 | |
| 878 | info->magic = SGI_SSWAP32(SGI_INFO_MAGIC); |
| 879 | info->b1 = SGI_SSWAP32(-1); |
| 880 | info->b2 = SGI_SSWAP16(-1); |
| 881 | info->b3 = SGI_SSWAP16(1); |
| 882 | /* You may want to replace this string !!!!!!! */ |
| 883 | strcpy( (char*)info->scsi_string, "IBM OEM 0662S12 3 30" ); |
| 884 | strcpy( (char*)info->serial, "0000" ); |
| 885 | info->check1816 = SGI_SSWAP16(18*256 +16 ); |
| 886 | strcpy( (char*)info->installer, "Sfx version 5.3, Oct 18, 1994" ); |
| 887 | return info; |
| 888 | } |
| 889 | #endif /* SGI_LABEL */ |