From bf72d12c6116df8b8a88cc66adcbb46053c4a3f5 Mon Sep 17 00:00:00 2001 From: Michal Soltys Date: Mon, 25 Oct 2010 12:30:19 +0200 Subject: [PATCH] com32/chain: minor fixes and cleanups - make sure opt.maps implies opt.sect, and simplify conditions throughout the files - move structure opt -> options.c - adjust #includes - omitted values in seg= and sect= are assumed 0 in all cases now - mangler_handover(): eax for gpt handover set only for iterated disks - rename parse_args() -> opt_parse_args() - rename mangler_common() -> mangler_init() - add opt_set_defs() - if mmapped sector would conflict with the file, turn off mmapping instead of bailing out - update temporary chain.txt to reflect changes + other fixups Signed-off-by: Michal Soltys --- com32/chain/chain.c | 55 ++++++++++++++++++++------------------------------- com32/chain/chain.h | 6 +----- com32/chain/mangle.c | 6 +++--- com32/chain/mangle.h | 7 ++++++- com32/chain/options.c | 30 ++++++++++++++++++++++++---- com32/chain/options.h | 11 +++++++---- doc/chain.txt | 17 ++++++++-------- 7 files changed, 72 insertions(+), 60 deletions(-) diff --git a/com32/chain/chain.c b/com32/chain/chain.c index 99eed0e..f0d1497 100644 --- a/com32/chain/chain.c +++ b/com32/chain/chain.c @@ -41,8 +41,6 @@ #include "partiter.h" #include "mangle.h" -struct options opt; - static int fixed_cnt = 128; /* see comments in main() */ static int overlap(const struct data_area *a, const struct data_area *b) @@ -496,38 +494,28 @@ bail: int main(int argc, char *argv[]) { struct part_iter *iter = NULL; - void *sbck = NULL; struct data_area fdat, hdat, sdat, data[3]; int ndata = 0; console_ansi_raw(); -/* openconsole(&dev_null_r, &dev_stdcon_w);*/ - /* Prepare and set defaults */ memset(&fdat, 0, sizeof(fdat)); memset(&hdat, 0, sizeof(hdat)); memset(&sdat, 0, sizeof(sdat)); - 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 */ - opt.chain = true; /* by def. do chainload */ - opt.foff = opt.soff = opt.fip = opt.sip = 0x7C00; - opt.drivename = "boot"; -#ifdef DEBUG - opt.warn = true; -#endif - /* Parse arguments */ - if (parse_args(argc, argv)) + opt_set_defs(); + if (opt_parse_args(argc, argv)) goto bail; + #if 0 /* Get max fixed disk number */ fixed_cnt = *(uint8_t *)(0x475); /* - * hmm, looks like we can't do that + * hmm, looks like we can't do that - + * some bioses/vms just set it to 1 + * and go on living happily * any better options than hardcoded 0x80 - 0xFF ? */ #endif @@ -563,21 +551,20 @@ int main(int argc, char *argv[]) error("The sector cannot be loaded at such high address.\n"); goto bail; } - if (opt.file && opt.maps && overlap(&fdat, &sdat)) { - error("WARNING: The sector won't be loaded, as it would conflict with the boot file.\n"); - opt.sect = false; - } else { - if (!(sdat.data = disk_read_sectors(&iter->di, iter->start_lba, 1))) { - error("Couldn't read the sector.\n"); + if (!(sdat.data = disk_read_sectors(&iter->di, iter->start_lba, 1))) { + error("Couldn't read the sector.\n"); + goto bail; + } + if (opt.save) { + if (!(sbck = malloc(sdat.size))) { + error("Couldn't allocate cmp-buf for option 'save'.\n"); goto bail; } - if (opt.save) { - if (!(sbck = malloc(sdat.size))) { - error("Couldn't allocate cmp-buf for option 'save'.\n"); - goto bail; - } - memcpy(sbck, sdat.data, sdat.size); - } + memcpy(sbck, sdat.data, sdat.size); + } + if (opt.file && opt.maps && overlap(&fdat, &sdat)) { + error("WARNING: The sector won't be mmapped, as it would conflict with the boot file.\n"); + opt.maps = false; } } @@ -587,7 +574,7 @@ int main(int argc, char *argv[]) goto bail; /* Verify possible conflicts */ if ( ( opt.file && overlap(&fdat, &hdat)) || - ( opt.sect && overlap(&sdat, &hdat) && opt.maps) ) { + ( opt.maps && overlap(&sdat, &hdat)) ) { error("WARNING: Handover area won't be prepared,\n" "as it would conflict with the boot file and/or the sector.\n"); opt.hand = false; @@ -596,7 +583,7 @@ int main(int argc, char *argv[]) /* Adjust registers */ - mangler_common(iter); + mangler_init(iter); mangler_handover(iter, &hdat); mangler_grldr(iter); @@ -634,7 +621,7 @@ int main(int argc, char *argv[]) if (opt.file) memcpy(data + ndata++, &fdat, sizeof(fdat)); - if (opt.sect && opt.maps) + if (opt.maps) memcpy(data + ndata++, &sdat, sizeof(sdat)); if (opt.hand) memcpy(data + ndata++, &hdat, sizeof(hdat)); diff --git a/com32/chain/chain.h b/com32/chain/chain.h index 6714475..fc481bc 100644 --- a/com32/chain/chain.h +++ b/com32/chain/chain.h @@ -1,9 +1,7 @@ #ifndef _COM32_CHAIN_CHAIN_H #define _COM32_CHAIN_CHAIN_H -#include -#include -#include "options.h" +#include struct data_area { void *data; @@ -11,8 +9,6 @@ struct data_area { addr_t size; }; -extern struct options opt; - #endif /* vim: set ts=8 sts=4 sw=4 noet: */ diff --git a/com32/chain/mangle.c b/com32/chain/mangle.c index e4d4be3..c32cd5c 100644 --- a/com32/chain/mangle.c +++ b/com32/chain/mangle.c @@ -392,7 +392,7 @@ int mangles_cmldr(struct data_area *data) } /* Set common registers */ -int mangler_common(const struct part_iter *iter) +int mangler_init(const struct part_iter *iter) { /* Set initial registry values */ if (opt.file) { @@ -415,7 +415,7 @@ int mangler_common(const struct part_iter *iter) /* ds:si & ds:bp */ int mangler_handover(const struct part_iter *iter, const struct data_area *data) { - if (opt.sect && opt.file && opt.maps && !opt.hptr) { + 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.eax.l = 0; @@ -423,7 +423,7 @@ int mangler_handover(const struct part_iter *iter, const struct data_area *data) /* base is really 0x7be */ opt.regs.esi.l = opt.regs.ebp.l = data->base; opt.regs.ds = 0; - if (iter->type == typegpt) + if (iter->index && iter->type == typegpt) /* must be iterated and GPT */ opt.regs.eax.l = 0x54504721; /* '!GPT' */ else opt.regs.eax.l = 0; diff --git a/com32/chain/mangle.h b/com32/chain/mangle.h index eff24b9..4e09793 100644 --- a/com32/chain/mangle.h +++ b/com32/chain/mangle.h @@ -4,21 +4,26 @@ #include "chain.h" #include "partiter.h" +/* file's manglers */ int manglef_isolinux(struct data_area *data); int manglef_grub(const struct part_iter *iter, struct data_area *data); int manglef_bpb(const struct part_iter *iter, struct data_area *data); /* int manglef_drmk(struct data_area *data);*/ +/* sector's manglers */ int mangles_bpb(const struct part_iter *iter, struct data_area *data); int mangles_save(const struct part_iter *iter, const struct data_area *data, void *org); int mangles_cmldr(struct data_area *data); +/* sector + file's manglers */ int manglesf_bss(struct data_area *sec, struct data_area *fil); -int mangler_common(const struct part_iter *iter); +/* registers' manglers */ +int mangler_init(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); +/* partition layout's manglers */ int manglepe_mbrchshide(struct part_iter *miter); #endif diff --git a/com32/chain/options.c b/com32/chain/options.c index 37b9f11..288fe77 100644 --- a/com32/chain/options.c +++ b/com32/chain/options.c @@ -6,7 +6,9 @@ #include "utility.h" #include "options.h" -int soi_s2n(char *ptr, unsigned int *seg, +struct options opt; + +static int soi_s2n(char *ptr, unsigned int *seg, unsigned int *off, unsigned int *ip, unsigned int def) @@ -49,7 +51,7 @@ bail: return -1; } -void usage(void) +static void usage(void) { unsigned int i; static const char key[] = "Press any key...\n"; @@ -116,7 +118,21 @@ Usage:\n\ } } -int parse_args(int argc, char *argv[]) +void opt_set_defs(void) +{ + 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 */ + opt.chain = true; /* by def. do chainload */ + opt.foff = opt.soff = opt.fip = opt.sip = 0x7C00; + opt.drivename = "boot"; +#ifdef DEBUG + opt.warn = true; +#endif +} + +int opt_parse_args(int argc, char *argv[]) { int i; unsigned int v; @@ -250,12 +266,13 @@ int parse_args(int argc, char *argv[]) } else if (!strncmp(argv[i], "sect=", 5) || !strcmp(argv[i], "sect")) { if (argv[i][4]) { - if (soi_s2n(argv[i] + 5, &opt.sseg, &opt.soff, &opt.sip, 0x7c00)) + if (soi_s2n(argv[i] + 5, &opt.sseg, &opt.soff, &opt.sip, 0)) goto bail; } opt.sect = true; } else if (!strcmp(argv[i], "nosect")) { opt.sect = false; + opt.maps = false; } else if (!strcmp(argv[i], "save")) { opt.save = true; } else if (!strcmp(argv[i], "nosave")) { @@ -331,6 +348,11 @@ int parse_args(int argc, char *argv[]) goto bail; } + if (opt.maps && !opt.sect) { + error("Option 'maps' requires option 'sect'.\n"); + goto bail; + } + return 0; bail: return -1; diff --git a/com32/chain/options.h b/com32/chain/options.h index bc244f6..17c6a73 100644 --- a/com32/chain/options.h +++ b/com32/chain/options.h @@ -1,6 +1,9 @@ #ifndef _COM32_CHAIN_OPTIONS_H #define _COM32_CHAIN_OPTIONS_H +#include +#include + struct options { unsigned int fseg; unsigned int foff; @@ -34,10 +37,10 @@ struct options { struct syslinux_rm_regs regs; }; -int soi_s2n(char *ptr, unsigned int *seg, unsigned int *off, - unsigned int *ip, unsigned int def); -void usage(void); -int parse_args(int argc, char *argv[]); +extern struct options opt; + +void opt_set_defs(void); +int opt_parse_args(int argc, char *argv[]); #endif diff --git a/doc/chain.txt b/doc/chain.txt index c508651..2624310 100644 --- a/doc/chain.txt +++ b/doc/chain.txt @@ -40,7 +40,7 @@ A "service-only" run is possible when either: or -- 'nofile' and 'nomaps' are in effect +- 'nofile' and 'nomaps' (or 'nosect') are in effect This is useful for invocations such as: @@ -117,6 +117,7 @@ If any of the fields are ommited (e.g. 0x2000::), they default to 0. sect=:: nosect *sect=0:0x7c00:0x7c00 + nosect sets: nomaps This triplet lets you alter the addresses a sector will use. File is loaded at , the jump is made to . This option is mostly used @@ -127,8 +128,7 @@ relocated sector at some particular address (e.g. DRKM). is being chainloaded, sector is not necessary. The defaults if option is not specified, are 0:0x7c00:0x7c00. -If some of the fields are ommited (e.g. 0x2000::), segment defaults to 0, -offset and ip to 0x7c00. +If some of the fields are ommited (e.g. 0x2000::), they default to 0. *maps nomaps @@ -172,16 +172,15 @@ together with 'setbpb' or other ones using that implicitly. - this option never applies to a loaded file - chain module will never save anything to disk by default -- writing is only performed, if the values actually got changed +- writing is only performed, if the values actually changed *hand nohand -By default, a handover area is always prepared if possible and potentially -useful - meaning it doesn't overlap with other areas, and syslinux chainloads a -partition. It's often not necessary though - usually, a chainloaded file or -kernel don't care about it anymore, so a user can disable it explicitly with -this option. +By default, a handover area is always prepared if possible - meaning it doesn't +overlap with other areas. It's often not necessary though - usually, a +chainloaded file or kernel don't care about it anymore, so a user can disable +it explicitly with this option. hptr *nohptr -- 2.7.4