Currently ntldr-img build fails on anything else than the most plain
compile on i386 system. Attached patch improves the situation a tiny bit
but build system for ntldr-img stays horrible, the worst being including
of target binary in the host tool. Could someon test that this patch
didn't break anything?
diff --git a/ntldr-img/Makefile.core.common b/ntldr-img/Makefile.core.common
index f46acd5..f5c0c60 100644
--- a/ntldr-img/Makefile.core.common
+++ b/ntldr-img/Makefile.core.common
@@ -6,32 +6,33 @@ g2hdr.bin: g2hdr.img
 platform_DATA += g2hdr.bin
 CLEANFILES += g2hdr.bin
 
+g2ldr.mbr: g2ldr.img
+	head -c 8192 $< > $@
+platform_DATA += g2ldr.mbr
+CLEANFILES += g2ldr.mbr
+
 grldr.mbr: grldr.img
 	head -c 8192 $< > $@
 CLEANFILES += grldr.mbr
 
 bin2h: contrib/ntldr-img/bin2h.c
-	$(CC) $^ -o $@
+	$(BUILD_CC) $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) $^ -o $@
 CLEANFILES += bin2h
 
 grub_mbr.h: grldr.mbr bin2h
 	./bin2h grub_mbr 8192 < $< > $@
 CLEANFILES += grub_mbr.h
 
-grub-ntldr-img: contrib/ntldr-img/grubinst.c contrib/ntldr-img/utils.c grub_mbr.h
-	$(CC) -I. -I$(srcdir)/contrib/ntldr-img -DLINUX -o $@ $(srcdir)/contrib/ntldr-img/grubinst.c $(srcdir)/contrib/ntldr-img/utils.c
+grub-ntldr-img$(EXEEXT): contrib/ntldr-img/grubinst.c contrib/ntldr-img/utils.c grub_mbr.h
+	$(HOST_CC) $(HOST_CFLAGS) $(HOST_CPPFLAGS) $(HOST_LDFLAGS) -DGRUB_UTIL=1 -I. -I$(srcdir)/contrib/ntldr-img -DLINUX -o $@ $(srcdir)/contrib/ntldr-img/grubinst.c $(srcdir)/contrib/ntldr-img/utils.c
 bin_PROGRAMS += grub-ntldr-img
 CLEANFILES += grub-ntldr-img
 
 # Compatibility symlink.
-grubinst: grub-ntldr-img
+grubinst$(EXEEXT): grub-ntldr-img$(EXEEXT)
 	rm -f $@
 	$(LN_S) $< $@
 noinst_PROGRAMS += grubinst
 CLEANFILES += grubinst
 
-g2ldr.mbr: grub-ntldr-img
-	./grub-ntldr-img --grub2 -o $@
-platform_DATA += g2ldr.mbr
-CLEANFILES += g2ldr.mbr
 endif
diff --git a/ntldr-img/Makefile.core.def b/ntldr-img/Makefile.core.def
index a41f4cc..59dd44b 100644
--- a/ntldr-img/Makefile.core.def
+++ b/ntldr-img/Makefile.core.def
@@ -19,3 +19,14 @@ image = {
   objcopyflags = '-O binary';
   enable = i386_pc;
 };
+
+image = {
+  name = g2ldr;
+  i386_pc = contrib/ntldr-img/grldrstart.S;
+  i386_pc = contrib/ntldr-img/ntfsbs.S;
+  i386_pc_ccasflags = '-DGRLDR_MBR -DBOOTGRUB2';
+  i386_pc_ldflags = '$(TARGET_IMG_LDFLAGS)';
+  i386_pc_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x7c00';
+  objcopyflags = '-O binary';
+  enable = i386_pc;
+};
diff --git a/ntldr-img/grldrstart.S b/ntldr-img/grldrstart.S
index 6553926..2f523f9 100644
--- a/ntldr-img/grldrstart.S
+++ b/ntldr-img/grldrstart.S
@@ -1302,7 +1302,7 @@ Entry_32:
 
 	. = Entry_32 + 0x03
 
-#ifdef	BOOTGRUB
+#if defined (BOOTGRUB)
 	.ascii	"GRLDR   "	/* OEM name string (of OS which formatted the disk). */
 #endif
 
@@ -1830,7 +1830,9 @@ msg_BootError_32:
 
 filename_32:
 
