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

📄 format.c

📁 ertfs文件系统里面既有完整ucos程序
💻 C
📖 第 1 页 / 共 3 页
字号:
                goto errex;
            }
        }
#if (INCLUDE_FAT32_BOOT_CODE)
        copybuff(&b[0],&FAT32_BOOT_CODE[512],512);
        if (!devio_write_format(driveno, (dword) 8, &(b[0]), 1, use_raw) )
        {
            goto errex;
        }
        if (!devio_write_format(driveno, (dword) 2, &(b[0]), 1, use_raw) )
        {
            goto errex;
        }      
        copybuff(&b[0],&FAT32_BOOT_CODE[0],512);
#else
        pc_memfill(&b[0], 512, '\0');
#endif
    }
#endif
    b[0] = (byte) 0xe9;    /* Jump vector. Used to id MS-DOS disk */
    b[1] = (byte) 0x00;
    b[2] = (byte) 0x00;
#if (FAT32)
    if (pfmt->fmttype == FMT_FAT32)
    {
        b[0x40] = 0x80;
        b[0x41] = 0x00;
        b[0x42] = 0x29;
#if (INCLUDE_FAT32_BOOT_CODE)
        b[0] = (byte) 0xeb;    /* Jump vector. Used to id MS-DOS disk */
        b[1] = (byte) 0x58;
        b[2] = (byte) 0x90;
#endif
    }
#endif
    /* Copy the OEM name   */
    pc_cppad(&b[3], (unsigned char *)pfmt->oemname, 8);
    /* bytes per sector   */

    fr_WORD ( &(b[11]), 512);   /*X*/
    /* sectors / cluster   */
    b[13] = pfmt->secpalloc;
    /* Number of reserved sectors. (Including block 0)   */
    fr_WORD ( &(b[14]), pfmt->secreserved); /*X*/
    /* number of dirents in root   */
    fr_WORD ( &(b[17]), pfmt->numroot);     /*X*/
    /* total sectors in the volume   */

    /* Set totsecs to 0 if size > 64k. This triggers sensing huge 4.0 
       partitions. */

    ltotsecs = pfmt->numcyl;
    ltotsecs *= pfmt->secptrk;
    ltotsecs *= pfmt->numhead;
#if (FAT32)
    if (pfmt->fmttype == FMT_FAT32)
        ltotsecs -= pfmt->numhide;
#endif
    
    if (ltotsecs > 0xffffL)
    {
        /* HUGE partition  the 3.xx totsecs field is zeroed   */
        totsecs = 0;
    }
    else
    {
        totsecs = (word) ltotsecs;
    }
    fr_WORD ( &(b[19]), totsecs);   /*X*/

    /* if secpfat was not provided calculate it here   */
    /* TONY - Bug fix 6/23/98 */
#if (FAT32)
    if (!pfmt->secpfat)
    {
        if (pfmt->fmttype == FMT_FAT32)
        {
            pfmt->secpfat = (ltotsecs + 128*pfmt->secpalloc)
                                /(128*pfmt->secpalloc + 1);
        }
    }
#endif
    /* Media descriptor   */
    b[21] = pfmt->mediadesc;
    /* sectors per trak   */
    fr_WORD ( &(b[24]), pfmt->secptrk); /*X*/
    /* number heads   */
    fr_WORD ( &(b[26]), pfmt->numhead); /*X*/
    /* number hidden sectors   */
#if (FAT32)
    if (pfmt->fmttype == FMT_FAT32)
        fr_DWORD ( &(b[28]), pfmt->numhide);
    else
#endif
    fr_WORD ( &(b[28]), 0);             /*X*/
    /* number of duplicate fats   */
    b[16] = pfmt->numfats;
