All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lukasz Majewski <l.majewski@majess.pl>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 5/6] gpt: part: Definition and declaration of GPT verification functions
Date: Fri, 13 Nov 2015 07:42:11 +0100	[thread overview]
Message-ID: <1447396932-26644-6-git-send-email-l.majewski@majess.pl> (raw)
In-Reply-To: <1447396932-26644-1-git-send-email-l.majewski@majess.pl>

This commit provides definition and declaration of GPT verification
functions - namely gpt_verify_headers() and gpt_verify_partitions().
The former is used to only check CRC32 of GPT's header and PTEs.
The latter examines each partition entry and compare attributes such as:
name, start offset and size with ones provided at '$partitions' env
variable.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>
---
 disk/part_efi.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/part.h  |  35 ++++++++++++++++++++
 2 files changed, 136 insertions(+)

diff --git a/disk/part_efi.c b/disk/part_efi.c
index 15627f2..59edf38 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -549,6 +549,107 @@ err:
 	return ret;
 }
 
+static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
+{
+	char *ess = (char *)es;
+	int i, j;
+
+	memset(s, '\0', n);
+
+	for (i = 0, j = 0; j < n; i += 2, j++) {
+		s[j] = ess[i];
+		if (!ess[i])
+			return;
+	}
+}
+
+int gpt_verify_headers(block_dev_desc_t *dev_desc, gpt_header *gpt_head,
+		       gpt_entry **gpt_pte)
+{
+	/*
+	 * This function validates AND
+	 * fills in the GPT header and PTE
+	 */
+	if (is_gpt_valid(dev_desc,
+			 GPT_PRIMARY_PARTITION_TABLE_LBA,
+			 gpt_head, gpt_pte) != 1) {
+		printf("%s: *** ERROR: Invalid GPT ***\n",
+		       __func__);
+		return -1;
+	}
+	if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
+			 gpt_head, gpt_pte) != 1) {
+		printf("%s: *** ERROR: Invalid Backup GPT ***\n",
+		       __func__);
+		return -1;
+	}
+
+	return 0;
+}
+
+int gpt_verify_partitions(block_dev_desc_t *dev_desc,
+			  disk_partition_t *partitions, int parts,
+			  gpt_header *gpt_head, gpt_entry **gpt_pte)
+{
+	char efi_str[PARTNAME_SZ + 1];
+	u64 gpt_part_size;
+	gpt_entry *gpt_e;
+	int ret, i;
+
+	ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
+	if (ret)
+		return ret;
+
+	gpt_e = *gpt_pte;
+
+	for (i = 0; i < parts; i++) {
+		if (i == gpt_head->num_partition_entries) {
+			error("More partitions than allowed!\n");
+			return -1;
+		}
+
+		/* Check if GPT and ENV partition names match */
+		gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
+					     PARTNAME_SZ + 1);
+
+		debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
+		      __func__, i, efi_str, partitions[i].name);
+
+		if (strncmp(efi_str, (char *)partitions[i].name,
+			    sizeof(partitions->name))) {
+			error("Partition name: %s does not match %s!\n",
+			      efi_str, (char *)partitions[i].name);
+			return -1;
+		}
+
+		/* Check if GPT and ENV start LBAs match */
+		debug("start LBA - GPT: %8llu, ENV: %8llu ",
+		      le64_to_cpu(gpt_e[i].starting_lba),
+		      (u64) partitions[i].start);
+
+		if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
+			error("Partition %s start: %llu does not match %llu!\n",
+			      efi_str, le64_to_cpu(gpt_e[i].starting_lba),
+			      (u64) partitions[i].start);
+			return -1;
+		}
+
+		/* Check if GPT and ENV sizes match */
+		gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
+			le64_to_cpu(gpt_e[i].starting_lba) + 1;
+		debug("size(LBA) - GPT: %8llu, ENV: %8llu\n",
+		      gpt_part_size, (u64) partitions[i].size);
+
+		if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
+			error("Partition %s size: %llu does not match %llu!\n",
+			      efi_str, gpt_part_size, (u64) partitions[i].size);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf)
 {
 	gpt_header *gpt_h;
diff --git a/include/part.h b/include/part.h
index 8ea9b30..4152052 100644
--- a/include/part.h
+++ b/include/part.h
@@ -264,6 +264,41 @@ int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf);
  * @return - '0' on success, otherwise error
  */
 int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf);
