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

📄 vvdriver.c

📁 基于周立功FAT32文件系统建立的项目
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
	Author: Johnny Chen
*/


//
//  提供底层读写等基本功能
//
//  描        述: FAT文件系统在PC上编译的虚拟卷驱动程序(底层驱动)
//



////////// 虚拟卷: 所有操作定位到volume.dat ///////////

#include "config.h"
#include "vvdriver.h"


char *vv_name_buf;
char *vv_name[3] = {
    "volume.dat",
    "128m_fat32_512_label(no).img",
    "32m_fat_512_label(fat).img",
};


uint8 VV_Initialize(void);
uint8 VV_ReadBlock(uint32 blockaddr, uint8 *buf);
uint8 VV_WriteBlock(uint32 blockaddr, uint8 *buf);


uint32 VV_GetVolumeFirstSect(uint8 Device)
{
	uint8 buffer[512];
	uint32 RelaStaSect;
	
	VV_ReadBlock(0, buffer);						/* 读SD卡的0块 */
	if((buffer[510]==0x55)&&(buffer[511]==0xAA))
	{
		if(((buffer[0]==0xEB)&&(buffer[2]==0x90))||(buffer[0] == 0xE9))/**/
		{
			RelaStaSect = 0;
		}
		else
		{
			RelaStaSect = buffer[454]+ 
					 	  buffer[455]*0x100 + 
					 	  buffer[456]*0x10000 + 
					 	  buffer[457]*0x1000000;

		}
	}
	return RelaStaSect;	
}

/*********************************************************************************************************
** 函数名称: CFCammand
** 功能描述: 底层驱动程序与上层的接口程序
**
** 输 入: Cammand:DISK_INIT:驱动程序初始化
**                 DISK_CLOSE:关闭驱动器(移除驱动程序)                 
**                 DISK_CREATE_BOOT_SECTOR:重建引导扇区
**                 DISK_READ_SECTOR:读扇区
**                 DISK_WRITE_SECTOR:写扇区
**        Parameter:剩余参数
** 输 出: DISK_READ_OK:读扇区完成
**        DISK_READ_NOT_OK:读扇区失败
**        DISK_WRITE_OK:写扇区完成
**        DISK_WRITE_NOT_OK:写扇区失败
**        DISK_INIT_OK:初始化完成
**        DISK_INIT_NOT_OK:初始化失败
**        BAD_DISK_COMMAND:无效的命令
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 陈明计
** 日 期: 2003年9月3日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint16 VVCammand(uint8 Cammand, void *Parameter)
{
	uint8 ret;
    uint16 rt;
    Disk_RW_Parameter * Dp;
    Disk_Info *DiskInfo;
    
    Dp = (Disk_RW_Parameter *)Parameter;

    switch (Cammand)
    {
        case DISK_INIT:			/*设备初始化*/
            rt = DISK_INIT_NOT_OK;
            ret = VV_Initialize();
            if (ret == VOL_NO_ERR)
            {
            	DiskInfo = GetEmptyDiskInfoAddr();
            	if (DiskInfo != NULL)
            	{
                	DiskInfo->DiakCommand = VVCammand;
                	DiskInfo->RsvdForLow = VV_GetVolumeFirstSect(0);/*获取卷首扇区*/
                	rt = DISK_INIT_OK;
            	}
	        }
            break;
            
        case DISK_CLOSE:		
            rt = RETURN_OK;
            break;
            
        case DISK_READ_SECTOR:	/*读扇区*/
            rt = DISK_READ_NOT_OK;
            if(VV_ReadBlock(Dp->SectorIndex + Dp->RsvdForLow,Dp->Buf) == VOL_NO_ERR)
            	rt = DISK_READ_OK;
            break;
            
        case DISK_WRITE_SECTOR:	/*写扇区*/
            rt = DISK_WRITE_NOT_OK;
            if(VV_WriteBlock(Dp->SectorIndex + Dp->RsvdForLow,Dp->Buf) == VOL_NO_ERR)
                rt = DISK_WRITE_OK;
            break;
            
        default:
            rt = BAD_DISK_COMMAND;
            break;
    }

    return rt;
}

