From b4ba30102958f77e6bef1354459a06416408c2d7 Mon Sep 17 00:00:00 2001 From: Michal Soltys Date: Sun, 22 Aug 2010 13:44:01 +0200 Subject: [PATCH] chain.c: Split chain into smaller files new file: chain.h new file: common.h new file: mangle.c new file: mangle.h new file: options.c new file: options.h new file: utility.c new file: utility.h Signed-off-by: Michal Soltys --- com32/chain/Makefile | 2 +- com32/chain/chain.c | 788 +------------------------------------------------ com32/chain/chain.h | 50 ++++ com32/chain/common.h | 9 + com32/chain/mangle.c | 318 ++++++++++++++++++++ com32/chain/mangle.h | 19 ++ com32/chain/options.c | 336 +++++++++++++++++++++ com32/chain/options.h | 10 + com32/chain/partiter.c | 12 +- com32/chain/partiter.h | 6 +- com32/chain/utility.c | 114 +++++++ com32/chain/utility.h | 15 + 12 files changed, 891 insertions(+), 788 deletions(-) create mode 100644 com32/chain/chain.h create mode 100644 com32/chain/common.h create mode 100644 com32/chain/mangle.c create mode 100644 com32/chain/mangle.h create mode 100644 com32/chain/options.c create mode 100644 com32/chain/options.h create mode 100644 com32/chain/utility.c create mode 100644 com32/chain/utility.h diff --git a/com32/chain/Makefile b/com32/chain/Makefile index 337775c..054c768 100644 --- a/com32/chain/Makefile +++ b/com32/chain/Makefile @@ -15,7 +15,7 @@ topdir = ../.. include ../MCONFIG -OBJS = chain.o partiter.o +OBJS = chain.o partiter.o utility.o options.o mangle.o GCCEXTRA = -Wextra -Wconversion -pedantic -Wno-error -DDEBUG all: chain.c32 diff --git a/com32/chain/chain.c b/com32/chain/chain.c index 5af6c15..d1939ec 100644 --- a/com32/chain/chain.c +++ b/com32/chain/chain.c @@ -34,83 +34,22 @@ #include #include #include +#include "common.h" +#include "chain.h" +#include "utility.h" +#include "options.h" #include "partiter.h" +#include "mangle.h" -/* used in checks, whenever addresses supplied by user are sane */ +struct options opt; -#define ADDRMAX 0x9EFFFu -#define ADDRMIN 0x500u - -static const char cmldr_signature[8] = "cmdcons"; static int fixed_cnt; -static struct options { - unsigned int fseg; - unsigned int foff; - unsigned int fip; - unsigned int sseg; - unsigned int soff; - unsigned int sip; - unsigned int drvoff; - const char *drivename; - const char *partition; - const char *file; - const char *grubcfg; - bool isolinux; - bool cmldr; - bool drmk; - bool grub; - bool grldr; - bool maps; - bool hand; - bool hptr; - bool swap; - bool hide; - bool sethid; - bool setgeo; - bool setdrv; - bool sect; - bool save; - bool filebpb; - bool warn; - uint16_t keeppxe; - struct syslinux_rm_regs regs; -} opt; - -struct data_area { - void *data; - addr_t base; - addr_t size; -}; - -static void wait_key(void) -{ - int cnt; - char junk; - - /* drain */ - do { - errno = 0; - cnt = read(0, &junk, 1); - } while (cnt > 0 || (cnt < 0 && errno == EAGAIN)); - - /* wait */ - do { - errno = 0; - cnt = read(0, &junk, 1); - } while (!cnt || (cnt < 0 && errno == EAGAIN)); -} - -static void error(const char *msg) -{ - fputs(msg, stderr); -} - -static int no_ov(const struct data_area *a, const struct data_area *b) +static int overlap(const struct data_area *a, const struct data_area *b) { return - a->base + a->size <= b->base || - b->base + b->size <= a->base; + a->base + a->size > b->base && + b->base + b->size > a->base; } static int is_phys(uint8_t sdifs) @@ -400,377 +339,6 @@ out: free(mbr); } -static uint32_t get_file_lba(const char *filename) -{ - com32sys_t inregs; - uint32_t lba; - - /* Start with clean registers */ - memset(&inregs, 0, sizeof(com32sys_t)); - - /* Put the filename in the bounce buffer */ - strlcpy(__com32.cs_bounce, filename, __com32.cs_bounce_size); - - /* Call comapi_open() which returns a structure pointer in SI - * to a structure whose first member happens to be the LBA. - */ - inregs.eax.w[0] = 0x0006; - inregs.esi.w[0] = OFFS(__com32.cs_bounce); - inregs.es = SEG(__com32.cs_bounce); - __com32.cs_intcall(0x22, &inregs, &inregs); - - if ((inregs.eflags.l & EFLAGS_CF) || inregs.esi.w[0] == 0) { - return 0; /* Filename not found */ - } - - /* Since the first member is the LBA, we simply cast */ - lba = *((uint32_t *) MK_PTR(inregs.ds, inregs.esi.w[0])); - - /* Clean the registers for the next call */ - memset(&inregs, 0, sizeof(com32sys_t)); - - /* Put the filename in the bounce buffer */ - strlcpy(__com32.cs_bounce, filename, __com32.cs_bounce_size); - - /* Call comapi_close() to free the structure */ - inregs.eax.w[0] = 0x0008; - inregs.esi.w[0] = OFFS(__com32.cs_bounce); - inregs.es = SEG(__com32.cs_bounce); - __com32.cs_intcall(0x22, &inregs, &inregs); - - return lba; -} - -/* Convert string seg:off:ip values into numerical seg:off:ip ones */ - -static int soi_s2n(char *ptr, unsigned int *seg, - unsigned int *off, - unsigned int *ip) -{ - unsigned int segval = 0, offval = 0, ipval = 0, val; - char *p; - - segval = strtoul(ptr, &p, 0); - if (*p == ':') - offval = strtoul(p+1, &p, 0); - if (*p == ':') - ipval = strtoul(p+1, NULL, 0); - - val = (segval << 4) + offval; - - if (val < ADDRMIN || val > ADDRMAX) { - error("Invalid seg:off:* address specified..\n"); - goto bail; - } - - val = (segval << 4) + ipval; - - if (ipval > 0xFFFE || val < ADDRMIN || val > ADDRMAX) { - error("Invalid seg:*:ip address specified.\n"); - goto bail; - } - - if (seg) - *seg = segval; - if (off) - *off = offval; - if (ip) - *ip = ipval; - - return 0; - -bail: - return -1; -} - -static void usage(void) -{ - static const char *const usage[] = { "\ -Usage:\n\ - chain.c32 [options]\n\ - chain.c32 {fd|hd} [] [options]\n\ - chain.c32 mbr{:|=} [] [options]\n\ - chain.c32 guid{:|=} [] [options]\n\ - chain.c32 label{:|=}