phat16.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00061 #include <errno.h>
00062 
00063 #include <fs/phatfs.h>
00064 #include <fs/phatvol.h>
00065 #include <fs/phatio.h>
00066 
00071 
00083 static void PhatTableLoc(PHATVOL * vol, u_long clust, int tabnum, u_long * sect, u_long * pos)
00084 {
00085     u_long tabpos = clust * 2;
00086 
00087     *sect = vol->vol_tab_sect[tabnum] + tabpos / vol->vol_sectsz;
00088     *pos = tabpos % vol->vol_sectsz;
00089 }
00090 
00100 int Phat16GetClusterLink(NUTDEVICE * dev, u_long clust, u_long * link)
00101 {
00102     u_long sect;
00103     u_long pos;
00104     int sbn;
00105     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00106 
00107     /* Do not seek beyond the end of the chain. */
00108     if (clust >= (PHATEOC & PHAT16CMASK)) {
00109         return -1;
00110     }
00111 
00112     /* Load the sector that contains the table entry. */
00113     PhatTableLoc(vol, clust, 0, &sect, &pos);
00114     if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
00115         return -1;
00116     }
00117 
00118     /* Get the 16 bit link value. */
00119     *link = vol->vol_buf[sbn].sect_data[pos];
00120     *link += (u_long)(vol->vol_buf[sbn].sect_data[pos + 1]) << 8;
00121 
00122     return 0;
00123 }
00124 
00134 int Phat16SetClusterLink(NUTDEVICE * dev, u_long clust, u_long link)
00135 {
00136     int tabnum;
00137     u_long sect;
00138     u_long pos;
00139     int sbn;
00140     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00141 
00142     for (tabnum = 0; tabnum < 2 && vol->vol_tab_sect[tabnum]; tabnum++) {
00143         PhatTableLoc(vol, clust, tabnum, &sect, &pos);
00144         if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
00145             return -1;
00146         }
00147         vol->vol_buf[sbn].sect_data[pos] = (u_char) link;
00148         vol->vol_buf[sbn].sect_data[pos + 1] = (u_char) (link >> 8);
00149         vol->vol_buf[sbn].sect_dirty = 1;
00150     }
00151     return 0;
00152 }
00153 
00162 int Phat16ReleaseChain(NUTDEVICE * dev, u_long first)
00163 {
00164     u_long next;
00165     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00166 
00167     while (first < (PHATEOC & PHAT16CMASK)) {
00168         if (Phat16GetClusterLink(dev, first, &next)) {
00169             /* Read error. */
00170             return -1;
00171         }
00172         if (next < 2) {
00173             /* Incomplete chain, should not happen. */
00174             break;
00175         }
00176         if (Phat16SetClusterLink(dev, first, 0)) {
00177             /* Write error. */
00178             return -1;
00179         }
00180         vol->vol_numfree++;
00181         first = next;
00182     }
00183     return 0;
00184 }
00185 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/