This source file includes following definitions.
- intel_long
- intel_short
- die
- usage
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <linux/a.out.h>
32 #include <linux/config.h>
33
34 #define MINIX_HEADER 32
35
36 #define N_MAGIC_OFFSET 1024
37 static int GCC_HEADER = sizeof(struct exec);
38
39 #define SYS_SIZE DEF_SYSSIZE
40
41 #define DEFAULT_MAJOR_ROOT 0
42 #define DEFAULT_MINOR_ROOT 0
43
44
45
46 #define SETUP_SECTS 4
47
48 #define STRINGIFY(x) #x
49
50 typedef union {
51 long l;
52 short s[2];
53 char b[4];
54 } conv;
55
56 long intel_long(long l)
57 {
58 conv t;
59
60 t.b[0] = l & 0xff; l >>= 8;
61 t.b[1] = l & 0xff; l >>= 8;
62 t.b[2] = l & 0xff; l >>= 8;
63 t.b[3] = l & 0xff; l >>= 8;
64 return t.l;
65 }
66
67 short intel_short(short l)
68 {
69 conv t;
70
71 t.b[0] = l & 0xff; l >>= 8;
72 t.b[1] = l & 0xff; l >>= 8;
73 return t.s[0];
74 }
75
76 void die(char * str)
77 {
78 fprintf(stderr,"%s\n",str);
79 exit(1);
80 }
81
82 void usage(void)
83 {
84 die("Usage: build bootsect setup system [rootdev] [> image]");
85 }
86
87 int main(int argc, char ** argv)
88 {
89 int i,c,id, sz;
90 unsigned long sys_size;
91 char buf[1024];
92 struct exec *ex = (struct exec *)buf;
93 char major_root, minor_root;
94 struct stat sb;
95 unsigned char setup_sectors;
96
97 if ((argc < 4) || (argc > 5))
98 usage();
99 if (argc > 4) {
100 if (!strcmp(argv[4], "CURRENT")) {
101 if (stat("/", &sb)) {
102 perror("/");
103 die("Couldn't stat /");
104 }
105 major_root = major(sb.st_dev);
106 minor_root = minor(sb.st_dev);
107 } else if (strcmp(argv[4], "FLOPPY")) {
108 if (stat(argv[4], &sb)) {
109 perror(argv[4]);
110 die("Couldn't stat root device.");
111 }
112 major_root = major(sb.st_rdev);
113 minor_root = minor(sb.st_rdev);
114 } else {
115 major_root = 0;
116 minor_root = 0;
117 }
118 } else {
119 major_root = DEFAULT_MAJOR_ROOT;
120 minor_root = DEFAULT_MINOR_ROOT;
121 }
122 fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
123 for (i=0;i<sizeof buf; i++) buf[i]=0;
124 if ((id=open(argv[1],O_RDONLY,0))<0)
125 die("Unable to open 'boot'");
126 if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
127 die("Unable to read header of 'boot'");
128 if (((long *) buf)[0]!=intel_long(0x04100301))
129 die("Non-Minix header of 'boot'");
130 if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
131 die("Non-Minix header of 'boot'");
132 if (((long *) buf)[3] != 0)
133 die("Illegal data segment in 'boot'");
134 if (((long *) buf)[4] != 0)
135 die("Illegal bss in 'boot'");
136 if (((long *) buf)[5] != 0)
137 die("Non-Minix header of 'boot'");
138 if (((long *) buf)[7] != 0)
139 die("Illegal symbol table in 'boot'");
140 i=read(id,buf,sizeof buf);
141 fprintf(stderr,"Boot sector %d bytes.\n",i);
142 if (i != 512)
143 die("Boot block must be exactly 512 bytes");
144 if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
145 die("Boot block hasn't got boot flag (0xAA55)");
146 buf[508] = (char) minor_root;
147 buf[509] = (char) major_root;
148 i=write(1,buf,512);
149 if (i!=512)
150 die("Write call failed");
151 close (id);
152
153 if ((id=open(argv[2],O_RDONLY,0))<0)
154 die("Unable to open 'setup'");
155 if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
156 die("Unable to read header of 'setup'");
157 if (((long *) buf)[0]!=intel_long(0x04100301))
158 die("Non-Minix header of 'setup'");
159 if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
160 die("Non-Minix header of 'setup'");
161 if (((long *) buf)[3] != 0)
162 die("Illegal data segment in 'setup'");
163 if (((long *) buf)[4] != 0)
164 die("Illegal bss in 'setup'");
165 if (((long *) buf)[5] != 0)
166 die("Non-Minix header of 'setup'");
167 if (((long *) buf)[7] != 0)
168 die("Illegal symbol table in 'setup'");
169 for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
170 if (write(1,buf,c)!=c)
171 die("Write call failed");
172 if (c != 0)
173 die("read-error on 'setup'");
174 close (id);
175 setup_sectors = (unsigned char)((i + 511) / 512);
176
177 if (setup_sectors < SETUP_SECTS)
178 setup_sectors = SETUP_SECTS;
179 fprintf(stderr,"Setup is %d bytes.\n",i);
180 for (c=0 ; c<sizeof(buf) ; c++)
181 buf[c] = '\0';
182 while (i < setup_sectors * 512) {
183 c = setup_sectors * 512 - i;
184 if (c > sizeof(buf))
185 c = sizeof(buf);
186 if (write(1,buf,c) != c)
187 die("Write call failed");
188 i += c;
189 }
190
191 if ((id=open(argv[3],O_RDONLY,0))<0)
192 die("Unable to open 'system'");
193 if (read(id,buf,GCC_HEADER) != GCC_HEADER)
194 die("Unable to read header of 'system'");
195 if (N_MAGIC(*ex) == ZMAGIC) {
196 GCC_HEADER = N_MAGIC_OFFSET;
197 lseek(id, GCC_HEADER, SEEK_SET);
198 } else if (N_MAGIC(*ex) != QMAGIC)
199 die("Non-GCC header of 'system'");
200 fprintf(stderr,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
201 (ex->a_text+ex->a_data+ex->a_bss)/1024,
202 ex->a_text /1024,
203 ex->a_data /1024,
204 ex->a_bss /1024);
205 sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
206 sys_size = (sz + 15) / 16;
207 if (sys_size > SYS_SIZE)
208 die("System is too big");
209 while (sz > 0) {
210 int l, n;
211
212 l = sz;
213 if (l > sizeof(buf))
214 l = sizeof(buf);
215 if ((n=read(id, buf, l)) != l) {
216 if (n == -1)
217 perror(argv[1]);
218 else
219 fprintf(stderr, "Unexpected EOF\n");
220 die("Can't read 'system'");
221 }
222 if (write(1, buf, l) != l)
223 die("Write failed");
224 sz -= l;
225 }
226 close(id);
227 if (lseek(1, 497, 0) == 497) {
228 if (write(1, &setup_sectors, 1) != 1)
229 die("Write of setup sectors failed");
230 }
231 if (lseek(1,500,0) == 500) {
232 buf[0] = (sys_size & 0xff);
233 buf[1] = ((sys_size >> 8) & 0xff);
234 if (write(1, buf, 2) != 2)
235 die("Write failed");
236 }
237 return(0);
238 }