root/tools/build.c

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

DEFINITIONS

This source file includes following definitions.
  1. die
  2. usage
  3. main

   1 /*
   2  *  linux/tools/build.c
   3  *
   4  *  (C) 1991  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 #include <stdio.h>      /* fprintf */
  20 #include <string.h>
  21 #include <stdlib.h>     /* contains exit */
  22 #include <sys/types.h>  /* unistd.h needs this */
  23 #include <sys/stat.h>
  24 #include <linux/fs.h>
  25 #include <unistd.h>     /* contains read/write */
  26 #include <fcntl.h>
  27 
  28 #define MINIX_HEADER 32
  29 #define GCC_HEADER 1024
  30 
  31 #define DEFAULT_MAJOR_ROOT 3
  32 #define DEFAULT_MINOR_ROOT 6
  33 
  34 /* max nr of sectors of setup: don't change unless you also change
  35  * bootsect etc */
  36 #define SETUP_SECTS 4
  37 
  38 #define STRINGIFY(x) #x
  39 
  40 void die(char * str)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42         fprintf(stderr,"%s\n",str);
  43         exit(1);
  44 }
  45 
  46 void usage(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         die("Usage: build bootsect setup system [> image]");
  49 }
  50 
  51 int main(int argc, char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53         int i,c,id;
  54         char buf[1024];
  55         char major_root, minor_root;
  56         struct stat sb;
  57 
  58         if ((argc != 4) && (argc != 5))
  59                 usage();
  60         if (argc == 5) {
  61                 if (strcmp(argv[4], "FLOPPY")) {
  62                         if (stat(argv[4], &sb)) {
  63                                 perror(argv[4]);
  64                                 die("Couldn't stat root device.");
  65                         }
  66                         major_root = MAJOR(sb.st_rdev);
  67                         minor_root = MINOR(sb.st_rdev);
  68                 } else {
  69                         major_root = 0;
  70                         minor_root = 0;
  71                 }
  72         } else {
  73                 major_root = DEFAULT_MAJOR_ROOT;
  74                 minor_root = DEFAULT_MINOR_ROOT;
  75         }
  76         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
  77         if ((major_root != 2) && (major_root != 3) &&
  78             (major_root != 0)) {
  79                 fprintf(stderr, "Illegal root device (major = %d)\n",
  80                         major_root);
  81                 die("Bad root device --- major #");
  82         }
  83         for (i=0;i<sizeof buf; i++) buf[i]=0;
  84         if ((id=open(argv[1],O_RDONLY,0))<0)
  85                 die("Unable to open 'boot'");
  86         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
  87                 die("Unable to read header of 'boot'");
  88         if (((long *) buf)[0]!=0x04100301)
  89                 die("Non-Minix header of 'boot'");
  90         if (((long *) buf)[1]!=MINIX_HEADER)
  91                 die("Non-Minix header of 'boot'");
  92         if (((long *) buf)[3]!=0)
  93                 die("Illegal data segment in 'boot'");
  94         if (((long *) buf)[4]!=0)
  95                 die("Illegal bss in 'boot'");
  96         if (((long *) buf)[5] != 0)
  97                 die("Non-Minix header of 'boot'");
  98         if (((long *) buf)[7] != 0)
  99                 die("Illegal symbol table in 'boot'");
 100         i=read(id,buf,sizeof buf);
 101         fprintf(stderr,"Boot sector %d bytes.\n",i);
 102         if (i != 512)
 103                 die("Boot block must be exactly 512 bytes");
 104         if ((*(unsigned short *)(buf+510)) != 0xAA55)
 105                 die("Boot block hasn't got boot flag (0xAA55)");
 106         buf[508] = (char) minor_root;
 107         buf[509] = (char) major_root;   
 108         i=write(1,buf,512);
 109         if (i!=512)
 110                 die("Write call failed");
 111         close (id);
 112         
 113         if ((id=open(argv[2],O_RDONLY,0))<0)
 114                 die("Unable to open 'setup'");
 115         if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
 116                 die("Unable to read header of 'setup'");
 117         if (((long *) buf)[0]!=0x04100301)
 118                 die("Non-Minix header of 'setup'");
 119         if (((long *) buf)[1]!=MINIX_HEADER)
 120                 die("Non-Minix header of 'setup'");
 121         if (((long *) buf)[3]!=0)
 122                 die("Illegal data segment in 'setup'");
 123         if (((long *) buf)[4]!=0)
 124                 die("Illegal bss in 'setup'");
 125         if (((long *) buf)[5] != 0)
 126                 die("Non-Minix header of 'setup'");
 127         if (((long *) buf)[7] != 0)
 128                 die("Illegal symbol table in 'setup'");
 129         for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
 130                 if (write(1,buf,c)!=c)
 131                         die("Write call failed");
 132         close (id);
 133         if (i > SETUP_SECTS*512)
 134                 die("Setup exceeds " STRINGIFY(SETUP_SECTS)
 135                         " sectors - rewrite build/boot/setup");
 136         fprintf(stderr,"Setup is %d bytes.\n",i);
 137         for (c=0 ; c<sizeof(buf) ; c++)
 138                 buf[c] = '\0';
 139         while (i<SETUP_SECTS*512) {
 140                 c = SETUP_SECTS*512-i;
 141                 if (c > sizeof(buf))
 142                         c = sizeof(buf);
 143                 if (write(1,buf,c) != c)
 144                         die("Write call failed");
 145                 i += c;
 146         }
 147         
 148         if ((id=open(argv[3],O_RDONLY,0))<0)
 149                 die("Unable to open 'system'");
 150         if (read(id,buf,GCC_HEADER) != GCC_HEADER)
 151                 die("Unable to read header of 'system'");
 152         if (((long *) buf)[5] != 0)
 153                 die("Non-GCC header of 'system'");
 154         for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
 155                 if (write(1,buf,c)!=c)
 156                         die("Write call failed");
 157         close(id);
 158         fprintf(stderr,"System is %d bytes.\n",i);
 159         return(0);
 160 }

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