-int yflash2_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
-{
- int written;
- int pos;
- int h;
- int i;
- int nRead;
- int error;
-
- T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),nand_chunk,(unsigned)data, (unsigned)tags));
-
- CheckInit();
-
-
- if(dev->param.inband_tags){
-
- yaffs_packed_tags2_tags_only * pt2tp;
- pt2tp = (yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];
- yaffs_pack_tags2_tags_only(pt2tp,tags);
-
- pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
- h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-
- lseek(h,pos,SEEK_SET);
- written = write(h,data,dev->param.total_bytes_per_chunk);
-
-
- if(yaffs_test_partial_write){
- close(h);
- exit(1);
- }
-
- if(written != dev->param.total_bytes_per_chunk) return YAFFS_FAIL;
-
-
- }
-
- else {
- /* First do a write of a partial page */
- int n_partials;
- int bpos;
-
- if(data)
- {
- pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
- h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-
-
- memcpy(localBuffer,data, dev->data_bytes_per_chunk);
-
- n_partials = rand()%20;
-
- for(i = 0; i < n_partials; i++){
- bpos = rand() % dev->data_bytes_per_chunk;
-
- localBuffer[bpos] |= (1 << (rand() & 7));
- }
-
- if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
- printf("nand simulator: data does not match\n");
-
- lseek(h,pos,SEEK_SET);
- written = write(h,localBuffer,dev->data_bytes_per_chunk);
-
- if(yaffs_test_partial_write){
- close(h);
- exit(1);
- }
-
-
- if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
- }
- // yflash2_MaybePowerFail(nand_chunk,1);
-
- if(tags)
- {
- pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
- h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-
- lseek(h,pos,SEEK_SET);
-
- if( 0 && dev->param.is_yaffs2)
- {
-
- written = write(h,tags,sizeof(struct yaffs_ext_tags));
- if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
- }
- else
- {
- yaffs_packed_tags2 pt;
- yaffs_pack_tags2(&pt,tags, !dev->param.no_tags_ecc);
- u8 * ptab = (u8 *)&pt;
-
- nRead = read(h,localBuffer,sizeof(pt));
- for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
- if(localBuffer[i] != 0xFF){
- printf("nand simulation: chunk %d oob byte %d was %0x2\n",
- nand_chunk,i,localBuffer[i]);
- error = 1;
- }
- }
-
- for(i = 0; i < sizeof(pt); i++)
- localBuffer[i] &= ptab[i];
-
- n_partials = rand()% sizeof(pt);
-
- for(i = 0; i < n_partials; i++){
- bpos = rand() % sizeof(pt);
-
- localBuffer[bpos] |= (1 << (rand() & 7));
- }
-
- if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
- printf("nand sim: tags corruption\n");
-
- lseek(h,pos,SEEK_SET);
-
- written = write(h,localBuffer,sizeof(pt));
- if(written != sizeof(pt)) return YAFFS_FAIL;
- }
- }
-
- //yflash2_MaybePowerFail(nand_chunk,2);
-
- /* Next do the whole write */
- if(data)
- {
- pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
- h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-
-
- memset(localBuffer,0xFF, PAGE_SIZE);
- for(i = 0; i < dev->data_bytes_per_chunk; i++){
- localBuffer[i] &= data[i];
- }
-
- if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
- printf("nand simulator: data does not match\n");
-
- lseek(h,pos,SEEK_SET);
- written = write(h,localBuffer,dev->data_bytes_per_chunk);
-
- if(yaffs_test_partial_write){
- close(h);
- exit(1);
- }
-
-
- if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
- }
-
- if(tags)
- {
- pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
- h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-
- lseek(h,pos,SEEK_SET);
-
- if( 0 && dev->param.is_yaffs2)
- {
-
- written = write(h,tags,sizeof(struct yaffs_ext_tags));
- if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
- }
- else
- {
- yaffs_packed_tags2 pt;
- yaffs_pack_tags2(&pt,tags,!dev->param.no_tags_ecc);
- u8 * ptab = (u8 *)&pt;
-
- nRead = read(h,localBuffer,sizeof(pt));
- for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
- if(localBuffer[i] != 0xFF){
- printf("nand simulation: chunk %d oob byte %d was %0x2\n",
- nand_chunk,i,localBuffer[i]);
- error = 1;
- }
- }
-
- for(i = 0; i < sizeof(pt); i++)
- localBuffer[i] &= ptab[i];
-
- if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
- printf("nand sim: tags corruption\n");
-
- lseek(h,pos,SEEK_SET);
-
- written = write(h,localBuffer,sizeof(pt));
- if(written != sizeof(pt)) return YAFFS_FAIL;
- }
- }
-
- yflash2_MaybePowerFail(nand_chunk,3);
-
- }
- return YAFFS_OK;
-
-}