[syslinux:master] com32/chain: move partition entries' mangling to mangle.c

syslinux-bot for Michal Soltys soltys at ziu.info
Mon Mar 26 15:03:25 PDT 2012


Commit-ID:  756c2e4179e70c92085207d1fe887fc9bff9fdeb
Gitweb:     http://www.syslinux.org/commit/756c2e4179e70c92085207d1fe887fc9bff9fdeb
Author:     Michal Soltys <soltys at ziu.info>
AuthorDate: Tue, 5 Oct 2010 23:54:04 +0200
Committer:  Michal Soltys <soltys at ziu.info>
CommitDate: Fri, 8 Oct 2010 12:40:51 +0200

com32/chain: move partition entries' mangling to mangle.c

All functions related to partition entries' mangling has been moved
to mangle.c, including appropriate renames:

pentry_mangle() -> manglepe_mbrchshide()
pem_setchs() -> mpe_setchs()
pem_sethide() -> mpe_sethide()

Signed-off-by: Michal Soltys <soltys at ziu.info>

---
 com32/chain/chain.c  |  158 +-------------------------------------------------
 com32/chain/mangle.c |  109 ++++++++++++++++++++++++++++++++++
 com32/chain/mangle.h |    2 +
 3 files changed, 113 insertions(+), 156 deletions(-)

diff --git a/com32/chain/chain.c b/com32/chain/chain.c
index 73d107d..1236b38 100644
--- a/com32/chain/chain.c
+++ b/com32/chain/chain.c
@@ -288,159 +288,6 @@ enomem:
     error("Out of memory\n");
     return;
 }
