Skip to contentSkip to conversation

Mingw support for grub2

PATCH In review Bean 4 participants 13 messages Aug 23, 2008 – Aug 29, 2008

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 Mingw support for grub2

Hi,

This patch add support for mingw, now you can create native executable for windows.

I still use cygwin to compile it:

./configure CC="gcc -mno-cygwin"

It might be possible to use pure mingw environment, but configure.ac would need some modification.

2008-08-23 Bean <[email protected]>

* include/grub/symbol.h: Replace #ifndef __CYGWIN__ with #if ! defined (__CYGWIN__) && ! defined (__MINGW32__).

* include/grub/util/misc.h: Add #include <grub/types.h>, add function fseeko, ftello, sync, asprintf, grub_util_get_disk_size and sleep for mingw.

* util/biosdisk.c (grub_util_biosdisk_open): Use grub_util_get_disk_size to get size in mingw.
(open_device): Use flag O_BINARY.
(find_root_device): Add handling for mingw.

* util/hostfs.c: Add #include <grub/util/misc.h> (grub_hostfs_open): Use "rb" flag to open file, use grub_util_get_disk_size to get disk size.

* util/misc.c: Add #include <winioctl.h> and <stdarg.h> in mingw. (fseeko): New function.
(ftello): Likewise.
(sync): Likewise.
(asprintf): Likewise.
(grub_util_get_disk_size): Likewise.

Signature · 1 line
Bean
include/grub/symbol.h +1 -1
index e951490..c662c14 100644
@@ -28,7 +28,7 @@
 # define EXT_C(sym)	sym
 #endif
 
-#ifndef __CYGWIN__
+#if ! defined (__CYGWIN__) && ! defined (__MINGW32__)
 #define FUNCTION(x)	.globl EXT_C(x) ; .type EXT_C(x), "function" ; EXT_C(x):
 #define VARIABLE(x)	.globl EXT_C(x) ; .type EXT_C(x), "object" ; EXT_C(x):
 #else
include/grub/util/misc.h +17 -0
index c9a8528..4d2ce8f 100644
@@ -24,6 +24,8 @@
 #include <setjmp.h>
 #include <unistd.h>
 
+#include <grub/types.h>
+
 #ifdef __NetBSD__
 /* NetBSD uses /boot for its boot block.  */
 # define DEFAULT_DIRECTORY	"/grub"
@@ -55,4 +57,19 @@ void grub_util_write_image_at (const void *img, size_t size, off_t offset,
 			       FILE *out);
 char *grub_util_get_disk_name (int disk, char *name);
 
+#ifdef __MINGW32__
+
+#include <windows.h>
+
+grub_int64_t fseeko (FILE *fp, grub_int64_t offset, int whence);
+grub_int64_t ftello (FILE *fp);
+void sync (void);
+int asprintf (char **buf, const char *fmt, ...);
+
+grub_int64_t grub_util_get_disk_size (char *name);
+
+#define sleep	Sleep
+
+#endif
+
 #endif /* ! GRUB_UTIL_MISC_HEADER */
util/biosdisk.c +19 -1
index 1137e98..bdede12 100644
@@ -157,7 +157,22 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
   disk->id = drive;
 
   /* Get the size.  */
