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

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