phat32.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 * 4;
00086 
00087     *sect = vol->vol_tab_sect[tabnum] + tabpos / vol->vol_sectsz;
00088     *pos = tabpos % vol->vol_sectsz;
00089 }
00090 
00100 int Phat32GetClusterLink(NUTDEVICE * dev, u_long clust, u_long * link)
00101 {
00102     u_long sect, pos;
00103     int sbn;
00104     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00105 
00106     /* Do not seek beyond the end of the chain. */
00107     if (clust >= (PHATEOC & PHAT32CMASK)) {
00108         return -1;
00109     }
00110 
00111     /* Load the sector that contains the table entry. */
00112     PhatTableLoc(vol, clust, 0, &sect, &pos);
00113     if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
00114         return -1;
00115     }
00116 
00117     /* Get the 32 bit link value. */
00118     *link = vol->vol_buf[sbn].sect_data[pos];
00119     *link += (u_long)(vol->vol_buf[sbn].sect_data[pos + 1]) << 8;
00120     *link += (u_long)(vol->vol_buf[sbn].sect_data[pos + 2]) << 16;
00121     *link += (u_long)(vol->vol_buf[sbn].sect_data[pos + 3]) << 24;
00122 
00123     return 0;
00124 }
00125 
00135 int Phat32SetClusterLink(NUTDEVICE * dev, u_long clust, u_long link)
00136 {
00137     int tabnum;
00138     u_long sect;
00139     u_long pos;
00140     int sbn;
00141     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00142 
00143     for (tabnum = 0; tabnum < 2 && vol->vol_tab_sect[tabnum]; tabnum++) {
00144         link &= PHAT32CMASK;
00145 
00146         PhatTableLoc(vol, clust, tabnum, &sect, &pos);
00147         if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
00148             return -1;
00149         }
00150         vol->vol_buf[sbn].sect_data[pos] = (u_char) link;
00151         vol->vol_buf[sbn].sect_data[pos + 1] = (u_char) (link >> 8);
00152         vol->vol_buf[sbn].sect_data[pos + 2] = (u_char) (link >> 16);
00153         vol->vol_buf[sbn].sect_data[pos + 3] = (u_char) (link >> 24);
00154         vol->vol_buf[sbn].sect_dirty = 1;
00155     }
00156     return 0;
00157 }
00158 
00167 int Phat32ReleaseChain(NUTDEVICE * dev, u_long first)
00168 {
00169     u_long next;
00170     PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00171 
00172     while (first < (PHATEOC & PHAT32CMASK)) {
00173         if (Phat32GetClusterLink(dev, first, &next)) {
00174             /* Read error. */
00175             return -1;
00176         }
00177         if (next < 2) {
00178             /* Incomplete chain, should not happen. */
00179             break;
00180         }
00181         if (Phat32SetClusterLink(dev, first, 0)) {
00182             /* Write error. */
00183             return -1;
00184         }
00185         vol->vol_numfree++;
00186         first = next;
00187     }
00188     return 0;
00189 }
00190 

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