From patchwork Wed Aug 23 14:01:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 805002 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3xcpzh5Bm5z9ryv for ; Thu, 24 Aug 2017 00:03:12 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 59CB2C21E1C; Wed, 23 Aug 2017 14:02:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 580F2C21DD9; Wed, 23 Aug 2017 14:01:43 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 3B1FEC21C2B; Wed, 23 Aug 2017 14:01:40 +0000 (UTC) Received: from mail.free-electrons.com (mail.free-electrons.com [62.4.15.54]) by lists.denx.de (Postfix) with ESMTP id EA701C21C26 for ; Wed, 23 Aug 2017 14:01:39 +0000 (UTC) Received: by mail.free-electrons.com (Postfix, from userid 110) id F2F4F20A5C; Wed, 23 Aug 2017 16:01:38 +0200 (CEST) Received: from localhost (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id CA64720A44; Wed, 23 Aug 2017 16:01:38 +0200 (CEST) From: Maxime Ripard To: Tom Rini Date: Wed, 23 Aug 2017 16:01:33 +0200 Message-Id: <20170823140133.1506-4-maxime.ripard@free-electrons.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170823140133.1506-1-maxime.ripard@free-electrons.com> References: <20170823140133.1506-1-maxime.ripard@free-electrons.com> Cc: u-boot@lists.denx.de, Maxime Ripard Subject: [U-Boot] [PATCH 4/4] part: efi: Disable overlap check X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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 Reviewed-by: Tom Rini --- disk/part_efi.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) 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;