Skip to contentSkip to conversation

common/filetype: move partition-table detection into own function

PATCH In review Hubert Feurstein 3 participants 4 messages Apr 19, 2013 – Apr 29, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 common/filetype: move partition-table detection into own function

---
 common/filetype.c  | 41 ++++++++++++++++++++++++-----------------
 include/filetype.h |  1 +
 2 files changed, 25 insertions(+), 17 deletions(-)
common/filetype.c +24 -17
index 8652f1d..48fcdff 100644
@@ -151,13 +151,34 @@ enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 	return filetype_mbr;
 }
 
+enum filetype file_detect_partition_table(const void *_buf, size_t bufsize)
+{
+	const u8 *buf8 = _buf;
+	enum filetype type;
+
+	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;
+
+	return filetype_unknown;
+}
+
 enum filetype file_detect_type(const void *_buf, size_t bufsize)
 {
 	const u32 *buf = _buf;
 	const u64 *buf64 = _buf;
 	const u8 *buf8 = _buf;
 	const u16 *buf16 = _buf;
-	enum filetype type;
 
 	if (bufsize < 9)
 		return filetype_unknown;
@@ -204,24 +225,10 @@ 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;
-
-	if (bufsize < 1536)
-		return filetype_unknown;
-
-	if (buf16[512 + 28] == le16_to_cpu(0xef53))
+	if (bufsize >= 1536 && buf16[512 + 28] == le16_to_cpu(0xef53))
 		return filetype_ext;
 
-	return filetype_unknown;
+	return file_detect_partition_table(_buf, bufsize);
 }
 
 enum filetype file_name_detect_type(const char *filename)
include/filetype.h +1 -0
index 78ca5d2..ee777ac 100644
@@ -31,6 +31,7 @@ enum filetype {
 
 const char *file_type_to_string(enum filetype f);
 const char *file_type_to_short_string(enum filetype f);
+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);
Signature · 7 lines
1.8.1.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/2 common/partition: check only for partition table types

The detection of the partition table fails when we have a barebox image in the MBR. So check only for partition table types.

---
 common/partitions.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
common/partitions.c +2 -2
index dd25160..683b258 100644
@@ -89,7 +89,7 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
 	struct partition_parser *parser;
 
 	/* first new partition table as EFI GPT */
-	type = file_detect_type(buf, SECTOR_SIZE * 2);
+	type = file_detect_partition_table(buf, SECTOR_SIZE * 2);
 
 	list_for_each_entry(parser, &partition_parser_list, list) {
 		if (parser->type == type)
@@ -100,7 +100,7 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
 	 * so if EFI GPT not enable take it as MBR
 	 * useful for compatibility
 	 */
-	type = file_detect_type(buf, SECTOR_SIZE);
+	type = file_detect_partition_table(buf, SECTOR_SIZE);
 
 	list_for_each_entry(parser, &partition_parser_list, list) {
 		if (parser->type == type)
Signature · 7 lines
1.8.1.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw
In reply to Hubert Feurstein
Quoted 60 lines · Hubert Feurstein
---
 common/filetype.c  | 41 ++++++++++++++++++++++++-----------------
 include/filetype.h |  1 +
 2 files changed, 25 insertions(+), 17 deletions(-)
common/filetype.c +22 -11
index 8652f1d..48fcdff 100644
@@ -151,13 +151,34 @@ enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 	return filetype_mbr;
 }
 
+enum filetype file_detect_partition_table(const void *_buf, size_t bufsize)
+{
+	const u8 *buf8 = _buf;
+	enum filetype type;
+
+	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;
+
+	return filetype_unknown;
+}
+
 enum filetype file_detect_type(const void *_buf, size_t bufsize)
 {
 	const u32 *buf = _buf;
 	const u64 *buf64 = _buf;
 	const u8 *buf8 = _buf;
 	const u16 *buf16 = _buf;
-	enum filetype type;
 
 	if (bufsize < 9)
 		return filetype_unknown;
@@ -204,24 +225,10 @@ 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;

common/filetype.c enum filetype file_detect_type(const void *_buf, size_t bufsize) · line 231

Quoted 10 lines
common/filetype.c +2 -6
-
-	if (bufsize < 1536)
-		return filetype_unknown;
-
-	if (buf16[512 + 28] == le16_to_cpu(0xef53))
+	if (bufsize >= 1536 && buf16[512 + 28] == le16_to_cpu(0xef53))
 		return filetype_ext;
 
-	return filetype_unknown;
+	return file_detect_partition_table(_buf, bufsize);

no you need to move this UP and keep the if bufsize < 1536

and we always need to return unknown at the end

Quoted 23 lines
common/filetype.c +0 -0
 }
 
 enum filetype file_name_detect_type(const char *filename)
include/filetype.h +1 -0
index 78ca5d2..ee777ac 100644
@@ -31,6 +31,7 @@ enum filetype {
 
 const char *file_type_to_string(enum filetype f);
 const char *file_type_to_short_string(enum filetype f);
+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);
Signature · 7 lines
1.8.1.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#3 lore raw
In reply to Hubert Feurstein

Hi Hubert,

Quoted 5 lines · Hubert Feurstein
---
 common/filetype.c  | 41 ++++++++++++++++++++++++-----------------
 include/filetype.h |  1 +
 2 files changed, 25 insertions(+), 17 deletions(-)

Modified this slightly to the below and applied both.

Thanks
Sascha

8<------------------------------------------------------

From c06956e15f8fb32d46e479950719c30bae57e5bb Mon Sep 17 00:00:00 2001
From: Hubert Feurstein <[email protected]>
Date: Fri, 19 Apr 2013 10:46:04 +0200
Subject: [PATCH] common/filetype: move partition-table detection into own
function

---
 common/filetype.c  | 36 ++++++++++++++++++++++++------------
 include/filetype.h |  1 +
 2 files changed, 25 insertions(+), 12 deletions(-)
common/filetype.c +24 -12
index 8652f1d..1ff3dd2 100644
@@ -151,6 +151,28 @@ enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 	return filetype_mbr;
 }
 
+enum filetype file_detect_partition_table(const void *_buf, size_t bufsize)
+{
+	const u8 *buf8 = _buf;
+	enum filetype type;
+
+	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;
+
+	return filetype_unknown;
+}
+
 enum filetype file_detect_type(const void *_buf, size_t bufsize)
 {
 	const u32 *buf = _buf;
@@ -204,21 +226,11 @@ 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);
+	type = file_detect_partition_table(_buf, bufsize);
 	if (type != filetype_unknown)
 		return type;
 
-	if (bufsize < 1536)
-		return filetype_unknown;
-
-	if (buf16[512 + 28] == le16_to_cpu(0xef53))
+	if (bufsize >= 1536 && buf16[512 + 28] == le16_to_cpu(0xef53))
 		return filetype_ext;
 
 	return filetype_unknown;
include/filetype.h +1 -0
index 78ca5d2..ee777ac 100644
@@ -31,6 +31,7 @@ enum filetype {
 
 const char *file_type_to_string(enum filetype f);
 const char *file_type_to_short_string(enum filetype f);
+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);
Signature · 12 lines
1.8.2.rc2

-- 
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox