diff mbox

[U-Boot] fs: make it possible to read the filesystem UUID

Message ID 1414768103-31231-1-git-send-email-christian.gmeiner@gmail.com
State Changes Requested
Delegated to: Tom Rini
Headers show

Commit Message

Christian Gmeiner Oct. 31, 2014, 3:08 p.m. UTC
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.

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>
---
 README           |  1 +
 common/Makefile  |  1 +
 fs/ext4/ext4fs.c | 15 +++++++++++++++
 fs/fs.c          | 39 +++++++++++++++++++++++++++++++++++++++
 include/ext4fs.h |  1 +
 include/fs.h     |  2 ++
 6 files changed, 59 insertions(+)

Comments

Simon Glass Nov. 1, 2014, 3:13 p.m. UTC | #1
Hi Christian,

On 31 October 2014 09:08, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
> 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.
>
> 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>

If this is version 2 you should have a change list. You can use patman
(tools/patman/patman) to automate this.

> ---
>  README           |  1 +
>  common/Makefile  |  1 +
>  fs/ext4/ext4fs.c | 15 +++++++++++++++
>  fs/fs.c          | 39 +++++++++++++++++++++++++++++++++++++++
>  include/ext4fs.h |  1 +
>  include/fs.h     |  2 ++
>  6 files changed, 59 insertions(+)
>
> diff --git a/README b/README
> index 7b5538e..53b84a6 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/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
> index cbdc220..61b6dc2 100644
> --- a/fs/ext4/ext4fs.c
> +++ b/fs/ext4/ext4fs.c
> @@ -187,6 +187,21 @@ int ext4fs_size(const char *filename)
>         return ext4fs_open(filename);
>  }
>
> +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;
> +#endif
> +
> +       return -ENOSYS;
> +}
> +
>  int ext4fs_read(char *buf, unsigned len)
>  {
>         if (ext4fs_root == NULL || ext4fs_file == NULL)
> diff --git a/fs/fs.c b/fs/fs.c
> index dd680f3..492d8b1 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -15,6 +15,7 @@
>   */
>
>  #include <config.h>
> +#include <errno.h>
>  #include <common.h>
>  #include <part.h>
>  #include <ext4fs.h>
> @@ -86,6 +87,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[] = {
> @@ -113,6 +115,7 @@ static struct fstype_info fstypes[] = {
>                 .size = ext4fs_size,
>                 .read = ext4_read_file,
>                 .write = fs_write_unsupported,
> +               .uuid = ext4fs_uuid,
>         },
>  #endif
>  #ifdef CONFIG_SANDBOX
> @@ -206,6 +209,17 @@ 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);
> +       int ret = -ENOSYS;
> +
> +       if (info->uuid)
> +               ret = info->uuid(uuid_str);
> +
> +       return ret;
> +}
> +
>  int fs_ls(const char *dirname)
>  {
>         int ret;
> @@ -443,3 +457,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 ret;

Here you should return CMD_RET_SUCCESS or CMD_RET_FAILURE. You should
not return raw errors since they are not the same thing.

> +
> +       if (argc == 4)
> +               setenv(argv[3], uuid);
> +       else
> +               printf("%s\n", uuid);
> +
> +       return 0;
> +}
> diff --git a/include/ext4fs.h b/include/ext4fs.h
> index 6c419f3..19816de 100644
> --- a/include/ext4fs.h
> +++ b/include/ext4fs.h
> @@ -137,6 +137,7 @@ void ext4fs_reinit_global(void);
>  int ext4fs_ls(const char *dirname);
>  int ext4fs_exists(const char *filename);
>  int ext4fs_size(const char *filename);
> +int ext4fs_uuid(char *uuid_str);
>  void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
>  int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
>  void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
> diff --git a/include/fs.h b/include/fs.h
> index 06a45f2..41082b3 100644
> --- a/include/fs.h
> +++ b/include/fs.h
> @@ -92,5 +92,7 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file,
>                 int fstype);
>  int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>                 int fstype);
> +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
> +               int fstype);

Can you please add a function comment for this?

>
>  #endif /* _FS_H */
> --
> 1.9.3
>

Regards,
Simon
Christian Gmeiner Nov. 3, 2014, 10:47 a.m. UTC | #2
Hi Simon,

2014-11-01 16:13 GMT+01:00 Simon Glass <sjg@chromium.org>:
> Hi Christian,
>
> On 31 October 2014 09:08, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
>> 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.
>>
>> 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>
>
> If this is version 2 you should have a change list. You can use patman
> (tools/patman/patman) to automate this.

Yes.. the first version was the RFC. I really need to look into
patman. Normally I do
these things per hand.

>
>> ---
>>  README           |  1 +
>>  common/Makefile  |  1 +
>>  fs/ext4/ext4fs.c | 15 +++++++++++++++
>>  fs/fs.c          | 39 +++++++++++++++++++++++++++++++++++++++
>>  include/ext4fs.h |  1 +
>>  include/fs.h     |  2 ++
>>  6 files changed, 59 insertions(+)
>>
>> diff --git a/README b/README
>> index 7b5538e..53b84a6 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/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
>> index cbdc220..61b6dc2 100644
>> --- a/fs/ext4/ext4fs.c
>> +++ b/fs/ext4/ext4fs.c
>> @@ -187,6 +187,21 @@ int ext4fs_size(const char *filename)
>>         return ext4fs_open(filename);
>>  }
>>
>> +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;
>> +#endif
>> +
>> +       return -ENOSYS;
>> +}
>> +
>>  int ext4fs_read(char *buf, unsigned len)
>>  {
>>         if (ext4fs_root == NULL || ext4fs_file == NULL)
>> diff --git a/fs/fs.c b/fs/fs.c
>> index dd680f3..492d8b1 100644
>> --- a/fs/fs.c
>> +++ b/fs/fs.c
>> @@ -15,6 +15,7 @@
>>   */
>>
>>  #include <config.h>
>> +#include <errno.h>
>>  #include <common.h>
>>  #include <part.h>
>>  #include <ext4fs.h>
>> @@ -86,6 +87,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[] = {
>> @@ -113,6 +115,7 @@ static struct fstype_info fstypes[] = {
>>                 .size = ext4fs_size,
>>                 .read = ext4_read_file,
>>                 .write = fs_write_unsupported,
>> +               .uuid = ext4fs_uuid,
>>         },
>>  #endif
>>  #ifdef CONFIG_SANDBOX
>> @@ -206,6 +209,17 @@ 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);
>> +       int ret = -ENOSYS;
>> +
>> +       if (info->uuid)
>> +               ret = info->uuid(uuid_str);
>> +
>> +       return ret;
>> +}
>> +
>>  int fs_ls(const char *dirname)
>>  {
>>         int ret;
>> @@ -443,3 +457,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 ret;
>
> Here you should return CMD_RET_SUCCESS or CMD_RET_FAILURE. You should
> not return raw errors since they are not the same thing.

Okay. will be fixed in next version of the patch.

>
>> +
>> +       if (argc == 4)
>> +               setenv(argv[3], uuid);
>> +       else
>> +               printf("%s\n", uuid);
>> +
>> +       return 0;
>> +}
>> diff --git a/include/ext4fs.h b/include/ext4fs.h
>> index 6c419f3..19816de 100644
>> --- a/include/ext4fs.h
>> +++ b/include/ext4fs.h
>> @@ -137,6 +137,7 @@ void ext4fs_reinit_global(void);
>>  int ext4fs_ls(const char *dirname);
>>  int ext4fs_exists(const char *filename);
>>  int ext4fs_size(const char *filename);
>> +int ext4fs_uuid(char *uuid_str);
>>  void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
>>  int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
>>  void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
>> diff --git a/include/fs.h b/include/fs.h
>> index 06a45f2..41082b3 100644
>> --- a/include/fs.h
>> +++ b/include/fs.h
>> @@ -92,5 +92,7 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file,
>>                 int fstype);
>>  int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>>                 int fstype);
>> +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>> +               int fstype);
>
> Can you please add a function comment for this?

