bbc6a8
From 435fa75e01ef40917239c7f775e505e86f70d202 Mon Sep 17 00:00:00 2001
bbc6a8
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <pali.rohar@gmail.com>
bbc6a8
Date: Thu, 22 Jun 2017 14:42:16 +0200
31cddd
Subject: [PATCH] * grub-core/fs/udf.c: Add support for UUID
bbc6a8
bbc6a8
Use same algorithm as in libblkid from util-linux v2.30.
bbc6a8
bbc6a8
1. Take first 16 bytes from UTF-8 encoded string of VolumeSetIdentifier
bbc6a8
2. If all bytes are hexadecimal digits, convert to lowercase and use as UUID
bbc6a8
3. If first 8 bytes are not all hexadecimal digits, convert those 8 bytes
bbc6a8
   to their hexadecimal representation, resulting in 16 bytes for UUID
bbc6a8
4. Otherwise, compose UUID from two parts:
bbc6a8
   1. part: converted first 8 bytes (which are hexadecimal digits) to lowercase
bbc6a8
   2. part: encoded following 4 bytes to their hexadecimal representation (16 bytes)
bbc6a8
bbc6a8
So UUID would always have 16 hexadecimal digits in lowercase variant.
bbc6a8
bbc6a8
According to UDF specification, first 16 Unicode characters of
bbc6a8
VolumeSetIdentifier should be unique value and first 8 should be
bbc6a8
hexadecimal characters.
bbc6a8
bbc6a8
In most cases all 16 characters are hexadecimal, but e.g. MS Windows
bbc6a8
format.exe set only first 8 as hexadecimal and remaining as fixed
bbc6a8
(non-unique) which violates specification.
bbc6a8
---
bbc6a8
 grub-core/fs/udf.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
bbc6a8
 1 file changed, 120 insertions(+), 1 deletion(-)
bbc6a8
bbc6a8
diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c
ec4acb
index 2587456336e..00a16098b47 100644
bbc6a8
--- a/grub-core/fs/udf.c
bbc6a8
+++ b/grub-core/fs/udf.c
bbc6a8
@@ -321,6 +321,32 @@ struct grub_udf_partmap
bbc6a8
   };
bbc6a8
 } GRUB_PACKED;
bbc6a8
 
bbc6a8
+struct grub_udf_pvd
bbc6a8
+{
bbc6a8
+  struct grub_udf_tag tag;
bbc6a8
+  grub_uint32_t seq_num;
bbc6a8
+  grub_uint32_t pvd_num;
bbc6a8
+  grub_uint8_t ident[32];
bbc6a8
+  grub_uint16_t vol_seq_num;
bbc6a8
+  grub_uint16_t max_vol_seq_num;
bbc6a8
+  grub_uint16_t interchange_level;
bbc6a8
+  grub_uint16_t max_interchange_level;
bbc6a8
+  grub_uint32_t charset_list;
bbc6a8
+  grub_uint32_t max_charset_list;
bbc6a8
+  grub_uint8_t volset_ident[128];
bbc6a8
+  struct grub_udf_charspec desc_charset;
bbc6a8
+  struct grub_udf_charspec expl_charset;
bbc6a8
+  struct grub_udf_extent_ad vol_abstract;
bbc6a8
+  struct grub_udf_extent_ad vol_copyright;
bbc6a8
+  struct grub_udf_regid app_ident;
bbc6a8
+  struct grub_udf_timestamp recording_time;
bbc6a8
+  struct grub_udf_regid imp_ident;
bbc6a8
+  grub_uint8_t imp_use[64];
bbc6a8
+  grub_uint32_t pred_vds_loc;
bbc6a8
+  grub_uint16_t flags;
bbc6a8
+  grub_uint8_t reserved[22];
bbc6a8
+} GRUB_PACKED;
bbc6a8
+
bbc6a8
 struct grub_udf_lvd
bbc6a8
 {
bbc6a8
   struct grub_udf_tag tag;
bbc6a8
@@ -348,6 +374,7 @@ struct grub_udf_aed
bbc6a8
 struct grub_udf_data
bbc6a8
 {
bbc6a8
   grub_disk_t disk;
bbc6a8
+  struct grub_udf_pvd pvd;
bbc6a8
   struct grub_udf_lvd lvd;
bbc6a8
   struct grub_udf_pd pds[GRUB_UDF_MAX_PDS];
bbc6a8
   struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS];
bbc6a8
@@ -692,7 +719,17 @@ grub_udf_mount (grub_disk_t disk)
bbc6a8
 	}
bbc6a8
 
bbc6a8
       tag.tag_ident = U16 (tag.tag_ident);
bbc6a8
-      if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
bbc6a8
+      if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PVD)
bbc6a8
+	{
bbc6a8
+	  if (grub_disk_read (disk, block << lbshift, 0,
bbc6a8
+			      sizeof (struct grub_udf_pvd),
bbc6a8
+			      &data->pvd))
bbc6a8
+	    {
bbc6a8
+	      grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem");
bbc6a8
+	      goto fail;
bbc6a8
+	    }
bbc6a8
+	}
bbc6a8
+      else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD)
bbc6a8
 	{
bbc6a8
 	  if (data->npd >= GRUB_UDF_MAX_PDS)
bbc6a8
 	    {
bbc6a8
@@ -1225,6 +1262,87 @@ grub_udf_label (grub_device_t device, char **label)
bbc6a8
   return grub_errno;
bbc6a8
 }
bbc6a8
 
bbc6a8
+static char *
bbc6a8
+gen_uuid_from_volset (char *volset_ident)
bbc6a8
+{
bbc6a8
+  grub_size_t i;
bbc6a8
+  grub_size_t len;
bbc6a8
+  grub_size_t nonhexpos;
bbc6a8
+  grub_uint8_t buf[17];
bbc6a8
+  char *uuid;
bbc6a8
+
bbc6a8
+  len = grub_strlen (volset_ident);
bbc6a8
+  if (len < 8)
bbc6a8
+    return NULL;
bbc6a8
+
bbc6a8
+  uuid = grub_malloc (17);
bbc6a8
+  if (!uuid)
bbc6a8
+    return NULL;
bbc6a8
+
bbc6a8
+  if (len > 16)
bbc6a8
+    len = 16;
bbc6a8
+
bbc6a8
+  grub_memset (buf, 0, sizeof (buf));
bbc6a8
+  grub_memcpy (buf, volset_ident, len);
bbc6a8
+
bbc6a8
+  nonhexpos = 16;
bbc6a8
+  for (i = 0; i < 16; ++i)
bbc6a8
+    {
bbc6a8
+      if (!grub_isxdigit (buf[i]))
bbc6a8
+        {
bbc6a8
+          nonhexpos = i;
bbc6a8
+          break;
bbc6a8
+        }
bbc6a8
+    }
bbc6a8
+
bbc6a8
+  if (nonhexpos < 8)
bbc6a8
+    {
bbc6a8
+      grub_snprintf (uuid, 17, "%02x%02x%02x%02x%02x%02x%02x%02x",
bbc6a8
+                    buf[0], buf[1], buf[2], buf[3],
bbc6a8
+                    buf[4], buf[5], buf[6], buf[7]);
bbc6a8
+    }
bbc6a8
+  else if (nonhexpos < 16)
bbc6a8
+    {
bbc6a8
+      for (i = 0; i < 8; ++i)
bbc6a8
+        uuid[i] = grub_tolower (buf[i]);
bbc6a8
+      grub_snprintf (uuid+8, 9, "%02x%02x%02x%02x",
bbc6a8
+                    buf[8], buf[9], buf[10], buf[11]);
bbc6a8
+    }
bbc6a8
+  else
bbc6a8
+    {
bbc6a8
+      for (i = 0; i < 16; ++i)
bbc6a8
+        uuid[i] = grub_tolower (buf[i]);
bbc6a8
+      uuid[16] = 0;
bbc6a8
+    }
bbc6a8
+
bbc6a8
+  return uuid;
bbc6a8
+}
bbc6a8
+
bbc6a8
+static grub_err_t
bbc6a8
+grub_udf_uuid (grub_device_t device, char **uuid)
bbc6a8
+{
bbc6a8
+  char *volset_ident;
bbc6a8
+  struct grub_udf_data *data;
bbc6a8
+  data = grub_udf_mount (device->disk);
bbc6a8
+
bbc6a8
+  if (data)
bbc6a8
+    {
bbc6a8
+      volset_ident = read_dstring (data->pvd.volset_ident, sizeof (data->pvd.volset_ident));
bbc6a8
+      if (volset_ident)
bbc6a8
+        {
bbc6a8
+          *uuid = gen_uuid_from_volset (volset_ident);
bbc6a8
+          grub_free (volset_ident);
bbc6a8
+        }
bbc6a8
+      else
bbc6a8
+        *uuid = 0;
bbc6a8
+      grub_free (data);
bbc6a8
+    }
bbc6a8
+  else
bbc6a8
+    *uuid = 0;
bbc6a8
+
bbc6a8
+  return grub_errno;
bbc6a8
+}
bbc6a8
+
bbc6a8
 static struct grub_fs grub_udf_fs = {
bbc6a8
   .name = "udf",
bbc6a8
   .dir = grub_udf_dir,
bbc6a8
@@ -1232,6 +1350,7 @@ static struct grub_fs grub_udf_fs = {
bbc6a8
   .read = grub_udf_read,
bbc6a8
   .close = grub_udf_close,
bbc6a8
   .label = grub_udf_label,
bbc6a8
+  .uuid = grub_udf_uuid,
bbc6a8
 #ifdef GRUB_UTIL
bbc6a8
   .reserved_first_sector = 1,
bbc6a8
   .blocklist_install = 1,