xref: /freebsd/sys/geom/label/g_label_ntfs.c (revision 271c3a9060f2ee55607ebe146523f888e1db2654)
1 /*-
2  * Copyright (c) 2005 Takanori Watanabe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 
35 #include <fs/ntfs/ntfs.h>
36 
37 #include <geom/geom.h>
38 #include <geom/label/g_label.h>
39 
40 #define G_LABEL_NTFS_DIR	"ntfs"
41 
42 
43 static void
44 g_label_ntfs_taste(struct g_consumer *cp, char *label, size_t size)
45 {
46 	struct g_provider *pp;
47 	struct bootfile *bf;
48 	struct filerec *fr;
49 	struct attr *atr;
50 	off_t voloff;
51 	char *filerecp, *ap;
52 	char mftrecsz, vnchar;
53 	int recsize, j;
54 
55 	g_topology_assert_not();
56 
57 	label[0] = '\0';
58 	pp = cp->provider;
59 	filerecp = NULL;
60 
61 	bf = (struct bootfile *)g_read_data(cp, 0, pp->sectorsize, NULL);
62 	if (bf == NULL || strncmp(bf->bf_sysid, "NTFS    ", 8) != 0)
63 		goto done;
64 
65 	mftrecsz = (char)bf->bf_mftrecsz;
66 	recsize = (mftrecsz > 0) ? (mftrecsz * bf->bf_bps * bf->bf_spc) : (1 << -mftrecsz);
67 	if (recsize % pp->sectorsize != 0)
68 		goto done;
69 
70 	voloff = bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
71 	    recsize * NTFS_VOLUMEINO;
72 	if (voloff % pp->sectorsize != 0)
73 		goto done;
74 
75 	filerecp = g_read_data(cp, voloff, recsize, NULL);
76 	if (filerecp == NULL)
77 		goto done;
78 	fr = (struct filerec *)filerecp;
79 
80 	if (fr->fr_fixup.fh_magic != NTFS_FILEMAGIC)
81 		goto done;
82 
83 	for (ap = filerecp + fr->fr_attroff;
84 	    atr = (struct attr *)ap, atr->a_hdr.a_type != -1;
85 	    ap += atr->a_hdr.reclen) {
86 		if (atr->a_hdr.a_type == NTFS_A_VOLUMENAME) {
87 			if(atr->a_r.a_datalen >= size *2){
88 				label[0] = 0;
89 				goto done;
90 			}
91 			/*
92 			 *UNICODE to ASCII.
93 			 * Should we need to use iconv(9)?
94 			 */
95 			for (j = 0; j < atr->a_r.a_datalen; j++) {
96 				vnchar = *(ap + atr->a_r.a_dataoff + j);
97 				if (j & 1) {
98 					if (vnchar) {
99 						label[0] = 0;
100 						goto done;
101 					}
102 				} else {
103 					label[j / 2] = vnchar;
104 				}
105 			}
106 			label[j / 2] = 0;
107 			break;
108 		}
109 	}
110 done:
111 	if (bf != NULL)
112 		g_free(bf);
113 	if (filerecp != NULL)
114 		g_free(filerecp);
115 }
116 
117 const struct g_label_desc g_label_ntfs = {
118 	.ld_taste = g_label_ntfs_taste,
119 	.ld_dir = G_LABEL_NTFS_DIR
120 };
121