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