-#if defined(__linux__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
+  {
+    grub_uint64_t size;
+
+    size = grub_util_get_disk_size (map[drive].device);
+
+    if (size % 512)
+      grub_util_error ("unaligned device size");
+
+    disk->total_sectors = size >> 9;
+
+    grub_util_info ("the size of %s is %llu", name, disk->total_sectors);
+
+    return GRUB_ERR_NONE;
+  }
+#elif defined(__linux__) || defined(__CYGWIN__)
   {
     unsigned long long nr;
     int fd;
@@ -275,6 +290,9 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
 #ifdef O_FSYNC
   flags |= O_FSYNC;
 #endif
+#ifdef O_BINARY
+  flags |= O_BINARY;
+#endif
   
 #ifdef __linux__
   /* Linux has a bug that the disk cache for a whole disk is not consistent
util/getroot.c +10 -1
index c8d31ed..5fcf2e5 100644
@@ -169,7 +169,16 @@ grub_get_prefix (const char *dir)
   return prefix;
 }
 
-#ifndef __CYGWIN__
+#ifdef __MINGW32__
+
+static char *
+find_root_device (const char *dir __attribute__ ((unused)),
+                  dev_t dev __attribute__ ((unused)))
+{
+  return 0;
+}
+
+#elif ! defined(__CYGWIN__)
 
 static char *
 find_root_device (const char *dir, dev_t dev)
util/hostfs.c +6 -1
index b74fbd3..8773c1c 100644
@@ -22,6 +22,7 @@
 #include <grub/disk.h>
 #include <grub/misc.h>
 #include <grub/dl.h>
+#include <grub/util/misc.h>
 
 #include <dirent.h>
 #include <stdio.h>
@@ -95,15 +96,19 @@ grub_hostfs_open (struct grub_file *file, const char *name)
 {
   FILE *f;
 
-  f = fopen (name, "r");
+  f = fopen (name, "rb");
   if (! f)
     return grub_error (GRUB_ERR_BAD_FILENAME,
 		       "can't open `%s'", name);
   file->data = f;
 
+#ifdef __MINGW32__
+  file->size = grub_util_get_disk_size (name);
+#else
   fseeko (f, 0, SEEK_END);
   file->size = ftello (f);
   fseeko (f, 0, SEEK_SET);
+#endif
 
   return GRUB_ERR_NONE;
 }
util/misc.c +100 -0

103 lines not on this page · raw message

#1 lore raw
Quoted 6 lines · Bean

Hi,

This patch add support for mingw, now you can create native executable for windows.

Nice!

Does grub-setup work?

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 65

Quoted 11 lines

...

include/grub/util/misc.h +6 -0
...
+#ifdef __MINGW32__
+
+#include <windows.h>
+
+grub_int64_t fseeko (FILE *fp, grub_int64_t offset, int whence);
+grub_int64_t ftello (FILE *fp);

The mingw runtime provides fseeko64/ftello64(), see /usr/include/mingw/stdio.h

So the following may work:

#ifdef __MINGW32__
#define fseeko fseeko64
#define ftello ftello64
#endif

or use inline functions.

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 68

Quoted 4 lines
include/grub/util/misc.h +3 -0
+void sync (void);
+int asprintf (char **buf, const char *fmt, ...);
+

I would suggest to add AC_CHECK_FUNC(asprintf) to configure. asprintf() is a GNU extension and not part of C99 or POSIX.

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 71

Quoted 4 lines
include/grub/util/misc.h +3 -0
+grub_int64_t grub_util_get_disk_size (char *name);
+
+#define sleep	Sleep

The Sleep() parameter specifies milliseconds.

#define sleep(s) Sleep((s)*1000)
or
inline void sleep(unsigned s) { Sleep(s * 1000); }
or
add sleep() to util/misc.c to avoid global inclusion of the namespace polluter windows.h :-)

Christian

#2 lore raw

Am Sonntag, den 24.08.2008, 14:40 +0200 schrieb Christian Franke:

Quoted 3 lines

I would suggest to add AC_CHECK_FUNC(asprintf) to configure. asprintf() is a GNU extension and not part of C99 or POSIX.

See my [RFC] GNU extention topic.
There's already in configure.ac a check for #define _GNU_SOURCE which is needed for asprintf to be defined.
Thugh I don't know much of cygwin/mingw stuff.

author
#3 lore raw
Quoted 9 lines · Felix Zielcke

Am Sonntag, den 24.08.2008, 14:40 +0200 schrieb Christian Franke:

Quoted 3 lines

I would suggest to add AC_CHECK_FUNC(asprintf) to configure. asprintf() is a GNU extension and not part of C99 or POSIX.

See my [RFC] GNU extention topic.
There's already in configure.ac a check for #define _GNU_SOURCE which is needed for asprintf to be defined.
Thugh I don't know much of cygwin/mingw stuff.

Hi,

I just check, _GNU_SOURCE is defined for mingw, but asprintf is still missing. I guess mingw implementation is not so completed.

Signature · 1 line
Bean
#4 lore raw

Am Sonntag, den 24.08.2008, 23:11 +0800 schrieb Bean:
Hi Bean,

Quoted 4 lines

Hi,

I just check, _GNU_SOURCE is defined for mingw, but asprintf is still missing. I guess mingw implementation is not so completed.

Oh sorry I forgot to tell you that you may need to add a `#include config.h'
For my util/getroot.c patch where I use asprintf I had to do it else it wasn't defined.

author
#5 lore raw
Quoted 5 lines · Felix Zielcke

Am Sonntag, den 24.08.2008, 23:11 +0800 schrieb Bean:
Hi Bean,

Quoted 2 lines

Hi,

Quoted 7 lines
Quoted 2 lines

I just check, _GNU_SOURCE is defined for mingw, but asprintf is still missing. I guess mingw implementation is not so completed.

Oh sorry I forgot to tell you that you may need to add a `#include config.h'
For my util/getroot.c patch where I use asprintf I had to do it else it wasn't defined.

Hi,

The result is the same after adding config.h. BTW, I check the internet, there is a patch to add asprintf to mingw, so this function might be available soon.

Signature · 1 line
Bean
author
#6 lore raw

Hi,

This is the new patch with modification suggested by Christian, it also change grub-mkdevicemap to support mingw device name //./PHYSICALDRIVE0 and fix some warnings.

Signature · 1 line
Bean
config.h.in +3 -0
index e2dd41f..044e81e 100644
@@ -25,6 +25,9 @@
 /* Define if C symbols get an underscore after compilation */
 #undef HAVE_ASM_USCORE
 
+/* Define to 1 if you have the `asprintf' function. */
+#undef HAVE_ASPRINTF
+
 /* Define to 1 if you have the <curses.h> header file. */
 #undef HAVE_CURSES_H
 
configure.ac +1 -1
index f561b8b..73cf23a 100644
@@ -183,7 +183,7 @@ if test "$target_cpu"-"$platform" = i386-pc; then
 fi
 
 # Check for functions.
-AC_CHECK_FUNCS(posix_memalign memalign)
+AC_CHECK_FUNCS(posix_memalign memalign asprintf)
 
 #
 # Check for target programs.
include/grub/symbol.h +1 -1
index e951490..c662c14 100644
@@ -28,7 +28,7 @@
 # define EXT_C(sym)	sym
 #endif
 
-#ifndef __CYGWIN__
+#if ! defined (__CYGWIN__) && ! defined (__MINGW32__)
 #define FUNCTION(x)	.globl EXT_C(x) ; .type EXT_C(x), "function" ; EXT_C(x):
 #define VARIABLE(x)	.globl EXT_C(x) ; .type EXT_C(x), "object" ; EXT_C(x):
 #else
include/grub/util/misc.h +21 -0
index c9a8528..3f145e8 100644
@@ -24,6 +24,9 @@
 #include <setjmp.h>
 #include <unistd.h>
 
+#include <config.h>
+#include <grub/types.h>
+
 #ifdef __NetBSD__
 /* NetBSD uses /boot for its boot block.  */
 # define DEFAULT_DIRECTORY	"/grub"
@@ -55,4 +58,22 @@ void grub_util_write_image_at (const void *img, size_t size, off_t offset,
 			       FILE *out);
 char *grub_util_get_disk_name (int disk, char *name);
 
+#ifndef  HAVE_ASPRINTF
+
+int asprintf (char **buf, const char *fmt, ...);
+
+#endif
+
+#ifdef __MINGW32__
+
+#define fseeko fseeko64
+#define ftello ftello64
+
+void sync (void);
+void sleep(int s);
+
+grub_int64_t grub_util_get_disk_size (char *name);
+
+#endif
+
 #endif /* ! GRUB_UTIL_MISC_HEADER */
util/biosdisk.c +19 -1
index 1137e98..bdede12 100644
@@ -157,7 +157,22 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
   disk->id = drive;
 
   /* Get the size.  */
-#if defined(__linux__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
+  {
+    grub_uint64_t size;
+
+    size = grub_util_get_disk_size (map[drive].device);
+
+    if (size % 512)
+      grub_util_error ("unaligned device size");
+
+    disk->total_sectors = size >> 9;
+
+    grub_util_info ("the size of %s is %llu", name, disk->total_sectors);
+
+    return GRUB_ERR_NONE;
+  }
+#elif defined(__linux__) || defined(__CYGWIN__)
   {
     unsigned long long nr;
     int fd;
@@ -275,6 +290,9 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
 #ifdef O_FSYNC
   flags |= O_FSYNC;
 #endif
+#ifdef O_BINARY
+  flags |= O_BINARY;
+#endif
   
 #ifdef __linux__
   /* Linux has a bug that the disk cache for a whole disk is not consistent
util/getroot.c +10 -1
index 826092b..77b534a 100644
@@ -169,7 +169,16 @@ grub_get_prefix (const char *dir)
   return prefix;
 }
 
-#ifndef __CYGWIN__
+#ifdef __MINGW32__
+
+static char *
+find_root_device (const char *dir __attribute__ ((unused)),
+                  dev_t dev __attribute__ ((unused)))
+{
+  return 0;
+}
+
+#elif ! defined(__CYGWIN__)
 
 static char *
 find_root_device (const char *dir, dev_t dev)
util/grub-mkdevicemap.c +8 -0
index 5b89437..6b8ebd7 100644
@@ -169,6 +169,9 @@ get_floppy_disk_name (char *name, int unit)
 #elif defined(__CYGWIN__)
   /* Cygwin */
   sprintf (name, "/dev/fd%d", unit);
+#elif defined(__MINGW32__)
+  (void) unit;
+  *name = 0;
 #else
 # warning "BIOS floppy drives cannot be guessed in your operating system."
   /* Set NAME to a bogus string.  */
@@ -214,6 +217,8 @@ get_ide_disk_name (char *name, int unit)
   /* Cygwin emulates all disks as /dev/sdX.  */
   (void) unit;
   *name = 0;
+#elif defined(__MINGW32__)
+  sprintf (name, "//./PHYSICALDRIVE%d", unit);
 #else
 # warning "BIOS IDE drives cannot be guessed in your operating system."
   /* Set NAME to a bogus string.  */
@@ -258,6 +263,9 @@ get_scsi_disk_name (char *name, int unit)
 #elif defined(__CYGWIN__)
   /* Cygwin emulates all disks as /dev/sdX.  */
   sprintf (name, "/dev/sd%c", unit + 'a');
+#elif defined(__MINGW32__)
+  (void) unit;
+  *name = 0;
 #else
 # warning "BIOS SCSI drives cannot be guessed in your operating system."
   /* Set NAME to a bogus string.  */
util/hostfs.c +6 -1
index b74fbd3..8773c1c 100644
@@ -22,6 +22,7 @@
 #include <grub/disk.h>
 #include <grub/misc.h>
 #include <grub/dl.h>
+#include <grub/util/misc.h>
 
 #include <dirent.h>
 #include <stdio.h>
@@ -95,15 +96,19 @@ grub_hostfs_open (struct grub_file *file, const char *name)
 {
   FILE *f;
 
-  f = fopen (name, "r");
+  f = fopen (name, "rb");
   if (! f)
     return grub_error (GRUB_ERR_BAD_FILENAME,
 		       "can't open `%s'", name);
   file->data = f;
 
+#ifdef __MINGW32__
+  file->size = grub_util_get_disk_size (name);
+#else
   fseeko (f, 0, SEEK_END);
   file->size = ftello (f);
   fseeko (f, 0, SEEK_SET);
+#endif
 
   return GRUB_ERR_NONE;
 }
util/misc.c +84 -0

93 lines not on this page · raw message

author
#7 lore raw
In reply to Christian Franke
Quoted 4 lines · Christian Franke
Quoted 3 lines · Bean

Hi,

Quoted 8 lines
Quoted 4 lines

This patch add support for mingw, now you can create native executable for windows.

Nice!

Does grub-setup work?

Yes, it works, although mingw can't use device names like /dev/sda, but it can use windows special name //./PHYSICALDRIVE0.

Quoted 17 lines

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 65

Quoted 13 lines

...

include/grub/util/misc.h +6 -0
...
+#ifdef __MINGW32__
+
+#include <windows.h>
+
+grub_int64_t fseeko (FILE *fp, grub_int64_t offset, int whence);
+grub_int64_t ftello (FILE *fp);

The mingw runtime provides fseeko64/ftello64(), see /usr/include/mingw/stdio.h

Quoted 8 lines

So the following may work:

#ifdef __MINGW32__
#define fseeko fseeko64
#define ftello ftello64
#endif

or use inline functions.

Oh, thanks for the tip.

Quoted 10 lines

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 68

Quoted 6 lines
include/grub/util/misc.h +3 -0
+void sync (void);
+int asprintf (char **buf, const char *fmt, ...);
+

I would suggest to add AC_CHECK_FUNC(asprintf) to configure. asprintf() is a GNU extension and not part of C99 or POSIX.

Good point.

Quoted 8 lines

include/grub/util/misc.h void grub_util_write_image_at (const void *img, size_t size, off_t offset, · line 71

Quoted 5 lines
include/grub/util/misc.h +3 -0
+grub_int64_t grub_util_get_disk_size (char *name);
+
+#define sleep  Sleep

The Sleep() parameter specifies milliseconds.

Quoted 6 lines

#define sleep(s) Sleep((s)*1000)
or
inline void sleep(unsigned s) { Sleep(s * 1000); }
or
add sleep() to util/misc.c to avoid global inclusion of the namespace polluter windows.h :-)

Yes, perhaps I should put it in util/misc.c.

Signature · 1 line
Bean
#8 lore raw
Quoted 2 lines · Bean

Yes, it works, although mingw can't use device names like /dev/sda, but it can use windows special name //./PHYSICALDRIVE0.

That should be with back slashes ('\'). Or is there some hack somewhere to support that?

author
#9 lore raw
Quoted 6 lines · Vesa Jääskeläinen
Quoted 2 lines · Bean

Yes, it works, although mingw can't use device names like /dev/sda, but it can use windows special name //./PHYSICALDRIVE0.

That should be with back slashes ('\'). Or is there some hack somewhere to support that?

Hi,

Mingw would translate / to \, so both are ok. But using / is more clear, as \ is the escape character in cygwin shell, so we need to use two of them, \\\\.\\PHYSICALDRIVE0.

Signature · 1 line
Bean
#10 lore raw
Quoted 4 lines · Bean
Quoted 3 lines · Vesa Jääskeläinen
Quoted 2 lines · Bean

Yes, it works, although mingw can't use device names like /dev/sda, but it can use windows special name //./PHYSICALDRIVE0.

Quoted 8 lines
Quoted 2 lines

That should be with back slashes ('\'). Or is there some hack somewhere to support that?

Hi,

Mingw would translate / to \, so both are ok. But using / is more clear, as \ is the escape character in cygwin shell, so we need to use two of them, \\\\.\\PHYSICALDRIVE0.

In cygwin shell... yes... but mingw programs are not meant to be run under that. Mingw programs are native windows programs.

#11 lore raw
Quoted 14 lines · Vesa Jääskeläinen
Quoted 12 lines · Bean
Quoted 8 lines · Vesa Jääskeläinen
Quoted 3 lines · Bean

Yes, it works, although mingw can't use device names like /dev/sda, but it can use windows special name //./PHYSICALDRIVE0.

That should be with back slashes ('\'). Or is there some hack somewhere to support that?

Hi,

Quoted 10 lines
Quoted 4 lines

Mingw would translate / to \, so both are ok. But using / is more clear, as \ is the escape character in cygwin shell, so we need to use two of them, \\\\.\\PHYSICALDRIVE0.

In cygwin shell... yes... but mingw programs are not meant to be run under that. Mingw programs are native windows programs.

Unlike Cygwin, Mingw does not provide an extra library layer. Most standard library functions directly link to msvcrt.dll.

The Win32 API (like the DOS API) itself accepts both / and \, only the user level tools usually don't. This is likely because in the early (CP/M->DOS) days, someone decided to use '/' as the option char. Later, when path names are 'borrowed' from Unix, it was too late :-)

In fact, all these work to open the first disk:
h = CreateFile("\\\\.\\PhysicalDrive0", ...) h = CreateFile("\\\\./PhysicalDrive0", ...) h = CreateFile("//.\\PhysicalDrive0", ...) h = CreateFile("//./PhysicalDrive0", ...)

Christian

author
#12 lore raw

Hi,

Committed.

BTW, I add a test in configure.ac, so that it can also work with msys/mingw environment.

Signature · 1 line
Bean