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

📄 vvdriver.c

📁 基于周立功FAT32文件系统建立的项目
💻 C
📖 第 1 页 / 共 2 页
字号:
                        BS_buf_write_byte((uint8)(SEC_NUM_FAT >> 16));
                        BS_buf_write_byte((uint8)(SEC_NUM_FAT >> 24));


                        /* Ext Flags */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        /* FS Version */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        /* Root Cluster */
                        BS_buf_write_byte(0x02);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        /*FSInfo sector */
                        BS_buf_write_byte(0x01);
                        BS_buf_write_byte(0x00);
                        /* Backup Boot Sector */
                        BS_buf_write_byte(0x06);
                        BS_buf_write_byte(0x00);

                        /* Reserved */
                        for (i = 12; i != 0; i--)
                            BS_buf_write_byte(0x00);

                        BS_buf_write_byte(0x80);        /* Driver number : 0x00 for floopy, 0x80 for hard disks */ 
                        BS_buf_write_byte(0x00);                    /* not used */
                        BS_buf_write_byte(FAT_EXT_SIGN);            /* extended boot signature */
                        BS_buf_write_byte(0x00);                    /* volume ID */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte('V');                     /* Volume Label */
                        BS_buf_write_byte('_');
                        BS_buf_write_byte('V');
                        BS_buf_write_byte('O');
                        BS_buf_write_byte('L');
                        BS_buf_write_byte('U');
                        BS_buf_write_byte('M');
                        BS_buf_write_byte('E');
                        BS_buf_write_byte(' ');
                        BS_buf_write_byte(' ');
                        BS_buf_write_byte(' ');
                        BS_buf_write_byte('F');                     /* File System Type in ASCII */
                        BS_buf_write_byte('A');
                        BS_buf_write_byte('T');
                        BS_buf_write_byte('3');
                        BS_buf_write_byte('2');
                        BS_buf_write_byte(' ');
                        BS_buf_write_byte(' ');
                        BS_buf_write_byte(' ');

                        for (i = 0; i < 420; i++)                /* Boot Code */
                            BS_buf_write_byte(0x00);

                        BS_buf_write_byte(0x55);                    /* Signature word */
                        BS_buf_write_byte(0xAA);
                        printf("\na-boot_sec_buf_index: %lu \n", boot_sec_buf_index);

                        /* FSInfo Sector  */

                        BS_buf_write_byte(0x52);    /* Lead Signature */
                        BS_buf_write_byte(0x52);
                        BS_buf_write_byte(0x61);
                        BS_buf_write_byte(0x41);
                        for (i = 0; i < 480; i++)  /* Reserved */
                        {
                            BS_buf_write_byte(0x00);
                        }
                        BS_buf_write_byte(0x72);    /* Structure signature */
                        BS_buf_write_byte(0x72);
                        BS_buf_write_byte(0x41);
                        BS_buf_write_byte(0x61);


                        BS_buf_write_byte((uint8)FREE_CLUS);    /* Free cluster count */
                        BS_buf_write_byte((uint8)(FREE_CLUS >> 8));
                        BS_buf_write_byte((uint8)(FREE_CLUS >> 16));
                        BS_buf_write_byte((uint8)(FREE_CLUS >> 24));

                        BS_buf_write_byte(0x02);    /* Next free cluster */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);

                        for (i = 12 / 4; i != 0; i--)  /* Reserved */
                        {
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        }

                        BS_buf_write_byte(0x00);  /* Trail signature */
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x55);
                        BS_buf_write_byte(0xAA);
                        printf("\nb-boot_sec_buf_index: %lu \n", boot_sec_buf_index);


                        for (i = 510/2; i != 0; i--)
                        {
                        BS_buf_write_byte(0x00);
                        BS_buf_write_byte(0x00);
                        }
                        BS_buf_write_byte(0x55);
                        BS_buf_write_byte(0xAA);
                        printf("\nc-boot_sec_buf_index: %lu \n", boot_sec_buf_index);


                        fwrite(boot_sec_buf, 1, BOOT_SEC_BUF_SIZE, fp);
                    }while(0);

                    printf("done");
                }

                /* 格式化FAT表 */
                printf("\nFormatting FAT entry ... ");
                if(fseek(fp, ADDR_FAT, SEEK_SET) == 0) 
                {
                    uint8   buf[SIZE_SEC];
                    uint32  *fat_entry_buf, i;

                    memset(buf, 0, SIZE_SEC);

                    /* 将FAT表清零同时将数据区根目录首簇-第2簇也清零 */
                    for (i = 0; i < SEC_NUM_FAT*FAT_NUM + 1; i++)   
                        fwrite(buf, 1, SIZE_SEC, fp);

                    /* 设置FAT表0、1、2这3个表项 */
                    fat_entry_buf = (uint32 *)&buf[0];
                    fat_entry_buf[0] = 0xffffff00 | HARD_DISK;
                    fat_entry_buf[1] = 0x0fffffff;              /* end-of-file marker */
                    fat_entry_buf[2] = 0x0fffffff;              /* 根目录占用了,无论有无设置label都要置此entry为EOF */
                    fseek(fp, ADDR_FAT, SEEK_SET);              /* FAT1 */
                    fwrite(buf, 1, 3*32, fp);
                    fseek(fp, ADDR_FAT + SIZE_FAT, SEEK_SET);   /* FAT2 */
                    fwrite(buf, 1, 3*32, fp);

                    /* 若设置了label的情况下还要在cluster2中添加label的dir结构体信息,没有label的则不用 */


                    printf("done");
                }
                else 
                {
                    printf("fail!");
                    ret = VOL_ERR;
                    goto EXIT;
                }

                printf("\n#Format info:");
                printf("\n FAT_ENTRY_NUM: %d, FAT_NUM: %d, SIZE_DATA_DEF: %lu", FAT_ENTRY_NUM, FAT_NUM, SIZE_DATA_DEF);
                printf("\n SEC_PER_CLUS: %d, SIZE_SEC: %d, SIZE_CLUS: %d", SEC_PER_CLUS, SIZE_SEC, SIZE_CLUS);
                printf("\n SIZE_FAT: 0x%lx, SIZE_DATA: 0x%lx, SIZE_VOL: 0x%lx(%lu)", SIZE_FAT, SIZE_DATA, SIZE_VOL, SIZE_VOL);
