Commit 59e890ef7bf5b80d96802e115255f969491ef645

Authored by Christian Gmeiner
Committed by Tom Rini
1 parent 1b51d32c0f

fs: make it possible to read the filesystem UUID

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 <interface> <dev>:<part>
    - print filesystem UUID
fsuuid <interface> <dev>:<part> <varname>
    - 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 <christian.gmeiner@gmail.com>
Acked-by: Stephen Warren <swarren@nvidia.com>

Showing 7 changed files with 94 additions and 0 deletions Side-by-side Diff

... ... @@ -989,6 +989,7 @@
989 989 CONFIG_CMD_EXT4 * ext4 command support
990 990 CONFIG_CMD_FS_GENERIC * filesystem commands (e.g. load, ls)
991 991 that work for multiple fs types
  992 + CONFIG_CMD_FS_UUID * Look up a filesystem UUID
992 993 CONFIG_CMD_SAVEENV saveenv
993 994 CONFIG_CMD_FDC * Floppy Disk Support
994 995 CONFIG_CMD_FAT * FAT command support
... ... @@ -188,6 +188,7 @@
188 188 obj-$(CONFIG_USB_STORAGE) += usb_storage.o
189 189 endif
190 190 obj-$(CONFIG_CMD_FASTBOOT) += cmd_fastboot.o
  191 +obj-$(CONFIG_CMD_FS_UUID) += cmd_fs_uuid.o
191 192  
192 193 obj-$(CONFIG_CMD_USB_MASS_STORAGE) += cmd_usb_mass_storage.o
193 194 obj-$(CONFIG_CMD_THOR_DOWNLOAD) += cmd_thordown.o
common/cmd_fs_uuid.c
  1 +/*
  2 + * cmd_fs_uuid.c -- fsuuid command
  3 + *
  4 + * Copyright (C) 2014, Bachmann electronic GmbH
  5 + *
  6 + * SPDX-License-Identifier: GPL-2.0+
  7 + */
  8 +
  9 +#include <common.h>
  10 +#include <command.h>
  11 +#include <fs.h>
  12 +
  13 +static int do_fs_uuid_wrapper(cmd_tbl_t *cmdtp, int flag,
  14 + int argc, char * const argv[])
  15 +{
  16 + return do_fs_uuid(cmdtp, flag, argc, argv, FS_TYPE_ANY);
  17 +}
  18 +
  19 +U_BOOT_CMD(
  20 + fsuuid, 4, 1, do_fs_uuid_wrapper,
  21 + "Look up a filesystem UUID",
  22 + "<interface> <dev>:<part>\n"
  23 + " - print filesystem UUID\n"
  24 + "fsuuid <interface> <dev>:<part> <varname>\n"
  25 + " - set environment variable to filesystem UUID\n"
  26 +);
... ... @@ -231,4 +231,19 @@
231 231  
232 232 return len_read;
233 233 }
  234 +
  235 +int ext4fs_uuid(char *uuid_str)
  236 +{
  237 + if (ext4fs_root == NULL)
  238 + return -1;
  239 +
  240 +#ifdef CONFIG_LIB_UUID
  241 + uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
  242 + uuid_str, UUID_STR_FORMAT_STD);
  243 +
  244 + return 0;
  245 +#else
  246 + return -ENOSYS;
  247 +#endif
  248 +}
... ... @@ -15,6 +15,7 @@
15 15 */
16 16  
17 17 #include <config.h>
  18 +#include <errno.h>
18 19 #include <common.h>
19 20 #include <part.h>
20 21 #include <ext4fs.h>
... ... @@ -67,6 +68,11 @@
67 68 {
68 69 }
69 70  
  71 +static inline int fs_uuid_unsupported(char *uuid_str)
  72 +{
  73 + return -1;
  74 +}
  75 +
70 76 struct fstype_info {
71 77 int fstype;
72 78 /*
... ... @@ -86,6 +92,7 @@
86 92 int (*read)(const char *filename, void *buf, int offset, int len);
87 93 int (*write)(const char *filename, void *buf, int offset, int len);
88 94 void (*close)(void);
  95 + int (*uuid)(char *uuid_str);
89 96 };
90 97  
91 98 static struct fstype_info fstypes[] = {
... ... @@ -100,6 +107,7 @@
100 107 .size = fat_size,
101 108 .read = fat_read_file,
102 109 .write = fs_write_unsupported,
  110 + .uuid = fs_uuid_unsupported,
103 111 },
104 112 #endif
105 113 #ifdef CONFIG_FS_EXT4
... ... @@ -113,6 +121,7 @@
113 121 .size = ext4fs_size,
114 122 .read = ext4_read_file,
115 123 .write = fs_write_unsupported,
  124 + .uuid = ext4fs_uuid,
116 125 },
117 126 #endif
118 127 #ifdef CONFIG_SANDBOX
... ... @@ -126,6 +135,7 @@
126 135 .size = sandbox_fs_size,
127 136 .read = fs_read_sandbox,
128 137 .write = fs_write_sandbox,
  138 + .uuid = fs_uuid_unsupported,
129 139 },
130 140 #endif
131 141 {
... ... @@ -138,6 +148,7 @@
138 148 .size = fs_size_unsupported,
139 149 .read = fs_read_unsupported,
140 150 .write = fs_write_unsupported,
  151 + .uuid = fs_uuid_unsupported,
141 152 },
142 153 };
143 154  
... ... @@ -206,6 +217,13 @@
206 217 fs_type = FS_TYPE_ANY;
207 218 }
208 219  
  220 +int fs_uuid(char *uuid_str)
  221 +{
  222 + struct fstype_info *info = fs_get_info(fs_type);
  223 +
  224 + return info->uuid(uuid_str);
  225 +}
  226 +
209 227 int fs_ls(const char *dirname)
210 228 {
211 229 int ret;
... ... @@ -442,5 +460,30 @@
442 460 puts("\n");
443 461  
444 462 return 0;
  463 +}
  464 +
  465 +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  466 + int fstype)
  467 +{
  468 + int ret;
  469 + char uuid[37];
  470 + memset(uuid, 0, sizeof(uuid));
  471 +
  472 + if (argc < 3 || argc > 4)
  473 + return CMD_RET_USAGE;
  474 +
  475 + if (fs_set_blk_dev(argv[1], argv[2], fstype))
  476 + return 1;
  477 +
  478 + ret = fs_uuid(uuid);
  479 + if (ret)
  480 + return CMD_RET_FAILURE;
  481 +
  482 + if (argc == 4)
  483 + setenv(argv[3], uuid);
  484 + else
  485 + printf("%s\n", uuid);
  486 +
  487 + return CMD_RET_SUCCESS;
445 488 }
... ... @@ -145,5 +145,6 @@
145 145 disk_partition_t *fs_partition);
146 146 int ext4_read_file(const char *filename, void *buf, int offset, int len);
147 147 int ext4_read_superblock(char *buffer);
  148 +int ext4fs_uuid(char *uuid_str);
148 149 #endif
... ... @@ -93,5 +93,12 @@
93 93 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
94 94 int fstype);
95 95  
  96 +/*
  97 + * Determine the UUID of the specified filesystem and print it. Optionally it is
  98 + * possible to store the UUID directly in env.
  99 + */
  100 +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  101 + int fstype);
  102 +
96 103 #endif /* _FS_H */