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

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