/*********************************************************************** * * Function: fat16_init_device * * Purpose: * Initializes the FAT16 interface for the selected device. * * Processing: * Copy the device name and function pointers into the FAT device * structure. Clear the commit flag to indicate the FAT cluster table * does not need to be written back to the device. Call the device * initialization function. If the device was initialized, read the * MBR into the FAT device structure. * * Parameters: * device : Device name * init_func : Pointer to initialization function * shutdown_func : Pointer to shutdown function * insert_ck_func : Pointer to insertion check function * ready_ck_func : Pointer to ready check function * busy_ck_func : Pointer to bust check function * set_sector_func : Pointer to sector set function * start_read_func : Pointer to read start function * start_write_func : Pointer to write start function * read_func : Pointer to read buffer function * write_func : Pointer to write buffer function * * Outputs: * Data in fat_data will be updated. * * Returns: * The pointer to a binded device structure, or NULL if the device * was not detected. * * Notes: * The calling function should check to make sure that NULL was not * returned. If NULL was returned, the device does not exist or * memory could not be allocated. * **********************************************************************/ FAT_DEVICE_TYPE * fat16_init_device( CHAR *device, // Name of device ivfunc init_func, // Pointer to device init function vvfunc shutdown_func, // Pointer to device shutdown function ivfunc insert_ck_func, // Pointer to check for insert func ivfunc ready_ck_func, // Pointer for ready? check ivfunc busy_ck_func, // Pointer for busy? check void (*set_sector_func)(UNS_32), // sector setting vvfunc start_read_func, // Pointer for read start vvfunc start_write_func, // Pointer for write start ivifunc read_func, // Pointer for read of data ivifunc write_func) // Pointer for write of data { FAT_DEVICE_TYPE *fat_data = (FAT_DEVICE_TYPE *) NULL; // Try to initialize the device if (init_func() == 1) { // Device initiailized, allocate the device data structure fat_data = lpc_new(sizeof(FAT_DEVICE_TYPE)); if (fat_data != NULL) { // Copy device name into device name in structure fat16_moveto(device, fat_data->device, (DSIZE - 1)); fat_data->device [DSIZE - 1] = '\0'; // Save function pointers fat_data->func.init_func = init_func; fat_data->func.shutdown_func = shutdown_func; fat_data->func.insert_ck_func = insert_ck_func; fat_data->func.ready_ck_func = ready_ck_func; fat_data->func.busy_ck_func = busy_ck_func; fat_data->func.set_sector_func = set_sector_func; fat_data->func.start_read_func = start_read_func; fat_data->func.start_write_func = start_write_func; fat_data->func.read_func = read_func; fat_data->func.write_func = write_func; // Set active partition to (-1), disable commit fat_data->fat_commit = 0; // Read MBR and populate MBR structure fat16_read_mbr(fat_data); } } return fat_data; }
//returns zero on failure int fat16_init() { //read mbr sector fat16_read_mbr((uint16_t*)mbr); //read partition table memcpy(part_table, mbr+MBR_OFFSET_PART_TABLE,sizeof(partition_table)*4); selected_part = fat16_find_partition(part_table, TABLE_ENTRIES); if(selected_part==-1) return 0; //start of boot sector boot_start_lba = __SWAP32(part_table[selected_part].lba_offset); //read fat boot sector fat16_read_fat_boot(&fat_boot); //start of root dir root_dir_start_lba = (__SWAP16(fat_boot.reserved_sectors) + __SWAP16(fat_boot.fat_size_sectors) * fat_boot.number_of_fats) * __SWAP16(fat_boot.sector_size); root_dir_start_lba /= 512; root_dir_start_lba += boot_start_lba; //start of cluster data: data_start_lba = ((__SWAP16(fat_boot.reserved_sectors) + __SWAP16(fat_boot.fat_size_sectors) * fat_boot.number_of_fats) * __SWAP16(fat_boot.sector_size)); data_start_lba += __SWAP16(fat_boot.root_dir_entries)*0x20; data_start_lba /= 512; data_start_lba += boot_start_lba; //start of FAT data fat_start_lba = boot_start_lba+((__SWAP16(fat_boot.sector_size)*__SWAP16(fat_boot.reserved_sectors))/512); //read FAT fat = (uint16_t*) malloc(__SWAP16(fat_boot.fat_size_sectors)* __SWAP16(fat_boot.sector_size)); fat16_read_fat(fat); cluster_size_bytes = __SWAP16(fat_boot.sector_size) * fat_boot.sectors_per_cluster; cluster_size_lba = cluster_size_bytes/512; return 1; }