[POWERPC] bootwrapper: Add a fatal error helper
authorMilton Miller <miltonm@bga.com>
Wed, 21 Mar 2007 15:02:44 +0000 (09:02 -0600)
committerPaul Mackerras <paulus@samba.org>
Mon, 26 Mar 2007 05:11:20 +0000 (15:11 +1000)
Add a macro fatal that calls printf then exit.  User must include stdio.h.

Typically replaces 3 lines with 1, although I added back some whitespace.

Signed-off-by: Milton Miller <miltonm@bga.com>
Signed-off-by: Paul Mackerras <paulus@samba.org>
arch/powerpc/boot/gunzip_util.c
arch/powerpc/boot/main.c
arch/powerpc/boot/of.c
arch/powerpc/boot/ops.h

index f7c95f2..8a97adf 100644 (file)
@@ -52,18 +52,14 @@ void gunzip_start(struct gunzip_state *state, void *src, int srclen)
                int r, flags;
 
                state->s.workspace = state->scratch;
                int r, flags;
 
                state->s.workspace = state->scratch;
-               if (zlib_inflate_workspacesize() > sizeof(state->scratch)) {
-                       printf("insufficient scratch space for gunzip\n\r");
-                       exit();
-               }
+               if (zlib_inflate_workspacesize() > sizeof(state->scratch))
+                       fatal("insufficient scratch space for gunzip\n\r");
 
                /* skip header */
                hdrlen = 10;
                flags = hdr[3];
 
                /* skip header */
                hdrlen = 10;
                flags = hdr[3];
-               if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
-                       printf("bad gzipped data\n\r");
-                       exit();
-               }
+               if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0)
+                       fatal("bad gzipped data\n\r");
                if ((flags & EXTRA_FIELD) != 0)
                        hdrlen = 12 + hdr[10] + (hdr[11] << 8);
                if ((flags & ORIG_NAME) != 0)
                if ((flags & EXTRA_FIELD) != 0)
                        hdrlen = 12 + hdr[10] + (hdr[11] << 8);
                if ((flags & ORIG_NAME) != 0)
@@ -74,16 +70,12 @@ void gunzip_start(struct gunzip_state *state, void *src, int srclen)
                                ;
                if ((flags & HEAD_CRC) != 0)
                        hdrlen += 2;
                                ;
                if ((flags & HEAD_CRC) != 0)
                        hdrlen += 2;
-               if (hdrlen >= srclen) {
-                       printf("gunzip_start: ran out of data in header\n\r");
-                       exit();
-               }
+               if (hdrlen >= srclen)
+                       fatal("gunzip_start: ran out of data in header\n\r");
 
                r = zlib_inflateInit2(&state->s, -MAX_WBITS);
 
                r = zlib_inflateInit2(&state->s, -MAX_WBITS);
-               if (r != Z_OK) {
-                       printf("inflateInit2 returned %d\n\r", r);
-                       exit();
-               }
+               if (r != Z_OK)
+                       fatal("inflateInit2 returned %d\n\r", r);
        }
 
        state->s.next_in = src + hdrlen;
        }
 
        state->s.next_in = src + hdrlen;
@@ -117,10 +109,8 @@ int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
                state->s.next_out = dst;
                state->s.avail_out = dstlen;
                r = zlib_inflate(&state->s, Z_FULL_FLUSH);
                state->s.next_out = dst;
                state->s.avail_out = dstlen;
                r = zlib_inflate(&state->s, Z_FULL_FLUSH);
-               if (r != Z_OK && r != Z_STREAM_END) {
-                       printf("inflate returned %d msg: %s\n\r", r, state->s.msg);
-                       exit();
-               }
+               if (r != Z_OK && r != Z_STREAM_END)
+                       fatal("inflate returned %d msg: %s\n\r", r, state->s.msg);
                len = state->s.next_out - (unsigned char *)dst;
        } else {
                /* uncompressed image */
                len = state->s.next_out - (unsigned char *)dst;
        } else {
                /* uncompressed image */
@@ -151,10 +141,8 @@ void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen)
        int len;
 
        len  = gunzip_partial(state, dst, dstlen);
        int len;
 
        len  = gunzip_partial(state, dst, dstlen);
