From: Andrey Shuvikov <mr_hyro@yahoo.com>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: stat for FreeBSD
Date: Thu, 12 Mar 2009 09:24:46 -0700 (PDT) [thread overview]
Message-ID: <275187.21714.qm@web42104.mail.mud.yahoo.com> (raw)
In-Reply-To: <20090207193905.GB988@thorin>
[-- Attachment #1: Type: text/plain, Size: 1198 bytes --]
--- On Sat, 2/7/09, Robert Millan <rmh@aybabtu.com> wrote:
> From: Robert Millan <rmh@aybabtu.com>
> Subject: Re: stat for FreeBSD
> To: "The development of GRUB 2" <grub-devel@gnu.org>
> Date: Saturday, February 7, 2009, 2:39 PM
> On Mon, Dec 15, 2008 at 09:24:41AM -0800, Andrey Shuvikov
> wrote:
> > I don't have much experience developing under
> FreeBSD but this call taken from the fdisk source code seems
> to work (see attachment also):
> >
> > error = ioctl(fd, DIOCGMEDIASIZE, &size);
>
> This seems fine. Could you provide a tested patch?
>
> --
> Robert Millan
>
> The DRM opt-in fallacy: "Your data belongs to us. We
> will decide when (and
> how) you may access your data; but nobody's
> threatening your freedom: we
> still allow you to remove your data and not access it at
> all."
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> http://lists.gnu.org/mailman/listinfo/grub-devel
Here is the patch to make grub-probe working on FreeBSD. It includes this
ioctl call, FreeBSD device names parsing, and accounts for the fact that disk drives are character devices under FreeBSD.
Regards,
Andrey Shuvikov
[-- Attachment #2: grub_probe_freebsd.patch --]
[-- Type: application/octet-stream, Size: 4778 bytes --]
Index: include/grub/util/getroot.h
===================================================================
--- include/grub/util/getroot.h (revision 2028)
+++ include/grub/util/getroot.h (working copy)
@@ -30,5 +30,6 @@
int grub_util_get_dev_abstraction (const char *os_dev);
char *grub_util_get_grub_dev (const char *os_dev);
const char *grub_util_check_block_device (const char *blk_dev);
+const char *grub_util_check_char_device (const char *blk_dev);
#endif /* ! GRUB_UTIL_GETROOT_HEADER */
Index: util/grub-probe.c
===================================================================
--- util/grub-probe.c (revision 2028)
+++ util/grub-probe.c (working copy)
@@ -112,8 +112,13 @@
if (path == NULL)
{
+#if defined(__FreeBSD__)
+ if (! grub_util_check_char_device (device_name))
+ grub_util_error ("%s is not a character device.\n", device_name);
+#else
if (! grub_util_check_block_device (device_name))
grub_util_error ("%s is not a block device.\n", device_name);
+#endif
}
else
device_name = grub_guess_root_device (path);
Index: util/getroot.c
===================================================================
--- util/getroot.c (revision 2028)
+++ util/getroot.c (working copy)
@@ -238,7 +238,11 @@
}
}
+#ifdef __FreeBSD__
+ if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
+#else
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
+#endif
{
#ifdef __linux__
/* Skip device names like /dev/dm-0, which are short-hand aliases
@@ -519,3 +523,18 @@
else
return 0;
}
+
+const char *
+grub_util_check_char_device (const char *blk_dev)
+{
+ struct stat st;
+
+ if (stat (blk_dev, &st) < 0)
+ grub_util_error ("Cannot stat `%s'", blk_dev);
+
+ if (S_ISCHR (st.st_mode))
+ return (blk_dev);
+ else
+ return 0;
+}
+
Index: util/hostdisk.c
===================================================================
--- util/hostdisk.c (revision 2028)
+++ util/hostdisk.c (working copy)
@@ -86,6 +86,10 @@
# define FLOPPY_MAJOR 2
#endif
+#ifdef __FreeBSD__
+# include <sys/disk.h> /* DIOCGMEDIASIZE */
+#endif
+
struct
{
char *drive;
@@ -179,7 +183,7 @@
return GRUB_ERR_NONE;
}
-#elif defined(__linux__) || defined(__CYGWIN__)
+#elif defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__)
{
unsigned long long nr;
int fd;
@@ -188,13 +192,21 @@
if (fd == -1)
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s' while attempting to get disk size", map[drive].device);
+#if defined(__FreeBSD__)
+ if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
+#else
if (fstat (fd, &st) < 0 || ! S_ISBLK (st.st_mode))
+#endif
{
close (fd);
goto fail;
}
+#if defined(__FreeBSD__)
+ if (ioctl (fd, DIOCGMEDIASIZE, &nr))
+#else
if (ioctl (fd, BLKGETSIZE64, &nr))
+#endif
{
close (fd);
goto fail;
@@ -741,6 +753,22 @@
path[8] = 0;
return path;
+#elif defined(__FreeBSD__)
+ char *path = xstrdup (os_dev);
+ if (strncmp ("/dev/", path, 5) == 0)
+ {
+ char *p;
+ for (p = path + 5; *p; ++p)
+ if (grub_isdigit(*p))
+ {
+ p = strchr (p, 's');
+ if (p)
+ *p = '\0';
+ break;
+ }
+ }
+ return path;
+
#else
# warning "The function `convert_system_partition_to_system_disk' might not work on your OS correctly."
return xstrdup (os_dev);
@@ -788,7 +816,11 @@
return 0;
}
+#if defined(__FreeBSD__)
+ if (! S_ISCHR (st.st_mode))
+#else
if (! S_ISBLK (st.st_mode))
+#endif
return make_device_name (drive, -1, -1);
#if defined(__linux__) || defined(__CYGWIN__)
@@ -933,6 +965,40 @@
return make_device_name (drive, dos_part, bsd_part);
}
+#elif defined(__FreeBSD__)
+ /* FreeBSD uses "/dev/[a-z]+[0-9]+(s[0-9]+[a-z]?)?". */
+ {
+ int dos_part = -1;
+ int bsd_part = -1;
+
+ if (strncmp ("/dev/", os_dev, 5) == 0)
+ {
+ char *p, *q;
+ long int n;
+
+ for (p = os_dev + 5; *p; ++p)
+ if (grub_isdigit(*p))
+ {
+ p = strchr (p, 's');
+ if (p)
+ {
+ p++;
+ n = strtol (p, &q, 10);
+ if (p != q && n != LONG_MIN && n != LONG_MAX)
+ {
+ dos_part = (int) n - 1;
+
+ if (*q >= 'a' && *q <= 'g')
+ bsd_part = *q - 'a';
+ }
+ }
+ break;
+ }
+ }
+
+ return make_device_name (drive, dos_part, bsd_part);
+ }
+
#else
# warning "The function `grub_util_biosdisk_get_grub_dev' might not work on your OS correctly."
return make_device_name (drive, -1, -1);
next prev parent reply other threads:[~2009-03-12 16:24 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-09 23:26 stat for FreeBSD Andrey Shuvikov
2008-12-14 1:12 ` Robert Millan
2008-12-14 14:26 ` Javier Martín
2008-12-15 17:24 ` Andrey Shuvikov
2009-02-07 19:39 ` Robert Millan
2009-03-12 16:24 ` Andrey Shuvikov [this message]
-- strict thread matches above, loose matches on Subject: below --
2009-04-06 15:48 Andrey Shuvikov
2009-04-07 0:35 ` Yoshinori K. Okuji
2009-04-08 14:32 Andrey Shuvikov
2009-04-09 23:31 ` Yoshinori K. Okuji
2009-04-10 3:51 Andrey Shuvikov
2009-04-10 7:34 ` Felix Zielcke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=275187.21714.qm@web42104.mail.mud.yahoo.com \
--to=mr_hyro@yahoo.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.