2017年3月1日 星期三

[C++] Virtual function

If B inherits from A, and redefines a method defined in A, then new instances of B will call
B's version. However, if the method is not virtual, then there is no polymorphic behavior, so if 
an instance of B is referenced as an A, then the method will be A's. For example:
struct A {
    void foo () { std::cout << "A::foo" << std::endl; }
};

struct B : public A {
    void foo () { std::cout << "B::foo" << std::endl; }
};

B b;
b.foo();
A *a = &b;
a->foo();
The output of the code above would be:
B::foo
A::foo
However, if the foo method had been virtual, then B::foo would have been printed twice.

http://stackoverflow.com/questions/11269501/can-i-use-a-method-overriding-a-non-virtual-method

Android: Unpack/Pack Images

boot.img and recovery.img

Unpack

These files are packed to a Android boot image format, if you use file command to see their type, the command will tell you that it is a data file.
file boot.img
# boot.img: data
if you use inspect into the file’s content, you will see “ANDROID” in the very beginning.
head -n 1 recovery.img
# ANDROID!H�=�a��Y(6�����G�GB��XH�=KERNEL
How to unpack these files? If you are using Ubuntu(or other Linux distributions, I believe), there is a handy tool: abootimg, it can create or extract such kind of images for you. Simply type the following command to install:
sudo apt-get install abootimg
Now you have abootimg installed in your system, you can easily extract boot.img:
mkdir boot
cd boot
abootimg -x ../boot.img
# after the successful execution of the last command, we will have initrd.img, zImage, etc.
# inside the boot folder.
Sometimes we need to change the init ramfs(packed in initrd.img), then we need to unpack the initrd.img. First of all, we need to make sure the initrd.img(some vendor might add a wrapper header to the original image):
file initrd.img
# the output should be similar to the following line:
# initrd.img: gzip compressed data, from Unix
Now we know the initrd.img is a gzip file, we can unpack it use gunzip(gzip -d) combined with cpio:
mkdir ramdisk
cd ramdisk
# "gunzip -c ../initrd.img" means unpack to standard output
gunzip -c ../initrd.img | cpio -i
That’ all, you can dig into the ramdisk folder now^_^.
Oh, forgot to tell you, the “abootimg-unpack-initrd” along with abootimg package do the same thing for you and can save you from typing a lot of command^)^.
abootimg-unpack-initrd initrd.img
# this command will create a ramdisk folder automatically and
# unpack the image to the ramdisk folder

Pack

  • initrd.img
    To reverse the unpack action, abootimg package provides another command named “abootimg-pack-initrd”, here is its usage:
    abootimg-pack-initrd [-f] [initrdimg_path] [ramdisk_folder_path]
    # -f: force write. if you would like to use this flag, make sure to place it as the first argument
    # initrdig_path: the path of the target initrd.img file
    # ramdisk_folder_path: the ramdisk folder path
    
  • boot.img/recovery.img
    To generate a boot.img or recovery.img, again, we need the abootimg tool.
    # to create an image:
    abootimg --create <bootimg> [-c "param=value"] [-f <bootimg.cfg>] -k <kernel> -r <ramdisk> [-s <secondstage>]
    
    # to update an existing image:
    abootimg -u <bootimg> [-c "param=value"] [-f <bootimg.cfg>] [-k <kernel>] [-r <ramdisk>] [-s <secondstage>]
    
    # Note: the option "-r <ramdisk>", where ramdisk is initrd.img, not the ramdisk folder.
    

system.img, userdata.img, cache.img, etc.

Unpack

Distinguish from boot image, images such as system.img is called user image in Android. Early version of Android(previous to Android 2.3 Gingerbread) use yaffs2 as its filesystem, and use ext4 as it filesystem from Gingerbread(read this post to get some detail about the switch: Ext4 filesystem hits Android, no need to fear data loss).
To decide what format is your image, we can make use of the “file” command:
file system.img
# if the iamge is a yaffs2 image, the output might look like:
# system.img: VMS Alpha Exectutable

