2017年8月13日星期日

tmux shortcut key

General Operation:
ctrl-b ? List combination key, press q to quit
ctrl-b x Exit curent panel / window

Panel Operation:
ctrl-b % Split panel (horziontal)
ctrl-b “ Split panel (vertical)
ctrl-b (arrow key) Switch panel
ctrl-b ctrl-(arrow key) Adjust panel size per 1 unit
ctrl-b alt-(arrow key) Adjust panel size per 5 unit
ctrl-b z Maximize current panel (restore to original panel size if issue this key again)
ctrl-b (page up | page down) Scroll up / scroll down
ctrl-b alt-o Rotate panel clockwise
ctrl-b ctrl-o Rotate panel anti-clockwise

Window Operation:
ctrl-b c Create window
ctrl-b , Rename window
ctrl-b . Rename window ID
ctrl-b (num key) Switch window according to window ID
ctrl-b p Switch to previous window
ctrl-b n Switch to next window

apt-get install is locked

https://askubuntu.com/questions/15433/unable-to-lock-the-administration-directory-var-lib-dpkg-is-another-process 
 
sudo apt-get install ???
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
 
Issue below command to solve the problem: 
$sudo rm /var/lib/apt/lists/lock

2017年8月8日星期二

Build Android from Source

1) Establish build environment
https://source.android.com/source/initializing

Ubuntu-16.04 64bit in VirtualBox
Increase swap size if system memory is small (e.g.: 3GB system memory with 18GB swap)
Recommend 180GB or above disk space

$sudo apt-get update
$sudo apt-get install openjdk-8-jdk

$sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip

[ Optional ] Install openjdk-7 for old ver AOSP (e.g. 5.x - lollipop)
$sudo add-apt-repository ppa:openjdk-r/ppa
$sudo apt-get update
$sudo apt-get install openjdk-7-jdk
$sudo update-java-alternatives -l
##set jname
$sudo update-java-alternatives -s java-1.7.0-openjdk-amd64

2) Download the source
https://source.android.com/source/downloading

$mkdir ~/bin
$PATH=~/bin:$PATH

$curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$chmod a+x ~/bin/repo 

$mkdir -p android
$cd android
$git config --global user.name "Your Name"
$git config --global user.email "you@example.com"
$repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.2_r27
$repo sync

[ Remark] Please ignore the following error message:
*curl: (22) The requested URL returned error: 404* *Server does not provide clone.bundle; ignoring.*

3) Preparing to Build
https://source.android.com/source/building

$. build/envsetup.sh

[ Remark ] Increase heap size to avoid the following error message.
Cannot connect to jack-server / heap size is small.
$export PATH=$PATH:~/.jack-server
$export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
$./prebuilts/sdk/tools/jack-admin kill-server
$./prebuilts/sdk/tools/jack-admin start-server


[ Remark ] Apply the patch first if using lolllipop version:
https://stackoverflow.com/questions/36048358/building-android-from-sources-unsupported-reloc-43/36073635

[Remark] Remember to clean the project after the makefile (*.mk) or configuration is changed
$make clean

$lunch
$make -j4


[ Remark ] Since pre-built libwebviewchromium.so module does NOT exist in the lollipop or previous version, it takes long time to build this module.

After success, the img can be found at:
<aosp>/out/target/product/<target>/

2017年6月11日星期日

Display the raw FAT32 file structure

Display the raw FAT32 file structure
Only show valid content (Skip zero-filled sector)

const TCHAR devicePath[] = TEXT("\\\\.\\K:");  //Specific the devicePath.
const DWORD addrBegin = 0;                              //Specific the addrBegin. (512 byte aligned)
const DWORD addrEnd = 128*1024*1024;        //Specific the addrEnd (512 byte aligned)

Tested on removable disk only.
Might not work or require administrator right if it is a non-removable disk.

Press 'x' to break the scan progress.



2017年3月10日星期五

Frequency Used GDB commands


set args set arguments
show args show arguments
Run
(or gdb –args <program> <args...>)
run with argument(if previous was set by "set args")


load load the program
attach process-id
(or gdb –pid <pid>)
debug an already running process
detach release the previous attached process


symbol-file filename Read symbol table information from file filename
i threads Display thread
i share Print the names of the shared libraries which are currently loaded


c continue
b add breakpoint
w add watchpoint
s step
n next
si step instruction


info all-registers show all registers
i r same as info registers
i r rax display register rax value (HEX)


p/x $rax display register rax value (HEX)
p/d $rax display register rax value (signed DEC)
p/u $rax display register rax value (unsigned DEC)
p/t $rax display register rax value (BIN)
p/f $rax display register rax value (Floating point number)
p/c $rax display register rax value (CHAR)


p/a $rip display register rip value (ADDRESS)
x /i $pc print next instruction to be executed


p *array@len display array content
p/x (short[2])0x12345678 display array content
x /256xb mem examine memory, 256=repeat count, x=hex, {b=byte, h=2bytes,w=4bytes,g=8bytes}
set *((int *) mem) = value set memory content (char, short, int, long, long long)
set *((char *) reg) = *((char *) reg) | 0x80 set peripheral register bit
set *((char *) reg) = *((char *) reg) & (~0x80) clear peripheral register bit


l filename:function list the source in the filename:function
l function list the source in function
l + list more
l - list less


bt backtrace
f n select frame number n


set $f00=*object_ptr set convenience variable
show convenience show convenience variable
source gdbscriptFile load GDB script file
define print_vars
p var1
p var2
end
create GDB function


disas view disassemble

BASH reference

#!/bin/bash    #tells *nix BASH should be used to run it

Special Parameters
$#    #Store the number of arguments passed from the command line
$1    #first parameter
$?    #Store the exit status of the last executed command
$_    #Print the last argument of the previous command
$$    #Return the process ID of the shell
$!     #Return the process ID of the last executed background process

${#var}    #Number of characters in $var
${#array} #The length of the first element in the array.

${para}  #same as $para. May be used for concatenating variables with strings.
${para-default},${para:-default}   #if para not set, use default
${para=default},${para:=default}    #if para not set, set it to default
${parameter+alt_value}, ${parameter:+alt_value}    #If parameter set, use alt_value, else use null string.
${parameter?err_msg}, ${parameter:?err_msg}If parameter set, use it, else print err_msg and abort the script with an exit status of 1.

The : makes a difference only when parameter has been declared and is null


$*     #* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@    #@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

For more details, please check:
http://tldp.org/LDP/abs/html/special-chars.html

[ is a synonym for test command. Even if it is built in to the shell it creates a new process.

[[ is a new improved version of it, which is a keyword, not a program.

Conditional Expression
[expr1 -ne expr2]    #Return true if expr1 is not equal to expr2
[expr1 -eq expr2]    #Return true if expr1 is equal to expr2
[expr1 -gt expr2]    #Return true if expr1 is greater than expr2
[expr1 -ge expr2]    #Return true if expr1 is greater than or equal to expr2
[expr1 -lt expr2]     #Return true if expr1 is less than expr 2
[expr1 -le expr2]    #Return true if expr1 is less than or equal to expr2
[-z  expr]    #Return true if the expression is null or empty
[expr =~ regular_expr]    #Return true if the regular expression is matched.
[expr1 -a expr2],[expr1]&&[expr2]    #Return true if both the expression is and
[expr1 -o expr2],[expr1]||[expr2]   #Return true if either of the expr1 or expr2  is true

[-a filepath],[-e filePath]    #Return true if file exists
[-f  filepath]   #Return true if it is file
[-d directory]    #Return true if it is directory
[-L filepath],[-h filePath]    #Return true if file is a symbolic link
[-S socket]    #Return true if file exists and socket file
[-b filepath]  #Return true if file is a block device
[-c filepath]  #Return true if file is a char device

[-r filepath]   #Return true if file is readable
[-w filepath]    #Return true if file is writable
[-x filepath]    #Return true if file is executable
[-u filepath]    #Return true if SUID is set
[-g filepath]    #Return true if SGID is set
[-k filepath]    #Return true if sticky bit is set
[-s filepath]    #Return true if file exists and has a size greater than 0

Number Notation
echo $((0xFFFF))          #Hex, Display 65535
echo $((032))                 #Octal, Output 26
echo $((2#11111111))  #Binary, Output 255

Frequency Used Command
source filepath    #executes the contents of a script in the current shell

2017年3月7日星期二

Dump the symbol table

objdump -TC ld-linux-x86-64.so.2

What's inside initrd (x86_64)

1 File Only - GenuineIntel.bin
Suspect extraction failed.
Get the answer from stackexchange with few modification.

http://unix.stackexchange.com/questions/163346/why-is-it-that-my-initrd-only-has-one-directory-namely-kernel

$file initrd.img
initrd.img: ASCII cpio archive (SVR4 with no CRC)
$mkdir initTree && cd initTree
$cpio -idv < ../initrd.img
`-- kernel
    `-- x86
        `-- microcode
            `-- GenuineIntel.bin
The cpio block skip method given doesn't work reliably. That's because the initrd images didn't have both archives concatenated on a 512 byte boundary.

Instead, do this:

apt-get install binwalk
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             ASCII cpio archive (SVR4 with no CRC), file name: "kernel", file name length: "0x00000007", file size: "0x00000000"
120           0x78            ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86", file name length: "0x0000000B", file size: "0x00000000"
244           0xF4            ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode", file name length: "0x00000015", file size: "0x00000000"
376           0x178           ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode/GenuineIntel.bin", file name length: "0x0000002A", file size: "0x00005400"
22032         0x5610          ASCII cpio archive (SVR4 with no CRC), file name: "TRAILER!!!", file name length: "0x0000000B", file size: "0x00000000"
22528         0x5800          gzip compressed data, from Unix, last modified: 2017-03-06 14:00:21
10181078      0x9B59D6        MySQL ISAM index file Version 3
 
dd if=initrd.img bs=22528 skip=1 | gunzip | cpio -id
1778+1 records in
1778+1 records out
40073444 bytes (40 MB, 38 MiB) copied, 3.15216 s, 12.7 MB/s
209980 blocks
 ls -l
total 39208
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 bin
drwxr-xr-x  3 yip yip     4096 Mar  7 21:36 conf
drwxr-xr-x 10 yip yip     4096 Mar  7 21:36 etc
-rwxr-xr-x  1 yip yip     6907 Mar  7 21:36 init
-rw-r--r--  1 yip yip 40095972 Mar  7 21:30 initrd.img
drwxr-xr-x  9 yip yip     4096 Mar  7 21:36 lib
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 lib64
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 run
drwxr-xr-x  2 yip yip     4096 Mar  7 21:36 sbin
drwxr-xr-x  7 yip yip     4096 Mar  7 21:36 scripts
drwxr-xr-x  4 yip yip     4096 Mar  7 21:36 usr
drwxr-xr-x  4 yip yip     4096 Mar  7 21:36 var