Compile and run ARM assembly code via SSH on Android Lollipop - android

I am trying to run some ARM assembly codes on Snapdragon 810 development board to evaluate performance on Cortex-A53 and A57 processors. My codes are nothing to do with android applications and they are C/Assembly coeds. Moreover, I want to get remote access to the board preferably via SSH and run gcc commands. The board default OS in Android Lollipop and my first intention was to install Linux on it to make things work desirably. However, Qualcomm customer support informed me that Linux isn't supported by this board and I have to deal with Android.
I've already searched over various forums. Some of them suggest to root the Android device, install QuickSSHD or SSHDroid on the device and simply SSH to it. However, I am not sure if the provided console has the capability of running gcc commands, generating executable and running it. Others, suggest to generate executable using cross-compilation and push the executable via adb console and run it on the Android device. This approach makes more sense, but I need to have remote access via SSH to the board and edit my code on the device continuously.
My question is, what is the best and easiest approach to get remote access via SSH to this device, compile and run C/ARM Assembly code, transfer files and get the real timing of my codes?!

Cross compile is the easiest option to generate the executable. Else you will have to port GCC to the target first. Don't even bother.
And Adb is a lot better when dealing with Android devices as you need not install any additional applications/executables to get it working. Adb can work on TCP connection as well. So there is no need of SSH for the task. And if the device is rooted, "adb root" followed by "adb shell" would give you the root console.

Related

Interact with SELinux on GenyMotion Virtual Device

I read a lot about SELinux, and I would have liked to try and get my hands on a case study for the university.
I have installed various virtual devices on GenyMotion, from Android 6 to Android 9, but nobody owns SElinux: when I do "sestatus" it answers "disabled".
I do not think it's enough to enable it, it seems that the modules must be installed in the kernel, can someone tell me how to do it? I tried but I did not find anything, I'm having a lot of difficulties, it's the first time I do these things.
Thanks to everyone in advance.
Finally I managed to make everything work: I was able to access Selinux information via a terminal in the GenyMotion virtual device. Selinux is already active on devices after version 4.3, there is no need to install it as it said #Martin Zeitler. I explain the solution to the problem (on Linux it works).
The first thing to do is to install a terminal in the virtual device: I have installed Termux. To do this, just download the apk from a site and drag it to the virtual device, the installation will start. Subsequently, it is necessary to provide the SuperUser permissions to the Termux terminal: banally, by opening the terminal, provide the SU command, and the device will ask you if you want to provide SuperUser permissions to the Termux application. When you enter the SU command, it will allow you to browse the folders on your device. With the "ls" command, providing the "-Z" option, you can see the SElinux context of the files. From the terminal, using the "getenforce" command, you can see how Selinux is running. Here you will see other useful commands: https://www.all-things-android.com/content/se-android-commands
Now, you will need to download the tool that allows us to interact with Selinux: setools. https://github.com/pasis/setools-android These tools are written in C, the native language of Android, so you need to download the NDK library to compile them: https://developer.android.com/ndk/downloads/
The first thing to do to compile, is to specify the project path:
export NDK_PROJECT_PATH=/my/project/path . The second step is to specify the makefile:
ndk-build NDK_APPLICATION_MK=/my/project/Makefile.am (for a complete guide: https://software.intel.com/en-us/articles/building-an-android-command-line-application-using-the-ndk-build-tools)
When you have finished compiling, you can send the Setools folder to the virtual device, using the adb program. ./adb push /setool/local/folder /remote/device/folder
(example: ./adb /setool /tmp) Now that you have Setool on your device, you can run the tools in the "/setool/obj/local/x86" folder. Thanks to "sepolicy-inject" you can also add rules to the policies.
Finally, thanks to this guide I realized that Selinux policies are stored in binary format, so it is not possible to interact with them without tools.
https://ge0n0sis.github.io/posts/2015/12/exploring-androids-selinux-kernel-policy/
one cannot "install SE Linux" on Android; because it's already installed and enabled by default since API level 21. you'd need to become user root, in order to access these management commands. this would only be possible on a rooted hardware device - or a rooted emulator image.
... just install CentOS or whatever else security enabled Linux distribution, for a test.

Wrapper library for avrdude

The problem:
Through an Android app, be able to programmatically verify arduino flash contents to ensure it has not been changed (maliciously)
I am able to do that with avrdude using the command as below in the android adb shell
avrdude -C/data/data/com.myapp.avrdude/local/etc/avrdude.conf -v -patmega2560 -cstk500v2 -P/dev/ttyACM0 -b115200 -D -Uflash:v:firmware.hex:i
This works well with arduino, but the problem comes in when I want to do the same with a board that uses an FTDI chip. When it's connected to the android device it does not show up in /dev/ location.
On a linux machine the arduino device appears as /dev/ttyACM0, and the FTDI device is /dev/ttyUSB0. The problem is the FTDI device does not appear on android therefore the avrdude command above becomes useless.
I do not want to build the kernel driver for FTDI as specified here
The approach I want to go with which I think is most viable is to find/build a simple java wrapper for avrdude that interacts with the usb device at a higher level, this way I can make use of the FTDI java library to execute a command to verify flash.
Is this approach viable? If not, what is the best way to approach this problem?
Looking at the FTDI site, it looks as though there may be a driver for some FTDI ICs: FTDI Android Driver
You may then well be building avrdude again in java ha.
I have used avrdude often when using arduino...mainly to remote re-format/upgrade firmware on a 3D printer from a raspberrypi...
I'd go
1) Look into the Arduino Driver...
2) if fail...Maybe try down the avenue of running C programs in java? So you can package avrdude with your app?
3) XY Problem -> Why not look at other avenues...The only way to really pop malicious firmware on is with physical access to the device and with a user that has the prior source code (you can set lock bits to prevent reading EEPROM). Even a timestamped and UUID 1024bit AES token/JWT like thing could be used on startup to verify the build?
https://en.wikipedia.org/wiki/Java_Native_Interface
building for JNI won't help while avrdude wasn't built with FTDI support.
I've downloaded avrdude-6.3.tar.gz and ran ./configure --enable-linuxgpio:
Configuration summary:
----------------------
...
DON'T HAVE libftdi1
DON'T HAVE libftdi
...
when looking for it:
$ sudo yum whatprovides */libftdi1
it gives me:
libftdi-devel-1.1-4.el7.x86_64 : Header files and static libraries for libftdi
Filename : /usr/include/libftdi1
sudo yum install libftdi-devel also installs libconfuse. it also needs flex and bison.
there are even drivers for D2XX and FT4222H.
see AN_357 Android D2XX Demo Application for FT4222H.
and there are builds for ARM libft4222-linux-1.4.1.231.tgz
... which would go into the jniLibs/armeabi directory.
it depends which chipset it is, but the TN 134 FTDI Android D2XX Driver states:
To accompany the native D2XX library, FTDI have provided a Java class and a JNI wrapper which can be
easily included in an application. The class provides access to all of the classic D2XX functions including
EEPROM functions.
it even comes with a video install guide.
also found the Arduino build-script for avrdude.

How to use android device as host to pull logs from another android device?

Usually we use windows/linux machines to pull logcat info from an android device.
Is it possible to use android tablet/phone to act as host and get the logs from another android phone connected to it?
This is possible. The host Android device needs to be defined as a USB host. Then, once a connection is established, all of the same commands can be run, assuming that ADB is installed on the host device.
See here for more discussion on USB Host Mode support and compatible cables: https://android.stackexchange.com/questions/36887/how-can-i-determine-if-my-device-has-usb-host-mode-otg-support
It is useful to have a terminal program, such as Terminal IDE to issue commands from, and using busybox to provide additional commands would also be beneficial.
ADB comes as part of the AOSP source, so it a device is based off AOSP, then it will possibly include ADB. ADB source is located in /system/core/adb in the AOSP source tree.
Here is an example of a session where a Nitrogen6X (host) running a custom AOSP is used to connect to a Galaxy S3 cellphone (client). Note the use of Busybox and ADB, both of which are installed on the host. Busybox also happens to be installed on the client, but this is not necessary:
Thus in order to pull logs, just use regular ADB commands (e.g. to copy files), or run logcat, as indicated below:

How to flash zip on Genymotion programmatically

I'm trying to flash google apps into a genymotion emulator. I can do this using the drag and drop feature, but it's required for my project that the google apps .zip is already loaded into the genymotion machine and it's flashed internally. (To avoid having to interact with the desktop)
I've checked questions like this where the flashing is achieved but using the clockworkmod recovery (Which I couldn't install on genymotion). Since there's no docs on the genymotion recovery mode I can't find the commands to flash the zip, but I'm pretty sure they exist.
Which commands can be used for flashing a .zip from the terminal?
The commands can also be run in the machine that's holding the genymotion.
Genymotion VMs contain some special scripts that interface with their launcher and skin controls. The one that you need is /system/bin/flash-archive.sh which is what gets run when you drag & drop a .zip archive.
You can run flash-archive.sh directly using ADB or from a process within the VM. So if you have the zip in /sdcard/Download/gapps.zip, you can run the following command from the host machine:
adb shell flash-archive.sh /sdcard/Download/gapps.zip
adb reboot
Don't forget to install the ARM translation package first, if applicable. It can be done using the same sequence.
Paul's answer is good.
Also, we will soon release features related to continuous integration and automation. A command line tool will be included on these features. It allows you to do all what you can do with your mouse, but through the command line. It will be available to paid licenses.
You can find a demo we did at Droidcon Paris here.

