Merge /spare/repo/linux-2.6/
[sfrench/cifs-2.6.git] / arch / i386 / boot / tools / build.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 1997 Martin Mares
4  */
5
6 /*
7  * This file builds a disk-image from three different files:
8  *
9  * - bootsect: compatibility mbr which prints an error message if
10  *             someone tries to boot the kernel directly.
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  * Cross compiling fixes by Gertjan van Wingerde, July 1996
23  * Rewritten by Martin Mares, April 1997
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/sysmacros.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <asm/boot.h>
36
37 typedef unsigned char byte;
38 typedef unsigned short word;
39 typedef unsigned long u32;
40
41 #define DEFAULT_MAJOR_ROOT 0
42 #define DEFAULT_MINOR_ROOT 0
43
44 /* Minimal number of setup sectors (see also bootsect.S) */
45 #define SETUP_SECTS 4
46
47 byte buf[1024];
48 int fd;
49 int is_big_kernel;
50
51 void die(const char * str, ...)
52 {
53         va_list args;
54         va_start(args, str);
55         vfprintf(stderr, str, args);
56         fputc('\n', stderr);
57         exit(1);
58 }
59
60 void file_open(const char *name)
61 {
62         if ((fd = open(name, O_RDONLY, 0)) < 0)
63                 die("Unable to open `%s': %m", name);
64 }
65
66 void usage(void)
67 {
68         die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
69 }
70
71 int main(int argc, char ** argv)
72 {
73         unsigned int i, sz, setup_sectors;
74         int c;
75         u32 sys_size;
76         byte major_root, minor_root;
77         struct stat sb;
78
79         if (argc > 2 && !strcmp(argv[1], "-b"))
80           {
81             is_big_kernel = 1;
82             argc--, argv++;
83           }
84         if ((argc < 4) || (argc > 5))
85                 usage();
86         if (argc > 4) {
87                 if (!strcmp(argv[4], "CURRENT")) {
88                         if (stat("/", &sb)) {
89                                 perror("/");
90                                 die("Couldn't stat /");
91                         }
92                         major_root = major(sb.st_dev);
93                         minor_root = minor(sb.st_dev);
94                 } else if (strcmp(argv[4], "FLOPPY")) {
95                         if (stat(argv[4], &sb)) {
96                                 perror(argv[4]);
97                                 die("Couldn't stat root device.");
98                         }
99                         major_root = major(sb.st_rdev);
100                         minor_root = minor(sb.st_rdev);
101                 } else {
102                         major_root = 0;
103                         minor_root = 0;
104                 }
105         } else {
106                 major_root = DEFAULT_MAJOR_ROOT;
107                 minor_root = DEFAULT_MINOR_ROOT;
108         }
109         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
110
111         file_open(argv[1]);
112         i = read(fd, buf, sizeof(buf));
113         fprintf(stderr,"Boot sector %d bytes.\n",i);
114         if (i != 512)
115                 die("Boot block must be exactly 512 bytes");
116         if (buf[510] != 0x55 || buf[511] != 0xaa)
117                 die("Boot block hasn't got boot flag (0xAA55)");
118         buf[508] = minor_root;
119         buf[509] = major_root;
120         if (write(1, buf, 512) != 512)
121                 die("Write call failed");
122         close (fd);
123
124         file_open(argv[2]);                                 /* Copy the setup code */
125         for (i=0 ; (c=read(fd, buf, sizeof(buf)))>0 ; i+=c )
126                 if (write(1, buf, c) != c)
127                         die("Write call failed");
128         if (c != 0)
129                 die("read-error on `setup'");
130         close (fd);
131
132         setup_sectors = (i + 511) / 512;        /* Pad unused space with zeros */
133         /* for compatibility with ancient versions of LILO. */
134         if (setup_sectors < SETUP_SECTS)
135                 setup_sectors = SETUP_SECTS;
136         fprintf(stderr, "Setup is %d bytes.\n", i);
137         memset(buf, 0, sizeof(buf));
138         while (i < setup_sectors * 512) {
139                 c = setup_sectors * 512 - i;
140                 if (c > sizeof(buf))
141                         c = sizeof(buf);
142                 if (write(1, buf, c) != c)
143                         die("Write call failed");
144                 i += c;
145         }
146
147         file_open(argv[3]);
148         if (fstat (fd, &sb))
149                 die("Unable to stat `%s': %m", argv[3]);
150         sz = sb.st_size;
151         fprintf (stderr, "System is %d kB\n", sz/1024);
152         sys_size = (sz + 15) / 16;
153         if (!is_big_kernel && sys_size > DEF_SYSSIZE)
154                 die("System is too big. Try using bzImage or modules.");
155         while (sz > 0) {
156                 int l, n;
157
158                 l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
159                 if ((n=read(fd, buf, l)) != l) {
160                         if (n < 0)
161                                 die("Error reading %s: %m", argv[3]);
162                         else
163                                 die("%s: Unexpected EOF", argv[3]);
164                 }
165                 if (write(1, buf, l) != l)
166                         die("Write failed");
167                 sz -= l;
168         }
169         close(fd);
170
171         if (lseek(1, 497, SEEK_SET) != 497)                 /* Write sizes to the bootsector */
172                 die("Output: seek failed");
173         buf[0] = setup_sectors;
174         if (write(1, buf, 1) != 1)
175                 die("Write of setup sector count failed");
176         if (lseek(1, 500, SEEK_SET) != 500)
177                 die("Output: seek failed");
178         buf[0] = (sys_size & 0xff);
179         buf[1] = ((sys_size >> 8) & 0xff);
180         buf[2] = ((sys_size >> 16) & 0xff);
181         buf[3] = ((sys_size >> 24) & 0xff);
182         if (write(1, buf, 4) != 4)
183                 die("Write of image length failed");
184
185         return 0;                                           /* Everything is OK */
186 }