All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Mingw support for grub2
@ 2008-08-23 14:11 Bean
  2008-08-24 12:40 ` Christian Franke
  0 siblings, 1 reply; 13+ messages in thread
From: Bean @ 2008-08-23 14:11 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 1095 bytes --]

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  <bean123ch@gmail.com>

	* 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.

-- 
Bean

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: mingw.diff --]
[-- Type: text/x-diff; name=mingw.diff, Size: 5611 bytes --]

diff --git a/include/grub/symbol.h b/include/grub/symbol.h
index e951490..c662c14 100644
--- a/include/grub/symbol.h
+++ b/include/grub/symbol.h
@@ -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
diff --git a/include/grub/util/misc.h b/include/grub/util/misc.h
index c9a8528..4d2ce8f 100644
--- a/include/grub/util/misc.h
+++ b/include/grub/util/misc.h
@@ -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 */
diff --git a/util/biosdisk.c b/util/biosdisk.c
index 1137e98..bdede12 100644
--- a/util/biosdisk.c
+++ b/util/biosdisk.c
@@ -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
diff --git a/util/getroot.c b/util/getroot.c
index c8d31ed..5fcf2e5 100644
--- a/util/getroot.c
+++ b/util/getroot.c
@@ -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)
diff --git a/util/hostfs.c b/util/hostfs.c
index b74fbd3..8773c1c 100644
--- a/util/hostfs.c
+++ b/util/hostfs.c
@@ -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;
 }
diff --git a/util/misc.c b/util/misc.c
index 7d877cc..6d3aae4 100644
--- a/util/misc.c
+++ b/util/misc.c
@@ -313,3 +313,103 @@ grub_arch_sync_caches (void *address __attribute__ ((unused)),
 		       grub_size_t len __attribute__ ((unused)))
 {
 }
+
+#ifdef __MINGW32__
+
+#include <winioctl.h>
+#include <stdarg.h>
+
+grub_int64_t
+fseeko (FILE *fp, grub_int64_t offset, int whence)
+{
+  fpos_t pos;
+
+  if (whence == SEEK_CUR)
+    {
+      fgetpos (fp, &pos);
+      pos += (fpos_t) offset;
+    }
+  else if (whence == SEEK_END)
+    {
+      pos = (fpos_t) (_filelengthi64 (fileno (fp)) + offset);
+    }
+  else if (whence == SEEK_SET)
+    pos = (fpos_t) offset;
+
+  return fsetpos(fp, &pos);
+}
+
+grub_int64_t
+ftello (FILE *fp)
+{
+  fpos_t pos;
+
+  return (fgetpos(fp, &pos)) ? (grub_int64_t) -1LL : (grub_int64_t) pos;
+}
+
+void sync (void)
+{
+}
+
+int
+asprintf (char **buf, const char *fmt, ...)
+{
+  int status;
+  va_list ap;
+
+  /* Should be large enough.  */
+  *buf = xmalloc (512);
+
+  va_start (ap, fmt);
+  status = vsprintf (*buf, fmt, ap);
+  va_end (ap);
+
+  return status;
+}
+
+grub_int64_t
+grub_util_get_disk_size (char *name)
+{
+  char new_name[strlen (name) + 1], *p;
+  HANDLE hd;
+  grub_int64_t size = -1LL;
+
+  strcpy (new_name, name);
+  for (p = new_name; *p; p++)
+    if (*p == '/')
+      *p = '\\';
+
+  hd = CreateFile (new_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
+                   0, OPEN_EXISTING, 0, 0);
+
+  if (hd == INVALID_HANDLE_VALUE)
+    return size;
+
+  if (! strncmp (new_name, "\\\\.\\", 4))
+    {
+      DWORD nr;
+      DISK_GEOMETRY g;
+
+      if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
+                             0, 0, &g, sizeof (g), &nr, 0))
+        goto fail;
+
+      size = g.Cylinders.QuadPart;
+      size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
+    }
+  else
+    {
+      LARGE_INTEGER s;
+
+      s.LowPart = GetFileSize (hd, &s.HighPart);
+      size = s.QuadPart;
+    }
+
+fail:
+
+  CloseHandle (hd);
+
+  return size;
+}
+
+#endif

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-23 14:11 [PATCH] Mingw support for grub2 Bean
@ 2008-08-24 12:40 ` Christian Franke
  2008-08-24 12:48   ` Felix Zielcke
  2008-08-24 14:12   ` Bean
  0 siblings, 2 replies; 13+ messages in thread
From: Christian Franke @ 2008-08-24 12:40 UTC (permalink / raw)
  To: The development of GRUB 2

Bean wrote:
> Hi,
>
> This patch add support for mingw, now you can create native executable
> for windows.
>
>   

Nice!

Does grub-setup work?


> ...
> --- a/include/grub/util/misc.h
> +++ b/include/grub/util/misc.h
> ...
> +#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.


> +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.


