162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * mst.c - NTFS multi sector transfer protection handling code. Part of the
462306a36Sopenharmony_ci *	   Linux-NTFS project.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (c) 2001-2004 Anton Altaparmakov
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "ntfs.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/**
1262306a36Sopenharmony_ci * post_read_mst_fixup - deprotect multi sector transfer protected data
1362306a36Sopenharmony_ci * @b:		pointer to the data to deprotect
1462306a36Sopenharmony_ci * @size:	size in bytes of @b
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * Perform the necessary post read multi sector transfer fixup and detect the
1762306a36Sopenharmony_ci * presence of incomplete multi sector transfers. - In that case, overwrite the
1862306a36Sopenharmony_ci * magic of the ntfs record header being processed with "BAAD" (in memory only!)
1962306a36Sopenharmony_ci * and abort processing.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * NOTE: We consider the absence / invalidity of an update sequence array to
2462306a36Sopenharmony_ci * mean that the structure is not protected at all and hence doesn't need to
2562306a36Sopenharmony_ci * be fixed up. Thus, we return success and not failure in this case. This is
2662306a36Sopenharmony_ci * in contrast to pre_write_mst_fixup(), see below.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ciint post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	u16 usa_ofs, usa_count, usn;
3162306a36Sopenharmony_ci	u16 *usa_pos, *data_pos;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	/* Setup the variables. */
3462306a36Sopenharmony_ci	usa_ofs = le16_to_cpu(b->usa_ofs);
3562306a36Sopenharmony_ci	/* Decrement usa_count to get number of fixups. */
3662306a36Sopenharmony_ci	usa_count = le16_to_cpu(b->usa_count) - 1;
3762306a36Sopenharmony_ci	/* Size and alignment checks. */
3862306a36Sopenharmony_ci	if ( size & (NTFS_BLOCK_SIZE - 1)	||
3962306a36Sopenharmony_ci	     usa_ofs & 1			||
4062306a36Sopenharmony_ci	     usa_ofs + (usa_count * 2) > size	||
4162306a36Sopenharmony_ci	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
4262306a36Sopenharmony_ci		return 0;
4362306a36Sopenharmony_ci	/* Position of usn in update sequence array. */
4462306a36Sopenharmony_ci	usa_pos = (u16*)b + usa_ofs/sizeof(u16);
4562306a36Sopenharmony_ci	/*
4662306a36Sopenharmony_ci	 * The update sequence number which has to be equal to each of the
4762306a36Sopenharmony_ci	 * u16 values before they are fixed up. Note no need to care for
4862306a36Sopenharmony_ci	 * endianness since we are comparing and moving data for on disk
4962306a36Sopenharmony_ci	 * structures which means the data is consistent. - If it is
5062306a36Sopenharmony_ci	 * consistenty the wrong endianness it doesn't make any difference.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	usn = *usa_pos;
5362306a36Sopenharmony_ci	/*
5462306a36Sopenharmony_ci	 * Position in protected data of first u16 that needs fixing up.
5562306a36Sopenharmony_ci	 */
5662306a36Sopenharmony_ci	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
5762306a36Sopenharmony_ci	/*
5862306a36Sopenharmony_ci	 * Check for incomplete multi sector transfer(s).
5962306a36Sopenharmony_ci	 */
6062306a36Sopenharmony_ci	while (usa_count--) {
6162306a36Sopenharmony_ci		if (*data_pos != usn) {
6262306a36Sopenharmony_ci			/*
6362306a36Sopenharmony_ci			 * Incomplete multi sector transfer detected! )-:
6462306a36Sopenharmony_ci			 * Set the magic to "BAAD" and return failure.
6562306a36Sopenharmony_ci			 * Note that magic_BAAD is already converted to le32.
6662306a36Sopenharmony_ci			 */
6762306a36Sopenharmony_ci			b->magic = magic_BAAD;
6862306a36Sopenharmony_ci			return -EINVAL;
6962306a36Sopenharmony_ci		}
7062306a36Sopenharmony_ci		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci	/* Re-setup the variables. */
7362306a36Sopenharmony_ci	usa_count = le16_to_cpu(b->usa_count) - 1;
7462306a36Sopenharmony_ci	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
7562306a36Sopenharmony_ci	/* Fixup all sectors. */
7662306a36Sopenharmony_ci	while (usa_count--) {
7762306a36Sopenharmony_ci		/*
7862306a36Sopenharmony_ci		 * Increment position in usa and restore original data from
7962306a36Sopenharmony_ci		 * the usa into the data buffer.
8062306a36Sopenharmony_ci		 */
8162306a36Sopenharmony_ci		*data_pos = *(++usa_pos);
8262306a36Sopenharmony_ci		/* Increment position in data as well. */
8362306a36Sopenharmony_ci		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
8462306a36Sopenharmony_ci	}
8562306a36Sopenharmony_ci	return 0;
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/**
8962306a36Sopenharmony_ci * pre_write_mst_fixup - apply multi sector transfer protection
9062306a36Sopenharmony_ci * @b:		pointer to the data to protect
9162306a36Sopenharmony_ci * @size:	size in bytes of @b
9262306a36Sopenharmony_ci *
9362306a36Sopenharmony_ci * Perform the necessary pre write multi sector transfer fixup on the data
9462306a36Sopenharmony_ci * pointer to by @b of @size.
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * Return 0 if fixup applied (success) or -EINVAL if no fixup was performed
9762306a36Sopenharmony_ci * (assumed not needed). This is in contrast to post_read_mst_fixup() above.
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * NOTE: We consider the absence / invalidity of an update sequence array to
10062306a36Sopenharmony_ci * mean that the structure is not subject to protection and hence doesn't need
10162306a36Sopenharmony_ci * to be fixed up. This means that you have to create a valid update sequence
10262306a36Sopenharmony_ci * array header in the ntfs record before calling this function, otherwise it
10362306a36Sopenharmony_ci * will fail (the header needs to contain the position of the update sequence
10462306a36Sopenharmony_ci * array together with the number of elements in the array). You also need to
10562306a36Sopenharmony_ci * initialise the update sequence number before calling this function
10662306a36Sopenharmony_ci * otherwise a random word will be used (whatever was in the record at that
10762306a36Sopenharmony_ci * position at that time).
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_ciint pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	le16 *usa_pos, *data_pos;
11262306a36Sopenharmony_ci	u16 usa_ofs, usa_count, usn;
11362306a36Sopenharmony_ci	le16 le_usn;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* Sanity check + only fixup if it makes sense. */
11662306a36Sopenharmony_ci	if (!b || ntfs_is_baad_record(b->magic) ||
11762306a36Sopenharmony_ci			ntfs_is_hole_record(b->magic))
11862306a36Sopenharmony_ci		return -EINVAL;
11962306a36Sopenharmony_ci	/* Setup the variables. */
12062306a36Sopenharmony_ci	usa_ofs = le16_to_cpu(b->usa_ofs);
12162306a36Sopenharmony_ci	/* Decrement usa_count to get number of fixups. */
12262306a36Sopenharmony_ci	usa_count = le16_to_cpu(b->usa_count) - 1;
12362306a36Sopenharmony_ci	/* Size and alignment checks. */
12462306a36Sopenharmony_ci	if ( size & (NTFS_BLOCK_SIZE - 1)	||
12562306a36Sopenharmony_ci	     usa_ofs & 1			||
12662306a36Sopenharmony_ci	     usa_ofs + (usa_count * 2) > size	||
12762306a36Sopenharmony_ci	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
12862306a36Sopenharmony_ci		return -EINVAL;
12962306a36Sopenharmony_ci	/* Position of usn in update sequence array. */
13062306a36Sopenharmony_ci	usa_pos = (le16*)((u8*)b + usa_ofs);
13162306a36Sopenharmony_ci	/*
13262306a36Sopenharmony_ci	 * Cyclically increment the update sequence number
13362306a36Sopenharmony_ci	 * (skipping 0 and -1, i.e. 0xffff).
13462306a36Sopenharmony_ci	 */
13562306a36Sopenharmony_ci	usn = le16_to_cpup(usa_pos) + 1;
13662306a36Sopenharmony_ci	if (usn == 0xffff || !usn)
13762306a36Sopenharmony_ci		usn = 1;
13862306a36Sopenharmony_ci	le_usn = cpu_to_le16(usn);
13962306a36Sopenharmony_ci	*usa_pos = le_usn;
14062306a36Sopenharmony_ci	/* Position in data of first u16 that needs fixing up. */
14162306a36Sopenharmony_ci	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
14262306a36Sopenharmony_ci	/* Fixup all sectors. */
14362306a36Sopenharmony_ci	while (usa_count--) {
14462306a36Sopenharmony_ci		/*
14562306a36Sopenharmony_ci		 * Increment the position in the usa and save the
14662306a36Sopenharmony_ci		 * original data from the data buffer into the usa.
14762306a36Sopenharmony_ci		 */
14862306a36Sopenharmony_ci		*(++usa_pos) = *data_pos;
14962306a36Sopenharmony_ci		/* Apply fixup to data. */
15062306a36Sopenharmony_ci		*data_pos = le_usn;
15162306a36Sopenharmony_ci		/* Increment position in data as well. */
15262306a36Sopenharmony_ci		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci	return 0;
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/**
15862306a36Sopenharmony_ci * post_write_mst_fixup - fast deprotect multi sector transfer protected data
15962306a36Sopenharmony_ci * @b:		pointer to the data to deprotect
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * Perform the necessary post write multi sector transfer fixup, not checking
16262306a36Sopenharmony_ci * for any errors, because we assume we have just used pre_write_mst_fixup(),
16362306a36Sopenharmony_ci * thus the data will be fine or we would never have gotten here.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_civoid post_write_mst_fixup(NTFS_RECORD *b)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	le16 *usa_pos, *data_pos;
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	u16 usa_ofs = le16_to_cpu(b->usa_ofs);
17062306a36Sopenharmony_ci	u16 usa_count = le16_to_cpu(b->usa_count) - 1;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	/* Position of usn in update sequence array. */
17362306a36Sopenharmony_ci	usa_pos = (le16*)b + usa_ofs/sizeof(le16);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	/* Position in protected data of first u16 that needs fixing up. */
17662306a36Sopenharmony_ci	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	/* Fixup all sectors. */
17962306a36Sopenharmony_ci	while (usa_count--) {
18062306a36Sopenharmony_ci		/*
18162306a36Sopenharmony_ci		 * Increment position in usa and restore original data from
18262306a36Sopenharmony_ci		 * the usa into the data buffer.
18362306a36Sopenharmony_ci		 */
18462306a36Sopenharmony_ci		*data_pos = *(++usa_pos);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci		/* Increment position in data as well. */
18762306a36Sopenharmony_ci		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci}
190