# if the image is a ext4 image, the output might be:
# system.img: data
If your image is a yaffs2 image, you can use the yaffs2utils tools to unpack or pack the image. I won’t dig into the detail of yaffs2 image because I have very limited experience on it.
Now you have a system.img that its type is “data”, how can you know whether it is a ext4 image? Android provide a tool named simg2img(which means: sparse image to image) to do the job. We can get the source code of simg2img in the AOSP(Android OpenSource Project), inside the system/extras/ext4_utils/ folder. Read this for an instruction to compile it without compiling the whole AOSP: How to pack and unpack system.img and userdata.img from an Android factory image.
Once you have simg2img installed, you can simply type this command in the terminal:
simg2img system.img system.ext4.img
If the command didn’t yield any error, congratulation! Double check with the “file” command:
file system.ext4.img
# should output something like:
# system.ext4.img: Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-0000-675f-946fc0f9f25b (extents) (large files)
Mount the image using this:
sudo mount -t ext4 -o loop system.ext4.img /mnt
# make sure you have the privilege(sudo or root) to mount the image
Do whatever you want to the files under /mnt(the mount point), and all your modification will be saved when you umount the image.

Pack

When we done with the modifications, we may want to repack the system image(note I won’t touch the topic that how to create a yaffs2 image here). In that case, we need another tools: make_ext4fs and mkuserimg.sh, the source code of these tools also in system/extra/ext4_utils/, you can follow the instructions in How to pack and unpack system.img and userdata.img from an Android factory image to compile the make_ext4fs. The mkuserimg.sh is a wrapper program that it will call make_ext4fs eventually.
mkuserimg.sh
# Usage:
# mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE

# Example:
sudo mkuserimg.sh -s system/ system.img ext4 /system 500m
# ths "-s" flag indicates that we want to generate a sparse image, otherwise it will gengerate
# am image that we can mount it directly without using simg2img.

# the first "system/" is the system folder(for example, the mount point of the system.ext4.img)
# the second "/system" specify the mount point inside Andriod system.
# the last argument(500m) specify the image size. when mounted in the target Android system, the
# actual partition size would be 10m(approximately) less than this size. the image size must
# conform to the partition size defined in MBR or EBR, or some image data might be overrided by
# subsequent image.



refer : http://rex-shen.net/android-unpackpack-factory-images/

[Visual Studio Code]

========================================================================
PlantUML
========================================================================
At "Extension Marketplace" to search and install the "PlantUML" https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml#overview

Reference the link to use PDF:
http://plantuml.com/pdf

If you want to use PDF, you have to download the following files:

  • avalon-framework-4.2.0.jar
  • batik-all-1.7.jar
  • commons-io-1.3.1.jar
  • commons-logging-1.0.4.jar
  • fop.jar
  • xml-apis-ext-1.3.04.jar
  • xmlgraphics-commons-1.4.jar

Those files have to be in the very same folder as plantuml.jar (this is important)

Now, you can use the -pdf flag in the command line:
java -jar plantuml.jar -pdf diagram.txt
http://s.plantuml.com/PlantUML_Language_Reference_Guide_ZH.pdf
========================================================================

[NXP] imx_usb_loader setting

Download :
https://github.com/boundarydevices/imx_usb_loader

$make

$./imx_usb -d

show: 
checking with conf_path /usr/etc/imx-loader.d/
...
...


Create the folder
$sudo mkdir -p /usr/etc/imx-loader.d/

Copy the conf file to /usr/etc/imx-loader.d/

Initramfs unpack and repack

unpack:

# dd if=uramdisk.img of=ramdisk.img.gz skip=64 bs=1
# gunzip ramdisk.img.gz
# mkdir ramdisk; cd ramdisk
# cpio -i < ../ramdisk.img



repack:

# find . | cpio --create --format='newc' | gzip > ../ramdisk.img
# mkimage -A arm -O linux -T ramdisk -C none -a LOADADDRESS -n "Label you want" -d ./ramdisk.img ./uramdisk.img


如果出現:
mount: only root can use "--types" option

repack步驟改為:

$ sudo chown -R root:root ramdisk

$ sudo find . | sudo cpio --create --format='newc' | sudo gzip > ../ramdisk.img
$ sudo mkimage -A arm -O linux -T ramdisk -C gzip -n "Label you want" -d ./ramdisk.img ./uramdisk.img


====================================================================
./mkimage -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
-A ==> set architecture to 'arch'
-O ==> set operating system to 'os'
-T ==> set image type to 'type'
-C ==> set compression type 'comp'
-a ==> set load address to 'addr' (hex)
-e ==> set entry point to 'ep' (hex)
-n ==> set image name to 'name'
-d ==> use image data from 'datafile'
-x ==> set XIP (execute in place)
參數說明:

-A 指定CPU的體系結構:

參數表示的CPU體系結構
alpha Alpha
arm A RM
x86 Intel x86
ia64 IA64
mips MIPS
mips64 MIPS 64 Bit
ppc PowerPC
s390 IBM S390
sh SuperH
sparc SPARC
sparc64 SPARC 64 Bit
m68k MC68000

-O 指定操作系統類型,可以取以下值:
openbsd、netbsd、freebsd、4_4bsd、linux、svr4、esix、solaris、irix、sco、dell、ncr、lynxos、vxworks、psos、qnx、u-boot、rtems、artos

-T 指定映像類型,可以取以下值:
standalone、kernel、ramdisk、multi、firmware、script、filesystem

-C 指定映像壓縮方式,可以取以下值:
none 不壓縮
gzip 用gzip的壓縮方式
bzip2 用bzip2的壓縮方式

-a 指定映像在記憶體中的載入地址,映像下載到記憶體時,要按照用mkimage製作映像時,這個參數所指定的地址值來下載

-e 指定映像運行的入口點地址

-n 指定映像檔名

-d 指定製作映像的源文件

其中指定參數 "-a 0x30008000 -e 0x30008000" 指定值不會隨軟體版本有所更改,這個值是根據CPU的記憶體配置而指定的。


====================================================================
cpio指令

指令選項:

i:使用copy-in模式,還原歸檔文件或列歸檔文件中的文件列表
o:使用copy-out模式,建立歸檔文件
p:使用copy-pass模式,將文件直接複制到目的目錄
c:使用老式的ascii歸檔格式。如果需要跟平台使用,就需要使用
d:倉建需要的目錄,如果需要文件不處於同一個目錄中,應該使用此選項
v:顯示處理過程
t:顯示歸檔文件中的文件列表
m:保持文件的時間標記
H:使用指定的格式歸檔文件

sample:

find /bin -print | cpio -o > bin.bak
find -print | cpio -o > ../backup.cpio
find -print | cpio -ov > ../backup.cpio
find -print | cpio -ov | gzip > ../backup.cpio.gz
find -print | cpio -ov | bzip2 > ../backup.cpio.bz2
cpio -t < backup.cpio
cpio -tv < backup.cpio
cpio -tv "*.c" < backup.cpio

cpio -i < ../backup.cpio
cpio -iv < ../backup.cpio
以上2個,使用選項i將文件恢複到當前的目錄

cpio -idv < ../backup.cpio
文件恢複時保持目錄結構

cpio -imdv < ../backup.cpio
cpio -idv "*.c" < ../backup.cpio

refer :
https://community.nxp.com/thread/300430

http://canred.blogspot.tw/2013/04/cpio.html

http://flykof.pixnet.net/blog/post/22988153-zimage%E8%BD%89uimage%E4%BD%BF%E7%94%A8mkimage