Yes I will add one  - directly here in the .h file?

>
>>
>>  #endif /* _FS_H */
>> --
>> 1.9.3
>>
>
> Regards,
> Simon

Greets
--
Christian Gmeiner, MSc

https://soundcloud.com/christian-gmeiner
Stephen Warren Nov. 3, 2014, 9 p.m. UTC | #3
On 10/31/2014 09:08 AM, Christian Gmeiner wrote:
> 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.
>
> 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
> =>

Acked-by: Stephen Warren <swarren@nvidia.com>

It'd be nice if you could implement fat_uuid() too, and plumb that in. 
That could be a separate commit though I suppose.

IIRC, the kernel accepts the format NNNNNNNN-MM where NNNNNNNN is the 
32-bit FAT disk ID (I don't recall the exact correct term) in 0-filled 
hex and MM is the partition number in 0-filled decimal.

> diff --git a/fs/fs.c b/fs/fs.c

> +int fs_uuid(char *uuid_str)
> +{
> +	struct fstype_info *info = fs_get_info(fs_type);
> +	int ret = -ENOSYS;
> +
> +	if (info->uuid)
> +		ret = info->uuid(uuid_str);
> +
> +	return ret;
> +}

Actually, that might be better as:

return info->uuid(uuid_str);

To make that work, you'd need to implement a fs_uuid_unsuported() 
function and fill it in for all the fs types in fstypes[]. That would be 
more consistent with the way other optional functionality works in this 
file.
Simon Glass Nov. 4, 2014, 6:31 a.m. UTC | #4
Hi Christian,

On 31 October 2014 09:08, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
> 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.
>
> 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>
> ---
>  README           |  1 +
>  common/Makefile  |  1 +
>  fs/ext4/ext4fs.c | 15 +++++++++++++++
>  fs/fs.c          | 39 +++++++++++++++++++++++++++++++++++++++
>  include/ext4fs.h |  1 +
>  include/fs.h     |  2 ++
>  6 files changed, 59 insertions(+)
>
> diff --git a/README b/README
> index 7b5538e..53b84a6 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/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
> index cbdc220..61b6dc2 100644
> --- a/fs/ext4/ext4fs.c
> +++ b/fs/ext4/ext4fs.c
> @@ -187,6 +187,21 @@ int ext4fs_size(const char *filename)
>         return ext4fs_open(filename);
>  }
>
> +int ext4fs_uuid(char *uuid_str)
> +{
> +       if (ext4fs_root == NULL)
> +               return -1;

One other tiny note - should that be -ENOENT or some other error perhaps?

Anyway otherwise (with my other comments) this looks good to me.

Regards,
Simon
Simon Glass Nov. 4, 2014, 6:41 p.m. UTC | #5
Hi Christian,

On 3 November 2014 02:47, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
> Hi Simon,

[snip]

>>> +
>>> +       if (argc == 4)
>>> +               setenv(argv[3], uuid);
>>> +       else
>>> +               printf("%s\n", uuid);
>>> +
>>> +       return 0;
>>> +}
>>> diff --git a/include/ext4fs.h b/include/ext4fs.h
>>> index 6c419f3..19816de 100644
>>> --- a/include/ext4fs.h
>>> +++ b/include/ext4fs.h
>>> @@ -137,6 +137,7 @@ void ext4fs_reinit_global(void);
>>>  int ext4fs_ls(const char *dirname);
>>>  int ext4fs_exists(const char *filename);
>>>  int ext4fs_size(const char *filename);
>>> +int ext4fs_uuid(char *uuid_str);
>>>  void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
>>>  int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
>>>  void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
>>> diff --git a/include/fs.h b/include/fs.h
>>> index 06a45f2..41082b3 100644
>>> --- a/include/fs.h
>>> +++ b/include/fs.h
>>> @@ -92,5 +92,7 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file,
>>>                 int fstype);
>>>  int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>>>                 int fstype);
>>> +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>>> +               int fstype);
>>
>> Can you please add a function comment for this?
>
> Yes I will add one  - directly here in the .h file?

