mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] partitions: dos: parse extended partition
@ 2013-11-08  0:17 Uwe Kleine-König
  2013-11-08  0:51 ` Uwe Kleine-König
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2013-11-08  0:17 UTC (permalink / raw)
  To: barebox; +Cc: jbe

DOS MBRs might contain an extended partition that holds several logical
partitions. Add these to the partitions of the block device.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 common/partitions/dos.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 1d8213b..f907abc 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -116,12 +116,64 @@ static int dos_get_disk_signature(struct param_d *p, void *_priv)
 	return 0;
 }
 
+static void dos_extended_partition(struct block_device *blk, struct partition_desc *pd,
+		struct partition *partition)
+{
+	uint8_t *buf = dma_alloc(SECTOR_SIZE);
+	uint32_t ebr_sector = partition->first_sec;
+	struct partition_entry *table = (struct partition_entry *)&buf[0x1be];
+
+	while (pd->used_entries < ARRAY_SIZE(pd->parts)) {
+		int rc, i;
+		int n = pd->used_entries;
+
+		dev_dbg(blk->dev, "expect EBR in sector %x\n", ebr_sector);
+
+		rc = block_read(blk, buf, ebr_sector, 1);
+		if (rc != 0) {
+			dev_err(blk->dev, "Cannot read EBR partition table\n");
+			goto out;
+		}
+
+		/* sanity checks */
+		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
+			dev_err(blk->dev, "sector %x doesn't contain an EBR signature\n");
+			goto out;
+		}
+
+		for (i = 0x1de; i < 0x1fe; ++i)
+			if (buf[i]) {
+				dev_err(blk->dev, "EBR's third or fourth partition non-empty\n");
+				goto out;
+			}
+		/* /sanity checks */
+
+		/* the first entry defines the extended partition */
+		pd->parts[n].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;
+		pd->used_entries++;
+
+		/* the second entry defines the start of the next ebr if != 0 */
+		if (get_unaligned_le32(&table[1].partition_start))
+			ebr_sector = partition->first_sec +
+				get_unaligned_le32(&table[1].partition_start);
+		else
+			break;
+	}
+
+out:
+	dma_free(buf);
+	return;
+}
+
 /**
  * Check if a DOS like partition describes this block device
  * @param blk Block device to register to
  * @param pd Where to store the partition information
  *
- * It seems at least on ARM this routine canot use temp. stack space for the
+ * 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,
@@ -129,6 +181,7 @@ static void dos_partition(void *buf, struct block_device *blk,
 {
 	struct partition_entry *table;
 	struct partition pentry;
+	struct partition *extended_partition = NULL;
 	uint8_t *buffer = buf;
 	int i;
 	struct disk_signature_priv *dsp;
@@ -150,11 +203,32 @@ static void dos_partition(void *buf, struct block_device *blk,
 			pd->parts[n].size = pentry.size;
 			pd->parts[n].dos_partition_type = pentry.dos_partition_type;
 			pd->used_entries++;
+			/*
+			 * Partitions of type 0x05 and 0x0f (and some more)
+			 * contain extended partitions. Only check for type 0x0f
+			 * here as this is the easiest to parse and common
+			 * enough.
+			 */
+			if (pentry.dos_partition_type == 0x0f) {
+				if (!extended_partition)
+					extended_partition = &pd->parts[n];
+				else
+					/*
+					 * An DOS MBR must only contain a single
+					 * extended partition. Just ignore all
+					 * but the first.
+					 */
+					dev_warn(blk->dev, "Skipping additional extended partition\n");
+			}
+
 		} else {
 			dev_dbg(blk->dev, "Skipping empty partition %d\n", i);
 		}
 	}
 
+	if (first_extended_partition)
+		dos_extended_partition(blk, pd, extended_partition);
+
 	dsp = xzalloc(sizeof(*dsp));
 	dsp->blk = blk;
 
-- 
1.8.4.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] partitions: dos: parse extended partition
  2013-11-08  0:17 [PATCH] partitions: dos: parse extended partition Uwe Kleine-König
@ 2013-11-08  0:51 ` Uwe Kleine-König
  2013-11-08 10:13   ` [PATCH v2] " Uwe Kleine-König
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2013-11-08  0:51 UTC (permalink / raw)
  To: barebox; +Cc: jbe

Hello,

On Fri, Nov 08, 2013 at 01:17:26AM +0100, Uwe Kleine-König wrote:
> DOS MBRs might contain an extended partition that holds several logical
> partitions. Add these to the partitions of the block device.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  common/partitions/dos.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 75 insertions(+), 1 deletion(-)
This even fails to build. See below.

> diff --git a/common/partitions/dos.c b/common/partitions/dos.c
> index 1d8213b..f907abc 100644
> --- a/common/partitions/dos.c
> +++ b/common/partitions/dos.c
> [...]
> @@ -129,6 +181,7 @@ static void dos_partition(void *buf, struct block_device *blk,
> [...]
> +					dev_warn(blk->dev, "Skipping additional extended partition\n");
> +			}
> +
>  		} else {
>  			dev_dbg(blk->dev, "Skipping empty partition %d\n", i);
>  		}
>  	}
>  
> +	if (first_extended_partition)
s/first_//

> +		dos_extended_partition(blk, pd, extended_partition);
> +
>  	dsp = xzalloc(sizeof(*dsp));
>  	dsp->blk = blk;
>  

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] partitions: dos: parse extended partition
  2013-11-08  0:51 ` Uwe Kleine-König
