Rooting the Samsung Alpha 5 (sm500fu) using Linux

TWRP

The time finally came when I needed to fiddle with my phone. You may wonder what on earth can be done with phone? Most are now sealed mono-units and even with a terminal emulator App the commands for Linux (which is underneath Android) don’t work….or do they?

As we know from Linux we need to invoke the magic word ‘sudo’ or ‘su’ to enable commands to run or to bring us the information we’re requesting, the same is true for Android but how do we do this? Typing SU into a suitable App won’t get us anywhere because an out-of-the-box phone is not ‘rooted’. To us in the Linux world we understand this meaning, to be root, in the Microsoft realm it’s called ‘administrator’. This in it’s self is the fundamental difference between Linux and Windows. On a Linux OS we’re always outside root, or as a ‘guest’ for Windows, however most windows PC’s run as admin all the time and hence the ease with which they can be hacked or infected.

So to get this mythical root access we’ll need to change some of the code of Android, we’re going to exploit a security bug in Lollipop to give us this root access. This is normally easily done by flashing (writing) a custom recovery which contains a suitable patch to grant root. Recovery is a bootable menu which allows you to load new software, to do a factory wipe or repair your OS – hence recovery. I’ll be using a modified TWRP from TheWhisp on XDA, others are ClockworkMod and Philz-recovery. Now I mentioned ‘this is normally easily done’ but some manufactures such as Sony, Asus and HTC ‘bootlock’ their phones and that’s a whole different hurdle to overcome in another post, in the case of Samsung they have a clever trick to make this hard, the stock Samsung ROM sees that the system folders have been modified and overwrites them to stock at boot rendering your hard work useless! Buggers!

Well that’s fine and dandy but I want root access, I want to remove all that bloatware that Samsung thinks I’ll need, I want to fiddle with my brand new phone and make it mine! We’re going to be rooting a Samsung Alpha 5 (SM500FU) using Linux (so no Odin) but the overall process remains similar for any Android device, you will need to read up on specific tricks or software needed for your device, the XDA forums are a great source. So let’s start:

The first thing is to read up fully on your device, there may be issues like bootloops that can be easily avoided, patches to solve issues and of course the latest revisions. For the Alpha5 we know about the OS overwriting the recovery.img, that as of 10th Dec 2015 our modified TWRP won’t give us root, WHAT?! but that’s the point of all this! Yes, but thanks to Chainfire we can flash superSu from our shiney new custom recovery to gain it….don’t worry.

First charge your phone to 100% just good practice. We’re going to need Heimdall (Odin for Linux) and I run it from the command line as it’s quicker and simpler, I compiled mine from GIT (source) to get V1.4.1:

 sudo apt-get install git

apt-get install build-essential cmake zlib1g-dev qt5-default libusb-1.0-0-dev libgl1-mesa-glx libgl1-mesa-dev

git clone git://github.com/Benjamin-Dobell/Heimdall.git

mkdir -p Heimdall/build

cd Heimdall/build

cmake -DCMAKE_BUILD_TYPE=Release ..

make

