Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1Dxuwq-0005Vj-T2 for mharc-grub-devel@gnu.org; Wed, 27 Jul 2005 19:04:25 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Dxuwn-0005UL-7b for grub-devel@gnu.org; Wed, 27 Jul 2005 19:04:21 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Dxuwk-0005Si-QI for grub-devel@gnu.org; Wed, 27 Jul 2005 19:04:19 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Dxuwi-0005Nk-Lq for grub-devel@gnu.org; Wed, 27 Jul 2005 19:04:17 -0400 Received: from [205.158.62.67] (helo=webmail-outgoing.us4.outblaze.com) by monty-python.gnu.org with esmtp (Exim 4.34) id 1Dxv0M-0006nm-3n for grub-devel@gnu.org; Wed, 27 Jul 2005 19:08:02 -0400 Received: from unknown (unknown [192.168.9.180]) by webmail-outgoing.us4.outblaze.com (Postfix) with QMQP id 6D7B518001B2 for ; Wed, 27 Jul 2005 22:56:34 +0000 (GMT) X-OB-Received: from unknown (205.158.62.49) by wfilter.us4.outblaze.com; 27 Jul 2005 22:56:34 -0000 Received: by ws1-1.us4.outblaze.com (Postfix, from userid 1001) id 600234BEAF; Wed, 27 Jul 2005 22:56:34 +0000 (GMT) Content-Transfer-Encoding: 7bit Content-Type: multipart/alternative; boundary="_----------=_1122504994796010" MIME-Version: 1.0 Date: Wed, 27 Jul 2005 17:56:34 -0500 Received: from [84.53.202.16] by ws1-1.us4.outblaze.com with http for nruslan@mail.com; Wed, 27 Jul 2005 17:56:34 -0500 From: "Ruslan Nikolaev" To: grub-devel@gnu.org X-Originating-Ip: 84.53.202.16 X-Originating-Server: ws1-1.us4.outblaze.com Message-Id: <20050727225634.600234BEAF@ws1-1.us4.outblaze.com> Subject: [PATCH] Updated ELF64 multiboot X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Jul 2005 23:04:22 -0000 This is a multi-part message in MIME format. --_----------=_1122504994796010 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-5" Hi! I've found some time and update patch. Now it contains functions grub_load_elf32, grub_load_elf64 and grub_load_elf. The changes are the same but now code splitted into 3 functions. In fact I don't have free time at all now :( New patch: diff -urN old/multiboot.c new/multiboot.c --- old/multiboot.c 2005-07-17 02:50:11.000000000 +0400 +++ new/multiboot.c 2005-07-27 18:52:44.000000000 +0400 @@ -81,6 +81,171 @@ return GRUB_ERR_NONE; } +static int +grub_load_elf32(grub_file_t file, void *buffer) +{ + Elf32_Ehdr *ehdr =3D (Elf32_Ehdr *) buffer; + Elf32_Phdr *phdr; + int i; + + if (ehdr->e_ident[EI_CLASS] !=3D ELFCLASS32) + return -1; + + if (grub_dl_check_header (ehdr, sizeof(Elf32_Ehdr))) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "No valid ELF header found"); + return -1; + } + + if (ehdr->e_type !=3D ET_EXEC) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type"); + return -1; + } + + /* FIXME: Should we support program headers at strange locations? */ + if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "Program header at a too high offset"); + return -1; + } + entry =3D ehdr->e_entry; + + /* Load every loadable segment in memory. */ + for (i =3D 0; i < ehdr->e_phnum; i++) + { + phdr =3D (Elf32_Phdr *) ((char *) buffer + ehdr->e_phoff + i * ehdr->e_phentsize); + + if (phdr->p_type =3D=3D PT_LOAD) + { + /* The segment should fit in the area reserved for the OS. */ + if ((phdr->p_paddr < grub_os_area_addr) + || (phdr->p_paddr + phdr->p_memsz + > grub_os_area_addr + grub_os_area_size)) + { + grub_error (GRUB_ERR_BAD_OS, "Segment doesn't fit in memory reserved for the OS"); + return -1; + } + + if (grub_file_seek (file, phdr->p_offset) =3D=3D -1) + { + grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header"); + return -1; + } + + if (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz) + !=3D (grub_ssize_t) phdr->p_filesz) + { + grub_error (GRUB_ERR_BAD_OS, "Couldn't read segment from file"); + return -1; + } + + if (phdr->p_filesz < phdr->p_memsz) + grub_memset ((char *) phdr->p_paddr + phdr->p_filesz, 0, + phdr->p_memsz - phdr->p_filesz); + } + } + return 0; +} + +static int +grub_load_elf64(grub_file_t file, void *buffer) +{ + Elf64_Ehdr *ehdr =3D (Elf64_Ehdr *) buffer; + Elf64_Phdr *phdr; + int i; + + if (ehdr->e_ident[EI_CLASS] !=3D ELFCLASS64) + return -1; + + if (ehdr->e_ident[EI_MAG0] !=3D ELFMAG0 + || ehdr->e_ident[EI_MAG1] !=3D ELFMAG1 + || ehdr->e_ident[EI_MAG2] !=3D ELFMAG2 + || ehdr->e_ident[EI_MAG3] !=3D ELFMAG3 + || ehdr->e_version !=3D EV_CURRENT + || ehdr->e_ident[EI_DATA] !=3D ELFDATA2LSB + || ehdr->e_machine !=3D EM_X86_64) + { + grub_error(GRUB_ERR_UNKNOWN_OS, "No valid ELF header found"); + return -1; + } + if (ehdr->e_type !=3D ET_EXEC) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type"); + return -1; + } + + /* FIXME: Should we support program headers at strange locations? */ + if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "Program header at a too high offset"); + return -1; + } + /* We still in 32-bit mode */ + if (ehdr->e_entry > 0xffffffff) + { + grub_error (GRUB_ERR_UNKNOWN_OS, "Invalid entry point for ELF64"); + return -1; + } + entry =3D ehdr->e_entry; + + /* Load every loadable segment in memory. */ + for (i =3D 0; i < ehdr->e_phnum; i++) + { + phdr =3D (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff + i * ehdr->e_phentsize); + + if (phdr->p_type =3D=3D PT_LOAD) + { + /* The segment should fit in the area reserved for the OS. */ + if ((phdr->p_paddr < (grub_uint64_t) grub_os_area_addr) + || (phdr->p_paddr + phdr->p_memsz + > (grub_uint64_t) grub_os_area_addr + (grub_uint64_t) grub_os_area_size)) + { + grub_error (GRUB_ERR_BAD_OS, + "Segment doesn't fit in memory reserved for the OS"); + return -1; + } + + if (grub_file_seek (file, phdr->p_offset) =3D=3D -1) + { + grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header"); + return -1; + } + + if (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr), phdr->p_filesz) + !=3D (grub_ssize_t) phdr->p_filesz) + { + grub_error (GRUB_ERR_BAD_OS, "Couldn't read segment from file"); + return -1; + } + + if (phdr->p_filesz < phdr->p_memsz) + grub_memset ((char *) ((grub_uint32_t) phdr->p_paddr) + phdr->p_filesz, 0, + phdr->p_memsz - phdr->p_filesz); + } + } + return 0; +} + +static void grub_load_elf(grub_file_t file, void *buffer) +{ + if (grub_load_elf32(file, buffer) < 0) + { + /* Error occured during loading ELF32 */ + if (grub_errno) + return; + + /* Otherwise it's possible that ELFCLASS is not 32-bit */ + if (grub_load_elf64(file, buffer) < 0) + { + if (grub_errno) + return; + + grub_error(GRUB_ERR_BAD_OS, "Class of ELF file is unsupported"); + } + } +} + void grub_rescue_cmd_multiboot (int argc, char *argv[]) { @@ -89,7 +254,6 @@ struct grub_multiboot_header *header; grub_ssize_t len; int i; - Elf32_Ehdr *ehdr; grub_dl_ref (my_mod); @@ -138,65 +302,10 @@ goto fail; } - ehdr =3D (Elf32_Ehdr *) buffer; - - if (grub_dl_check_header (ehdr, sizeof(*ehdr))) - { - grub_error (GRUB_ERR_UNKNOWN_OS, "No valid ELF header found"); - goto fail; - } - - if (ehdr->e_type !=3D ET_EXEC) - { - grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type"); - goto fail; - } - - /* FIXME: Should we support program headers at strange locations? */ - if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH) - { - grub_error (GRUB_ERR_UNKNOWN_OS, "Program header at a too high offset"); - goto fail; - } - - entry =3D ehdr->e_entry; - - /* Load every loadable segment in memory. */ - for (i =3D 0; i < ehdr->e_phnum; i++) - { - Elf32_Phdr *phdr; - phdr =3D (Elf32_Phdr *) (buffer + ehdr->e_phoff + i * ehdr->e_phentsize); - - if (phdr->p_type =3D=3D PT_LOAD) - { - /* The segment should fit in the area reserved for the OS. */ - if ((phdr->p_paddr < grub_os_area_addr) - || (phdr->p_paddr + phdr->p_memsz - > grub_os_area_addr + grub_os_area_size)) - { - grub_error (GRUB_ERR_BAD_OS, - "Segment doesn't fit in memory reserved for the OS"); - goto fail; - } -=20=20 - if (grub_file_seek (file, phdr->p_offset) =3D=3D -1) - { - grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header"); - goto fail; - } - - if (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz) - !=3D (grub_ssize_t) phdr->p_filesz) - { - grub_error (GRUB_ERR_BAD_OS, "Couldn't read segment from file"); - goto fail; - } - - if (phdr->p_filesz < phdr->p_memsz) - grub_memset ((char *) phdr->p_paddr + phdr->p_filesz, 0, - phdr->p_memsz - phdr->p_filesz); - } - } + /* Load ELF file */ + grub_load_elf(file, buffer); + if (grub_errno) + goto fail; mbi =3D grub_malloc (sizeof (struct grub_multiboot_info)); if (!mbi) --=20 ___________________________________________________________ Sign-up for Ads Free at Mail.com http://promo.mail.com/adsfreejump.htm --_----------=_1122504994796010 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="iso-8859-5"

Hi!

I've found some time and update patch. Now it contains functions grub_lo= ad_elf32, grub_load_elf64 and grub_load_elf. The changes are the same but n= ow code splitted into 3 functions.

In fact I don't have free time at all now :(

 

New patch:

diff -urN old/multiboot.c new/multiboot.c
--- old/multiboot.c 20= 05-07-17 02:50:11.000000000 +0400
+++ new/multiboot.c 2005-07-27 18= :52:44.000000000 +0400
@@ -81,6 +81,171 @@
   return GRUB_E= RR_NONE;
 }
 
