root/zBoot/xtract.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

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

   1 /*
   2  *  linux/tools/build.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * This file builds a disk-image from three different files:
   9  *
  10  * - bootsect: max 510 bytes of 8086 machine code, loads the rest
  11  * - setup: max 4 sectors of 8086 machine code, sets up system parm
  12  * - system: 80386 code for actual system
  13  *
  14  * It does some checking that all files are of the correct type, and
  15  * just writes the result to stdout, removing headers and padding to
  16  * the right amount. It also writes some system data to stderr.
  17  */
  18 
  19 /*
  20  * Changes by tytso to allow root device specification
  21  */
  22 
  23 #include <stdio.h>      /* fprintf */
  24 #include <string.h>
  25 #include <stdlib.h>     /* contains exit */
  26 #include <sys/types.h>  /* unistd.h needs this */
  27 #include <sys/stat.h>
  28 #include <sys/sysmacros.h>
  29 #include <unistd.h>     /* contains read/write */
  30 #include <fcntl.h>
  31 #include <a.out.h>
  32 #include <linux/config.h>
  33 
  34 #define GCC_HEADER 1024
  35 
  36 #define STRINGIFY(x) #x
  37 
  38 void die(char * str)
     /* [<][>][^][v][top][bottom][index][help] */
  39 {
  40         fprintf(stderr,"%s\n",str);
  41         exit(1);
  42 }
  43 
  44 void usage(void)
     /* [<][>][^][v][top][bottom][index][help] */
  45 {
  46         die("Usage: xtract system [ | gzip | piggyback > piggy.s]");
  47 }
  48 
  49 int main(int argc, char ** argv)
     /* [<][>][^][v][top][bottom][index][help] */
  50 {
  51         int i,c,id, sz;
  52         char buf[1024];
  53         char major_root, minor_root;
  54         struct stat sb;
  55 
  56         struct exec *ex = (struct exec *)buf;
  57 
  58         if (argc  != 2)
  59                 usage();
  60         
  61         if ((id=open(argv[1],O_RDONLY,0))<0)
  62                 die("Unable to open 'system'");
  63         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
  64                 die("Unable to read header of 'system'");
  65         if (N_MAGIC(*ex) != ZMAGIC)
  66                 die("Non-GCC header of 'system'");
  67 
  68         sz = N_SYMOFF(*ex) - GCC_HEADER + 4;    /* +4 to get the same result than tools/build */
  69 
  70         fprintf(stderr, "System size is %d\n", sz);
  71 
  72         while (sz)
  73         {
  74                 int l, n;
  75 
  76                 l = sz;
  77                 if (l > sizeof(buf)) l = sizeof(buf);
  78 
  79                 if ((n=read(id, buf, l)) !=l)
  80                 {
  81                         if (n == -1) 
  82                            perror(argv[1]);
  83                         else
  84                            fprintf(stderr, "Unexpected EOF\n");
  85 
  86                         die("Can't read system");
  87                 }
  88 
  89                 write(1, buf, l);
  90                 sz -= l;
  91         }
  92 
  93         close(id);
  94         return(0);
  95 }

/* [<][>][^][v][top][bottom][index][help] */