> +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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 12:40 ` Christian Franke
@ 2008-08-24 12:48   ` Felix Zielcke
  2008-08-24 15:11     ` Bean
  2008-08-24 14:12   ` Bean
  1 sibling, 1 reply; 13+ messages in thread
From: Felix Zielcke @ 2008-08-24 12:48 UTC (permalink / raw)
  To: The development of GRUB 2

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

> 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.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 12:40 ` Christian Franke
  2008-08-24 12:48   ` Felix Zielcke
@ 2008-08-24 14:12   ` Bean
  2008-08-24 16:04     ` Vesa Jääskeläinen
  1 sibling, 1 reply; 13+ messages in thread
From: Bean @ 2008-08-24 14:12 UTC (permalink / raw)
  To: The development of GRUB 2

On Sun, Aug 24, 2008 at 8:40 PM, Christian Franke
<Christian.Franke@t-online.de> wrote:
> Bean wrote:
>>
>> Hi,
>>
>> 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.

>
>
>> ...
>> --- a/include/grub/util/misc.h
>> +++ b/include/grub/util/misc.h
>> ...
>> +#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.

Oh, thanks for the tip.


>
>
>> +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.

>
>> +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 :-)

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

-- 
Bean



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 12:48   ` Felix Zielcke
@ 2008-08-24 15:11     ` Bean
  2008-08-24 15:25       ` Felix Zielcke
  0 siblings, 1 reply; 13+ messages in thread
From: Bean @ 2008-08-24 15:11 UTC (permalink / raw)
  To: The development of GRUB 2

On Sun, Aug 24, 2008 at 8:48 PM, Felix Zielcke <fzielcke@z-51.de> wrote:
> Am Sonntag, den 24.08.2008, 14:40 +0200 schrieb Christian Franke:
>
>> 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.

-- 
Bean



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 15:11     ` Bean
@ 2008-08-24 15:25       ` Felix Zielcke
  2008-08-24 15:31         ` Bean
  0 siblings, 1 reply; 13+ messages in thread
From: Felix Zielcke @ 2008-08-24 15:25 UTC (permalink / raw)
  To: The development of GRUB 2

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

> 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.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 15:25       ` Felix Zielcke
@ 2008-08-24 15:31         ` Bean
  2008-08-24 15:49           ` Bean
  0 siblings, 1 reply; 13+ messages in thread
From: Bean @ 2008-08-24 15:31 UTC (permalink / raw)
  To: The development of GRUB 2

On Sun, Aug 24, 2008 at 11:25 PM, Felix Zielcke <fzielcke@z-51.de> wrote:
> Am Sonntag, den 24.08.2008, 23:11 +0800 schrieb Bean:
> Hi Bean,
>
>> 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.

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.

-- 
Bean



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 15:31         ` Bean
@ 2008-08-24 15:49           ` Bean
  0 siblings, 0 replies; 13+ messages in thread
From: Bean @ 2008-08-24 15:49 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 182 bytes --]

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.

-- 
Bean

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: mingw_2.diff --]
[-- Type: text/x-diff; name=mingw_2.diff, Size: 7300 bytes --]

diff --git a/config.h.in b/config.h.in
index e2dd41f..044e81e 100644
--- a/config.h.in
+++ b/config.h.in
@@ -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
 
diff --git a/configure.ac b/configure.ac
index f561b8b..73cf23a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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.
diff --git a/include/grub/symbol.h b/include/grub/symbol.h
index e951490..c662c14 100644
--- a/include/grub/symbol.h
+++ b/include/grub/symbol.h
@@ -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
diff --git a/include/grub/util/misc.h b/include/grub/util/misc.h
index c9a8528..3f145e8 100644
--- a/include/grub/util/misc.h
+++ b/include/grub/util/misc.h
@@ -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 */
diff --git a/util/biosdisk.c b/util/biosdisk.c
index 1137e98..bdede12 100644
--- a/util/biosdisk.c
+++ b/util/biosdisk.c
@@ -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
diff --git a/util/getroot.c b/util/getroot.c
index 826092b..77b534a 100644
--- a/util/getroot.c
+++ b/util/getroot.c
@@ -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)
diff --git a/util/grub-mkdevicemap.c b/util/grub-mkdevicemap.c
index 5b89437..6b8ebd7 100644
--- a/util/grub-mkdevicemap.c
+++ b/util/grub-mkdevicemap.c
@@ -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.  */
diff --git a/util/hostfs.c b/util/hostfs.c
index b74fbd3..8773c1c 100644
--- a/util/hostfs.c
+++ b/util/hostfs.c
@@ -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;
 }
diff --git a/util/misc.c b/util/misc.c
index 7d877cc..4f20ff4 100644
--- a/util/misc.c
+++ b/util/misc.c
@@ -259,6 +259,8 @@ grub_memalign (grub_size_t align, grub_size_t size)
 #elif defined(HAVE_MEMALIGN)
   p = memalign (align, size);
 #else
+  (void) align;
+  (void) size;
   grub_util_error ("grub_memalign is not supported");
 #endif
   
@@ -313,3 +315,85 @@ grub_arch_sync_caches (void *address __attribute__ ((unused)),
 		       grub_size_t len __attribute__ ((unused)))
 {
 }
+
+#ifndef  HAVE_ASPRINTF
+
+int
+asprintf (char **buf, const char *fmt, ...)
+{
+  int status;
+  va_list ap;
+
+  /* Should be large enough.  */
+  *buf = xmalloc (512);
+
+  va_start (ap, fmt);
+  status = vsprintf (*buf, fmt, ap);
+  va_end (ap);
+
+  return status;
+}
+
+#endif
+
+#ifdef __MINGW32__
+
+#include <windows.h>
+#include <winioctl.h>
+#include <stdarg.h>
+
+void sync (void)
+{
+}
+
+void sleep (int s)
+{
+  Sleep (s * 1000);
+}
+
+grub_int64_t
+grub_util_get_disk_size (char *name)
+{
+  char new_name[strlen (name) + 1], *p;
+  HANDLE hd;
+  grub_int64_t size = -1LL;
+
+  strcpy (new_name, name);
+  for (p = new_name; *p; p++)
+    if (*p == '/')
+      *p = '\\';
+
+  hd = CreateFile (new_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
+                   0, OPEN_EXISTING, 0, 0);
+
+  if (hd == INVALID_HANDLE_VALUE)
+    return size;
+
+  if (! strncmp (new_name, "\\\\.\\", 4))
+    {
+      DWORD nr;
+      DISK_GEOMETRY g;
+
+      if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
+                             0, 0, &g, sizeof (g), &nr, 0))
+        goto fail;
+
+      size = g.Cylinders.QuadPart;
+      size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
+    }
+  else
+    {
+      LARGE_INTEGER s;
+
+      s.LowPart = GetFileSize (hd, &s.HighPart);
+      size = s.QuadPart;
+    }
+
+fail:
+
+  CloseHandle (hd);
+
+  return size;
+}
+
+#endif

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 14:12   ` Bean
@ 2008-08-24 16:04     ` Vesa Jääskeläinen
  2008-08-24 16:14       ` Bean
  0 siblings, 1 reply; 13+ messages in thread
