Bug#923253: grub-pc: Calculates wrong partition offset for EBR partitions on FreeBSD

James Clarke jrtc27 at debian.org
Mon Feb 25 14:29:16 GMT 2019


Package: grub-pc
Version: ...
Tags: upstream patch

Hi,
On FreeBSD, grub uses the "start" rather than the "offset" config
property to calculate the starting sector for partitions, which gives
the wrong value for EBR partitions:

> root at lemon:/home/jrtc27/src/grub2/grub2-2.02+dfsg1# geom part list
> (output trimmed and annotated)
> Geom name: ada0
> scheme: MBR
> Providers:
> 1. Name: ada0s1
>    offset: 1048576 # ie 2048 sectors
>    start: 2048
> 2. Name: ada0s2
>    offset: 64000883712 # ie 125001726 sectors
>    start: 125001726
>
> Geom name: ada0s2
> scheme: EBR
> Providers:
> 1. Name: ada0s5
>    offset: 1024 # ie 2 sectors; add 125001726 from ada0s2 to get 125001728
>    start: 0
> 2. Name: ada0s6
>    offset: 183628727296 # ie 358649858 sectors; add 125001726 from ada0s2 to get 483651584
>    start: 358647810 # add 125001726 from ada0s2 to get 483649536
> root at lemon:~# fdisk -l /dev/ada0
> Disk /dev/ada0: 238.5 GiB, 256060514304 bytes, 500118192 sectors
> Units: sectors of 1 * 512 = 512 bytes
> Sector size (logical/physical): 512 bytes / 512 bytes
> I/O size (minimum/optimal): 512 bytes / 512 bytes
> Disklabel type: dos
> Disk identifier: 0x20b8cc40
>
> Device      Boot     Start       End   Sectors   Size Id Type
> /dev/ada0p1 *         2048 124999679 124997632  59.6G 83 Linux
> /dev/ada0p2      125001726 500117503 375115778 178.9G  5 Extended
> /dev/ada0p5      125001728 483649535 358647808   171G a5 FreeBSD
> /dev/ada0p6      483651584 500117503  16465920   7.9G 82 Linux swap / Solaris

This clearly shows that the "start" field is wrong and "offset" should
be used instead. The attached patch does this and has been tested on the
above system (consider it Signed-off-by: James Clarke
<jrtc27 at jrtc27.com> if forwarding upstream).

Thanks,
James
-------------- next part --------------
Description: osdep/freebsd: Fix partition calculation for EBR entries
 For EBR partitions, "start" is the relative starting sector of the EBR header
 itself, whereas "offset" is the relative starting byte of the partition's
 contents, excluding the EBR header and any padding. Thus we must use "offset",
 and divide by the sector size to convert to sectors.
Author: James Clarke <jrtc27 at jrtc27.com>
Last-Update: 2019-02-25
---
--- a/grub-core/osdep/freebsd/getroot.c
+++ b/grub-core/osdep/freebsd/getroot.c
@@ -338,8 +338,9 @@ grub_util_follow_gpart_up (const char *n
 	    grub_util_follow_gpart_up (name_tmp, &off, name_out);
 	    free (name_tmp);
 	    LIST_FOREACH (config, &provider->lg_config, lg_config)
-	      if (strcasecmp (config->lg_name, "start") == 0)
-		off += strtoull (config->lg_val, 0, 10);
+	      if (strcasecmp (config->lg_name, "offset") == 0)
+		off += strtoull (config->lg_val, 0, 10)
+		       / provider->lg_sectorsize;
 	    if (off_out)
 	      *off_out = off;
 	    return;


More information about the Pkg-grub-devel mailing list