Difference between revisions of "Template:S5P4418BuildFromSource"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
(Compile U-Boot)
Line 35: Line 35:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Compile U-Boot===
+
===Compile Linux kernel 4.4.y===
Download the U-Boot source code and compile it. Note that the github's branch is nanopi2-lollipop-mr1:
+
====Compile Kernel====
 +
* Download Kernel Source Code
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
git clone https://github.com/friendlyarm/uboot_nanopi2.git
+
git clone https://github.com/friendlyarm/linux.git
cd uboot_nanopi2
+
cd linux
git checkout nanopi2-lollipop-mr1
+
git checkout nanopi2-v4.4.y
make s5p4418_nanopi2_config
+
make CROSS_COMPILE=arm-linux-
+
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
The NanoPC-T2's kernel source code is in the "nanopi2-v4.4.y" branch.You need to switch to this branch.
 +
* Compile Ubuntu Kernel
 +
<syntaxhighlight lang="bash">
 +
touch .scmversion
 +
make ARCH=arm nanopi2_linux_defconfig
 +
make ARCH=arm
 +
</syntaxhighlight>
 +
After your compilation succeeds an "arch/arm/boot/zImage" will be generated and a DTB file(s5p4418-nanopi2-rev*.dtb) will be generated in the "arch/arm/boot/dts/" directory. You can use them to replace the existing zImage and DTB files in the boot section of your bootable SD card.<br />
  
After your compilation succeeds a u-boot.bin will be generated. If you want to test it flash it to your installation SD card via fastboot. Here is how you can do it:<br/>
+
====如何使用新编译的内核====
1) On your host PC run "sudo apt-get install android-tools-fastboot" to install the fastboot utility;<br />
+
* 更新SD卡上的内核
2) Connect your NanoPC-T2 to your host PC via a serial cable (e.g. PSU-ONECOME). Press the enter key within two seconds right after you power on your NanoPC-T2 and you will enter uboot's command line mode;<br />
+
如果您是使用SD卡启动Ubuntu系统,则在PC上复制编译生成的zImage和DTB文件到SD卡的boot分区(即分区1,设备是/dev/sdX1)即可。
3) After type in "fastboot" and press "enter" you will enter the fastboot mode;<br />
+
 
4) Connect your NanoPC-T2 to this host PC via a microUSB cable and type in the following command to flash u-boot.bin:<br />
+
* 更新eMMC上Ubuntu的内核
 +
从eMMC启动时可通过以下方法来更新内核:<br />
 +
1) 启动完成后,系统通常会自动mount eMMC的boot分区(设备是/dev/mmcblk0p1), 可输入命令mount来查看; <br />
 +
2) 连接网络,使用scp/ftp等方式复制新编译的zImage和DTB文件并替换boot分区下的文件; <br />
 +
3) 也可以将编译好的内核复制到SD卡或U盘,然后在板止复制到boot分区下; <br />
 +
4) 更新完成后,输入 reboot 命令重启即可,注意不要直接断电或按Reset键,否则可能会损坏文件. <br />
 +
 
 +
* 使用新的内核来生成boot.img
 +
如果要生成直接烧写eMMC的文件,则需要使用新编译的内核来生成boot.img,然后复制到烧写用的SD卡即可直接烧写到eMMC. <br />
 +
对于Ubuntu, 则需要使用按以下方法来生成boot.img : <br />
 +
1) 下载debian_nanopi2
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
fastboot flash bootloader u-boot.bin
+
git clone https://github.com/friendlyarm/debian_nanopi2.git
 +
</syntaxhighlight>
 +
2) 复制用于新的zImage和DTB文件到debian_nanopi2/boot/, ; <br />
 +
3) 生成Debian的 boot.img
 +
<syntaxhighlight lang="bash">
 +
cd debian_nanopi2
 +
mkdir rootfs
 +
./build.sh
 +
</syntaxhighlight>
 +
新的 boot.img 在 debian_nanopi2/sd-fuse_nanopi2/debian 下. <br />
 +
其中命令"mkdir rootfs"只是创建一个空的目录使得build.sh可以运行,因此生成的其它文件比如rootfs.img不能使用。
 +
 
 +
===Compile U-Boot===
 +
Download the U-Boot source code and compile it. Note that the github's branch is nanopi2-v2016.01:
 +
<syntaxhighlight lang="bash">
 +
git clone https://github.com/friendlyarm/u-boot.git
 +
cd u-boot
 +
git checkout nanopi2-v2016.01
 +
make s5p4418_nanopi2_defconfig
 +
make CROSS_COMPILE=arm-linux-
 
</syntaxhighlight>
 