#if (FAT32)
    if (pfmt->fmttype == FMT_FAT32)
    {
        fr_WORD ( &(b[22]), (word)0);
        fr_DWORD ( &(b[36]), (dword)pfmt->secpfat);
        fr_DWORD ( &(b[40]), (dword)0); /* flags and version */
        fr_DWORD ( &(b[44]), (dword)2); /* root dir starting cluster */
        fr_WORD ( &(b[48]), (word)1);   /* info sector */
        fr_WORD ( &(b[50]), (word)6); /* backup boot sector */
    }
    else
        fr_WORD ( &(b[22]), (word)pfmt->secpfat); /*X*/
#else
    fr_WORD ( &(b[22]), pfmt->secpfat); /*X*/
#endif

    /* Now fill in 4.0 specific section of the boot block   */
    if (ltotsecs > 0xffffL)
    {
        /* HUGE partition   */
        fr_DWORD ( &(b[32]), ltotsecs); /*X*/
    }
    else 
    {
        fr_DWORD ( &(b[32]), 0L);       /*X*/
    }

#if (FAT32)
    if (pfmt->fmttype == FMT_DOS) {
#endif
    b[36] = pfmt->physical_drive_no;
    b[38] = 0x29;                       /* extended boot signature */
    fr_DWORD(&(b[39]) , pfmt->binary_volume_label); /*X*/
#if (FAT32)
    }
    if (pfmt->fmttype == FMT_FAT32)
    {
        pc_cppad( &(b[0x47]), (byte*)pfmt->text_volume_label, 11);
        pc_cppad( &(b[0x52]), (byte*)"FAT32",8);
    }
    else
#endif
    pc_cppad( &(b[43]), (unsigned char*)pfmt->text_volume_label, 11);

    /* Count the size of the area managed by the fat.   */
    ldata_area = ltotsecs;
    ldata_area -= pfmt->numfats * pfmt->secpfat;
    ldata_area -= pfmt->secreserved;
    
    /* Note: numroot must be an even multiple op INOPBLOCK   */
    ldata_area -= pfmt->numroot/INOPBLOCK;

    /* Nibbles/fat entry if < 4087 clusters then 12 bit else 16   */
    lnclusters =  ldata_area/pfmt->secpalloc;
    if (lnclusters > 0xffffL)
    {
#if (!FAT32)
        goto errex;
#endif
    }
    else
    {
        nclusters = (word) lnclusters;
    }

    fausize = (int) ( (nclusters < 4087) ? 3 : 4 );

#if (FAT32)
    fausize = (word) ( (lnclusters > 0xffffL) ? 8 : fausize);
#endif


    /* Check the FAT. 
    if ( (nibbles needed) > (nibbles if fatblocks)
            trouble;
    */
    {
    long ltotnibbles;
    long lnibsinfatbls;

        /* Total nibbles = (# clusters * nibbles/cluster)   */
        ltotnibbles = nclusters;
        ltotnibbles *= fausize;

        /* How many nibbles are available.   */
        lnibsinfatbls = pfmt->secpfat;
        lnibsinfatbls <<= 10;            /* 1024 nibbles/block */

        if (ltotnibbles > lnibsinfatbls)
        {
            goto errex;
        }
    }

    if (pfmt->numroot % INOPBLOCK)
    {
        goto errex;
    }
         
 
    if (!devio_write_format(driveno, (dword) 0 + pfmt->numhide, &(b[0]), 1, use_raw) )
    {
        goto errex;
    }
#if (FAT32)
// 
    if (pfmt->fmttype == FMT_FAT32)
    {
    // HEREHERE - Put the initial free cluster value in right
        pc_memfill(&b[0], 512, '\0');
        fr_DWORD( &(b[0]), (dword) 0x41615252ul);
        fr_DWORD( &(b[0x01e4]), (dword) FSINFOSIG);
        fr_DWORD( &(b[0x01e8]), (dword)(lnclusters-1));
        fr_DWORD( &(b[0x01ec]), (dword)0x00000002);
        fr_WORD( &(b[0x01fe]), (word)0xaa55);

        if (!devio_write_format(driveno, (dword) 7, &(b[0]), 1, use_raw) )
        {
            goto errex;
        }
        if (!devio_write_format(driveno, (dword) 1, &(b[0]), 1, use_raw) )
        {
            goto errex;
        }
    }
