xref: /freebsd/usr.sbin/fstyp/ext2fs.c (revision be3a49ee62b6af938565ef159b535aea59a92f16)
1*be3a49eeSEdward Tomasz Napierala /*-
2*be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2005 Stanislav Sedov
3*be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2014 The FreeBSD Foundation
4*be3a49eeSEdward Tomasz Napierala  * All rights reserved.
5*be3a49eeSEdward Tomasz Napierala  *
6*be3a49eeSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
7*be3a49eeSEdward Tomasz Napierala  * from the FreeBSD Foundation.
8*be3a49eeSEdward Tomasz Napierala  *
9*be3a49eeSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
10*be3a49eeSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
11*be3a49eeSEdward Tomasz Napierala  * are met:
12*be3a49eeSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
13*be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
14*be3a49eeSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
15*be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
16*be3a49eeSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
17*be3a49eeSEdward Tomasz Napierala  *
18*be3a49eeSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*be3a49eeSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*be3a49eeSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*be3a49eeSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*be3a49eeSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*be3a49eeSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*be3a49eeSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*be3a49eeSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*be3a49eeSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*be3a49eeSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*be3a49eeSEdward Tomasz Napierala  * SUCH DAMAGE.
29*be3a49eeSEdward Tomasz Napierala  */
30*be3a49eeSEdward Tomasz Napierala 
31*be3a49eeSEdward Tomasz Napierala #include <sys/cdefs.h>
32*be3a49eeSEdward Tomasz Napierala __FBSDID("$FreeBSD$");
33*be3a49eeSEdward Tomasz Napierala 
34*be3a49eeSEdward Tomasz Napierala #include <stdio.h>
35*be3a49eeSEdward Tomasz Napierala #include <stdint.h>
36*be3a49eeSEdward Tomasz Napierala #include <stdlib.h>
37*be3a49eeSEdward Tomasz Napierala #include <string.h>
38*be3a49eeSEdward Tomasz Napierala 
39*be3a49eeSEdward Tomasz Napierala #include "fstyp.h"
40*be3a49eeSEdward Tomasz Napierala 
41*be3a49eeSEdward Tomasz Napierala #define EXT2FS_SB_OFFSET	1024
42*be3a49eeSEdward Tomasz Napierala #define EXT2_SUPER_MAGIC	0xef53
43*be3a49eeSEdward Tomasz Napierala #define EXT2_DYNAMIC_REV	1
44*be3a49eeSEdward Tomasz Napierala 
45*be3a49eeSEdward Tomasz Napierala typedef struct e2sb {
46*be3a49eeSEdward Tomasz Napierala 	uint8_t		fake1[56];
47*be3a49eeSEdward Tomasz Napierala 	uint16_t	s_magic;
48*be3a49eeSEdward Tomasz Napierala 	uint8_t		fake2[18];
49*be3a49eeSEdward Tomasz Napierala 	uint32_t	s_rev_level;
50*be3a49eeSEdward Tomasz Napierala 	uint8_t		fake3[40];
51*be3a49eeSEdward Tomasz Napierala 	char		s_volume_name[16];
52*be3a49eeSEdward Tomasz Napierala } e2sb_t;
53*be3a49eeSEdward Tomasz Napierala 
54*be3a49eeSEdward Tomasz Napierala int
55*be3a49eeSEdward Tomasz Napierala fstyp_ext2fs(FILE *fp, char *label, size_t size)
56*be3a49eeSEdward Tomasz Napierala {
57*be3a49eeSEdward Tomasz Napierala 	e2sb_t *fs;
58*be3a49eeSEdward Tomasz Napierala 	char *s_volume_name;
59*be3a49eeSEdward Tomasz Napierala 
60*be3a49eeSEdward Tomasz Napierala 	fs = (e2sb_t *)read_buf(fp, EXT2FS_SB_OFFSET, 512);
61*be3a49eeSEdward Tomasz Napierala 	if (fs == NULL)
62*be3a49eeSEdward Tomasz Napierala 		return (1);
63*be3a49eeSEdward Tomasz Napierala 
64*be3a49eeSEdward Tomasz Napierala 	/* Check for magic and versio n*/
65*be3a49eeSEdward Tomasz Napierala 	if (fs->s_magic == EXT2_SUPER_MAGIC &&
66*be3a49eeSEdward Tomasz Napierala 	    fs->s_rev_level == EXT2_DYNAMIC_REV) {
67*be3a49eeSEdward Tomasz Napierala 		//G_LABEL_DEBUG(1, "ext2fs file system detected on %s.",
68*be3a49eeSEdward Tomasz Napierala 		//    pp->name);
69*be3a49eeSEdward Tomasz Napierala 	} else {
70*be3a49eeSEdward Tomasz Napierala 		free(fs);
71*be3a49eeSEdward Tomasz Napierala 		return (1);
72*be3a49eeSEdward Tomasz Napierala 	}
73*be3a49eeSEdward Tomasz Napierala 
74*be3a49eeSEdward Tomasz Napierala 	s_volume_name = fs->s_volume_name;
75*be3a49eeSEdward Tomasz Napierala 	/* Terminate label */
76*be3a49eeSEdward Tomasz Napierala 	s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
77*be3a49eeSEdward Tomasz Napierala 
78*be3a49eeSEdward Tomasz Napierala 	if (s_volume_name[0] == '/')
79*be3a49eeSEdward Tomasz Napierala 		s_volume_name += 1;
80*be3a49eeSEdward Tomasz Napierala 
81*be3a49eeSEdward Tomasz Napierala 	/* Check for volume label */
82*be3a49eeSEdward Tomasz Napierala 	if (s_volume_name[0] == '\0') {
83*be3a49eeSEdward Tomasz Napierala 		free(fs);
84*be3a49eeSEdward Tomasz Napierala 		return (1);
85*be3a49eeSEdward Tomasz Napierala 	}
86*be3a49eeSEdward Tomasz Napierala 
87*be3a49eeSEdward Tomasz Napierala 	strlcpy(label, s_volume_name, size);
88*be3a49eeSEdward Tomasz Napierala 
89*be3a49eeSEdward Tomasz Napierala 	return (0);
90*be3a49eeSEdward Tomasz Napierala }
91