Yes I think so - this is where the API is

Regards,
Simon
Christian Gmeiner Nov. 11, 2014, 12:18 p.m. UTC | #6
Hi


2014-11-03 22:00 GMT+01:00 Stephen Warren <swarren@wwwdotorg.org>:
> On 10/31/2014 09:08 AM, Christian Gmeiner wrote:
>>
>> 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.
>>
>> 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
>> =>
>
>
> Acked-by: Stephen Warren <swarren@nvidia.com>
>
> It'd be nice if you could implement fat_uuid() too, and plumb that in. That
> could be a separate commit though I suppose.
>
> IIRC, the kernel accepts the format NNNNNNNN-MM where NNNNNNNN is the 32-bit
> FAT disk ID (I don't recall the exact correct term) in 0-filled hex and MM
> is the partition number in 0-filled decimal.
>

Lets see if I can find the motivation to look into fat :)

>> diff --git a/fs/fs.c b/fs/fs.c
>
>
>> +int fs_uuid(char *uuid_str)
>> +{
>> +       struct fstype_info *info = fs_get_info(fs_type);
>> +       int ret = -ENOSYS;
>> +
>> +       if (info->uuid)
>> +               ret = info->uuid(uuid_str);
>> +
>> +       return ret;
>> +}
>
>
> Actually, that might be better as:
>
> return info->uuid(uuid_str);
>
> To make that work, you'd need to implement a fs_uuid_unsuported() function
> and fill it in for all the fs types in fstypes[]. That would be more
> consistent with the way other optional functionality works in this file.

I will do it that way - looks more consistent.

greets
--
Christian Gmeiner, MSc

https://soundcloud.com/christian-gmeiner
Christian Gmeiner Nov. 11, 2014, 12:20 p.m. UTC | #7
Hi

2014-11-04 7:31 GMT+01:00 Simon Glass <sjg@chromium.org>:
> Hi Christian,
>
> On 31 October 2014 09:08, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
>> 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.
>>
>> 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>
>> ---
>>  README           |  1 +
>>  common/Makefile  |  1 +
>>  fs/ext4/ext4fs.c | 15 +++++++++++++++
>>  fs/fs.c          | 39 +++++++++++++++++++++++++++++++++++++++
>>  include/ext4fs.h |  1 +
>>  include/fs.h     |  2 ++
>>  6 files changed, 59 insertions(+)
>>
>> diff --git a/README b/README
>> index 7b5538e..53b84a6 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/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
>> index cbdc220..61b6dc2 100644
>> --- a/fs/ext4/ext4fs.c
>> +++ b/fs/ext4/ext4fs.c
>> @@ -187,6 +187,21 @@ int ext4fs_size(const char *filename)
>>         return ext4fs_open(filename);
>>  }
>>
>> +int ext4fs_uuid(char *uuid_str)
>> +{
>> +       if (ext4fs_root == NULL)
>> +               return -1;
>
> One other tiny note - should that be -ENOENT or some other error perhaps?
>

I used the other functions as inspiration. I think that needs to be in
a follow up commit
as it affects more functions.

greets
--
Christian Gmeiner, MSc

https://soundcloud.com/christian-gmeiner
Christian Gmeiner Nov. 11, 2014, 12:21 p.m. UTC | #8
Hi

2014-11-04 19:41 GMT+01:00 Simon Glass <sjg@chromium.org>:
> Hi Christian,
>
> On 3 November 2014 02:47, Christian Gmeiner <christian.gmeiner@gmail.com> wrote:
>> Hi Simon,
>
> [snip]
>
>>>> +
>>>> +       if (argc == 4)
>>>> +               setenv(argv[3], uuid);
>>>> +       else
>>>> +               printf("%s\n", uuid);
>>>> +
>>>> +       return 0;
>>>> +}
>>>> diff --git a/include/ext4fs.h b/include/ext4fs.h
>>>> index 6c419f3..19816de 100644
>>>> --- a/include/ext4fs.h
>>>> +++ b/include/ext4fs.h
>>>> @@ -137,6 +137,7 @@ void ext4fs_reinit_global(void);
>>>>  int ext4fs_ls(const char *dirname);
>>>>  int ext4fs_exists(const char *filename);
>>>>  int ext4fs_size(const char *filename);
>>>> +int ext4fs_uuid(char *uuid_str);
>>>>  void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
>>>>  int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
>>>>  void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
>>>> diff --git a/include/fs.h b/include/fs.h
>>>> index 06a45f2..41082b3 100644
>>>> --- a/include/fs.h
>>>> +++ b/include/fs.h
>>>> @@ -92,5 +92,7 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file,
>>>>                 int fstype);
>>>>  int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>>>>                 int fstype);
>>>> +int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
>>>> +               int fstype);
>>>
>>> Can you please add a function comment for this?
>>
>> Yes I will add one  - directly here in the .h file?
>
> Yes I think so - this is where the API is
>

