[syslinux] [PATCH 02/23] com32/chain: comments, minor adjustments

Michal Soltys soltys at ziu.info
Mon Nov 5 16:32:36 PST 2012


- add some comments to clarify c{nul,add,max} modes
- resilient against -Wconversion (uint8_t casts)
- minor handover comment/flow changes
- clean up some old comment-outs

Signed-off-by: Michal Soltys <soltys at ziu.info>
---
 com32/chain/chain.c   |   12 +++++++-----
 com32/chain/options.c |   16 +---------------
 com32/chain/utility.c |   37 ++++++++++++++++++++-----------------
 com32/chain/utility.h |   19 ++++++++++++++++---
 4 files changed, 44 insertions(+), 40 deletions(-)

diff --git a/com32/chain/chain.c b/com32/chain/chain.c
index f0ccd97..060043c 100644
--- a/com32/chain/chain.c
+++ b/com32/chain/chain.c
@@ -404,7 +404,11 @@ static int setup_handover(const struct part_iter *iter,
     uint32_t synth_size;
     uint32_t *plen;
 
-    if (!iter->index) { /* implies typeraw or non-iterated */
+    /*
+     * we have to cover both non-iterated but otherwise properly detected
+     * gpt/dos schemes as well as raw disks; checking index for 0 covers both
+     */
+    if (iter->index == 0) {
 	uint32_t len;
 	/* RAW handover protocol */
 	synth_size = sizeof(struct disk_dos_part_entry);
@@ -453,7 +457,8 @@ static int setup_handover(const struct part_iter *iter,
 	disk_dos_part_dump(ha);
 	disk_gpt_part_dump((struct disk_gpt_part_entry *)(plen + 1));
 #endif
-    } else if (iter->type == typedos) {
+    /* the only possible case left is dos scheme */
+    } else {
 	/* MBR handover protocol */
 	synth_size = sizeof(struct disk_dos_part_entry);
 	ha = malloc(synth_size);
@@ -472,9 +477,6 @@ static int setup_handover(const struct part_iter *iter,
 	dprintf("MBR handover:\n");
 	disk_dos_part_dump(ha);
 #endif
-    } else {
-	/* shouldn't ever happen */
-	goto bail;
     }
 
     data->base = 0x7be;
diff --git a/com32/chain/options.c b/com32/chain/options.c
index 5f7433d..b831afa 100644
--- a/com32/chain/options.c
+++ b/com32/chain/options.c
@@ -166,7 +166,7 @@ void opt_set_defs(void)
 int opt_parse_args(int argc, char *argv[])
 {
     int i;
-    unsigned int v;
+    size_t v;
     char *p;
 
     for (i = 1; i < argc; i++) {
@@ -182,7 +182,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.bss = true;
 	    opt.maps = false;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	} else if (!strncmp(argv[i], "bs=", 3)) {
 	    opt.file = argv[i] + 3;
 	    opt.sect = false;
@@ -198,7 +197,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.fip = 0;
 	    opt.file = argv[i] + 6;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if (!strncmp(argv[i], "reactos=", 8)) {
 	    /*
@@ -212,7 +210,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.fip = 0x8100;
 	    opt.file = argv[i] + 8;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if (!strncmp(argv[i], "cmldr=", 6)) {
 	    opt.fseg = 0x2000;  /* CMLDR wants this address */
@@ -221,7 +218,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.file = argv[i] + 6;
 	    opt.cmldr = true;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if (!strncmp(argv[i], "freedos=", 8)) {
 	    opt.fseg = 0x60;    /* FREEDOS wants this address */
@@ -230,7 +226,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.sseg = 0x1FE0;
 	    opt.file = argv[i] + 8;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if ( (v = 6, !strncmp(argv[i], "msdos=", v) ||
 		     !strncmp(argv[i], "pcdos=", v)) ||
@@ -241,7 +236,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.sseg = 0x8000;
 	    opt.file = argv[i] + v;
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if (!strncmp(argv[i], "drmk=", 5)) {
 	    opt.fseg = 0x70;    /* DRMK wants this address */
@@ -253,7 +247,6 @@ int opt_parse_args(int argc, char *argv[])
 	    opt.file = argv[i] + 5;
 	    /* opt.drmk = true; */
 	    opt.setbpb = true;
-	    /* opt.save = true; */
 	    opt.hand = false;
 	} else if (!strncmp(argv[i], "grub=", 5)) {
 	    opt.fseg = 0x800;	/* stage2 wants this address */
@@ -371,13 +364,6 @@ int opt_parse_args(int argc, char *argv[])
 	goto bail;
     }
 
-#if 0
-    if ((!opt.maps || !opt.sect) && !opt.file) {
-	error("You have to load something.\n");
-	goto bail;
-    }
-#endif
-
     if (opt.filebpb && !opt.file) {
 	error("Option 'filebpb' requires a file.\n");
 	goto bail;
diff --git a/com32/chain/utility.c b/com32/chain/utility.c
index bc5b81a..d40c0dd 100644
--- a/com32/chain/utility.c
+++ b/com32/chain/utility.c
@@ -48,16 +48,6 @@ static const char *bpbtypes[] = {
     [7] =  "7.0",
 };
 
-void error(const char *msg)
-{
-    fputs(msg, stderr);
-}
-
-int guid_is0(const struct guid *guid)
-{
-    return !*(const uint64_t *)guid && !*((const uint64_t *)guid + 1);
-}
-
 void wait_key(void)
 {
     int cnt;
@@ -76,7 +66,19 @@ void wait_key(void)
     } while (!cnt || (cnt < 0 && errno == EAGAIN));
 }
 
-void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, uint32_t mode)
+/*
+ * mode explanation:
+ *
+ * cnul - "strict" mode, never returning higher value than obtained from cbios
+ * cadd - if the disk is larger than reported geometry /and/ if the geometry has
+ *        less cylinders than 1024 - it means that the total size is somewhere
+ *        between cs and cs+1; in this particular case, we bump the cs to be able
+ *        to return matching chs triplet
+ * cmax - assume we can use any cylinder value
+ *
+ * by default, lba2chs() calls use cadd
+ */
+void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, int mode)
 {
     uint32_t c, h, s, t;
     uint32_t cs, hs, ss;
@@ -89,9 +91,10 @@ void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, uint32_t m
 	cs = di->cyl;
 	hs = di->head;
 	ss = di->spt;
-	if (mode == l2c_cadd && cs < 1024 && di->lbacnt > cs*hs*ss)
-	    cs++;
-	else if (mode == l2c_cmax)
+	if (mode == l2c_cadd) {
+	    if (cs < 1024 && di->lbacnt > cs*hs*ss)
+		cs++;
+	} else if (mode == l2c_cmax)
 	    cs = 1024;
     } else {
 	if (di->disk & 0x80) {
@@ -116,9 +119,9 @@ void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, uint32_t m
 	c = t / hs;
     }
 
-    (*dst)[0] = h;
-    (*dst)[1] = s | ((c & 0x300) >> 2);
-    (*dst)[2] = c;
+    (*dst)[0] = (uint8_t)h;
+    (*dst)[1] = (uint8_t)(s | ((c & 0x300) >> 2));
+    (*dst)[2] = (uint8_t)c;
 }
 
 uint32_t get_file_lba(const char *filename)
diff --git a/com32/chain/utility.h b/com32/chain/utility.h
index 8b49122..a0519b7 100644
--- a/com32/chain/utility.h
+++ b/com32/chain/utility.h
@@ -32,6 +32,7 @@
 #define _COM32_CHAIN_UTILITY_H
 
 #include <stdint.h>
+#include <stdio.h>
 #include <syslinux/disk.h>
 
 #define bpbUNK	0
@@ -43,18 +44,30 @@
 #define bpbVNT	6
 #define bpbV70	7
 
+/* see utility.c for details */
 #define l2c_cnul 0
 #define l2c_cadd 1
 #define l2c_cmax 2
 
-void error(const char *msg);
-int guid_is0(const struct guid *guid);
 void wait_key(void);
-void lba2chs(disk_chs *dst, const struct disk_info *di, uint64_t lba, uint32_t mode);
+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, unsigned int *off);
 int bpb_detect(const uint8_t *bpb, const char *tag);
 
+static inline
+void error(const char *msg)
+{
+    fputs(msg, stderr);
+}
+
+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: */
-- 
1.7.10.4




More information about the Syslinux mailing list