How to disable android device usb port - android

I am trying to search if there is a way to disable android device usb port, software level mainly. So users can still charge android device but cannot communicate with PC anymore.
I got some clues in link1. But it looks there is not an easy way for this.
Android OS is a Linux based OS. So I am wondering if there is a config file in Android OS that allows root users to disable usb port, like files under path /etc in Linux OS.

EDIT: Use this command in android terminal emulator. Root priveleges required, but you can put a password on Superuser so that no one else can undo this or edit the script:
echo 0 > /sys/devices/virtual/android_usb/android0/enable
or...
su -c 'echo 0 > /sys/devices/virtual/android_usb/android0/enable'
To automate this, you need to make a script and put it in /system/etc/init.d, let's say /system/etc/init.d/usb_off:
#!/system/bin/sh
echo 0 > /sys/devices/virtual/android_usb/android0/enable
Then make sure it's executable with:
chmod +x /system/etc/init.d/usb_off
You can also try this, but it's reported not to work. Execute these in the command line via Android Terminal Emulator:
setprop persist.sys.usb.config ''
setprop sys.usb.config ''

You can tell the usb-storage module to ignore a VID/PID combination
Make a file such as /etc/modprobe.d/android.conf having contents of the form
options usb-storage quirks=vid##:pid##:i
Where the vid/pid are given in hex without prefix
A serious limitation of this technique is that some installations will load the usb-storage driver during the initread stage before /etc is present and honored, so it may get loaded without the ignore quirk. To work around that you can modify the inittread archive, or you can unload and reload the usb-storage module (if nothing else in your system is using it).
Another downside is that if you decide you want to mount it, you'd have to reload the driver without the quirk (for example using insmod).

Related

How to root or GPIO access on custom android-based development board

I have this development board Open-Q 820
It is running an Android 7.0 based on some sources from CodeAurora (that seem based on AOSP sources). Seems it uses proprietary bootloader that can not be changed. I need to access GPIO (/system/class/gpio) from my android app to control an external device. Problem is I can not do this from android app - seems only root can access this files.
I can call "adb root" command from PC and then call from "adb shell" something like
echo 0 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio0/direction
echo 1 > /sys/class/gpio/gpio0/value
and it is working. But I need to do the same from my android app. I know how to access terminal and su from android app. I just dont know how to root custom device. I tried to install SuperSU.apk app and its corresponding su native app manually. It starts and says it need to update su binaries. After installing binaries and reboot the system then it becomes broken - infinite android logo. If I not install binaries update then SuperSU can grant permissions to my app but actully app still can not use su (I dont know why - I use this library inside my app: https://github.com/Chrisplus/RootManager). And after reboot SuperSU not working longer.
So may be you know some SuperSU alternatives (Knigroot not working too) or steps how to correctly install some superuser management app. Or may be I can use su directly (I tried but my app has not rights to access /system/xbin/su). Or may be I can make /sys/class/gpio accessible by android apps somehow (I tried chmod 777 on it - not works). May be some SElinux rights, but I have no experience with this.
Thank you
Magisk helped me. Since it is have option to patch custom boot image. I used this option and now root access works.

File manager detects Android device but gvfs folder is empty. How can I access the MTP device from terminal?

Directories /run/user/1000/gvfs and ~/.gvfs are emtpy and non-existing, respectively. My graphical file manager (Thunar) is able to detect and access the internal and external memory of the device.
Command gvfs-mount -l yields:
Volume(0): SAMSUNG Android
Type: GProxyVolume (GProxyVolumeMonitorMTP)
Mount(0): SAMSUNG Android -> mtp://[usb:002,003]/
Type: GProxyShadowMount (GProxyVolumeMonitorMTP)
Where can I find the mount point of the device to access it from the command line? I'm using Ubuntu 16.04.
You also need to install gvfs-fuse:
gvfsd-fuse maintains a fuse mount to make gvfs backends available to POSIX applications. The mount point for the fuse filesystem is provided by the [PATH] argument.
gvfsd-fuse is normally started by gvfsd(1). In this case, the mount point is $XDG_RUNTIME_DIR/gvfs or $HOME/.gvfs.
Here is a great explanation:
/run/user/$uid/gvfs or ~$user/.gvfs is the mount point for the FUSE interface to GVFS.
...
The GVFS-FUSE gateway makes GVFS filesystem drivers accessible to all applications, not just the ones using Gnome libraries.
Execute following commands
sudo apt install gvfs-fuse
pkill thunar
pkill gvfs
and run Thunar again.
Just had the same issue with Android 11. Solved it by going into the phone's 'Settings - Connected devices - USB' and selecting 'File Transfer' which was apparently disabled by default.

Adb backup does not work

I need help from you guys, because I don't know what I did wrong with adb backup.
I want to backup my Samsung Galaxy S3 LTE (GT-I9305) without root. I googled it and found Full Android Backup with SDK Manager. I installed all i need for this like Java Development Kit 8 version 66 (JDK 8u66 x64) and Android Studio which contains SDK Manager. Then they sad i should download the Google USB Driver. I downloaded it but nothing happened. I thought it should work now and then I went to C:\Users\MYNAME\AppData\Local\Android\sdk\platform-tools and left-clicked adb whilst holding Shift. Then I clicked "Open command window here". After this I typed in "adb devices" and it said:
List of devices attached
3204cfaaf8611199 device
Then I typed in:
adb backup -apk -shared -all -f C:\Users\NAME\backup.ab
It said
Now unlock your device and confirm the backup operation.
I did this and my Smartphone opened "Full Backup" and then I was able to set a password for the backup. I don`t need a password and I just clicked "Back up my data" Then it went back to my homescreen and said "Starting backup..." as a toast message and immediately after this it said "Backup finished" and created a backup file with 40 bytes. I don't know what I should do now.
Rather than trying to find an old version of adb, it's easier to add quotes to the arguments to adb backup :
adb backup "-apk -shared -all -f C:\Users\NAME\backup.ab"
The line, which correctly invokes adb, needs to look like this at my side:
adb backup "-apk -obb -shared -all -system" -f phone-20180522-120000.adb
This line is for Linux, but should do for Windows and OS-X as well. For Linux (and probably OS-X), you can use a script like following, which automatically assigns a timestamp to the file:
adb backup "-apk -obb -shared -all -system" -f "${PHONE:-phone}-`date +%Y%m%d-%H%M%S`.adb`"
Important After doing a backup, verify your data! At my side, sometimes some corruption of the file shows up (and I doubt it is my computer, as I only observe such errors with adb).
Here is a check instruction I use:
set -o pipefail
for a in *.adb;
do
echo "$a";
dd if="$a" bs=24 skip=1 | zlib-flate -uncompress | tar tf - >/dev/null;
echo "ret=$?";
done
It should show ret=0, but it doen't.
If you see inflate: data: invalid code lengths set your archive is corrupted and - very likely - unusable for restore!
If you see tar: Unexpected EOF in archive your backup probably is usable (all backups end this way, I do not know why).
Even if you see just ret=0 there might be undiscovered errors which still prevent a restore.
There is definitively missing an adb verify command, to verify correctness of a backup!
FYI
I post this update, as all other answers were helpful, but not exact to the last detail. (When used with full quotes, a backup was done, but called backup.ab)
Here is my environment:
OS: Ubuntu 16.04
Phone: Android 7.0
ADB: 1.0.32
The full procedure was (just in case somebody stumbles upon this not knowing what is needed):
Zeroth: Install adb (on Ubuntu: sudo apt-get install android-tools-adb)
First: Enable USB debugging mode on the phone.
Second: Attach phone to USB of the computer with an USB data cable (a charge only cable is not enough)
Note that the Ubuntu Phone Manager might show up and ask you for the phone's PIN. You do not need that, close this if it happens.
Third: run adb devices - you should see something like XXXXXXXXXXXX unauthorized
After some time on your phone a message shows up which asks for USB debugging permission
Allow this once. For security reasons, you should not automatically trust the computer, as the fingerprint can be easily faked.
Note that if something is obstructing a single pixel of this confirmation window (like some accessibility feature button) you cannot tap on the OK. First move the obstructing window.
Forth: Now again run adb devices - you should see something like XXXXXXXXXXXX device
If more than one line shows up, you attached more than one phone to the computer. The easy way is to unplug all those you do not want to backup. (The complex way is to set the environment variable ANDROID_SERIAL=XXXXXXXXXXXX.)
Now run adb backup as shown above. Change the file to your needs.
This brings up a window which asks for backup permission.
Do not close the backup window while the backup is taken. When I tried this, the backup stopped and the resulting archive was broken.
You can give a password to encrypt your backup. I do not have any information on how secure the password is. But it is very likely, that you cannot restore the backup taken in case you ever forget this password.
I have no idea on how to restore such a backup to other phones. I even never tried the restore, so I cannot help here, too.
Notes:
adb help shows all possible options to adb backup
Close all open applications on your phone before the backup. I do not know if this is needed, but it certainly cannot hurt.
Leave the phone alone while the backup runs. I do not know if this is needed, but it certainly cannot hurt.
Do not be impatient. Leave the backup plenty of time. At my side I see a progress of about 100 MB/Minute (YMMV). So the backup takes about half an hour for my phone (2.5 GB).
Apparently adb backup does not completely backup everything!
I had a look into the backup (cd X && dd if=../XXXX.adb bs=24 skip=1 | zlib-flate -uncompress | tar xf -) and did not find all installed applications.
What I was able to find was (note that this list is incomplete):
Internal emulated SD-Card: shared/0
External SD-Card: shared/1
Apps: apps/ - many apps (like Google Authenticator) were missing
Calendar: apps/com.android.providers.calendar/db/calendar.db (probably)
What I was not able to find (note that this list is incomplete):
Alarms. (I was probably not able to detect the right app)
Full external SD-card (there is more on this card than what is shown below share/1. For example the apps, which are moved-to-SD.)
Conclusions:
Sometimes adb backup might create corrupt backups, which can go unnoticed. So either check your backup or do frequent backups and pretend to be lucky enough such that not all backups go corrupt.
With adb backup you get a backup of most of your precious data, like camera images and so on. It might be a bit difficult to unpack it, though.
adb backup is not enough to do a complete backup of your phone.
adb backup is not enough to backup your SD-card such, that if it breaks, you are able to replace it by a new one with the contents restored. This is very sad!
PS: The typo "Forth" is not an accidental one.
PPS: zlib-flate can be found in package qpdf on Ubuntu 18.04
This just worked using Ubuntu 15.10 ("wily") to backup a Galaxy SM-G900V running Android version 6.0.1.
Installed using:
sudo apt-get install android-tools-adb
Then I ran the following from the command line (without 'sudo'):
adb backup -apk -shared -all -f backup.adb
Result:
user#hostname:~/droid$ ls -lah
total 1.2GB
drwxrwxr-x 2 user user 4.0BK Mar 6 18:43 .
drwxr-xr-x 52 user user 4.0KB Mar 6 17:40 ..
-rw-r----- 1 user user 1.2GB Mar 6 18:29 backup.ab
Thank you "NG". I was beginning to think my phone was borked.
Forgot to add: Running adb version: Android Debug Bridge version 1.0.31
Seem not all apps can be backed up. Android apparently allow apps to disable backup :(
https://developer.android.com/guide/topics/manifest/application-element.html (search for allowbackup)
The problem is the adb version of your PC. Using adb version 1.0.31 the backup/restore will be sucessfull.
More info on: https://android.stackexchange.com/questions/83080/adb-backup-creates-0-byte-file-prompts-for-current-backup-password-even-though
and How to downgrade my SDK Version?
If your device has an adb version prior to 1.0.31, then you must use an adb version of 1.0.31 on PC. Versions of adb 1.0.32+ broken the backwards compatibility. Source: Issue 208337
Just incase anyone is still having trouble - In windows, I found putting the quotes around only the directory, for example "C:\Users\NAME\backup.ab" worked.
I found this solution :
adb backup -apk -shared -all -f "<path-where-the-backup-will-be-created>.ab"
And to restore :
adb restore "<path-where-the-backup-is>.ab"

Android Device Driver make node

I have a course project that involves setting up a device driver on Android. I have previously worked with device drivers in the Linux kernel and we used two commands to initialize the device and make a node: insmod and mknod
Now when I launched the emulator shell using adb shell, I was able to use insmod but mknod did not work. I have tried to find alternatives but was not lucky.
From what I know, mknod in the Linux kernel lists the device under the /dev directory and allows user programs to read/write to it by using its file ops.
So what is its alternative for Android?
Perhaps, the android device you're using don't have mknod command. It need to be supported from rootfs, usually Android rootfs are built using busybox. Most probably, mknod was dropped from busybox config. Possible option could be, use custom Android image where you've mknod installed.

Is it possible to disable a USB port through the Android SDK or NDK?

Is there a way of disabling a USB port on an Android enabled device using the Android SDK or NDK?
This applies to a USB accessories port (i.e. not for ADB)...
If you have a rooted device, you can try this as it worked for me...find the file indicating devices authorized for your port and clear it as follows
(Note the path will be specific to your system so it may take a bit of searching - I have a platform based on Freescale HW & BSP) You can try this from a shell session but ultimately it can be done from a script or shell commands executed from within an application
$su
#cd /sys/devices/platform/fsl-ehci.0/usb1/
#echo 0 > authorized
If you need to re-enable, just do
#cd /sys/devices/platform/fsl-ehci.0/usb1/
#echo 1 > authorized
Settings changed in this way worked for me to enable & disable both a USB keyboard and a mass storage device (thumb drive). NOTE: What you set this way may not persist through a reboot or power cycle (it did not in my case) so plan accordingly.

Categories

Resources