46968b6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
46968b6
From: Diego Domingos <diegodo@br.ibm.com>
46968b6
Date: Mon, 14 Dec 2020 17:47:16 +0100
46968b6
Subject: [PATCH] ieee1275/powerpc: enables device mapper discovery
46968b6
46968b6
this patch enables the device mapper discovery on ofpath.c. Currently,
46968b6
when we are dealing with a device like /dev/dm-* the ofpath returns null
46968b6
since there is no function implemented to handle this case.
46968b6
46968b6
This patch implements a function that will look into /sys/block/dm-*
46968b6
devices and search recursively inside slaves directory to find the root
46968b6
disk.
46968b6
46968b6
Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
46968b6
---
46968b6
 grub-core/osdep/linux/ofpath.c | 64 +++++++++++++++++++++++++++++++++++++++++-
46968b6
 1 file changed, 63 insertions(+), 1 deletion(-)
46968b6
46968b6
diff --git a/grub-core/osdep/linux/ofpath.c b/grub-core/osdep/linux/ofpath.c
46968b6
index 0f5d54e9f2d..cc849d9c94c 100644
46968b6
--- a/grub-core/osdep/linux/ofpath.c
46968b6
+++ b/grub-core/osdep/linux/ofpath.c
46968b6
@@ -37,6 +37,7 @@
46968b6
 #include <fcntl.h>
46968b6
 #include <errno.h>
46968b6
 #include <ctype.h>
46968b6
+#include <dirent.h>
46968b6
 
46968b6
 #ifdef __sparc__
46968b6
 typedef enum
46968b6
@@ -755,13 +756,74 @@ strip_trailing_digits (const char *p)
46968b6
   return new;
46968b6
 }
46968b6
 
46968b6
+static char *
46968b6
+get_slave_from_dm(const char * device){
46968b6
+  char *curr_device, *tmp;
46968b6
+  char *directory;
46968b6
+  char *ret = NULL;
46968b6
+
46968b6
+  directory = grub_strdup (device);
46968b6
+  tmp = get_basename(directory);
46968b6
+  curr_device = grub_strdup (tmp);
46968b6
+  *tmp = '\0';
46968b6
+
46968b6
+  /* Recursively check for slaves devices so we can find the root device */
46968b6
+  while ((curr_device[0] == 'd') && (curr_device[1] == 'm') && (curr_device[2] == '-')){
46968b6
+    DIR *dp;
46968b6
+    struct dirent *ep;
46968b6
+    char* device_path;
46968b6
+
46968b6
+    device_path = grub_xasprintf ("/sys/block/%s/slaves", curr_device);
46968b6
+    dp = opendir(device_path);
46968b6
+    free(device_path);
46968b6
+
46968b6
+    if (dp != NULL)
46968b6
+    {
46968b6
+      ep = readdir (dp);
46968b6
+      while (ep != NULL){
46968b6
+
46968b6
+	/* avoid some system directories */
46968b6
+        if (!strcmp(ep->d_name,"."))
46968b6
+            goto next_dir;
46968b6
+        if (!strcmp(ep->d_name,".."))
46968b6
+            goto next_dir;
46968b6
+
46968b6
+	free (curr_device);
46968b6
+	free (ret);
46968b6
+	curr_device = grub_strdup (ep->d_name);
46968b6
+	ret = grub_xasprintf ("%s%s", directory, curr_device);
46968b6
+	break;
46968b6
+
46968b6
+        next_dir:
46968b6
+         ep = readdir (dp);
46968b6
+         continue;
46968b6
+      }
46968b6
+      closedir (dp);
46968b6
+    }
46968b6
+    else
46968b6
+      grub_util_warn (_("cannot open directory `%s'"), device_path);
46968b6
+  }
46968b6
+
46968b6
+  free (directory);
46968b6
+  free (curr_device);
46968b6
+
46968b6
+  return ret;
46968b6
+}
46968b6
+
46968b6
 char *
46968b6
 grub_util_devname_to_ofpath (const char *sys_devname)
46968b6
 {
46968b6
-  char *name_buf, *device, *devnode, *devicenode, *ofpath;
46968b6
+  char *name_buf, *device, *devnode, *devicenode, *ofpath, *realname;
46968b6
 
46968b6
   name_buf = xrealpath (sys_devname);
46968b6
 
46968b6
+  realname = get_slave_from_dm (name_buf);
46968b6
+  if (realname)
46968b6
+    {
46968b6
+      free (name_buf);
46968b6
+      name_buf = realname;
46968b6
+    }
46968b6
+
46968b6
   device = get_basename (name_buf);
46968b6
   devnode = strip_trailing_digits (name_buf);
46968b6
   devicenode = strip_trailing_digits (device);