+
+/**
+ * gpt_verify_headers() - Function to read and CRC32 check of the GPT's header
+ *                        and partition table entries (PTE)
+ *
+ * As a side effect if sets gpt_head and gpt_pte so they point to GPT data.
+ *
+ * @param dev_desc - block device descriptor
+ * @param gpt_head - pointer to GPT header data read from medium
+ * @param gpt_pte - pointer to GPT partition table enties read from medium
+ *
+ * @return - '0' on success, otherwise error
+ */
+int gpt_verify_headers(block_dev_desc_t *dev_desc, gpt_header *gpt_head,
+		       gpt_entry **gpt_pte);
+
+/**
+ * gpt_verify_partitions() - Function to check if partitions' name, start and
+ *                           size correspond to '$partitions' env variable
+ *
+ * This function checks if on medium stored GPT data is in sync with information
+ * provided in '$partitions' environment variable. Specificially, name, start
+ * and size of the partition is checked.
+ *
+ * @param dev_desc - block device descriptor
+ * @param partitions - partition data read from '$partitions' env variable
+ * @param parts - number of partitions read from '$partitions' env variable
+ * @param gpt_head - pointer to GPT header data read from medium
+ * @param gpt_pte - pointer to GPT partition table enties read from medium
+ *
+ * @return - '0' on success, otherwise error
+ */
+int gpt_verify_partitions(block_dev_desc_t *dev_desc,
+			  disk_partition_t *partitions, int parts,
+			  gpt_header *gpt_head, gpt_entry **gpt_pte);
 #endif
 
 #endif /* _PART_H */
-- 
2.1.4

  parent reply	other threads:[~2015-11-13  6:42 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-13  6:42 [U-Boot] [PATCH 0/6] gpt: command: Add support for "gpt verify" command Lukasz Majewski
2015-11-13  6:42 ` [U-Boot] [PATCH 1/6] gpt: command: Remove duplicated check for empty partition description Lukasz Majewski
2015-11-18 23:38   ` Tom Rini
2015-11-13  6:42 ` [U-Boot] [PATCH 2/6] gpt: command: cosmetic: Replace printf with puts Lukasz Majewski
2015-11-18 23:37   ` Tom Rini
2015-11-19  5:43     ` Lukasz Majewski
2015-11-13  6:42 ` [U-Boot] [PATCH 3/6] gpt: doc: README: Update README entry for gpt verify extension Lukasz Majewski
2015-11-18 23:38   ` Tom Rini
2015-11-13  6:42 ` [U-Boot] [PATCH 4/6] gpt: doc: Update gpt command's help description Lukasz Majewski
2015-11-18 23:38   ` Tom Rini
2015-11-23 22:44   ` [U-Boot] [U-Boot, " Tom Rini
2015-11-13  6:42 ` Lukasz Majewski [this message]
2015-11-18 23:39   ` [U-Boot] [PATCH 5/6] gpt: part: Definition and declaration of GPT verification functions Tom Rini
2015-11-13  6:42 ` [U-Boot] [PATCH 6/6] gpt: command: Extend gpt command to support GPT table verification Lukasz Majewski
2015-11-18 23:40   ` Tom Rini
2015-11-19  5:45     ` Lukasz Majewski
2015-11-20  7:06 ` [U-Boot] [PATCH v2 0/5] gpt: command: Add support for "gpt verify" command Lukasz Majewski
2015-11-20  7:06   ` [U-Boot] [PATCH v2 1/5] gpt: command: Remove duplicated check for empty partition description Lukasz Majewski
2015-11-23 22:44     ` [U-Boot] [U-Boot, v2, " Tom Rini
2015-11-20  7:06   ` [U-Boot] [PATCH v2 2/5] gpt: doc: README: Update README entry for gpt verify extension Lukasz Majewski
2015-11-23 22:44     ` [U-Boot] [U-Boot, v2, " Tom Rini
2015-11-20  7:06   ` [U-Boot] [PATCH v2 4/5] gpt: part: Definition and declaration of GPT verification functions Lukasz Majewski
2015-11-20 11:19     ` Przemyslaw Marczak
2015-11-23 22:44     ` [U-Boot] [U-Boot, v2, " Tom Rini
2015-11-24  9:56       ` Przemyslaw Marczak
2015-11-24 18:56         ` Tom Rini
2015-11-25  9:06           ` Przemyslaw Marczak
2015-11-20  7:06   ` [U-Boot] [PATCH v2 5/5] gpt: command: Extend gpt command to support GPT table verification Lukasz Majewski
2015-11-20 11:20     ` Przemyslaw Marczak
2015-11-23 22:44     ` [U-Boot] [U-Boot, v2, " Tom Rini
2015-11-23 22:43   ` [U-Boot] [PATCH v2 0/5] gpt: command: Add support for "gpt verify" command Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447396932-26644-6-git-send-email-l.majewski@majess.pl \
    --to=l.majewski@majess.pl \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.