| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * dosio.c -- Disk I/O module for the ext2fs/DOS library. |
| 3 | * |
| 4 | * Copyright (c) 1997 by Theodore Ts'o. |
| 5 | * |
| 6 | * Copyright (c) 1997 Mark Habersack |
| 7 | * This file may be distributed under the terms of the GNU Public License. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <bios.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <ctype.h> |
| 16 | #include <io.h> |
| 17 | #ifdef HAVE_ERRNO_H |
| 18 | #include <errno.h> |
| 19 | #endif |
| 20 | |
| 21 | #include <linux/types.h> |
| 22 | #include "utils.h" |
| 23 | #include "dosio.h" |
| 24 | #include "et/com_err.h" |
| 25 | #include "ext2_err.h" |
| 26 | #include "ext2fs/io.h" |
| 27 | |
| 28 | /* |
| 29 | * Some helper macros |
| 30 | */ |
| 31 | #define LINUX_EXT2FS 0x83 |
| 32 | #define LINUX_SWAP 0x82 |
| 33 | #define WRITE_ERR(_msg_) write(2, _msg_, strlen(_msg_)) |
| 34 | #define WRITE_ERR_S(_msg_) write(2, _msg_, sizeof(_msg_)) |
| 35 | |
| 36 | /* |
| 37 | * Exported variables |
| 38 | */ |
| 39 | unsigned long _dio_error; |
| 40 | unsigned long _dio_hw_error; |
| 41 | |
| 42 | /* |
| 43 | * Array of all opened partitions |
| 44 | */ |
| 45 | static PARTITION **partitions = NULL; |
| 46 | static unsigned short npart = 0; /* Number of mapped partitions */ |
| 47 | static PARTITION *active = NULL; |
| 48 | |
| 49 | /* |
| 50 | * I/O Manager routine prototypes |
| 51 | */ |
| 52 | static errcode_t dos_open(const char *dev, int flags, io_channel *channel); |
| 53 | static errcode_t dos_close(io_channel channel); |
| 54 | static errcode_t dos_set_blksize(io_channel channel, int blksize); |
| 55 | static errcode_t dos_read_blk(io_channel channel, unsigned long block, |
| 56 | int count, void *buf); |
| 57 | static errcode_t dos_write_blk(io_channel channel, unsigned long block, |
| 58 | int count, const void *buf); |
| 59 | static errcode_t dos_flush(io_channel channel); |
| 60 | |
| 61 | static struct struct_io_manager struct_dos_manager = { |
| 62 | EXT2_ET_MAGIC_IO_MANAGER, |
| 63 | "DOS I/O Manager", |
| 64 | dos_open, |
| 65 | dos_close, |
| 66 | dos_set_blksize, |
| 67 | dos_read_blk, |
| 68 | dos_write_blk, |
| 69 | dos_flush |
| 70 | }; |
| 71 | io_manager dos_io_manager = &struct_dos_manager; |
| 72 | |
| 73 | /* |
| 74 | * Macro taken from unix_io.c |
| 75 | */ |
| 76 | /* |
| 77 | * For checking structure magic numbers... |
| 78 | */ |
| 79 | |
| 80 | #define EXT2_CHECK_MAGIC(struct, code) \ |
| 81 | if ((struct)->magic != (code)) return (code) |
| 82 | |
| 83 | /* |
| 84 | * Calculates a CHS address of a sector from its LBA |
| 85 | * offset for the given partition. |
| 86 | */ |
| 87 | static void lba2chs(unsigned long lba_addr, CHS *chs, PARTITION *part) |
| 88 | { |
| 89 | unsigned long abss; |
| 90 | |
| 91 | chs->offset = lba_addr & 0x000001FF; |
| 92 | abss = (lba_addr >> 9) + part->start; |
| 93 | chs->cyl = abss / (part->sects * part->heads); |
| 94 | chs->head = (abss / part->sects) % part->heads; |
| 95 | chs->sector = (abss % part->sects) + 1; |
| 96 | } |
| 97 | |
| 98 | #ifdef __TURBOC__ |
| 99 | #pragma argsused |
| 100 | #endif |
| 101 | /* |
| 102 | * Scans the passed partition table looking for *pno partition |
| 103 | * that has LINUX_EXT2FS type. |
| 104 | * |
| 105 | * TODO: |
| 106 | * For partition numbers >5 Linux uses DOS extended partitions - |
| 107 | * dive into them an return an appropriate entry. Also dive into |
| 108 | * extended partitions when scanning for a first Linux/ext2fs. |
| 109 | */ |
| 110 | static PTABLE_ENTRY *scan_partition_table(PTABLE_ENTRY *pentry, |
| 111 | unsigned short phys, |
| 112 | unsigned char *pno) |
| 113 | { |
| 114 | unsigned i; |
| 115 | |
| 116 | if(*pno != 0xFF && *pno >= 5) |
| 117 | return NULL; /* We don't support extended partitions for now */ |
| 118 | |
| 119 | if(*pno != 0xFF) |
| 120 | { |
| 121 | if(pentry[*pno].type == LINUX_EXT2FS) |
| 122 | return &pentry[*pno]; |
| 123 | else |
| 124 | { |
| 125 | if(!pentry[*pno].type) |
| 126 | *pno = 0xFE; |
| 127 | else if(pentry[*pno].type == LINUX_SWAP) |
| 128 | *pno = 0xFD; |
| 129 | return NULL; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | for(i = 0; i < 4; i++) |
| 134 | if(pentry[i].type == LINUX_EXT2FS) |
| 135 | { |
| 136 | *pno = i; |
| 137 | return &pentry[i]; |
| 138 | } |
| 139 | |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Allocate libext2fs structures associated with I/O manager |
| 145 | */ |
| 146 | static io_channel alloc_io_channel(PARTITION *part) |
| 147 | { |
| 148 | io_channel ioch; |
| 149 | |
| 150 | ioch = (io_channel)malloc(sizeof(struct struct_io_channel)); |
| 151 | if (!ioch) |
| 152 | return NULL; |
| 153 | memset(ioch, 0, sizeof(struct struct_io_channel)); |
| 154 | ioch->magic = EXT2_ET_MAGIC_IO_CHANNEL; |
| 155 | ioch->manager = dos_io_manager; |
| 156 | ioch->name = (char *)malloc(strlen(part->dev)+1); |
| 157 | if (!ioch->name) { |
| 158 | free(ioch); |
| 159 | return NULL; |
| 160 | } |
| 161 | strcpy(ioch->name, part->dev); |
| 162 | ioch->private_data = part; |
| 163 | ioch->block_size = 1024; /* The smallest ext2fs block size */ |
| 164 | ioch->read_error = 0; |
| 165 | ioch->write_error = 0; |
| 166 | |
| 167 | return ioch; |
| 168 | } |
| 169 | |
| 170 | #ifdef __TURBOC__ |
| 171 | #pragma argsused |
| 172 | #endif |
| 173 | /* |
| 174 | * Open the 'name' partition, initialize all information structures |
| 175 | * we need to keep and create libext2fs I/O manager. |
| 176 | */ |
| 177 | static errcode_t dos_open(const char *dev, int flags, io_channel *channel) |
| 178 | { |
| 179 | unsigned char *tmp, sec[512]; |
| 180 | PARTITION *part; |
| 181 | PTABLE_ENTRY *pent; |
| 182 | PARTITION **newparts; |
| 183 | |
| 184 | if(!dev) |
| 185 | { |
| 186 | _dio_error = ERR_BADDEV; |
| 187 | return EXT2_ET_BAD_DEVICE_NAME; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * First check whether the dev name is OK |
| 192 | */ |
| 193 | tmp = (unsigned char*)strrchr(dev, '/'); |
| 194 | if(!tmp) |
| 195 | { |
| 196 | _dio_error = ERR_BADDEV; |
| 197 | return EXT2_ET_BAD_DEVICE_NAME; |
| 198 | } |
| 199 | *tmp = 0; |
| 200 | if(strcmp(dev, "/dev")) |
| 201 | { |
| 202 | _dio_error = ERR_BADDEV; |
| 203 | return EXT2_ET_BAD_DEVICE_NAME; |
| 204 | } |
| 205 | *tmp++ = '/'; |
| 206 | |
| 207 | /* |
| 208 | * Check whether the partition data is already in cache |
| 209 | */ |
| 210 | |
| 211 | part = (PARTITION*)malloc(sizeof(PARTITION)); |
| 212 | if (!part) |
| 213 | return ENOMEM; |
| 214 | { |
| 215 | int i = 0; |
| 216 | |
| 217 | for(;i < npart; i++) |
| 218 | if(!strcmp(partitions[i]->dev, dev)) |
| 219 | { |
| 220 | /* Found it! Make it the active one */ |
| 221 | active = partitions[i]; |
| 222 | *channel = alloc_io_channel(active); |
| 223 | if (!*channel) |
| 224 | return ENOMEM; |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Drive number & optionally partn number |
| 231 | */ |
| 232 | switch(tmp[0]) |
| 233 | { |
| 234 | case 'h': |
| 235 | case 's': |
| 236 | part->phys = 0x80; |
| 237 | part->phys += toupper(tmp[2]) - 'A'; |
| 238 | /* |
| 239 | * Do we have the partition number? |
| 240 | */ |
| 241 | if(tmp[3]) |
| 242 | part->pno = isdigit((int)tmp[3]) ? tmp[3] - '0' - 1: 0; |
| 243 | else |
| 244 | part->pno = 0xFF; |
| 245 | break; |
| 246 | |
| 247 | case 'f': |
| 248 | if(tmp[2]) |
| 249 | part->phys = isdigit((int)tmp[2]) ? tmp[2] - '0' : 0; |
| 250 | else |
| 251 | part->phys = 0x00; /* We'll assume /dev/fd0 */ |
| 252 | break; |
| 253 | |
| 254 | default: |
| 255 | _dio_error = ERR_BADDEV; |
| 256 | return ENODEV; |
| 257 | } |
| 258 | |
| 259 | if(part->phys < 0x80) |
| 260 | { |
| 261 | /* We don't support floppies for now */ |
| 262 | _dio_error = ERR_NOTSUPP; |
| 263 | return EINVAL; |
| 264 | } |
| 265 | |
| 266 | part->dev = strdup(dev); |
| 267 | |
| 268 | /* |
| 269 | * Get drive's geometry |
| 270 | */ |
| 271 | _dio_hw_error = biosdisk(DISK_GET_GEOMETRY, |
| 272 | part->phys, |
| 273 | 0, /* head */ |
| 274 | 0, /* cylinder */ |
| 275 | 1, /* sector */ |
| 276 | 1, /* just one sector */ |
| 277 | sec); |
| 278 | |
| 279 | if(!HW_OK()) |
| 280 | { |
| 281 | _dio_error = ERR_HARDWARE; |
| 282 | if (part) |
| 283 | free(part); |
| 284 | return EFAULT; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Calculate the geometry |
| 289 | */ |
| 290 | part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1); |
| 291 | part->heads = sec[3] + 1; |
| 292 | part->sects = sec[0] & 0x3F; |
| 293 | |
| 294 | /* |
| 295 | * Now that we know all we need, let's look for the partition |
| 296 | */ |
| 297 | _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec); |
| 298 | |
| 299 | if(!HW_OK()) |
| 300 | { |
| 301 | _dio_error = ERR_HARDWARE; |
| 302 | if (part) |
| 303 | free(part); |
| 304 | return EFAULT; |
| 305 | } |
| 306 | |
| 307 | pent = (PTABLE_ENTRY*)&sec[0x1BE]; |
| 308 | pent = scan_partition_table(pent, part->phys, &part->pno); |
| 309 | |
| 310 | if(!pent) |
| 311 | { |
| 312 | _dio_error = part->pno == 0xFE ? ERR_EMPTYPART : |
| 313 | part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS; |
| 314 | if (part) |
| 315 | free(part); |
| 316 | return ENODEV; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Calculate the remaining figures |
| 321 | */ |
| 322 | { |
| 323 | unsigned long fsec, fhead, fcyl; |
| 324 | |
| 325 | fsec = (unsigned long)(pent->start_sec & 0x3F); |
| 326 | fhead = (unsigned long)pent->start_head; |
| 327 | fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl; |
| 328 | part->start = fsec + fhead * part->sects + fcyl * |
| 329 | (part->heads * part->sects) - 1; |
| 330 | part->len = pent->size; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Add the partition to the table |
| 335 | */ |
| 336 | newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart); |
| 337 | if (!newparts) { |
| 338 | free(part); |
| 339 | return ENOMEM; |
| 340 | } |
| 341 | partitions = newparts; |
| 342 | partitions[npart++] = active = part; |
| 343 | |
| 344 | /* |
| 345 | * Now alloc all libe2fs structures |
| 346 | */ |
| 347 | *channel = alloc_io_channel(active); |
| 348 | if (!*channel) |
| 349 | return ENOMEM; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static errcode_t dos_close(io_channel channel) |
| 355 | { |
| 356 | if (channel->name) |
| 357 | free(channel->name); |
| 358 | if (channel) |
| 359 | free(channel); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static errcode_t dos_set_blksize(io_channel channel, int blksize) |
| 365 | { |
| 366 | channel->block_size = blksize; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static errcode_t dos_read_blk(io_channel channel, unsigned long block, |
| 372 | int count, void *buf) |
| 373 | { |
| 374 | PARTITION *part; |
| 375 | size_t size; |
| 376 | ext2_loff_t loc; |
| 377 | CHS chs; |
| 378 | |
| 379 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 380 | part = (PARTITION*)channel->private_data; |
| 381 | |
| 382 | size = (size_t)((count < 0) ? -count : count * channel->block_size); |
| 383 | loc = (ext2_loff_t) block * channel->block_size; |
| 384 | |
| 385 | lba2chs(loc, &chs, part); |
| 386 | /* |
| 387 | * Potential bug here: |
| 388 | * If DJGPP is used then reads of >18 sectors will fail! |
| 389 | * Have to rewrite biosdisk. |
| 390 | */ |
| 391 | _dio_hw_error = biosdisk(DISK_READ, |
| 392 | part->phys, |
| 393 | chs.head, |
| 394 | chs.cyl, |
| 395 | chs.sector, |
| 396 | size < 512 ? 1 : size/512, |
| 397 | buf); |
| 398 | |
| 399 | if(!HW_OK()) |
| 400 | { |
| 401 | _dio_error = ERR_HARDWARE; |
| 402 | return EFAULT; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static errcode_t dos_write_blk(io_channel channel, unsigned long block, |
| 409 | int count, const void *buf) |
| 410 | { |
| 411 | PARTITION *part; |
| 412 | size_t size; |
| 413 | ext2_loff_t loc; |
| 414 | CHS chs; |
| 415 | |
| 416 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
| 417 | part = (PARTITION*)channel->private_data; |
| 418 | |
| 419 | if(count == 1) |
| 420 | size = (size_t)channel->block_size; |
| 421 | else |
| 422 | { |
| 423 | if (count < 0) |
| 424 | size = (size_t)-count; |
| 425 | else |
| 426 | size = (size_t)(count * channel->block_size); |
| 427 | } |
| 428 | |
| 429 | loc = (ext2_loff_t)block * channel->block_size; |
| 430 | lba2chs(loc, &chs, part); |
| 431 | _dio_hw_error = biosdisk(DISK_WRITE, |
| 432 | part->phys, |
| 433 | chs.head, |
| 434 | chs.cyl, |
| 435 | chs.sector, |
| 436 | size < 512 ? 1 : size/512, |
| 437 | (void*)buf); |
| 438 | |
| 439 | if(!HW_OK()) |
| 440 | { |
| 441 | _dio_error = ERR_HARDWARE; |
| 442 | return EFAULT; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | #ifdef __TURBOC__ |
| 449 | #pragma argsused |
| 450 | #endif |
| 451 | static errcode_t dos_flush(io_channel channel) |
| 452 | { |
| 453 | /* |
| 454 | * No buffers, no flush... |
| 455 | */ |
| 456 | return 0; |
| 457 | } |