1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <config.h>
27#include <malloc.h>
28
29#include "dos.h"
30#include "fdos.h"
31
32
33
34
35
36
37static int fill_fs (BootSector_t *boot, Fs_t *fs)
38{
39
40 fs -> fat_start = __le16_to_cpu (boot -> nrsvsect);
41 fs -> fat_len = __le16_to_cpu (boot -> fatlen);
42 fs -> nb_fat = boot -> nfat;
43
44 fs -> dir_start = fs -> fat_start + fs -> nb_fat * fs -> fat_len;
45 fs -> dir_len = __le16_to_cpu (boot -> dirents) * MDIR_SIZE / SZ_STD_SECTOR;
46 fs -> cluster_size = boot -> clsiz;
47 fs -> num_clus = (fs -> tot_sectors - fs -> dir_start - fs -> dir_len) / fs -> cluster_size;
48
49 return (0);
50}
51
52
53
54
55
56int fs_init (Fs_t *fs)
57{
58 BootSector_t *boot;
59
60
61 if (dev_open () < 0) {
62 PRINTF ("Unable to initialize the fdc\n");
63 return (-1);
64 }
65 init_subdir ();
66
67
68 if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) {
69 PRINTF ("Unable to allocate space for boot sector\n");
70 return (-1);
71 }
72
73
74 if (dev_read (boot, 0, 1)){
75 PRINTF ("Error during boot sector read\n");
76 free (boot);
77 return (-1);
78 }
79
80
81 if (boot -> jump [0] != JUMP_0_1 && boot -> jump [0] != JUMP_0_2) {
82 PRINTF ("Not a DOS diskette\n");
83 free (boot);
84 return (-1);
85 }
86
87 if (boot -> descr < MEDIA_STD) {
88
89 PRINTF ("unrecognized diskette type\n");
90 free (boot);
91 return (-1);
92 }
93
94 if (check_dev (boot, fs) < 0) {
95 PRINTF ("Bad diskette\n");
96 free (boot);
97 return (-1);
98 }
99
100 if (fill_fs (boot, fs) < 0) {
101 free (boot);
102
103 return (-1);
104 }
105
106
107 if (read_fat (boot, fs) < 0) {
108 free (boot);
109 return (-1);
110 }
111
112 free (boot);
113 return (0);
114}
115