[PATCH 1/2] detect_fs: use device instead of file

Vicente Bergas vicencb at gmail.com
Thu Oct 1 16:08:38 PDT 2015


detect_fs would usually mount a device on a directory,
so, use a device-specific type detection.

Signed-off-by: Vicente Bergas <vicencb at gmail.com>
---
 common/filetype.c  | 37 +++++++++++++++++++++++++++++++++++++++
 fs/fs.c            |  2 +-
 include/filetype.h |  1 +
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/common/filetype.c b/common/filetype.c
index 28a4b2c..6240fd1 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -361,3 +361,40 @@ err_out:
 
 	return type;
 }
+
+enum filetype cdev_detect_type(const char *name)
+{
+	enum filetype type = filetype_unknown;
+	int ret;
+	struct cdev *cdev;
+	void *buf;
+
+	cdev = cdev_by_name(name);
+	if (!cdev)
+		return type;
+	buf = xzalloc(FILE_TYPE_SAFE_BUFSIZE);
+	ret = cdev_read(cdev, buf, FILE_TYPE_SAFE_BUFSIZE, 0, 0);
+	if (ret < 0)
+		goto err_out;
+
+	type = file_detect_type(buf, ret);
+
+	if (type == filetype_mbr) {
+		unsigned long bootsec;
+		/*
+		 * Get the first partition start sector
+		 * and check for FAT in it
+		 */
+		is_fat_or_mbr(buf, &bootsec);
+
+		ret = cdev_read(cdev, buf, 512, bootsec * 512, 0);
+		if (ret < 0)
+			goto err_out;
+
+		type = is_fat_or_mbr((u8 *)buf, NULL);
+	}
+
+err_out:
+	free(buf);
+	return type;
+}
diff --git a/fs/fs.c b/fs/fs.c
index 67c78cd..c041e41 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -1201,7 +1201,7 @@ EXPORT_SYMBOL(register_fs_driver);
 
 static const char *detect_fs(const char *filename)
 {
-	enum filetype type = file_name_detect_type(filename);
+	enum filetype type = cdev_detect_type(filename);
 	struct driver_d *drv;
 	struct fs_driver_d *fdrv;
 
diff --git a/include/filetype.h b/include/filetype.h
index e452b7a..cde543e 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -45,6 +45,7 @@ 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 cdev_detect_type(const char *name);
 enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec);
 int is_fat_boot_sector(const void *_buf);
 
-- 
2.6.0




More information about the barebox mailing list