4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * This file builds a disk-image from three different files:
10 * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
11 * - setup: 8086 machine code, sets up system parm
12 * - system: 80386 code for actual system
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.
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
25 #include <stdio.h> /* fprintf */
27 #include <stdlib.h> /* contains exit */
28 #include <sys/types.h> /* unistd.h needs this */
30 #include <sys/sysmacros.h>
31 #include <unistd.h> /* contains read/write */
35 #define MINIX_HEADER 32
37 #define N_MAGIC_OFFSET 1024
39 static int GCC_HEADER
= sizeof(struct exec
);
43 #define SYS_SIZE 0xffff
45 #define SYS_SIZE DEF_SYSSIZE
48 #define DEFAULT_MAJOR_ROOT 0
49 #define DEFAULT_MINOR_ROOT 0
51 /* max nr of sectors of setup: don't change unless you also change
55 #define STRINGIFY(x) #x
64 long intel_long(long l
)
68 t
.b
[0] = l
& 0xff; l
>>= 8;
69 t
.b
[1] = l
& 0xff; l
>>= 8;
70 t
.b
[2] = l
& 0xff; l
>>= 8;
71 t
.b
[3] = l
& 0xff; l
>>= 8;
79 t
.b
[0] = i
& 0xff; i
>>= 8;
80 t
.b
[1] = i
& 0xff; i
>>= 8;
81 t
.b
[2] = i
& 0xff; i
>>= 8;
82 t
.b
[3] = i
& 0xff; i
>>= 8;
86 short intel_short(short l
)
90 t
.b
[0] = l
& 0xff; l
>>= 8;
91 t
.b
[1] = l
& 0xff; l
>>= 8;
95 void die(const char * str
)
97 fprintf(stderr
,"%s\n",str
);
103 die("Usage: build bootsect setup system [rootdev] [> image]");
106 int main(int argc
, char ** argv
)
108 int i
,c
,id
,sz
,tmp_int
;
109 unsigned long sys_size
, tmp_long
;
112 struct exec
*ex
= (struct exec
*)buf
;
114 char major_root
, minor_root
;
116 unsigned char setup_sectors
;
118 if ((argc
< 4) || (argc
> 5))
121 if (!strcmp(argv
[4], "CURRENT")) {
122 if (stat("/", &sb
)) {
124 die("Couldn't stat /");
126 major_root
= major(sb
.st_dev
);
127 minor_root
= minor(sb
.st_dev
);
128 } else if (strcmp(argv
[4], "FLOPPY")) {
129 if (stat(argv
[4], &sb
)) {
131 die("Couldn't stat root device.");
133 major_root
= major(sb
.st_rdev
);
134 minor_root
= minor(sb
.st_rdev
);
140 major_root
= DEFAULT_MAJOR_ROOT
;
141 minor_root
= DEFAULT_MINOR_ROOT
;
143 fprintf(stderr
, "Root device is (%d, %d)\n", major_root
, minor_root
);
144 for (i
=0;i
<sizeof buf
; i
++) buf
[i
]=0;
145 if ((id
=open(argv
[1],O_RDONLY
,0))<0)
146 die("Unable to open 'boot'");
147 if (read(id
,buf
,MINIX_HEADER
) != MINIX_HEADER
)
148 die("Unable to read header of 'boot'");
149 if (((long *) buf
)[0]!=intel_long(0x04100301))
150 die("Non-Minix header of 'boot'");
151 if (((long *) buf
)[1]!=intel_long(MINIX_HEADER
))
152 die("Non-Minix header of 'boot'");
153 if (((long *) buf
)[3] != 0)
154 die("Illegal data segment in 'boot'");
155 if (((long *) buf
)[4] != 0)
156 die("Illegal bss in 'boot'");
157 if (((long *) buf
)[5] != 0)
158 die("Non-Minix header of 'boot'");
159 if (((long *) buf
)[7] != 0)
160 die("Illegal symbol table in 'boot'");
161 i
=read(id
,buf
,sizeof buf
);
162 fprintf(stderr
,"Boot sector %d bytes.\n",i
);
164 die("Boot block must be exactly 512 bytes");
165 if ((*(unsigned short *)(buf
+510)) != (unsigned short)intel_short(0xAA55))
166 die("Boot block hasn't got boot flag (0xAA55)");
167 buf
[508] = (char) minor_root
;
168 buf
[509] = (char) major_root
;
171 die("Write call failed");
174 if ((id
=open(argv
[2],O_RDONLY
,0))<0)
175 die("Unable to open 'setup'");
176 if (read(id
,buf
,MINIX_HEADER
) != MINIX_HEADER
)
177 die("Unable to read header of 'setup'");
178 if (((long *) buf
)[0]!=intel_long(0x04100301))
179 die("Non-Minix header of 'setup'");
180 if (((long *) buf
)[1]!=intel_long(MINIX_HEADER
))
181 die("Non-Minix header of 'setup'");
182 if (((long *) buf
)[3] != 0)
183 die("Illegal data segment in 'setup'");
184 if (((long *) buf
)[4] != 0)
185 die("Illegal bss in 'setup'");
186 if (((long *) buf
)[5] != 0)
187 die("Non-Minix header of 'setup'");
188 if (((long *) buf
)[7] != 0)
189 die("Illegal symbol table in 'setup'");
190 for (i
=0 ; (c
=read(id
,buf
,sizeof buf
))>0 ; i
+=c
)
191 #ifdef __BIG_KERNEL__
194 /* Working with memcpy because of alignment constraints
195 on Sparc - Gertjan */
196 memcpy(&tmp_long
, &buf
[2], sizeof(long));
197 if (tmp_long
!= intel_long(0x53726448) )
198 die("Wrong magic in loader header of 'setup'");
199 memcpy(&tmp_int
, &buf
[6], sizeof(int));
200 if (tmp_int
< intel_int(0x200))
201 die("Wrong version of loader header of 'setup'");
202 buf
[0x11] = 1; /* LOADED_HIGH */
203 tmp_long
= intel_long(0x100000);
204 memcpy(&buf
[0x14], &tmp_long
, sizeof(long)); /* code32_start */
207 if (write(1,buf
,c
)!=c
)
208 die("Write call failed");
209 #ifdef __BIG_KERNEL__
213 die("read-error on 'setup'");
215 setup_sectors
= (unsigned char)((i
+ 511) / 512);
216 /* for compatibility with LILO */
217 if (setup_sectors
< SETUP_SECTS
)
218 setup_sectors
= SETUP_SECTS
;
219 fprintf(stderr
,"Setup is %d bytes.\n",i
);
220 for (c
=0 ; c
<sizeof(buf
) ; c
++)
222 while (i
< setup_sectors
* 512) {
223 c
= setup_sectors
* 512 - i
;
226 if (write(1,buf
,c
) != c
)
227 die("Write call failed");
231 if ((id
=open(argv
[3],O_RDONLY
,0))<0)
232 die("Unable to open 'system'");
234 if (read(id
,buf
,GCC_HEADER
) != GCC_HEADER
)
235 die("Unable to read header of 'system'");
236 if (N_MAGIC(*ex
) == ZMAGIC
) {
237 GCC_HEADER
= N_MAGIC_OFFSET
;
238 lseek(id
, GCC_HEADER
, SEEK_SET
);
239 } else if (N_MAGIC(*ex
) != QMAGIC
)
240 die("Non-GCC header of 'system'");
241 fprintf(stderr
,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
242 (ex
->a_text
+ex
->a_data
+ex
->a_bss
)/1024,
246 sz
= N_SYMOFF(*ex
) - GCC_HEADER
+ 4;
248 if (fstat (id
, &sb
)) {
250 die ("Unable to stat 'system'");
253 fprintf (stderr
, "System is %d kB\n", sz
/1024);
255 sys_size
= (sz
+ 15) / 16;
256 if (sys_size
> SYS_SIZE
)
257 die("System is too big");
264 if ((n
=read(id
, buf
, l
)) != l
) {
268 fprintf(stderr
, "Unexpected EOF\n");
269 die("Can't read 'system'");
271 if (write(1, buf
, l
) != l
)
276 if (lseek(1, 497, 0) == 497) {
277 if (write(1, &setup_sectors
, 1) != 1)
278 die("Write of setup sectors failed");
280 if (lseek(1,500,0) == 500) {
281 buf
[0] = (sys_size
& 0xff);
282 buf
[1] = ((sys_size
>> 8) & 0xff);
283 if (write(1, buf
, 2) != 2)