root/zBoot/xtract.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. die
  2. usage
  3. main

   1 /*
   2  *  linux/zBoot/xtract.c
   3  *
   4  *  Copyright (C) 1993  Hannu Savolainen
   5  *
   6  *      Extracts the system image and writes it to the stdout.
   7  *      based on tools/build.c by Linus Torvalds
   8  */
   9 
  10 #include <stdio.h>      /* fprintf */
  11 #include <string.h>
  12 #include <stdlib.h>     /* contains exit */
  13 #include <sys/types.h>  /* unistd.h needs this */
  14 #include <sys/stat.h>
  15 #include <sys/sysmacros.h>
  16 #include <unistd.h>     /* contains read/write */
  17 #include <fcntl.h>
  18 #include <a.out.h>
  19 #include <linux/config.h>
  20 
  21 #define N_MAGIC_OFFSET 1024
  22 
  23 static int GCC_HEADER = sizeof(struct exec);
  24 
  25 #define STRINGIFY(x) #x
  26 
  27 void die(char * str)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         fprintf(stderr,"%s\n",str);
  30         exit(1);
  31 }
  32 
  33 void usage(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35         die("Usage: xtract system [ | gzip | piggyback > piggy.s]");
  36 }
  37 
  38 int main(int argc, char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         int i,c,id, sz;
  41         char buf[1024];
  42         char major_root, minor_root;
  43         struct stat sb;
  44 
  45         struct exec *ex = (struct exec *)buf;
  46 
  47         if (argc  != 2)
  48                 usage();
  49         
  50         if ((id=open(argv[1],O_RDONLY,0))<0)
  51                 die("Unable to open 'system'");
  52         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
  53                 die("Unable to read header of 'system'");
  54         if (N_MAGIC(*ex) == ZMAGIC) {
  55                 GCC_HEADER = N_MAGIC_OFFSET;
  56                 lseek(id, GCC_HEADER, SEEK_SET);
  57         } else if (N_MAGIC(*ex) != QMAGIC)
  58                 die("Non-GCC header of 'system'");
  59 
  60         sz = N_SYMOFF(*ex) - GCC_HEADER + 4;    /* +4 to get the same result than tools/build */
  61 
  62         fprintf(stderr, "System size is %d\n", sz);
  63 
  64         while (sz)
  65         {
  66                 int l, n;
  67 
  68                 l = sz;
  69                 if (l > sizeof(buf)) l = sizeof(buf);
  70 
  71                 if ((n=read(id, buf, l)) !=l)
  72                 {
  73                         if (n == -1) 
  74                            perror(argv[1]);
  75                         else
  76                            fprintf(stderr, "Unexpected EOF\n");
  77 
  78                         die("Can't read system");
  79                 }
  80 
  81                 write(1, buf, l);
  82                 sz -= l;
  83         }
  84 
  85         close(id);
  86         return(0);
  87 }

/* [previous][next][first][last][top][bottom][index][help] */