| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2012 Davidlohr Bueso <[email protected]> |
| 4 |
* 2013 Karel Zak <[email protected]> |
| 5 |
* |
| 6 |
* This is a re-written version for libfdisk, the original was fdisksgilabel.c |
| 7 |
* from util-linux fdisk, by: |
| 8 |
* |
| 9 |
* Andreas Neuper, Sep 1998, |
| 10 |
* Arnaldo Carvalho de Melo <[email protected]>, Mar 1999, |
| 11 |
* Phillip Kesling <[email protected]>, Mar 2003. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "c.h" |
| 15 |
#include "all-io.h" |
| 16 |
|
| 17 |
#include "blkdev.h" |
| 18 |
|
| 19 |
#include "bitops.h" |
| 20 |
#include "pt-sgi.h" |
| 21 |
#include "pt-mbr.h" |
| 22 |
#include "fdiskP.h" |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION: sgi |
| 26 |
* @title: SGI |
| 27 |
* @short_description: disk label specific functions |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
/* |
| 32 |
* in-memory fdisk SGI stuff |
| 33 |
*/ |
| 34 |
struct fdisk_sgi_label { |
| 35 |
struct fdisk_label head; /* generic fdisk part */ |
| 36 |
struct sgi_disklabel *header; /* on-disk data (pointer to cxt->firstsector) */ |
| 37 |
|
| 38 |
struct sgi_freeblocks { |
| 39 |
unsigned int first; |
| 40 |
unsigned int last; |
| 41 |
} freelist[SGI_MAXPARTITIONS + 1]; |
| 42 |
}; |
| 43 |
|
| 44 |
static const struct fdisk_parttype sgi_parttypes[] = |
| 45 |
{ |
| 46 |
{SGI_TYPE_VOLHDR, N_("SGI volhdr")}, |
| 47 |
{SGI_TYPE_TRKREPL, N_("SGI trkrepl")}, |
| 48 |
{SGI_TYPE_SECREPL, N_("SGI secrepl")}, |
| 49 |
{SGI_TYPE_SWAP, N_("SGI raw")}, |
| 50 |
{SGI_TYPE_BSD, N_("SGI bsd")}, |
| 51 |
{SGI_TYPE_SYSV, N_("SGI sysv")}, |
| 52 |
{SGI_TYPE_ENTIRE_DISK, N_("SGI volume")}, |
| 53 |
{SGI_TYPE_EFS, N_("SGI efs")}, |
| 54 |
{SGI_TYPE_LVOL, N_("SGI lvol")}, |
| 55 |
{SGI_TYPE_RLVOL, N_("SGI rlvol")}, |
| 56 |
{SGI_TYPE_XFS, N_("SGI xfs")}, |
| 57 |
{SGI_TYPE_XFSLOG, N_("SGI xfslog")}, |
| 58 |
{SGI_TYPE_XLV, N_("SGI xlv")}, |
| 59 |
{SGI_TYPE_XVM, N_("SGI xvm")}, |
| 60 |
{MBR_LINUX_SWAP_PARTITION, N_("Linux swap")}, |
| 61 |
{MBR_LINUX_DATA_PARTITION, N_("Linux native")}, |
| 62 |
{MBR_LINUX_LVM_PARTITION, N_("Linux LVM")}, |
| 63 |
{MBR_LINUX_RAID_PARTITION, N_("Linux RAID")}, |
| 64 |
{0, NULL } |
| 65 |
}; |
| 66 |
|
| 67 |
static unsigned int sgi_get_start_sector(struct fdisk_context *cxt, int i ); |
| 68 |
static unsigned int sgi_get_num_sectors(struct fdisk_context *cxt, int i ); |
| 69 |
static int sgi_get_bootpartition(struct fdisk_context *cxt); |
| 70 |
static int sgi_get_swappartition(struct fdisk_context *cxt); |
| 71 |
|
| 72 |
/* Returns a pointer buffer with on-disk data. */ |
| 73 |
static inline struct sgi_disklabel *self_disklabel(struct fdisk_context *cxt) |
| 74 |
{ |
| 75 |
assert(cxt); |
| 76 |
assert(cxt->label); |
| 77 |
assert(fdisk_is_label(cxt, SGI)); |
| 78 |
|
| 79 |
return ((struct fdisk_sgi_label *) cxt->label)->header; |
| 80 |
} |
| 81 |
|
| 82 |
/* Returns in-memory fdisk data. */ |
| 83 |
static inline struct fdisk_sgi_label *self_label(struct fdisk_context *cxt) |
| 84 |
{ |
| 85 |
assert(cxt); |
| 86 |
assert(cxt->label); |
| 87 |
assert(fdisk_is_label(cxt, SGI)); |
| 88 |
|
| 89 |
return (struct fdisk_sgi_label *) cxt->label; |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* Information within second on-disk block |
| 94 |
*/ |
| 95 |
#define SGI_INFO_MAGIC 0x00072959 |
| 96 |
|
| 97 |
struct sgi_info { |
| 98 |
unsigned int magic; /* looks like a magic number */ |
| 99 |
unsigned int a2; |
| 100 |
unsigned int a3; |
| 101 |
unsigned int a4; |
| 102 |
unsigned int b1; |
| 103 |
unsigned short b2; |
| 104 |
unsigned short b3; |
| 105 |
unsigned int c[16]; |
| 106 |
unsigned short d[3]; |
| 107 |
unsigned char scsi_string[50]; |
| 108 |
unsigned char serial[137]; |
| 109 |
unsigned short check1816; |
| 110 |
unsigned char installer[225]; |
| 111 |
}; |
| 112 |
|
| 113 |
static struct sgi_info *sgi_new_info(void) |
| 114 |
{ |
| 115 |
struct sgi_info *info = calloc(1, sizeof(struct sgi_info)); |
| 116 |
|
| 117 |
if (!info) |
| 118 |
return NULL; |
| 119 |
|
| 120 |
info->magic = cpu_to_be32(SGI_INFO_MAGIC); |
| 121 |
info->b1 = cpu_to_be32(-1); |
| 122 |
info->b2 = cpu_to_be16(-1); |
| 123 |
info->b3 = cpu_to_be16(1); |
| 124 |
|
| 125 |
/* You may want to replace this string !!!!!!! */ |
| 126 |
strncpy((char *) info->scsi_string, "IBM OEM 0662S12 3 30", |
| 127 |
sizeof(info->scsi_string)); |
| 128 |
strncpy((char *) info->serial, "0000", sizeof(info->serial)); |
| 129 |
info->check1816 = cpu_to_be16(18 * 256 + 16); |
| 130 |
strncpy((char *) info->installer, "Sfx version 5.3, Oct 18, 1994", |
| 131 |
sizeof(info->installer)); |
| 132 |
|
| 133 |
return info; |
| 134 |
} |
| 135 |
|
| 136 |
static void sgi_free_info(struct sgi_info *info) |
| 137 |
{ |
| 138 |
free(info); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* fdisk_sgi_create_info: |
| 143 |
* @cxt: context |
| 144 |
* |
| 145 |
* This function add hint about SGI label (e.g. set "sgilabel" as volume name) |
| 146 |
* to the first SGI volume. This is probably old SGI convention without any |
| 147 |
* effect to the device partitioning. |
| 148 |
* |
| 149 |
* Returns: 0 on success, <0 on error |
| 150 |
*/ |
| 151 |
int fdisk_sgi_create_info(struct fdisk_context *cxt) |
| 152 |
{ |
| 153 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 154 |
|
| 155 |
/* I keep SGI's habit to write the sgilabel to the second block */ |
| 156 |
sgilabel->volume[0].block_num = cpu_to_be32(2); |
| 157 |
sgilabel->volume[0].num_bytes = cpu_to_be32(sizeof(struct sgi_info)); |
| 158 |
memcpy((char *) sgilabel->volume[0].name, "sgilabel", 8); |
| 159 |
|
| 160 |
fdisk_info(cxt, _("SGI info created on second sector.")); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/* |
| 166 |
* only dealing with free blocks here |
| 167 |
*/ |
| 168 |
static void set_freelist(struct fdisk_context *cxt, |
| 169 |
size_t i, unsigned int f, unsigned int l) |
| 170 |
{ |
| 171 |
struct fdisk_sgi_label *sgi = self_label(cxt); |
| 172 |
|
| 173 |
if (i < ARRAY_SIZE(sgi->freelist)) { |
| 174 |
sgi->freelist[i].first = f; |
| 175 |
sgi->freelist[i].last = l; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static void add_to_freelist(struct fdisk_context *cxt, |
| 180 |
unsigned int f, unsigned int l) |
| 181 |
{ |
| 182 |
struct fdisk_sgi_label *sgi = self_label(cxt); |
| 183 |
size_t i; |
| 184 |
|
| 185 |
for (i = 0; i < ARRAY_SIZE(sgi->freelist); i++) { |
| 186 |
if (sgi->freelist[i].last == 0) |
| 187 |
break; |
| 188 |
} |
| 189 |
set_freelist(cxt, i, f, l); |
| 190 |
} |
| 191 |
|
| 192 |
static void clear_freelist(struct fdisk_context *cxt) |
| 193 |
{ |
| 194 |
struct fdisk_sgi_label *sgi = self_label(cxt); |
| 195 |
|
| 196 |
memset(sgi->freelist, 0, sizeof(sgi->freelist)); |
| 197 |
} |
| 198 |
|
| 199 |
static unsigned int is_in_freelist(struct fdisk_context *cxt, unsigned int b) |
| 200 |
{ |
| 201 |
struct fdisk_sgi_label *sgi = self_label(cxt); |
| 202 |
size_t i; |
| 203 |
|
| 204 |
for (i = 0; i < ARRAY_SIZE(sgi->freelist); i++) { |
| 205 |
if (sgi->freelist[i].first <= b |
| 206 |
&& sgi->freelist[i].last >= b) |
| 207 |
return sgi->freelist[i].last; |
| 208 |
} |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static int sgi_get_nsect(struct fdisk_context *cxt) |
| 215 |
{ |
| 216 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 217 |
return be16_to_cpu(sgilabel->devparam.nsect); |
| 218 |
} |
| 219 |
|
| 220 |
static int sgi_get_ntrks(struct fdisk_context *cxt) |
| 221 |
{ |
| 222 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 223 |
return be16_to_cpu(sgilabel->devparam.ntrks); |
| 224 |
} |
| 225 |
|
| 226 |
static size_t count_used_partitions(struct fdisk_context *cxt) |
| 227 |
{ |
| 228 |
size_t i, ct = 0; |
| 229 |
|
| 230 |
for (i = 0; i < cxt->label->nparts_max; i++) |
| 231 |
ct += sgi_get_num_sectors(cxt, i) > 0; |
| 232 |
|
| 233 |
return ct; |
| 234 |
} |
| 235 |
|
| 236 |
static int sgi_probe_label(struct fdisk_context *cxt) |
| 237 |
{ |
| 238 |
struct fdisk_sgi_label *sgi; |
| 239 |
struct sgi_disklabel *sgilabel; |
| 240 |
|
| 241 |
assert(cxt); |
| 242 |
assert(cxt->label); |
| 243 |
assert(fdisk_is_label(cxt, SGI)); |
| 244 |
assert(sizeof(struct sgi_disklabel) <= 512); |
| 245 |
|
| 246 |
/* map first sector to header */ |
| 247 |
sgi = (struct fdisk_sgi_label *) cxt->label; |
| 248 |
sgi->header = (struct sgi_disklabel *) cxt->firstsector; |
| 249 |
sgilabel = sgi->header; |
| 250 |
|
| 251 |
if (be32_to_cpu(sgilabel->magic) != SGI_LABEL_MAGIC) { |
| 252 |
sgi->header = NULL; |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
* test for correct checksum |
| 258 |
*/ |
| 259 |
if (sgi_pt_checksum(sgilabel) != 0) |
| 260 |
fdisk_warnx(cxt, _("Detected an SGI disklabel with wrong checksum.")); |
| 261 |
|
| 262 |
clear_freelist(cxt); |
| 263 |
cxt->label->nparts_max = SGI_MAXPARTITIONS; |
| 264 |
cxt->label->nparts_cur = count_used_partitions(cxt); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
static int sgi_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labelitem *item) |
| 269 |
{ |
| 270 |
struct sgi_disklabel *sgilabel; |
| 271 |
struct sgi_device_parameter *sgiparam; |
| 272 |
int rc = 0; |
| 273 |
|
| 274 |
assert(cxt); |
| 275 |
assert(cxt->label); |
| 276 |
assert(fdisk_is_label(cxt, SGI)); |
| 277 |
|
| 278 |
sgilabel = self_disklabel(cxt); |
| 279 |
sgiparam = &sgilabel->devparam; |
| 280 |
|
| 281 |
switch (item->id) { |
| 282 |
case SGI_LABELITEM_PCYLCOUNT: |
| 283 |
item->name = _("Physical cylinders"); |
| 284 |
item->type = 'j'; |
| 285 |
item->data.num64 = (uint64_t) be16_to_cpu(sgiparam->pcylcount); |
| 286 |
break; |
| 287 |
case SGI_LABELITEM_SPARECYL: |
| 288 |
item->name = _("Extra sects/cyl"); |
| 289 |
item->type = 'j'; |
| 290 |
item->data.num64 = (uint64_t) sgiparam->sparecyl; |
| 291 |
break; |
| 292 |
case SGI_LABELITEM_ILFACT: |
| 293 |
item->name = _("Interleave"); |
| 294 |
item->type = 'j'; |
| 295 |
item->data.num64 = (uint64_t) be16_to_cpu(sgiparam->ilfact); |
| 296 |
break; |
| 297 |
case SGI_LABELITEM_BOOTFILE: |
| 298 |
item->name = _("Bootfile"); |
| 299 |
item->type = 's'; |
| 300 |
item->data.str = *sgilabel->boot_file ? strdup((char *) sgilabel->boot_file) : NULL; |
| 301 |
break; |
| 302 |
default: |
| 303 |
if (item->id < __FDISK_NLABELITEMS) |
| 304 |
rc = 1; /* unsupported generic item */ |
| 305 |
else |
| 306 |
rc = 2; /* out of range */ |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
return rc; |
| 311 |
} |
| 312 |
|
| 313 |
static unsigned int sgi_get_start_sector(struct fdisk_context *cxt, int i) |
| 314 |
{ |
| 315 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 316 |
return be32_to_cpu(sgilabel->partitions[i].first_block); |
| 317 |
} |
| 318 |
|
| 319 |
static unsigned int sgi_get_num_sectors(struct fdisk_context *cxt, int i) |
| 320 |
{ |
| 321 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 322 |
return be32_to_cpu(sgilabel->partitions[i].num_blocks); |
| 323 |
} |
| 324 |
|
| 325 |
static int sgi_get_sysid(struct fdisk_context *cxt, int i) |
| 326 |
{ |
| 327 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 328 |
return be32_to_cpu(sgilabel->partitions[i].type); |
| 329 |
} |
| 330 |
|
| 331 |
static int sgi_get_bootpartition(struct fdisk_context *cxt) |
| 332 |
{ |
| 333 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 334 |
return be16_to_cpu(sgilabel->root_part_num); |
| 335 |
} |
| 336 |
|
| 337 |
static int sgi_get_swappartition(struct fdisk_context *cxt) |
| 338 |
{ |
| 339 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 340 |
return be16_to_cpu(sgilabel->swap_part_num); |
| 341 |
} |
| 342 |
|
| 343 |
static unsigned int sgi_get_lastblock(struct fdisk_context *cxt) |
| 344 |
{ |
| 345 |
return cxt->geom.heads * cxt->geom.sectors * cxt->geom.cylinders; |
| 346 |
} |
| 347 |
|
| 348 |
static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, size_t n) |
| 349 |
{ |
| 350 |
struct fdisk_parttype *t; |
| 351 |
|
| 352 |
if (n >= cxt->label->nparts_max) |
| 353 |
return NULL; |
| 354 |
|
| 355 |
t = fdisk_label_get_parttype_from_code(cxt->label, sgi_get_sysid(cxt, n)); |
| 356 |
return t ? : fdisk_new_unknown_parttype(sgi_get_sysid(cxt, n), NULL); |
| 357 |
} |
| 358 |
|
| 359 |
/* fdisk_get_partition() backend */ |
| 360 |
static int sgi_get_partition(struct fdisk_context *cxt, size_t n, struct fdisk_partition *pa) |
| 361 |
{ |
| 362 |
fdisk_sector_t start, len; |
| 363 |
|
| 364 |
pa->used = sgi_get_num_sectors(cxt, n) > 0; |
| 365 |
if (!pa->used) |
| 366 |
return 0; |
| 367 |
|
| 368 |
start = sgi_get_start_sector(cxt, n); |
| 369 |
len = sgi_get_num_sectors(cxt, n); |
| 370 |
|
| 371 |
pa->type = sgi_get_parttype(cxt, n); |
| 372 |
pa->size = len; |
| 373 |
pa->start = start; |
| 374 |
|
| 375 |
if (pa->type && pa->type->code == SGI_TYPE_ENTIRE_DISK) |
| 376 |
pa->wholedisk = 1; |
| 377 |
|
| 378 |
pa->attrs = sgi_get_swappartition(cxt) == (int) n ? "swap" : |
| 379 |
sgi_get_bootpartition(cxt) == (int) n ? "boot" : NULL; |
| 380 |
if (pa->attrs) |
| 381 |
pa->attrs = strdup(pa->attrs); |
| 382 |
|
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
static int sgi_check_bootfile(struct fdisk_context *cxt, const char *name) |
| 388 |
{ |
| 389 |
size_t sz; |
| 390 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 391 |
|
| 392 |
sz = strlen(name); |
| 393 |
|
| 394 |
if (sz < 2) { |
| 395 |
/* "/a" is minimum */ |
| 396 |
fdisk_warnx(cxt, _("Invalid bootfile! The bootfile must " |
| 397 |
"be an absolute non-zero pathname, " |
| 398 |
"e.g. \"/unix\" or \"/unix.save\".")); |
| 399 |
return -EINVAL; |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
if (sz > sizeof(sgilabel->boot_file)) { |
| 404 |
fdisk_warnx(cxt, P_("Name of bootfile is too long: %zu byte maximum.", |
| 405 |
"Name of bootfile is too long: %zu bytes maximum.", |
| 406 |
sizeof(sgilabel->boot_file)), |
| 407 |
sizeof(sgilabel->boot_file)); |
| 408 |
return -EINVAL; |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
if (*name != '/') { |
| 413 |
fdisk_warnx(cxt, _("Bootfile must have a fully qualified pathname.")); |
| 414 |
return -EINVAL; |
| 415 |
} |
| 416 |
|
| 417 |
if (strncmp(name, (char *) sgilabel->boot_file, |
| 418 |
sizeof(sgilabel->boot_file)) != 0) { |
| 419 |
fdisk_warnx(cxt, _("Be aware that the bootfile is not checked " |
| 420 |
"for existence. SGI's default is \"/unix\", " |
| 421 |
"and for backup \"/unix.save\".")); |
| 422 |
return 0; /* filename is correct and did change */ |
| 423 |
} |
| 424 |
|
| 425 |
return 1; /* filename did not change */ |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* fdisk_sgi_set_bootfile: |
| 430 |
* @cxt: context |
| 431 |
* |
| 432 |
* Allows to set SGI boot file. The function uses Ask API for dialog with |
| 433 |
* user. |
| 434 |
* |
| 435 |
* Returns: 0 on success, <0 on error |
| 436 |
*/ |
| 437 |
int fdisk_sgi_set_bootfile(struct fdisk_context *cxt) |
| 438 |
{ |
| 439 |
int rc = 0; |
| 440 |
size_t sz; |
| 441 |
char *name = NULL; |
| 442 |
struct sgi_disklabel *sgilabel = self_disklabel(cxt); |
| 443 |
|
| 444 |
fdisk_info(cxt, _("The current boot file is: %s"), sgilabel->boot_file); |
| 445 |
|
| 446 |
rc = fdisk_ask_string(cxt, _("Enter full path of the new boot file"), &name); |
| 447 |
if (rc == 0) |
| 448 |
rc = sgi_check_bootfile(cxt, name); |
| 449 |
if (rc) { |
| 450 |
if (rc == 1) { |
| 451 |
fdisk_info(cxt, _("Boot file is unchanged.")); |
| 452 |
rc = 0; |
| 453 |
} |
| 454 |
goto done; |
| 455 |
} |
| 456 |
|
| 457 |
memset(sgilabel->boot_file, 0, sizeof(sgilabel->boot_file)); |
| 458 |
sz = strlen(name); |
| 459 |
|
| 460 |
assert(sz <= sizeof(sgilabel->boot_file)); /* see sgi_check_bootfile() */ |
| 461 |
|
| 462 |
memcpy(sgilabel->boot_file, name, sz); |
| 463 |
|
| 464 |
fdisk_info(cxt, _("Bootfile has been changed to \"%s\"."), name); |
| 465 |
done: |
| 466 |
free(name); |
| 467 |
return rc; |
| 468 |
} |
| 469 |
|
| 470 |
static int sgi_write_disklabel(struct fdisk_context *cxt) |
| 471 |
{ |
| 472 |
struct sgi_disklabel *sgilabel; |
| 473 |
struct sgi_info *info = NULL; |
| 474 |
|
| 475 |
assert(cxt); |
| 476 |
assert(cxt->label); |
| 477 |
assert(fdisk_is_label(cxt, SGI)); |
| 478 |
|
| 479 |
sgilabel = self_disklabel(cxt); |
| 480 |
sgilabel->csum = 0; |
| 481 |
sgilabel->csum = cpu_to_be32(sgi_pt_checksum(sgilabel)); |
| 482 |
|
| 483 |
assert(sgi_pt_checksum(sgilabel) == 0); |
| 484 |
|
| 485 |
if (lseek(cxt->dev_fd, 0, SEEK_SET) < 0) |
| 486 |
goto err; |
| 487 |
if (write_all(cxt->dev_fd, sgilabel, DEFAULT_SECTOR_SIZE)) |
| 488 |
goto err; |
| 489 |
if (!strncmp((char *) sgilabel->volume[0].name, "sgilabel", 8)) { |
| 490 |
/* |
| 491 |
* Keep this habit of first writing the "sgilabel". |
| 492 |
* I never tested whether it works without. (AN 1998-10-02) |
| 493 |
*/ |
| 494 |
int infostartblock |
| 495 |
= be32_to_cpu(sgilabel->volume[0].block_num); |
| 496 |
|
| 497 |
if (lseek(cxt->dev_fd, (off_t) infostartblock * |
| 498 |
DEFAULT_SECTOR_SIZE, SEEK_SET) < 0) |
| 499 |
goto err; |
| 500 |
info = sgi_new_info(); |
| 501 |
if (!info) |
| 502 |
goto err; |
| 503 |
if (write_all(cxt->dev_fd, info, sizeof(*info))) |
| 504 |
goto err; |
| 505 |
} |
| 506 |
|
| 507 |
sgi_free_info(info); |
| 508 |
return 0; |
| 509 |
err: |
| 510 |
sgi_free_info(info); |
| 511 |
return -errno; |
| 512 |
} |
| 513 |
|
| 514 |
static int compare_start(struct fdisk_context *cxt, |
| 515 |
const void *x, const void *y) |
| 516 |
{ |
| 517 |
/* |
| 518 |
* Sort according to start sectors and prefer the largest partition: |
| 519 |
* entry zero is the entire-disk entry. |
| 520 |
*/ |
| 521 |
const unsigned int i = *(const int *) x; |
| 522 |
const unsigned int j = *(const int *) y; |
| 523 |
unsigned int a = sgi_get_start_sector(cxt, i); |
| 524 |
unsigned int b = sgi_get_start_sector(cxt, j); |
| 525 |
unsigned int c = sgi_get_num_sectors(cxt, i); |
| 526 |
unsigned int d = sgi_get_num_sectors(cxt, j); |
| 527 |
|
| 528 |
if (a == b) |
| 529 |
return (d > c) ? 1 : (d == c) ? 0 : -1; |
| 530 |
return (a > b) ? 1 : -1; |
| 531 |
} |
| 532 |
|
| 533 |
static void generic_swap(void *a0, void *b0, int size) |
| 534 |
{ |
| 535 |
char *a = a0, *b = b0; |
| 536 |
|
| 537 |
for (; size > 0; --size, a++, b++) { |
| 538 |
char t = *a; |
| 539 |
*a = *b; |
| 540 |
*b = t; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
/* heap sort, based on Matt Mackall's linux kernel version */ |
| 546 |
static void sort(void *base0, size_t num, size_t size, struct fdisk_context *cxt, |
| 547 |
int (*cmp_func)(struct fdisk_context *, const void *, const void *)) |
| 548 |
{ |
| 549 |
/* pre-scale counters for performance */ |
| 550 |
int i = (num/2 - 1) * size; |
| 551 |
size_t n = num * size, c, r; |
| 552 |
char *base = base0; |
| 553 |
|
| 554 |
/* heapify */ |
| 555 |
for ( ; i >= 0; i -= size) { |
| 556 |
for (r = i; r * 2 + size < n; r = c) { |
| 557 |
c = r * 2 + size; |
| 558 |
if (c < n - size && |
| 559 |
cmp_func(cxt, base + c, base + c + size) < 0) |
| 560 |
c += size; |
| 561 |
if (cmp_func(cxt, base + r, base + c) >= 0) |
| 562 |
break; |
| 563 |
generic_swap(base + r, base + c, size); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
/* sort */ |
| 568 |
for (i = n - size; i > 0; i -= size) { |
| 569 |
generic_swap(base, base + i, size); |
| 570 |
for (r = 0; r * 2 + size < (size_t) i; r = c) { |
| 571 |
c = r * 2 + size; |
| 572 |
if (c < i - size && |
| 573 |
cmp_func(cxt, base + c, base + c + size) < 0) |
| 574 |
c += size; |
| 575 |
if (cmp_func(cxt, base + r, base + c) >= 0) |
| 576 |
break; |
| 577 |
generic_swap(base + r, base + c, size); |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
static int verify_disklabel(struct fdisk_context *cxt, int verbose) |
| 583 |
{ |
| 584 |
int Index[SGI_MAXPARTITIONS]; /* list of valid partitions */ |
| 585 |
int sortcount = 0; /* number of used partitions, i.e. non-zero lengths */ |
| 586 |
int entire = 0, i = 0; |
| 587 |
unsigned int start = 0; |
| 588 |
long long gap = 0; /* count unused blocks */ |
| 589 |
unsigned int lastblock = sgi_get_lastblock(cxt); |
| 590 |
|
| 591 |
assert(cxt); |
| 592 |
assert(cxt->label); |
| 593 |
assert(fdisk_is_label(cxt, SGI)); |
| 594 |
|
| 595 |
clear_freelist(cxt); |
| 596 |
memset(Index, 0, sizeof(Index)); |
| 597 |
|
| 598 |
for (i=0; i < SGI_MAXPARTITIONS; i++) { |
| 599 |
if (sgi_get_num_sectors(cxt, i) != 0) { |
| 600 |
Index[sortcount++] = i; |
| 601 |
if (sgi_get_sysid(cxt, i) == SGI_TYPE_ENTIRE_DISK |
| 602 |
&& entire++ == 1 && verbose) { |
| 603 |
fdisk_info(cxt, _("More than one entire " |
| 604 |
"disk entry present.")); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
if (sortcount == 0) { |
| 609 |
if (verbose) |
| 610 |
fdisk_info(cxt, _("No partitions defined.")); |
| 611 |
if (lastblock) |
| 612 |
add_to_freelist(cxt, 0, lastblock); |
| 613 |
return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1; |
| 614 |
} |
| 615 |
|
| 616 |
sort(Index, sortcount, sizeof(Index[0]), cxt, compare_start); |
| 617 |
|
| 618 |
if (sgi_get_sysid(cxt, Index[0]) == SGI_TYPE_ENTIRE_DISK) { |
| 619 |
if (verbose && Index[0] != 10) |
| 620 |
fdisk_info(cxt, _("IRIX likes it when partition 11 " |
| 621 |
"covers the entire disk.")); |
| 622 |
|
| 623 |
if (verbose && sgi_get_start_sector(cxt, Index[0]) != 0) |
| 624 |
fdisk_info(cxt, _("The entire disk partition should " |
| 625 |
"start at block 0, not at block %d."), |
| 626 |
sgi_get_start_sector(cxt, Index[0])); |
| 627 |
|
| 628 |
if (verbose && sgi_get_num_sectors(cxt, Index[0]) != lastblock) |
| 629 |
DBG(LABEL, ul_debug( |
| 630 |
"entire disk partition=%ds, but disk=%ds", |
| 631 |
sgi_get_num_sectors(cxt, Index[0]), |
| 632 |
lastblock)); |
| 633 |
lastblock = sgi_get_num_sectors(cxt, Index[0]); |
| 634 |
} else if (verbose) { |
| 635 |
fdisk_info(cxt, _("Partition 11 should cover the entire disk.")); |
| 636 |
DBG(LABEL, ul_debug("sysid=%d\tpartition=%d", |
| 637 |
sgi_get_sysid(cxt, Index[0]), Index[0]+1)); |
| 638 |
} |
| 639 |
for (i=1, start=0; i<sortcount; i++) { |
| 640 |
int cylsize = sgi_get_nsect(cxt) * sgi_get_ntrks(cxt); |
| 641 |
|
| 642 |
if (verbose && cylsize |
| 643 |
&& (sgi_get_start_sector(cxt, Index[i]) % cylsize) != 0) |
| 644 |
DBG(LABEL, ul_debug("partition %d does not start on " |
| 645 |
"cylinder boundary.", Index[i]+1)); |
| 646 |
|
| 647 |
if (verbose && cylsize |
| 648 |
&& sgi_get_num_sectors(cxt, Index[i]) % cylsize != 0) |
| 649 |
DBG(LABEL, ul_debug("partition %d does not end on " |
| 650 |
"cylinder boundary.", Index[i]+1)); |
| 651 |
|
| 652 |
/* We cannot handle several "entire disk" entries. */ |
| 653 |
if (sgi_get_sysid(cxt, Index[i]) == SGI_TYPE_ENTIRE_DISK) |
| 654 |
continue; |
| 655 |
|
| 656 |
if (start > sgi_get_start_sector(cxt, Index[i])) { |
| 657 |
if (verbose) |
| 658 |
fdisk_info(cxt, |
| 659 |
P_("Partitions %d and %d overlap by %d sector.", |
| 660 |
"Partitions %d and %d overlap by %d sectors.", |
| 661 |
start - sgi_get_start_sector(cxt, Index[i])), |
| 662 |
Index[i-1]+1, Index[i]+1, |
| 663 |
start - sgi_get_start_sector(cxt, Index[i])); |
| 664 |
if (gap > 0) gap = -gap; |
| 665 |
if (gap == 0) gap = -1; |
| 666 |
} |
| 667 |
if (start < sgi_get_start_sector(cxt, Index[i])) { |
| 668 |
if (verbose) |
| 669 |
fdisk_info(cxt, |
| 670 |
P_("Unused gap of %8u sector: sector %8u", |
| 671 |
"Unused gap of %8u sectors: sectors %8u-%u", |
| 672 |
sgi_get_start_sector(cxt, Index[i]) - start), |
| 673 |
sgi_get_start_sector(cxt, Index[i]) - start, |
| 674 |
start, sgi_get_start_sector(cxt, Index[i])-1); |
| 675 |
gap += sgi_get_start_sector(cxt, Index[i]) - start; |
| 676 |
add_to_freelist(cxt, start, |
| 677 |
sgi_get_start_sector(cxt, Index[i])); |
| 678 |
} |
| 679 |
start = sgi_get_start_sector(cxt, Index[i]) |
| 680 |
+ sgi_get_num_sectors(cxt, Index[i]); |
| 681 |
/* Align free space on cylinder boundary. */ |
| 682 |
if (cylsize && start % cylsize) |
| 683 |
start += cylsize - (start % cylsize); |
| 684 |
|
| 685 |
DBG(LABEL, ul_debug("%2d:%12d\t%12d\t%12d", Index[i], |
| 686 |
sgi_get_start_sector(cxt, Index[i]), |
| 687 |
sgi_get_num_sectors(cxt, Index[i]), |
| 688 |
sgi_get_sysid(cxt, Index[i]))); |
| 689 |
} |
| 690 |
if (start < lastblock) { |
| 691 |
if (verbose) |
| 692 |
fdisk_info(cxt, P_("Unused gap of %8u sector: sector %8u", |
| 693 |
"Unused gap of %8u sectors: sectors %8u-%u", |
| 694 |
lastblock - start), |
| 695 |
lastblock - start, start, lastblock-1); |
| 696 |
gap += lastblock - start; |
| 697 |
add_to_freelist(cxt, start, lastblock); |
| 698 |
} |
| 699 |
/* |
| 700 |
* Done with arithmetic. Go for details now. |
| 701 |
*/ |
| 702 |
if (verbose) { |
| 703 |
if (sgi_get_bootpartition(cxt) < 0 |
| 704 |
|| !sgi_get_num_sectors(cxt, sgi_get_bootpartition(cxt))) |
| 705 |
fdisk_info(cxt, _("The boot partition does not exist.")); |
| 706 |
|
| 707 |
if (sgi_get_swappartition(cxt) < 0 |
| 708 |
|| !sgi_get_num_sectors(cxt, sgi_get_swappartition(cxt))) |
| 709 |
fdisk_info(cxt, _("The swap partition does not exist.")); |
| 710 |
|
| 711 |
else if (sgi_get_sysid(cxt, sgi_get_swappartition(cxt)) != SGI_TYPE_SWAP |
| 712 |
&& sgi_get_sysid(cxt, sgi_get_swappartition(cxt)) != MBR_LINUX_SWAP_PARTITION) |
| 713 |
fdisk_info(cxt, _("The swap partition has no swap type.")); |
| 714 |
|
| 715 |
if (sgi_check_bootfile(cxt, "/unix")) |
| 716 |
fdisk_info(cxt, _("You have chosen an unusual bootfile name.")); |
| 717 |
} |
| 718 |
|
| 719 |
return (gap > 0) ? 1 : (gap == 0) ? 0 : -1; |
| 720 |
} |
| 721 |
|
| 722 |
static int sgi_verify_disklabel(struct fdisk_context *cxt) |
| 723 |
{ |
| 724 |
return verify_disklabel(cxt, 1); |
| 725 |
} |
| 726 |
|
| 727 |
static int sgi_gaps(struct fdisk_context *cxt) |
| 728 |
{ |
| 729 |
/* |
| 730 |
* returned value is: |
| 731 |
* = 0 : disk is properly filled to the rim |
| 732 |
* < 0 : there is an overlap |
| 733 |
* > 0 : there is still some vacant space |
| 734 |
*/ |
| 735 |
return verify_disklabel(cxt, 0); |
| 736 |
} |
| 737 |
|
| 738 |
/* Returns partition index of first entry marked as entire disk. */ |
| 739 |
static int sgi_entire(struct fdisk_context *cxt) |
| 740 |
{ |
| 741 |
size_t i; |
| 742 |
|
| 743 |
for (i = 0; i < SGI_MAXPARTITIONS; i++) |
| 744 |
if (sgi_get_sysid(cxt, i) == SGI_TYPE_ENTIRE_DISK) |
| 745 |
return i; |
| 746 |
return -1; |
| 747 |
} |
| 748 |
|
| 749 |
static int set_partition(struct fdisk_context *cxt, size_t i, |
| 750 |
unsigned int start, unsigned int length, int sys) |
| 751 |
{ |
| 752 |
struct sgi_disklabel *sgilabel; |
| 753 |
|
| 754 |
assert(cxt); |
| 755 |
assert(cxt->label); |
| 756 |
assert(fdisk_is_label(cxt, SGI)); |
| 757 |
|
| 758 |
sgilabel = self_disklabel(cxt); |
| 759 |
sgilabel->partitions[i].type = cpu_to_be32(sys); |
| 760 |
sgilabel->partitions[i].num_blocks = cpu_to_be32(length); |
| 761 |
sgilabel->partitions[i].first_block = cpu_to_be32(start); |
| 762 |
|
| 763 |
fdisk_label_set_changed(cxt->label, 1); |
| 764 |
|
| 765 |
if (sgi_gaps(cxt) < 0) /* rebuild freelist */ |
| 766 |
fdisk_warnx(cxt, _("Partition overlap on the disk.")); |
| 767 |
if (length) { |
| 768 |
struct fdisk_parttype *t = |
| 769 |
fdisk_label_get_parttype_from_code(cxt->label, sys); |
| 770 |
fdisk_info_new_partition(cxt, i + 1, start, start + length, t); |
| 771 |
} |
| 772 |
|
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
static void sgi_set_entire(struct fdisk_context *cxt) |
| 777 |
{ |
| 778 |
size_t n; |
| 779 |
|
| 780 |
for (n = 10; n < cxt->label->nparts_max; n++) { |
| 781 |
if (!sgi_get_num_sectors(cxt, n)) { |
| 782 |
set_partition(cxt, n, 0, sgi_get_lastblock(cxt), SGI_TYPE_ENTIRE_DISK); |
| 783 |
break; |
| 784 |
} |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
static void sgi_set_volhdr(struct fdisk_context *cxt) |
| 789 |
{ |
| 790 |
size_t n; |
| 791 |
|
| 792 |
for (n = 8; n < cxt->label->nparts_max; n++) { |
| 793 |
if (!sgi_get_num_sectors(cxt, n)) { |
| 794 |
/* Choose same default volume header size as IRIX fx uses. */ |
| 795 |
if (4096 < sgi_get_lastblock(cxt)) |
| 796 |
set_partition(cxt, n, 0, 4096, SGI_TYPE_VOLHDR); |
| 797 |
break; |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
static int sgi_delete_partition(struct fdisk_context *cxt, size_t partnum) |
| 803 |
{ |
| 804 |
int rc; |
| 805 |
|
| 806 |
assert(cxt); |
| 807 |
assert(cxt->label); |
| 808 |
|
| 809 |
if (partnum > cxt->label->nparts_max) |
| 810 |
return -EINVAL; |
| 811 |
|
| 812 |
rc = set_partition(cxt, partnum, 0, 0, 0); |
| 813 |
|
| 814 |
cxt->label->nparts_cur = count_used_partitions(cxt); |
| 815 |
|
| 816 |
return rc; |
| 817 |
} |
| 818 |
|
| 819 |
static int sgi_add_partition(struct fdisk_context *cxt, |
| 820 |
struct fdisk_partition *pa, |
| 821 |
size_t *partno) |
| 822 |
{ |
| 823 |
struct fdisk_sgi_label *sgi; |
| 824 |
char mesg[256]; |
| 825 |
unsigned int first = 0, last = 0; |
| 826 |
struct fdisk_ask *ask; |
| 827 |
int sys = pa && pa->type ? pa->type->code : SGI_TYPE_XFS; |
| 828 |
int rc; |
| 829 |
size_t n; |
| 830 |
|
| 831 |
assert(cxt); |
| 832 |
assert(cxt->label); |
| 833 |
assert(fdisk_is_label(cxt, SGI)); |
| 834 |
|
| 835 |
rc = fdisk_partition_next_partno(pa, cxt, &n); |
| 836 |
if (rc) |
| 837 |
return rc; |
| 838 |
if (n == 10) |
| 839 |
sys = SGI_TYPE_ENTIRE_DISK; |
| 840 |
else if (n == 8) |
| 841 |
sys = 0; |
| 842 |
|
| 843 |
sgi = self_label(cxt); |
| 844 |
|
| 845 |
if (sgi_get_num_sectors(cxt, n)) { |
| 846 |
fdisk_warnx(cxt, _("Partition %zu is already defined. " |
| 847 |
"Delete it before re-adding it."), n + 1); |
| 848 |
return -EINVAL; |
| 849 |
} |
| 850 |
if (!cxt->script && sgi_entire(cxt) == -1 && sys != SGI_TYPE_ENTIRE_DISK) { |
| 851 |
fdisk_info(cxt, _("Attempting to generate entire disk entry automatically.")); |
| 852 |
sgi_set_entire(cxt); |
| 853 |
sgi_set_volhdr(cxt); |
| 854 |
} |
| 855 |
if (sgi_gaps(cxt) == 0 && sys != SGI_TYPE_ENTIRE_DISK) { |
| 856 |
fdisk_warnx(cxt, _("The entire disk is already covered with partitions.")); |
| 857 |
return -EINVAL; |
| 858 |
} |
| 859 |
if (sgi_gaps(cxt) < 0) { |
| 860 |
fdisk_warnx(cxt, _("You got a partition overlap on the disk. Fix it first!")); |
| 861 |
return -EINVAL; |
| 862 |
} |
| 863 |
|
| 864 |
if (sys == SGI_TYPE_ENTIRE_DISK) { |
| 865 |
first = 0; |
| 866 |
last = sgi_get_lastblock(cxt); |
| 867 |
} else { |
| 868 |
first = sgi->freelist[0].first; |
| 869 |
last = sgi->freelist[0].last; |
| 870 |
} |
| 871 |
|
| 872 |
/* first sector */ |
| 873 |
if (pa && pa->start_follow_default) |
| 874 |
; |
| 875 |
else if (pa && fdisk_partition_has_start(pa)) { |
| 876 |
first = pa->start; |
| 877 |
last = is_in_freelist(cxt, first); |
| 878 |
|
| 879 |
if (sys != SGI_TYPE_ENTIRE_DISK && !last) |
| 880 |
return -ERANGE; |
| 881 |
} else { |
| 882 |
snprintf(mesg, sizeof(mesg), _("First %s"), |
| 883 |
fdisk_get_unit(cxt, FDISK_SINGULAR)); |
| 884 |
ask = fdisk_new_ask(); |
| 885 |
if (!ask) |
| 886 |
return -ENOMEM; |
| 887 |
|
| 888 |
fdisk_ask_set_query(ask, mesg); |
| 889 |
fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER); |
| 890 |
|
| 891 |
fdisk_ask_number_set_low(ask, fdisk_scround(cxt, first)); /* minimal */ |
| 892 |
fdisk_ask_number_set_default(ask, fdisk_scround(cxt, first)); /* default */ |
| 893 |
fdisk_ask_number_set_high(ask, fdisk_scround(cxt, last) - 1); /* maximal */ |
| 894 |
|
| 895 |
rc = fdisk_do_ask(cxt, ask); |
| 896 |
first = fdisk_ask_number_get_result(ask); |
| 897 |
fdisk_unref_ask(ask); |
| 898 |
|
| 899 |
if (rc) |
| 900 |
return rc; |
| 901 |
if (fdisk_use_cylinders(cxt)) |
| 902 |
first *= fdisk_get_units_per_sector(cxt); |
| 903 |
} |
| 904 |
|
| 905 |
if (first && sys == SGI_TYPE_ENTIRE_DISK) |
| 906 |
fdisk_info(cxt, _("It is highly recommended that the " |
| 907 |
"eleventh partition covers the entire " |
| 908 |
"disk and is of type 'SGI volume'.")); |
| 909 |
if (!last) |
| 910 |
last = is_in_freelist(cxt, first); |
| 911 |
|
| 912 |
/* last sector */ |
| 913 |
if (pa && pa->end_follow_default) |
| 914 |
last -= 1ULL; |
| 915 |
else if (pa && fdisk_partition_has_size(pa)) { |
| 916 |
if (first + pa->size - 1ULL > last) |
| 917 |
return -ERANGE; |
| 918 |
last = first + pa->size - 1ULL; |
| 919 |
} else { |
| 920 |
snprintf(mesg, sizeof(mesg), |
| 921 |
_("Last %s or +%s or +size{K,M,G,T,P}"), |
| 922 |
fdisk_get_unit(cxt, FDISK_SINGULAR), |
| 923 |
fdisk_get_unit(cxt, FDISK_PLURAL)); |
| 924 |
|
| 925 |
ask = fdisk_new_ask(); |
| 926 |
if (!ask) |
| 927 |
return -ENOMEM; |
| 928 |
|
| 929 |
fdisk_ask_set_query(ask, mesg); |
| 930 |
fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET); |
| 931 |
|
| 932 |
fdisk_ask_number_set_low(ask, fdisk_scround(cxt, first)); /* minimal */ |
| 933 |
fdisk_ask_number_set_default(ask, fdisk_scround(cxt, last) - 1);/* default */ |
| 934 |
fdisk_ask_number_set_high(ask, fdisk_scround(cxt, last) - 1);/* maximal */ |
| 935 |
fdisk_ask_number_set_base(ask, fdisk_scround(cxt, first)); |
| 936 |
fdisk_ask_number_set_wrap_negative(ask, 1); /* wrap negative around high */ |
| 937 |
|
| 938 |
if (fdisk_use_cylinders(cxt)) |
| 939 |
fdisk_ask_number_set_unit(ask, |
| 940 |
cxt->sector_size * |
| 941 |
fdisk_get_units_per_sector(cxt)); |
| 942 |
else |
| 943 |
fdisk_ask_number_set_unit(ask,cxt->sector_size); |
| 944 |
|
| 945 |
rc = fdisk_do_ask(cxt, ask); |
| 946 |
last = fdisk_ask_number_get_result(ask) + 1; |
| 947 |
|
| 948 |
fdisk_unref_ask(ask); |
| 949 |
if (rc) |
| 950 |
return rc; |
| 951 |
if (fdisk_use_cylinders(cxt)) |
| 952 |
last *= fdisk_get_units_per_sector(cxt); |
| 953 |
} |
| 954 |
|
| 955 |
if (sys == SGI_TYPE_ENTIRE_DISK |
| 956 |
&& (first != 0 || last != sgi_get_lastblock(cxt))) |
| 957 |
fdisk_info(cxt, _("It is highly recommended that the " |
| 958 |
"eleventh partition covers the entire " |
| 959 |
"disk and is of type 'SGI volume'.")); |
| 960 |
|
| 961 |
set_partition(cxt, n, first, last - first, sys); |
| 962 |
cxt->label->nparts_cur = count_used_partitions(cxt); |
| 963 |
if (partno) |
| 964 |
*partno = n; |
| 965 |
return 0; |
| 966 |
} |
| 967 |
|
| 968 |
static int sgi_create_disklabel(struct fdisk_context *cxt) |
| 969 |
{ |
| 970 |
struct fdisk_sgi_label *sgi; |
| 971 |
struct sgi_disklabel *sgilabel; |
| 972 |
int rc; |
| 973 |
|
| 974 |
assert(cxt); |
| 975 |
assert(cxt->label); |
| 976 |
assert(fdisk_is_label(cxt, SGI)); |
| 977 |
|
| 978 |
if (cxt->geom.heads && cxt->geom.sectors) { |
| 979 |
fdisk_sector_t llsectors; |
| 980 |
|
| 981 |
if (blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &llsectors) == 0) { |
| 982 |
/* the get device size ioctl was successful */ |
| 983 |
fdisk_sector_t llcyls; |
| 984 |
int sec_fac = cxt->sector_size / 512; |
| 985 |
|
| 986 |
llcyls = llsectors / (cxt->geom.heads * cxt->geom.sectors * sec_fac); |
| 987 |
cxt->geom.cylinders = llcyls; |
| 988 |
if (cxt->geom.cylinders != llcyls) /* truncated? */ |
| 989 |
cxt->geom.cylinders = ~0; |
| 990 |
} else { |
| 991 |
/* otherwise print error and use truncated version */ |
| 992 |
fdisk_warnx(cxt, |
| 993 |
_("BLKGETSIZE ioctl failed on %s. " |
| 994 |
"Using geometry cylinder value of %ju. " |
| 995 |
"This value may be truncated for devices " |
| 996 |
"> 33.8 GB."), cxt->dev_path, |
| 997 |
(uintmax_t) cxt->geom.cylinders); |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
rc = fdisk_init_firstsector_buffer(cxt, 0, 0); |
| 1002 |
if (rc) |
| 1003 |
return rc; |
| 1004 |
|
| 1005 |
sgi = (struct fdisk_sgi_label *) cxt->label; |
| 1006 |
sgi->header = (struct sgi_disklabel *) cxt->firstsector; |
| 1007 |
|
| 1008 |
sgilabel = sgi->header; |
| 1009 |
|
| 1010 |
sgilabel->magic = cpu_to_be32(SGI_LABEL_MAGIC); |
| 1011 |
sgilabel->root_part_num = cpu_to_be16(0); |
| 1012 |
sgilabel->swap_part_num = cpu_to_be16(1); |
| 1013 |
|
| 1014 |
/* sizeof(sgilabel->boot_file) = 16 > 6 */ |
| 1015 |
memset(sgilabel->boot_file, 0, 16); |
| 1016 |
strncpy((char *) sgilabel->boot_file, "/unix", |
| 1017 |
sizeof(sgilabel->boot_file)); |
| 1018 |
|
| 1019 |
sgilabel->devparam.skew = (0); |
| 1020 |
sgilabel->devparam.gap1 = (0); |
| 1021 |
sgilabel->devparam.gap2 = (0); |
| 1022 |
sgilabel->devparam.sparecyl = (0); |
| 1023 |
sgilabel->devparam.pcylcount = cpu_to_be16(cxt->geom.cylinders); |
| 1024 |
sgilabel->devparam.head_vol0 = cpu_to_be16(0); |
| 1025 |
sgilabel->devparam.ntrks = cpu_to_be16(cxt->geom.heads); |
| 1026 |
/* tracks/cylinder (heads) */ |
| 1027 |
sgilabel->devparam.cmd_tag_queue_depth = (0); |
| 1028 |
sgilabel->devparam.unused0 = (0); |
| 1029 |
sgilabel->devparam.unused1 = cpu_to_be16(0); |
| 1030 |
sgilabel->devparam.nsect = cpu_to_be16(cxt->geom.sectors); |
| 1031 |
/* sectors/track */ |
| 1032 |
sgilabel->devparam.bytes = cpu_to_be16(cxt->sector_size); |
| 1033 |
sgilabel->devparam.ilfact = cpu_to_be16(1); |
| 1034 |
sgilabel->devparam.flags = cpu_to_be32( |
| 1035 |
SGI_DEVPARAM_TRACK_FWD |
| 1036 |
| SGI_DEVPARAM_IGNORE_ERRORS |
| 1037 |
| SGI_DEVPARAM_RESEEK); |
| 1038 |
sgilabel->devparam.datarate = cpu_to_be32(0); |
| 1039 |
sgilabel->devparam.retries_on_error = cpu_to_be32(1); |
| 1040 |
sgilabel->devparam.ms_per_word = cpu_to_be32(0); |
| 1041 |
sgilabel->devparam.xylogics_gap1 = cpu_to_be16(0); |
| 1042 |
sgilabel->devparam.xylogics_syncdelay = cpu_to_be16(0); |
| 1043 |
sgilabel->devparam.xylogics_readdelay = cpu_to_be16(0); |
| 1044 |
sgilabel->devparam.xylogics_gap2 = cpu_to_be16(0); |
| 1045 |
sgilabel->devparam.xylogics_readgate = cpu_to_be16(0); |
| 1046 |
sgilabel->devparam.xylogics_writecont = cpu_to_be16(0); |
| 1047 |
|
| 1048 |
memset(&(sgilabel->volume), 0, |
| 1049 |
sizeof(struct sgi_volume) * SGI_MAXVOLUMES); |
| 1050 |
memset(&(sgilabel->partitions), 0, |
| 1051 |
sizeof(struct sgi_partition) * SGI_MAXPARTITIONS); |
| 1052 |
cxt->label->nparts_max = SGI_MAXPARTITIONS; |
| 1053 |
|
| 1054 |
/* don't create default layout when a script defined */ |
| 1055 |
if (!cxt->script) { |
| 1056 |
sgi_set_entire(cxt); |
| 1057 |
sgi_set_volhdr(cxt); |
| 1058 |
} |
| 1059 |
cxt->label->nparts_cur = count_used_partitions(cxt); |
| 1060 |
|
| 1061 |
fdisk_info(cxt, _("Created a new SGI disklabel.")); |
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static int sgi_set_partition(struct fdisk_context *cxt, |
| 1066 |
size_t i, |
| 1067 |
struct fdisk_partition *pa) |
| 1068 |
{ |
| 1069 |
struct sgi_disklabel *sgilabel; |
| 1070 |
|
| 1071 |
if (i >= cxt->label->nparts_max) |
| 1072 |
return -EINVAL; |
| 1073 |
|
| 1074 |
sgilabel = self_disklabel(cxt); |
| 1075 |
|
| 1076 |
if (pa->type) { |
| 1077 |
struct fdisk_parttype *t = pa->type; |
| 1078 |
|
| 1079 |
if (sgi_get_num_sectors(cxt, i) == 0) /* caught already before, ... */ { |
| 1080 |
fdisk_warnx(cxt, _("Sorry, only for non-empty partitions you can change the tag.")); |
| 1081 |
return -EINVAL; |
| 1082 |
} |
| 1083 |
|
| 1084 |
if ((i == 10 && t->code != SGI_TYPE_ENTIRE_DISK) |
| 1085 |
|| (i == 8 && t->code != 0)) |
| 1086 |
fdisk_info(cxt, _("Consider leaving partition 9 as volume header (0), " |
| 1087 |
"and partition 11 as entire volume (6), " |
| 1088 |
"as IRIX expects it.")); |
| 1089 |
|
| 1090 |
if (cxt->script == NULL |
| 1091 |
&& ((t->code != SGI_TYPE_ENTIRE_DISK) && (t->code != SGI_TYPE_VOLHDR)) |
| 1092 |
&& (sgi_get_start_sector(cxt, i) < 1)) { |
| 1093 |
int yes = 0; |
| 1094 |
fdisk_ask_yesno(cxt, |
| 1095 |
_("It is highly recommended that the partition at offset 0 " |
| 1096 |
"is of type \"SGI volhdr\", the IRIX system will rely on it to " |
| 1097 |
"retrieve from its directory standalone tools like sash and fx. " |
| 1098 |
"Only the \"SGI volume\" entire disk section may violate this. " |
| 1099 |
"Are you sure about tagging this partition differently?"), &yes); |
| 1100 |
if (!yes) |
| 1101 |
return 1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
sgilabel->partitions[i].type = cpu_to_be32(t->code); |
| 1105 |
} |
| 1106 |
|
| 1107 |
if (fdisk_partition_has_start(pa)) |
| 1108 |
sgilabel->partitions[i].first_block = cpu_to_be32(pa->start); |
| 1109 |
if (fdisk_partition_has_size(pa)) |
| 1110 |
sgilabel->partitions[i].num_blocks = cpu_to_be32(pa->size); |
| 1111 |
|
| 1112 |
fdisk_label_set_changed(cxt->label, 1); |
| 1113 |
return 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
static int sgi_partition_is_used( |
| 1118 |
struct fdisk_context *cxt, |
| 1119 |
size_t i) |
| 1120 |
{ |
| 1121 |
assert(cxt); |
| 1122 |
assert(fdisk_is_label(cxt, SGI)); |
| 1123 |
|
| 1124 |
if (i >= cxt->label->nparts_max) |
| 1125 |
return 0; |
| 1126 |
return sgi_get_num_sectors(cxt, i) ? 1 : 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static int sgi_toggle_partition_flag(struct fdisk_context *cxt, size_t i, unsigned long flag) |
| 1130 |
{ |
| 1131 |
struct sgi_disklabel *sgilabel; |
| 1132 |
assert(cxt); |
| 1133 |
assert(cxt->label); |
| 1134 |
assert(fdisk_is_label(cxt, SGI)); |
| 1135 |
|
| 1136 |
if (i >= cxt->label->nparts_max) |
| 1137 |
return -EINVAL; |
| 1138 |
|
| 1139 |
sgilabel = self_disklabel(cxt); |
| 1140 |
|
| 1141 |
switch (flag) { |
| 1142 |
case SGI_FLAG_BOOT: |
| 1143 |
sgilabel->root_part_num = |
| 1144 |
be16_to_cpu(sgilabel->root_part_num) == i ? |
| 1145 |
0 : cpu_to_be16(i); |
| 1146 |
fdisk_label_set_changed(cxt->label, 1); |
| 1147 |
break; |
| 1148 |
case SGI_FLAG_SWAP: |
| 1149 |
sgilabel->swap_part_num = |
| 1150 |
be16_to_cpu(sgilabel->swap_part_num) == i ? |
| 1151 |
0 : cpu_to_be16(i); |
| 1152 |
fdisk_label_set_changed(cxt->label, 1); |
| 1153 |
break; |
| 1154 |
default: |
| 1155 |
return 1; |
| 1156 |
} |
| 1157 |
|
| 1158 |
return 0; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static const struct fdisk_field sgi_fields[] = |
| 1162 |
{ |
| 1163 |
{ FDISK_FIELD_DEVICE, N_("Device"), 10, 0 }, |
| 1164 |
{ FDISK_FIELD_START, N_("Start"), 5, FDISK_FIELDFL_NUMBER }, |
| 1165 |
{ FDISK_FIELD_END, N_("End"), 5, FDISK_FIELDFL_NUMBER }, |
| 1166 |
{ FDISK_FIELD_SECTORS, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER }, |
| 1167 |
{ FDISK_FIELD_CYLINDERS,N_("Cylinders"), 5, FDISK_FIELDFL_NUMBER }, |
| 1168 |
{ FDISK_FIELD_SIZE, N_("Size"), 5, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_EYECANDY }, |
| 1169 |
{ FDISK_FIELD_TYPEID, N_("Id"), 2, FDISK_FIELDFL_NUMBER }, |
| 1170 |
{ FDISK_FIELD_TYPE, N_("Type"), 0.1, FDISK_FIELDFL_EYECANDY }, |
| 1171 |
{ FDISK_FIELD_ATTR, N_("Attrs"), 0, FDISK_FIELDFL_NUMBER } |
| 1172 |
}; |
| 1173 |
|
| 1174 |
static const struct fdisk_label_operations sgi_operations = |
| 1175 |
{ |
| 1176 |
.probe = sgi_probe_label, |
| 1177 |
.write = sgi_write_disklabel, |
| 1178 |
.verify = sgi_verify_disklabel, |
| 1179 |
.get_item = sgi_get_disklabel_item, |
| 1180 |
.create = sgi_create_disklabel, |
| 1181 |
|
| 1182 |
.get_part = sgi_get_partition, |
| 1183 |
.set_part = sgi_set_partition, |
| 1184 |
.add_part = sgi_add_partition, |
| 1185 |
.del_part = sgi_delete_partition, |
| 1186 |
|
| 1187 |
.part_is_used = sgi_partition_is_used, |
| 1188 |
.part_toggle_flag = sgi_toggle_partition_flag |
| 1189 |
}; |
| 1190 |
|
| 1191 |
/* Allocates an SGI label driver. */ |
| 1192 |
struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt __attribute__ ((__unused__))) |
| 1193 |
{ |
| 1194 |
struct fdisk_label *lb; |
| 1195 |
struct fdisk_sgi_label *sgi; |
| 1196 |
|
| 1197 |
sgi = calloc(1, sizeof(*sgi)); |
| 1198 |
if (!sgi) |
| 1199 |
return NULL; |
| 1200 |
|
| 1201 |
/* initialize generic part of the driver */ |
| 1202 |
lb = (struct fdisk_label *) sgi; |
| 1203 |
lb->name = "sgi"; |
| 1204 |
lb->id = FDISK_DISKLABEL_SGI; |
| 1205 |
lb->op = &sgi_operations; |
| 1206 |
lb->parttypes = sgi_parttypes; |
| 1207 |
lb->nparttypes = ARRAY_SIZE(sgi_parttypes) - 1; |
| 1208 |
lb->fields = sgi_fields; |
| 1209 |
lb->nfields = ARRAY_SIZE(sgi_fields); |
| 1210 |
|
| 1211 |
lb->flags |= FDISK_LABEL_FL_REQUIRE_GEOMETRY; |
| 1212 |
|
| 1213 |
/* return calloc() result to keep static anaylizers happy */ |
| 1214 |
return (struct fdisk_label *) sgi; |
| 1215 |
} |