root/tools/build.c

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

DEFINITIONS

This source file includes following definitions.
  1. intel_long
  2. intel_short
  3. die
  4. usage
  5. 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 <linux/config.h>
  32 #include <linux/a.out.h>
  33 
  34 #define MINIX_HEADER 32
  35 #define GCC_HEADER 1024
  36 
  37 #define SYS_SIZE DEF_SYSSIZE
  38 
  39 #define DEFAULT_MAJOR_ROOT 0
  40 #define DEFAULT_MINOR_ROOT 0
  41 
  42 /* max nr of sectors of setup: don't change unless you also change
  43  * bootsect etc */
  44 #define SETUP_SECTS 4
  45 
  46 #define STRINGIFY(x) #x
  47 
  48 typedef union {
  49         long l;
  50         short s[2];
  51         char b[4];
  52 } conv;
  53 
  54 long intel_long(long l)
     /* [<][>][^][v][top][bottom][index][help] */
  55 {
  56         conv t;
  57 
  58         t.b[0] = l & 0xff; l >>= 8;
  59         t.b[1] = l & 0xff; l >>= 8;
  60         t.b[2] = l & 0xff; l >>= 8;
  61         t.b[3] = l & 0xff; l >>= 8;
  62         return t.l;
  63 }
  64 
  65 short intel_short(short l)
     /* [<][>][^][v][top][bottom][index][help] */
  66 {
  67         conv t;
  68 
  69         t.b[0] = l & 0xff; l >>= 8;
  70         t.b[1] = l & 0xff; l >>= 8;
  71         return t.s[0];
  72 }
  73 
  74 void die(char * str)
     /* [<][>][^][v][top][bottom][index][help] */
  75 {
  76         fprintf(stderr,"%s\n",str);
  77         exit(1);
  78 }
  79 
  80 void usage(void)
     /* [<][>][^][v][top][bottom][index][help] */
  81 {
  82         die("Usage: build bootsect setup system [rootdev] [> image]");
  83 }
  84 
  85 int main(int argc, char ** argv)
     /* [<][>][^][v][top][bottom][index][help] */
  86 {
  87         int i,c,id, sz;
  88         unsigned long sys_size;
  89         char buf[1024];
  90         struct exec *ex = (struct exec *)buf;
  91         char major_root, minor_root;
  92         struct stat sb;
  93 
  94         if ((argc < 4) || (argc > 5))
  95                 usage();
  96         if (argc > 4) {
  97                 if (strcmp(argv[4], "FLOPPY")) {
  98                         if (stat(argv[4], &sb)) {
  99                                 perror(argv[4]);
 100                                 die("Couldn't stat root device.");
 101                         }
 102                         major_root = major(sb.st_rdev);
 103                         minor_root = minor(sb.st_rdev);
 104                 } else {
 105                         major_root = 0;
 106                         minor_root = 0;
 107                 }
 108         } else {
 109                 major_root = DEFAULT_MAJOR_ROOT;
 110                 minor_root = DEFAULT_MINOR_ROOT;
 111         }
 112         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
 113         for (i=0;i<sizeof buf; i++) buf[i]=0;
 114         if ((id=open(argv[1],O_RDONLY,0))<0)
 115                 die("Unable to open 'boot'");
 116         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 117                 die("Unable to read header of 'boot'");
 118         if (((long *) buf)[0]!=intel_long(0x04100301))
 119                 die("Non-Minix header of 'boot'");
 120         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 121                 die("Non-Minix header of 'boot'");
 122         if (((long *) buf)[3] != 0)
 123                 die("Illegal data segment in 'boot'");
 124         if (((long *) buf)[4] != 0)
 125                 die("Illegal bss in 'boot'");
 126         if (((long *) buf)[5] != 0)
 127                 die("Non-Minix header of 'boot'");
 128         if (((long *) buf)[7] != 0)
 129                 die("Illegal symbol table in 'boot'");
 130         i=read(id,buf,sizeof buf);
 131         fprintf(stderr,"Boot sector %d bytes.\n",i);
 132         if (i != 512)
 133                 die("Boot block must be exactly 512 bytes");
 134         if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
 135                 die("Boot block hasn't got boot flag (0xAA55)");
 136         buf[508] = (char) minor_root;
 137         buf[509] = (char) major_root;   
 138         i=write(1,buf,512);
 139         if (i!=512)
 140                 die("Write call failed");
 141         close (id);
 142         
 143         if ((id=open(argv[2],O_RDONLY,0))<0)
 144                 die("Unable to open 'setup'");
 145         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 146                 die("Unable to read header of 'setup'");
 147         if (((long *) buf)[0]!=intel_long(0x04100301))
 148                 die("Non-Minix header of 'setup'");
 149         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 150                 die("Non-Minix header of 'setup'");
 151         if (((long *) buf)[3] != 0)
 152                 die("Illegal data segment in 'setup'");
 153         if (((long *) buf)[4] != 0)
 154                 die("Illegal bss in 'setup'");
 155         if (((long *) buf)[5] != 0)
 156                 die("Non-Minix header of 'setup'");
 157         if (((long *) buf)[7] != 0)
 158                 die("Illegal symbol table in 'setup'");
 159         for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
 160                 if (write(1,buf,c)!=c)
 161                         die("Write call failed");
 162         if (c != 0)
 163                 die("read-error on 'setup'");
 164         close (id);
 165         if (i > SETUP_SECTS*512)
 166                 die("Setup exceeds " STRINGIFY(SETUP_SECTS)
 167                         " sectors - rewrite build/boot/setup");
 168         fprintf(stderr,"Setup is %d bytes.\n",i);
 169         for (c=0 ; c<sizeof(buf) ; c++)
 170                 buf[c] = '\0';
 171         while (i<SETUP_SECTS*512) {
 172                 c = SETUP_SECTS*512-i;
 173                 if (c > sizeof(buf))
 174                         c = sizeof(buf);
 175                 if (write(1,buf,c) != c)
 176                         die("Write call failed");
 177                 i += c;
 178         }
 179         
 180         if ((id=open(argv[3],O_RDONLY,0))<0)
 181                 die("Unable to open 'system'");
 182         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
 183                 die("Unable to read header of 'system'");
 184         if (N_MAGIC(*ex) != ZMAGIC)
 185                 die("Non-GCC header of 'system'");
 186         sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
 187         fprintf(stderr,"System is %d bytes.\n", sz);
 188         sys_size = (sz + 15) / 16;
 189         if (sys_size > SYS_SIZE)
 190                 die("System is too big");
 191         while (sz > 0) {
 192                 int l, n;
 193 
 194                 l = sz;
 195                 if (l > sizeof(buf))
 196                         l = sizeof(buf);
 197                 if ((n=read(id, buf, l)) != l) {
 198                         if (n == -1) 
 199                                 perror(argv[1]);
 200                         else
 201                                 fprintf(stderr, "Unexpected EOF\n");
 202                         die("Can't read 'system'");
 203                 }
 204                 write(1, buf, l);
 205                 sz -= l;
 206         }
 207         close(id);
 208         if (lseek(1,500,0) == 500) {
 209                 buf[0] = (sys_size & 0xff);
 210                 buf[1] = ((sys_size >> 8) & 0xff);
 211                 write(1, buf, 2);
 212         }
 213         return(0);
 214 }

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