OpenHarmony-4.0-Release 源码编译记录

本文基于 Ubuntu 20.04.4 LTS

1、环境准备

这个没啥好说的,都是搞机的,用之前编译 aosp 的 linux 环境就行,有小伙伴担心会把之前的环境搞崩,

也有用 docker 编译的,我这里就直接在 aosp 环境下搞了,还省事。

安装下面这三东西,是为了下载 Harmony 源码

sudo apt install curl

sudo apt install python3-pip

sudo apt install git-lfs

安装下面这五个东西,是为了解决编译到最后报错(头铁不信的,你可以试试,等最后再安装)

sudo apt install default-jdk

sudo apt install mtd-utils

sudo apt install scons

sudo apt install gcc-arm-none-eabi

sudo apt install gcc-arm-linux-gnueabi

2、安装 repo

务必按照此步骤安装repo。ubuntu默认提供的repo每次启动都会访问googlge服务器,会被墙。

此外20.04下需要root身份执行--否则提示没权限。

curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > /usr/local/bin/repo

chmod a+x /usr/local/bin/repo

pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests

3、下载源码

[官网版本更新说明}(https://gitee.com/openharmony/docs/tree/master/zh-cn/release-notes)

目前最新为 4.0 版本,内核基于 5.10,老版本 2.x 1.x 已经停止维护,内核基于 4.19

参考官网下载方式

repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-4.0-Release --no-repo-verify

repo sync -c

repo forall -c 'git lfs pull'

注意那个 4.0 之前没有 v,以前的版本是有这个 v 的,不然会初始化失败,还有就是注意检查电脑是否断网了

4、下载编译依赖工具包

源码下载完成后,进入根目录执行 bash build/prebuilts_download.sh

看你网速快慢,我这大概20分钟,下载完成后会多出 openharmony_prebuilts 文件夹

5、编译源码

./build.sh --product-name rk3568 --ccache

第一次编译前面阶段 2W 多个文件,后面阶段 6W 多个文件,整体时间看你电脑性能。

编译中间要是出错看提示就行,少啥就自行安装,比如这种

default-jdk is not installed. please install it.

mtd-utils is not installed. please install it.

scons is not installed. please install it.

gcc-arm-none-eabi is not installed. please install it.

gcc-arm-linux-gnueabi is not installed. please install it.

Missing dependencies, please check!

[OHOS INFO] Set cache size limit to 100.0 GB

[OHOS INFO] generated build prop info to /hom

6、烧写固件(劝你先别烧写)

最终固件路径 out\rk3568\packages\phone\images\

里面文件和普通 rk3568 aosp 项目大差不差,所以烧写方式也是一样的。

打开 rk 烧写工具,选择加载配置,选择 images 里的 config.cfg,把对应的 image

都重新选择替换就行。想要看图的可以去参考这个OpenHarmony 入门

7、内核及驱动移植

没有任何修改编译后的固件肯定是无法在板子上跑起来的,我这里修改了很长时间也没有搞定,这里记录一下一些心得。

主要原因就是内核版本差异太大,无法平行移植,有些小伙伴直接整个 kernel 替换了就成功跑起来了。

3568 有些版本的内核用的是 4.19(我这里就是),而最新的 harmony 都是基于 5.10 了

harmony 内核源码修改路径 out\kernel\src_tmp\linux-5.10

你要是内核版本一致就可以直接替换所有文件,然后稍微修改一下如下编译规则文件

out\kernel\src_tmp\linux-5.10\make-ohos.sh

替换原有 TOYBRICK_DTB 为你自己的,比如 MY3568_DTB, 总共 4 处修改

java 复制代码
#!/bin/bash

set -e

SCRIPTPATH=$(dirname $realpath "$0")
export PATH=$(realpath $SCRIPTPATH/../../../../)/prebuilts/clang/ohos/linux-x86_64/llvm/bin/:$(realpath $SCRIPTPATH/../../../../)/prebuilts/develop_tools/pahole/bin/:$PATH
export PRODUCT_PATH=vendor/hihope/rk3568
IMAGE_SIZE=64  # 64M
IMAGE_BLOCKS=4096
ENABLE_LTO_O0=${3}

CPUs=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
MAKE="make LLVM=1 LLVM_IAS=1 CROSS_COMPILE=aarch64-linux-gnu-"
[ "${ENABLE_LTO_O0}" == "enable_lto_O0" ] && MAKE="${MAKE} KCFLAGS=-Wl,--lto-O0"
BUILD_PATH=boot_linux
EXTLINUX_PATH=${BUILD_PATH}/extlinux
EXTLINUX_CONF=${EXTLINUX_PATH}/extlinux.conf
# TOYBRICK_DTB=toybrick.dtb
MY3568_DTB=rk3568-evb1-ddr4-v10.dtb
if [ ${KBUILD_OUTPUT} ]; then
	OBJ_PATH=${KBUILD_OUTPUT}/
fi

ID_MODEL=1
ID_ARCH=2
ID_UART=3
ID_DTB=4
ID_IMAGE=5
ID_CONF=6
model_list=(
	"TB-RK3568X0   arm64 0xfe660000 rk3568-toybrick-x0-linux  Image rockchip_linux_defconfig"
	"TB-RK3568X10  arm64 0xfe660000 rk3568-toybrick-x10-linux Image rockchip_linux_defconfig"
	"MYBOARD3568   arm64 0xfe660000 rk3568-evb1-ddr4-v10 Image rockchip_linux_defconfig"
)


function help()
{
	echo "Usage: ./make-ohos.sh {BOARD_NAME}"
	echo "e.g."
	for i in "${model_list[@]}"; do
		echo "  ./make-ohos.sh $(echo $i | awk '{print $1}')"
	done
}


function make_extlinux_conf()
{
	dtb_path=$1
	uart=$2
	image=$3
	
	echo "label rockchip-kernel-5.10" > ${EXTLINUX_CONF}
	echo "	kernel /extlinux/${image}" >> ${EXTLINUX_CONF}
	# echo "	fdt /extlinux/${TOYBRICK_DTB}" >> ${EXTLINUX_CONF}
	echo "	fdt /extlinux/${MY3568_DTB}" >> ${EXTLINUX_CONF}
	cmdline="append earlycon=uart8250,mmio32,${uart} root=PARTUUID=614e0000-0000-4b53-8000-1d28000054a9 rw rootwait rootfstype=ext4"
	echo "  ${cmdline}" >> ${EXTLINUX_CONF}
}

function make_kernel_image()
{
	arch=$1
	conf=$2
	dtb=$3
	
	if [ "$GPUDRIVER" == "mesa3d" ]; then 
		config_base="arch/${arch}/configs/${conf}"
		config_frag="../../../../device/soc/rockchip/panfrost.config"
		ARCH=${arch} ./scripts/kconfig/merge_config.sh ${config_base} ${config_frag}
	else
		${MAKE} ARCH=${arch} ${conf}
	fi

	if [ $? -ne 0 ]; then
		echo "FAIL: ${MAKE} ARCH=${arch} ${conf}"
		return -1
	fi

	${MAKE} ARCH=${arch} ${dtb}.img -j${CPUs}
	if [ $? -ne 0 ]; then
		echo "FAIL: ${MAKE} ARCH=${arch} ${dtb}.img"
		return -2
	fi

	return 0
}

function make_ext2_image()
{
	blocks=${IMAGE_BLOCKS}
	block_size=$((${IMAGE_SIZE} * 1024 * 1024 / ${blocks}))

	if [ "`uname -m`" == "aarch64" ]; then
		echo y | sudo mke2fs -b ${block_size} -d boot_linux -i 8192 -t ext2 boot_linux.img ${blocks}
	else
		genext2fs -B ${blocks} -b ${block_size} -d boot_linux -i 8192 -U boot_linux.img
	fi

	return $?
}

function make_boot_linux()
{
	arch=${!ID_ARCH}
	uart=${!ID_UART}
	dtb=${!ID_DTB}
	image=${!ID_IMAGE}
	conf=${!ID_CONF}
	if [ ${arch} == "arm" ]; then
		dtb_path=arch/arm/boot/dts
	else
		dtb_path=arch/arm64/boot/dts/rockchip
	fi

	rm -rf ${BUILD_PATH}
	mkdir -p ${EXTLINUX_PATH}

	make_kernel_image ${arch} ${conf} ${dtb}
	if [ $? -ne 0 ]; then
		exit 1
	fi
	make_extlinux_conf ${dtb_path} ${uart} ${image}
	cp -f ${OBJ_PATH}arch/${arch}/boot/${image} ${EXTLINUX_PATH}/
	# cp -f ${OBJ_PATH}${dtb_path}/${dtb}.dtb ${EXTLINUX_PATH}/${TOYBRICK_DTB}
	cp -f ${OBJ_PATH}${dtb_path}/${dtb}.dtb ${EXTLINUX_PATH}/${MY3568_DTB}
	cp -f logo*.bmp ${BUILD_PATH}/
	if [ "enable_ramdisk" != "${ramdisk_flag}" ]; then
		make_ext2_image
	fi
}

ramdisk_flag=$2
found=0
for i in "${model_list[@]}"; do
	if [ "$(echo $i | awk '{print $1}')" == "$1" ]; then
		make_boot_linux $i
		found=1
	fi
done

device\board\hihope\rk3568\kernel\build_kernel.sh

修改内核编译指定使用刚刚的 MYBOARD3568,替换原来 TB-RK3568X0

java 复制代码
# eval $MAKE_OHOS_ENV ./make-ohos.sh TB-RK3568X0 $RAMDISK_ARG ${ENABLE_LTO_O0}
eval $MAKE_OHOS_ENV ./make-ohos.sh MYBOARD3568 $RAMDISK_ARG ${ENABLE_LTO_O0}

再次执行 ./build.sh --product-name rk3568 --ccache 重新编译

8、编译错误举例

错误1、内核版本差异,dts 文件属性不兼容,出现在 rk3568-dram-default-timing.dtsi 中 260 行,去修改注释即可

[OHOS ERROR] DTC arch/arm64/boot/dts/rockchip/rk3568-evb1-ddr4-v10.dtb

[OHOS ERROR] Error: /home/ubuntu/code/OpenHarmony4.0/out/kernel/src_tmp/linux-5.10/arch/arm64/boot/dts/rockchip/rk3568-dram-default-timing.dtsi:260.27-28 syntax error

[OHOS ERROR] FATAL ERROR: Unable to parse input tree

[OHOS ERROR] make[3]: *** [scripts/Makefile.lib:329: arch/arm64/boot/dts/rockchip/rk3568-evb1-ddr4-v10.dtb] Error 1

[OHOS ERROR] make[2]: *** [/home/ubuntu/code/OpenHarmony4.0/out/kernel/src_tmp/linux-5.10/Makefile:1405: rockchip/rk3568-evb1-ddr4-v10.dtb] Error 2

[OHOS ERROR] make[2]: *** Waiting for unfinished jobs...

[OHOS ERROR] make[1]: *** [arch/arm64/Makefile:208: rk3568-evb1-ddr4-v10.img] Error 2

[OHOS ERROR] make[1]: Leaving directory '/home/ubuntu/code/OpenHarmony4.0/out/kernel/OBJ/linux-5.10'

[OHOS ERROR] make: *** [Makefile:185: __sub-make] Error 2

错误2、修改的配置文件有问题

[OHOS ERROR] FAILED: .../kernel/src_tmp/linux-5.10/boot_linux .../kernel/checkpoint/compile_check

[OHOS ERROR] /usr/bin/env .../.../device/board/hihope/rk3568/kernel/build_kernel.sh .../.../kernel/linux/linux-5.10 /home/ubuntu/code/OpenHarmony4.0/out/rk3568/packages/phone/images /home/ubuntu/code/OpenHarmony4.0/device/board/hihope/rk3568 vendor/hihope/rk3568 /home/ubuntu/code/OpenHarmony4.0 rockchip rk3568 hihope root default disable_lto_O0

device\board\hihope\rk3568\kernel\logo.bmp

device\board\hihope\rk3568\kernel\logo_kernel.bmp

10、一些常见路径

applications\standard 系统核心应用,包括日历、电话、相机、联系人、Setting、SystemUI 等

out\kernel\src_tmp\linux-5.10 最终编译内核源码路径

out\kernel\OBJ\linux-5.10 内核源码编译 obj 路径

kernel\linux\linux-5.10 SDK自带原生 kernel 路径

device\board\hihope\rk3568 编译脚本路径

openharmony kernel 代码构成如下:

①、SDK 自带原生 kernel(kernel\linux\linux-5.10\)

②、openharmony HDF驱动补丁包 hdf.patch(kernel\linux\patches\linux-5.10\rk3568_patch\)

③、方案商需求的内核补丁包 kernel.patch(kernel\linux\patches\linux-5.10\rk3568_patch\)

由1中kernel加上2、3中的patch,组合成最终的kernel代码(out\kernel\src_tmp\linux-5.10)

该工作由 device\board\hihope\rk3568\kernel\build_kernel.sh完成

脚本每次执行会时开头都会删除 out\kernel\src_tmp\linux-5.10 目录,重新组合生成。

相关推荐
沈剑心2 小时前
如何在鸿蒙系统上实现「沉浸式」页面?
前端·harmonyos
Georgewu3 小时前
【HarmonyOS】鸿蒙应用加载读取csv文件
前端·harmonyos
Georgewu3 小时前
【HarmonyOS】 鸿蒙图片或视频保存相册
前端·harmonyos
川石教育9 小时前
鸿蒙开发-ArkTS 中使用 filter 组件
harmonyos·鸿蒙·鸿蒙应用开发·鸿蒙开发·鸿蒙开发培训·arkts语言
李洋-蛟龙腾飞公司9 小时前
HarmonyOS Next 应用元服务开发-分布式数据对象迁移数据权限与基础数据
分布式·华为·harmonyos
Damon小智9 小时前
HarmonyOS NEXT 技术实践-实现音乐服务卡片
华为·harmonyos·鸿蒙·harmonyos next·服务卡片
play_big_knife9 小时前
鸿蒙项目云捐助第十七讲云捐助我的页面上半部分的实现
华为·harmonyos·鸿蒙·云开发·鸿蒙开发·鸿蒙next·华为云开发
枫叶丹415 小时前
【HarmonyOS之旅】HarmonyOS开发基础知识(三)
华为od·华为·华为云·harmonyos
SoraLuna20 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
AORO_BEIDOU1 天前
单北斗+鸿蒙系统+国产芯片,遨游防爆手机自主可控“三保险”
华为·智能手机·harmonyos