mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 02/12] partition: allocate struct partition_desc in parser
Date: Mon, 19 Feb 2024 09:31:30 +0100	[thread overview]
Message-ID: <20240219083140.2713047-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20240219083140.2713047-1-s.hauer@pengutronix.de>

Allocate struct partition_desc in parser rather than in the core. Doing
so allows the efi/dos partition code to embed the struct partition_desc
is a efi/dos specific struct type.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions.c        | 10 ++++++----
 common/partitions/dos.c    | 14 ++++++++++++--
 common/partitions/efi.c    | 14 ++++++++++++--
 common/partitions/parser.h |  3 ++-
 4 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/common/partitions.c b/common/partitions.c
index cfcd0e080b..05ebde7ca3 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -113,13 +113,12 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
  */
 int parse_partition_table(struct block_device *blk)
 {
-	struct partition_desc *pdesc;
+	struct partition_desc *pdesc = NULL;
 	int i;
 	int rc = 0;
 	struct partition_parser *parser;
 	uint8_t *buf;
 
-	pdesc = xzalloc(sizeof(*pdesc));
 	buf = malloc(2 * SECTOR_SIZE);
 
 	rc = block_read(blk, buf, 0, 2);
@@ -132,7 +131,9 @@ int parse_partition_table(struct block_device *blk)
 	if (!parser)
 		goto on_error;
 
-	parser->parse(buf, blk, pdesc);
+	pdesc = parser->parse(buf, blk);
+	if (!pdesc)
+		goto on_error;
 
 	if (!pdesc->used_entries)
 		goto on_error;
@@ -150,7 +151,8 @@ int parse_partition_table(struct block_device *blk)
 
 on_error:
 	free(buf);
-	free(pdesc);
+	if (pdesc)
+		parser->partition_free(pdesc);
 	return rc;
 }
 
diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 56ec1e3f48..01a251201e 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -171,8 +171,7 @@ static void dos_extended_partition(struct block_device *blk, struct partition_de
  * It seems at least on ARM this routine cannot use temp. stack space for the
  * sector. So, keep the malloc/free.
  */
-static void dos_partition(void *buf, struct block_device *blk,
-			  struct partition_desc *pd)
+static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 {
 	struct partition_entry *table;
 	struct partition pentry;
@@ -181,6 +180,7 @@ static void dos_partition(void *buf, struct block_device *blk,
 	int i;
 	struct disk_signature_priv *dsp;
 	uint32_t signature = get_unaligned_le32(buf + 0x1b8);
+	struct partition_desc *pd;
 
 	if (signature)
 		sprintf(blk->cdev.diskuuid, "%08x", signature);
@@ -189,6 +189,8 @@ static void dos_partition(void *buf, struct block_device *blk,
 
 	table = (struct partition_entry *)&buffer[446];
 
+	pd = xzalloc(sizeof(*pd));
+
 	for (i = 0; i < 4; i++) {
 		int n;
 
@@ -244,10 +246,18 @@ static void dos_partition(void *buf, struct block_device *blk,
 	dev_add_param_uint32(blk->dev, "nt_signature",
 			dos_set_disk_signature, dos_get_disk_signature,
 			&dsp->signature, "%08x", dsp);
+
+	return pd;
+}
+
+static void dos_partition_free(struct partition_desc *pd)
+{
+	free(pd);
 }
 
 static struct partition_parser dos = {
 	.parse = dos_partition,
+	.partition_free = dos_partition_free,
 	.type = filetype_mbr,
 };
 
diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index d0e14d5abb..effe512949 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -431,14 +431,14 @@ static void part_set_efi_name(gpt_entry *pte, char *dest)
 	dest[i] = 0;
 }
 
-static void efi_partition(void *buf, struct block_device *blk,
-			  struct partition_desc *pd)
+static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
 {
 	gpt_header *gpt = NULL;
 	gpt_entry *ptes = NULL;
 	int i = 0;
 	int nb_part;
 	struct partition *pentry;
+	struct partition_desc *pd;
 
 	if (!find_valid_gpt(buf, blk, &gpt, &ptes) || !gpt || !ptes)
 		goto out;
@@ -456,6 +456,8 @@ static void efi_partition(void *buf, struct block_device *blk,
 		nb_part = MAX_PARTITION;
 	}
 
+	pd = xzalloc(sizeof(*pd));
+
 	for (i = 0; i < nb_part; i++) {
 		if (!is_pte_valid(&ptes[i], last_lba(blk))) {
 			dev_dbg(blk->dev, "Invalid pte %d\n", i);
@@ -474,10 +476,18 @@ static void efi_partition(void *buf, struct block_device *blk,
 out:
 	kfree(gpt);
 	kfree(ptes);
+
+	return pd;
+}
+
+static void efi_partition_free(struct partition_desc *pd)
+{
+	free(pd);
 }
 
 static struct partition_parser efi_partition_parser = {
 	.parse = efi_partition,
+	.partition_free = efi_partition_free,
 	.type = filetype_gpt,
 };
 
diff --git a/common/partitions/parser.h b/common/partitions/parser.h
index 9cc41a7573..3eec2cdb21 100644
--- a/common/partitions/parser.h
+++ b/common/partitions/parser.h
@@ -32,7 +32,8 @@ struct partition_desc {
 };
 
 struct partition_parser {
-	void (*parse)(void *buf, struct block_device *blk, struct partition_desc *pd);
+	struct partition_desc *(*parse)(void *buf, struct block_device *blk);
+	void (*partition_free)(struct partition_desc *pd);
 	enum filetype type;
 
 	struct list_head list;
-- 
2.39.2




  parent reply	other threads:[~2024-02-19  8:32 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19  8:31 [PATCH 00/12] Partition table manipulation support Sascha Hauer
2024-02-19  8:31 ` [PATCH 01/12] partitions: dos: save indention level Sascha Hauer
2024-02-19  8:31 ` Sascha Hauer [this message]
2024-02-19  8:31 ` [PATCH 03/12] partition: allocate struct partition in parser Sascha Hauer
2024-02-19  8:31 ` [PATCH 04/12] partition: efi: keep raw data Sascha Hauer
2024-02-19  8:31 ` [PATCH 05/12] uuid: implement random uuid/guid Sascha Hauer
2024-02-19  8:31 ` [PATCH 06/12] linux/sizes.h: add more defines Sascha Hauer
2024-02-19  8:31 ` [PATCH 07/12] partition: add PARTITION_LINUX_DATA_GUID define Sascha Hauer
2024-02-19  8:31 ` [PATCH 08/12] partitions: move parser.h to include/partitions.h Sascha Hauer
2024-02-19  8:31 ` [PATCH 09/12] partitions: implement partition manipulation support Sascha Hauer
2024-02-19  8:31 ` [PATCH 10/12] partitions: dos: " Sascha Hauer
2024-02-28 17:37   ` Ahmad Fatoum
2024-02-29  7:16     ` Sascha Hauer
2024-02-19  8:31 ` [PATCH 11/12] partitions: efi: " Sascha Hauer
2024-02-28 17:36   ` Ahmad Fatoum
2024-02-19  8:31 ` [PATCH 12/12] commands: add parted Sascha Hauer
2024-02-19  9:38   ` Ulrich Ölmann
2024-02-20 10:47 ` [PATCH 00/12] Partition table manipulation support 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=20240219083140.2713047-3-s.hauer@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