在openharmony有个比较麻烦的操作就是需要对内核做小的调整的时候经常需要进行验证,可能需要修改庞大的补丁文件,然后还要全部重新编译整个内核才行,本文推荐一个方式可以用来快速编译验证。

     内核编译是通过调用build_kernel.sh这个脚本来实现的,可以沿着这个思路修改这个脚本直接使用out/kernel/src_tmp内核源码直接重新编译,这样你只需要修改out/kernel/src_tmp下的代码重新编译即可,把复制内核源码,打补丁过程全部绕过,结合之前 linux刷机 的文章通过脚本就可以自动编译刷机的过程。

首先提供脚本如下

fast_build_kernel.sh

使用的时候需要自己去修改OHOS_HOME的路径。

#如果不是rk3568的开发板,就要修改对应的脚本路径
#重启开发板进入loader模式
hdc shell reboot loader
#openharmony的源码路径
export OHOS_HOME=/home/ubuntu/openharmony

#环境变量,这里要根据你开发板编译选项来确定
export USE_CCACHE=1
export LC_NAME=zh_CN.UTF-8
export SOURCE_ROOT_DIR=$OHOS_HOME
export CCACHE_DIR=/home/ubuntu/.ccache
export NODE_HOME=$OHOS_HOME/prebuilts/build-tools/common/nodejs/node-v14.21.1-linux-x64
export CCACHE_LOGFILE=/home/ubuntu/.ccache/ccache.log
export CCACHE_BASEDIR=$OHOS_HOME
export PATH=$OHOS_HOME/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$OHOS_HOME/prebuilts/build-tools/common/nodejs/node-v14.21.1-linux-x64/bin:$OHOS_HOME/prebuilts/build-tools/linux-x86/bin:$OHOS_HOME/prebuilts/python/linux-x86/current//bin:/home/ubuntu/miniconda3/bin:/home/ubuntu/.local/bin:/home/ubuntu/.cargo/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.local/share/JetBrains/Toolbox/scripts

#为了不妨碍已有的编译脚本,这里重新复制原来的编译脚本
#rebuild_kernel.sh为删除原来的复制内核代码和打补丁的脚本,即保持原来的out/src_temp/下的源码为原样
cp rebuild_kernel.sh $OHOS_HOME/device/board/hihope/rk3568/kernel/rebuild_kernel.sh
#复制修改之后的脚本,还是一样的原则保持out/kernel/src_tmp下的源码不变,目前社区的rk3568保持不变,如果你自己有修改就要自己去检查。
cp remake-boot.sh $OHOS_HOME/out/kernel/src_tmp/linux-5.10
cp remake-ohos.sh $OHOS_HOME/out/kernel/src_tmp/linux-5.10

#进入工作目录重新编译内核
cd $OHOS_HOME/out/rk3568
/usr/bin/env ../../device/board/hihope/rk3568/kernel/rebuild_kernel.sh ../../kernel/linux/linux-5.10 $OHOS_HOME/out/rk3568/packages/phone/images $OHOS_HOME/device/board/hihope/rk3568 vendor/hihope/rk3568 $OHOS_HOME rockchip rk3568 hihope root default disable_lto_O0 enable_ramdisk
#刷机
sudo upgrade_tool db $OHOS_HOME/out/rk3568/packages/phone/images/MiniLoaderAll.bin
sudo upgrade_tool di -resource $OHOS_HOME/out/kernel/OBJ/linux-5.10/resource.img
sudo upgrade_tool di -boot_linux $OHOS_HOME/out/kernel/src_tmp/linux-5.10/boot_linux.img
#重启
sudo upgrade_tool rd

remake-ohos.sh

目前社区4.1使用的是和原始make-ohos.sh文件一致

#!/bin/bash

set -ex

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
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"
)


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}
	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} modules_prepare
	if [ $? -ne 0 ]; then
		echo "FAIL: ${MAKE} ARCH=${arch} modules_prepare"
		return -2
	fi

	${MAKE} ARCH=${arch} ${dtb}.img -j2
	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 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

remake-boot.sh

目前社区4.1使用的也是和原始make-boot.sh文件一致

#!/bin/bash

set -ex
BOOT_LINUX=${1}/kernel/src_tmp/linux-5.10
OUT_IMAGE=${1}/rk3568/packages/phone/images/
IMAGE_SIZE=64  # 64M
IMAGE_BLOCKS=4096

BUILD_PATH=boot_linux
EXTLINUX_PATH=${BUILD_PATH}/extlinux
EXTLINUX_CONF=${EXTLINUX_PATH}/extlinux.conf
TOYBRICK_DTB=toybrick.dtb

function make_boot_image()
{
        blocks=${IMAGE_BLOCKS}
        block_size=$((${IMAGE_SIZE} * 1024 * 1024 / ${blocks}))
        echo "blocks = ${blocks}  block_size ${block_size}"
        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 $?
}

cd ${BOOT_LINUX}
make_boot_image
cd -
cp ${BOOT_LINUX}/boot_linux.img ${OUT_IMAGE}

rebuild_kernel.sh

#!/bin/bash

# Copyright (c) 2021-2023 HiHope Open Source Organization .
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

pushd ${1}
ROOT_DIR=${5}
export PRODUCT_PATH=${4}
export DEVICE_COMPANY=${6}
export DEVICE_NAME=${7}
export PRODUCT_COMPANY=${8}
KERNEL_FORM=${9}
KERNEL_PROD=${10}
ENABLE_LTO_O0=${11}

