28 #include <sys/types.h>
30 #include <sys/sysmacros.h>
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
55 #define STRINGIFY(x) #x
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;
90 t.
b[0] = l & 0xff; l >>= 8;
91 t.
b[1] = l & 0xff; l >>= 8;
103 die(
"Usage: build bootsect setup system [rootdev] [> image]");
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 /");
128 }
else if (
strcmp(argv[4],
"FLOPPY")) {
129 if (
stat(argv[4], &sb)) {
131 die(
"Couldn't stat root device.");
143 fprintf(stderr,
"Root device is (%d, %d)\n", major_root, minor_root);
144 for (i=0;i<
sizeof buf; i++) buf[i]=0;
146 die(
"Unable to open 'boot'");
148 die(
"Unable to read header of 'boot'");
149 if (((
long *) buf)[0]!=
intel_long(0x04100301))
150 die(
"Non-Minix header of 'boot'");
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");
175 die(
"Unable to open 'setup'");
177 die(
"Unable to read header of 'setup'");
178 if (((
long *) buf)[0]!=
intel_long(0x04100301))
179 die(
"Non-Minix header of 'setup'");
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__
196 memcpy(&tmp_long, &buf[2],
sizeof(
long));
198 die(
"Wrong magic in loader header of 'setup'");
199 memcpy(&tmp_int, &buf[6],
sizeof(
int));
201 die(
"Wrong version of loader header of 'setup'");
204 memcpy(&buf[0x14], &tmp_long,
sizeof(
long));
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);
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;
227 die(
"Write call failed");
232 die(
"Unable to open 'system'");
234 if (
read(
id,buf,GCC_HEADER) != GCC_HEADER)
235 die(
"Unable to read header of 'system'");
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",
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;
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)