Linux-EFI Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] partitions/efi: Fix partition name parsing in GUID partition entry
@ 2018-11-24 16:21 Nikolai Merinov
  2018-11-25 21:16 ` kbuild test robot
  2019-12-24  9:21 ` [PATCH v2] " Nikolai Merinov
  0 siblings, 2 replies; 13+ messages in thread
From: Nikolai Merinov @ 2018-11-24 16:21 UTC (permalink / raw)
  To: Davidlohr Bueso, linux-efi
  Cc: Jens Axboe, linux-block, linux-kernel, Nikolai Merinov

GUID partition entry defined to have a partition name as 36 UTF-16LE
code units. This means that on big-endian platforms ASCII symbols
would be read with 0xXX00 efi_char16_t character code. In order to
correctly extract ASCII characters from a partition name field we
should be converted from 16LE to CPU architecture.

The problem exists on all big endian platforms.

Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
---
 block/partitions/efi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/partitions/efi.c b/block/partitions/efi.c
index 39f70d968754..ea50ee1505ed 100644
--- a/block/partitions/efi.c
+++ b/block/partitions/efi.c
@@ -729,7 +729,7 @@ int efi_partition(struct parsed_partitions *state)
 				ARRAY_SIZE(ptes[i].partition_name));
 		info->volname[label_max] = 0;
 		while (label_count < label_max) {
-			u8 c = ptes[i].partition_name[label_count] & 0xff;
+			u8 c = le16_to_cpu(ptes[i].partition_name[label_count]) & 0xff;
 			if (c && !isprint(c))
 				c = '!';
 			info->volname[label_count] = c;
-- 
2.14.1

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] partitions/efi: Fix partition name parsing in GUID partition entry
  2018-11-24 16:21 [PATCH] partitions/efi: Fix partition name parsing in GUID partition entry Nikolai Merinov
@ 2018-11-25 21:16 ` kbuild test robot
  2019-12-24  9:21 ` [PATCH v2] " Nikolai Merinov
  1 sibling, 0 replies; 13+ messages in thread
From: kbuild test robot @ 2018-11-25 21:16 UTC (permalink / raw)
  Cc: kbuild-all, Davidlohr Bueso, linux-efi, Jens Axboe, linux-block,
	linux-kernel, Nikolai Merinov

[-- Attachment #1: Type: text/plain, Size: 3474 bytes --]

Hi Nikolai,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on block/for-next]
[also build test WARNING on v4.20-rc3 next-20181123]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Nikolai-Merinov/partitions-efi-Fix-partition-name-parsing-in-GUID-partition-entry/20181125-060728
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: x86_64-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
>> block/partitions/efi.c:732:32: warning: cast to restricted __le16

vim +732 block/partitions/efi.c

   672	
   673	/**
   674	 * efi_partition(struct parsed_partitions *state)
   675	 * @state: disk parsed partitions
   676	 *
   677	 * Description: called from check.c, if the disk contains GPT
   678	 * partitions, sets up partition entries in the kernel.
   679	 *
   680	 * If the first block on the disk is a legacy MBR,
   681	 * it will get handled by msdos_partition().
   682	 * If it's a Protective MBR, we'll handle it here.
   683	 *
   684	 * We do not create a Linux partition for GPT, but
   685	 * only for the actual data partitions.
   686	 * Returns:
   687	 * -1 if unable to read the partition table
   688	 *  0 if this isn't our partition table
   689	 *  1 if successful
   690	 *
   691	 */
   692	int efi_partition(struct parsed_partitions *state)
   693	{
   694		gpt_header *gpt = NULL;
   695		gpt_entry *ptes = NULL;
   696		u32 i;
   697		unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
   698	
   699		if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
   700			kfree(gpt);
   701			kfree(ptes);
   702			return 0;
   703		}
   704	
   705		pr_debug("GUID Partition Table is valid!  Yea!\n");
   706	
   707		for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
   708			struct partition_meta_info *info;
   709			unsigned label_count = 0;
   710			unsigned label_max;
   711			u64 start = le64_to_cpu(ptes[i].starting_lba);
   712			u64 size = le64_to_cpu(ptes[i].ending_lba) -
   713				   le64_to_cpu(ptes[i].starting_lba) + 1ULL;
   714	
   715			if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
   716				continue;
   717	
   718			put_partition(state, i+1, start * ssz, size * ssz);
   719	
   720			/* If this is a RAID volume, tell md */
   721			if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID))
   722				state->parts[i + 1].flags = ADDPART_FLAG_RAID;
   723	
   724			info = &state->parts[i + 1].info;
   725			efi_guid_to_str(&ptes[i].unique_partition_guid, info->uuid);
   726	
   727			/* Naively convert UTF16-LE to 7 bits. */
   728			label_max = min(ARRAY_SIZE(info->volname) - 1,
   729					ARRAY_SIZE(ptes[i].partition_name));
   730			info->volname[label_max] = 0;
   731			while (label_count < label_max) {
 > 732				u8 c = le16_to_cpu(ptes[i].partition_name[label_count]) & 0xff;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 66576 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v2] partitions/efi: Fix partition name parsing in GUID partition entry
  2018-11-24 16:21 [PATCH] partitions/efi: Fix partition name parsing in GUID partition entry Nikolai Merinov
  2018-11-25 21:16 ` kbuild test robot
@ 2019-12-24  9:21 ` " Nikolai Merinov
  2019-12-24  9:51   ` Ard Biesheuvel
  2020-01-08 13:39   ` Christoph Hellwig
  1 sibling, 2 replies; 13+ messages in thread
