ntfs.c revision 1.1 1 1.1 christos /* $NetBSD: ntfs.c,v 1.1 2018/01/09 03:31:15 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.1 christos __RCSID("$NetBSD: ntfs.c,v 1.1 2018/01/09 03:31:15 christos Exp $");
39 1.1 christos
40 1.1 christos #include <stdint.h>
41 1.1 christos #include <stdio.h>
42 1.1 christos #include <stdlib.h>
43 1.1 christos #include <string.h>
44 1.1 christos
45 1.1 christos #include "fstyp.h"
46 1.1 christos
47 1.1 christos #define NTFS_A_VOLUMENAME 0x60
48 1.1 christos #define NTFS_FILEMAGIC ((uint32_t)(0x454C4946))
49 1.1 christos #define NTFS_VOLUMEINO 3
50 1.1 christos
51 1.1 christos struct ntfs_attr {
52 1.1 christos uint32_t a_type;
53 1.1 christos uint32_t reclen;
54 1.1 christos uint8_t a_flag;
55 1.1 christos uint8_t a_namelen;
56 1.1 christos uint8_t a_nameoff;
57 1.1 christos uint8_t reserved1;
58 1.1 christos uint8_t a_compression;
59 1.1 christos uint8_t reserved2;
60 1.1 christos uint16_t a_index;
61 1.1 christos uint16_t a_datalen;
62 1.1 christos uint16_t reserved3;
63 1.1 christos uint16_t a_dataoff;
64 1.1 christos uint16_t a_indexed;
65 1.1 christos } __packed;
66 1.1 christos
67 1.1 christos struct ntfs_filerec {
68 1.1 christos uint32_t fr_hdrmagic;
69 1.1 christos uint16_t fr_hdrfoff;
70 1.1 christos uint16_t fr_hdrfnum;
71 1.1 christos uint8_t reserved[8];
72 1.1 christos uint16_t fr_seqnum;
73 1.1 christos uint16_t fr_nlink;
74 1.1 christos uint16_t fr_attroff;
75 1.1 christos uint16_t fr_flags;
76 1.1 christos uint32_t fr_size;
77 1.1 christos uint32_t fr_allocated;
78 1.1 christos uint64_t fr_mainrec;
79 1.1 christos uint16_t fr_attrnum;
80 1.1 christos } __packed;
81 1.1 christos
82 1.1 christos struct ntfs_bootfile {
83 1.1 christos uint8_t reserved1[3];
84 1.1 christos uint8_t bf_sysid[8];
85 1.1 christos uint16_t bf_bps;
86 1.1 christos uint8_t bf_spc;
87 1.1 christos uint8_t reserved2[7];
88 1.1 christos uint8_t bf_media;
89 1.1 christos uint8_t reserved3[2];
90 1.1 christos uint16_t bf_spt;
91 1.1 christos uint16_t bf_heads;
92 1.1 christos uint8_t reserver4[12];
93 1.1 christos uint64_t bf_spv;
94 1.1 christos uint64_t bf_mftcn;
95 1.1 christos uint64_t bf_mftmirrcn;
96 1.1 christos int8_t bf_mftrecsz;
97 1.1 christos uint32_t bf_ibsz;
98 1.1 christos uint32_t bf_volsn;
99 1.1 christos } __packed;
100 1.1 christos
101 1.1 christos int
102 1.1 christos fstyp_ntfs(FILE *fp, char *label, size_t size)
103 1.1 christos {
104 1.1 christos struct ntfs_bootfile *bf;
105 1.1 christos struct ntfs_filerec *fr;
106 1.1 christos struct ntfs_attr *atr;
107 1.1 christos off_t voloff;
108 1.1 christos char *filerecp, *ap;
109 1.1 christos int8_t mftrecsz;
110 1.1 christos char vnchar;
111 1.1 christos size_t recsize, j;
112 1.1 christos
113 1.1 christos filerecp = NULL;
114 1.1 christos
115 1.1 christos bf = read_buf(fp, 0, 512);
116 1.1 christos if (bf == NULL || strncmp((char*)bf->bf_sysid, "NTFS ", 8) != 0)
117 1.1 christos goto fail;
118 1.1 christos
119 1.1 christos mftrecsz = bf->bf_mftrecsz;
120 1.1 christos recsize = mftrecsz > 0 ? (size_t)(mftrecsz * bf->bf_bps * bf->bf_spc)
121 1.1 christos : (size_t)(1 << -mftrecsz);
122 1.1 christos
123 1.1 christos voloff = (off_t)(bf->bf_mftcn * bf->bf_spc * bf->bf_bps +
124 1.1 christos recsize * NTFS_VOLUMEINO);
125 1.1 christos
126 1.1 christos filerecp = read_buf(fp, voloff, recsize);
127 1.1 christos if (filerecp == NULL)
128 1.1 christos goto fail;
129 1.1 christos fr = (struct ntfs_filerec *)filerecp;
130 1.1 christos
131 1.1 christos if (fr->fr_hdrmagic != NTFS_FILEMAGIC)
132 1.1 christos goto fail;
133 1.1 christos
134 1.1 christos for (ap = filerecp + fr->fr_attroff;
135 1.1 christos atr = (struct ntfs_attr *)ap, (int)atr->a_type != -1;
136 1.1 christos ap += atr->reclen) {
137 1.1 christos if (atr->a_type == NTFS_A_VOLUMENAME) {
138 1.1 christos if(atr->a_datalen >= size *2){
139 1.1 christos goto fail;
140 1.1 christos }
141 1.1 christos /*
142 1.1 christos * UNICODE to ASCII.
143 1.1 christos * Should we need to use iconv(9)?
144 1.1 christos */
145 1.1 christos for (j = 0; j < atr->a_datalen; j++) {
146 1.1 christos vnchar = *(ap + atr->a_dataoff + j);
147 1.1 christos if (j & 1) {
148 1.1 christos if (vnchar) {
149 1.1 christos goto fail;
150 1.1 christos }
151 1.1 christos } else {
152 1.1 christos label[j / 2] = vnchar;
153 1.1 christos }
154 1.1 christos }
155 1.1 christos label[j / 2] = 0;
156 1.1 christos break;
157 1.1 christos }
158 1.1 christos }
159 1.1 christos
160 1.1 christos free(bf);
161 1.1 christos free(filerecp);
162 1.1 christos
163 1.1 christos return 0;
164 1.1 christos
165 1.1 christos fail:
166 1.1 christos free(bf);
167 1.1 christos free(filerecp);
168 1.1 christos
169 1.1 christos return 1;
170 1.1 christos }
171