Android 底层基础知识
编译源码
进入 cupcake 工作拷贝的顶层目录,执行:
/make_image15.sh
部分执行结果:
out/target/product/littleton/root/ 内核需要使用的 initramfs
out/target/product/littleton/system 文件系统的系统分区
out/target/product/littleton/data/ 文件系统数据分区
编译内核
此处内核编译主要针对驱动组之外的同事
1> 设置工具链
内核的 linux-2.6.28-a1/Makefile 中设定了:CROSS_COMPILE ?= arm-linux-
所以设置PATH环境变量,保证能找到正确的工具链
假设工具链位于: /usr/local/marvell-arm-linux-4.1.1/ 设置为:export PATH:=/usr/local/marvell-arm-linux-4.1.1/bin/:$PATH
2> 更改编译选项(网络启动或者本机启动)
内核顶层目录执行:
make menuconfig
General setup --->
java代码:
复制代码
3> 编译
内核顶层目录执行 make zImage
编译好的内核:arch/arm/boot/zImage
搭建网络开发环境
1> 安装nfs服务器
sudo apt-get install nfs-kernel-server nfs-common
2> 修改nfs服务器配置文件/etc/exports ,确保有以下配置项
/nfsroot/rootfs *(rw,no_root_squash,sync)我们在内核中已经固定,手机通过网络方式启动,默认从 /nfsroot/rootfs 读取文件系统,修改配置项后需要重启nfs服务器:
sudo /etc/init.d/nfs-kernel-server restart
3> 配置网络根文件系统
拷贝 out/target/product/littleton/root/ 内容到 /nfsroot/rootfs 目录
拷贝 out/target/product/littleton/system 内容到 /nfsroot/rootfs/system
修改 /nfsroot/rootfs/init.rc 去掉几个mount命令
为了使大家的过程,结果统一,可以使用脚本 mkfs.cupcake 完成 在执行 mkfs.cupcake.nfs 脚本前先到 cupcake-jianping 目录下执行: . ./make_env15.sh设置环境变量,获取通过手动输入android源码的位置,让脚本来设置环境变量。
通过 tftp 下载内核到pc内存 0x80800000 地址处
java代码:
复制代码
擦除原来的内核分区,0x100000 为分区起始地址,0x300000为分区长度
java代码:
复制代码复制代码
擦除原来的flash系统分区
java代码:
复制代码
烧写数据到flash系统分区
java代码:
复制代码
下载数据分区镜像文件到pc内存 0x80800000 地址处
java代码:
复制代码
擦除原来的flash数据分区
java代码:
复制代码
烧写数据镜像到flash数据分区
java代码:
复制代码
四;
五:
复制代码
系统变量解析
LOCAL_MODULE - 编译的目标对象
LOCAL_SRC_FILES - 编译的源文件
LOCAL_C_INCLUDES - 需要包含的头文件目录
LOCAL_SHARED_LIBRARIES - 链接时需要的外部库
LOCAL_PRELINK_MODULE - 是否需要prelink处理
BUILD_SHARED_LIBRARY - 指明要编译成动态库
LOCAL_PATH - 编译时的目录
$(call 目录,目录….) 目录引入操作符,如该目录下有个文件夹名称 src,则可以这样写 $(call src),那么就会得到 src 目录的完整路径
include $(CLEAR_VARS) -清除之前的一些系统变量
CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
在 build/core/config.mk 定义 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
通过include 包含自定义的.mk文件(即是自定义编译规则)或是引用系统其他的.mk文件(系统定义的编译规则)。
LOCAL_SRC_FILES - 编译的源文件
可以是.c, .cpp, .java, .S(汇编文件)或是.aidl等格式
不同的文件用空格隔开。如果编译目录子目录,采用相对路径,如子目录/文件名。也可以通过$(call 目录),指明编译某目录
下所有.c/.cpp/.java/.S/ .aidl文件.追加文件 LOCAL_SRC_FILES += 文件
LOCAL_C_INCLUDES - 需要包含的头文件目录
可以是系统定义路径,也可以是相对路径. 如该编译目录下有个include目录,写法是include/*.h
LOCAL_SHARED_LIBRARIES - 链接时需要的外部共享库
LOCAL_STATIC_LIBRARIES - 链接时需要的外部外部静态
LOCAL_JAVA_LIBRARIES 加入jar包
LOCAL_MODULE - 编译的目标对象
module 是指系统的 native code,通常针对c,c++代码
java代码:
复制代码
Java 应用程序的名字用该变量定义
java代码:
复制代码
进入 cupcake 工作拷贝的顶层目录,执行:
/make_image15.sh
部分执行结果:
out/target/product/littleton/root/ 内核需要使用的 initramfs
out/target/product/littleton/system 文件系统的系统分区
out/target/product/littleton/data/ 文件系统数据分区
编译内核
此处内核编译主要针对驱动组之外的同事
1> 设置工具链
内核的 linux-2.6.28-a1/Makefile 中设定了:CROSS_COMPILE ?= arm-linux-
所以设置PATH环境变量,保证能找到正确的工具链
假设工具链位于: /usr/local/marvell-arm-linux-4.1.1/ 设置为:export PATH:=/usr/local/marvell-arm-linux-4.1.1/bin/:$PATH
2> 更改编译选项(网络启动或者本机启动)
内核顶层目录执行:
make menuconfig
General setup --->
java代码:
- Initial RAM filesystem and RAM disk (initramfs/initrd) support
- () Initramfs source file(s) (NEW)
- //如果需要支持网络启动反选 [] Initial RAM filesystem and RAM disk (initramfs/initrd) support
- //如果需要支持本地启动选中
- Initial RAM filesystem and RAM disk (initramfs/initrd) support
- //设置 () Initramfs source file(s) (NEW) 为 root
- //拷贝 cupcake 编译结果 out/target/product/littleton/root/ 到内核顶层目录
3> 编译
内核顶层目录执行 make zImage
编译好的内核:arch/arm/boot/zImage
搭建网络开发环境
1> 安装nfs服务器
sudo apt-get install nfs-kernel-server nfs-common
2> 修改nfs服务器配置文件/etc/exports ,确保有以下配置项
/nfsroot/rootfs *(rw,no_root_squash,sync)我们在内核中已经固定,手机通过网络方式启动,默认从 /nfsroot/rootfs 读取文件系统,修改配置项后需要重启nfs服务器:
sudo /etc/init.d/nfs-kernel-server restart
3> 配置网络根文件系统
拷贝 out/target/product/littleton/root/ 内容到 /nfsroot/rootfs 目录
拷贝 out/target/product/littleton/system 内容到 /nfsroot/rootfs/system
修改 /nfsroot/rootfs/init.rc 去掉几个mount命令
为了使大家的过程,结果统一,可以使用脚本 mkfs.cupcake 完成 在执行 mkfs.cupcake.nfs 脚本前先到 cupcake-jianping 目录下执行: . ./make_env15.sh设置环境变量,获取通过手动输入android源码的位置,让脚本来设置环境变量。
通过 tftp 下载内核到pc内存 0x80800000 地址处
java代码:
- blob> tftp zImage0917
- Begin init ether usbnet!!!
擦除原来的内核分区,0x100000 为分区起始地址,0x300000为分区长度
java代码:
- blob> nanderase -z 0x100000 0x400000
- the current NAND chip does not support Block Unlocking.
- Erase 0x300000 length data from flash: 0x100000
- Erase flash from 0x100000, length 0x300000
下载系统分区镜像文件到pc内存 0x80800000 地址处
java代码:
- blob> tftp system0918.img
- TFTPing system0918.img
- received 113138 blocks (57925824 bytes)
- tftp_cmd: file 'system0918.img' loaded via tftp to address 0x80800000.
擦除原来的flash系统分区
java代码:
- blob> nanderase -z 0x500000 0x4000000
- the current NAND chip does not support Block Unlocking.
- Erase 0x3e0f800 length data from flash: 0x400000
- Erase flash from 0x400000, length 0x3e0f800
烧写数据到flash系统分区
java代码:
- blob> nandwrite -y 0x80800000 0x500000 57925824
- the current NAND chip does not support Block Unlocking.
- Write 0x373e0c0 length data from RAM: 0x80800000 to flash: 0x400000
- Write flash from 0x400000, length 0x3591800
- Erase flash from 0x400000, length 0x3591800
下载数据分区镜像文件到pc内存 0x80800000 地址处
java代码:
- blob> tftp data0918.img
- TFTPing data0918.img
- received 33992 blocks (17402880 bytes)
- tftp_cmd: file 'data0918.img' loaded via tftp to address 0x80800000.
- blob>
擦除原来的flash数据分区
java代码:
- blob> nanderase -z 0x4500000 0xBB00000
- the current NAND chip does not support Block Unlocking.
- Erase 0xa81f000 length data from flash: 0x4400000
- Erase flash from 0x4400000, length 0xa81f000
烧写数据镜像到flash数据分区
java代码:
- blob> nandwrite -y 0x80800000 0x4500000 17402880
- the current NAND chip does not support Block Unlocking.
- Write 0x1098c00 length data from RAM: 0x80800000 to flash: 0x4400000
- Write flash from 0x4400000, length 0x1018000
- Erase flash from 0x4400000, length 0x1018000
二:
烧写内存 0x80800000 开始 实际长度为 3145156 的内核数据到起始地址为 0x100000 的内核分区 java代码: blob> nandwrite -z 0x80800000 0x100000 3145156 the current NAND chip does not support Block Unlocking. Write 0x2ffdc4 length data from RAM: 0x80800000 to flash: 0x100000 Write flash from 0x100000, length 0x2ffdc4 Erase flash from 0x100000, length 0x300000 复制代码 下载系统分区镜像文件到pc内存 0x80800000 地址处 java代码: blob> tftp system0918.img TFTPing system0918.img OK. received 113138 blocks (57925824 bytes) 复制代码 擦除原来的flash系统分区 java代码: blob> nanderase -z 0x500000 0x4000000 the current NAND chip does not support Block Unlocking. Erase 0x3e0f800 length data from flash: 0x400000 Erase flash from 0x400000, length 0x3e0f800 复制代码 烧写数据到flash系统分区 java代码: blob> nandwrite -y 0x80800000 0x500000 57925824 the current NAND chip does not support Block Unlocking. Write 0x373e0c0 length data from RAM: 0x80800000 to flash: 0x400000 Write flash from 0x400000, length 0x3591800 Erase flash from 0x400000, length 0x3591800 复制代码 擦除原来的flash数据分区 java代码: blob> nanderase -z 0x4500000 0xBB00000 the current NAND chip does not support Block Unlocking. Erase 0xa81f000 length data from flash: 0x4400000 Erase flash from 0x4400000, length 0xa81f000 复制代码 烧写数据镜像到flash数据分区 java代码: blob> nandwrite -y 0x80800000 0x4500000 17402880 the current NAND chip does not support Block Unlocking. Write 0x1098c00 length data from RAM: 0x80800000 to flash: 0x4400000 Write flash from 0x4400000, length 0x1018000 Erase flash from 0x4400000, length 0x1018000 复制代码
三:
下载系统分区镜像文件到pc内存 0x80800000 地址处 java代码: blob> tftp system0918.img TFTPing system0918.img received 113138 blocks (57925824 bytes) tftp_cmd: file 'system0918.img' loaded via tftp to address 0x80800000. 复制代码 擦除原来的flash系统分区 java代码: blob> nanderase -z 0x500000 0x4000000 the current NAND chip does not support Block Unlocking. Erase 0x3e0f800 length data from flash: 0x400000 Erase flash from 0x400000, length 0x3e0f800 复制代码 烧写数据到flash系统分区 java代码: blob> nandwrite -y 0x80800000 0x500000 57925824 the current NAND chip does not support Block Unlocking. Write 0x373e0c0 length data from RAM: 0x80800000 to flash: 0x400000 Write flash from 0x400000, length 0x3591800 Erase flash from 0x400000, length 0x3591800 复制代码 下载数据分区镜像文件到pc内存 0x80800000 地址处 java代码: blob> tftp data0918.img TFTPing data0918.img received 33992 blocks (17402880 bytes) tftp_cmd: file 'data0918.img' loaded via tftp to address 0x80800000. blob> 复制代码 擦除原来的flash数据分区 java代码: blob> nanderase -z 0x4500000 0xBB00000 the current NAND chip does not support Block Unlocking. Erase 0xa81f000 length data from flash: 0x4400000 Erase flash from 0x4400000, length 0xa81f000 复制代码 烧写数据镜像到flash数据分区 java代码: blob> nandwrite -y 0x80800000 0x4500000 17402880 the current NAND chip does not support Block Unlocking. Write 0x1098c00 length data from RAM: 0x80800000 to flash: 0x4400000 Write flash from 0x4400000, length 0x1018000 Erase flash from 0x4400000, length 0x1018000 复制代码
四;
flash分区图: java代码: nanderase -z 0x100000 0x400000 tftp zImage nandwrite -z 0x80800000 0x100000 //烧写system.img: nanderase -z 0x500000 0x4000000 tftp system.img nandwrite -y 0x80800000 0x500000 //烧写 userdata.img : nanderase -z 0x4500000 0xBB00000 tftp userdata.img nandwrite -y 0x80800000 0x4500000 复制代码 涉及的内容: svn服务器的使用 android的编译系统,原理,工具链,辅助工具,参数等,环境变量,怎样单独添加编译一个单独的模块等。 android 的编译结果:文件系统分析 文件系统的使用,启动流程 设置模块流程分析 1. Android编译系统分析 编译脚本及系统变量 build/envsetup.sh脚本分析 在编译源代码之前通常需要在android源代码顶层目录执行 . ./build/envsetup.sh 目的是为了使用脚本 envsetup.sh 里面定义了一些函数: java代码: function help() function get_abs_build_var() function get_build_var() function check_product() function check_variant() function setpaths() function printconfig() function set_stuff_for_environment() function set_sequence_number() function settitle() function choosetype() function chooseproduct() function choosevariant() function tapas() function choosecombo() function print_lunch_menu() function lunch() function gettop function m() function findmakefile() function mm() function mmm() function croot() function pid() function gdbclient() function jgrep() function cgrep() function resgrep() function getprebuilt function tracedmdump() function runhat() function getbugreports() function startviewserver() function stopviewserver() function isviewserverstarted() function smoketest() function runtest() function runtest_py() function godir () 复制代码 choosecombo 命令分析: java代码: function choosecombo(){ choosesim $1 echo echo choosetype $2 echo echo chooseproduct $3 echo echo choosevariant $4 echo set_stuff_for_environment printconfig } 复制代码
五:
会依次进行如下选择: java代码: Build for the simulator or the device? 1. Device 2. Simulator Which would you like? [1] Build type choices are: 1. release 2. debug Which would you like? [1] Product choices are: 1. emulator 2. generic 3. sim 4. littleton You can also type the name of a product if you know it. Which would you like? [littleton] Variant choices are: 1. user 2. userdebug 3. eng Which would you like? [eng] user 复制代码 默认选择以后会出现: java代码: TARGET_PRODUCT=littleton TARGET_BUILD_VARIANT=user TARGET_SIMULATOR=false TARGET_BUILD_TYPE=release TARGET_ARCH=arm HOST_ARCH=x86 HOST_OS=linux HOST_BUILD_TYPE=release BUILD_ID= 复制代码 function chooseproduct()函数分析: java代码: choices=(`/bin/ls build/target/board/*/BoardConfig.mk vendor/*/*/BoardConfig.mk 2> /dev/null`) //读取 build/target/board/* 目录下的板配置文件:BoardConfig.mk //读取 vendor/*/*/目录下的板配置文件:BoardConfig.mk //choices 的值为: build/target/board/emulator/BoardConfig.mk build/target/board/generic/BoardConfig.mk build/target/board/sim/BoardConfig.mk vendor/marvell/littleton/BoardConfig.mk 复制代码 经过: java代码: for choice in ${choices[@]} do # The product name is the name of the directory containing # the makefile we found, above. prodlist=(${prodlist[@]} `dirname ${choice} | xargs basename`) done //的处理,prodlist的值为: emulator generic sim littleton //所以选择菜单为: Product choices are: 1. emulator 2. generic 3. sim 4. littleton //如果选择 4,那么 TARGET_PRODUCT 被赋值为: littleton。 board_config_mk := \ $(strip $(wildcard \ $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \ vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \ )) 复制代码 怎样添加一个模块 java代码: LOCAL_PATH:= $(call my-dir) #编译静态库 include $(CLEAR_VARS) LOCAL_MODULE = libhellos LOCAL_CFLAGS = $(L_CFLAGS) LOCAL_SRC_FILES = hellos.c LOCAL_C_INCLUDES = $(INCLUDES) LOCAL_SHARED_LIBRARIES := libcutils LOCAL_COPY_HEADERS_TO := libhellos LOCAL_COPY_HEADERS := hellos.h include $(BUILD_STATIC_LIBRARY) #编译动态库 include $(CLEAR_VARS) LOCAL_MODULE = libhellod LOCAL_CFLAGS = $(L_CFLAGS) LOCAL_SRC_FILES = hellod.c LOCAL_C_INCLUDES = $(INCLUDES) LOCAL_SHARED_LIBRARIES := libcutils LOCAL_COPY_HEADERS_TO := libhellod LOCAL_COPY_HEADERS := hellod.h include $(BUILD_SHARED_LIBRARY) BUILD_TEST=true ifeq ($(BUILD_TEST),true) 复制代码
六:
java代码:
- #使用静态库
- include $(CLEAR_VARS)
- LOCAL_MODULE := hellos
- LOCAL_STATIC_LIBRARIES := libhellos
- LOCAL_SHARED_LIBRARIES :=
- LOCAL_LDLIBS += -ldl
- LOCAL_CFLAGS := $(L_CFLAGS)
- LOCAL_SRC_FILES := mains.c
- LOCAL_C_INCLUDES := $(INCLUDES)
- include $(BUILD_EXECUTABLE)
- #使用动态库
- include $(CLEAR_VARS)
- LOCAL_MODULE := hellod
- LOCAL_MODULE_TAGS := debug
- LOCAL_SHARED_LIBRARIES := libc libcutils libhellod
- LOCAL_LDLIBS += -ldl
- LOCAL_CFLAGS := $(L_CFLAGS)
- LOCAL_SRC_FILES := maind.c
- LOCAL_C_INCLUDES := $(INCLUDES)
- include $(BUILD_EXECUTABLE)
- endif # ifeq ($(WPA_BUILD_SUPPLICANT),true)
- #local_target_dir := $(TARGET_OUT)/etc/wifi
- #include $(CLEAR_VARS)
- #LOCAL_MODULE := wpa_supplicant.conf
- #LOCAL_MODULE_TAGS := user
- #LOCAL_MODULE_CLASS := ETC
- #LOCAL_MODULE_PATH := $(local_target_dir)
- #LOCAL_SRC_FILES := $(LOCAL_MODULE)
- #include $(BUILD_PREBUILT)
系统变量解析
LOCAL_MODULE - 编译的目标对象
LOCAL_SRC_FILES - 编译的源文件
LOCAL_C_INCLUDES - 需要包含的头文件目录
LOCAL_SHARED_LIBRARIES - 链接时需要的外部库
LOCAL_PRELINK_MODULE - 是否需要prelink处理
BUILD_SHARED_LIBRARY - 指明要编译成动态库
LOCAL_PATH - 编译时的目录
$(call 目录,目录….) 目录引入操作符,如该目录下有个文件夹名称 src,则可以这样写 $(call src),那么就会得到 src 目录的完整路径
include $(CLEAR_VARS) -清除之前的一些系统变量
CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
在 build/core/config.mk 定义 CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
通过include 包含自定义的.mk文件(即是自定义编译规则)或是引用系统其他的.mk文件(系统定义的编译规则)。
LOCAL_SRC_FILES - 编译的源文件
可以是.c, .cpp, .java, .S(汇编文件)或是.aidl等格式
不同的文件用空格隔开。如果编译目录子目录,采用相对路径,如子目录/文件名。也可以通过$(call 目录),指明编译某目录
下所有.c/.cpp/.java/.S/ .aidl文件.追加文件 LOCAL_SRC_FILES += 文件
LOCAL_C_INCLUDES - 需要包含的头文件目录
可以是系统定义路径,也可以是相对路径. 如该编译目录下有个include目录,写法是include/*.h
LOCAL_SHARED_LIBRARIES - 链接时需要的外部共享库
LOCAL_STATIC_LIBRARIES - 链接时需要的外部外部静态
LOCAL_JAVA_LIBRARIES 加入jar包
LOCAL_MODULE - 编译的目标对象
module 是指系统的 native code,通常针对c,c++代码
java代码:
- ./system/core/sh/Android.mk:32:LOCAL_MODULE:= sh
- ./system/core/libcutils/Android.mk:71:LOCAL_MODULE := libcutils
- ./system/core/cpio/Android.mk:9:LOCAL_MODULE := mkbootfs
- ./system/core/mkbootimg/Android.mk:8:LOCAL_MODULE := mkbootimg
- ./system/core/toolbox/Android.mk:61:LOCAL_MODULE:= toolbox
- ./system/core/logcat/Android.mk:10:LOCAL_MODULE:= logcat
- ./system/core/adb/Android.mk:65:LOCAL_MODULE := adb
- ./system/core/adb/Android.mk:125:LOCAL_MODULE := adbd
- ./system/core/init/Android.mk:20:LOCAL_MODULE:= init
- ./system/core/vold/Android.mk:24:LOCAL_MODULE:= vold
- ./system/core/mountd/Android.mk:13:LOCAL_MODULE:= mountd
- LOCAL_PACKAGE_NAME
Java 应用程序的名字用该变量定义
java代码:
- ./packages/apps/Music/Android.mk:9:LOCAL_PACKAGE_NAME := Music
- ./packages/apps/Browser/Android.mk:14:LOCAL_PACKAGE_NAME := Browser
- ./packages/apps/Settings/Android.mk:8:LOCAL_PACKAGE_NAME := Settings
- ./packages/apps/Stk/Android.mk:10:LOCAL_PACKAGE_NAME := Stk
- ./packages/apps/Contacts/Android.mk:10:LOCAL_PACKAGE_NAME := Contacts
- ./packages/apps/Mms/Android.mk:8:LOCAL_PACKAGE_NAME := Mms
- ./packages/apps/Camera/Android.mk:8:LOCAL_PACKAGE_NAME := Camera
- ./packages/apps/Phone/Android.mk:11:LOCAL_PACKAGE_NAME := Phone
- ./packages/apps/VoiceDialer/Android.mk:8:LOCAL_PACKAGE_NAME := VoiceDialer
http://www.eoeandroid.com/thread-91316-1-1.html