-#ifdef	BOOTGRUB
+#ifdef	BOOTGRUB2
+	.ascii	"G2LDR      \0"
+#elif defined (BOOTGRUB)
 	.ascii	"GRLDR      \0"
 #else
 	.ascii	"KERNEL  SYS\0"
@@ -2002,7 +2004,7 @@ Entry_12_16:
 
 	. = Entry_12_16 + 0x03
 
-#ifdef	BOOTGRUB
+#ifdef BOOTGRUB
 	.ascii	"GRLDR   "
 #endif
 
@@ -2555,7 +2557,9 @@ msg_BootError_12_16:
 
 filename_12_16:
 
-#ifdef	BOOTGRUB
+#ifdef	BOOTGRUB2
+	.ascii	"G2LDR      \0"
+#elif defined (BOOTGRUB)
 	.ascii	"GRLDR      \0"
 #else
 	.ascii	"KERNEL  SYS\0"
@@ -3378,8 +3382,11 @@ msg_No_grldr_ext2:
 	.ascii	"No "
 
 filename_ext2:
+#ifdef	BOOTGRUB2
+	.ascii	"g2ldr\0"
+#else
 	.ascii	"grldr\0"
-
+#endif
 	. = Entry_ext2 + 0x1ee
 
 filename_end_ext2:
@@ -3426,9 +3433,7 @@ boot_error_ext2:
 	. = Entry_ext2 + 0x1fe
 
 	.word	0xAA55
-
 	. = _start1 + 0xA00
-
 #define INSIDE_GRLDR
 
 #include "ntfsbs.S"
@@ -5783,4 +5788,3 @@ grldr_signature:
 
 pre_stage2_start:
 
-
diff --git a/ntldr-img/grubinst.c b/ntldr-img/grubinst.c
index 5989da9..f6a4d84 100644
--- a/ntldr-img/grubinst.c
+++ b/ntldr-img/grubinst.c
@@ -23,6 +23,7 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <unistd.h>
 
 #ifndef WIN32
 
@@ -146,7 +147,9 @@ void help(void)
 
 int afg,gfg,def_drive,def_part,time_out,hot_key,part_num;
 int def_spt,def_hds,def_ssc,def_tsc;
-char *save_fn,*restore_fn,*boot_file,boot_file_83[12],*key_name;
+char *save_fn,*restore_fn,boot_file_83[12];
+const char *key_name;
+const char *boot_file;
 unsigned short load_seg;
 
 static char fn_buf[24];
@@ -284,10 +287,13 @@ void list(int hd)
   xe.cur=xe.nxt=0xFF;
   fprintf(stderr," #  id        base        leng\n");
   while (! xd_enum(hd,&xe))
-    fprintf(stderr,"%2d  %02X    %8X    %8X\n",xe.cur,xe.dfs,xe.bse,xe.len);
+    fprintf(stderr,"%2d  %02llX    %8llX    %8llX\n",xe.cur,
+	    (unsigned long long) xe.dfs,
+	    (unsigned long long) xe.bse,
+	    (unsigned long long) xe.len);
 }
 
-int is_grldr_mbr(char* buf)
+int is_grldr_mbr(unsigned char* buf)
 {
   int i,n;
 
@@ -296,13 +302,13 @@ int is_grldr_mbr(char* buf)
 
   while ((i>n) && (buf[i]==0))
     i--;
-  return (! strcmp(&buf[i-n+1],"Missing MBR-helper."));
+  return (! memcmp(&buf[i-n+1],"Missing MBR-helper.", sizeof("Missing MBR-helper.")));
 }
 
 int install(char* fn)
 {
-  int hd,nn,fs,slen;
-  char prev_mbr[sizeof(grub_mbr)];
+  int hd = -1,nn,fs,slen;
+  unsigned char prev_mbr[sizeof(grub_mbr)];
   unsigned long ssec;
 
   if (fn==NULL)
@@ -320,7 +326,7 @@ int install(char* fn)
           print_syserr("open");
           return errno;
         }
-      r1=valueat(grub_mbr[0x1FFA],0,unsigned short);
+      r1=get16(&grub_mbr[0x1FFA],0);
       nn=read(hd,grub_mbr,sizeof(grub_mbr));
       if (nn==-1)
         {
@@ -328,19 +334,19 @@ int install(char* fn)
           close(hd);
           return errno;
         }
-      if (nn<sizeof(grub_mbr))
+      if (nn<(int)sizeof(grub_mbr))
         {
           print_apperr("The input file is too short");
           close(hd);
           return 1;
         }
