blob: 97ceef541952fae1349847f252527e7a8d2cb35e [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 Meyering45e338f2009-02-23 18:07:50 +0100281 free(part);
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000282 return EFAULT;
283 }
284
285 /*
286 * Calculate the geometry
287 */
288 part->cyls = (unsigned short)(((sec[0] >> 6) << 8) + sec[1] + 1);
289 part->heads = sec[3] + 1;
290 part->sects = sec[0] & 0x3F;
291
292 /*
293 * Now that we know all we need, let's look for the partition
294 */
295 _dio_hw_error = biosdisk(DISK_READ, part->phys, 0, 0, 1, 1, sec);
296
297 if(!HW_OK())
298 {
299 _dio_error = ERR_HARDWARE;
Jim Meyering45e338f2009-02-23 18:07:50 +0100300 free(part);
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000301 return EFAULT;
302 }
303
304 pent = (PTABLE_ENTRY*)&sec[0x1BE];
305 pent = scan_partition_table(pent, part->phys, &part->pno);
306
307 if(!pent)
308 {
309 _dio_error = part->pno == 0xFE ? ERR_EMPTYPART :
310 part->pno == 0xFD ? ERR_LINUXSWAP : ERR_NOTEXT2FS;
Jim Meyering45e338f2009-02-23 18:07:50 +0100311 free(part);
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000312 return ENODEV;
313 }
314
315 /*
316 * Calculate the remaining figures
317 */
318 {
319 unsigned long fsec, fhead, fcyl;
320
321 fsec = (unsigned long)(pent->start_sec & 0x3F);
322 fhead = (unsigned long)pent->start_head;
323 fcyl = ((pent->start_sec >> 6) << 8) + pent->start_cyl;
324 part->start = fsec + fhead * part->sects + fcyl *
325 (part->heads * part->sects) - 1;
326 part->len = pent->size;
327 }
328
329 /*
330 * Add the partition to the table
331 */
332 newparts = (PARTITION**)realloc(partitions, sizeof(PARTITION) * npart);
333 if (!newparts) {
334 free(part);
335 return ENOMEM;
336 }
337 partitions = newparts;
338 partitions[npart++] = active = part;
339
340 /*
341 * Now alloc all libe2fs structures
342 */
343 *channel = alloc_io_channel(active);
344 if (!*channel)
345 return ENOMEM;
346
347 return 0;
348}
349
350static errcode_t dos_close(io_channel channel)
351{
Jim Meyering45e338f2009-02-23 18:07:50 +0100352 free(channel->name);
353 free(channel);
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000354
355 return 0;
356}
357
358static errcode_t dos_set_blksize(io_channel channel, int blksize)
359{
360 channel->block_size = blksize;
361
362 return 0;
363}
364
365static errcode_t dos_read_blk(io_channel channel, unsigned long block,
366 int count, void *buf)
367{
368 PARTITION *part;
369 size_t size;
370 ext2_loff_t loc;
371 CHS chs;
372
373 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
374 part = (PARTITION*)channel->private_data;
375
376 size = (size_t)((count < 0) ? -count : count * channel->block_size);
377 loc = (ext2_loff_t) block * channel->block_size;
378
379 lba2chs(loc, &chs, part);
380 /*
381 * Potential bug here:
382 * If DJGPP is used then reads of >18 sectors will fail!
383 * Have to rewrite biosdisk.
384 */
385 _dio_hw_error = biosdisk(DISK_READ,
386 part->phys,
387 chs.head,
388 chs.cyl,
389 chs.sector,
390 size < 512 ? 1 : size/512,
391 buf);
392
393 if(!HW_OK())
394 {
395 _dio_error = ERR_HARDWARE;
396 return EFAULT;
397 }
398
399 return 0;
400}
401
402static errcode_t dos_write_blk(io_channel channel, unsigned long block,
403 int count, const void *buf)
404{
405 PARTITION *part;
406 size_t size;
407 ext2_loff_t loc;
408 CHS chs;
409
410 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
411 part = (PARTITION*)channel->private_data;
412
413 if(count == 1)
414 size = (size_t)channel->block_size;
415 else
416 {
417 if (count < 0)
418 size = (size_t)-count;
419 else
420 size = (size_t)(count * channel->block_size);
421 }
422
423 loc = (ext2_loff_t)block * channel->block_size;
424 lba2chs(loc, &chs, part);
425 _dio_hw_error = biosdisk(DISK_WRITE,
426 part->phys,
427 chs.head,
428 chs.cyl,
429 chs.sector,
430 size < 512 ? 1 : size/512,
431 (void*)buf);
432
433 if(!HW_OK())
434 {
435 _dio_error = ERR_HARDWARE;
436 return EFAULT;
437 }
438
439 return 0;
440}
441
442#ifdef __TURBOC__
443#pragma argsused
444#endif
445static errcode_t dos_flush(io_channel channel)
446{
447 /*
448 * No buffers, no flush...
449 */
450 return 0;
451}