1275680Strasz/*-
2275680Strasz * Copyright (c) 2005 Stanislav Sedov
3275680Strasz * Copyright (c) 2014 The FreeBSD Foundation
4275680Strasz * All rights reserved.
5275680Strasz *
6275680Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
7275680Strasz * from the FreeBSD Foundation.
8275680Strasz *
9275680Strasz * Redistribution and use in source and binary forms, with or without
10275680Strasz * modification, are permitted provided that the following conditions
11275680Strasz * are met:
12275680Strasz * 1. Redistributions of source code must retain the above copyright
13275680Strasz *    notice, this list of conditions and the following disclaimer.
14275680Strasz * 2. Redistributions in binary form must reproduce the above copyright
15275680Strasz *    notice, this list of conditions and the following disclaimer in the
16275680Strasz *    documentation and/or other materials provided with the distribution.
17275680Strasz *
18275680Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19275680Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20275680Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21275680Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22275680Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23275680Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24275680Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25275680Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26275680Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27275680Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28275680Strasz * SUCH DAMAGE.
29275680Strasz */
30275680Strasz
31275680Strasz#include <sys/cdefs.h>
32275680Strasz__FBSDID("$FreeBSD: releng/10.2/usr.sbin/fstyp/ext2fs.c 277437 2015-01-20 20:44:16Z trasz $");
33275680Strasz
34275680Strasz#include <stdio.h>
35275680Strasz#include <stdint.h>
36275680Strasz#include <stdlib.h>
37275680Strasz#include <string.h>
38275680Strasz
39275680Strasz#include "fstyp.h"
40275680Strasz
41275680Strasz#define EXT2FS_SB_OFFSET	1024
42275680Strasz#define EXT2_SUPER_MAGIC	0xef53
43275680Strasz#define EXT2_DYNAMIC_REV	1
44275680Strasz
45275680Strasztypedef struct e2sb {
46275680Strasz	uint8_t		fake1[56];
47275680Strasz	uint16_t	s_magic;
48275680Strasz	uint8_t		fake2[18];
49275680Strasz	uint32_t	s_rev_level;
50275680Strasz	uint8_t		fake3[40];
51275680Strasz	char		s_volume_name[16];
52275680Strasz} e2sb_t;
53275680Strasz
54275680Straszint
55275680Straszfstyp_ext2fs(FILE *fp, char *label, size_t size)
56275680Strasz{
57275680Strasz	e2sb_t *fs;
58275680Strasz	char *s_volume_name;
59275680Strasz
60275680Strasz	fs = (e2sb_t *)read_buf(fp, EXT2FS_SB_OFFSET, 512);
61275680Strasz	if (fs == NULL)
62275680Strasz		return (1);
63275680Strasz
64275680Strasz	/* Check for magic and versio n*/
65275680Strasz	if (fs->s_magic == EXT2_SUPER_MAGIC &&
66275680Strasz	    fs->s_rev_level == EXT2_DYNAMIC_REV) {
67275680Strasz		//G_LABEL_DEBUG(1, "ext2fs file system detected on %s.",
68275680Strasz		//    pp->name);
69275680Strasz	} else {
70275680Strasz		free(fs);
71275680Strasz		return (1);
72275680Strasz	}
73275680Strasz
74275680Strasz	s_volume_name = fs->s_volume_name;
75275680Strasz	/* Terminate label */
76275680Strasz	s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
77275680Strasz
78275680Strasz	if (s_volume_name[0] == '/')
79275680Strasz		s_volume_name += 1;
80275680Strasz
81275680Strasz	strlcpy(label, s_volume_name, size);
82277436Strasz	free(fs);
83275680Strasz
84275680Strasz	return (0);
85275680Strasz}
86