ntfs.c revision 1.3 1 1.3 christos /* $NetBSD: ntfs.c,v 1.3 2021/12/02 14:26:12 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.1 christos * Copyright (c) 2016 The DragonFly Project
6 1.1 christos * Copyright (c) 2005 Takanori Watanabe
7 1.1 christos * Copyright (c) 2014 The FreeBSD Foundation
8 1.1 christos * All rights reserved.
9 1.1 christos *
10 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
11 1.1 christos * by Tomohiro Kusumi <kusumi.tomohiro (at) gmail.com>.
12 1.1 christos *
13 1.1 christos * This software was developed by Edward Tomasz Napierala under sponsorship
14 1.1 christos * from the FreeBSD Foundation.
15 1.1 christos *
16 1.1 christos * Redistribution and use in source and binary forms, with or without
17 1.1 christos * modification, are permitted provided that the following conditions
18 1.1 christos * are met:
19 1.1 christos * 1. Redistributions of source code must retain the above copyright
20 1.1 christos * notice, this list of conditions and the following disclaimer.
21 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 christos * notice, this list of conditions and the following disclaimer in the
23 1.1 christos * documentation and/or other materials provided with the distribution.
24 1.1 christos *
25 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 christos * SUCH DAMAGE.
36 1.1 christos */
37 1.1 christos #include <sys/cdefs.h>
38 1.3 christos __RCSID("$NetBSD: ntfs.c,v 1.3 2021/12/02 14:26:12 christos Exp $");
39 1.1 christos
40 1.2 tkusumi #include <err.h>
41 1.2 tkusumi #include <iconv.h>
42 1.3 christos #include <langinfo.h>
43 1.1 christos #include <stdint.h>
44 1.1 christos #include <stdio.h>
45 1.1 christos #include <stdlib.h>
46 1.1 christos #include <string.h>
47 1.1 christos
48 1.1 christos #include "fstyp.h"
49 1.1 christos
50 1.1 christos #define NTFS_A_VOLUMENAME 0x60
51 1.1 christos #define NTFS_FILEMAGIC ((uint32_t)(0x454C4946))
52 1.1 christos #define NTFS_VOLUMEINO 3
53 1.1 christos
54 1.1 christos struct ntfs_attr {
55 1.1 christos uint32_t a_type;
56 1.1 christos uint32_t reclen;
57 1.1 christos uint8_t a_flag;
58 1.1 christos uint8_t a_namelen;
59 1.1 christos uint8_t a_nameoff;
60 1.1 christos uint8_t reserved1;
61 1.1 christos uint8_t a_compression;
62 1.1 christos uint8_t reserved2;
63 1.1 christos uint16_t a_index;
64 1.1 christos uint16_t a_datalen;
65 1.1 christos uint16_t reserved3;
66 1.1 christos uint16_t a_dataoff;
67 1.1 christos uint16_t a_indexed;
68 1.1 christos } __packed;
69 1.1 christos
70 1.1 christos struct ntfs_filerec {
71 1.1 christos uint32_t fr_hdrmagic;
72 1.1 christos uint16_t fr_hdrfoff;
73 1.1 christos uint16_t fr_hdrfnum;
74 1.1 christos uint8_t reserved[8];
75 1.1 christos uint16_t fr_seqnum;
76 1.1 christos uint16_t fr_nlink;
77 1.1 christos uint16_t fr_attroff;
78 1.1 christos uint16_t fr_flags;
79 1.1 christos uint32_t fr_size;
80 1.1 christos uint32_t fr_allocated;
81 1.1 christos uint64_t fr_mainrec;
82 1.1 christos uint16_t fr_attrnum;
83 1.1 christos } __packed;
84 1.1 christos
85 1.1 christos struct ntfs_bootfile {
86 1.1 christos uint8_t reserved1[3];
87 1.1 christos uint8_t bf_sysid[8];
88 1.1 christos uint16_t bf_bps;
89 1.1 christos uint8_t bf_spc;
90 1.1 christos uint8_t reserved2[7];
91 1.1 christos uint8_t bf_media;
92 1.1 christos uint8_t reserved3[2];
93 1.1 christos uint16_t bf_spt;
94 1.1 christos uint16_t bf_heads;
95 1.1 christos uint8_t reserver4[12];
96 1.1 christos uint64_t bf_spv;
97 1.1 christos uint64_t bf_mftcn;
98 1.1 christos uint64_t bf_mftmirrcn;
99 1.1 christos int8_t bf_mftrecsz;
100 1.1 christos uint32_t bf_ibsz;
101 1.1 christos uint32_t bf_volsn;
102 1.1 christos } __packed;
103 1.1 christos
104 1.2 tkusumi static void
105 1.2 tkusumi convert_label(const void *label /* LE */, size_t labellen, char *label_out,
106 1.2 tkusumi size_t label_sz)
107 1.2 tkusumi {
108 1.2 tkusumi char *label_out_orig;
109 1.2 tkusumi iconv_t cd;
110 1.2 tkusumi size_t rc;
111 1.2 tkusumi
112 1.3 christos cd = iconv_open(nl_langinfo(CODESET), NTFS_ENC);
113 1.2 tkusumi if (cd == (iconv_t)-1) {
114 1.2 tkusumi warn("ntfs: Could not open iconv");
115 1.2 tkusumi return;
116 1.2 tkusumi }
117 1.2 tkusumi
118 1.2 tkusumi label_out_orig = label_out;
119 1.2 tkusumi
120 1.2 tkusumi rc = iconv(cd, __UNCONST(&label), &labellen, &label_out,
121 1.2 tkusumi &label_sz);
122 1.2 tkusumi if (rc == (size_t)-1) {
123 1.2 tkusumi warn("ntfs: iconv()");
124 1.2 tkusumi *label_out_orig = '\0';
125 1.2 tkusumi } else {
126 1.2 tkusumi /* NUL-terminate result (iconv advances label_out). */
127 1.2 tkusumi if (label_sz == 0)
128 1.2 tkusumi label_out--;
129 1.2 tkusumi *label_out = '\0';
130 1.2 tkusumi }
131 1.2 tkusumi
132 1.2 tkusumi iconv_close(cd);
133 1.2 tkusumi }
134 1.2 tkusumi
135 1.1 christos int
136 1.1 christos fstyp_ntfs(FILE *fp, char *label, size_t size)
137 1.1 christos {
138 1.1 christos struct ntfs_bootfile *bf;
139 1.1 christos struct ntfs_filerec *fr;
140 1.1 christos struct ntfs_attr *atr;
141 1.1 christos off_t voloff;
142 1.1 christos char *filerecp, *ap;
143 1.1 christos int8_t mftrecsz;
144 1.2 tkusumi size_t recsize;
145 1.1 christos
146 1.1 christos filerecp = NULL;
147 1.1 christos
148 1.1 christos bf = read_buf(fp, 0, 512);
149 1.1 christos if (bf == NULL || strncmp((char*)bf->bf_sysid, "NTFS ", 8) != 0)
150 1.1 christos goto fail;
151 1.2 tkusumi if (!show_label)
152 1.2 tkusumi goto ok;
153 1.1 christos
154 1.1 christos mftrecsz = bf->bf_mftrecsz;
155 1.1 christos recsize = mftrecsz > 0 ? (size_t)(mftrecsz * bf->bf_bps * bf->bf_spc)
156 1.1 christos : (size_t)(1 << -mftrecsz);
157 1.1 christos
158 1.2 tkusumi voloff = (off_t)((off_t)bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
159 1.2 tkusumi (off_t)recsize * NTFS_VOLUMEINO);
160 1.1 christos
161 1.1 christos filerecp = read_buf(fp, voloff, recsize);
162 1.1 christos if (filerecp == NULL)
163 1.1 christos goto fail;
164 1.1 christos fr = (struct ntfs_filerec *)filerecp;
165 1.1 christos
166 1.1 christos if (fr->fr_hdrmagic != NTFS_FILEMAGIC)
167 1.1 christos goto fail;
168 1.1 christos
169 1.1 christos for (ap = filerecp + fr->fr_attroff;
170 1.1 christos atr = (struct ntfs_attr *)ap, (int)atr->a_type != -1;
171 1.1 christos ap += atr->reclen) {
172 1.2 tkusumi if (atr->a_type != NTFS_A_VOLUMENAME)
173 1.2 tkusumi continue;
174 1.2 tkusumi
175 1.2 tkusumi convert_label(ap + atr->a_dataoff,
176 1.2 tkusumi atr->a_datalen, label, size);
177 1.2 tkusumi break;
178 1.1 christos }
179 1.1 christos
180 1.2 tkusumi ok:
181 1.1 christos free(bf);
182 1.1 christos free(filerecp);
183 1.1 christos
184 1.1 christos return 0;
185 1.1 christos
186 1.1 christos fail:
187 1.1 christos free(bf);
188 1.1 christos free(filerecp);
189 1.1 christos
190 1.1 christos return 1;
191 1.1 christos }
192