File grub2-setup-try-fs-embed-if-mbr-gap-too-small.patch of Package grub2

Index: grub-2.02~beta2/util/setup.c
===================================================================
--- grub-2.02~beta2.orig/util/setup.c
+++ grub-2.02~beta2/util/setup.c
@@ -501,8 +501,42 @@ SETUP (const char *dir,
       err = grub_util_ldm_embed (dest_dev->disk, &nsec, maxsec,
 				 GRUB_EMBED_PCBIOS, &sectors);
     else if (ctx.dest_partmap)
-      err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
-				     GRUB_EMBED_PCBIOS, &sectors);
+      {
+        err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
+				       GRUB_EMBED_PCBIOS, &sectors);
+#ifdef GRUB_SETUP_BIOS
+        if (err == GRUB_ERR_OUT_OF_RANGE
+            && dest_dev->disk->id == root_dev->disk->id
+            && dest_dev->disk->dev->id == root_dev->disk->dev->id)
+          {
+            grub_fs_t root_fs;
+
+            root_fs = grub_fs_probe (root_dev);
+            if (root_fs && root_fs->embed)
+              {
+                grub_disk_addr_t *fs_sectors;
+                unsigned int fs_nsec;
+
+                fs_sectors = NULL;
+                fs_nsec = core_sectors;
+                err = root_fs->embed (root_dev, &fs_nsec, maxsec,
+                                      GRUB_EMBED_PCBIOS, &fs_sectors);
+                if (!err && fs_nsec >= core_sectors)
+                  {
+                    sectors = fs_sectors;
+                    nsec = fs_nsec;
+                    ctx.container = root_dev->disk->partition;
+                    core_dev = root_dev;
+                  }
+                else
+                  {
+                    if (fs_sectors)
+                      grub_free (fs_sectors);
+                  }
+              }
+          }
+#endif
+      }
     else
       err = fs->embed (dest_dev, &nsec, maxsec,
 		       GRUB_EMBED_PCBIOS, &sectors);
@@ -584,7 +618,7 @@ SETUP (const char *dir,
 
     /* Write the core image onto the disk.  */
     for (i = 0; i < nsec; i++)
-      grub_disk_write (dest_dev->disk, sectors[i], 0,
+      grub_disk_write (core_dev->disk, sectors[i], 0,
 		       GRUB_DISK_SECTOR_SIZE,
 		       core_img + i * GRUB_DISK_SECTOR_SIZE);