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 03/12] partition: allocate struct partition in parser
Date: Mon, 19 Feb 2024 09:31:31 +0100	[thread overview]
Message-ID: <20240219083140.2713047-4-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20240219083140.2713047-1-s.hauer@pengutronix.de>

Allocate struct partition in the parser rather than in the core
partition code. Doing so allows the parser to embed struct partition
in an implementation specific struct type.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions.c        | 12 ++++----
 common/partitions/dos.c    | 56 ++++++++++++++++++++++----------------
 common/partitions/efi.c    | 13 +++++++--
 common/partitions/parser.h |  5 ++--
 4 files changed, 51 insertions(+), 35 deletions(-)

diff --git a/common/partitions.c b/common/partitions.c
index 05ebde7ca3..0d8c2849ab 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -27,8 +27,7 @@ static LIST_HEAD(partition_parser_list);
  * @param no Partition number
  * @return 0 on success
  */
-static int register_one_partition(struct block_device *blk,
-					struct partition *part, int no)
+static int register_one_partition(struct block_device *blk, struct partition *part)
 {
 	char *partition_name;
 	int ret;
@@ -38,7 +37,7 @@ static int register_one_partition(struct block_device *blk,
 		.size = part->size * SECTOR_SIZE,
 	};
 
-	partition_name = basprintf("%s.%d", blk->cdev.name, no);
+	partition_name = basprintf("%s.%d", blk->cdev.name, part->num);
 	if (!partition_name)
 		return -ENOMEM;
 
@@ -117,6 +116,7 @@ int parse_partition_table(struct block_device *blk)
 	int i;
 	int rc = 0;
 	struct partition_parser *parser;
+	struct partition *part;
 	uint8_t *buf;
 
 	buf = malloc(2 * SECTOR_SIZE);
@@ -135,12 +135,12 @@ int parse_partition_table(struct block_device *blk)
 	if (!pdesc)
 		goto on_error;
 
-	if (!pdesc->used_entries)
+	if (list_empty(&pdesc->partitions))
 		goto on_error;
 
 	/* at least one partition description found */
-	for (i = 0; i < pdesc->used_entries; i++) {
-		rc = register_one_partition(blk, &pdesc->parts[i], i);
+	list_for_each_entry(part, &pdesc->partitions, list) {
+		rc = register_one_partition(blk, part);
 		if (rc != 0)
 			dev_err(blk->dev,
 				"Failed to register partition %d on %s (%d)\n",
diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 01a251201e..7ca3c4e37e 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -113,10 +113,10 @@ static void dos_extended_partition(struct block_device *blk, struct partition_de
 	uint32_t ebr_sector = partition->first_sec;
 	struct partition_entry *table = (struct partition_entry *)&buf[0x1be];
 	unsigned partno = 5;
+	struct partition *pentry;
 
-	while (pd->used_entries < ARRAY_SIZE(pd->parts)) {
+	while (1) {
 		int rc, i;
-		int n = pd->used_entries;
 
 		dev_dbg(blk->dev, "expect EBR in sector %x\n", ebr_sector);
 
@@ -139,15 +139,19 @@ static void dos_extended_partition(struct block_device *blk, struct partition_de
 			}
 		/* /sanity checks */
 
+		pentry = xzalloc(sizeof(*pentry));
+
 		/* the first entry defines the extended partition */
-		pd->parts[n].first_sec = ebr_sector +
+		pentry->first_sec = ebr_sector +
 			get_unaligned_le32(&table[0].partition_start);
-		pd->parts[n].size = get_unaligned_le32(&table[0].partition_size);
-		pd->parts[n].dos_partition_type = table[0].type;
+		pentry->size = get_unaligned_le32(&table[0].partition_size);
+		pentry->dos_partition_type = table[0].type;
+		pentry->num = partno;
 		if (signature)
-			sprintf(pd->parts[n].partuuid, "%08x-%02u",
+			sprintf(pentry->partuuid, "%08x-%02u",
 				signature, partno);
-		pd->used_entries++;
+
+		list_add_tail(&pentry->list, &pd->partitions);
 		partno++;
 
 		/* the second entry defines the start of the next ebr if != 0 */
@@ -174,7 +178,7 @@ static void dos_extended_partition(struct block_device *blk, struct partition_de
 static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 {
 	struct partition_entry *table;
-	struct partition pentry;
+	struct partition *pentry;
 	struct partition *extended_partition = NULL;
 	uint8_t *buffer = buf;
 	int i;
@@ -190,33 +194,30 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 	table = (struct partition_entry *)&buffer[446];
 
 	pd = xzalloc(sizeof(*pd));
+	INIT_LIST_HEAD(&pd->partitions);
 
 	for (i = 0; i < 4; i++) {
-		int n;
+		uint64_t first_sec = get_unaligned_le32(&table[i].partition_start);
 
-		pentry.first_sec = get_unaligned_le32(&table[i].partition_start);
-		pentry.size = get_unaligned_le32(&table[i].partition_size);
-		pentry.dos_partition_type = table[i].type;
-
-		if (pentry.first_sec == 0) {
+		if (first_sec == 0) {
 			dev_dbg(blk->dev, "Skipping empty partition %d\n", i);
 			continue;
 		}
 
-		n = pd->used_entries;
-		pd->parts[n].first_sec = pentry.first_sec;
-		pd->parts[n].size = pentry.size;
-		pd->parts[n].dos_partition_type = pentry.dos_partition_type;
+		pentry = xzalloc(sizeof(*pentry));
+
+		pentry->first_sec = first_sec;
+		pentry->size = get_unaligned_le32(&table[i].partition_size);
+		pentry->dos_partition_type = table[i].type;
+
 		if (signature)
-			sprintf(pd->parts[n].partuuid, "%08x-%02d",
-					signature, i + 1);
-		pd->used_entries++;
+			sprintf(pentry->partuuid, "%08x-%02d", signature, i + 1);
 
-		if (is_extended_partition(&pentry)) {
-			pd->parts[n].size = 2;
+		if (is_extended_partition(pentry)) {
+			pentry->size = 2;
 
 			if (!extended_partition)
-				extended_partition = &pd->parts[n];
+				extended_partition = pentry;
 			else
 				/*
 				 * An DOS MBR must only contain a single
@@ -225,6 +226,8 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 				 */
 				dev_warn(blk->dev, "Skipping additional extended partition\n");
 		}
+
+		list_add_tail(&pentry->list, &pd->partitions);
 	}
 
 	if (extended_partition)
@@ -252,6 +255,11 @@ static struct partition_desc *dos_partition(void *buf, struct block_device *blk)
 
 static void dos_partition_free(struct partition_desc *pd)
 {
+	struct partition *part, *tmp;
+
+	list_for_each_entry_safe(part, tmp, &pd->partitions, list)
+		free(part);
+
 	free(pd);
 }
 
diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index effe512949..5612f6261b 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -438,7 +438,7 @@ static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
 	int i = 0;
 	int nb_part;
 	struct partition *pentry;
-	struct partition_desc *pd;
+	struct partition_desc *pd = NULL;
 
 	if (!find_valid_gpt(buf, blk, &gpt, &ptes) || !gpt || !ptes)
 		goto out;
@@ -457,6 +457,7 @@ static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
 	}
 
 	pd = xzalloc(sizeof(*pd));
+	INIT_LIST_HEAD(&pd->partitions);
 
 	for (i = 0; i < nb_part; i++) {
 		if (!is_pte_valid(&ptes[i], last_lba(blk))) {
@@ -464,14 +465,15 @@ static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
 			continue;
 		}
 
-		pentry = &pd->parts[pd->used_entries];
+		pentry = xzalloc(sizeof(*pentry));
 		pentry->first_sec = le64_to_cpu(ptes[i].starting_lba);
 		pentry->size = le64_to_cpu(ptes[i].ending_lba) - pentry->first_sec;
 		pentry->size++;
 		part_set_efi_name(&ptes[i], pentry->name);
 		snprintf(pentry->partuuid, sizeof(pentry->partuuid), "%pUl", &ptes[i].unique_partition_guid);
 		pentry->typeuuid = ptes[i].partition_type_guid;
-		pd->used_entries++;
+		pentry->num = i;
+		list_add_tail(&pentry->list, &pd->partitions);
 	}
 out:
 	kfree(gpt);
@@ -482,6 +484,11 @@ static struct partition_desc *efi_partition(void *buf, struct block_device *blk)
 
 static void efi_partition_free(struct partition_desc *pd)
 {
+	struct partition *part, *tmp;
+
+	list_for_each_entry_safe(part, tmp, &pd->partitions, list)
+		free(part);
+
 	free(pd);
 }
 
diff --git a/common/partitions/parser.h b/common/partitions/parser.h
index 3eec2cdb21..ab593109e6 100644
--- a/common/partitions/parser.h
+++ b/common/partitions/parser.h
@@ -24,11 +24,12 @@ struct partition {
 		u8 dos_partition_type;
 		guid_t typeuuid;
 	};
+	struct list_head list;
+	int num;
 };
 
 struct partition_desc {
-	int used_entries;
-	struct partition parts[MAX_PARTITION];
+	struct list_head partitions;
 };
 
 struct partition_parser {
-- 
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 ` [PATCH 02/12] partition: allocate struct partition_desc in parser Sascha Hauer
2024-02-19  8:31 ` Sascha Hauer [this message]
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-4-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