@ 2013-11-08 10:13   ` Uwe Kleine-König
  2013-11-08 14:38     ` Sascha Hauer
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2013-11-08 10:13 UTC (permalink / raw)
  To: barebox

DOS MBRs might contain an extended partition that holds several logical
partitions. Add these to the partitions of the block device.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 common/partitions/dos.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 64314a8..37addfd 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -116,12 +116,64 @@ static int dos_get_disk_signature(struct param_d *p, void *_priv)
 	return 0;
 }
 
+static void dos_extended_partition(struct block_device *blk, struct partition_desc *pd,
+		struct partition *partition)
+{
+	uint8_t *buf = dma_alloc(SECTOR_SIZE);
+	uint32_t ebr_sector = partition->first_sec;
+	struct partition_entry *table = (struct partition_entry *)&buf[0x1be];
+
+	while (pd->used_entries < ARRAY_SIZE(pd->parts)) {
+		int rc, i;
+		int n = pd->used_entries;
+
+		dev_dbg(blk->dev, "expect EBR in sector %x\n", ebr_sector);
+
+		rc = block_read(blk, buf, ebr_sector, 1);
+		if (rc != 0) {
+			dev_err(blk->dev, "Cannot read EBR partition table\n");
+			goto out;
+		}
+
+		/* sanity checks */
+		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
+			dev_err(blk->dev, "sector %x doesn't contain an EBR signature\n", ebr_sector);
+			goto out;
+		}
+
+		for (i = 0x1de; i < 0x1fe; ++i)
+			if (buf[i]) {
+				dev_err(blk->dev, "EBR's third or fourth partition non-empty\n");
+				goto out;
+			}
+		/* /sanity checks */
+
+		/* the first entry defines the extended partition */
+		pd->parts[n].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;
+		pd->used_entries++;
+
+		/* the second entry defines the start of the next ebr if != 0 */
+		if (get_unaligned_le32(&table[1].partition_start))
+			ebr_sector = partition->first_sec +
+				get_unaligned_le32(&table[1].partition_start);
+		else
+			break;
+	}
+
+out:
+	dma_free(buf);
+	return;
+}
+
 /**
  * Check if a DOS like partition describes this block device
  * @param blk Block device to register to
  * @param pd Where to store the partition information
  *
- * It seems at least on ARM this routine canot use temp. stack space for the
+ * 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,
@@ -129,6 +181,7 @@ static void dos_partition(void *buf, struct block_device *blk,
 {
 	struct partition_entry *table;
 	struct partition pentry;
+	struct partition *extended_partition = NULL;
 	uint8_t *buffer = buf;
 	int i;
 	struct disk_signature_priv *dsp;
@@ -150,11 +203,32 @@ static void dos_partition(void *buf, struct block_device *blk,
 			pd->parts[n].size = pentry.size;
 			pd->parts[n].dos_partition_type = pentry.dos_partition_type;
 			pd->used_entries++;
+			/*
+			 * Partitions of type 0x05 and 0x0f (and some more)
+			 * contain extended partitions. Only check for type 0x0f
+			 * here as this is the easiest to parse and common
+			 * enough.
+			 */
+			if (pentry.dos_partition_type == 0x0f) {
+				if (!extended_partition)
+					extended_partition = &pd->parts[n];
+				else
+					/*
+					 * An DOS MBR must only contain a single
+					 * extended partition. Just ignore all
+					 * but the first.
+					 */
+					dev_warn(blk->dev, "Skipping additional extended partition\n");
+			}
+
 		} else {
 			dev_dbg(blk->dev, "Skipping empty partition %d\n", i);
 		}
 	}
 
+	if (extended_partition)
+		dos_extended_partition(blk, pd, extended_partition);
+
 	dsp = xzalloc(sizeof(*dsp));
 	dsp->blk = blk;
 
