diff --git a/sys/pci/hd.c b/sys/pci/hd.c index 5ebfbd8..2e25322 100644 --- a/sys/pci/hd.c +++ b/sys/pci/hd.c @@ -64,10 +64,6 @@ //GPT char *secbuf = 0x0; - - // Clear memory for drive name - bzero( &name, 16 ); - // Allocate memory for hdd structure hdd = ( struct driveInfo * ) kmalloc( sizeof( struct driveInfo ) ); @@ -101,16 +97,16 @@ if( device_add( 0, 'c', devInfo ) == 0x0 ) { kprintf( "ad%i - Start: [0x0], Size: [0x%x/0x%X]\n", hdC, hdd->hdSize, hdd->hdSize * 512 ); + + bzero( &name, 16 ); sprintf( name, "ad%ip%i", hdC, hdd->part ); + devfs_makeNode( name, 'b', 0x1, 0x0 ); hdRead( devInfo->info, data, 0x0, 0x1 ); if( d[0].dp_type == 0xEE ) { - // MrOlsen (2016-01-14) DEBUG: This was just to help debug - //kprintf( "%s - Type: [0x%X - %s], Start: [0x%X], Size: [0x%X]\n", name, d[0].dp_type, (d[0].dp_type >= 0 && d[0].dp_type <= 255) ? part_types[d[0].dp_type] : "Unknown", d[0].dp_start, d[0].dp_size ); - - secbuf = ( char * ) kmalloc( 65536 ); + secbuf = ( char * ) kmalloc( 65536 ); if( gptread( &freebsd_ufs_uuid, devInfo, secbuf ) == -1 ) { @@ -120,17 +116,17 @@ } else { - devInfo2 = (struct device_interface *) kmalloc(sizeof(struct device_interface)); + devInfo2 = ( struct device_interface * ) kmalloc( sizeof( struct device_interface ) ); - hdd2 = (struct driveInfo *) kmalloc(sizeof(struct driveInfo)); + hdd2 = ( struct driveInfo * ) kmalloc( sizeof( struct driveInfo ) ); - memcpy(devInfo2, devInfo, sizeof(struct device_interface)); + memcpy( devInfo2, devInfo, sizeof( struct device_interface ) ); - memcpy(hdd2, hdd, sizeof(struct driveInfo)); + memcpy( hdd2, hdd, sizeof( struct driveInfo ) ); //hdd2->parOffset = d[i].dp_start; - devInfo2->info = hdd2; + devInfo2->info = hdd2; minor++; @@ -148,9 +144,12 @@ if( device_add( minor, 'c', devInfo2 ) == 0x0 ) { - sprintf(name, "ad%ip%i", hdC, hdd->part); - kprintf("[%s] - Type: [0x%X - %s], Start: [%i], Offset: [%i], Size: [%i], MM: [%i:%i]\n", name, d[0].dp_type, (d[0].dp_type >= 0 && d[0].dp_type <= 255) ? part_types[d[0].dp_type] : "Unknown", hdd2->lba_start, hdd2->parOffset, hdd2->lba_end - hdd2->lba_start, devInfo->major, minor); - devfs_makeNode(name, 'c', 0x1, minor); + bzero( &name, 16 ); + sprintf( name, "ad%ip%i", hdC, hdd->part ); + + kprintf( "%s - Type: [0x%X - %s], Start: [%i], Offset: [%i], Size: [%i], MM: [%i:%i]\n", name, d[0].dp_type, (d[0].dp_type >= 0 && d[0].dp_type <= 255) ? part_types[d[0].dp_type] : "Unknown", hdd2->lba_start, hdd2->parOffset, hdd2->lba_end - hdd2->lba_start, devInfo->major, minor ); + + devfs_makeNode( name, 'c', 0x1, minor ); } @@ -160,255 +159,310 @@ for( i = 0x0; i < 0x4; i++ ) { - //MrOlsen (2016-01-14) DEBUG: This was just debugging code - //kprintf( "Type: 0x%X\n", d[i].dp_type ); - if( d[i].dp_type == 0xEE ) - kprintf("Motherfucker Jones! We're GPT.... Lick My Nuts Now Bitch!\n"); + kprintf( "This is a GPT partitioned device.\n" ); if( d[i].dp_type != 0x0 ) { - devInfo2 = (struct device_interface *) kmalloc(sizeof(struct device_interface)); - hdd2 = (struct driveInfo *) kmalloc(sizeof(struct driveInfo)); - memcpy(devInfo2, devInfo, sizeof(struct device_interface)); - memcpy(hdd2, hdd, sizeof(struct driveInfo)); - hdd2->parOffset = d[i].dp_start; - devInfo2->info = hdd2; + devInfo2 = ( struct device_interface * ) kmalloc( sizeof( struct device_interface ) ); + hdd2 = ( struct driveInfo * ) kmalloc( sizeof( struct driveInfo ) ); + + memcpy( devInfo2, devInfo, sizeof( struct device_interface ) ); + memcpy( hdd2, hdd, sizeof( struct driveInfo ) ); + + hdd2->parOffset = d[i].dp_start; + devInfo2->info = hdd2; + minor++; if( device_add( minor, 'c', devInfo2 ) == 0x0 ) { - sprintf(name, "ad%is%i", hdC, i + 1); + bzero( &name, 16 ); + sprintf( name, "ad%is%i", hdC, i + 1 ); - kprintf("<%s> - Type: [0x%X - %s], Start: [0x%X], Size: [0x%X], MM: [%i:%i]\n", name, d[i].dp_type, (d[i].dp_type >= 0 && d[i].dp_type <= 255) ? part_types[d[i].dp_type] : "Unknown", d[i].dp_start, d[i].dp_size, devInfo->major, minor); + kprintf( "%s - Type: [0x%X - %s], Start: [0x%X], Size: [0x%X], MM: [%i:%i]\n", name, d[i].dp_type, (d[i].dp_type >= 0 && d[i].dp_type <= 255) ? part_types[d[i].dp_type] : "Unknown", d[i].dp_start, d[i].dp_size, devInfo->major, minor ); - - devfs_makeNode(name, 'c', 0x1, minor); + devfs_makeNode( name, 'c', 0x1, minor ); if( d[i].dp_type == 0xA5 ) { - //Why do i need to add 1? - hdRead(devInfo->info, data2, d[i].dp_start + 1, 0x1); + // XXX - Why do i need to add 1? + hdRead( devInfo->info, data2, d[i].dp_start + 1, 0x1 ); for( x = 0; x < bsdd->d_npartitions; x++ ) { if( bsdd->d_partitions[x].p_size > 0 ) { + bzero( &name, 16 ); sprintf( name, "ad%is%i%c", hdC, i + 1, 'a' + x ); //New Nodes - devInfo2 = (struct device_interface *) kmalloc(sizeof(struct device_interface)); - hdd2 = (struct driveInfo *) kmalloc(sizeof(struct driveInfo)); + devInfo2 = ( struct device_interface * ) kmalloc( sizeof( struct device_interface ) ); + hdd2 = ( struct driveInfo * ) kmalloc( sizeof( struct driveInfo ) ); - memcpy(devInfo2, devInfo, sizeof(struct device_interface)); + memcpy( devInfo2, devInfo, sizeof(struct device_interface ) ); - memcpy(hdd2, hdd, sizeof(struct driveInfo)); + memcpy( hdd2, hdd, sizeof( struct driveInfo ) ); //hdd2->parOffset = d[i].dp_start + bsdd->d_partitions[x].p_offset; - hdd2->lba_start = d[i].dp_start; + hdd2->lba_start = d[i].dp_start; - hdd2->parOffset = bsdd->d_partitions[x].p_offset; + hdd2->parOffset = bsdd->d_partitions[x].p_offset; - devInfo2->info = hdd2; + devInfo2->info = hdd2; minor++; - device_add(minor, 'c', devInfo2); + device_add( minor, 'c', devInfo2 ); - devfs_makeNode(name, 'c', 0x1, minor); + devfs_makeNode( name, 'c', 0x1, minor ); - kprintf("{%s} - Type: [%s], Start: [0x%X], Size: [0x%X], MM: [%i:%i]\n", name, fstypenames[bsdd->d_partitions[x].p_fstype], bsdd->d_partitions[x].p_offset, bsdd->d_partitions[x].p_size, devInfo->major, minor); + kprintf( "%s - Type: [%s], Start: [0x%X], Size: [0x%X], MM: [%i:%i]\n", name, fstypenames[bsdd->d_partitions[x].p_fstype], bsdd->d_partitions[x].p_offset, bsdd->d_partitions[x].p_size, devInfo->major, minor ); } - } - } - } - } - } } } - kfree(data); + kfree( data ); hdC++; - return (0x0); + return ( 0x0 ); } int hdStandby() { - /* Not Implemented */ - return (0x0); + + /* Not Implemented */ + return ( 0x0 ); } int hdStart() { - /* Not Implemented */ - return (0x0); + + /* Not Implemented */ + return ( 0x0 ); + } int hdStop() { - /* Not Implemented */ - return (0x0); + + /* Not Implemented */ + return ( 0x0 ); } int hdIoctl() { - /* Not Implemented */ - return (0x0); + + /* Not Implemented */ + return ( 0x0 ); + } int hdReset() { - /* Not Implemented */ - return (0x0); + + /* Not Implemented */ + return ( 0x0 ); + } -int hdInit(struct device_node *dev) { - u_int8_t retVal = 0x0; - int counter = 0x0; - u_int16_t *tmp = 0x0; - struct driveInfo *hdd = dev->devInfo->info; +int hdInit( struct device_node *dev ) { - for (counter = 1000000; counter >= 0; counter--) { - retVal = inportByte(hdd->hdPort + ATA_COMMAND) & ATA_S_BUSY; + u_int8_t retVal = 0x0; + int counter = 0x0; + u_int16_t *tmp = 0x0; + struct driveInfo *hdd = dev->devInfo->info; - if (!retVal) - goto ready; - } + for( counter = 1000000; counter >= 0; counter-- ) { - kprintf("Error Initializing Drive\n"); - return (1); + retVal = inportByte( hdd->hdPort + ATA_COMMAND ) & ATA_S_BUSY; - ready: + if( !retVal ) { - outportByte(hdd->hdPort + ATA_DRIVE, hdd->hdDev); - outportByte(hdd->hdPort + ATA_COMMAND, ATA_IDENTIFY); + goto ready; - //for (counter = 1000000; counter >= 0; counter--) { - for (counter = 10000000; counter >= 0; counter--) { - retVal = inportByte(hdd->hdPort + ATA_COMMAND); - if ((retVal & 1) != 0x0) { - kprintf("Error Drive Not Available\n"); - return (1); - } - if ((retVal & 8) != 0x0) { - goto go; - } - } + } - kprintf("Time Out Waiting On Drive\n"); - return (1); + } - go: + kprintf( "Error Initializing Drive\n" ); - tmp = (u_int16_t *) hdd->ata_identify; + return( 1 ); - for (counter = 0; counter < 256; counter++) { - tmp[counter] = inportWord(hdd->hdPort + ATA_DATA); - } + ready: - retVal = tmp[0x2F] & 0xFF; + outportByte( hdd->hdPort + ATA_DRIVE, hdd->hdDev ); + outportByte( hdd->hdPort + ATA_COMMAND, ATA_IDENTIFY ); - switch (retVal) { - case 0: - hdd->hdShift = 0; - hdd->hdMulti = 1; - break; - case 2: - hdd->hdShift = 1; - hdd->hdMulti = retVal; - break; - case 4: - hdd->hdShift = 2; - hdd->hdMulti = retVal; - break; - case 8: - hdd->hdShift = 3; - hdd->hdMulti = retVal; - break; - case 16: - hdd->hdShift = 4; - hdd->hdMulti = retVal; - break; - case 32: - hdd->hdShift = 5; - hdd->hdMulti = retVal; - break; - case 64: - hdd->hdShift = 6; - hdd->hdMulti = retVal; - break; - case 128: - hdd->hdShift = 7; - hdd->hdMulti = retVal; - break; - default: - kprintf("Error BLOCK Mode Unavailable: [%x]\n", retVal); - return (1); - } + //for (counter = 1000000; counter >= 0; counter--) { + for( counter = 10000000; counter >= 0; counter-- ) { - if (hdd->ata_identify->command_set_enabled1 & ATA_IDENTIFY_COMMAND_SET_SUPPORTED1_48BIT_ENABLE) { - hdd->lba_high = hdd->ata_identify->max_48bit_lba[7] << 24; - hdd->lba_high |= hdd->ata_identify->max_48bit_lba[6] << 16; - hdd->lba_high |= hdd->ata_identify->max_48bit_lba[5] << 8; - hdd->lba_high |= hdd->ata_identify->max_48bit_lba[4]; + retVal = inportByte( hdd->hdPort + ATA_COMMAND ); - hdd->lba_low = hdd->ata_identify->max_48bit_lba[3] << 24; - hdd->lba_low |= hdd->ata_identify->max_48bit_lba[2] << 16; - hdd->lba_low |= hdd->ata_identify->max_48bit_lba[1] << 8; - hdd->lba_low |= hdd->ata_identify->max_48bit_lba[0]; - } - else { - hdd->lba_high = 0; - hdd->lba_low = hdd->ata_identify->total_num_sectors[3] << 24; - hdd->lba_low |= hdd->ata_identify->total_num_sectors[2] << 16; - hdd->lba_low |= hdd->ata_identify->total_num_sectors[1] << 8; - hdd->lba_low |= hdd->ata_identify->total_num_sectors[0]; - } + if( ( retVal & 1 ) != 0x0 ) { - // If the ATA device reports its sector size (bit 12 of Word 106), then use that instead. - if (hdd->ata_identify->physical_logical_sector_info & ATA_IDENTIFY_SECTOR_LARGER_THEN_512_ENABLE) { - hdd->sector_size = hdd->ata_identify->words_per_logical_sector[3] << 24; - hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[2] << 16; - hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[1] << 8; - hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[0]; - } - else { - // Default the sector size to 512 bytes - hdd->sector_size = 512; - } + kprintf( "Error Drive Not Available\n" ); - kprintf("LBA [0x%X - 0x%X], LBA_HIGH: %i, LBA_LOW: %i, SECTOR_SIZE: %i\n", hdd->ata_identify->command_set_enabled1, hdd->ata_identify->command_set_enabled1 & ATA_IDENTIFY_COMMAND_SET_SUPPORTED1_48BIT_ENABLE, hdd->lba_high, hdd->lba_low, hdd->sector_size); + return( 1 ); - outportByte(hdd->hdPort + hdSecCount, retVal); - outportByte(hdd->hdPort + hdHead, hdd->hdDev); - outportByte(hdd->hdPort + hdCmd, 0xC6); + } - hdd->hdMask = retVal; - //hdd->hdSize = (hdd->hdSector[0x7B] * 256 * 256 * 256) + (hdd->hdSector[0x7A] * 256 * 256) + (hdd->hdSector[0x79] * 256) + hdd->hdSector[0x78]; + if( ( retVal & 8 ) != 0x0 ) { - // MrOlsen (2016-01-11) NOTE: Must phase out hdSize - hdd->hdSize = hdd->lba_low + hdd->lba_high; - hdd->hdEnable = 1; + goto go; - kprintf("Drive: [0x%X/0x%X], Size: [%i Sectors/%i MB]\n", hdd->hdPort, hdd->hdDev, hdd->hdSize, hdd->hdSize / 2048); - dev->devInfo->size = hdd->hdSize * 512; - dev->devInfo->initialized = 0x1; - return (0x0); + } + + } + + kprintf( "Time Out Waiting On Drive\n" ); + + return( 1 ); + + go: + + tmp = ( u_int16_t * ) hdd->ata_identify; + + for( counter = 0; counter < 256; counter++ ) { + + tmp[counter] = inportWord( hdd->hdPort + ATA_DATA ); + + } + + retVal = tmp[0x2F] & 0xFF; + + switch( retVal ) { + + case 0: + + hdd->hdShift = 0; + hdd->hdMulti = 1; + + break; + case 2: + + hdd->hdShift = 1; + hdd->hdMulti = retVal; + + break; + case 4: + + hdd->hdShift = 2; + hdd->hdMulti = retVal; + + break; + case 8: + + hdd->hdShift = 3; + hdd->hdMulti = retVal; + + break; + case 16: + + hdd->hdShift = 4; + hdd->hdMulti = retVal; + + break; + case 32: + + hdd->hdShift = 5; + hdd->hdMulti = retVal; + + break; + case 64: + + hdd->hdShift = 6; + hdd->hdMulti = retVal; + + break; + case 128: + + hdd->hdShift = 7; + hdd->hdMulti= retVal; + + break; + default: + + kprintf( "Error BLOCK Mode Unavailable: [%x]\n", retVal ); + + return (1); + + } + + + if( hdd->ata_identify->command_set_enabled1 & ATA_IDENTIFY_COMMAND_SET_SUPPORTED1_48BIT_ENABLE ) { + + hdd->lba_high = hdd->ata_identify->max_48bit_lba[7] << 24; + hdd->lba_high |= hdd->ata_identify->max_48bit_lba[6] << 16; + hdd->lba_high |= hdd->ata_identify->max_48bit_lba[5] << 8; + hdd->lba_high |= hdd->ata_identify->max_48bit_lba[4]; + + hdd->lba_low = hdd->ata_identify->max_48bit_lba[3] << 24; + hdd->lba_low |= hdd->ata_identify->max_48bit_lba[2] << 16; + hdd->lba_low |= hdd->ata_identify->max_48bit_lba[1] << 8; + hdd->lba_low |= hdd->ata_identify->max_48bit_lba[0]; + + } else { + + hdd->lba_high = 0; + + hdd->lba_low = hdd->ata_identify->total_num_sectors[3] << 24; + hdd->lba_low |= hdd->ata_identify->total_num_sectors[2] << 16; + hdd->lba_low |= hdd->ata_identify->total_num_sectors[1] << 8; + hdd->lba_low |= hdd->ata_identify->total_num_sectors[0]; + + } + + // If the ATA device reports its sector size (bit 12 of Word 106), then use that instead. + if( hdd->ata_identify->physical_logical_sector_info & ATA_IDENTIFY_SECTOR_LARGER_THEN_512_ENABLE ) { + + hdd->sector_size = hdd->ata_identify->words_per_logical_sector[3] << 24; + hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[2] << 16; + hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[1] << 8; + hdd->sector_size |= hdd->ata_identify->words_per_logical_sector[0]; + + } else { + + // Default the sector size to 512 bytes + hdd->sector_size = 512; + + } + + kprintf( "LBA [0x%X - 0x%X], LBA_HIGH: %i, LBA_LOW: %i, SECTOR_SIZE: %i\n", hdd->ata_identify->command_set_enabled1, hdd->ata_identify->command_set_enabled1 & ATA_IDENTIFY_COMMAND_SET_SUPPORTED1_48BIT_ENABLE, hdd->lba_high, hdd->lba_low, hdd->sector_size ); + + + outportByte( hdd->hdPort + hdSecCount, retVal ); + outportByte( hdd->hdPort + hdHead, hdd->hdDev ); + outportByte( hdd->hdPort + hdCmd, 0xC6 ); + + hdd->hdMask = retVal; + //hdd->hdSize = (hdd->hdSector[0x7B] * 256 * 256 * 256) + (hdd->hdSector[0x7A] * 256 * 256) + (hdd->hdSector[0x79] * 256) + hdd->hdSector[0x78]; + + // MrOlsen (2016-01-11) NOTE: Must phase out hdSize + hdd->hdSize = hdd->lba_low + hdd->lba_high; + hdd->hdEnable = 1; + + kprintf( "Drive: [0x%X/0x%X], Size: [%i Sectors/%i MB]\n", hdd->hdPort, hdd->hdDev, hdd->hdSize, hdd->hdSize / 2048 ); + + dev->devInfo->size = hdd->hdSize * 512; + dev->devInfo->initialized = 0x1; + + return (0x0); + } int hdWrite(struct driveInfo *hdd, void *baseAddr, uInt32 startSector, uInt32 sectorCount) {