From e89f1a1f188c196eade0d2ad05b662326d870be3 Mon Sep 17 00:00:00 2001 From: Vicente Bergas Date: Fri, 2 Oct 2015 00:08:38 +0100 Subject: detect_fs: use device instead of file detect_fs would usually mount a device on a directory, so, use a device-specific type detection. Signed-off-by: Vicente Bergas Signed-off-by: Sascha Hauer --- common/filetype.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'common/filetype.c') diff --git a/common/filetype.c b/common/filetype.c index 28a4b2ca25..dc2ff3f5f0 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; +} -- cgit v1.2.3