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
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
23 #include <ext2fs/ext2_types.h>
26 #include "et/com_err.h"
28 #include "ext2fs/io.h"
33 #define LINUX_EXT2FS 0x83
34 #define LINUX_SWAP 0x82
35 #define WRITE_ERR(_msg_) write(2, _msg_, strlen(_msg_))
36 #define WRITE_ERR_S(_msg_) write(2, _msg_, sizeof(_msg_))
41 unsigned long _dio_error;
42 unsigned long _dio_hw_error;
45 * Array of all opened partitions
47 static PARTITION **partitions = NULL;
48 static unsigned short npart = 0; /* Number of mapped partitions */
49 static PARTITION *active = NULL;
52 * I/O Manager routine prototypes
54 static errcode_t dos_open(const char *dev, int flags, io_channel *channel);
55 static errcode_t dos_close(io_channel channel);
56 static errcode_t dos_set_blksize(io_channel channel, int blksize);
57 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
58 int count, void *buf);
59 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
60 int count, const void *buf);
61 static errcode_t dos_flush(io_channel channel);
63 static struct struct_io_manager struct_dos_manager = {
64 EXT2_ET_MAGIC_IO_MANAGER,
73 io_manager dos_io_manager = &struct_dos_manager;
76 * Macro taken from unix_io.c
79 * For checking structure magic numbers...
82 #define EXT2_CHECK_MAGIC(struct, code) \
83 if ((struct)->magic != (code)) return (code)
86 * Calculates a CHS address of a sector from its LBA
87 * offset for the given partition.
89 static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part)
93 chs->offset = lba_addr & 0x000001FF;
94 abss = (lba_addr >> 9) + part->start;
95 chs->cyl = abss / (part->sects * part->heads);
96 chs->head = (abss / part->sects) % part->heads;
97 chs->sector = (abss % part->sects) + 1;
104 * Scans the passed partition table looking for *pno partition
105 * that has LINUX_EXT2FS type.
108 * For partition numbers >5 Linux uses DOS extended partitions -
109 * dive into them an return an appropriate entry. Also dive into
110 * extended partitions when scanning for a first Linux/ext2fs.
112 static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry,
118 if(*pno != 0xFF && *pno >= 5)
119 return NULL; /* We don't support extended partitions for now */
123 if(pentry[*pno].type == LINUX_EXT2FS)
124 return &pentry[*pno];
127 if(!pentry[*pno].type)
129 else if(pentry[*pno].type == LINUX_SWAP)
135 for(i = 0; i < 4; i++)
136 if(pentry[i].type == LINUX_EXT2FS)
146 * Allocate libext2fs structures associated with I/O manager
148 static io_channel alloc_io_channel(PARTITION *part)
152 ioch = (io_channel)malloc(sizeof(struct struct_io_channel));
155 memset(ioch, 0, sizeof(struct struct_io_channel));
156 ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL;
157 ioch->manager = dos_io_manager;
158 ioch->name = (char *)malloc(strlen(part->dev)+1);
163 strcpy(ioch->name, part->dev);
164 ioch->private_data = part;
165 ioch->block_size = 1024; /* The smallest ext2fs block size */
166 ioch->read_error = 0;
167 ioch->write_error = 0;
176 * Open the 'name' partition, initialize all information structures
177 * we need to keep and create libext2fs I/O manager.
179 static errcode_t dos_open(const char *dev, int flags, io_channel *channel)
181 unsigned char *tmp, sec[512];
184 PARTITION **newparts;
188 _dio_error = ERR_BADDEV;
189 return EXT2_ET_BAD_DEVICE_NAME;
193 * First check whether the dev name is OK
195 tmp = (unsigned char*)strrchr(dev, '/');
198 _dio_error = ERR_BADDEV;
199 return EXT2_ET_BAD_DEVICE_NAME;
202 if(strcmp(dev, "/dev"))
204 _dio_error = ERR_BADDEV;
205 return EXT2_ET_BAD_DEVICE_NAME;
210 * Check whether the partition data is already in cache
213 part = (PARTITION*)malloc(sizeof(PARTITION));
220 if(!strcmp(partitions[i]->dev, dev))
222 /* Found it! Make it the active one */
223 active = partitions[i];
224 *channel = alloc_io_channel(active);
232 * Drive number & optionally partn number
239 part->phys += toupper(tmp[2]) - 'A';
241 * Do we have the partition number?
244 part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0;
251 part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0;
253 part->phys = 0x00; /* We'll assume /dev/fd0 */
257 _dio_error = ERR_BADDEV;
261 if(part->phys < 0x80)
263 /* We don't support floppies for now */
264 _dio_error = ERR_NOTSUPP;
268 part->dev = strdup(dev);
271 * Get drive's geometry
273 _dio_hw_error = biosdisk(DISK_GET_GEOMETRY,
278 1, /* just one sector */
283 _dio_error = ERR_HARDWARE;
290 * Calculate the geometry
292 part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1);
293 part->heads = sec[3] + 1;
294 part->sects = sec[0] & 0x3F;
297 * Now that we know all we need, let's look for the partition
299 _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec);
303 _dio_error = ERR_HARDWARE;
309 pent = (PTABLE_ENTRY*)&sec[0x1BE];
310 pent = scan_partition_table(pent, part->phys, &part->pno);
314 _dio_error = part->pno == 0xFE ? ERR_EMPTYPART :
315 part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS;
322 * Calculate the remaining figures
325 unsigned long fsec, fhead, fcyl;
327 fsec = (unsigned long)(pent->start_sec & 0x3F);
328 fhead = (unsigned long)pent->start_head;
329 fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl;
330 part->start = fsec + fhead * part->sects + fcyl *
331 (part->heads * part->sects) - 1;
332 part->len = pent->size;
336 * Add the partition to the table
338 newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart);
343 partitions = newparts;
344 partitions[npart++] = active = part;
347 * Now alloc all libe2fs structures
349 *channel = alloc_io_channel(active);
356 static errcode_t dos_close(io_channel channel)
364 static errcode_t dos_set_blksize(io_channel channel, int blksize)
366 channel->block_size = blksize;
371 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
372 int count, void *buf)
379 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
380 part = (PARTITION*)channel->private_data;
382 size = (size_t)((count < 0) ? -count : count * channel->block_size);
383 loc = (ext2_loff_t) block * channel->block_size;
385 lba2chs(loc, &chs, part);
387 * Potential bug here:
388 * If DJGPP is used then reads of >18 sectors will fail!
389 * Have to rewrite biosdisk.
391 _dio_hw_error = biosdisk(DISK_READ,
396 size < 512 ? 1 : size/512,
401 _dio_error = ERR_HARDWARE;
408 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
409 int count, const void *buf)
416 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
417 part = (PARTITION*)channel->private_data;
420 size = (size_t)channel->block_size;
424 size = (size_t)-count;
426 size = (size_t)(count * channel->block_size);
429 loc = (ext2_loff_t)block * channel->block_size;
430 lba2chs(loc, &chs, part);
431 _dio_hw_error = biosdisk(DISK_WRITE,
436 size < 512 ? 1 : size/512,
441 _dio_error = ERR_HARDWARE;
451 static errcode_t dos_flush(io_channel channel)
454 * No buffers, no flush...