tizen 2.3 release
[framework/system/deviced.git] / src / fsck-msdos / boot.c
1 /*
2  * Copyright (C) 1995, 1997 Wolfgang Solfrank
3  * Copyright (c) 1995 Martin Husemann
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Martin Husemann
16  *      and Wolfgang Solfrank.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33
34 #include <sys/cdefs.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <unistd.h>
41
42 #include "ext.h"
43 #include "fsutil.h"
44
45 int
46 readboot(dosfs, boot)
47         int dosfs;
48         struct bootblock *boot;
49 {
50         u_char block[DOSBOOTBLOCKSIZE];
51         u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
52         u_char backup[DOSBOOTBLOCKSIZE];
53         int ret = FSOK;
54
55         if (read(dosfs, block, sizeof block) < sizeof block) {
56                 perror("could not read boot block");
57                 exit(2);
58         }
59
60         if (block[510] != 0x55 || block[511] != 0xaa) {
61                 pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]);
62                 exit(2);
63         }
64
65         memset(boot, 0, sizeof *boot);
66         boot->ValidFat = -1;
67
68         /* decode bios parameter block */
69         boot->BytesPerSec = block[11] + (block[12] << 8);
70         boot->SecPerClust = block[13];
71         boot->ResSectors = block[14] + (block[15] << 8);
72         boot->FATs = block[16];
73         boot->RootDirEnts = block[17] + (block[18] << 8);
74         boot->Sectors = block[19] + (block[20] << 8);
75         boot->Media = block[21];
76         boot->FATsmall = block[22] + (block[23] << 8);
77         boot->SecPerTrack = block[24] + (block[25] << 8);
78         boot->Heads = block[26] + (block[27] << 8);
79         boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
80         boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
81
82         boot->FATsecs = boot->FATsmall;
83
84         if (!boot->RootDirEnts)
85                 boot->flags |= FAT32;
86         if (boot->flags & FAT32) {
87                 boot->FATsecs = block[36] + (block[37] << 8)
88                                 + (block[38] << 16) + (block[39] << 24);
89                 if (block[40] & 0x80)
90                         boot->ValidFat = block[40] & 0x0f;
91
92                 /* check version number: */
93                 if (block[42] || block[43]) {
94                         /* Correct?                             XXX */
95                         pfatal("Unknown file system version: %x.%x",
96                                block[43], block[42]);
97                         exit(2);
98                 }
99                 boot->RootCl = block[44] + (block[45] << 8)
100                                + (block[46] << 16) + (block[47] << 24);
101                 boot->FSInfo = block[48] + (block[49] << 8);
102                 boot->Backup = block[50] + (block[51] << 8);
103
104                 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
105                     != boot->FSInfo * boot->BytesPerSec
106                     || read(dosfs, fsinfo, sizeof fsinfo)
107                     != sizeof fsinfo) {
108                         perror("could not read fsinfo block");
109                         return FSFATAL;
110                 }
111                 if (memcmp(fsinfo, "RRaA", 4)
112                     || memcmp(fsinfo + 0x1e4, "rrAa", 4)
113                     || fsinfo[0x1fc]
114                     || fsinfo[0x1fd]
115                     || fsinfo[0x1fe] != 0x55
116                     || fsinfo[0x1ff] != 0xaa
117                     || fsinfo[0x3fc]
118                     || fsinfo[0x3fd]
119                     || fsinfo[0x3fe] != 0x55
120                     || fsinfo[0x3ff] != 0xaa) {
121                         pwarn("Invalid signature in fsinfo block\n");
122                         if (ask(1, "fix")) {
123                                 memcpy(fsinfo, "RRaA", 4);
124                                 memcpy(fsinfo + 0x1e4, "rrAa", 4);
125                                 fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
126                                 fsinfo[0x1fe] = 0x55;
127                                 fsinfo[0x1ff] = 0xaa;
128                                 fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
129                                 fsinfo[0x3fe] = 0x55;
130                                 fsinfo[0x3ff] = 0xaa;
131                                 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
132                                     != boot->FSInfo * boot->BytesPerSec
133                                     || write(dosfs, fsinfo, sizeof fsinfo)
134                                     != sizeof fsinfo) {
135                                         perror("Unable to write FSInfo");
136                                         return FSFATAL;
137                                 }
138                                 ret = FSBOOTMOD;
139                         } else
140                                 boot->FSInfo = 0;
141                 }
142                 if (boot->FSInfo) {
143                         boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
144                                        + (fsinfo[0x1ea] << 16)
145                                        + (fsinfo[0x1eb] << 24);
146                         boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
147                                        + (fsinfo[0x1ee] << 16)
148                                        + (fsinfo[0x1ef] << 24);
149                 }
150
151                 if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
152                     != boot->Backup * boot->BytesPerSec
153                     || read(dosfs, backup, sizeof backup) != sizeof  backup) {
154                         perror("could not read backup bootblock");
155                         return FSFATAL;
156                 }
157                 backup[65] = block[65];                         /* XXX */
158                 if (memcmp(block + 11, backup + 11, 79)) {
159                         char tmp[255];
160                         int i;
161
162                         /*
163                          * For now, lets not bail out if they don't match
164                          * It seems a lot of sdcards are formatted with
165                          * the backup either empty or containing garbage.
166                          */
167
168                         pwarn("Primary/Backup bootblock miscompare\n");
169
170                         strcpy(tmp, "");
171                         pwarn("Primary:\n");
172                         for (i = 0; i < 79; i++) {
173                                 char tmp2[16];
174                                 snprintf(tmp2, sizeof(tmp2), "%.2x ", block[11 + i]);
175                                 strcat(tmp, tmp2);
176                         }
177                         pwarn("%s\n", tmp);
178
179                         strcpy(tmp, "");
180                         pwarn("Backup:\n");
181                         for (i = 0; i < 79; i++) {
182                                 char tmp2[16];
183                                 snprintf(tmp2, sizeof(tmp2), "%.2x ", backup[11 + i]);
184                                 strcat(tmp, tmp2);
185                         }
186                         pwarn("%s\n", tmp);
187                 }
188                 /* Check backup FSInfo?                                 XXX */
189         }
190
191         if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
192                 pfatal("Invalid sector size: %u", boot->BytesPerSec);
193                 return FSFATAL;
194         }
195         if (boot->SecPerClust == 0) {
196                 pfatal("Invalid cluster size: %u", boot->SecPerClust);
197                 return FSFATAL;
198         }
199         if (boot->BytesPerSec == 0) {
200                 pfatal("Invalid sector size: %u", boot->BytesPerSec);
201                 return FSFATAL;
202         }
203         if (boot->FATs == 0) {
204                 pfatal("Invalid number of FATs: %u", boot->FATs);
205                 return FSFATAL;
206         }
207         if (boot->Sectors) {
208                 boot->HugeSectors = 0;
209                 boot->NumSectors = boot->Sectors;
210         } else
211                 boot->NumSectors = boot->HugeSectors;
212
213         boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
214             / boot->BytesPerSec
215             + boot->ResSectors
216             + boot->FATs * boot->FATsecs
217             - CLUST_FIRST * boot->SecPerClust;
218
219         boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
220
221         if (boot->flags&FAT32)
222                 boot->ClustMask = CLUST32_MASK;
223         else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
224                 boot->ClustMask = CLUST12_MASK;
225         else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
226                 boot->ClustMask = CLUST16_MASK;
227         else {
228                 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
229                        boot->NumClusters);
230                 return FSFATAL;
231         }
232
233         switch (boot->ClustMask) {
234         case CLUST32_MASK:
235                 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
236                 break;
237         case CLUST16_MASK:
238                 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
239                 break;
240         default:
241                 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
242                 break;
243         }
244
245         if (boot->NumFatEntries < boot->NumClusters) {
246                 pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
247                        boot->NumClusters, boot->FATsecs);
248                 return FSFATAL;
249         }
250         boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
251
252         boot->NumFiles = 1;
253         boot->NumFree = 0;
254
255         return ret;
256 }
257
258 int
259 writefsinfo(dosfs, boot)
260         int dosfs;
261         struct bootblock *boot;
262 {
263         u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
264
265         if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
266             != boot->FSInfo * boot->BytesPerSec
267             || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
268                 perror("could not read fsinfo block");
269                 return FSFATAL;
270         }
271         fsinfo[0x1e8] = (u_char)boot->FSFree;
272         fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
273         fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
274         fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
275         fsinfo[0x1ec] = (u_char)boot->FSNext;
276         fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
277         fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
278         fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
279         if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
280             != boot->FSInfo * boot->BytesPerSec
281             || write(dosfs, fsinfo, sizeof fsinfo)
282             != sizeof fsinfo) {
283                 perror("Unable to write FSInfo");
284                 return FSFATAL;
285         }
286         /*
287          * Technically, we should return FSBOOTMOD here.
288          *
289          * However, since Win95 OSR2 (the first M$ OS that has
290          * support for FAT32) doesn't maintain the FSINFO block
291          * correctly, it has to be fixed pretty often.
292          *
293          * Therefor, we handle the FSINFO block only informally,
294          * fixing it if necessary, but otherwise ignoring the
295          * fact that it was incorrect.
296          */
297         return 0;
298 }