root/tools/build.c

/* [previous][next][first][last][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)
     /* [previous][next][first][last][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)
     /* [previous][next][first][last][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)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         fprintf(stderr,"%s\n",str);
  77         exit(1);
  78 }
  79 
  80 void usage(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         die("Usage: build bootsect setup system [rootdev] [> image]");
  83 }
  84 
  85 int main(int argc, char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         int i,c,id, sz;
  88         char buf[1024];
  89         struct exec *ex = (struct exec *)buf;
  90         char major_root, minor_root;
  91         struct stat sb;
  92 
  93         if ((argc < 4) || (argc > 5))
  94                 usage();
  95         if (argc > 4) {
  96                 if (strcmp(argv[4], "FLOPPY")) {
  97                         if (stat(argv[4], &sb)) {
  98                                 perror(argv[4]);
  99                                 die("Couldn't stat root device.");
 100                         }
 101                         major_root = major(sb.st_rdev);
 102                         minor_root = minor(sb.st_rdev);
 103                 } else {
 104                         major_root = 0;
 105                         minor_root = 0;
 106                 }
 107         } else {
 108                 major_root = DEFAULT_MAJOR_ROOT;
 109                 minor_root = DEFAULT_MINOR_ROOT;
 110         }
 111         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
 112         for (i=0;i<sizeof buf; i++) buf[i]=0;
 113         if ((id=open(argv[1],O_RDONLY,0))<0)
 114                 die("Unable to open 'boot'");
 115         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 116                 die("Unable to read header of 'boot'");
 117         if (((long *) buf)[0]!=intel_long(0x04100301))
 118                 die("Non-Minix header of 'boot'");
 119         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 120                 die("Non-Minix header of 'boot'");
 121         if (((long *) buf)[3] != 0)
 122                 die("Illegal data segment in 'boot'");
 123         if (((long *) buf)[4] != 0)
 124                 die("Illegal bss in 'boot'");
 125         if (((long *) buf)[5] != 0)
 126                 die("Non-Minix header of 'boot'");
 127         if (((long *) buf)[7] != 0)
 128                 die("Illegal symbol table in 'boot'");
 129         i=read(id,buf,sizeof buf);
 130         fprintf(stderr,"Boot sector %d bytes.\n",i);
 131         if (i != 512)
 132                 die("Boot block must be exactly 512 bytes");
 133         if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
 134                 die("Boot block hasn't got boot flag (0xAA55)");
 135         buf[508] = (char) minor_root;
 136         buf[509] = (char) major_root;   
 137         i=write(1,buf,512);
 138         if (i!=512)
 139                 die("Write call failed");
 140         close (id);
 141         
 142         if ((id=open(argv[2],O_RDONLY,0))<0)
 143                 die("Unable to open 'setup'");
 144         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 145                 die("Unable to read header of 'setup'");
 146         if (((long *) buf)[0]!=intel_long(0x04100301))
 147                 die("Non-Minix header of 'setup'");
 148         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 149                 die("Non-Minix header of 'setup'");
 150         if (((long *) buf)[3] != 0)
 151                 die("Illegal data segment in 'setup'");
 152         if (((long *) buf)[4] != 0)
 153                 die("Illegal bss in 'setup'");
 154         if (((long *) buf)[5] != 0)
 155                 die("Non-Minix header of 'setup'");
 156         if (((long *) buf)[7] != 0)
 157                 die("Illegal symbol table in 'setup'");
 158         for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
 159                 if (write(1,buf,c)!=c)
 160                         die("Write call failed");
 161         if (c != 0)
 162                 die("read-error on 'setup'");
 163         close (id);
 164         if (i > SETUP_SECTS*512)
 165                 die("Setup exceeds " STRINGIFY(SETUP_SECTS)
 166                         " sectors - rewrite build/boot/setup");
 167         fprintf(stderr,"Setup is %d bytes.\n",i);
 168         for (c=0 ; c<sizeof(buf) ; c++)
 169                 buf[c] = '\0';
 170         while (i<SETUP_SECTS*512) {
 171                 c = SETUP_SECTS*512-i;
 172                 if (c > sizeof(buf))
 173                         c = sizeof(buf);
 174                 if (write(1,buf,c) != c)
 175                         die("Write call failed");
 176                 i += c;
 177         }
 178         
 179         if ((id=open(argv[3],O_RDONLY,0))<0)
 180                 die("Unable to open 'system'");
 181         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
 182                 die("Unable to read header of 'system'");
 183         if (N_MAGIC(*ex) != ZMAGIC)
 184                 die("Non-GCC header of 'system'");
 185         sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
 186         fprintf(stderr,"System is %d bytes.\n", sz);
 187         if (sz > SYS_SIZE*16)
 188                 die("System is too big");
 189         while (sz > 0) {
 190                 int l, n;
 191 
 192                 l = sz;
 193                 if (l > sizeof(buf))
 194                         l = sizeof(buf);
 195                 if ((n=read(id, buf, l)) != l) {
 196                         if (n == -1) 
 197                                 perror(argv[1]);
 198                         else
 199                                 fprintf(stderr, "Unexpected EOF\n");
 200                         die("Can't read 'system'");
 201                 }
 202                 write(1, buf, l);
 203                 sz -= l;
 204         }
 205         close(id);
 206         return(0);
 207 }

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