2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 static const char rcsid[] =
29 "$FreeBSD: src/usr.sbin/btxld/btxld.c,v 1.4 2000/01/04 14:10:36 marcel Exp $";
32 #include <sys/types.h>
50 #define BTX_PATH "/sys/boot/i386/btx"
52 #define I_LDR 0 /* BTX loader */
53 #define I_BTX 1 /* BTX kernel */
54 #define I_CLNT 2 /* Client program */
56 #define F_BIN 0 /* Binary */
57 #define F_AOUT 1 /* ZMAGIC a.out */
58 #define F_ELF 2 /* 32-bit ELF */
59 #define F_CNT 3 /* Number of formats */
61 #define IMPURE 1 /* Writable text */
62 #define MAXU32 0xffffffff /* Maximum unsigned 32-bit quantity */
64 #define align(x, y) (((x) + (y) - 1) & ~((y) - 1))
67 uint32_t fmt; /* Format */
68 uint32_t flags; /* Bit flags */
69 uint32_t size; /* Size of file */
70 uint32_t text; /* Size of text segment */
71 uint32_t data; /* Size of data segment */
72 uint32_t bss; /* Size of bss segment */
73 uint32_t org; /* Program origin */
74 uint32_t entry; /* Program entry point */
77 static const char *const fmtlist[] = {"bin", "aout", "elf"};
79 static const char binfo[] =
80 "kernel: ver=%u.%02u size=%x load=%x entry=%x map=%uM "
82 static const char cinfo[] =
83 "client: fmt=%s size=%x text=%x data=%x bss=%x entry=%x\n";
84 static const char oinfo[] =
85 "output: fmt=%s size=%x text=%x data=%x org=%x entry=%x\n";
87 static const char *lname =
88 BTX_PATH "/btxldr/btxldr"; /* BTX loader */
89 static const char *bname =
90 BTX_PATH "/btx/btx"; /* BTX kernel */
91 static const char *oname =
92 "a.out"; /* Output filename */
94 static int ppage = -1; /* First page present */
95 static int wpage = -1; /* First page writable */
97 static unsigned int format; /* Output format */
99 static uint32_t centry; /* Client entry address */
100 static uint32_t lentry; /* Loader entry address */
102 static int Eflag; /* Client entry option */
104 static int quiet; /* Inhibit warnings */
105 static int verbose; /* Display information */
107 static const char *tname; /* Temporary output file */
108 static const char *fname; /* Current input file */
110 static void cleanup(void);
111 static void btxld(const char *);
112 static void getbtx(int, struct btx_hdr *);
113 static void gethdr(int, struct hdr *);
114 static void puthdr(int, struct hdr *);
115 static void copy(int, int, size_t, off_t);
116 static size_t readx(int, void *, size_t, off_t);
117 static void writex(int, const void *, size_t);
118 static void seekx(int, off_t);
119 static unsigned int optfmt(const char *);
120 static uint32_t optaddr(const char *);
121 static int optpage(const char *, int);
122 static void Warn(const char *, const char *, ...);
123 static void usage(void);
126 * A link editor for BTX clients.
129 main(int argc, char *argv[])
133 while ((c = getopt(argc, argv, "qvb:E:e:f:l:o:P:W:")) != -1)
145 centry = optaddr(optarg);
149 lentry = optaddr(optarg);
152 format = optfmt(optarg);
161 ppage = optpage(optarg, 1);
164 wpage = optpage(optarg, BTX_MAXCWR);
179 * Clean up after errors.
189 * Read the input files; write the output file; display information.
192 btxld(const char *iname)
194 char name[FILENAME_MAX];
196 struct hdr ihdr, ohdr;
197 unsigned int ldr_size, cwr;
202 for (i = I_LDR; i <= I_CLNT; i++) {
203 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
204 if ((fdi[i] = open(fname, O_RDONLY)) == -1)
208 gethdr(fdi[i], &ihdr);
209 if (ihdr.fmt != F_BIN)
210 Warn(fname, "Loader format is %s; processing as %s",
211 fmtlist[ihdr.fmt], fmtlist[F_BIN]);
212 ldr_size = ihdr.size;
215 getbtx(fdi[i], &btx);
218 gethdr(fdi[i], &ihdr);
219 if (ihdr.org && ihdr.org != BTX_PGSIZE)
221 "Client origin is 0x%x; expecting 0 or 0x%x",
222 ihdr.org, BTX_PGSIZE);
225 memset(&ohdr, 0, sizeof(ohdr));
227 ohdr.text = ldr_size;
228 ohdr.data = btx.btx_textsz + ihdr.size;
232 if (wpage > 0 || (wpage == -1 && !(ihdr.flags & IMPURE))) {
236 cwr = howmany(ihdr.text, BTX_PGSIZE);
237 if (cwr > BTX_MAXCWR)
241 if (ppage > 0 || (ppage && wpage && ihdr.org >= BTX_PGSIZE)) {
242 btx.btx_flags |= BTX_MAPONE;
246 btx.btx_pgctl -= cwr;
247 btx.btx_entry = Eflag ? centry : ihdr.entry;
248 if (snprintf(name, sizeof(name), "%s.tmp", oname) >= sizeof(name))
249 errx(2, "%s: Filename too long", oname);
250 if ((fdo = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1)
252 if (!(tname = strdup(name)))
255 for (i = I_LDR; i <= I_CLNT; i++) {
256 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
259 copy(fdi[i], fdo, ldr_size, 0);
260 seekx(fdo, ohdr.size += ohdr.text);
263 writex(fdo, &btx, sizeof(btx));
264 copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx),
268 copy(fdi[i], fdo, ihdr.size, 0);
269 if (ftruncate(fdo, ohdr.size += ohdr.data))
277 if (rename(tname, oname))
278 err(2, "%s: Can't rename to %s", tname, oname);
281 printf(binfo, btx.btx_majver, btx.btx_minver, btx.btx_textsz,
282 BTX_ORIGIN(btx), BTX_ENTRY(btx), BTX_MAPPED(btx) *
283 BTX_PGSIZE / 0x100000, !!(btx.btx_flags & BTX_MAPONE),
284 BTX_MAPPED(btx) - btx.btx_pgctl - BTX_PGBASE /
285 BTX_PGSIZE - BTX_MAPPED(btx) * 4 / BTX_PGSIZE);
286 printf(cinfo, fmtlist[ihdr.fmt], ihdr.size, ihdr.text,
287 ihdr.data, ihdr.bss, ihdr.entry);
288 printf(oinfo, fmtlist[ohdr.fmt], ohdr.size, ohdr.text,
289 ohdr.data, ohdr.org, ohdr.entry);
294 * Read BTX file header.
297 getbtx(int fd, struct btx_hdr * btx)
299 if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) ||
300 btx->btx_magic[0] != BTX_MAG0 ||
301 btx->btx_magic[1] != BTX_MAG1 ||
302 btx->btx_magic[2] != BTX_MAG2)
303 errx(1, "%s: Not a BTX kernel", fname);
307 * Get file size and read a.out or ELF header.
310 gethdr(int fd, struct hdr *hdr)
313 const struct exec *ex;
314 const Elf32_Ehdr *ee;
315 const Elf32_Phdr *ep;
317 unsigned int fmt, x, n, i;
319 memset(hdr, 0, sizeof(*hdr));
322 if (sb.st_size > MAXU32)
323 errx(1, "%s: Too big", fname);
324 hdr->size = sb.st_size;
325 if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd,
328 for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--)
332 if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) {
335 if (x == OMAGIC || x == NMAGIC) {
337 Warn(fname, "Treating %s NMAGIC as OMAGIC",
339 hdr->flags |= IMPURE;
341 hdr->text = ex->a_text;
342 hdr->data = ex->a_data;
343 hdr->bss = ex->a_bss;
344 hdr->entry = ex->a_entry;
345 if (ex->a_entry >= BTX_PGSIZE)
346 hdr->org = BTX_PGSIZE;
351 if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) {
353 for (n = i = 0; i < ee->e_phnum; i++) {
354 ep = (void *)((uint8_t *)p + ee->e_phoff +
355 ee->e_phentsize * i);
356 if (ep->p_type == PT_LOAD)
359 hdr->text = ep->p_filesz;
360 hdr->org = ep->p_paddr;
361 if (ep->p_flags & PF_W)
362 hdr->flags |= IMPURE;
365 hdr->data = ep->p_filesz;
366 hdr->bss = ep->p_memsz - ep->p_filesz;
370 "Ignoring extra %s PT_LOAD segments",
374 hdr->entry = ee->e_entry;
377 if (munmap(p, hdr->size))
382 * Write a.out or ELF header.
385 puthdr(int fd, struct hdr *hdr)
392 memset(&ex, 0, sizeof(ex));
393 N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0);
394 hdr->text = N_ALIGN(ex, hdr->text);
395 ex.a_text = hdr->text;
396 hdr->data = N_ALIGN(ex, hdr->data);
397 ex.a_data = hdr->data;
398 ex.a_entry = hdr->entry;
399 writex(fd, &ex, sizeof(ex));
400 hdr->size = N_ALIGN(ex, sizeof(ex));
401 seekx(fd, hdr->size);
405 eh.e.e_entry = hdr->entry;
406 eh.p[0].p_vaddr = eh.p[0].p_paddr = hdr->org;
407 eh.p[0].p_filesz = eh.p[0].p_memsz = hdr->text;
408 eh.p[1].p_offset = eh.p[0].p_offset + eh.p[0].p_filesz;
409 eh.p[1].p_vaddr = eh.p[1].p_paddr = align(eh.p[0].p_paddr +
411 eh.p[1].p_filesz = eh.p[1].p_memsz = hdr->data;
412 eh.sh[2].sh_addr = eh.p[0].p_vaddr;
413 eh.sh[2].sh_offset = eh.p[0].p_offset;
414 eh.sh[2].sh_size = eh.p[0].p_filesz;
415 eh.sh[3].sh_addr = eh.p[1].p_vaddr;
416 eh.sh[3].sh_offset = eh.p[1].p_offset;
417 eh.sh[3].sh_size = eh.p[1].p_filesz;
418 writex(fd, &eh, sizeof(eh));
419 hdr->size = sizeof(eh);
424 * Safe copy from input file to output file.
427 copy(int fdi, int fdo, size_t nbyte, off_t offset)
433 if ((n = sizeof(buf)) > nbyte)
435 if (readx(fdi, buf, n, offset) != n)
436 errx(2, "%s: Short read", fname);
444 * Safe read from input file.
447 readx(int fd, void *buf, size_t nbyte, off_t offset)
451 if (offset != -1 && lseek(fd, offset, SEEK_SET) != offset)
453 if ((n = read(fd, buf, nbyte)) == -1)
459 * Safe write to output file.
462 writex(int fd, const void *buf, size_t nbyte)
466 if ((n = write(fd, buf, nbyte)) == -1)
469 errx(2, "%s: Short write", tname);
473 * Safe seek in output file.
476 seekx(int fd, off_t offset)
478 if (lseek(fd, offset, SEEK_SET) != offset)
483 * Convert an option argument to a format code.
486 optfmt(const char *arg)
490 for (i = 0; i < F_CNT && strcmp(arg, fmtlist[i]); i++);
492 errx(1, "%s: Unknown format", arg);
497 * Convert an option argument to an address.
500 optaddr(const char *arg)
506 x = strtoul(arg, &s, 0);
507 if (errno || !*arg || *s || x > MAXU32)
508 errx(1, "%s: Illegal address", arg);
513 * Convert an option argument to a page number.
516 optpage(const char *arg, int hi)
522 x = strtol(arg, &s, 0);
523 if (errno || !*arg || *s || x < 0 || x > hi)
524 errx(1, "%s: Illegal page number", arg);
532 Warn(const char *locus, const char *fmt, ...)
538 asprintf(&s, "%s: Warning: %s", locus, fmt);
547 * Display usage information.
552 fprintf(stderr, "%s\n%s\n",
553 "usage: btxld [-qv] [-b file] [-E address] [-e address] [-f format]",
554 " [-l file] [-o filename] [-P page] [-W page] file");