-      if (valueat(grub_mbr[0x1FFC],0,unsigned long)!=0xAA555247)
+      if (get32(&grub_mbr[0x1FFC],0)!=0xAA555247)
         {
           print_apperr("Invalid input file");
           close(hd);
           return 1;
         }
-      r2=valueat(grub_mbr[0x1FFA],0,unsigned short);
+      r2=get16(&grub_mbr[0x1FFA],0);
       if (r1!=r2)
         {
           char buf[80];
@@ -356,29 +362,29 @@ int install(char* fn)
 
   if (boot_file)
     {
-      unsigned short ofs,len;
+      unsigned short ofs;
 
       // Patching the FAT32 boot sector
-      ofs=valueat(grub_mbr,0x400+0x1EC,unsigned short) & 0x7FF;
-      strcpy(&grub_mbr[0x400+ofs],boot_file_83);
+      ofs=get16(&grub_mbr,0x400+0x1EC) & 0x7FF;
+      strcpy((char *) &grub_mbr[0x400+ofs],boot_file_83);
       if (load_seg)
-        valueat(grub_mbr,0x400+0x1EA,unsigned short)=load_seg;
+        set16(&grub_mbr,0x400+0x1EA,load_seg);
 
       // Patching the FAT12/FAT16 boot sector
-      ofs=valueat(grub_mbr,0x600+0x1EC,unsigned short) & 0x7FF;
-      strcpy(&grub_mbr[0x600+ofs],boot_file_83);
+      ofs=get16(&grub_mbr,0x600+0x1EC) & 0x7FF;
+      strcpy((char *) &grub_mbr[0x600+ofs],boot_file_83);
       if (load_seg)
-        valueat(grub_mbr,0x600+0x1EA,unsigned short)=load_seg;
+        set16(&grub_mbr,0x600+0x1EA,load_seg);
 
       // Patching the EXT2 boot sector
-      ofs=valueat(grub_mbr,0x800+0x1EE,unsigned short) & 0x7FF;
-      strcpy(&grub_mbr[0x800+ofs],boot_file);
+      ofs=get16(grub_mbr,0x800+0x1EE) & 0x7FF;
+      strcpy((char *) &grub_mbr[0x800+ofs],boot_file);
 
       // Patching the NTFS sector
-      ofs=valueat(grub_mbr,0xA00+0x1EC,unsigned short) & 0x7FF;
-      strcpy(&grub_mbr[0xA00+ofs],boot_file);
+      ofs=get16(grub_mbr,0xA00+0x1EC) & 0x7FF;
+      strcpy((char *) &grub_mbr[0xA00+ofs],boot_file);
       if (load_seg)
-        valueat(grub_mbr,0xA00+0x1EA,unsigned short)=load_seg;
+        set16(grub_mbr,0xA00+0x1EA,load_seg);
 
       if (afg & AFG_VERBOSE)
         {
@@ -415,15 +421,15 @@ int install(char* fn)
     }
 
   memset(&grub_mbr[512],0,512);
-  valueat(grub_mbr,2,unsigned char)=gfg;
-  valueat(grub_mbr,3,unsigned char)=time_out;
-  valueat(grub_mbr,4,unsigned short)=hot_key;
-  valueat(grub_mbr,6,unsigned char)=def_drive;
-  valueat(grub_mbr,7,unsigned char)=def_part;
+  grub_mbr[2] = gfg;
+  grub_mbr[3]=time_out;
+  set16(&grub_mbr,4,hot_key);
+  grub_mbr[6] = def_drive;
+  grub_mbr[7] = def_part;
   if ((key_name==NULL) && (hot_key==0x3920))
     key_name="SPACE";
   if (key_name)
-    strcpy(&grub_mbr[0x1fec],key_name);
+    strcpy((char *) &grub_mbr[0x1fec],key_name);
 
   hd=open(fn,O_RDWR | O_BINARY,S_IREAD | S_IWRITE);
   if (hd==-1)
@@ -455,13 +461,14 @@ int install(char* fn)
             }
           ssec=xe.bse;
           if (afg & AFG_VERBOSE)
-            fprintf(stderr,"Part Fs: %02X (%s)\nPart Leng: %u\n",xe.dfs,dfs2str(xe.dfs),xe.len);
+            fprintf(stderr,"Part Fs: %02X (%s)\nPart Leng: %llu\n",xe.dfs,dfs2str(xe.dfs),
+		    (unsigned long long) xe.len);
         }
     }
   else
     ssec=0;
   if (afg & AFG_VERBOSE)
