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