Blob Blame History Raw
From cff35642eac6699e30549db2db3341e7657bf4a6 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Tue, 18 Sep 2018 14:57:13 -0400
Subject: [PATCH] Fix partition number detection when it's not provided.

We need to actually get the partition number from the child device when
we're called without it.

Resolves: rhbz#1616305

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 src/creator.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/src/creator.c b/src/creator.c
index ef782e2b6475..987fa033e5b7 100644
--- a/src/creator.c
+++ b/src/creator.c
@@ -350,6 +350,36 @@ efi_generate_file_device_path_from_esp(uint8_t *buf, ssize_t size,
 	return ret;
 }
 
+static int
+get_part(char *devpath)
+{
+	int fd;
+	int partition = -1;
+	struct device *dev = NULL;
+
+	fd = open(devpath, O_RDONLY);
+	if (fd < 0) {
+		efi_error("could not open device for ESP");
+		goto err;
+	}
+
+	dev = device_get(fd, -1);
+	if (dev == NULL) {
+		efi_error("could not get ESP disk info");
+		goto err;
+	}
+
+	partition = dev->part;
+	if (partition < 0)
+		partition = 0;
+err:
+	if (dev)
+		device_free(dev);
+	if (fd >= 0)
+		close(fd);
+	return partition;
+}
+
 ssize_t NONNULL(3) PUBLIC
 efi_generate_file_device_path(uint8_t *buf, ssize_t size,
 			      const char * const filepath,
@@ -374,6 +404,19 @@ efi_generate_file_device_path(uint8_t *buf, ssize_t size,
 		efi_error("could not find parent device for file");
 		goto err;
 	}
+        debug("child_devpath:%s", child_devpath);
+
+	debug("parent_devpath:%s", parent_devpath);
+	debug("child_devpath:%s", child_devpath);
+	debug("rc:%d", rc);
+
+	rc = get_part(child_devpath);
+	if (rc < 0) {
+		efi_error("Couldn't get partition number for %s",
+			  child_devpath);
+		goto err;
+	}
+	debug("detected partition:%d", rc);
 
 	va_start(ap, options);
 
-- 
2.19.1