xref: /openbmc/linux/fs/ntfs/mst.c (revision 74be2d3b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * mst.c - NTFS multi sector transfer protection handling code. Part of the
4  *	   Linux-NTFS project.
5  *
6  * Copyright (c) 2001-2004 Anton Altaparmakov
7  */
8 
9 #include "ntfs.h"
10 
11 /**
12  * post_read_mst_fixup - deprotect multi sector transfer protected data
13  * @b:		pointer to the data to deprotect
14  * @size:	size in bytes of @b
15  *
16  * Perform the necessary post read multi sector transfer fixup and detect the
17  * presence of incomplete multi sector transfers. - In that case, overwrite the
18  * magic of the ntfs record header being processed with "BAAD" (in memory only!)
19  * and abort processing.
20  *
21  * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
22  *
23  * NOTE: We consider the absence / invalidity of an update sequence array to
24  * mean that the structure is not protected at all and hence doesn't need to
25  * be fixed up. Thus, we return success and not failure in this case. This is
26  * in contrast to pre_write_mst_fixup(), see below.
27  */
28 int post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
29 {
30 	u16 usa_ofs, usa_count, usn;
31 	u16 *usa_pos, *data_pos;
32 
33 	/* Setup the variables. */
34 	usa_ofs = le16_to_cpu(b->usa_ofs);
35 	/* Decrement usa_count to get number of fixups. */
36 	usa_count = le16_to_cpu(b->usa_count) - 1;
37 	/* Size and alignment checks. */
38 	if ( size & (NTFS_BLOCK_SIZE - 1)	||
39 	     usa_ofs & 1			||
40 	     usa_ofs + (usa_count * 2) > size	||
41 	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
42 		return 0;
43 	/* Position of usn in update sequence array. */
44 	usa_pos = (u16*)b + usa_ofs/sizeof(u16);
45 	/*
46 	 * The update sequence number which has to be equal to each of the
47 	 * u16 values before they are fixed up. Note no need to care for
48 	 * endianness since we are comparing and moving data for on disk
49 	 * structures which means the data is consistent. - If it is
50 	 * consistenty the wrong endianness it doesn't make any difference.
51 	 */
52 	usn = *usa_pos;
53 	/*
54 	 * Position in protected data of first u16 that needs fixing up.
55 	 */
56 	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
57 	/*
58 	 * Check for incomplete multi sector transfer(s).
59 	 */
60 	while (usa_count--) {
61 		if (*data_pos != usn) {
62 			/*
63 			 * Incomplete multi sector transfer detected! )-:
64 			 * Set the magic to "BAAD" and return failure.
65 			 * Note that magic_BAAD is already converted to le32.
66 			 */
67 			b->magic = magic_BAAD;
68 			return -EINVAL;
69 		}
70 		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
71 	}
72 	/* Re-setup the variables. */
73 	usa_count = le16_to_cpu(b->usa_count) - 1;
74 	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
75 	/* Fixup all sectors. */
76 	while (usa_count--) {
77 		/*
78 		 * Increment position in usa and restore original data from
79 		 * the usa into the data buffer.
80 		 */
81 		*data_pos = *(++usa_pos);
82 		/* Increment position in data as well. */
83 		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
84 	}
85 	return 0;
86 }
87 
88 /**
89  * pre_write_mst_fixup - apply multi sector transfer protection
90  * @b:		pointer to the data to protect
91  * @size:	size in bytes of @b
92  *
93  * Perform the necessary pre write multi sector transfer fixup on the data
94  * pointer to by @b of @size.
95  *
96  * Return 0 if fixup applied (success) or -EINVAL if no fixup was performed
97  * (assumed not needed). This is in contrast to post_read_mst_fixup() above.
98  *
99  * NOTE: We consider the absence / invalidity of an update sequence array to
100  * mean that the structure is not subject to protection and hence doesn't need
101  * to be fixed up. This means that you have to create a valid update sequence
102  * array header in the ntfs record before calling this function, otherwise it
103  * will fail (the header needs to contain the position of the update sequence
104  * array together with the number of elements in the array). You also need to
105  * initialise the update sequence number before calling this function
106  * otherwise a random word will be used (whatever was in the record at that
107  * position at that time).
108  */
109 int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
110 {
111 	le16 *usa_pos, *data_pos;
112 	u16 usa_ofs, usa_count, usn;
113 	le16 le_usn;
114 
115 	/* Sanity check + only fixup if it makes sense. */
116 	if (!b || ntfs_is_baad_record(b->magic) ||
117 			ntfs_is_hole_record(b->magic))
118 		return -EINVAL;
119 	/* Setup the variables. */
120 	usa_ofs = le16_to_cpu(b->usa_ofs);
121 	/* Decrement usa_count to get number of fixups. */
122 	usa_count = le16_to_cpu(b->usa_count) - 1;
123 	/* Size and alignment checks. */
124 	if ( size & (NTFS_BLOCK_SIZE - 1)	||
125 	     usa_ofs & 1			||
126 	     usa_ofs + (usa_count * 2) > size	||
127 	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
128 		return -EINVAL;
129 	/* Position of usn in update sequence array. */
130 	usa_pos = (le16*)((u8*)b + usa_ofs);
131 	/*
132 	 * Cyclically increment the update sequence number
133 	 * (skipping 0 and -1, i.e. 0xffff).
134 	 */
135 	usn = le16_to_cpup(usa_pos) + 1;
136 	if (usn == 0xffff || !usn)
137 		usn = 1;
138 	le_usn = cpu_to_le16(usn);
139 	*usa_pos = le_usn;
140 	/* Position in data of first u16 that needs fixing up. */
141 	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
142 	/* Fixup all sectors. */
143 	while (usa_count--) {
144 		/*
145 		 * Increment the position in the usa and save the
146 		 * original data from the data buffer into the usa.
147 		 */
148 		*(++usa_pos) = *data_pos;
149 		/* Apply fixup to data. */
150 		*data_pos = le_usn;
151 		/* Increment position in data as well. */
152 		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
153 	}
154 	return 0;
155 }
156 
157 /**
158  * post_write_mst_fixup - fast deprotect multi sector transfer protected data
159  * @b:		pointer to the data to deprotect
160  *
161  * Perform the necessary post write multi sector transfer fixup, not checking
162  * for any errors, because we assume we have just used pre_write_mst_fixup(),
163  * thus the data will be fine or we would never have gotten here.
164  */
165 void post_write_mst_fixup(NTFS_RECORD *b)
166 {
167 	le16 *usa_pos, *data_pos;
168 
169 	u16 usa_ofs = le16_to_cpu(b->usa_ofs);
170 	u16 usa_count = le16_to_cpu(b->usa_count) - 1;
171 
172 	/* Position of usn in update sequence array. */
173 	usa_pos = (le16*)b + usa_ofs/sizeof(le16);
174 
175 	/* Position in protected data of first u16 that needs fixing up. */
176 	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
177 
178 	/* Fixup all sectors. */
179 	while (usa_count--) {
180 		/*
181 		 * Increment position in usa and restore original data from
182 		 * the usa into the data buffer.
183 		 */
184 		*data_pos = *(++usa_pos);
185 
186 		/* Increment position in data as well. */
187 		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
188 	}
189 }
190