grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cross-compiling GRUB


From: Marco Gerards
Subject: Cross-compiling GRUB
Date: Sun, 10 Apr 2005 19:06:49 +0200
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux)

Hi,

At the moment I am compiling GRUB on one PPC system and running it on
another.  At the moment this is very unproductive for me and making it
hard, if not impossible, to do any testing.  So I set up a
cross-compiler so I can compile GRUB 2 for the PPC on my AMD64.

There are three problems with GRUB 2 CVS ATM.  Most importantly is
that grub-mkimage can not be run on the AMD64 because it is little
endian.  This patch fixed the endian issues in grub-mkimage and it all
works now.  Another issue is that some check in configure.ac was
wrong.  The last is that genmk.rb did not work correctly because it
produces PPC .o files which it tries to link with the AMD64 linker. :)

I think all these fixes are too obvious and should not cause any
copyright problems.  Okuji, do you agree with this?

Johan, Hollis, this patch touches some of your files.  It would be
nice if both of you could have a quick look at this patch. :)

Thanks,
Marco

2005-04-10  Marco Gerards  <address@hidden>

        * configure.ac: Fix the test for cross-compiling.

        * genmk.rb (Program): Use `$(CC)' instead of `$(BUILD_CC)'.  Don't
        define GRUB_UTIL anymore.

        * util/powerpc/ieee1275/grub-mkimage.c (load_note): Endian fixes
        so this function works on other systems than just big endian.
        (load_modules): Likewise.
        (add_segments): Likewise.

Index: configure.ac
===================================================================
RCS file: /cvsroot/grub/grub2/configure.ac,v
retrieving revision 1.9
diff -u -p -u -p -r1.9 configure.ac
--- configure.ac        4 Apr 2004 13:45:59 -0000       1.9
+++ configure.ac        10 Apr 2005 16:36:43 -0000
@@ -109,7 +109,7 @@ AC_CHECK_TOOL(LD, ld)
 AC_PATH_PROG(RUBY, ruby)
 
 # For cross-compiling.
-if test "x$build" = "x$host"; then
+if test "x$build" != "x$host"; then
   AC_CHECK_PROGS(BUILD_CC, [gcc egcs cc],
                 [AC_MSG_ERROR([none of gcc, egcs and cc is found. set BUILD_CC 
manually.])])
 else
Index: genmk.rb
===================================================================
RCS file: /cvsroot/grub/grub2/genmk.rb,v
retrieving revision 1.12
diff -u -p -u -p -r1.12 genmk.rb
--- genmk.rb    8 Mar 2005 01:01:06 -0000       1.12
+++ genmk.rb    10 Apr 2005 16:36:43 -0000
@@ -236,7 +236,7 @@ class Program
 MOSTLYCLEANFILES += #{deps_str}
 
 address@hidden: #{objs_str}
