/**
* ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
*
* Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
* Copyright (c) 2001-2012 Anton Altaparmakov
* Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
*
* Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program (in the main directory of the source in the file COPYING); if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/stringify.h>
#include <linux/kernel.h>
#include <linux/uuid.h>
#include "ldm.h"
#include "check.h"
#include "msdos.h"
/**
* ldm_debug/info/error/crit - Output an error message
* @f: A printf format string containing the message
* @...: Variables to substitute into @f
*
* ldm_debug() writes a DEBUG level message to the syslog but only if the
* driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
*/
#ifndef CONFIG_LDM_DEBUG
#define ldm_debug(...) do {} while (0)
#else
#define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
#endif
#define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
#define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
#define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
static __printf(3, 4)
void _ldm_printk(const char *level, const char *function, const char *fmt, ...)
{
struct va_format vaf;
va_list args;
va_start (args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
printk("%s%s(): %pV\n", level, function, &vaf);
va_end(args);
}
/**
* ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
* @data: Raw database PRIVHEAD structure loaded from the device
* @ph: In-memory privhead structure in which to return parsed information
*
* This parses the LDM database PRIVHEAD structure supplied in @data and
* sets up the in-memory privhead structure @ph with the obtained information.
*
* Return: 'true' @ph contains the PRIVHEAD data
* 'false' @ph contents are undefined
*/
static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
{
bool is_vista = false;
BUG_ON(!data || !ph);
if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
ldm_error("Cannot find PRIVHEAD structure. LDM database is"
" corrupt. Aborting.");
return false;
}
ph->ver_major = get_unaligned_be16(data + 0x000C);
ph->ver_minor = get_unaligned_be16(data + 0x000E);
ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
ph->config_start = get_unaligned_be64(data + 0x012B);
ph->config_size = get_unaligned_be64(data + 0x0133);
/* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
if (ph->ver_major == 2 && ph->ver_minor == 12)
is_vista = true;
if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
" Aborting.", ph->ver_major, ph->ver_minor);
return false;
}
ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
ph->ver_minor, is_vista ? "Vista" : "2000/XP");
if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
/* Warn the user and continue, carefully. */
ldm_info("Database is normally %u bytes, it claims to "
"be %llu bytes.", LDM_DB_SIZE,
(unsigned long long)ph->config_size);
}
if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
ph->logical_disk_size > ph->config_start)) {
ldm_error("PRIVHEAD disk size doesn't match real disk size");
return false;
}
if (uuid_parse(data + 0x0030, &ph->disk_id)) {
ldm_error("PRIVHEAD contains an invalid GUID.");
return false;
}
ldm_debug("Parsed PRIVHEAD successfully.");
return true;
}
/**
* ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
* @data: Raw database TOCBLOCK structure loaded from the device
* @toc: In-memory toc structure in which to return parsed information
*
* This parses the LDM Database TOCBLOCK (table of contents) structure supplied
* in @data and sets up the in-memory tocblock structure @toc with the obtained
* information.
*
* N.B. The *_start and *_size values returned in @toc are not range-checked.
*
* Return: 'true' @toc contains the TOCBLOCK data
* 'false' @toc contents are undefined
*/
static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
{
BUG_ON (!data || !toc);
if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
return false;
}
strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
toc->bitmap1_size = get_unaligned_be64(data + 0x36);
if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
sizeof (toc->bitmap1_name)) != 0) {
ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
TOC_BITMAP1, toc->bitmap1_name);
return false;
}
strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
toc->bitmap2_start = get_unaligned_be64(data + 0x50);
toc->bitmap2_size = get_unaligned_be64(data + 0x58);
if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
sizeof (toc->bitmap2_name)) != 0) {
ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
TOC_BITMAP2, toc->bitmap2_name);
return false;
}
ldm_debug ("Parsed TOCBLOCK successfully.");
return true;
}
/**
* ldm_parse_vmdb - Read the LDM Database VMDB structure
* @data: Raw database VMDB structure loaded from the device
* @vm: In-memory vmdb structure in which to return parsed information
*
* This parses the LDM Database VMDB structure supplied in @data and sets up
* the in-memory vmdb structure @vm with the obtained information.
*
* N.B. The *_start, *_size and *_seq values will be range-checked later.
*
* Return: 'true' @vm contains VMDB info
* 'false' @vm contents are undefined
*/
static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
{
BUG_ON (!data || !vm);
if (MAGIC_VMDB != get_unaligned_be32(data)) {
ldm_crit ("Cannot find the VMDB, database may be corrupt.");
return false;
}
vm->ver_major = get_unaligned_be16(data + 0x12);
vm->ver_minor = get_unaligned_be16(data + 0x14);
if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
"Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
return false;
}
vm->vblk_size = get_unaligned_be32(data + 0x08);
if (vm->vblk_size == 0) {
ldm_error ("Illegal VBLK size");
return false;
}
vm->vblk_offset = get_unaligned_be32(data + 0x0C);
vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
ldm_debug ("Parsed VMDB successfully.");
return true;
}
/**
* ldm_compare_privheads - Compare two privhead objects
* @ph1: First privhead
* @ph2: Second privhead
*
* This compares the two privhead structures @ph1 and @ph2.
*
* Return: 'true' Identical
* 'false' Different
*/
static bool ldm_compare_privheads (const struct privhead *ph1,
const struct privhead *ph2)
{
BUG_ON (!ph1 || !ph2);
|