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