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