Fix code to search for MMC device

The findDevice() function is broken currently because it's looking for a
device in sysfs with the name of mmcblk*. The problem is that the boot
partitions are also showing up in sysfs (mmcblk0boot0 and mmcblk0boot1),
and the findDevice() function is getting confused.

Instead of relying on the name of the device to find the MMC device, we
now look at the following entry in sysfs, to make sure we found an MMC
device:

/sys/block/<dev_name>/device/type

The contents of that file should be MMC.

Tested:
Ran eStoraged on a machine to confirm that it created a D-Bus object
with the mmcblk0 device, instead of mmcblk0boot0.
$ busctl tree xyz.openbmc_project.eStoraged
`-/xyz
  `-/xyz/openbmc_project
    `-/xyz/openbmc_project/inventory
      `-/xyz/openbmc_project/inventory/storage
        `-/xyz/openbmc_project/inventory/storage/mmcblk0

Signed-off-by: John Wedig <johnwedig@google.com>
Change-Id: I786934fcdc950b55c62bc7e3784e29d5ba73099f
diff --git a/src/util.cpp b/src/util.cpp
index 4b366bb..811cbf8 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -126,17 +126,47 @@
     /* Look for the eMMC in the specified searchDir directory. */
     for (auto const& dirEntry : std::filesystem::directory_iterator{searchDir})
     {
-        std::filesystem::path curDevice(dirEntry.path().filename());
-        if (curDevice.string().starts_with("mmcblk"))
+        /*
+         * We will look at the 'type' file to determine if this is an MMC
+         * device.
+         */
+        std::filesystem::path curPath(dirEntry.path());
+        curPath /= "device/type";
+        if (!std::filesystem::exists(curPath))
         {
-            sysfsDir = dirEntry.path();
-            sysfsDir /= "device";
+            /* The 'type' file doesn't exist. This must not be an eMMC. */
+            continue;
+        }
 
-            deviceFile = "/dev";
-            deviceFile /= curDevice;
+        try
+        {
+            std::ifstream typeFile(curPath, std::ios_base::in);
+            std::string devType;
+            typeFile >> devType;
+            if (devType.compare("MMC") == 0)
+            {
+                /* Found it. Get the sysfs directory and device file. */
+                std::filesystem::path deviceName(dirEntry.path().filename());
 
-            luksName = "luks-" + curDevice.string();
-            return true;
+                sysfsDir = dirEntry.path();
+                sysfsDir /= "device";
+
+                deviceFile = "/dev";
+                deviceFile /= deviceName;
+
+                luksName = "luks-" + deviceName.string();
+                return true;
+            }
+        }
+        catch (...)
+        {
+            lg2::error("Failed to read device type for {PATH}", "PATH", curPath,
+                       "REDFISH_MESSAGE_ID",
+                       std::string("OpenBMC.0.1.FindDeviceFail"));
+            /*
+             * We will still continue searching, though. Maybe this wasn't the
+             * device we were looking for, anyway.
+             */
         }
     }