Initial import package mtools: Programs for accessing MS-DOS disks without mounting...
[profile/ivi/mtools.git] / init.c
1 /*  Copyright 1986-1992 Emmet P. Gray.
2  *  Copyright 1996-2002,2006-2009 Alain Knaff.
3  *  This file is part of mtools.
4  *
5  *  Mtools is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  Mtools is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Initialize an MSDOS diskette.  Read the boot sector, and switch to the
19  * proper floppy disk device to match the format on the disk.  Sets a bunch
20  * of global variables.  Returns 0 on success, or 1 on failure.
21  */
22
23 #include "sysincludes.h"
24 #include "msdos.h"
25 #include "stream.h"
26 #include "mtools.h"
27 #include "fsP.h"
28 #include "plain_io.h"
29 #include "floppyd_io.h"
30 #include "xdf_io.h"
31 #include "buffer.h"
32 #include "file_name.h"
33
34 #define FULL_CYL
35
36 unsigned int num_clus;                  /* total number of cluster */
37
38
39 /*
40  * Read the boot sector.  We glean the disk parameters from this sector.
41  */
42 static int read_boot(Stream_t *Stream, union bootsector * boot, int size)
43 {       
44         /* read the first sector, or part of it */
45         if(!size)
46                 size = BOOTSIZE;
47         if(size > MAX_BOOT)
48                 size = MAX_BOOT;
49
50         if (force_read(Stream, boot->characters, 0, size) != size)
51                 return -1;
52         return 0;
53 }
54
55 static int fs_flush(Stream_t *Stream)
56 {
57         DeclareThis(Fs_t);
58
59         fat_write(This);
60         return 0;
61 }
62
63 static doscp_t *get_dosConvert(Stream_t *Stream)
64 {
65   DeclareThis(Fs_t);
66   return This->cp;
67 }
68
69 Class_t FsClass = {
70         read_pass_through, /* read */
71         write_pass_through, /* write */
72         fs_flush, 
73         fs_free, /* free */
74         0, /* set geometry */
75         get_data_pass_through,
76         0, /* pre allocate */
77         get_dosConvert, /* dosconvert */
78 };
79
80 static int get_media_type(Stream_t *St, union bootsector *boot)
81 {
82         int media;
83
84         media = boot->boot.descr;
85         if(media < 0xf0){
86                 char temp[512];
87                 /* old DOS disk. Media descriptor in the first FAT byte */
88                 /* old DOS disk always have 512-byte sectors */
89                 if (force_read(St,temp,(mt_off_t) 512,512) == 512)
90                         media = (unsigned char) temp[0];
91                 else
92                         media = 0;
93         } else
94                 media += 0x100;
95         return media;
96 }
97
98
99 Stream_t *GetFs(Stream_t *Fs)
100 {
101         while(Fs && Fs->Class != &FsClass)
102                 Fs = Fs->Next;
103         return Fs;
104 }
105
106 Stream_t *find_device(char drive, int mode, struct device *out_dev,
107                       union bootsector *boot,
108                       char *name, int *media, mt_size_t *maxSize,
109                       int *isRop)
110 {
111         char errmsg[200];
112         Stream_t *Stream;
113         struct device *dev;
114         int r;
115         int isRo=0;
116
117         Stream = NULL;
118         sprintf(errmsg, "Drive '%c:' not supported", drive);    
119                                         /* open the device */
120         for (dev=devices; dev->name; dev++) {
121                 FREE(&Stream);
122                 if (dev->drive != drive)
123                         continue;
124                 *out_dev = *dev;
125                 expand(dev->name,name);
126 #ifdef USING_NEW_VOLD
127                 strcpy(name, getVoldName(dev, name));
128 #endif
129
130                 Stream = 0;
131                 if(out_dev->misc_flags & FLOPPYD_FLAG) {
132                     Stream = 0;
133 #ifdef USE_FLOPPYD
134                     Stream = FloppydOpen(out_dev, dev, name, mode,
135                                          errmsg, 0, 1);
136                     if(Stream && maxSize)
137                         *maxSize = max_off_t_31;
138 #endif
139                 } else {
140
141 #ifdef USE_XDF
142                     Stream = XdfOpen(out_dev, name, mode, errmsg, 0);
143                     if(Stream) {
144                         out_dev->use_2m = 0x7f;
145                         if(maxSize)
146                             *maxSize = max_off_t_31;
147                     }
148 #endif
149
150                 
151                     if (!Stream)
152                         Stream = SimpleFileOpen(out_dev, dev, name,
153                                                 isRop ? mode | O_RDWR: mode,
154                                                 errmsg, 0, 1, maxSize);
155                 
156                     if(Stream) {
157                         isRo=0;
158                     } else if(isRop &&
159                        (errno == EPERM || errno == EACCES || errno == EROFS)) {
160                         Stream = SimpleFileOpen(out_dev, dev, name,
161                                                 mode | O_RDONLY,
162                                                 errmsg, 0, 1, maxSize);
163                         if(Stream) {
164                                 isRo=1;
165                         }
166                     }
167                 }
168
169                 if( !Stream)
170                     continue;
171
172                 /* read the boot sector */
173                 if ((r=read_boot(Stream, boot, out_dev->blocksize)) < 0){
174                         sprintf(errmsg,
175                                 "init %c: could not read boot sector",
176                                 drive);
177                         continue;
178                 }
179
180                 if((*media= get_media_type(Stream, boot)) <= 0xf0 ){
181                         if (boot->boot.jump[2]=='L')
182                                 sprintf(errmsg,
183                                         "diskette %c: is Linux LILO, not DOS",
184                                         drive);
185                         else
186                                 sprintf(errmsg,"init %c: non DOS media", drive);
187                         continue;
188                 }
189
190                 /* set new parameters, if needed */
191                 errno = 0;
192                 if(SET_GEOM(Stream, out_dev, dev, *media, boot)){
193                         if(errno)
194 #ifdef HAVE_SNPRINTF
195                                 snprintf(errmsg, 199,
196                                         "Can't set disk parameters for %c: %s",
197                                         drive, strerror(errno));
198 #else
199                                 sprintf(errmsg,
200                                         "Can't set disk parameters for %c: %s",
201                                         drive, strerror(errno));
202 #endif
203                         else
204                                 sprintf(errmsg,
205                                         "Can't set disk parameters for %c",
206                                         drive);
207                         continue;
208                 }
209                 break;
210         }
211
212         /* print error msg if needed */ 
213         if ( dev->drive == 0 ){
214                 FREE(&Stream);
215                 fprintf(stderr,"%s\n",errmsg);
216                 return NULL;
217         }
218         if(isRop)
219                 *isRop = isRo;
220         return Stream;
221 }
222
223
224 Stream_t *fs_init(char drive, int mode, int *isRop)
225 {
226         int blocksize;
227         int media,i;
228         int nhs;
229         int disk_size = 0;      /* In case we don't happen to set this below */
230         size_t tot_sectors;
231         char name[EXPAND_BUF];
232         int cylinder_size;
233         struct device dev;
234         mt_size_t maxSize;
235
236         union bootsector boot;
237
238         Fs_t *This;
239
240         This = New(Fs_t);
241         if (!This)
242                 return NULL;
243
244         This->Direct = NULL;
245         This->Next = NULL;
246         This->refs = 1;
247         This->Buffer = 0;
248         This->Class = &FsClass;
249         This->preallocatedClusters = 0;
250         This->lastFatSectorNr = 0;
251         This->lastFatAccessMode = 0;
252         This->lastFatSectorData = 0;
253         This->drive = drive;
254         This->last = 0;
255
256         This->Direct = find_device(drive, mode, &dev, &boot, name, &media,
257                                    &maxSize, isRop);
258         if(!This->Direct)
259                 return NULL;
260         
261         This->sector_size = WORD_S(secsiz);
262         if(This->sector_size > MAX_SECTOR){
263                 fprintf(stderr,"init %c: sector size too big\n", drive);
264                 return NULL;
265         }
266
267         i = log_2(This->sector_size);
268
269         if(i == 24) {
270                 fprintf(stderr,
271                         "init %c: sector size (%d) not a small power of two\n",
272                         drive, This->sector_size);
273                 return NULL;
274         }
275         This->sectorShift = i;
276         This->sectorMask = This->sector_size - 1;
277
278
279         cylinder_size = dev.heads * dev.sectors;
280         This->serialized = 0;
281         if ((media & ~7) == 0xf8){
282                 i = media & 3;
283                 This->cluster_size = old_dos[i].cluster_size;
284                 tot_sectors = cylinder_size * old_dos[i].tracks;
285                 This->fat_start = 1;
286                 This->fat_len = old_dos[i].fat_len;
287                 This->dir_len = old_dos[i].dir_len;
288                 This->num_fat = 2;
289                 This->sector_size = 512;
290                 This->sectorShift = 9;
291                 This->sectorMask = 511;
292                 This->fat_bits = 12;
293                 nhs = 0;
294         } else {
295                 struct label_blk_t *labelBlock;
296                 /*
297                  * all numbers are in sectors, except num_clus
298                  * (which is in clusters)
299                  */
300                 tot_sectors = WORD_S(psect);
301                 if(!tot_sectors) {
302                         tot_sectors = DWORD_S(bigsect);                 
303                         nhs = DWORD_S(nhs);
304                 } else
305                         nhs = WORD_S(nhs);
306
307
308                 This->cluster_size = boot.boot.clsiz;           
309                 This->fat_start = WORD_S(nrsvsect);
310                 This->fat_len = WORD_S(fatlen);
311                 This->dir_len = WORD_S(dirents) * MDIR_SIZE / This->sector_size;
312                 This->num_fat = boot.boot.nfat;
313
314                 if (This->fat_len) {
315                         labelBlock = &boot.boot.ext.old.labelBlock;
316                 } else {
317                         labelBlock = &boot.boot.ext.fat32.labelBlock;
318                 }
319
320                 if(labelBlock->dos4 == 0x29) {
321                         This->serialized = 1;
322                         This->serial_number = _DWORD(labelBlock->serial);
323                 }
324         }
325
326         if (tot_sectors >= (maxSize >> This->sectorShift)) {
327                 fprintf(stderr, "Big disks not supported on this architecture\n");
328                 exit(1);
329         }
330
331         if(!mtools_skip_check && (tot_sectors % dev.sectors)){
332                 fprintf(stderr,
333                         "Total number of sectors (%d) not a multiple of"
334                         " sectors per track (%d)!\n", (int) tot_sectors,
335                         dev.sectors);
336                 fprintf(stderr,
337                         "Add mtools_skip_check=1 to your .mtoolsrc file "
338                         "to skip this test\n");
339                 exit(1);
340         }
341
342         /* full cylinder buffering */
343 #ifdef FULL_CYL
344         disk_size = (dev.tracks) ? cylinder_size : 512;
345 #else /* FULL_CYL */
346         disk_size = (dev.tracks) ? dev.sectors : 512;
347 #endif /* FULL_CYL */
348
349 #if (defined OS_sysv4 && !defined OS_solaris)
350         /*
351          * The driver in Dell's SVR4 v2.01 is unreliable with large writes.
352          */
353         disk_size = 0;
354 #endif /* (defined sysv4 && !defined(solaris)) */
355
356 #ifdef OS_linux
357         disk_size = cylinder_size;
358 #endif
359
360 #if 1
361         if(disk_size > 256) {
362                 disk_size = dev.sectors;
363                 if(dev.sectors % 2)
364                         disk_size <<= 1;
365         }
366 #endif
367         if (disk_size % 2)
368                 disk_size *= 2;
369
370         if(!dev.blocksize || dev.blocksize < This->sector_size)
371                 blocksize = This->sector_size;
372         else
373                 blocksize = dev.blocksize;
374         if (disk_size)
375                 This->Next = buf_init(This->Direct,
376                                       8 * disk_size * blocksize,
377                                       disk_size * blocksize,
378                                       This->sector_size);
379         else
380                 This->Next = This->Direct;
381
382         if (This->Next == NULL) {
383                 perror("init: allocate buffer");
384                 This->Next = This->Direct;
385         }
386
387         /* read the FAT sectors */
388         if(fat_read(This, &boot, dev.fat_bits, tot_sectors, dev.use_2m&0x7f)){
389                 This->num_fat = 1;
390                 FREE(&This->Next);
391                 Free(This->Next);
392                 return NULL;
393         }
394
395         /* Set the codepage */
396         This->cp = cp_open(dev.codepage);
397         if(This->cp == NULL) {
398                 fs_free((Stream_t *)This);
399                 FREE(&This->Next);
400                 Free(This->Next);
401                 return NULL;
402         }
403
404         return (Stream_t *) This;
405 }
406
407 char getDrive(Stream_t *Stream)
408 {
409         DeclareThis(Fs_t);
410
411         if(This->Class != &FsClass)
412                 return getDrive(GetFs(Stream));
413         else
414                 return This->drive;
415 }
416
417 int fsPreallocateClusters(Fs_t *Fs, long size)
418 {
419         if(size > 0 && getfreeMinClusters((Stream_t *)Fs, size) != 1)
420                 return -1;
421
422         Fs->preallocatedClusters += size;
423         return 0;
424 }