-#if 0
-static void hide_unhide(const struct part_iter *_iter)
-{
-    int i;
-    struct disk_dos_mbr *mbr = NULL;
-    struct disk_dos_part_entry *pt;
-    const uint16_t mask =
-	(1 << 0x01) | (1 << 0x04) | (1 << 0x06) |
-	(1 << 0x07) | (1 << 0x0b) | (1 << 0x0c) | (1 << 0x0e);
-    uint8_t t;
-    bool write_back = false;
-
-    if (_iter->type != typedos) {
-	error("Option 'hide' is only meaningful for legacy partition scheme.\n");
-	goto bail;
-    }
-    if (!(mbr = disk_read_sectors(&_iter->di, 0, 1))) {
-	error("WARNING: Couldn't read MBR to hide/unhide partitions.\n");
-	goto bail;
-    }
-
-    if (_iter->index < 1 || _iter->index > 4)
-	error("WARNING: option 'hide' specified with a non-primary partition.\n");
-
-    for (i = 1; i <= 4; i++) {
-	pt = mbr->table + i - 1;
-	t = pt->ostype;
-	if ((t <= 0x1f) && ((mask >> (t & ~0x10u)) & 1)) {
-	    /* It's a hideable partition type */
-	    if (i == _iter->index)
-		t &= (uint8_t)(~0x10u);	/* unhide */
-	    else
-		t |= 0x10u;	/* hide */
-	}
-	if (t != pt->ostype) {
-	    write_back = true;
-	    pt->ostype = t;
-	}
-    }
-    if (write_back && disk_write_verify_sector(&_iter->di, 0, mbr))
-	error("WARNING: failed to write MBR for option 'hide'\n");
-
-bail:
-    free(mbr);
-}
-#endif
-
-static int pem_sethide(struct part_iter *miter, struct part_iter *iter)
-{
-    struct disk_dos_part_entry *mdp, *dp;
-    static const uint16_t mask =
-	(1 << 0x01) | (1 << 0x04) | (1 << 0x06) |
-	(1 << 0x07) | (1 << 0x0b) | (1 << 0x0c) | (1 << 0x0e);
-    uint8_t t;
-
-    mdp = (struct disk_dos_part_entry *)miter->record;
-    dp = (struct disk_dos_part_entry *)iter->record;
-    t = dp->ostype;
-
-    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 */
-	else
-	    t |= 0x10u;	/* hide */
-    }
-    if (dp->ostype != t) {
-	dp->ostype = t;
-	/*
-	 * the type of the partition being booted has to be adjusted in
-	 * the match iterator (miter) as well
-	 */
-	if (miter->index == iter->index) {
-	    mdp->ostype = t;
-	}
-	return -1;
-    }
-    return 0;
-}
-
-static int pem_setchs(const struct disk_info *di,
-		     struct disk_dos_part_entry *dp,
-		     uint32_t lba1)
-{
-    uint32_t ochs1, ochs2;
-
-    ochs1 = *(uint32_t *)dp->start;
-    ochs2 = *(uint32_t *)dp->end;
-
-    *(uint32_t *)dp->start =
-	lba2chs(di, lba1, l2c_cadd) |
-	(*(uint32_t *)dp->start & 0xFF000000);
-
-    *(uint32_t *)dp->end =
-	lba2chs(di, lba1 + dp->length - 1, l2c_cadd) |
-	(*(uint32_t *)dp->end & 0xFF000000);
-
-    return
-	*(uint32_t *)dp->start != ochs1 ||
-	*(uint32_t *)dp->end != ochs2;
-}
-
-static int pentry_mangle(struct part_iter *miter)
-{
-    int wb = 0, werr = 0;
-    struct part_iter *iter = NULL;
-    struct disk_dos_part_entry *dp;
-    int ridx;
-
-    if (miter->type != typedos) {
-	error("Partition entry mangling ('[un]hide[all]', 'mbrchs')\n"
-	      "is meaningful only for legacy partition scheme.\n");
-	goto bail;
-    }
-    if (opt.hide &&
-	    ((miter->index < 1 && opt.hide < 4) || /* try to hide a disk */
-	     (miter->index > 4 && opt.hide == 1))) /* try to hide a part when limited to pri */
-	error("WARNING: It's impossible to hide the selected partition (or you selected a disk).\n");
-
-    if (!(iter = pi_begin(&miter->di, 1)))  /* turn on stepall */
-	goto bail;
-
-    while (!pi_next(&iter) && !werr && (opt.hide & 2 || opt.mbrchs)) {
-	ridx = iter->rawindex;
-	dp = (struct disk_dos_part_entry *)iter->record;
-
-	if (dp->ostype) {
-	    if (opt.hide & 2 || (opt.hide & 1 && ridx <= 4)) {
-		wb |= pem_sethide(miter, iter);
-	    }
-	    if (opt.mbrchs) {
-		wb |= pem_setchs(&iter->di, dp, (uint32_t)iter->start_lba);
-		if (ridx > 4)
-		    wb |= pem_setchs(&iter->di, dp + 1, iter->sub.dos.nebr_lba);
-	    }
-	}
-
-	if (ridx >= 4 && wb && !werr) {
-	    werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
-	    wb = 0;
-	}
-    }
-    /* last write */
-    if (wb && !werr)
-	werr |= disk_write_sectors(&miter->di, iter->sub.dos.cebr_lba, iter->data, 1);
-
-bail:
-    pi_del(&iter);
-    if (werr)
-	error("WARNING: failed to write E/MBR for partition\n"
-	      "mangling options ('[un]hide[all]', 'mbrchs').\n");
-    return 0;
-}
 
 int find_dp(struct part_iter **_iter)
 {
@@ -690,9 +537,8 @@ int main(int argc, char *argv[])
 	goto bail;
 
     /* Perform initial partition entry mangling */
-    if (opt.hide || opt.mbrchs)
-	pentry_mangle(iter);
-/*	hide_unhide(iter);*/
+    if (manglepe_mbrchshide(iter))
+	goto bail;
 
     /* Load the boot file */
     if (opt.file) {
diff --git a/com32/chain/mangle.c b/com32/chain/mangle.c
index 0d8d7f0..b7645cc 100644
--- a/com32/chain/mangle.c
+++ b/com32/chain/mangle.c
@@ -429,4 +429,113 @@ int mangler_grldr(const struct part_iter *iter)
     return 0;
 }
 
+static int mpe_sethide(struct part_iter *miter, struct part_iter *iter)
+{
+    struct disk_dos_part_entry *mdp, *dp;
+    static const uint16_t mask =
+	(1 << 0x01) | (1 << 0x04) | (1 << 0x06) |
+	(1 << 0x07) | (1 << 0x0b) | (1 << 0x0c) | (1 << 0x0e);
+    uint8_t t;
+
+    mdp = (struct disk_dos_part_entry *)miter->record;
+    dp = (struct disk_dos_part_entry *)iter->record;
+    t = dp->ostype;
+
+    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 */
+	else
+	    t |= 0x10u;	/* hide */
+    }
+    if (dp->ostype != t) {
+	dp->ostype = t;
+	/*
+	 * the type of the partition being booted has to be adjusted in
+	 * the match iterator (miter) as well
+	 */
+	if (miter->index == iter->index) {
+	    mdp->ostype = t;
+	}
+	return -1;
+    }
+    return 0;
+}
+
+static int mpe_setchs(const struct disk_info *di,
+		     struct disk_dos_part_entry *dp,
+		     uint32_t lba1)
+{
+    uint32_t ochs1, ochs2;
+
+    ochs1 = *(uint32_t *)dp->start;
+    ochs2 = *(uint32_t *)dp->end;
+
+    *(uint32_t *)dp->start =
+	lba2chs(di, lba1, l2c_cadd) |
+	(*(uint32_t *)dp->start & 0xFF000000);
+
+    *(uint32_t *)dp->end =
+	lba2chs(di, lba1 + dp->length - 1, l2c_cadd) |
+	(*(uint32_t *)dp->end & 0xFF000000);
+
+    return
+	*(uint32_t *)dp->start != ochs1 ||
+	*(uint32_t *)dp->end != ochs2;
+}
+
+int manglepe_mbrchshide(struct part_iter *miter)
+{
+    int wb = 0, werr = 0;
+    struct part_iter *iter = NULL;
+    struct disk_dos_part_entry *dp;
+    int ridx;
+
+    if (!(opt.mbrchs || opt.hide))
+	return 0;
+
+    if (miter->type != typedos) {
+	error("Partition entry mangling ('[un]hide[all]', 'mbrchs')\n"
+	      "is meaningful only for legacy partition scheme.\n");
+	return -1;
+    }
+    if (opt.hide &&
+	    ((miter->index < 1 && opt.hide < 4) || /* try to hide a disk */
+	     (miter->index > 4 && opt.hide == 1))) /* try to hide a part when limited to pri */
+	error("WARNING: It's impossible to hide the selected partition (or you selected a disk).\n");
+
+    if (!(iter = pi_begin(&miter->di, 1)))  /* turn on stepall */
+	return -1;
+
+    while (!pi_next(&iter) && !werr && (opt.hide & 2 || opt.mbrchs)) {
+	ridx = iter->rawindex;
+	dp = (struct disk_dos_part_entry *)iter->record;
+
+	if (dp->ostype) {
+	    if (opt.hide & 2 || (opt.hide & 1 && ridx <= 4)) {
+		wb |= mpe_sethide(miter, iter);
+	    }
+	    if (opt.mbrchs) {
+		wb |= mpe_setchs(&iter->di, dp, (uint32_t)iter->start_lba);
+		if (ridx > 4)
+		    wb |= mpe_setchs(&iter->di, dp + 1, iter->sub.dos.nebr_lba);
+	    }
+	}
+
+	if (ridx >= 4 && wb && !werr) {
+	    werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
+	    wb = 0;
+	}
+    }
+    /* last write */
+    if (wb && !werr)
+	werr |= disk_write_sectors(&miter->di, iter->sub.dos.cebr_lba, iter->data, 1);
+
+    pi_del(&iter);
+    if (werr)
+	error("WARNING: failed to write E/MBR for partition\n"
+	      "mangling options ('[un]hide[all]', 'mbrchs').\n");
+    return 0;
+}
+
 /* vim: set ts=8 sts=4 sw=4 noet: */
diff --git a/com32/chain/mangle.h b/com32/chain/mangle.h
index 76bab4b..eff24b9 100644
--- a/com32/chain/mangle.h
+++ b/com32/chain/mangle.h
@@ -19,6 +19,8 @@ int mangler_common(const struct part_iter *iter);
 int mangler_handover(const struct part_iter *iter, const struct data_area *data);
 int mangler_grldr(const struct part_iter *iter);
 
+int manglepe_mbrchshide(struct part_iter *miter);
+
 #endif
 
 /* vim: set ts=8 sts=4 sw=4 noet: */


More information about the Syslinux-commits mailing list