From e40d4b267726315c5896f875d4a83cd3a42042dd Mon Sep 17 00:00:00 2001 From: Pankaj Raghav Date: Sat, 26 Mar 2022 21:37:56 +0100 Subject: [PATCH] Kernel: Remove hardcoded values for block size in IDEChannel --- Kernel/Storage/ATA/IDEChannel.cpp | 23 ++++++++++++----------- Kernel/Storage/ATA/IDEChannel.h | 1 + 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Kernel/Storage/ATA/IDEChannel.cpp b/Kernel/Storage/ATA/IDEChannel.cpp index f3ffdae92c..0efb001fd2 100644 --- a/Kernel/Storage/ATA/IDEChannel.cpp +++ b/Kernel/Storage/ATA/IDEChannel.cpp @@ -378,8 +378,8 @@ UNMAP_AFTER_INIT void IDEChannel::detect_disks() } // FIXME: Handle possible OOM situation here. - ByteBuffer wbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors(); - ByteBuffer bbuf = ByteBuffer::create_uninitialized(512).release_value_but_fixme_should_propagate_errors(); + ByteBuffer wbuf = ByteBuffer::create_uninitialized(m_logical_sector_size).release_value_but_fixme_should_propagate_errors(); + ByteBuffer bbuf = ByteBuffer::create_uninitialized(m_logical_sector_size).release_value_but_fixme_should_propagate_errors(); u8* b = bbuf.data(); u16* w = (u16*)wbuf.data(); @@ -407,13 +407,12 @@ UNMAP_AFTER_INIT void IDEChannel::detect_disks() if (identify_block.commands_and_feature_sets_supported[1] & (1 << 10)) max_addressable_block = identify_block.user_addressable_logical_sectors_count; - dbgln("IDEChannel: {} {} {} device found: Name={}, Capacity={}, Capabilities={:#04x}", channel_type_string(), channel_string(i), !command_set_is_atapi ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), max_addressable_block * 512, capabilities); - // FIXME: Don't assume all drives will have logical sector size of 512 bytes. + dbgln("IDEChannel: {} {} {} device found: Name={}, Capacity={}, Capabilities={:#04x}", channel_type_string(), channel_string(i), !command_set_is_atapi ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), max_addressable_block * m_logical_sector_size, capabilities); ATADevice::Address address = { m_channel_type == ChannelType::Primary ? static_cast(0) : static_cast(1), static_cast(i) }; if (i == 0) { - m_master = ATADiskDevice::create(m_parent_controller, address, capabilities, 512, max_addressable_block); + m_master = ATADiskDevice::create(m_parent_controller, address, capabilities, m_logical_sector_size, max_addressable_block); } else { - m_slave = ATADiskDevice::create(m_parent_controller, address, capabilities, 512, max_addressable_block); + m_slave = ATADiskDevice::create(m_parent_controller, address, capabilities, m_logical_sector_size, max_addressable_block); } } } @@ -478,8 +477,9 @@ bool IDEChannel::ata_do_read_sector() VERIFY(!m_current_request.is_null()); dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector"); auto& request = *m_current_request; - auto out_buffer = request.buffer().offset(m_current_request_block_index * 512); - auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](Bytes bytes) { + auto block_size = m_current_request->block_size(); + auto out_buffer = request.buffer().offset(m_current_request_block_index * block_size); + auto result = request.write_to_buffer_buffered(out_buffer, block_size, [&](Bytes bytes) { for (size_t i = 0; i < bytes.size(); i += sizeof(u16)) *(u16*)bytes.offset_pointer(i) = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get()); return bytes.size(); @@ -519,9 +519,10 @@ void IDEChannel::ata_do_write_sector() u8 status = m_io_group.control_base().in(); VERIFY(status & ATA_SR_DRQ); - auto in_buffer = request.buffer().offset(m_current_request_block_index * 512); - dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status); - auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](ReadonlyBytes readonly_bytes) { + auto block_size = m_current_request->block_size(); + auto in_buffer = request.buffer().offset(m_current_request_block_index * block_size); + dbgln_if(PATA_DEBUG, "IDEChannel: Writing {} bytes (part {}) (status={:#02x})...", block_size, m_current_request_block_index, status); + auto result = request.read_from_buffer_buffered(in_buffer, block_size, [&](ReadonlyBytes readonly_bytes) { for (size_t i = 0; i < readonly_bytes.size(); i += sizeof(u16)) IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)readonly_bytes.offset(i)); return readonly_bytes.size(); diff --git a/Kernel/Storage/ATA/IDEChannel.h b/Kernel/Storage/ATA/IDEChannel.h index 7d3246d43d..bc1f3c1540 100644 --- a/Kernel/Storage/ATA/IDEChannel.h +++ b/Kernel/Storage/ATA/IDEChannel.h @@ -105,6 +105,7 @@ public: private: void complete_current_request(AsyncDeviceRequest::RequestResult); void initialize(); + static constexpr size_t m_logical_sector_size = 512; protected: enum class LBAMode : u8 {