From: Vesa Jääskeläinen @ 2008-08-24 16:04 UTC (permalink / raw)
  To: The development of GRUB 2

Bean wrote:
> 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?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 16:04     ` Vesa Jääskeläinen
@ 2008-08-24 16:14       ` Bean
  2008-08-24 16:16         ` Vesa Jääskeläinen
  0 siblings, 1 reply; 13+ messages in thread
From: Bean @ 2008-08-24 16:14 UTC (permalink / raw)
  To: The development of GRUB 2

On Mon, Aug 25, 2008 at 12:04 AM, Vesa Jääskeläinen <chaac@nic.fi> wrote:
> Bean wrote:
>> 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.

-- 
Bean



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 16:14       ` Bean
@ 2008-08-24 16:16         ` Vesa Jääskeläinen
  2008-08-24 16:42           ` Christian Franke
  0 siblings, 1 reply; 13+ messages in thread
From: Vesa Jääskeläinen @ 2008-08-24 16:16 UTC (permalink / raw)
  To: The development of GRUB 2

Bean wrote:
> On Mon, Aug 25, 2008 at 12:04 AM, Vesa Jääskeläinen <chaac@nic.fi> wrote:
>> Bean wrote:
>>> 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.

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



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 16:16         ` Vesa Jääskeläinen
@ 2008-08-24 16:42           ` Christian Franke
  2008-08-29 19:59             ` Bean
  0 siblings, 1 reply; 13+ messages in thread
From: Christian Franke @ 2008-08-24 16:42 UTC (permalink / raw)
  To: The development of GRUB 2

Vesa Jääskeläinen wrote:
> Bean wrote:
>   
>> On Mon, Aug 25, 2008 at 12:04 AM, Vesa Jääskeläinen <chaac@nic.fi> wrote:
>>     
>>> Bean wrote:
>>>       
>>>> 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.
>>     
>
> 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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Mingw support for grub2
  2008-08-24 16:42           ` Christian Franke
@ 2008-08-29 19:59             ` Bean
  0 siblings, 0 replies; 13+ messages in thread
From: Bean @ 2008-08-29 19:59 UTC (permalink / raw)
  To: The development of GRUB 2

Hi,

Committed.

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

-- 
Bean



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2008-08-29 19:59 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-23 14:11 [PATCH] Mingw support for grub2 Bean
2008-08-24 12:40 ` Christian Franke
2008-08-24 12:48   ` Felix Zielcke
2008-08-24 15:11     ` Bean
2008-08-24 15:25       ` Felix Zielcke
2008-08-24 15:31         ` Bean
2008-08-24 15:49           ` Bean
2008-08-24 14:12   ` Bean
2008-08-24 16:04     ` Vesa Jääskeläinen
2008-08-24 16:14       ` Bean
2008-08-24 16:16         ` Vesa Jääskeläinen
2008-08-24 16:42           ` Christian Franke
2008-08-29 19:59             ` Bean

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.