需要板子一起學習的可以這里購買(含資料):點擊跳轉
GEC6818內核源碼下載:點擊跳轉
一、環境配置?????????
????????由于GEC6818對應是64位系統,虛擬機中的linux系統也要是64位,比如:ubuntu16.04.rar
? ?
? ?但是,ubuntu16.04.rar系統,對虛擬機有要求:
? ?打開ubuntu16.04.vmx查看對虛擬機的要求:
? ?virtualHW.version = "12" ----- VM為12
? ?如果說電腦上的VM低于12,不要重新安裝VM,
? ?virtualHW.version = "10"
二、源碼拷貝
????????把6818GEC.tar.gz拷貝到linux系統下,不能采用文件共享來在windows下編譯uboot和內核
第一步:拷貝源碼到linux的家目錄下
第二步:解壓源碼
sudo tar zxvf 6818GEC.tar.gz
第三步:修改源碼的權限
chmod 777 6818GEC
第四步:查看源碼的目錄內容
$ ls -l
total 40
drwxrwxr-x 15 gec gec 4096 Feb 15 2017 buildroot
drwxrwxr-x 20 gec gec 4096 Jul 17 2017 GEC6818uboot ---->開發板Uboot的源碼
drwxrwxr-x 25 gec gec 4096 Jul 17 2017 kernel ---> 開發板內核的源碼
drwxrwxr-x 3 gec gec 4096 Jul 18 2016 linux
-rwxrwxr-x 1 gec gec 6019 Jul 17 2017 mk ---> 腳本
drwxrwxr-x 5 gec gec 4096 Dec 21 2016 out ----> UBOOT和內核的輸出
drwxrwxr-x 3 gec gec 4096 Jul 18 2016 prebuilts ---> 編譯uboot與內核使用到的工具鏈
drwxrwxr-x 3 gec gec 4096 Jul 18 2016 prototype
drwxrwxr-x 3 gec gec 4096 Feb 14 2017 tools
三、mk腳本分析
1) 關于路徑變量
? ? BS_DIR_TOP=$(cd `dirname $0` ; pwd) ----> gec@ubuntu:~/6818GEC$ ./mk 1 2 3 1 ?./mk 相當于 $0
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $() ----->提取內容,并且把內容賦值給變量BS_DIR_TOP
?? ??? ??? ??? ??? ??? ??? ? ? ? gec@ubuntu:~/6818GEC$ pwd
?? ??? ??? ??? ??? ??? ??? ??? ?/home/gec/6818GEC
?? ??? ??? ??? ??? ??? ??? ??? ?BS_DIR_TOP=/home/gec/6818GEC
?? ?BS_DIR_RELEASE=${BS_DIR_TOP}/out/release
?? ?BS_DIR_TARGET=${BS_DIR_TOP}/out/target/product/GEC6818/
?? ?BS_DIR_UBOOT=${BS_DIR_TOP}/GEC6818uboot ?----> uboot的路徑
?? ?BS_DIR_KERNEL=${BS_DIR_TOP}/kernel ----> 內核的路徑
?? ?BS_DIR_BUILDROOT=${BS_DIR_TOP}/buildroot
?? ?
?? ?以上都是變量進行賦值
2)關于路徑變量
? BS_CROSS_TOOLCHAIN_BOOTLOADER=${BS_DIR_TOP}/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
? BS_CROSS_TOOLCHAIN_KERNEL=${BS_DIR_TOP}/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
? 以上都是工具邏的路徑
3) Target Config
? ? BS_CONFIG_BOOTLOADER_UBOOT=GEC6818_config ?---->編譯uboot要使用到的配置文件
?? ?BS_CONFIG_KERNEL=GEC6818_defconfig ---> 編譯kernel要使用到的配置文件
?? ??? ?gec@ubuntu:~/6818GEC/kernel/arch/arm/configs$ ls
?? ??? ?GEC6818_defconfig?
?? ?BS_CONFIG_FILESYSTEM=PRODUCT-GEC6818-userdebug
?? ?BS_CONFIT_BUILDROOT=GEC6818_defconfig
4)編譯各個模塊的功能函數
? ? setup_environment()
?? ?build_bootloader_uboot()
?? ?build_kernel()
?? ?
? 5) 普通變量
?? ?threads=4
?? ?uboot=no ----> 默認情況下為no
?? ?kernel=no
?? ?system=no
?? ?buildroot=no
? 6) 分析shell語句
? ?if [ -z $1 ]; then
?? ?uboot=yes
?? ?kernel=yes
?? ?system=yes
?? ?buildroot=yes
? fi
7) case語句
? ?while [ "$1" ]; do
? ? case "$1" in
?? ?-j=*)
?? ??? ?x=$1
?? ??? ?threads=${x#-j=}
?? ??? ?;;
?? ?-u|--uboot)
?? ??? ?uboot=yes
?? ? ? ?;;
?? ?-k|--kernel)
?? ? ? ?kernel=yes
?? ? ? ?;;
?? ?-s|--system)
?? ??? ?system=yes
?? ? ? ?;;
?? ?-b|--buildroot)
?? ? ? ?buildroot=yes
?? ? ? ?;;
?? ?-a|--all)
?? ??? ?uboot=yes
?? ??? ?kernel=yes
?? ??? ?system=yes
?? ??? ?buildroot=yes
?? ? ? ?;;
?? ?如果$1 的值為 -u 或者 --uboot,則執行
?? ? ? uboot=yes
8) ?根據變量的值的狀態調用相應的函數
? ?if [ "${uboot}" = yes ]; then
?? ?build_bootloader_uboot || exit 1
?? ?fi
?? ?if [ "${kernel}" = yes ]; then
?? ??? ?build_kernel || exit 1
?? ?fi
?? ?
9)分析相關的函數
?? ? ?build_bootloader_uboot
?? ? ?
?? ? ?build_kernel()
?? ?{
?? ??? ?export PATH=${BS_DIR_UBOOT}/tools:$PATH ?---> 配置PATH
?? ??? ?# Compiler kernel
?? ??? ?cd ${BS_DIR_KERNEL} || return 1 ---> 進入到內核目錄
?? ??? ?make ${BS_CONFIG_KERNEL} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} || return 1-->
?? ??? ?等 價于 make GEC6818_defconfig ARCH=arm CROSS_COMPILE=/home/gec/6818GEC/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
?? ??? ?make -j${threads} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} || return 1
?? ??? ?make -j${threads} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} uImage || return 1 --->
?? ??? ?生成內核的鏡像:uImage
?? ??? ?# Copy uImage to release directory
?? ??? ?#cp -v ${BS_DIR_KERNEL}/arch/arm/boot/uImage ${BS_DIR_RELEASE}
?? ??? ?#echo "^_^ kernel path: ${BS_DIR_RELEASE}/uImage"
?? ??? ?# generate boot.img
?? ??? ?cd ${BS_DIR_TOP} || return 1
?? ??? ?echo 'boot.img ->' ${BS_DIR_RELEASE}
?? ??? ?# Make boot.img with ext4 format, 64MB
?? ??? ?cp -v ${BS_DIR_KERNEL}/arch/arm/boot/uImage ${BS_DIR_TARGET}/boot --->
?? ??? ?把uImage拷貝到${BS_DIR_TARGET}/boot目錄下
?? ??? ?
?? ??? ?mkuserimg.sh -s ${BS_DIR_TARGET}/boot ${BS_DIR_TARGET}/boot.img ext4 boot 67108864 --->
?? ??? ?通過mkuserimg.sh腳本中的命令,把${BS_DIR_TARGET}/boot目錄的uImage文件,制作成
?? ??? ?${BS_DIR_TARGET}/目錄 下boot.img鏡像文件
?? ??? ?cp -av ${BS_DIR_TARGET}/boot.img ${BS_DIR_RELEASE} || return 1; --->
?? ??? ?把編譯好的boot.img拷貝到 ${BS_DIR_RELEASE}目錄下
?? ??? ?return 0
?? ?}
?? ?
?? ?綜上分析得出:
?? ?編譯uboot:./mk -u
?? ?編譯內核:./mk -k?
四、使用mk腳本來編譯內核
~/6818GEC$ ls
buildroot ?GEC6818uboot ?kernel ?linux ?mk ?out ?prebuilts ?prototype ?tools
~/6818GEC$ ./mk -k?
?內核編譯輸出的結果:
? ?
Kernel: arch/arm/boot/Image is ready ---> 內核ImageKernel: arch/arm/boot/zImage is ready ---> 內核zImageUIMAGE ?arch/arm/boot/uImage ---->內核的最終鏡像gec@ubuntu:~/6818GEC/kernel/arch/arm/boot$ lsbootp ?compressed ?dts ?Image ?install.sh ?Makefile ?uImage ?zImageImage Name: ? Linux-3.4.39-gecCreated: ? ? ?Sun Feb 17 19:14:47 2019Image Type: ? ARM Linux Kernel Image (uncompressed)Data Size: ? ?5532568 Bytes = 5402.90 kB = 5.28 MBLoad Address: 40008000Entry Point: ?40008000Image arch/arm/boot/uImage is readyboot.img -> /home/gec/6818GEC/out/release'/home/gec/6818GEC/kernel/arch/arm/boot/uImage' -> '/home/gec/6818GEC/out/target/product/GEC6818//boot/uImage'make_ext4fs -s -T -1 -l 67108864 -a boot /home/gec/6818GEC/out/target/product/GEC6818//boot.img /home/gec/6818GEC/out/target/product/GEC6818//bootCreating filesystem with parameters:Size: 67108864Block size: 4096Blocks per group: 32768Inodes per group: 4096Inode size: 256Journal blocks: 1024Label:?Blocks: 16384Block groups: 1Reserved block group size: 7Created filesystem with 18/4096 inodes and 4212/16384 blocks'/home/gec/6818GEC/out/target/product/GEC6818//boot.img' -> '/home/gec/6818GEC/out/release/boot.img'
? ? 最終在6818GEC/out/release/boot.img找到鏡像文件boot.img
?? ?
?? ?
注意事項:
?? ? ? 問題一:在首次編譯內核時,未先編譯uboot,會出現錯誤
?? ? ? "mkimage" command not found - U-Boot images will not be built
?? ? ? 解決辦法:先編譯u-boot,再重新編譯內核
?? ? ? ./mk -u?
?? ? ? ./mk -k
?
五、燒寫內核到開發板
第一步:把編譯生成的boot.img單獨拷貝,并且與fastboot.exe、adb.exe AdbWinUsbApi.dll,AdbWinApi.dll
? 第二步:連接USB線到開發板與電腦
?? ??? ?大端的USB口接電腦
?? ??? ?小端的USB口接開發板
? 第三步:啟動開發板并且運行在u-boot命令行下,執行fastboot命令? ?
X6818# fastboot
Fastboot Partitions:mmc.2: ubootpak, img : 0x200, 0x78000mmc.2: 2ndboot, img : 0x200, 0x4000mmc.2: bootloader, img : 0x8000, 0x70000mmc.2: boot, fs : 0x100000, 0x4000000mmc.2: system, fs : 0x4100000, 0x2f200000mmc.2: cache, fs : 0x33300000, 0x1ac00000mmc.2: misc, fs : 0x4e000000, 0x800000mmc.2: recovery, fs : 0x4e900000, 0x1600000mmc.2: userdata, fs : 0x50000000, 0x0Support fstype : 2nd boot factory raw fat ext4 emmc nand ubi ubifs Reserved part : partmap mem env cmd DONE: Logo bmp 311 by 300 (3bpp), len=280854 DRAW: 0x47000000 -> 0x46000000 Load USB Driver: androidCore usb device tie configuration doneOTG cable Connected!
在PC端的設備管理器上顯示:Android Device
第四步:在PC端的命令行
?? ?ctrl + r ---> cmd ---->彈出終端界面,并且進入到boot.img目錄下
第五步:通過fastboot命令上傳文件到開發板
fastboot flash boot boot.img
sending 'boot' (16848 KB)... OKAY
writing 'boot'... OKAY
第六步:重啟動開發板
X6818# reset
六、使用make命令來編譯kernel源碼
第一步:進入到內核源碼的目錄下(~/6818GEC/kernel)
make clean -----> 清理一下項目
第二步:拷貝內核源碼編譯的配置文件(***_config)
cp arch/arm/configs/GEC6818_defconfig .config
第三步:執行make menuconfig?
make menuconfig ----->配置內核(以菜單來進行配置)
第四步:把執行make menuconfig修改后的.config拷貝回到對應的硬件平臺配置目錄下
cp .config arch/arm/configs/GEC6818_defconfig?
第五步:執行make 進行源碼的編譯
make ?ARCH=arm CROSS_COMPILE=/home/gec/6818GEC/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
七、內核源碼的結構
Linux 內核源代碼包含如下目錄。
?? ?● arch :包含和硬件體系結構相關的代碼,每種平臺占一個相應的目錄,如i386、arm、arm64、powerpc、mips 等。Linux 內核目前已經支持30 種左右的體系結構。在arch目錄下,存放的是各個平臺以及各個平臺的芯片對Linux 內核進程調度、內存管理、中斷等的支持,以及每個具體的SoC 和電路板的板級支持代碼。
?? ?● block:塊設備驅動程序I/O 調度。
?? ?● crypto:常用加密和散列算法(如AES、SHA 等),還有一些壓縮和CRC 校驗算法。
?? ?● documentation:內核各部分的通用解釋和注釋。
?? ?● drivers :設備驅動程序,每個不同的驅動占用一個子目錄,如char、block、net、mtd、i2c 等。
?? ?● fs:所支持的各種文件系統,如EXT、FAT、NTFS、JFFS2 等。
?? ?● include:頭文件,與系統相關的頭文件放置在include/linux 子目錄下。
?? ?● init:內核初始化代碼。著名的start_kernel() 就位于init/main.c 文件中。
?? ?● ipc:進程間通信的代碼。
?? ?● kernel :內核最核心的部分,包括進程調度、定時器等,而和平臺相關的一部分代碼
?? ?放在arch/*/kernel 目錄下。
?? ?● lib:庫文件代碼。
?? ?● mm:內存管理代碼,和平臺相關的一部分代碼放在arch/*/mm 目錄下。
?? ?● net:網絡相關代碼,實現各種常見的網絡協議。
?? ?● scripts:用于配置內核的腳本文件。
?? ?● security:主要是一個SELinux 的模塊。
?? ?● sound:ALSA、OSS 音頻設備的驅動核心代碼和常用設備驅動。
?? ?● usr:實現用于打包和壓縮的cpio 等。
?? ?● include:內核API 級別頭文件。
??
? ?
覺得有幫助的話,打賞一下唄。。
? ? ? ? ? ?