#endif
    /* Now write the fats out   */
    for (i = 0; i < pfmt->numfats; i++)
    {
        pc_memfill(&b[0], 512, '\0');
        /* The first 3(4) bytes of a fat are MEDIADESC,FF,FF,(FF)   */
        b[0] = pfmt->mediadesc;
        b[1] = (byte) 0xff;
        b[2] = (byte) 0xff;
#if (FAT32)
        j = (word) fausize;
        if (j==8) j=12;
        while(j > 3) 
        {
            if (j%4 == 0 && fausize == 8)
                b[--j] = (byte) 0x0f;
            else
                b[--j] = (byte) 0xff;
        }
#else
        if (fausize == 4)
            b[3] = (byte) 0xff;
#endif

        blockno = pfmt->secreserved + (i * pfmt->secpfat);
        for ( j = 0; j < pfmt->secpfat; j++)
        {
            /* WRITE   */
            if (!devio_write_format(driveno, blockno, &(b[0]), 1, use_raw) )
            {
                goto errex;
            }
            blockno += 1;
            pc_memfill(&b[0], 512, '\0');
        }
    }
    
    /* Now write the root sectors   */
    blockno = pfmt->secreserved + pfmt->numfats * pfmt->secpfat;
    pc_memfill(&b[0], 512, '\0');
#if (FAT32)
    if (pfmt->fmttype == FMT_FAT32)
    {
            // Bug fix 11-22-99 use <pfmt->secpalloc instead of 8
        for(k=0;k<pfmt->secpalloc;k++) // Is 8 blocks per cluster?
        {
            if (!devio_write_format(driveno, blockno+k, &(b[0]), 1, use_raw) )
                goto errex;
        }
    }
    else
#endif
    for ( j = 0; j < (pfmt->numroot/INOPBLOCK) ; j++)
    {
        if (!devio_write_format(driveno, blockno, &(b[0]), 1, use_raw) )
        {
            goto errex;
        }
        blockno += 1;
    }
    ret_val = TRUE;
    set_errno(0);

errex:      /* Not only errors return through here. Everything does. */
    pc_free_buf(buf,TRUE);
    return(ret_val);

}

word pc_fat_size(word nreserved, word cluster_size, word n_fat_copies,
                 word root_sectors, dword volume_size, 
                 int *nibs_per_entry)   /*__fn__*/
{

    dword  fat_size;
    dword  total_clusters;
    word entries_per_block;
    

#if (FAT32)
    if ((root_sectors == 0) || ((volume_size>>11) >= 512))/* FAT32 Format */
    {
        fat_size = (volume_size + 128*cluster_size) / (128*cluster_size + 1);
        *nibs_per_entry = 8;
        return ((word)fat_size);
    }
#endif

    /* Calulate total cluster size. Assuming zero size fat:
        We round up to the nearest cluster boundary */
    total_clusters = volume_size - nreserved - root_sectors;
    total_clusters /= cluster_size;

    /* Calculate the number of fat entries per block in the FAT. If
        < 4087 clusters total the fat entries are 12 bits hence 341 
        will fit. else 256 will fit
        we add in n_fat_copies * 12 here since it take 12 blocks to represent
        4087 clusters in 3 nibble form. So we add in the worst case FAT size
        here to enhance the accuracy of our guess of the total clusters.
    */  

    if (total_clusters <= (dword) (4087 + (n_fat_copies * 12)) )
    {
        entries_per_block = 341;
        *nibs_per_entry = 3;
    }
    else
    {
        entries_per_block = 256;
        *nibs_per_entry = 4;
    }

    fat_size = (total_clusters + entries_per_block - 1)/entries_per_block;

    return((word) fat_size);    
}



⌨️ 快捷键说明

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