KERNEL_SRC_TMP_PATH=${ROOT_DIR}/out/kernel/src_tmp/linux-5.10
KERNEL_OBJ_TMP_PATH=${ROOT_DIR}/out/kernel/OBJ/linux-5.10
KERNEL_SOURCE=${ROOT_DIR}/kernel/linux/linux-5.10
KERNEL_PATCH_PATH=${ROOT_DIR}/kernel/linux/patches/linux-5.10
KERNEL_PATCH=${ROOT_DIR}/kernel/linux/patches/linux-5.10/rk3568_patch/kernel.patch
BUILD_SCRIPT_PATH=${3}
NEWIP_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/newip/apply_newip.sh
TZDRIVER_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/tzdriver/apply_tzdriver.sh
XPM_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/xpm/apply_xpm.sh
CED_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/container_escape_detection/apply_ced.sh
HIDEADDR_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/memory_security/apply_hideaddr.sh
QOS_AUTH_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/qos_auth/apply_qos_auth.sh
UNIFIED_COLLECTION_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/ucollection/apply_ucollection.sh
CODE_SIGN_PATCH_FILE=${ROOT_DIR}/kernel/linux/common_modules/code_sign/apply_code_sign.sh

HARMONY_CONFIG_PATH=${ROOT_DIR}/kernel/linux/config/linux-5.10
DEVICE_CONFIG_PATH=${ROOT_DIR}/kernel/linux/config/linux-5.10/${DEVICE_NAME}
DEFCONFIG_BASE_FILE=${HARMONY_CONFIG_PATH}/base_defconfig
DEFCONFIG_TYPE_FILE=${HARMONY_CONFIG_PATH}/type/standard_defconfig
DEFCONFIG_FORM_FILE=${HARMONY_CONFIG_PATH}/form/${KERNEL_FORM}_defconfig
DEFCONFIG_ARCH_FILE=${DEVICE_CONFIG_PATH}/arch/arm64_defconfig
DEFCONFIG_PROC_FILE=${DEVICE_CONFIG_PATH}/product/${KERNEL_PROD}_defconfig

RAMDISK_ARG="disable_ramdisk"
MAKE_OHOS_ENV="GPUDRIVER=mali"
export KBUILD_OUTPUT=${KERNEL_OBJ_TMP_PATH}

for i in "$@"
do
    case $i in
        enable_ramdisk)
            RAMDISK_ARG=enable_ramdisk
            ;;
        enable_mesa3d)
            MAKE_OHOS_ENV="GPUDRIVER=mesa3d"
            ;;
    esac
done

cd ${KERNEL_SRC_TMP_PATH}

eval $MAKE_OHOS_ENV ./remake-ohos.sh TB-RK3568X0 $RAMDISK_ARG ${ENABLE_LTO_O0}

mkdir -p ${2}

if [ "enable_ramdisk" != "${12}" ]; then
    cp ${KERNEL_OBJ_TMP_PATH}/boot_linux.img ${2}/boot_linux.img
fi
cp ${KERNEL_OBJ_TMP_PATH}/resource.img ${2}/resource.img
cp ${3}/loader/MiniLoaderAll.bin ${2}/MiniLoaderAll.bin
cp ${3}/loader/uboot.img ${2}/uboot.img

if [ "enable_absystem" == "${14}" ]; then
    cp ${3}/loader/parameter_ab.txt ${2}/parameter_ab.txt
    cp ${3}/loader/config_ab.cfg ${2}/config_ab.cfg
else
    cp ${3}/loader/parameter.txt ${2}/parameter.txt
    cp ${3}/loader/config.cfg ${2}/config.cfg
fi

popd

../kernel/src_tmp/linux-5.10/remake-boot.sh ..

使用方法

只要将这三个脚本放在一个目录下,首先按照正常方法编译rk3568的的代码,编程成功之后,直接在out/kernel/src_tmp下修改内核源码或者配置,修改OHOS_HOME的路径到你自己的代码工程路径下架,插上开发板,直接执行fast_build_kernel.sh,直接完整编译到刷机的过程。如果发现源码没有重新编译,建议删除out/kernel/OBJ/linux-5.10/下的.o文件。rk芯片在resource.img中有dtb,如果没有修改设备树可以不刷这个resource分区。

其他开发板

对于其他类型的开发板就不能直接使用上述脚本,可以按照以下步骤进行操作,操作之前先确保内核能够正常编译通过。

1 修改build_kernel.sh

  请找到自己开发板的build_kernel.sh的脚本,然修改脚本内容,获取编译的参数和环境变量值,内容如下:

2 编译报错

      按照正常编译过程编译,这个时候肯定会报错,如果没有报错删除out/kernel目录再编译,错误内容大致如下。

           

 3 复制脚本编译参数和环境变量

       把脚本的参数和当前的环境变量,筛查出有效值后填入fast_build_kernel.sh。

4 修改脚本

      还原修改之后的build_kernel.sh,复制build_kernel.sh,make-ohos.sh,make-boot.sh,把这个脚本中所有的修改源码的代码全部删除,保证out/kernel/src_tmp不会变化,这样在第一次编译通过之后,就可以同样用fast_build_kernel.sh进行快速编译了。特别是针对那些在社区的脚本做了定制化修改的,务必要把build_kernel.sh,make-ohos.sh,make-boot.sh这三个脚本中修改源码的部分删除,保持out/kernel/src_tmp的源码不变。

    

   

 

 

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