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.
24 #include <ext2fs/ext2_types.h>
27 #include "et/com_err.h"
29 #include "ext2fs/io.h"
34 #define LINUX_EXT2FS 0x83
35 #define LINUX_SWAP 0x82
36 #define WRITE_ERR(_msg_) write(2, _msg_, strlen(_msg_))
37 #define WRITE_ERR_S(_msg_) write(2, _msg_, sizeof(_msg_))
42 unsigned long _dio_error;
43 unsigned long _dio_hw_error;
46 * Array of all opened partitions
48 static PARTITION **partitions = NULL;
49 static unsigned short npart = 0; /* Number of mapped partitions */
50 static PARTITION *active = NULL;
53 * I/O Manager routine prototypes
55 static errcode_t dos_open(const char *dev, int flags, io_channel *channel);
56 static errcode_t dos_close(io_channel channel);
57 static errcode_t dos_set_blksize(io_channel channel, int blksize);
58 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
59 int count, void *buf);
60 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
61 int count, const void *buf);
62 static errcode_t dos_flush(io_channel channel);
64 static struct struct_io_manager struct_dos_manager = {
65 EXT2_ET_MAGIC_IO_MANAGER,
74 io_manager dos_io_manager = &struct_dos_manager;
77 * Macro taken from unix_io.c
80 * For checking structure magic numbers...
83 #define EXT2_CHECK_MAGIC(struct, code) \
84 if ((struct)->magic != (code)) return (code)
87 * Calculates a CHS address of a sector from its LBA
88 * offset for the given partition.
90 static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part)
94 chs->offset = lba_addr & 0x000001FF;
95 abss = (lba_addr >> 9) + part->start;
96 chs->cyl = abss / (part->sects * part->heads);
97 chs->head = (abss / part->sects) % part->heads;
98 chs->sector = (abss % part->sects) + 1;
105 * Scans the passed partition table looking for *pno partition
106 * that has LINUX_EXT2FS type.
109 * For partition numbers >5 Linux uses DOS extended partitions -
110 * dive into them an return an appropriate entry. Also dive into
111 * extended partitions when scanning for a first Linux/ext2fs.
113 static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry,
119 if(*pno != 0xFF && *pno >= 5)
120 return NULL; /* We don't support extended partitions for now */
124 if(pentry[*pno].type == LINUX_EXT2FS)
125 return &pentry[*pno];
128 if(!pentry[*pno].type)
130 else if(pentry[*pno].type == LINUX_SWAP)
136 for(i = 0; i < 4; i++)
137 if(pentry[i].type == LINUX_EXT2FS)
147 * Allocate libext2fs structures associated with I/O manager
149 static io_channel alloc_io_channel(PARTITION *part)
153 ioch = (io_channel)malloc(sizeof(struct struct_io_channel));
156 memset(ioch, 0, sizeof(struct struct_io_channel));
157 ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL;
158 ioch->manager = dos_io_manager;
159 ioch->name = (char *)malloc(strlen(part->dev)+1);
164 strcpy(ioch->name, part->dev);
165 ioch->private_data = part;
166 ioch->block_size = 1024; /* The smallest ext2fs block size */
167 ioch->read_error = 0;
168 ioch->write_error = 0;
177 * Open the 'name' partition, initialize all information structures
178 * we need to keep and create libext2fs I/O manager.
180 static errcode_t dos_open(const char *dev, int flags, io_channel *channel)
182 unsigned char *tmp, sec[512];
185 PARTITION **newparts;
189 _dio_error = ERR_BADDEV;
190 return EXT2_ET_BAD_DEVICE_NAME;
194 * First check whether the dev name is OK
196 tmp = (unsigned char*)strrchr(dev, '/');
199 _dio_error = ERR_BADDEV;
200 return EXT2_ET_BAD_DEVICE_NAME;
203 if(strcmp(dev, "/dev"))
205 _dio_error = ERR_BADDEV;
206 return EXT2_ET_BAD_DEVICE_NAME;
211 * Check whether the partition data is already in cache
214 part = (PARTITION*)malloc(sizeof(PARTITION));
221 if(!strcmp(partitions[i]->dev, dev))
223 /* Found it! Make it the active one */
224 active = partitions[i];
225 *channel = alloc_io_channel(active);
233 * Drive number & optionally partn number
240 part->phys += toupper(tmp[2]) - 'A';
242 * Do we have the partition number?
245 part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0;
252 part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0;
254 part->phys = 0x00; /* We'll assume /dev/fd0 */
258 _dio_error = ERR_BADDEV;
262 if(part->phys < 0x80)
264 /* We don't support floppies for now */
265 _dio_error = ERR_NOTSUPP;
269 part->dev = strdup(dev);
272 * Get drive's geometry
274 _dio_hw_error = biosdisk(DISK_GET_GEOMETRY,
279 1, /* just one sector */
284 _dio_error = ERR_HARDWARE;
291 * Calculate the geometry
293 part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1);
294 part->heads = sec[3] + 1;
295 part->sects = sec[0] & 0x3F;
298 * Now that we know all we need, let's look for the partition
300 _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec);
304 _dio_error = ERR_HARDWARE;
310 pent = (PTABLE_ENTRY*)&sec[0x1BE];
311 pent = scan_partition_table(pent, part->phys, &part->pno);
315 _dio_error = part->pno == 0xFE ? ERR_EMPTYPART :
316 part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS;
323 * Calculate the remaining figures
326 unsigned long fsec, fhead, fcyl;
328 fsec = (unsigned long)(pent->start_sec & 0x3F);
329 fhead = (unsigned long)pent->start_head;
330 fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl;
331 part->start = fsec + fhead * part->sects + fcyl *
332 (part->heads * part->sects) - 1;
333 part->len = pent->size;
337 * Add the partition to the table
339 newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart);
344 partitions = newparts;
345 partitions[npart++] = active = part;
348 * Now alloc all libe2fs structures
350 *channel = alloc_io_channel(active);
357 static errcode_t dos_close(io_channel channel)
365 static errcode_t dos_set_blksize(io_channel channel, int blksize)
367 channel->block_size = blksize;
372 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
373 int count, void *buf)
380 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
381 part = (PARTITION*)channel->private_data;
383 size = (size_t)((count < 0) ? -count : count * channel->block_size);
384 loc = (ext2_loff_t) block * channel->block_size;
386 lba2chs(loc, &chs, part);
388 * Potential bug here:
389 * If DJGPP is used then reads of >18 sectors will fail!
390 * Have to rewrite biosdisk.
392 _dio_hw_error = biosdisk(DISK_READ,
397 size < 512 ? 1 : size/512,
402 _dio_error = ERR_HARDWARE;
409 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
410 int count, const void *buf)
417 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
418 part = (PARTITION*)channel->private_data;
421 size = (size_t)channel->block_size;
425 size = (size_t)-count;
427 size = (size_t)(count * channel->block_size);
430 loc = (ext2_loff_t)block * channel->block_size;
431 lba2chs(loc, &chs, part);
432 _dio_hw_error = biosdisk(DISK_WRITE,
437 size < 512 ? 1 : size/512,
442 _dio_error = ERR_HARDWARE;
452 static errcode_t dos_flush(io_channel channel)
455 * No buffers, no flush...