blob: c44f5e2ec1006fc93d09c7b958ccd5d4cb0ad709 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 *
Domen Puncerf4549442005-06-25 14:58:59 -07009 * - bootsect: compatibility mbr which prints an error message if
10 * someone tries to boot the kernel directly.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * - 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
37typedef unsigned char byte;
38typedef unsigned short word;
39typedef 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
47byte buf[1024];
48int fd;
49int is_big_kernel;
50
51void 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
60void 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
66void usage(void)
67{
68 die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
69}
70
71int main(int argc, char ** argv)
72{
73 unsigned int i, c, sz, setup_sectors;
74 u32 sys_size;
75 byte major_root, minor_root;
76 struct stat sb;
77
78 if (argc > 2 && !strcmp(argv[1], "-b"))
79 {
80 is_big_kernel = 1;
81 argc--, argv++;
82 }
83 if ((argc < 4) || (argc > 5))
84 usage();
85 if (argc > 4) {
86 if (!strcmp(argv[4], "CURRENT")) {
87 if (stat("/", &sb)) {
88 perror("/");
89 die("Couldn't stat /");
90 }
91 major_root = major(sb.st_dev);
92 minor_root = minor(sb.st_dev);
93 } else if (strcmp(argv[4], "FLOPPY")) {
94 if (stat(argv[4], &sb)) {
95 perror(argv[4]);
96 die("Couldn't stat root device.");
97 }
98 major_root = major(sb.st_rdev);
99 minor_root = minor(sb.st_rdev);
100 } else {
101 major_root = 0;
102 minor_root = 0;
103 }
104 } else {
105 major_root = DEFAULT_MAJOR_ROOT;
106 minor_root = DEFAULT_MINOR_ROOT;
107 }
108 fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
109
110 file_open(argv[1]);
111 i = read(fd, buf, sizeof(buf));
112 fprintf(stderr,"Boot sector %d bytes.\n",i);
113 if (i != 512)
114 die("Boot block must be exactly 512 bytes");
115 if (buf[510] != 0x55 || buf[511] != 0xaa)
116 die("Boot block hasn't got boot flag (0xAA55)");
117 buf[508] = minor_root;
118 buf[509] = major_root;
119 if (write(1, buf, 512) != 512)
120 die("Write call failed");
121 close (fd);
122
123 file_open(argv[2]); /* Copy the setup code */
124 for (i=0 ; (c=read(fd, buf, sizeof(buf)))>0 ; i+=c )
125 if (write(1, buf, c) != c)
126 die("Write call failed");
127 if (c != 0)
128 die("read-error on `setup'");
129 close (fd);
130
131 setup_sectors = (i + 511) / 512; /* Pad unused space with zeros */
132 /* for compatibility with ancient versions of LILO. */
133 if (setup_sectors < SETUP_SECTS)
134 setup_sectors = SETUP_SECTS;
135 fprintf(stderr, "Setup is %d bytes.\n", i);
136 memset(buf, 0, sizeof(buf));
137 while (i < setup_sectors * 512) {
138 c = setup_sectors * 512 - i;
139 if (c > sizeof(buf))
140 c = sizeof(buf);
141 if (write(1, buf, c) != c)
142 die("Write call failed");
143 i += c;
144 }
145
146 file_open(argv[3]);
147 if (fstat (fd, &sb))
148 die("Unable to stat `%s': %m", argv[3]);
149 sz = sb.st_size;
150 fprintf (stderr, "System is %d kB\n", sz/1024);
151 sys_size = (sz + 15) / 16;
152 /* 0x40000*16 = 4.0 MB, reasonable estimate for the current maximum */
153 if (sys_size > (is_big_kernel ? 0x40000 : DEF_SYSSIZE))
154 die("System is too big. Try using %smodules.",
155 is_big_kernel ? "" : "bzImage or ");
156 while (sz > 0) {
157 int l, n;
158
159 l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
160 if ((n=read(fd, buf, l)) != l) {
161 if (n < 0)
162 die("Error reading %s: %m", argv[3]);
163 else
164 die("%s: Unexpected EOF", argv[3]);
165 }
166 if (write(1, buf, l) != l)
167 die("Write failed");
168 sz -= l;
169 }
170 close(fd);
171
172 if (lseek(1, 497, SEEK_SET) != 497) /* Write sizes to the bootsector */
173 die("Output: seek failed");
174 buf[0] = setup_sectors;
175 if (write(1, buf, 1) != 1)
176 die("Write of setup sector count failed");
177 if (lseek(1, 500, SEEK_SET) != 500)
178 die("Output: seek failed");
179 buf[0] = (sys_size & 0xff);
180 buf[1] = ((sys_size >> 8) & 0xff);
H. Peter Anvinf8eeaaf2005-09-06 15:17:24 -0700181 buf[2] = ((sys_size >> 16) & 0xff);
182 buf[3] = ((sys_size >> 24) & 0xff);
183 if (write(1, buf, 4) != 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 die("Write of image length failed");
185
186 return 0; /* Everything is OK */
187}