ia64/xen-unstable

changeset 6487:77af70556792

Make mbootpack work on big-endian systems.
Signed-off-by: Michal Ostrowski <mostrows@watson.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue Aug 30 13:12:47 2005 +0000 (2005-08-30)
parents 1de508b9063b
children 48e94e358663
files tools/misc/mbootpack/Makefile tools/misc/mbootpack/buildimage.c tools/misc/mbootpack/mbootpack.c tools/misc/mbootpack/mbootpack.h
line diff
     1.1 --- a/tools/misc/mbootpack/Makefile	Tue Aug 30 11:53:47 2005 +0000
     1.2 +++ b/tools/misc/mbootpack/Makefile	Tue Aug 30 13:12:47 2005 +0000
     1.3 @@ -20,8 +20,7 @@ GDB	:= gdb
     1.4  INCS	:= -I. -I-
     1.5  DEFS	:= 
     1.6  LDFLAGS	:= 
     1.7 -CC	:= gcc
     1.8 -CFLAGS 	:= -Wall -Wpointer-arith -Wcast-qual -Wno-unused -Wno-format
     1.9 +CFLAGS	:= -Wall -Wpointer-arith -Wcast-qual -Wno-unused -Wno-format
    1.10  CFLAGS	+= -Wmissing-prototypes
    1.11  #CFLAGS	+= -pipe -g -O0 -Wcast-align
    1.12  CFLAGS	+= -pipe -O3 
    1.13 @@ -34,7 +33,7 @@ DEPFLAGS = -Wp,-MD,.$(@F).d
    1.14  DEPS     = .*.d
    1.15  
    1.16  mbootpack: $(OBJS)
    1.17 -	$(CC) -o $@ $(filter-out %.a, $^) $(LDFLAGS)
    1.18 +	$(HOSTCC) -o $@ $(filter-out %.a, $^) $(LDFLAGS)
    1.19  
    1.20  clean:
    1.21  	$(RM) mbootpack *.o $(DEPS) bootsect setup bzimage_header.c bin2c
    1.22 @@ -48,7 +47,7 @@ setup: setup.S
    1.23  	$(LD) -m elf_i386 -Ttext 0x0 -s --oformat binary setup.o -o $@
    1.24  
    1.25  bin2c: bin2c.o 
    1.26 -	$(CC) -o $@ $^ 
    1.27 +	$(HOSTCC) -o $@ $^ 
    1.28  
    1.29  bzimage_header.c: bootsect setup bin2c
    1.30  	./bin2c -n 8 -b1 -a bzimage_bootsect bootsect > bzimage_header.c
    1.31 @@ -58,10 +57,10 @@ buildimage.c: bzimage_header.c
    1.32  	@
    1.33  
    1.34  %.o: %.S
    1.35 -	$(CC) $(DEPFLAGS) $(CFLAGS) $(INCS) $(DEFS) -c $< -o $@
    1.36 +	$(HOSTCC) $(DEPFLAGS) $(CFLAGS) $(INCS) $(DEFS) -c $< -o $@
    1.37  
    1.38  %.o: %.c
    1.39 -	$(CC) $(DEPFLAGS) $(CFLAGS) $(INCS) $(DEFS) -c $< -o $@
    1.40 +	$(HOSTCC) $(DEPFLAGS) $(CFLAGS) $(INCS) $(DEFS) -c $< -o $@
    1.41  
    1.42  .PHONY: all clean gdb
    1.43  .PRECIOUS: $(OBJS) $(OBJS:.o=.c) $(DEPS)
     2.1 --- a/tools/misc/mbootpack/buildimage.c	Tue Aug 30 11:53:47 2005 +0000
     2.2 +++ b/tools/misc/mbootpack/buildimage.c	Tue Aug 30 13:12:47 2005 +0000
     2.3 @@ -43,6 +43,7 @@
     2.4  #include "mbootpack.h"
     2.5  #include "mb_header.h"
     2.6  
     2.7 +
     2.8  /*  We will build an image that a bzImage-capable bootloader will load like 
     2.9   *  this:
    2.10   * 
    2.11 @@ -105,8 +106,8 @@ void make_bzImage(section_t *sections,
    2.12      section_t *s;
    2.13  
    2.14      /* Patch the kernel and mbi addresses into the setup code */
    2.15 -    *(address_t *)(bzimage_setup + BZ_ENTRY_OFFSET) = entry;
    2.16 -    *(address_t *)(bzimage_setup + BZ_MBI_OFFSET) = mbi;
    2.17 +    *(address_t *)(bzimage_setup + BZ_ENTRY_OFFSET) = eswap(entry);
    2.18 +    *(address_t *)(bzimage_setup + BZ_MBI_OFFSET) = eswap(mbi);
    2.19      if (!quiet) printf("Kernel entry is %p, MBI is %p.\n", entry, mbi);
    2.20  
    2.21      /* Write out header and trampoline */
     3.1 --- a/tools/misc/mbootpack/mbootpack.c	Tue Aug 30 11:53:47 2005 +0000
     3.2 +++ b/tools/misc/mbootpack/mbootpack.c	Tue Aug 30 13:12:47 2005 +0000
     3.3 @@ -252,20 +252,21 @@ static address_t load_kernel(const char 
     3.4      for (i = 0; i <= MIN(len - 12, MULTIBOOT_SEARCH - 12); i += 4)
     3.5      {
     3.6          mbh = (struct multiboot_header *)(headerbuf + i);
     3.7 -        if (mbh->magic != MULTIBOOT_MAGIC 
     3.8 -            || ((mbh->magic+mbh->flags+mbh->checksum) & 0xffffffff))
     3.9 +        if (eswap(mbh->magic) != MULTIBOOT_MAGIC 
    3.10 +            || ((eswap(mbh->magic)+eswap(mbh->flags)+eswap(mbh->checksum)) 
    3.11 +				& 0xffffffff))
    3.12          {
    3.13              /* Not a multiboot header */
    3.14              continue;
    3.15          }
    3.16 -        if (mbh->flags & MULTIBOOT_UNSUPPORTED) {
    3.17 +        if (eswap(mbh->flags) & MULTIBOOT_UNSUPPORTED) {
    3.18              /* Requires options we don't support */
    3.19              printf("Fatal: found a multiboot header, but it "
    3.20                      "requires multiboot options that I\n"
    3.21                      "don't understand.  Sorry.\n");
    3.22              exit(1);
    3.23          } 
    3.24 -        if (mbh->flags & MULTIBOOT_VIDEO_MODE) { 
    3.25 +        if (eswap(mbh->flags) & MULTIBOOT_VIDEO_MODE) { 
    3.26              /* Asked for screen mode information */
    3.27              /* XXX carry on regardless */
    3.28              printf("Warning: found a multiboot header which asks "
    3.29 @@ -275,22 +276,22 @@ static address_t load_kernel(const char 
    3.30          }
    3.31          /* This kernel will do: place and load it */
    3.32  
    3.33 -        if (mbh->flags & MULTIBOOT_AOUT_KLUDGE) {
    3.34 +        if (eswap(mbh->flags) & MULTIBOOT_AOUT_KLUDGE) {
    3.35  
    3.36              /* Load using the offsets in the multiboot header */
    3.37              if(!quiet) 
    3.38                  printf("Loading %s using multiboot header.\n", filename);
    3.39  
    3.40              /* How much is there? */
    3.41 -            start = mbh->load_addr;            
    3.42 -            if (mbh->load_end_addr != 0) 
    3.43 -                loadsize = mbh->load_end_addr - mbh->load_addr;
    3.44 +            start = eswap(mbh->load_addr);            
    3.45 +            if (eswap(mbh->load_end_addr) != 0) 
    3.46 +                loadsize = eswap(mbh->load_end_addr) - eswap(mbh->load_addr);
    3.47              else 
    3.48                  loadsize = sb.st_size;
    3.49              
    3.50              /* How much memory will it take up? */ 
    3.51 -            if (mbh->bss_end_addr != 0)
    3.52 -                size = mbh->bss_end_addr - mbh->load_addr;
    3.53 +            if (eswap(mbh->bss_end_addr) != 0)
    3.54 +                size = eswap(mbh->bss_end_addr) - eswap(mbh->load_addr);
    3.55              else
    3.56                  size = loadsize;
    3.57              
    3.58 @@ -335,32 +336,34 @@ static address_t load_kernel(const char 
    3.59              
    3.60              /* Done. */
    3.61              if (!quiet) printf("Loaded kernel from %s\n", filename);
    3.62 -            return mbh->entry_addr;
    3.63 +            return eswap(mbh->entry_addr);
    3.64              
    3.65          } else {
    3.66  
    3.67              /* Now look for an ELF32 header */    
    3.68              ehdr = (Elf32_Ehdr *)headerbuf;
    3.69 -            if (*(unsigned long *)ehdr != 0x464c457f 
    3.70 +            if (*(unsigned long *)ehdr != eswap(0x464c457f)
    3.71                  || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
    3.72                  || ehdr->e_ident[EI_CLASS] != ELFCLASS32
    3.73 -                || ehdr->e_machine != EM_386)
    3.74 +                || eswap(ehdr->e_machine) != EM_386)
    3.75              {
    3.76                  printf("Fatal: kernel has neither ELF32/x86 nor multiboot load"
    3.77                         " headers.\n");
    3.78                  exit(1);
    3.79              }
    3.80 -            if (ehdr->e_phoff + ehdr->e_phnum*sizeof(*phdr) > HEADERBUF_SIZE) {
    3.81 +            if (eswap(ehdr->e_phoff) + eswap(ehdr->e_phnum)*sizeof(*phdr) 
    3.82 +				> HEADERBUF_SIZE) {
    3.83                  /* Don't expect this will happen with sane kernels */
    3.84                  printf("Fatal: too much ELF for me.  Try increasing "
    3.85                         "HEADERBUF_SIZE in mbootpack.\n");
    3.86                  exit(1);
    3.87              }
    3.88 -            if (ehdr->e_phoff + ehdr->e_phnum*sizeof (*phdr) > len) {
    3.89 +            if (eswap(ehdr->e_phoff) + eswap(ehdr->e_phnum)*sizeof (*phdr) 
    3.90 +				> len) {
    3.91                  printf("Fatal: malformed ELF header overruns EOF.\n");
    3.92                  exit(1);
    3.93              }
    3.94 -            if (ehdr->e_phnum <= 0) {
    3.95 +            if (eswap(ehdr->e_phnum) <= 0) {
    3.96                  printf("Fatal: ELF kernel has no program headers.\n");
    3.97                  exit(1);
    3.98              }
    3.99 @@ -368,22 +371,22 @@ static address_t load_kernel(const char 
   3.100              if(!quiet) 
   3.101                  printf("Loading %s using ELF header.\n", filename);
   3.102  
   3.103 -            if (ehdr->e_type != ET_EXEC 
   3.104 -                || ehdr->e_version != EV_CURRENT
   3.105 -                || ehdr->e_phentsize != sizeof (Elf32_Phdr)) {
   3.106 +            if (eswap(ehdr->e_type) != ET_EXEC 
   3.107 +                || eswap(ehdr->e_version) != EV_CURRENT
   3.108 +                || eswap(ehdr->e_phentsize) != sizeof (Elf32_Phdr)) {
   3.109                  printf("Warning: funny-looking ELF header.\n");
   3.110              }
   3.111 -            phdr = (Elf32_Phdr *)(headerbuf + ehdr->e_phoff);
   3.112 +            phdr = (Elf32_Phdr *)(headerbuf + eswap(ehdr->e_phoff));
   3.113  
   3.114              /* Obey the program headers to load the kernel */
   3.115 -            for(i = 0; i < ehdr->e_phnum; i++) {
   3.116 +            for(i = 0; i < eswap(ehdr->e_phnum); i++) {
   3.117  
   3.118 -                start = phdr[i].p_paddr;
   3.119 -                size = phdr[i].p_memsz;
   3.120 -                if (phdr[i].p_type != PT_LOAD) 
   3.121 +                start = eswap(phdr[i].p_paddr);
   3.122 +                size = eswap(phdr[i].p_memsz);
   3.123 +                if (eswap(phdr[i].p_type) != PT_LOAD) 
   3.124                      loadsize = 0;
   3.125                  else 
   3.126 -                    loadsize = MIN((long int)phdr[i].p_filesz, size);
   3.127 +                    loadsize = MIN((long int)eswap(phdr[i].p_filesz), size);
   3.128  
   3.129                  if ((buffer = malloc(size)) == NULL) {
   3.130                      printf("Fatal: malloc() for kernel load failed: %s\n",
   3.131 @@ -396,7 +399,7 @@ static address_t load_kernel(const char 
   3.132  
   3.133                  /* Load section from file */ 
   3.134                  if (loadsize > 0) {
   3.135 -                    if (fseek(fp, phdr[i].p_offset, SEEK_SET) != 0) {
   3.136 +                    if (fseek(fp, eswap(phdr[i].p_offset), SEEK_SET) != 0) {
   3.137                          printf("Fatal: seek failed in %s\n",
   3.138                                  strerror(errno));
   3.139                          exit(1);
   3.140 @@ -452,7 +455,7 @@ static address_t load_kernel(const char 
   3.141           
   3.142              /* Done! */
   3.143              if (!quiet) printf("Loaded kernel from %s\n", filename);
   3.144 -            return ehdr->e_entry;
   3.145 +            return eswap(ehdr->e_entry);
   3.146          }
   3.147  
   3.148      }
   3.149 @@ -568,12 +571,12 @@ int main(int argc, char **argv)
   3.150      /* Command line */
   3.151      p = (char *)(mbi + 1);
   3.152      sprintf(p, "%s %s", imagename, command_line);
   3.153 -    mbi->cmdline = ((address_t)p) + mbi_reloc_offset;
   3.154 +    mbi->cmdline = eswap(((address_t)p) + mbi_reloc_offset);
   3.155      p += command_line_len;
   3.156  
   3.157      /* Bootloader ID */
   3.158      sprintf(p, version_string);
   3.159 -    mbi->boot_loader_name = ((address_t)p) + mbi_reloc_offset;
   3.160 +    mbi->boot_loader_name = eswap(((address_t)p) + mbi_reloc_offset);
   3.161      p += strlen(version_string) + 1;
   3.162  
   3.163      /* Next is space for the module command lines */
   3.164 @@ -582,17 +585,17 @@ int main(int argc, char **argv)
   3.165      /* Last come the module info structs */
   3.166      modp = (struct mod_list *)
   3.167          ((((address_t)p + mod_command_line_space) + 3) & ~3);
   3.168 -    mbi->mods_count = modules;
   3.169 -    mbi->mods_addr = ((address_t)modp) + mbi_reloc_offset;
   3.170 +    mbi->mods_count = eswap(modules);
   3.171 +    mbi->mods_addr = eswap(((address_t)modp) + mbi_reloc_offset);
   3.172  
   3.173      /* Memory information will be added at boot time, by setup.S 
   3.174       * or trampoline.S. */
   3.175 -    mbi->flags = MB_INFO_CMDLINE | MB_INFO_BOOT_LOADER_NAME;
   3.176 +    mbi->flags = eswap(MB_INFO_CMDLINE | MB_INFO_BOOT_LOADER_NAME);
   3.177  
   3.178  
   3.179      /* Load the modules */
   3.180      if (modules) {
   3.181 -        mbi->flags |= MB_INFO_MODS;
   3.182 +        mbi->flags = eswap(eswap(mbi->flags) | MB_INFO_MODS);
   3.183                  
   3.184          /* Go back and parse the module command lines */
   3.185          optind = opterr = 1;
   3.186 @@ -652,10 +655,10 @@ int main(int argc, char **argv)
   3.187              if (p != NULL) *p = ' ';
   3.188  
   3.189              /* Fill in the module info struct */
   3.190 -            modp->mod_start = start;
   3.191 -            modp->mod_end = start + size;
   3.192 -            modp->cmdline = (address_t)mod_clp + mbi_reloc_offset;
   3.193 -            modp->pad = 0;
   3.194 +            modp->mod_start = eswap(start);
   3.195 +            modp->mod_end = eswap(start + size);
   3.196 +            modp->cmdline = eswap((address_t)mod_clp + mbi_reloc_offset);
   3.197 +            modp->pad = eswap(0);
   3.198              modp++;
   3.199  
   3.200              /* Store the module command line */
     4.1 --- a/tools/misc/mbootpack/mbootpack.h	Tue Aug 30 11:53:47 2005 +0000
     4.2 +++ b/tools/misc/mbootpack/mbootpack.h	Tue Aug 30 13:12:47 2005 +0000
     4.3 @@ -32,6 +32,24 @@
     4.4  #undef NDEBUG
     4.5  #include <stdio.h>
     4.6  
     4.7 +#include <endian.h>
     4.8 +#include <byteswap.h>
     4.9 +#if __BYTE_ORDER == __LITTLE_ENDIAN
    4.10 +#define eswap(x) (x)
    4.11 +#else 
    4.12 +#define eswap(x)                                               \
    4.13 +        ({                                                      \
    4.14 +               typeof(x) y = (x);                              \
    4.15 +               switch(sizeof(y))                               \
    4.16 +               {                                               \
    4.17 +               case 2: y = __bswap_16(y); break;               \
    4.18 +               case 4: y = __bswap_32(y); break;               \
    4.19 +               case 8: y = __bswap_64(y); break;               \
    4.20 +               }                                               \
    4.21 +               y;                                              \
    4.22 +        })	
    4.23 +#endif			
    4.24 +
    4.25  /* Flags */
    4.26  extern int quiet;
    4.27