本文介绍的方法可以用来为一个定制的底板来制作一个定制的BSP,其中包括定制的内核,设备树,文件系统。

通过增加一个layer的方法来实现

yocto关于layers的文档:http://www.yoctoproject.org/docs/2.1.3/dev-manual/dev-manual.html#understanding-and-creating-layers

这个方法适合于正式生产时使用,且方便管理对整个BSP包的修改,但不适合快速的验证问题与调试,如果需要快速的方法,请直接修改代码。在yocto中查找并修改软件源代码 / yocto how to find and modify the source for software packages

新建layer

layer会是一个包含了所有对BSP做的修改的包。

1、在BSP包中执行. sources/poky/oe-init-build-env导入编译环境,

2、在build目录下新建自定义的layers

bitbake-layers create-layer meta-[custom]                                 [custom]可以改为自定义layer的名字

将meta-custom移动到sources目录下

mv meta-custom ../sources/

3、将自定义layer导入到编译系统中,打开meta-custom/README文档,可以看到以下说明

I. Adding the custom layer to your build
=================================================

--- replace with specific instructions for the custom layer ---

In order to use this layer, you need to make the build system aware of
it.

Assuming the custom layer exists at the top-level of your
yocto build tree, you can add it to the build system by adding the
location of the custom layer to bblayers.conf, along with any
other layers needed. e.g.:

  BBLAYERS ?= " \
    /path/to/yocto/meta \
    /path/to/yocto/meta-poky \
    /path/to/yocto/meta-yocto-bsp \
    /path/to/yocto/meta-custom \
    "

根据说明,编辑build/conf/bblayers.conf,新加meta-custom层

vi build/conf/bblayers.conf

如:

# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

OEROOT := "/home/nicola/work/AM335X/sources/poky"
BBLAYERS  ?= " \
  ${OEROOT}/meta \
  ${OEROOT}/meta-poky \
  ${OEROOT}/../meta-phytec \
  ${OEROOT}/../meta-yogurt \
  ${OEROOT}/../meta-openembedded/meta-oe \
  ${OEROOT}/../meta-openembedded/meta-networking \
  ${OEROOT}/../meta-openembedded/meta-python \
  ${OEROOT}/../meta-openembedded/meta-multimedia \
  ${OEROOT}/../meta-qt5 \
  ${OEROOT}/../meta-openembedded/meta-ruby \
  ${OEROOT}/../meta-custom \
  "

即在BBLAERS加入了meta-custom,执行完这一步骤之后,meta-custom层已经导入到编译系统了,再执行编译的时候meta-custom层的东西都会生效。

新增加自定义machine

增加的machine用于自定义一个设备树文件和在linux中显示的设备名称。

1、在meta-custom/conf目录下新建machine目录,然后加入自定义machine的.conf文件

mkdir -p meta-custom/conf/machine
vi meta-custom/conf/machine/custom-am335x-1.conf

具体.conf 文件写法参考原来phytec machine的配置文件写法

#@TYPE: Machine
#@NAME: custom-am335x-1
#@DESCRIPTION: PHYTEC phyCORE-AM335x 512MiB RAM, 512MiB NAND flash, 8MiB SPI flash, PCM-953 (Kit)
#@ARTICLENUMBERS: KPCM-051-SYS-LINUX, PCM-051-12102F0C.A1
#@SUPPORTEDIMAGE: phytec-qt5demo-image

require conf/machine/include/phyam335x.inc

SOC_FAMILY .= ":phycore-am335x"

# Kernel
KERNEL_DEVICETREE = "custom-am335x-057.dtb"                         #这里设备树可以改成自定义的设备树文件

# Barebox 
BAREBOX_BIN = "images/barebox-am33xx-phytec-phycore.img"
BAREBOX_IPL_BIN = "images/barebox-am33xx-phytec-phycore-mlo-512mb.img"

# resistivetouch, the rdk has one attached
MACHINE_FEATURES += " resistivetouch sgx"

2、将新增加的machine编译进barebox,mlo(AM335X)中,在meta-custom新建recipse-bsp/barebox目录,新建barebox_[版本号].bbappend,barebpx-ipl_[版本号].bbappend(AM335X需要)文件,这个文件名请参考meta-phytec/recipes-bsp/barebox中的名称,版本请在BSP版本、设备树、config文件对照表 / BSP version, device tree, config file cross reference table 中查询,例子:

mkdir -p meta-custom/recipes-bsp/barebox
cd meta-custom/recipes-bsp/barebox
vi barebox_2017.10.0-phy3.bbappend
vi barebox-ipl_2017.10.0-phy3.bbappend

在barebox_2017.10.0-phy3.bb.bbappend和barebox-ipl_2017.10.0-phy3.bbappend都加入下面内容

COMPATIBLE_MACHINE .= "|custom-am335x-1"

3、将新增加的machine编译进内核中,在meta-custom新建recipes-kernel/linux目录,新建linux-[mainline or vendor]_[版本].bbappend文件,这个文件名请参考meta-phytec/recipes-kernel/linux/中的名称,版本请在BSP版本、设备树、config文件对照表 / BSP version, device tree, config file cross reference table 中查询如:

mkdir -p meta-custom/recipes-kernel/linux
cd meta-custom/recipes-kernel/linux
vi linux-mainline_4.9.98-phy1.bbappend

在linux-mainline_4.9.98-phy1.bbappend加入下面内容将自定义machine编译进内核

COMPATIBLE_MACHINE .= "|custom-am335x-1"

内核打补丁

如果需要修改内核代码,则需要通过patch 补丁的方法来修改。

