summaryrefslogtreecommitdiffstats
path: root/usr.sbin/btxld/btxld.c
diff options
context:
space:
mode:
authorru <ru@FreeBSD.org>2003-03-11 13:48:58 +0000
committerru <ru@FreeBSD.org>2003-03-11 13:48:58 +0000
commit559f61486d2a20e399357d595f60a0421ddc8525 (patch)
tree1faef6026c1ec0889b5f1e86fbd8a6adfb404f36 /usr.sbin/btxld/btxld.c
parentedbba08415a57bf64f5e46cae22e3e58a95527aa (diff)
downloadFreeBSD-src-559f61486d2a20e399357d595f60a0421ddc8525.zip
FreeBSD-src-559f61486d2a20e399357d595f60a0421ddc8525.tar.gz
Make this work on different endianness machines.
Tested on: sparc64 : FreeBSD/i386 bootstrap loader, Revision 1.1 : (ru@panther.freebsd.org, Tue Mar 11 05:31:14 PST 2003) : Loading /boot/defaults/loader.conf
Diffstat (limited to 'usr.sbin/btxld/btxld.c')
-rw-r--r--usr.sbin/btxld/btxld.c83
1 files changed, 47 insertions, 36 deletions
diff --git a/usr.sbin/btxld/btxld.c b/usr.sbin/btxld/btxld.c
index d5750f2..9216031 100644
--- a/usr.sbin/btxld/btxld.c
+++ b/usr.sbin/btxld/btxld.c
@@ -42,9 +42,10 @@ static const char rcsid[] =
#include <string.h>
#include <unistd.h>
-#include "a.out.h"
#include "btx.h"
#include "elfh.h"
+#include "endian.h"
+#include "i386_a.out.h"
#define BTX_PATH "/sys/boot/i386/btx"
@@ -191,7 +192,7 @@ static void
btxld(const char *iname)
{
char name[FILENAME_MAX];
- struct btx_hdr btx;
+ struct btx_hdr btx, btxle;
struct hdr ihdr, ohdr;
unsigned int ldr_size, cwr;
int fdi[3], fdo, i;
@@ -259,7 +260,11 @@ btxld(const char *iname)
seekx(fdo, ohdr.size += ohdr.text);
break;
case I_BTX:
- writex(fdo, &btx, sizeof(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;
@@ -300,6 +305,9 @@ getbtx(int fd, struct btx_hdr * btx)
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);
}
/*
@@ -309,7 +317,7 @@ static void
gethdr(int fd, struct hdr *hdr)
{
struct stat sb;
- const struct exec *ex;
+ const struct i386_exec *ex;
const Elf32_Ehdr *ee;
const Elf32_Phdr *ep;
void *p;
@@ -328,20 +336,20 @@ gethdr(int fd, struct hdr *hdr)
switch (fmt) {
case F_AOUT:
ex = p;
- if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) {
+ if (hdr->size >= sizeof(struct i386_exec) && !I386_N_BADMAG(*ex)) {
hdr->fmt = fmt;
- x = N_GETMAGIC(*ex);
+ 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 = ex->a_text;
- hdr->data = ex->a_data;
- hdr->bss = ex->a_bss;
- hdr->entry = ex->a_entry;
- if (ex->a_entry >= BTX_PGSIZE)
+ 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;
@@ -349,20 +357,21 @@ gethdr(int fd, struct hdr *hdr)
ee = p;
if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) {
hdr->fmt = fmt;
- for (n = i = 0; i < ee->e_phnum; i++) {
- ep = (void *)((uint8_t *)p + ee->e_phoff +
- ee->e_phentsize * i);
- if (ep->p_type == PT_LOAD)
+ 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 = ep->p_filesz;
- hdr->org = ep->p_paddr;
- if (ep->p_flags & PF_W)
+ 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 = ep->p_filesz;
- hdr->bss = ep->p_memsz - ep->p_filesz;
+ hdr->data = LE32TOH(ep->p_filesz);
+ hdr->bss = LE32TOH(ep->p_memsz) -
+ LE32TOH(ep->p_filesz);
break;
case 2:
Warn(fname,
@@ -370,7 +379,7 @@ gethdr(int fd, struct hdr *hdr)
fmtlist[fmt]);
}
}
- hdr->entry = ee->e_entry;
+ hdr->entry = LE32TOH(ee->e_entry);
}
}
if (munmap(p, hdr->size))
@@ -383,31 +392,33 @@ gethdr(int fd, struct hdr *hdr)
static void
puthdr(int fd, struct hdr *hdr)
{
- struct exec ex;
+ struct i386_exec ex;
struct elfh eh;
switch (hdr->fmt) {
case F_AOUT:
memset(&ex, 0, sizeof(ex));
- N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0);
- hdr->text = N_ALIGN(ex, hdr->text);
- ex.a_text = hdr->text;
- hdr->data = N_ALIGN(ex, hdr->data);
- ex.a_data = hdr->data;
- ex.a_entry = hdr->entry;
+ 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 = N_ALIGN(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 = hdr->entry;
- eh.p[0].p_vaddr = eh.p[0].p_paddr = hdr->org;
- eh.p[0].p_filesz = eh.p[0].p_memsz = hdr->text;
- eh.p[1].p_offset = eh.p[0].p_offset + eh.p[0].p_filesz;
- eh.p[1].p_vaddr = eh.p[1].p_paddr = align(eh.p[0].p_paddr +
- eh.p[0].p_memsz, 4);
- eh.p[1].p_filesz = eh.p[1].p_memsz = hdr->data;
+ 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;
OpenPOWER on IntegriCloud