-    fprintf(stderr,"Start sector: %u\n",ssec);
+    fprintf(stderr,"Start sector: %llu\n", (unsigned long long) ssec);
   if ((ssec) && (go_sect(hd,ssec)))
     {
       print_apperr("Can\'t seek to the start sector");
@@ -475,7 +482,7 @@ int install(char* fn)
       close(hd);
       return errno;
     }
-  if (nn<sizeof(prev_mbr))
+  if (nn<(int)sizeof(prev_mbr))
     {
       print_apperr("The input file is too short");
       close(hd);
@@ -510,14 +517,14 @@ int install(char* fn)
     {
       int n,nfs,sln;
       unsigned long ofs;
-      char bs[1024];
+      unsigned char bs[1024];
 
       ofs=0xFFFFFFFF;
       for (n=0x1BE;n<0x1FE;n+=16)
         if (prev_mbr[n+4])
           {
-            if (ofs>valueat(prev_mbr[n],8,unsigned long))
-              ofs=valueat(prev_mbr[n],8,unsigned long);
+            if (ofs>get32(&prev_mbr[n],8))
+              ofs=get32(&prev_mbr[n],8);
           }
       if (ofs<(sizeof(prev_mbr)>>9))
         {
@@ -548,8 +555,8 @@ int install(char* fn)
       if (sln)
         {
           memcpy(&grub_mbr[0xB],&bs[0xB],sln);
-          valueat(grub_mbr[0],0x1C,unsigned long)=0;
-          valueat(grub_mbr[0],0xE,unsigned short)+=ofs;
+          set32(&grub_mbr[0],0x1C,0);
+          set16(&grub_mbr[0],0xE,get16(&grub_mbr[0],0xE) + ofs);
         }
     }
   else if (fs==FST_NTFS)
@@ -600,13 +607,13 @@ int install(char* fn)
           close(hd);
           return 1;
         }
-      if (strncmp(&prev_mbr[1024+3],"GRLDR",5))
+      if (memcmp(&prev_mbr[1024+3],"GRLDR",5))
         {
           print_apperr("GRLDR is not installed");
           close(hd);
           return 1;
         }
-      if (valueat(prev_mbr,512+510,unsigned short)!=0xAA55)
+      if (get16(prev_mbr,512+510)!=0xAA55)
         {
           print_apperr("No previous saved MBR");
           close(hd);
@@ -641,8 +648,8 @@ int install(char* fn)
               close(h2);
               return errno;
             }
-          if ((nn<512) || (nn & 0x1FF!=0) ||
-              (fs!=FST_EXT2) && (valueat(grub_mbr,510,unsigned short)!=0xAA55))
+          if ((nn<512) || ((nn & 0x1FF)!=0) ||
+              ((fs!=FST_EXT2) && (get16(grub_mbr,510)!=0xAA55)))
             {
               print_apperr("Invalid restore file");
               close(hd);
@@ -709,32 +716,32 @@ int install(char* fn)
             }
           else if (fs==FST_EXT2)
             {
-              memcpy(grub_mbr,&grub_mbr[0x800],slen);
+              memcpy(&grub_mbr,&grub_mbr[0x800],slen);
               grub_mbr[0x25]=part_num;
               if (afg & AFG_LBA_MODE)
                 grub_mbr[2]=0x42;
               else if (afg & AFG_CHS_MODE)
                 grub_mbr[2]=0x2;
               if (def_spt!=-1)
-                valueat(grub_mbr,0x18,unsigned short)=def_spt;
+                set16(&grub_mbr,0x18,def_spt);
               else if ((afg & AFG_IS_FLOPPY)==0)
-                valueat(grub_mbr,0x18,unsigned short)=63;
+                set16(&grub_mbr,0x18,63);
               if (def_hds!=-1)
-                valueat(grub_mbr,0x1A,unsigned short)=def_hds;
+                set16(&grub_mbr,0x1A,def_hds);
               else if ((afg & AFG_IS_FLOPPY)==0)
-                valueat(grub_mbr,0x1A,unsigned short)=255;
+                set16(&grub_mbr,0x1A,255);
               if (def_tsc!=-1)
-                valueat(grub_mbr,0x20,unsigned long)=def_tsc;
-              valueat(grub_mbr,0x1C,unsigned long)=ssec;
+                set32(&grub_mbr,0x20,def_tsc);
+              set32(&grub_mbr,0x1C,ssec);
               // s_inode_size
               if (prev_mbr[1024+0x4C]) // s_rev_level
-                valueat(grub_mbr,0x26,unsigned short)=valueat(prev_mbr[1024],0x58,unsigned short);
+                set16(&grub_mbr,0x26,get16(&prev_mbr[1024],0x58));
               else
-                valueat(grub_mbr,0x26,unsigned short)=0x80;
+                set16(&grub_mbr,0x26,0x80);
               // s_inodes_per_group
-              valueat(grub_mbr,0x28,unsigned long)=valueat(prev_mbr[1024],0x28,unsigned long);
+              set32(&grub_mbr,0x28,get32(&prev_mbr[1024],0x28));
               // s_first_data_block+1
-              valueat(grub_mbr,0x2C,unsigned long)=valueat(prev_mbr[1024],0x14,unsigned long)+1;
+              set32(&grub_mbr,0x2C,get32(&prev_mbr[1024],0x14)+1);
             }
           else
             {
@@ -762,17 +769,17 @@ int install(char* fn)
       else if (fs==FST_FAT16)
         {
           memcpy(&grub_mbr[0xB],&prev_mbr[0xB],0x3E - 0xB);
-          valueat(grub_mbr,0x1C,unsigned long)=ssec;
+          set32(grub_mbr,0x1C,ssec);
         }
       else if (fs==FST_FAT32)
         {
           memcpy(&grub_mbr[0xB],&prev_mbr[0xB],0x5A - 0xB);
-          valueat(grub_mbr,0x1C,unsigned long)=ssec;
+          set32(grub_mbr,0x1C,ssec);
         }
       else if (fs==FST_NTFS)
         {
           memcpy(&grub_mbr[0xB],&prev_mbr[0xB],0x54 - 0xB);
-          valueat(grub_mbr,0x1C,unsigned long)=ssec;
+          set32(grub_mbr,0x1C,ssec);
         }
     }
   if (! (afg & AFG_READ_ONLY))
diff --git a/ntldr-img/ntfsbs.S b/ntldr-img/ntfsbs.S
index 88b9180..039caf3 100644
--- a/ntldr-img/ntfsbs.S
+++ b/ntldr-img/ntfsbs.S
@@ -432,7 +432,11 @@ msg_NTFS_Not_Found_Error:
 	.ascii "No "
 
 nt_boot_image:
+#ifdef	BOOTGRUB2
+	.ascii "g2ldr\0"
+#else
 	.ascii "grldr\0"
+#endif
 
 	. = nt_boot_image + 8
 
diff --git a/ntldr-img/utils.c b/ntldr-img/utils.c
index 1b68c72..dee3dfe 100644
--- a/ntldr-img/utils.c
+++ b/ntldr-img/utils.c
@@ -25,6 +25,7 @@
 #endif
 
 #include <stdio.h>
+#include <string.h>
 #include <fcntl.h>
 
 #include "utils.h"
@@ -72,7 +73,7 @@ int go_sect(int hd,unsigned long sec)
       bs[1]=sec>>23;
       if (bs[1])
         return 1;
-      return (lseek(hd,bs[0],SEEK_SET)!=bs[0]);
+      return (lseek(hd,bs[0],SEEK_SET)!=(off_t)bs[0]);
     }
 }
 