1、进入内核源码目录,对源码进行修改

2、在内核源码目录中执行下列操作,生成补丁

$ git status                   # Show changes files
$ git add <file>               # Add a special file to the staging area
$ git commit -m "custom kernel"   # Creates a commit with a not so useful commit message
$ git format-patch -1 -o ~/    # Creates a patch of the last commit and saves it in your home folder 

3、在meta-custom/recipes-kernel/linux中新建features目录,将生成的补丁复制到features目录下

mkdir -p meta-custom/recipes-kernel/linux/features
cp ~/0001-custom-kernel.patch meta-custom/recipes-kernel/linux/features

4、修改linux-mainline_4.9.98-phy1.bbappend,加入以下内容对内核补丁生效

FILESEXTRAPATHS_prepend := "${THISDIR}/features:"
SRC_URI_append = " \
file://0001-custom-kernel.patch \
"

内核menuconfig更改

以下以am335x的vendor内核 linux-ti 来举例,您的linux内核名称请在 BSP版本、设备树、config文件对照表 / BSP version, device tree, config file cross reference table 中查询。

1、当需要对内核菜单修改时,可以执行以下操作

bitbake linux-ti -c clean
bitbake linux-ti -c menuconfig

2、内核菜单修改后,生成相应修改的内容,这个只是与BSP中自带config的差异,可以用vi等工具打开这个文件确认。

bitbake linux-ti -c diffconfig

 执行这步操作后,会在build/tmp/work/maidean_am335x_1-phytec-linux-gnueabi/linux-ti/4.4.19-phy2-r0.0目录下对更改的配置生成fragment.cfg文件

3、将相应的配置更改文件加入自定义的layer上,

cp build/tmp/work/maidean_am335x_1-phytec-linux-gnueabi/linux-ti/4.4.19-phy2-r0.0/fragment.cfg  meta-custom/recipes-kernel/linux/features/usbserial.cfg  #要更改fragment.cfg的名字

4、修改linux-ti_4.4.141-phy2.bbappend文件,加入更改后的内核菜单配置

FILESEXTRAPATHS_prepend := "${THISDIR}/features:"
SRC_URI_append = " \
file://0001-custom-kernel.patch \
file://usbserial.cfg \
"

文件系统增加firmware文件

一些硬件模块(wifi, 4g模块等)需要把firmware文件放在linux的/lib/firmware, 可参考以下方法在自动义的layer上给文件系统增加firmware文件.

在yocto中, 默认有了linux-firmware的recipe, 以下方法是往linux-firmware的recipe中增加额外的firmware文件. 

1 首先需要在自定义的layer中创建linux-firmware的目录, bbappend文件, 并把firmware文件复制到linux-firmware的目录下

cd meta-custom
mkdir -p recipes-kernel/linux-firmware/linux-firmware/
touch recipes-kernel/linux-firmware/linux-firmware_%.bbappend
cp ~/example-firmware.bin recipes-kernel/linux-firmware/linux-firmware/			 #firmware文件复制到linux-firmware目录下

2 在linux-firmware的bbappend文件中增加以下内容

recipes-kernel/linux-firmware/linux-firmware_%.bbappend
FILESEXTRAPATHS_prepend := "${THISDIR}/linux-firmware:"
SRC_URI += "file://example-firmware.bin"

do_install_append () {
        install -m 0644 ${WORKDIR}/example-firmware.bin ${D}/lib/firmware/example-firmware.bin
}

# NOTE: Use "=+" instead of "+=". Otherwise file is placed into the linux-firmware package.
PACKAGES =+ "${PN}-example"
FILES_${PN}-example = "/lib/firmware/example-firmware.bin"

3 在镜像中安装该firmware的包, 在build/conf/local.conf或者对应image的bbappend文件中添加以下内容

# file local.conf or image recipe
IMAGE_INSTALL += "linux-firmware-example"		# 安装firmware包的名字要与linux-firmware_%.bbappend文件中PACKAGES的配置名字一样

配置systemd服务

yocto提供专门的接口来在bb文件中配置systemd,文档如下

https://docs.yoctoproject.org/ref-manual/classes.html#systemd

可以在yocto的source文件夹找到很多类似的例子,如

http://cgit.openembedded.org/meta-openembedded/tree/meta-webserver/recipes-httpd/sthttpd/sthttpd_2.26.4.bb?h=dizzy

修改barebox的环境变量/增加脚本

在layer中新建一个修改barebox的bbappend文件,一般路径为 recipes-bsp/barebox/barebox_%.bbappend。这个路径需要和meta-phytec中的barebox路径一致。

下面举4个例子,更多的例子可以参考meta-phytec中的barebox_%.bb文件。

python do_env_append() {
    env_rm(d, "config-expansions")                       #删除config-expansions这个配置脚本,该脚本位于barebox的env路径下。
    env_add(d, "nv/linux.bootargs.quiet", "quiet\n")     #增加linux.bootargs.quiet环境变量,并设置值为"quiet"。
    env_add(d, "nv/autoboot_timeout", "0")               #修改autoboot_timeout环境变量,设置值为0。
    env_add(d, "init/initlcd",                           #在env目录中的init目录下,增加initlcd脚本,该脚本开机时会自动执行,下面为脚本内容。
"""#!/bin/sh

#enable int pin
gpio_set_value 152 0
gpio_direction_output 152 1

""")
}

在增加这个bbappend文件后,重新执行bitbake并不会重新编译barebox,需要先

bitbake -c cleanall barebox

后,再编译镜像,才可以将之前对环境变量的修改增加到barebox中。

  • No labels