root/arch/i386/boot/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: exactly 512 bytes of 8086 machine code, loads the rest
  11  * - setup: 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  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  22  */
  23 
  24 #include <stdio.h>      /* fprintf */
  25 #include <string.h>
  26 #include <stdlib.h>     /* contains exit */
  27 #include <sys/types.h>  /* unistd.h needs this */
  28 #include <sys/stat.h>
  29 #include <sys/sysmacros.h>
  30 #include <unistd.h>     /* contains read/write */
  31 #include <fcntl.h>
  32 #include <linux/a.out.h>
  33 #include <linux/config.h>
  34 #include <errno.h>
  35 
  36 #define MINIX_HEADER 32
  37 
  38 #define N_MAGIC_OFFSET 1024
  39 #ifndef __BFD__
  40 static int GCC_HEADER = sizeof(struct exec);
  41 #endif
  42 
  43 #ifdef __BIG_KERNEL__
  44 #define SYS_SIZE 0xffff
  45 #else
  46 #define SYS_SIZE DEF_SYSSIZE
  47 #endif
  48 
  49 #define DEFAULT_MAJOR_ROOT 0
  50 #define DEFAULT_MINOR_ROOT 0
  51 
  52 /* max nr of sectors of setup: don't change unless you also change
  53  * bootsect etc */
  54 #define SETUP_SECTS 4
  55 
  56 #define STRINGIFY(x) #x
  57 
  58 typedef union {
  59         long l;
  60         short s[2];
  61         char b[4];
  62 } conv;
  63 
  64 long intel_long(long l)
     /* [previous][next][first][last][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         t.b[2] = l & 0xff; l >>= 8;
  71         t.b[3] = l & 0xff; l >>= 8;
  72         return t.l;
  73 }
  74 
  75 short intel_short(short l)
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77         conv t;
  78 
  79         t.b[0] = l & 0xff; l >>= 8;
  80         t.b[1] = l & 0xff; l >>= 8;
  81         return t.s[0];
  82 }
  83 
  84 void die(const char * str)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86         fprintf(stderr,"%s\n",str);
  87         exit(1);
  88 }
  89 
  90 void usage(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         die("Usage: build bootsect setup system [rootdev] [> image]");
  93 }
  94 
  95 int main(int argc, char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  96 {
  97         int i,c,id, sz;
  98         unsigned long sys_size;
  99         char buf[1024];
 100 #ifndef __BFD__
 101         struct exec *ex = (struct exec *)buf;
 102 #endif
 103         char major_root, minor_root;
 104         struct stat sb;
 105         unsigned char setup_sectors;
 106 
 107         if ((argc < 4) || (argc > 5))
 108                 usage();
 109         if (argc > 4) {
 110                 if (!strcmp(argv[4], "CURRENT")) {
 111                         if (stat("/", &sb)) {
 112                                 perror("/");
 113                                 die("Couldn't stat /");
 114                         }
 115                         major_root = major(sb.st_dev);
 116                         minor_root = minor(sb.st_dev);
 117                 } else if (strcmp(argv[4], "FLOPPY")) {
 118                         if (stat(argv[4], &sb)) {
 119                                 perror(argv[4]);
 120                                 die("Couldn't stat root device.");
 121                         }
 122                         major_root = major(sb.st_rdev);
 123                         minor_root = minor(sb.st_rdev);
 124                 } else {
 125                         major_root = 0;
 126                         minor_root = 0;
 127                 }
 128         } else {
 129                 major_root = DEFAULT_MAJOR_ROOT;
 130                 minor_root = DEFAULT_MINOR_ROOT;
 131         }
 132         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
 133         for (i=0;i<sizeof buf; i++) buf[i]=0;
 134         if ((id=open(argv[1],O_RDONLY,0))<0)
 135                 die("Unable to open 'boot'");
 136         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 137                 die("Unable to read header of 'boot'");
 138         if (((long *) buf)[0]!=intel_long(0x04100301))
 139                 die("Non-Minix header of 'boot'");
 140         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 141                 die("Non-Minix header of 'boot'");
 142         if (((long *) buf)[3] != 0)
 143                 die("Illegal data segment in 'boot'");
 144         if (((long *) buf)[4] != 0)
 145                 die("Illegal bss in 'boot'");
 146         if (((long *) buf)[5] != 0)
 147                 die("Non-Minix header of 'boot'");
 148         if (((long *) buf)[7] != 0)
 149                 die("Illegal symbol table in 'boot'");
 150         i=read(id,buf,sizeof buf);
 151         fprintf(stderr,"Boot sector %d bytes.\n",i);
 152         if (i != 512)
 153                 die("Boot block must be exactly 512 bytes");
 154         if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
 155                 die("Boot block hasn't got boot flag (0xAA55)");
 156         buf[508] = (char) minor_root;
 157         buf[509] = (char) major_root;   
 158         i=write(1,buf,512);
 159         if (i!=512)
 160                 die("Write call failed");
 161         close (id);
 162         
 163         if ((id=open(argv[2],O_RDONLY,0))<0)
 164                 die("Unable to open 'setup'");
 165         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 166                 die("Unable to read header of 'setup'");
 167         if (((long *) buf)[0]!=intel_long(0x04100301))
 168                 die("Non-Minix header of 'setup'");
 169         if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
 170                 die("Non-Minix header of 'setup'");
 171         if (((long *) buf)[3] != 0)
 172                 die("Illegal data segment in 'setup'");
 173         if (((long *) buf)[4] != 0)
 174                 die("Illegal bss in 'setup'");
 175         if (((long *) buf)[5] != 0)
 176                 die("Non-Minix header of 'setup'");
 177         if (((long *) buf)[7] != 0)
 178                 die("Illegal symbol table in 'setup'");
 179         for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
 180 #ifdef __BIG_KERNEL__
 181         {
 182                 if (!i) {
 183                         if (*((long *)(&buf[2])) != 0x53726448 )
 184                                 die("Wrong magic in loader header of 'setup'");
 185                         if (*((int *)(&buf[6])) < 0x200 )
 186                                 die("Wrong version of loader header of 'setup'");
 187                         buf[0x11] = 1; /* LOADED_HIGH */
 188                         *((long *)(&buf[0x14])) = 0x100000; /* code32_start */
 189                 }
 190 #endif
 191                 if (write(1,buf,c)!=c)
 192                         die("Write call failed");
 193 #ifdef __BIG_KERNEL__
 194         }
 195 #endif
 196         if (c != 0)
 197                 die("read-error on 'setup'");
 198         close (id);
 199         setup_sectors = (unsigned char)((i + 511) / 512);
 200         /* for compatibility with LILO */
 201         if (setup_sectors < SETUP_SECTS)
 202                 setup_sectors = SETUP_SECTS;
 203         fprintf(stderr,"Setup is %d bytes.\n",i);
 204         for (c=0 ; c<sizeof(buf) ; c++)
 205                 buf[c] = '\0';
 206         while (i < setup_sectors * 512) {
 207                 c = setup_sectors * 512 - i;
 208                 if (c > sizeof(buf))
 209                         c = sizeof(buf);
 210                 if (write(1,buf,c) != c)
 211                         die("Write call failed");
 212                 i += c;
 213         }
 214         
 215         if ((id=open(argv[3],O_RDONLY,0))<0)
 216                 die("Unable to open 'system'");
 217 #ifndef __BFD__
 218         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
 219                 die("Unable to read header of 'system'");
 220         if (N_MAGIC(*ex) == ZMAGIC) {
 221                 GCC_HEADER = N_MAGIC_OFFSET;
 222                 lseek(id, GCC_HEADER, SEEK_SET);
 223         } else if (N_MAGIC(*ex) != QMAGIC)
 224                 die("Non-GCC header of 'system'");
 225         fprintf(stderr,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
 226                 (ex->a_text+ex->a_data+ex->a_bss)/1024,
 227                 ex->a_text /1024,
 228                 ex->a_data /1024,
 229                 ex->a_bss  /1024);
 230         sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
 231 #else
 232         if (fstat (id, &sb)) {
 233           perror ("fstat");
 234           die ("Unable to stat 'system'");
 235         }
 236         sz = sb.st_size;
 237         fprintf (stderr, "System is %d kB\n", sz/1024);
 238 #endif
 239         sys_size = (sz + 15) / 16;
 240         if (sys_size > SYS_SIZE)
 241                 die("System is too big");
 242         while (sz > 0) {
 243                 int l, n;
 244 
 245                 l = sz;
 246                 if (l > sizeof(buf))
 247                         l = sizeof(buf);
 248                 if ((n=read(id, buf, l)) != l) {
 249                         if (n == -1) 
 250                                 perror(argv[1]);
 251                         else
 252                                 fprintf(stderr, "Unexpected EOF\n");
 253                         die("Can't read 'system'");
 254                 }
 255                 if (write(1, buf, l) != l)
 256                         die("Write failed");
 257                 sz -= l;
 258         }
 259         close(id);
 260         if (lseek(1, 497, 0) == 497) {
 261                 if (write(1, &setup_sectors, 1) != 1)
 262                         die("Write of setup sectors failed");
 263         }
 264         if (lseek(1,500,0) == 500) {
 265                 buf[0] = (sys_size & 0xff);
 266                 buf[1] = ((sys_size >> 8) & 0xff);
 267                 if (write(1, buf, 2) != 2)
 268                         die("Write failed");
 269         }
 270         return(0);
 271 }

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