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

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