From: Nikolai Merinov @ 2019-12-24  9:21 UTC (permalink / raw)
  To: Davidlohr Bueso, Jens Axboe, linux-efi, linux-block, linux-kernel
  Cc: Nikolai Merinov

GUID partition entry defined to have a partition name as 36 UTF-16LE
code units. This means that on big-endian platforms ASCII symbols
would be read with 0xXX00 efi_char16_t character code. In order to
correctly extract ASCII characters from a partition name field we
should be converted from 16LE to CPU architecture.

The problem exists on all big endian platforms.

Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>

diff --git a/block/partitions/efi.c b/block/partitions/efi.c
index db2fef7dfc47..51287a8a3bea 100644
--- a/block/partitions/efi.c
+++ b/block/partitions/efi.c
@@ -715,7 +715,7 @@ int efi_partition(struct parsed_partitions *state)
 				ARRAY_SIZE(ptes[i].partition_name));
 		info->volname[label_max] = 0;
 		while (label_count < label_max) {
-			u8 c = ptes[i].partition_name[label_count] & 0xff;
+			u8 c = le16_to_cpu(ptes[i].partition_name[label_count]) & 0xff;
 			if (c && !isprint(c))
 				c = '!';
 			info->volname[label_count] = c;
diff --git a/block/partitions/efi.h b/block/partitions/efi.h
index 3e8576157575..0b6d5b7be111 100644
--- a/block/partitions/efi.h
+++ b/block/partitions/efi.h
@@ -88,7 +88,7 @@ typedef struct _gpt_entry {
 	__le64 starting_lba;
 	__le64 ending_lba;
 	gpt_entry_attributes attributes;
-	efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
+	__le16 partition_name[72 / sizeof (__le16)];
 } __packed gpt_entry;
 
 typedef struct _gpt_mbr_record {
-- 
2.17.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] partitions/efi: Fix partition name parsing in GUID partition entry
  2019-12-24  9:21 ` [PATCH v2] " Nikolai Merinov
@ 2019-12-24  9:51   ` Ard Biesheuvel
  2020-01-08 13:39   ` Christoph Hellwig
  1 sibling, 0 replies; 13+ messages in thread
From: Ard Biesheuvel @ 2019-12-24  9:51 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: Davidlohr Bueso, Jens Axboe, linux-efi, linux-block,
	Linux Kernel Mailing List

On Tue, 24 Dec 2019 at 10:29, Nikolai Merinov
<n.merinov@inango-systems.com> wrote:
>
> GUID partition entry defined to have a partition name as 36 UTF-16LE
> code units. This means that on big-endian platforms ASCII symbols
> would be read with 0xXX00 efi_char16_t character code. In order to
> correctly extract ASCII characters from a partition name field we
> should be converted from 16LE to CPU architecture.
>
> The problem exists on all big endian platforms.
>
> Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
>

Acked-by: Ard Biesheuvel <ardb@kernel.org>

> diff --git a/block/partitions/efi.c b/block/partitions/efi.c
> index db2fef7dfc47..51287a8a3bea 100644
> --- a/block/partitions/efi.c
> +++ b/block/partitions/efi.c
> @@ -715,7 +715,7 @@ int efi_partition(struct parsed_partitions *state)
>                                 ARRAY_SIZE(ptes[i].partition_name));
>                 info->volname[label_max] = 0;
>                 while (label_count < label_max) {
> -                       u8 c = ptes[i].partition_name[label_count] & 0xff;
> +                       u8 c = le16_to_cpu(ptes[i].partition_name[label_count]) & 0xff;
>                         if (c && !isprint(c))
>                                 c = '!';
>                         info->volname[label_count] = c;
> diff --git a/block/partitions/efi.h b/block/partitions/efi.h
> index 3e8576157575..0b6d5b7be111 100644
> --- a/block/partitions/efi.h
> +++ b/block/partitions/efi.h
> @@ -88,7 +88,7 @@ typedef struct _gpt_entry {
>         __le64 starting_lba;
>         __le64 ending_lba;
>         gpt_entry_attributes attributes;
> -       efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
> +       __le16 partition_name[72 / sizeof (__le16)];
>  } __packed gpt_entry;
>
>  typedef struct _gpt_mbr_record {
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] partitions/efi: Fix partition name parsing in GUID partition entry
  2019-12-24  9:21 ` [PATCH v2] " Nikolai Merinov
  2019-12-24  9:51   ` Ard Biesheuvel
@ 2020-01-08 13:39   ` Christoph Hellwig
  2020-01-13 10:27     ` [PATCH v3] " Nikolai Merinov
  1 sibling, 1 reply; 13+ messages in thread
From: Christoph Hellwig @ 2020-01-08 13:39 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: Davidlohr Bueso, Jens Axboe, linux-efi, linux-block, linux-kernel

> index db2fef7dfc47..51287a8a3bea 100644
> --- a/block/partitions/efi.c
> +++ b/block/partitions/efi.c
> @@ -715,7 +715,7 @@ int efi_partition(struct parsed_partitions *state)
>  				ARRAY_SIZE(ptes[i].partition_name));
>  		info->volname[label_max] = 0;
>  		while (label_count < label_max) {
> -			u8 c = ptes[i].partition_name[label_count] & 0xff;
> +			u8 c = le16_to_cpu(ptes[i].partition_name[label_count]) & 0xff;
>  			if (c && !isprint(c))
>  				c = '!';
>
This adds an overly long line.  Please add a an efi_char_from_cpu or
similarly named helper to encapsulate this logic.

Otherwise the change looks good.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-01-08 13:39   ` Christoph Hellwig
@ 2020-01-13 10:27     ` " Nikolai Merinov
  2020-02-18 13:34       ` Nikolai Merinov
  2020-02-18 18:53       ` Christoph Hellwig
  0 siblings, 2 replies; 13+ messages in thread
From: Nikolai Merinov @ 2020-01-13 10:27 UTC (permalink / raw)
  To: hch, Davidlohr Bueso, Jens Axboe, Ard Biesheuvel, linux-efi,
	linux-block, linux-kernel
  Cc: Nikolai Merinov

GUID partition entry defined to have a partition name as 36 UTF-16LE
code units. This means that on big-endian platforms ASCII symbols
would be read with 0xXX00 efi_char16_t character code. In order to
correctly extract ASCII characters from a partition name field we
should be converted from 16LE to CPU architecture.

The problem exists on all big endian platforms.

Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
---
 block/partitions/efi.c | 3 ++-
 block/partitions/efi.h | 2 +-
 include/linux/efi.h    | 5 +++++
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/block/partitions/efi.c b/block/partitions/efi.c
index db2fef7dfc47..f1d0820de844 100644
--- a/block/partitions/efi.c
+++ b/block/partitions/efi.c
@@ -715,7 +715,8 @@ int efi_partition(struct parsed_partitions *state)
 				ARRAY_SIZE(ptes[i].partition_name));
 		info->volname[label_max] = 0;
 		while (label_count < label_max) {
-			u8 c = ptes[i].partition_name[label_count] & 0xff;
+			u8 c = 0xff & efi_char16le_to_cpu(
+					ptes[i].partition_name[label_count]);
 			if (c && !isprint(c))
 				c = '!';
 			info->volname[label_count] = c;
diff --git a/block/partitions/efi.h b/block/partitions/efi.h
index 3e8576157575..4d4cae0bb79e 100644
--- a/block/partitions/efi.h
+++ b/block/partitions/efi.h
@@ -88,7 +88,7 @@ typedef struct _gpt_entry {
 	__le64 starting_lba;
 	__le64 ending_lba;
 	gpt_entry_attributes attributes;
-	efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
+	efi_char16le_t partition_name[72 / sizeof(efi_char16le_t)];
 } __packed gpt_entry;
 
 typedef struct _gpt_mbr_record {
diff --git a/include/linux/efi.h b/include/linux/efi.h
index aa54586db7a5..47882f2d45db 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -45,9 +45,14 @@
 typedef unsigned long efi_status_t;
 typedef u8 efi_bool_t;
 typedef u16 efi_char16_t;		/* UNICODE character */
+typedef __le16 efi_char16le_t;		/* UTF16-LE */
+typedef __be16 efi_char16be_t;		/* UTF16-BE */
 typedef u64 efi_physical_addr_t;
 typedef void *efi_handle_t;
 
+#define efi_char16le_to_cpu le16_to_cpu
+#define efi_char16be_to_cpu be16_to_cpu
+
 /*
  * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
  * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
-- 
2.17.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-01-13 10:27     ` [PATCH v3] " Nikolai Merinov
@ 2020-02-18 13:34       ` Nikolai Merinov
  2020-02-18 18:53       ` Christoph Hellwig
  1 sibling, 0 replies; 13+ messages in thread
From: Nikolai Merinov @ 2020-02-18 13:34 UTC (permalink / raw)
  To: hch, Davidlohr Bueso, Jens Axboe, Ard Biesheuvel
  Cc: linux-efi, linux-block, linux-kernel

Hello,

Did you have a time to look at this patch? Should I make any modification? 

Regards,
Nikolai

----- Original Message -----
> From: "n merinov" <n.merinov@inango-systems.com>
> To: hch@infradead.org, "Davidlohr Bueso" <dave@stgolabs.net>, "Jens Axboe" <axboe@kernel.dk>, "Ard Biesheuvel"
> <ardb@kernel.org>, linux-efi@vger.kernel.org, linux-block@vger.kernel.org, "linux-kernel"
> <linux-kernel@vger.kernel.org>
> Cc: "n merinov" <n.merinov@inango-systems.com>
> Sent: Monday, January 13, 2020 3:27:23 PM
> Subject: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry

> GUID partition entry defined to have a partition name as 36 UTF-16LE
> code units. This means that on big-endian platforms ASCII symbols
> would be read with 0xXX00 efi_char16_t character code. In order to
> correctly extract ASCII characters from a partition name field we
> should be converted from 16LE to CPU architecture.
> 
> The problem exists on all big endian platforms.
> 
> Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
> ---
> block/partitions/efi.c | 3 ++-
> block/partitions/efi.h | 2 +-
> include/linux/efi.h    | 5 +++++
> 3 files changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/block/partitions/efi.c b/block/partitions/efi.c
> index db2fef7dfc47..f1d0820de844 100644
> --- a/block/partitions/efi.c
> +++ b/block/partitions/efi.c
> @@ -715,7 +715,8 @@ int efi_partition(struct parsed_partitions *state)
> 				ARRAY_SIZE(ptes[i].partition_name));
> 		info->volname[label_max] = 0;
> 		while (label_count < label_max) {
> -			u8 c = ptes[i].partition_name[label_count] & 0xff;
> +			u8 c = 0xff & efi_char16le_to_cpu(
> +					ptes[i].partition_name[label_count]);
> 			if (c && !isprint(c))
> 				c = '!';
> 			info->volname[label_count] = c;
> diff --git a/block/partitions/efi.h b/block/partitions/efi.h
> index 3e8576157575..4d4cae0bb79e 100644
> --- a/block/partitions/efi.h
> +++ b/block/partitions/efi.h
> @@ -88,7 +88,7 @@ typedef struct _gpt_entry {
> 	__le64 starting_lba;
> 	__le64 ending_lba;
> 	gpt_entry_attributes attributes;
> -	efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
> +	efi_char16le_t partition_name[72 / sizeof(efi_char16le_t)];
> } __packed gpt_entry;
> 
> typedef struct _gpt_mbr_record {
> diff --git a/include/linux/efi.h b/include/linux/efi.h
> index aa54586db7a5..47882f2d45db 100644
> --- a/include/linux/efi.h
> +++ b/include/linux/efi.h
> @@ -45,9 +45,14 @@
> typedef unsigned long efi_status_t;
> typedef u8 efi_bool_t;
> typedef u16 efi_char16_t;		/* UNICODE character */
> +typedef __le16 efi_char16le_t;		/* UTF16-LE */
> +typedef __be16 efi_char16be_t;		/* UTF16-BE */
> typedef u64 efi_physical_addr_t;
> typedef void *efi_handle_t;
> 
> +#define efi_char16le_to_cpu le16_to_cpu
> +#define efi_char16be_to_cpu be16_to_cpu
> +
> /*
>  * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
>  * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
> --
> 2.17.1

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-01-13 10:27     ` [PATCH v3] " Nikolai Merinov
  2020-02-18 13:34       ` Nikolai Merinov
@ 2020-02-18 18:53       ` Christoph Hellwig
  2020-02-24 11:38         ` Nikolai Merinov
  1 sibling, 1 reply; 13+ messages in thread
From: Christoph Hellwig @ 2020-02-18 18:53 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: hch, Davidlohr Bueso, Jens Axboe, Ard Biesheuvel, linux-efi,
	linux-block, linux-kernel

On Mon, Jan 13, 2020 at 03:27:23PM +0500, Nikolai Merinov wrote:
> GUID partition entry defined to have a partition name as 36 UTF-16LE
> code units. This means that on big-endian platforms ASCII symbols
> would be read with 0xXX00 efi_char16_t character code. In order to
> correctly extract ASCII characters from a partition name field we
> should be converted from 16LE to CPU architecture.
> 
> The problem exists on all big endian platforms.
> 
> Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
> ---
>  block/partitions/efi.c | 3 ++-
>  block/partitions/efi.h | 2 +-
>  include/linux/efi.h    | 5 +++++
>  3 files changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/block/partitions/efi.c b/block/partitions/efi.c
> index db2fef7dfc47..f1d0820de844 100644
> --- a/block/partitions/efi.c
> +++ b/block/partitions/efi.c
> @@ -715,7 +715,8 @@ int efi_partition(struct parsed_partitions *state)
>  				ARRAY_SIZE(ptes[i].partition_name));
>  		info->volname[label_max] = 0;
>  		while (label_count < label_max) {
> -			u8 c = ptes[i].partition_name[label_count] & 0xff;
> +			u8 c = 0xff & efi_char16le_to_cpu(
> +					ptes[i].partition_name[label_count]);

Why are you swapping the order of the comparism to an unusual one here?

> -	efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
> +	efi_char16le_t partition_name[72 / sizeof(efi_char16le_t)];
>  } __packed gpt_entry;
>  
>  typedef struct _gpt_mbr_record {
> diff --git a/include/linux/efi.h b/include/linux/efi.h
> index aa54586db7a5..47882f2d45db 100644
> --- a/include/linux/efi.h
> +++ b/include/linux/efi.h
> @@ -45,9 +45,14 @@
>  typedef unsigned long efi_status_t;
>  typedef u8 efi_bool_t;
>  typedef u16 efi_char16_t;		/* UNICODE character */
> +typedef __le16 efi_char16le_t;		/* UTF16-LE */
> +typedef __be16 efi_char16be_t;		/* UTF16-BE */
>  typedef u64 efi_physical_addr_t;
>  typedef void *efi_handle_t;
>  
> +#define efi_char16le_to_cpu le16_to_cpu
> +#define efi_char16be_to_cpu be16_to_cpu

I'd rather use plain __le16 and le16_to_cpu here.  Also the be
variants seems to be entirely unused.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-02-18 18:53       ` Christoph Hellwig
@ 2020-02-24 11:38         ` Nikolai Merinov
  2020-02-24 17:08           ` hch
  0 siblings, 1 reply; 13+ messages in thread
From: Nikolai Merinov @ 2020-02-24 11:38 UTC (permalink / raw)
  To: hch
  Cc: Davidlohr Bueso, Jens Axboe, Ard Biesheuvel, linux-efi,
	linux-block, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 549 bytes --]

Hi Christoph, 

> I'd rather use plain __le16 and le16_to_cpu here. Also the be 
> variants seems to be entirely unused. 

Looks like I misunderstood your comment from https://patchwork.kernel.org/patch/11309223/: 

> Please add a an efi_char_from_cpu or similarly named helper 
> to encapsulate this logic. 

The "le16_to_cpu(ptes[i].partition_name[label_count])" call is the 
full implementation of the "efi_char_from_cpu" logic. Do you want 
to encapsulate "utf16_le_to_7bit_string" logic entirely like in
the attached version?

Regards,
Nikolai

[-- Warning: decoded text below may be mangled --]
[-- Attachment #2: v4-0001-partitions-efi-Fix-partition-name-parsing-in-GUID.patch --]
[-- Type: text/x-patch; name=v4-0001-partitions-efi-Fix-partition-name-parsing-in-GUID.patch, Size: 3292 bytes --]

From 842cf22d6f6f91872bcb04ac6abe7794fede23fd Mon Sep 17 00:00:00 2001
From: Nikolai Merinov <n.merinov@inango-systems.com>
Date: Sat, 24 Nov 2018 20:42:27 +0500
Subject: [PATCH v4] partitions/efi: Fix partition name parsing in GUID
 partition entry

GUID partition entry defined to have a partition name as 36 UTF-16LE
code units. This means that on big-endian platforms ASCII symbols
would be read with 0xXX00 efi_char16_t character code. In order to
correctly extract ASCII characters from a partition name field we
should be converted from 16LE to CPU architecture.

The problem exists on all big endian platforms.

Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
Fixes: eec7ecfede74 ("genhd, efi: add efi partition metadata to hd_structs")
---
 block/partitions/efi.c | 35 ++++++++++++++++++++++++++---------
 block/partitions/efi.h |  2 +-
 2 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/block/partitions/efi.c b/block/partitions/efi.c
index db2fef7dfc47..d26a0654d7ca 100644
--- a/block/partitions/efi.c
+++ b/block/partitions/efi.c
@@ -656,6 +656,30 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
         return 0;
 }
 
+/**
+ * utf16_le_to_7bit(): Naively converts UTF-16LE string to 7bit characters
+ * @in: input UTF-16LE string
+ * @size: size of the input string
+ * @out: output string ptr, should be capable to store @size+1 characters
+ *
+ * Description: Converts @size UTF16-LE symbols from @in string to 7bit
+ * characters and store them to @out. Adds trailing zero to @out array.
+ */
+static void utf16_le_to_7bit(const __le16 *in, unsigned int size, u8 *out)
+{
+	unsigned int i = 0;
+
+	out[size] = 0;
+	while (i < size) {
+		u8 c = le16_to_cpu(in[i]) & 0xff;
+
+		if (c && !isprint(c))
+			c = '!';
+		out[i] = c;
+		i++;
+	}
+}
+
 /**
  * efi_partition(struct parsed_partitions *state)
  * @state: disk parsed partitions
@@ -692,7 +716,6 @@ int efi_partition(struct parsed_partitions *state)
 
 	for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
 		struct partition_meta_info *info;
-		unsigned label_count = 0;
 		unsigned label_max;
 		u64 start = le64_to_cpu(ptes[i].starting_lba);
 		u64 size = le64_to_cpu(ptes[i].ending_lba) -
@@ -713,14 +736,8 @@ int efi_partition(struct parsed_partitions *state)
 		/* Naively convert UTF16-LE to 7 bits. */
 		label_max = min(ARRAY_SIZE(info->volname) - 1,
 				ARRAY_SIZE(ptes[i].partition_name));
-		info->volname[label_max] = 0;
-		while (label_count < label_max) {
-			u8 c = ptes[i].partition_name[label_count] & 0xff;
-			if (c && !isprint(c))
-				c = '!';
-			info->volname[label_count] = c;
-			label_count++;
-		}
+		utf16_le_to_7bit(ptes[i].partition_name, label_max,
+				 info->volname);
 		state->parts[i + 1].has_info = true;
 	}
 	kfree(ptes);
diff --git a/block/partitions/efi.h b/block/partitions/efi.h
index 3e8576157575..0b6d5b7be111 100644
--- a/block/partitions/efi.h
+++ b/block/partitions/efi.h
@@ -88,7 +88,7 @@ typedef struct _gpt_entry {
 	__le64 starting_lba;
 	__le64 ending_lba;
 	gpt_entry_attributes attributes;
-	efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
+	__le16 partition_name[72 / sizeof (__le16)];
 } __packed gpt_entry;
 
 typedef struct _gpt_mbr_record {
-- 
2.17.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-02-24 11:38         ` Nikolai Merinov
@ 2020-02-24 17:08           ` hch
  2020-03-06  8:43             ` Nikolai Merinov
  0 siblings, 1 reply; 13+ messages in thread
From: hch @ 2020-02-24 17:08 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: hch, Davidlohr Bueso, Jens Axboe, Ard Biesheuvel, linux-efi,
	linux-block, linux-kernel

On Mon, Feb 24, 2020 at 01:38:39PM +0200, Nikolai Merinov wrote:
> Hi Christoph, 
> 
> > I'd rather use plain __le16 and le16_to_cpu here. Also the be 
> > variants seems to be entirely unused. 
> 
> Looks like I misunderstood your comment from https://patchwork.kernel.org/patch/11309223/: 
> 
> > Please add a an efi_char_from_cpu or similarly named helper 
> > to encapsulate this logic. 
> 
> The "le16_to_cpu(ptes[i].partition_name[label_count])" call is the 
> full implementation of the "efi_char_from_cpu" logic. Do you want 
> to encapsulate "utf16_le_to_7bit_string" logic entirely like in
> the attached version?

I think I though of just the inner loop, but your new version looks even
better, so:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-02-24 17:08           ` hch
@ 2020-03-06  8:43             ` Nikolai Merinov
  2020-03-06  9:25               ` Ard Biesheuvel
  0 siblings, 1 reply; 13+ messages in thread
From: Nikolai Merinov @ 2020-03-06  8:43 UTC (permalink / raw)
  To: hch
  Cc: Davidlohr Bueso, Jens Axboe, Ard Biesheuvel, linux-efi,
	linux-block, linux-kernel

Hi Christoph,

Should I perform any other steps in order to get this change in the master?

Regards,
Nikolai

----- Original Message -----
> From: "hch" <hch@infradead.org>
> To: "n merinov" <n.merinov@inango-systems.com>
> Cc: "hch" <hch@infradead.org>, "Davidlohr Bueso" <dave@stgolabs.net>, "Jens Axboe" <axboe@kernel.dk>, "Ard Biesheuvel"
> <ardb@kernel.org>, "linux-efi" <linux-efi@vger.kernel.org>, "linux-block" <linux-block@vger.kernel.org>, "linux-kernel"
> <linux-kernel@vger.kernel.org>
> Sent: Monday, February 24, 2020 10:08:13 PM
> Subject: Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry

> On Mon, Feb 24, 2020 at 01:38:39PM +0200, Nikolai Merinov wrote:
>> Hi Christoph,
>> 
>> > I'd rather use plain __le16 and le16_to_cpu here. Also the be
>> > variants seems to be entirely unused.
>> 
>> Looks like I misunderstood your comment from
>> https://patchwork.kernel.org/patch/11309223/:
>> 
>> > Please add a an efi_char_from_cpu or similarly named helper
>> > to encapsulate this logic.
>> 
>> The "le16_to_cpu(ptes[i].partition_name[label_count])" call is the
>> full implementation of the "efi_char_from_cpu" logic. Do you want
>> to encapsulate "utf16_le_to_7bit_string" logic entirely like in
>> the attached version?
> 
> I think I though of just the inner loop, but your new version looks even
> better, so:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-03-06  8:43             ` Nikolai Merinov
@ 2020-03-06  9:25               ` Ard Biesheuvel
  2020-03-06 10:14                 ` Ard Biesheuvel
  0 siblings, 1 reply; 13+ messages in thread
From: Ard Biesheuvel @ 2020-03-06  9:25 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: hch, Davidlohr Bueso, Jens Axboe, linux-efi, linux-block, linux-kernel

On Fri, 6 Mar 2020 at 09:43, Nikolai Merinov
<n.merinov@inango-systems.com> wrote:
>
> Hi Christoph,
>
> Should I perform any other steps in order to get this change in the master?
>

I can take it via the EFI tree with an ack from Dave.


>
> ----- Original Message -----
> > From: "hch" <hch@infradead.org>
> > To: "n merinov" <n.merinov@inango-systems.com>
> > Cc: "hch" <hch@infradead.org>, "Davidlohr Bueso" <dave@stgolabs.net>, "Jens Axboe" <axboe@kernel.dk>, "Ard Biesheuvel"
> > <ardb@kernel.org>, "linux-efi" <linux-efi@vger.kernel.org>, "linux-block" <linux-block@vger.kernel.org>, "linux-kernel"
> > <linux-kernel@vger.kernel.org>
> > Sent: Monday, February 24, 2020 10:08:13 PM
> > Subject: Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
>
> > On Mon, Feb 24, 2020 at 01:38:39PM +0200, Nikolai Merinov wrote:
> >> Hi Christoph,
> >>
> >> > I'd rather use plain __le16 and le16_to_cpu here. Also the be
> >> > variants seems to be entirely unused.
> >>
> >> Looks like I misunderstood your comment from
> >> https://patchwork.kernel.org/patch/11309223/:
> >>
> >> > Please add a an efi_char_from_cpu or similarly named helper
> >> > to encapsulate this logic.
> >>
> >> The "le16_to_cpu(ptes[i].partition_name[label_count])" call is the
> >> full implementation of the "efi_char_from_cpu" logic. Do you want
> >> to encapsulate "utf16_le_to_7bit_string" logic entirely like in
> >> the attached version?
> >
> > I think I though of just the inner loop, but your new version looks even
> > better, so:
> >
> > Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
  2020-03-06  9:25               ` Ard Biesheuvel
@ 2020-03-06 10:14                 ` Ard Biesheuvel
  0 siblings, 0 replies; 13+ messages in thread
From: Ard Biesheuvel @ 2020-03-06 10:14 UTC (permalink / raw)
  To: Nikolai Merinov
  Cc: hch, Davidlohr Bueso, Jens Axboe, linux-efi, linux-block, linux-kernel

On Fri, 6 Mar 2020 at 10:25, Ard Biesheuvel <ardb@kernel.org> wrote:
>
> On Fri, 6 Mar 2020 at 09:43, Nikolai Merinov
> <n.merinov@inango-systems.com> wrote:
> >
> > Hi Christoph,
> >
> > Should I perform any other steps in order to get this change in the master?
> >
>
> I can take it via the EFI tree with an ack from Dave.
>

... or actually, I'm sure Dave is fine with it, so I'll just queue it
in efi/next directly (with Christoph's ack)

>
> >
> > ----- Original Message -----
> > > From: "hch" <hch@infradead.org>
> > > To: "n merinov" <n.merinov@inango-systems.com>
> > > Cc: "hch" <hch@infradead.org>, "Davidlohr Bueso" <dave@stgolabs.net>, "Jens Axboe" <axboe@kernel.dk>, "Ard Biesheuvel"
> > > <ardb@kernel.org>, "linux-efi" <linux-efi@vger.kernel.org>, "linux-block" <linux-block@vger.kernel.org>, "linux-kernel"
> > > <linux-kernel@vger.kernel.org>
> > > Sent: Monday, February 24, 2020 10:08:13 PM
> > > Subject: Re: [PATCH v3] partitions/efi: Fix partition name parsing in GUID partition entry
> >
> > > On Mon, Feb 24, 2020 at 01:38:39PM +0200, Nikolai Merinov wrote:
> > >> Hi Christoph,
> > >>
> > >> > I'd rather use plain __le16 and le16_to_cpu here. Also the be
> > >> > variants seems to be entirely unused.
> > >>
> > >> Looks like I misunderstood your comment from
> > >> https://patchwork.kernel.org/patch/11309223/:
> > >>
> > >> > Please add a an efi_char_from_cpu or similarly named helper
> > >> > to encapsulate this logic.
> > >>
> > >> The "le16_to_cpu(ptes[i].partition_name[label_count])" call is the
> > >> full implementation of the "efi_char_from_cpu" logic. Do you want
> > >> to encapsulate "utf16_le_to_7bit_string" logic entirely like in
> > >> the attached version?
> > >
> > > I think I though of just the inner loop, but your new version looks even
> > > better, so:
> > >
> > > Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, back to index

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-24 16:21 [PATCH] partitions/efi: Fix partition name parsing in GUID partition entry Nikolai Merinov
2018-11-25 21:16 ` kbuild test robot
2019-12-24  9:21 ` [PATCH v2] " Nikolai Merinov
2019-12-24  9:51   ` Ard Biesheuvel
2020-01-08 13:39   ` Christoph Hellwig
2020-01-13 10:27     ` [PATCH v3] " Nikolai Merinov
2020-02-18 13:34       ` Nikolai Merinov
2020-02-18 18:53       ` Christoph Hellwig
2020-02-24 11:38         ` Nikolai Merinov
2020-02-24 17:08           ` hch
2020-03-06  8:43             ` Nikolai Merinov
2020-03-06  9:25               ` Ard Biesheuvel
2020-03-06 10:14                 ` Ard Biesheuvel

Linux-EFI Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-efi/0 linux-efi/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-efi linux-efi/ https://lore.kernel.org/linux-efi \
		linux-efi@vger.kernel.org
	public-inbox-index linux-efi

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-efi


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git