Method and system for Android to send SCSI command to USB device and storage medium
By using the C library to call the libusb_bulk_transfer interface of the libusb library on an Android device, transmitting the SCSI command descriptor block to the USB device, solving the problem of sending SCSI commands to the USB device on an Android device, realizing functions such as unlocking partitions and obtaining device information, and having good cross-platform compatibility and performance.
Patent Information
- Application Number
- CN202411677260.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-22
- Publication Date
- 2025-04-11
AI Technical Summary
On Android devices, there is a lack of effective ways to send SCSI commands to USB devices, especially when it is necessary to unlock partitions or obtain device information.
The C library calls the libusb_bulk_transfer interface of the libusb library, transmits the CBW packet to the USB device, and uses the USB device control chip to perform corresponding operations.
It implements sending standard and customized SCSI commands to USB devices on Android devices, solving problems such as unlocking partitions and obtaining device information, and has good cross-platform compatibility and performance.
Smart Images

Figure CN119806855A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data transmission, and in particular to a method, system and storage medium for an Android to send a SCSI command to a USB device. Background Art
[0002] A USB flash drive is required to exchange data between a mobile phone and a PC or other mobile device. In the field of security, after the USB flash drive is connected to Android, the user needs to enter a password to unlock the USB flash drive partition before it can be seen. This requires a customized secure USB flash drive. The hiding and display of partitions are based on the security chip of the USB flash drive, so in such scenarios, Android applications (hereinafter referred to as APP) often need to send custom SCSI control commands to the USB mass storage device (hereinafter referred to as USB device) to unlock the partition, or the APP needs to send standard SCSI commands to obtain the Identify and Smart information of the USB device. This involves the need for the APP to send standard and custom SCSI commands to the USB device, so how to send SCSI commands to the USB device? This is the technical problem to be solved by the present invention. Summary of the invention
[0003] The purpose of the present invention is to overcome the deficiencies of the prior art and to provide a method, system and storage medium for Android to send SCSI commands to a USB device.
[0004] The objective of the present invention is achieved through the following technical solutions: The first aspect of the present invention provides: a method for sending a SCSI command from an Android device to a USB device, comprising the following steps: The Android application requests to interact with the USB device; In the C library, call the libusb_bulk_transfer interface of the libusb library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The USB device control chip performs corresponding operations according to the SCSI command descriptor block.
[0005] Preferably, when performing a read operation, the following steps are included: The C library packages the command code of the read command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Wrap CBW command block; Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to obtain the DataBuffer of the cached Identify information from the USB device input endpoint; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The C library parses the Identify information and returns the parsed information to the JAVA layer, which is then displayed on the interface by the UI layer.
[0006] Preferably, when performing a write operation, the following steps are included: Determine the USB device status; The C library packages the command code of the write command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Wrap CBW command block; Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to send the password data KekBuffer entered by the user to the output endpoint of the USB device; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The command execution status is returned to the JAVA layer and displayed on the interface by the UI layer.
[0007] Preferably, the preset bytes are 13Byte.
[0008] Preferably, the SCSI command descriptor block includes standard SCSI commands and / or custom SCSI commands. Preferably, the Android application communicates with the USB device from user space.
[0009] The second aspect of the present invention provides: a system for sending a SCSI command from an Android device to a USB device, which is used to implement any of the above methods for sending a SCSI command from an Android device to a USB device, comprising: The request module is used to request the interaction with the USB device using the Android application; The transmission module is used to call the libusb_bulk_transfer interface of the libusb library in the C library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The execution module is used to use the USB device control chip to perform corresponding operations according to the SCSI command descriptor block.
[0010] The third aspect of the present invention provides: a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are loaded and executed by a processor, any of the above-mentioned methods of sending SCSI commands from Android to a USB device is implemented.
[0011] The beneficial effects of the present invention are: 1) Good portability, cross-platform, encapsulation of business logic code and sending SCSI commands, can be used on Android, Windows, and Linux, reducing maintenance costs.
[0012] 2) C-based implementation is faster; standard and custom SCSI commands can be sent; the problem of no effective way to send SCSI commands on Android is solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Figure 1 Flowchart of the method for sending SCSI commands to USB devices for Android; Figure 2 This is a flowchart of the read operation; Figure 3 This is a flowchart of the write operation. DETAILED DESCRIPTION
[0014] The technical solution of the present invention will be clearly and completely described below in conjunction with the embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0015] See also Figure 1-Figure 3 The first aspect of the present invention provides: a method for sending a SCSI command from an Android device to a USB device, comprising the following steps: The Android application requests to interact with the USB device; In the C library, call the libusb_bulk_transfer interface of the libusb library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The USB device control chip performs corresponding operations according to the SCSI command descriptor block.
[0016] In this embodiment, standard and customized SCSI commands are supported, and the APP provides an operation interface, such as a UI that displays USB device information and unlocks partitions. The UI layer language is usually implemented by VUE and JAVA. JAVA calls JNI to interact with the cross-platform C library. The C library encapsulates business logic, encapsulates SCSI commands, and parses data that interact with USB devices. The specific implementation is to use the libusb library that supports the USB BOT protocol. Libusb is a cross-platform open source library that allows applications to communicate with USB devices from user space. The C library calls the interface libusb_bulk_transfer provided by libusb to transmit the CBW packet to transfer the SCSI command descriptor block (CDB) to the USB device. The device control chip will perform corresponding operations according to the SCSI command, such as unlocking the partition.
[0017] In some embodiments, when performing a read operation, the following steps are included: The C library packages the command code of the read command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Pack the CBW command block; (according to the CBW protocol CDB is in bytes 16-31) Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to obtain the DataBuffer of the cached Identify information from the USB device input endpoint; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The C library parses the Identify information and returns the parsed information to the JAVA layer, which is then displayed on the interface by the UI layer.
[0018] In this embodiment, the interaction between the Android host and the USB device is divided into read operations and write operations. The following describes our implementation method using the two scenarios of read and write. Figure 2 As shown in the figure, in the read command scenario, take the standard Identify command (command code is 85) as an example: the UI layer needs to display USB device information, such as capacity, serial number, etc. The UI layer will call JNI through JAVA and send the request to the C library. After the C library interacts with the device as the host side, it returns the execution result to the JAVA layer, which is then displayed on the interface by the UI.
[0019] In some embodiments, when performing a write operation, the following steps are included: Determine the USB device status; The C library packages the command code of the write command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Wrap CBW command block; Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to send the password data KekBuffer entered by the user to the output endpoint of the USB device; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The command execution status is returned to the JAVA layer and displayed on the interface by the UI layer.
[0020] In this embodiment, if Figure 3 As shown in the figure, in the data writing scenario, take the custom unlock partition command (command code is F1) as an example: the user clicks unlock on the interface and enters the password. The request is also sent to the C library. After the host side and the device interact, the execution result is returned to the JAVA layer and then displayed on the interface by the UI.
[0021] Whether it is a read or write command, data transmission to the bulk endpoint of a USB device through libusb must go through three stages: CBW->DATA->CSW. CBW stage: CBW is a data block that carries the SCSI command sent by the host to the device. After receiving the CBW, the device can know what to do in the next DATA stage. DATA stage: There are three situations in the DATA stage: no data needs to be transmitted; IN transmission (input endpoint, device to host); OUT transmission (output endpoint, host to device). CSW stage: The CSW stage is when the device feeds back commands and data transmission results to the host.
[0022] In some embodiments, the preset bytes are 13 bytes.
[0023] In some embodiments, the SCSI command descriptor block includes standard SCSI commands and / or custom SCSI commands. In some embodiments, an Android application communicates with a USB device from user space.
[0024] The second aspect of the present invention provides: a system for sending a SCSI command from an Android device to a USB device, which is used to implement any of the above methods for sending a SCSI command from an Android device to a USB device, comprising: The request module is used to request the interaction with the USB device using the Android application; The transmission module is used to call the libusb_bulk_transfer interface of the libusb library in the C library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The execution module is used to use the USB device control chip to perform corresponding operations according to the SCSI command descriptor block.
[0025] The third aspect of the present invention provides: a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are loaded and executed by a processor, any of the above-mentioned methods of sending SCSI commands from Android to a USB device is implemented.
[0026] The above is only a preferred embodiment of the present invention. It should be understood that the present invention is not limited to the form disclosed herein, and should not be regarded as excluding other embodiments, but can be used in various other combinations, modifications and environments, and can be modified within the scope of the concept described herein through the above teachings or the technology or knowledge of the relevant field. The changes and modifications made by those skilled in the art shall not deviate from the spirit and scope of the present invention, and shall be within the scope of protection of the claims attached to the present invention.
Claims
1. A method for sending a SCSI command from an Android device to a USB device, characterized in that: The following steps are involved: The Android application requests to interact with the USB device; In the C library, call the libusb_bulk_transfer interface of the libusb library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The USB device control chip performs corresponding operations according to the SCSI command descriptor block.
2. The method for sending a SCSI command from Android to a USB device according to claim 1, characterized in that: When performing a read operation, the following steps are included: The C library packages the command code of the read command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Wrap CBW command block; Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to obtain the DataBuffer of the cached Identify information from the USB device input endpoint; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The C library parses the Identify information and returns the parsed information to the JAVA layer, which is then displayed on the interface by the UI layer.
3. The method for sending a SCSI command from Android to a USB device according to claim 1, characterized in that: When performing a write operation, the following steps are included: Determine the USB device status; The C library packages the command code of the write command into the SCSI command descriptor block; Call the libusb_get_descriptor interface of the libusb library to obtain the USB device descriptor, bulk input endpoint, and bulk output endpoint; Wrap CBW command block; Call the libusb_bulk_transfer interface of the libusb library to send a CBW packet to the output endpoint of the USB device, and the USB device performs corresponding operations according to the SCSI command; Call the libusb_bulk_transfer interface of the libusb library to send the password data KekBuffer entered by the user to the output endpoint of the USB device; Call the libusb_bulk_transfer interface of the libusb library to send the preset bytes of cached data to the USB device input endpoint, receive the CSW packet sent by the USB device, and the host side determines the command execution status based on the CSW packet; The command execution status is returned to the JAVA layer and displayed on the interface by the UI layer.
4. The method for sending a SCSI command from Android to a USB device according to claim 2 or 3, characterized in that: The preset bytes are 13Byte.
5. The method for sending a SCSI command from Android to a USB device according to any one of claims 1 to 3, characterized in that: The SCSI command descriptor block includes standard SCSI commands and / or user-defined SCSI commands.
6. The method for sending a SCSI command from Android to a USB device according to any one of claims 1 to 3, characterized in that: Android application communicates with USB device from user space.
7. A system for sending SCSI commands from Android to a USB device, characterized in that: The method for implementing the method of sending a SCSI command from Android to a USB device as described in any one of claims 1 to 6 comprises: The request module is used to request the interaction with the USB device using the Android application; The transmission module is used to call the libusb_bulk_transfer interface of the libusb library in the C library to transfer the SCSI command descriptor block to the USB device by transmitting the CBW packet; The execution module is used to use the USB device control chip to perform corresponding operations according to the SCSI command descriptor block.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are loaded and executed by the processor, the method for sending SCSI commands from Android to a USB device as described in any one of claims 1-6 is implemented.