2 * dosio.c -- Disk I/O module for the ext2fs/DOS library.
4 * Copyright (c) 1997 by Theodore Ts'o.
6 * Copyright (c) 1997 Mark Habersack
7 * This file may be distributed under the terms of the GNU Public License.
20 #include <ext2fs/ext2_types.h>
23 #include "et/com_err.h"
25 #include "ext2fs/io.h"
30 #define LINUX_EXT2FS 0x83
31 #define LINUX_SWAP 0x82
32 #define WRITE_ERR(_msg_) write(2, _msg_, strlen(_msg_))
33 #define WRITE_ERR_S(_msg_) write(2, _msg_, sizeof(_msg_))
38 unsigned long _dio_error;
39 unsigned long _dio_hw_error;
42 * Array of all opened partitions
44 static PARTITION **partitions = NULL;
45 static unsigned short npart = 0; /* Number of mapped partitions */
46 static PARTITION *active = NULL;
49 * I/O Manager routine prototypes
51 static errcode_t dos_open(const char *dev, int flags, io_channel *channel);
52 static errcode_t dos_close(io_channel channel);
53 static errcode_t dos_set_blksize(io_channel channel, int blksize);
54 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
55 int count, void *buf);
56 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
57 int count, const void *buf);
58 static errcode_t dos_flush(io_channel channel);
60 static struct struct_io_manager struct_dos_manager = {
61 EXT2_ET_MAGIC_IO_MANAGER,
70 io_manager dos_io_manager = &struct_dos_manager;
73 * Macro taken from unix_io.c
76 * For checking structure magic numbers...
79 #define EXT2_CHECK_MAGIC(struct, code) \
80 if ((struct)->magic != (code)) return (code)
83 * Calculates a CHS address of a sector from its LBA
84 * offset for the given partition.
86 static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part)
90 chs->offset = lba_addr & 0x000001FF;
91 abss = (lba_addr >> 9) + part->start;
92 chs->cyl = abss / (part->sects * part->heads);
93 chs->head = (abss / part->sects) % part->heads;
94 chs->sector = (abss % part->sects) + 1;
101 * Scans the passed partition table looking for *pno partition
102 * that has LINUX_EXT2FS type.
105 * For partition numbers >5 Linux uses DOS extended partitions -
106 * dive into them an return an appropriate entry. Also dive into
107 * extended partitions when scanning for a first Linux/ext2fs.
109 static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry,
115 if(*pno != 0xFF && *pno >= 5)
116 return NULL; /* We don't support extended partitions for now */
120 if(pentry[*pno].type == LINUX_EXT2FS)
121 return &pentry[*pno];
124 if(!pentry[*pno].type)
126 else if(pentry[*pno].type == LINUX_SWAP)
132 for(i = 0; i < 4; i++)
133 if(pentry[i].type == LINUX_EXT2FS)
143 * Allocate libext2fs structures associated with I/O manager
145 static io_channel alloc_io_channel(PARTITION *part)
149 ioch = (io_channel)malloc(sizeof(struct struct_io_channel));
152 memset(ioch, 0, sizeof(struct struct_io_channel));
153 ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL;
154 ioch->manager = dos_io_manager;
155 ioch->name = (char *)malloc(strlen(part->dev)+1);
160 strcpy(ioch->name, part->dev);
161 ioch->private_data = part;
162 ioch->block_size = 1024; /* The smallest ext2fs block size */
163 ioch->read_error = 0;
164 ioch->write_error = 0;
173 * Open the 'name' partition, initialize all information structures
174 * we need to keep and create libext2fs I/O manager.
176 static errcode_t dos_open(const char *dev, int flags, io_channel *channel)
178 unsigned char *tmp, sec[512];
181 PARTITION **newparts;
185 _dio_error = ERR_BADDEV;
186 return EXT2_ET_BAD_DEVICE_NAME;
190 * First check whether the dev name is OK
192 tmp = (unsigned char*)strrchr(dev, '/');
195 _dio_error = ERR_BADDEV;
196 return EXT2_ET_BAD_DEVICE_NAME;
199 if(strcmp(dev, "/dev"))
201 _dio_error = ERR_BADDEV;
202 return EXT2_ET_BAD_DEVICE_NAME;
207 * Check whether the partition data is already in cache
210 part = (PARTITION*)malloc(sizeof(PARTITION));
217 if(!strcmp(partitions[i]->dev, dev))
219 /* Found it! Make it the active one */
220 active = partitions[i];
221 *channel = alloc_io_channel(active);
229 * Drive number & optionally partn number
236 part->phys += toupper(tmp[2]) - 'A';
238 * Do we have the partition number?
241 part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0;
248 part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0;
250 part->phys = 0x00; /* We'll assume /dev/fd0 */
254 _dio_error = ERR_BADDEV;
258 if(part->phys < 0x80)
260 /* We don't support floppies for now */
261 _dio_error = ERR_NOTSUPP;
265 part->dev = strdup(dev);
268 * Get drive's geometry
270 _dio_hw_error = biosdisk(DISK_GET_GEOMETRY,
275 1, /* just one sector */
280 _dio_error = ERR_HARDWARE;
287 * Calculate the geometry
289 part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1);
290 part->heads = sec[3] + 1;
291 part->sects = sec[0] & 0x3F;
294 * Now that we know all we need, let's look for the partition
296 _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec);
300 _dio_error = ERR_HARDWARE;
306 pent = (PTABLE_ENTRY*)&sec[0x1BE];
307 pent = scan_partition_table(pent, part->phys, &part->pno);
311 _dio_error = part->pno == 0xFE ? ERR_EMPTYPART :
312 part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS;
319 * Calculate the remaining figures
322 unsigned long fsec, fhead, fcyl;
324 fsec = (unsigned long)(pent->start_sec & 0x3F);
325 fhead = (unsigned long)pent->start_head;
326 fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl;
327 part->start = fsec + fhead * part->sects + fcyl *
328 (part->heads * part->sects) - 1;
329 part->len = pent->size;
333 * Add the partition to the table
335 newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart);
340 partitions = newparts;
341 partitions[npart++] = active = part;
344 * Now alloc all libe2fs structures
346 *channel = alloc_io_channel(active);
353 static errcode_t dos_close(io_channel channel)
363 static errcode_t dos_set_blksize(io_channel channel, int blksize)
365 channel->block_size = blksize;
370 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
371 int count, void *buf)
378 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
379 part = (PARTITION*)channel->private_data;
381 size = (size_t)((count < 0) ? -count : count * channel->block_size);
382 loc = (ext2_loff_t) block * channel->block_size;
384 lba2chs(loc, &chs, part);
386 * Potential bug here:
387 * If DJGPP is used then reads of >18 sectors will fail!
388 * Have to rewrite biosdisk.
390 _dio_hw_error = biosdisk(DISK_READ,
395 size < 512 ? 1 : size/512,
400 _dio_error = ERR_HARDWARE;
407 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
408 int count, const void *buf)
415 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
416 part = (PARTITION*)channel->private_data;
419 size = (size_t)channel->block_size;
423 size = (size_t)-count;
425 size = (size_t)(count * channel->block_size);
428 loc = (ext2_loff_t)block * channel->block_size;
429 lba2chs(loc, &chs, part);
430 _dio_hw_error = biosdisk(DISK_WRITE,
435 size < 512 ? 1 : size/512,
440 _dio_error = ERR_HARDWARE;
450 static errcode_t dos_flush(io_channel channel)
453 * No buffers, no flush...