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 .magic = EXT2_ET_MAGIC_IO_MANAGER,
66 .name = "DOS I/O Manager",
69 .set_blksize = dos_set_blksize,
70 .read_blk = dos_read_blk,
71 .write_blk = dos_write_blk,
75 io_manager dos_io_manager = &struct_dos_manager;
78 * Macro taken from unix_io.c
81 * For checking structure magic numbers...
84 #define EXT2_CHECK_MAGIC(struct, code) \
85 if ((struct)->magic != (code)) return (code)
88 * Calculates a CHS address of a sector from its LBA
89 * offset for the given partition.
91 static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part)
95 chs->offset = lba_addr & 0x000001FF;
96 abss = (lba_addr >> 9) + part->start;
97 chs->cyl = abss / (part->sects * part->heads);
98 chs->head = (abss / part->sects) % part->heads;
99 chs->sector = (abss % part->sects) + 1;
106 * Scans the passed partition table looking for *pno partition
107 * that has LINUX_EXT2FS type.
110 * For partition numbers >5 Linux uses DOS extended partitions -
111 * dive into them an return an appropriate entry. Also dive into
112 * extended partitions when scanning for a first Linux/ext2fs.
114 static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry,
120 if(*pno != 0xFF && *pno >= 5)
121 return NULL; /* We don't support extended partitions for now */
125 if(pentry[*pno].type == LINUX_EXT2FS)
126 return &pentry[*pno];
129 if(!pentry[*pno].type)
131 else if(pentry[*pno].type == LINUX_SWAP)
137 for(i = 0; i < 4; i++)
138 if(pentry[i].type == LINUX_EXT2FS)
148 * Allocate libext2fs structures associated with I/O manager
150 static io_channel alloc_io_channel(PARTITION *part)
154 ioch = (io_channel)malloc(sizeof(struct struct_io_channel));
157 memset(ioch, 0, sizeof(struct struct_io_channel));
158 ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL;
159 ioch->manager = dos_io_manager;
160 ioch->name = (char *)malloc(strlen(part->dev)+1);
165 strcpy(ioch->name, part->dev);
166 ioch->private_data = part;
167 ioch->block_size = 1024; /* The smallest ext2fs block size */
168 ioch->read_error = 0;
169 ioch->write_error = 0;
178 * Open the 'name' partition, initialize all information structures
179 * we need to keep and create libext2fs I/O manager.
181 static errcode_t dos_open(const char *dev, int flags, io_channel *channel)
183 unsigned char *tmp, sec[512];
186 PARTITION **newparts;
190 _dio_error = ERR_BADDEV;
191 return EXT2_ET_BAD_DEVICE_NAME;
195 * First check whether the dev name is OK
197 tmp = (unsigned char*)strrchr(dev, '/');
200 _dio_error = ERR_BADDEV;
201 return EXT2_ET_BAD_DEVICE_NAME;
204 if(strcmp(dev, "/dev"))
206 _dio_error = ERR_BADDEV;
207 return EXT2_ET_BAD_DEVICE_NAME;
212 * Check whether the partition data is already in cache
215 part = (PARTITION*)malloc(sizeof(PARTITION));
222 if(!strcmp(partitions[i]->dev, dev))
224 /* Found it! Make it the active one */
225 active = partitions[i];
226 *channel = alloc_io_channel(active);
234 * Drive number & optionally partn number
241 part->phys += toupper(tmp[2]) - 'A';
243 * Do we have the partition number?
246 part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0;
253 part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0;
255 part->phys = 0x00; /* We'll assume /dev/fd0 */
259 _dio_error = ERR_BADDEV;
263 if(part->phys < 0x80)
265 /* We don't support floppies for now */
266 _dio_error = ERR_NOTSUPP;
270 part->dev = strdup(dev);
273 * Get drive's geometry
275 _dio_hw_error = biosdisk(DISK_GET_GEOMETRY,
280 1, /* just one sector */
285 _dio_error = ERR_HARDWARE;
292 * Calculate the geometry
294 part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1);
295 part->heads = sec[3] + 1;
296 part->sects = sec[0] & 0x3F;
299 * Now that we know all we need, let's look for the partition
301 _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec);
305 _dio_error = ERR_HARDWARE;
311 pent = (PTABLE_ENTRY*)&sec[0x1BE];
312 pent = scan_partition_table(pent, part->phys, &part->pno);
316 _dio_error = part->pno == 0xFE ? ERR_EMPTYPART :
317 part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS;
324 * Calculate the remaining figures
327 unsigned long fsec, fhead, fcyl;
329 fsec = (unsigned long)(pent->start_sec & 0x3F);
330 fhead = (unsigned long)pent->start_head;
331 fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl;
332 part->start = fsec + fhead * part->sects + fcyl *
333 (part->heads * part->sects) - 1;
334 part->len = pent->size;
338 * Add the partition to the table
340 newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart);
345 partitions = newparts;
346 partitions[npart++] = active = part;
349 * Now alloc all libe2fs structures
351 *channel = alloc_io_channel(active);
358 static errcode_t dos_close(io_channel channel)
366 static errcode_t dos_set_blksize(io_channel channel, int blksize)
368 channel->block_size = blksize;
373 static errcode_t dos_read_blk(io_channel channel, unsigned long block,
374 int count, void *buf)
381 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
382 part = (PARTITION*)channel->private_data;
384 size = (size_t)((count < 0) ? -count : count * channel->block_size);
385 loc = (ext2_loff_t) block * channel->block_size;
387 lba2chs(loc, &chs, part);
389 * Potential bug here:
390 * If DJGPP is used then reads of >18 sectors will fail!
391 * Have to rewrite biosdisk.
393 _dio_hw_error = biosdisk(DISK_READ,
398 size < 512 ? 1 : size/512,
403 _dio_error = ERR_HARDWARE;
410 static errcode_t dos_write_blk(io_channel channel, unsigned long block,
411 int count, const void *buf)
418 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
419 part = (PARTITION*)channel->private_data;
422 size = (size_t)channel->block_size;
426 size = (size_t)-count;
428 size = (size_t)(count * channel->block_size);
431 loc = (ext2_loff_t)block * channel->block_size;
432 lba2chs(loc, &chs, part);
433 _dio_hw_error = biosdisk(DISK_WRITE,
438 size < 512 ? 1 : size/512,
443 _dio_error = ERR_HARDWARE;
453 static errcode_t dos_flush(io_channel channel)
456 * No buffers, no flush...