+static int
+grub_load_elf32(grub_file_= t file, void *buffer)
+{
+  Elf32_Ehdr *ehdr =3D (Elf32_Ehdr *) = buffer;
+  Elf32_Phdr *phdr;
+  int i;
+
+  if (= ehdr->e_ident[EI_CLASS] !=3D ELFCLASS32)
+    return -= 1;
+
+  if (grub_dl_check_header (ehdr, sizeof(Elf32_Ehdr)))
= +    {
+      grub_error (GRUB_E= RR_UNKNOWN_OS, "No valid ELF header found");
+    &n= bsp; return -1;
+    }
+
+  if (ehdr->e_typ= e !=3D ET_EXEC)
+    {
+     = grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
+  = ;    return -1;
+    }
+
+  /* = FIXME: Should we support program headers at strange locations?  */
= +  if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize >= GRUB_MB_SEARCH)
+    {
+     = ; grub_error (GRUB_ERR_UNKNOWN_OS, "Program header at a too high offset");<= BR>+      return -1;
+    }
+=   entry =3D ehdr->e_entry;
+
+  /* Load every loadable s= egment in memory.  */
+  for (i =3D 0; i < ehdr->e_phnum= ; i++)
+    {
+      phdr =3D= (Elf32_Phdr *) ((char *) buffer + ehdr->e_phoff + i * ehdr->e_phents= ize);
+
+      if (phdr->p_type =3D=3D PT= _LOAD)
+        {
+  &nb= sp;       /* The segment should fit in the ar= ea reserved for the OS.  */
+      &n= bsp;   if ((phdr->p_paddr < grub_os_area_addr)
+ &= nbsp;            || = (phdr->p_paddr + phdr->p_memsz
+     &nbs= p;        > grub_os_area_addr + grub_= os_area_size))
+         &n= bsp;  {
+         &nbs= p;    grub_error (GRUB_ERR_BAD_OS, "Segment doesn't fit in m= emory reserved for the OS");
+       =        return -1;
+    = ;        }
+
+   &n= bsp;      if (grub_file_seek (file, phdr->p_off= set) =3D=3D -1)
+         &= nbsp;  {
+         &nb= sp;    grub_error (GRUB_ERR_BAD_OS, "Invalid offset in progr= am header");
+         &nbs= p;    return -1;
+      &nb= sp;     }
+
+      =     if (grub_file_read (file, (void *) phdr->p_paddr, phd= r->p_filesz)
+         =      !=3D (grub_ssize_t) phdr->p_filesz)
+ &= nbsp;          {
+ &nb= sp;            grub_= error (GRUB_ERR_BAD_OS, "Couldn't read segment from file");
+  = ;            return = -1;
+            = }
+
+          if (phdr-= >p_filesz < phdr->p_memsz)
+      = ;      grub_memset ((char *) phdr->p_paddr + ph= dr->p_filesz, 0,
+        &nb= sp;       phdr->p_memsz - phdr->p_files= z);
+        }
+   = }
+  return 0;
+}
+
+static int
+grub_load_elf64(grub_= file_t file, void *buffer)
+{
+  Elf64_Ehdr *ehdr =3D (Elf64_Ehd= r *) buffer;
+  Elf64_Phdr *phdr;
+  int i;
+
+ = if (ehdr->e_ident[EI_CLASS] !=3D ELFCLASS64)
+    ret= urn -1;
+
+  if (ehdr->e_ident[EI_MAG0] !=3D ELFMAG0
+&nbs= p;     || ehdr->e_ident[EI_MAG1] !=3D ELFMAG1
+&n= bsp;     || ehdr->e_ident[EI_MAG2] !=3D ELFMAG2
+=       || ehdr->e_ident[EI_MAG3] !=3D ELFMAG3+      || ehdr->e_version !=3D EV_CURRENT
+=       || ehdr->e_ident[EI_DATA] !=3D ELFDATA2LS= B
+      || ehdr->e_machine !=3D EM_X86_64)<= BR>+    {
+      grub_error(GRUB= _ERR_UNKNOWN_OS, "No valid ELF header found");
+    =   return -1;
+    }
+  if (ehdr->e_type != =3D ET_EXEC)
+    {
+      gr= ub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
+  &n= bsp;   return -1;
+    }
+
+  /* FIX= ME: Should we support program headers at strange locations?  */
+&n= bsp; if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GR= UB_MB_SEARCH)
+    {
+      g= rub_error (GRUB_ERR_UNKNOWN_OS, "Program header at a too high offset");
= +      return -1;
+    }
+&nb= sp; /* We still in 32-bit mode */
+  if (ehdr->e_entry > 0xff= ffffff)
+    {
+      grub_er= ror (GRUB_ERR_UNKNOWN_OS, "Invalid entry point for ELF64");
+  = ;    return -1;
+    }
+  &nb= sp; entry =3D ehdr->e_entry;
+
+  /* Load every loadable segm= ent in memory.  */
+  for (i =3D 0; i < ehdr->e_phnum; i= ++)
+    {
+      phdr =3D (E= lf64_Phdr *) ((char *) buffer + ehdr->e_phoff + i * ehdr->e_phentsize= );
+
+      if (phdr->p_type =3D=3D PT_LO= AD)
+        {
+   =        /* The segment should fit in the area = reserved for the OS.  */
+       = ;   if ((phdr->p_paddr < (grub_uint64_t) grub_os_area_addr)=
+           &nb= sp;  || (phdr->p_paddr + phdr->p_memsz
+   &nb= sp;          > (grub_uint64= _t) grub_os_area_addr + (grub_uint64_t) grub_os_area_size))
+  = ;          {
+  &= nbsp;           grub_erro= r (GRUB_ERR_BAD_OS,
+        &n= bsp;         "Segment doesn't fit i= n memory reserved for the OS");
+      &nb= sp;       return -1;
+   &n= bsp;        }
+
+   = ;        if (grub_file_seek (file, phdr-= >p_offset) =3D=3D -1)
+       &nbs= p;    {
+        =       grub_error (GRUB_ERR_BAD_OS, "Invalid offset= in program header");
+        &= nbsp;     return -1;
+     =        }
+
+    &nb= sp;      if (grub_file_read (file, (void *) ((grub= _uint32_t) phdr->p_paddr), phdr->p_filesz)
+   &nb= sp;          !=3D (grub_ssize_= t) phdr->p_filesz)
+        &= nbsp;   {
+        &nb= sp;     grub_error (GRUB_ERR_BAD_OS, "Couldn't read seg= ment from file");
+         = ;     return -1;
+     &nbs= p;      }
+
+     &= nbsp;    if (phdr->p_filesz < phdr->p_memsz)
+&n= bsp;          grub_memset ((ch= ar *) ((grub_uint32_t) phdr->p_paddr) + phdr->p_filesz, 0,
+ = ;            &n= bsp;   phdr->p_memsz - phdr->p_filesz);
+  &nb= sp;     }
+    }
+  return 0;=
+}
+
+static void grub_load_elf(grub_file_t file, void *buffer)+{
+  if (grub_load_elf32(file, buffer) < 0)
+  &= nbsp; {
+       /* Error occured during lo= ading ELF32 */
+       if (grub_errno)
= +         return;
+
+ &n= bsp;     /* Otherwise it's possible that ELFCLASS is no= t 32-bit */
+       if (grub_load_elf64(fi= le, buffer) < 0)
+         {<= BR>+            if (= grub_errno)
+          = ;    return;
+
+      &n= bsp;     grub_error(GRUB_ERR_BAD_OS, "Class of ELF file= is unsupported");
+         }+    }
+}
+
 void
 grub_rescue_cmd_m= ultiboot (int argc, char *argv[])
 {
@@ -89,7 +254,6 @@
 = ;  struct grub_multiboot_header *header;
   grub_ssize_t = len;
   int i;
-  Elf32_Ehdr *ehdr;
 
 = ;  grub_dl_ref (my_mod);
 
@@ -138,65 +302,10 @@
 &= nbsp;     goto fail;
     }
