summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>2013-02-16 14:47:06 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2013-02-16 23:39:08 +0100
commita7d9ec049844616b5b5f45847e6dff133ea52d96 (patch)
tree1838fb9cf86f97c6f7c8db5b5080e999a64af628
parentee2599db249de5119b2132da5dbe479385ab44b6 (diff)
downloadbarebox-a7d9ec049844616b5b5f45847e6dff133ea52d96.tar.gz
barebox-a7d9ec049844616b5b5f45847e6dff133ea52d96.tar.xz
filetype: add GPT support
GPT need to be check before MBR Cc: Rob Herring <rob.herring@calxeda.com> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--common/filetype.c52
-rw-r--r--include/filetype.h1
2 files changed, 53 insertions, 0 deletions
diff --git a/common/filetype.c b/common/filetype.c
index 22fc621a14..8652f1d7af 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -48,6 +48,7 @@ static const struct filetype_str filetype_str[] = {
[filetype_bmp] = { "BMP image", "bmp" },
[filetype_png] = { "PNG image", "png" },
[filetype_ext] = { "ext filesystem", "ext" },
+ [filetype_gpt] = { "GUID Partition Table", "gpt" },
};
const char *file_type_to_string(enum filetype f)
@@ -69,9 +70,53 @@ const char *file_type_to_short_string(enum filetype f)
#define MBR_StartSector 8 /* MBR: Offset of Starting Sector in Partition Table Entry */
#define BS_55AA 510 /* Boot sector signature (2) */
#define MBR_Table 446 /* MBR: Partition table offset (2) */
+#define MBR_partition_size 16 /* MBR: Partition table offset (2) */
#define BS_FilSysType32 82 /* File system type (1) */
#define BS_FilSysType 54 /* File system type (1) */
+#define MBR_PART_sys_ind 4
+#define MBR_PART_start_sect 8
+#define MBR_OSTYPE_EFI_GPT 0xee
+
+static inline int pmbr_part_valid(const uint8_t *buf)
+{
+ if (buf[MBR_PART_sys_ind] == MBR_OSTYPE_EFI_GPT &&
+ get_unaligned_le32(&buf[MBR_PART_start_sect]) == 1UL) {
+ return 1;
+ }
+
+ return 0;
+}
+
+/**
+ * is_gpt_valid(): test Protective MBR for validity and EFI PART
+ * @mbr: pointer to a legacy mbr structure
+ *
+ * Description: Returns 1 if PMBR is valid and EFI PART, 0 otherwise.
+ * Validity depends on three things:
+ * 1) MSDOS signature is in the last two bytes of the MBR
+ * 2) One partition of type 0xEE is found
+ * 3) EFI GPT signature is at offset 512
+ */
+static int is_gpt_valid(const uint8_t *buf)
+{
+ int i;
+
+ if (get_unaligned_le16(&buf[BS_55AA]) != 0xAA55)
+ return 0;
+
+ if (strncmp(&buf[512], "EFI PART", 8))
+ return 0;
+
+ buf += MBR_Table;
+
+ for (i = 0; i < 4; i++, buf += MBR_partition_size) {
+ if (pmbr_part_valid(buf))
+ return 1;
+ }
+ return 0;
+}
+
enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
{
/*
@@ -159,6 +204,13 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
if (bufsize < 512)
return filetype_unknown;
+ /*
+ * EFI GPT need to be detected before MBR otherwise
+ * we will detect a MBR
+ */
+ if (bufsize >= 520 && is_gpt_valid(buf8))
+ return filetype_gpt;
+
type = is_fat_or_mbr(buf8, NULL);
if (type != filetype_unknown)
return type;
diff --git a/include/filetype.h b/include/filetype.h
index 4d43757b8c..78ca5d2043 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -23,6 +23,7 @@ enum filetype {
filetype_bmp,
filetype_png,
filetype_ext,
+ filetype_gpt,
filetype_max,
};