Great!

Cheers
--
Christian Gmeiner, MSc

https://soundcloud.com/christian-gmeiner
diff mbox

Patch

diff --git a/README b/README
index 7b5538e..53b84a6 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/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index cbdc220..61b6dc2 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -187,6 +187,21 @@  int ext4fs_size(const char *filename)
 	return ext4fs_open(filename);
 }
 
+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;
+#endif
+
+	return -ENOSYS;
+}
+
 int ext4fs_read(char *buf, unsigned len)
 {
 	if (ext4fs_root == NULL || ext4fs_file == NULL)
diff --git a/fs/fs.c b/fs/fs.c
index dd680f3..492d8b1 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -15,6 +15,7 @@ 
  */
 
 #include <config.h>
+#include <errno.h>
 #include <common.h>
 #include <part.h>
 #include <ext4fs.h>
@@ -86,6 +87,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[] = {
@@ -113,6 +115,7 @@  static struct fstype_info fstypes[] = {
 		.size = ext4fs_size,
 		.read = ext4_read_file,
 		.write = fs_write_unsupported,
+		.uuid = ext4fs_uuid,
 	},
 #endif
 #ifdef CONFIG_SANDBOX
@@ -206,6 +209,17 @@  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);
+	int ret = -ENOSYS;
+
+	if (info->uuid)
+		ret = info->uuid(uuid_str);
+
+	return ret;
+}
+
 int fs_ls(const char *dirname)
 {
 	int ret;
@@ -443,3 +457,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 ret;
+
+	if (argc == 4)
+		setenv(argv[3], uuid);
+	else
+		printf("%s\n", uuid);
+
+	return 0;
+}
diff --git a/include/ext4fs.h b/include/ext4fs.h
index 6c419f3..19816de 100644
--- a/include/ext4fs.h
+++ b/include/ext4fs.h
@@ -137,6 +137,7 @@  void ext4fs_reinit_global(void);
 int ext4fs_ls(const char *dirname);
 int ext4fs_exists(const char *filename);
 int ext4fs_size(const char *filename);
+int ext4fs_uuid(char *uuid_str);
 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
 int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
 void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
diff --git a/include/fs.h b/include/fs.h
index 06a45f2..41082b3 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -92,5 +92,7 @@  int file_exists(const char *dev_type, const char *dev_part, const char *file,
 		int fstype);
 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
 		int fstype);
+int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
+		int fstype);
 
 #endif /* _FS_H */