xref: /aosp_15_r20/external/e2fsprogs/lib/blkid/probe.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * probe.c - identify a block device by its contents, and return a dev
3*6a54128fSAndroid Build Coastguard Worker  *           struct with the details
4*6a54128fSAndroid Build Coastguard Worker  *
5*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1999 by Andries Brouwer
6*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
7*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 2001 by Andreas Dilger
8*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 2004 Kay Sievers <[email protected]>
9*6a54128fSAndroid Build Coastguard Worker  *
10*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
11*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the
12*6a54128fSAndroid Build Coastguard Worker  * GNU Lesser General Public License.
13*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
14*6a54128fSAndroid Build Coastguard Worker  */
15*6a54128fSAndroid Build Coastguard Worker 
16*6a54128fSAndroid Build Coastguard Worker #include "config.h"
17*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
18*6a54128fSAndroid Build Coastguard Worker #include <string.h>
19*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
20*6a54128fSAndroid Build Coastguard Worker #include <time.h>
21*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
22*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
23*6a54128fSAndroid Build Coastguard Worker #include <ctype.h>
24*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
25*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_STAT_H
26*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
27*6a54128fSAndroid Build Coastguard Worker #endif
28*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_MKDEV_H
29*6a54128fSAndroid Build Coastguard Worker #include <sys/mkdev.h>
30*6a54128fSAndroid Build Coastguard Worker #endif
31*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
32*6a54128fSAndroid Build Coastguard Worker #include <sys/utsname.h>
33*6a54128fSAndroid Build Coastguard Worker #endif
34*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
35*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
36*6a54128fSAndroid Build Coastguard Worker #endif
37*6a54128fSAndroid Build Coastguard Worker #include "blkidP.h"
38*6a54128fSAndroid Build Coastguard Worker #include "uuid/uuid.h"
39*6a54128fSAndroid Build Coastguard Worker #include "probe.h"
40*6a54128fSAndroid Build Coastguard Worker 
figure_label_len(const unsigned char * label,int len)41*6a54128fSAndroid Build Coastguard Worker static int figure_label_len(const unsigned char *label, int len)
42*6a54128fSAndroid Build Coastguard Worker {
43*6a54128fSAndroid Build Coastguard Worker 	const unsigned char *end = label + len - 1;
44*6a54128fSAndroid Build Coastguard Worker 
45*6a54128fSAndroid Build Coastguard Worker 	while (end >= label && (*end == ' ' || *end == 0))
46*6a54128fSAndroid Build Coastguard Worker 		--end;
47*6a54128fSAndroid Build Coastguard Worker 	if (end >= label)
48*6a54128fSAndroid Build Coastguard Worker 		return end - label + 1;
49*6a54128fSAndroid Build Coastguard Worker 	return 0;
50*6a54128fSAndroid Build Coastguard Worker }
51*6a54128fSAndroid Build Coastguard Worker 
get_buffer(struct blkid_probe * pr,blkid_loff_t off,size_t len)52*6a54128fSAndroid Build Coastguard Worker static unsigned char *get_buffer(struct blkid_probe *pr,
53*6a54128fSAndroid Build Coastguard Worker 			  blkid_loff_t off, size_t len)
54*6a54128fSAndroid Build Coastguard Worker {
55*6a54128fSAndroid Build Coastguard Worker 	ssize_t		ret_read;
56*6a54128fSAndroid Build Coastguard Worker 	unsigned char	*newbuf;
57*6a54128fSAndroid Build Coastguard Worker 
58*6a54128fSAndroid Build Coastguard Worker 	if (off + len <= SB_BUFFER_SIZE) {
59*6a54128fSAndroid Build Coastguard Worker 		if (!pr->sbbuf) {
60*6a54128fSAndroid Build Coastguard Worker 			pr->sbbuf = malloc(SB_BUFFER_SIZE);
61*6a54128fSAndroid Build Coastguard Worker 			if (!pr->sbbuf)
62*6a54128fSAndroid Build Coastguard Worker 				return NULL;
63*6a54128fSAndroid Build Coastguard Worker 			if (lseek(pr->fd, 0, SEEK_SET) < 0)
64*6a54128fSAndroid Build Coastguard Worker 				return NULL;
65*6a54128fSAndroid Build Coastguard Worker 			ret_read = read(pr->fd, pr->sbbuf, SB_BUFFER_SIZE);
66*6a54128fSAndroid Build Coastguard Worker 			if (ret_read < 0)
67*6a54128fSAndroid Build Coastguard Worker 				ret_read = 0;
68*6a54128fSAndroid Build Coastguard Worker 			pr->sb_valid = ret_read;
69*6a54128fSAndroid Build Coastguard Worker 		}
70*6a54128fSAndroid Build Coastguard Worker 		if (off+len > pr->sb_valid)
71*6a54128fSAndroid Build Coastguard Worker 			return NULL;
72*6a54128fSAndroid Build Coastguard Worker 		return pr->sbbuf + off;
73*6a54128fSAndroid Build Coastguard Worker 	} else {
74*6a54128fSAndroid Build Coastguard Worker 		if (len > pr->buf_max) {
75*6a54128fSAndroid Build Coastguard Worker 			newbuf = realloc(pr->buf, len);
76*6a54128fSAndroid Build Coastguard Worker 			if (newbuf == NULL)
77*6a54128fSAndroid Build Coastguard Worker 				return NULL;
78*6a54128fSAndroid Build Coastguard Worker 			pr->buf = newbuf;
79*6a54128fSAndroid Build Coastguard Worker 			pr->buf_max = len;
80*6a54128fSAndroid Build Coastguard Worker 		}
81*6a54128fSAndroid Build Coastguard Worker 		if (blkid_llseek(pr->fd, off, SEEK_SET) < 0)
82*6a54128fSAndroid Build Coastguard Worker 			return NULL;
83*6a54128fSAndroid Build Coastguard Worker 		ret_read = read(pr->fd, pr->buf, len);
84*6a54128fSAndroid Build Coastguard Worker 		if (ret_read != (ssize_t) len)
85*6a54128fSAndroid Build Coastguard Worker 			return NULL;
86*6a54128fSAndroid Build Coastguard Worker 		return pr->buf;
87*6a54128fSAndroid Build Coastguard Worker 	}
88*6a54128fSAndroid Build Coastguard Worker }
89*6a54128fSAndroid Build Coastguard Worker 
90*6a54128fSAndroid Build Coastguard Worker 
91*6a54128fSAndroid Build Coastguard Worker /*
92*6a54128fSAndroid Build Coastguard Worker  * This is a special case code to check for an MDRAID device.  We do
93*6a54128fSAndroid Build Coastguard Worker  * this special since it requires checking for a superblock at the end
94*6a54128fSAndroid Build Coastguard Worker  * of the device.
95*6a54128fSAndroid Build Coastguard Worker  */
check_mdraid(int fd,unsigned char * ret_uuid)96*6a54128fSAndroid Build Coastguard Worker static int check_mdraid(int fd, unsigned char *ret_uuid)
97*6a54128fSAndroid Build Coastguard Worker {
98*6a54128fSAndroid Build Coastguard Worker 	struct mdp_superblock_s *md;
99*6a54128fSAndroid Build Coastguard Worker 	blkid_loff_t		offset;
100*6a54128fSAndroid Build Coastguard Worker 	char			buf[4096];
101*6a54128fSAndroid Build Coastguard Worker 
102*6a54128fSAndroid Build Coastguard Worker 	if (fd < 0)
103*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
104*6a54128fSAndroid Build Coastguard Worker 
105*6a54128fSAndroid Build Coastguard Worker 	offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536;
106*6a54128fSAndroid Build Coastguard Worker 
107*6a54128fSAndroid Build Coastguard Worker 	if (blkid_llseek(fd, offset, 0) < 0 ||
108*6a54128fSAndroid Build Coastguard Worker 	    read(fd, buf, 4096) != 4096)
109*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_IO;
110*6a54128fSAndroid Build Coastguard Worker 	/* Check for magic number */
111*6a54128fSAndroid Build Coastguard Worker 	if (memcmp("\251+N\374", buf, 4) && memcmp("\374N+\251", buf, 4))
112*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
113*6a54128fSAndroid Build Coastguard Worker 
114*6a54128fSAndroid Build Coastguard Worker 	if (!ret_uuid)
115*6a54128fSAndroid Build Coastguard Worker 		return 0;
116*6a54128fSAndroid Build Coastguard Worker 	*ret_uuid = 0;
117*6a54128fSAndroid Build Coastguard Worker 
118*6a54128fSAndroid Build Coastguard Worker 	/* The MD UUID is not contiguous in the superblock, make it so */
119*6a54128fSAndroid Build Coastguard Worker 	md = (struct mdp_superblock_s *)buf;
120*6a54128fSAndroid Build Coastguard Worker 	if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) {
121*6a54128fSAndroid Build Coastguard Worker 		memcpy(ret_uuid, &md->set_uuid0, 4);
122*6a54128fSAndroid Build Coastguard Worker 		memcpy(ret_uuid + 4, &md->set_uuid1, 12);
123*6a54128fSAndroid Build Coastguard Worker 	}
124*6a54128fSAndroid Build Coastguard Worker 	return 0;
125*6a54128fSAndroid Build Coastguard Worker }
126*6a54128fSAndroid Build Coastguard Worker 
set_uuid(blkid_dev dev,uuid_t uuid,const char * tag)127*6a54128fSAndroid Build Coastguard Worker static void set_uuid(blkid_dev dev, uuid_t uuid, const char *tag)
128*6a54128fSAndroid Build Coastguard Worker {
129*6a54128fSAndroid Build Coastguard Worker 	char	str[37];
130*6a54128fSAndroid Build Coastguard Worker 
131*6a54128fSAndroid Build Coastguard Worker 	if (!uuid_is_null(uuid)) {
132*6a54128fSAndroid Build Coastguard Worker 		uuid_unparse(uuid, str);
133*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(dev, tag ? tag : "UUID", str, sizeof(str));
134*6a54128fSAndroid Build Coastguard Worker 	}
135*6a54128fSAndroid Build Coastguard Worker }
136*6a54128fSAndroid Build Coastguard Worker 
get_ext2_info(blkid_dev dev,struct blkid_magic * id,unsigned char * buf)137*6a54128fSAndroid Build Coastguard Worker static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
138*6a54128fSAndroid Build Coastguard Worker 			  unsigned char *buf)
139*6a54128fSAndroid Build Coastguard Worker {
140*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es = (struct ext2_super_block *) buf;
141*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker 	DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n",
144*6a54128fSAndroid Build Coastguard Worker 		   blkid_le32(es->s_feature_compat),
145*6a54128fSAndroid Build Coastguard Worker 		   blkid_le32(es->s_feature_incompat),
146*6a54128fSAndroid Build Coastguard Worker 		   blkid_le32(es->s_feature_ro_compat)));
147*6a54128fSAndroid Build Coastguard Worker 
148*6a54128fSAndroid Build Coastguard Worker 	if (es->s_volume_name[0])
149*6a54128fSAndroid Build Coastguard Worker 		label = es->s_volume_name;
150*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
151*6a54128fSAndroid Build Coastguard Worker 
152*6a54128fSAndroid Build Coastguard Worker 	set_uuid(dev, es->s_uuid, 0);
153*6a54128fSAndroid Build Coastguard Worker 
154*6a54128fSAndroid Build Coastguard Worker 	if ((es->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
155*6a54128fSAndroid Build Coastguard Worker 	    !uuid_is_null(es->s_journal_uuid))
156*6a54128fSAndroid Build Coastguard Worker 		set_uuid(dev, es->s_journal_uuid, "EXT_JOURNAL");
157*6a54128fSAndroid Build Coastguard Worker 
158*6a54128fSAndroid Build Coastguard Worker 	if (strcmp(id->bim_type, "ext2") &&
159*6a54128fSAndroid Build Coastguard Worker 	    ((blkid_le32(es->s_feature_incompat) &
160*6a54128fSAndroid Build Coastguard Worker 	      EXT2_FEATURE_INCOMPAT_UNSUPPORTED) == 0))
161*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2"));
162*6a54128fSAndroid Build Coastguard Worker }
163*6a54128fSAndroid Build Coastguard Worker 
164*6a54128fSAndroid Build Coastguard Worker /*
165*6a54128fSAndroid Build Coastguard Worker  * Check to see if a filesystem is in /proc/filesystems.
166*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if found, 0 if not
167*6a54128fSAndroid Build Coastguard Worker  */
fs_proc_check(const char * fs_name)168*6a54128fSAndroid Build Coastguard Worker static int fs_proc_check(const char *fs_name)
169*6a54128fSAndroid Build Coastguard Worker {
170*6a54128fSAndroid Build Coastguard Worker 	FILE	*f;
171*6a54128fSAndroid Build Coastguard Worker 	char	buf[80], *cp, *t;
172*6a54128fSAndroid Build Coastguard Worker 
173*6a54128fSAndroid Build Coastguard Worker 	f = fopen("/proc/filesystems", "r");
174*6a54128fSAndroid Build Coastguard Worker 	if (!f)
175*6a54128fSAndroid Build Coastguard Worker 		return (0);
176*6a54128fSAndroid Build Coastguard Worker 	while (!feof(f)) {
177*6a54128fSAndroid Build Coastguard Worker 		if (!fgets(buf, sizeof(buf), f))
178*6a54128fSAndroid Build Coastguard Worker 			break;
179*6a54128fSAndroid Build Coastguard Worker 		cp = buf;
180*6a54128fSAndroid Build Coastguard Worker 		if (!isspace(*cp)) {
181*6a54128fSAndroid Build Coastguard Worker 			while (*cp && !isspace(*cp))
182*6a54128fSAndroid Build Coastguard Worker 				cp++;
183*6a54128fSAndroid Build Coastguard Worker 		}
184*6a54128fSAndroid Build Coastguard Worker 		while (*cp && isspace(*cp))
185*6a54128fSAndroid Build Coastguard Worker 			cp++;
186*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, '\n')) != NULL)
187*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
188*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, '\t')) != NULL)
189*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
190*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, ' ')) != NULL)
191*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
192*6a54128fSAndroid Build Coastguard Worker 		if (!strcmp(fs_name, cp)) {
193*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
194*6a54128fSAndroid Build Coastguard Worker 			return (1);
195*6a54128fSAndroid Build Coastguard Worker 		}
196*6a54128fSAndroid Build Coastguard Worker 	}
197*6a54128fSAndroid Build Coastguard Worker 	fclose(f);
198*6a54128fSAndroid Build Coastguard Worker 	return (0);
199*6a54128fSAndroid Build Coastguard Worker }
200*6a54128fSAndroid Build Coastguard Worker 
201*6a54128fSAndroid Build Coastguard Worker /*
202*6a54128fSAndroid Build Coastguard Worker  * Check to see if a filesystem is available as a module
203*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if found, 0 if not
204*6a54128fSAndroid Build Coastguard Worker  */
check_for_modules(const char * fs_name)205*6a54128fSAndroid Build Coastguard Worker static int check_for_modules(const char *fs_name)
206*6a54128fSAndroid Build Coastguard Worker {
207*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
208*6a54128fSAndroid Build Coastguard Worker 	struct utsname	uts;
209*6a54128fSAndroid Build Coastguard Worker 	FILE		*f;
210*6a54128fSAndroid Build Coastguard Worker 	char		buf[1024], *cp;
211*6a54128fSAndroid Build Coastguard Worker 	int		namesz;
212*6a54128fSAndroid Build Coastguard Worker 
213*6a54128fSAndroid Build Coastguard Worker 	if (uname(&uts))
214*6a54128fSAndroid Build Coastguard Worker 		return (0);
215*6a54128fSAndroid Build Coastguard Worker 	snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
216*6a54128fSAndroid Build Coastguard Worker 
217*6a54128fSAndroid Build Coastguard Worker 	f = fopen(buf, "r");
218*6a54128fSAndroid Build Coastguard Worker 	if (!f)
219*6a54128fSAndroid Build Coastguard Worker 		return (0);
220*6a54128fSAndroid Build Coastguard Worker 
221*6a54128fSAndroid Build Coastguard Worker 	namesz = strlen(fs_name);
222*6a54128fSAndroid Build Coastguard Worker 
223*6a54128fSAndroid Build Coastguard Worker 	while (!feof(f)) {
224*6a54128fSAndroid Build Coastguard Worker 		if (!fgets(buf, sizeof(buf), f))
225*6a54128fSAndroid Build Coastguard Worker 			break;
226*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strchr(buf, ':')) != NULL)
227*6a54128fSAndroid Build Coastguard Worker 			*cp = 0;
228*6a54128fSAndroid Build Coastguard Worker 		else
229*6a54128fSAndroid Build Coastguard Worker 			continue;
230*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strrchr(buf, '/')) != NULL)
231*6a54128fSAndroid Build Coastguard Worker 			cp++;
232*6a54128fSAndroid Build Coastguard Worker 		else
233*6a54128fSAndroid Build Coastguard Worker 			cp = buf;
234*6a54128fSAndroid Build Coastguard Worker 		if (!strncmp(cp, fs_name, namesz) &&
235*6a54128fSAndroid Build Coastguard Worker 		    (!strcmp(cp + namesz, ".ko") ||
236*6a54128fSAndroid Build Coastguard Worker 		     !strcmp(cp + namesz, ".ko.gz"))) {
237*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
238*6a54128fSAndroid Build Coastguard Worker 			return (1);
239*6a54128fSAndroid Build Coastguard Worker 		}
240*6a54128fSAndroid Build Coastguard Worker 	}
241*6a54128fSAndroid Build Coastguard Worker 	fclose(f);
242*6a54128fSAndroid Build Coastguard Worker #endif
243*6a54128fSAndroid Build Coastguard Worker 	return (0);
244*6a54128fSAndroid Build Coastguard Worker }
245*6a54128fSAndroid Build Coastguard Worker 
linux_version_code(void)246*6a54128fSAndroid Build Coastguard Worker static int linux_version_code(void)
247*6a54128fSAndroid Build Coastguard Worker {
248*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
249*6a54128fSAndroid Build Coastguard Worker 	struct utsname	ut;
250*6a54128fSAndroid Build Coastguard Worker 	static int	version_code = -1;
251*6a54128fSAndroid Build Coastguard Worker 	int		major, minor, rev;
252*6a54128fSAndroid Build Coastguard Worker 	char		*endptr;
253*6a54128fSAndroid Build Coastguard Worker 	const char 	*cp;
254*6a54128fSAndroid Build Coastguard Worker 
255*6a54128fSAndroid Build Coastguard Worker 	if (version_code > 0)
256*6a54128fSAndroid Build Coastguard Worker 		return version_code;
257*6a54128fSAndroid Build Coastguard Worker 
258*6a54128fSAndroid Build Coastguard Worker 	if (uname(&ut))
259*6a54128fSAndroid Build Coastguard Worker 		return 0;
260*6a54128fSAndroid Build Coastguard Worker 	cp = ut.release;
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker 	major = strtol(cp, &endptr, 10);
263*6a54128fSAndroid Build Coastguard Worker 	if (cp == endptr || *endptr != '.')
264*6a54128fSAndroid Build Coastguard Worker 		return 0;
265*6a54128fSAndroid Build Coastguard Worker 	cp = endptr + 1;
266*6a54128fSAndroid Build Coastguard Worker 	minor = strtol(cp, &endptr, 10);
267*6a54128fSAndroid Build Coastguard Worker 	if (cp == endptr || *endptr != '.')
268*6a54128fSAndroid Build Coastguard Worker 		return 0;
269*6a54128fSAndroid Build Coastguard Worker 	cp = endptr + 1;
270*6a54128fSAndroid Build Coastguard Worker 	rev = strtol(cp, &endptr, 10);
271*6a54128fSAndroid Build Coastguard Worker 	if (cp == endptr)
272*6a54128fSAndroid Build Coastguard Worker 		return 0;
273*6a54128fSAndroid Build Coastguard Worker 	version_code = (((major * 256) + minor) * 256) + rev;
274*6a54128fSAndroid Build Coastguard Worker 	return version_code;
275*6a54128fSAndroid Build Coastguard Worker #else
276*6a54128fSAndroid Build Coastguard Worker 	return 0;
277*6a54128fSAndroid Build Coastguard Worker #endif
278*6a54128fSAndroid Build Coastguard Worker }
279*6a54128fSAndroid Build Coastguard Worker 
280*6a54128fSAndroid Build Coastguard Worker #define EXT4_SUPPORTS_EXT2 (2 * 65536 + 6*256 + 29)
281*6a54128fSAndroid Build Coastguard Worker 
system_supports_ext2(void)282*6a54128fSAndroid Build Coastguard Worker static int system_supports_ext2(void)
283*6a54128fSAndroid Build Coastguard Worker {
284*6a54128fSAndroid Build Coastguard Worker 	static time_t	last_check = 0;
285*6a54128fSAndroid Build Coastguard Worker 	static int	ret = -1;
286*6a54128fSAndroid Build Coastguard Worker 	time_t		now = time(0);
287*6a54128fSAndroid Build Coastguard Worker 
288*6a54128fSAndroid Build Coastguard Worker 	if (ret != -1 || (now - last_check) < 5)
289*6a54128fSAndroid Build Coastguard Worker 		return ret;
290*6a54128fSAndroid Build Coastguard Worker 	last_check = now;
291*6a54128fSAndroid Build Coastguard Worker 	ret = (fs_proc_check("ext2") || check_for_modules("ext2"));
292*6a54128fSAndroid Build Coastguard Worker 	return ret;
293*6a54128fSAndroid Build Coastguard Worker }
294*6a54128fSAndroid Build Coastguard Worker 
system_supports_ext4(void)295*6a54128fSAndroid Build Coastguard Worker static int system_supports_ext4(void)
296*6a54128fSAndroid Build Coastguard Worker {
297*6a54128fSAndroid Build Coastguard Worker 	static time_t	last_check = 0;
298*6a54128fSAndroid Build Coastguard Worker 	static int	ret = -1;
299*6a54128fSAndroid Build Coastguard Worker 	time_t		now = time(0);
300*6a54128fSAndroid Build Coastguard Worker 
301*6a54128fSAndroid Build Coastguard Worker 	if (ret != -1 || (now - last_check) < 5)
302*6a54128fSAndroid Build Coastguard Worker 		return ret;
303*6a54128fSAndroid Build Coastguard Worker 	last_check = now;
304*6a54128fSAndroid Build Coastguard Worker 	ret = (fs_proc_check("ext4") || check_for_modules("ext4"));
305*6a54128fSAndroid Build Coastguard Worker 	return ret;
306*6a54128fSAndroid Build Coastguard Worker }
307*6a54128fSAndroid Build Coastguard Worker 
system_supports_ext4dev(void)308*6a54128fSAndroid Build Coastguard Worker static int system_supports_ext4dev(void)
309*6a54128fSAndroid Build Coastguard Worker {
310*6a54128fSAndroid Build Coastguard Worker 	static time_t	last_check = 0;
311*6a54128fSAndroid Build Coastguard Worker 	static int	ret = -1;
312*6a54128fSAndroid Build Coastguard Worker 	time_t		now = time(0);
313*6a54128fSAndroid Build Coastguard Worker 
314*6a54128fSAndroid Build Coastguard Worker 	if (ret != -1 || (now - last_check) < 5)
315*6a54128fSAndroid Build Coastguard Worker 		return ret;
316*6a54128fSAndroid Build Coastguard Worker 	last_check = now;
317*6a54128fSAndroid Build Coastguard Worker 	ret = (fs_proc_check("ext4dev") || check_for_modules("ext4dev"));
318*6a54128fSAndroid Build Coastguard Worker 	return ret;
319*6a54128fSAndroid Build Coastguard Worker }
320*6a54128fSAndroid Build Coastguard Worker 
probe_ext4dev(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)321*6a54128fSAndroid Build Coastguard Worker static int probe_ext4dev(struct blkid_probe *probe,
322*6a54128fSAndroid Build Coastguard Worker 			 struct blkid_magic *id,
323*6a54128fSAndroid Build Coastguard Worker 			 unsigned char *buf)
324*6a54128fSAndroid Build Coastguard Worker {
325*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es;
326*6a54128fSAndroid Build Coastguard Worker 	es = (struct ext2_super_block *)buf;
327*6a54128fSAndroid Build Coastguard Worker 
328*6a54128fSAndroid Build Coastguard Worker 	/* Distinguish from jbd */
329*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(es->s_feature_incompat) &
330*6a54128fSAndroid Build Coastguard Worker 	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
331*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
332*6a54128fSAndroid Build Coastguard Worker 
333*6a54128fSAndroid Build Coastguard Worker 	/*
334*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem does not have a journal and ext2 and ext4
335*6a54128fSAndroid Build Coastguard Worker 	 * is not present, then force this to be detected as an
336*6a54128fSAndroid Build Coastguard Worker 	 * ext4dev filesystem.
337*6a54128fSAndroid Build Coastguard Worker 	 */
338*6a54128fSAndroid Build Coastguard Worker 	if (!(blkid_le32(es->s_feature_compat) &
339*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
340*6a54128fSAndroid Build Coastguard Worker 	    !system_supports_ext2() && !system_supports_ext4() &&
341*6a54128fSAndroid Build Coastguard Worker 	    system_supports_ext4dev() &&
342*6a54128fSAndroid Build Coastguard Worker 	    linux_version_code() >= EXT4_SUPPORTS_EXT2)
343*6a54128fSAndroid Build Coastguard Worker 		goto force_ext4dev;
344*6a54128fSAndroid Build Coastguard Worker 
345*6a54128fSAndroid Build Coastguard Worker 	/*
346*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem is marked as OK for use by in-development
347*6a54128fSAndroid Build Coastguard Worker 	 * filesystem code, but ext4dev is not supported, and ext4 is,
348*6a54128fSAndroid Build Coastguard Worker 	 * then don't call ourselves ext4dev, since we should be
349*6a54128fSAndroid Build Coastguard Worker 	 * detected as ext4 in that case.
350*6a54128fSAndroid Build Coastguard Worker 	 *
351*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem is marked as in use by production
352*6a54128fSAndroid Build Coastguard Worker 	 * filesystem, then it can only be used by ext4 and NOT by
353*6a54128fSAndroid Build Coastguard Worker 	 * ext4dev, so always disclaim we are ext4dev in that case.
354*6a54128fSAndroid Build Coastguard Worker 	 */
355*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
356*6a54128fSAndroid Build Coastguard Worker 		if (!system_supports_ext4dev() && system_supports_ext4())
357*6a54128fSAndroid Build Coastguard Worker 			return -BLKID_ERR_PARAM;
358*6a54128fSAndroid Build Coastguard Worker 	} else
359*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
360*6a54128fSAndroid Build Coastguard Worker 
361*6a54128fSAndroid Build Coastguard Worker force_ext4dev:
362*6a54128fSAndroid Build Coastguard Worker     	get_ext2_info(probe->dev, id, buf);
363*6a54128fSAndroid Build Coastguard Worker 	return 0;
364*6a54128fSAndroid Build Coastguard Worker }
365*6a54128fSAndroid Build Coastguard Worker 
probe_ext4(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)366*6a54128fSAndroid Build Coastguard Worker static int probe_ext4(struct blkid_probe *probe, struct blkid_magic *id,
367*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
368*6a54128fSAndroid Build Coastguard Worker {
369*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es;
370*6a54128fSAndroid Build Coastguard Worker 	es = (struct ext2_super_block *)buf;
371*6a54128fSAndroid Build Coastguard Worker 
372*6a54128fSAndroid Build Coastguard Worker 	/* Distinguish from jbd */
373*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(es->s_feature_incompat) &
374*6a54128fSAndroid Build Coastguard Worker 	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
375*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
376*6a54128fSAndroid Build Coastguard Worker 
377*6a54128fSAndroid Build Coastguard Worker 	/*
378*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem does not have a journal and ext2 is not
379*6a54128fSAndroid Build Coastguard Worker 	 * present, then force this to be detected as an ext2
380*6a54128fSAndroid Build Coastguard Worker 	 * filesystem.
381*6a54128fSAndroid Build Coastguard Worker 	 */
382*6a54128fSAndroid Build Coastguard Worker 	if (!(blkid_le32(es->s_feature_compat) &
383*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
384*6a54128fSAndroid Build Coastguard Worker 	    !system_supports_ext2() && system_supports_ext4() &&
385*6a54128fSAndroid Build Coastguard Worker 	    linux_version_code() >= EXT4_SUPPORTS_EXT2)
386*6a54128fSAndroid Build Coastguard Worker 		goto force_ext4;
387*6a54128fSAndroid Build Coastguard Worker 
388*6a54128fSAndroid Build Coastguard Worker 	/* Ext4 has at least one feature which ext3 doesn't understand */
389*6a54128fSAndroid Build Coastguard Worker 	if (!(blkid_le32(es->s_feature_ro_compat) &
390*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) &&
391*6a54128fSAndroid Build Coastguard Worker 	    !(blkid_le32(es->s_feature_incompat) &
392*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
393*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
394*6a54128fSAndroid Build Coastguard Worker 
395*6a54128fSAndroid Build Coastguard Worker force_ext4:
396*6a54128fSAndroid Build Coastguard Worker 	/*
397*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem is a OK for use by in-development
398*6a54128fSAndroid Build Coastguard Worker 	 * filesystem code, and ext4dev is supported or ext4 is not
399*6a54128fSAndroid Build Coastguard Worker 	 * supported, then don't call ourselves ext4, so we can redo
400*6a54128fSAndroid Build Coastguard Worker 	 * the detection and mark the filesystem as ext4dev.
401*6a54128fSAndroid Build Coastguard Worker 	 *
402*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem is marked as in use by production
403*6a54128fSAndroid Build Coastguard Worker 	 * filesystem, then it can only be used by ext4 and NOT by
404*6a54128fSAndroid Build Coastguard Worker 	 * ext4dev.
405*6a54128fSAndroid Build Coastguard Worker 	 */
406*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(es->s_flags) & EXT2_FLAGS_TEST_FILESYS) {
407*6a54128fSAndroid Build Coastguard Worker 		if (system_supports_ext4dev() || !system_supports_ext4())
408*6a54128fSAndroid Build Coastguard Worker 			return -BLKID_ERR_PARAM;
409*6a54128fSAndroid Build Coastguard Worker 	}
410*6a54128fSAndroid Build Coastguard Worker     	get_ext2_info(probe->dev, id, buf);
411*6a54128fSAndroid Build Coastguard Worker 	return 0;
412*6a54128fSAndroid Build Coastguard Worker }
413*6a54128fSAndroid Build Coastguard Worker 
probe_ext3(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)414*6a54128fSAndroid Build Coastguard Worker static int probe_ext3(struct blkid_probe *probe, struct blkid_magic *id,
415*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
416*6a54128fSAndroid Build Coastguard Worker {
417*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es;
418*6a54128fSAndroid Build Coastguard Worker 	es = (struct ext2_super_block *)buf;
419*6a54128fSAndroid Build Coastguard Worker 
420*6a54128fSAndroid Build Coastguard Worker 	/* ext3 requires journal */
421*6a54128fSAndroid Build Coastguard Worker 	if (!(blkid_le32(es->s_feature_compat) &
422*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
423*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
424*6a54128fSAndroid Build Coastguard Worker 
425*6a54128fSAndroid Build Coastguard Worker 	/* Any features which ext3 doesn't understand */
426*6a54128fSAndroid Build Coastguard Worker 	if ((blkid_le32(es->s_feature_ro_compat) &
427*6a54128fSAndroid Build Coastguard Worker 	     EXT3_FEATURE_RO_COMPAT_UNSUPPORTED) ||
428*6a54128fSAndroid Build Coastguard Worker 	    (blkid_le32(es->s_feature_incompat) &
429*6a54128fSAndroid Build Coastguard Worker 	     EXT3_FEATURE_INCOMPAT_UNSUPPORTED))
430*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
431*6a54128fSAndroid Build Coastguard Worker 
432*6a54128fSAndroid Build Coastguard Worker     	get_ext2_info(probe->dev, id, buf);
433*6a54128fSAndroid Build Coastguard Worker 	return 0;
434*6a54128fSAndroid Build Coastguard Worker }
435*6a54128fSAndroid Build Coastguard Worker 
probe_ext2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)436*6a54128fSAndroid Build Coastguard Worker static int probe_ext2(struct blkid_probe *probe, struct blkid_magic *id,
437*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
438*6a54128fSAndroid Build Coastguard Worker {
439*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es;
440*6a54128fSAndroid Build Coastguard Worker 
441*6a54128fSAndroid Build Coastguard Worker 	es = (struct ext2_super_block *)buf;
442*6a54128fSAndroid Build Coastguard Worker 
443*6a54128fSAndroid Build Coastguard Worker 	/* Distinguish between ext3 and ext2 */
444*6a54128fSAndroid Build Coastguard Worker 	if ((blkid_le32(es->s_feature_compat) &
445*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_COMPAT_HAS_JOURNAL))
446*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
447*6a54128fSAndroid Build Coastguard Worker 
448*6a54128fSAndroid Build Coastguard Worker 	/* Any features which ext2 doesn't understand */
449*6a54128fSAndroid Build Coastguard Worker 	if ((blkid_le32(es->s_feature_ro_compat) &
450*6a54128fSAndroid Build Coastguard Worker 	     EXT2_FEATURE_RO_COMPAT_UNSUPPORTED) ||
451*6a54128fSAndroid Build Coastguard Worker 	    (blkid_le32(es->s_feature_incompat) &
452*6a54128fSAndroid Build Coastguard Worker 	     EXT2_FEATURE_INCOMPAT_UNSUPPORTED))
453*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
454*6a54128fSAndroid Build Coastguard Worker 
455*6a54128fSAndroid Build Coastguard Worker 	/*
456*6a54128fSAndroid Build Coastguard Worker 	 * If ext2 is not present, but ext4 or ext4dev are, then
457*6a54128fSAndroid Build Coastguard Worker 	 * disclaim we are ext2
458*6a54128fSAndroid Build Coastguard Worker 	 */
459*6a54128fSAndroid Build Coastguard Worker 	if (!system_supports_ext2() &&
460*6a54128fSAndroid Build Coastguard Worker 	    (system_supports_ext4() || system_supports_ext4dev()) &&
461*6a54128fSAndroid Build Coastguard Worker 	    linux_version_code() >= EXT4_SUPPORTS_EXT2)
462*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
463*6a54128fSAndroid Build Coastguard Worker 
464*6a54128fSAndroid Build Coastguard Worker 	get_ext2_info(probe->dev, id, buf);
465*6a54128fSAndroid Build Coastguard Worker 	return 0;
466*6a54128fSAndroid Build Coastguard Worker }
467*6a54128fSAndroid Build Coastguard Worker 
probe_jbd(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)468*6a54128fSAndroid Build Coastguard Worker static int probe_jbd(struct blkid_probe *probe, struct blkid_magic *id,
469*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf)
470*6a54128fSAndroid Build Coastguard Worker {
471*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *es = (struct ext2_super_block *) buf;
472*6a54128fSAndroid Build Coastguard Worker 
473*6a54128fSAndroid Build Coastguard Worker 	if (!(blkid_le32(es->s_feature_incompat) &
474*6a54128fSAndroid Build Coastguard Worker 	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
475*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
476*6a54128fSAndroid Build Coastguard Worker 
477*6a54128fSAndroid Build Coastguard Worker 	get_ext2_info(probe->dev, id, buf);
478*6a54128fSAndroid Build Coastguard Worker 
479*6a54128fSAndroid Build Coastguard Worker 	return 0;
480*6a54128fSAndroid Build Coastguard Worker }
481*6a54128fSAndroid Build Coastguard Worker 
482*6a54128fSAndroid Build Coastguard Worker #define FAT_ATTR_VOLUME_ID		0x08
483*6a54128fSAndroid Build Coastguard Worker #define FAT_ATTR_DIR			0x10
484*6a54128fSAndroid Build Coastguard Worker #define FAT_ATTR_LONG_NAME		0x0f
485*6a54128fSAndroid Build Coastguard Worker #define FAT_ATTR_MASK			0x3f
486*6a54128fSAndroid Build Coastguard Worker #define FAT_ENTRY_FREE			0xe5
487*6a54128fSAndroid Build Coastguard Worker 
488*6a54128fSAndroid Build Coastguard Worker static const char *no_name = "NO NAME    ";
489*6a54128fSAndroid Build Coastguard Worker 
search_fat_label(struct vfat_dir_entry * dir,int count)490*6a54128fSAndroid Build Coastguard Worker static unsigned char *search_fat_label(struct vfat_dir_entry *dir, int count)
491*6a54128fSAndroid Build Coastguard Worker {
492*6a54128fSAndroid Build Coastguard Worker 	int i;
493*6a54128fSAndroid Build Coastguard Worker 
494*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < count; i++) {
495*6a54128fSAndroid Build Coastguard Worker 		if (dir[i].name[0] == 0x00)
496*6a54128fSAndroid Build Coastguard Worker 			break;
497*6a54128fSAndroid Build Coastguard Worker 
498*6a54128fSAndroid Build Coastguard Worker 		if ((dir[i].name[0] == FAT_ENTRY_FREE) ||
499*6a54128fSAndroid Build Coastguard Worker 		    (dir[i].cluster_high != 0 || dir[i].cluster_low != 0) ||
500*6a54128fSAndroid Build Coastguard Worker 		    ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME))
501*6a54128fSAndroid Build Coastguard Worker 			continue;
502*6a54128fSAndroid Build Coastguard Worker 
503*6a54128fSAndroid Build Coastguard Worker 		if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) ==
504*6a54128fSAndroid Build Coastguard Worker 		    FAT_ATTR_VOLUME_ID) {
505*6a54128fSAndroid Build Coastguard Worker 			return dir[i].name;
506*6a54128fSAndroid Build Coastguard Worker 		}
507*6a54128fSAndroid Build Coastguard Worker 	}
508*6a54128fSAndroid Build Coastguard Worker 	return 0;
509*6a54128fSAndroid Build Coastguard Worker }
510*6a54128fSAndroid Build Coastguard Worker 
511*6a54128fSAndroid Build Coastguard Worker /* FAT label extraction from the root directory taken from Kay
512*6a54128fSAndroid Build Coastguard Worker  * Sievers's volume_id library */
probe_fat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)513*6a54128fSAndroid Build Coastguard Worker static int probe_fat(struct blkid_probe *probe,
514*6a54128fSAndroid Build Coastguard Worker 		      struct blkid_magic *id __BLKID_ATTR((unused)),
515*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
516*6a54128fSAndroid Build Coastguard Worker {
517*6a54128fSAndroid Build Coastguard Worker 	struct vfat_super_block *vs = (struct vfat_super_block *) buf;
518*6a54128fSAndroid Build Coastguard Worker 	struct msdos_super_block *ms = (struct msdos_super_block *) buf;
519*6a54128fSAndroid Build Coastguard Worker 	struct vfat_dir_entry *dir;
520*6a54128fSAndroid Build Coastguard Worker 	char serno[10];
521*6a54128fSAndroid Build Coastguard Worker 	const unsigned char *label = 0, *vol_label = 0, *tmp;
522*6a54128fSAndroid Build Coastguard Worker 	unsigned char	*vol_serno;
523*6a54128fSAndroid Build Coastguard Worker 	int label_len = 0, maxloop = 100;
524*6a54128fSAndroid Build Coastguard Worker 	__u16 sector_size, dir_entries, reserved;
525*6a54128fSAndroid Build Coastguard Worker 	__u32 sect_count, fat_size, dir_size, cluster_count, fat_length;
526*6a54128fSAndroid Build Coastguard Worker 	__u32 buf_size, start_data_sect, next, root_start, root_dir_entries;
527*6a54128fSAndroid Build Coastguard Worker 
528*6a54128fSAndroid Build Coastguard Worker 	/* sector size check */
529*6a54128fSAndroid Build Coastguard Worker 	tmp = (unsigned char *)&ms->ms_sector_size;
530*6a54128fSAndroid Build Coastguard Worker 	sector_size = tmp[0] + (tmp[1] << 8);
531*6a54128fSAndroid Build Coastguard Worker 	if (sector_size != 0x200 && sector_size != 0x400 &&
532*6a54128fSAndroid Build Coastguard Worker 	    sector_size != 0x800 && sector_size != 0x1000)
533*6a54128fSAndroid Build Coastguard Worker 		return 1;
534*6a54128fSAndroid Build Coastguard Worker 
535*6a54128fSAndroid Build Coastguard Worker 	tmp = (unsigned char *)&ms->ms_dir_entries;
536*6a54128fSAndroid Build Coastguard Worker 	dir_entries = tmp[0] + (tmp[1] << 8);
537*6a54128fSAndroid Build Coastguard Worker 	reserved =  blkid_le16(ms->ms_reserved);
538*6a54128fSAndroid Build Coastguard Worker 	tmp = (unsigned char *)&ms->ms_sectors;
539*6a54128fSAndroid Build Coastguard Worker 	sect_count = tmp[0] + (tmp[1] << 8);
540*6a54128fSAndroid Build Coastguard Worker 	if (sect_count == 0)
541*6a54128fSAndroid Build Coastguard Worker 		sect_count = blkid_le32(ms->ms_total_sect);
542*6a54128fSAndroid Build Coastguard Worker 
543*6a54128fSAndroid Build Coastguard Worker 	fat_length = blkid_le16(ms->ms_fat_length);
544*6a54128fSAndroid Build Coastguard Worker 	if (fat_length == 0)
545*6a54128fSAndroid Build Coastguard Worker 		fat_length = blkid_le32(vs->vs_fat32_length);
546*6a54128fSAndroid Build Coastguard Worker 
547*6a54128fSAndroid Build Coastguard Worker 	fat_size = fat_length * ms->ms_fats;
548*6a54128fSAndroid Build Coastguard Worker 	dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
549*6a54128fSAndroid Build Coastguard Worker 			(sector_size-1)) / sector_size;
550*6a54128fSAndroid Build Coastguard Worker 
551*6a54128fSAndroid Build Coastguard Worker 	cluster_count = sect_count - (reserved + fat_size + dir_size);
552*6a54128fSAndroid Build Coastguard Worker 	if (ms->ms_cluster_size == 0)
553*6a54128fSAndroid Build Coastguard Worker 		return 1;
554*6a54128fSAndroid Build Coastguard Worker 	cluster_count /= ms->ms_cluster_size;
555*6a54128fSAndroid Build Coastguard Worker 
556*6a54128fSAndroid Build Coastguard Worker 	if (cluster_count > FAT32_MAX)
557*6a54128fSAndroid Build Coastguard Worker 		return 1;
558*6a54128fSAndroid Build Coastguard Worker 
559*6a54128fSAndroid Build Coastguard Worker 	if (ms->ms_fat_length) {
560*6a54128fSAndroid Build Coastguard Worker 		/* the label may be an attribute in the root directory */
561*6a54128fSAndroid Build Coastguard Worker 		root_start = (reserved + fat_size) * sector_size;
562*6a54128fSAndroid Build Coastguard Worker 		root_dir_entries = vs->vs_dir_entries[0] +
563*6a54128fSAndroid Build Coastguard Worker 			(vs->vs_dir_entries[1] << 8);
564*6a54128fSAndroid Build Coastguard Worker 
565*6a54128fSAndroid Build Coastguard Worker 		buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
566*6a54128fSAndroid Build Coastguard Worker 		dir = (struct vfat_dir_entry *) get_buffer(probe, root_start,
567*6a54128fSAndroid Build Coastguard Worker 							   buf_size);
568*6a54128fSAndroid Build Coastguard Worker 		if (dir)
569*6a54128fSAndroid Build Coastguard Worker 			vol_label = search_fat_label(dir, root_dir_entries);
570*6a54128fSAndroid Build Coastguard Worker 
571*6a54128fSAndroid Build Coastguard Worker 		if (!vol_label || !memcmp(vol_label, no_name, 11))
572*6a54128fSAndroid Build Coastguard Worker 			vol_label = ms->ms_label;
573*6a54128fSAndroid Build Coastguard Worker 		vol_serno = ms->ms_serno;
574*6a54128fSAndroid Build Coastguard Worker 
575*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "SEC_TYPE", "msdos",
576*6a54128fSAndroid Build Coastguard Worker 			      sizeof("msdos"));
577*6a54128fSAndroid Build Coastguard Worker 	} else {
578*6a54128fSAndroid Build Coastguard Worker 		/* Search the FAT32 root dir for the label attribute */
579*6a54128fSAndroid Build Coastguard Worker 		buf_size = vs->vs_cluster_size * sector_size;
580*6a54128fSAndroid Build Coastguard Worker 		start_data_sect = reserved + fat_size;
581*6a54128fSAndroid Build Coastguard Worker 
582*6a54128fSAndroid Build Coastguard Worker 		next = blkid_le32(vs->vs_root_cluster);
583*6a54128fSAndroid Build Coastguard Worker 		while (next && --maxloop) {
584*6a54128fSAndroid Build Coastguard Worker 			__u32 next_sect_off;
585*6a54128fSAndroid Build Coastguard Worker 			__u64 next_off, fat_entry_off;
586*6a54128fSAndroid Build Coastguard Worker 			int count;
587*6a54128fSAndroid Build Coastguard Worker 
588*6a54128fSAndroid Build Coastguard Worker 			next_sect_off = (next - 2) * vs->vs_cluster_size;
589*6a54128fSAndroid Build Coastguard Worker 			next_off = (__u64) (start_data_sect + next_sect_off) *
590*6a54128fSAndroid Build Coastguard Worker 				sector_size;
591*6a54128fSAndroid Build Coastguard Worker 
592*6a54128fSAndroid Build Coastguard Worker 			dir = (struct vfat_dir_entry *)
593*6a54128fSAndroid Build Coastguard Worker 				get_buffer(probe, next_off, buf_size);
594*6a54128fSAndroid Build Coastguard Worker 			if (dir == NULL)
595*6a54128fSAndroid Build Coastguard Worker 				break;
596*6a54128fSAndroid Build Coastguard Worker 
597*6a54128fSAndroid Build Coastguard Worker 			count = buf_size / sizeof(struct vfat_dir_entry);
598*6a54128fSAndroid Build Coastguard Worker 
599*6a54128fSAndroid Build Coastguard Worker 			vol_label = search_fat_label(dir, count);
600*6a54128fSAndroid Build Coastguard Worker 			if (vol_label)
601*6a54128fSAndroid Build Coastguard Worker 				break;
602*6a54128fSAndroid Build Coastguard Worker 
603*6a54128fSAndroid Build Coastguard Worker 			/* get FAT entry */
604*6a54128fSAndroid Build Coastguard Worker 			fat_entry_off =
605*6a54128fSAndroid Build Coastguard Worker 				((unsigned int) reserved *
606*6a54128fSAndroid Build Coastguard Worker 				 (unsigned int) sector_size) +
607*6a54128fSAndroid Build Coastguard Worker 				(next * sizeof(__u32));
608*6a54128fSAndroid Build Coastguard Worker 			buf = get_buffer(probe, fat_entry_off, buf_size);
609*6a54128fSAndroid Build Coastguard Worker 			if (buf == NULL)
610*6a54128fSAndroid Build Coastguard Worker 				break;
611*6a54128fSAndroid Build Coastguard Worker 
612*6a54128fSAndroid Build Coastguard Worker 			/* set next cluster */
613*6a54128fSAndroid Build Coastguard Worker 			next = blkid_le32(*((__u32 *) buf) & 0x0fffffff);
614*6a54128fSAndroid Build Coastguard Worker 		}
615*6a54128fSAndroid Build Coastguard Worker 
616*6a54128fSAndroid Build Coastguard Worker 		if (!vol_label || !memcmp(vol_label, no_name, 11))
617*6a54128fSAndroid Build Coastguard Worker 			vol_label = vs->vs_label;
618*6a54128fSAndroid Build Coastguard Worker 		vol_serno = vs->vs_serno;
619*6a54128fSAndroid Build Coastguard Worker 	}
620*6a54128fSAndroid Build Coastguard Worker 
621*6a54128fSAndroid Build Coastguard Worker 	if (vol_label && memcmp(vol_label, no_name, 11)) {
622*6a54128fSAndroid Build Coastguard Worker 		if ((label_len = figure_label_len(vol_label, 11)))
623*6a54128fSAndroid Build Coastguard Worker 			label = vol_label;
624*6a54128fSAndroid Build Coastguard Worker 	}
625*6a54128fSAndroid Build Coastguard Worker 
626*6a54128fSAndroid Build Coastguard Worker 	/* We can't just print them as %04X, because they are unaligned */
627*6a54128fSAndroid Build Coastguard Worker 	sprintf(serno, "%02X%02X-%02X%02X", vol_serno[3], vol_serno[2],
628*6a54128fSAndroid Build Coastguard Worker 		vol_serno[1], vol_serno[0]);
629*6a54128fSAndroid Build Coastguard Worker 
630*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", (const char *) label, label_len);
631*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "UUID", serno, sizeof(serno)-1);
632*6a54128fSAndroid Build Coastguard Worker 
633*6a54128fSAndroid Build Coastguard Worker 	return 0;
634*6a54128fSAndroid Build Coastguard Worker }
635*6a54128fSAndroid Build Coastguard Worker 
636*6a54128fSAndroid Build Coastguard Worker /*
637*6a54128fSAndroid Build Coastguard Worker  * The FAT filesystem could be without a magic string in superblock
638*6a54128fSAndroid Build Coastguard Worker  * (e.g. old floppies).  This heuristic for FAT detection is inspired
639*6a54128fSAndroid Build Coastguard Worker  * by http://vrfy.org/projects/volume_id/ and Linux kernel.
640*6a54128fSAndroid Build Coastguard Worker  * [7-Jul-2005, Karel Zak <[email protected]>]
641*6a54128fSAndroid Build Coastguard Worker  */
probe_fat_nomagic(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)642*6a54128fSAndroid Build Coastguard Worker static int probe_fat_nomagic(struct blkid_probe *probe,
643*6a54128fSAndroid Build Coastguard Worker 			     struct blkid_magic *id __BLKID_ATTR((unused)),
644*6a54128fSAndroid Build Coastguard Worker 			     unsigned char *buf)
645*6a54128fSAndroid Build Coastguard Worker {
646*6a54128fSAndroid Build Coastguard Worker 	struct msdos_super_block *ms;
647*6a54128fSAndroid Build Coastguard Worker 
648*6a54128fSAndroid Build Coastguard Worker 	ms = (struct msdos_super_block *)buf;
649*6a54128fSAndroid Build Coastguard Worker 
650*6a54128fSAndroid Build Coastguard Worker 	/* heads check */
651*6a54128fSAndroid Build Coastguard Worker 	if (ms->ms_heads == 0)
652*6a54128fSAndroid Build Coastguard Worker 		return 1;
653*6a54128fSAndroid Build Coastguard Worker 
654*6a54128fSAndroid Build Coastguard Worker 	/* cluster size check*/
655*6a54128fSAndroid Build Coastguard Worker 	if (ms->ms_cluster_size == 0 ||
656*6a54128fSAndroid Build Coastguard Worker 	    (ms->ms_cluster_size & (ms->ms_cluster_size-1)))
657*6a54128fSAndroid Build Coastguard Worker 		return 1;
658*6a54128fSAndroid Build Coastguard Worker 
659*6a54128fSAndroid Build Coastguard Worker 	/* media check */
660*6a54128fSAndroid Build Coastguard Worker 	if (ms->ms_media < 0xf8 && ms->ms_media != 0xf0)
661*6a54128fSAndroid Build Coastguard Worker 		return 1;
662*6a54128fSAndroid Build Coastguard Worker 
663*6a54128fSAndroid Build Coastguard Worker 	/* fat counts(Linux kernel expects at least 1 FAT table) */
664*6a54128fSAndroid Build Coastguard Worker 	if (!ms->ms_fats)
665*6a54128fSAndroid Build Coastguard Worker 		return 1;
666*6a54128fSAndroid Build Coastguard Worker 
667*6a54128fSAndroid Build Coastguard Worker 	/*
668*6a54128fSAndroid Build Coastguard Worker 	 * OS/2 and apparently DFSee will place a FAT12/16-like
669*6a54128fSAndroid Build Coastguard Worker 	 * pseudo-superblock in the first 512 bytes of non-FAT
670*6a54128fSAndroid Build Coastguard Worker 	 * filesystems --- at least JFS and HPFS, and possibly others.
671*6a54128fSAndroid Build Coastguard Worker 	 * So we explicitly check for those filesystems at the
672*6a54128fSAndroid Build Coastguard Worker 	 * FAT12/16 filesystem magic field identifier, and if they are
673*6a54128fSAndroid Build Coastguard Worker 	 * present, we rule this out as a FAT filesystem, despite the
674*6a54128fSAndroid Build Coastguard Worker 	 * FAT-like pseudo-header.
675*6a54128fSAndroid Build Coastguard Worker          */
676*6a54128fSAndroid Build Coastguard Worker 	if ((memcmp(ms->ms_magic, "JFS     ", 8) == 0) ||
677*6a54128fSAndroid Build Coastguard Worker 	    (memcmp(ms->ms_magic, "HPFS    ", 8) == 0))
678*6a54128fSAndroid Build Coastguard Worker 		return 1;
679*6a54128fSAndroid Build Coastguard Worker 
680*6a54128fSAndroid Build Coastguard Worker 	return probe_fat(probe, id, buf);
681*6a54128fSAndroid Build Coastguard Worker }
682*6a54128fSAndroid Build Coastguard Worker 
probe_ntfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)683*6a54128fSAndroid Build Coastguard Worker static int probe_ntfs(struct blkid_probe *probe,
684*6a54128fSAndroid Build Coastguard Worker 		      struct blkid_magic *id __BLKID_ATTR((unused)),
685*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
686*6a54128fSAndroid Build Coastguard Worker {
687*6a54128fSAndroid Build Coastguard Worker 	struct ntfs_super_block *ns;
688*6a54128fSAndroid Build Coastguard Worker 	struct master_file_table_record *mft;
689*6a54128fSAndroid Build Coastguard Worker 	struct file_attribute *attr;
690*6a54128fSAndroid Build Coastguard Worker 	char		uuid_str[17], label_str[129], *cp;
691*6a54128fSAndroid Build Coastguard Worker 	int		bytes_per_sector, sectors_per_cluster;
692*6a54128fSAndroid Build Coastguard Worker 	int		mft_record_size, attr_off, attr_len;
693*6a54128fSAndroid Build Coastguard Worker 	unsigned int	i, attr_type, val_len;
694*6a54128fSAndroid Build Coastguard Worker 	int		val_off;
695*6a54128fSAndroid Build Coastguard Worker 	__u64		nr_clusters;
696*6a54128fSAndroid Build Coastguard Worker 	blkid_loff_t off;
697*6a54128fSAndroid Build Coastguard Worker 	unsigned char *buf_mft, *val;
698*6a54128fSAndroid Build Coastguard Worker 
699*6a54128fSAndroid Build Coastguard Worker 	ns = (struct ntfs_super_block *) buf;
700*6a54128fSAndroid Build Coastguard Worker 
701*6a54128fSAndroid Build Coastguard Worker 	bytes_per_sector = ns->bios_parameter_block[0] +
702*6a54128fSAndroid Build Coastguard Worker 		(ns->bios_parameter_block[1]  << 8);
703*6a54128fSAndroid Build Coastguard Worker 	sectors_per_cluster = ns->bios_parameter_block[2];
704*6a54128fSAndroid Build Coastguard Worker 
705*6a54128fSAndroid Build Coastguard Worker 	if ((bytes_per_sector < 512) || (sectors_per_cluster == 0))
706*6a54128fSAndroid Build Coastguard Worker 		return 1;
707*6a54128fSAndroid Build Coastguard Worker 
708*6a54128fSAndroid Build Coastguard Worker 	if (ns->cluster_per_mft_record < 0)
709*6a54128fSAndroid Build Coastguard Worker 		mft_record_size = 1 << (0-ns->cluster_per_mft_record);
710*6a54128fSAndroid Build Coastguard Worker 	else
711*6a54128fSAndroid Build Coastguard Worker 		mft_record_size = ns->cluster_per_mft_record *
712*6a54128fSAndroid Build Coastguard Worker 			sectors_per_cluster * bytes_per_sector;
713*6a54128fSAndroid Build Coastguard Worker 	nr_clusters = blkid_le64(ns->number_of_sectors) / sectors_per_cluster;
714*6a54128fSAndroid Build Coastguard Worker 
715*6a54128fSAndroid Build Coastguard Worker 	if ((blkid_le64(ns->mft_cluster_location) > nr_clusters) ||
716*6a54128fSAndroid Build Coastguard Worker 	    (blkid_le64(ns->mft_mirror_cluster_location) > nr_clusters))
717*6a54128fSAndroid Build Coastguard Worker 		return 1;
718*6a54128fSAndroid Build Coastguard Worker 
719*6a54128fSAndroid Build Coastguard Worker 	off = blkid_le64(ns->mft_mirror_cluster_location) *
720*6a54128fSAndroid Build Coastguard Worker 		bytes_per_sector * sectors_per_cluster;
721*6a54128fSAndroid Build Coastguard Worker 
722*6a54128fSAndroid Build Coastguard Worker 	buf_mft = get_buffer(probe, off, mft_record_size);
723*6a54128fSAndroid Build Coastguard Worker 	if (!buf_mft)
724*6a54128fSAndroid Build Coastguard Worker 		return 1;
725*6a54128fSAndroid Build Coastguard Worker 
726*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(buf_mft, "FILE", 4))
727*6a54128fSAndroid Build Coastguard Worker 		return 1;
728*6a54128fSAndroid Build Coastguard Worker 
729*6a54128fSAndroid Build Coastguard Worker 	off = blkid_le64(ns->mft_cluster_location) * bytes_per_sector *
730*6a54128fSAndroid Build Coastguard Worker 		sectors_per_cluster;
731*6a54128fSAndroid Build Coastguard Worker 
732*6a54128fSAndroid Build Coastguard Worker 	buf_mft = get_buffer(probe, off, mft_record_size);
733*6a54128fSAndroid Build Coastguard Worker 	if (!buf_mft)
734*6a54128fSAndroid Build Coastguard Worker 		return 1;
735*6a54128fSAndroid Build Coastguard Worker 
736*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(buf_mft, "FILE", 4))
737*6a54128fSAndroid Build Coastguard Worker 		return 1;
738*6a54128fSAndroid Build Coastguard Worker 
739*6a54128fSAndroid Build Coastguard Worker 	off += MFT_RECORD_VOLUME * mft_record_size;
740*6a54128fSAndroid Build Coastguard Worker 
741*6a54128fSAndroid Build Coastguard Worker 	buf_mft = get_buffer(probe, off, mft_record_size);
742*6a54128fSAndroid Build Coastguard Worker 	if (!buf_mft)
743*6a54128fSAndroid Build Coastguard Worker 		return 1;
744*6a54128fSAndroid Build Coastguard Worker 
745*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(buf_mft, "FILE", 4))
746*6a54128fSAndroid Build Coastguard Worker 		return 1;
747*6a54128fSAndroid Build Coastguard Worker 
748*6a54128fSAndroid Build Coastguard Worker 	mft = (struct master_file_table_record *) buf_mft;
749*6a54128fSAndroid Build Coastguard Worker 
750*6a54128fSAndroid Build Coastguard Worker 	attr_off = blkid_le16(mft->attrs_offset);
751*6a54128fSAndroid Build Coastguard Worker 	label_str[0] = 0;
752*6a54128fSAndroid Build Coastguard Worker 
753*6a54128fSAndroid Build Coastguard Worker 	while (1) {
754*6a54128fSAndroid Build Coastguard Worker 		attr = (struct file_attribute *) (buf_mft + attr_off);
755*6a54128fSAndroid Build Coastguard Worker 		attr_len = blkid_le16(attr->len);
756*6a54128fSAndroid Build Coastguard Worker 		attr_type = blkid_le32(attr->type);
757*6a54128fSAndroid Build Coastguard Worker 		val_off = blkid_le16(attr->value_offset);
758*6a54128fSAndroid Build Coastguard Worker 		val_len = blkid_le32(attr->value_len);
759*6a54128fSAndroid Build Coastguard Worker 
760*6a54128fSAndroid Build Coastguard Worker 		attr_off += attr_len;
761*6a54128fSAndroid Build Coastguard Worker 
762*6a54128fSAndroid Build Coastguard Worker 		if ((attr_off > mft_record_size) ||
763*6a54128fSAndroid Build Coastguard Worker 		    (attr_len == 0))
764*6a54128fSAndroid Build Coastguard Worker 			break;
765*6a54128fSAndroid Build Coastguard Worker 
766*6a54128fSAndroid Build Coastguard Worker 		if (attr_type == MFT_RECORD_ATTR_END)
767*6a54128fSAndroid Build Coastguard Worker 			break;
768*6a54128fSAndroid Build Coastguard Worker 
769*6a54128fSAndroid Build Coastguard Worker 		if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
770*6a54128fSAndroid Build Coastguard Worker 			if (val_len > sizeof(label_str))
771*6a54128fSAndroid Build Coastguard Worker 				val_len = sizeof(label_str)-1;
772*6a54128fSAndroid Build Coastguard Worker 
773*6a54128fSAndroid Build Coastguard Worker 			for (i=0, cp=label_str; i < val_len; i+=2,cp++) {
774*6a54128fSAndroid Build Coastguard Worker 				val = ((__u8 *) attr) + val_off + i;
775*6a54128fSAndroid Build Coastguard Worker 				*cp = val[0];
776*6a54128fSAndroid Build Coastguard Worker 				if (val[1])
777*6a54128fSAndroid Build Coastguard Worker 					*cp = '?';
778*6a54128fSAndroid Build Coastguard Worker 			}
779*6a54128fSAndroid Build Coastguard Worker 			*cp = 0;
780*6a54128fSAndroid Build Coastguard Worker 		}
781*6a54128fSAndroid Build Coastguard Worker 	}
782*6a54128fSAndroid Build Coastguard Worker 
783*6a54128fSAndroid Build Coastguard Worker 	sprintf(uuid_str, "%016llX", blkid_le64(ns->volume_serial));
784*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
785*6a54128fSAndroid Build Coastguard Worker 	if (label_str[0])
786*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "LABEL", label_str, 0);
787*6a54128fSAndroid Build Coastguard Worker 	return 0;
788*6a54128fSAndroid Build Coastguard Worker }
789*6a54128fSAndroid Build Coastguard Worker 
790*6a54128fSAndroid Build Coastguard Worker 
probe_xfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)791*6a54128fSAndroid Build Coastguard Worker static int probe_xfs(struct blkid_probe *probe,
792*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
793*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf)
794*6a54128fSAndroid Build Coastguard Worker {
795*6a54128fSAndroid Build Coastguard Worker 	struct xfs_super_block *xs;
796*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
797*6a54128fSAndroid Build Coastguard Worker 
798*6a54128fSAndroid Build Coastguard Worker 	xs = (struct xfs_super_block *)buf;
799*6a54128fSAndroid Build Coastguard Worker 
800*6a54128fSAndroid Build Coastguard Worker 	if (strlen(xs->xs_fname))
801*6a54128fSAndroid Build Coastguard Worker 		label = xs->xs_fname;
802*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, sizeof(xs->xs_fname));
803*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, xs->xs_uuid, 0);
804*6a54128fSAndroid Build Coastguard Worker 	return 0;
805*6a54128fSAndroid Build Coastguard Worker }
806*6a54128fSAndroid Build Coastguard Worker 
probe_reiserfs(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)807*6a54128fSAndroid Build Coastguard Worker static int probe_reiserfs(struct blkid_probe *probe,
808*6a54128fSAndroid Build Coastguard Worker 			  struct blkid_magic *id, unsigned char *buf)
809*6a54128fSAndroid Build Coastguard Worker {
810*6a54128fSAndroid Build Coastguard Worker 	struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf;
811*6a54128fSAndroid Build Coastguard Worker 	unsigned int blocksize;
812*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
813*6a54128fSAndroid Build Coastguard Worker 
814*6a54128fSAndroid Build Coastguard Worker 	blocksize = blkid_le16(rs->rs_blocksize);
815*6a54128fSAndroid Build Coastguard Worker 
816*6a54128fSAndroid Build Coastguard Worker 	/* The blocksize must be at least 1k */
817*6a54128fSAndroid Build Coastguard Worker 	if ((blocksize >> 10) == 0)
818*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_PARAM;
819*6a54128fSAndroid Build Coastguard Worker 
820*6a54128fSAndroid Build Coastguard Worker 	/* If the superblock is inside the journal, we have the wrong one */
821*6a54128fSAndroid Build Coastguard Worker 	if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block))
822*6a54128fSAndroid Build Coastguard Worker 		return -BLKID_ERR_BIG;
823*6a54128fSAndroid Build Coastguard Worker 
824*6a54128fSAndroid Build Coastguard Worker 	/* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
825*6a54128fSAndroid Build Coastguard Worker 	if (id->bim_magic[6] == '2' || id->bim_magic[6] == '3') {
826*6a54128fSAndroid Build Coastguard Worker 		if (strlen(rs->rs_label))
827*6a54128fSAndroid Build Coastguard Worker 			label = rs->rs_label;
828*6a54128fSAndroid Build Coastguard Worker 		set_uuid(probe->dev, rs->rs_uuid, 0);
829*6a54128fSAndroid Build Coastguard Worker 	}
830*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, sizeof(rs->rs_label));
831*6a54128fSAndroid Build Coastguard Worker 
832*6a54128fSAndroid Build Coastguard Worker 	return 0;
833*6a54128fSAndroid Build Coastguard Worker }
834*6a54128fSAndroid Build Coastguard Worker 
probe_reiserfs4(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)835*6a54128fSAndroid Build Coastguard Worker static int probe_reiserfs4(struct blkid_probe *probe,
836*6a54128fSAndroid Build Coastguard Worker 			   struct blkid_magic *id __BLKID_ATTR((unused)),
837*6a54128fSAndroid Build Coastguard Worker 			   unsigned char *buf)
838*6a54128fSAndroid Build Coastguard Worker {
839*6a54128fSAndroid Build Coastguard Worker 	struct reiser4_super_block *rs4 = (struct reiser4_super_block *) buf;
840*6a54128fSAndroid Build Coastguard Worker 	const unsigned char *label = 0;
841*6a54128fSAndroid Build Coastguard Worker 
842*6a54128fSAndroid Build Coastguard Worker 	if (strlen((char *) rs4->rs4_label))
843*6a54128fSAndroid Build Coastguard Worker 		label = rs4->rs4_label;
844*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, rs4->rs4_uuid, 0);
845*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
846*6a54128fSAndroid Build Coastguard Worker 		      sizeof(rs4->rs4_label));
847*6a54128fSAndroid Build Coastguard Worker 
848*6a54128fSAndroid Build Coastguard Worker 	return 0;
849*6a54128fSAndroid Build Coastguard Worker }
850*6a54128fSAndroid Build Coastguard Worker 
probe_jfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)851*6a54128fSAndroid Build Coastguard Worker static int probe_jfs(struct blkid_probe *probe,
852*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
853*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf)
854*6a54128fSAndroid Build Coastguard Worker {
855*6a54128fSAndroid Build Coastguard Worker 	struct jfs_super_block *js;
856*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
857*6a54128fSAndroid Build Coastguard Worker 
858*6a54128fSAndroid Build Coastguard Worker 	js = (struct jfs_super_block *)buf;
859*6a54128fSAndroid Build Coastguard Worker 
860*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(js->js_bsize) != (1U << blkid_le16(js->js_l2bsize)))
861*6a54128fSAndroid Build Coastguard Worker 		return 1;
862*6a54128fSAndroid Build Coastguard Worker 
863*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le32(js->js_pbsize) != (1U << blkid_le16(js->js_l2pbsize)))
864*6a54128fSAndroid Build Coastguard Worker 		return 1;
865*6a54128fSAndroid Build Coastguard Worker 
866*6a54128fSAndroid Build Coastguard Worker 	if ((blkid_le16(js->js_l2bsize) - blkid_le16(js->js_l2pbsize)) !=
867*6a54128fSAndroid Build Coastguard Worker 	    blkid_le16(js->js_l2bfactor))
868*6a54128fSAndroid Build Coastguard Worker 		return 1;
869*6a54128fSAndroid Build Coastguard Worker 
870*6a54128fSAndroid Build Coastguard Worker 	if (strlen((char *) js->js_label))
871*6a54128fSAndroid Build Coastguard Worker 		label = (char *) js->js_label;
872*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, sizeof(js->js_label));
873*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, js->js_uuid, 0);
874*6a54128fSAndroid Build Coastguard Worker 	return 0;
875*6a54128fSAndroid Build Coastguard Worker }
876*6a54128fSAndroid Build Coastguard Worker 
probe_zfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))877*6a54128fSAndroid Build Coastguard Worker static int probe_zfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
878*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
879*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf __BLKID_ATTR((unused)))
880*6a54128fSAndroid Build Coastguard Worker {
881*6a54128fSAndroid Build Coastguard Worker #if 0
882*6a54128fSAndroid Build Coastguard Worker 	char *vdev_label;
883*6a54128fSAndroid Build Coastguard Worker 	const char *pool_name = 0;
884*6a54128fSAndroid Build Coastguard Worker 
885*6a54128fSAndroid Build Coastguard Worker 	/* read nvpair data for pool name, pool GUID (complex) */
886*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", pool_name, sizeof(pool_name));
887*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, pool_guid, 0);
888*6a54128fSAndroid Build Coastguard Worker #endif
889*6a54128fSAndroid Build Coastguard Worker 	return 0;
890*6a54128fSAndroid Build Coastguard Worker }
891*6a54128fSAndroid Build Coastguard Worker 
probe_luks(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)892*6a54128fSAndroid Build Coastguard Worker static int probe_luks(struct blkid_probe *probe,
893*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id __BLKID_ATTR((unused)),
894*6a54128fSAndroid Build Coastguard Worker 		       unsigned char *buf)
895*6a54128fSAndroid Build Coastguard Worker {
896*6a54128fSAndroid Build Coastguard Worker 	char uuid[41];
897*6a54128fSAndroid Build Coastguard Worker 
898*6a54128fSAndroid Build Coastguard Worker 	/* 168 is the offset to the 40 character uuid:
899*6a54128fSAndroid Build Coastguard Worker 	 * http://luks.endorphin.org/LUKS-on-disk-format.pdf */
900*6a54128fSAndroid Build Coastguard Worker 	strncpy(uuid, (char *) buf+168, 40);
901*6a54128fSAndroid Build Coastguard Worker 	uuid[40] = 0;
902*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "UUID", uuid, 40);
903*6a54128fSAndroid Build Coastguard Worker 	return 0;
904*6a54128fSAndroid Build Coastguard Worker }
905*6a54128fSAndroid Build Coastguard Worker 
probe_romfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)906*6a54128fSAndroid Build Coastguard Worker static int probe_romfs(struct blkid_probe *probe,
907*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id __BLKID_ATTR((unused)),
908*6a54128fSAndroid Build Coastguard Worker 		       unsigned char *buf)
909*6a54128fSAndroid Build Coastguard Worker {
910*6a54128fSAndroid Build Coastguard Worker 	struct romfs_super_block *ros;
911*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
912*6a54128fSAndroid Build Coastguard Worker 
913*6a54128fSAndroid Build Coastguard Worker 	ros = (struct romfs_super_block *)buf;
914*6a54128fSAndroid Build Coastguard Worker 
915*6a54128fSAndroid Build Coastguard Worker 	if (strlen((char *) ros->ros_volume))
916*6a54128fSAndroid Build Coastguard Worker 		label = (char *) ros->ros_volume;
917*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, 0);
918*6a54128fSAndroid Build Coastguard Worker 	return 0;
919*6a54128fSAndroid Build Coastguard Worker }
920*6a54128fSAndroid Build Coastguard Worker 
probe_cramfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)921*6a54128fSAndroid Build Coastguard Worker static int probe_cramfs(struct blkid_probe *probe,
922*6a54128fSAndroid Build Coastguard Worker 			struct blkid_magic *id __BLKID_ATTR((unused)),
923*6a54128fSAndroid Build Coastguard Worker 			unsigned char *buf)
924*6a54128fSAndroid Build Coastguard Worker {
925*6a54128fSAndroid Build Coastguard Worker 	struct cramfs_super_block *csb;
926*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
927*6a54128fSAndroid Build Coastguard Worker 
928*6a54128fSAndroid Build Coastguard Worker 	csb = (struct cramfs_super_block *)buf;
929*6a54128fSAndroid Build Coastguard Worker 
930*6a54128fSAndroid Build Coastguard Worker 	if (strlen((char *) csb->name))
931*6a54128fSAndroid Build Coastguard Worker 		label = (char *) csb->name;
932*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, 0);
933*6a54128fSAndroid Build Coastguard Worker 	return 0;
934*6a54128fSAndroid Build Coastguard Worker }
935*6a54128fSAndroid Build Coastguard Worker 
probe_swap0(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))936*6a54128fSAndroid Build Coastguard Worker static int probe_swap0(struct blkid_probe *probe,
937*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id __BLKID_ATTR((unused)),
938*6a54128fSAndroid Build Coastguard Worker 		       unsigned char *buf __BLKID_ATTR((unused)))
939*6a54128fSAndroid Build Coastguard Worker {
940*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "UUID", 0, 0);
941*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", 0, 0);
942*6a54128fSAndroid Build Coastguard Worker 	return 0;
943*6a54128fSAndroid Build Coastguard Worker }
944*6a54128fSAndroid Build Coastguard Worker 
probe_swap1(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf __BLKID_ATTR ((unused)))945*6a54128fSAndroid Build Coastguard Worker static int probe_swap1(struct blkid_probe *probe,
946*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id,
947*6a54128fSAndroid Build Coastguard Worker 		       unsigned char *buf __BLKID_ATTR((unused)))
948*6a54128fSAndroid Build Coastguard Worker {
949*6a54128fSAndroid Build Coastguard Worker 	struct swap_id_block *sws;
950*6a54128fSAndroid Build Coastguard Worker 
951*6a54128fSAndroid Build Coastguard Worker 	probe_swap0(probe, id, buf);
952*6a54128fSAndroid Build Coastguard Worker 	/*
953*6a54128fSAndroid Build Coastguard Worker 	 * Version 1 swap headers are always located at offset of 1024
954*6a54128fSAndroid Build Coastguard Worker 	 * bytes, although the swap signature itself is located at the
955*6a54128fSAndroid Build Coastguard Worker 	 * end of the page (which may vary depending on hardware
956*6a54128fSAndroid Build Coastguard Worker 	 * pagesize).
957*6a54128fSAndroid Build Coastguard Worker 	 */
958*6a54128fSAndroid Build Coastguard Worker 	sws = (struct swap_id_block *) get_buffer(probe, 1024, 1024);
959*6a54128fSAndroid Build Coastguard Worker 	if (!sws)
960*6a54128fSAndroid Build Coastguard Worker 		return 1;
961*6a54128fSAndroid Build Coastguard Worker 
962*6a54128fSAndroid Build Coastguard Worker 	/* check for wrong version or zeroed pagecount, for sanity */
963*6a54128fSAndroid Build Coastguard Worker 	if (!memcmp(id->bim_magic, "SWAPSPACE2", id->bim_len) &&
964*6a54128fSAndroid Build Coastguard Worker 			(sws->sws_version != 1 || sws->sws_lastpage == 0))
965*6a54128fSAndroid Build Coastguard Worker 		return 1;
966*6a54128fSAndroid Build Coastguard Worker 
967*6a54128fSAndroid Build Coastguard Worker 	/* arbitrary sanity check.. is there any garbage down there? */
968*6a54128fSAndroid Build Coastguard Worker 	if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0)  {
969*6a54128fSAndroid Build Coastguard Worker 		if (sws->sws_volume[0])
970*6a54128fSAndroid Build Coastguard Worker 			blkid_set_tag(probe->dev, "LABEL", sws->sws_volume,
971*6a54128fSAndroid Build Coastguard Worker 				      sizeof(sws->sws_volume));
972*6a54128fSAndroid Build Coastguard Worker 		if (sws->sws_uuid[0])
973*6a54128fSAndroid Build Coastguard Worker 			set_uuid(probe->dev, sws->sws_uuid, 0);
974*6a54128fSAndroid Build Coastguard Worker 	}
975*6a54128fSAndroid Build Coastguard Worker 	return 0;
976*6a54128fSAndroid Build Coastguard Worker }
977*6a54128fSAndroid Build Coastguard Worker 
probe_iso9660(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)978*6a54128fSAndroid Build Coastguard Worker static int probe_iso9660(struct blkid_probe *probe,
979*6a54128fSAndroid Build Coastguard Worker 			 struct blkid_magic *id __BLKID_ATTR((unused)),
980*6a54128fSAndroid Build Coastguard Worker 			 unsigned char *buf)
981*6a54128fSAndroid Build Coastguard Worker {
982*6a54128fSAndroid Build Coastguard Worker 	struct iso_volume_descriptor *iso;
983*6a54128fSAndroid Build Coastguard Worker 	const unsigned char *label;
984*6a54128fSAndroid Build Coastguard Worker 
985*6a54128fSAndroid Build Coastguard Worker 	iso = (struct iso_volume_descriptor *) buf;
986*6a54128fSAndroid Build Coastguard Worker 	label = iso->volume_id;
987*6a54128fSAndroid Build Coastguard Worker 
988*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", (const char *) label,
989*6a54128fSAndroid Build Coastguard Worker 		      figure_label_len(label, 32));
990*6a54128fSAndroid Build Coastguard Worker 	return 0;
991*6a54128fSAndroid Build Coastguard Worker }
992*6a54128fSAndroid Build Coastguard Worker 
993*6a54128fSAndroid Build Coastguard Worker 
994*6a54128fSAndroid Build Coastguard Worker static const char
995*6a54128fSAndroid Build Coastguard Worker *udf_magic[] = { "BEA01", "BOOT2", "CD001", "CDW02", "NSR02",
996*6a54128fSAndroid Build Coastguard Worker 		 "NSR03", "TEA01", 0 };
997*6a54128fSAndroid Build Coastguard Worker 
probe_udf(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf __BLKID_ATTR ((unused)))998*6a54128fSAndroid Build Coastguard Worker static int probe_udf(struct blkid_probe *probe,
999*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
1000*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf __BLKID_ATTR((unused)))
1001*6a54128fSAndroid Build Coastguard Worker {
1002*6a54128fSAndroid Build Coastguard Worker 	int j, bs;
1003*6a54128fSAndroid Build Coastguard Worker 	struct iso_volume_descriptor *isosb;
1004*6a54128fSAndroid Build Coastguard Worker 	const char ** m;
1005*6a54128fSAndroid Build Coastguard Worker 
1006*6a54128fSAndroid Build Coastguard Worker 	/* determine the block size by scanning in 2K increments
1007*6a54128fSAndroid Build Coastguard Worker 	   (block sizes larger than 2K will be null padded) */
1008*6a54128fSAndroid Build Coastguard Worker 	for (bs = 1; bs < 16; bs++) {
1009*6a54128fSAndroid Build Coastguard Worker 		isosb = (struct iso_volume_descriptor *)
1010*6a54128fSAndroid Build Coastguard Worker 			get_buffer(probe, (blkid_loff_t) bs*2048+32768,
1011*6a54128fSAndroid Build Coastguard Worker 				   sizeof(*isosb));
1012*6a54128fSAndroid Build Coastguard Worker 		if (!isosb)
1013*6a54128fSAndroid Build Coastguard Worker 			return 1;
1014*6a54128fSAndroid Build Coastguard Worker 		if (isosb->vd_id[0])
1015*6a54128fSAndroid Build Coastguard Worker 			break;
1016*6a54128fSAndroid Build Coastguard Worker 	}
1017*6a54128fSAndroid Build Coastguard Worker 
1018*6a54128fSAndroid Build Coastguard Worker 	/* Scan up to another 64 blocks looking for additional VSD's */
1019*6a54128fSAndroid Build Coastguard Worker 	for (j = 1; j < 64; j++) {
1020*6a54128fSAndroid Build Coastguard Worker 		if (j > 1) {
1021*6a54128fSAndroid Build Coastguard Worker 			isosb = (struct iso_volume_descriptor *)
1022*6a54128fSAndroid Build Coastguard Worker 				get_buffer(probe, j*bs*2048+32768,
1023*6a54128fSAndroid Build Coastguard Worker 					   sizeof(*isosb));
1024*6a54128fSAndroid Build Coastguard Worker 			if (!isosb)
1025*6a54128fSAndroid Build Coastguard Worker 				return 1;
1026*6a54128fSAndroid Build Coastguard Worker 		}
1027*6a54128fSAndroid Build Coastguard Worker 		/* If we find NSR0x then call it udf:
1028*6a54128fSAndroid Build Coastguard Worker 		   NSR01 for UDF 1.00
1029*6a54128fSAndroid Build Coastguard Worker 		   NSR02 for UDF 1.50
1030*6a54128fSAndroid Build Coastguard Worker 		   NSR03 for UDF 2.00 */
1031*6a54128fSAndroid Build Coastguard Worker 		if (!memcmp(isosb->vd_id, "NSR0", 4))
1032*6a54128fSAndroid Build Coastguard Worker 			return probe_iso9660(probe, id, buf);
1033*6a54128fSAndroid Build Coastguard Worker 		for (m = udf_magic; *m; m++)
1034*6a54128fSAndroid Build Coastguard Worker 			if (!memcmp(*m, isosb->vd_id, 5))
1035*6a54128fSAndroid Build Coastguard Worker 				break;
1036*6a54128fSAndroid Build Coastguard Worker 		if (*m == 0)
1037*6a54128fSAndroid Build Coastguard Worker 			return 1;
1038*6a54128fSAndroid Build Coastguard Worker 	}
1039*6a54128fSAndroid Build Coastguard Worker 	return 1;
1040*6a54128fSAndroid Build Coastguard Worker }
1041*6a54128fSAndroid Build Coastguard Worker 
probe_ocfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1042*6a54128fSAndroid Build Coastguard Worker static int probe_ocfs(struct blkid_probe *probe,
1043*6a54128fSAndroid Build Coastguard Worker 		      struct blkid_magic *id __BLKID_ATTR((unused)),
1044*6a54128fSAndroid Build Coastguard Worker 		      unsigned char *buf)
1045*6a54128fSAndroid Build Coastguard Worker {
1046*6a54128fSAndroid Build Coastguard Worker 	struct ocfs_volume_header ovh;
1047*6a54128fSAndroid Build Coastguard Worker 	struct ocfs_volume_label ovl;
1048*6a54128fSAndroid Build Coastguard Worker 	__u32 major;
1049*6a54128fSAndroid Build Coastguard Worker 
1050*6a54128fSAndroid Build Coastguard Worker 	memcpy(&ovh, buf, sizeof(ovh));
1051*6a54128fSAndroid Build Coastguard Worker 	memcpy(&ovl, buf+512, sizeof(ovl));
1052*6a54128fSAndroid Build Coastguard Worker 
1053*6a54128fSAndroid Build Coastguard Worker 	major = ocfsmajor(ovh);
1054*6a54128fSAndroid Build Coastguard Worker 	if (major == 1)
1055*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev,"SEC_TYPE","ocfs1",sizeof("ocfs1"));
1056*6a54128fSAndroid Build Coastguard Worker 	else if (major >= 9)
1057*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev,"SEC_TYPE","ntocfs",sizeof("ntocfs"));
1058*6a54128fSAndroid Build Coastguard Worker 
1059*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", ovl.label, ocfslabellen(ovl));
1060*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "MOUNT", ovh.mount, ocfsmountlen(ovh));
1061*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, ovl.vol_id, 0);
1062*6a54128fSAndroid Build Coastguard Worker 	return 0;
1063*6a54128fSAndroid Build Coastguard Worker }
1064*6a54128fSAndroid Build Coastguard Worker 
probe_ocfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1065*6a54128fSAndroid Build Coastguard Worker static int probe_ocfs2(struct blkid_probe *probe,
1066*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id __BLKID_ATTR((unused)),
1067*6a54128fSAndroid Build Coastguard Worker 		       unsigned char *buf)
1068*6a54128fSAndroid Build Coastguard Worker {
1069*6a54128fSAndroid Build Coastguard Worker 	struct ocfs2_super_block *osb;
1070*6a54128fSAndroid Build Coastguard Worker 
1071*6a54128fSAndroid Build Coastguard Worker 	osb = (struct ocfs2_super_block *)buf;
1072*6a54128fSAndroid Build Coastguard Worker 
1073*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", osb->s_label, sizeof(osb->s_label));
1074*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, osb->s_uuid, 0);
1075*6a54128fSAndroid Build Coastguard Worker 	return 0;
1076*6a54128fSAndroid Build Coastguard Worker }
1077*6a54128fSAndroid Build Coastguard Worker 
probe_oracleasm(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1078*6a54128fSAndroid Build Coastguard Worker static int probe_oracleasm(struct blkid_probe *probe,
1079*6a54128fSAndroid Build Coastguard Worker 			   struct blkid_magic *id __BLKID_ATTR((unused)),
1080*6a54128fSAndroid Build Coastguard Worker 			   unsigned char *buf)
1081*6a54128fSAndroid Build Coastguard Worker {
1082*6a54128fSAndroid Build Coastguard Worker 	struct oracle_asm_disk_label *dl;
1083*6a54128fSAndroid Build Coastguard Worker 
1084*6a54128fSAndroid Build Coastguard Worker 	dl = (struct oracle_asm_disk_label *)buf;
1085*6a54128fSAndroid Build Coastguard Worker 
1086*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", dl->dl_id, sizeof(dl->dl_id));
1087*6a54128fSAndroid Build Coastguard Worker 	return 0;
1088*6a54128fSAndroid Build Coastguard Worker }
1089*6a54128fSAndroid Build Coastguard Worker 
probe_gfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1090*6a54128fSAndroid Build Coastguard Worker static int probe_gfs(struct blkid_probe *probe,
1091*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
1092*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf)
1093*6a54128fSAndroid Build Coastguard Worker {
1094*6a54128fSAndroid Build Coastguard Worker 	struct gfs2_sb *sbd;
1095*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
1096*6a54128fSAndroid Build Coastguard Worker 
1097*6a54128fSAndroid Build Coastguard Worker 	sbd = (struct gfs2_sb *)buf;
1098*6a54128fSAndroid Build Coastguard Worker 
1099*6a54128fSAndroid Build Coastguard Worker 	if (blkid_be32(sbd->sb_fs_format) == GFS_FORMAT_FS &&
1100*6a54128fSAndroid Build Coastguard Worker 	    blkid_be32(sbd->sb_multihost_format) == GFS_FORMAT_MULTI)
1101*6a54128fSAndroid Build Coastguard Worker 	{
1102*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "UUID", 0, 0);
1103*6a54128fSAndroid Build Coastguard Worker 
1104*6a54128fSAndroid Build Coastguard Worker 		if (strlen(sbd->sb_locktable))
1105*6a54128fSAndroid Build Coastguard Worker 			label = sbd->sb_locktable;
1106*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1107*6a54128fSAndroid Build Coastguard Worker 		return 0;
1108*6a54128fSAndroid Build Coastguard Worker 	}
1109*6a54128fSAndroid Build Coastguard Worker 	return 1;
1110*6a54128fSAndroid Build Coastguard Worker }
1111*6a54128fSAndroid Build Coastguard Worker 
probe_gfs2(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1112*6a54128fSAndroid Build Coastguard Worker static int probe_gfs2(struct blkid_probe *probe,
1113*6a54128fSAndroid Build Coastguard Worker 		     struct blkid_magic *id __BLKID_ATTR((unused)),
1114*6a54128fSAndroid Build Coastguard Worker 		     unsigned char *buf)
1115*6a54128fSAndroid Build Coastguard Worker {
1116*6a54128fSAndroid Build Coastguard Worker 	struct gfs2_sb *sbd;
1117*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
1118*6a54128fSAndroid Build Coastguard Worker 
1119*6a54128fSAndroid Build Coastguard Worker 	sbd = (struct gfs2_sb *)buf;
1120*6a54128fSAndroid Build Coastguard Worker 
1121*6a54128fSAndroid Build Coastguard Worker 	if (blkid_be32(sbd->sb_fs_format) == GFS2_FORMAT_FS &&
1122*6a54128fSAndroid Build Coastguard Worker 	    blkid_be32(sbd->sb_multihost_format) == GFS2_FORMAT_MULTI)
1123*6a54128fSAndroid Build Coastguard Worker 	{
1124*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "UUID", 0, 0);
1125*6a54128fSAndroid Build Coastguard Worker 
1126*6a54128fSAndroid Build Coastguard Worker 		if (strlen(sbd->sb_locktable))
1127*6a54128fSAndroid Build Coastguard Worker 			label = sbd->sb_locktable;
1128*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "LABEL", label, sizeof(sbd->sb_locktable));
1129*6a54128fSAndroid Build Coastguard Worker 		return 0;
1130*6a54128fSAndroid Build Coastguard Worker 	}
1131*6a54128fSAndroid Build Coastguard Worker 	return 1;
1132*6a54128fSAndroid Build Coastguard Worker }
1133*6a54128fSAndroid Build Coastguard Worker 
unicode_16be_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1134*6a54128fSAndroid Build Coastguard Worker static void unicode_16be_to_utf8(unsigned char *str, int out_len,
1135*6a54128fSAndroid Build Coastguard Worker 				 const unsigned char *buf, int in_len)
1136*6a54128fSAndroid Build Coastguard Worker {
1137*6a54128fSAndroid Build Coastguard Worker 	int i, j;
1138*6a54128fSAndroid Build Coastguard Worker 	unsigned int c;
1139*6a54128fSAndroid Build Coastguard Worker 
1140*6a54128fSAndroid Build Coastguard Worker 	for (i = j = 0; i + 2 <= in_len; i += 2) {
1141*6a54128fSAndroid Build Coastguard Worker 		c = (buf[i] << 8) | buf[i+1];
1142*6a54128fSAndroid Build Coastguard Worker 		if (c == 0) {
1143*6a54128fSAndroid Build Coastguard Worker 			str[j] = '\0';
1144*6a54128fSAndroid Build Coastguard Worker 			break;
1145*6a54128fSAndroid Build Coastguard Worker 		} else if (c < 0x80) {
1146*6a54128fSAndroid Build Coastguard Worker 			if (j+1 >= out_len)
1147*6a54128fSAndroid Build Coastguard Worker 				break;
1148*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) c;
1149*6a54128fSAndroid Build Coastguard Worker 		} else if (c < 0x800) {
1150*6a54128fSAndroid Build Coastguard Worker 			if (j+2 >= out_len)
1151*6a54128fSAndroid Build Coastguard Worker 				break;
1152*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0xc0 | (c >> 6));
1153*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1154*6a54128fSAndroid Build Coastguard Worker 		} else {
1155*6a54128fSAndroid Build Coastguard Worker 			if (j+3 >= out_len)
1156*6a54128fSAndroid Build Coastguard Worker 				break;
1157*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0xe0 | (c >> 12));
1158*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1159*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1160*6a54128fSAndroid Build Coastguard Worker 		}
1161*6a54128fSAndroid Build Coastguard Worker 	}
1162*6a54128fSAndroid Build Coastguard Worker 	str[j] = '\0';
1163*6a54128fSAndroid Build Coastguard Worker }
1164*6a54128fSAndroid Build Coastguard Worker 
unicode_16le_to_utf8(unsigned char * str,int out_len,const unsigned char * buf,int in_len)1165*6a54128fSAndroid Build Coastguard Worker static void unicode_16le_to_utf8(unsigned char *str, int out_len,
1166*6a54128fSAndroid Build Coastguard Worker 				 const unsigned char *buf, int in_len)
1167*6a54128fSAndroid Build Coastguard Worker {
1168*6a54128fSAndroid Build Coastguard Worker 	int i, j;
1169*6a54128fSAndroid Build Coastguard Worker 	unsigned int c;
1170*6a54128fSAndroid Build Coastguard Worker 
1171*6a54128fSAndroid Build Coastguard Worker 	for (i = j = 0; i + 2 <= in_len; i += 2) {
1172*6a54128fSAndroid Build Coastguard Worker 		c = (buf[i+1] << 8) | buf[i];
1173*6a54128fSAndroid Build Coastguard Worker 		if (c == 0) {
1174*6a54128fSAndroid Build Coastguard Worker 			str[j] = '\0';
1175*6a54128fSAndroid Build Coastguard Worker 			break;
1176*6a54128fSAndroid Build Coastguard Worker 		} else if (c < 0x80) {
1177*6a54128fSAndroid Build Coastguard Worker 			if (j+1 >= out_len)
1178*6a54128fSAndroid Build Coastguard Worker 				break;
1179*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) c;
1180*6a54128fSAndroid Build Coastguard Worker 		} else if (c < 0x800) {
1181*6a54128fSAndroid Build Coastguard Worker 			if (j+2 >= out_len)
1182*6a54128fSAndroid Build Coastguard Worker 				break;
1183*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0xc0 | (c >> 6));
1184*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1185*6a54128fSAndroid Build Coastguard Worker 		} else {
1186*6a54128fSAndroid Build Coastguard Worker 			if (j+3 >= out_len)
1187*6a54128fSAndroid Build Coastguard Worker 				break;
1188*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0xe0 | (c >> 12));
1189*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | ((c >> 6) & 0x3f));
1190*6a54128fSAndroid Build Coastguard Worker 			str[j++] = (unsigned char) (0x80 | (c & 0x3f));
1191*6a54128fSAndroid Build Coastguard Worker 		}
1192*6a54128fSAndroid Build Coastguard Worker 	}
1193*6a54128fSAndroid Build Coastguard Worker 	str[j] = '\0';
1194*6a54128fSAndroid Build Coastguard Worker }
1195*6a54128fSAndroid Build Coastguard Worker 
probe_hfs(struct blkid_probe * probe __BLKID_ATTR ((unused)),struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1196*6a54128fSAndroid Build Coastguard Worker static int probe_hfs(struct blkid_probe *probe __BLKID_ATTR((unused)),
1197*6a54128fSAndroid Build Coastguard Worker 			 struct blkid_magic *id __BLKID_ATTR((unused)),
1198*6a54128fSAndroid Build Coastguard Worker 			 unsigned char *buf)
1199*6a54128fSAndroid Build Coastguard Worker {
1200*6a54128fSAndroid Build Coastguard Worker 	struct hfs_mdb *hfs = (struct hfs_mdb *)buf;
1201*6a54128fSAndroid Build Coastguard Worker 	char	uuid_str[17];
1202*6a54128fSAndroid Build Coastguard Worker 	__u64	uuid;
1203*6a54128fSAndroid Build Coastguard Worker 
1204*6a54128fSAndroid Build Coastguard Worker 	if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
1205*6a54128fSAndroid Build Coastguard Worker 	    (memcmp(hfs->embed_sig, "HX", 2) == 0))
1206*6a54128fSAndroid Build Coastguard Worker 		return 1;	/* Not hfs, but an embedded HFS+ */
1207*6a54128fSAndroid Build Coastguard Worker 
1208*6a54128fSAndroid Build Coastguard Worker 	memcpy(&uuid, hfs->finder_info.id, 8);
1209*6a54128fSAndroid Build Coastguard Worker 	uuid = blkid_le64(uuid);
1210*6a54128fSAndroid Build Coastguard Worker 	if (uuid) {
1211*6a54128fSAndroid Build Coastguard Worker 		sprintf(uuid_str, "%016llX", uuid);
1212*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1213*6a54128fSAndroid Build Coastguard Worker 	}
1214*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", (char *)hfs->label, hfs->label_len);
1215*6a54128fSAndroid Build Coastguard Worker 	return 0;
1216*6a54128fSAndroid Build Coastguard Worker }
1217*6a54128fSAndroid Build Coastguard Worker 
1218*6a54128fSAndroid Build Coastguard Worker 
1219*6a54128fSAndroid Build Coastguard Worker #define HFSPLUS_SECTOR_SIZE        512
1220*6a54128fSAndroid Build Coastguard Worker 
probe_hfsplus(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1221*6a54128fSAndroid Build Coastguard Worker static int probe_hfsplus(struct blkid_probe *probe,
1222*6a54128fSAndroid Build Coastguard Worker 			 struct blkid_magic *id,
1223*6a54128fSAndroid Build Coastguard Worker 			 unsigned char *buf)
1224*6a54128fSAndroid Build Coastguard Worker {
1225*6a54128fSAndroid Build Coastguard Worker 	struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
1226*6a54128fSAndroid Build Coastguard Worker 	struct hfsplus_bnode_descriptor *descr;
1227*6a54128fSAndroid Build Coastguard Worker 	struct hfsplus_bheader_record *bnode;
1228*6a54128fSAndroid Build Coastguard Worker 	struct hfsplus_catalog_key *key;
1229*6a54128fSAndroid Build Coastguard Worker 	struct hfsplus_vol_header *hfsplus;
1230*6a54128fSAndroid Build Coastguard Worker 	struct hfs_mdb *sbd = (struct hfs_mdb *) buf;
1231*6a54128fSAndroid Build Coastguard Worker 	unsigned int alloc_block_size;
1232*6a54128fSAndroid Build Coastguard Worker 	unsigned int alloc_first_block;
1233*6a54128fSAndroid Build Coastguard Worker 	unsigned int embed_first_block;
1234*6a54128fSAndroid Build Coastguard Worker 	unsigned int off = 0;
1235*6a54128fSAndroid Build Coastguard Worker 	unsigned int blocksize;
1236*6a54128fSAndroid Build Coastguard Worker 	unsigned int cat_block;
1237*6a54128fSAndroid Build Coastguard Worker 	unsigned int ext_block_start;
1238*6a54128fSAndroid Build Coastguard Worker 	unsigned int ext_block_count;
1239*6a54128fSAndroid Build Coastguard Worker 	unsigned int record_count;
1240*6a54128fSAndroid Build Coastguard Worker 	unsigned int leaf_node_head;
1241*6a54128fSAndroid Build Coastguard Worker 	unsigned int leaf_node_count;
1242*6a54128fSAndroid Build Coastguard Worker 	unsigned int leaf_node_size;
1243*6a54128fSAndroid Build Coastguard Worker 	unsigned int leaf_block;
1244*6a54128fSAndroid Build Coastguard Worker 	unsigned int label_len;
1245*6a54128fSAndroid Build Coastguard Worker 	__u64 leaf_off, uuid;
1246*6a54128fSAndroid Build Coastguard Worker 	char	uuid_str[17], label[512];
1247*6a54128fSAndroid Build Coastguard Worker 	int ext;
1248*6a54128fSAndroid Build Coastguard Worker 
1249*6a54128fSAndroid Build Coastguard Worker 	/* Check for a HFS+ volume embedded in a HFS volume */
1250*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(sbd->signature, "BD", 2) == 0) {
1251*6a54128fSAndroid Build Coastguard Worker 		if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
1252*6a54128fSAndroid Build Coastguard Worker 		    (memcmp(sbd->embed_sig, "HX", 2) != 0))
1253*6a54128fSAndroid Build Coastguard Worker 			/* This must be an HFS volume, so fail */
1254*6a54128fSAndroid Build Coastguard Worker 			return 1;
1255*6a54128fSAndroid Build Coastguard Worker 
1256*6a54128fSAndroid Build Coastguard Worker 		alloc_block_size = blkid_be32(sbd->al_blk_size);
1257*6a54128fSAndroid Build Coastguard Worker 		alloc_first_block = blkid_be16(sbd->al_bl_st);
1258*6a54128fSAndroid Build Coastguard Worker 		embed_first_block = blkid_be16(sbd->embed_startblock);
1259*6a54128fSAndroid Build Coastguard Worker 		off = (alloc_first_block * 512) +
1260*6a54128fSAndroid Build Coastguard Worker 			(embed_first_block * alloc_block_size);
1261*6a54128fSAndroid Build Coastguard Worker 		buf = get_buffer(probe, off + (id->bim_kboff * 1024),
1262*6a54128fSAndroid Build Coastguard Worker 				 sizeof(*sbd));
1263*6a54128fSAndroid Build Coastguard Worker 		if (!buf)
1264*6a54128fSAndroid Build Coastguard Worker 			return 1;
1265*6a54128fSAndroid Build Coastguard Worker 
1266*6a54128fSAndroid Build Coastguard Worker 		hfsplus = (struct hfsplus_vol_header *) buf;
1267*6a54128fSAndroid Build Coastguard Worker 	}
1268*6a54128fSAndroid Build Coastguard Worker 
1269*6a54128fSAndroid Build Coastguard Worker 	hfsplus = (struct hfsplus_vol_header *) buf;
1270*6a54128fSAndroid Build Coastguard Worker 
1271*6a54128fSAndroid Build Coastguard Worker 	if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
1272*6a54128fSAndroid Build Coastguard Worker 	    (memcmp(hfsplus->signature, "HX", 2) != 0))
1273*6a54128fSAndroid Build Coastguard Worker 		return 1;
1274*6a54128fSAndroid Build Coastguard Worker 
1275*6a54128fSAndroid Build Coastguard Worker 	memcpy(&uuid, hfsplus->finder_info.id, 8);
1276*6a54128fSAndroid Build Coastguard Worker 	uuid = blkid_le64(uuid);
1277*6a54128fSAndroid Build Coastguard Worker 	if (uuid) {
1278*6a54128fSAndroid Build Coastguard Worker 		sprintf(uuid_str, "%016llX", uuid);
1279*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(probe->dev, "UUID", uuid_str, 0);
1280*6a54128fSAndroid Build Coastguard Worker 	}
1281*6a54128fSAndroid Build Coastguard Worker 
1282*6a54128fSAndroid Build Coastguard Worker 	blocksize = blkid_be32(hfsplus->blocksize);
1283*6a54128fSAndroid Build Coastguard Worker 	if (blocksize < HFSPLUS_SECTOR_SIZE)
1284*6a54128fSAndroid Build Coastguard Worker 		return 1;
1285*6a54128fSAndroid Build Coastguard Worker 
1286*6a54128fSAndroid Build Coastguard Worker 	memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
1287*6a54128fSAndroid Build Coastguard Worker 	cat_block = blkid_be32(extents[0].start_block);
1288*6a54128fSAndroid Build Coastguard Worker 
1289*6a54128fSAndroid Build Coastguard Worker 	buf = get_buffer(probe, off + ((__u64) cat_block * blocksize), 0x2000);
1290*6a54128fSAndroid Build Coastguard Worker 	if (!buf)
1291*6a54128fSAndroid Build Coastguard Worker 		return 0;
1292*6a54128fSAndroid Build Coastguard Worker 
1293*6a54128fSAndroid Build Coastguard Worker 	bnode = (struct hfsplus_bheader_record *)
1294*6a54128fSAndroid Build Coastguard Worker 		&buf[sizeof(struct hfsplus_bnode_descriptor)];
1295*6a54128fSAndroid Build Coastguard Worker 
1296*6a54128fSAndroid Build Coastguard Worker 	leaf_node_head = blkid_be32(bnode->leaf_head);
1297*6a54128fSAndroid Build Coastguard Worker 	leaf_node_size = blkid_be16(bnode->node_size);
1298*6a54128fSAndroid Build Coastguard Worker 	leaf_node_count = blkid_be32(bnode->leaf_count);
1299*6a54128fSAndroid Build Coastguard Worker 	if (leaf_node_count == 0)
1300*6a54128fSAndroid Build Coastguard Worker 		return 0;
1301*6a54128fSAndroid Build Coastguard Worker 
1302*6a54128fSAndroid Build Coastguard Worker 	leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
1303*6a54128fSAndroid Build Coastguard Worker 
1304*6a54128fSAndroid Build Coastguard Worker 	/* get physical location */
1305*6a54128fSAndroid Build Coastguard Worker 	for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
1306*6a54128fSAndroid Build Coastguard Worker 		ext_block_start = blkid_be32(extents[ext].start_block);
1307*6a54128fSAndroid Build Coastguard Worker 		ext_block_count = blkid_be32(extents[ext].block_count);
1308*6a54128fSAndroid Build Coastguard Worker 		if (ext_block_count == 0)
1309*6a54128fSAndroid Build Coastguard Worker 			return 0;
1310*6a54128fSAndroid Build Coastguard Worker 
1311*6a54128fSAndroid Build Coastguard Worker 		/* this is our extent */
1312*6a54128fSAndroid Build Coastguard Worker 		if (leaf_block < ext_block_count)
1313*6a54128fSAndroid Build Coastguard Worker 			break;
1314*6a54128fSAndroid Build Coastguard Worker 
1315*6a54128fSAndroid Build Coastguard Worker 		leaf_block -= ext_block_count;
1316*6a54128fSAndroid Build Coastguard Worker 	}
1317*6a54128fSAndroid Build Coastguard Worker 	if (ext == HFSPLUS_EXTENT_COUNT)
1318*6a54128fSAndroid Build Coastguard Worker 		return 0;
1319*6a54128fSAndroid Build Coastguard Worker 
1320*6a54128fSAndroid Build Coastguard Worker 	leaf_off = (__u64) (ext_block_start + leaf_block) * blocksize;
1321*6a54128fSAndroid Build Coastguard Worker 
1322*6a54128fSAndroid Build Coastguard Worker 	buf = get_buffer(probe, off + leaf_off, leaf_node_size);
1323*6a54128fSAndroid Build Coastguard Worker 	if (!buf)
1324*6a54128fSAndroid Build Coastguard Worker 		return 0;
1325*6a54128fSAndroid Build Coastguard Worker 
1326*6a54128fSAndroid Build Coastguard Worker 	descr = (struct hfsplus_bnode_descriptor *) buf;
1327*6a54128fSAndroid Build Coastguard Worker 	record_count = blkid_be16(descr->num_recs);
1328*6a54128fSAndroid Build Coastguard Worker 	if (record_count == 0)
1329*6a54128fSAndroid Build Coastguard Worker 		return 0;
1330*6a54128fSAndroid Build Coastguard Worker 
1331*6a54128fSAndroid Build Coastguard Worker 	if (descr->type != HFS_NODE_LEAF)
1332*6a54128fSAndroid Build Coastguard Worker 		return 0;
1333*6a54128fSAndroid Build Coastguard Worker 
1334*6a54128fSAndroid Build Coastguard Worker 	key = (struct hfsplus_catalog_key *)
1335*6a54128fSAndroid Build Coastguard Worker 		&buf[sizeof(struct hfsplus_bnode_descriptor)];
1336*6a54128fSAndroid Build Coastguard Worker 
1337*6a54128fSAndroid Build Coastguard Worker 	if (blkid_be32(key->parent_id) != HFSPLUS_POR_CNID)
1338*6a54128fSAndroid Build Coastguard Worker 		return 0;
1339*6a54128fSAndroid Build Coastguard Worker 
1340*6a54128fSAndroid Build Coastguard Worker 	label_len = blkid_be16(key->unicode_len) * 2;
1341*6a54128fSAndroid Build Coastguard Worker 	unicode_16be_to_utf8((unsigned char *)label, sizeof(label),
1342*6a54128fSAndroid Build Coastguard Worker 			     key->unicode, label_len);
1343*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, 0);
1344*6a54128fSAndroid Build Coastguard Worker 	return 0;
1345*6a54128fSAndroid Build Coastguard Worker }
1346*6a54128fSAndroid Build Coastguard Worker 
1347*6a54128fSAndroid Build Coastguard Worker #define LVM2_LABEL_SIZE 512
lvm2_calc_crc(const void * buf,unsigned int size)1348*6a54128fSAndroid Build Coastguard Worker static unsigned int lvm2_calc_crc(const void *buf, unsigned int size)
1349*6a54128fSAndroid Build Coastguard Worker {
1350*6a54128fSAndroid Build Coastguard Worker 	static const unsigned int crctab[] = {
1351*6a54128fSAndroid Build Coastguard Worker 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1352*6a54128fSAndroid Build Coastguard Worker 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1353*6a54128fSAndroid Build Coastguard Worker 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1354*6a54128fSAndroid Build Coastguard Worker 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1355*6a54128fSAndroid Build Coastguard Worker 	};
1356*6a54128fSAndroid Build Coastguard Worker 	unsigned int i, crc = 0xf597a6cf;
1357*6a54128fSAndroid Build Coastguard Worker 	const __u8 *data = (const __u8 *) buf;
1358*6a54128fSAndroid Build Coastguard Worker 
1359*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < size; i++) {
1360*6a54128fSAndroid Build Coastguard Worker 		crc ^= *data++;
1361*6a54128fSAndroid Build Coastguard Worker 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1362*6a54128fSAndroid Build Coastguard Worker 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1363*6a54128fSAndroid Build Coastguard Worker 	}
1364*6a54128fSAndroid Build Coastguard Worker 	return crc;
1365*6a54128fSAndroid Build Coastguard Worker }
1366*6a54128fSAndroid Build Coastguard Worker 
probe_lvm2(struct blkid_probe * probe,struct blkid_magic * id,unsigned char * buf)1367*6a54128fSAndroid Build Coastguard Worker static int probe_lvm2(struct blkid_probe *probe,
1368*6a54128fSAndroid Build Coastguard Worker 			struct blkid_magic *id,
1369*6a54128fSAndroid Build Coastguard Worker 			unsigned char *buf)
1370*6a54128fSAndroid Build Coastguard Worker {
1371*6a54128fSAndroid Build Coastguard Worker 	int sector = (id->bim_kboff) << 1;
1372*6a54128fSAndroid Build Coastguard Worker 	struct lvm2_pv_label_header *label= (struct lvm2_pv_label_header *)buf;
1373*6a54128fSAndroid Build Coastguard Worker 	char *p, *q, uuid[40];
1374*6a54128fSAndroid Build Coastguard Worker 	unsigned int i, b;
1375*6a54128fSAndroid Build Coastguard Worker 
1376*6a54128fSAndroid Build Coastguard Worker 	/* buf is at 0k or 1k offset; find label inside */
1377*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(buf, "LABELONE", 8) == 0) {
1378*6a54128fSAndroid Build Coastguard Worker 		label = (struct lvm2_pv_label_header *)buf;
1379*6a54128fSAndroid Build Coastguard Worker 	} else if (memcmp(buf + 512, "LABELONE", 8) == 0) {
1380*6a54128fSAndroid Build Coastguard Worker 		label = (struct lvm2_pv_label_header *)(buf + 512);
1381*6a54128fSAndroid Build Coastguard Worker 		sector++;
1382*6a54128fSAndroid Build Coastguard Worker 	} else {
1383*6a54128fSAndroid Build Coastguard Worker 		return 1;
1384*6a54128fSAndroid Build Coastguard Worker 	}
1385*6a54128fSAndroid Build Coastguard Worker 
1386*6a54128fSAndroid Build Coastguard Worker 	if (blkid_le64(label->sector_xl) != (unsigned) sector) {
1387*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE,
1388*6a54128fSAndroid Build Coastguard Worker 		    printf("LVM2: label for sector %llu found at sector %d\n",
1389*6a54128fSAndroid Build Coastguard Worker 			   blkid_le64(label->sector_xl), sector));
1390*6a54128fSAndroid Build Coastguard Worker 		return 1;
1391*6a54128fSAndroid Build Coastguard Worker 	}
1392*6a54128fSAndroid Build Coastguard Worker 
1393*6a54128fSAndroid Build Coastguard Worker 	if (lvm2_calc_crc(&label->offset_xl, LVM2_LABEL_SIZE -
1394*6a54128fSAndroid Build Coastguard Worker 			  ((char *)&label->offset_xl - (char *)label)) !=
1395*6a54128fSAndroid Build Coastguard Worker 			blkid_le32(label->crc_xl)) {
1396*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE,
1397*6a54128fSAndroid Build Coastguard Worker 		    printf("LVM2: label checksum incorrect at sector %d\n",
1398*6a54128fSAndroid Build Coastguard Worker 			   sector));
1399*6a54128fSAndroid Build Coastguard Worker 		return 1;
1400*6a54128fSAndroid Build Coastguard Worker 	}
1401*6a54128fSAndroid Build Coastguard Worker 
1402*6a54128fSAndroid Build Coastguard Worker 	for (i=0, b=1, p=uuid, q= (char *) label->pv_uuid; i < LVM2_ID_LEN;
1403*6a54128fSAndroid Build Coastguard Worker 	     i++, b <<= 1) {
1404*6a54128fSAndroid Build Coastguard Worker 		if (b & 0x4444440)
1405*6a54128fSAndroid Build Coastguard Worker 			*p++ = '-';
1406*6a54128fSAndroid Build Coastguard Worker 		*p++ = *q++;
1407*6a54128fSAndroid Build Coastguard Worker 	}
1408*6a54128fSAndroid Build Coastguard Worker 
1409*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "UUID", uuid, LVM2_ID_LEN+6);
1410*6a54128fSAndroid Build Coastguard Worker 
1411*6a54128fSAndroid Build Coastguard Worker 	return 0;
1412*6a54128fSAndroid Build Coastguard Worker }
1413*6a54128fSAndroid Build Coastguard Worker 
probe_btrfs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1414*6a54128fSAndroid Build Coastguard Worker static int probe_btrfs(struct blkid_probe *probe,
1415*6a54128fSAndroid Build Coastguard Worker 			struct blkid_magic *id __BLKID_ATTR((unused)),
1416*6a54128fSAndroid Build Coastguard Worker 			unsigned char *buf)
1417*6a54128fSAndroid Build Coastguard Worker {
1418*6a54128fSAndroid Build Coastguard Worker 	struct btrfs_super_block *bs;
1419*6a54128fSAndroid Build Coastguard Worker 	const char *label = 0;
1420*6a54128fSAndroid Build Coastguard Worker 
1421*6a54128fSAndroid Build Coastguard Worker 	bs = (struct btrfs_super_block *)buf;
1422*6a54128fSAndroid Build Coastguard Worker 
1423*6a54128fSAndroid Build Coastguard Worker 	if (strlen(bs->label))
1424*6a54128fSAndroid Build Coastguard Worker 		label = bs->label;
1425*6a54128fSAndroid Build Coastguard Worker 	blkid_set_tag(probe->dev, "LABEL", label, sizeof(bs->label));
1426*6a54128fSAndroid Build Coastguard Worker 	set_uuid(probe->dev, bs->fsid, 0);
1427*6a54128fSAndroid Build Coastguard Worker 	return 0;
1428*6a54128fSAndroid Build Coastguard Worker }
1429*6a54128fSAndroid Build Coastguard Worker 
probe_f2fs(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1430*6a54128fSAndroid Build Coastguard Worker static int probe_f2fs(struct blkid_probe *probe,
1431*6a54128fSAndroid Build Coastguard Worker             struct blkid_magic *id __BLKID_ATTR((unused)),
1432*6a54128fSAndroid Build Coastguard Worker             unsigned char *buf)
1433*6a54128fSAndroid Build Coastguard Worker {
1434*6a54128fSAndroid Build Coastguard Worker     struct f2fs_super_block *bs;
1435*6a54128fSAndroid Build Coastguard Worker 
1436*6a54128fSAndroid Build Coastguard Worker     bs = (struct f2fs_super_block *)buf;
1437*6a54128fSAndroid Build Coastguard Worker     set_uuid(probe->dev, bs->uuid, 0);
1438*6a54128fSAndroid Build Coastguard Worker     return 0;
1439*6a54128fSAndroid Build Coastguard Worker }
1440*6a54128fSAndroid Build Coastguard Worker 
exfat_block_to_offset(const struct exfat_super_block * sb,uint64_t block)1441*6a54128fSAndroid Build Coastguard Worker static uint64_t exfat_block_to_offset(const struct exfat_super_block *sb,
1442*6a54128fSAndroid Build Coastguard Worker                                       uint64_t block)
1443*6a54128fSAndroid Build Coastguard Worker {
1444*6a54128fSAndroid Build Coastguard Worker     return block << sb->block_bits;
1445*6a54128fSAndroid Build Coastguard Worker }
1446*6a54128fSAndroid Build Coastguard Worker 
exfat_cluster_to_block(const struct exfat_super_block * sb,uint32_t cluster)1447*6a54128fSAndroid Build Coastguard Worker static uint64_t exfat_cluster_to_block(const struct exfat_super_block *sb,
1448*6a54128fSAndroid Build Coastguard Worker                                        uint32_t cluster)
1449*6a54128fSAndroid Build Coastguard Worker {
1450*6a54128fSAndroid Build Coastguard Worker     return sb->cluster_block_start +
1451*6a54128fSAndroid Build Coastguard Worker             ((uint64_t)(cluster - EXFAT_FIRST_DATA_CLUSTER) << sb->bpc_bits);
1452*6a54128fSAndroid Build Coastguard Worker }
1453*6a54128fSAndroid Build Coastguard Worker 
exfat_cluster_to_offset(const struct exfat_super_block * sb,uint32_t cluster)1454*6a54128fSAndroid Build Coastguard Worker static uint64_t exfat_cluster_to_offset(const struct exfat_super_block *sb,
1455*6a54128fSAndroid Build Coastguard Worker                                         uint32_t cluster)
1456*6a54128fSAndroid Build Coastguard Worker {
1457*6a54128fSAndroid Build Coastguard Worker     return exfat_block_to_offset(sb, exfat_cluster_to_block(sb, cluster));
1458*6a54128fSAndroid Build Coastguard Worker }
1459*6a54128fSAndroid Build Coastguard Worker 
exfat_next_cluster(struct blkid_probe * probe,const struct exfat_super_block * sb,uint32_t cluster)1460*6a54128fSAndroid Build Coastguard Worker static uint32_t exfat_next_cluster(struct blkid_probe *probe,
1461*6a54128fSAndroid Build Coastguard Worker                                    const struct exfat_super_block *sb,
1462*6a54128fSAndroid Build Coastguard Worker                                    uint32_t cluster)
1463*6a54128fSAndroid Build Coastguard Worker {
1464*6a54128fSAndroid Build Coastguard Worker     uint32_t *next;
1465*6a54128fSAndroid Build Coastguard Worker     uint64_t offset;
1466*6a54128fSAndroid Build Coastguard Worker 
1467*6a54128fSAndroid Build Coastguard Worker     offset = exfat_block_to_offset(sb, sb->fat_block_start)
1468*6a54128fSAndroid Build Coastguard Worker             + (uint64_t) cluster * sizeof (cluster);
1469*6a54128fSAndroid Build Coastguard Worker     next = (uint32_t *)get_buffer(probe, offset, sizeof (uint32_t));
1470*6a54128fSAndroid Build Coastguard Worker 
1471*6a54128fSAndroid Build Coastguard Worker     return next ? *next : 0;
1472*6a54128fSAndroid Build Coastguard Worker }
1473*6a54128fSAndroid Build Coastguard Worker 
find_exfat_entry_label(struct blkid_probe * probe,const struct exfat_super_block * sb)1474*6a54128fSAndroid Build Coastguard Worker static struct exfat_entry_label *find_exfat_entry_label(
1475*6a54128fSAndroid Build Coastguard Worker     struct blkid_probe *probe, const struct exfat_super_block *sb)
1476*6a54128fSAndroid Build Coastguard Worker {
1477*6a54128fSAndroid Build Coastguard Worker     uint32_t cluster = sb->rootdir_cluster;
1478*6a54128fSAndroid Build Coastguard Worker     uint64_t offset = exfat_cluster_to_offset(sb, cluster);
1479*6a54128fSAndroid Build Coastguard Worker     uint8_t *entry;
1480*6a54128fSAndroid Build Coastguard Worker     const size_t max_iter = 10000;
1481*6a54128fSAndroid Build Coastguard Worker     size_t i = 0;
1482*6a54128fSAndroid Build Coastguard Worker 
1483*6a54128fSAndroid Build Coastguard Worker     for (; i < max_iter; ++i) {
1484*6a54128fSAndroid Build Coastguard Worker         entry = (uint8_t *)get_buffer(probe, offset, EXFAT_ENTRY_SIZE);
1485*6a54128fSAndroid Build Coastguard Worker         if (!entry)
1486*6a54128fSAndroid Build Coastguard Worker             return NULL;
1487*6a54128fSAndroid Build Coastguard Worker         if (entry[0] == EXFAT_ENTRY_EOD)
1488*6a54128fSAndroid Build Coastguard Worker             return NULL;
1489*6a54128fSAndroid Build Coastguard Worker         if (entry[0] == EXFAT_ENTRY_LABEL)
1490*6a54128fSAndroid Build Coastguard Worker             return (struct exfat_entry_label*) entry;
1491*6a54128fSAndroid Build Coastguard Worker 
1492*6a54128fSAndroid Build Coastguard Worker         offset += EXFAT_ENTRY_SIZE;
1493*6a54128fSAndroid Build Coastguard Worker         if (offset % CLUSTER_SIZE(sb) == 0) {
1494*6a54128fSAndroid Build Coastguard Worker             cluster = exfat_next_cluster(probe, sb, cluster);
1495*6a54128fSAndroid Build Coastguard Worker             if (cluster < EXFAT_FIRST_DATA_CLUSTER)
1496*6a54128fSAndroid Build Coastguard Worker                 return NULL;
1497*6a54128fSAndroid Build Coastguard Worker             if (cluster > EXFAT_LAST_DATA_CLUSTER)
1498*6a54128fSAndroid Build Coastguard Worker                 return NULL;
1499*6a54128fSAndroid Build Coastguard Worker             offset = exfat_cluster_to_offset(sb, cluster);
1500*6a54128fSAndroid Build Coastguard Worker         }
1501*6a54128fSAndroid Build Coastguard Worker     }
1502*6a54128fSAndroid Build Coastguard Worker 
1503*6a54128fSAndroid Build Coastguard Worker     return NULL;
1504*6a54128fSAndroid Build Coastguard Worker }
1505*6a54128fSAndroid Build Coastguard Worker 
probe_exfat(struct blkid_probe * probe,struct blkid_magic * id __BLKID_ATTR ((unused)),unsigned char * buf)1506*6a54128fSAndroid Build Coastguard Worker static int probe_exfat(struct blkid_probe *probe,
1507*6a54128fSAndroid Build Coastguard Worker 		       struct blkid_magic *id __BLKID_ATTR((unused)),
1508*6a54128fSAndroid Build Coastguard Worker                        unsigned char *buf)
1509*6a54128fSAndroid Build Coastguard Worker {
1510*6a54128fSAndroid Build Coastguard Worker     struct exfat_super_block *sb;
1511*6a54128fSAndroid Build Coastguard Worker     struct exfat_entry_label *label;
1512*6a54128fSAndroid Build Coastguard Worker     char uuid[40];
1513*6a54128fSAndroid Build Coastguard Worker 
1514*6a54128fSAndroid Build Coastguard Worker     sb = (struct exfat_super_block *)buf;
1515*6a54128fSAndroid Build Coastguard Worker     if (!sb || CLUSTER_SIZE(sb) == 0) {
1516*6a54128fSAndroid Build Coastguard Worker         DBG(DEBUG_PROBE, printf("bad exfat superblock.\n"));
1517*6a54128fSAndroid Build Coastguard Worker         return errno ? - errno : 1;
1518*6a54128fSAndroid Build Coastguard Worker     }
1519*6a54128fSAndroid Build Coastguard Worker 
1520*6a54128fSAndroid Build Coastguard Worker     label = find_exfat_entry_label(probe, sb);
1521*6a54128fSAndroid Build Coastguard Worker     if (label) {
1522*6a54128fSAndroid Build Coastguard Worker         unsigned char utf8_label[128];
1523*6a54128fSAndroid Build Coastguard Worker         unicode_16le_to_utf8(utf8_label, sizeof(utf8_label), label->name, label->length * 2);
1524*6a54128fSAndroid Build Coastguard Worker         blkid_set_tag(probe->dev, "LABEL", (char *) utf8_label, 0);
1525*6a54128fSAndroid Build Coastguard Worker     } else {
1526*6a54128fSAndroid Build Coastguard Worker         blkid_set_tag(probe->dev, "LABEL", "disk", 4);
1527*6a54128fSAndroid Build Coastguard Worker     }
1528*6a54128fSAndroid Build Coastguard Worker 
1529*6a54128fSAndroid Build Coastguard Worker     memset(uuid, 0, sizeof (uuid));
1530*6a54128fSAndroid Build Coastguard Worker     snprintf(uuid, sizeof (uuid), "%02hhX%02hhX-%02hhX%02hhX",
1531*6a54128fSAndroid Build Coastguard Worker              sb->volume_serial[3], sb->volume_serial[2],
1532*6a54128fSAndroid Build Coastguard Worker              sb->volume_serial[1], sb->volume_serial[0]);
1533*6a54128fSAndroid Build Coastguard Worker     blkid_set_tag(probe->dev, "UUID", uuid, strlen(uuid));
1534*6a54128fSAndroid Build Coastguard Worker 
1535*6a54128fSAndroid Build Coastguard Worker     return 0;
1536*6a54128fSAndroid Build Coastguard Worker }
1537*6a54128fSAndroid Build Coastguard Worker 
1538*6a54128fSAndroid Build Coastguard Worker /*
1539*6a54128fSAndroid Build Coastguard Worker  * Various filesystem magics that we can check for.  Note that kboff and
1540*6a54128fSAndroid Build Coastguard Worker  * sboff are in kilobytes and bytes respectively.  All magics are in
1541*6a54128fSAndroid Build Coastguard Worker  * byte strings so we don't worry about endian issues.
1542*6a54128fSAndroid Build Coastguard Worker  */
1543*6a54128fSAndroid Build Coastguard Worker static struct blkid_magic type_array[] = {
1544*6a54128fSAndroid Build Coastguard Worker /*  type     kboff   sboff len  magic			probe */
1545*6a54128fSAndroid Build Coastguard Worker   { "oracleasm", 0,	32,  8, "ORCLDISK",		probe_oracleasm },
1546*6a54128fSAndroid Build Coastguard Worker   { "ntfs",	 0,	 3,  8, "NTFS    ",		probe_ntfs },
1547*6a54128fSAndroid Build Coastguard Worker   { "jbd",	 1,   0x38,  2, "\123\357",		probe_jbd },
1548*6a54128fSAndroid Build Coastguard Worker   { "ext4dev",	 1,   0x38,  2, "\123\357",		probe_ext4dev },
1549*6a54128fSAndroid Build Coastguard Worker   { "ext4",	 1,   0x38,  2, "\123\357",		probe_ext4 },
1550*6a54128fSAndroid Build Coastguard Worker   { "ext3",	 1,   0x38,  2, "\123\357",		probe_ext3 },
1551*6a54128fSAndroid Build Coastguard Worker   { "ext2",	 1,   0x38,  2, "\123\357",		probe_ext2 },
1552*6a54128fSAndroid Build Coastguard Worker   { "reiserfs",	 8,   0x34,  8, "ReIsErFs",		probe_reiserfs },
1553*6a54128fSAndroid Build Coastguard Worker   { "reiserfs", 64,   0x34,  9, "ReIsEr2Fs",		probe_reiserfs },
1554*6a54128fSAndroid Build Coastguard Worker   { "reiserfs", 64,   0x34,  9, "ReIsEr3Fs",		probe_reiserfs },
1555*6a54128fSAndroid Build Coastguard Worker   { "reiserfs", 64,   0x34,  8, "ReIsErFs",		probe_reiserfs },
1556*6a54128fSAndroid Build Coastguard Worker   { "reiserfs",	 8,	20,  8, "ReIsErFs",		probe_reiserfs },
1557*6a54128fSAndroid Build Coastguard Worker   { "reiser4",  64,	 0,  7, "ReIsEr4",		probe_reiserfs4 },
1558*6a54128fSAndroid Build Coastguard Worker   { "gfs2",     64,      0,  4, "\x01\x16\x19\x70",     probe_gfs2 },
1559*6a54128fSAndroid Build Coastguard Worker   { "gfs",      64,      0,  4, "\x01\x16\x19\x70",     probe_gfs },
1560*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,   0x52,  5, "MSWIN",                probe_fat },
1561*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,   0x52,  8, "FAT32   ",             probe_fat },
1562*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,   0x36,  5, "MSDOS",                probe_fat },
1563*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,   0x36,  8, "FAT16   ",             probe_fat },
1564*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,   0x36,  8, "FAT12   ",             probe_fat },
1565*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,      0,  1, "\353",                 probe_fat_nomagic },
1566*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,      0,  1, "\351",                 probe_fat_nomagic },
1567*6a54128fSAndroid Build Coastguard Worker   { "vfat",      0,  0x1fe,  2, "\125\252",             probe_fat_nomagic },
1568*6a54128fSAndroid Build Coastguard Worker   { "minix",     1,   0x10,  2, "\177\023",             0 },
1569*6a54128fSAndroid Build Coastguard Worker   { "minix",     1,   0x10,  2, "\217\023",             0 },
1570*6a54128fSAndroid Build Coastguard Worker   { "minix",	 1,   0x10,  2, "\150\044",		0 },
1571*6a54128fSAndroid Build Coastguard Worker   { "minix",	 1,   0x10,  2, "\170\044",		0 },
1572*6a54128fSAndroid Build Coastguard Worker   { "vxfs",	 1,	 0,  4, "\365\374\001\245",	0 },
1573*6a54128fSAndroid Build Coastguard Worker   { "xfs",	 0,	 0,  4, "XFSB",			probe_xfs },
1574*6a54128fSAndroid Build Coastguard Worker   { "romfs",	 0,	 0,  8, "-rom1fs-",		probe_romfs },
1575*6a54128fSAndroid Build Coastguard Worker   { "bfs",	 0,	 0,  4, "\316\372\173\033",	0 },
1576*6a54128fSAndroid Build Coastguard Worker   { "cramfs",	 0,	 0,  4, "E=\315\050",		probe_cramfs },
1577*6a54128fSAndroid Build Coastguard Worker   { "qnx4",	 0,	 4,  6, "QNX4FS",		0 },
1578*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "BEA01",		probe_udf },
1579*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "BOOT2",		probe_udf },
1580*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "CD001",		probe_udf },
1581*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "CDW02",		probe_udf },
1582*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "NSR02",		probe_udf },
1583*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "NSR03",		probe_udf },
1584*6a54128fSAndroid Build Coastguard Worker   { "udf",	32,	 1,  5, "TEA01",		probe_udf },
1585*6a54128fSAndroid Build Coastguard Worker   { "iso9660",	32,	 1,  5, "CD001",		probe_iso9660 },
1586*6a54128fSAndroid Build Coastguard Worker   { "iso9660",	32,	 9,  5, "CDROM",		probe_iso9660 },
1587*6a54128fSAndroid Build Coastguard Worker   { "jfs",	32,	 0,  4, "JFS1",			probe_jfs },
1588*6a54128fSAndroid Build Coastguard Worker   /* ZFS has 128 root blocks (#4 is the first used), check only 6 of them */
1589*6a54128fSAndroid Build Coastguard Worker   { "zfs",     128,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1590*6a54128fSAndroid Build Coastguard Worker   { "zfs",     128,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1591*6a54128fSAndroid Build Coastguard Worker   { "zfs",     132,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1592*6a54128fSAndroid Build Coastguard Worker   { "zfs",     132,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1593*6a54128fSAndroid Build Coastguard Worker   { "zfs",     136,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1594*6a54128fSAndroid Build Coastguard Worker   { "zfs",     136,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1595*6a54128fSAndroid Build Coastguard Worker   { "zfs",     384,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1596*6a54128fSAndroid Build Coastguard Worker   { "zfs",     384,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1597*6a54128fSAndroid Build Coastguard Worker   { "zfs",     388,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1598*6a54128fSAndroid Build Coastguard Worker   { "zfs",     388,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1599*6a54128fSAndroid Build Coastguard Worker   { "zfs",     392,	 0,  8, "\0\0\0\0\0\xba\xb1\x0c", probe_zfs },
1600*6a54128fSAndroid Build Coastguard Worker   { "zfs",     392,	 0,  8, "\x0c\xb1\xba\0\0\0\0\0", probe_zfs },
1601*6a54128fSAndroid Build Coastguard Worker   { "hfsplus",	 1,	 0,  2, "BD",			probe_hfsplus },
1602*6a54128fSAndroid Build Coastguard Worker   { "hfsplus",	 1,	 0,  2, "H+",			probe_hfsplus },
1603*6a54128fSAndroid Build Coastguard Worker   { "hfsplus",	 1,	 0,  2, "HX",			probe_hfsplus },
1604*6a54128fSAndroid Build Coastguard Worker   { "hfs",	 1,	 0,  2, "BD",			probe_hfs },
1605*6a54128fSAndroid Build Coastguard Worker   { "ufs",	 8,  0x55c,  4, "T\031\001\000",	0 },
1606*6a54128fSAndroid Build Coastguard Worker   { "hpfs",	 8,	 0,  4, "I\350\225\371",	0 },
1607*6a54128fSAndroid Build Coastguard Worker   { "sysv",	 0,  0x3f8,  4, "\020~\030\375",	0 },
1608*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0,  0xff6, 10, "SWAP-SPACE",		probe_swap0 },
1609*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0,  0xff6, 10, "SWAPSPACE2",		probe_swap1 },
1610*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0,  0xff6,  9, "S1SUSPEND",		probe_swap1 },
1611*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0,  0xff6,  9, "S2SUSPEND",		probe_swap1 },
1612*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0,  0xff6,  9, "ULSUSPEND",		probe_swap1 },
1613*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x1ff6, 10, "SWAP-SPACE",		probe_swap0 },
1614*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x1ff6, 10, "SWAPSPACE2",		probe_swap1 },
1615*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x1ff6,  9, "S1SUSPEND",		probe_swap1 },
1616*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x1ff6,  9, "S2SUSPEND",		probe_swap1 },
1617*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x1ff6,  9, "ULSUSPEND",		probe_swap1 },
1618*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x3ff6, 10, "SWAP-SPACE",		probe_swap0 },
1619*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x3ff6, 10, "SWAPSPACE2",		probe_swap1 },
1620*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x3ff6,  9, "S1SUSPEND",		probe_swap1 },
1621*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x3ff6,  9, "S2SUSPEND",		probe_swap1 },
1622*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x3ff6,  9, "ULSUSPEND",		probe_swap1 },
1623*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x7ff6, 10, "SWAP-SPACE",		probe_swap0 },
1624*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0x7ff6, 10, "SWAPSPACE2",		probe_swap1 },
1625*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x7ff6,  9, "S1SUSPEND",		probe_swap1 },
1626*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x7ff6,  9, "S2SUSPEND",		probe_swap1 },
1627*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0x7ff6,  9, "ULSUSPEND",		probe_swap1 },
1628*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0xfff6, 10, "SWAP-SPACE",		probe_swap0 },
1629*6a54128fSAndroid Build Coastguard Worker   { "swap",	 0, 0xfff6, 10, "SWAPSPACE2",		probe_swap1 },
1630*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0xfff6,  9, "S1SUSPEND",		probe_swap1 },
1631*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0xfff6,  9, "S2SUSPEND",		probe_swap1 },
1632*6a54128fSAndroid Build Coastguard Worker   { "swsuspend", 0, 0xfff6,  9, "ULSUSPEND",		probe_swap1 },
1633*6a54128fSAndroid Build Coastguard Worker   { "ocfs",	 0,	 8,  9,	"OracleCFS",		probe_ocfs },
1634*6a54128fSAndroid Build Coastguard Worker   { "ocfs2",	 1,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1635*6a54128fSAndroid Build Coastguard Worker   { "ocfs2",	 2,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1636*6a54128fSAndroid Build Coastguard Worker   { "ocfs2",	 4,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1637*6a54128fSAndroid Build Coastguard Worker   { "ocfs2",	 8,	 0,  6,	"OCFSV2",		probe_ocfs2 },
1638*6a54128fSAndroid Build Coastguard Worker   { "crypt_LUKS", 0,	 0,  6,	"LUKS\xba\xbe",		probe_luks },
1639*6a54128fSAndroid Build Coastguard Worker   { "squashfs",	 0,	 0,  4,	"sqsh",			0 },
1640*6a54128fSAndroid Build Coastguard Worker   { "squashfs",	 0,	 0,  4,	"hsqs",			0 },
1641*6a54128fSAndroid Build Coastguard Worker   { "lvm2pv",	 0,  0x218,  8, "LVM2 001",		probe_lvm2 },
1642*6a54128fSAndroid Build Coastguard Worker   { "lvm2pv",	 0,  0x018,  8, "LVM2 001",		probe_lvm2 },
1643*6a54128fSAndroid Build Coastguard Worker   { "lvm2pv",	 1,  0x018,  8, "LVM2 001",		probe_lvm2 },
1644*6a54128fSAndroid Build Coastguard Worker   { "lvm2pv",	 1,  0x218,  8, "LVM2 001",		probe_lvm2 },
1645*6a54128fSAndroid Build Coastguard Worker   { "btrfs",	 64,  0x40,  8, "_BHRfS_M",		probe_btrfs },
1646*6a54128fSAndroid Build Coastguard Worker   { "f2fs",	 1,      0,  4, "\x10\x20\xf5\xf2",	probe_f2fs },
1647*6a54128fSAndroid Build Coastguard Worker   { "exfat",     0,      3,  8, "EXFAT   ",             probe_exfat },
1648*6a54128fSAndroid Build Coastguard Worker   {   NULL,	 0,	 0,  0, NULL,			NULL }
1649*6a54128fSAndroid Build Coastguard Worker };
1650*6a54128fSAndroid Build Coastguard Worker 
1651*6a54128fSAndroid Build Coastguard Worker /*
1652*6a54128fSAndroid Build Coastguard Worker  * Verify that the data in dev is consistent with what is on the actual
1653*6a54128fSAndroid Build Coastguard Worker  * block device (using the devname field only).  Normally this will be
1654*6a54128fSAndroid Build Coastguard Worker  * called when finding items in the cache, but for long running processes
1655*6a54128fSAndroid Build Coastguard Worker  * is also desirable to revalidate an item before use.
1656*6a54128fSAndroid Build Coastguard Worker  *
1657*6a54128fSAndroid Build Coastguard Worker  * If we are unable to revalidate the data, we return the old data and
1658*6a54128fSAndroid Build Coastguard Worker  * do not set the BLKID_BID_FL_VERIFIED flag on it.
1659*6a54128fSAndroid Build Coastguard Worker  */
blkid_verify(blkid_cache cache,blkid_dev dev)1660*6a54128fSAndroid Build Coastguard Worker blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
1661*6a54128fSAndroid Build Coastguard Worker {
1662*6a54128fSAndroid Build Coastguard Worker 	struct blkid_magic *id;
1663*6a54128fSAndroid Build Coastguard Worker 	struct blkid_probe probe;
1664*6a54128fSAndroid Build Coastguard Worker 	blkid_tag_iterate iter;
1665*6a54128fSAndroid Build Coastguard Worker 	unsigned char *buf;
1666*6a54128fSAndroid Build Coastguard Worker 	const char *type, *value;
1667*6a54128fSAndroid Build Coastguard Worker 	struct stat st;
1668*6a54128fSAndroid Build Coastguard Worker 	time_t now;
1669*6a54128fSAndroid Build Coastguard Worker 	double diff;
1670*6a54128fSAndroid Build Coastguard Worker 	int idx;
1671*6a54128fSAndroid Build Coastguard Worker 
1672*6a54128fSAndroid Build Coastguard Worker 	if (!dev)
1673*6a54128fSAndroid Build Coastguard Worker 		return NULL;
1674*6a54128fSAndroid Build Coastguard Worker 
1675*6a54128fSAndroid Build Coastguard Worker 	now = time(0);
1676*6a54128fSAndroid Build Coastguard Worker 	diff = difftime(now, dev->bid_time);
1677*6a54128fSAndroid Build Coastguard Worker 
1678*6a54128fSAndroid Build Coastguard Worker 	if (stat(dev->bid_name, &st) < 0) {
1679*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE,
1680*6a54128fSAndroid Build Coastguard Worker 		    printf("blkid_verify: error %s (%d) while "
1681*6a54128fSAndroid Build Coastguard Worker 			   "trying to stat %s\n", strerror(errno), errno,
1682*6a54128fSAndroid Build Coastguard Worker 			   dev->bid_name));
1683*6a54128fSAndroid Build Coastguard Worker 	open_err:
1684*6a54128fSAndroid Build Coastguard Worker 		if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
1685*6a54128fSAndroid Build Coastguard Worker 			/* We don't have read permission, just return cache data. */
1686*6a54128fSAndroid Build Coastguard Worker 			DBG(DEBUG_PROBE, printf("returning unverified data for %s\n",
1687*6a54128fSAndroid Build Coastguard Worker 						dev->bid_name));
1688*6a54128fSAndroid Build Coastguard Worker 			return dev;
1689*6a54128fSAndroid Build Coastguard Worker 		}
1690*6a54128fSAndroid Build Coastguard Worker 		blkid_free_dev(dev);
1691*6a54128fSAndroid Build Coastguard Worker 		return NULL;
1692*6a54128fSAndroid Build Coastguard Worker 	}
1693*6a54128fSAndroid Build Coastguard Worker 
1694*6a54128fSAndroid Build Coastguard Worker 	if ((now >= dev->bid_time) &&
1695*6a54128fSAndroid Build Coastguard Worker 	    (st.st_mtime <= dev->bid_time) &&
1696*6a54128fSAndroid Build Coastguard Worker 	    ((diff < BLKID_PROBE_MIN) ||
1697*6a54128fSAndroid Build Coastguard Worker 	     (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
1698*6a54128fSAndroid Build Coastguard Worker 	      diff < BLKID_PROBE_INTERVAL)))
1699*6a54128fSAndroid Build Coastguard Worker 		return dev;
1700*6a54128fSAndroid Build Coastguard Worker 
1701*6a54128fSAndroid Build Coastguard Worker 	DBG(DEBUG_PROBE,
1702*6a54128fSAndroid Build Coastguard Worker 	    printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t"
1703*6a54128fSAndroid Build Coastguard Worker 		   "time since last check %lu)\n",
1704*6a54128fSAndroid Build Coastguard Worker 		   dev->bid_name, (unsigned long)dev->bid_time,
1705*6a54128fSAndroid Build Coastguard Worker 		   (unsigned long)st.st_mtime, (unsigned long)diff));
1706*6a54128fSAndroid Build Coastguard Worker 
1707*6a54128fSAndroid Build Coastguard Worker 	if ((probe.fd = open(dev->bid_name, O_RDONLY)) < 0) {
1708*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE, printf("blkid_verify: error %s (%d) while "
1709*6a54128fSAndroid Build Coastguard Worker 					"opening %s\n", strerror(errno), errno,
1710*6a54128fSAndroid Build Coastguard Worker 					dev->bid_name));
1711*6a54128fSAndroid Build Coastguard Worker 		goto open_err;
1712*6a54128fSAndroid Build Coastguard Worker 	}
1713*6a54128fSAndroid Build Coastguard Worker 
1714*6a54128fSAndroid Build Coastguard Worker 	probe.cache = cache;
1715*6a54128fSAndroid Build Coastguard Worker 	probe.dev = dev;
1716*6a54128fSAndroid Build Coastguard Worker 	probe.sbbuf = 0;
1717*6a54128fSAndroid Build Coastguard Worker 	probe.buf = 0;
1718*6a54128fSAndroid Build Coastguard Worker 	probe.buf_max = 0;
1719*6a54128fSAndroid Build Coastguard Worker 
1720*6a54128fSAndroid Build Coastguard Worker 	/*
1721*6a54128fSAndroid Build Coastguard Worker 	 * Iterate over the type array.  If we already know the type,
1722*6a54128fSAndroid Build Coastguard Worker 	 * then try that first.  If it doesn't work, then blow away
1723*6a54128fSAndroid Build Coastguard Worker 	 * the type information, and try again.
1724*6a54128fSAndroid Build Coastguard Worker 	 *
1725*6a54128fSAndroid Build Coastguard Worker 	 */
1726*6a54128fSAndroid Build Coastguard Worker try_again:
1727*6a54128fSAndroid Build Coastguard Worker 	type = 0;
1728*6a54128fSAndroid Build Coastguard Worker 	if (!dev->bid_type || !strcmp(dev->bid_type, "mdraid")) {
1729*6a54128fSAndroid Build Coastguard Worker 		uuid_t	uuid;
1730*6a54128fSAndroid Build Coastguard Worker 
1731*6a54128fSAndroid Build Coastguard Worker 		if (check_mdraid(probe.fd, uuid) == 0) {
1732*6a54128fSAndroid Build Coastguard Worker 			set_uuid(dev, uuid, 0);
1733*6a54128fSAndroid Build Coastguard Worker 			type = "mdraid";
1734*6a54128fSAndroid Build Coastguard Worker 			goto found_type;
1735*6a54128fSAndroid Build Coastguard Worker 		}
1736*6a54128fSAndroid Build Coastguard Worker 	}
1737*6a54128fSAndroid Build Coastguard Worker 	for (id = type_array; id->bim_type; id++) {
1738*6a54128fSAndroid Build Coastguard Worker 		if (dev->bid_type &&
1739*6a54128fSAndroid Build Coastguard Worker 		    strcmp(id->bim_type, dev->bid_type))
1740*6a54128fSAndroid Build Coastguard Worker 			continue;
1741*6a54128fSAndroid Build Coastguard Worker 
1742*6a54128fSAndroid Build Coastguard Worker 		idx = id->bim_kboff + (id->bim_sboff >> 10);
1743*6a54128fSAndroid Build Coastguard Worker 		buf = get_buffer(&probe, (__u64) idx << 10, 1024);
1744*6a54128fSAndroid Build Coastguard Worker 		if (!buf)
1745*6a54128fSAndroid Build Coastguard Worker 			continue;
1746*6a54128fSAndroid Build Coastguard Worker 
1747*6a54128fSAndroid Build Coastguard Worker 		if (memcmp(id->bim_magic, buf + (id->bim_sboff & 0x3ff),
1748*6a54128fSAndroid Build Coastguard Worker 			   id->bim_len))
1749*6a54128fSAndroid Build Coastguard Worker 			continue;
1750*6a54128fSAndroid Build Coastguard Worker 
1751*6a54128fSAndroid Build Coastguard Worker 		if ((id->bim_probe == NULL) ||
1752*6a54128fSAndroid Build Coastguard Worker 		    (id->bim_probe(&probe, id, buf) == 0)) {
1753*6a54128fSAndroid Build Coastguard Worker 			type = id->bim_type;
1754*6a54128fSAndroid Build Coastguard Worker 			goto found_type;
1755*6a54128fSAndroid Build Coastguard Worker 		}
1756*6a54128fSAndroid Build Coastguard Worker 	}
1757*6a54128fSAndroid Build Coastguard Worker 
1758*6a54128fSAndroid Build Coastguard Worker 	if (!id->bim_type && dev->bid_type) {
1759*6a54128fSAndroid Build Coastguard Worker 		/*
1760*6a54128fSAndroid Build Coastguard Worker 		 * Zap the device filesystem information and try again
1761*6a54128fSAndroid Build Coastguard Worker 		 */
1762*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE,
1763*6a54128fSAndroid Build Coastguard Worker 		    printf("previous fs type %s not valid, "
1764*6a54128fSAndroid Build Coastguard Worker 			   "trying full probe\n", dev->bid_type));
1765*6a54128fSAndroid Build Coastguard Worker 		iter = blkid_tag_iterate_begin(dev);
1766*6a54128fSAndroid Build Coastguard Worker 		while (blkid_tag_next(iter, &type, &value) == 0)
1767*6a54128fSAndroid Build Coastguard Worker 			blkid_set_tag(dev, type, 0, 0);
1768*6a54128fSAndroid Build Coastguard Worker 		blkid_tag_iterate_end(iter);
1769*6a54128fSAndroid Build Coastguard Worker 		goto try_again;
1770*6a54128fSAndroid Build Coastguard Worker 	}
1771*6a54128fSAndroid Build Coastguard Worker 
1772*6a54128fSAndroid Build Coastguard Worker 	if (!dev->bid_type) {
1773*6a54128fSAndroid Build Coastguard Worker 		blkid_free_dev(dev);
1774*6a54128fSAndroid Build Coastguard Worker 		dev = 0;
1775*6a54128fSAndroid Build Coastguard Worker 		goto found_type;
1776*6a54128fSAndroid Build Coastguard Worker 	}
1777*6a54128fSAndroid Build Coastguard Worker 
1778*6a54128fSAndroid Build Coastguard Worker found_type:
1779*6a54128fSAndroid Build Coastguard Worker 	if (dev && type) {
1780*6a54128fSAndroid Build Coastguard Worker 		dev->bid_devno = st.st_rdev;
1781*6a54128fSAndroid Build Coastguard Worker 		dev->bid_time = time(0);
1782*6a54128fSAndroid Build Coastguard Worker 		dev->bid_flags |= BLKID_BID_FL_VERIFIED;
1783*6a54128fSAndroid Build Coastguard Worker 		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
1784*6a54128fSAndroid Build Coastguard Worker 
1785*6a54128fSAndroid Build Coastguard Worker 		blkid_set_tag(dev, "TYPE", type, 0);
1786*6a54128fSAndroid Build Coastguard Worker 
1787*6a54128fSAndroid Build Coastguard Worker 		DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n",
1788*6a54128fSAndroid Build Coastguard Worker 			   dev->bid_name, (long long)st.st_rdev, type));
1789*6a54128fSAndroid Build Coastguard Worker 	}
1790*6a54128fSAndroid Build Coastguard Worker 
1791*6a54128fSAndroid Build Coastguard Worker 	free(probe.sbbuf);
1792*6a54128fSAndroid Build Coastguard Worker 	free(probe.buf);
1793*6a54128fSAndroid Build Coastguard Worker 	if (probe.fd >= 0)
1794*6a54128fSAndroid Build Coastguard Worker 		close(probe.fd);
1795*6a54128fSAndroid Build Coastguard Worker 
1796*6a54128fSAndroid Build Coastguard Worker 	return dev;
1797*6a54128fSAndroid Build Coastguard Worker }
1798*6a54128fSAndroid Build Coastguard Worker 
blkid_known_fstype(const char * fstype)1799*6a54128fSAndroid Build Coastguard Worker int blkid_known_fstype(const char *fstype)
1800*6a54128fSAndroid Build Coastguard Worker {
1801*6a54128fSAndroid Build Coastguard Worker 	struct blkid_magic *id;
1802*6a54128fSAndroid Build Coastguard Worker 
1803*6a54128fSAndroid Build Coastguard Worker 	for (id = type_array; id->bim_type; id++) {
1804*6a54128fSAndroid Build Coastguard Worker 		if (strcmp(fstype, id->bim_type) == 0)
1805*6a54128fSAndroid Build Coastguard Worker 			return 1;
1806*6a54128fSAndroid Build Coastguard Worker 	}
1807*6a54128fSAndroid Build Coastguard Worker 	return 0;
1808*6a54128fSAndroid Build Coastguard Worker }
1809*6a54128fSAndroid Build Coastguard Worker 
1810*6a54128fSAndroid Build Coastguard Worker #ifdef TEST_PROGRAM
main(int argc,char ** argv)1811*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
1812*6a54128fSAndroid Build Coastguard Worker {
1813*6a54128fSAndroid Build Coastguard Worker 	blkid_dev dev;
1814*6a54128fSAndroid Build Coastguard Worker 	blkid_cache cache;
1815*6a54128fSAndroid Build Coastguard Worker 	int ret;
1816*6a54128fSAndroid Build Coastguard Worker 
1817*6a54128fSAndroid Build Coastguard Worker 	if (argc != 2) {
1818*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "Usage: %s device\n"
1819*6a54128fSAndroid Build Coastguard Worker 			"Probe a single device to determine type\n", argv[0]);
1820*6a54128fSAndroid Build Coastguard Worker 		exit(1);
1821*6a54128fSAndroid Build Coastguard Worker 	}
1822*6a54128fSAndroid Build Coastguard Worker 	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
1823*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "%s: error creating cache (%d)\n",
1824*6a54128fSAndroid Build Coastguard Worker 			argv[0], ret);
1825*6a54128fSAndroid Build Coastguard Worker 		exit(1);
1826*6a54128fSAndroid Build Coastguard Worker 	}
1827*6a54128fSAndroid Build Coastguard Worker 	dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
1828*6a54128fSAndroid Build Coastguard Worker 	if (!dev) {
1829*6a54128fSAndroid Build Coastguard Worker 		printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
1830*6a54128fSAndroid Build Coastguard Worker 		return (1);
1831*6a54128fSAndroid Build Coastguard Worker 	}
1832*6a54128fSAndroid Build Coastguard Worker 	printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
1833*6a54128fSAndroid Build Coastguard Worker 	if (dev->bid_label)
1834*6a54128fSAndroid Build Coastguard Worker 		printf("LABEL='%s'\n", dev->bid_label);
1835*6a54128fSAndroid Build Coastguard Worker 	if (dev->bid_uuid)
1836*6a54128fSAndroid Build Coastguard Worker 		printf("UUID='%s'\n", dev->bid_uuid);
1837*6a54128fSAndroid Build Coastguard Worker 
1838*6a54128fSAndroid Build Coastguard Worker 	blkid_free_dev(dev);
1839*6a54128fSAndroid Build Coastguard Worker 	return (0);
1840*6a54128fSAndroid Build Coastguard Worker }
1841*6a54128fSAndroid Build Coastguard Worker #endif
1842