6b2dd0f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
752ceb1
From: Nicholas Vinson <nvinson234@gmail.com>
752ceb1
Date: Mon, 16 Apr 2018 22:36:25 -0700
752ceb1
Subject: [PATCH] grub-probe: Add PARTUUID detection support
752ceb1
752ceb1
Add PARTUUID detection support grub-probe for MBR and GPT partition schemes.
752ceb1
752ceb1
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
752ceb1
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
752ceb1
---
752ceb1
 util/grub-probe.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
752ceb1
 1 file changed, 48 insertions(+)
752ceb1
752ceb1
diff --git a/util/grub-probe.c b/util/grub-probe.c
752ceb1
index 21cb80fbe15..e45dbf9e049 100644
752ceb1
--- a/util/grub-probe.c
752ceb1
+++ b/util/grub-probe.c
752ceb1
@@ -28,6 +28,7 @@
752ceb1
 #include <grub/partition.h>
752ceb1
 #include <grub/msdos_partition.h>
752ceb1
 #include <grub/gpt_partition.h>
752ceb1
+#include <grub/i386/pc/boot.h>
752ceb1
 #include <grub/emu/hostdisk.h>
752ceb1
 #include <grub/emu/getroot.h>
752ceb1
 #include <grub/term.h>
752ceb1
@@ -62,6 +63,7 @@ enum {
752ceb1
   PRINT_DRIVE,
752ceb1
   PRINT_DEVICE,
752ceb1
   PRINT_PARTMAP,
752ceb1
+  PRINT_PARTUUID,
752ceb1
   PRINT_ABSTRACTION,
752ceb1
   PRINT_CRYPTODISK_UUID,
752ceb1
   PRINT_HINT_STR,
752ceb1
@@ -85,6 +87,7 @@ static const char *targets[] =
752ceb1
     [PRINT_DRIVE]              = "drive",
752ceb1
     [PRINT_DEVICE]             = "device",
752ceb1
     [PRINT_PARTMAP]            = "partmap",
752ceb1
+    [PRINT_PARTUUID]           = "partuuid",
752ceb1
     [PRINT_ABSTRACTION]        = "abstraction",
752ceb1
     [PRINT_CRYPTODISK_UUID]    = "cryptodisk_uuid",
752ceb1
     [PRINT_HINT_STR]           = "hints_string",
752ceb1
@@ -181,6 +184,45 @@ probe_partmap (grub_disk_t disk, char delim)
752ceb1
     }
752ceb1
 }
752ceb1
 
752ceb1
+static void
752ceb1
+probe_partuuid (grub_disk_t disk, char delim)
752ceb1
+{
752ceb1
+  grub_partition_t p = disk->partition;
752ceb1
+
752ceb1
+  /*
752ceb1
+   * Nested partitions not supported for now.
752ceb1
+   * Non-nested partitions must have disk->partition->parent == NULL
752ceb1
+   */
752ceb1
+  if (p && p->parent == NULL)
752ceb1
+    {
752ceb1
+      disk->partition = p->parent;
752ceb1
+
752ceb1
+      if (strcmp(p->partmap->name, "msdos") == 0)
752ceb1
+	{
752ceb1
+	    /*
752ceb1
+	     * The partition GUID for MSDOS is the partition number (starting
752ceb1
+	     * with 1) prepended with the NT disk signature.
752ceb1
+	     */
752ceb1
+	    grub_uint32_t nt_disk_sig;
752ceb1
+
752ceb1
+	    if (grub_disk_read (disk, 0, GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
752ceb1
+				sizeof(nt_disk_sig), &nt_disk_sig) == 0)
752ceb1
+	      grub_printf ("%08x-%02x",
752ceb1
+			   grub_le_to_cpu32(nt_disk_sig), 1 + p->number);
752ceb1
+	}
752ceb1
+      else if (strcmp(p->partmap->name, "gpt") == 0)
752ceb1
+	{
752ceb1
+	  struct grub_gpt_partentry gptdata;
752ceb1
+
752ceb1
+	  if (grub_disk_read (disk, p->offset, p->index,
752ceb1
+			      sizeof(gptdata), &gptdata) == 0)
752ceb1
+	    print_gpt_guid(gptdata.guid);
752ceb1
+	}
752ceb1
+
752ceb1
+      disk->partition = p;
752ceb1
+    }
752ceb1
+}
752ceb1
+
752ceb1
 static void
752ceb1
 probe_cryptodisk_uuid (grub_disk_t disk, char delim)
752ceb1
 {
752ceb1
@@ -635,6 +677,12 @@ probe (const char *path, char **device_names, char delim)
752ceb1
 	/* Check if dev->disk itself is contained in a partmap.  */
752ceb1
 	probe_partmap (dev->disk, delim);
752ceb1
 
752ceb1
+      else if (print == PRINT_PARTUUID)
752ceb1
+	{
752ceb1
+	  probe_partuuid (dev->disk, delim);
752ceb1
+	  putchar (delim);
752ceb1
+	}
752ceb1
+
752ceb1
       else if (print == PRINT_MSDOS_PARTTYPE)
752ceb1
 	{
752ceb1
 	  if (dev->disk->partition