-- 
1.8.4.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] partitions: dos: parse extended partition
  2013-11-08 10:13   ` [PATCH v2] " Uwe Kleine-König
@ 2013-11-08 14:38     ` Sascha Hauer
  0 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2013-11-08 14:38 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Fri, Nov 08, 2013 at 11:13:41AM +0100, Uwe Kleine-König wrote:
> DOS MBRs might contain an extended partition that holds several logical
> partitions. Add these to the partitions of the block device.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Applied, thanks

Sascha

> ---
>  common/partitions/dos.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 75 insertions(+), 1 deletion(-)
> 
> diff --git a/common/partitions/dos.c b/common/partitions/dos.c
> index 64314a8..37addfd 100644
> --- a/common/partitions/dos.c
> +++ b/common/partitions/dos.c
> @@ -116,12 +116,64 @@ static int dos_get_disk_signature(struct param_d *p, void *_priv)
>  	return 0;
>  }
>  
> +static void dos_extended_partition(struct block_device *blk, struct partition_desc *pd,
> +		struct partition *partition)
> +{
> +	uint8_t *buf = dma_alloc(SECTOR_SIZE);
> +	uint32_t ebr_sector = partition->first_sec;
> +	struct partition_entry *table = (struct partition_entry *)&buf[0x1be];
> +
> +	while (pd->used_entries < ARRAY_SIZE(pd->parts)) {
> +		int rc, i;
> +		int n = pd->used_entries;
> +
> +		dev_dbg(blk->dev, "expect EBR in sector %x\n", ebr_sector);
> +
> +		rc = block_read(blk, buf, ebr_sector, 1);
> +		if (rc != 0) {
> +			dev_err(blk->dev, "Cannot read EBR partition table\n");
> +			goto out;
> +		}
> +
> +		/* sanity checks */
> +		if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
> +			dev_err(blk->dev, "sector %x doesn't contain an EBR signature\n", ebr_sector);
> +			goto out;
> +		}
> +
> +		for (i = 0x1de; i < 0x1fe; ++i)
> +			if (buf[i]) {
> +				dev_err(blk->dev, "EBR's third or fourth partition non-empty\n");
> +				goto out;
> +			}
> +		/* /sanity checks */
> +
> +		/* the first entry defines the extended partition */
> +		pd->parts[n].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;
> +		pd->used_entries++;
> +
> +		/* the second entry defines the start of the next ebr if != 0 */
> +		if (get_unaligned_le32(&table[1].partition_start))
> +			ebr_sector = partition->first_sec +
> +				get_unaligned_le32(&table[1].partition_start);
> +		else
> +			break;
> +	}
> +
> +out:
> +	dma_free(buf);
> +	return;
> +}
> +
>  /**
>   * Check if a DOS like partition describes this block device
>   * @param blk Block device to register to
>   * @param pd Where to store the partition information
>   *
> - * It seems at least on ARM this routine canot use temp. stack space for the
> + * 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,
> @@ -129,6 +181,7 @@ static void dos_partition(void *buf, struct block_device *blk,
>  {
>  	struct partition_entry *table;
>  	struct partition pentry;
> +	struct partition *extended_partition = NULL;
>  	uint8_t *buffer = buf;
>  	int i;
>  	struct disk_signature_priv *dsp;
> @@ -150,11 +203,32 @@ static void dos_partition(void *buf, struct block_device *blk,
>  			pd->parts[n].size = pentry.size;
>  			pd->parts[n].dos_partition_type = pentry.dos_partition_type;
>  			pd->used_entries++;
> +			/*
> +			 * Partitions of type 0x05 and 0x0f (and some more)
> +			 * contain extended partitions. Only check for type 0x0f
> +			 * here as this is the easiest to parse and common
> +			 * enough.
> +			 */
> +			if (pentry.dos_partition_type == 0x0f) {
> +				if (!extended_partition)
> +					extended_partition = &pd->parts[n];
> +				else
> +					/*
> +					 * An DOS MBR must only contain a single
> +					 * extended partition. Just ignore all
> +					 * but the first.
> +					 */
> +					dev_warn(blk->dev, "Skipping additional extended partition\n");
> +			}
> +
>  		} else {
>  			dev_dbg(blk->dev, "Skipping empty partition %d\n", i);
>  		}
>  	}
>  
> +	if (extended_partition)
> +		dos_extended_partition(blk, pd, extended_partition);
> +
>  	dsp = xzalloc(sizeof(*dsp));
>  	dsp->blk = blk;
>  
> -- 
> 1.8.4.rc3
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-11-08 14:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-08  0:17 [PATCH] partitions: dos: parse extended partition Uwe Kleine-König
2013-11-08  0:51 ` Uwe Kleine-König
2013-11-08 10:13   ` [PATCH v2] " Uwe Kleine-König
2013-11-08 14:38     ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox