mirror of
https://github.com/FDOS/kernel.git
synced 2025-07-30 01:04:31 +02:00
Optimized initdisk.c by using constants instead of variables where possible.
git-svn-id: https://svn.code.sf.net/p/freedos/svn/kernel/trunk@1450 6ac86273-5f31-0410-b378-82cca8765d1b
This commit is contained in:
parent
a93b71a18b
commit
9e0b5e5bf6
@ -394,25 +394,24 @@ void printCHS(char *title, struct CHS *chs)
|
|||||||
Portions copyright 1992, 1993 Remy Card
|
Portions copyright 1992, 1993 Remy Card
|
||||||
and 1991 Linus Torvalds
|
and 1991 Linus Torvalds
|
||||||
*/
|
*/
|
||||||
|
/* defaults: */
|
||||||
|
#define MAXCLUSTSIZE 128
|
||||||
|
#define NSECTORFAT12 8
|
||||||
|
#define NFAT 2
|
||||||
|
|
||||||
VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
||||||
{
|
{
|
||||||
UBYTE maxclustsize;
|
|
||||||
ULONG fatdata;
|
ULONG fatdata;
|
||||||
|
|
||||||
bpb *defbpb = &pddt->ddt_defbpb;
|
bpb *defbpb = &pddt->ddt_defbpb;
|
||||||
|
|
||||||
/* FAT related items */
|
/* FAT related items */
|
||||||
defbpb->bpb_nfat = 2;
|
defbpb->bpb_nfat = NFAT;
|
||||||
defbpb->bpb_ndirent = (FileSystem == FAT32
|
|
||||||
|| FileSystem == FAT32_LBA) ? 0 : 512;
|
|
||||||
/* normal value of number of entries in root dir */
|
/* normal value of number of entries in root dir */
|
||||||
defbpb->bpb_nreserved = (FileSystem == FAT32
|
defbpb->bpb_ndirent = 512;
|
||||||
|| FileSystem == FAT32_LBA) ? 0x20 : 1;
|
defbpb->bpb_nreserved = 1;
|
||||||
|
/* SEC_SIZE * DIRENT_SIZE / defbpb->bpb_ndirent + defbpb->bpb_nreserved */
|
||||||
fatdata =
|
fatdata = NumSectors - (DIRENT_SIZE + 1);
|
||||||
NumSectors - cdiv(defbpb->bpb_ndirent * DIRENT_SIZE,
|
|
||||||
defbpb->bpb_nbyte) - defbpb->bpb_nreserved;
|
|
||||||
maxclustsize = 128;
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (FileSystem != FAT12)
|
if (FileSystem != FAT12)
|
||||||
DebugPrintf(("%ld sectors for FAT+data, starting with %d sectors/cluster\n", fatdata, defbpb->bpb_nsector));
|
DebugPrintf(("%ld sectors for FAT+data, starting with %d sectors/cluster\n", fatdata, defbpb->bpb_nsector));
|
||||||
@ -423,9 +422,9 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
case FAT12:
|
case FAT12:
|
||||||
case FAT12_LBA:
|
case FAT12_LBA:
|
||||||
{
|
{
|
||||||
unsigned fatdat, fatlength, clust, maxclust;
|
unsigned fatdat;
|
||||||
/* in DOS, FAT12 defaults to 4096kb (8 sector) - clusters. */
|
/* in DOS, FAT12 defaults to 4096kb (8 sector) - clusters. */
|
||||||
defbpb->bpb_nsector = 8;
|
defbpb->bpb_nsector = NSECTORFAT12;
|
||||||
/* Force maximal fatdata=32696 sectors since with our only possible sector
|
/* Force maximal fatdata=32696 sectors since with our only possible sector
|
||||||
size (512 bytes) this is the maximum for 4k clusters.
|
size (512 bytes) this is the maximum for 4k clusters.
|
||||||
#clus*secperclus+#fats*fatlength= 4077 * 8 + 2 * 12 = 32640.
|
#clus*secperclus+#fats*fatlength= 4077 * 8 + 2 * 12 = 32640.
|
||||||
@ -433,26 +432,28 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
fatdat = (unsigned)fatdata;
|
fatdat = (unsigned)fatdata;
|
||||||
if (fatdata > 32640)
|
if (fatdata > 32640)
|
||||||
fatdat = 32640;
|
fatdat = 32640;
|
||||||
/* The "+2*defbpb->bpb_nsector" is for the reserved first two FAT entries */
|
/* The "+2*NSECTOR" is for the reserved first two FAT entries */
|
||||||
fatlength = cdiv(fatdat + 2 * defbpb->bpb_nsector,
|
defbpb->bpb_nfsect = cdiv(fatdat + 2 * NSECTORFAT12,
|
||||||
defbpb->bpb_nbyte * 2 * defbpb->bpb_nsector / 3 +
|
SEC_SIZE * 2 * NSECTORFAT12 / 3 + NFAT);
|
||||||
defbpb->bpb_nfat);
|
#if DEBUG
|
||||||
/* Need to calculate number of clusters, since the unused parts of the
|
/* Need to calculate number of clusters, since the unused parts of the
|
||||||
* FATS and data area together could make up space for an additional,
|
* FATS and data area together could make up space for an additional,
|
||||||
* not really present cluster. */
|
* not really present cluster.
|
||||||
clust =
|
* (This is really done in fatfs.c, bpbtodpb) */
|
||||||
(fatdat - defbpb->bpb_nfat * fatlength) / defbpb->bpb_nsector;
|
|
||||||
maxclust = (fatlength * 2 * defbpb->bpb_nbyte) / 3;
|
|
||||||
if (maxclust > FAT12MAX)
|
|
||||||
maxclust = FAT12MAX;
|
|
||||||
DebugPrintf(("FAT12: #clu=%u, fatlength=%u, maxclu=%u, limit=%u\n",
|
|
||||||
clust, fatlength, maxclust, FAT12MAX));
|
|
||||||
if (clust > maxclust - 2)
|
|
||||||
{
|
{
|
||||||
clust = maxclust - 2;
|
unsigned clust = (fatdat - 2 * defbpb->bpb_nfsect) / NSECTORFAT12;
|
||||||
DebugPrintf(("FAT12: too many clusters: setting to maxclu-2\n"));
|
unsigned maxclust = (defbpb->bpb_nfsect * 2 * SEC_SIZE) / 3;
|
||||||
|
if (maxclust > FAT12MAX)
|
||||||
|
maxclust = FAT12MAX;
|
||||||
|
printf(("FAT12: #clu=%u, fatlength=%u, maxclu=%u, limit=%u\n",
|
||||||
|
clust, defbpb->bpb_nfsect, maxclust, FAT12MAX));
|
||||||
|
if (clust > maxclust - 2)
|
||||||
|
{
|
||||||
|
clust = maxclust - 2;
|
||||||
|
printf(("FAT12: too many clusters: setting to maxclu-2\n"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defbpb->bpb_nfsect = fatlength;
|
#endif
|
||||||
memcpy(pddt->ddt_fstype, MSDOS_FAT12_SIGN, 8);
|
memcpy(pddt->ddt_fstype, MSDOS_FAT12_SIGN, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -477,14 +478,12 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
defbpb->bpb_nsector));
|
defbpb->bpb_nsector));
|
||||||
|
|
||||||
fatlength = (unsigned)cdiv(fatdata + 2 * defbpb->bpb_nsector,
|
fatlength = (unsigned)cdiv(fatdata + 2 * defbpb->bpb_nsector,
|
||||||
(ULONG)defbpb->bpb_nbyte * defbpb->bpb_nsector / 2 +
|
(SEC_SIZE/2) * defbpb->bpb_nsector + NFAT);
|
||||||
defbpb->bpb_nfat);
|
|
||||||
/* Need to calculate number of clusters, since the unused parts of the
|
/* Need to calculate number of clusters, since the unused parts of the
|
||||||
* FATS and data area together could make up space for an additional,
|
* FATS and data area together could make up space for an additional,
|
||||||
* not really present cluster. */
|
* not really present cluster. */
|
||||||
clust =
|
clust = (fatdata - NFAT * fatlength) / defbpb->bpb_nsector;
|
||||||
(fatdata - defbpb->bpb_nfat * fatlength) / defbpb->bpb_nsector;
|
maxclust = (unsigned long)fatlength * (SEC_SIZE/2);
|
||||||
maxclust = ((unsigned long)fatlength * defbpb->bpb_nbyte) / 2;
|
|
||||||
if (maxclust > FAT16MAX)
|
if (maxclust > FAT16MAX)
|
||||||
maxclust = FAT16MAX;
|
maxclust = FAT16MAX;
|
||||||
DebugPrintf(("FAT16: #clu=%lu, fatlen=%u, maxclu=%lu, limit=%u\n",
|
DebugPrintf(("FAT16: #clu=%lu, fatlen=%u, maxclu=%lu, limit=%u\n",
|
||||||
@ -505,7 +504,7 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
break;
|
break;
|
||||||
defbpb->bpb_nsector <<= 1;
|
defbpb->bpb_nsector <<= 1;
|
||||||
}
|
}
|
||||||
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= maxclustsize);
|
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= MAXCLUSTSIZE);
|
||||||
defbpb->bpb_nfsect = fatlength;
|
defbpb->bpb_nfsect = fatlength;
|
||||||
memcpy(pddt->ddt_fstype, MSDOS_FAT16_SIGN, 8);
|
memcpy(pddt->ddt_fstype, MSDOS_FAT16_SIGN, 8);
|
||||||
break;
|
break;
|
||||||
@ -530,17 +529,18 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
if (NumSectors <= 532480UL) /* disks up to 260 MB, 0.5K cluster */
|
if (NumSectors <= 532480UL) /* disks up to 260 MB, 0.5K cluster */
|
||||||
nsector = 1;
|
nsector = 1;
|
||||||
defbpb->bpb_nsector = nsector;
|
defbpb->bpb_nsector = nsector;
|
||||||
|
defbpb->bpb_ndirent = 0;
|
||||||
|
defbpb->bpb_nreserved = 0x20;
|
||||||
|
fatdata = NumSectors - 0x20;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
fatlength = cdiv(fatdata + 2 * defbpb->bpb_nsector,
|
fatlength = cdiv(fatdata + 2 * defbpb->bpb_nsector,
|
||||||
(ULONG)defbpb->bpb_nbyte * defbpb->bpb_nsector / 4 +
|
(SEC_SIZE/4) * defbpb->bpb_nsector + NFAT);
|
||||||
defbpb->bpb_nfat);
|
|
||||||
/* Need to calculate number of clusters, since the unused parts of the
|
/* Need to calculate number of clusters, since the unused parts of the
|
||||||
* FATS and data area together could make up space for an additional,
|
* FATS and data area together could make up space for an additional,
|
||||||
* not really present cluster. */
|
* not really present cluster. */
|
||||||
clust =
|
clust = (fatdata - NFAT * fatlength) / defbpb->bpb_nsector;
|
||||||
(fatdata - defbpb->bpb_nfat * fatlength) / defbpb->bpb_nsector;
|
maxclust = fatlength * (SEC_SIZE/4);
|
||||||
maxclust = (fatlength * defbpb->bpb_nbyte) / 4;
|
|
||||||
if (maxclust > FAT32MAX)
|
if (maxclust > FAT32MAX)
|
||||||
maxclust = FAT32MAX;
|
maxclust = FAT32MAX;
|
||||||
DebugPrintf(("FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
|
DebugPrintf(("FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
|
||||||
@ -554,7 +554,7 @@ VOID CalculateFATData(ddt * pddt, ULONG NumSectors, UBYTE FileSystem)
|
|||||||
break;
|
break;
|
||||||
defbpb->bpb_nsector <<= 1;
|
defbpb->bpb_nsector <<= 1;
|
||||||
}
|
}
|
||||||
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= maxclustsize);
|
while (defbpb->bpb_nsector && defbpb->bpb_nsector <= MAXCLUSTSIZE);
|
||||||
defbpb->bpb_nfsect = 0;
|
defbpb->bpb_nfsect = 0;
|
||||||
defbpb->bpb_xnfsect = fatlength;
|
defbpb->bpb_xnfsect = fatlength;
|
||||||
/* set up additional FAT32 fields */
|
/* set up additional FAT32 fields */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user