source: mainline/uspace/srv/fs/fat/fat_fat.c@ 6da81e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6da81e0 was 6da81e0, checked in by Jakub Jermar <jakub@…>, 15 years ago

Modify _fat_block_get() to return the "current" cluster number to the caller.

  • Property mode set to 100644
File size: 19.0 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file fat_fat.c
35 * @brief Functions that manipulate the File Allocation Tables.
36 */
37
38#include "fat_fat.h"
39#include "fat_dentry.h"
40#include "fat.h"
41#include "../../vfs/vfs.h"
42#include <libfs.h>
43#include <libblock.h>
44#include <errno.h>
45#include <byteorder.h>
46#include <align.h>
47#include <assert.h>
48#include <fibril_synch.h>
49#include <mem.h>
50
51/*
52 * Convenience macros for computing some frequently used values from the
53 * primitive boot sector members.
54 */
55#define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \
56 (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)
57#define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))
58
59#define CLBN2PBN(bs, cl, bn) \
60 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
61
62/**
63 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
64 * during allocation of clusters. The lock does not have to be held durring
65 * deallocation of clusters.
66 */
67static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
68
69/** Walk the cluster chain.
70 *
71 * @param bs Buffer holding the boot sector for the file.
72 * @param dev_handle Device handle of the device with the file.
73 * @param firstc First cluster to start the walk with.
74 * @param lastc If non-NULL, output argument hodling the last cluster
75 * number visited.
76 * @param numc If non-NULL, output argument holding the number of
77 * clusters seen during the walk.
78 * @param max_clusters Maximum number of clusters to visit.
79 *
80 * @return EOK on success or a negative error code.
81 */
82int
83fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
84 fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
85{
86 block_t *b;
87 uint16_t clusters = 0;
88 fat_cluster_t clst = firstc;
89 int rc;
90
91 if (firstc == FAT_CLST_RES0) {
92 /* No space allocated to the file. */
93 if (lastc)
94 *lastc = firstc;
95 if (numc)
96 *numc = 0;
97 return EOK;
98 }
99
100 while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
101 aoff64_t fsec; /* sector offset relative to FAT1 */
102 unsigned fidx; /* FAT1 entry index */
103
104 assert(clst >= FAT_CLST_FIRST);
105 if (lastc)
106 *lastc = clst; /* remember the last cluster number */
107 fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs);
108 fidx = clst % (BPS(bs) / sizeof(fat_cluster_t));
109 /* read FAT1 */
110 rc = block_get(&b, dev_handle, RSCNT(bs) + fsec,
111 BLOCK_FLAGS_NONE);
112 if (rc != EOK)
113 return rc;
114 clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
115 assert(clst != FAT_CLST_BAD);
116 rc = block_put(b);
117 if (rc != EOK)
118 return rc;
119 clusters++;
120 }
121
122 if (lastc && clst < FAT_CLST_LAST1)
123 *lastc = clst;
124 if (numc)
125 *numc = clusters;
126
127 return EOK;
128}
129
130/** Read block from file located on a FAT file system.
131 *
132 * @param block Pointer to a block pointer for storing result.
133 * @param bs Buffer holding the boot sector of the file system.
134 * @param nodep FAT node.
135 * @param bn Block number.
136 * @param flags Flags passed to libblock.
137 *
138 * @return EOK on success or a negative error code.
139 */
140int
141fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
142 aoff64_t bn, int flags)
143{
144 if (!nodep->size)
145 return ELIMIT;
146
147 if (nodep->firstc == FAT_CLST_ROOT)
148 goto fall_through;
149
150 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
151 nodep->lastc_cached_valid) {
152 /*
153 * This is a request to read a block within the last cluster
154 * when fortunately we have the last cluster number cached.
155 */
156 return block_get(block, nodep->idx->dev_handle,
157 CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
158 }
159
160fall_through:
161 return _fat_block_get(block, bs, nodep->idx->dev_handle, nodep->firstc,
162 NULL, bn, flags);
163}
164
165/** Read block from file located on a FAT file system.
166 *
167 * @param block Pointer to a block pointer for storing result.
168 * @param bs Buffer holding the boot sector of the file system.
169 * @param dev_handle Device handle of the file system.
170 * @param fcl First cluster used by the file. Can be zero if the file
171 * is empty.
172 * @param clp If not NULL, address where the cluster containing bn
173 * will be stored.
174 * stored
175 * @param bn Block number.
176 * @param flags Flags passed to libblock.
177 *
178 * @return EOK on success or a negative error code.
179 */
180int
181_fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
182 fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
183{
184 uint16_t clusters;
185 unsigned max_clusters;
186 fat_cluster_t c;
187 int rc;
188
189 /*
190 * This function can only operate on non-zero length files.
191 */
192 if (fcl == FAT_CLST_RES0)
193 return ELIMIT;
194
195 if (fcl == FAT_CLST_ROOT) {
196 /* root directory special case */
197 assert(bn < RDS(bs));
198 rc = block_get(block, dev_handle,
199 RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
200 return rc;
201 }
202
203 max_clusters = bn / SPC(bs);
204 rc = fat_cluster_walk(bs, dev_handle, fcl, &c, &clusters, max_clusters);
205 if (rc != EOK)
206 return rc;
207 assert(clusters == max_clusters);
208
209 rc = block_get(block, dev_handle, CLBN2PBN(bs, c, bn), flags);
210
211 if (clp)
212 *clp = c;
213
214 return rc;
215}
216
217/** Fill the gap between EOF and a new file position.
218 *
219 * @param bs Buffer holding the boot sector for nodep.
220 * @param nodep FAT node with the gap.
221 * @param mcl First cluster in an independent cluster chain that will
222 * be later appended to the end of the node's own cluster
223 * chain. If pos is still in the last allocated cluster,
224 * this argument is ignored.
225 * @param pos Position in the last node block.
226 *
227 * @return EOK on success or a negative error code.
228 */
229int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
230{
231 block_t *b;
232 aoff64_t o, boundary;
233 int rc;
234
235 boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
236
237 /* zero out already allocated space */
238 for (o = nodep->size; o < pos && o < boundary;
239 o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
240 int flags = (o % BPS(bs) == 0) ?
241 BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
242 rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
243 if (rc != EOK)
244 return rc;
245 memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
246 b->dirty = true; /* need to sync node */
247 rc = block_put(b);
248 if (rc != EOK)
249 return rc;
250 }
251
252 if (o >= pos)
253 return EOK;
254
255 /* zero out the initial part of the new cluster chain */
256 for (o = boundary; o < pos; o += BPS(bs)) {
257 rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
258 NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
259 if (rc != EOK)
260 return rc;
261 memset(b->data, 0, min(BPS(bs), pos - o));
262 b->dirty = true; /* need to sync node */
263 rc = block_put(b);
264 if (rc != EOK)
265 return rc;
266 }
267
268 return EOK;
269}
270
271/** Get cluster from the first FAT.
272 *
273 * @param bs Buffer holding the boot sector for the file system.
274 * @param dev_handle Device handle for the file system.
275 * @param clst Cluster which to get.
276 * @param value Output argument holding the value of the cluster.
277 *
278 * @return EOK or a negative error code.
279 */
280int
281fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
282 fat_cluster_t clst, fat_cluster_t *value)
283{
284 block_t *b;
285 fat_cluster_t *cp;
286 int rc;
287
288 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno +
289 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
290 if (rc != EOK)
291 return rc;
292 cp = (fat_cluster_t *)b->data +
293 clst % (BPS(bs) / sizeof(fat_cluster_t));
294 *value = uint16_t_le2host(*cp);
295 rc = block_put(b);
296
297 return rc;
298}
299
300/** Set cluster in one instance of FAT.
301 *
302 * @param bs Buffer holding the boot sector for the file system.
303 * @param dev_handle Device handle for the file system.
304 * @param fatno Number of the FAT instance where to make the change.
305 * @param clst Cluster which is to be set.
306 * @param value Value to set the cluster with.
307 *
308 * @return EOK on success or a negative error code.
309 */
310int
311fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
312 fat_cluster_t clst, fat_cluster_t value)
313{
314 block_t *b;
315 fat_cluster_t *cp;
316 int rc;
317
318 assert(fatno < FATCNT(bs));
319 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno +
320 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
321 if (rc != EOK)
322 return rc;
323 cp = (fat_cluster_t *)b->data +
324 clst % (BPS(bs) / sizeof(fat_cluster_t));
325 *cp = host2uint16_t_le(value);
326 b->dirty = true; /* need to sync block */
327 rc = block_put(b);
328 return rc;
329}
330
331/** Replay the allocatoin of clusters in all shadow instances of FAT.
332 *
333 * @param bs Buffer holding the boot sector of the file system.
334 * @param dev_handle Device handle of the file system.
335 * @param lifo Chain of allocated clusters.
336 * @param nclsts Number of clusters in the lifo chain.
337 *
338 * @return EOK on success or a negative error code.
339 */
340int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
341 fat_cluster_t *lifo, unsigned nclsts)
342{
343 uint8_t fatno;
344 unsigned c;
345 int rc;
346
347 for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
348 for (c = 0; c < nclsts; c++) {
349 rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
350 c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
351 if (rc != EOK)
352 return rc;
353 }
354 }
355
356 return EOK;
357}
358
359/** Allocate clusters in all copies of FAT.
360 *
361 * This function will attempt to allocate the requested number of clusters in
362 * all instances of the FAT. The FAT will be altered so that the allocated
363 * clusters form an independent chain (i.e. a chain which does not belong to any
364 * file yet).
365 *
366 * @param bs Buffer holding the boot sector of the file system.
367 * @param dev_handle Device handle of the file system.
368 * @param nclsts Number of clusters to allocate.
369 * @param mcl Output parameter where the first cluster in the chain
370 * will be returned.
371 * @param lcl Output parameter where the last cluster in the chain
372 * will be returned.
373 *
374 * @return EOK on success, a negative error code otherwise.
375 */
376int
377fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
378 fat_cluster_t *mcl, fat_cluster_t *lcl)
379{
380 block_t *blk;
381 fat_cluster_t *lifo; /* stack for storing free cluster numbers */
382 unsigned found = 0; /* top of the free cluster number stack */
383 unsigned b, c, cl;
384 int rc;
385
386 lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
387 if (!lifo)
388 return ENOMEM;
389
390 /*
391 * Search FAT1 for unused clusters.
392 */
393 fibril_mutex_lock(&fat_alloc_lock);
394 for (b = 0, cl = 0; b < SF(bs); b++) {
395 rc = block_get(&blk, dev_handle, RSCNT(bs) + b,
396 BLOCK_FLAGS_NONE);
397 if (rc != EOK)
398 goto error;
399 for (c = 0; c < BPS(bs) / sizeof(fat_cluster_t); c++, cl++) {
400 /*
401 * Check if the cluster is physically there. This check
402 * becomes necessary when the file system is created
403 * with fewer total sectors than how many is inferred
404 * from the size of the file allocation table.
405 */
406 if ((cl >= 2) &&
407 ((cl - 2) * SPC(bs) + SSA(bs) >= TS(bs))) {
408 rc = block_put(blk);
409 if (rc != EOK)
410 goto error;
411 goto out;
412 }
413
414 fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
415 if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
416 /*
417 * The cluster is free. Put it into our stack
418 * of found clusters and mark it as non-free.
419 */
420 lifo[found] = cl;
421 *clst = (found == 0) ?
422 host2uint16_t_le(FAT_CLST_LAST1) :
423 host2uint16_t_le(lifo[found - 1]);
424 blk->dirty = true; /* need to sync block */
425 if (++found == nclsts) {
426 /* we are almost done */
427 rc = block_put(blk);
428 if (rc != EOK)
429 goto error;
430 /* update the shadow copies of FAT */
431 rc = fat_alloc_shadow_clusters(bs,
432 dev_handle, lifo, nclsts);
433 if (rc != EOK)
434 goto error;
435 *mcl = lifo[found - 1];
436 *lcl = lifo[0];
437 free(lifo);
438 fibril_mutex_unlock(&fat_alloc_lock);
439 return EOK;
440 }
441 }
442 }
443 rc = block_put(blk);
444 if (rc != EOK) {
445error:
446 fibril_mutex_unlock(&fat_alloc_lock);
447 free(lifo);
448 return rc;
449 }
450 }
451out:
452 fibril_mutex_unlock(&fat_alloc_lock);
453
454 /*
455 * We could not find enough clusters. Now we need to free the clusters
456 * we have allocated so far.
457 */
458 while (found--) {
459 rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
460 FAT_CLST_RES0);
461 if (rc != EOK) {
462 free(lifo);
463 return rc;
464 }
465 }
466
467 free(lifo);
468 return ENOSPC;
469}
470
471/** Free clusters forming a cluster chain in all copies of FAT.
472 *
473 * @param bs Buffer hodling the boot sector of the file system.
474 * @param dev_handle Device handle of the file system.
475 * @param firstc First cluster in the chain which is to be freed.
476 *
477 * @return EOK on success or a negative return code.
478 */
479int
480fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
481{
482 unsigned fatno;
483 fat_cluster_t nextc;
484 int rc;
485
486 /* Mark all clusters in the chain as free in all copies of FAT. */
487 while (firstc < FAT_CLST_LAST1) {
488 assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
489 rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
490 if (rc != EOK)
491 return rc;
492 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
493 rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
494 FAT_CLST_RES0);
495 if (rc != EOK)
496 return rc;
497 }
498
499 firstc = nextc;
500 }
501
502 return EOK;
503}
504
505/** Append a cluster chain to the last file cluster in all FATs.
506 *
507 * @param bs Buffer holding the boot sector of the file system.
508 * @param nodep Node representing the file.
509 * @param mcl First cluster of the cluster chain to append.
510 * @param lcl Last cluster of the cluster chain to append.
511 *
512 * @return EOK on success or a negative error code.
513 */
514int
515fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
516 fat_cluster_t lcl)
517{
518 dev_handle_t dev_handle = nodep->idx->dev_handle;
519 fat_cluster_t lastc;
520 uint16_t numc;
521 uint8_t fatno;
522 int rc;
523
524 if (nodep->lastc_cached_valid) {
525 lastc = nodep->lastc_cached_value;
526 nodep->lastc_cached_valid = false;
527 } else {
528 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lastc,
529 &numc, (uint16_t) -1);
530 if (rc != EOK)
531 return rc;
532
533 if (numc == 0) {
534 /* No clusters allocated to the node yet. */
535 nodep->firstc = mcl;
536 nodep->dirty = true; /* need to sync node */
537 return EOK;
538 }
539 }
540
541 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
542 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lastc,
543 mcl);
544 if (rc != EOK)
545 return rc;
546 }
547
548 nodep->lastc_cached_valid = true;
549 nodep->lastc_cached_value = lcl;
550
551 return EOK;
552}
553
554/** Chop off node clusters in all copies of FAT.
555 *
556 * @param bs Buffer holding the boot sector of the file system.
557 * @param nodep FAT node where the chopping will take place.
558 * @param lcl Last cluster which will remain in the node. If this
559 * argument is FAT_CLST_RES0, then all clusters will
560 * be chopped off.
561 *
562 * @return EOK on success or a negative return code.
563 */
564int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
565{
566 int rc;
567 dev_handle_t dev_handle = nodep->idx->dev_handle;
568
569 nodep->lastc_cached_valid = false;
570 if (lcl == FAT_CLST_RES0) {
571 /* The node will have zero size and no clusters allocated. */
572 rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
573 if (rc != EOK)
574 return rc;
575 nodep->firstc = FAT_CLST_RES0;
576 nodep->dirty = true; /* need to sync node */
577 } else {
578 fat_cluster_t nextc;
579 unsigned fatno;
580
581 rc = fat_get_cluster(bs, dev_handle, FAT1, lcl, &nextc);
582 if (rc != EOK)
583 return rc;
584
585 /* Terminate the cluster chain in all copies of FAT. */
586 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
587 rc = fat_set_cluster(bs, dev_handle, fatno, lcl,
588 FAT_CLST_LAST1);
589 if (rc != EOK)
590 return rc;
591 }
592
593 /* Free all following clusters. */
594 rc = fat_free_clusters(bs, dev_handle, nextc);
595 if (rc != EOK)
596 return rc;
597 }
598
599 nodep->lastc_cached_valid = true;
600 nodep->lastc_cached_value = lcl;
601
602 return EOK;
603}
604
605int
606fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
607{
608 int i;
609 block_t *b;
610 int rc;
611
612 for (i = 0; i < SPC(bs); i++) {
613 rc = _fat_block_get(&b, bs, dev_handle, c, NULL, i,
614 BLOCK_FLAGS_NOREAD);
615 if (rc != EOK)
616 return rc;
617 memset(b->data, 0, BPS(bs));
618 b->dirty = true;
619 rc = block_put(b);
620 if (rc != EOK)
621 return rc;
622 }
623
624 return EOK;
625}
626
627/** Perform basic sanity checks on the file system.
628 *
629 * Verify if values of boot sector fields are sane. Also verify media
630 * descriptor. This is used to rule out cases when a device obviously
631 * does not contain a fat file system.
632 */
633int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
634{
635 fat_cluster_t e0, e1;
636 unsigned fat_no;
637 int rc;
638
639 /* Check number of FATs. */
640 if (bs->fatcnt == 0)
641 return ENOTSUP;
642
643 /* Check total number of sectors. */
644
645 if (bs->totsec16 == 0 && bs->totsec32 == 0)
646 return ENOTSUP;
647
648 if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
649 bs->totsec16 != bs->totsec32)
650 return ENOTSUP;
651
652 /* Check media descriptor. Must be between 0xf0 and 0xff. */
653 if ((bs->mdesc & 0xf0) != 0xf0)
654 return ENOTSUP;
655
656 /* Check number of sectors per FAT. */
657 if (bs->sec_per_fat == 0)
658 return ENOTSUP;
659
660 /*
661 * Check that the root directory entries take up whole blocks.
662 * This check is rather strict, but it allows us to treat the root
663 * directory and non-root directories uniformly in some places.
664 * It can be removed provided that functions such as fat_read() are
665 * sanitized to support file systems with this property.
666 */
667 if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
668 uint16_t_le2host(bs->bps) != 0)
669 return ENOTSUP;
670
671 /* Check signature of each FAT. */
672
673 for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
674 rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
675 if (rc != EOK)
676 return EIO;
677
678 rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
679 if (rc != EOK)
680 return EIO;
681
682 /* Check that first byte of FAT contains the media descriptor. */
683 if ((e0 & 0xff) != bs->mdesc)
684 return ENOTSUP;
685
686 /*
687 * Check that remaining bits of the first two entries are
688 * set to one.
689 */
690 if ((e0 >> 8) != 0xff || e1 != 0xffff)
691 return ENOTSUP;
692 }
693
694 return EOK;
695}
696
697/**
698 * @}
699 */
Note: See TracBrowser for help on using the repository browser.