@@ -96,15 +97,15 @@ int xd_enum(int hd,xde_t* xe)
             return 1;
           if (read(hd,ebuf,nn)!=nn)
             return 1;
-          if (valueat(ebuf,0x1FE,unsigned short)!=0xAA55)
+          if (get16(ebuf,0x1FE)!=0xAA55)
             return 1;
           np=0;
           for (i=0x1BE;i<0x1FE;i+=16)
             if (ebuf[i+4])
               {
-                if ((pt[np][1]=valueat(ebuf,i+12,unsigned long))==0)
+                if ((pt[np][1]=get32(ebuf,i+12))==0)
                   return 1;
-                pt[np++][0]=valueat(ebuf,i+8,unsigned long);
+                pt[np++][0]=get32(ebuf,i+8);
               }
           if (np==0)
             return 1;
@@ -148,8 +149,8 @@ int xd_enum(int hd,xde_t* xe)
                 return 1;
               xe->cur=xe->nxt;
               xe->dfs=ebuf[xe->nxt*16+4+0x1BE];
-              xe->bse=valueat(ebuf,xe->nxt*16+8+0x1BE,unsigned long);
-              xe->len=valueat(ebuf,xe->nxt*16+12+0x1BE,unsigned long);
+              xe->bse=get32(ebuf,xe->nxt*16+8+0x1BE);
+              xe->len=get32(ebuf,xe->nxt*16+12+0x1BE);
               return 0;
             }
           else if (xe->nxt!=0xFF)
