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

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