📄 diskappl.c
字号:
/*
* Copyright (c) 2002, Jan Szymanski.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jan Szymanski.
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* some components of FAT12 for 8MB,16 SmartMedia *********/
/* 8Mb 16Mb */
/* logical sector logical sector */
/* Master Boot Sector 0 0 */
/* Not used 1-24 1-40 */
/* Partition Boot Sector 25 41 */
/* FAT 26-28 42-44 */
/* FAT (copy) 29-31 45-47 */
/* Root Directory 32-47 48-63 */
/* Data Area 48-15999 64- */
/**********************************************************/
/**********************************************************/
/* SYSTEM Includes */
/**********************************************************/
/**********************************************************/
/* Local Functions Prototypes */
/**********************************************************/
//void SM_find_sec_phy(void);
//unsigned int SM_log_to_phy(void); /* global now */
//unsigned int SM_start_cluster(unsigned int phsector,
//unsigned char *fileextension);
//unsigned char SM_read_FAT(void);
//unsigned int SM_find_next_inFAT(unsigned int cluster);
unsigned char quartertab(unsigned int min,unsigned int max,unsigned char zone);
/**********************************************************/
/**********************************************************/
/* Local Variables */
/**********************************************************/
/***********************************************************
Function: SM_disk_init
Purpose: to initialize after reset
Author: Jan Szymanski
Original Date: 06-08-2001
Date last Mod:
Comments: We perform reset here, read parameters and set
some software flags.
***********************************************************/
void SM_disk_init(void)
{
SM_reset(); /* hardware/software reset */
SM_read_info(); /* read SmartMedia parameters */
SM_flag1 = 0; /* no logical to physical conversion */
/* table yet */
/* more to come in the future */
}
/* end of SM_disk_init ************************************/
/*-------------------------*/
unsigned char quartertab(unsigned int min,unsigned int max,unsigned char zone)
{
unsigned int i,block;
unsigned long sector;
unsigned int temp,temp1;
unsigned int startblock,endblock;
putchar_uart0('a');
/* first fill the table with predefined constant */
for (i=0;i<256;i++)
SM_log_to_phyADD[i] = 0xffff;
if(zone==0)
{
sector=0; /* start with physical sector 0 */
startblock=0;
endblock=1024;
}
if(zone==1)
{
sector=0x8000;
startblock=1024;
endblock=2048;
}
if(zone==2)
{
sector=0x10000;
startblock=2048;
endblock=3072;
}
if(zone==3)
{
sector=0x18000;
startblock=3072;
endblock=4096;
}
for (block=startblock;block<endblock;block++)// one zone only
{
//WAS SM_page_read(SM_page_data,sector);
//TRY
SM_extra_read(SM_page_data,sector);
//hexout_uart0_long(sector);// for debug only
sector+=SM_info[7]; /* sectors per block: 16 for 8MB, 32 for 16MB */
if(SM_page_data[517]==0xff)//block status byte;
{
if((SM_page_data[518]&0xF8)==0x10) /* valid logical address */
{
temp=SM_page_data[518]&0x07; /* only bits 2,1,0 valid */
temp<<= 8; /* *256 */
temp+=SM_page_data[519];
temp>>=1; /* this is a logical block address */
//putchar_uart0(',');
//hexout_uart0_int(temp);
//putchar_uart0(' ');
//if (temp>999) return SM_INVALID_READING;
if ((temp>=min)&&(temp<max))
{
temp1=temp-min;
//hexout_uart0_int(temp1);
//putchar_uart0(' ');
SM_log_to_phyADD[temp1]=block;
} /* */
}
}//try
}
putchar_uart0('b');
return(0);
}
/***********************************************************
Function: SM_log_to_phy
Purpose: to do a translation of logical and physical
addresses. The result is an updated table
SM_log_to_phyADD1[1024]
Author: Jan Szymanski
Original Date: 06-08-2001
Date last Mod:
Comments:
***********************************************************/
unsigned char SM_log_to_phy(void)
{
//unsigned int SM_log_to_phyADD[256];
unsigned char zone;
unsigned char maxzones;
unsigned int i;
unsigned int min,max;
unsigned int *fptr,*fptrstart;
unsigned char error,ferror;
/* make sure we know the size of SmartMedia*/
SM_read_info(); /* read SmartMedia parameters */
//if ((SM_info[1]!=0xE6)&&(SM_info[1]!=0x73))return SM_NOT_REC;
//if ((SM_info[1]!=0x75)&&(SM_info[1]!=0x76))return SM_NOT_REC;
/* sizes other than 8,16,32,64 MB not supported yet */
/* for all blocks we read only one sector and check the */
/* redundant area for logical address */
if(SM_info[1]==0x73) maxzones=1;// 16MB card
if(SM_info[1]==0x75) maxzones=2;// 32MB card
if(SM_info[1]==0x76) maxzones=4;// 64MB card
fptrstart=(unsigned int *)SM_log_to_phyADD1;
zone=0;
while(zone<maxzones)
{
fptr=fptrstart;
min=0;
max=256;
while(max<=1024)
{
error=quartertab(min,max,zone);
ferror=Flash_seg_write ((unsigned char *)SM_log_to_phyADD,(unsigned char *)fptr);
min+=256;
max+=256;
fptr+=256;
hexout_uart0_int(SM_info[2]);
putchar_uart0(',');
hexout_uart0_int(min);
putchar_uart0(',');
hexout_uart0_int(max);
putchar_uart0(',');
// putstring_uart0("press");
// while(ischar_uart0()==0){}
// getchar_uart0();
}
zone+=1;
fptrstart+=1024;
}
// chech for more zones
/*-------------------------*/
//error=quartertab(0,256);
//if(SM_log_to_phyADD[0]==0xffff) return SM_NOT_FORMATTED;
//ferror=Flash_seg_write (SM_log_to_phyADD, SM_log_to_phyADD1);
/*-------------------------*/
//error=quartertab(256,512);
//ferror=Flash_seg_write (SM_log_to_phyADD, SM_log_to_phyADD1+256);
/*-------------------------*/
//error=quartertab(512,768);
//ferror=Flash_seg_write (SM_log_to_phyADD, SM_log_to_phyADD1+512);
/*-------------------------*/
//error=quartertab(768,1000);
//ferror=Flash_seg_write (SM_log_to_phyADD, SM_log_to_phyADD1+768);
/*-------------------------*/
/* for debugging only */
//for(i=0;i<400;i++)
// {
// hexout_uart0_int(i);
// putchar_uart0('-');
// hexout_uart0_int(SM_log_to_phyADD[i]);
// putchar_uart0(' ');
// }
/* end of for debugging */
//if(SM_log_to_phyADD[0]==0xffff) return SM_NOT_FORMATTED;
SM_flag1 = 1; /* set the flag, that table exists */
/* to save time */
return SM_OK;
}
/* End of SM_log_to_phy ***********************************/
/***********************************************************
Function: SM_start_cluster
Purpose: to find start cluster number for filename.
fileextension
Author: Jan Szymanski
Original Date: 06-08-2001
Date last Mod:
Comments:
***********************************************************/
unsigned int SM_start_cluster(unsigned int phsector,
unsigned char *fileextension)
{
unsigned int i,j;
unsigned int entry,sector;
unsigned char *bptr,*temp;
unsigned char a,b,c,d;
unsigned int SCN; /* start cluster number */
/* temp holds physical sector number */
/* there are 256 directory entries, each entry 32 bytes */
/* 256*32=8192 bytes = 16 sectors */
/* we need to search for filename.ext, first file with extention ext */
/* will be taken */
sector=0;
for(i=phsector;i<phsector+16;i++)
{
SM_page_read(SM_page_data,i);
entry=0xff;
bptr=SM_page_data;
//hexout_uart0_int(bptr);
//putstring_uart0("press");
//while(ischar_uart0()==0){}
//c=getchar_uart0();
for(j=0;j<16;j++)
{
d=*bptr;
temp=bptr;
if (d!=0xe5) /* not deleted directory */
{
temp+=8;
a=*temp++;
b=*temp++;
c=*temp++;
//hexout_uart0(d);
//hexout_uart0(a);
//hexout_uart0(b);
//hexout_uart0(c);
//putchar_uart0(',');
//if((a==*fileextension)&&(b==*(fileextension+1))&&(c==*(fileextension+2)))
if((a=='M')&&(b=='P')&&(c=='3'))
{
entry=j;
putstring_uart0("found MP3");
sector=i;
temp+=16; // +27
SCN=*temp--; //+26
SCN<<=8;
SCN+=*temp;
break;
}
}
bptr+=32;
}
// if(entry!=0xff)
// {
// sector=i;
// SCN=*(bptr+27);
// SCN<<=8;
// SCN+=*(bptr+26);
// break;
//if (SCN!=0) break;
// }
if(sector!=0)break; /* already found */
}
//hexout_uart0_int(SCN);
if(sector!=0)
{
//dump_memory(SM_page_data,sector);
//hexout_PC_int(entry);
putstring_uart0(" SCN=");
hexout_uart0_int(SCN);
return SCN;
}
else return 0; /* error */
}
/* End of SM_start_cluster ********************************/
/***********************************************************
Function: SM_read_FAT
Purpose: to read FAT
Author: Jan Szymanski
Original Date: 06-08-2001
Date last Mod:
Comments:
***********************************************************/
unsigned char SM_read_FAT(void)
{
unsigned long temp;
unsigned int i;
unsigned char FATsectors;
unsigned char *fptr;
/* for 8MB FAT is located in sectors 26-28 and FAT copy in 29-31 */
/* for 16MB FAT is located in sectors 42-44 and FAT copy in 45-47 */
/* for 32MB FAT is located in sectors 36-41 and FAT copy in 42-47 */
/* for 64MB FAT is located in sectors 56-67 and FAT copy in 67-79 */
if(SM_info[1]==0xE6) /* 8MB */
{
temp=SM_log_to_phyADD1[1]<<4; /* physical address of logical block 1 */
temp+=10; /* physical sector coresponding to logical sector 26 */
FATsectors=3;//26,27,28
fptr=(unsigned char *)SM_FAT_data;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -