fdisk: (sun) remove nasty sunlabel macro

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-01-18 14:43:00 +01:00
parent 137b57e56b
commit d6ab93f18a
3 changed files with 125 additions and 60 deletions

View file

@ -588,8 +588,7 @@ get_partition_dflt(struct fdisk_context *cxt, int warn, int max, int dflt) {
if ((!fdisk_is_disklabel(cxt, SUN) && if ((!fdisk_is_disklabel(cxt, SUN) &&
!fdisk_is_disklabel(cxt, SGI) && !pe->part_table->sys_ind) !fdisk_is_disklabel(cxt, SGI) && !pe->part_table->sys_ind)
|| (fdisk_is_disklabel(cxt, SUN) && || (fdisk_is_disklabel(cxt, SUN) &&
(!sunlabel->partitions[i].num_sectors || sun_is_empty_type(cxt, i))
!sunlabel->part_tags[i].tag))
|| (fdisk_is_disklabel(cxt, SGI) && || (fdisk_is_disklabel(cxt, SGI) &&
(!sgi_get_num_sectors(cxt, i)))) (!sgi_get_num_sectors(cxt, i))))
fprintf(stderr, fprintf(stderr,

View file

@ -33,6 +33,7 @@ static int other_endian = 0;
*/ */
struct fdisk_sun_label { struct fdisk_sun_label {
struct fdisk_label head; /* generic part */ struct fdisk_label head; /* generic part */
struct sun_disk_label *header; /* on-disk data (pointer to cxt->firstsector) */
}; };
#define is_sun_label(_l) ((_l)->id == FDISK_DISKLABEL_SUN) #define is_sun_label(_l) ((_l)->id == FDISK_DISKLABEL_SUN)
@ -69,10 +70,29 @@ static inline uint32_t __swap32(uint32_t x) {
#define SSWAP32(x) (other_endian ? __swap32(x) \ #define SSWAP32(x) (other_endian ? __swap32(x) \
: (uint32_t)(x)) : (uint32_t)(x))
static inline struct sun_disk_label *
label_get_sun_disklabel(struct fdisk_label *lb)
{
assert(lb);
assert(is_sun_label(lb));
return ((struct fdisk_sun_label *) lb)->header;
}
#define context_get_sun_disklabel(_c) label_get_sun_disklabel((_c)->label)
int sun_is_empty_type(struct fdisk_context *cxt, int i)
{
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
return (!sunlabel->partitions[i].num_sectors ||
!sunlabel->part_tags[i].tag);
}
static void set_sun_partition(struct fdisk_context *cxt, static void set_sun_partition(struct fdisk_context *cxt,
int i, uint32_t start, uint32_t stop, uint16_t sysid) int i, uint32_t start, uint32_t stop, uint16_t sysid)
{ {
assert(cxt); struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->part_tags[i].tag = SSWAP16(sysid); sunlabel->part_tags[i].tag = SSWAP16(sysid);
sunlabel->part_tags[i].flag = SSWAP16(0); sunlabel->part_tags[i].flag = SSWAP16(0);
@ -84,11 +104,12 @@ static void set_sun_partition(struct fdisk_context *cxt,
print_partition_size(cxt, i + 1, start, stop, sysid); print_partition_size(cxt, i + 1, start, stop, sysid);
} }
static size_t partitions_in_use(struct fdisk_context *cxt, struct fdisk_label *lb) static size_t partitions_in_use(struct fdisk_label *lb)
{ {
struct sun_disk_label *sunlabel = label_get_sun_disklabel(lb);
size_t ct = 0, i; size_t ct = 0, i;
assert(is_sun_label(lb)); assert(sunlabel);
for (i = 0; i < lb->nparts_max; i++) { for (i = 0; i < lb->nparts_max; i++) {
if (sunlabel->partitions[i].num_sectors) if (sunlabel->partitions[i].num_sectors)
@ -97,20 +118,24 @@ static size_t partitions_in_use(struct fdisk_context *cxt, struct fdisk_label *l
return ct; return ct;
} }
static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb)
{ {
struct fdisk_sun_label *sun;
struct sun_disk_label *sunlabel;
unsigned short *ush; unsigned short *ush;
int csum; int csum;
int need_fixing = 0;
assert(cxt); assert(cxt);
assert(lb); assert(lb);
assert(is_sun_label(lb)); assert(is_sun_label(lb));
sunlabel = (struct sun_disk_label *) cxt->firstsector;
if (sunlabel->magic != SUN_LABEL_MAGIC && if (sunlabel->magic != SUN_LABEL_MAGIC &&
sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) { sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) {
other_endian = 0; other_endian = 0;
return 0; return 0; /* failed */
} }
other_endian = (sunlabel->magic == SUN_LABEL_MAGIC_SWAPPED); other_endian = (sunlabel->magic == SUN_LABEL_MAGIC_SWAPPED);
@ -124,48 +149,52 @@ static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb)
"Probably you'll have to set all the values,\n" "Probably you'll have to set all the values,\n"
"e.g. heads, sectors, cylinders and partitions\n" "e.g. heads, sectors, cylinders and partitions\n"
"or force a fresh label (s command in main menu)\n")); "or force a fresh label (s command in main menu)\n"));
} else { return 1;
int need_fixing = 0;
lb->nparts_max = SUN_NUM_PARTITIONS;
cxt->geom.heads = SSWAP16(sunlabel->nhead);
cxt->geom.cylinders = SSWAP16(sunlabel->ncyl);
cxt->geom.sectors = SSWAP16(sunlabel->nsect);
if (sunlabel->version != SSWAP32(SUN_LABEL_VERSION)) {
fprintf(stderr,_("Detected sun disklabel with wrong version [0x%08x].\n"),
SSWAP32(sunlabel->version));
need_fixing = 1;
}
if (sunlabel->sanity != SSWAP32(SUN_LABEL_SANE)) {
fprintf(stderr,_("Detected sun disklabel with wrong sanity [0x%08x].\n"),
SSWAP32(sunlabel->sanity));
need_fixing = 1;
}
if (sunlabel->num_partitions != SSWAP16(SUN_NUM_PARTITIONS)) {
fprintf(stderr,_("Detected sun disklabel with wrong num_partitions [%u].\n"),
SSWAP16(sunlabel->num_partitions));
need_fixing = 1;
}
if (need_fixing) {
fprintf(stderr, _("Warning: Wrong values need to be "
"fixed up and will be corrected "
"by w(rite)\n"));
sunlabel->version = SSWAP32(SUN_LABEL_VERSION);
sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
sunlabel->num_partitions = SSWAP16(SUN_NUM_PARTITIONS);
ush = (unsigned short *)sunlabel;
csum = 0;
while(ush < (unsigned short *)(&sunlabel->cksum))
csum ^= *ush++;
sunlabel->cksum = csum;
set_changed(0);
}
lb->nparts_cur = partitions_in_use(cxt, lb);
} }
/* map first sector buffer to sun header */
sun = (struct fdisk_sun_label *) lb;
sun->header = (struct sun_disk_label *) cxt->firstsector;
lb->nparts_max = SUN_NUM_PARTITIONS;
cxt->geom.heads = SSWAP16(sunlabel->nhead);
cxt->geom.cylinders = SSWAP16(sunlabel->ncyl);
cxt->geom.sectors = SSWAP16(sunlabel->nsect);
if (sunlabel->version != SSWAP32(SUN_LABEL_VERSION)) {
fprintf(stderr,_("Detected sun disklabel with wrong version [0x%08x].\n"),
SSWAP32(sunlabel->version));
need_fixing = 1;
}
if (sunlabel->sanity != SSWAP32(SUN_LABEL_SANE)) {
fprintf(stderr,_("Detected sun disklabel with wrong sanity [0x%08x].\n"),
SSWAP32(sunlabel->sanity));
need_fixing = 1;
}
if (sunlabel->num_partitions != SSWAP16(SUN_NUM_PARTITIONS)) {
fprintf(stderr,_("Detected sun disklabel with wrong num_partitions [%u].\n"),
SSWAP16(sunlabel->num_partitions));
need_fixing = 1;
}
if (need_fixing) {
fprintf(stderr, _("Warning: Wrong values need to be "
"fixed up and will be corrected "
"by w(rite)\n"));
sunlabel->version = SSWAP32(SUN_LABEL_VERSION);
sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
sunlabel->num_partitions = SSWAP16(SUN_NUM_PARTITIONS);
ush = (unsigned short *)sunlabel;
csum = 0;
while(ush < (unsigned short *)(&sunlabel->cksum))
csum ^= *ush++;
sunlabel->cksum = csum;
set_changed(0);
}
lb->nparts_cur = partitions_in_use(lb);
return 1; return 1;
} }
@ -176,12 +205,22 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l
unsigned int ndiv, sec_fac; unsigned int ndiv, sec_fac;
int res; int res;
struct fdisk_sun_label *sun; /* libfdisk sun handler */
struct sun_disk_label *sunlabel; /* on disk data */
assert(cxt); assert(cxt);
assert(lb); assert(lb);
assert(is_sun_label(lb)); assert(is_sun_label(lb));
fprintf(stderr, _("Building a new Sun disklabel.\n")); fprintf(stderr, _("Building a new Sun disklabel.\n"));
/* map first sector to header */
fdisk_zeroize_firstsector(cxt);
sun = (struct fdisk_sun_label *) lb;
sun->header = (struct sun_disk_label *) cxt->firstsector;
sunlabel = sun->header;
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
other_endian = 1; other_endian = 1;
#else #else
@ -250,7 +289,9 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l
SUN_TAG_LINUX_SWAP); SUN_TAG_LINUX_SWAP);
sunlabel->part_tags[1].flag |= SSWAP16(SUN_FLAG_UNMNT); sunlabel->part_tags[1].flag |= SSWAP16(SUN_FLAG_UNMNT);
set_sun_partition(cxt, 2, 0, cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors, SUN_TAG_BACKUP); set_sun_partition(cxt, 2, 0,
cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors,
SUN_TAG_BACKUP);
{ {
unsigned short *ush = (unsigned short *)sunlabel; unsigned short *ush = (unsigned short *)sunlabel;
@ -262,13 +303,14 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l
set_all_unchanged(); set_all_unchanged();
set_changed(0); set_changed(0);
lb->nparts_cur = partitions_in_use(cxt, lb); lb->nparts_cur = partitions_in_use(lb);
return 0; return 0;
} }
void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask) void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
struct sun_tag_flag *p = &sunlabel->part_tags[i]; struct sun_tag_flag *p = &sunlabel->part_tags[i];
p->flag ^= SSWAP16(mask); p->flag ^= SSWAP16(mask);
@ -283,6 +325,7 @@ static void fetch_sun(struct fdisk_context *cxt,
uint32_t *start, uint32_t *start,
uint32_t *stop) uint32_t *stop)
{ {
struct sun_disk_label *sunlabel;
int continuous = 1; int continuous = 1;
size_t i; size_t i;
@ -290,6 +333,8 @@ static void fetch_sun(struct fdisk_context *cxt,
assert(lb); assert(lb);
assert(is_sun_label(lb)); assert(is_sun_label(lb));
sunlabel = label_get_sun_disklabel(lb);
*start = 0; *start = 0;
*stop = cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors; *stop = cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors;
@ -413,6 +458,7 @@ static int sun_add_partition(
int n, int n,
struct fdisk_parttype *t) struct fdisk_parttype *t)
{ {
struct sun_disk_label *sunlabel = label_get_sun_disklabel(lb);
uint32_t starts[SUN_NUM_PARTITIONS], lens[SUN_NUM_PARTITIONS]; uint32_t starts[SUN_NUM_PARTITIONS], lens[SUN_NUM_PARTITIONS];
struct sun_partition *part = &sunlabel->partitions[n]; struct sun_partition *part = &sunlabel->partitions[n];
struct sun_tag_flag *tag = &sunlabel->part_tags[n]; struct sun_tag_flag *tag = &sunlabel->part_tags[n];
@ -423,10 +469,6 @@ static int sun_add_partition(
size_t i; size_t i;
unsigned int first, last; unsigned int first, last;
assert(cxt);
assert(lb);
assert(is_sun_label(lb));
if (part->num_sectors && tag->tag != SSWAP16(SUN_TAG_UNASSIGNED)) { if (part->num_sectors && tag->tag != SSWAP16(SUN_TAG_UNASSIGNED)) {
printf(_("Partition %d is already defined. Delete " printf(_("Partition %d is already defined. Delete "
"it before re-adding it.\n"), n + 1); "it before re-adding it.\n"), n + 1);
@ -534,7 +576,7 @@ and is of type `Whole disk'\n"));
sys = SUN_TAG_BACKUP; sys = SUN_TAG_BACKUP;
set_sun_partition(cxt, n, first, last, sys); set_sun_partition(cxt, n, first, last, sys);
lb->nparts_cur = partitions_in_use(cxt, lb); lb->nparts_cur = partitions_in_use(lb);
return 0; return 0;
} }
@ -542,14 +584,19 @@ static int sun_delete_partition(struct fdisk_context *cxt,
struct fdisk_label *lb, struct fdisk_label *lb,
int partnum) int partnum)
{ {
struct sun_partition *part = &sunlabel->partitions[partnum]; struct sun_disk_label *sunlabel;
struct sun_tag_flag *tag = &sunlabel->part_tags[partnum]; struct sun_partition *part;
struct sun_tag_flag *tag;
unsigned int nsec; unsigned int nsec;
assert(cxt); assert(cxt);
assert(lb); assert(lb);
assert(is_sun_label(lb)); assert(is_sun_label(lb));
sunlabel = label_get_sun_disklabel(lb);
part = &sunlabel->partitions[partnum];
tag = &sunlabel->part_tags[partnum];
if (partnum == 2 && if (partnum == 2 &&
tag->tag == SSWAP16(SUN_TAG_BACKUP) && tag->tag == SSWAP16(SUN_TAG_BACKUP) &&
!part->start_cylinder && !part->start_cylinder &&
@ -561,13 +608,14 @@ static int sun_delete_partition(struct fdisk_context *cxt,
"sectors\n"), nsec); "sectors\n"), nsec);
tag->tag = SSWAP16(SUN_TAG_UNASSIGNED); tag->tag = SSWAP16(SUN_TAG_UNASSIGNED);
part->num_sectors = 0; part->num_sectors = 0;
lb->nparts_cur = partitions_in_use(cxt, lb); lb->nparts_cur = partitions_in_use(lb);
return 0; return 0;
} }
void sun_list_table(struct fdisk_context *cxt, int xtra) void sun_list_table(struct fdisk_context *cxt, int xtra)
{ {
struct sun_disk_label *sunlabel;
size_t i; size_t i;
int w; int w;
@ -575,6 +623,8 @@ void sun_list_table(struct fdisk_context *cxt, int xtra)
assert(cxt->label); assert(cxt->label);
assert(fdisk_is_disklabel(cxt, SUN)); assert(fdisk_is_disklabel(cxt, SUN));
sunlabel = context_get_sun_disklabel(cxt);
w = strlen(cxt->dev_path); w = strlen(cxt->dev_path);
if (xtra) if (xtra)
printf( printf(
@ -628,6 +678,7 @@ void sun_list_table(struct fdisk_context *cxt, int xtra)
void sun_set_alt_cyl(struct fdisk_context *cxt) void sun_set_alt_cyl(struct fdisk_context *cxt)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->acyl = sunlabel->acyl =
SSWAP16(read_int(cxt, 0,SSWAP16(sunlabel->acyl), 65535, 0, SSWAP16(read_int(cxt, 0,SSWAP16(sunlabel->acyl), 65535, 0,
_("Number of alternate cylinders"))); _("Number of alternate cylinders")));
@ -635,11 +686,13 @@ void sun_set_alt_cyl(struct fdisk_context *cxt)
void sun_set_ncyl(struct fdisk_context *cxt, int cyl) void sun_set_ncyl(struct fdisk_context *cxt, int cyl)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->ncyl = SSWAP16(cyl); sunlabel->ncyl = SSWAP16(cyl);
} }
void sun_set_xcyl(struct fdisk_context *cxt) void sun_set_xcyl(struct fdisk_context *cxt)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->apc = sunlabel->apc =
SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->apc), cxt->geom.sectors, 0, SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->apc), cxt->geom.sectors, 0,
_("Extra sectors per cylinder"))); _("Extra sectors per cylinder")));
@ -647,6 +700,7 @@ void sun_set_xcyl(struct fdisk_context *cxt)
void sun_set_ilfact(struct fdisk_context *cxt) void sun_set_ilfact(struct fdisk_context *cxt)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->intrlv = sunlabel->intrlv =
SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->intrlv), 32, 0, SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->intrlv), 32, 0,
_("Interleave factor"))); _("Interleave factor")));
@ -654,6 +708,7 @@ void sun_set_ilfact(struct fdisk_context *cxt)
void sun_set_rspeed(struct fdisk_context *cxt) void sun_set_rspeed(struct fdisk_context *cxt)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->rpm = sunlabel->rpm =
SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->rpm), 100000, 0, SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->rpm), 100000, 0,
_("Rotation speed (rpm)"))); _("Rotation speed (rpm)")));
@ -661,6 +716,7 @@ void sun_set_rspeed(struct fdisk_context *cxt)
void sun_set_pcylcount(struct fdisk_context *cxt) void sun_set_pcylcount(struct fdisk_context *cxt)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
sunlabel->pcyl = sunlabel->pcyl =
SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->pcyl), 65535, 0, SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->pcyl), 65535, 0,
_("Number of physical cylinders"))); _("Number of physical cylinders")));
@ -669,10 +725,15 @@ void sun_set_pcylcount(struct fdisk_context *cxt)
static int sun_write_disklabel(struct fdisk_context *cxt, static int sun_write_disklabel(struct fdisk_context *cxt,
struct fdisk_label *lb __attribute__((__unused__))) struct fdisk_label *lb __attribute__((__unused__)))
{ {
unsigned short *ush = (unsigned short *)sunlabel; struct sun_disk_label *sunlabel;
unsigned short *ush;
unsigned short csum = 0; unsigned short csum = 0;
assert(cxt); assert(cxt);
assert(lb);
sunlabel = label_get_sun_disklabel(lb);
ush = (unsigned short *) sunlabel;
while(ush < (unsigned short *)(&sunlabel->cksum)) while(ush < (unsigned short *)(&sunlabel->cksum))
csum ^= *ush++; csum ^= *ush++;
@ -690,6 +751,7 @@ static struct fdisk_parttype *sun_get_parttype(
struct fdisk_label *lb, struct fdisk_label *lb,
int n) int n)
{ {
struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt);
struct fdisk_parttype *t; struct fdisk_parttype *t;
assert(cxt); assert(cxt);
@ -711,6 +773,7 @@ static int sun_set_parttype(
int i, int i,
struct fdisk_parttype *t) struct fdisk_parttype *t)
{ {
struct sun_disk_label *sunlabel;
struct sun_partition *part; struct sun_partition *part;
struct sun_tag_flag *tag; struct sun_tag_flag *tag;
@ -718,6 +781,8 @@ static int sun_set_parttype(
assert(lb); assert(lb);
assert(is_sun_label(lb)); assert(is_sun_label(lb));
sunlabel = label_get_sun_disklabel(lb);
if (i < 0 || (size_t) i >= lb->nparts_max || !t || t->type > UINT16_MAX) if (i < 0 || (size_t) i >= lb->nparts_max || !t || t->type > UINT16_MAX)
return -EINVAL; return -EINVAL;

View file

@ -73,7 +73,6 @@ struct sun_disk_label {
#define SUN_LABEL_MAGIC 0xDABE #define SUN_LABEL_MAGIC 0xDABE
#define SUN_LABEL_MAGIC_SWAPPED 0xBEDA #define SUN_LABEL_MAGIC_SWAPPED 0xBEDA
#define sunlabel ((struct sun_disk_label *)cxt->firstsector)
/* fdisksunlabel.c */ /* fdisksunlabel.c */
extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys); extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys);
@ -86,4 +85,6 @@ extern void sun_set_rspeed(struct fdisk_context *cxt);
extern void sun_set_pcylcount(struct fdisk_context *cxt); extern void sun_set_pcylcount(struct fdisk_context *cxt);
extern void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask); extern void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask);
extern int sun_is_empty_type(struct fdisk_context *cxt, int i);
#endif /* FDISK_SUN_LABEL_H */ #endif /* FDISK_SUN_LABEL_H */