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