</syntaxhighlight>
<br />
+
After your compilation succeeds a bootloader.img will be generated. If you want to test it flash it to your installation SD card to replace an existing U-Boot v2014.07 file via fastboot, sd-fuse_nanopi2 or eflasher ROM.<br/>
Warning: you cannot update this SD card by commanding "dd". This command will cause trouble when booting the NanoPC-T2.<br />
+
Note: you cannot use mixed U-Boot files. For example you cannot use fastboot to update an existing U-Boot V2014.07 and you cannot use bootloader.img to replace an existing u-boot.bin
  
 
===Prepare mkimage===
 
===Prepare mkimage===

Revision as of 06:55, 26 December 2017

1 Make Your Own OS Image

1.1 Install Cross Compiler

Download the compiler package:

git clone https://github.com/friendlyarm/prebuilts.git
sudo mkdir -p /opt/FriendlyARM/toolchain
sudo tar xf prebuilts/gcc-x64/arm-cortexa9-linux-gnueabihf-4.9.3.tar.xz -C /opt/FriendlyARM/toolchain/

Then add the compiler's directory to "PATH" by appending the following lines in "~/.bashrc":

export PATH=/opt/FriendlyARM/toolchain/4.9.3/bin:$PATH
export GCC_COLORS=auto

Execute "~/.bashrc" to make the changes take effect. Note that there is a space after the first ".":

. ~/.bashrc

This compiler is a 64-bit one therefore it cannot be run on a 32-bit Linux machine. After the compiler is installed you can verify it by running the following commands:

arm-linux-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gcc
COLLECT_LTO_WRAPPER=/opt/FriendlyARM/toolchain/4.9.3/libexec/gcc/arm-cortexa9-linux-gnueabihf/4.9.3/lto-wrapper
Target: arm-cortexa9-linux-gnueabihf
Configured with: /work/toolchain/build/src/gcc-4.9.3/configure --build=x86_64-build_pc-linux-gnu
--host=x86_64-build_pc-linux-gnu --target=arm-cortexa9-linux-gnueabihf --prefix=/opt/FriendlyARM/toolchain/4.9.3
--with-sysroot=/opt/FriendlyARM/toolchain/4.9.3/arm-cortexa9-linux-gnueabihf/sys-root --enable-languages=c,c++
--with-arch=armv7-a --with-tune=cortex-a9 --with-fpu=vfpv3 --with-float=hard
...
Thread model: posix
gcc version 4.9.3 (ctng-1.21.0-229g-FA)

1.2 Compile Linux kernel 4.4.y

1.2.1 Compile Kernel

  • Download Kernel Source Code
git clone https://github.com/friendlyarm/linux.git
cd linux
git checkout nanopi2-v4.4.y

The NanoPC-T2's kernel source code is in the "nanopi2-v4.4.y" branch.You need to switch to this branch.

  • Compile Ubuntu Kernel
touch .scmversion
make ARCH=arm nanopi2_linux_defconfig
make ARCH=arm

After your compilation succeeds an "arch/arm/boot/zImage" will be generated and a DTB file(s5p4418-nanopi2-rev*.dtb) will be generated in the "arch/arm/boot/dts/" directory. You can use them to replace the existing zImage and DTB files in the boot section of your bootable SD card.

1.2.2 如何使用新编译的内核

  • 更新SD卡上的内核

如果您是使用SD卡启动Ubuntu系统,则在PC上复制编译生成的zImage和DTB文件到SD卡的boot分区(即分区1,设备是/dev/sdX1)即可。

  • 更新eMMC上Ubuntu的内核

从eMMC启动时可通过以下方法来更新内核:
1) 启动完成后,系统通常会自动mount eMMC的boot分区(设备是/dev/mmcblk0p1), 可输入命令mount来查看;
2) 连接网络,使用scp/ftp等方式复制新编译的zImage和DTB文件并替换boot分区下的文件;
3) 也可以将编译好的内核复制到SD卡或U盘,然后在板止复制到boot分区下;
4) 更新完成后,输入 reboot 命令重启即可,注意不要直接断电或按Reset键,否则可能会损坏文件.

  • 使用新的内核来生成boot.img

如果要生成直接烧写eMMC的文件,则需要使用新编译的内核来生成boot.img,然后复制到烧写用的SD卡即可直接烧写到eMMC.
对于Ubuntu, 则需要使用按以下方法来生成boot.img :
1) 下载debian_nanopi2

git clone https://github.com/friendlyarm/debian_nanopi2.git

2) 复制用于新的zImage和DTB文件到debian_nanopi2/boot/, ;
3) 生成Debian的 boot.img

cd debian_nanopi2
mkdir rootfs
./build.sh

新的 boot.img 在 debian_nanopi2/sd-fuse_nanopi2/debian 下.
其中命令"mkdir rootfs"只是创建一个空的目录使得build.sh可以运行,因此生成的其它文件比如rootfs.img不能使用。

1.3 Compile U-Boot

Download the U-Boot source code and compile it. Note that the github's branch is nanopi2-v2016.01:

git clone https://github.com/friendlyarm/u-boot.git 
cd u-boot
git checkout nanopi2-v2016.01
make s5p4418_nanopi2_defconfig
make CROSS_COMPILE=arm-linux-

After your compilation succeeds a bootloader.img will be generated. If you want to test it flash it to your installation SD card to replace an existing U-Boot v2014.07 file via fastboot, sd-fuse_nanopi2 or eflasher ROM.
Note: you cannot use mixed U-Boot files. For example you cannot use fastboot to update an existing U-Boot V2014.07 and you cannot use bootloader.img to replace an existing u-boot.bin

1.4 Prepare mkimage

You need the mkimage utility to compile a U-Boot source code package. Make sure this utility works well on your host before you start compiling a uImage.
You can install this utility by either commanding "sudo apt-get install u-boot-tools" or following the commands below:

cd uboot_nanopi2
make CROSS_COMPILE=arm-linux- tools
sudo mkdir -p /usr/local/sbin && sudo cp -v tools/mkimage /usr/local/sbin

1.5 Compile Linux Kernel

1.5.1 Compile Kernel

  • Download Kernel Source Code
git clone https://github.com/friendlyarm/linux-3.4.y.git
cd linux-3.4.y
git checkout nanopi2-lollipop-mr1

The NanoPC-T2's kernel source code lies in the "nanopi2-lollipop-mr1" branch.

  • Compile Android Kernel
make nanopi2_android_defconfig
touch .scmversion
make uImage
  • Compile Debian Kernel
make nanopi2_linux_defconfig
touch .scmversion
make uImage

After your compilation succeeds a uImage will be generated in the "arch/arm/boot/uImage" directory. This kernel is for LCD output. You can use it to replace the existing uImage.
If you want to generate a kernel for HDMI output you need to run nanopi2_linux_hdmi_defconfig and do it this way:

make nanopi2_linux_hdmi_defconfig
touch .scmversion
make menuconfig

After your compilation succeeds a uImage will be generated for HDMI 720P. If you want a uImage for 1080P you can do it this way:

touch .scmversion
make nanopi2_linux_hdmi_defconfig
make menuconfig
  Device Drivers -->
    Graphics support -->
      Nexell Graphics -->
        [ ] LCD
        [*] HDMI
        (0)   Display In  [0=Display 0, 1=Display 1]
              Resolution (1920 * 1080p)  --->
make uImage

After your compilation succeeds a uImage will be generated for HDMI 1080P. You can use it to replace the existing uImage.hdmi.

  • Compile Kernel for Ubuntu Core

The steps here are nearly the same as the steps for compiling a Debian kernel:
LCD Output:

make nanopi2_core-qt_defconfig

HDMI Output:

make nanopi2_core-qt_hdmi_defconfig

Select your configuration file and run the following commands to generate a uImage.

touch .scmversion
make uImage

1.5.2 User Your Generated Kernel

  • Update the kernel file in SD card

If you use an SD card to boot Android you can copy your generated uImage file to your SD card's boot section(e.g. section 1 /dev/sdX1).
If you use an SD card to Debian and you generated a uImage for an HDMI monitor you can use that uImage to replace the uImage.hdmi file in the SD card's boot section. If you use an SD card to Debian and you generated a uImage for an LCD you can use that uImage to replace the uImage file in the SD card's boot section.

  • Update Android kernel file in eMMC

If you want to update the kernel file in eMMC you need firstly boot your board, then mount eMMC's boot section, replace the boot section's kernel file with your generated one and reboot your board to make your new kernel run.
If you boot your board from eMMC you can update your kernel file by following the steps below:
1) After Android is loaded mount eMMC's boot section (in our example eMMC's device name was /dev/mmcblk0p1) by using the following commands:

su
mount -t ext4 /dev/block/mmcblk0p1 /mnt/media_rw/sdcard1/

2) Connect your board to a host PC running Ubuntu and copy the uImage file to eMMC's boot section by running the following commands

adb push uImage /mnt/media_rw/sdcard1/

3) Or you can copy your generated kernel file to an external storage card(e.g. an SD card or a USB drive), connect the storage card to your board the move the file from the card to eMMC's boot section
4) After update is done type in "reboot" to reload Android. Note don't directly power off and on the board or press the reset button to reboot the board. These two actions will damage your kernel file

  • Update Debian kernel file in eMMC

If you boot your board from eMMC you can update your kernel file by following the steps below:
1) When Debian is being loaded eMMC's boot section will be automatically mounted(in our example eMMC's device name was /dev/mmcblk0p1). You can use "mount" to verify that
2) Connect your board to a host PC via Ethernet and copy your generated uImage file via scp/ftp to eMMC's boot section and replace the existing file. If your file is for an LCD use your uImage file to replace the existing uImage. If your file is for an HDMI monitor use your uImage.hdmi file to replace the existing uImage.hdmi file
3) Or you can copy your generated kernel file to an external storage card(e.g. an SD card or a USB drive), connect the storage card to your board the move the file from the card to eMMC's boot section
4) After update is done type in "reboot" to reload Debian. Note don't directly power off and on the board or press the reset button to reboot the board. These two actions will damage your kernel file

  • Generate Your boot.img

If you want to generate an image file that can be flashed to eMMC you need to generate a boot.img file and copy it to your installation SD card
For Android copy the uImage file to Android source code's "device/friendly-arm/nanopi2/boot/" directory and compile this whole Android source code. After your compilation is successful you will get a boot.img file.
For Debian follow the steps below to generate a boot.img file
1) Download debian_nanopi2

git clone https://github.com/friendlyarm/debian_nanopi2.git

2) Copy the image file for an HDMI monitor and use it to replace the "debian_nanopi2/boot/uImage.hdmi" file and copy the image file for an LCD and use it to replace the "debian_nanopi2/boot/uImage" file
3) Generate Debian's boot.img

cd debian_nanopi2
mkdir rootfs
./build.sh

A newly generated boot.img will be under the "debian_nanopi2/sd-fuse_nanopi2/debian" directory.
The "mkdir rootfs" command creates a working directory for the build.sh script to run. It also creates some files such as "rootfs.img" but these files are useless.

1.5.3 Compile Kernel Modules

Android contains kernel modules which are in the "/lib/modules" directory in the system section. If you want to add your own modules to the kernel or you changed your kernel configurations you need to recompile these new modules.
Compile Original Kernel Modules:

cd linux-3.4.y
make CROSS_COMPILE=arm-linux- modules

Here we have two new modules and we can compile them by following the commands below:

cd /opt/FriendlyARM/s5p4418/android
./vendor/friendly-arm/build/common/build-modules.sh

The "/opt/FriendlyARM/s5p4418/android" directory points to the top directory of Android source code. You can get more details by specifying option "-h".
After your compilation succeeds new modules will be generated

1.6 Compile Android

We provide two Android versions: Android 4.4 and Android 5.1. Both of them are compiled the same way.

  • Install Cross Compiler

Install 64 bit Ubuntu 16.04 on your host PC.

sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zip
sudo apt-get install flex libncurses5-dev zlib1g-dev gawk minicom

For more details refer to https://source.android.com/source/initializing.html

  • Download Android 5.1's Source Code

You need to use repo to get the Android source code. Refer to https://source.android.com/source/downloading.html

mkdir android && cd android
repo init -u https://github.com/friendlyarm/android_manifest.git -b nanopi2-lollipop-mr1
repo sync

The "android" directory is the working directory.

If you want to try Android4.4's source code you can run the following commands:

mkdir android && cd android
repo init -u https://github.com/friendlyarm/android_manifest.git -b nanopi2-kitkat
repo sync

Option "-b" specifies a branch

  • Compile System Package
source build/envsetup.sh
lunch aosp_nanopi2-userdebug
make -j8

After your compilation succeeds the following files will be generated in the "out/target/product/nanopi2/" directory.

filename partition Description
boot.img boot -
cache.img cache -
userdata.img userdata -
system.img system -
partmap.txt - partition description file
  • Flash Image to eMMC

After compiling Android successfully you can flash it to eMMC with either of the following methods
1) fastboot: right after the NanoPC-T2 is booted from eMMC press any key to enter the uboot commandline mode and type in "fastboot"
Connect your board to a host PC running Ubuntu with a USB cable and run the following commands in the PC's terminal:

cd out/target/product/nanopi2
sudo fastboot flash boot boot.img
sudo fastboot flash cache cache.img
sudo fastboot flash userdata userdata.img
sudo fastboot flash system system.img
sudo fastboot reboot

2) Use an SD Card
Copy these files: boot.img, cache.img, userdata.img, system.img, partmap.txt from the out/target/product/nanopi2 directory to your installation SD card's images/android directory and you can use this SD card to flash Android to eMMC