How to write dead simple native SERVER program (not app) for Android devices?

First of all, I believe, it's not a duplicate question. I don't want to write a native app with NDK, I just want to use Android device as a cheap Linux server. Server, in this context, means a program, which has no UI. Also, the server is written in bare C++, but uses no libraries at all, so that's why should that question differ from others.
The story
We've written a small server for Linux, it has a webserver interface, so you connect to it with a vanilla browser, and you can play with it. We've compiled it to Linux/x86, Linux/ARM (GuruPlug), Linux/PPC (some kind of NAS), and Mac OS X Darwin platforms.
The Linux/ARM version also runs well on my Palm Pré smartphone. I've just copied the executable onto my phone (in webOS scene, there's no such thing like jailbreak, there's an official "developer mode", which if you activate by typing a secret key combination, you can log in to your phone with ssh or use local ssh app). So, our small server program (daemon, may say) is running on even the smallest Linux devices. (It's funny, when connect with your giga-powered desktop machine's browser to a smartphone server.)
The server program is designed to run on even the smallest Linux (as I said, it runs on NAS, router, smartphone), it requires only STDLIB and PTHREADS.
The question
What't the straight way to get that simple server program run on an Android device?
We have no Android device yet. But I assume, any ARM-based one will do. The server's memory footprint is so small, that it will be no problem.
I assume, the device must be rooted to copy executable to it and run.
I don't want to install a whole new Linux on the device. It should keep running Android, while our server works in the background. The Linux distribution which enough for Dalvik, is enough for our server, I'm pretty sure.
The server should use no external things (USB, touchscreen, audio, camera, mic - thanks, not) only a port for webserver (e.g. 8080), which is not allocated by Android.
I have ARM GCC toolchain, the installation of it is not the part of the question (except if there're tricks).
The installation of the program should be manual (scp, whatever). Also, I don't want to make a package or any automated deployment.
The problem of the launching of the server program is another question, we'd ignore now.
So, I want just do the same thing with an Android device (phone, tablet), just as I did on my webOS device: copy the server program and run on it. Computers are for that, running programs, aren't they? ;)
I have not really tried it, but it should be possible to use the ARM GCC compiler you have, or the one provided by the Android NDK to compile native application, the same as you would use it on your desktop.
If your application is small (not too many files), you can manage the compilation by hand (or in a simple Makefile). The following commands are from the LuaJIT compilation guide for Android:
/tmp$ cat test.c
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
/tmp$ NDK=/opt/ndk
/tmp$ NDKABI=8
/tmp$ NDKVER=$NDK/toolchains/arm-linux-androideabi-4.4.3
/tmp$ NDKP=$NDKVER/prebuilt/darwin-x86/bin/arm-linux-androideabi-
/tmp$ NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
/tmp$ ${NDKP}gcc $NDKF -o test test.c
/tmp$ file test
test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Obviously, use linux-x86 instead of darwin-x86 if you are on Linux (not Mac OS X, also not sure about Windows), and change the path to NDK to where you installed it.
Now you have to move the compiled binary somewhere to your Android device, and run adb shell to see if it works:
/tmp$ ADB=/opt/sdk/platform-tools/adb
/tmp$ $ADB push test /data/
332 KB/s (3343 bytes in 0.009s)
/tmp$ $ADB shell
# /data/test
Hello world!
You definitely need a rooted android device.
I recommend that you install busybox to get a bunch of nice command line tools such as wget, tar, gzip and many more.
You can get shell access to your device by using adb shell and then su to get root access. You can either use adb push or wget to put your server on the device.
The /system partition is mounted as read only and depending on the manufacturer there are a lot of protections that restrict you from writing there so you should copy your server somewhere on the /data partition (e.g. mkdir /data/myserver). After this, everything should be pretty straight forward.
Perhaps the question is more a sysadmin issue (how to install your executable on your Android device) than a developer issue (how to cross compile a C program for Android)?
I have a chroot partition with my android galaxy phone. It is arm with floating point processor, so all kinds of compilers work. Warning to you: the busybox/android distro utils don't run as GNU software. Their tar have problem unpacking the first thing I tried. POSIX tar not supported. Their zip have problem with some field 1 or 8. Their mount works like somthing from space. Their partition itself that you will want to have software put on does not support sym link. After a few weeks working on the chroot partition, I start to feel that the whole android is just a new underlying BIOS, with lots of firmware.

Categories

Resources