1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
9 * This file builds a disk-image from three different files:
11 * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
12 * - setup: 8086 machine code, sets up system parm
13 * - system: 80386 code for actual system
15 * It does some checking that all files are of the correct type, and
16 * just writes the result to stdout, removing headers and padding to
17 * the right amount. It also writes some system data to stderr.
21 * Changes by tytso to allow root device specification
22 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
23 * Cross compiling fixes by Gertjan van Wingerde, July 1996
26 #include <stdio.h> /* fprintf */
28 #include <stdlib.h> /* contains exit */
29 #include <sys/types.h> /* unistd.h needs this */
31 #include <sys/sysmacros.h>
32 #include <unistd.h> /* contains read/write */
36 #define MINIX_HEADER 32
38 #define N_MAGIC_OFFSET 1024
40 static int GCC_HEADER
= sizeof(struct exec
);
44 #define SYS_SIZE 0xffff
46 #define SYS_SIZE DEF_SYSSIZE
49 #define DEFAULT_MAJOR_ROOT 0
50 #define DEFAULT_MINOR_ROOT 0
52 /* max nr of sectors of setup: don't change unless you also change
56 #define STRINGIFY(x) #x
65 long intel_long(long l
)
69 t
.b
[0] = l
& 0xff; l
>>= 8;
70 t
.b
[1] = l
& 0xff; l
>>= 8;
71 t
.b
[2] = l
& 0xff; l
>>= 8;
72 t
.b
[3] = l
& 0xff; l
>>= 8;
80 t
.b
[0] = i
& 0xff; i
>>= 8;
81 t
.b
[1] = i
& 0xff; i
>>= 8;
82 t
.b
[2] = i
& 0xff; i
>>= 8;
83 t
.b
[3] = i
& 0xff; i
>>= 8;
87 short intel_short(short l
)
91 t
.b
[0] = l
& 0xff; l
>>= 8;
92 t
.b
[1] = l
& 0xff; l
>>= 8;
96 void die(const char * str
)
98 fprintf(stderr
,"%s\n",str
);
104 die("Usage: build bootsect setup system [rootdev] [> image]");
107 int main(int argc
, char ** argv
)
109 int i
,c
,id
,sz
,tmp_int
;
110 unsigned long sys_size
, tmp_long
;
113 struct exec
*ex
= (struct exec
*)buf
;
115 char major_root
, minor_root
;
117 unsigned char setup_sectors
;
119 if ((argc
< 4) || (argc
> 5))
122 if (!strcmp(argv
[4], "CURRENT")) {
123 if (stat("/", &sb
)) {
125 die("Couldn't stat /");
127 major_root
= major(sb
.st_dev
);
128 minor_root
= minor(sb
.st_dev
);
129 } else if (strcmp(argv
[4], "FLOPPY")) {
130 if (stat(argv
[4], &sb
)) {
132 die("Couldn't stat root device.");
134 major_root
= major(sb
.st_rdev
);
135 minor_root
= minor(sb
.st_rdev
);
141 major_root
= DEFAULT_MAJOR_ROOT
;
142 minor_root
= DEFAULT_MINOR_ROOT
;
144 fprintf(stderr
, "Root device is (%d, %d)\n", major_root
, minor_root
);
145 for (i
=0;i
<sizeof buf
; i
++) buf
[i
]=0;
146 if ((id
=open(argv
[1],O_RDONLY
,0))<0)
147 die("Unable to open 'boot'");
148 if (read(id
,buf
,MINIX_HEADER
) != MINIX_HEADER
)
149 die("Unable to read header of 'boot'");
150 if (((long *) buf
)[0]!=intel_long(0x04100301))
151 die("Non-Minix header of 'boot'");
152 if (((long *) buf
)[1]!=intel_long(MINIX_HEADER
))
153 die("Non-Minix header of 'boot'");
154 if (((long *) buf
)[3] != 0)
155 die("Illegal data segment in 'boot'");
156 if (((long *) buf
)[4] != 0)
157 die("Illegal bss in 'boot'");
158 if (((long *) buf
)[5] != 0)
159 die("Non-Minix header of 'boot'");
160 if (((long *) buf
)[7] != 0)
161 die("Illegal symbol table in 'boot'");
162 i
=read(id
,buf
,sizeof buf
);
163 fprintf(stderr
,"Boot sector %d bytes.\n",i
);
165 die("Boot block must be exactly 512 bytes");
166 if ((*(unsigned short *)(buf
+510)) != (unsigned short)intel_short(0xAA55))
167 die("Boot block hasn't got boot flag (0xAA55)");
168 buf
[508] = (char) minor_root
;
169 buf
[509] = (char) major_root
;
172 die("Write call failed");
175 if ((id
=open(argv
[2],O_RDONLY
,0))<0)
176 die("Unable to open 'setup'");
177 if (read(id
,buf
,MINIX_HEADER
) != MINIX_HEADER
)
178 die("Unable to read header of 'setup'");
179 if (((long *) buf
)[0]!=intel_long(0x04100301))
180 die("Non-Minix header of 'setup'");
181 if (((long *) buf
)[1]!=intel_long(MINIX_HEADER
))
182 die("Non-Minix header of 'setup'");
183 if (((long *) buf
)[3] != 0)
184 die("Illegal data segment in 'setup'");
185 if (((long *) buf
)[4] != 0)
186 die("Illegal bss in 'setup'");
187 if (((long *) buf
)[5] != 0)
188 die("Non-Minix header of 'setup'");
189 if (((long *) buf
)[7] != 0)
190 die("Illegal symbol table in 'setup'");
191 for (i
=0 ; (c
=read(id
,buf
,sizeof buf
))>0 ; i
+=c
)
192 #ifdef __BIG_KERNEL__
195 /* Working with memcpy because of alignment constraints
196 on Sparc - Gertjan */
197 memcpy(&tmp_long
, &buf
[2], sizeof(long));
198 if (tmp_long
!= intel_long(0x53726448) )
199 die("Wrong magic in loader header of 'setup'");
200 memcpy(&tmp_int
, &buf
[6], sizeof(int));
201 if (tmp_int
< intel_int(0x200))
202 die("Wrong version of loader header of 'setup'");
203 buf
[0x11] = 1; /* LOADED_HIGH */
204 tmp_long
= intel_long(0x100000);
205 memcpy(&buf
[0x14], &tmp_long
, sizeof(long)); /* code32_start */
208 if (write(1,buf
,c
)!=c
)
209 die("Write call failed");
210 #ifdef __BIG_KERNEL__
214 die("read-error on 'setup'");
216 setup_sectors
= (unsigned char)((i
+ 511) / 512);
217 /* for compatibility with LILO */
218 if (setup_sectors
< SETUP_SECTS
)
219 setup_sectors
= SETUP_SECTS
;
220 fprintf(stderr
,"Setup is %d bytes.\n",i
);
221 for (c
=0 ; c
<sizeof(buf
) ; c
++)
223 while (i
< setup_sectors
* 512) {
224 c
= setup_sectors
* 512 - i
;
227 if (write(1,buf
,c
) != c
)
228 die("Write call failed");
232 if ((id
=open(argv
[3],O_RDONLY
,0))<0)
233 die("Unable to open 'system'");
235 if (read(id
,buf
,GCC_HEADER
) != GCC_HEADER
)
236 die("Unable to read header of 'system'");
237 if (N_MAGIC(*ex
) == ZMAGIC
) {
238 GCC_HEADER
= N_MAGIC_OFFSET
;
239 lseek(id
, GCC_HEADER
, SEEK_SET
);
240 } else if (N_MAGIC(*ex
) != QMAGIC
)
241 die("Non-GCC header of 'system'");
242 fprintf(stderr
,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
243 (ex
->a_text
+ex
->a_data
+ex
->a_bss
)/1024,
247 sz
= N_SYMOFF(*ex
) - GCC_HEADER
+ 4;
249 if (fstat (id
, &sb
)) {
251 die ("Unable to stat 'system'");
254 fprintf (stderr
, "System is %d kB\n", sz
/1024);
256 sys_size
= (sz
+ 15) / 16;
257 if (sys_size
> SYS_SIZE
)
258 die("System is too big");
265 if ((n
=read(id
, buf
, l
)) != l
) {
269 fprintf(stderr
, "Unexpected EOF\n");
270 die("Can't read 'system'");
272 if (write(1, buf
, l
) != l
)
277 if (lseek(1, 497, 0) == 497) {
278 if (write(1, &setup_sectors
, 1) != 1)
279 die("Write of setup sectors failed");
281 if (lseek(1,500,0) == 500) {
282 buf
[0] = (sys_size
& 0xff);
283 buf
[1] = ((sys_size
>> 8) & 0xff);
284 if (write(1, buf
, 2) != 2)