-       if (len < dstlen) {
-               printf("gunzip_block: ran out of data\n\r");
-               exit();
-       }
+       if (len < dstlen)
+               fatal("gunzip_block: ran out of data\n\r");
 }
 
 /**
 }
 
 /**
index d872b75..df9e95a 100644 (file)
@@ -118,10 +118,9 @@ static struct addr_range prep_kernel(void)
        gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
        gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
 
        gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
        gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
 
-       if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei)) {
-               printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
-               exit();
-       }
+       if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
+               fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
+
        if (platform_ops.image_hdr)
                platform_ops.image_hdr(elfheader);
 
        if (platform_ops.image_hdr)
                platform_ops.image_hdr(elfheader);
 
@@ -135,11 +134,9 @@ static struct addr_range prep_kernel(void)
        if (platform_ops.vmlinux_alloc) {
                addr = platform_ops.vmlinux_alloc(ei.memsize);
        } else {
        if (platform_ops.vmlinux_alloc) {
                addr = platform_ops.vmlinux_alloc(ei.memsize);
        } else {
-               if ((unsigned long)_start < ei.memsize) {
-                       printf("Insufficient memory for kernel at address 0!"
+               if ((unsigned long)_start < ei.memsize)
+                       fatal("Insufficient memory for kernel at address 0!"
                               " (_start=%lx)\n\r", _start);
                               " (_start=%lx)\n\r", _start);
-                       exit();
-               }
        }
 
        /* Finally, gunzip the kernel */
        }
 
        /* Finally, gunzip the kernel */
@@ -189,11 +186,9 @@ static struct addr_range prep_initrd(struct addr_range vmlinux,
                printf("Allocating 0x%lx bytes for initrd ...\n\r",
                       initrd_size);
                initrd_addr = (unsigned long)malloc(initrd_size);
                printf("Allocating 0x%lx bytes for initrd ...\n\r",
                       initrd_size);
                initrd_addr = (unsigned long)malloc(initrd_size);
-               if (! initrd_addr) {
-                       printf("Can't allocate memory for initial "
+               if (! initrd_addr)
+                       fatal("Can't allocate memory for initial "
                               "ramdisk !\n\r");
                               "ramdisk !\n\r");
-                       exit();
-               }
                printf("Relocating initrd 0x%p <- 0x%p (0x%lx bytes)\n\r",
                       initrd_addr, old_addr, initrd_size);
                memmove((void *)initrd_addr, old_addr, initrd_size);
                printf("Relocating initrd 0x%p <- 0x%p (0x%lx bytes)\n\r",
                       initrd_addr, old_addr, initrd_size);
                memmove((void *)initrd_addr, old_addr, initrd_size);
@@ -203,10 +198,8 @@ static struct addr_range prep_initrd(struct addr_range vmlinux,
 
        /* Tell the kernel initrd address via device tree */
        devp = finddevice("/chosen");
 
        /* Tell the kernel initrd address via device tree */
        devp = finddevice("/chosen");
-       if (! devp) {
-               printf("Device tree has no chosen node!\n\r");
-               exit();
-       }
+       if (! devp)
+               fatal("Device tree has no chosen node!\n\r");
 
        initrd_start = (u32)initrd_addr;
        initrd_end = (u32)initrd_addr + initrd_size;
 
        initrd_start = (u32)initrd_addr;
        initrd_end = (u32)initrd_addr + initrd_size;
@@ -303,7 +296,6 @@ void start(void *sp)
                kentry((unsigned long)initrd.addr, initrd.size,
                       loader_info.promptr);
 
                kentry((unsigned long)initrd.addr, initrd.size,
                       loader_info.promptr);
 
-       /* console closed so printf below may not work */
-       printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");
-       exit();
+       /* console closed so printf in fatal below may not work */
+       fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
 }
 }
index c6f0d97..2cec5c1 100644 (file)
@@ -212,10 +212,9 @@ static void *of_vmlinux_alloc(unsigned long size)
 {
        void *p = malloc(size);
 
 {
        void *p = malloc(size);
 
-       if (!p) {
-               printf("Can't allocate memory for kernel image!\n\r");
-               exit();
-       }
+       if (!p)
+               fatal("Can't allocate memory for kernel image!\n\r");
+
        return p;
 }
 
        return p;
 }
 
index 93608b7..ea5368c 100644 (file)
@@ -158,6 +158,8 @@ static inline void exit(void)
                platform_ops.exit();
        for(;;);
 }
                platform_ops.exit();
        for(;;);
 }
+#define fatal(args...) { printf(args); exit(); }
+
 
 #define BSS_STACK(size) \
        static char _bss_stack[size]; \
 
 #define BSS_STACK(size) \
        static char _bss_stack[size]; \