btxld.c (112092) | btxld.c (130927) |
---|---|
1/* 2 * Copyright (c) 1998 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 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. 25 */ 26 27#ifndef lint 28static const char rcsid[] = | 1/* 2 * Copyright (c) 1998 Robert Nordier 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 12 unchanged lines hidden (view full) --- 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. 25 */ 26 27#ifndef lint 28static const char rcsid[] = |
29 "$FreeBSD: head/usr.sbin/btxld/btxld.c 112092 2003-03-11 13:48:58Z ru $"; | 29 "$FreeBSD: head/usr.sbin/btxld/btxld.c 130927 2004-06-22 17:05:39Z obrien $"; |
30#endif /* not lint */ 31 32#include <sys/param.h> | 30#endif /* not lint */ 31 32#include <sys/param.h> |
33#include <sys/endian.h> |
|
33#include <sys/stat.h> 34#include <sys/mman.h> 35 | 34#include <sys/stat.h> 35#include <sys/mman.h> 36 |
37#include <a.out.h> |
|
36#include <err.h> 37#include <errno.h> 38#include <fcntl.h> 39#include <stdarg.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45#include "btx.h" 46#include "elfh.h" | 38#include <err.h> 39#include <errno.h> 40#include <fcntl.h> 41#include <stdarg.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46 47#include "btx.h" 48#include "elfh.h" |
47#include "endian.h" 48#include "i386_a.out.h" | |
49 50#define BTX_PATH "/sys/boot/i386/btx" 51 52#define I_LDR 0 /* BTX loader */ 53#define I_BTX 1 /* BTX kernel */ 54#define I_CLNT 2 /* Client program */ 55 56#define F_BIN 0 /* Binary */ --- 199 unchanged lines hidden (view full) --- 256 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname; 257 switch (i) { 258 case I_LDR: 259 copy(fdi[i], fdo, ldr_size, 0); 260 seekx(fdo, ohdr.size += ohdr.text); 261 break; 262 case I_BTX: 263 btxle = btx; | 49 50#define BTX_PATH "/sys/boot/i386/btx" 51 52#define I_LDR 0 /* BTX loader */ 53#define I_BTX 1 /* BTX kernel */ 54#define I_CLNT 2 /* Client program */ 55 56#define F_BIN 0 /* Binary */ --- 199 unchanged lines hidden (view full) --- 256 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname; 257 switch (i) { 258 case I_LDR: 259 copy(fdi[i], fdo, ldr_size, 0); 260 seekx(fdo, ohdr.size += ohdr.text); 261 break; 262 case I_BTX: 263 btxle = btx; |
264 btxle.btx_pgctl = HTOLE16(btxle.btx_pgctl); 265 btxle.btx_textsz = HTOLE16(btxle.btx_textsz); 266 btxle.btx_entry = HTOLE32(btxle.btx_entry); | 264 btxle.btx_pgctl = htole16(btxle.btx_pgctl); 265 btxle.btx_textsz = htole16(btxle.btx_textsz); 266 btxle.btx_entry = htole32(btxle.btx_entry); |
267 writex(fdo, &btxle, sizeof(btxle)); 268 copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx), 269 sizeof(btx)); 270 break; 271 case I_CLNT: 272 copy(fdi[i], fdo, ihdr.size, 0); 273 if (ftruncate(fdo, ohdr.size += ohdr.data)) 274 err(2, "%s", tname); --- 25 unchanged lines hidden (view full) --- 300static void 301getbtx(int fd, struct btx_hdr * btx) 302{ 303 if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) || 304 btx->btx_magic[0] != BTX_MAG0 || 305 btx->btx_magic[1] != BTX_MAG1 || 306 btx->btx_magic[2] != BTX_MAG2) 307 errx(1, "%s: Not a BTX kernel", fname); | 267 writex(fdo, &btxle, sizeof(btxle)); 268 copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx), 269 sizeof(btx)); 270 break; 271 case I_CLNT: 272 copy(fdi[i], fdo, ihdr.size, 0); 273 if (ftruncate(fdo, ohdr.size += ohdr.data)) 274 err(2, "%s", tname); --- 25 unchanged lines hidden (view full) --- 300static void 301getbtx(int fd, struct btx_hdr * btx) 302{ 303 if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) || 304 btx->btx_magic[0] != BTX_MAG0 || 305 btx->btx_magic[1] != BTX_MAG1 || 306 btx->btx_magic[2] != BTX_MAG2) 307 errx(1, "%s: Not a BTX kernel", fname); |
308 btx->btx_pgctl = LE16TOH(btx->btx_pgctl); 309 btx->btx_textsz = LE16TOH(btx->btx_textsz); 310 btx->btx_entry = LE32TOH(btx->btx_entry); | 308 btx->btx_pgctl = le16toh(btx->btx_pgctl); 309 btx->btx_textsz = le16toh(btx->btx_textsz); 310 btx->btx_entry = le32toh(btx->btx_entry); |
311} 312 313/* 314 * Get file size and read a.out or ELF header. 315 */ 316static void 317gethdr(int fd, struct hdr *hdr) 318{ 319 struct stat sb; | 311} 312 313/* 314 * Get file size and read a.out or ELF header. 315 */ 316static void 317gethdr(int fd, struct hdr *hdr) 318{ 319 struct stat sb; |
320 const struct i386_exec *ex; | 320 const struct exec *ex; |
321 const Elf32_Ehdr *ee; 322 const Elf32_Phdr *ep; 323 void *p; 324 unsigned int fmt, x, n, i; 325 326 memset(hdr, 0, sizeof(*hdr)); 327 if (fstat(fd, &sb)) 328 err(2, "%s", fname); 329 if (sb.st_size > MAXU32) 330 errx(1, "%s: Too big", fname); 331 hdr->size = sb.st_size; 332 if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd, 333 0)) == MAP_FAILED) 334 err(2, "%s", fname); 335 for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--) 336 switch (fmt) { 337 case F_AOUT: 338 ex = p; | 321 const Elf32_Ehdr *ee; 322 const Elf32_Phdr *ep; 323 void *p; 324 unsigned int fmt, x, n, i; 325 326 memset(hdr, 0, sizeof(*hdr)); 327 if (fstat(fd, &sb)) 328 err(2, "%s", fname); 329 if (sb.st_size > MAXU32) 330 errx(1, "%s: Too big", fname); 331 hdr->size = sb.st_size; 332 if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd, 333 0)) == MAP_FAILED) 334 err(2, "%s", fname); 335 for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--) 336 switch (fmt) { 337 case F_AOUT: 338 ex = p; |
339 if (hdr->size >= sizeof(struct i386_exec) && !I386_N_BADMAG(*ex)) { | 339 if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) { |
340 hdr->fmt = fmt; | 340 hdr->fmt = fmt; |
341 x = I386_N_GETMAGIC(*ex); | 341 x = N_GETMAGIC(*ex); |
342 if (x == OMAGIC || x == NMAGIC) { 343 if (x == NMAGIC) 344 Warn(fname, "Treating %s NMAGIC as OMAGIC", 345 fmtlist[fmt]); 346 hdr->flags |= IMPURE; 347 } | 342 if (x == OMAGIC || x == NMAGIC) { 343 if (x == NMAGIC) 344 Warn(fname, "Treating %s NMAGIC as OMAGIC", 345 fmtlist[fmt]); 346 hdr->flags |= IMPURE; 347 } |
348 hdr->text = LE32TOH(ex->a_text); 349 hdr->data = LE32TOH(ex->a_data); 350 hdr->bss = LE32TOH(ex->a_bss); 351 hdr->entry = LE32TOH(ex->a_entry); 352 if (LE32TOH(ex->a_entry) >= BTX_PGSIZE) | 348 hdr->text = le32toh(ex->a_text); 349 hdr->data = le32toh(ex->a_data); 350 hdr->bss = le32toh(ex->a_bss); 351 hdr->entry = le32toh(ex->a_entry); 352 if (le32toh(ex->a_entry) >= BTX_PGSIZE) |
353 hdr->org = BTX_PGSIZE; 354 } 355 break; 356 case F_ELF: 357 ee = p; 358 if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) { 359 hdr->fmt = fmt; | 353 hdr->org = BTX_PGSIZE; 354 } 355 break; 356 case F_ELF: 357 ee = p; 358 if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) { 359 hdr->fmt = fmt; |
360 for (n = i = 0; i < LE16TOH(ee->e_phnum); i++) { 361 ep = (void *)((uint8_t *)p + LE32TOH(ee->e_phoff) + 362 LE16TOH(ee->e_phentsize) * i); 363 if (LE32TOH(ep->p_type) == PT_LOAD) | 360 for (n = i = 0; i < le16toh(ee->e_phnum); i++) { 361 ep = (void *)((uint8_t *)p + le32toh(ee->e_phoff) + 362 le16toh(ee->e_phentsize) * i); 363 if (le32toh(ep->p_type) == PT_LOAD) |
364 switch (n++) { 365 case 0: | 364 switch (n++) { 365 case 0: |
366 hdr->text = LE32TOH(ep->p_filesz); 367 hdr->org = LE32TOH(ep->p_paddr); 368 if (LE32TOH(ep->p_flags) & PF_W) | 366 hdr->text = le32toh(ep->p_filesz); 367 hdr->org = le32toh(ep->p_paddr); 368 if (le32toh(ep->p_flags) & PF_W) |
369 hdr->flags |= IMPURE; 370 break; 371 case 1: | 369 hdr->flags |= IMPURE; 370 break; 371 case 1: |
372 hdr->data = LE32TOH(ep->p_filesz); 373 hdr->bss = LE32TOH(ep->p_memsz) - 374 LE32TOH(ep->p_filesz); | 372 hdr->data = le32toh(ep->p_filesz); 373 hdr->bss = le32toh(ep->p_memsz) - 374 le32toh(ep->p_filesz); |
375 break; 376 case 2: 377 Warn(fname, 378 "Ignoring extra %s PT_LOAD segments", 379 fmtlist[fmt]); 380 } 381 } | 375 break; 376 case 2: 377 Warn(fname, 378 "Ignoring extra %s PT_LOAD segments", 379 fmtlist[fmt]); 380 } 381 } |
382 hdr->entry = LE32TOH(ee->e_entry); | 382 hdr->entry = le32toh(ee->e_entry); |
383 } 384 } 385 if (munmap(p, hdr->size)) 386 err(2, "%s", fname); 387} 388 389/* 390 * Write a.out or ELF header. 391 */ 392static void 393puthdr(int fd, struct hdr *hdr) 394{ | 383 } 384 } 385 if (munmap(p, hdr->size)) 386 err(2, "%s", fname); 387} 388 389/* 390 * Write a.out or ELF header. 391 */ 392static void 393puthdr(int fd, struct hdr *hdr) 394{ |
395 struct i386_exec ex; | 395 struct exec ex; |
396 struct elfh eh; 397 398 switch (hdr->fmt) { 399 case F_AOUT: 400 memset(&ex, 0, sizeof(ex)); | 396 struct elfh eh; 397 398 switch (hdr->fmt) { 399 case F_AOUT: 400 memset(&ex, 0, sizeof(ex)); |
401 I386_N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0); 402 hdr->text = I386_N_ALIGN(ex, hdr->text); 403 ex.a_text = HTOLE32(hdr->text); 404 hdr->data = I386_N_ALIGN(ex, hdr->data); 405 ex.a_data = HTOLE32(hdr->data); 406 ex.a_entry = HTOLE32(hdr->entry); | 401 N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0); 402 hdr->text = N_ALIGN(ex, hdr->text); 403 ex.a_text = htole32(hdr->text); 404 hdr->data = N_ALIGN(ex, hdr->data); 405 ex.a_data = htole32(hdr->data); 406 ex.a_entry = htole32(hdr->entry); |
407 writex(fd, &ex, sizeof(ex)); | 407 writex(fd, &ex, sizeof(ex)); |
408 hdr->size = I386_N_ALIGN(ex, sizeof(ex)); | 408 hdr->size = N_ALIGN(ex, sizeof(ex)); |
409 seekx(fd, hdr->size); 410 break; 411 case F_ELF: 412 eh = elfhdr; | 409 seekx(fd, hdr->size); 410 break; 411 case F_ELF: 412 eh = elfhdr; |
413 eh.e.e_entry = HTOLE32(hdr->entry); 414 eh.p[0].p_vaddr = eh.p[0].p_paddr = HTOLE32(hdr->org); 415 eh.p[0].p_filesz = eh.p[0].p_memsz = HTOLE32(hdr->text); 416 eh.p[1].p_offset = HTOLE32(LE32TOH(eh.p[0].p_offset) + 417 LE32TOH(eh.p[0].p_filesz)); | 413 eh.e.e_entry = htole32(hdr->entry); 414 eh.p[0].p_vaddr = eh.p[0].p_paddr = htole32(hdr->org); 415 eh.p[0].p_filesz = eh.p[0].p_memsz = htole32(hdr->text); 416 eh.p[1].p_offset = htole32(le32toh(eh.p[0].p_offset) + 417 le32toh(eh.p[0].p_filesz)); |
418 eh.p[1].p_vaddr = eh.p[1].p_paddr = | 418 eh.p[1].p_vaddr = eh.p[1].p_paddr = |
419 HTOLE32(align(LE32TOH(eh.p[0].p_paddr) + LE32TOH(eh.p[0].p_memsz), | 419 htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz), |
420 4)); | 420 4)); |
421 eh.p[1].p_filesz = eh.p[1].p_memsz = HTOLE32(hdr->data); | 421 eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data); |
422 eh.sh[2].sh_addr = eh.p[0].p_vaddr; 423 eh.sh[2].sh_offset = eh.p[0].p_offset; 424 eh.sh[2].sh_size = eh.p[0].p_filesz; 425 eh.sh[3].sh_addr = eh.p[1].p_vaddr; 426 eh.sh[3].sh_offset = eh.p[1].p_offset; 427 eh.sh[3].sh_size = eh.p[1].p_filesz; 428 writex(fd, &eh, sizeof(eh)); 429 hdr->size = sizeof(eh); --- 137 unchanged lines hidden --- | 422 eh.sh[2].sh_addr = eh.p[0].p_vaddr; 423 eh.sh[2].sh_offset = eh.p[0].p_offset; 424 eh.sh[2].sh_size = eh.p[0].p_filesz; 425 eh.sh[3].sh_addr = eh.p[1].p_vaddr; 426 eh.sh[3].sh_offset = eh.p[1].p_offset; 427 eh.sh[3].sh_size = eh.p[1].p_filesz; 428 writex(fd, &eh, sizeof(eh)); 429 hdr->size = sizeof(eh); --- 137 unchanged lines hidden --- |