xref: /freebsd/sys/geom/label/g_label_ntfs.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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 attr    *atr;
49 	char           *filerecp = NULL, *ap, vnchar;
50 	struct filerec *fr;
51 	char		mftrecsz;
52 	int		recsize;
53 	int		j;
54 	off_t		voloff;
55 	g_topology_assert_not();
56 	pp = cp->provider;
57 	label[0] = '\0';
58 
59 	bf = (struct bootfile *)g_read_data(cp, 0, pp->sectorsize, NULL);
60 
61 	if (bf == NULL || strncmp(bf->bf_sysid, "NTFS    ", 8) != 0) {
62 		goto done;
63 	}
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 
79 	fr = (struct filerec *)filerecp;
80 
81 	if(fr->fr_fixup.fh_magic != NTFS_FILEMAGIC){
82 		label[0] = 0;
83 		goto done;
84 	}
85 
86 	for (ap = filerecp + fr->fr_attroff; atr = (struct attr *)ap, atr->a_hdr.a_type != -1; ap += atr->a_hdr.reclen) {
87 		if (atr->a_hdr.a_type == NTFS_A_VOLUMENAME) {
88 			if(atr->a_r.a_datalen >= size *2){
89 				label[0] = 0;
90 				goto done;
91 			}
92 			/*
93 			 *UNICODE to ASCII.
94 			 * Should we need to use iconv(9)?
95 			 */
96 			for (j = 0; j < atr->a_r.a_datalen; j++) {
97 				vnchar = *(ap + atr->a_r.a_dataoff + j);
98 				if ((j & 1)) {
99 					if (vnchar) {
100 						label[0] = 0;
101 						goto done;
102 					}
103 				} else {
104 					label[j / 2] = vnchar;
105 				}
106 			}
107 			label[j / 2] = 0;
108 			break;
109 		}
110 	}
111 done:
112 	if (bf != NULL)
113 		g_free(bf);
114 	if (filerecp != NULL)
115 		g_free(filerecp);
116 	return;
117 
118 }
119 
120 const struct g_label_desc g_label_ntfs = {
121 	.ld_taste = g_label_ntfs_taste,
122 	.ld_dir = G_LABEL_NTFS_DIR
123 };
124