[syslinux] chain.c32 (and partiter) updates v3

Michal Soltys soltys at ziu.info
Tue Jan 29 06:05:47 PST 2013


This is updated version including Shao's comments (though I skipped anonymous
struct/unions, as per HPA's comment they were acceptable). The summary diff
between old and new version is below - the changes are cosmetic except the very
last commit addressing (hopefully) 'fs' option behavior from whole disk point
of view.


The following changes since commit 7307d60063ee4303da4de45f9d984fdc8df92146:

  memdisk: Fix order of sectors/track and bytes/sector (2012-10-23 10:53:17 +0100)

are available in the git repository at:

  git://hasevolq.net/syslinux.git chain_upd_v3

for you to fetch changes up to 2d2c5ad19ed5cfad7f4b36d45684a833ee721faf:

  com32/chain: fix potential problem with 'fs' option (2013-01-29 14:50:22 +0100)

----------------------------------------------------------------
Michal Soltys (28):
      com32/chain: update licenses and #ifndef stanzas
      com32/chain: comments, minor adjustment
      com32/chain: use anonymous and unnamed aggregates
      com32/chain: change stepall into flags
      com32/chain: shuffle code before partiter simplifications
      com32/chain: partiter - simplifications and updates
      com32/chain: remove redundant rawindex
      com32/chain: partiter - more precise comments / output
      com32/chain: remove unused ebr_start
      com32/chain: add pi_errored() in partiter
      com32/chain: mangle and related updates
      com32/chain: cleaner variable names
      com32/chain: use disk_guid for part_guid for 0th partition
      com32/chain: warning/error/help/doc tidying
      com32/chain: implement relax flag
      com32/chain: implement handling of non-standard hybrid GPT+MBR layouts
      com32/chain: recognize exFAT
      com32/chain: remove common.h
      com32/chain: rely more on addr_t, replace ADDR*
      com32/chain: index -1 partiter adjustment
      com32/chain: manglepe_fixchs() correction
      com32/chain: partiter: rename start_lba
      com32/chain: use single value for partiter related options (flags)
      com32/chain: (partiter) dos_next_ebr() fixup
      com32/chain: (partiter) add cebr_siz for completness
      com32/chain: (utility) use enum for BPB types
      com32/chain: sizeof style change (trivial)
      com32/chain: fix potential problem with 'fs' option

 com32/chain/Makefile   |    6 +-
 com32/chain/chain.c    |  171 +++++++------
 com32/chain/chain.h    |   19 +-
 com32/chain/common.h   |    9 -
 com32/chain/mangle.c   |  244 ++++++++++++-------
 com32/chain/mangle.h   |   34 ++-
 com32/chain/options.c  |  230 ++++++++++--------
 com32/chain/options.h  |   54 ++++-
 com32/chain/partiter.c |  619 ++++++++++++++++++------------------------------
 com32/chain/partiter.h |   85 ++++---
 com32/chain/utility.c  |   97 ++++++--
 com32/chain/utility.h  |   79 ++++--
 doc/chain.txt          |   41 ++--
 13 files changed, 918 insertions(+), 770 deletions(-)
 delete mode 100644 com32/chain/common.h


Summary diff between old and current versions:

diff --git a/com32/chain/Makefile b/com32/chain/Makefile
index ef0929e..00b3323 100644
--- a/com32/chain/Makefile
+++ b/com32/chain/Makefile
@@ -1,6 +1,6 @@
 ## -----------------------------------------------------------------------
 ##
-##   Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
+##   Copyright 2001-2009 H. Peter Anvin - All Rights Reserved
 ##   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
 ##   Copyright 2010 Shao Miller
 ##   Copyright 2010-2012 Michal Soltys
diff --git a/com32/chain/chain.c b/com32/chain/chain.c
index dcf6d0b..ae95d45 100644
--- a/com32/chain/chain.c
+++ b/com32/chain/chain.c
@@ -111,7 +111,7 @@ static int find_by_guid(const struct guid *gpt_guid,
 	}
 	/* Check for a matching GPT disk/partition guid */
 	do {
-	    if (!memcmp(&boot_part->gpt.part_guid, gpt_guid, sizeof(*gpt_guid)))
+	    if (!memcmp(&boot_part->gpt.part_guid, gpt_guid, sizeof *gpt_guid))
 		goto ok;
 	} while (!pi_next(boot_part));
     }
@@ -240,14 +240,14 @@ static void do_boot(struct data_area *data, int ndata)
 
 	/* Mapping table; start out with identity mapping everything */
 	for (i = 0; i < 256; i++)
