1a497ee34SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0-or-later
22e9fb2c1SBart Van Assche /*
39be96f3fSAl Viro * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
49be96f3fSAl Viro *
59be96f3fSAl Viro * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
697387e3bSAnton Altaparmakov * Copyright (c) 2001-2012 Anton Altaparmakov
79be96f3fSAl Viro * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
89be96f3fSAl Viro *
99be96f3fSAl Viro * Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
109be96f3fSAl Viro */
119be96f3fSAl Viro
129be96f3fSAl Viro #include <linux/slab.h>
139be96f3fSAl Viro #include <linux/pagemap.h>
149be96f3fSAl Viro #include <linux/stringify.h>
159be96f3fSAl Viro #include <linux/kernel.h>
167244ad69SAndy Shevchenko #include <linux/uuid.h>
171442f76dSChristoph Hellwig #include <linux/msdos_partition.h>
187244ad69SAndy Shevchenko
199be96f3fSAl Viro #include "ldm.h"
209be96f3fSAl Viro #include "check.h"
219be96f3fSAl Viro
22210eaaaeSBart Van Assche /*
239be96f3fSAl Viro * ldm_debug/info/error/crit - Output an error message
249be96f3fSAl Viro * @f: A printf format string containing the message
259be96f3fSAl Viro * @...: Variables to substitute into @f
269be96f3fSAl Viro *
279be96f3fSAl Viro * ldm_debug() writes a DEBUG level message to the syslog but only if the
289be96f3fSAl Viro * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
299be96f3fSAl Viro */
309be96f3fSAl Viro #ifndef CONFIG_LDM_DEBUG
319be96f3fSAl Viro #define ldm_debug(...) do {} while (0)
329be96f3fSAl Viro #else
339be96f3fSAl Viro #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
349be96f3fSAl Viro #endif
359be96f3fSAl Viro
369be96f3fSAl Viro #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
379be96f3fSAl Viro #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
389be96f3fSAl Viro #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
399be96f3fSAl Viro
409be96f3fSAl Viro static __printf(3, 4)
_ldm_printk(const char * level,const char * function,const char * fmt,...)419be96f3fSAl Viro void _ldm_printk(const char *level, const char *function, const char *fmt, ...)
429be96f3fSAl Viro {
439be96f3fSAl Viro struct va_format vaf;
449be96f3fSAl Viro va_list args;
459be96f3fSAl Viro
469be96f3fSAl Viro va_start (args, fmt);
479be96f3fSAl Viro
489be96f3fSAl Viro vaf.fmt = fmt;
499be96f3fSAl Viro vaf.va = &args;
509be96f3fSAl Viro
519be96f3fSAl Viro printk("%s%s(): %pV\n", level, function, &vaf);
529be96f3fSAl Viro
539be96f3fSAl Viro va_end(args);
549be96f3fSAl Viro }
559be96f3fSAl Viro
569be96f3fSAl Viro /**
579be96f3fSAl Viro * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
589be96f3fSAl Viro * @data: Raw database PRIVHEAD structure loaded from the device
599be96f3fSAl Viro * @ph: In-memory privhead structure in which to return parsed information
609be96f3fSAl Viro *
619be96f3fSAl Viro * This parses the LDM database PRIVHEAD structure supplied in @data and
629be96f3fSAl Viro * sets up the in-memory privhead structure @ph with the obtained information.
639be96f3fSAl Viro *
649be96f3fSAl Viro * Return: 'true' @ph contains the PRIVHEAD data
659be96f3fSAl Viro * 'false' @ph contents are undefined
669be96f3fSAl Viro */
ldm_parse_privhead(const u8 * data,struct privhead * ph)679be96f3fSAl Viro static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
689be96f3fSAl Viro {
699be96f3fSAl Viro bool is_vista = false;
709be96f3fSAl Viro
719be96f3fSAl Viro BUG_ON(!data || !ph);
729be96f3fSAl Viro if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
739be96f3fSAl Viro ldm_error("Cannot find PRIVHEAD structure. LDM database is"
749be96f3fSAl Viro " corrupt. Aborting.");
759be96f3fSAl Viro return false;
769be96f3fSAl Viro }
779be96f3fSAl Viro ph->ver_major = get_unaligned_be16(data + 0x000C);
789be96f3fSAl Viro ph->ver_minor = get_unaligned_be16(data + 0x000E);
799be96f3fSAl Viro ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
809be96f3fSAl Viro ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
819be96f3fSAl Viro ph->config_start = get_unaligned_be64(data + 0x012B);
829be96f3fSAl Viro ph->config_size = get_unaligned_be64(data + 0x0133);
839be96f3fSAl Viro /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
849be96f3fSAl Viro if (ph->ver_major == 2 && ph->ver_minor == 12)
859be96f3fSAl Viro is_vista = true;
869be96f3fSAl Viro if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
879be96f3fSAl Viro ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
889be96f3fSAl Viro " Aborting.", ph->ver_major, ph->ver_minor);
899be96f3fSAl Viro return false;
909be96f3fSAl Viro }
919be96f3fSAl Viro ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
929be96f3fSAl Viro ph->ver_minor, is_vista ? "Vista" : "2000/XP");
939be96f3fSAl Viro if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
949be96f3fSAl Viro /* Warn the user and continue, carefully. */
959be96f3fSAl Viro ldm_info("Database is normally %u bytes, it claims to "
969be96f3fSAl Viro "be %llu bytes.", LDM_DB_SIZE,
979be96f3fSAl Viro (unsigned long long)ph->config_size);
989be96f3fSAl Viro }
999be96f3fSAl Viro if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
1009be96f3fSAl Viro ph->logical_disk_size > ph->config_start)) {
1019be96f3fSAl Viro ldm_error("PRIVHEAD disk size doesn't match real disk size");
1029be96f3fSAl Viro return false;
1039be96f3fSAl Viro }
10459b9c629SChristoph Hellwig if (uuid_parse(data + 0x0030, &ph->disk_id)) {
1059be96f3fSAl Viro ldm_error("PRIVHEAD contains an invalid GUID.");
1069be96f3fSAl Viro return false;
1079be96f3fSAl Viro }
1089be96f3fSAl Viro ldm_debug("Parsed PRIVHEAD successfully.");
1099be96f3fSAl Viro return true;
1109be96f3fSAl Viro }
1119be96f3fSAl Viro
1129be96f3fSAl Viro /**
1139be96f3fSAl Viro * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
1149be96f3fSAl Viro * @data: Raw database TOCBLOCK structure loaded from the device
1159be96f3fSAl Viro * @toc: In-memory toc structure in which to return parsed information
1169be96f3fSAl Viro *
1179be96f3fSAl Viro * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
1189be96f3fSAl Viro * in @data and sets up the in-memory tocblock structure @toc with the obtained
1199be96f3fSAl Viro * information.
1209be96f3fSAl Viro *
1219be96f3fSAl Viro * N.B. The *_start and *_size values returned in @toc are not range-checked.
1229be96f3fSAl Viro *
1239be96f3fSAl Viro * Return: 'true' @toc contains the TOCBLOCK data
1249be96f3fSAl Viro * 'false' @toc contents are undefined
1259be96f3fSAl Viro */
ldm_parse_tocblock(const u8 * data,struct tocblock * toc)1269be96f3fSAl Viro static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
1279be96f3fSAl Viro {
1289be96f3fSAl Viro BUG_ON (!data || !toc);
1299be96f3fSAl Viro
1309be96f3fSAl Viro if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
1319be96f3fSAl Viro ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
1329be96f3fSAl Viro return false;
1339be96f3fSAl Viro }
1349be96f3fSAl Viro strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
1359be96f3fSAl Viro toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
1369be96f3fSAl Viro toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
1379be96f3fSAl Viro toc->bitmap1_size = get_unaligned_be64(data + 0x36);
1389be96f3fSAl Viro
1399be96f3fSAl Viro if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
1409be96f3fSAl Viro sizeof (toc->bitmap1_name)) != 0) {
1419be96f3fSAl Viro ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
1429be96f3fSAl Viro TOC_BITMAP1, toc->bitmap1_name);
1439be96f3fSAl Viro return false;
1449be96f3fSAl Viro }
1459be96f3fSAl Viro strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
1469be96f3fSAl Viro toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
1479be96f3fSAl Viro toc->bitmap2_start = get_unaligned_be64(data + 0x50);
1489be96f3fSAl Viro toc->bitmap2_size = get_unaligned_be64(data + 0x58);
1499be96f3fSAl Viro if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
1509be96f3fSAl Viro sizeof (toc->bitmap2_name)) != 0) {
1519be96f3fSAl Viro ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
1529be96f3fSAl Viro TOC_BITMAP2, toc->bitmap2_name);
1539be96f3fSAl Viro return false;
1549be96f3fSAl Viro }
1559be96f3fSAl Viro ldm_debug ("Parsed TOCBLOCK successfully.");
1569be96f3fSAl Viro return true;
1579be96f3fSAl Viro }
1589be96f3fSAl Viro
1599be96f3fSAl Viro /**
1609be96f3fSAl Viro * ldm_parse_vmdb - Read the LDM Database VMDB structure
1619be96f3fSAl Viro * @data: Raw database VMDB structure loaded from the device
1629be96f3fSAl Viro * @vm: In-memory vmdb structure in which to return parsed information
1639be96f3fSAl Viro *
1649be96f3fSAl Viro * This parses the LDM Database VMDB structure supplied in @data and sets up
1659be96f3fSAl Viro * the in-memory vmdb structure @vm with the obtained information.
1669be96f3fSAl Viro *
1679be96f3fSAl Viro * N.B. The *_start, *_size and *_seq values will be range-checked later.
1689be96f3fSAl Viro *
1699be96f3fSAl Viro * Return: 'true' @vm contains VMDB info
1709be96f3fSAl Viro * 'false' @vm contents are undefined
1719be96f3fSAl Viro */
ldm_parse_vmdb(const u8 * data,struct vmdb * vm)1729be96f3fSAl Viro static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
1739be96f3fSAl Viro {
1749be96f3fSAl Viro BUG_ON (!data || !vm);
1759be96f3fSAl Viro
1769be96f3fSAl Viro if (MAGIC_VMDB != get_unaligned_be32(data)) {
1779be96f3fSAl Viro ldm_crit ("Cannot find the VMDB, database may be corrupt.");
1789be96f3fSAl Viro return false;
1799be96f3fSAl Viro }
1809be96f3fSAl Viro
1819be96f3fSAl Viro vm->ver_major = get_unaligned_be16(data + 0x12);
1829be96f3fSAl Viro vm->ver_minor = get_unaligned_be16(data + 0x14);
1839be96f3fSAl Viro if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
1849be96f3fSAl Viro ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
1859be96f3fSAl Viro "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
1869be96f3fSAl Viro return false;
1879be96f3fSAl Viro }
1889be96f3fSAl Viro
1899be96f3fSAl Viro vm->vblk_size = get_unaligned_be32(data + 0x08);
1909be96f3fSAl Viro if (vm->vblk_size == 0) {
1919be96f3fSAl Viro ldm_error ("Illegal VBLK size");
1929be96f3fSAl Viro return false;
1939be96f3fSAl Viro }
1949be96f3fSAl Viro
1959be96f3fSAl Viro vm->vblk_offset = get_unaligned_be32(data + 0x0C);
1969be96f3fSAl Viro vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
1979be96f3fSAl Viro
1989be96f3fSAl Viro ldm_debug ("Parsed VMDB successfully.");
1999be96f3fSAl Viro return true;
2009be96f3fSAl Viro }
2019be96f3fSAl Viro
2029be96f3fSAl Viro /**
2039be96f3fSAl Viro * ldm_compare_privheads - Compare two privhead objects
2049be96f3fSAl Viro * @ph1: First privhead
2059be96f3fSAl Viro * @ph2: Second privhead
2069be96f3fSAl Viro *
2079be96f3fSAl Viro * This compares the two privhead structures @ph1 and @ph2.
2089be96f3fSAl Viro *
2099be96f3fSAl Viro * Return: 'true' Identical
2109be96f3fSAl Viro * 'false' Different
2119be96f3fSAl Viro */
ldm_compare_privheads(const struct privhead * ph1,const struct privhead * ph2)2129be96f3fSAl Viro static bool ldm_compare_privheads (const struct privhead *ph1,
2139be96f3fSAl Viro const struct privhead *ph2)
2149be96f3fSAl Viro {
2159be96f3fSAl Viro BUG_ON (!ph1 || !ph2);
2169be96f3fSAl Viro
2179be96f3fSAl Viro return ((ph1->ver_major == ph2->ver_major) &&
2189be96f3fSAl Viro (ph1->ver_minor == ph2->ver_minor) &&
2199be96f3fSAl Viro (ph1->logical_disk_start == ph2->logical_disk_start) &&
2209be96f3fSAl Viro (ph1->logical_disk_size == ph2->logical_disk_size) &&
2219be96f3fSAl Viro (ph1->config_start == ph2->config_start) &&
2229be96f3fSAl Viro (ph1->config_size == ph2->config_size) &&
22359b9c629SChristoph Hellwig uuid_equal(&ph1->disk_id, &ph2->disk_id));
2249be96f3fSAl Viro }
2259be96f3fSAl Viro
2269be96f3fSAl Viro /**
2279be96f3fSAl Viro * ldm_compare_tocblocks - Compare two tocblock objects
2289be96f3fSAl Viro * @toc1: First toc
2299be96f3fSAl Viro * @toc2: Second toc
2309be96f3fSAl Viro *
2319be96f3fSAl Viro * This compares the two tocblock structures @toc1 and @toc2.
2329be96f3fSAl Viro *
2339be96f3fSAl Viro * Return: 'true' Identical
2349be96f3fSAl Viro * 'false' Different
2359be96f3fSAl Viro */
ldm_compare_tocblocks(const struct tocblock * toc1,const struct tocblock * toc2)2369be96f3fSAl Viro static bool ldm_compare_tocblocks (const struct tocblock *toc1,
2379be96f3fSAl Viro const struct tocblock *toc2)
2389be96f3fSAl Viro {
2399be96f3fSAl Viro BUG_ON (!toc1 || !toc2);
2409be96f3fSAl Viro
2419be96f3fSAl Viro return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
2429be96f3fSAl Viro (toc1->bitmap1_size == toc2->bitmap1_size) &&
2439be96f3fSAl Viro (toc1->bitmap2_start == toc2->bitmap2_start) &&
2449be96f3fSAl Viro (toc1->bitmap2_size == toc2->bitmap2_size) &&
2459be96f3fSAl Viro !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
2469be96f3fSAl Viro sizeof (toc1->bitmap1_name)) &&
2479be96f3fSAl Viro !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
2489be96f3fSAl Viro sizeof (toc1->bitmap2_name)));
2499be96f3fSAl Viro }
2509be96f3fSAl Viro
2519be96f3fSAl Viro /**
2529be96f3fSAl Viro * ldm_validate_privheads - Compare the primary privhead with its backups
2539be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
2549be96f3fSAl Viro * @ph1: Memory struct to fill with ph contents
2559be96f3fSAl Viro *
2569be96f3fSAl Viro * Read and compare all three privheads from disk.
2579be96f3fSAl Viro *
2589be96f3fSAl Viro * The privheads on disk show the size and location of the main disk area and
2599be96f3fSAl Viro * the configuration area (the database). The values are range-checked against
2609be96f3fSAl Viro * @hd, which contains the real size of the disk.
2619be96f3fSAl Viro *
2629be96f3fSAl Viro * Return: 'true' Success
2639be96f3fSAl Viro * 'false' Error
2649be96f3fSAl Viro */
ldm_validate_privheads(struct parsed_partitions * state,struct privhead * ph1)2659be96f3fSAl Viro static bool ldm_validate_privheads(struct parsed_partitions *state,
2669be96f3fSAl Viro struct privhead *ph1)
2679be96f3fSAl Viro {
2689be96f3fSAl Viro static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
2699be96f3fSAl Viro struct privhead *ph[3] = { ph1 };
2709be96f3fSAl Viro Sector sect;
2719be96f3fSAl Viro u8 *data;
2729be96f3fSAl Viro bool result = false;
2739be96f3fSAl Viro long num_sects;
2749be96f3fSAl Viro int i;
2759be96f3fSAl Viro
2769be96f3fSAl Viro BUG_ON (!state || !ph1);
2779be96f3fSAl Viro
2789be96f3fSAl Viro ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
2799be96f3fSAl Viro ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
2809be96f3fSAl Viro if (!ph[1] || !ph[2]) {
2819be96f3fSAl Viro ldm_crit ("Out of memory.");
2829be96f3fSAl Viro goto out;
2839be96f3fSAl Viro }
2849be96f3fSAl Viro
2859be96f3fSAl Viro /* off[1 & 2] are relative to ph[0]->config_start */
2869be96f3fSAl Viro ph[0]->config_start = 0;
2879be96f3fSAl Viro
2889be96f3fSAl Viro /* Read and parse privheads */
2899be96f3fSAl Viro for (i = 0; i < 3; i++) {
2909be96f3fSAl Viro data = read_part_sector(state, ph[0]->config_start + off[i],
2919be96f3fSAl Viro §);
2929be96f3fSAl Viro if (!data) {
2939be96f3fSAl Viro ldm_crit ("Disk read failed.");
2949be96f3fSAl Viro goto out;
2959be96f3fSAl Viro }
2969be96f3fSAl Viro result = ldm_parse_privhead (data, ph[i]);
2979be96f3fSAl Viro put_dev_sector (sect);
2989be96f3fSAl Viro if (!result) {
2999be96f3fSAl Viro ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
3009be96f3fSAl Viro if (i < 2)
3019be96f3fSAl Viro goto out; /* Already logged */
3029be96f3fSAl Viro else
3039be96f3fSAl Viro break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
3049be96f3fSAl Viro }
3059be96f3fSAl Viro }
3069be96f3fSAl Viro
307a08aa9bcSChristoph Hellwig num_sects = get_capacity(state->disk);
3089be96f3fSAl Viro
3099be96f3fSAl Viro if ((ph[0]->config_start > num_sects) ||
3109be96f3fSAl Viro ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
3119be96f3fSAl Viro ldm_crit ("Database extends beyond the end of the disk.");
3129be96f3fSAl Viro goto out;
3139be96f3fSAl Viro }
3149be96f3fSAl Viro
3159be96f3fSAl Viro if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
3169be96f3fSAl Viro ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
3179be96f3fSAl Viro > ph[0]->config_start)) {
3189be96f3fSAl Viro ldm_crit ("Disk and database overlap.");
3199be96f3fSAl Viro goto out;
3209be96f3fSAl Viro }
3219be96f3fSAl Viro
3229be96f3fSAl Viro if (!ldm_compare_privheads (ph[0], ph[1])) {
3239be96f3fSAl Viro ldm_crit ("Primary and backup PRIVHEADs don't match.");
3249be96f3fSAl Viro goto out;
3259be96f3fSAl Viro }
3269be96f3fSAl Viro /* FIXME ignore this for now
3279be96f3fSAl Viro if (!ldm_compare_privheads (ph[0], ph[2])) {
3289be96f3fSAl Viro ldm_crit ("Primary and backup PRIVHEADs don't match.");
3299be96f3fSAl Viro goto out;
3309be96f3fSAl Viro }*/
3319be96f3fSAl Viro ldm_debug ("Validated PRIVHEADs successfully.");
3329be96f3fSAl Viro result = true;
3339be96f3fSAl Viro out:
3349be96f3fSAl Viro kfree (ph[1]);
3359be96f3fSAl Viro kfree (ph[2]);
3369be96f3fSAl Viro return result;
3379be96f3fSAl Viro }
3389be96f3fSAl Viro
3399be96f3fSAl Viro /**
3409be96f3fSAl Viro * ldm_validate_tocblocks - Validate the table of contents and its backups
3419be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
342a08aa9bcSChristoph Hellwig * @base: Offset, into @state->disk, of the database
3439be96f3fSAl Viro * @ldb: Cache of the database structures
3449be96f3fSAl Viro *
3459be96f3fSAl Viro * Find and compare the four tables of contents of the LDM Database stored on
346a08aa9bcSChristoph Hellwig * @state->disk and return the parsed information into @toc1.
3479be96f3fSAl Viro *
3489be96f3fSAl Viro * The offsets and sizes of the configs are range-checked against a privhead.
3499be96f3fSAl Viro *
3509be96f3fSAl Viro * Return: 'true' @toc1 contains validated TOCBLOCK info
3519be96f3fSAl Viro * 'false' @toc1 contents are undefined
3529be96f3fSAl Viro */
ldm_validate_tocblocks(struct parsed_partitions * state,unsigned long base,struct ldmdb * ldb)3539be96f3fSAl Viro static bool ldm_validate_tocblocks(struct parsed_partitions *state,
3549be96f3fSAl Viro unsigned long base, struct ldmdb *ldb)
3559be96f3fSAl Viro {
3569be96f3fSAl Viro static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
3579be96f3fSAl Viro struct tocblock *tb[4];
3589be96f3fSAl Viro struct privhead *ph;
3599be96f3fSAl Viro Sector sect;
3609be96f3fSAl Viro u8 *data;
3619be96f3fSAl Viro int i, nr_tbs;
3629be96f3fSAl Viro bool result = false;
3639be96f3fSAl Viro
3649be96f3fSAl Viro BUG_ON(!state || !ldb);
3659be96f3fSAl Viro ph = &ldb->ph;
3669be96f3fSAl Viro tb[0] = &ldb->toc;
3676da2ec56SKees Cook tb[1] = kmalloc_array(3, sizeof(*tb[1]), GFP_KERNEL);
3689be96f3fSAl Viro if (!tb[1]) {
3699be96f3fSAl Viro ldm_crit("Out of memory.");
3709be96f3fSAl Viro goto err;
3719be96f3fSAl Viro }
3729be96f3fSAl Viro tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
3739be96f3fSAl Viro tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
3749be96f3fSAl Viro /*
3759be96f3fSAl Viro * Try to read and parse all four TOCBLOCKs.
3769be96f3fSAl Viro *
3779be96f3fSAl Viro * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
3789be96f3fSAl Viro * skip any that fail as long as we get at least one valid TOCBLOCK.
3799be96f3fSAl Viro */
3809be96f3fSAl Viro for (nr_tbs = i = 0; i < 4; i++) {
3819be96f3fSAl Viro data = read_part_sector(state, base + off[i], §);
3829be96f3fSAl Viro if (!data) {
3839be96f3fSAl Viro ldm_error("Disk read failed for TOCBLOCK %d.", i);
3849be96f3fSAl Viro continue;
3859be96f3fSAl Viro }
3869be96f3fSAl Viro if (ldm_parse_tocblock(data, tb[nr_tbs]))
3879be96f3fSAl Viro nr_tbs++;
3889be96f3fSAl Viro put_dev_sector(sect);
3899be96f3fSAl Viro }
3909be96f3fSAl Viro if (!nr_tbs) {
3919be96f3fSAl Viro ldm_crit("Failed to find a valid TOCBLOCK.");
3929be96f3fSAl Viro goto err;
3939be96f3fSAl Viro }
3949be96f3fSAl Viro /* Range check the TOCBLOCK against a privhead. */
3959be96f3fSAl Viro if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
3969be96f3fSAl Viro ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
3979be96f3fSAl Viro ph->config_size)) {
3989be96f3fSAl Viro ldm_crit("The bitmaps are out of range. Giving up.");
3999be96f3fSAl Viro goto err;
4009be96f3fSAl Viro }
4019be96f3fSAl Viro /* Compare all loaded TOCBLOCKs. */
4029be96f3fSAl Viro for (i = 1; i < nr_tbs; i++) {
4039be96f3fSAl Viro if (!ldm_compare_tocblocks(tb[0], tb[i])) {
4049be96f3fSAl Viro ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
4059be96f3fSAl Viro goto err;
4069be96f3fSAl Viro }
4079be96f3fSAl Viro }
4089be96f3fSAl Viro ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
4099be96f3fSAl Viro result = true;
4109be96f3fSAl Viro err:
4119be96f3fSAl Viro kfree(tb[1]);
4129be96f3fSAl Viro return result;
4139be96f3fSAl Viro }
4149be96f3fSAl Viro
4159be96f3fSAl Viro /**
4169be96f3fSAl Viro * ldm_validate_vmdb - Read the VMDB and validate it
4179be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
4189be96f3fSAl Viro * @base: Offset, into @bdev, of the database
4199be96f3fSAl Viro * @ldb: Cache of the database structures
4209be96f3fSAl Viro *
4219be96f3fSAl Viro * Find the vmdb of the LDM Database stored on @bdev and return the parsed
4229be96f3fSAl Viro * information in @ldb.
4239be96f3fSAl Viro *
4249be96f3fSAl Viro * Return: 'true' @ldb contains validated VBDB info
4259be96f3fSAl Viro * 'false' @ldb contents are undefined
4269be96f3fSAl Viro */
ldm_validate_vmdb(struct parsed_partitions * state,unsigned long base,struct ldmdb * ldb)4279be96f3fSAl Viro static bool ldm_validate_vmdb(struct parsed_partitions *state,
4289be96f3fSAl Viro unsigned long base, struct ldmdb *ldb)
4299be96f3fSAl Viro {
4309be96f3fSAl Viro Sector sect;
4319be96f3fSAl Viro u8 *data;
4329be96f3fSAl Viro bool result = false;
4339be96f3fSAl Viro struct vmdb *vm;
4349be96f3fSAl Viro struct tocblock *toc;
4359be96f3fSAl Viro
4369be96f3fSAl Viro BUG_ON (!state || !ldb);
4379be96f3fSAl Viro
4389be96f3fSAl Viro vm = &ldb->vm;
4399be96f3fSAl Viro toc = &ldb->toc;
4409be96f3fSAl Viro
4419be96f3fSAl Viro data = read_part_sector(state, base + OFF_VMDB, §);
4429be96f3fSAl Viro if (!data) {
4439be96f3fSAl Viro ldm_crit ("Disk read failed.");
4449be96f3fSAl Viro return false;
4459be96f3fSAl Viro }
4469be96f3fSAl Viro
4479be96f3fSAl Viro if (!ldm_parse_vmdb (data, vm))
4489be96f3fSAl Viro goto out; /* Already logged */
4499be96f3fSAl Viro
4509be96f3fSAl Viro /* Are there uncommitted transactions? */
4519be96f3fSAl Viro if (get_unaligned_be16(data + 0x10) != 0x01) {
4529be96f3fSAl Viro ldm_crit ("Database is not in a consistent state. Aborting.");
4539be96f3fSAl Viro goto out;
4549be96f3fSAl Viro }
4559be96f3fSAl Viro
4569be96f3fSAl Viro if (vm->vblk_offset != 512)
4579be96f3fSAl Viro ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
4589be96f3fSAl Viro
4599be96f3fSAl Viro /*
4609be96f3fSAl Viro * The last_vblkd_seq can be before the end of the vmdb, just make sure
4619be96f3fSAl Viro * it is not out of bounds.
4629be96f3fSAl Viro */
4639be96f3fSAl Viro if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
4649be96f3fSAl Viro ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
4659be96f3fSAl Viro "Database is corrupt. Aborting.");
4669be96f3fSAl Viro goto out;
4679be96f3fSAl Viro }
4689be96f3fSAl Viro
4699be96f3fSAl Viro result = true;
4709be96f3fSAl Viro out:
4719be96f3fSAl Viro put_dev_sector (sect);
4729be96f3fSAl Viro return result;
4739be96f3fSAl Viro }
4749be96f3fSAl Viro
4759be96f3fSAl Viro
4769be96f3fSAl Viro /**
4779be96f3fSAl Viro * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
4789be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
4799be96f3fSAl Viro *
4809be96f3fSAl Viro * This function provides a weak test to decide whether the device is a dynamic
4819be96f3fSAl Viro * disk or not. It looks for an MS-DOS-style partition table containing at
4829be96f3fSAl Viro * least one partition of type 0x42 (formerly SFS, now used by Windows for
4839be96f3fSAl Viro * dynamic disks).
4849be96f3fSAl Viro *
4859be96f3fSAl Viro * N.B. The only possible error can come from the read_part_sector and that is
4869be96f3fSAl Viro * only likely to happen if the underlying device is strange. If that IS
4879be96f3fSAl Viro * the case we should return zero to let someone else try.
4889be96f3fSAl Viro *
489a08aa9bcSChristoph Hellwig * Return: 'true' @state->disk is a dynamic disk
490a08aa9bcSChristoph Hellwig * 'false' @state->disk is not a dynamic disk, or an error occurred
4919be96f3fSAl Viro */
ldm_validate_partition_table(struct parsed_partitions * state)4929be96f3fSAl Viro static bool ldm_validate_partition_table(struct parsed_partitions *state)
4939be96f3fSAl Viro {
4949be96f3fSAl Viro Sector sect;
4959be96f3fSAl Viro u8 *data;
4961442f76dSChristoph Hellwig struct msdos_partition *p;
4979be96f3fSAl Viro int i;
4989be96f3fSAl Viro bool result = false;
4999be96f3fSAl Viro
5009be96f3fSAl Viro BUG_ON(!state);
5019be96f3fSAl Viro
5029be96f3fSAl Viro data = read_part_sector(state, 0, §);
5039be96f3fSAl Viro if (!data) {
5049be96f3fSAl Viro ldm_info ("Disk read failed.");
5059be96f3fSAl Viro return false;
5069be96f3fSAl Viro }
5079be96f3fSAl Viro
5089be96f3fSAl Viro if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
5099be96f3fSAl Viro goto out;
5109be96f3fSAl Viro
5111442f76dSChristoph Hellwig p = (struct msdos_partition *)(data + 0x01BE);
5129be96f3fSAl Viro for (i = 0; i < 4; i++, p++)
5131b177499SArnd Bergmann if (p->sys_ind == LDM_PARTITION) {
5149be96f3fSAl Viro result = true;
5159be96f3fSAl Viro break;
5169be96f3fSAl Viro }
5179be96f3fSAl Viro
5189be96f3fSAl Viro if (result)
5199be96f3fSAl Viro ldm_debug ("Found W2K dynamic disk partition type.");
5209be96f3fSAl Viro
5219be96f3fSAl Viro out:
5229be96f3fSAl Viro put_dev_sector (sect);
5239be96f3fSAl Viro return result;
5249be96f3fSAl Viro }
5259be96f3fSAl Viro
5269be96f3fSAl Viro /**
5279be96f3fSAl Viro * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
5289be96f3fSAl Viro * @ldb: Cache of the database structures
5299be96f3fSAl Viro *
5309be96f3fSAl Viro * The LDM Database contains a list of all partitions on all dynamic disks.
5319be96f3fSAl Viro * The primary PRIVHEAD, at the beginning of the physical disk, tells us
5329be96f3fSAl Viro * the GUID of this disk. This function searches for the GUID in a linked
5339be96f3fSAl Viro * list of vblk's.
5349be96f3fSAl Viro *
5359be96f3fSAl Viro * Return: Pointer, A matching vblk was found
5369be96f3fSAl Viro * NULL, No match, or an error
5379be96f3fSAl Viro */
ldm_get_disk_objid(const struct ldmdb * ldb)5389be96f3fSAl Viro static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
5399be96f3fSAl Viro {
5409be96f3fSAl Viro struct list_head *item;
5419be96f3fSAl Viro
5429be96f3fSAl Viro BUG_ON (!ldb);
5439be96f3fSAl Viro
5449be96f3fSAl Viro list_for_each (item, &ldb->v_disk) {
5459be96f3fSAl Viro struct vblk *v = list_entry (item, struct vblk, list);
54659b9c629SChristoph Hellwig if (uuid_equal(&v->vblk.disk.disk_id, &ldb->ph.disk_id))
5479be96f3fSAl Viro return v;
5489be96f3fSAl Viro }
5499be96f3fSAl Viro
5509be96f3fSAl Viro return NULL;
5519be96f3fSAl Viro }
5529be96f3fSAl Viro
5539be96f3fSAl Viro /**
5549be96f3fSAl Viro * ldm_create_data_partitions - Create data partitions for this device
5559be96f3fSAl Viro * @pp: List of the partitions parsed so far
5569be96f3fSAl Viro * @ldb: Cache of the database structures
5579be96f3fSAl Viro *
5589be96f3fSAl Viro * The database contains ALL the partitions for ALL disk groups, so we need to
5599be96f3fSAl Viro * filter out this specific disk. Using the disk's object id, we can find all
5609be96f3fSAl Viro * the partitions in the database that belong to this disk.
5619be96f3fSAl Viro *
5629be96f3fSAl Viro * Add each partition in our database, to the parsed_partitions structure.
5639be96f3fSAl Viro *
5649be96f3fSAl Viro * N.B. This function creates the partitions in the order it finds partition
5659be96f3fSAl Viro * objects in the linked list.
5669be96f3fSAl Viro *
5679be96f3fSAl Viro * Return: 'true' Partition created
5689be96f3fSAl Viro * 'false' Error, probably a range checking problem
5699be96f3fSAl Viro */
ldm_create_data_partitions(struct parsed_partitions * pp,const struct ldmdb * ldb)5709be96f3fSAl Viro static bool ldm_create_data_partitions (struct parsed_partitions *pp,
5719be96f3fSAl Viro const struct ldmdb *ldb)
5729be96f3fSAl Viro {
5739be96f3fSAl Viro struct list_head *item;
5749be96f3fSAl Viro struct vblk *vb;
5759be96f3fSAl Viro struct vblk *disk;
5769be96f3fSAl Viro struct vblk_part *part;
5779be96f3fSAl Viro int part_num = 1;
5789be96f3fSAl Viro
5799be96f3fSAl Viro BUG_ON (!pp || !ldb);
5809be96f3fSAl Viro
5819be96f3fSAl Viro disk = ldm_get_disk_objid (ldb);
5829be96f3fSAl Viro if (!disk) {
5839be96f3fSAl Viro ldm_crit ("Can't find the ID of this disk in the database.");
5849be96f3fSAl Viro return false;
5859be96f3fSAl Viro }
5869be96f3fSAl Viro
5879be96f3fSAl Viro strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
5889be96f3fSAl Viro
5899be96f3fSAl Viro /* Create the data partitions */
5909be96f3fSAl Viro list_for_each (item, &ldb->v_part) {
5919be96f3fSAl Viro vb = list_entry (item, struct vblk, list);
5929be96f3fSAl Viro part = &vb->vblk.part;
5939be96f3fSAl Viro
5949be96f3fSAl Viro if (part->disk_id != disk->obj_id)
5959be96f3fSAl Viro continue;
5969be96f3fSAl Viro
5979be96f3fSAl Viro put_partition (pp, part_num, ldb->ph.logical_disk_start +
5989be96f3fSAl Viro part->start, part->size);
5999be96f3fSAl Viro part_num++;
6009be96f3fSAl Viro }
6019be96f3fSAl Viro
6029be96f3fSAl Viro strlcat(pp->pp_buf, "\n", PAGE_SIZE);
6039be96f3fSAl Viro return true;
6049be96f3fSAl Viro }
6059be96f3fSAl Viro
6069be96f3fSAl Viro
6079be96f3fSAl Viro /**
6089be96f3fSAl Viro * ldm_relative - Calculate the next relative offset
6099be96f3fSAl Viro * @buffer: Block of data being worked on
6109be96f3fSAl Viro * @buflen: Size of the block of data
6119be96f3fSAl Viro * @base: Size of the previous fixed width fields
6129be96f3fSAl Viro * @offset: Cumulative size of the previous variable-width fields
6139be96f3fSAl Viro *
6149be96f3fSAl Viro * Because many of the VBLK fields are variable-width, it's necessary
6159be96f3fSAl Viro * to calculate each offset based on the previous one and the length
6169be96f3fSAl Viro * of the field it pointed to.
6179be96f3fSAl Viro *
6189be96f3fSAl Viro * Return: -1 Error, the calculated offset exceeded the size of the buffer
6199be96f3fSAl Viro * n OK, a range-checked offset into buffer
6209be96f3fSAl Viro */
ldm_relative(const u8 * buffer,int buflen,int base,int offset)6219be96f3fSAl Viro static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
6229be96f3fSAl Viro {
6239be96f3fSAl Viro
6249be96f3fSAl Viro base += offset;
6259be96f3fSAl Viro if (!buffer || offset < 0 || base > buflen) {
6269be96f3fSAl Viro if (!buffer)
6279be96f3fSAl Viro ldm_error("!buffer");
6289be96f3fSAl Viro if (offset < 0)
6299be96f3fSAl Viro ldm_error("offset (%d) < 0", offset);
6309be96f3fSAl Viro if (base > buflen)
6319be96f3fSAl Viro ldm_error("base (%d) > buflen (%d)", base, buflen);
6329be96f3fSAl Viro return -1;
6339be96f3fSAl Viro }
6349be96f3fSAl Viro if (base + buffer[base] >= buflen) {
6359be96f3fSAl Viro ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
6369be96f3fSAl Viro buffer[base], buflen);
6379be96f3fSAl Viro return -1;
6389be96f3fSAl Viro }
6399be96f3fSAl Viro return buffer[base] + offset + 1;
6409be96f3fSAl Viro }
6419be96f3fSAl Viro
6429be96f3fSAl Viro /**
6439be96f3fSAl Viro * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
6449be96f3fSAl Viro * @block: Pointer to the variable-width number to convert
6459be96f3fSAl Viro *
6469be96f3fSAl Viro * Large numbers in the LDM Database are often stored in a packed format. Each
6479be96f3fSAl Viro * number is prefixed by a one byte width marker. All numbers in the database
6489be96f3fSAl Viro * are stored in big-endian byte order. This function reads one of these
6499be96f3fSAl Viro * numbers and returns the result
6509be96f3fSAl Viro *
6519be96f3fSAl Viro * N.B. This function DOES NOT perform any range checking, though the most
6529be96f3fSAl Viro * it will read is eight bytes.
6539be96f3fSAl Viro *
6549be96f3fSAl Viro * Return: n A number
6559be96f3fSAl Viro * 0 Zero, or an error occurred
6569be96f3fSAl Viro */
ldm_get_vnum(const u8 * block)6579be96f3fSAl Viro static u64 ldm_get_vnum (const u8 *block)
6589be96f3fSAl Viro {
6599be96f3fSAl Viro u64 tmp = 0;
6609be96f3fSAl Viro u8 length;
6619be96f3fSAl Viro
6629be96f3fSAl Viro BUG_ON (!block);
6639be96f3fSAl Viro
6649be96f3fSAl Viro length = *block++;
6659be96f3fSAl Viro
6669be96f3fSAl Viro if (length && length <= 8)
6679be96f3fSAl Viro while (length--)
6689be96f3fSAl Viro tmp = (tmp << 8) | *block++;
6699be96f3fSAl Viro else
6709be96f3fSAl Viro ldm_error ("Illegal length %d.", length);
6719be96f3fSAl Viro
6729be96f3fSAl Viro return tmp;
6739be96f3fSAl Viro }
6749be96f3fSAl Viro
6759be96f3fSAl Viro /**
6769be96f3fSAl Viro * ldm_get_vstr - Read a length-prefixed string into a buffer
6779be96f3fSAl Viro * @block: Pointer to the length marker
6789be96f3fSAl Viro * @buffer: Location to copy string to
6799be96f3fSAl Viro * @buflen: Size of the output buffer
6809be96f3fSAl Viro *
6819be96f3fSAl Viro * Many of the strings in the LDM Database are not NULL terminated. Instead
6829be96f3fSAl Viro * they are prefixed by a one byte length marker. This function copies one of
6839be96f3fSAl Viro * these strings into a buffer.
6849be96f3fSAl Viro *
6859be96f3fSAl Viro * N.B. This function DOES NOT perform any range checking on the input.
6869be96f3fSAl Viro * If the buffer is too small, the output will be truncated.
6879be96f3fSAl Viro *
6889be96f3fSAl Viro * Return: 0, Error and @buffer contents are undefined
6899be96f3fSAl Viro * n, String length in characters (excluding NULL)
6909be96f3fSAl Viro * buflen-1, String was truncated.
6919be96f3fSAl Viro */
ldm_get_vstr(const u8 * block,u8 * buffer,int buflen)6929be96f3fSAl Viro static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
6939be96f3fSAl Viro {
6949be96f3fSAl Viro int length;
6959be96f3fSAl Viro
6969be96f3fSAl Viro BUG_ON (!block || !buffer);
6979be96f3fSAl Viro
6989be96f3fSAl Viro length = block[0];
6999be96f3fSAl Viro if (length >= buflen) {
7009be96f3fSAl Viro ldm_error ("Truncating string %d -> %d.", length, buflen);
7019be96f3fSAl Viro length = buflen - 1;
7029be96f3fSAl Viro }
7039be96f3fSAl Viro memcpy (buffer, block + 1, length);
7049be96f3fSAl Viro buffer[length] = 0;
7059be96f3fSAl Viro return length;
7069be96f3fSAl Viro }
7079be96f3fSAl Viro
7089be96f3fSAl Viro
7099be96f3fSAl Viro /**
7109be96f3fSAl Viro * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
7119be96f3fSAl Viro * @buffer: Block of data being worked on
7129be96f3fSAl Viro * @buflen: Size of the block of data
7139be96f3fSAl Viro * @vb: In-memory vblk in which to return information
7149be96f3fSAl Viro *
7159be96f3fSAl Viro * Read a raw VBLK Component object (version 3) into a vblk structure.
7169be96f3fSAl Viro *
7179be96f3fSAl Viro * Return: 'true' @vb contains a Component VBLK
7189be96f3fSAl Viro * 'false' @vb contents are not defined
7199be96f3fSAl Viro */
ldm_parse_cmp3(const u8 * buffer,int buflen,struct vblk * vb)7209be96f3fSAl Viro static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
7219be96f3fSAl Viro {
7229be96f3fSAl Viro int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
7239be96f3fSAl Viro struct vblk_comp *comp;
7249be96f3fSAl Viro
7259be96f3fSAl Viro BUG_ON (!buffer || !vb);
7269be96f3fSAl Viro
7279be96f3fSAl Viro r_objid = ldm_relative (buffer, buflen, 0x18, 0);
7289be96f3fSAl Viro r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
7299be96f3fSAl Viro r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
7309be96f3fSAl Viro r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
7319be96f3fSAl Viro r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
7329be96f3fSAl Viro
7339be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
7349be96f3fSAl Viro r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
7359be96f3fSAl Viro r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
7369be96f3fSAl Viro len = r_cols;
7379be96f3fSAl Viro } else {
7389be96f3fSAl Viro r_stripe = 0;
7399be96f3fSAl Viro len = r_parent;
7409be96f3fSAl Viro }
7419be96f3fSAl Viro if (len < 0)
7429be96f3fSAl Viro return false;
7439be96f3fSAl Viro
7449be96f3fSAl Viro len += VBLK_SIZE_CMP3;
7459be96f3fSAl Viro if (len != get_unaligned_be32(buffer + 0x14))
7469be96f3fSAl Viro return false;
7479be96f3fSAl Viro
7489be96f3fSAl Viro comp = &vb->vblk.comp;
7499be96f3fSAl Viro ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
7509be96f3fSAl Viro sizeof (comp->state));
7519be96f3fSAl Viro comp->type = buffer[0x18 + r_vstate];
7529be96f3fSAl Viro comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
7539be96f3fSAl Viro comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
7549be96f3fSAl Viro comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
7559be96f3fSAl Viro
7569be96f3fSAl Viro return true;
7579be96f3fSAl Viro }
7589be96f3fSAl Viro
7599be96f3fSAl Viro /**
7609be96f3fSAl Viro * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
7619be96f3fSAl Viro * @buffer: Block of data being worked on
7629be96f3fSAl Viro * @buflen: Size of the block of data
7639be96f3fSAl Viro * @vb: In-memory vblk in which to return information
7649be96f3fSAl Viro *
7659be96f3fSAl Viro * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
7669be96f3fSAl Viro *
7679be96f3fSAl Viro * Return: 'true' @vb contains a Disk Group VBLK
7689be96f3fSAl Viro * 'false' @vb contents are not defined
7699be96f3fSAl Viro */
ldm_parse_dgr3(const u8 * buffer,int buflen,struct vblk * vb)7709be96f3fSAl Viro static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
7719be96f3fSAl Viro {
7729be96f3fSAl Viro int r_objid, r_name, r_diskid, r_id1, r_id2, len;
7739be96f3fSAl Viro struct vblk_dgrp *dgrp;
7749be96f3fSAl Viro
7759be96f3fSAl Viro BUG_ON (!buffer || !vb);
7769be96f3fSAl Viro
7779be96f3fSAl Viro r_objid = ldm_relative (buffer, buflen, 0x18, 0);
7789be96f3fSAl Viro r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
7799be96f3fSAl Viro r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
7809be96f3fSAl Viro
7819be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
7829be96f3fSAl Viro r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
7839be96f3fSAl Viro r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
7849be96f3fSAl Viro len = r_id2;
785*e233fe1aSMichal Orzel } else
7869be96f3fSAl Viro len = r_diskid;
7879be96f3fSAl Viro if (len < 0)
7889be96f3fSAl Viro return false;
7899be96f3fSAl Viro
7909be96f3fSAl Viro len += VBLK_SIZE_DGR3;
7919be96f3fSAl Viro if (len != get_unaligned_be32(buffer + 0x14))
7929be96f3fSAl Viro return false;
7939be96f3fSAl Viro
7949be96f3fSAl Viro dgrp = &vb->vblk.dgrp;
7959be96f3fSAl Viro ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
7969be96f3fSAl Viro sizeof (dgrp->disk_id));
7979be96f3fSAl Viro return true;
7989be96f3fSAl Viro }
7999be96f3fSAl Viro
8009be96f3fSAl Viro /**
8019be96f3fSAl Viro * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
8029be96f3fSAl Viro * @buffer: Block of data being worked on
8039be96f3fSAl Viro * @buflen: Size of the block of data
8049be96f3fSAl Viro * @vb: In-memory vblk in which to return information
8059be96f3fSAl Viro *
8069be96f3fSAl Viro * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
8079be96f3fSAl Viro *
8089be96f3fSAl Viro * Return: 'true' @vb contains a Disk Group VBLK
8099be96f3fSAl Viro * 'false' @vb contents are not defined
8109be96f3fSAl Viro */
ldm_parse_dgr4(const u8 * buffer,int buflen,struct vblk * vb)8119be96f3fSAl Viro static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
8129be96f3fSAl Viro {
8139be96f3fSAl Viro char buf[64];
8149be96f3fSAl Viro int r_objid, r_name, r_id1, r_id2, len;
8159be96f3fSAl Viro
8169be96f3fSAl Viro BUG_ON (!buffer || !vb);
8179be96f3fSAl Viro
8189be96f3fSAl Viro r_objid = ldm_relative (buffer, buflen, 0x18, 0);
8199be96f3fSAl Viro r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
8209be96f3fSAl Viro
8219be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
8229be96f3fSAl Viro r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
8239be96f3fSAl Viro r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
8249be96f3fSAl Viro len = r_id2;
825*e233fe1aSMichal Orzel } else
8269be96f3fSAl Viro len = r_name;
8279be96f3fSAl Viro if (len < 0)
8289be96f3fSAl Viro return false;
8299be96f3fSAl Viro
8309be96f3fSAl Viro len += VBLK_SIZE_DGR4;
8319be96f3fSAl Viro if (len != get_unaligned_be32(buffer + 0x14))
8329be96f3fSAl Viro return false;
8339be96f3fSAl Viro
8349be96f3fSAl Viro ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
8359be96f3fSAl Viro return true;
8369be96f3fSAl Viro }
8379be96f3fSAl Viro
8389be96f3fSAl Viro /**
8399be96f3fSAl Viro * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
8409be96f3fSAl Viro * @buffer: Block of data being worked on
8419be96f3fSAl Viro * @buflen: Size of the block of data
8429be96f3fSAl Viro * @vb: In-memory vblk in which to return information
8439be96f3fSAl Viro *
8449be96f3fSAl Viro * Read a raw VBLK Disk object (version 3) into a vblk structure.
8459be96f3fSAl Viro *
8469be96f3fSAl Viro * Return: 'true' @vb contains a Disk VBLK
8479be96f3fSAl Viro * 'false' @vb contents are not defined
8489be96f3fSAl Viro */
ldm_parse_dsk3(const u8 * buffer,int buflen,struct vblk * vb)8499be96f3fSAl Viro static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
8509be96f3fSAl Viro {
8519be96f3fSAl Viro int r_objid, r_name, r_diskid, r_altname, len;
8529be96f3fSAl Viro struct vblk_disk *disk;
8539be96f3fSAl Viro
8549be96f3fSAl Viro BUG_ON (!buffer || !vb);
8559be96f3fSAl Viro
8569be96f3fSAl Viro r_objid = ldm_relative (buffer, buflen, 0x18, 0);
8579be96f3fSAl Viro r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
8589be96f3fSAl Viro r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
8599be96f3fSAl Viro r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
8609be96f3fSAl Viro len = r_altname;
8619be96f3fSAl Viro if (len < 0)
8629be96f3fSAl Viro return false;
8639be96f3fSAl Viro
8649be96f3fSAl Viro len += VBLK_SIZE_DSK3;
8659be96f3fSAl Viro if (len != get_unaligned_be32(buffer + 0x14))
8669be96f3fSAl Viro return false;
8679be96f3fSAl Viro
8689be96f3fSAl Viro disk = &vb->vblk.disk;
8699be96f3fSAl Viro ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
8709be96f3fSAl Viro sizeof (disk->alt_name));
87159b9c629SChristoph Hellwig if (uuid_parse(buffer + 0x19 + r_name, &disk->disk_id))
8729be96f3fSAl Viro return false;
8739be96f3fSAl Viro
8749be96f3fSAl Viro return true;
8759be96f3fSAl Viro }
8769be96f3fSAl Viro
8779be96f3fSAl Viro /**
8789be96f3fSAl Viro * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
8799be96f3fSAl Viro * @buffer: Block of data being worked on
8809be96f3fSAl Viro * @buflen: Size of the block of data
8819be96f3fSAl Viro * @vb: In-memory vblk in which to return information
8829be96f3fSAl Viro *
8839be96f3fSAl Viro * Read a raw VBLK Disk object (version 4) into a vblk structure.
8849be96f3fSAl Viro *
8859be96f3fSAl Viro * Return: 'true' @vb contains a Disk VBLK
8869be96f3fSAl Viro * 'false' @vb contents are not defined
8879be96f3fSAl Viro */
ldm_parse_dsk4(const u8 * buffer,int buflen,struct vblk * vb)8889be96f3fSAl Viro static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
8899be96f3fSAl Viro {
8909be96f3fSAl Viro int r_objid, r_name, len;
8919be96f3fSAl Viro struct vblk_disk *disk;
8929be96f3fSAl Viro
8939be96f3fSAl Viro BUG_ON (!buffer || !vb);
8949be96f3fSAl Viro
8959be96f3fSAl Viro r_objid = ldm_relative (buffer, buflen, 0x18, 0);
8969be96f3fSAl Viro r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
8979be96f3fSAl Viro len = r_name;
8989be96f3fSAl Viro if (len < 0)
8999be96f3fSAl Viro return false;
9009be96f3fSAl Viro
9019be96f3fSAl Viro len += VBLK_SIZE_DSK4;
9029be96f3fSAl Viro if (len != get_unaligned_be32(buffer + 0x14))
9039be96f3fSAl Viro return false;
9049be96f3fSAl Viro
9059be96f3fSAl Viro disk = &vb->vblk.disk;
906bc163c20SAndy Shevchenko import_uuid(&disk->disk_id, buffer + 0x18 + r_name);
9079be96f3fSAl Viro return true;
9089be96f3fSAl Viro }
9099be96f3fSAl Viro
9109be96f3fSAl Viro /**
9119be96f3fSAl Viro * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
9129be96f3fSAl Viro * @buffer: Block of data being worked on
9139be96f3fSAl Viro * @buflen: Size of the block of data
9149be96f3fSAl Viro * @vb: In-memory vblk in which to return information
9159be96f3fSAl Viro *
9169be96f3fSAl Viro * Read a raw VBLK Partition object (version 3) into a vblk structure.
9179be96f3fSAl Viro *
9189be96f3fSAl Viro * Return: 'true' @vb contains a Partition VBLK
9199be96f3fSAl Viro * 'false' @vb contents are not defined
9209be96f3fSAl Viro */
ldm_parse_prt3(const u8 * buffer,int buflen,struct vblk * vb)9219be96f3fSAl Viro static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
9229be96f3fSAl Viro {
9239be96f3fSAl Viro int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
9249be96f3fSAl Viro struct vblk_part *part;
9259be96f3fSAl Viro
9269be96f3fSAl Viro BUG_ON(!buffer || !vb);
9279be96f3fSAl Viro r_objid = ldm_relative(buffer, buflen, 0x18, 0);
9289be96f3fSAl Viro if (r_objid < 0) {
9299be96f3fSAl Viro ldm_error("r_objid %d < 0", r_objid);
9309be96f3fSAl Viro return false;
9319be96f3fSAl Viro }
9329be96f3fSAl Viro r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
9339be96f3fSAl Viro if (r_name < 0) {
9349be96f3fSAl Viro ldm_error("r_name %d < 0", r_name);
9359be96f3fSAl Viro return false;
9369be96f3fSAl Viro }
9379be96f3fSAl Viro r_size = ldm_relative(buffer, buflen, 0x34, r_name);
9389be96f3fSAl Viro if (r_size < 0) {
9399be96f3fSAl Viro ldm_error("r_size %d < 0", r_size);
9409be96f3fSAl Viro return false;
9419be96f3fSAl Viro }
9429be96f3fSAl Viro r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
9439be96f3fSAl Viro if (r_parent < 0) {
9449be96f3fSAl Viro ldm_error("r_parent %d < 0", r_parent);
9459be96f3fSAl Viro return false;
9469be96f3fSAl Viro }
9479be96f3fSAl Viro r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
9489be96f3fSAl Viro if (r_diskid < 0) {
9499be96f3fSAl Viro ldm_error("r_diskid %d < 0", r_diskid);
9509be96f3fSAl Viro return false;
9519be96f3fSAl Viro }
9529be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
9539be96f3fSAl Viro r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
9549be96f3fSAl Viro if (r_index < 0) {
9559be96f3fSAl Viro ldm_error("r_index %d < 0", r_index);
9569be96f3fSAl Viro return false;
9579be96f3fSAl Viro }
9589be96f3fSAl Viro len = r_index;
959*e233fe1aSMichal Orzel } else
9609be96f3fSAl Viro len = r_diskid;
9619be96f3fSAl Viro if (len < 0) {
9629be96f3fSAl Viro ldm_error("len %d < 0", len);
9639be96f3fSAl Viro return false;
9649be96f3fSAl Viro }
9659be96f3fSAl Viro len += VBLK_SIZE_PRT3;
9669be96f3fSAl Viro if (len > get_unaligned_be32(buffer + 0x14)) {
9679be96f3fSAl Viro ldm_error("len %d > BE32(buffer + 0x14) %d", len,
9689be96f3fSAl Viro get_unaligned_be32(buffer + 0x14));
9699be96f3fSAl Viro return false;
9709be96f3fSAl Viro }
9719be96f3fSAl Viro part = &vb->vblk.part;
9729be96f3fSAl Viro part->start = get_unaligned_be64(buffer + 0x24 + r_name);
9739be96f3fSAl Viro part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
9749be96f3fSAl Viro part->size = ldm_get_vnum(buffer + 0x34 + r_name);
9759be96f3fSAl Viro part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
9769be96f3fSAl Viro part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
9779be96f3fSAl Viro if (vb->flags & VBLK_FLAG_PART_INDEX)
9789be96f3fSAl Viro part->partnum = buffer[0x35 + r_diskid];
9799be96f3fSAl Viro else
9809be96f3fSAl Viro part->partnum = 0;
9819be96f3fSAl Viro return true;
9829be96f3fSAl Viro }
9839be96f3fSAl Viro
9849be96f3fSAl Viro /**
9859be96f3fSAl Viro * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
9869be96f3fSAl Viro * @buffer: Block of data being worked on
9879be96f3fSAl Viro * @buflen: Size of the block of data
9889be96f3fSAl Viro * @vb: In-memory vblk in which to return information
9899be96f3fSAl Viro *
9909be96f3fSAl Viro * Read a raw VBLK Volume object (version 5) into a vblk structure.
9919be96f3fSAl Viro *
9929be96f3fSAl Viro * Return: 'true' @vb contains a Volume VBLK
9939be96f3fSAl Viro * 'false' @vb contents are not defined
9949be96f3fSAl Viro */
ldm_parse_vol5(const u8 * buffer,int buflen,struct vblk * vb)9959be96f3fSAl Viro static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
9969be96f3fSAl Viro {
9979be96f3fSAl Viro int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
9989be96f3fSAl Viro int r_id1, r_id2, r_size2, r_drive, len;
9999be96f3fSAl Viro struct vblk_volu *volu;
10009be96f3fSAl Viro
10019be96f3fSAl Viro BUG_ON(!buffer || !vb);
10029be96f3fSAl Viro r_objid = ldm_relative(buffer, buflen, 0x18, 0);
10039be96f3fSAl Viro if (r_objid < 0) {
10049be96f3fSAl Viro ldm_error("r_objid %d < 0", r_objid);
10059be96f3fSAl Viro return false;
10069be96f3fSAl Viro }
10079be96f3fSAl Viro r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
10089be96f3fSAl Viro if (r_name < 0) {
10099be96f3fSAl Viro ldm_error("r_name %d < 0", r_name);
10109be96f3fSAl Viro return false;
10119be96f3fSAl Viro }
10129be96f3fSAl Viro r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
10139be96f3fSAl Viro if (r_vtype < 0) {
10149be96f3fSAl Viro ldm_error("r_vtype %d < 0", r_vtype);
10159be96f3fSAl Viro return false;
10169be96f3fSAl Viro }
10179be96f3fSAl Viro r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
10189be96f3fSAl Viro if (r_disable_drive_letter < 0) {
10199be96f3fSAl Viro ldm_error("r_disable_drive_letter %d < 0",
10209be96f3fSAl Viro r_disable_drive_letter);
10219be96f3fSAl Viro return false;
10229be96f3fSAl Viro }
10239be96f3fSAl Viro r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
10249be96f3fSAl Viro if (r_child < 0) {
10259be96f3fSAl Viro ldm_error("r_child %d < 0", r_child);
10269be96f3fSAl Viro return false;
10279be96f3fSAl Viro }
10289be96f3fSAl Viro r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
10299be96f3fSAl Viro if (r_size < 0) {
10309be96f3fSAl Viro ldm_error("r_size %d < 0", r_size);
10319be96f3fSAl Viro return false;
10329be96f3fSAl Viro }
10339be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
10349be96f3fSAl Viro r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
10359be96f3fSAl Viro if (r_id1 < 0) {
10369be96f3fSAl Viro ldm_error("r_id1 %d < 0", r_id1);
10379be96f3fSAl Viro return false;
10389be96f3fSAl Viro }
10399be96f3fSAl Viro } else
10409be96f3fSAl Viro r_id1 = r_size;
10419be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
10429be96f3fSAl Viro r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
10439be96f3fSAl Viro if (r_id2 < 0) {
10449be96f3fSAl Viro ldm_error("r_id2 %d < 0", r_id2);
10459be96f3fSAl Viro return false;
10469be96f3fSAl Viro }
10479be96f3fSAl Viro } else
10489be96f3fSAl Viro r_id2 = r_id1;
10499be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
10509be96f3fSAl Viro r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
10519be96f3fSAl Viro if (r_size2 < 0) {
10529be96f3fSAl Viro ldm_error("r_size2 %d < 0", r_size2);
10539be96f3fSAl Viro return false;
10549be96f3fSAl Viro }
10559be96f3fSAl Viro } else
10569be96f3fSAl Viro r_size2 = r_id2;
10579be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
10589be96f3fSAl Viro r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
10599be96f3fSAl Viro if (r_drive < 0) {
10609be96f3fSAl Viro ldm_error("r_drive %d < 0", r_drive);
10619be96f3fSAl Viro return false;
10629be96f3fSAl Viro }
10639be96f3fSAl Viro } else
10649be96f3fSAl Viro r_drive = r_size2;
10659be96f3fSAl Viro len = r_drive;
10669be96f3fSAl Viro if (len < 0) {
10679be96f3fSAl Viro ldm_error("len %d < 0", len);
10689be96f3fSAl Viro return false;
10699be96f3fSAl Viro }
10709be96f3fSAl Viro len += VBLK_SIZE_VOL5;
10719be96f3fSAl Viro if (len > get_unaligned_be32(buffer + 0x14)) {
10729be96f3fSAl Viro ldm_error("len %d > BE32(buffer + 0x14) %d", len,
10739be96f3fSAl Viro get_unaligned_be32(buffer + 0x14));
10749be96f3fSAl Viro return false;
10759be96f3fSAl Viro }
10769be96f3fSAl Viro volu = &vb->vblk.volu;
10779be96f3fSAl Viro ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
10789be96f3fSAl Viro sizeof(volu->volume_type));
10799be96f3fSAl Viro memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
10809be96f3fSAl Viro sizeof(volu->volume_state));
10819be96f3fSAl Viro volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
10829be96f3fSAl Viro volu->partition_type = buffer[0x41 + r_size];
10839be96f3fSAl Viro memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
10849be96f3fSAl Viro if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
10859be96f3fSAl Viro ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
10869be96f3fSAl Viro sizeof(volu->drive_hint));
10879be96f3fSAl Viro }
10889be96f3fSAl Viro return true;
10899be96f3fSAl Viro }
10909be96f3fSAl Viro
10919be96f3fSAl Viro /**
10929be96f3fSAl Viro * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
10939be96f3fSAl Viro * @buf: Block of data being worked on
10949be96f3fSAl Viro * @len: Size of the block of data
10959be96f3fSAl Viro * @vb: In-memory vblk in which to return information
10969be96f3fSAl Viro *
10979be96f3fSAl Viro * Read a raw VBLK object into a vblk structure. This function just reads the
10989be96f3fSAl Viro * information common to all VBLK types, then delegates the rest of the work to
10999be96f3fSAl Viro * helper functions: ldm_parse_*.
11009be96f3fSAl Viro *
11019be96f3fSAl Viro * Return: 'true' @vb contains a VBLK
11029be96f3fSAl Viro * 'false' @vb contents are not defined
11039be96f3fSAl Viro */
ldm_parse_vblk(const u8 * buf,int len,struct vblk * vb)11049be96f3fSAl Viro static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
11059be96f3fSAl Viro {
11069be96f3fSAl Viro bool result = false;
11079be96f3fSAl Viro int r_objid;
11089be96f3fSAl Viro
11099be96f3fSAl Viro BUG_ON (!buf || !vb);
11109be96f3fSAl Viro
11119be96f3fSAl Viro r_objid = ldm_relative (buf, len, 0x18, 0);
11129be96f3fSAl Viro if (r_objid < 0) {
11139be96f3fSAl Viro ldm_error ("VBLK header is corrupt.");
11149be96f3fSAl Viro return false;
11159be96f3fSAl Viro }
11169be96f3fSAl Viro
11179be96f3fSAl Viro vb->flags = buf[0x12];
11189be96f3fSAl Viro vb->type = buf[0x13];
11199be96f3fSAl Viro vb->obj_id = ldm_get_vnum (buf + 0x18);
11209be96f3fSAl Viro ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
11219be96f3fSAl Viro
11229be96f3fSAl Viro switch (vb->type) {
11239be96f3fSAl Viro case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
11249be96f3fSAl Viro case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
11259be96f3fSAl Viro case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
11269be96f3fSAl Viro case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
11279be96f3fSAl Viro case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
11289be96f3fSAl Viro case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
11299be96f3fSAl Viro case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
11309be96f3fSAl Viro }
11319be96f3fSAl Viro
11329be96f3fSAl Viro if (result)
11339be96f3fSAl Viro ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
11349be96f3fSAl Viro (unsigned long long) vb->obj_id, vb->type);
11359be96f3fSAl Viro else
11369be96f3fSAl Viro ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
11379be96f3fSAl Viro (unsigned long long) vb->obj_id, vb->type);
11389be96f3fSAl Viro
11399be96f3fSAl Viro return result;
11409be96f3fSAl Viro }
11419be96f3fSAl Viro
11429be96f3fSAl Viro
11439be96f3fSAl Viro /**
11449be96f3fSAl Viro * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
11459be96f3fSAl Viro * @data: Raw VBLK to add to the database
11469be96f3fSAl Viro * @len: Size of the raw VBLK
11479be96f3fSAl Viro * @ldb: Cache of the database structures
11489be96f3fSAl Viro *
11499be96f3fSAl Viro * The VBLKs are sorted into categories. Partitions are also sorted by offset.
11509be96f3fSAl Viro *
11519be96f3fSAl Viro * N.B. This function does not check the validity of the VBLKs.
11529be96f3fSAl Viro *
11539be96f3fSAl Viro * Return: 'true' The VBLK was added
11549be96f3fSAl Viro * 'false' An error occurred
11559be96f3fSAl Viro */
ldm_ldmdb_add(u8 * data,int len,struct ldmdb * ldb)11569be96f3fSAl Viro static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
11579be96f3fSAl Viro {
11589be96f3fSAl Viro struct vblk *vb;
11599be96f3fSAl Viro struct list_head *item;
11609be96f3fSAl Viro
11619be96f3fSAl Viro BUG_ON (!data || !ldb);
11629be96f3fSAl Viro
11639be96f3fSAl Viro vb = kmalloc (sizeof (*vb), GFP_KERNEL);
11649be96f3fSAl Viro if (!vb) {
11659be96f3fSAl Viro ldm_crit ("Out of memory.");
11669be96f3fSAl Viro return false;
11679be96f3fSAl Viro }
11689be96f3fSAl Viro
11699be96f3fSAl Viro if (!ldm_parse_vblk (data, len, vb)) {
11709be96f3fSAl Viro kfree(vb);
11719be96f3fSAl Viro return false; /* Already logged */
11729be96f3fSAl Viro }
11739be96f3fSAl Viro
11749be96f3fSAl Viro /* Put vblk into the correct list. */
11759be96f3fSAl Viro switch (vb->type) {
11769be96f3fSAl Viro case VBLK_DGR3:
11779be96f3fSAl Viro case VBLK_DGR4:
11789be96f3fSAl Viro list_add (&vb->list, &ldb->v_dgrp);
11799be96f3fSAl Viro break;
11809be96f3fSAl Viro case VBLK_DSK3:
11819be96f3fSAl Viro case VBLK_DSK4:
11829be96f3fSAl Viro list_add (&vb->list, &ldb->v_disk);
11839be96f3fSAl Viro break;
11849be96f3fSAl Viro case VBLK_VOL5:
11859be96f3fSAl Viro list_add (&vb->list, &ldb->v_volu);
11869be96f3fSAl Viro break;
11879be96f3fSAl Viro case VBLK_CMP3:
11889be96f3fSAl Viro list_add (&vb->list, &ldb->v_comp);
11899be96f3fSAl Viro break;
11909be96f3fSAl Viro case VBLK_PRT3:
11919be96f3fSAl Viro /* Sort by the partition's start sector. */
11929be96f3fSAl Viro list_for_each (item, &ldb->v_part) {
11939be96f3fSAl Viro struct vblk *v = list_entry (item, struct vblk, list);
11949be96f3fSAl Viro if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
11959be96f3fSAl Viro (v->vblk.part.start > vb->vblk.part.start)) {
11969be96f3fSAl Viro list_add_tail (&vb->list, &v->list);
11979be96f3fSAl Viro return true;
11989be96f3fSAl Viro }
11999be96f3fSAl Viro }
12009be96f3fSAl Viro list_add_tail (&vb->list, &ldb->v_part);
12019be96f3fSAl Viro break;
12029be96f3fSAl Viro }
12039be96f3fSAl Viro return true;
12049be96f3fSAl Viro }
12059be96f3fSAl Viro
12069be96f3fSAl Viro /**
12079be96f3fSAl Viro * ldm_frag_add - Add a VBLK fragment to a list
12089be96f3fSAl Viro * @data: Raw fragment to be added to the list
12099be96f3fSAl Viro * @size: Size of the raw fragment
12109be96f3fSAl Viro * @frags: Linked list of VBLK fragments
12119be96f3fSAl Viro *
12129be96f3fSAl Viro * Fragmented VBLKs may not be consecutive in the database, so they are placed
12139be96f3fSAl Viro * in a list so they can be pieced together later.
12149be96f3fSAl Viro *
12159be96f3fSAl Viro * Return: 'true' Success, the VBLK was added to the list
12169be96f3fSAl Viro * 'false' Error, a problem occurred
12179be96f3fSAl Viro */
ldm_frag_add(const u8 * data,int size,struct list_head * frags)12189be96f3fSAl Viro static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
12199be96f3fSAl Viro {
12209be96f3fSAl Viro struct frag *f;
12219be96f3fSAl Viro struct list_head *item;
12229be96f3fSAl Viro int rec, num, group;
12239be96f3fSAl Viro
12249be96f3fSAl Viro BUG_ON (!data || !frags);
12259be96f3fSAl Viro
12269be96f3fSAl Viro if (size < 2 * VBLK_SIZE_HEAD) {
12275336da37SColin Ian King ldm_error("Value of size is too small.");
12289be96f3fSAl Viro return false;
12299be96f3fSAl Viro }
12309be96f3fSAl Viro
12319be96f3fSAl Viro group = get_unaligned_be32(data + 0x08);
12329be96f3fSAl Viro rec = get_unaligned_be16(data + 0x0C);
12339be96f3fSAl Viro num = get_unaligned_be16(data + 0x0E);
12349be96f3fSAl Viro if ((num < 1) || (num > 4)) {
12359be96f3fSAl Viro ldm_error ("A VBLK claims to have %d parts.", num);
12369be96f3fSAl Viro return false;
12379be96f3fSAl Viro }
12389be96f3fSAl Viro if (rec >= num) {
12399be96f3fSAl Viro ldm_error("REC value (%d) exceeds NUM value (%d)", rec, num);
12409be96f3fSAl Viro return false;
12419be96f3fSAl Viro }
12429be96f3fSAl Viro
12439be96f3fSAl Viro list_for_each (item, frags) {
12449be96f3fSAl Viro f = list_entry (item, struct frag, list);
12459be96f3fSAl Viro if (f->group == group)
12469be96f3fSAl Viro goto found;
12479be96f3fSAl Viro }
12489be96f3fSAl Viro
12499be96f3fSAl Viro f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
12509be96f3fSAl Viro if (!f) {
12519be96f3fSAl Viro ldm_crit ("Out of memory.");
12529be96f3fSAl Viro return false;
12539be96f3fSAl Viro }
12549be96f3fSAl Viro
12559be96f3fSAl Viro f->group = group;
12569be96f3fSAl Viro f->num = num;
12579be96f3fSAl Viro f->rec = rec;
12589be96f3fSAl Viro f->map = 0xFF << num;
12599be96f3fSAl Viro
12609be96f3fSAl Viro list_add_tail (&f->list, frags);
12619be96f3fSAl Viro found:
12629be96f3fSAl Viro if (rec >= f->num) {
12639be96f3fSAl Viro ldm_error("REC value (%d) exceeds NUM value (%d)", rec, f->num);
12649be96f3fSAl Viro return false;
12659be96f3fSAl Viro }
12669be96f3fSAl Viro if (f->map & (1 << rec)) {
12679be96f3fSAl Viro ldm_error ("Duplicate VBLK, part %d.", rec);
12689be96f3fSAl Viro f->map &= 0x7F; /* Mark the group as broken */
12699be96f3fSAl Viro return false;
12709be96f3fSAl Viro }
12719be96f3fSAl Viro f->map |= (1 << rec);
127297387e3bSAnton Altaparmakov if (!rec)
127397387e3bSAnton Altaparmakov memcpy(f->data, data, VBLK_SIZE_HEAD);
12749be96f3fSAl Viro data += VBLK_SIZE_HEAD;
12759be96f3fSAl Viro size -= VBLK_SIZE_HEAD;
127697387e3bSAnton Altaparmakov memcpy(f->data + VBLK_SIZE_HEAD + rec * size, data, size);
12779be96f3fSAl Viro return true;
12789be96f3fSAl Viro }
12799be96f3fSAl Viro
12809be96f3fSAl Viro /**
12819be96f3fSAl Viro * ldm_frag_free - Free a linked list of VBLK fragments
12829be96f3fSAl Viro * @list: Linked list of fragments
12839be96f3fSAl Viro *
12849be96f3fSAl Viro * Free a linked list of VBLK fragments
12859be96f3fSAl Viro *
12869be96f3fSAl Viro * Return: none
12879be96f3fSAl Viro */
ldm_frag_free(struct list_head * list)12889be96f3fSAl Viro static void ldm_frag_free (struct list_head *list)
12899be96f3fSAl Viro {
12909be96f3fSAl Viro struct list_head *item, *tmp;
12919be96f3fSAl Viro
12929be96f3fSAl Viro BUG_ON (!list);
12939be96f3fSAl Viro
12949be96f3fSAl Viro list_for_each_safe (item, tmp, list)
12959be96f3fSAl Viro kfree (list_entry (item, struct frag, list));
12969be96f3fSAl Viro }
12979be96f3fSAl Viro
12989be96f3fSAl Viro /**
12999be96f3fSAl Viro * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
13009be96f3fSAl Viro * @frags: Linked list of VBLK fragments
13019be96f3fSAl Viro * @ldb: Cache of the database structures
13029be96f3fSAl Viro *
13039be96f3fSAl Viro * Now that all the fragmented VBLKs have been collected, they must be added to
13049be96f3fSAl Viro * the database for later use.
13059be96f3fSAl Viro *
13069be96f3fSAl Viro * Return: 'true' All the fragments we added successfully
13079be96f3fSAl Viro * 'false' One or more of the fragments we invalid
13089be96f3fSAl Viro */
ldm_frag_commit(struct list_head * frags,struct ldmdb * ldb)13099be96f3fSAl Viro static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
13109be96f3fSAl Viro {
13119be96f3fSAl Viro struct frag *f;
13129be96f3fSAl Viro struct list_head *item;
13139be96f3fSAl Viro
13149be96f3fSAl Viro BUG_ON (!frags || !ldb);
13159be96f3fSAl Viro
13169be96f3fSAl Viro list_for_each (item, frags) {
13179be96f3fSAl Viro f = list_entry (item, struct frag, list);
13189be96f3fSAl Viro
13199be96f3fSAl Viro if (f->map != 0xFF) {
13209be96f3fSAl Viro ldm_error ("VBLK group %d is incomplete (0x%02x).",
13219be96f3fSAl Viro f->group, f->map);
13229be96f3fSAl Viro return false;
13239be96f3fSAl Viro }
13249be96f3fSAl Viro
13259be96f3fSAl Viro if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
13269be96f3fSAl Viro return false; /* Already logged */
13279be96f3fSAl Viro }
13289be96f3fSAl Viro return true;
13299be96f3fSAl Viro }
13309be96f3fSAl Viro
13319be96f3fSAl Viro /**
13329be96f3fSAl Viro * ldm_get_vblks - Read the on-disk database of VBLKs into memory
13339be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
1334a08aa9bcSChristoph Hellwig * @base: Offset, into @state->disk, of the database
13359be96f3fSAl Viro * @ldb: Cache of the database structures
13369be96f3fSAl Viro *
13379be96f3fSAl Viro * To use the information from the VBLKs, they need to be read from the disk,
13389be96f3fSAl Viro * unpacked and validated. We cache them in @ldb according to their type.
13399be96f3fSAl Viro *
13409be96f3fSAl Viro * Return: 'true' All the VBLKs were read successfully
13419be96f3fSAl Viro * 'false' An error occurred
13429be96f3fSAl Viro */
ldm_get_vblks(struct parsed_partitions * state,unsigned long base,struct ldmdb * ldb)13439be96f3fSAl Viro static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
13449be96f3fSAl Viro struct ldmdb *ldb)
13459be96f3fSAl Viro {
13469be96f3fSAl Viro int size, perbuf, skip, finish, s, v, recs;
13479be96f3fSAl Viro u8 *data = NULL;
13489be96f3fSAl Viro Sector sect;
13499be96f3fSAl Viro bool result = false;
13509be96f3fSAl Viro LIST_HEAD (frags);
13519be96f3fSAl Viro
13529be96f3fSAl Viro BUG_ON(!state || !ldb);
13539be96f3fSAl Viro
13549be96f3fSAl Viro size = ldb->vm.vblk_size;
13559be96f3fSAl Viro perbuf = 512 / size;
13569be96f3fSAl Viro skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
13579be96f3fSAl Viro finish = (size * ldb->vm.last_vblk_seq) >> 9;
13589be96f3fSAl Viro
13599be96f3fSAl Viro for (s = skip; s < finish; s++) { /* For each sector */
13609be96f3fSAl Viro data = read_part_sector(state, base + OFF_VMDB + s, §);
13619be96f3fSAl Viro if (!data) {
13629be96f3fSAl Viro ldm_crit ("Disk read failed.");
13639be96f3fSAl Viro goto out;
13649be96f3fSAl Viro }
13659be96f3fSAl Viro
13669be96f3fSAl Viro for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
13679be96f3fSAl Viro if (MAGIC_VBLK != get_unaligned_be32(data)) {
13689be96f3fSAl Viro ldm_error ("Expected to find a VBLK.");
13699be96f3fSAl Viro goto out;
13709be96f3fSAl Viro }
13719be96f3fSAl Viro
13729be96f3fSAl Viro recs = get_unaligned_be16(data + 0x0E); /* Number of records */
13739be96f3fSAl Viro if (recs == 1) {
13749be96f3fSAl Viro if (!ldm_ldmdb_add (data, size, ldb))
13759be96f3fSAl Viro goto out; /* Already logged */
13769be96f3fSAl Viro } else if (recs > 1) {
13779be96f3fSAl Viro if (!ldm_frag_add (data, size, &frags))
13789be96f3fSAl Viro goto out; /* Already logged */
13799be96f3fSAl Viro }
13809be96f3fSAl Viro /* else Record is not in use, ignore it. */
13819be96f3fSAl Viro }
13829be96f3fSAl Viro put_dev_sector (sect);
13839be96f3fSAl Viro data = NULL;
13849be96f3fSAl Viro }
13859be96f3fSAl Viro
13869be96f3fSAl Viro result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
13879be96f3fSAl Viro out:
13889be96f3fSAl Viro if (data)
13899be96f3fSAl Viro put_dev_sector (sect);
13909be96f3fSAl Viro ldm_frag_free (&frags);
13919be96f3fSAl Viro
13929be96f3fSAl Viro return result;
13939be96f3fSAl Viro }
13949be96f3fSAl Viro
13959be96f3fSAl Viro /**
13969be96f3fSAl Viro * ldm_free_vblks - Free a linked list of vblk's
13979be96f3fSAl Viro * @lh: Head of a linked list of struct vblk
13989be96f3fSAl Viro *
13999be96f3fSAl Viro * Free a list of vblk's and free the memory used to maintain the list.
14009be96f3fSAl Viro *
14019be96f3fSAl Viro * Return: none
14029be96f3fSAl Viro */
ldm_free_vblks(struct list_head * lh)14039be96f3fSAl Viro static void ldm_free_vblks (struct list_head *lh)
14049be96f3fSAl Viro {
14059be96f3fSAl Viro struct list_head *item, *tmp;
14069be96f3fSAl Viro
14079be96f3fSAl Viro BUG_ON (!lh);
14089be96f3fSAl Viro
14099be96f3fSAl Viro list_for_each_safe (item, tmp, lh)
14109be96f3fSAl Viro kfree (list_entry (item, struct vblk, list));
14119be96f3fSAl Viro }
14129be96f3fSAl Viro
14139be96f3fSAl Viro
14149be96f3fSAl Viro /**
14159be96f3fSAl Viro * ldm_partition - Find out whether a device is a dynamic disk and handle it
14169be96f3fSAl Viro * @state: Partition check state including device holding the LDM Database
14179be96f3fSAl Viro *
14189be96f3fSAl Viro * This determines whether the device @bdev is a dynamic disk and if so creates
14199be96f3fSAl Viro * the partitions necessary in the gendisk structure pointed to by @hd.
14209be96f3fSAl Viro *
14219be96f3fSAl Viro * We create a dummy device 1, which contains the LDM database, and then create
14229be96f3fSAl Viro * each partition described by the LDM database in sequence as devices 2+. For
14239be96f3fSAl Viro * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
14249be96f3fSAl Viro * and so on: the actual data containing partitions.
14259be96f3fSAl Viro *
1426a08aa9bcSChristoph Hellwig * Return: 1 Success, @state->disk is a dynamic disk and we handled it
1427a08aa9bcSChristoph Hellwig * 0 Success, @state->disk is not a dynamic disk
14289be96f3fSAl Viro * -1 An error occurred before enough information had been read
1429a08aa9bcSChristoph Hellwig * Or @state->disk is a dynamic disk, but it may be corrupted
14309be96f3fSAl Viro */
ldm_partition(struct parsed_partitions * state)14319be96f3fSAl Viro int ldm_partition(struct parsed_partitions *state)
14329be96f3fSAl Viro {
14339be96f3fSAl Viro struct ldmdb *ldb;
14349be96f3fSAl Viro unsigned long base;
14359be96f3fSAl Viro int result = -1;
14369be96f3fSAl Viro
14379be96f3fSAl Viro BUG_ON(!state);
14389be96f3fSAl Viro
14399be96f3fSAl Viro /* Look for signs of a Dynamic Disk */
14409be96f3fSAl Viro if (!ldm_validate_partition_table(state))
14419be96f3fSAl Viro return 0;
14429be96f3fSAl Viro
14439be96f3fSAl Viro ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
14449be96f3fSAl Viro if (!ldb) {
14459be96f3fSAl Viro ldm_crit ("Out of memory.");
14469be96f3fSAl Viro goto out;
14479be96f3fSAl Viro }
14489be96f3fSAl Viro
14499be96f3fSAl Viro /* Parse and check privheads. */
14509be96f3fSAl Viro if (!ldm_validate_privheads(state, &ldb->ph))
14519be96f3fSAl Viro goto out; /* Already logged */
14529be96f3fSAl Viro
14539be96f3fSAl Viro /* All further references are relative to base (database start). */
14549be96f3fSAl Viro base = ldb->ph.config_start;
14559be96f3fSAl Viro
14569be96f3fSAl Viro /* Parse and check tocs and vmdb. */
14579be96f3fSAl Viro if (!ldm_validate_tocblocks(state, base, ldb) ||
14589be96f3fSAl Viro !ldm_validate_vmdb(state, base, ldb))
14599be96f3fSAl Viro goto out; /* Already logged */
14609be96f3fSAl Viro
14619be96f3fSAl Viro /* Initialize vblk lists in ldmdb struct */
14629be96f3fSAl Viro INIT_LIST_HEAD (&ldb->v_dgrp);
14639be96f3fSAl Viro INIT_LIST_HEAD (&ldb->v_disk);
14649be96f3fSAl Viro INIT_LIST_HEAD (&ldb->v_volu);
14659be96f3fSAl Viro INIT_LIST_HEAD (&ldb->v_comp);
14669be96f3fSAl Viro INIT_LIST_HEAD (&ldb->v_part);
14679be96f3fSAl Viro
14689be96f3fSAl Viro if (!ldm_get_vblks(state, base, ldb)) {
14699be96f3fSAl Viro ldm_crit ("Failed to read the VBLKs from the database.");
14709be96f3fSAl Viro goto cleanup;
14719be96f3fSAl Viro }
14729be96f3fSAl Viro
14739be96f3fSAl Viro /* Finally, create the data partition devices. */
14749be96f3fSAl Viro if (ldm_create_data_partitions(state, ldb)) {
14759be96f3fSAl Viro ldm_debug ("Parsed LDM database successfully.");
14769be96f3fSAl Viro result = 1;
14779be96f3fSAl Viro }
14789be96f3fSAl Viro /* else Already logged */
14799be96f3fSAl Viro
14809be96f3fSAl Viro cleanup:
14819be96f3fSAl Viro ldm_free_vblks (&ldb->v_dgrp);
14829be96f3fSAl Viro ldm_free_vblks (&ldb->v_disk);
14839be96f3fSAl Viro ldm_free_vblks (&ldb->v_volu);
14849be96f3fSAl Viro ldm_free_vblks (&ldb->v_comp);
14859be96f3fSAl Viro ldm_free_vblks (&ldb->v_part);
14869be96f3fSAl Viro out:
14879be96f3fSAl Viro kfree (ldb);
14889be96f3fSAl Viro return result;
14899be96f3fSAl Viro }
1490