1 /*
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 *
7 * @(#)mkboot.c 8.1 (Berkeley) 07/15/93
8 */
9
10 #ifndef lint
11 static char copyright[] =
12 "@(#) Copyright (c) 1990, 1993\n\
13 The Regents of the University of California. All rights reserved.\n";
14 #endif /* not lint */
15
16 #ifndef lint
17 static char sccsid[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90";
18 #endif /* not lint */
19
20 #include <sys/param.h>
21 #include <sys/exec.h>
22 #include <sys/file.h>
23
24 #include <hp300/stand/volhdr.h>
25
26 #include <stdio.h>
27 #include <ctype.h>
28
29 #define LIF_NUMDIR 8
30
31 #define LIF_VOLSTART 0
32 #define LIF_VOLSIZE sizeof(struct lifvol)
33 #define LIF_DIRSTART 512
34 #define LIF_DIRSIZE (LIF_NUMDIR * sizeof(struct lifdir))
35 #define LIF_FILESTART 8192
36
37 #define btolifs(b) (((b) + (SECTSIZE - 1)) / SECTSIZE)
38 #define lifstob(s) ((s) * SECTSIZE)
39
40 int lpflag;
41 int loadpoint;
42 struct load ld;
43 struct lifvol lifv;
44 struct lifdir lifd[LIF_NUMDIR];
45 struct exec ex;
46 char buf[10240];
47
48 /*
49 * Old Format:
50 * sector 0: LIF volume header (40 bytes)
51 * sector 1: <unused>
52 * sector 2: LIF directory (8 x 32 == 256 bytes)
53 * sector 3-: LIF file 0, LIF file 1, etc.
54 * where sectors are 256 bytes.
55 *
56 * New Format:
57 * sector 0: LIF volume header (40 bytes)
58 * sector 1: <unused>
59 * sector 2: LIF directory (8 x 32 == 256 bytes)
60 * sector 3: <unused>
61 * sector 4-31: disklabel (~300 bytes right now)
62 * sector 32-: LIF file 0, LIF file 1, etc.
63 */
main(argc,argv)64 main(argc, argv)
65 char **argv;
66 {
67 int ac;
68 char **av;
69 int from1, from2, from3, to;
70 register int n;
71 char *n1, *n2, *n3, *lifname();
72
73 ac = --argc;
74 av = ++argv;
75 if (ac == 0)
76 usage();
77 if (!strcmp(av[0], "-l")) {
78 av++;
79 ac--;
80 if (ac == 0)
81 usage();
82 sscanf(av[0], "0x%x", &loadpoint);
83 lpflag++;
84 av++;
85 ac--;
86 }
87 if (ac == 0)
88 usage();
89 from1 = open(av[0], O_RDONLY, 0);
90 if (from1 < 0) {
91 perror("open");
92 exit(1);
93 }
94 n1 = av[0];
95 av++;
96 ac--;
97 if (ac == 0)
98 usage();
99 if (ac > 1) {
100 from2 = open(av[0], O_RDONLY, 0);
101 if (from2 < 0) {
102 perror("open");
103 exit(1);
104 }
105 n2 = av[0];
106 av++;
107 ac--;
108 if (ac > 1) {
109 from3 = open(av[0], O_RDONLY, 0);
110 if (from3 < 0) {
111 perror("open");
112 exit(1);
113 }
114 n3 = av[0];
115 av++;
116 ac--;
117 } else
118 from3 = -1;
119 } else
120 from2 = from3 = -1;
121 to = open(av[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
122 if (to < 0) {
123 perror("open");
124 exit(1);
125 }
126 /* clear possibly unused directory entries */
127 strncpy(lifd[1].dir_name, " ", 10);
128 lifd[1].dir_type = -1;
129 lifd[1].dir_addr = 0;
130 lifd[1].dir_length = 0;
131 lifd[1].dir_flag = 0xFF;
132 lifd[1].dir_exec = 0;
133 lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
134 /* record volume info */
135 lifv.vol_id = VOL_ID;
136 strncpy(lifv.vol_label, "BOOT43", 6);
137 lifv.vol_addr = btolifs(LIF_DIRSTART);
138 lifv.vol_oct = VOL_OCT;
139 lifv.vol_dirsize = btolifs(LIF_DIRSIZE);
140 lifv.vol_version = 1;
141 /* output bootfile one */
142 lseek(to, LIF_FILESTART, 0);
143 putfile(from1, to);
144 n = btolifs(ld.count + sizeof(ld));
145 strcpy(lifd[0].dir_name, lifname(n1));
146 lifd[0].dir_type = DIR_TYPE;
147 lifd[0].dir_addr = btolifs(LIF_FILESTART);
148 lifd[0].dir_length = n;
149 bcddate(from1, lifd[0].dir_toc);
150 lifd[0].dir_flag = DIR_FLAG;
151 lifd[0].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
152 lifv.vol_length = lifd[0].dir_addr + lifd[0].dir_length;
153 /* if there is an optional second boot program, output it */
154 if (from2 >= 0) {
155 lseek(to, LIF_FILESTART+lifstob(n), 0);
156 putfile(from2, to);
157 n = btolifs(ld.count + sizeof(ld));
158 strcpy(lifd[1].dir_name, lifname(n2));
159 lifd[1].dir_type = DIR_TYPE;
160 lifd[1].dir_addr = lifv.vol_length;
161 lifd[1].dir_length = n;
162 bcddate(from2, lifd[1].dir_toc);
163 lifd[1].dir_flag = DIR_FLAG;
164 lifd[1].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
165 lifv.vol_length = lifd[1].dir_addr + lifd[1].dir_length;
166 }
167 /* ditto for three */
168 if (from3 >= 0) {
169 lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n), 0);
170 putfile(from3, to);
171 n = btolifs(ld.count + sizeof(ld));
172 strcpy(lifd[2].dir_name, lifname(n3));
173 lifd[2].dir_type = DIR_TYPE;
174 lifd[2].dir_addr = lifv.vol_length;
175 lifd[2].dir_length = n;
176 bcddate(from3, lifd[2].dir_toc);
177 lifd[2].dir_flag = DIR_FLAG;
178 lifd[2].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
179 lifv.vol_length = lifd[2].dir_addr + lifd[2].dir_length;
180 }
181 /* output volume/directory header info */
182 lseek(to, LIF_VOLSTART, 0);
183 write(to, &lifv, LIF_VOLSIZE);
184 lseek(to, LIF_DIRSTART, 0);
185 write(to, lifd, LIF_DIRSIZE);
186 exit(0);
187 }
188
putfile(from,to)189 putfile(from, to)
190 {
191 register int n, tcnt, dcnt;
192
193 n = read(from, &ex, sizeof(ex));
194 if (n != sizeof(ex)) {
195 fprintf(stderr, "error reading file header\n");
196 exit(1);
197 }
198 if (ex.a_magic == OMAGIC) {
199 tcnt = ex.a_text;
200 dcnt = ex.a_data;
201 }
202 else if (ex.a_magic == NMAGIC) {
203 tcnt = (ex.a_text + PGOFSET) & ~PGOFSET;
204 dcnt = ex.a_data;
205 }
206 else {
207 fprintf(stderr, "bad magic number\n");
208 exit(1);
209 }
210 ld.address = lpflag ? loadpoint : ex.a_entry;
211 ld.count = tcnt + dcnt;
212 write(to, &ld, sizeof(ld));
213 while (tcnt) {
214 n = sizeof(buf);
215 if (n > tcnt)
216 n = tcnt;
217 n = read(from, buf, n);
218 if (n < 0) {
219 perror("read");
220 exit(1);
221 }
222 if (n == 0) {
223 fprintf(stderr, "short read\n");
224 exit(1);
225 }
226 if (write(to, buf, n) < 0) {
227 perror("write");
228 exit(1);
229 }
230 tcnt -= n;
231 }
232 while (dcnt) {
233 n = sizeof(buf);
234 if (n > dcnt)
235 n = dcnt;
236 n = read(from, buf, n);
237 if (n < 0) {
238 perror("read");
239 exit(1);
240 }
241 if (n == 0) {
242 fprintf(stderr, "short read\n");
243 exit(1);
244 }
245 if (write(to, buf, n) < 0) {
246 perror("write");
247 exit(1);
248 }
249 dcnt -= n;
250 }
251 }
252
usage()253 usage()
254 {
255 fprintf(stderr,
256 "usage: mkboot [-l loadpoint] prog1 [ prog2 ] outfile\n");
257 exit(1);
258 }
259
260 char *
lifname(str)261 lifname(str)
262 char *str;
263 {
264 static char lname[10] = "SYS_XXXXX";
265 register int i;
266
267 for (i = 4; i < 9; i++) {
268 if (islower(*str))
269 lname[i] = toupper(*str);
270 else if (isalnum(*str) || *str == '_')
271 lname[i] = *str;
272 else
273 break;
274 str++;
275 }
276 for ( ; i < 10; i++)
277 lname[i] = '\0';
278 return(lname);
279 }
280
281 #include <sys/stat.h>
282 #include <time.h> /* XXX */
283
bcddate(fd,toc)284 bcddate(fd, toc)
285 int fd;
286 char *toc;
287 {
288 struct stat statb;
289 struct tm *tm;
290
291 fstat(fd, &statb);
292 tm = localtime(&statb.st_ctime);
293 *toc = ((tm->tm_mon+1) / 10) << 4;
294 *toc++ |= (tm->tm_mon+1) % 10;
295 *toc = (tm->tm_mday / 10) << 4;
296 *toc++ |= tm->tm_mday % 10;
297 *toc = (tm->tm_year / 10) << 4;
298 *toc++ |= tm->tm_year % 10;
299 *toc = (tm->tm_hour / 10) << 4;
300 *toc++ |= tm->tm_hour % 10;
301 *toc = (tm->tm_min / 10) << 4;
302 *toc++ |= tm->tm_min % 10;
303 *toc = (tm->tm_sec / 10) << 4;
304 *toc |= tm->tm_sec % 10;
305 }
306