-	    p[i] = (uint8_t)i;
+	    p[i] = i;
 
 	/* And the actual swap */
 	p[driveno] = swapdrive;
 	p[swapdrive] = driveno;
 
 	/* Adjust registers */
-	opt.regs.ds = opt.regs.cs = (uint16_t)(endimage >> 4);
+	opt.regs.ds = opt.regs.cs = endimage >> 4;
 	opt.regs.esi.l = opt.regs.es = 0;
 	opt.regs.ecx.l = sizeof swapstub >> 2;
 	opt.regs.ip = 0x10;	/* Installer offset */
@@ -346,10 +346,10 @@ int find_dp(struct part_iter **_iter)
 
 	/* 'fs' => we should lookup the syslinux partition number and use it */
 	if (!strcmp(opt.drivename, "fs")) {
-	    while (!pi_next(iter)) {
+	    do {
 		if (iter->abs_lba == fs_lba)
 		    break;
-	    }
+	    } while (!pi_next(iter));
 	    /* broken part structure or other problems */
 	    if (iter->status) {
 		error("Can't find myself on the drive I booted from.");
@@ -394,8 +394,7 @@ static int setup_handover(const struct part_iter *iter,
 		   struct data_area *data)
 {
     struct disk_dos_part_entry *ha;
-    uint32_t synth_size;
-    uint32_t *plen;
+    uint32_t synth_size = sizeof *ha;
 
     /*
      * we have to cover both non-iterated but otherwise properly detected
@@ -404,7 +403,6 @@ static int setup_handover(const struct part_iter *iter,
     if (iter->index == 0) {
 	uint32_t len;
 	/* RAW handover protocol */
-	synth_size = sizeof(struct disk_dos_part_entry);
 	ha = malloc(synth_size);
 	if (!ha) {
 	    critm();
@@ -412,24 +410,24 @@ static int setup_handover(const struct part_iter *iter,
 	}
 	len = ~0u;
 	if (iter->length < len)
-	    len = (uint32_t)iter->length;
-	lba2chs(&ha->start, &iter->di, 0, l2c_cadd);
-	lba2chs(&ha->end, &iter->di, len - 1, l2c_cadd);
+	    len = iter->length;
+	lba2chs(&ha->start, &iter->di, 0, L2C_CADD);
+	lba2chs(&ha->end, &iter->di, len - 1, L2C_CADD);
 	ha->active_flag = 0x80;
 	ha->ostype = 0xDA;	/* "Non-FS Data", anything is good here though ... */
 	ha->start_lba = 0;
 	ha->length = len;
     } else if (iter->type == typegpt) {
+	uint32_t *plen;
 	/* GPT handover protocol */
-	synth_size = sizeof(struct disk_dos_part_entry) +
-	    sizeof(uint32_t) + (uint32_t)iter->gpt.pe_size;
+	synth_size += sizeof *plen + iter->gpt.pe_size;
 	ha = malloc(synth_size);
 	if (!ha) {
 	    critm();
 	    goto bail;
 	}
-	lba2chs(&ha->start, &iter->di, iter->abs_lba, l2c_cadd);
-	lba2chs(&ha->end, &iter->di, iter->abs_lba + iter->length - 1, l2c_cadd);
+	lba2chs(&ha->start, &iter->di, iter->abs_lba, L2C_CADD);
+	lba2chs(&ha->end, &iter->di, iter->abs_lba + iter->length - 1, L2C_CADD);
 	ha->active_flag = 0x80;
 	ha->ostype = 0xED;
 	/* All bits set by default */
@@ -437,12 +435,12 @@ static int setup_handover(const struct part_iter *iter,
 	ha->length = ~0u;
 	/* If these fit the precision, pass them on */
 	if (iter->abs_lba < ha->start_lba)
-	    ha->start_lba = (uint32_t)iter->abs_lba;
+	    ha->start_lba = iter->abs_lba;
 	if (iter->length < ha->length)
-	    ha->length = (uint32_t)iter->length;
+	    ha->length = iter->length;
 	/* Next comes the GPT partition record length */
-	plen = (uint32_t *) (ha + 1);
-	plen[0] = (uint32_t)iter->gpt.pe_size;
+	plen = (uint32_t *)(ha + 1);
+	plen[0] = iter->gpt.pe_size;
 	/* Next comes the GPT partition record copy */
 	memcpy(plen + 1, iter->record, plen[0]);
 #ifdef DEBUG
@@ -451,9 +449,8 @@ static int setup_handover(const struct part_iter *iter,
 	disk_gpt_part_dump((struct disk_gpt_part_entry *)(plen + 1));
 #endif
     /* the only possible case left is dos scheme */
-    } else {
+    } else if (iter->type == typedos) {
 	/* MBR handover protocol */
-	synth_size = sizeof(struct disk_dos_part_entry);
 	ha = malloc(synth_size);
 	if (!ha) {
 	    critm();
@@ -461,15 +458,18 @@ static int setup_handover(const struct part_iter *iter,
 	}
 	memcpy(ha, iter->record, synth_size);
 	/* make sure these match bios imaginations and are ebr agnostic */
-	lba2chs(&ha->start, &iter->di, iter->abs_lba, l2c_cadd);
-	lba2chs(&ha->end, &iter->di, iter->abs_lba + iter->length - 1, l2c_cadd);
-	ha->start_lba = (uint32_t)iter->abs_lba;
-	ha->length = (uint32_t)iter->length;
+	lba2chs(&ha->start, &iter->di, iter->abs_lba, L2C_CADD);
+	lba2chs(&ha->end, &iter->di, iter->abs_lba + iter->length - 1, L2C_CADD);
+	ha->start_lba = iter->abs_lba;
+	ha->length = iter->length;
 
 #ifdef DEBUG
 	dprintf("MBR handover:\n");
 	disk_dos_part_dump(ha);
 #endif
+    } else {
+	/* shouldn't ever happen */
+	goto bail;
     }
 
     data->base = 0x7be;
@@ -490,9 +490,9 @@ int main(int argc, char *argv[])
 
     console_ansi_raw();
 
-    memset(&fdat, 0, sizeof(fdat));
-    memset(&hdat, 0, sizeof(hdat));
-    memset(&sdat, 0, sizeof(sdat));
+    memset(&fdat, 0, sizeof fdat);
+    memset(&hdat, 0, sizeof hdat);
+    memset(&sdat, 0, sizeof sdat);
 
     opt_set_defs();
     if (opt_parse_args(argc, argv))
@@ -612,11 +612,11 @@ int main(int argc, char *argv[])
      */
 
     if (opt.file)
-	memcpy(data + ndata++, &fdat, sizeof(fdat));
+	memcpy(data + ndata++, &fdat, sizeof fdat);
     if (opt.maps)
-	memcpy(data + ndata++, &sdat, sizeof(sdat));
+	memcpy(data + ndata++, &sdat, sizeof sdat);
     if (opt.hand)
-	memcpy(data + ndata++, &hdat, sizeof(hdat));
+	memcpy(data + ndata++, &hdat, sizeof hdat);
 
 #ifdef DEBUG
     dprintf("iter->di dsk, bps: %X, %u\niter->di lbacnt, C*H*S: %"PRIu64", %u\n"
diff --git a/com32/chain/chain.h b/com32/chain/chain.h
index 1d08b0e..fb5914b 100644
--- a/com32/chain/chain.h
+++ b/com32/chain/chain.h
@@ -13,8 +13,8 @@
  *
  * ----------------------------------------------------------------------- */
 
-#ifndef _COM32_CHAIN_CHAIN_H
-#define _COM32_CHAIN_CHAIN_H
+#ifndef COM32_CHAIN_CHAIN_H
+#define COM32_CHAIN_CHAIN_H
 
 #include <syslinux/movebits.h>
 
diff --git a/com32/chain/mangle.c b/com32/chain/mangle.c
index 6eab95d..ffdaab8 100644
--- a/com32/chain/mangle.c
+++ b/com32/chain/mangle.c
@@ -161,7 +161,7 @@ int manglef_grub(const struct part_iter *iter, struct data_area *data)
     if (!(opt.file && opt.grub))
 	return 0;
 
-    if (data->size < sizeof(struct grub_stage2_patch_area)) {
+    if (data->size < sizeof *stage2) {
 	error("The file specified by grub=<loader> is too small to be stage2 of GRUB Legacy.");
 	goto bail;
     }
@@ -203,7 +203,7 @@ int manglef_grub(const struct part_iter *iter, struct data_area *data)
      *   0-3:  primary partitions
      *   4-*:  logical partitions
      */
-    stage2->install_partition.part1 = (uint8_t)(iter->index - 1);
+    stage2->install_partition.part1 = iter->index - 1;
 
     /*
      * Grub Legacy reserves 89 bytes (from 0x8217 to 0x826f) for the
@@ -211,7 +211,7 @@ int manglef_grub(const struct part_iter *iter, struct data_area *data)
      * the default config filename "/boot/grub/menu.lst".
      */
     if (opt.grubcfg) {
-	if (strlen(opt.grubcfg) > sizeof(stage2->config_file) - 1) {
+	if (strlen(opt.grubcfg) >= sizeof stage2->config_file) {
 	    error("The config filename length can't exceed 88 characters.");
 	    goto bail;
 	}
@@ -263,9 +263,9 @@ int manglef_drmk(struct data_area *data)
     data->size = tsize;
     /* ds:bp is assumed by DRMK to be the boot sector */
     /* offset 28 is the FAT HiddenSectors value */
-    opt.regs.ds = (uint16_t)((tsize >> 4) + (opt.fseg - 2));
+    opt.regs.ds = (tsize >> 4) + (opt.fseg - 2);
     /* "Patch" into tail of the new space */
-    *(uint32_t *)((char*)data->data + tsize - 4) = (uint32_t)fs_lba;
+    *(uint32_t *)((char*)data->data + tsize - 4) = fs_lba;
 
     return 0;
 bail:
@@ -284,14 +284,14 @@ static int mangle_bpb(const struct part_iter *iter, struct data_area *data, cons
     /* BPB: hidden sectors 32bit*/
     else if (bpbV34 <= type && type <= bpbV70) {
 	if (iter->abs_lba < ~0u)
-	    *(uint32_t *) ((char *)data->data + 0x1c) = (uint32_t)iter->abs_lba;
+	    *(uint32_t *) ((char *)data->data + 0x1c) = iter->abs_lba;
 	else
 	    /* won't really help much, but ... */
 	    *(uint32_t *) ((char *)data->data + 0x1c) = ~0u;
     /* BPB: hidden sectors 16bit*/
     } else if (bpbV30 <= type && type <= bpbV32) {
 	if (iter->abs_lba < 0xFFFF)
-	    *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)iter->abs_lba;
+	    *(uint16_t *) ((char *)data->data + 0x1c) = iter->abs_lba;
 	else
 	    /* won't really help much, but ... */
 	    *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)~0u;
@@ -300,7 +300,7 @@ static int mangle_bpb(const struct part_iter *iter, struct data_area *data, cons
     /* BPB: legacy geometry */
     if (bpbV30 <= type && type <= bpbV70) {
 	if (iter->di.cbios)
-	    *(uint32_t *)((char *)data->data + 0x18) = (uint32_t)((iter->di.head << 16) | iter->di.spt);
+	    *(uint32_t *)((char *)data->data + 0x18) = (iter->di.head << 16) | iter->di.spt;
 	else {
 	    if (iter->di.disk & 0x80)
 		*(uint32_t *)((char *)data->data + 0x18) = 0x00FF003F;
@@ -310,8 +310,7 @@ static int mangle_bpb(const struct part_iter *iter, struct data_area *data, cons
     }
     /* BPB: drive */
     if (off >= 0) {
-	*(uint8_t *)((char *)data->data + off) = (uint8_t)
-	    (opt.swap ? iter->di.disk & 0x80 : iter->di.disk);
+	*(uint8_t *)((char *)data->data + off) = (opt.swap ? iter->di.disk & 0x80 : iter->di.disk);
     }
 
     return 0;
@@ -422,7 +421,7 @@ int mangles_cmldr(struct data_area *data)
     if (!(opt.sect && opt.cmldr))
 	return 0;
 
-    memcpy((char *)data->data + 3, cmldr_signature, sizeof(cmldr_signature));
+    memcpy((char *)data->data + 3, cmldr_signature, sizeof cmldr_signature);
     return 0;
 }
 
@@ -431,18 +430,18 @@ int mangler_init(const struct part_iter *iter)
 {
     /* Set initial registry values */
     if (opt.file) {
-	opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.fseg;
-	opt.regs.ip = (uint16_t)opt.fip;
+	opt.regs.cs = opt.regs.ds = opt.regs.ss = opt.fseg;
+	opt.regs.ip = opt.fip;
     } else {
-	opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.sseg;
-	opt.regs.ip = (uint16_t)opt.sip;
+	opt.regs.cs = opt.regs.ds = opt.regs.ss = opt.sseg;
+	opt.regs.ip = opt.sip;
     }
 
     if (opt.regs.ip == 0x7C00 && !opt.regs.cs)
 	opt.regs.esp.l = 0x7C00;
 
     /* DOS kernels want the drive number in BL instead of DL. Indulge them. */
-    opt.regs.ebx.b[0] = opt.regs.edx.b[0] = (uint8_t)iter->di.disk;
+    opt.regs.ebx.b[0] = opt.regs.edx.b[0] = iter->di.disk;
 
     return 0;
 }
@@ -452,7 +451,7 @@ int mangler_handover(const struct part_iter *iter, const struct data_area *data)
 {
     if (opt.file && opt.maps && !opt.hptr) {
 	opt.regs.esi.l = opt.regs.ebp.l = opt.soff;
-	opt.regs.ds = (uint16_t)opt.sseg;
+	opt.regs.ds = opt.sseg;
 	opt.regs.eax.l = 0;
     } else if (opt.hand) {
 	/* base is really 0x7be */
@@ -476,7 +475,7 @@ int mangler_handover(const struct part_iter *iter, const struct data_area *data)
 int mangler_grldr(const struct part_iter *iter)
 {
     if (opt.grldr)
-	opt.regs.edx.b[1] = (uint8_t)(iter->index - 1);
+	opt.regs.edx.b[1] = iter->index - 1;
 
     return 0;
 }
@@ -505,8 +504,8 @@ static int fliphide(struct part_iter *iter, struct part_iter *miter)
 
     if ((t <= 0x1f) && ((mask >> (t & ~0x10u)) & 1)) {
 	/* It's a hideable partition type */
-	if (miter->index == iter->index || opt.hide & 4)
-	    t &= (uint8_t)(~0x10u);	/* unhide */
+	if (miter->index == iter->index || opt.hide & HIDE_REV)
+	    t &= ~0x10u;	/* unhide */
 	else
 	    t |= 0x10u;	/* hide */
     }
@@ -530,7 +529,7 @@ int manglepe_hide(struct part_iter *miter)
     struct part_iter *iter = NULL;
     int ridx;
 
-    if (!(opt.hide & 1))
+    if (!(opt.hide & HIDE_ON))
 	return 0;
 
     if (miter->type != typedos) {
@@ -538,7 +537,7 @@ int manglepe_hide(struct part_iter *miter)
 	return -1;
     }
 
-    if (miter->index > 4 && !(opt.hide & 2))
+    if (miter->index > 4 && !(opt.hide & HIDE_EXT))
 	warn("Specified partition is logical, so it can't be unhidden without 'unhideall'.");
 
     if (!(iter = pi_begin(&miter->di, PIF_STEPALL | opt.piflags)))
@@ -546,7 +545,7 @@ int manglepe_hide(struct part_iter *miter)
 
     while (!pi_next(iter) && !werr) {
 	ridx = iter->index0;
-	if (!(opt.hide & 2) && ridx > 3)
+	if (!(opt.hide & HIDE_EXT) && ridx > 3)
 	    break;  /* skip when we're constrained to pri only */
 
 	if (iter->index != -1)
@@ -610,13 +609,13 @@ static int updchs(struct part_iter *iter, int ext)
      */
 
     if (dp->start_lba || iter->index != -1) {
-	lba2chs(&dp->start, &iter->di, lba, l2c_cadd);
+	lba2chs(&dp->start, &iter->di, lba, L2C_CADD);
     } else {
 	memset(&dp->start, 0, sizeof dp->start);
     }
 
     if ((dp->start_lba || iter->index != -1) && dp->length) {
-	lba2chs(&dp->end, &iter->di, lba + dp->length - 1, l2c_cadd);
+	lba2chs(&dp->end, &iter->di, lba + dp->length - 1, L2C_CADD);
     } else {
 	memset(&dp->end, 0, sizeof dp->end);
     }
diff --git a/com32/chain/mangle.h b/com32/chain/mangle.h
index 5b1ed5a..d4a5b75 100644
--- a/com32/chain/mangle.h
+++ b/com32/chain/mangle.h
@@ -28,8 +28,8 @@
  *
  * ----------------------------------------------------------------------- */
 
-#ifndef _COM32_CHAIN_MANGLE_H
-#define _COM32_CHAIN_MANGLE_H
+#ifndef COM32_CHAIN_MANGLE_H
+#define COM32_CHAIN_MANGLE_H
 
 #include "chain.h"
 #include "partiter.h"
diff --git a/com32/chain/options.c b/com32/chain/options.c
index 42519be..4e722a0 100644
--- a/com32/chain/options.c
+++ b/com32/chain/options.c
@@ -167,7 +167,7 @@ static void usage(void)
 
 void opt_set_defs(void)
 {
-    memset(&opt, 0, sizeof(opt));
+    memset(&opt, 0, sizeof opt);
     opt.sect = true;	    /* by def. load sector */
     opt.maps = true;	    /* by def. map sector */
     opt.hand = true;	    /* by def. prepare handover */
@@ -300,15 +300,15 @@ int opt_parse_args(int argc, char *argv[])
 	} else if (!strcmp(argv[i], "noswap")) {
 	    opt.swap = false;
 	} else if (!strcmp(argv[i], "nohide")) {
-	    opt.hide = 0b000;
+	    opt.hide = HIDE_OFF;
 	} else if (!strcmp(argv[i], "hide")) {
-	    opt.hide = 0b001;
+	    opt.hide = HIDE_ON;
 	} else if (!strcmp(argv[i], "hideall")) {
-	    opt.hide = 0b011;
+	    opt.hide = HIDE_ON | HIDE_EXT;
 	} else if (!strcmp(argv[i], "unhide")) {
-	    opt.hide = 0b101;
+	    opt.hide = HIDE_ON | HIDE_REV;
 	} else if (!strcmp(argv[i], "unhideall")) {
-	    opt.hide = 0b111;
+	    opt.hide = HIDE_ON | HIDE_EXT | HIDE_REV;
 	} else if (!strcmp(argv[i], "setbpb")) {
 	    opt.setbpb = true;
 	} else if (!strcmp(argv[i], "nosetbpb")) {
diff --git a/com32/chain/options.h b/com32/chain/options.h
index d245aa7..df96e2d 100644
--- a/com32/chain/options.h
+++ b/com32/chain/options.h
@@ -28,13 +28,15 @@
  *
  * ----------------------------------------------------------------------- */
 
-#ifndef _COM32_CHAIN_OPTIONS_H
-#define _COM32_CHAIN_OPTIONS_H
+#ifndef COM32_CHAIN_OPTIONS_H
+#define COM32_CHAIN_OPTIONS_H
 
 #include <stdint.h>
 #include <syslinux/bootrm.h>
 #include <syslinux/movebits.h>
 
+enum {HIDE_OFF = 0, HIDE_ON = 1, HIDE_EXT = 2, HIDE_REV = 4};
+
 struct options {
     const char *drivename;
     const char *partition;
diff --git a/com32/chain/partiter.c b/com32/chain/partiter.c
index bd61bdf..1eb5350 100644
--- a/com32/chain/partiter.c
+++ b/com32/chain/partiter.c
@@ -84,7 +84,7 @@ static int pi_ctor(struct part_iter *iter,
 	const struct disk_info *di, int flags
 )
 {
-    memcpy(&iter->di, di, sizeof(struct disk_info));
+    memcpy(&iter->di, di, sizeof *di);
     iter->flags = flags;
     iter->index0 = -1;
     iter->length = di->lbacnt;
@@ -102,12 +102,12 @@ static int pi_dos_ctor(struct part_iter *iter,
     if (pi_ctor(iter, di, flags))
 	return -1;
 
-    if (!(iter->data = malloc(sizeof(struct disk_dos_mbr)))) {
+    if (!(iter->data = malloc(sizeof *mbr))) {
 	critm();
 	goto bail;
     }
 
-    memcpy(iter->data, mbr, sizeof(struct disk_dos_mbr));
+    memcpy(iter->data, mbr, sizeof *mbr);
 
     iter->dos.bebr_index0 = -1;
     iter->dos.disk_sig = mbr->disk_sig;
@@ -144,8 +144,8 @@ static int pi_gpt_ctor(struct part_iter *iter,
     iter->gpt.ufirst = gpth->lba_first_usable;
     iter->gpt.ulast = gpth->lba_last_usable;
 
-    memcpy(&iter->gpt.disk_guid, &gpth->disk_guid, sizeof(struct guid));
-    memcpy(&iter->gpt.part_guid, &gpth->disk_guid, sizeof(struct guid));
+    memcpy(&iter->gpt.disk_guid, &gpth->disk_guid, sizeof gpth->disk_guid);
+    memcpy(&iter->gpt.part_guid, &gpth->disk_guid, sizeof gpth->disk_guid);
 
     iter->type = typegpt;
     return 0;
@@ -529,10 +529,10 @@ static int pi_gpt_next(struct part_iter *iter)
 static struct part_iter *pi_alloc(void)
 {
     struct part_iter *iter;
-    if (!(iter = malloc(sizeof(struct part_iter))))
+    if (!(iter = malloc(sizeof *iter)))
 	critm();
     else
-	memset(iter, 0, sizeof(struct part_iter));
+	memset(iter, 0, sizeof *iter);
     return iter;
 }
 
@@ -585,7 +585,7 @@ struct part_iter *pi_begin(const struct disk_info *di, int flags)
     }
 
     if (gpth && gpth->rev.uint32 == 0x00010000 &&
-	    !memcmp(gpth->sig, disk_gpt_sig_magic, sizeof(disk_gpt_sig_magic))) {
+	    !memcmp(gpth->sig, disk_gpt_sig_magic, sizeof gpth->sig)) {
 	/* looks like GPT v1.0 */
 	uint64_t gpt_loff;	    /* offset to GPT partition list in sectors */
 	uint64_t gpt_lsiz;	    /* size of GPT partition list in bytes */
@@ -615,11 +615,11 @@ struct part_iter *pi_begin(const struct disk_info *di, int flags)
 		!sane(gpth->lba_last_usable, gpt_lcnt) ||
 		gpth->lba_last_usable + gpt_lcnt >= gpth->lba_alt ||
 		gpth->lba_alt >= di->lbacnt ||
-		gpth->part_size < sizeof(struct disk_gpt_part_entry))) {
+		gpth->part_size < sizeof *gptl)) {
 	    error("Invalid GPT header's values.");
 	    goto bail;
 	}
-	if (!(gptl = disk_read_sectors(di, gpt_loff, (uint8_t)gpt_lcnt))) {
+	if (!(gptl = disk_read_sectors(di, gpt_loff, gpt_lcnt))) {
 	    error("Couldn't read GPT partition list.");
 	    goto bail;
 	}
@@ -628,11 +628,11 @@ struct part_iter *pi_begin(const struct disk_info *di, int flags)
 	    warn("Checksum of the main GPT partition list is invalid, trying backup.");
 	    free(gptl);
 	    /* secondary array directly precedes secondary header */
-	    if (!(gptl = disk_read_sectors(di, gpth->lba_alt - gpt_lcnt, (uint8_t)gpt_lcnt))) {
+	    if (!(gptl = disk_read_sectors(di, gpth->lba_alt - gpt_lcnt, gpt_lcnt))) {
 		error("Couldn't read backup GPT partition list.");
 		goto bail;
 	    }
-	    if (!valid_crc(gpth->table_chksum, (const uint8_t *)gptl, (unsigned int)gpt_lsiz)) {
+	    if (!valid_crc(gpth->table_chksum, (const uint8_t *)gptl, gpt_lsiz)) {
 		error("Checksum of the backup GPT partition list is invalid, giving up.");
 		goto bail;
 	    }
diff --git a/com32/chain/partiter.h b/com32/chain/partiter.h
index 78cd0d9..13dec84 100644
--- a/com32/chain/partiter.h
+++ b/com32/chain/partiter.h
@@ -34,24 +34,19 @@
  * Provides disk / partition iteration.
  */
 
-#ifndef _COM32_CHAIN_PARTITER_H
-#define _COM32_CHAIN_PARTITER_H
+#ifndef COM32_CHAIN_PARTITER_H
+#define COM32_CHAIN_PARTITER_H
 
 #include <stdint.h>
 #include <syslinux/disk.h>
 
 /* status */
 
-#define PI_ERRLOAD  3
-#define PI_INSANE   2
-#define PI_DONE	    1
-#define PI_OK	    0
+enum {PI_OK, PI_DONE, PI_INSANE, PI_ERRLOAD};
 
 /* flags */
 
-#define PIF_STEPALL 0b001
-#define PIF_RELAX   0b010
-#define PIF_PREFMBR 0b100
+enum {PIF_STEPALL = 1, PIF_RELAX = 2, PIF_PREFMBR = 4};
 
 struct itertype;
 struct part_iter;
@@ -72,7 +67,7 @@ struct part_iter {
     int index0;	    /* including holes, from -1 (disk, then parts from 0) */
     int index;	    /* excluding holes, from  0 (disk, then parts from 1), -1 means hole, if PIF_STEPALL is set */
     int flags;	    /* flags, see #defines above */
-    int status;	    /* current status, see #defines above */
+    int status;	    /* current status, see enums above */
     struct disk_info di;
     union {
 	struct {
@@ -86,7 +81,7 @@ struct part_iter {
 	    uint32_t nebr_lba;	  /* absolute lba of next ext. partition */
 	    uint32_t nebr_siz;	  /* size of next ext. partition */
 
-	    int bebr_index0;	  /* index of (0-3) of base ext. part., -1 of not present in MBR */
+	    int bebr_index0;	  /* index of (0-3) of base ext. part., -1 if not present in MBR */
 	    int logskipcnt;	  /* how many logical holes were skipped */
 	} dos;
 	struct {
diff --git a/com32/chain/utility.c b/com32/chain/utility.c
index abf642c..1c2c033 100644
--- a/com32/chain/utility.c
+++ b/com32/chain/utility.c
@@ -67,6 +67,15 @@ void wait_key(void)
     } while (!cnt || (cnt < 0 && errno == EAGAIN));
 }
 
+int guid_is0(const struct guid *guid)
+{
+    return
+	!(guid->data1 ||
+	  guid->data2 ||
+	  guid->data3 ||
+	  guid->data4);
+}
+
 /*
  * mode explanation:
  *
@@ -77,7 +86,8 @@ void wait_key(void)
  *        to return matching chs triplet
  * cmax - assume we can use any cylinder value
  *
- * by default, lba2chs() calls use cadd
+ * by default cadd seems most reasonable, giving consistent results with e.g.
+ * sfdisk's behavior
  */
 void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, int mode)
 {
@@ -92,10 +102,10 @@ void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, int mode)
 	cs = di->cyl;
 	hs = di->head;
 	ss = di->spt;
-	if (mode == l2c_cadd) {
+	if (mode == L2C_CADD) {
 	    if (cs < 1024 && di->lbacnt > cs*hs*ss)
 		cs++;
-	} else if (mode == l2c_cmax)
+	} else if (mode == L2C_CMAX)
 	    cs = 1024;
     } else {
 	if (di->disk & 0x80) {
@@ -114,15 +124,15 @@ void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, int mode)
 	h = hs - 1;
 	c = cs - 1;
     } else {
-	s = ((uint32_t)lba % ss) + 1;
-	t = (uint32_t)lba / ss;
+	s = (lba % ss) + 1;
+	t = lba / ss;
 	h = t % hs;
 	c = t / hs;
     }
 
-    (*dst)[0] = (uint8_t)h;
-    (*dst)[1] = (uint8_t)(s | ((c & 0x300) >> 2));
-    (*dst)[2] = (uint8_t)c;
+    (*dst)[0] = h;
+    (*dst)[1] = s | ((c & 0x300) >> 2);
+    (*dst)[2] = c;
 }
 
 uint32_t get_file_lba(const char *filename)
@@ -131,7 +141,7 @@ uint32_t get_file_lba(const char *filename)
     uint32_t lba;
 
     /* Start with clean registers */
-    memset(&inregs, 0, sizeof(com32sys_t));
+    memset(&inregs, 0, sizeof inregs);
 
     /* Put the filename in the bounce buffer */
     strlcpy(__com32.cs_bounce, filename, __com32.cs_bounce_size);
@@ -152,7 +162,7 @@ uint32_t get_file_lba(const char *filename)
     lba = *((uint32_t *) MK_PTR(inregs.ds, inregs.esi.w[0]));
 
     /* Clean the registers for the next call */
-    memset(&inregs, 0, sizeof(com32sys_t));
+    memset(&inregs, 0, sizeof inregs);
 
     /* Put the filename in the bounce buffer */
     strlcpy(__com32.cs_bounce, filename, __com32.cs_bounce_size);
diff --git a/com32/chain/utility.h b/com32/chain/utility.h
index 49c7946..596017b 100644
--- a/com32/chain/utility.h
+++ b/com32/chain/utility.h
@@ -28,28 +28,19 @@
  *
  * ----------------------------------------------------------------------- */
 
-#ifndef _COM32_CHAIN_UTILITY_H
-#define _COM32_CHAIN_UTILITY_H
+#ifndef COM32_CHAIN_UTILITY_H
+#define COM32_CHAIN_UTILITY_H
 
 #include <stdint.h>
 #include <stdio.h>
 #include <syslinux/disk.h>
 #include <syslinux/movebits.h>
 
-#define bpbUNK	0
-#define bpbV20	1
-#define bpbV30	2
-#define bpbV32	3
-#define bpbV34	4
-#define bpbV40	5
-#define bpbVNT	6
-#define bpbV70	7
-#define bpbEXF	8
+/* most (all ?) bpb "types" known to humankind as of 2012 */
+enum {bpbUNK, bpbV20, bpbV30, bpbV32, bpbV34, bpbV40, bpbVNT, bpbV70, bpbEXF};
 
 /* see utility.c for details */
-#define l2c_cnul 0
-#define l2c_cadd 1
-#define l2c_cmax 2
+enum {L2C_CNUL, L2C_CADD, L2C_CMAX};
 
 /* first usable and first unusable offsets */
 #define dosmin ((addr_t)0x500u)
@@ -60,6 +51,7 @@ void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, int mode);
 uint32_t get_file_lba(const char *filename);
 int drvoff_detect(int type);
 int bpb_detect(const uint8_t *bpb, const char *tag);
+int guid_is0(const struct guid *guid);
 
 static inline int warn(const char *x)
 {
@@ -78,12 +70,6 @@ static inline int crit(const char *x)
 
 #define critm()  crit("Malloc failure.")
 
-static inline int guid_is0(const struct guid *guid)
-{
-    return !*(const uint64_t *)guid && !*((const uint64_t *)guid + 1);
-}
-
-
 #endif
 
 /* vim: set ts=8 sts=4 sw=4 noet: */


More information about the Syslinux mailing list