xref: /freebsd/sys/geom/label/g_label_ntfs.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 Takanori Watanabe
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 
37 #include <geom/geom.h>
38 #include <geom/label/g_label.h>
39 
40 #define	NTFS_A_VOLUMENAME	0x60
41 #define	NTFS_FILEMAGIC		((uint32_t)(0x454C4946))
42 #define	NTFS_VOLUMEINO		3
43 
44 #define G_LABEL_NTFS_DIR	"ntfs"
45 
46 struct ntfs_attr {
47 	uint32_t	a_type;
48 	uint32_t	reclen;
49 	uint8_t		a_flag;
50 	uint8_t		a_namelen;
51 	uint8_t		a_nameoff;
52 	uint8_t		reserved1;
53 	uint8_t		a_compression;
54 	uint8_t		reserved2;
55 	uint16_t	a_index;
56 	uint16_t	a_datalen;
57 	uint16_t	reserved3;
58 	uint16_t	a_dataoff;
59 	uint16_t	a_indexed;
60 } __packed;
61 
62 struct ntfs_filerec {
63 	uint32_t	fr_hdrmagic;
64 	uint16_t	fr_hdrfoff;
65 	uint16_t	fr_hdrfnum;
66 	uint8_t		reserved[8];
67 	uint16_t	fr_seqnum;
68 	uint16_t	fr_nlink;
69 	uint16_t	fr_attroff;
70 	uint16_t	fr_flags;
71 	uint32_t	fr_size;
72 	uint32_t	fr_allocated;
73 	uint64_t	fr_mainrec;
74 	uint16_t	fr_attrnum;
75 } __packed;
76 
77 struct ntfs_bootfile {
78 	uint8_t		reserved1[3];
79 	uint8_t		bf_sysid[8];
80 	uint16_t	bf_bps;
81 	uint8_t		bf_spc;
82 	uint8_t		reserved2[7];
83 	uint8_t		bf_media;
84 	uint8_t		reserved3[2];
85 	uint16_t	bf_spt;
86 	uint16_t	bf_heads;
87 	uint8_t		reserver4[12];
88 	uint64_t	bf_spv;
89 	uint64_t	bf_mftcn;
90 	uint64_t	bf_mftmirrcn;
91 	int8_t		bf_mftrecsz;
92 	uint32_t	bf_ibsz;
93 	uint32_t	bf_volsn;
94 } __packed;
95 
96 static void
97 g_label_ntfs_taste(struct g_consumer *cp, char *label, size_t size)
98 {
99 	struct g_provider *pp;
100 	struct ntfs_bootfile *bf;
101 	struct ntfs_filerec *fr;
102 	struct ntfs_attr *atr;
103 	off_t voloff;
104 	char *filerecp, *ap;
105 	int8_t mftrecsz;
106 	char vnchar;
107 	int recsize, j;
108 
109 	g_topology_assert_not();
110 
111 	label[0] = '\0';
112 	pp = cp->provider;
113 	filerecp = NULL;
114 
115 	bf = (struct ntfs_bootfile *)g_read_data(cp, 0, pp->sectorsize, NULL);
116 	if (bf == NULL || strncmp(bf->bf_sysid, "NTFS    ", 8) != 0)
117 		goto done;
118 
119 	mftrecsz = bf->bf_mftrecsz;
120 	recsize = (mftrecsz > 0) ? (mftrecsz * bf->bf_bps * bf->bf_spc) : (1 << -mftrecsz);
121 	if (recsize == 0 || recsize % pp->sectorsize != 0)
122 		goto done;
123 
124 	voloff = bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
125 	    recsize * NTFS_VOLUMEINO;
126 	if (voloff % pp->sectorsize != 0)
127 		goto done;
128 
129 	filerecp = g_read_data(cp, voloff, recsize, NULL);
130 	if (filerecp == NULL)
131 		goto done;
132 	fr = (struct ntfs_filerec *)filerecp;
133 
134 	if (fr->fr_hdrmagic != NTFS_FILEMAGIC)
135 		goto done;
136 
137 	for (ap = filerecp + fr->fr_attroff;
138 	    atr = (struct ntfs_attr *)ap, atr->a_type != -1;
139 	    ap += atr->reclen) {
140 		if (atr->a_type == NTFS_A_VOLUMENAME) {
141 			if(atr->a_datalen >= size *2){
142 				label[0] = 0;
143 				goto done;
144 			}
145 			/*
146 			 *UNICODE to ASCII.
147 			 * Should we need to use iconv(9)?
148 			 */
149 			for (j = 0; j < atr->a_datalen; j++) {
150 				vnchar = *(ap + atr->a_dataoff + j);
151 				if (j & 1) {
152 					if (vnchar) {
153 						label[0] = 0;
154 						goto done;
155 					}
156 				} else {
157 					label[j / 2] = vnchar;
158 				}
159 			}
160 			label[j / 2] = 0;
161 			break;
162 		}
163 	}
164 done:
165 	if (bf != NULL)
166 		g_free(bf);
167 	if (filerecp != NULL)
168 		g_free(filerecp);
169 }
170 
171 struct g_label_desc g_label_ntfs = {
172 	.ld_taste = g_label_ntfs_taste,
173 	.ld_dir = G_LABEL_NTFS_DIR,
174 	.ld_enabled = 1
175 };
176 
177 G_LABEL_INIT(ntfs, g_label_ntfs, "Create device nodes for NTFS volumes");
178