sudo cp bin/* /usr/local/bin

And finally to check it works: sudo heimdall-frontend which we can close now we know it’s all working.

Now we need the correct recovery image which will be a .tar file, I got mine HERE, put it somewhere you can find it again. If the file ends ‘.tar.md5’ rename it without the ‘.md5’ so that Archive manager can open it. Now extract the recovery.img and open a terminal in the folder which contains the recovery.img We now need the SuperSu.zip file from HERE, this file needs to be copied onto your external sdcard, don’t put it in a folder just straight onto the card on it’s own. Once that’s done power-off your phone and unplug the USB.

Thanks to the Alpha5 and Heimdall both being picky the following is the way I got my PC/Heimdall/Alpha5 to talk to each other. This seems to work regardless of which USB I was plugged into:

With our terminal still open in the folder with our recovery.img in:

sudo heimdall detect this will return that no device can be found or detected and is a final check that Heimdall is available.

Connect your phone. Press and keep holding the Volume-down+Home+Power buttons all at the same time until the phone vibrates, then release all the buttons and you will enter a screen warning you that you’re up to no good, ignore this and press Volume-up to enter ‘download mode’, now in the terminal:

heimdall flash –RECOVERY recovery.img –no-reboot

And WAIT………………….there should be an output in the terminal saying things like ‘downloading PIT file’, if successful it will end by saying it’s reattached the kernel and returns you to command prompt (the :~$ sign). On the phone there will be a progress bar moving across the screen, wait until it’s finished moving and then I like to wait a little longer to make sure the write has been completed before doing:

Press and hold the Volume-up+Home+Power buttons without releasing until the phone buzzes and enters our spanking new custom recovery. At this point and after several hours there was much cheering and rejoicing……….Do a full backup. Really, do a full backup!

Now at last for the point of the exercise, Root access. Still in recovery go to ‘install from zip’ and open up your external sdcard and select the SuperSu.zip file, hit install or with TWRP ‘swipe to confirm’ and hey presto root! Now reboot system to start deleting all those annoying apps you never wanted, well actually the first thing you’ll need is a superuser app to grant and manage root access, I use this one.

Now the caveats – If you’re going attempt to root your phone I’d like to assume you know you’re way around basic computing. Read up. It worked with my phone (SM500FU running stock 5.0 Lollipop) and desktop (debian based latest), yours will be different. Be prepared that when it all goes wrong (it will at some point) you will be in a world of hurt, but don’t panic, stop and google without touching your phone. During this process I ended up for a while with no recovery at all! Couldn’t find anything or anyone else who’d had the same, but I calmly retraced my steps, compiled Heimdall from source and through trail and error got Heimdall to ‘talk’ to my phone again resulting in the process above and a happy rooted phone.

At some point I’ll flash a custom ROM and will write and tell y’all about it, happy flashing!

Advertisements

Finger prints on my gadgets

handprint-crop

It’s something almost all of us have, a touch screen device. Almost all mobile phones now have a touch interface of some description that we daily poke at with our grubby fingers. Even if your fingers are particularly clean right now they are still producing sweat and oils which get all over the screens of our devices each and every time we glide our finger over them.

Most middle to high end phones (Samsung S series, iphone etc) have a factory applied oleophobic coating, a mixture of chemical compounds which repel oil, they decrease the amount of fingerprints visible on the screen. This coating doesn’t stop them altogether, but it reduces the amount and makes wiping them off far easier.

All well and good then? Yes, for a bit. This oleophobic coating is just a coating applied to the screen after manufacture, it doesn’t last forever, Gorilla Glass or Dragontrail it wears off dependant on the use your screen sees. My trusty rooted Samsung S2 came with this coating, I can now see that the center of the screen (which has taken a beating from my digit) attracts more fingerprints than the outer edges of the screen as the oleophobic coating has been worn off.

I’m not obsessed about having a perfectly clean screen, but I also don’t like the smeared look! I also don’t like screen protectors, why have super-tough glass covered by a bit of plastic……………. There is a Japanese company which is now making an oleophobic coating which can be applied to any glass touch screen. Made by Fusso it can last between 3-6 months, but it’s not cheap, at about $25 per kit. Another online ‘tip’ is to coat your screen with Rain-X, however Rain-X is hydrophobic, or H2O repellent, it’ll keep the rain off at least!

As ever I want it NOW, I want it better and I want it cheaper. This issue is not directly linked to my good old S2, but for my new Xperia Z1 Compact which comes with a crappy piece of plastic over my Dragontrail screen. Yes i’m aware that this ‘crappy piece of plastic’ is actually an anti-shatter film (ASF), but really Sony? (I hear the Z2 is finally free of an ASF on top of the screen at last). If I drop my phone from high enough it’s going to get bust, most likely the screen, with or without the ASF it’s a $200 repair. So now thats off and I have a nice clean Dragontrail screen which feels great, it’s getting covered in fingerprints, there’s no oleophobic coating…………

To find a solution without paying $25 we need to know what makes a good oleophobic coating. Well we need a/or a composition of compounds that are not soluble in Lipids, or fats. If the compound is not soluble in our chosen solvent (or liquid) it will naturally be phobic to it, or repel it. The cheapest is water (we all know oil and water don’t mix), fine for my Z1 to be drenched in water but not my S2! Fluorocarbons are the next (basically Fluorine and Carbon combined), great……….where do I get those then? Actually they are probably in your kitchen right now, it’s almost a household name, it might have helped you cook today, Teflon. Teflon is a Fluoropolymer, or a solid version of Fluorocarbon, that’s why your bacon doesn’t stick to the frying pan. As a long lasting polymer applying this involves some chemistry and high heat which is not very DIY, or phone friendly. An easier solution would be to buy Teflon as a wax or liquid thats easy to apply, well there are products used on skis and snowboards which contain Fluorocarbon, or liquid Teflon. These are $10-$15, but I think theres a cheaper way – PTFE tape. Polytetrafluoroethylene to be exact, this is used mostly in plumbing to wrap around threaded joints to stop leaks. So we have Fluorene bound as a polymer with ethylene (a hydrocarbon) to form a soft pliable tape, Teflon-tape! (Except ‘Teflon’ is owned by Dupont who don’t make tapes, hence why it’s called PTFE tape not Teflon-tape)

So the process:

Make sure your screen is really clean, having brought a roll of PTFE tape for about 50c, rip a length off and wrap it around your finger, now gently rub the tape over the screen always in the same direction, keep changing through 45 degrees. This will leave streaks and bits of PTFE tape on the screen which is what we want, it’s leaving an extremely thin layer of fluorocarbon behind. Now a light squirt of cleaner gently removing it then a buff-up using a microfibre cloth. Keep repeating to slowly build the layers up, an almost free oleophobic coating!

Fingerprints are now drastically reduced on my ‘naked’ Z1 Compact, those that do get on are easily cleaned by breathing on the screen and wiping it. Feel free to comment especially if you have a great finger smudge removing tip!

EDIT: In my ongoing search for cheap oleophobic coatings i’ve also discovered silicone based water proofing spray (such as Fabsil) to work rather well, it’s far quicker to apply than PTFE tape. Simply spray a small amount onto a cloth, wipe over the screen, leave for a bit then polish off using a microfibre cloth.

Image here

Stopping screen tearing

It’s a blight of Linux generally, the dreaded screen tearing, certainly for users who use Nvidia cards/drivers. On some Distro’s i’ve had tearing, others not. On some Nvidia cards i’ve had tearing, on others not. It almost seems like it’s the luck of the draw. I’ve trawled Google for answers, i’ve tried almost everything until, almost by chance, I stumbled on an answer, for me at least, hopefully it will help others or point them in the right direction to solving their tearing issues.

I must admit I like my desktop to look clean, simple but very ‘fancy’. Rather duplicital i’ll admit. I like flat icons and window borders but I like the fancy opacity that compositing brings. I’ll get it out of the way now, I like Gnome 3, I’m running Gnome 3.12 right now. My current system has a nice big fancy Nvidia card coupled to two HD monitors and it’s great, no tearing. The issue I have with tearing is my bog standard Ubuntu/Unity Core2 media machine. All it does is stream video and music so it’s just got a GT218 Nvidia card with HDMI-out, it’s quite capable of supplying full 1080p to my 1080p TV, but here’s the rub, I get tearing in films! That sucks!

So what is ‘screen tearing’ or ‘tearing’ anyway? Basically it’s two (or more) frames being shown at the same time on your screen, this results in imperfections in the playback, or lines horizontally across the entire screen, or the areas of the screen that are changing quickly. It’s caused by the refresh rate (number of times in a given period that the image is updated) of your video card not matching the refresh rate of your screen. To solve this problem there are settings available to us to synchronize the refresh rates, only they don’t always work! EG: Compiz and Nvidia have Vsync settings, it doesn’t matter which one of these is enabled, or not, we can still get tearing.

Lots of threads talk about adding things like “userevents” “true” to our Xorg config (etc/X11/xorg.config), switching from Compiz to Mutter and so on, having tried all of these plus some, none of them work…..

For my system the following did work and it’s rather simple, which I like:

sudo apt-get install mplayer2 && smplayer    — this install Mplayer 2 and smplayer

sudo apt-get install vdpau-va-driver && libvdpau1    — This installs vdpau drivers designed by Nvidia, these help to offset some of the decoding and post-processing (making the image you see nicer than the raw decoded image) to the GPU (Graphical-Processing-Unit) instead of the CPU. Basically we’re telling our hardware that all things to do with graphics is to be done by the GPU, the CPU can get on with keeping the rest of the system working!

sudo gedit /home/user/.mplayer/config    — (user is of course whatever your home folder is called, EG: Fred)

Into that file add:

[default]

ao=pulse

vo=vdpau

Save the file and exit (ao=pulse says to use pulseaudio, vo=vdpau says to use the vdpau-drivers during playback in Mplayer.)

Along with these I also have the following settings:

Nvidia-settings: Vsync=enabled, Allow-flipping=enabled, Compiz-compositor=enabled, Compiz-openGL=disabled.

Now I can play full 1080 films without any tearing at all. Hopefully this will help someone, or point them down the right road to tear-free film enjoyment, feel free to comment.

Creating a SWAP partition

We will start with a quick overview of what exactly SWAP is, and when it’s needed, then we’ll look at creating a SWAP partition on our system.

SWAP is useful when we have limited RAM, under 4gb for current Gnome etc desktop environments and applications. It’s basically an extension of ram on a hard-drive (SSD or HDD (Solid-State Drive or Hard-Disk Drive)).

As we open applications they write information to the RAM, the more we open or the larger the application the more the RAM fills to a point it’s exhausted. If we had no SWAP at this point the OOM-Killer (Out-Of-Memory Killer) would kick in to try and free up space, however this usually leads to a hang or reboot as OOM-Killer can sometimes select a critical system process! By having SWAP we allow the Linux kernel to move inactive pages (sets of information in bytes) about applications we maybe opened three hours ago to the SWAP partition on our hard-drive.

“Why doesn’t RAM just get cleared out by new information?” Calling information from scratch about an application on your disk is slow (for those with spinning HDD’s), calling it from RAM is many times faster so the kernel attempts to keep this information in RAM for as long as possible, either until it’s needed again or until new information needs the space it occupies. The next best thing to RAM is to keep all the relevant information about the old application together (paged) and write it to our SWAP partition, here it can sit until we shutdown incase we re-open that application from three hours ago.

So SWAP is a way of keeping your system working with small amounts of RAM, there are lots of tweaks to tune how the kernel deals with this old information, over time i’ll try and cover most of the ones I have found to actually do something!

Lets say that we installed our Linux OS and we decided that our 4gb of RAM would be plenty, now however we’re using lots of applications simultaneously or a couple of large applications, we keep getting hangs, ‘failed to fork’ notices or reboots and we can’t just put more RAM in, we need a SWAP partition! (SWAP needs to be a continuous block of disk space for best results, hence why we are using a partition. An alternative would be to set up a SWAP file). Below is how we go about creating a SWAP partition:

First we need to know what our HDD’s are called and what the existing partition numbers are. I have used Gnome Disk Utility for a long time, it’s an easy to use nicely laid out application with useful tools and information, like SMART data (Self-monitoring, analysis and Reporting Technology), and the disk and partition names & numbers. We can also erase, format and adjust partitions, it’s a lot faster than using terminal to create a new partition, and if you’re unsure of your disk names/numbers it might be better to see it visually! As a useful quick way of checking disks and partitions we can open terminal and enter:

Sudo parted -l

This shows us the HDD names, EG: disk /dev/sda:128GB (sda= solid-state drive A. for a spinning disk it would be hda), beneath that it lists existing partitions EG:

Number   Start          End          Size         Type         File system   Flags
1              2097kB     44.6GB    44.6GB     primary    ext4               boot
2              44.6GB     127GB     82.8GB     primary    ext4

In the above we have two partitions: /dev/sda2 and /dev/sda1, we can see from the ‘flags’ that sda1 is a bootable partition, where the OS resides.

From within Gnome Disk Utility we select the disk we want to use to create our SWAP partition on. It can be the disk with the OS & /home on or a separate disk. When selected it will highlight, now click on the cogs in the lower left just beneath the partition graphic.

We want ‘Format‘. Three options will now be displayed: Erase, Type and Name.

Erase: Does what it says, it will either overwrite, or overwrite with 0’s first. Overwriting with 0’s is the best as it clears everything, however depending on the partition size it can be slow.

Type: Is what we want the partition to end up as, for this we will need ‘compatible with Linux systems (ext4)’.

Name: We can call it a name but we’ll spot it as it will always have a ‘SWAP’ flag with it.

Then we hit ‘Format‘, this will now create the new partition. Once this has completed highlight the new partition, click on the cogs again but this time we want ‘Edit partition‘.

Select ‘Linux swap (0x82)‘, then hit ‘Change

We have now created a new SWAP partition, take a note of what it’s called eg: sda2. Now we need to tell our OS that it exists and needs to use it. For this job we do need terminal:

sudo blkid /dev/sda2    — This tells us the UUID (Universally Unique Identifier) of the new SWAP partition. (We’ll use 49883f4d-88d9-482f-8a1b-90cbdf123aa8 from now on, replace this with your own UUID!)

sudo swapon -U 49883f4d-88d9-482f-8a1b-90cbdf123aa8    — This mounts and turns on the new SWAP partition immediately.

sudo gedit /etc/fstab    — This will open up fstab (file systems table) which lists all our partitions and HDD’s we want mounted automatically at boot. The first two will be the root and home partitions, beneath those lines we need to add the following line to this file to enable SWAP at every boot:

UUID=49883f4d-88d9-482f-8a1b-90cbdf123aa8     none    swap    sw      0   0    — save the file then:

sudo update-initramfs -u    — to make sure everything updates prior to rebooting.

All done! If you hibernate (I always use suspend) your PC you’ll also need to do the following:

sudo gedit /etc/initramfs-tools/conf.d/resume    — To edit initramfs file and add the line:

RESUME=UUID=49883f4d-88d9-482f-8a1b-90cbdf123aa8    — Save and exit, then:

sudo update-initramfs -u    — To make sure everything updates prior to rebooting.

We now have a SWAP partition set up and running to stop any out-of-memory issues or hangs. There are other tweaks we can do to reduce the amount of writing the OS does to SWAP, i’ll be covering these at a later stage, feel free to comment.

Memory Overcommit Settings

Today I delved into the underworld of Linux memory allocation, in particular into overcommitting memory (RAM).

After a couple of X11 hangs I decided I needed to learn a little more about the various settings that come as stock with the Linux kernel, to try to tame them, or at least reduce or stop these annoying hangs followed by reboots!

Most applications ask for more memory than they might actually need to startup, some of this is down to bad software design, or they expect that you’ll need that much at some point in the future….a sort of “this is my worst case scenario requirement of RAM, and i’ll tell you that now before we start!”

The stock linux kernel settings kind of just agrees to the applications request without checking if the actual resource, or the hardware, could support the total requested memory in that worst case scenario, partly because most applications never need what they ask for. But, what happens when they do……..

To see your memory system now, under ‘default’ settings, enter the following into terminal:

sudo cat /proc/meminfo

We can see lots of lines but the four we’re interested in are:

MemTotal: The total amount of physical RAM available on your system.

MemFree: The total amount of physical RAM not being used for anything.

CommitLimit: The total amount of memory, both RAM and SWAP, available to commit to the running and requested applications (not necessarily directly related to the actual physical RAM amount, we will see why later).

Commited_AS: The total amount of memory required in the worse case scenario right now if all the applications actually used what they asked for at startup!

If the application/s needed what they originally asked for, an out-of-memory or ‘OOM’ would happen. This would mean that the OOM-killer would kick in to try and free up actual memory by killing running processes it thinks might help to free up memory. By then though a kernel-panic (or at best  X11 would hang) might have happened resulting in a frozen system (aka blue-screen in MS terms) or of course OOM-killer killed a critical system process.

To solve the random selections of the OOM-killer potentially killing off a critical system process, or not kicking in prior to a kernel-panic, we can change the following:

vm.overcommit_ratio=100: The percentage of total actual memory resources available to applications. This might be the total of RAM + SWAP, or just RAM if you have no SWAP. (IE: RAM=1gb & SWAP=1gb, overcommit_ratio=100 would mean 2gb could be allocated to applications. overcommit_ratio=50 would mean 1gb could be allocated to applications – this would obviously not be a sensible choice as 1gb would never be used!)

vm.overcommit_memory=2: This tells the kernel to never agree to allocate more than the total percentage of actual memory determined by overcommit_ratio= and disables the OOM-killer daemon.

We can change the above settings by entering the following into terminal:

sudo sync    — this tells any files in cache on RAM to write to disk now

sudo sh -c “sync; echo 3 > /proc/sys/vm/drop_caches”    — this drops all caches from RAM

sudo cat /proc/meminfo    — check that Committed_AS is below CommitLimit

sudo sysctl -w vm.overcommit_ratio=99    — use 99% of physical memory

sudo sysctl -w vm.overcommit_memory=2    — only allow applications to start if there is enough memory determined by the above command

So now when we try to open a memory hungry application, or we have to many applications open already, the new application is refused with a notification that IE: ‘file manager failed to fork’, or failed to start because there isn’t the available memory. Potentially the application could theoretically start with what memory is available now, but it may continue to require memory to a point the system is unusable as a result and hangs or crashes. A web-browser would be a good example, it opens with only one tab, but during the day you open a dozen more, at some point memory would be exhausted.

By using the two above tweaks we end up with a system that cannot agree to give applications more memory allocation than it physically has. This stops hangs or kernel panics that render the entire system useless, potentially losing those last bits of information you were inputting, instead it simply tells you that there is no more memory, you need to go buy more RAM!

We now know our system will just tell us there’s no more memory for that new application to open, and we like it, we want these settings to survive power cycles (rebooting), we do this by adding the above commands into:

sudo gedit /etc/sysctl.conf    — I use gedit, but nano, vi etc all work

Add: sudo sysctl -w vm.overcommit_ratio=99 and sudo sysctl -w vm.overcommit_memory=2 to the bottom of that file on separate lines and save. Mine look like this:

#system tweaks
vm.swappiness=5
vm.vfs_cache_pressure=50
vm.overcommit_ratio=99
vm.overcommit_memory=2

(I use 99% just to give a little allowance).

Of course you could increase the size of your SWAP partition as CommitLimit is a total of RAM+SWAP (remembering that SWAP is disk based so slower than RAM) so you can open all those tabs, or applications without getting ‘failed to fork’ messages, or you could add a SWAP partition if you haven’t got one already.

“But I have an SSD and SWAP is bad”, well yes it is if you are constantly using it because you only have 1gb of RAM! If you have 4+gb of ram, and depending on what you use your system for, SWAP on an SSD would act as a final safety net saving you from kernel panic under stock settings, or by using the above settings it would stop the constant ‘failed to fork’, but if that’s a regular message following these changes i’d suggest you buy more RAM!

NB: Default is: vm.overcommit_memory=0 which means in short that no tabs are kept on actual available memory space, the kernel agrees to all requests for memory from applications and OOM-killer is activated, in my experience followed by hangs and reboots…….

Feel free to contact me, the above is a condensed and simplified explanation for those still learning.

Introduction

This blog is going to consist of the system tweaks I have learnt and made to my various Linux OS’s. Hopefully this will help others as they Google, primarily it will act as future reference for me.

It may well have random ramblings or views over time to do with technology generally, but mostly it will revolve around things Linux related.