#endif        
            }
        }
    }
    else
    {
        printf("\n#Test: volume data file has been exist. \n");
        goto EXIT;
    }

EXIT:
    fclose(fp);

    return VOL_NO_ERR;
}


// 功能描述 : 从volume读入一个块
// blockaddr: block index
// buf      : buffer (512 bytes - 1 sector)
uint8 VV_ReadBlock(uint32 blockaddr, uint8 *buf)
{
    FILE    *fp;
    uint8   ret = VOL_NO_ERR;

	if (blockaddr >= SEC_NUM_VOL)	
		return VOL_ERR_OVER_RANGE;				/* 操作超出容量范围 operate over the range */

    fp = fopen(VIRTUAL_VOL_NAME, "rb+");
    if (fp == NULL)
    {
        printf("\nERR: virtual volume file is not exist.");
        return VOL_ERR;
    }

    if(fseek(fp, blockaddr*SIZE_SEC, SEEK_SET) == 0) 
    {
        fread(buf, 1, SIZE_SEC, fp);
    }
    else
        ret = VOL_ERR;

    return ret;
}



// 功能描述 : 向volume写入一个块
// blockaddr: block index
// buf      : buffer (512 bytes - 1 sector)
uint8 VV_WriteBlock(uint32 blockaddr, uint8 *buf)
{
    FILE    *fp;
    uint8   ret = VOL_NO_ERR;

	if (blockaddr >= SEC_NUM_VOL)	
		return VOL_ERR_OVER_RANGE;				/* 操作超出容量范围 operate over the range */

    fp = fopen(VIRTUAL_VOL_NAME, "rb+");
    if (fp == NULL)
    {
        printf("\nERR: virtual volume file is not exist.");
        return VOL_ERR;
    }

    if(fseek(fp, blockaddr*SIZE_SEC, SEEK_SET) == 0) 
    {
         fwrite(buf, 1, SIZE_SEC, fp);
    }
    else
        ret = VOL_ERR;

    return ret;
}


void VV_SetVVName(uint8 vv)
{
    vv_name_buf = vv_name[vv];
    printf("\n VV_NAME: %s \n", vv_name_buf);
}


#if 0
void VV_VolumeFormat(void)
{
}
#endif
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/


⌨️ 快捷键说明

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