mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 3/6] partitions: gpt: factor out a function to write primary/alternate GPT
Date: Wed, 03 Dec 2025 16:19:07 +0100	[thread overview]
Message-ID: <20251203-efi-partition-refresh-v1-3-f0b6e79b5fa0@pengutronix.de> (raw)
In-Reply-To: <20251203-efi-partition-refresh-v1-0-f0b6e79b5fa0@pengutronix.de>

In preparation for writing the inactive GPT first factor out a function
which either writes the primary or alternate GPT based on a boolean
argument.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions/efi.c | 92 ++++++++++++++++++++++++++++---------------------
 1 file changed, 53 insertions(+), 39 deletions(-)

diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 76c5393dddb04e2966d3a4b4478d5085f008d935..8da62acf36646b62ff2fac25673adbc48650d299 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -740,75 +740,89 @@ static int efi_protective_mbr(struct block_device *blk)
 	return ret;
 }
 
-static __maybe_unused int efi_partition_write(struct partition_desc *pd)
+static int __efi_partition_write(struct efi_partition_desc *epd, bool primary)
 {
-	struct block_device *blk = pd->blk;
-	struct efi_partition_desc *epd = container_of(pd, struct efi_partition_desc, pd);
-	gpt_header *gpt = epd->gpt, *altgpt;
+	struct block_device *blk = epd->pd.blk;
+	gpt_header *gpt;
+	unsigned int count, size;
+	uint64_t my_lba, partition_entry_lba;
 	int ret;
-	uint32_t count;
-	uint64_t from, size;
 
-	if (le32_to_cpu(gpt->num_partition_entries) != 128) {
-		/*
-		 * This is not yet properly implemented. At least writing of the
-		 * alternative GPT is not correctly implemented for this case as
-		 * we can't assume that the partition entries are written at
-		 * last_lba() - 32, we would have to calculate that from the number
-		 * of partition entries.
-		 */
-		pr_err("num_partition_entries is != 128. This is not yet supported for writing\n");
-		return -EINVAL;
-	}
+	gpt = xmemdup(epd->gpt, SECTOR_SIZE);
 
 	count = le32_to_cpu(gpt->num_partition_entries) *
 		le32_to_cpu(gpt->sizeof_partition_entry);
 
-	gpt->my_lba = cpu_to_le64(1);
-	gpt->alternate_lba = cpu_to_le64(last_lba(blk));
+	size = count / GPT_BLOCK_SIZE;
+
+	if (primary) {
+		my_lba = 1;
+		partition_entry_lba = le64_to_cpu(gpt->partition_entry_lba);
+		gpt->alternate_lba = cpu_to_le64(last_lba(blk));
+	} else {
+		my_lba = last_lba(blk);
+		partition_entry_lba = last_lba(blk) - 32;
+		gpt->alternate_lba = cpu_to_le64(1);
+	}
+
+	gpt->my_lba = cpu_to_le64(my_lba);
+	gpt->partition_entry_lba = cpu_to_le64(partition_entry_lba);
 	gpt->partition_entry_array_crc32 = cpu_to_le32(efi_crc32(
 			(const unsigned char *)epd->ptes, count));
 	gpt->header_crc32 = 0;
 	gpt->header_crc32 = cpu_to_le32(efi_crc32((const unsigned char *)gpt,
 						  le32_to_cpu(gpt->header_size)));
 
-	ret = efi_protective_mbr(blk);
+	ret = block_write(blk, gpt, my_lba, 1);
 	if (ret)
-		return ret;
+		goto err_block_write;
 
-	ret = block_write(blk, gpt, 1, 1);
+	ret = block_write(blk, epd->ptes, partition_entry_lba, size);
 	if (ret)
 		goto err_block_write;
 
-	from = le64_to_cpu(gpt->partition_entry_lba);
-	size = count / GPT_BLOCK_SIZE;
+err_block_write:
+	free(gpt);
 
-	ret = block_write(blk, epd->ptes, from, size);
-	if (ret)
-		goto err_block_write;
+	return ret;
+}
 
-	altgpt = xmemdup(gpt, SECTOR_SIZE);
+static __maybe_unused int efi_partition_write(struct partition_desc *pd)
+{
+	struct block_device *blk = pd->blk;
+	struct efi_partition_desc *epd = container_of(pd, struct efi_partition_desc, pd);
+	gpt_header *gpt = epd->gpt;
+	int ret;
 
-	altgpt->alternate_lba = cpu_to_le64(1);
-	altgpt->my_lba = cpu_to_le64(last_lba(blk));
-	altgpt->partition_entry_lba = cpu_to_le64(last_lba(blk) - 32);
-	altgpt->header_crc32 = 0;
-	altgpt->header_crc32 = cpu_to_le32(efi_crc32((const unsigned char *)altgpt,
-						  le32_to_cpu(altgpt->header_size)));
-	ret = block_write(blk, altgpt, last_lba(blk), 1);
+	if (le32_to_cpu(gpt->num_partition_entries) != 128) {
+		/*
+		 * This is not yet properly implemented. At least writing of the
+		 * alternative GPT is not correctly implemented for this case as
+		 * we can't assume that the partition entries are written at
+		 * last_lba() - 32, we would have to calculate that from the number
+		 * of partition entries.
+		 */
+		pr_err("num_partition_entries is != 128. This is not yet supported for writing\n");
+		return -EINVAL;
+	}
 
-	free(altgpt);
+	ret = efi_protective_mbr(blk);
+	if (ret)
+		return ret;
 
+	ret = __efi_partition_write(epd, true);
 	if (ret)
 		goto err_block_write;
-	ret = block_write(blk, epd->ptes, last_lba(blk) - 32, size);
+
+	ret = __efi_partition_write(epd, false);
 	if (ret)
 		goto err_block_write;
 
-	return 0;
+	ret = 0;
 
 err_block_write:
-	pr_err("Cannot write to block device: %pe\n", ERR_PTR(ret));
+	if (ret)
+		pr_err("Cannot write to block device: %pe\n", ERR_PTR(ret));
 
 	return ret;
 }

-- 
2.47.3




  parent reply	other threads:[~2025-12-03 15:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-03 15:19 [PATCH 0/6] partitions: GPT: refresh partition tables when necessary Sascha Hauer
2025-12-03 15:19 ` [PATCH 1/6] partitions: gpt: pass epd context pointer to find_valid_gpt() Sascha Hauer
2025-12-15 12:28   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 2/6] partitions: gpt: only write actual ptes size to device Sascha Hauer
2025-12-15 12:29   ` Ahmad Fatoum
2025-12-03 15:19 ` Sascha Hauer [this message]
2025-12-15 12:34   ` [PATCH 3/6] partitions: gpt: factor out a function to write primary/alternate GPT Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 4/6] partitions: gpt: write inactive GPT first Sascha Hauer
2025-12-15 12:38   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 5/6] partitions: gpt: fix GPT restauration from alternate GPT Sascha Hauer
2025-12-15 12:41   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 6/6] partitions: gpt: refresh partition tables when necessary Sascha Hauer
2025-12-15 12:47   ` Ahmad Fatoum
2025-12-15 14:13     ` Sascha Hauer
2025-12-15 14:15       ` Ahmad Fatoum
2025-12-15 14:09 ` [PATCH 0/6] partitions: GPT: " Sascha Hauer

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=20251203-efi-partition-refresh-v1-3-f0b6e79b5fa0@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox