Monday, January 12, 2015

Android Booting Sequence Stages Step By Step

Android Booting Sequence Step By Step

In Booting of Android OS from power on

Stage 1 : Power on and boot ROM code execution
Stage 2 : The boot loader loading
Stage 3 : Starting of Linux kernel
Stage 4 : The init process
Stage 5 : Zygote and Dalvik
Stage 6 : The system server initiation

Stage 1 Power on and boot ROM code execution 

At power on the CPU will be in a state where no initializations have been done. Internal clocks are not set up and the only memory available is the internal RAM.
When power supplies are stable the execution will start with the Boot ROM code. The Boot ROM code will detect the boot media using a system register that maps to some physical balls on the ASIC. This is to determine where to find the first stage of the boot loader.
Once the boot media sequence is established the boot ROM will try to load the first stage boot loader to internal RAM. Once the boot loader is in place the boot ROM code will perform a jump and execution continues in the boot loader.

Stage 2 The boot loader loading

The first boot loader stage will detect and set up external RAM.
Once external RAM is available and the system is ready the to run something more significant the first stage will load the main boot loader and place it in external RAM.
The second stage of the boot loader is the first major program that will run. This may contain code to set up file systems, additional memory, network support, loading code for the modem CPU and setting up low level memory protections and security options.
Once the boot loader is done with any special tasks it will look for a Linux kernel to boot. It will load this from the boot media (or some other source depending on system configuration) and place it in the RAM.
Once the boot loader is done it will perform a jump to the Linux kernel, usually some decompression routine, and the kernel assumes system responsibility

Stage 3 Starting of Linux kernel

It will set up everything that is needed for the system to run. Initialize interrupt controllers, set up memory protections, caches and scheduling.
Once the memory management units and caches have been initialized the system will be able to use virtual memory and launch user space processes.
The kernel will look in the root file system for the init process (found under system/core/init in the Android open source tree) and launch it as the initial user space process.

Stage 4 The init process

The init process in Android will look for a file called init.rc. This is a script that describes the system services, file system and other parameters that need to be set up. The init.rc script is placed in system/core/rootdir in the Android open source project.
The init process will parse the init script and launch the system service processes.

Stage 5 Zygote and Dalvik

init runs the C++ program /system/bin/app_process, and gives the resulting process the name "zygote"
app_process executes, and executes a runtime environment for a dalvik class
app_process does a 'runtime.start("com.android.internal.os.ZygoteInit", startSystemServer)
com.android.internal.os.ZygoteInit:main() starts executing
The profiler is started the Zygote socket is registered (for later communication to start apps) classes and resources are preloaded if startSystemServer is set, then the system server is started
Zygote runs in "select loop mode", where a single process spins waiting for communication to start subsequent apps.
Eventually, a call is made to Zygote.forkAndSpecialize(), which does the actual forking

Stage 6 The system server initiation

The system server is the first java component to run in the system. It will start all the Android services such as telephony manager and bluetooth. Start up of each service is currently written directly into the run method of the system server. source can be found in the file frameworks/base/services/java/com/android/server/SystemServer.java in the open source project. Once the System Server is up and running and the system boot has completed there is a standard broadcast action called ACTION_BOOT_COMPLETED. To start your own service, register an alarm or otherwise make your application perform some action after boot you should register to receive this broadcast intent .



Monday, January 5, 2015

How to start express and websocket server in same port in NodeJS?

How to use the same port to start the web server (express) and web socket server in the same port in NodeJS ?


//app.js
var WebSocketServer = require('ws').Server
    , http = require('http')
    , express = require('express')
    , app = express();
app.use(express.static(__dirname + '/'));
var server = http.createServer(app);
var wss = new WebSocketServer({server:server});
wss.on('connection', function (ws) {
    ws.on('close', function () {
        console.log('close:');
    });
    ws.on('message', function (message) {
        console.log('message:', message);
    });
});
server.listen(3000);

Saturday, November 29, 2014

http://www.androidwebterminal.com/

My pet project Android Web Terminal was homeless for few months. I found a new home and everyone is welcome http://www.androidwebterminal.com/

Thursday, September 4, 2014

Rooting Samsung Galexy S5 - Stuck or failed in flashing cache

Today I wanted to root my S5. When i rooted using CF-Auto-Root-k3g-k3gxx-smg900h.zip Odin got stuck in the flashing cache. I tried doing the same thing few times and still get stuck. Then i thought of trying with latest Odin v3.09 and it worked.

You can download Odin v3.09 from here
http://www.android.gs/download-odin-3-09/

Friday, August 29, 2014

Is 24 Hour date format in Android using java code.

private boolean is24HourFormat(Context context) {
   Locale locale = Locale.getDefault();
   java.text.DateFormat natural =
                java.text.DateFormat.getTimeInstance(java.text.DateFormat.LONG, locale);
   String value = "";
   if (natural instanceof SimpleDateFormat) {
            SimpleDateFormat sdf = (SimpleDateFormat) natural;
            String pattern = sdf.toPattern();
            if (pattern.indexOf('H') >= 0) {
                value = "24";
            } else {
                value = "12";
            }
        } else {
            value = "12";
        }
 
        return value.equals("24");
    }

Thursday, August 14, 2014

dalvikvm: DexOpt: source file mod time mismatch

Today, When I was running my app, I started to see this error

dalvikvm: DexOpt: source file mod time mismatch
dalvikvm: Cached DEX 'aaaa.zip' (/data/dalvik-cache/aaaa.zip@classes.dex) is stale and not writable
After Googling this error I could not found anything useful. Then I took a look at the /data/dalvik-cache/. There was a aaaa.zip@classes.dex already in this folder even the application was not installed. So I removed the existing file and did a new installation. all worked fine. 

I have not seen this error in previous versions of Android (below 4.4.2) . If you are going to launch a new pre-compiled zip make sure to remove previously cached files in /data/dalvik-cache/


Thursday, August 7, 2014

NullPointerException when new PhoneStateListener

When I new PhoneStateListener() I am getting this error

java.lang.NullPointerException
  at android.os.Handler.<init>(Handler.java:229)
  at android.os.Handler.<init>(Handler.java:137)
  at android.telephony.PhoneStateListener$2.<init>(PhoneStateListener.java:420)
  at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:420)

I looked at the Handler.java:. I believe the reason for this error is mQueue = looper.mQueue; so looper is null.  Only way to fix this problem is to call Looper.prepareMainLooper(); at the beginning of the process