From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Mon, 24 Jan 2022 12:21:58 +0100 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nBxQ6-00Bopb-Os for lore@lore.pengutronix.de; Mon, 24 Jan 2022 12:21:58 +0100 Received: from localhost ([127.0.0.1] helo=metis.ext.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1nBxQ4-0002ZL-VO; Mon, 24 Jan 2022 12:21:56 +0100 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nBxQ2-0002Xg-S6; Mon, 24 Jan 2022 12:21:54 +0100 Received: from [2a0a:edc0:0:1101:1d::39] (helo=dude03.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1nBxQ2-00C6w4-SB; Mon, 24 Jan 2022 12:21:54 +0100 Received: from mol by dude03.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1nBxQ0-00CemL-N7; Mon, 24 Jan 2022 12:21:52 +0100 From: Michael Olbrich To: oss-tools@pengutronix.de Date: Mon, 24 Jan 2022 12:21:41 +0100 Message-Id: <20220124112143.3016473-3-m.olbrich@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220124112143.3016473-1-m.olbrich@pengutronix.de> References: <20220124112143.3016473-1-m.olbrich@pengutronix.de> MIME-Version: 1.0 Subject: [OSS-Tools] [PATCH 2/4] state: implement helper to find device path from diskuuid X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Olbrich Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false Signed-off-by: Michael Olbrich --- I didn't find a good place for this helper. It has nothing to do with device trees and I didn't want to add it to code that is shared with barebox. If there is a better place for it then I don't mind moving it. Michael Makefile.am | 1 + src/barebox-state-compat.c | 64 ++++++++++++++++++++++++++++++++++++++ src/libbb.h | 2 ++ 3 files changed, 67 insertions(+) create mode 100644 src/barebox-state-compat.c diff --git a/Makefile.am b/Makefile.am index d53ee7c6f9c3..a9eb0fd304eb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ barebox_state_SOURCES = \ src/barebox-state/state.c \ src/barebox-state/state.h \ src/barebox-state/state_variables.c \ + src/barebox-state-compat.c \ src/barebox-state.c \ src/barebox-state.h \ \ diff --git a/src/barebox-state-compat.c b/src/barebox-state-compat.c new file mode 100644 index 000000000000..1ed8fefa2253 --- /dev/null +++ b/src/barebox-state-compat.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Pengutronix, Michael Olbrich + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include + +int devpath_from_diskuuid(const char *diskuuid, char **devpath) +{ + struct udev *udev; + struct udev_enumerate *enumerate; + struct udev_list_entry *devices, *dev_list_entry; + int ret = 0; + + udev = udev_new(); + if (!udev) { + fprintf(stderr, "Can't create udev\n"); + return -ENODEV; + } + + enumerate = udev_enumerate_new(udev); + udev_enumerate_add_match_subsystem(enumerate, "block"); + udev_enumerate_scan_devices(enumerate); + devices = udev_enumerate_get_list_entry(enumerate); + udev_list_entry_foreach(dev_list_entry, devices) { + const char *path, *devtype, *outpath, *uuid; + struct udev_device *device; + + path = udev_list_entry_get_name(dev_list_entry); + device = udev_device_new_from_syspath(udev, path); + + /* distinguish device (disk) from partitions */ + devtype = udev_device_get_devtype(device); + if (!devtype) + continue; + if (strcmp(devtype, "disk")) + continue; + + uuid = udev_device_get_property_value(device, "ID_PART_TABLE_UUID"); + if (!strcmp(uuid, diskuuid)) { + outpath = udev_device_get_devnode(device); + *devpath = strdup(outpath); + goto out; + } + } + ret = -ENODEV; + +out: + udev_enumerate_unref(enumerate); + udev_unref(udev); + + return ret; +} diff --git a/src/libbb.h b/src/libbb.h index 9f9d32d12d94..5350341d2281 100644 --- a/src/libbb.h +++ b/src/libbb.h @@ -3,4 +3,6 @@ #include
+int devpath_from_diskuuid(const char *diskuuid, char **devpath); + #endif -- 2.30.2 _______________________________________________ OSS-Tools mailing list OSS-Tools@pengutronix.de