From: Karel Zak Date: Wed, 19 Jun 2013 12:32:48 +0000 (+0200) Subject: fdisk: use functions from pt-mbr.h X-Git-Tag: v2.24-rc1~171 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b44244cb98aee904faf3d95a55b9e42cc105cabd;p=thirdparty%2Futil-linux.git fdisk: use functions from pt-mbr.h Signed-off-by: Karel Zak --- diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 593024c97a..8f96a29bde 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -171,8 +171,8 @@ static int xbsd_add_part (struct fdisk_context *cxt, return rc; #if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__) - begin = get_start_sect(xbsd_part); - end = begin + get_nr_sects(xbsd_part) - 1; + begin = dos_partition_get_start(xbsd_part); + end = begin + dos_partition_get_size(xbsd_part) - 1; #else begin = 0; end = xbsd_dlabel.d_secperunit - 1; @@ -289,7 +289,7 @@ bsd_command_prompt (struct fdisk_context *cxt) if (p && is_bsd_partition_type(p->sys_ind)) { xbsd_part = p; xbsd_part_index = t; - ss = get_start_sect(xbsd_part); + ss = dos_partition_get_start(xbsd_part); /* TODO - partname uses static buffer!!! */ cxt->dev_path = partname(cxt->parent->dev_path, t+1, 0); @@ -640,7 +640,7 @@ static int xbsd_write_bootstrap (struct fdisk_context *cxt) sector = 0; alpha_bootblock_checksum(disklabelbuffer); #else - sector = get_start_sect(xbsd_part); + sector = dos_partition_get_start(xbsd_part); #endif if (lseek(cxt->dev_fd, (off_t) sector * SECTOR_SIZE, SEEK_SET) == -1) { fdisk_warn(cxt, _("seek failed %s"), cxt->dev_path); @@ -798,8 +798,8 @@ static int xbsd_initlabel (struct fdisk_context *cxt, d -> d_npartitions = 4; pp = &d -> d_partitions[2]; /* Partition C should be the NetBSD partition */ - pp -> p_offset = get_start_sect(p); - pp -> p_size = get_nr_sects(p); + pp -> p_offset = dos_partition_get_start(p); + pp -> p_size = dos_partition_get_size(p); pp -> p_fstype = BSD_FS_UNUSED; pp = &d -> d_partitions[3]; /* Partition D should be the whole disk */ @@ -832,7 +832,7 @@ xbsd_readlabel (struct fdisk_context *cxt, struct dos_partition *p, struct xbsd_ /* p is used only to get the starting sector */ #if !defined (__alpha__) - sector = (p ? get_start_sect(p) : 0); + sector = (p ? dos_partition_get_start(p) : 0); #elif defined (__alpha__) sector = 0; #endif @@ -871,7 +871,7 @@ xbsd_writelabel (struct fdisk_context *cxt, struct dos_partition *p, struct xbsd unsigned int sector; #if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__) - sector = get_start_sect(p) + BSD_LABELSECTOR; + sector = dos_partition_get_start(p) + BSD_LABELSECTOR; #else sector = BSD_LABELSECTOR; #endif @@ -960,8 +960,8 @@ xbsd_link_part (struct fdisk_context *cxt) p = dos_get_pt_entry(k); - xbsd_dlabel.d_partitions[i].p_size = get_nr_sects(p); - xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p); + xbsd_dlabel.d_partitions[i].p_size = dos_partition_get_size(p); + xbsd_dlabel.d_partitions[i].p_offset = dos_partition_get_start(p); xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind); } #endif diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index 0b3c55e4bf..a8435282c2 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -119,26 +119,6 @@ static void read_pte(struct fdisk_context *cxt, int pno, sector_t offset) pe->pt_entry = pe->ex_entry = NULL; } -static void mbr_set_id(unsigned char *b, unsigned int id) -{ - store4_little_endian(&b[440], id); -} - -static void mbr_set_magic(unsigned char *b) -{ - b[510] = 0x55; - b[511] = 0xaa; -} - -int mbr_is_valid_magic(unsigned char *b) -{ - return (b[510] == 0x55 && b[511] == 0xaa); -} - -static unsigned int mbr_get_id(const unsigned char *b) -{ - return read4_little_endian(&b[440]); -} static void clear_partition(struct dos_partition *p) { @@ -152,8 +132,8 @@ static void clear_partition(struct dos_partition *p) p->eh = 0; p->es = 0; p->ec = 0; - set_start_sect(p,0); - set_nr_sects(p,0); + dos_partition_set_start(p,0); + dos_partition_set_size(p,0); } void dos_init(struct fdisk_context *cxt) @@ -223,15 +203,15 @@ static int dos_delete_partition(struct fdisk_context *cxt, size_t partnum) /* delete this link in the chain */ p = ptes[partnum-1].ex_entry; *p = *q; - set_start_sect(p, get_start_sect(q)); - set_nr_sects(p, get_nr_sects(q)); + dos_partition_set_start(p, dos_partition_get_start(q)); + dos_partition_set_size(p, dos_partition_get_size(q)); ptes[partnum-1].changed = 1; } else if (cxt->label->nparts_max > 5) { /* 5 will be moved to 4 */ /* the first logical in a longer chain */ struct pte *pete = &ptes[5]; if (pete->pt_entry) /* prevent SEGFAULT */ - set_start_sect(pete->pt_entry, + dos_partition_set_start(pete->pt_entry, get_partition_start(pete) - extended_offset); pete->offset = extended_offset; @@ -264,7 +244,7 @@ static void read_extended(struct fdisk_context *cxt, int ext) pex->ex_entry = pex->pt_entry; p = pex->pt_entry; - if (!get_start_sect(p)) { + if (!dos_partition_get_start(p)) { fprintf(stderr, _("Bad offset in primary extended partition\n")); return; @@ -289,13 +269,13 @@ static void read_extended(struct fdisk_context *cxt, int ext) return; } - read_pte(cxt, cxt->label->nparts_max, extended_offset + get_start_sect(p)); + read_pte(cxt, cxt->label->nparts_max, extended_offset + dos_partition_get_start(p)); if (!extended_offset) - extended_offset = get_start_sect(p); + extended_offset = dos_partition_get_start(p); q = p = pt_offset(pe->sectorbuffer, 0); - for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) { + for (i = 0; i < 4; i++, p++) if (dos_partition_get_size(p)) { if (IS_EXTENDED (p->sys_ind)) { if (pe->ex_entry) fprintf(stderr, @@ -338,7 +318,7 @@ static void read_extended(struct fdisk_context *cxt, int ext) for (i = 4; i < cxt->label->nparts_max; i++) { struct pte *pe = &ptes[i]; - if (!get_nr_sects(pe->pt_entry) && + if (!dos_partition_get_size(pe->pt_entry) && (cxt->label->nparts_max > 5 || ptes[4].pt_entry->sys_ind)) { printf(_("omitting empty partition (%zd)\n"), i+1); dos_delete_partition(cxt, i); @@ -544,8 +524,8 @@ static void set_partition(struct fdisk_context *cxt, } p->boot_ind = 0; p->sys_ind = sysid; - set_start_sect(p, start - offset); - set_nr_sects(p, stop - start + 1); + dos_partition_set_start(p, start - offset); + dos_partition_set_size(p, stop - start + 1); if (!doext) { struct fdisk_parttype *t = fdisk_get_parttype_from_code(cxt, sysid); @@ -593,7 +573,7 @@ static void fill_bounds(struct fdisk_context *cxt, last[i] = 0; } else { first[i] = get_partition_start(pe); - last[i] = first[i] + get_nr_sects(p) - 1; + last[i] = first[i] + dos_partition_get_size(p) - 1; } } } @@ -628,12 +608,12 @@ static int add_partition(struct fdisk_context *cxt, int n, struct fdisk_parttype if (extended_offset) { first[ext_index] = extended_offset; - last[ext_index] = get_start_sect(q) + - get_nr_sects(q) - 1; + last[ext_index] = dos_partition_get_start(q) + + dos_partition_get_size(q) - 1; } } else { start = extended_offset + cxt->first_lba; - limit = get_start_sect(q) + get_nr_sects(q) - 1; + limit = dos_partition_get_start(q) + dos_partition_get_size(q) - 1; } if (fdisk_context_use_cylinders(cxt)) for (i = 0; i < cxt->label->nparts_max; i++) @@ -875,10 +855,10 @@ static void check_consistency(struct fdisk_context *cxt, struct dos_partition *p pes = p->es & 0x3f; /* compute logical beginning (c, h, s) */ - long2chs(cxt, get_start_sect(p), &lbc, &lbh, &lbs); + long2chs(cxt, dos_partition_get_start(p), &lbc, &lbh, &lbs); /* compute logical ending (c, h, s) */ - long2chs(cxt, get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les); + long2chs(cxt, dos_partition_get_start(p) + dos_partition_get_size(p) - 1, &lec, &leh, &les); /* Same physical / logical beginning? */ if (cxt->geom.cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) { @@ -944,8 +924,8 @@ static int dos_verify_disklabel(struct fdisk_context *cxt) if (extended_offset) { struct pte *pex = &ptes[ext_index]; - sector_t e_last = get_start_sect(pex->pt_entry) + - get_nr_sects(pex->pt_entry) - 1; + sector_t e_last = dos_partition_get_start(pex->pt_entry) + + dos_partition_get_size(pex->pt_entry) - 1; for (i = 4; i < cxt->label->nparts_max; i++) { total++; @@ -1253,7 +1233,7 @@ int dos_list_table(struct fdisk_context *cxt, p = pe->pt_entry; if (p && !is_cleared_partition(p)) { - unsigned int psects = get_nr_sects(p); + unsigned int psects = dos_partition_get_size(p); unsigned int pblocks = psects; unsigned int podd = 0; struct fdisk_parttype *type = @@ -1312,8 +1292,8 @@ void dos_list_table_expert(struct fdisk_context *cxt, int extend) cylinder(p->bs, p->bc), p->eh, sector(p->es), cylinder(p->es, p->ec), - (unsigned long) get_start_sect(p), - (unsigned long) get_nr_sects(p), p->sys_ind); + (unsigned long) dos_partition_get_start(p), + (unsigned long) dos_partition_get_size(p), p->sys_ind); if (p->sys_ind) { check_consistency(cxt, p, i); fdisk_warn_alignment(cxt, get_partition_start(pe), i); @@ -1350,12 +1330,12 @@ static void fix_chain_of_logicals(struct fdisk_context *cxt) ptes[j].offset = ojj; ptes[j+1].offset = oj; pj = ptes[j].pt_entry; - set_start_sect(pj, get_start_sect(pj)+oj-ojj); + dos_partition_set_start(pj, dos_partition_get_start(pj)+oj-ojj); pjj = ptes[j+1].pt_entry; - set_start_sect(pjj, get_start_sect(pjj)+ojj-oj); - set_start_sect(ptes[j-1].ex_entry, + dos_partition_set_start(pjj, dos_partition_get_start(pjj)+ojj-oj); + dos_partition_set_start(ptes[j-1].ex_entry, ojj-extended_offset); - set_start_sect(ptes[j].ex_entry, + dos_partition_set_start(ptes[j].ex_entry, oj-extended_offset); goto stage1; } @@ -1366,16 +1346,16 @@ static void fix_chain_of_logicals(struct fdisk_context *cxt) for (j = 4; j < cxt->label->nparts_max - 1; j++) { pj = ptes[j].pt_entry; pjj = ptes[j+1].pt_entry; - sj = get_start_sect(pj); - sjj = get_start_sect(pjj); + sj = dos_partition_get_start(pj); + sjj = dos_partition_get_start(pjj); oj = ptes[j].offset; ojj = ptes[j+1].offset; if (oj+sj > ojj+sjj) { tmp = *pj; *pj = *pjj; *pjj = tmp; - set_start_sect(pj, ojj+sjj-oj); - set_start_sect(pjj, oj+sj-ojj); + dos_partition_set_start(pj, ojj+sjj-oj); + dos_partition_set_start(pjj, oj+sj-ojj); goto stage2; } } @@ -1436,7 +1416,7 @@ void dos_move_begin(struct fdisk_context *cxt, int i) if (warn_geometry(cxt)) return; - if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED (p->sys_ind)) { + if (!p->sys_ind || !dos_partition_get_size(p) || IS_EXTENDED (p->sys_ind)) { printf(_("Partition %d has no data area\n"), i + 1); return; } @@ -1456,24 +1436,24 @@ void dos_move_begin(struct fdisk_context *cxt, int i) if (!prev_p) continue; - end = get_partition_start(prev_pe) + get_nr_sects(prev_p); + end = get_partition_start(prev_pe) + dos_partition_get_size(prev_p); if (!is_cleared_partition(prev_p) && end > free_start && end <= curr_start) free_start = end; } - last = get_partition_start(pe) + get_nr_sects(p) - 1; + last = get_partition_start(pe) + dos_partition_get_size(p) - 1; if (fdisk_ask_number(cxt, free_start, curr_start, last, _("New beginning of data"), &res)) return; new = res - pe->offset; - if (new != get_nr_sects(p)) { - unsigned int sects = get_nr_sects(p) + get_start_sect(p) - new; - set_nr_sects(p, sects); - set_start_sect(p, new); + if (new != dos_partition_get_size(p)) { + unsigned int sects = dos_partition_get_size(p) + dos_partition_get_start(p) - new; + dos_partition_set_size(p, sects); + dos_partition_set_start(p, new); pe->changed = 1; } } diff --git a/fdisks/fdiskdoslabel.h b/fdisks/fdiskdoslabel.h index 4387074661..2705c3dad6 100644 --- a/fdisks/fdiskdoslabel.h +++ b/fdisks/fdiskdoslabel.h @@ -33,54 +33,16 @@ static inline unsigned int pt_entry_flag(unsigned char *b) return ((unsigned int) b[510]) + (((unsigned int) b[511]) << 8); } - -/* start_sect and nr_sects are stored little endian on all machines */ -/* moreover, they are not aligned correctly */ -static inline void store4_little_endian(unsigned char *cp, unsigned int val) -{ - cp[0] = (val & 0xff); - cp[1] = ((val >> 8) & 0xff); - cp[2] = ((val >> 16) & 0xff); - cp[3] = ((val >> 24) & 0xff); -} - -static inline unsigned int read4_little_endian(const unsigned char *cp) -{ - return (unsigned int)(cp[0]) + ((unsigned int)(cp[1]) << 8) - + ((unsigned int)(cp[2]) << 16) - + ((unsigned int)(cp[3]) << 24); -} - -static inline sector_t get_nr_sects(struct dos_partition *p) -{ - return read4_little_endian(p->nr_sects); -} - -static inline void set_nr_sects(struct dos_partition *p, sector_t nr_sects) -{ - store4_little_endian(p->nr_sects, nr_sects); -} - -static inline void set_start_sect(struct dos_partition *p, unsigned int start_sect) -{ - store4_little_endian(p->start_sect, start_sect); -} - -static inline sector_t get_start_sect(struct dos_partition *p) -{ - return read4_little_endian(p->start_sect); -} - static inline sector_t get_partition_start(struct pte *pe) { - return pe->offset + get_start_sect(pe->pt_entry); + return pe->offset + dos_partition_get_start(pe->pt_entry); } static inline int is_cleared_partition(struct dos_partition *p) { return !(!p || p->boot_ind || p->bh || p->bs || p->bc || p->sys_ind || p->eh || p->es || p->ec || - get_start_sect(p) || get_nr_sects(p)); + dos_partition_get_start(p) || dos_partition_get_size(p)); } extern struct dos_partition *dos_get_pt_entry(int); @@ -96,8 +58,6 @@ extern void dos_fix_partition_table_order(struct fdisk_context *cxt); extern void dos_move_begin(struct fdisk_context *cxt, int i); extern void dos_toggle_active(struct fdisk_context *cxt, int i); -extern int mbr_is_valid_magic(unsigned char *b); - #define is_dos_compatible(_x) \ (fdisk_is_disklabel(_x, DOS) && \ fdisk_dos_is_compatible(fdisk_context_get_label(_x, NULL))) diff --git a/include/pt-mbr.h b/include/pt-mbr.h index 283ec7fac8..5e5e603765 100644 --- a/include/pt-mbr.h +++ b/include/pt-mbr.h @@ -13,7 +13,7 @@ struct dos_partition { #define MBR_PT_OFFSET 0x1be /* assemble badly aligned little endian integer */ -static inline unsigned int __dos_assemble4le(const unsigned char *p) +static inline unsigned int __dos_assemble_4le(const unsigned char *p) { return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); } @@ -26,24 +26,45 @@ static inline void __dos_store_4le(unsigned char *p, unsigned int val) p[3] = ((val >> 24) & 0xff); } -static inline unsigned int dos_partition_start(struct dos_partition *p) +static inline unsigned int dos_partition_get_start(struct dos_partition *p) { - return __dos_assemble4le(&(p->start_sect[0])); + return __dos_assemble_4le(&(p->start_sect[0])); } -static inline unsigned int dos_partition_size(struct dos_partition *p) +static inline void dos_partition_set_start(struct dos_partition *p, unsigned int n) { - return __dos_assemble4le(&(p->nr_sects[0])); + __dos_store_4le(p->start_sect, n); } -static inline int is_valid_mbr_signature(const unsigned char *mbr) +static inline unsigned int dos_partition_get_size(struct dos_partition *p) +{ + return __dos_assemble_4le(&(p->nr_sects[0])); +} + +static inline void dos_partition_set_size(struct dos_partition *p, unsigned int n) +{ + __dos_store_4le(p->nr_sects, n); +} + +static inline int mbr_is_valid_magic(const unsigned char *mbr) { return mbr[510] == 0x55 && mbr[511] == 0xaa ? 1 : 0; } -static inline unsigned int dos_parttable_id(const unsigned char *mbr) +static inline void mbr_set_magic(unsigned char *b) +{ + b[510] = 0x55; + b[511] = 0xaa; +} + +static inline unsigned int mbr_get_id(const unsigned char *mbr) +{ + return __dos_assemble_4le(&mbr[440]); +} + +static inline void mbr_set_id(unsigned char *b, unsigned int id) { - return __dos_assemble4le(&mbr[440]); + __dos_store_4le(&b[440], id); } enum { diff --git a/libblkid/src/partitions/dos.c b/libblkid/src/partitions/dos.c index c45e06ebd2..c92d8ec0fe 100644 --- a/libblkid/src/partitions/dos.c +++ b/libblkid/src/partitions/dos.c @@ -57,7 +57,7 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab, if (!data) goto leave; /* malformed partition? */ - if (!is_valid_mbr_signature(data)) + if (!mbr_is_valid_magic(data)) goto leave; p0 = (struct dos_partition *) (data + MBR_PT_OFFSET); @@ -79,8 +79,8 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab, blkid_partition par; /* the start is relative to the parental ext.partition */ - start = dos_partition_start(p) * ssf; - size = dos_partition_size(p) * ssf; + start = dos_partition_get_start(p) * ssf; + size = dos_partition_get_size(p) * ssf; abs_start = cur_start + start; /* absolute start */ if (!size || is_extended(p)) @@ -110,8 +110,8 @@ static int parse_dos_extended(blkid_probe pr, blkid_parttable tab, * is junk. */ for (p = p0, i = 0; i < 4; i++, p++) { - start = dos_partition_start(p) * ssf; - size = dos_partition_size(p) * ssf; + start = dos_partition_get_start(p) * ssf; + size = dos_partition_get_size(p) * ssf; if (size && is_extended(p)) break; @@ -183,7 +183,7 @@ static int probe_dos_pt(blkid_probe pr, blkid_probe_use_wiper(pr, MBR_PT_OFFSET, 512 - MBR_PT_OFFSET); - id = dos_parttable_id(data); + id = mbr_get_id(data); if (id) snprintf(idstr, sizeof(idstr), "%08x", id); @@ -217,8 +217,8 @@ static int probe_dos_pt(blkid_probe pr, for (p = p0, i = 0; i < 4; i++, p++) { blkid_partition par; - start = dos_partition_start(p) * ssf; - size = dos_partition_size(p) * ssf; + start = dos_partition_get_start(p) * ssf; + size = dos_partition_get_size(p) * ssf; if (!size) { /* Linux kernel ignores empty partitions, but partno for @@ -242,8 +242,8 @@ static int probe_dos_pt(blkid_probe pr, /* Parse logical partitions */ for (p = p0, i = 0; i < 4; i++, p++) { - start = dos_partition_start(p) * ssf; - size = dos_partition_size(p) * ssf; + start = dos_partition_get_start(p) * ssf; + size = dos_partition_get_size(p) * ssf; if (!size) continue; @@ -257,7 +257,7 @@ static int probe_dos_pt(blkid_probe pr, for (p = p0, i = 0; i < 4; i++, p++) { size_t n; - if (!dos_partition_size(p) || is_extended(p)) + if (!dos_partition_get_size(p) || is_extended(p)) continue; for (n = 0; n < ARRAY_SIZE(dos_nested); n++) { diff --git a/libblkid/src/partitions/gpt.c b/libblkid/src/partitions/gpt.c index dc057d358b..eea9ba4377 100644 --- a/libblkid/src/partitions/gpt.c +++ b/libblkid/src/partitions/gpt.c @@ -169,7 +169,7 @@ static int is_pmbr_valid(blkid_probe pr) if (!data) goto failed; - if (!is_valid_mbr_signature(data)) + if (!mbr_is_valid_magic(data)) goto failed; p = (struct dos_partition *) (data + MBR_PT_OFFSET); diff --git a/libblkid/src/partitions/minix.c b/libblkid/src/partitions/minix.c index 64df4d867a..2aad80f649 100644 --- a/libblkid/src/partitions/minix.c +++ b/libblkid/src/partitions/minix.c @@ -59,8 +59,8 @@ static int probe_minix_pt(blkid_probe pr, if (p->sys_ind != MBR_MINIX_PARTITION) continue; - start = dos_partition_start(p); - size = dos_partition_size(p); + start = dos_partition_get_start(p); + size = dos_partition_get_size(p); if (parent && !blkid_is_nested_dimension(parent, start, size)) { DBG(LOWPROBE, blkid_debug(