From patchwork Wed Nov 12 13:35:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Gmeiner X-Patchwork-Id: 409974 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 19C7C14009B for ; Thu, 13 Nov 2014 00:35:24 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 46FA14B6F8; Wed, 12 Nov 2014 14:35:20 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EYgppqQZLHh5; Wed, 12 Nov 2014 14:35:19 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6BD494B6EE; Wed, 12 Nov 2014 14:35:19 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 01FC34B6EE for ; Wed, 12 Nov 2014 14:35:15 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dcViEB5de3C1 for ; Wed, 12 Nov 2014 14:35:14 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-wi0-f169.google.com (mail-wi0-f169.google.com [209.85.212.169]) by theia.denx.de (Postfix) with ESMTPS id C95964B6EB for ; Wed, 12 Nov 2014 14:35:11 +0100 (CET) Received: by mail-wi0-f169.google.com with SMTP id n3so4920718wiv.4 for ; Wed, 12 Nov 2014 05:35:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=NeVhUDOymG1192su9hAYMNOu7UMWvHdSP9fhxqM6IDg=; b=M+ucIArLSfzAqOUaM7o1bAJhw864IHy9r2OyyUWWoiLqYCqof1FiUO7LhfwAWdMPFZ AoQnNp0If0ZcVt/dtV3sT2lX4wxZmYaSYc4XyomoGeLjTGHNYHg9AK65GCz9hBbI4fH6 BsA6sXOZfhciO0Yf/5ya0Bxzwm6vjuRklDaLTgipn5mZn+6euIE4GhpEZv/YOjh7hvBn koQkN6hVw/6cuSUWhz9rV5VvcWvwu0uc0S5nXzxhzKSScOgoRlt/K2N+g5L093K4UTBC W2jlsEkFuI7EifToYuJCoZ/q1e9zecINRU4VEEnrXIMlWNo/qPEk8sJlSOcGbAqOrP18 PuEg== X-Received: by 10.180.73.173 with SMTP id m13mr49417947wiv.10.1415799311105; Wed, 12 Nov 2014 05:35:11 -0800 (PST) Received: from chgm-pc.bachmann.at.bachmann.at (089144223232.atnat0032.highway.bob.at. [89.144.223.232]) by mx.google.com with ESMTPSA id x13sm31497383wjw.18.2014.11.12.05.35.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Nov 2014 05:35:10 -0800 (PST) From: Christian Gmeiner To: u-boot@lists.denx.de Date: Wed, 12 Nov 2014 14:35:04 +0100 Message-Id: <1415799305-16621-1-git-send-email-christian.gmeiner@gmail.com> X-Mailer: git-send-email 1.9.3 Cc: swarren@nvidia.com Subject: [U-Boot] [PATCH v3] fs: make it possible to read the filesystem UUID X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.13 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Some filesystems have a UUID stored in its superblock. To allow using root=UUID=... for the kernel command line we need a way to read-out the filesystem UUID. changes rfc -> v1: - make the environment variable an option parameter. If not given, the UUID is printed out. If given, it is stored in the env variable. - corrected typos - return error codes changes v1 -> v2: - fix return code of do_fs_uuid(..) - document do_fs_uuid(..) - implement fs_uuid_unsuported(..) be more consistent with the way other optional functionality works changes v2 -> v3: - change ext4fs_uuid(..) to make use of #if .. #else .. #endif construct to get rid of unreachable code Hit any key to stop autoboot: 0 => fsuuid fsuuid - Look up a filesystem UUID Usage: fsuuid : - print filesystem UUID fsuuid : - set environment variable to filesystem UUID => fsuuid mmc 0:1 d9f9fc05-45ae-4a36-a616-fccce0e4f887 => fsuuid mmc 0:2 eb3db83c-7b28-499f-95ce-9e0bb21cda81 => fsuuid mmc 0:1 uuid1 => fsuuid mmc 0:2 uuid2 => printenv uuid1 uuid1=d9f9fc05-45ae-4a36-a616-fccce0e4f887 => printenv uuid2 uuid2=eb3db83c-7b28-499f-95ce-9e0bb21cda81 => Signed-off-by: Christian Gmeiner Acked-by: Stephen Warren --- README | 1 + common/Makefile | 1 + common/cmd_fs_uuid.c | 26 ++++++++++++++++++++++++++ fs/ext4/ext4fs.c | 15 +++++++++++++++ fs/fs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/ext4fs.h | 1 + include/fs.h | 7 +++++++ 7 files changed, 94 insertions(+) create mode 100644 common/cmd_fs_uuid.c diff --git a/README b/README index c3a9dfc..b48de5f 100644 --- a/README +++ b/README @@ -989,6 +989,7 @@ The following options need to be configured: CONFIG_CMD_EXT4 * ext4 command support CONFIG_CMD_FS_GENERIC * filesystem commands (e.g. load, ls) that work for multiple fs types + CONFIG_CMD_FS_UUID * Look up a filesystem UUID CONFIG_CMD_SAVEENV saveenv CONFIG_CMD_FDC * Floppy Disk Support CONFIG_CMD_FAT * FAT command support diff --git a/common/Makefile b/common/Makefile index 6cc4de8..508a0b2 100644 --- a/common/Makefile +++ b/common/Makefile @@ -188,6 +188,7 @@ obj-y += usb.o usb_hub.o obj-$(CONFIG_USB_STORAGE) += usb_storage.o endif obj-$(CONFIG_CMD_FASTBOOT) += cmd_fastboot.o +obj-$(CONFIG_CMD_FS_UUID) += cmd_fs_uuid.o obj-$(CONFIG_CMD_USB_MASS_STORAGE) += cmd_usb_mass_storage.o obj-$(CONFIG_CMD_THOR_DOWNLOAD) += cmd_thordown.o diff --git a/common/cmd_fs_uuid.c b/common/cmd_fs_uuid.c new file mode 100644 index 0000000..613f3a4 --- /dev/null +++ b/common/cmd_fs_uuid.c @@ -0,0 +1,26 @@ +/* + * cmd_fs_uuid.c -- fsuuid command + * + * Copyright (C) 2014, Bachmann electronic GmbH + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include +#include + +static int do_fs_uuid_wrapper(cmd_tbl_t *cmdtp, int flag, + int argc, char * const argv[]) +{ + return do_fs_uuid(cmdtp, flag, argc, argv, FS_TYPE_ANY); +} + +U_BOOT_CMD( + fsuuid, 4, 1, do_fs_uuid_wrapper, + "Look up a filesystem UUID", + " :\n" + " - print filesystem UUID\n" + "fsuuid : \n" + " - set environment variable to filesystem UUID\n" +); diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c index cbdc220..87d5128 100644 --- a/fs/ext4/ext4fs.c +++ b/fs/ext4/ext4fs.c @@ -231,3 +231,18 @@ int ext4_read_file(const char *filename, void *buf, int offset, int len) return len_read; } + +int ext4fs_uuid(char *uuid_str) +{ + if (ext4fs_root == NULL) + return -1; + +#ifdef CONFIG_LIB_UUID + uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id, + uuid_str, UUID_STR_FORMAT_STD); + + return 0; +#else + return -ENOSYS; +#endif +} diff --git a/fs/fs.c b/fs/fs.c index dd680f3..e4ad6bc 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -67,6 +68,11 @@ static inline void fs_close_unsupported(void) { } +static inline int fs_uuid_unsupported(char *uuid_str) +{ + return -1; +} + struct fstype_info { int fstype; /* @@ -86,6 +92,7 @@ struct fstype_info { int (*read)(const char *filename, void *buf, int offset, int len); int (*write)(const char *filename, void *buf, int offset, int len); void (*close)(void); + int (*uuid)(char *uuid_str); }; static struct fstype_info fstypes[] = { @@ -100,6 +107,7 @@ static struct fstype_info fstypes[] = { .size = fat_size, .read = fat_read_file, .write = fs_write_unsupported, + .uuid = fs_uuid_unsupported, }, #endif #ifdef CONFIG_FS_EXT4 @@ -113,6 +121,7 @@ static struct fstype_info fstypes[] = { .size = ext4fs_size, .read = ext4_read_file, .write = fs_write_unsupported, + .uuid = ext4fs_uuid, }, #endif #ifdef CONFIG_SANDBOX @@ -126,6 +135,7 @@ static struct fstype_info fstypes[] = { .size = sandbox_fs_size, .read = fs_read_sandbox, .write = fs_write_sandbox, + .uuid = fs_uuid_unsupported, }, #endif { @@ -138,6 +148,7 @@ static struct fstype_info fstypes[] = { .size = fs_size_unsupported, .read = fs_read_unsupported, .write = fs_write_unsupported, + .uuid = fs_uuid_unsupported, }, }; @@ -206,6 +217,13 @@ static void fs_close(void) fs_type = FS_TYPE_ANY; } +int fs_uuid(char *uuid_str) +{ + struct fstype_info *info = fs_get_info(fs_type); + + return info->uuid(uuid_str); +} + int fs_ls(const char *dirname) { int ret; @@ -443,3 +461,28 @@ int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], return 0; } + +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], + int fstype) +{ + int ret; + char uuid[37]; + memset(uuid, 0, sizeof(uuid)); + + if (argc < 3 || argc > 4) + return CMD_RET_USAGE; + + if (fs_set_blk_dev(argv[1], argv[2], fstype)) + return 1; + + ret = fs_uuid(uuid); + if (ret) + return CMD_RET_FAILURE; + + if (argc == 4) + setenv(argv[3], uuid); + else + printf("%s\n", uuid); + + return CMD_RET_SUCCESS; +} diff --git a/include/ext4fs.h b/include/ext4fs.h index 6c419f3..38970e4 100644 --- a/include/ext4fs.h +++ b/include/ext4fs.h @@ -145,4 +145,5 @@ int ext4fs_probe(block_dev_desc_t *fs_dev_desc, disk_partition_t *fs_partition); int ext4_read_file(const char *filename, void *buf, int offset, int len); int ext4_read_superblock(char *buffer); +int ext4fs_uuid(char *uuid_str); #endif diff --git a/include/fs.h b/include/fs.h index 06a45f2..491c60a 100644 --- a/include/fs.h +++ b/include/fs.h @@ -93,4 +93,11 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file, int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], int fstype); +/* + * Determine the UUID of the specified filesystem and print it. Optionally it is + * possible to store the UUID directly in env. + */ +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], + int fstype); + #endif /* _FS_H */