@@ -160,8 +161,8 @@ int xd_enum(int hd,xde_t* xe)
                 {
                   xe->cur=cc;
                   xe->dfs=ebuf[cc*16+4+0x1BE];
-                  xe->bse=valueat(ebuf,cc*16+8+0x1BE,unsigned long);
-                  xe->len=valueat(ebuf,cc*16+12+0x1BE,unsigned long);
+                  xe->bse=get32(ebuf,cc*16+8+0x1BE);
+                  xe->len=get32(ebuf,cc*16+12+0x1BE);
                   return 0;
                 }
               cc++;
@@ -176,19 +177,17 @@ int xd_enum(int hd,xde_t* xe)
             if ((ebuf[i*16+4+0x1BE]==5) || (ebuf[i*16+4+0x1BE]==0xF)) break;
           if (i==4)
             return 1;
-          xe->ebs=xe->bse=valueat(ebuf,i*16+8+0x1BE,unsigned long);
+          xe->ebs=xe->bse=get32(ebuf,i*16+8+0x1BE);
         }
       else
         {
           // Is end of extended partition chain ?
-          if ((ebuf[4+0x1CE]!=0x5) && (ebuf[4+0x1CE]!=0xF) ||
-              (valueat(ebuf,8+0x1CE,unsigned long)==0))
+          if (((ebuf[4+0x1CE]!=0x5) && (ebuf[4+0x1CE]!=0xF)) ||
+              (get32(ebuf,8+0x1CE)==0))
             return 1;
-          xe->bse=xe->ebs+valueat(ebuf,8+0x1CE,unsigned long);
+          xe->bse=xe->ebs+get32(ebuf,8+0x1CE);
         }
       {
-        int r;
-
         while (1)
           {
             if (go_sect(hd,xe->bse))
@@ -197,17 +196,19 @@ int xd_enum(int hd,xde_t* xe)
             if (read(hd,ebuf,nn)!=nn)
               return 1;
 
-            if (valueat(ebuf,0x1FE,unsigned short)!=0xAA55)
+            if (get16(ebuf,0x1FE)!=0xAA55)
               return 1;
 
             if ((ebuf[4+0x1BE]==5) || (ebuf[4+0x1BE]==0xF))
-              if (valueat(ebuf,8+0x1BE,unsigned long)==0)
-                return 1;
-              else
-                {
-                  xe->bse=xe->ebs+valueat(ebuf,8+0x1BE,unsigned long);
-                  continue;
-                }
+	      {
+		if (get32(ebuf,8+0x1BE)==0)
+		  return 1;
+		else
+		  {
+		    xe->bse=xe->ebs+get32(ebuf,8+0x1BE);
+		    continue;
+		  }
+	      }
             break;
           }
         kk=(ebuf[4+0x1BE]!=0);
@@ -215,8 +216,8 @@ int xd_enum(int hd,xde_t* xe)
           {
             xe->cur=cc;
             xe->dfs=ebuf[4+0x1BE];
-            xe->bse+=valueat(ebuf,8+0x1BE,unsigned long);
-            xe->len=valueat(ebuf,12+0x1BE,unsigned long);
+            xe->bse+=get32(ebuf,8+0x1BE);
+            xe->len=get32(ebuf,12+0x1BE);
             return 0;
           }
       }
@@ -255,8 +256,8 @@ int chk_mbr(unsigned char* buf)
     i+=16;
   if (i>=0x1FE)
     return 0;
-  a1=valueat(buf[i],8,unsigned long);
-  a2=a1+valueat(buf[i],12,unsigned long)-1;
+  a1=get32(&buf[i],8);
+  a2=a1+get32(&buf[i],12)-1;
   if (a1>=a2)
     return 0;
   split_chs(buf+i+5,&c2,&h2,&s2);