-       $(BUILD_CC) -o $@ $^ $(BUILD_LDFLAGS) $(#{prefix}_LDFLAGS)
+       $(CC) -o $@ $^ $(LDFLAGS) $(#{prefix}_LDFLAGS)
 
 " + objs.collect_with_index do |obj, i|
       src = sources[i]
@@ -245,11 +245,11 @@ MOSTLYCLEANFILES += #{deps_str}
       dir = File.dirname(src)
 
       "#{obj}: #{src}
-       $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 
$(#{prefix}_CFLAGS) -c -o $@ $<
+       $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) 
$(#{prefix}_CFLAGS) -c -o $@ $<
 
 #{dep}: #{src}
        set -e; \
-         $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 
$(#{prefix}_CFLAGS) -M $< \
+         $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) 
$(#{prefix}_CFLAGS) -M $< \
          | sed 's,#{Regexp.quote(fake_obj)}[ :]*,#{obj} $@ : ,g' > $@; \
          [ -s $@ ] || rm -f $@
 
Index: util/powerpc/ieee1275/grub-mkimage.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/powerpc/ieee1275/grub-mkimage.c,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 grub-mkimage.c
--- util/powerpc/ieee1275/grub-mkimage.c        9 Jan 2005 18:11:05 -0000       
1.2
+++ util/powerpc/ieee1275/grub-mkimage.c        10 Apr 2005 16:36:44 -0000
@@ -84,15 +84,16 @@ load_note (Elf32_Phdr *phdr, FILE *out)
   note.descriptor.load_base = grub_cpu_to_be32 (0x00004000);
 
   /* Write the note data to the new segment.  */
-  grub_util_write_image_at (&note, note_size, phdr->p_offset, out);
+  grub_util_write_image_at (&note, note_size,
+                           grub_be_to_cpu32 (phdr->p_offset), out);
 
   /* Fill in the rest of the segment header.  */
-  phdr->p_type = PT_NOTE;
-  phdr->p_flags = PF_R;
-  phdr->p_align = sizeof (long);
+  phdr->p_type = grub_cpu_to_be32 (PT_NOTE);
+  phdr->p_flags = grub_cpu_to_be32 (PF_R);
+  phdr->p_align = grub_cpu_to_be32 (sizeof (long));
   phdr->p_vaddr = 0;
   phdr->p_paddr = 0;
-  phdr->p_filesz = note_size;
+  phdr->p_filesz = grub_cpu_to_be32 (note_size);
   phdr->p_memsz = 0;
 }
 
@@ -120,9 +121,9 @@ load_modules (Elf32_Phdr *phdr, const ch
 
   module_img = xmalloc (total_module_size);
   modinfo = (struct grub_module_info *) module_img;
-  modinfo->magic = GRUB_MODULE_MAGIC;
-  modinfo->offset = sizeof (struct grub_module_info);
-  modinfo->size = total_module_size;
+  modinfo->magic = grub_cpu_to_be32 (GRUB_MODULE_MAGIC);
+  modinfo->offset = grub_cpu_to_be32 (sizeof (struct grub_module_info));
+  modinfo->size = grub_cpu_to_be32 (total_module_size);
 
   /* Load all the modules, with headers, into module_img.  */
   for (p = path_list; p; p = p->next)
@@ -144,16 +145,17 @@ load_modules (Elf32_Phdr *phdr, const ch
     }
 
   /* Write the module data to the new segment.  */
-  grub_util_write_image_at (module_img, total_module_size, phdr->p_offset, 
out);
+  grub_util_write_image_at (module_img, total_module_size,
+                           grub_cpu_to_be32 (phdr->p_offset), out);
 
   /* Fill in the rest of the segment header.  */
-  phdr->p_type = PT_LOAD;
-  phdr->p_flags = PF_R | PF_W | PF_X;
-  phdr->p_align = sizeof (long);
-  phdr->p_vaddr = GRUB_IEEE1275_MODULE_BASE;
-  phdr->p_paddr = GRUB_IEEE1275_MODULE_BASE;
-  phdr->p_filesz = total_module_size;
-  phdr->p_memsz = total_module_size;
+  phdr->p_type = grub_cpu_to_be32 (PT_LOAD);
+  phdr->p_flags = grub_cpu_to_be32 (PF_R | PF_W | PF_X);
+  phdr->p_align = grub_cpu_to_be32 (sizeof (long));
+  phdr->p_vaddr = grub_cpu_to_be32 (GRUB_IEEE1275_MODULE_BASE);
+  phdr->p_paddr = grub_cpu_to_be32 (GRUB_IEEE1275_MODULE_BASE);
+  phdr->p_filesz = grub_cpu_to_be32 (total_module_size);
+  phdr->p_memsz = grub_cpu_to_be32 (total_module_size);
 }
 
 void
@@ -170,27 +172,33 @@ add_segments (char *dir, FILE *out, int 
   in = fopen (kernel_path, "rb");
   if (! in)
     grub_util_error ("cannot open %s", kernel_path);
-  grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
-
-  phdrs = xmalloc (ehdr.e_phentsize * (ehdr.e_phnum + 2));
 
+  grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
+  
+  phdrs = xmalloc (grub_be_to_cpu16 (ehdr.e_phentsize)
+                  * (grub_be_to_cpu16 (ehdr.e_phnum) + 2));
   /* Copy all existing segments.  */
-  for (i = 0; i < ehdr.e_phnum; i++)
+  for (i = 0; i < grub_be_to_cpu16 (ehdr.e_phnum); i++)
     {
       char *segment_img;
 
       phdr = phdrs + i;
 
       /* Read segment header.  */
-      grub_util_read_at (phdr, sizeof (Elf32_Phdr), (ehdr.e_phoff
-                                                    + (i * ehdr.e_phentsize)),
+      grub_util_read_at (phdr, sizeof (Elf32_Phdr),
+                        (grub_be_to_cpu32 (ehdr.e_phoff)
+                         + (i * grub_be_to_cpu16 (ehdr.e_phentsize))),
                         in);
-      grub_util_info ("copying segment %d, type %d", i, phdr->p_type);
+      grub_util_info ("copying segment %d, type %d", i,
+                     grub_be_to_cpu32 (phdr->p_type));
 
       /* Read segment data and write it to new file.  */
-      segment_img = xmalloc (phdr->p_filesz);
-      grub_util_read_at (segment_img, phdr->p_filesz, phdr->p_offset, in);
-      grub_util_write_image_at (segment_img, phdr->p_filesz, phdr->p_offset, 
out);
+      segment_img = xmalloc (grub_be_to_cpu32 (phdr->p_filesz));
+  
+      grub_util_read_at (segment_img, grub_be_to_cpu32 (phdr->p_filesz),
+                        grub_be_to_cpu32 (phdr->p_offset), in);
+      grub_util_write_image_at (segment_img, grub_be_to_cpu32 (phdr->p_filesz),
+                               grub_be_to_cpu32 (phdr->p_offset), out);
 
       free (segment_img);
     }
@@ -198,11 +206,12 @@ add_segments (char *dir, FILE *out, int 
   if (mods[0] != NULL)
     {
       /* Construct new segment header for modules.  */
-      phdr = phdrs + ehdr.e_phnum;
-      ehdr.e_phnum++;
+      phdr = phdrs + grub_be_to_cpu16 (ehdr.e_phnum);
+      ehdr.e_phnum = grub_cpu_to_be16 (grub_be_to_cpu16 (ehdr.e_phnum) + 1);
 
       /* Fill in p_offset so the callees know where to write.  */
-      phdr->p_offset = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
+      phdr->p_offset = grub_cpu_to_be32 (ALIGN_UP (grub_util_get_fp_size (out),
+                                                  sizeof (long)));
 
       load_modules (phdr, dir, mods, out);
     }
@@ -210,11 +219,12 @@ add_segments (char *dir, FILE *out, int 
   if (chrp)
     {
       /* Construct new segment header for the CHRP note.  */
-      phdr = phdrs + ehdr.e_phnum;
-      ehdr.e_phnum++;
+      phdr = phdrs + grub_be_to_cpu16 (ehdr.e_phnum);
+      ehdr.e_phnum = grub_cpu_to_be16 (grub_be_to_cpu16 (ehdr.e_phnum) + 1);
 
       /* Fill in p_offset so the callees know where to write.  */
-      phdr->p_offset = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
+      phdr->p_offset = grub_cpu_to_be32 (ALIGN_UP (grub_util_get_fp_size (out),
+                                                  sizeof (long)));
 
       load_note (phdr, out);
     }
@@ -226,11 +236,12 @@ add_segments (char *dir, FILE *out, int 
 
   /* Append entire segment table to the file.  */
   phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
-  grub_util_write_image_at (phdrs, ehdr.e_phentsize * ehdr.e_phnum, phdroff,
+  grub_util_write_image_at (phdrs, grub_be_to_cpu16 (ehdr.e_phentsize)
+                           * grub_be_to_cpu16 (ehdr.e_phnum), phdroff,
                            out);
 
   /* Write ELF header.  */
-  ehdr.e_phoff = phdroff;
+  ehdr.e_phoff = grub_cpu_to_be32 (phdroff);
   grub_util_write_image_at (&ehdr, sizeof (ehdr), 0, out);
 
   free (phdrs);






reply via email to

[Prev in Thread] Current Thread [Next in Thread]