void mywrite( FILE *fp, void *data, size_t len ) { /*************************************************/ char err_msg[ERR_BUFF_SIZE+1]; SDWrite( fp, data, len ); if( SDError( fp, err_msg ) ) { Error( SM_IO_WRITE_ERR, fName, err_msg ); CSuicide(); } }
static void _SDWrite( file_handle fp, const void *buffer, size_t size ) { unsigned amount; amount = INT_MAX; while( size > 0 ) { if( amount > size ) amount = (unsigned)size; SDWrite( fp, buffer, amount ); chkIOErr( fp, SM_IO_WRITE_ERR, "temporary file" ); buffer = (char *)buffer + amount; size -= amount; } }
static void CLIWrite( dw_sectnum sect, const void *block, dw_size_t size ) { /*********************************************************************/ char *temp; section_data *cur_sec; cur_sec = &Sections[sect]; if ( cur_sec->sec_type == DEFAULT_SECTION ) { if ( ( initial_section_type == DEFAULT_SECTION ) || ( initial_section_type == FILE_SECTION ) ) { cur_sec->sec_type = FILE_SECTION; SDSetAttr( REC_FIXED | SEEK ); temp = tmpnam( NULL ); cur_sec->u2.filename = FMemAlloc( strlen( temp ) + 1 ); strcpy( cur_sec->u2.filename, temp ); cur_sec->u1.fp = SDOpen( temp, UPDATE_FILE ); chkIOErr( cur_sec->u1.fp, SM_OPENING_FILE, temp ); } else { cur_sec->sec_type = initial_section_type; cur_sec->u1.size = MEM_INCREMENT; cur_sec->u2.data = FMemAlloc( MEM_INCREMENT ); } } switch( cur_sec->sec_type ) { case( MEM_SECTION ): if ( cur_sec->u1.size <= ( cur_sec->cur_offset + size ) ) { temp = FMemAlloc( cur_sec->u1.size + MEM_INCREMENT ); memcpy( temp, cur_sec->u2.data, cur_sec->u1.size ); FMemFree( cur_sec->u2.data ); cur_sec->u2.data = temp; cur_sec->u1.size += MEM_INCREMENT; } memcpy( ( cur_sec->u2.data + cur_sec->cur_offset ), block, size ); break; case( FILE_SECTION ): SDWrite( cur_sec->u1.fp, (byte *)block, size ); chkIOErr( cur_sec->u1.fp, SM_IO_WRITE_ERR, "temporary file" ); break; default: Error( CP_FATAL_ERROR, "Internal browse generator error" ); CSuicide(); }; cur_sec->cur_offset += size; if( cur_sec->cur_offset > cur_sec->max_offset ) { cur_sec->max_offset = cur_sec->cur_offset; } }
static void DumpCurrPage( void ) { //==================================== // Dump current page to disk. if( PageFlags & PF_DIRTY ) { if( CurrPage > MaxPage ) { MaxPage = CurrPage; } SDSeek( PageFile, CurrPage, PAGE_SIZE ); ChkIOErr( PageFile, SM_IO_WRITE_ERR ); SDWrite( PageFile, ObjCode, PAGE_SIZE ); ChkIOErr( PageFile, SM_IO_WRITE_ERR ); PageFlags &= ~PF_DIRTY; } }
bool MassStorage_t::CmdWrite10() { // Uart.Printf("\rCmdWrite10"); #if READ_ONLY SenseData.SenseKey = SCSI_SENSE_KEY_DATA_PROTECT; SenseData.AdditionalSenseCode = SCSI_ASENSE_WRITE_PROTECTED; SenseData.AdditionalSenseQualifier = SCSI_ASENSEQ_NO_QUALIFIER; return false; #else // Check case 8: Hi != Do if(CmdBlock.Flags & 0x80) { SenseData.SenseKey = SCSI_SENSE_KEY_ILLEGAL_REQUEST; SenseData.AdditionalSenseCode = SCSI_ASENSE_INVALID_COMMAND; return false; } // TODO: Check if ready if(false) { SenseData.SenseKey = SCSI_SENSE_KEY_NOT_READY; SenseData.AdditionalSenseCode = SCSI_ASENSE_MEDIUM_NOT_PRESENT; return false; } uint32_t BlockAddress=0; uint16_t TotalBlocks=0; // Get transaction size if(ReadWriteCommon(&BlockAddress, &TotalBlocks) == false) return false; // Uart.Printf("Addr=%u; Len=%u\r", BlockAddress, TotalBlocks); uint32_t BlocksToWrite1, BlocksToWrite2=0, BytesToReceive1, BytesToReceive2=0; bool Rslt = CH_SUCCESS; // Fill Buf1 BytesToReceive1 = MIN(MS_DATABUF_SZ, TotalBlocks * MMCSD_BLOCK_SIZE); BlocksToWrite1 = BytesToReceive1 / MMCSD_BLOCK_SIZE; // Start reception to Buf1 Usb.PEpBulkOut->StartReceiveToBuf(Buf1, BytesToReceive1); while(TotalBlocks != 0) { // ==== Wait end of reception1 ==== if(Usb.PEpBulkOut->WaitUntilReady() != OK) { Uart.Printf("Rcv1 fail\r"); return false; } // Buf1 is full, start receiving to Buf2 TotalBlocks -= BlocksToWrite1; if(TotalBlocks != 0) { BytesToReceive2 = MIN(MS_DATABUF_SZ, TotalBlocks * MMCSD_BLOCK_SIZE); BlocksToWrite2 = BytesToReceive2 / MMCSD_BLOCK_SIZE; Usb.PEpBulkOut->StartReceiveToBuf(Buf2, BytesToReceive2); } // Write Buf1 to SD Rslt = SDWrite(BlockAddress, Buf1, BlocksToWrite1); if(Rslt != CH_SUCCESS) { Uart.Printf("Wr1 fail\r"); return false; } CmdBlock.DataTransferLen -= BytesToReceive1; if(TotalBlocks == 0) return true; BlockAddress += BlocksToWrite1; // ==== Wait end of reception2 ==== if(Usb.PEpBulkOut->WaitUntilReady() != OK) { Uart.Printf("Rcv2 fail\r"); return false; } // Buf2 is full, start reception of Buf1 TotalBlocks -= BlocksToWrite2; if(TotalBlocks != 0) { BytesToReceive1 = MIN(MS_DATABUF_SZ, TotalBlocks * MMCSD_BLOCK_SIZE); BlocksToWrite1 = BytesToReceive1 / MMCSD_BLOCK_SIZE; Usb.PEpBulkOut->StartReceiveToBuf(Buf1, BytesToReceive1); } // Write Buf2 to SD Rslt = SDWrite(BlockAddress, Buf2, BlocksToWrite2); if(Rslt != CH_SUCCESS) { Uart.Printf("Wr2 fail\r"); return false; } CmdBlock.DataTransferLen -= BytesToReceive2; if(TotalBlocks == 0) return true; BlockAddress += BlocksToWrite2; } // while return true; #endif }