2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1997 Martin Mares
7 * This file builds a disk-image from three different files:
9 * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
10 * - setup: 8086 machine code, sets up system parm
11 * - system: 80386 code for actual system
13 * It does some checking that all files are of the correct type, and
14 * just writes the result to stdout, removing headers and padding to
15 * the right amount. It also writes some system data to stderr.
19 * Changes by tytso to allow root device specification
20 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
21 * Cross compiling fixes by Gertjan van Wingerde, July 1996
22 * Rewritten by Martin Mares, April 1997
29 #include <sys/types.h>
31 #include <sys/sysmacros.h>
36 #define DEFAULT_MAJOR_ROOT 0
37 #define DEFAULT_MINOR_ROOT 0
39 /* Minimal number of setup sectors (see also bootsect.S) */
46 __attribute__((noreturn))
47 void die(const char *str, ...)
51 vfprintf(stderr, str, args);
56 void file_open(const char *name)
58 fd = open(name, O_RDONLY, 0);
60 die("Unable to open `%s': %m", name);
63 __attribute__((noreturn))
66 die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
69 int main(int argc, char **argv)
71 unsigned int i, c, sz, setup_sectors;
73 uint8_t major_root, minor_root;
76 if (argc > 2 && !strcmp(argv[1], "-b")) {
80 if ((argc < 4) || (argc > 5))
83 if (!strcmp(argv[4], "CURRENT")) {
86 die("Couldn't stat /");
88 major_root = major(sb.st_dev);
89 minor_root = minor(sb.st_dev);
90 } else if (strcmp(argv[4], "FLOPPY")) {
91 if (stat(argv[4], &sb)) {
93 die("Couldn't stat root device.");
95 major_root = major(sb.st_rdev);
96 minor_root = minor(sb.st_rdev);
102 major_root = DEFAULT_MAJOR_ROOT;
103 minor_root = DEFAULT_MINOR_ROOT;
105 fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
108 i = read(fd, buf, sizeof(buf));
109 fprintf(stderr, "Boot sector %d bytes.\n", i);
111 die("Boot block must be exactly 512 bytes");
112 if (buf[510] != 0x55 || buf[511] != 0xaa)
113 die("Boot block hasn't got boot flag (0xAA55)");
114 buf[508] = minor_root;
115 buf[509] = major_root;
116 if (write(1, buf, 512) != 512)
117 die("Write call failed");
120 /* Copy the setup code */
122 for (i = 0; (c = read(fd, buf, sizeof(buf))) > 0; i += c)
123 if (write(1, buf, c) != c)
124 die("Write call failed");
126 die("read-error on `setup'");
129 /* Pad unused space with zeros */
130 setup_sectors = (i + 511) / 512;
131 /* for compatibility with ancient versions of LILO. */
132 if (setup_sectors < SETUP_SECTS)
133 setup_sectors = SETUP_SECTS;
134 fprintf(stderr, "Setup is %d bytes.\n", i);
135 memset(buf, 0, sizeof(buf));
136 while (i < setup_sectors * 512) {
137 c = setup_sectors * 512 - i;
140 if (write(1, buf, c) != c)
141 die("Write call failed");
147 die("Unable to stat `%s': %m", argv[3]);
149 fprintf(stderr, "System is %d kB\n", sz / 1024);
150 sys_size = (sz + 15) / 16;
151 /* 0x28000*16 = 2.5 MB, conservative estimate for the current maximum */
152 if (sys_size > (is_big_kernel ? 0x28000 : DEF_SYSSIZE))
153 die("System is too big. Try using %smodules.",
154 is_big_kernel ? "" : "bzImage or ");
155 if (sys_size > 0xffff)
157 "warning: kernel is too big for standalone boot "
162 l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
163 n = read(fd, buf, l);
166 die("Error reading %s: %m", argv[3]);
168 die("%s: Unexpected EOF", argv[3]);
170 if (write(1, buf, l) != l)
176 /* Write sizes to the bootsector */
177 if (lseek(1, 497, SEEK_SET) != 497)
178 die("Output: seek failed");
179 buf[0] = setup_sectors;
180 if (write(1, buf, 1) != 1)
181 die("Write of setup sector count failed");
182 if (lseek(1, 500, SEEK_SET) != 500)
183 die("Output: seek failed");
184 buf[0] = (sys_size & 0xff);
185 buf[1] = ((sys_size >> 8) & 0xff);
186 if (write(1, buf, 2) != 2)
187 die("Write of image length failed");