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

📄 hostdisk.c

📁 ertfs文件系统里面既有完整ucos程序
💻 C
字号:
/*
* EBS - RTFS (Real Time File Manager)
*
* Copyright Peter Van Oudenaren , 1993
* All rights reserved.
* This code may not be redistributed in source or linkable object form
* without the consent of its author.
*/
/* hostdisk.c - Simulate disk drive in a file on the host operating system

Summary

 Description
    Provides a device driver that reads and writes data to a file on the 
    host disk. This version of the driver creates a file named
    FILEDISK.DAT. This is targeted for hosts runnig Win95/NT but it can
    be migrated to other hosts.


    The format routine requires an initialized external variable 
    named:
        extern word host_disk_size;
    that tells it what size in blocks the file should be formatted to.


    This driver is used primarilly for the MKROM utility but it is 
    fully functional and can be used for other purposes.

*/
#include <pcdisk.h>
#include <stdio.h>

/* Set this to one for it to compile. This is not portable code so we need 
   to be able to turn it off */
#define USE_HOSTDISK 0

#if (USE_HOSTDISK)

extern word host_disk_size;

/* filedisk - Device driver that uses a DOS file as a virtual disk.

 Description
    Provides a "virtual volume" that resides in a (host) DOS file. This 
    volumecan be used to prototype applications. It can also be used to build
    the image of a rom disk which can then be accessed through the
    rom disk driver. (see mkrom.c)

    To enable this driver set USE_HOSTDISK to 1 in pcconf.h
*/
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\types.h>
#include <stdlib.h>

int fd_fd = -1;     /* file disk file descriptor */

/*
*
*   Perform io to and from the filedisk. 
*
*   If the reading flag is true copy data from the filedisk (read).
*   else copy to the filedisk. (write).
*   
*/

BOOLEAN hostdisk_io(word driveno, dword block, void KS_FAR *buffer, word count, BOOLEAN reading) /*__fn__*/
{
    long ltemp;
    int nbytes;
    int dup_fd;

   /* We don't use drive no. You could have multiple drives if wanted */
   driveno = driveno;

    ltemp = (block * 512);
    if (lseek(fd_fd, ltemp, SEEK_SET) != ltemp)
    {
        return(FALSE);
    }

    nbytes = (word) (count * 512);
    if (reading)
    {
        if (read(fd_fd,buffer,nbytes) != nbytes)
        {
            return(FALSE);
        }
    }
    else
    {
        if (write(fd_fd,buffer,nbytes) != nbytes)
        {
            return(FALSE);
        }

        // TONY - Need to make sure this works
        /* Flush the file after each write */
        dup_fd = dup(fd_fd);
        if (dup_fd > 0)
            close(dup_fd);
    }
    return(TRUE);
}


int hostdisk_perform_device_ioctl(int driveno, int opcode, PFVOID pargs)
{
DDRIVE *pdr;
DEV_GEOMETRY gc;        // used by DEVCTL_GET_GEOMETRY


    pdr = pc_drno_to_drive_struct(driveno);
    if (!pdr)
        return (-1);

    switch (opcode)
    {
        case DEVCTL_GET_GEOMETRY:

        pc_memfill(&gc, sizeof(gc), '\0');
        /* Now set the geometry section */
        gc.dev_geometry_heads       = 1;
        gc.dev_geometry_cylinders   = host_disk_size;
        gc.dev_geometry_secptrack   = 1;

        copybuff(pargs, &gc, sizeof(gc));
        return (0);

        case DEVCTL_FORMAT:
        {
            /* Fill the file. with zeroes */
            byte KS_FAR *p;
            dword block;
            int i;
            BLKBUFF *buf;

            /* The file should be open already */
            if (fd_fd < 0)
                return(-1);
            /* Grab some working space   */
            buf = pc_scratch_blk();
            if (!buf)
            {
                return(-1);
            }
            p = buf->data;
            pc_memfill(p, 512, (byte) 0);
            for (block = 0; block < host_disk_size; block++)
            {
                hostdisk_io(0, block, p, 1, FALSE);
            }
            pc_free_buf(buf, TRUE);
            return(0);
        }
        break;
        case DEVCTL_REPORT_REMOVE:
            return(0);

        case DEVCTL_CHECKSTATUS:
                return(DEVTEST_NOCHANGE);

        case DEVCTL_WARMSTART:
            fd_fd = open("FILEDISK.DAT",O_RDWR|O_BINARY|O_CREAT, S_IREAD | S_IWRITE);
            if (fd_fd < 0)
            {
                fd_fd = -1;
                return(-1);
            }
            pdr->drive_flags |= DRIVE_FLAGS_VALID;
            return(0);
            /* Fall through */
        case DEVCTL_POWER_RESTORE:
            /* Fall through */
        case DEVCTL_POWER_LOSS:
            /* Fall through */
        default:
            break;
    }
    return(0);

}

#endif  // USE_HOSTDISK


⌨️ 快捷键说明

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