&= nbsp;
-  ehdr =3D (Elf32_Ehdr *) buffer;
-
-  if (grub_d= l_check_header (ehdr, sizeof(*ehdr)))
-    {
- &n= bsp;    grub_error (GRUB_ERR_UNKNOWN_OS, "No valid ELF heade= r found");
-      goto fail;
-  &n= bsp; }
-
-  if (ehdr->e_type !=3D ET_EXEC)
-  &n= bsp; {
-      grub_error (GRUB_ERR_UNKNOWN_OS, = "invalid ELF file type");
-      goto fail;
= -    }
-
-  /* FIXME: Should we support program h= eaders at strange locations?  */
-  if (ehdr->e_phoff + ehd= r->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH)
-  &= nbsp; {
-      grub_error (GRUB_ERR_UNKNOWN_OS,= "Program header at a too high offset");
-     = goto fail;
-    }
-
-  entry =3D ehdr->e_e= ntry;
-
-  /* Load every loadable segment in memory.  */-  for (i =3D 0; i < ehdr->e_phnum; i++)
-   = {
-      Elf32_Phdr *phdr;
-  &nb= sp;   phdr =3D (Elf32_Phdr *) (buffer + ehdr->e_phoff + i * eh= dr->e_phentsize);
-
-      if (phdr->p= _type =3D=3D PT_LOAD)
- {
-   /* The segment should fi= t in the area reserved for the OS.  */
-   if ((phdr->= p_paddr < grub_os_area_addr)
-       |= | (phdr->p_paddr + phdr->p_memsz
-    > grub_os_= area_addr + grub_os_area_size))
-     {
- &n= bsp;     grub_error (GRUB_ERR_BAD_OS,
-  =    "Segment doesn't fit in memory reserved for the OS");
-&nbs= p;      goto fail;
-     }<= BR>-  
-   if (grub_file_seek (file, phdr->p_off= set) =3D=3D -1)
-     {
-    =    grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header= ");
-       goto fail;
-  &nb= sp;  }
-
-   if (grub_file_read (file, (void *) phdr-&= gt;p_paddr, phdr->p_filesz)
-       != =3D (grub_ssize_t) phdr->p_filesz)
-     {
-&n= bsp;      grub_error (GRUB_ERR_BAD_OS, "Couldn't r= ead segment from file");
-       goto fail= ;
-     }
-
-   if (phdr->p_files= z < phdr->p_memsz)
-     grub_memset ((char *)= phdr->p_paddr + phdr->p_filesz, 0,
-    phdr->= p_memsz - phdr->p_filesz);
- }
-    }
+&nbs= p; /* Load ELF file */
+  grub_load_elf(file, buffer);
+  i= f (grub_errno)
+    goto fail;
 
   = mbi =3D grub_malloc (sizeof (struct grub_multiboot_info));
   = if (!mbi)


--=20

___________________________________________________________
Sign-up f= or Ads Free at Mail.com
http://www.mail.com/?sr=3Dsignup

--_----------=_1122504994796010--