2009年9月30日 星期三

模擬NAND flash bad block managemnet

#include stdio.h
#include string.h
#include stdlib.h

#define FLASH_SIZE 256
int writepage(unsigned char *buf,int page);
void copypages(int dest,int src,int size);
void readtest(void);
int readpage(unsigned char *buf,int page);
unsigned char *flash;
int writesize=4; //page size = 4 bytes
int erasesize=16; //block size =16 bytes
unsigned long bbt=0x0;

static inline int generic_ffs(int x);



int main(){
int size=sizeof(unsigned char)*FLASH_SIZE;
flash=(unsigned char *)malloc(size);
memset(flash,0xff,size);
int i=0;
int ret;
int j=0;
int to=32; //The buf [] will be written from the offset to (page=8)
int block=0;
int writelen=48*sizeof(unsigned char);
int phys_erase_shift=generic_ffs( erasesize)-1;
int page_shift = generic_ffs(writesize) - 1;
int realpage =to >> page_shift;
int skip_count=0;
int oldrealpage=0;
int partition_size=96*sizeof(unsigned char);
int end_page_of_part=(to+partition_size-1) >> page_shift;
printf("end_page_of_part=%d\n",end_page_of_part);

unsigned char buf []={0x14, 0xf0, 0x9f, 0xe1, //8
0x14, 0xf0, 0x9f, 0xe2, //9
0x14, 0xf0, 0x9f, 0xe3, //10
0x14, 0xf0, 0x9f, 0xe4, //11
0x14, 0xf0, 0x9f, 0xe5, //12
0x14, 0xf0, 0x9f, 0xe6, //13
0x14, 0xf0, 0x9f, 0xe7, //14
0x14, 0xf0, 0x9f, 0xe8, //15
0x14, 0xf0, 0x9f, 0xe9, //16
0x14, 0xf0, 0x9f, 0xea, //17
0x14, 0xf0, 0x9f, 0xeb, //18
0x14, 0xf0, 0x9f, 0xec}; //19
unsigned char *pbuf=(unsigned char *)malloc( partition_size);
memset(pbuf,0x00,partition_size);
memcpy(pbuf,buf,writelen);

unsigned char *tmp=pbuf; //for free pbuf
while(1){
int bytes = writesize;
unsigned char *wbuf = pbuf;
if(realpage > end_page_of_part ){
printf("Out of partion!!\n");
break;
}
ret = writepage(wbuf,realpage);
int begin_page=0;
if (ret){
skip_count++;
to= realpage << page_shift;
block=to >> phys_erase_shift;
bbt |= (1 << block);
printf("block= %d realpage=%d flash[%d]=%02x bbt=%08x\n\n",block,realpage,block << phys_erase_shift,flash[block << phys_erase_shift],bbt);

block+=1;
to=block << phys_erase_shift;
realpage= to >> page_shift;
continue;
}
if(skip_count){
to= oldrealpage << page_shift;
block=to >> phys_erase_shift;
to=block << phys_erase_shift;
begin_page= to >> page_shift;
copypages(realpage, begin_page,writesize*(oldrealpage-begin_page+1));
realpage=realpage+ (oldrealpage-begin_page)+1;
writepage(wbuf,realpage);
skip_count=0;
}

writelen -= bytes;
if (!writelen)
break;
pbuf += bytes;
oldrealpage=realpage;
realpage++;

}
printf("Flash=>\n");
for(i=0;i if(i==0)
printf(" page %d " , i );
printf("\t%02x ",flash[i]);
j=i+1;
if((j%4)==0)
printf("\n page %d " , j/4 );
}

readtest();
if(flash!=NULL){
free(flash);
flash=NULL;
}
if(tmp!=NULL){
free(tmp);
tmp=NULL;
}

return 0;
}

void readtest(){

int readlen=48*sizeof(unsigned char);
unsigned char *readbuf=(unsigned char*)malloc(readlen);
memset(readbuf,0x00,readlen);
unsigned char *tmp=readbuf; //for free readbuf
int to=32; //read the data from flash offset=32
int page_shift = generic_ffs(writesize) - 1;
int realpage =to >> page_shift;
int ret=0;
int i,j;
int phys_erase_shift=generic_ffs( erasesize)-1;
int pagenum_of_block=erasesize/writesize;
while(1){

int bytes = writesize;
unsigned char *rbuf = readbuf;
int block=(realpage << page_shift) >> phys_erase_shift;

if(((1 << block)&bbt)!=0){
realpage+= pagenum_of_block;
continue;
}
ret = readpage(rbuf,realpage);

if(ret){
printf("Read the page error!!\n");
}


readlen -= bytes;
if(!readlen)
break;
readbuf+=bytes;
realpage++;

}
printf("\n\n");
readlen=48*sizeof(unsigned char);
printf("Read from flash=>\n");
for(i=0;i //printf("dsd");
printf("\t%02x ",tmp[i]);
j=i+1;
if((j%4)==0)
printf("\n" );
}


if(tmp!=NULL){
free(tmp);
tmp=NULL;
}
return;

}

void copypages(int dest,int src,int size){

int dest_index=dest*writesize;
int src_index=src*writesize;

memcpy ( flash+dest_index,flash+src_index, size);
memset (flash+src_index, 0xff,size);

return;

}

int readpage(unsigned char *buf,int page){

int index=page*writesize;
memcpy ( buf,flash+index , writesize);

return 0;

}

int writepage(unsigned char *buf,int page){

int index=page*writesize;
if( page==13 || page ==16)//Let the page 13 be the bad page
return 1;
memcpy ( flash+index, buf, writesize);
return 0;

}

static inline int generic_ffs(int x)
{
int r = 1;

if (!x)
return 0;
if (!(x & 0xffff)) {
x >>= 16;
r += 16;
}
if (!(x & 0xff)) {
x >>= 8;
r += 8;
}
if (!(x & 0xf)) {
x >>= 4;
r += 4;
}
if (!(x & 3)) {
x >>= 2;
r += 2;
}
if (!(x & 1)) {
x >>= 1;
r += 1;
}
return r;
}

1 則留言:

匿名 提到...

i will go to the place where you go to set the background, select the picture (which is under the maximum size limit btw) i select save, and nothing happens. the background just stays the default color. can someone please tell me whats wrong?
__________________
[url=http://jail-break-ps3.blogspot.com/2010/12/jail-break-ps3.html]Jail break ps3[/url]