[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 09/16] partmap/gpt: Add grub_gpt_partition_get_label()
From: |
Vitaly Kuzmichev |
Subject: |
[PATCH v5 09/16] partmap/gpt: Add grub_gpt_partition_get_label() |
Date: |
Tue, 22 Aug 2023 23:39:17 +0200 |
This patch adds a function to provide partition PARTLABEL on GPT
formatted disks by extracting first 72 bytes of UTF-16LE encoded
partition name from corresponding GPT entry and converting it
to UTF-8 string.
Signed-off-by: Vitaly Kuzmichev <vitaly.kuzmichev@rtsoft.de>
---
grub-core/partmap/gpt.c | 36 ++++++++++++++++++++++++++++++++++++
include/grub/gpt_partition.h | 3 +++
2 files changed, 39 insertions(+)
diff --git a/grub-core/partmap/gpt.c b/grub-core/partmap/gpt.c
index b43f46987..451ca2440 100644
--- a/grub-core/partmap/gpt.c
+++ b/grub-core/partmap/gpt.c
@@ -50,6 +50,42 @@ static struct grub_partition_map grub_gpt_partition_map;
+static char *
+get_utf8 (const char *in, grub_size_t len)
+{
+ return (char *) grub_utf16_to_utf8_alloc (in,
+ len / sizeof (grub_uint16_t),
+ UTF16_LE);
+}
+
+char *
+grub_gpt_partition_get_label (grub_device_t dev)
+{
+ struct grub_gpt_partentry gptdata;
+ grub_disk_t disk;
+ grub_partition_t pt;
+ char *part_label = NULL;
+
+ if (dev && dev->disk && dev->disk->partition &&
dev->disk->partition->partmap)
+ {
+ pt = dev->disk->partition;
+ if (grub_strcmp (pt->partmap->name, "gpt") == 0)
+ {
+ disk = grub_disk_open (dev->disk->name);
+ if (disk && grub_disk_read (disk, pt->offset, pt->index,
+ sizeof (gptdata), &gptdata) == 0)
+ part_label = get_utf8 (gptdata.name, sizeof (gptdata.name));
+ if (!part_label)
+ grub_error_push ();
+ grub_disk_close (disk);
+ if (!part_label)
+ grub_error_pop ();
+ }
+ }
+
+ return part_label;
+}
+
char *
grub_gpt_partition_get_uuid (grub_device_t dev)
{
diff --git a/include/grub/gpt_partition.h b/include/grub/gpt_partition.h
index e52834ac2..8917f40ac 100644
--- a/include/grub/gpt_partition.h
+++ b/include/grub/gpt_partition.h
@@ -74,6 +74,9 @@ grub_gpt_partition_map_iterate (grub_disk_t disk,
grub_partition_iterate_hook_t hook,
void *hook_data);
+char *
+grub_gpt_partition_get_label (grub_device_t dev);
+
char *
grub_gpt_partition_get_uuid (grub_device_t dev);
--
2.34.1
Re: [PATCH v5 00/16] Support searching by PARTUUID and PARTLABEL, Daniel Kiper, 2023/08/30