0

I have one phone that upon plugged into a computer, shows up as an MTP device. The SD card and the internal storage are visible on the computer. I can also change it to only mount the SD card and then it unmounts the SD card from the phone and directly connects it like if it were to be an SD to USB adapter.

I want to be able to do this on another phone, but not with an SD card, I want to be able to use a part of the internal flash memory in the same manner so that I can connect the phone as if it were a USB stick and I also want to be able to connect it as an MTP device. It would be handy if I can do this with tmpfs too, so I can basically use the phone's available RAM as temporary high speed USB storage.

The reason I want to do this is because MTP is incredibly slow for loading the directories, file transfer is a bit faster but still a fraction of what USB 2.0 can achieve. I want to use the RAM so that the internal storage isn't affected and filled up for no reason since the phone doesn't have that much free storage. On the other phone, connecting it in SD card adapter mode causes windows to see it as an actual sd card so Windows doesn't open the storage in MTP mode which misses file properties and is super sluggish. Another reason for using RAM is that the internal storage can't be unmounted while Android is running on the phone so you wouldn't be able to use it as USB storage in anything other than MTP.

Are there any ways of doing this using busybox, or by modifying some system files?

I know this has to be possible but I don't know how it works with the SD card adapter mode on the ohter phone and I don't know how to replicate it for other storage media. The phone is rooted so I can do anything to the system I want. Preferrably on Android 4 and up. How can I accomplish such result?

  • Update

I was able to get UMS working on the phone thanks to Irfan's comment. I can change the drive that mounts by editing the "file" file.

To do this, first the command cat /proc/partitions. Then you can write echo '/dev/block/vold/<major>:<minor>' -n> /sys/devices/virtual/android_usb/android0/f_mass_storage/lun/file

I am not done yet though, I need to know how to now how to forward the path in "file" to tmpfs. I can't find a device file for tmpfs like all the other partitions. Is there a vold partition for tmpfs, or can I maybe link to a folder in file? How can I put the path to tmpfs inside "file"?

0

1 Answer 1

1

I managed to get it working! Thanks to Irfan's answer I knew exactly what to do!

Here are the steps:

  1. Create a folder to mount a new RAM disk. I used /data/local/ and created a new folder. Then mounted a new RAM disk to it.

    mkdir /data/local/temp
    mount -t tmpfs tmpfs /data/local/temp
    
  2. Next, creat a new disk image. You can choose the block size and the amount of blocks for any size you like. The total size of the file is bs * count. I chose a count of 200000 for a 90MB file on the RAM.

    dd if=/dev/zero of=/data/local/temp/ram bs=512 count=200000

  3. We now want to create a loopback device so we can mount the new file on the RAM disk as a virtual semi-device. First we want to know the major value for loopback devices.

    ls -l /dev/block/loop0

  4. We now know what major value the first loopback node has. In my case it showed 7, 0. We know we will have to use major value 7 for the new device node. We can now create a new node. For this we use the command "mknod". The -m parameter specifies the permissions. I already have files loop0-17. loop0 is used for the built in cdrom, and the rest are used for mounting .asec files.

    mknod -m 0660 /dev/block/loop18 b 7 18

  5. To mount the disk image on the RAM disk we have to use losetup.

    losetup /dev/block/loop18 /data/local/temp/ram

  6. We can check what backing file associated with each loop device by reading the backing_file with the cat command.

    cat /sys/block/loop18/loop/backing_file

  7. Now that the virtual device is ready we can format it. I will use mke2fs, but you can use other methods to format the device. Make2fs will format the drive EXT2. For Windows you should format FAT32, or you can use this amazing Ext3 filesystem driver. This adds native support for EXT to Windows and is very awesome.

    mke2fs /dev/block/loop18

  8. We can finally setup UMS (USB Mass Storage) so the computer can read the device directly. You can set lun, or lun1 depending on if you want multiple virtual usb devices.

    cd /sys/class/android_usb/android0
    echo /dev/block/loop18 > f_mass_storage/lun/file
    echo mass_storage > functions
    echo 1 > enable
    

That should be it, normally a USB device shows up on the computer now.

I tested the speed and it's a lot better than MTP, and it actually uses the full RAM speed, but it's limited by USB 2.0 speeds, unless your device supports USB 3.

I ran speed test

While testing I noticed when enabling USB gadget mode and writing an incorrect path to "file", Windows breaks and suddenly can't read usb devices anymore. After this you can't reboot unless if you hold the power button down. This may be a bug in Windows, I'm not sure why it does this. Device manager also malfunctions after this.

Here is the source code of the mass storage driver on Google Source. And finally here is the documentation on the configuration on kernel.org.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .