2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Takanori Watanabe
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <geom/geom.h>
38 #include <geom/label/g_label.h>
40 #define NTFS_A_VOLUMENAME 0x60
41 #define NTFS_FILEMAGIC ((uint32_t)(0x454C4946))
42 #define NTFS_VOLUMEINO 3
51 uint8_t a_compression;
70 uint32_t fr_allocated;
75 struct ntfs_bootfile {
85 uint8_t reserver4[12];
88 uint64_t bf_mftmirrcn;
95 g_label_ntfs_taste(struct g_consumer *cp, char *label, size_t size)
97 struct g_provider *pp;
98 struct ntfs_bootfile *bf;
99 struct ntfs_filerec *fr;
100 struct ntfs_attr *atr;
107 g_topology_assert_not();
113 bf = (struct ntfs_bootfile *)g_read_data(cp, 0, pp->sectorsize, NULL);
114 if (bf == NULL || strncmp(bf->bf_sysid, "NTFS ", 8) != 0)
117 mftrecsz = bf->bf_mftrecsz;
118 recsize = (mftrecsz > 0) ? (mftrecsz * bf->bf_bps * bf->bf_spc) : (1 << -mftrecsz);
119 if (recsize == 0 || recsize % pp->sectorsize != 0)
122 voloff = bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
123 recsize * NTFS_VOLUMEINO;
124 if (voloff % pp->sectorsize != 0)
127 filerecp = g_read_data(cp, voloff, recsize, NULL);
128 if (filerecp == NULL)
130 fr = (struct ntfs_filerec *)filerecp;
132 if (fr->fr_hdrmagic != NTFS_FILEMAGIC)
135 for (ap = filerecp + fr->fr_attroff;
136 atr = (struct ntfs_attr *)ap, atr->a_type != -1;
138 if (atr->a_type == NTFS_A_VOLUMENAME) {
139 if(atr->a_datalen >= size *2){
145 * Should we need to use iconv(9)?
147 for (j = 0; j < atr->a_datalen; j++) {
148 vnchar = *(ap + atr->a_dataoff + j);
155 label[j / 2] = vnchar;
165 if (filerecp != NULL)
169 struct g_label_desc g_label_ntfs = {
170 .ld_taste = g_label_ntfs_taste,
171 .ld_dirprefix = "ntfs/",
175 G_LABEL_INIT(ntfs, g_label_ntfs, "Create device nodes for NTFS volumes");