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