package com.github.mjdev.libaums.driver.scsi;

import android.util.Log;
import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import com.github.mjdev.libaums.driver.scsi.commands.CommandBlockWrapper;
import com.github.mjdev.libaums.driver.scsi.commands.CommandStatusWrapper;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiInquiry;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiInquiryResponse;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiRead10;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiReadCapacity;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiReadCapacityResponse;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiTestUnitReady;
import com.github.mjdev.libaums.driver.scsi.commands.ScsiWrite10;
import com.github.mjdev.libaums.usb.UsbCommunication;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class ScsiBlockDevice implements BlockDeviceDriver {
    private static final String TAG = "ScsiBlockDevice";
    private int blockSize;
    private int lastBlockAddress;
    private UsbCommunication usbCommunication;
    private ScsiWrite10 writeCommand = new ScsiWrite10();
    private ScsiRead10 readCommand = new ScsiRead10();
    private CommandStatusWrapper csw = new CommandStatusWrapper();
    private ByteBuffer outBuffer = ByteBuffer.allocate(31);
    private ByteBuffer cswBuffer = ByteBuffer.allocate(13);

    public ScsiBlockDevice(UsbCommunication usbCommunication) {
        this.usbCommunication = usbCommunication;
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public void init() throws IOException {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(36);
        transferCommand(new ScsiInquiry((byte) byteBufferAllocate.array().length), byteBufferAllocate);
        byteBufferAllocate.clear();
        ScsiInquiryResponse scsiInquiryResponse = ScsiInquiryResponse.read(byteBufferAllocate);
        String str = TAG;
        Log.d(str, "inquiry response: " + scsiInquiryResponse);
        if (scsiInquiryResponse.getPeripheralQualifier() != 0 || scsiInquiryResponse.getPeripheralDeviceType() != 0) {
            throw new IOException("unsupported PeripheralQualifier or PeripheralDeviceType");
        }
        if (!transferCommand(new ScsiTestUnitReady(), null)) {
            Log.w(str, "unit not ready!");
        }
        ScsiReadCapacity scsiReadCapacity = new ScsiReadCapacity();
        byteBufferAllocate.clear();
        transferCommand(scsiReadCapacity, byteBufferAllocate);
        byteBufferAllocate.clear();
        ScsiReadCapacityResponse scsiReadCapacityResponse = ScsiReadCapacityResponse.read(byteBufferAllocate);
        this.blockSize = scsiReadCapacityResponse.getBlockLength();
        this.lastBlockAddress = scsiReadCapacityResponse.getLogicalBlockAddress();
        Log.i(str, "Block size: " + this.blockSize);
        Log.i(str, "Last block address: " + this.lastBlockAddress);
    }

    private boolean transferCommand(CommandBlockWrapper commandBlockWrapper, ByteBuffer byteBuffer) throws IOException {
        byte[] bArrArray = this.outBuffer.array();
        Arrays.fill(bArrArray, (byte) 0);
        this.outBuffer.clear();
        commandBlockWrapper.serialize(this.outBuffer);
        this.outBuffer.clear();
        if (this.usbCommunication.bulkOutTransfer(this.outBuffer) != bArrArray.length) {
            throw new IOException("Writing all bytes on command " + commandBlockWrapper + " failed!");
        }
        int i = commandBlockWrapper.getdCbwDataTransferLength();
        if (i > 0) {
            if (commandBlockWrapper.getDirection() == CommandBlockWrapper.Direction.IN) {
                int iBulkInTransfer = 0;
                do {
                    iBulkInTransfer += this.usbCommunication.bulkInTransfer(byteBuffer);
                } while (iBulkInTransfer < i);
                if (iBulkInTransfer != i) {
                    throw new IOException("Unexpected command size (" + iBulkInTransfer + ") on response to " + commandBlockWrapper);
                }
            } else {
                int iBulkOutTransfer = 0;
                do {
                    iBulkOutTransfer += this.usbCommunication.bulkOutTransfer(byteBuffer);
                } while (iBulkOutTransfer < i);
                if (iBulkOutTransfer != i) {
                    throw new IOException("Could not write all bytes: " + commandBlockWrapper);
                }
            }
        }
        this.cswBuffer.clear();
        if (this.usbCommunication.bulkInTransfer(this.cswBuffer) != 13) {
            throw new IOException("Unexpected command size while expecting csw");
        }
        this.cswBuffer.clear();
        this.csw.read(this.cswBuffer);
        if (this.csw.getbCswStatus() != 0) {
            throw new IOException("Unsuccessful Csw status: " + ((int) this.csw.getbCswStatus()));
        }
        if (this.csw.getdCswTag() == commandBlockWrapper.getdCbwTag()) {
            return this.csw.getbCswStatus() == 0;
        }
        throw new IOException("wrong csw tag!");
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public synchronized void read(long j, ByteBuffer byteBuffer) throws IOException {
        if (byteBuffer.remaining() % this.blockSize != 0) {
            throw new IllegalArgumentException("dest.remaining() must be multiple of blockSize!");
        }
        this.readCommand.init((int) j, byteBuffer.remaining(), this.blockSize);
        transferCommand(this.readCommand, byteBuffer);
        byteBuffer.position(byteBuffer.limit());
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public synchronized void write(long j, ByteBuffer byteBuffer) throws IOException {
        if (byteBuffer.remaining() % this.blockSize != 0) {
            throw new IllegalArgumentException("src.remaining() must be multiple of blockSize!");
        }
        this.writeCommand.init((int) j, byteBuffer.remaining(), this.blockSize);
        transferCommand(this.writeCommand, byteBuffer);
        byteBuffer.position(byteBuffer.limit());
    }

    @Override // com.github.mjdev.libaums.driver.BlockDeviceDriver
    public int getBlockSize() {
        return this.blockSize;
    }
}
