Blame SOURCES/0447-ieee1275-powerpc-enables-device-mapper-discovery.patch

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