⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldm.c

📁 ARM 嵌入式 系统 设计与实例开发 实验教材 二源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * ldm - Part of the Linux-NTFS project. * * Copyright (C) 2001 Richard Russon <ldm@flatcap.org> * Copyright (C) 2001 Anton Altaparmakov <antona@users.sf.net> (AIA) * * Documentation is available at http://linux-ntfs.sf.net/ldm * * 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 Linux-NTFS source * in the file COPYING); if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * 28/10/2001 - Added sorting of ldm partitions. (AIA) */#include <linux/types.h>#include <asm/unaligned.h>#include <asm/byteorder.h>#include <linux/genhd.h>#include <linux/blkdev.h>#include <linux/slab.h>#include "check.h"#include "ldm.h"#include "msdos.h"#if 0 /* Fool kernel-doc since it doesn't do macros yet. *//** * ldm_debug - output an error message if debugging was enabled at compile time * @f:		a printf format string containing the message * @...:	the 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. */static void ldm_debug(const char *f, ...);#endif#ifdef CONFIG_LDM_DEBUG#define ldm_debug(f, a...)						\	{								\		printk(LDM_DEBUG " DEBUG (%s, %d): %s: ",		\				__FILE__, __LINE__, __FUNCTION__);	\		printk(f, ##a);						\	}#else	/* !CONFIG_LDM_DEBUG */#define ldm_debug(f, a...)	do {} while (0)#endif	/* !CONFIG_LDM_DEBUG *//* Necessary forward declarations. */static int create_partition(struct gendisk *, int, int, int);static int parse_privhead(const u8 *, struct privhead *);static u64 get_vnum(const u8 *, int *);static int get_vstr(const u8 *, u8 *, const int);/** * parse_vblk_part - parse a LDM database vblk partition record * @buffer:	vblk partition record loaded from the LDM database * @buf_size:	size of @buffer in bytes * @vb:		in memory vblk structure to return parsed information in * * This parses the LDM database vblk record of type VBLK_PART, i.e. a partition * record, supplied in @buffer and sets up the in memory vblk structure @vb * with the obtained information. * * Return 1 on success and -1 on error, in which case @vb is undefined. */static int parse_vblk_part(const u8 *buffer, const int buf_size,		struct vblk *vb){	int err, rel_objid, rel_name, rel_size, rel_parent;	if (0x34 >= buf_size)		return -1;	/* Calculate relative offsets. */	rel_objid  = 1 + buffer[0x18];	if (0x18 + rel_objid >= buf_size)		return -1;	rel_name   = 1 + buffer[0x18 + rel_objid] + rel_objid;	if (0x34 + rel_name >= buf_size)		return -1;	rel_size   = 1 + buffer[0x34 + rel_name] + rel_name;	if (0x34 + rel_size >= buf_size)		return -1;	rel_parent = 1 + buffer[0x34 + rel_size] + rel_size;	if (0x34 + rel_parent >= buf_size)		return -1;	/* Setup @vb. */	vb->vblk_type    = VBLK_PART;	vb->obj_id       = get_vnum(buffer + 0x18, &err);	if (err || 0x34 + rel_parent + buffer[0x34 + rel_parent] >= buf_size)		return -1;	vb->disk_id      = get_vnum(buffer + 0x34 + rel_parent, &err);	if (err || 0x24 + rel_name + 8 > buf_size)		return -1;	vb->start_sector = BE64(buffer + 0x24 + rel_name);	if (0x34 + rel_name + buffer[0x34 + rel_name] >= buf_size)		return -1;	vb->num_sectors  = get_vnum(buffer + 0x34 + rel_name, &err);	if (err || 0x18 + rel_objid + buffer[0x18 + rel_objid] >= buf_size)		return -1;	err = get_vstr(buffer + 0x18 + rel_objid, vb->name, sizeof(vb->name));	if (err == -1)		return err;	ldm_debug("Parsed Partition VBLK successfully.\n");	return 1;}/** * parse_vblk - parse a LDM database vblk record * @buffer:	vblk record loaded from the LDM database * @buf_size:	size of @buffer in bytes * @vb:		in memory vblk structure to return parsed information in * * This parses the LDM database vblk record supplied in @buffer and sets up * the in memory vblk structure @vb with the obtained information. * * Return 1 on success, 0 if successful but record not in use, and -1 on error. * If the return value is 0 or -1, @vb is undefined. * * NOTE: Currently the only record type we handle is VBLK_PART, i.e. records * describing a partition. For all others, we just set @vb->vblk_type to 0 and * return success. This of course means that if @vb->vblk_type is zero, all * other fields in @vb are undefined. */static int parse_vblk(const u8 *buffer, const int buf_size, struct vblk *vb){	int err = 1;	if (buf_size < 0x14)		return -1;	if (MAGIC_VBLK != BE32(buffer)) {		printk(LDM_CRIT "Cannot find VBLK, database may be corrupt.\n");		return -1;	}	if ((BE16(buffer + 0x0E) == 0) ||       /* Record is not in use. */	    (BE16(buffer + 0x0C) != 0))         /* Part 2 of an ext. record */		return 0;	/* FIXME: What about extended VBLKs? */	switch (buffer[0x13]) {	case VBLK_PART:		err = parse_vblk_part(buffer, buf_size, vb);		break;	default:		vb->vblk_type = 0;	}	if (err != -1)		ldm_debug("Parsed VBLK successfully.\n");	return err;}/** * add_partition_to_list - insert partition into a partition list * @pl:		sorted list of partitions * @hd:		gendisk structure to which the data partition belongs * @disk_minor:	minor number of the disk device * @start:	first sector within the disk device * @size:	number of sectors on the partition device * * This sanity checks the partition specified by @start and @size against the * device specified by @hd and inserts the partition into the sorted partition * list @pl if the checks pass. * * On success return 1, otherwise return -1. * * TODO: Add sanity check for overlapping partitions. (AIA) */ static int add_partition_to_list(struct list_head *pl, const struct gendisk *hd,		const int disk_minor, const unsigned long start,		const unsigned long size){	struct ldm_part *lp, *lptmp;	struct list_head *tmp;	if (!hd->part)		return -1;	if ((start < 1) || ((start + size) > hd->part[disk_minor].nr_sects)) {		printk(LDM_CRIT "LDM partition exceeds physical disk. "				"Skipping.\n");		return -1;	}	lp = (struct ldm_part*)kmalloc(sizeof(struct ldm_part), GFP_KERNEL);	if (!lp) {		printk(LDM_CRIT "Not enough memory! Aborting LDM partition "				"parsing.\n");		return -2;	}	INIT_LIST_HEAD(&lp->part_list);	lp->start = start;	lp->size = size;	list_for_each(tmp, pl) {		lptmp = list_entry(tmp, struct ldm_part, part_list);		if (start > lptmp->start)			continue;		if (start < lptmp->start)			break;		printk(LDM_CRIT "Duplicate LDM partition entry! Skipping.\n");		kfree(lp);		return -1;	}	list_add_tail(&lp->part_list, tmp);	ldm_debug("Added LDM partition successfully.\n");	return 1;}/** * create_data_partitions - create the data partition devices * @hd:			gendisk structure in which to create the data partitions * @first_sector:	first sector within the disk device * @first_part_minor:	first minor number of data partition devices * @dev:		partition device holding the LDM database * @vm:			in memory vmdb structure of @dev * @ph:			in memory privhead structure of the disk device * @dk:			in memory ldmdisk structure of the disk device * * The database contains ALL the partitions for ALL the disks, so we need to * filter out this specific disk. Using the disk's object id, we can find all * the partitions in the database that belong to this disk. * * For each found partition, we create a corresponding partition device starting * with minor number @first_part_minor. But we do this in such a way that we * actually sort the partitions in order of on-disk position. Any invalid * partitions are completely ignored/skipped (an error is output but that's * all). * * Return 1 on success and -1 on error. */static int create_data_partitions(struct gendisk *hd,		const unsigned long first_sector, int first_part_minor,		struct block_device *bdev, const struct vmdb *vm,		const struct privhead *ph, const struct ldmdisk *dk,		unsigned long base){	Sector sect;	unsigned char *data;	struct vblk *vb;	LIST_HEAD(pl);		/* Sorted list of partitions. */	struct ldm_part *lp;	struct list_head *tmp;	int vblk;	int vsize;		/* VBLK size. */	int perbuf;		/* VBLKs per buffer. */	int buffer, lastbuf, lastofs, err, disk_minor;	vb = (struct vblk*)kmalloc(sizeof(struct vblk), GFP_KERNEL);	if (!vb)		goto no_mem;	vsize   = vm->vblk_size;	if (vsize < 1 || vsize > 512)		goto err_out;	perbuf  = 512 / vsize;	if (perbuf < 1 || 512 % vsize)		goto err_out;					/* 512 == VMDB size */	lastbuf = vm->last_vblk_seq / perbuf - 1;	lastofs = vm->last_vblk_seq % perbuf;	if (lastofs)		lastbuf++;	if (OFF_VBLK * LDM_BLOCKSIZE + vm->last_vblk_seq * vsize >			ph->config_size * 512)		goto err_out;	/*	 * Get the minor number of the parent device so we can check we don't	 * go beyond the end of the device.	 */	disk_minor = (first_part_minor >> hd->minor_shift) << hd->minor_shift;	for (buffer = 0; buffer < lastbuf; buffer++) {		data = read_dev_sector(bdev, base + 2*OFF_VBLK + buffer, &sect);		if (!data)			goto read_err;		for (vblk = 0; vblk < perbuf; vblk++) {			u8 *block;						if (lastofs && buffer == lastbuf - 1 && vblk >= lastofs)				break;			block = data + vsize * vblk;			if (block + vsize > data + 512)				goto brelse_out;			if (parse_vblk(block, vsize, vb) != 1)				continue;			if (vb->vblk_type != VBLK_PART)				continue;			if (dk->obj_id != vb->disk_id)				continue;			/* Ignore invalid partition errors. */			if (add_partition_to_list(&pl, hd, disk_minor,					first_sector + vb->start_sector +					ph->logical_disk_start,					vb->num_sectors) < -1)				goto brelse_out;		}		put_dev_sector(sect);	}	err = 1;out:	/* Finally create the nicely sorted data partitions. */	printk(" <");	list_for_each(tmp, &pl) {		lp = list_entry(tmp, struct ldm_part, part_list);		add_gd_partition(hd, first_part_minor++, lp->start, lp->size);	}	printk(" >\n");	if (!list_empty(&pl)) {		struct list_head *tmp2;		/* Cleanup the partition list which is now superfluous. */		list_for_each_safe(tmp, tmp2, &pl) {			lp = list_entry(tmp, struct ldm_part, part_list);			list_del(tmp);			kfree(lp);		}	}	kfree(vb);	return err;brelse_out:	put_dev_sector(sect);	goto err_out;no_mem:	printk(LDM_CRIT "Not enough memory to allocate required buffers.\n");	goto err_out;read_err:	printk(LDM_CRIT "Disk read failed in create_partitions.\n");err_out:	err = -1;	goto out;}/** * get_vnum - convert a variable-width, big endian number, to cpu u64 one * @block:	pointer to the variable-width number to convert * @err:	address of an integer into which to return the error code. * * This converts a variable-width, big endian number into a 64-bit, CPU format * number and returns the result with err set to 0. If an error occurs return 0 * with err set to -1. * * The first byte of a variable-width number is the size of the number in bytes. */static u64 get_vnum(const u8 *block, int *err){	u64 tmp = 0ULL;	u8 length = *block++;	if (length && length <= 8) {		while (length--)			tmp = (tmp << 8) | *block++;		*err = 0;	} else {		printk(LDM_ERR "Illegal length in get_vnum(): %d.\n", length);		*err = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -