summaryrefslogtreecommitdiffstats
path: root/common/filetype.c
diff options
context:
space:
mode:
authorZahari Doychev <zahari.doychev@linux.com>2014-11-04 08:54:01 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2014-11-05 14:53:28 +0100
commitf95ce0fff0775c04954ae2e4542308baaa434ac8 (patch)
tree72266c00d1af91a67d6be1d327d799a91dfbc386 /common/filetype.c
parentc0d6aa097d8f32102f1a7034a9b230ff6a3d469b (diff)
downloadbarebox-f95ce0fff0775c04954ae2e4542308baaa434ac8.tar.gz
barebox-f95ce0fff0775c04954ae2e4542308baaa434ac8.tar.xz
common: fix mbr filetype detection
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> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/filetype.c')
-rw-r--r--common/filetype.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/common/filetype.c b/common/filetype.c
index c8f3582cd1..810d9a505c 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)
{
/*