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