diff mbox

[U-Boot,4/4] part: efi: Disable overlap check

Message ID 20170823140133.1506-4-maxime.ripard@free-electrons.com
State Accepted
Commit 79c5912e8d8748d5e36c7dc6376891c9f451e375
Delegated to: Tom Rini
Headers show

Commit Message

Maxime Ripard Aug. 23, 2017, 2:01 p.m. UTC
The current code checks that no partitions overlap with the GPT partition
table using the offset of the first LBA usable for that partition.

This works fine, unless you have a partition entry that is further away
than it usually is and you want to create partitions in the gap between the
GPT header and the GPT partition entries, for example to reflash a
bootloader that needs to be set there.

Rework the test to something a bit smarter that checks whether a partition
would overlap with either the GPT header or the partition entries, no
matter where it is on the disk.

Partitions that do not have a start LBA specified will still start at the
first LBA usable set in the GPT header, to avoid weird behaviours.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 disk/part_efi.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

Comments

Tom Rini Aug. 25, 2017, 1:05 a.m. UTC | #1
On Wed, Aug 23, 2017 at 04:01:33PM +0200, Maxime Ripard wrote:

> The current code checks that no partitions overlap with the GPT partition
> table using the offset of the first LBA usable for that partition.
> 
> This works fine, unless you have a partition entry that is further away
> than it usually is and you want to create partitions in the gap between the
> GPT header and the GPT partition entries, for example to reflash a
> bootloader that needs to be set there.
> 
> Rework the test to something a bit smarter that checks whether a partition
> would overlap with either the GPT header or the partition entries, no
> matter where it is on the disk.
> 
> Partitions that do not have a start LBA specified will still start at the
> first LBA usable set in the GPT header, to avoid weird behaviours.
> 
> Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Reviewed-by: Tom Rini <trini@konsulko.com>
Tom Rini Sept. 4, 2017, 12:41 a.m. UTC | #2
On Wed, Aug 23, 2017 at 04:01:33PM +0200, Maxime Ripard wrote:

> The current code checks that no partitions overlap with the GPT partition
> table using the offset of the first LBA usable for that partition.
> 
> This works fine, unless you have a partition entry that is further away
> than it usually is and you want to create partitions in the gap between the
> GPT header and the GPT partition entries, for example to reflash a
> bootloader that needs to be set there.
> 
> Rework the test to something a bit smarter that checks whether a partition
> would overlap with either the GPT header or the partition entries, no
> matter where it is on the disk.
> 
> Partitions that do not have a start LBA specified will still start at the
> first LBA usable set in the GPT header, to avoid weird behaviours.
> 
> Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> Reviewed-by: Tom Rini <trini@konsulko.com>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/disk/part_efi.c b/disk/part_efi.c
index 807d01de39d0..2973d52f6abb 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -445,24 +445,38 @@  int gpt_fill_pte(struct blk_desc *dev_desc,
 	char *str_type_guid;
 	unsigned char *bin_type_guid;
 #endif
+	size_t hdr_start = gpt_h->my_lba;
+	size_t hdr_end = hdr_start + 1;
+
+	size_t pte_start = gpt_h->partition_entry_lba;
+	size_t pte_end = pte_start +
+		gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry /
+		dev_desc->blksz;
 
 	for (i = 0; i < parts; i++) {
 		/* partition starting lba */
 		lbaint_t start = partitions[i].start;
 		lbaint_t size = partitions[i].size;
 
-		if (start && (start < offset)) {
-			printf("Partition overlap\n");
-			return -1;
-		}
-
 		if (start) {
-			gpt_e[i].starting_lba = cpu_to_le64(start);
 			offset = start + size;
 		} else {
-			gpt_e[i].starting_lba = cpu_to_le64(offset);
+			start = offset;
 			offset += size;
 		}
+
+		/*
+		 * If our partition overlaps with either the GPT
+		 * header, or the partition entry, reject it.
+		 */
+		if (((start <= hdr_end && hdr_start <= (start + size)) ||
+		     (start <= pte_end && pte_start <= (start + size)))) {
+			printf("Partition overlap\n");
+			return -1;
+		}
+
+		gpt_e[i].starting_lba = cpu_to_le64(start);
+
 		if (offset > (last_usable_lba + 1)) {
 			printf("Partitions layout exceds disk size\n");
 			return -1;