#define BOOT_SEC_BUF_SIZE       (512*3)
uint16  boot_sec_buf_index = 0;
uint8   boot_sec_buf[BOOT_SEC_BUF_SIZE];
void BS_buf_write_byte(uint8 ch)
{
    if (boot_sec_buf_index < BOOT_SEC_BUF_SIZE) {
        boot_sec_buf[boot_sec_buf_index] = ch;
        boot_sec_buf_index++;
        if(boot_sec_buf_index%6 == 0)
            printf("\n");
        printf("%4lu-0x%02x | ", boot_sec_buf_index, ch);
    }
    else
    {
        printf("\n BOOT_SEC_BUF_SIZE: %lu, boot_sec_buf_index: %lu, ch: 0x%02x", BOOT_SEC_BUF_SIZE, boot_sec_buf_index, ch);
        printf("\n boot_sec_buf OP error: overflow.");
        getchar();
    }
}


 
// 功能描述: 初始化virtual volume (1)建虚拟卷文件 (2)创建引导扇区
// 输   入: 无
// 输   出: 0:   正确    >0:   错误码
uint8 VV_Initialize(void)
{
    FILE    *fp;
    uint8   ret = VOL_NO_ERR; 


    fp = fopen(VIRTUAL_VOL_NAME, "rb+");
    if (fp == NULL) // 若没有卷文件则创建一个虚拟卷文件
    {
        printf("\n#Volume data file isnot exist, creating ... ");
        fp = fopen(VIRTUAL_VOL_NAME, "wb");
        if (fp == NULL)
            printf("\n!fat32 init error: creat volume data file failed!");
        else
        {
            if(fseek(fp, SIZE_VOL - 1, SEEK_SET) == 0)
            {
                printf("\n 1..");
                if (0xff == fputc(0xff, fp)) 
                {
                    printf("2..");
					fseek(fp, -1, SEEK_CUR);
					if (0x00 == fputc(0x00, fp))
						printf("3.");

                    printf("creat volume file done (size: %lu)", SIZE_VOL);
                }
                else
                {
                    printf("\nVolume file rw error!!");
                    ret = VOL_ERR;
                    goto EXIT;
                }
                

#if FORMAT_VOL_QUICK
                printf("\n--- now format volume file ---");

                /* 创建引导扇区 共3个扇区 */
                printf("\nCreating boot sector ... ");
                if(fseek(fp, ADDR_RESEV, SEEK_SET) == 0) 
                {
                    uint16   i;

                    memset(boot_sec_buf, 0, BOOT_SEC_BUF_SIZE);
                    boot_sec_buf_index = 0;
                    do {
                        /* -- PARTITION BOOT RECORD -- */
                        BS_buf_write_byte(0xEB);                    /* JMP inst to PBR boot code */
                        BS_buf_write_byte(0x3C);
                        BS_buf_write_byte(0x90);
                        BS_buf_write_byte('M');                     /* OEM name */
                        BS_buf_write_byte('S');
                        BS_buf_write_byte('W');
                        BS_buf_write_byte('I');
                        BS_buf_write_byte('N');
                        BS_buf_write_byte('4');
                        BS_buf_write_byte('.');
                        BS_buf_write_byte('1');
                        BS_buf_write_byte((uint8)SIZE_SEC);                /* number of bytes per sector */
                        BS_buf_write_byte((uint8)(SIZE_SEC >> 8));
                        BS_buf_write_byte(SEC_PER_CLUS);            /* Number of sector per cluster */
                        BS_buf_write_byte(SEC_NUM_RESEV);           /* number of reserved sector */
                        BS_buf_write_byte(SEC_NUM_RESEV >> 8);
                        BS_buf_write_byte(FAT_NUM);                 /* Number of FAT */
                        BS_buf_write_byte(0x00);                    /* number of root directory entries, fat32 must be 0x0000 */
                        BS_buf_write_byte(0x00);

                        /* Total Sectors (FAT16)*/
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);

                        BS_buf_write_byte(HARD_DISK);                             /* Media Byte */
                        /* Number of sector in each FAT */
                        BS_buf_write_byte(0x00);                                  /* 0x0000 for FAT32 */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);                    /* Number of sectors on a track */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);                    /* Number of heads */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);                    /* Number of hidden sectors */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        /* number of sectors > 65535 */
                        BS_buf_write_byte((uint8)SEC_NUM_VOL);
                        BS_buf_write_byte((uint8)(SEC_NUM_VOL >> 8));
                        BS_buf_write_byte((uint8)(SEC_NUM_VOL >> 16));
                        BS_buf_write_byte((uint8)(SEC_NUM_VOL >> 24));

                        BS_buf_write_byte((uint8)SEC_NUM_FAT);           /* nb sector for each fat */
                        BS_buf_write_byte((uint8)(SEC_NUM_FAT >> 8));

⌨️ 快捷键说明

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