src/kernel/fs/fat32.c
/*!
 * @note read or write a clus.
 * @param clus_no
 * @param buffer
 * @param offset
 * @param length
 * @param write
 */
@note read or write a clus.
@note read or write a clus .
static void _clus_rw(uint32_t clus_no, void * buffer, uint32_t offset, uint32_t length , bool write){
    ASSERT(offset<fat32.byts_per_clus);
    if(offset+length>fat32.byts_per_clus){
        length = fat32.byts_per_clus-offset;
    }
    uint32_t first_sec =_first_sec_in_clus(clus_no);
    uint32_t max_sec = first_sec+fat32.bpb.sec_per_clus;
    uint32_t sec_shift = offset/fat32.bpb.byts_per_sec;
    uint32_t offset_in_sec = offset%fat32.bpb.byts_per_sec;
    uint32_t buffer_offset = 0;
    for(uint32_t probe_sec = first_sec+sec_shift; probe_sec < max_sec; probe_sec++,offset_in_sec = 0){
        uint32_t cpy_len;
        if(length<=0){
            break;
        }
        if(offset_in_sec+length>fat32.bpb.byts_per_sec){
            cpy_len = fat32.bpb.byts_per_sec - offset_in_sec;
        }
        else{
            cpy_len = length;
        }
        if(write){
            block_t * block = block_get_write(probe_sec+fat32.partition_sec_offset,fat32_dev);
            memcpy(block->data+offset_in_sec,buffer + buffer_offset,  cpy_len);
            block_put_write(block);
        }
        else{
            block_t * block = block_get_read(probe_sec+fat32.partition_sec_offset,fat32_dev);
            memcpy(buffer + buffer_offset, block->data+offset_in_sec, cpy_len);
            block_put_read(block);
        }
        buffer_offset+=cpy_len;
        length -= cpy_len;
    }
}
static void _clus_rw ( uint32_t clus_no , void * buffer , uint32_t offset , uint32_t length , bool write ) { ASSERT ( offset < fat32 . byts_per_clus ) ; if ( offset + length > fat32 . byts_per_clus ) { length = fat32 . byts_per_clus - offset ; } uint32_t first_sec = _first_sec_in_clus ( clus_no ) ; uint32_t max_sec = first_sec + fat32 . bpb . sec_per_clus ; uint32_t sec_shift = offset / fat32 . bpb . byts_per_sec ; uint32_t offset_in_sec = offset % fat32 . bpb . byts_per_sec ; uint32_t buffer_offset = 0 ; for ( uint32_t probe_sec = first_sec + sec_shift ; probe_sec < max_sec ; probe_sec ++ , offset_in_sec = 0 ) { uint32_t cpy_len ; if ( length <= 0 ) { break ; } if ( offset_in_sec + length > fat32 . bpb . byts_per_sec ) { cpy_len = fat32 . bpb . byts_per_sec - offset_in_sec ; } else { cpy_len = length ; } if ( write ) { block_t * block = block_get_write ( probe_sec + fat32 . partition_sec_offset , fat32_dev ) ; memcpy ( block -> data + offset_in_sec , buffer + buffer_offset , cpy_len ) ; block_put_write ( block ) ; } else { block_t * block = block_get_read ( probe_sec + fat32 . partition_sec_offset , fat32_dev ) ; memcpy ( buffer + buffer_offset , block -> data + offset_in_sec , cpy_len ) ; block_put_read ( block ) ; } buffer_offset += cpy_len ; length -= cpy_len ; } }
@note read or write a clus.
@note read or write a clus .