⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flashfs.c

📁 flash驱动程序,Nor flash类型
💻 C
📖 第 1 页 / 共 5 页
字号:
                        else if (!flashfs_Find (slot, NULL, &isDynamic))                        {                            /* Open for read but no file */                            TRACE3("%C: '%s' not found\n", slot->name);                            m->errno = ENOENT;                        }                        if (ESUCCESS == m->errno)                        {                            /* initialise record, mark as busy */                            slot->inuse  = 1;                            slot->fp     = fp;                            slot->mmap   = NULL;                            fp->flags  |= _IOB_OPEN;                            fp->devuse = (PTR) slot;                        }                    }                    else                    {                        /* NULL names are not allowed */                        m->errno = EINVAL;                    }                }            }            else            {                /* establish a default error of no device (for no legitimate flash)     */                m->errno = ENOTFOUND;            }        }    }    /* Finally, reply to (OPEN) message */    sendreply(m);    return;} /* flashfs_msgOpen */static void flashfs_msgClose(ATMOS_MESSAGE *m){    MSG_D_CLOSE(c, m);    OPENFILE *optr = (OPENFILE *)c->fp->devuse;    m->errno = 0;    if (c->fp->flags & _IOB_OPEN)    {        if (optr->mmap != NULL)        {            free (optr->mmap);            optr->mmap = NULL;        }        if (c->fp->flags & _IOB_WRITE)        {            flashfs_PartitionChange (optr->partition);            m->errno = flashfs_CloseFile (optr);        }        optr->inuse = 0;        c->fp->flags = 0;        c->fp->qid = NULL;    }    sendreply(m);}static void flashfs_msgRead(ATMOS_MESSAGE *m){    MSG_D_READ(r, m);    FILE *fp = r->fp;    r->actual = 0;    if (fp->flags & _IOB_OPEN)    {        if (!(fp->flags & _IOB_DIRECTORY))        {            if (!(fp->flags & _IOB_WRITE))            {                OPENFILE *optr = (OPENFILE *)fp->devuse;                U32 bytes;                bytes = r->len;                if ((optr->fpos + bytes) > optr->fmax)                {                    fp->flags |= _IOB_EOF;                    bytes = (optr->fmax - optr->fpos);                }                flashfs_PartitionChange (optr->partition);                flash_read_fs_data(optr->datptr + optr->fpos, r->buffer, bytes);                optr->fpos += bytes;                r->actual = bytes;            }            else            {                /* Not possible to combine read and write */                m->errno = EACCES;            }        } /* not a directory, i.e. a file (r or w ) */        else        {            /* a directory, not a file */            m->errno = EISDIR;        }    } /* open fp */    else    {        /* not open */        m->errno = ENOENT;    }    sendreply(m);    return;}static void flashfs_msgWrite(ATMOS_MESSAGE *m){    MSG_D_WRITE(w, m);    FILE *fp = w->fp;    w->actual = 0;    if (fp->flags & _IOB_OPEN)    {        if (!(fp->flags & _IOB_DIRECTORY))        {            if (fp->flags & _IOB_WRITE)            {                OPENFILE *  slot = (OPENFILE *) fp->devuse;                U32         newPos;                /*                ** See how much space there is left in the flash.                */                if ((slot->fpos + w->len) > slot->limit)                {                    w->actual = slot->limit - slot->fpos;                }                else                {                    w->actual = w->len;                }                if (w->actual != 0)                {                    flashfs_PartitionChange (slot->partition);                    m->errno = flash_write_fs_data (w->buffer, slot->datptr + slot->fpos, w->actual);                }                else if (w->len != 0)                {                    /*                    ** Wrote zero but requested non-zero: no more space.                    */                    m->errno = ENOSPC;                }                newPos = slot->fpos + (U32) w->actual;                    /*                ** Ensure maximum limit of file set correctly                */                if (newPos > slot->fmax)                {                    slot->fmax = newPos;                }                TRACE4 ("Write '%s' Act %d Req %d Pos %d Len %d -> %d\n", slot->name, w->actual, w->len, slot->fpos, slot->fmax, m->errno);                slot->fpos = newPos;            }            else            {                m->errno = EACCES;            }        }        else        {            m->errno = EISDIR;        }    }    else    {        m->errno = ENOENT;    }    sendreply(m);    return;}static void flashfs_msgFlush(ATMOS_MESSAGE *m){    sendreply(m);    return;}static void flashfs_msgRemove (ATMOS_MESSAGE *m){    MSG_D_REMOVE (r, m);    OPENFILE     slot;    m->errno = flashfs_ParseFilename (r->name, slot.name, &slot.partition);    if (ESUCCESS == m->errno)    {        flashfs_PartitionChange (slot.partition);        m->errno = flashfs_RemoveFile (&slot);    }    sendreply(m);}static void flashfs_msgRename (ATMOS_MESSAGE *m){    MSG_D_RENAME (r, m);    OPENFILE     fslot;    m->errno = flashfs_ParseFilename (r->fname, fslot.name, &fslot.partition);    if (ESUCCESS == m->errno)    {        OPENFILE    tslot;        m->errno = flashfs_ParseFilename (r->tname, tslot.name, &tslot.partition);        if (ESUCCESS == m->errno)        {            /*            ** Both names must reference the same partition.            */            if (fslot.partition == tslot.partition)            {                BOOL    isDynamic;                flashfs_PartitionChange (fslot.partition);                if (flashfs_Find (&fslot, NULL, &isDynamic))                {                    /*                    ** New name must not exist                    */                    if (!flashfs_Find (&tslot, NULL, &isDynamic))                    {                        FLASHFS_DIRENT  entry;                        fslot.datptr = fslot.datptr - sizeof (entry) + sizeof (entry.data);                        flash_read_fs_data (fslot.datptr, (BYTE *) &entry, sizeof (entry));                        memcpy (entry.name, tslot.name, FLASHFS_NAME_LENGTH);                        m->errno = flash_write_fs_data ((BYTE *) &entry, fslot.datptr, sizeof (entry));                        if (ESUCCESS == m->errno)                        {                            m->errno = flashfs_program_header ();                        }                    }                    else                    {                        m->errno = EEXIST;                    }                }                else                {                    m->errno = ENOENT;                }            }            else            {                m->errno = EINVAL;            }        }    }    sendreply(m);}static void flashfs_msgSetattr(ATMOS_MESSAGE *m){    MSG_D_SETATTR(s, m);	PTR		    values [MAX_FLASHFS_ATTRIBUTES];    OPENFILE *  f;    assert (NULL != m);    assert (m->code == MSG_N_SETATTR);	assert (NULL != s->fp);	assert (NULL != s->fp->devuse);	assert (NULL != s->fp->devuse);    f = (OPENFILE *) s->fp->devuse;    flashfs_InitialiseAttributes (f, &values [0]);    /* setattribute_handler replies to message */    setattribute_handler(m, flashfsKeys, &values [0], MAX_FLASHFS_ATTRIBUTES - 1);    return;}static void flashfs_msgGetattr(ATMOS_MESSAGE *m){    MSG_D_GETATTR(g, m);	PTR		    values [MAX_FLASHFS_ATTRIBUTES];    OPENFILE *  f;    assert (NULL != m);    assert (m->code == MSG_N_GETATTR);	assert (NULL != g->fp);	assert (NULL != g->fp->devuse);	assert (NULL != g->fp->devuse);    f = (OPENFILE *) g->fp->devuse;    flashfs_InitialiseAttributes (f, &values [0]);    /* getattribute_handler replies to message */    getattribute_handler(m, flashfsKeys, &values [0]);    return;}/*********************************************************************** *                                                                     * *            MAGNOLIA                                                 * *            ========                                                 * *                                                                     * ***********************************************************************/static void flashfs_msgFseek(ATMOS_MESSAGE *m){    MSG_D_FSEEK(r, m);    FILE *fp  = r->fp;    if (fp->flags & _IOB_OPEN)    {        /* Don't support this for a directory fp */        if (!(r->fp->flags & _IOB_DIRECTORY))        {            OPENFILE *optr = (OPENFILE *)fp->devuse;            WORD offset    = (WORD) r->offset;            WORD origin    = (WORD) r->origin;            if (origin == SEEK_CUR)            {                offset += optr->fpos;            }            else if (origin == SEEK_END)            {                offset = optr->fmax - offset;            }            /*  else origin == SEEK_SET by implication ... */            /* Clip resulting position, make safe */            if (offset < 0)            {                offset = 0;            }            if (offset >= (WORD) optr->fmax)            {                offset = optr->fmax;                fp->flags |= _IOB_EOF;  /* set end of file marker   */            }            else            {                fp->flags &= ~_IOB_EOF; /* reset end of file marker */            }            optr->fpos = offset;        } /* not a directory, i.e. a file (r or w ) */        else        {            /* not supported for directories */            m->errno = EISDIR;        }    } /* open fp */    else    {        /* not open */        m->errno = ENOENT;    }    sendreply(m);}static void flashfs_msgFtell(ATMOS_MESSAGE *m){    MSG_D_FTELL(t, m);    FILE *fp  = t->fp;    if (fp->flags & _IOB_OPEN)    {        /* Don't support this for a dirtectory fp */        if (!(fp->flags & _IOB_DIRECTORY))        {            OPENFILE *optr = (OPENFILE *)fp->devuse;            t->offset = optr->fpos;        } /* not a directory, i.e. a file (r or w ) */        else            /* not supported for directories */            m->errno = EISDIR;    } /* open fp */    else        /* not open */        m->errno = ENOENT;    sendreply(m);}/*********************************************************************** *                                                                     * *            GREEN                                                    * *            =====                                                    * *                                                                     * ***********************************************************************/static void flashfs_msgStat(ATMOS_MESSAGE *m){    MSG_D_STAT(f, m);    if (NULL != f->name)    {        OPENFILE    slot;        BOOL        isDynamic;        m->errno = flashfs_ParseFilename (f->name, slot.name, &slot.partition);        TRACE3("%C: STAT '%s' rawpartition %d\n", slot.name, slot.partition);        if (ESUCCESS == m->errno)        {            flashfs_PartitionChange (slot.partition);            if (flashfs_Find (&slot, NULL, &isDynamic))            {                struct stat *   statbuf = f->stat;                memset (statbuf, 0, sizeof (struct stat));                statbuf->st_ino  = slot.datptr - sizeof (FLASHFS_DIRENT);                statbuf->st_nlink = isDynamic;                statbuf->st_mode = (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH);                statbuf->st_blksize = 1;                statbuf->st_blocks = slot.fmax;                statbuf->st_size = slot.fmax;            }            else            {                m->errno = ENOENT;            }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -