mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] fix mbr detection
@ 2014-11-04  7:54 Zahari Doychev
  2014-11-04  7:54 ` [PATCH] common: fix mbr filetype detection Zahari Doychev
  2014-11-05 13:54 ` [PATCH] fix mbr detection Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Zahari Doychev @ 2014-11-04  7:54 UTC (permalink / raw)
  To: barebox

I have a strange problem using usb sticks in barebox. After formatting some
previously FAT sticks they still have file system type set to FAT32 in the mbr.
In this way there are detected as FAT fs partitions instead of MBRs and the usb
media partitons can not be parsed.

This patch should resolve this problem. Please review it and check if it is
fine and not causing any side effects.

Regards,
Zahari

Zahari Doychev (1):
  common: fix mbr filetype detection

 common/filetype.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/partitions.c |  2 ++
 include/filetype.h  |  1 +
 3 files changed, 51 insertions(+)

-- 
2.0.4


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

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

* [PATCH] common: fix mbr filetype detection
  2014-11-04  7:54 [PATCH] fix mbr detection Zahari Doychev
@ 2014-11-04  7:54 ` Zahari Doychev
  2014-11-05 13:54 ` [PATCH] fix mbr detection Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Zahari Doychev @ 2014-11-04  7:54 UTC (permalink / raw)
  To: barebox

Sometimes mbr is erroneously recocognised as FAT partion. Due to this the mbr
partition parser is not being called and the partitions on the media are not
detected. This patch should  fix the problem. The checking is done as in the
linux kernel.

I have seen the problem using usb sticks. Although partitioning and formatting
them under linux. The file system type field in the mbr remains there which
causes the wrong detections as FAT32 type and not as mbr.

Signed-off-by: Zahari Doychev <zahari.doychev@linux.com>
---
 common/filetype.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/partitions.c |  2 ++
 include/filetype.h  |  1 +
 3 files changed, 51 insertions(+)

diff --git a/common/filetype.c b/common/filetype.c
index c8f3582..810d9a5 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -24,6 +24,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <envfs.h>
+#include <disks.h>
 
 struct filetype_str {
 	const char *name;	/* human readable filetype */
@@ -87,6 +88,10 @@ const char *file_type_to_short_string(enum filetype f)
 #define MBR_PART_start_sect	8
 #define MBR_OSTYPE_EFI_GPT	0xee
 
+#define FAT_BS_reserved		14
+#define FAT_BS_fats		16
+#define FAT_BS_media		21
+
 static inline int pmbr_part_valid(const uint8_t *buf)
 {
 	if (buf[MBR_PART_sys_ind] == MBR_OSTYPE_EFI_GPT &&
@@ -126,6 +131,49 @@ static int is_gpt_valid(const uint8_t *buf)
 	return 0;
 }
 
+static inline int fat_valid_media(u8 media)
+{
+	return (0xf8 <= media || media == 0xf0);
+}
+
+static int is_fat_with_no_mbr(const unsigned char  *sect)
+{
+	if (!get_unaligned_le16(&sect[FAT_BS_reserved]))
+		return 0;
+
+	if (!sect[FAT_BS_fats])
+		return 0;
+
+	if (!fat_valid_media(sect[FAT_BS_media]))
+		return 0;
+
+	return 1;
+}
+
+int is_fat_boot_sector(const void *sect)
+{
+	struct partition_entry *p;
+	int slot;
+
+	p = (struct partition_entry *) (sect + MBR_Table);
+	/* max 4 partitions */
+	for (slot = 1; slot <= 4; slot++, p++) {
+		if (p->boot_indicator && p->boot_indicator != 0x80) {
+			/*
+			 * Even without a valid boot inidicator value
+			 * its still possible this is valid FAT filesystem
+			 * without a partition table.
+			 */
+			if (slot == 1 && is_fat_with_no_mbr(sect))
+				return 1;
+			 else
+				return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
 enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 {
 	/*
diff --git a/common/partitions.c b/common/partitions.c
index 694c6f6..37d9cb7 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -107,6 +107,8 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
 	 * useful for compatibility
 	 */
 	type = file_detect_partition_table(buf, SECTOR_SIZE);
+	if (type == filetype_fat && !is_fat_boot_sector(buf))
+		type = filetype_mbr;
 
 	list_for_each_entry(parser, &partition_parser_list, list) {
 		if (parser->type == type)
diff --git a/include/filetype.h b/include/filetype.h
index eedf4b4..2c3c38d 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -44,6 +44,7 @@ enum filetype file_detect_partition_table(const void *_buf, size_t bufsize);
 enum filetype file_detect_type(const void *_buf, size_t bufsize);
 enum filetype file_name_detect_type(const char *filename);
 enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec);
+int is_fat_boot_sector(const void *_buf);
 
 #define ARM_HEAD_SIZE			0x30
 #define ARM_HEAD_MAGICWORD_OFFSET	0x20
-- 
2.0.4


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

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

* Re: [PATCH] fix mbr detection
  2014-11-04  7:54 [PATCH] fix mbr detection Zahari Doychev
  2014-11-04  7:54 ` [PATCH] common: fix mbr filetype detection Zahari Doychev
@ 2014-11-05 13:54 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2014-11-05 13:54 UTC (permalink / raw)
  To: Zahari Doychev; +Cc: barebox

Hi,

On Tue, Nov 04, 2014 at 08:54:00AM +0100, Zahari Doychev wrote:
> I have a strange problem using usb sticks in barebox. After formatting some
> previously FAT sticks they still have file system type set to FAT32 in the mbr.
> In this way there are detected as FAT fs partitions instead of MBRs and the usb
> media partitons can not be parsed.
> 
> This patch should resolve this problem. Please review it and check if it is
> fine and not causing any side effects.

Well, I don't see any side effects, but that does not mean much...

Applied, thanks.

Sascha

-- 
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] 3+ messages in thread

end of thread, other threads:[~2014-11-05 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-04  7:54 [PATCH] fix mbr detection Zahari Doychev
2014-11-04  7:54 ` [PATCH] common: fix mbr filetype detection Zahari Doychev
2014-11-05 13:54 ` [PATCH] fix mbr detection Sascha Hauer

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