1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*
* Copyright (c) 1998 Robert Nordier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btx.h"
#include "elfh.h"
#include "endian.h"
#include "i386_a.out.h"
#define BTX_PATH "/sys/boot/i386/btx"
#define I_LDR 0 /* BTX loader */
#define I_BTX 1 /* BTX kernel */
#define I_CLNT 2 /* Client program */
#define F_BIN 0 /* Binary */
#define F_AOUT 1 /* ZMAGIC a.out */
#define F_ELF 2 /* 32-bit ELF */
#define F_CNT 3 /* Number of formats */
#define IMPURE 1 /* Writable text */
#define MAXU32 0xffffffff /* Maximum unsigned 32-bit quantity */
#define align(x, y) (((x) + (y) - 1) & ~((y) - 1))
struct hdr {
uint32_t fmt; /* Format */
uint32_t flags; /* Bit flags */
uint32_t size; /* Size of file */
uint32_t text; /* Size of text segment */
uint32_t data; /* Size of data segment */
uint32_t bss; /* Size of bss segment */
uint32_t org; /* Program origin */
uint32_t entry; /* Program entry point */
};
static const char *const fmtlist[] = {"bin", "aout", "elf"};
static const char binfo[] =
"kernel: ver=%u.%02u size=%x load=%x entry=%x map=%uM "
"pgctl=%x:%x\n";
static const char cinfo[] =
"client: fmt=%s size=%x text=%x data=%x bss=%x entry=%x\n";
static const char oinfo[] =
"output: fmt=%s size=%x text=%x data=%x org=%x entry=%x\n";
static const char *lname =
BTX_PATH "/btxldr/btxldr"; /* BTX loader */
static const char *bname =
BTX_PATH "/btx/btx"; /* BTX kernel */
static const char *oname =
"a.out"; /* Output filename */
static int ppage = -1; /* First page present */
static int wpage = -1; /* First page writable */
static unsigned int format; /* Output format */
static uint32_t centry; /* Client entry address */
static uint32_t lentry; /* Loader entry address */
static int Eflag; /* Client entry option */
static int quiet; /* Inhibit warnings */
static int verbose; /* Display information */
static const char *tname; /* Temporary output file */
static const char *fname; /* Current input file */
static void cleanup(void);
static void btxld(const char *);
static void getbtx(int, struct btx_hdr *);
static void gethdr(int, struct hdr *);
static void puthdr(int, struct hdr *);
static void copy(int, int, size_t, off_t);
static size_t readx(int, void *, size_t, off_t);
static void writex(int, const void *, size_t);
static void seekx(int, off_t);
static unsigned int optfmt(const char *);
static uint32_t optaddr(const char *);
static int optpage(const char *, int);
static void Warn(const char *, const char *, ...);
static void usage(void);
/*
* A link editor for BTX clients.
*/
int
main(int argc, char *argv[])
{
int c;
while ((c = getopt(argc, argv, "qvb:E:e:f:l:o:P:W:")) != -1)
switch (c) {
case 'q':
quiet = 1;
break;
case 'v':
verbose = 1;
break;
case 'b':
bname = optarg;
break;
case 'E':
centry = optaddr(optarg);
Eflag = 1;
break;
case 'e':
lentry = optaddr(optarg);
break;
case 'f':
format = optfmt(optarg);
break;
case 'l':
lname = optarg;
break;
case 'o':
oname = optarg;
break;
case 'P':
ppage = optpage(optarg, 1);
break;
case 'W':
wpage = optpage(optarg, BTX_MAXCWR);
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
atexit(cleanup);
btxld(*argv);
return 0;
}
/*
* Clean up after errors.
*/
static void
cleanup(void)
{
if (tname)
remove(tname);
}
/*
* Read the input files; write the output file; display information.
*/
static void
btxld(const char *iname)
{
char name[FILENAME_MAX];
struct btx_hdr btx, btxle;
struct hdr ihdr, ohdr;
unsigned int ldr_size, cwr;
int fdi[3], fdo, i;
ldr_size = 0;
for (i = I_LDR; i <= I_CLNT; i++) {
fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
if ((fdi[i] = open(fname, O_RDONLY)) == -1)
err(2, "%s", fname);
switch (i) {
case I_LDR:
gethdr(fdi[i], &ihdr);
if (ihdr.fmt != F_BIN)
Warn(fname, "Loader format is %s; processing as %s",
fmtlist[ihdr.fmt], fmtlist[F_BIN]);
ldr_size = ihdr.size;
break;
case I_BTX:
getbtx(fdi[i], &btx);
break;
case I_CLNT:
gethdr(fdi[i], &ihdr);
if (ihdr.org && ihdr.org != BTX_PGSIZE)
Warn(fname,
"Client origin is 0x%x; expecting 0 or 0x%x",
ihdr.org, BTX_PGSIZE);
}
}
memset(&ohdr, 0, sizeof(ohdr));
ohdr.fmt = format;
ohdr.text = ldr_size;
ohdr.data = btx.btx_textsz + ihdr.size;
ohdr.org = lentry;
ohdr.entry = lentry;
cwr = 0;
if (wpage > 0 || (wpage == -1 && !(ihdr.flags & IMPURE))) {
if (wpage > 0)
cwr = wpage;
else {
cwr = howmany(ihdr.text, BTX_PGSIZE);
if (cwr > BTX_MAXCWR)
cwr = BTX_MAXCWR;
}
}
if (ppage > 0 || (ppage && wpage && ihdr.org >= BTX_PGSIZE)) {
btx.btx_flags |= BTX_MAPONE;
if (!cwr)
cwr++;
}
btx.btx_pgctl -= cwr;
btx.btx_entry = Eflag ? centry : ihdr.entry;
if (snprintf(name, sizeof(name), "%s.tmp", oname) >= sizeof(name))
errx(2, "%s: Filename too long", oname);
if ((fdo = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1)
err(2, "%s", name);
if (!(tname = strdup(name)))
err(2, NULL);
puthdr(fdo, &ohdr);
for (i = I_LDR; i <= I_CLNT; i++) {
fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
switch (i) {
case I_LDR:
copy(fdi[i], fdo, ldr_size, 0);
seekx(fdo, ohdr.size += ohdr.text);
break;
case I_BTX:
btxle = btx;
btxle.btx_pgctl = HTOLE16(btxle.btx_pgctl);
btxle.btx_textsz = HTOLE16(btxle.btx_textsz);
btxle.btx_entry = HTOLE32(btxle.btx_entry);
writex(fdo, &btxle, sizeof(btxle));
copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx),
sizeof(btx));
break;
case I_CLNT:
copy(fdi[i], fdo, ihdr.size, 0);
if (ftruncate(fdo, ohdr.size += ohdr.data))
err(2, "%s", tname);
}
if (close(fdi[i]))
err(2, "%s", fname);
}
if (close(fdo))
err(2, "%s", tname);
if (rename(tname, oname))
err(2, "%s: Can't rename to %s", tname, oname);
tname = NULL;
if (verbose) {
printf(binfo, btx.btx_majver, btx.btx_minver, btx.btx_textsz,
BTX_ORIGIN(btx), BTX_ENTRY(btx), BTX_MAPPED(btx) *
BTX_PGSIZE / 0x100000, !!(btx.btx_flags & BTX_MAPONE),
BTX_MAPPED(btx) - btx.btx_pgctl - BTX_PGBASE /
BTX_PGSIZE - BTX_MAPPED(btx) * 4 / BTX_PGSIZE);
printf(cinfo, fmtlist[ihdr.fmt], ihdr.size, ihdr.text,
ihdr.data, ihdr.bss, ihdr.entry);
printf(oinfo, fmtlist[ohdr.fmt], ohdr.size, ohdr.text,
ohdr.data, ohdr.org, ohdr.entry);
}
}
/*
* Read BTX file header.
*/
static void
getbtx(int fd, struct btx_hdr * btx)
{
if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) ||
btx->btx_magic[0] != BTX_MAG0 ||
btx->btx_magic[1] != BTX_MAG1 ||
btx->btx_magic[2] != BTX_MAG2)
errx(1, "%s: Not a BTX kernel", fname);
btx->btx_pgctl = LE16TOH(btx->btx_pgctl);
btx->btx_textsz = LE16TOH(btx->btx_textsz);
btx->btx_entry = LE32TOH(btx->btx_entry);
}
/*
* Get file size and read a.out or ELF header.
*/
static void
gethdr(int fd, struct hdr *hdr)
{
struct stat sb;
const struct i386_exec *ex;
const Elf32_Ehdr *ee;
const Elf32_Phdr *ep;
void *p;
unsigned int fmt, x, n, i;
memset(hdr, 0, sizeof(*hdr));
if (fstat(fd, &sb))
err(2, "%s", fname);
if (sb.st_size > MAXU32)
errx(1, "%s: Too big", fname);
hdr->size = sb.st_size;
if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd,
0)) == MAP_FAILED)
err(2, "%s", fname);
for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--)
switch (fmt) {
case F_AOUT:
ex = p;
if (hdr->size >= sizeof(struct i386_exec) && !I386_N_BADMAG(*ex)) {
hdr->fmt = fmt;
x = I386_N_GETMAGIC(*ex);
if (x == OMAGIC || x == NMAGIC) {
if (x == NMAGIC)
Warn(fname, "Treating %s NMAGIC as OMAGIC",
fmtlist[fmt]);
hdr->flags |= IMPURE;
}
hdr->text = LE32TOH(ex->a_text);
hdr->data = LE32TOH(ex->a_data);
hdr->bss = LE32TOH(ex->a_bss);
hdr->entry = LE32TOH(ex->a_entry);
if (LE32TOH(ex->a_entry) >= BTX_PGSIZE)
hdr->org = BTX_PGSIZE;
}
break;
case F_ELF:
ee = p;
if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) {
hdr->fmt = fmt;
for (n = i = 0; i < LE16TOH(ee->e_phnum); i++) {
ep = (void *)((uint8_t *)p + LE32TOH(ee->e_phoff) +
LE16TOH(ee->e_phentsize) * i);
if (LE32TOH(ep->p_type) == PT_LOAD)
switch (n++) {
case 0:
hdr->text = LE32TOH(ep->p_filesz);
hdr->org = LE32TOH(ep->p_paddr);
if (LE32TOH(ep->p_flags) & PF_W)
hdr->flags |= IMPURE;
break;
case 1:
hdr->data = LE32TOH(ep->p_filesz);
hdr->bss = LE32TOH(ep->p_memsz) -
LE32TOH(ep->p_filesz);
break;
case 2:
Warn(fname,
"Ignoring extra %s PT_LOAD segments",
fmtlist[fmt]);
}
}
hdr->entry = LE32TOH(ee->e_entry);
}
}
if (munmap(p, hdr->size))
err(2, "%s", fname);
}
/*
* Write a.out or ELF header.
*/
static void
puthdr(int fd, struct hdr *hdr)
{
struct i386_exec ex;
struct elfh eh;
switch (hdr->fmt) {
case F_AOUT:
memset(&ex, 0, sizeof(ex));
I386_N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0);
hdr->text = I386_N_ALIGN(ex, hdr->text);
ex.a_text = HTOLE32(hdr->text);
hdr->data = I386_N_ALIGN(ex, hdr->data);
ex.a_data = HTOLE32(hdr->data);
ex.a_entry = HTOLE32(hdr->entry);
writex(fd, &ex, sizeof(ex));
hdr->size = I386_N_ALIGN(ex, sizeof(ex));
seekx(fd, hdr->size);
break;
case F_ELF:
eh = elfhdr;
eh.e.e_entry = HTOLE32(hdr->entry);
eh.p[0].p_vaddr = eh.p[0].p_paddr = HTOLE32(hdr->org);
eh.p[0].p_filesz = eh.p[0].p_memsz = HTOLE32(hdr->text);
eh.p[1].p_offset = HTOLE32(LE32TOH(eh.p[0].p_offset) +
LE32TOH(eh.p[0].p_filesz));
eh.p[1].p_vaddr = eh.p[1].p_paddr =
HTOLE32(align(LE32TOH(eh.p[0].p_paddr) + LE32TOH(eh.p[0].p_memsz),
4));
eh.p[1].p_filesz = eh.p[1].p_memsz = HTOLE32(hdr->data);
eh.sh[2].sh_addr = eh.p[0].p_vaddr;
eh.sh[2].sh_offset = eh.p[0].p_offset;
eh.sh[2].sh_size = eh.p[0].p_filesz;
eh.sh[3].sh_addr = eh.p[1].p_vaddr;
eh.sh[3].sh_offset = eh.p[1].p_offset;
eh.sh[3].sh_size = eh.p[1].p_filesz;
writex(fd, &eh, sizeof(eh));
hdr->size = sizeof(eh);
}
}
/*
* Safe copy from input file to output file.
*/
static void
copy(int fdi, int fdo, size_t nbyte, off_t offset)
{
char buf[8192];
size_t n;
while (nbyte) {
if ((n = sizeof(buf)) > nbyte)
n = nbyte;
if (readx(fdi, buf, n, offset) != n)
errx(2, "%s: Short read", fname);
writex(fdo, buf, n);
nbyte -= n;
offset = -1;
}
}
/*
* Safe read from input file.
*/
static size_t
readx(int fd, void *buf, size_t nbyte, off_t offset)
{
ssize_t n;
if (offset != -1 && lseek(fd, offset, SEEK_SET) != offset)
err(2, "%s", fname);
if ((n = read(fd, buf, nbyte)) == -1)
err(2, "%s", fname);
return n;
}
/*
* Safe write to output file.
*/
static void
writex(int fd, const void *buf, size_t nbyte)
{
ssize_t n;
if ((n = write(fd, buf, nbyte)) == -1)
err(2, "%s", tname);
if (n != nbyte)
errx(2, "%s: Short write", tname);
}
/*
* Safe seek in output file.
*/
static void
seekx(int fd, off_t offset)
{
if (lseek(fd, offset, SEEK_SET) != offset)
err(2, "%s", tname);
}
/*
* Convert an option argument to a format code.
*/
static unsigned int
optfmt(const char *arg)
{
unsigned int i;
for (i = 0; i < F_CNT && strcmp(arg, fmtlist[i]); i++);
if (i == F_CNT)
errx(1, "%s: Unknown format", arg);
return i;
}
/*
* Convert an option argument to an address.
*/
static uint32_t
optaddr(const char *arg)
{
char *s;
unsigned long x;
errno = 0;
x = strtoul(arg, &s, 0);
if (errno || !*arg || *s || x > MAXU32)
errx(1, "%s: Illegal address", arg);
return x;
}
/*
* Convert an option argument to a page number.
*/
static int
optpage(const char *arg, int hi)
{
char *s;
long x;
errno = 0;
x = strtol(arg, &s, 0);
if (errno || !*arg || *s || x < 0 || x > hi)
errx(1, "%s: Illegal page number", arg);
return x;
}
/*
* Display a warning.
*/
static void
Warn(const char *locus, const char *fmt, ...)
{
va_list ap;
char *s;
if (!quiet) {
asprintf(&s, "%s: Warning: %s", locus, fmt);
va_start(ap, fmt);
vwarnx(s, ap);
va_end(ap);
free(s);
}
}
/*
* Display usage information.
*/
static void
usage(void)
{
fprintf(stderr, "%s\n%s\n",
"usage: btxld [-qv] [-b file] [-E address] [-e address] [-f format]",
" [-l file] [-o filename] [-P page] [-W page] file");
exit(1);
}
|