@@ -303,8 +304,8 @@ int chk_mbr(unsigned char* buf)
     {
       if (buf[i+4])
         {
-          if ((! chk_chs(nhd,spt,valueat(buf[i],8,unsigned long),buf+i+1)) ||
-              (! chk_chs(nhd,spt,valueat(buf[i],8,unsigned long)+valueat(buf[i],12,unsigned long)-1,buf+i+5)))
+          if ((! chk_chs(nhd,spt,get32(&buf[i],8),buf+i+1)) ||
+              (! chk_chs(nhd,spt,get32(&buf[i],8)+get32(&buf[i],12)-1,buf+i+5)))
             return 0;
         }
       i+=16;
@@ -320,20 +321,20 @@ int get_fstype(unsigned char* buf)
     return FST_MBR;
 
   // The first sector of EXT2 might not contain the 0xAA55 signature
-  if (valueat(buf[1024],56,unsigned short)==EXT2_SUPER_MAGIC)
+  if (get16(&buf[1024],56)==EXT2_SUPER_MAGIC)
     return FST_EXT2;
-  if (valueat(buf[0],0x1FE,unsigned short)!=0xAA55)
+  if (get16(&buf[0],0x1FE)!=0xAA55)
     return FST_OTHER;
-  if (! strncmp(&buf[0x36],"FAT",3))
+  if (! memcmp(&buf[0x36],"FAT",3))
     return ((buf[0x26]==0x28) || (buf[0x26]==0x29))?FST_FAT16:FST_OTHER;
-  if (! strncmp(&buf[0x52],"FAT32",5))
+  if (! memcmp(&buf[0x52],"FAT32",5))
     return ((buf[0x42]==0x28) || (buf[0x42]==0x29))?FST_FAT32:FST_OTHER;
-  if (! strncmp(&buf[0x3],"NTFS",4))
+  if (! memcmp(&buf[0x3],"NTFS",4))
     return ((buf[0]==0xEB) && (buf[1]==0x52))?FST_NTFS:FST_OTHER;
   return FST_OTHER;
 }
 
-char* fst2str(int fs)
+const char* fst2str(int fs)
 {
   switch (fs) {
   case FST_OTHER:
@@ -355,7 +356,7 @@ char* fst2str(int fs)
 
 typedef struct {
   int id;
-  char* str;
+  const char* str;
 } fstab_t;
 
 static fstab_t fstab[]= {
@@ -380,7 +381,7 @@ static fstab_t fstab[]= {
   {0xA5,"FBSD"},
   {0,"Other"}};
 
-char* dfs2str(int fs)
+const char* dfs2str(int fs)
 {
   int i;
 
diff --git a/ntldr-img/utils.h b/ntldr-img/utils.h
index 05ab114..e48aac8 100644
--- a/ntldr-img/utils.h
+++ b/ntldr-img/utils.h
@@ -42,16 +42,45 @@ typedef struct {
   unsigned long	bse;		// Partition start address
   unsigned long len;		// Partition length
   unsigned long ebs;		// Base address for the extended partition
-} xde_t;
+} __attribute__ ((packed)) xde_t;
 
-#define valueat(buf,ofs,type)	*((type*)(((char*)&buf)+ofs))
+static inline unsigned short
+get16 (const void *buf_, unsigned offset)
+{
+  unsigned char *buf = (unsigned char *) buf_ + offset;
+  return buf[0] | (buf[1] << 8);
+}
+static inline unsigned int
+get32 (const void *buf_, unsigned offset)
+{
+  unsigned char *buf = (unsigned char *) buf_ + offset;
+  return buf[0] | (buf[1] << 8) | (buf[1] << 16) | (buf[1] << 24);
+}
+
+static inline void
+set16 (void *buf_, unsigned offset, unsigned short val)
+{
+  unsigned char *buf = (unsigned char *) buf_ + offset;
+  buf[0] = val;
+  buf[1] = val >> 8;
+}
+
+static inline void
+set32 (void *buf_, unsigned offset, unsigned int val)
+{
+  unsigned char *buf = (unsigned char *) buf_ + offset;
+  buf[0] = val;
+  buf[1] = val >> 8;
+  buf[2] = val >> 16;
+  buf[3] = val >> 24;
+}
 
 extern int mbr_nhd, mbr_spt;
 int go_sect(int,unsigned long);
 int xd_enum(int,xde_t*);
 int get_fstype(unsigned char*);
-char* fst2str(int);
-char* dfs2str(int);
+const char* fst2str(int);
+const char* dfs2str(int);
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to