1284589Sallanjude/*-
2284589Sallanjude * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
3284589Sallanjude * All rights reserved.
4284589Sallanjude *
5284589Sallanjude * Redistribution and use in source and binary forms, with or without
6284589Sallanjude * modification, are permitted provided that the following conditions
7284589Sallanjude * are met:
8284589Sallanjude * 1. Redistributions of source code must retain the above copyright
9284589Sallanjude *    notice, this list of conditions and the following disclaimer.
10284589Sallanjude * 2. Redistributions in binary form must reproduce the above copyright
11284589Sallanjude *    notice, this list of conditions and the following disclaimer in the
12284589Sallanjude *    documentation and/or other materials provided with the distribution.
13284589Sallanjude *
14284589Sallanjude * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15284589Sallanjude * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16284589Sallanjude * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17284589Sallanjude * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18284589Sallanjude * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19284589Sallanjude * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20284589Sallanjude * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21284589Sallanjude * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22284589Sallanjude * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23284589Sallanjude * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24284589Sallanjude * SUCH DAMAGE.
25284589Sallanjude */
26284589Sallanjude
27284589Sallanjude#include <sys/cdefs.h>
28284589Sallanjude__FBSDID("$FreeBSD$");
29284589Sallanjude
30284589Sallanjude#include <sys/disk.h>
31284589Sallanjude#include <sys/types.h>
32284589Sallanjude#include <stdio.h>
33284589Sallanjude#include <stdlib.h>
34284589Sallanjude#include <string.h>
35284589Sallanjude
36284589Sallanjude#include <geom/eli/g_eli.h>
37284589Sallanjude
38284589Sallanjude#include "fstyp.h"
39284589Sallanjude
40284589Sallanjudeint
41284589Sallanjudefstyp_geli(FILE *fp, char *label __unused, size_t labelsize __unused)
42284589Sallanjude{
43284589Sallanjude	int error;
44284589Sallanjude	off_t mediasize;
45284589Sallanjude	u_int sectorsize;
46284589Sallanjude	struct g_eli_metadata md;
47284589Sallanjude	u_char *buf;
48284589Sallanjude
49284589Sallanjude	error = ioctl(fileno(fp), DIOCGMEDIASIZE, &mediasize);
50284589Sallanjude	if (error != 0)
51284589Sallanjude		return (1);
52284589Sallanjude	error = ioctl(fileno(fp), DIOCGSECTORSIZE, &sectorsize);
53284589Sallanjude	if (error != 0)
54284589Sallanjude		return (1);
55284589Sallanjude	buf = (u_char *)read_buf(fp, mediasize - sectorsize, sectorsize);
56284589Sallanjude	if (buf == NULL)
57284589Sallanjude		goto gelierr;
58284589Sallanjude	error = eli_metadata_decode(buf, &md);
59284589Sallanjude	if (error)
60284589Sallanjude		goto gelierr;
61284589Sallanjude
62293776Sallanjude	if (strcmp(md.md_magic, G_ELI_MAGIC) == 0) {
63284589Sallanjude		free(buf);
64284589Sallanjude		return (0);
65284589Sallanjude	}
66284589Sallanjude
67284589Sallanjudegelierr:
68284589Sallanjude	free(buf);
69284589Sallanjude
70284589Sallanjude	return (1);
71284589Sallanjude}
72