diff options
Diffstat (limited to 'e2fsprogs')
-rw-r--r-- | e2fsprogs/ext2fs/dosio.c | 456 | ||||
-rw-r--r-- | e2fsprogs/ext2fs/dosio.h | 153 |
2 files changed, 0 insertions, 609 deletions
diff --git a/e2fsprogs/ext2fs/dosio.c b/e2fsprogs/ext2fs/dosio.c deleted file mode 100644 index d695b18ef..000000000 --- a/e2fsprogs/ext2fs/dosio.c +++ /dev/null @@ -1,456 +0,0 @@ -/* - * dosio.c -- Disk I/O module for the ext2fs/DOS library. - * - * Copyright (c) 1997 by Theodore Ts'o. - * - * Copyright (c) 1997 Mark Habersack - * This file may be distributed under the terms of the GNU Public License. - * - */ - -#include <stdio.h> -#include <bios.h> -#include <string.h> -#include <ctype.h> -#include <io.h> -#ifdef HAVE_ERRNO_H -#include <errno.h> -#endif - -#include <ext2fs/ext2_types.h> -#include "utils.h" -#include "dosio.h" -#include "et/com_err.h" -#include "ext2_err.h" -#include "ext2fs/io.h" - -/* - * Some helper macros - */ -#define LINUX_EXT2FS 0x83 -#define LINUX_SWAP 0x82 -#define WRITE_ERR(_msg_) write(2, _msg_, strlen(_msg_)) -#define WRITE_ERR_S(_msg_) write(2, _msg_, sizeof(_msg_)) - -/* - * Exported variables - */ -unsigned long _dio_error; -unsigned long _dio_hw_error; - -/* - * Array of all opened partitions - */ -static PARTITION **partitions = NULL; -static unsigned short npart = 0; /* Number of mapped partitions */ -static PARTITION *active = NULL; - -/* - * I/O Manager routine prototypes - */ -static errcode_t dos_open(const char *dev, int flags, io_channel *channel); -static errcode_t dos_close(io_channel channel); -static errcode_t dos_set_blksize(io_channel channel, int blksize); -static errcode_t dos_read_blk(io_channel channel, unsigned long block, - int count, void *buf); -static errcode_t dos_write_blk(io_channel channel, unsigned long block, - int count, const void *buf); -static errcode_t dos_flush(io_channel channel); - -static struct struct_io_manager struct_dos_manager = { - EXT2_ET_MAGIC_IO_MANAGER, - "DOS I/O Manager", - dos_open, - dos_close, - dos_set_blksize, - dos_read_blk, - dos_write_blk, - dos_flush -}; -io_manager dos_io_manager = &struct_dos_manager; - -/* - * Macro taken from unix_io.c - */ -/* - * For checking structure magic numbers... - */ - -#define EXT2_CHECK_MAGIC(struct, code) \ - if ((struct)->magic != (code)) return (code) - -/* - * Calculates a CHS address of a sector from its LBA - * offset for the given partition. - */ -static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part) -{ - unsigned long abss; - - chs->offset = lba_addr & 0x000001FF; - abss = (lba_addr >> 9) + part->start; - chs->cyl = abss / (part->sects * part->heads); - chs->head = (abss / part->sects) % part->heads; - chs->sector = (abss % part->sects) + 1; -} - -#ifdef __TURBOC__ -#pragma argsused -#endif -/* - * Scans the passed partition table looking for *pno partition - * that has LINUX_EXT2FS type. - * - * TODO: - * For partition numbers >5 Linux uses DOS extended partitions - - * dive into them an return an appropriate entry. Also dive into - * extended partitions when scanning for a first Linux/ext2fs. - */ -static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry, - unsigned short phys, - unsigned char *pno) -{ - unsigned i; - - if(*pno != 0xFF && *pno >= 5) - return NULL; /* We don't support extended partitions for now */ - - if(*pno != 0xFF) - { - if(pentry[*pno].type == LINUX_EXT2FS) - return &pentry[*pno]; - else - { - if(!pentry[*pno].type) - *pno = 0xFE; - else if(pentry[*pno].type == LINUX_SWAP) - *pno = 0xFD; - return NULL; - } - } - - for(i = 0; i < 4; i++) - if(pentry[i].type == LINUX_EXT2FS) - { - *pno = i; - return &pentry[i]; - } - - return NULL; -} - -/* - * Allocate libext2fs structures associated with I/O manager - */ -static io_channel alloc_io_channel(PARTITION *part) -{ - io_channel ioch; - - ioch = (io_channel)malloc(sizeof(struct struct_io_channel)); - if (!ioch) - return NULL; - memset(ioch, 0, sizeof(struct struct_io_channel)); - ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL; - ioch->manager = dos_io_manager; - ioch->name = (char *)malloc(strlen(part->dev)+1); - if (!ioch->name) { - free(ioch); - return NULL; - } - strcpy(ioch->name, part->dev); - ioch->private_data = part; - ioch->block_size = 1024; /* The smallest ext2fs block size */ - ioch->read_error = 0; - ioch->write_error = 0; - - return ioch; -} - -#ifdef __TURBOC__ -#pragma argsused -#endif -/* - * Open the 'name' partition, initialize all information structures - * we need to keep and create libext2fs I/O manager. - */ -static errcode_t dos_open(const char *dev, int flags, io_channel *channel) -{ - unsigned char *tmp, sec[512]; - PARTITION *part; - PTABLE_ENTRY *pent; - PARTITION **newparts; - - if(!dev) - { - _dio_error = ERR_BADDEV; - return EXT2_ET_BAD_DEVICE_NAME; - } - - /* - * First check whether the dev name is OK - */ - tmp = (unsigned char*)strrchr(dev, '/'); - if(!tmp) - { - _dio_error = ERR_BADDEV; - return EXT2_ET_BAD_DEVICE_NAME; - } - *tmp = 0; - if(strcmp(dev, "/dev")) - { - _dio_error = ERR_BADDEV; - return EXT2_ET_BAD_DEVICE_NAME; - } - *tmp++ = '/'; - - /* - * Check whether the partition data is already in cache - */ - - part = (PARTITION*)malloc(sizeof(PARTITION)); - if (!part) - return ENOMEM; - { - int i = 0; - - for(;i < npart; i++) - if(!strcmp(partitions[i]->dev, dev)) - { - /* Found it! Make it the active one */ - active = partitions[i]; - *channel = alloc_io_channel(active); - if (!*channel) - return ENOMEM; - return 0; - } - } - - /* - * Drive number & optionally partn number - */ - switch(tmp[0]) - { - case 'h': - case 's': - part->phys = 0x80; - part->phys += toupper(tmp[2]) - 'A'; - /* - * Do we have the partition number? - */ - if(tmp[3]) - part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0; - else - part->pno = 0xFF; - break; - - case 'f': - if(tmp[2]) - part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0; - else - part->phys = 0x00; /* We'll assume /dev/fd0 */ - break; - - default: - _dio_error = ERR_BADDEV; - return ENODEV; - } - - if(part->phys < 0x80) - { - /* We don't support floppies for now */ - _dio_error = ERR_NOTSUPP; - return EINVAL; - } - - part->dev = strdup(dev); - - /* - * Get drive's geometry - */ - _dio_hw_error = biosdisk(DISK_GET_GEOMETRY, - part->phys, - 0, /* head */ - 0, /* cylinder */ - 1, /* sector */ - 1, /* just one sector */ - sec); - - if(!HW_OK()) - { - _dio_error = ERR_HARDWARE; - if (part) - free(part); - return EFAULT; - } - - /* - * Calculate the geometry - */ - part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1); - part->heads = sec[3] + 1; - part->sects = sec[0] & 0x3F; - - /* - * Now that we know all we need, let's look for the partition - */ - _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec); - - if(!HW_OK()) - { - _dio_error = ERR_HARDWARE; - if (part) - free(part); - return EFAULT; - } - - pent = (PTABLE_ENTRY*)&sec[0x1BE]; - pent = scan_partition_table(pent, part->phys, &part->pno); - - if(!pent) - { - _dio_error = part->pno == 0xFE ? ERR_EMPTYPART : - part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS; - if (part) - free(part); - return ENODEV; - } - - /* - * Calculate the remaining figures - */ - { - unsigned long fsec, fhead, fcyl; - - fsec = (unsigned long)(pent->start_sec & 0x3F); - fhead = (unsigned long)pent->start_head; - fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl; - part->start = fsec + fhead * part->sects + fcyl * - (part->heads * part->sects) - 1; - part->len = pent->size; - } - - /* - * Add the partition to the table - */ - newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart); - if (!newparts) { - free(part); - return ENOMEM; - } - partitions = newparts; - partitions[npart++] = active = part; - - /* - * Now alloc all libe2fs structures - */ - *channel = alloc_io_channel(active); - if (!*channel) - return ENOMEM; - - return 0; -} - -static errcode_t dos_close(io_channel channel) -{ - if (channel->name) - free(channel->name); - if (channel) - free(channel); - - return 0; -} - -static errcode_t dos_set_blksize(io_channel channel, int blksize) -{ - channel->block_size = blksize; - - return 0; -} - -static errcode_t dos_read_blk(io_channel channel, unsigned long block, - int count, void *buf) -{ - PARTITION *part; - size_t size; - ext2_loff_t loc; - CHS chs; - - EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); - part = (PARTITION*)channel->private_data; - - size = (size_t)((count < 0) ? -count : count * channel->block_size); - loc = (ext2_loff_t) block * channel->block_size; - - lba2chs(loc, &chs, part); - /* - * Potential bug here: - * If DJGPP is used then reads of >18 sectors will fail! - * Have to rewrite biosdisk. - */ - _dio_hw_error = biosdisk(DISK_READ, - part->phys, - chs.head, - chs.cyl, - chs.sector, - size < 512 ? 1 : size/512, - buf); - - if(!HW_OK()) - { - _dio_error = ERR_HARDWARE; - return EFAULT; - } - - return 0; -} - -static errcode_t dos_write_blk(io_channel channel, unsigned long block, - int count, const void *buf) -{ - PARTITION *part; - size_t size; - ext2_loff_t loc; - CHS chs; - - EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); - part = (PARTITION*)channel->private_data; - - if(count == 1) - size = (size_t)channel->block_size; - else - { - if (count < 0) - size = (size_t)-count; - else - size = (size_t)(count * channel->block_size); - } - - loc = (ext2_loff_t)block * channel->block_size; - lba2chs(loc, &chs, part); - _dio_hw_error = biosdisk(DISK_WRITE, - part->phys, - chs.head, - chs.cyl, - chs.sector, - size < 512 ? 1 : size/512, - (void*)buf); - - if(!HW_OK()) - { - _dio_error = ERR_HARDWARE; - return EFAULT; - } - - return 0; -} - -#ifdef __TURBOC__ -#pragma argsused -#endif -static errcode_t dos_flush(io_channel channel) -{ - /* - * No buffers, no flush... - */ - return 0; -} diff --git a/e2fsprogs/ext2fs/dosio.h b/e2fsprogs/ext2fs/dosio.h deleted file mode 100644 index a0d652d5b..000000000 --- a/e2fsprogs/ext2fs/dosio.h +++ /dev/null @@ -1,153 +0,0 @@ -/* - * v1.0 - * - * Disk I/O include file for the ext2fs/DOS library. - * - * Copyright (c) 1997 Mark Habersack - * This file may be distributed under the terms of the GNU Public License. - * - */ -#ifndef __diskio_h -#define __diskio_h -#ifdef __TURBOC__ -#ifndef __LARGE__ -# error "ext2fs/DOS library requires LARGE model!" -#endif -#endif - -#ifdef __TURBOC__ -#include "msdos.h" -#endif - -/* - * A helper structure used in LBA => CHS conversion - */ -typedef struct -{ - unsigned short cyl; /* Cylinder (or track) */ - unsigned short head; - unsigned short sector; - unsigned short offset; /* Offset of byte within the sector */ -} CHS; - -/* - * All partition data we need is here - */ -typedef struct -{ - char *dev; /* _Linux_ device name (like "/dev/hda1") */ - unsigned char phys; /* Physical DOS drive number */ - unsigned long start; /* LBA address of partition start */ - unsigned long len; /* length of partition in sectors */ - unsigned char pno; /* Partition number (read from *dev) */ - - /* This partition's drive geometry */ - unsigned short cyls; - unsigned short heads; - unsigned short sects; -} PARTITION; - -/* - * PC partition table entry format - */ -#ifdef __DJGPP__ -#pragma pack(1) -#endif -typedef struct -{ - unsigned char active; - unsigned char start_head; - unsigned char start_sec; - unsigned char start_cyl; - unsigned char type; - unsigned char end_head; - unsigned char end_sec; - unsigned char end_cyl; - unsigned long first_sec_rel; - unsigned long size; -} PTABLE_ENTRY; -#ifdef __DJGPP__ -#pragma pack() -#endif - -/* - * INT 0x13 operation codes - */ -#define DISK_READ 0x02 -#define DISK_WRITE 0x03 -#define DISK_GET_GEOMETRY 0x08 -#define DISK_READY 0x10 - -/* - * Errors to put in _dio_error - */ -#define ERR_BADDEV 0x00000001L -#define ERR_HARDWARE 0x00000002L -#define ERR_NOTSUPP 0x00000003L -#define ERR_NOTEXT2FS 0x00000004L -#define ERR_EMPTYPART 0x00000005L -#define ERR_LINUXSWAP 0x00000006L - -/* - * Functions in diskio.c - */ - -/* - * Variable contains last module's error - */ -extern unsigned long _dio_error; - -/* - * This one contains last hardware error (if _dio_error == ERR_HARDWARE) - */ -extern unsigned long _dio_hw_error; - -/* - * Macros to check for disk hardware errors - */ -#define HW_OK() ((unsigned char)_dio_hw_error == 0x00) -#define HW_BAD_CMD() ((unsigned char)_dio_hw_error == 0x01) -#define HW_NO_ADDR_MARK() ((unsigned char)_dio_hw_error == 0x02) -#define HW_WRITE_PROT() ((unsigned char)_dio_hw_error == 0x03) -#define HW_NO_SECTOR() ((unsigned char)_dio_hw_error == 0x04) -#define HW_RESET_FAIL() ((unsigned char)_dio_hw_error == 0x05) -#define HW_DISK_CHANGED() ((unsigned char)_dio_hw_error == 0x06) -#define HW_DRIVE_FAIL() ((unsigned char)_dio_hw_error == 0x07) -#define HW_DMA_OVERRUN() ((unsigned char)_dio_hw_error == 0x08) -#define HW_DMA_BOUNDARY() ((unsigned char)_dio_hw_error == 0x09) -#define HW_BAD_SECTOR() ((unsigned char)_dio_hw_error == 0x0A) -#define HW_BAD_TRACK() ((unsigned char)_dio_hw_error == 0x0B) -#define HW_UNSUPP_TRACK() ((unsigned char)_dio_hw_error == 0x0C) -#define HW_BAD_CRC_ECC() ((unsigned char)_dio_hw_error == 0x10) -#define HW_CRC_ECC_CORR() ((unsigned char)_dio_hw_error == 0x11) -#define HW_CONTR_FAIL() ((unsigned char)_dio_hw_error == 0x20) -#define HW_SEEK_FAIL() ((unsigned char)_dio_hw_error == 0x40) -#define HW_ATTACH_FAIL() ((unsigned char)_dio_hw_error == 0x80) -#define HW_DRIVE_NREADY() ((unsigned char)_dio_hw_error == 0xAA) -#define HW_UNDEF_ERROR() ((unsigned char)_dio_hw_error == 0xBB) -#define HW_WRITE_FAULT() ((unsigned char)_dio_hw_error == 0xCC) -#define HW_STATUS_ERROR() ((unsigned char)_dio_hw_error == 0xE0) -#define HW_SENSE_FAIL() ((unsigned char)_dio_hw_error == 0xFF) - - -/* - * Open the specified partition. - * String 'dev' must have a format: - * - * /dev/{sd|hd|fd}[X] - * - * where, - * - * only one of the option in curly braces can be used and X is an optional - * partition number for the given device. If X is not specified, function - * scans the drive's partition table in search for the first Linux ext2fs - * partition (signature 0x83). Along the way it dives into every extended - * partition encountered. - * Scan ends if either (a) there are no more used partition entries, or - * (b) there is no Xth partition. - * - * Routine returns 0 on success and !=0 otherwise. - */ -int open_partition(char *dev); - -#endif /* __diskio_h */ |