memdisk: minor cleanups
authorH. Peter Anvin <hpa@zytor.com>
Mon, 8 Jun 2009 01:46:59 +0000 (18:46 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Mon, 8 Jun 2009 01:46:59 +0000 (18:46 -0700)
Minor cleanups to the source base; make die() also print an error
message and move die() into conio.c.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
memdisk/conio.c
memdisk/conio.h
memdisk/memdisk.h
memdisk/msetup.c
memdisk/setup.c
memdisk/unzip.c

index 9ccc8e2..0e75bd7 100644 (file)
@@ -109,11 +109,11 @@ static char *number(char *str, long num, int base, int size, int precision,
                    int type)
 {
     char c, sign, tmp[66];
-    const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+    const char *digits = "0123456789abcdef";
     int i;
 
     if (type & LARGE)
-       digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+       digits = "0123456789ABCDEF";
     if (type & LEFT)
        type &= ~ZEROPAD;
     if (base < 2 || base > 36)
@@ -173,9 +173,6 @@ static char *number(char *str, long num, int base, int size, int precision,
     return str;
 }
 
-/* Forward decl. needed for IP address printing stuff... */
-int sprintf(char *buf, const char *fmt, ...);
-
 int vsprintf(char *buf, const char *fmt, va_list args)
 {
     int len;
@@ -346,6 +343,7 @@ repeat:
     return str - buf;
 }
 
+#if 0
 int sprintf(char *buf, const char *fmt, ...)
 {
     va_list args;
@@ -356,18 +354,41 @@ int sprintf(char *buf, const char *fmt, ...)
     va_end(args);
     return i;
 }
+#endif
+
+int vprintf(const char *fmt, va_list args)
+{
+    char printf_buf[2048];
+    int printed;
+
+    printed = vsprintf(printf_buf, fmt, args);
+    puts(printf_buf);
+    return printed;
+}
 
 int printf(const char *fmt, ...)
 {
-    char printf_buf[1024];
     va_list args;
     int printed;
 
     va_start(args, fmt);
-    printed = vsprintf(printf_buf, fmt, args);
+    printed = vprintf(fmt, args);
     va_end(args);
+    return printed;
+}
 
-    puts(printf_buf);
+/*
+ * Jump here if all hope is gone...
+ */
+void __attribute__ ((noreturn)) die(const char *fmt, ...)
+{
+    va_list ap;
 
-    return printed;
+    va_start(ap, fmt);
+    vprintf(fmt, ap);
+    va_end(ap);
+
+    sti();
+    for (;;)
+       asm volatile("hlt");
 }
index 9fa8991..9775e62 100644 (file)
@@ -25,7 +25,9 @@
 
 int putchar(int);
 int puts(const char *);
+int vprintf(const char *, va_list ap);
 int printf(const char *, ...);
+void __attribute__((noreturn)) die(const char *, ...);
 unsigned int atou(const char *);
 
 #endif
index 5c82c9b..6bfeb90 100644 (file)
@@ -30,9 +30,6 @@ typedef void (*syscall_t) (uint8_t, com32sys_t *, com32sys_t *);
 extern __cdecl syscall_t syscall;
 extern void *sys_bounce;
 
-/* What to call when we're dead */
-extern void __attribute__ ((noreturn)) die(void);
-
 /* Standard routines */
 void *memcpy(void *, const void *, size_t);
 void *memset(void *, int, size_t);
index adb35f9..3674b28 100644 (file)
@@ -135,8 +135,7 @@ void get_mem(void)
        get_dos_mem();
        if (get_e801()) {
            if (get_88()) {
-               puts("MEMDISK: Unable to obtain memory map\n");
-               die();
+               die("MEMDISK: Unable to obtain memory map\n");
            }
        }
     }
index a46b12e..0a7c8c6 100644 (file)
@@ -225,7 +225,7 @@ static inline uint32_t rdz_32(uint32_t addr)
 #define CMD_BOOL       ((char *)-2)    /* Found boolean option */
 #define CMD_HASDATA(X) ((int)(X) >= 0)
 
-const char *getcmditem(const char *what)
+static const char *getcmditem(const char *what)
 {
     const char *p;
     const char *wp = what;
@@ -293,18 +293,23 @@ void unzip_if_needed(uint32_t * where_p, uint32_t * size_p)
                  &orig_crc, &offset) == 0) {
 
        if (offset + zbytes > size) {
-           /* Assertion failure; check_zip is supposed to guarantee this
-              never happens. */
-           puts("internal error: check_zip returned nonsense\n");
-           die();
+           /*
+            * Assertion failure; check_zip is supposed to guarantee this
+            * never happens.
+            */
+           die("internal error: check_zip returned nonsense\n");
        }
 
-       /* Find a good place to put it: search memory ranges in descending order
-          until we find one that is legal and fits */
+       /*
+        * Find a good place to put it: search memory ranges in descending
+        * order until we find one that is legal and fits
+        */
        okmem = 0;
        for (i = nranges - 1; i >= 0; i--) {
-           /* We can't use > 4G memory (32 bits only.)  Truncate to 2^32-1
-              so we don't have to deal with funny wraparound issues. */
+           /*
+            * We can't use > 4G memory (32 bits only.)  Truncate to 2^32-1
+            * so we don't have to deal with funny wraparound issues.
+            */
 
            /* Must be memory */
            if (ranges[i].type != 1)
@@ -333,16 +338,25 @@ void unzip_if_needed(uint32_t * where_p, uint32_t * size_p)
            if (startrange >= endrange)
                continue;
 
-           /* Must be large enough... don't rely on gzwhere for this (wraparound) */
+           /*
+            * Must be large enough... don't rely on gzwhere for this
+            * (wraparound)
+            */
            if (endrange - startrange < gzdatasize)
                continue;
 
-           /* This is where the gz image should be put if we put it in this range */
+           /*
+            * This is where the gz image would be put if we put it in this
+            * range...
+            */
            gzwhere = (endrange - gzdatasize) & ~(UNZIP_ALIGN - 1);
 
            /* Cast to uint64_t just in case we're flush with the top byte */
            if ((uint64_t) where + size >= gzwhere && where < endrange) {
-               /* Need to move source data to avoid compressed/uncompressed overlap */
+               /*
+                * Need to move source data to avoid compressed/uncompressed
+                * overlap
+                */
                uint32_t newwhere;
 
                if (gzwhere - startrange < size)
@@ -361,12 +375,9 @@ void unzip_if_needed(uint32_t * where_p, uint32_t * size_p)
            break;
        }
 
-       if (!okmem) {
-           printf
-               ("Not enough memory to decompress image (need 0x%08x bytes)\n",
+       if (!okmem)
+           die("Not enough memory to decompress image (need 0x%08x bytes)\n",
                 gzdatasize);
-           die();
-       }
 
        printf("gzip image: decompressed addr 0x%08x, len 0x%08x: ",
               target, gzdatasize);
@@ -453,7 +464,7 @@ struct dosemu_header {
 
 #define FOUR(a,b,c,d) (((a) << 24)|((b) << 16)|((c) << 8)|(d))
 
-const struct geometry *get_disk_image_geometry(uint32_t where, uint32_t size)
+static const struct geometry *get_disk_image_geometry(uint32_t where, uint32_t size)
 {
     static struct geometry hd_geometry;
     struct dosemu_header dosemu;
@@ -668,16 +679,6 @@ const struct geometry *get_disk_image_geometry(uint32_t where, uint32_t size)
 }
 
 /*
- * Jump here if all hope is gone...
- */
-void __attribute__ ((noreturn)) die(void)
-{
-    sti();
-    for (;;)
-       asm volatile("hlt");
-}
-
-/*
  * Find a $PnP installation check structure; return (ES << 16) + DI value
  */
 static uint32_t pnp_install_check(void)
@@ -796,10 +797,8 @@ void setup(const struct real_mode_args *rm_args_ptr)
     /* Show signs of life */
     printf("%s  %s\n", memdisk_version, copyright);
 
-    if (!shdr->ramdisk_image || !shdr->ramdisk_size) {
-       puts("MEMDISK: No ramdisk image specified!\n");
-       die();
-    }
+    if (!shdr->ramdisk_image || !shdr->ramdisk_size)
+       die("MEMDISK: No ramdisk image specified!\n");
 
     ramdisk_image = shdr->ramdisk_image;
     ramdisk_size = shdr->ramdisk_size;
@@ -965,10 +964,8 @@ void setup(const struct real_mode_args *rm_args_ptr)
     printf("Total size needed = %u bytes, allocating %uK\n",
           total_size, (total_size + 0x3ff) >> 10);
 
-    if (total_size > dos_mem) {
-       puts("MEMDISK: Insufficient low memory\n");
-       die();
-    }
+    if (total_size > dos_mem)
+       die("MEMDISK: Insufficient low memory\n");
 
     driveraddr = stddosmem - total_size;
     driveraddr &= ~0x3FF;
@@ -1141,10 +1138,9 @@ void setup(const struct real_mode_args *rm_args_ptr)
 
     /* Relocate the real-mode code to below the stub */
     rm_base = (driveraddr - rm_args.rm_size) & ~15;
-    if (rm_base < boot_base + boot_len) {
-       puts("MEMDISK: bootstrap too large to load\n");
-       die();
-    }
+    if (rm_base < boot_base + boot_len)
+       die("MEMDISK: bootstrap too large to load\n");
+
     relocate_rm_code(rm_base);
 
     /* Reboot into the new "disk" */
index c4b56ef..9144cf3 100644 (file)
@@ -144,8 +144,7 @@ static int fill_inbuf(void)
 {
     /* This should never happen.  We have already pointed the algorithm
        to all the data we have. */
-    printf("failed\nDecompression error: ran out of input data\n");
-    die();
+    die("failed\nDecompression error: ran out of input data\n");
 }
 
 /* ===========================================================================
@@ -175,8 +174,7 @@ static void flush_window(void)
 
 static void error(char *x)
 {
-    printf("failed\nDecompression error: %s\n", x);
-    die();
+    die("failed\nDecompression error: %s\n", x);
 }
 
 /* GZIP header */