2 -> openwrt 源码文件构成及作用

如果要在 openWrt 系统上移植设备驱动、增加软件包、修订bsp相关内容时,首先面临的问题是,我们在什么地方找到bsp的设备树文件呢?到哪里修改驱动相关内容呢?我的应用如何打包到镜像中呢?我们带着疑惑来往下看,首先认知 wrt 的源码构成:

第 1 节
配置文件 与 包管理文件;
在这里插入图片描述
make menuconfig 中的部分参数来源此 config 文件夹中配置内容。

第 2 节
include 文件
在这里插入图片描述
注意下图中、文件路径的位置
在这里插入图片描述
从图中的调用关系看出,在package/boot/uboot-layerscape(注:选择目标芯片)/Makefile 文件中,包含了 inclued/u-boot.mk 文件; u-boot.mk 文件为编译通用文件, 而 相关平台下的/Makefile 文件是具体入口配置内容。 例:include/u-boot.mk

PKG_NAME ?= u-boot			# 包名称: u-boot

ifndef PKG_SOURCE_PROTO   
PKG_SOURCE = $(PKG_NAME)-$(PKG_VERSION).tar.bz2  #下载uboot的源码版本及url地址
PKG_SOURCE_URL = \
        https://sources.openwrt.org \
        ftp://ftp.denx.de/pub/u-boot			# uboot 开源地址
endif
# 编译路径存储规则
PKG_BUILD_DIR = $(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_NAME)-$(PKG_VERSION)

PKG_TARGETS := bin
PKG_FLAGS:=nonshared

PKG_LICENSE:=GPL-2.0 GPL-2.0+
PKG_LICENSE_FILES:=Licenses/README

PKG_BUILD_PARALLEL:=1

export GCC_HONOUR_COPTS=s
# uboot 的 安装方式,关键字 install
define Package/u-boot/install/default
        $(CP) $(patsubst %,$(PKG_BUILD_DIR)/%,$(UBOOT_IMAGE)) $(1)/
endef

Package/u-boot/install = $(Package/u-boot/install/default)
# 编译生成内容
define U-Boot/Init
  BUILD_TARGET:=
  BUILD_SUBTARGET:=
  BUILD_DEVICES:=
  NAME:=
  DEPENDS:=
  HIDDEN:=
  DEFAULT:=
  VARIANT:=$(1)
  UBOOT_CONFIG:=$(1)
  UBOOT_IMAGE:=u-boot.bin
  HIDDEN:=
  DEFAULT:=
  VARIANT:=$(1)
  UBOOT_CONFIG:=$(1)
  UBOOT_IMAGE:=u-boot.bin
endef
# 编译依赖条件
TARGET_DEP = TARGET_$(BUILD_TARGET)$(if $(BUILD_SUBTARGET),_$(BUILD_SUBTARGET))
# 编译规则
UBOOT_MAKE_FLAGS = \
        HOSTCC="$(HOSTCC)" \
        HOSTCFLAGS="$(HOST_CFLAGS) $(HOST_CPPFLAGS) -std=gnu11" \
        HOSTLDFLAGS="$(HOST_LDFLAGS)"
#  准备编译环境的相关变量
define Build/U-Boot/Target
  $(eval $(call U-Boot/Init,$(1)))
  $(eval $(call U-Boot/Default,$(1)))
  $(eval $(call U-Boot/$(1),$(1)))
  
 define Package/u-boot-$(1)
    SECTION:=boot
    CATEGORY:=Boot Loaders
    TITLE:=U-Boot for $(NAME)
    VARIANT:=$(VARIANT)
    DEPENDS:=@!IN_SDK $(DEPENDS)
    HIDDEN:=$(HIDDEN)
    ifneq ($(BUILD_TARGET),)
      DEPENDS += @$(TARGET_DEP)
      ifneq ($(BUILD_DEVICES),)
        DEFAULT := y if ($(TARGET_DEP)_Default \
                $(patsubst %,|| $(TARGET_DEP)_DEVICE_%,$(BUILD_DEVICES)) \
                $(patsubst %,|| $(patsubst TARGET_%,TARGET_DEVICE_%,$(TARGET_DEP))_DEVICE_%,$(BUILD_DEVICES)))
      endif
    endif
    $(if $(DEFAULT),DEFAULT:=$(DEFAULT))
    URL:=http://www.denx.de/wiki/U-Boot
  endef
  
  define Package/u-boot-$(1)/install
        $$(Package/u-boot/install)
  endef
endef
# 预编译配置
define Build/Configure/U-Boot
        +$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR) $(UBOOT_CONFIGURE_VARS) $(UBOOT_CONFIG)_config
endef

DTC=$(wildcard $(LINUX_DIR)/scripts/dtc/dtc)

# 编译 uboot
define Build/Compile/U-Boot
        +$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR) \
                CROSS_COMPILE=$(TARGET_CROSS) \
                $(if $(DTC),DTC="$(DTC)") \
                $(UBOOT_MAKE_FLAGS)
endef
# make 缺省参数 , 执行的语句
define BuildPackage/U-Boot/Defaults
  Build/Configure/Default = $$$$(Build/Configure/U-Boot)
  Build/Compile/Default = $$$$(Build/Compile/U-Boot)
endef
# make u-boot ,执行的语句
define BuildPackage/U-Boot
  $(eval $(call BuildPackage/U-Boot/Defaults))
  $(foreach type,$(if $(DUMP),$(UBOOT_TARGETS),$(BUILD_VARIANT)), \
    $(eval $(call Build/U-Boot/Target,$(type)))
  )
  $(eval $(call Build/DefaultTargets))
  $(foreach type,$(if $(DUMP),$(UBOOT_TARGETS),$(BUILD_VARIANT)), \
    $(call BuildPackage,u-boot-$(type))
  )
endef

第 3 节
package 文件夹下内容, openwrt 包源码是在编译时下载的,此文件夹下只有部分基础包的源码文件,具体内容如下:

|-- package
|   |-- Makefile					# 顶层 makefile 文件
|   |-- base-files
|   |   |-- Makefile				# base-file 文件夹下的 makefile
|   |   |-- files                   ## 包含文件夹: etc/ sbin/ bin/ lib/ usr/ 相关内容,rootfs基础文件内容
|   |   `-- image-config.in
|   |-- boot						# 各 目标主机的 uboot 包文件,3 节的实例文件就在此文件夹下
|   |   |-- arm-trusted-firmware-mvebu
|   |   |-- arm-trusted-firmware-rockchip
|   |   |-- arm-trusted-firmware-sunxi
|   |   |-- at91bootstrap
|   |   |-- fconfig
|   |   |-- grub2
|   |   |-- imx-bootlets
|   |   |-- kexec-tools
|   |   |-- kobs-ng
|   |   |-- mt7623n-preloader
|   |   |-- tfa-layerscape
|   |   |-- uboot-at91
|   |   |-- uboot-envtools
|   |   |-- uboot-fritz4040
|   |   |-- uboot-imx6
|   |   |-- uboot-kirkwood
|   |   |-- uboot-lantiq
|   |   |-- uboot-layerscape	# LS1046A 目标机
|   |   |-- uboot-mediatek
|   |   |-- uboot-mvebu
|   |   |-- uboot-mxs
|   |   |-- uboot-omap
|   |   |-- uboot-oxnas
|   |   |-- uboot-ramips
|   |   |-- uboot-rockchip
|   |   |-- uboot-sunxi
|   |   |-- uboot-tegra
|   |   `-- uboot-zynq
|   |-- devel					# 调试 工具相关 配置文件
|   |   |-- binutils
|   |   |-- gdb
|   |   |-- perf
|   |   |-- strace
|   |   |-- trace-cmd
|   |   `-- valgrind
|   |-- feeds                   # 4 个 组件包集合内容
|   |   |-- freifunk            # 此文件是 master 版本的内容,而非 19.07 内容。
|   |   |-- luci
|   |   |-- packages
|   |   |-- routing
|   |   `-- telephony
|   |-- firmware                # 系统引入 firmware 包内容
|   |   |-- amd64-microcode
|   |   |-- ath10k-ct-firmware
|   |   |-- b43legacy-firmware
|   |   |-- cypress-firmware
|   |   |-- cypress-nvram
|   |   |-- intel-microcode
|   |   |-- ipq-wifi
|   |   |-- lantiq
|   |   |-- layerscape
|   |   |-- linux-firmware
|   |   |-- prism54-firmware
|   |   `-- wireless-regdb
|   |-- kernel                   #内核 引入的模块 相关配置
|   |   |-- acx-mac80211
|   |   |-- ath10k-ct
|   |   |-- bcm27xx-gpu-fw
|   |   |-- bcm63xx-cfe
|   |   |-- broadcom-wl
|   |   |-- button-hotplug
|   |   |-- cryptodev-linux
|   |   |-- dtc
|   |   |-- exfat
|   |   |-- gpio-button-hotplug
|   |   |-- gpio-nct5104d
|   |   |-- hwmon-gsc
|   |   |-- lantiq
|   |   |-- linux
|   |   |-- mac80211
|   |   |-- mt76
|   |   |-- mt7621-qtn-rgmii
|   |   |-- mwlwifi
|   |   |-- nat46
|   |   |-- om-watchdog
|   |   |-- rtc-rv5c386a
|   |   |-- rtl8812au-ct
|   |   `-- trelay
|   |-- libs                     # 系统中引用的库文件
|   |   |-- argp-standalone
|   |   |-- elfutils
|   |   |-- gettext
|   |   |-- gettext-full
|   |   |-- gmp
|   |   |-- jansson
|   |   |-- libaudit
|   |   |-- libbsd
|   |   |-- libevent2
|   |   |-- libiconv
|   |   |-- libiconv-full
|   |   |-- libjson-c
|   |   |-- libmnl
|   |   |-- libnetfilter-conntrack
|   |   |-- libnfnetlink
|   |   |-- libnftnl
|   |   |-- libnl
|   |   |-- libnl-tiny
|   |   |-- libpcap
|   |   |-- libselinux
|   |   |-- libsemanage
|   |   |-- libsepol
|   |   |-- libtool
|   |   |-- libubox
|   |   |-- libunwind
|   |   |-- libusb
|   |   |-- mbedtls
|   |   |-- musl-fts
|   |   |-- ncurses
|   |   |-- nettle
|   |   |-- openssl
|   |   |-- pcre
|   |   |-- popt
|   |   |-- readline
|   |   |-- sysfsutils
|   |   |-- toolchain
|   |   |-- uclibc++
|   |   |-- uclient
|   |   |-- ustream-ssl
|   |   |-- wolfssl
|   |   `-- zlib
|   |-- network               #网络相关分类包 内容
|   |   |-- config
|   |   |-- ipv6
|   |   |-- services
|   |   `-- utils
|   |-- system               #系统基础的组件包内容
|   |   |-- ca-certificates
|   |   |-- fstools
|   |   |-- fwtool
|   |   |-- iucode-tool
|   |   |-- mtd
|   |   |-- openwrt-keyring
|   |   |-- opkg
|   |   |-- procd
|   |   |-- refpolicy
|   |   |-- rpcd
|   |   |-- selinux-policy
|   |   |-- ubox
|   |   |-- ubus
|   |   |-- ucert
|   |   |-- uci
|   |   |-- urandom-seed
|   |   |-- urngd
|   |   |-- usign
|   |   `-- zram-swap
|   `-- utils               #其他综合包 相关内容
|       |-- adb                      # adb 工具
|       |-- bcm27xx-userland
|       |-- bsdiff
|       |-- busybox                 # busybox
|       |-- bzip2
|       |-- checkpolicy
|       |-- ct-bugcheck
|       |-- e2fsprogs
|       |-- f2fs-tools
|       |-- fbtest
|       |-- fritz-tools
|       |-- jboot-tools
|       |-- jsonfilter
|       |-- lua
|       |-- lua5.3                     # 脚本语言 lua
|       |-- mdadm
|       |-- mtd-utils
|       |-- nvram
|       |-- osafeloader
|       |-- oseama
|       |-- otrx
|       |-- policycoreutils
|       |-- px5g-mbedtls
|       |-- px5g-wolfssl
|       |-- ravpower-mcu
|       |-- secilc
|       |-- spidev_test
|       |-- ugps
|       |-- usbmode
|       |-- usbreset
|       |-- usbutils                   
|       `-- util-linux

从此部分内容需特别关注: rootfs 文件夹配置内容如下:
package/base-files$ tree -L 2

|-- Makefile
|-- files
|   |-- bin
|   |-- etc
|   |-- lib
|   |-- rom
|   |-- sbin
|   `-- usr
`-- image-config.in

第 4 节
target 存放用于各类平台相关的配置,定义固件和内核的具体过程;如:
在这里插入图片描述
进一步查看平台相关的配置文件内容, layerscape 文件夹下内容:
在这里插入图片描述
此处需要注意 LS1046 补丁文件,根据 board 型号编译后后;下载源码、打 LS1046 设备树补丁文件;
路径如下:

设备树路径

build_dir/target-aarch64_generic_musl/linux-layerscape_armv8_64b/linux-5.4.83/arch/arm64/boot/dts/freescale
检索 ls fsl-ls1046* -l 内容如下

-rw-r--r-- 1 robot robot   825 Dec 11 20:23 fsl-ls1046-post.dtsi
-rw-r--r-- 1 robot robot 31528 Dec 24 12:42 fsl-ls1046a-frwy-sdk.dtb
-rw-r--r-- 1 robot robot  2348 Dec 24 11:40 fsl-ls1046a-frwy-sdk.dts
-rw-r--r-- 1 robot robot 32763 Dec 24 12:42 fsl-ls1046a-frwy-usdpaa.dtb
-rw-r--r-- 1 robot robot  2678 Dec 24 11:40 fsl-ls1046a-frwy-usdpaa.dts
-rw-r--r-- 1 robot robot 29852 Dec 24 12:42 fsl-ls1046a-frwy.dtb
-rw-r--r-- 1 robot robot  2596 Dec 24 11:40 fsl-ls1046a-frwy.dts
-rw-r--r-- 1 robot robot 34317 Dec 24 12:42 fsl-ls1046a-qds-sdk.dtb
-rw-r--r-- 1 robot robot  4140 Dec 24 11:40 fsl-ls1046a-qds-sdk.dts
-rw-r--r-- 1 robot robot 32757 Dec 24 12:42 fsl-ls1046a-qds.dtb
-rw-r--r-- 1 robot robot  5730 Dec 24 11:40 fsl-ls1046a-qds.dts
-rw-r--r-- 1 robot robot 31556 Dec 24 12:42 fsl-ls1046a-rdb-sdk.dtb
-rw-r--r-- 1 robot robot  4217 Dec 24 11:40 fsl-ls1046a-rdb-sdk.dts
-rw-r--r-- 1 robot robot 32959 Dec 24 12:42 fsl-ls1046a-rdb-usdpaa.dtb
-rw-r--r-- 1 robot robot  3263 Dec 24 11:40 fsl-ls1046a-rdb-usdpaa.dts
-rw-r--r-- 1 robot robot 29948 Dec 24 12:42 fsl-ls1046a-rdb.dtb       #生成烧写文件
-rw-r--r-- 1 robot robot  2924 Dec 24 11:40 fsl-ls1046a-rdb.dts       #源文件
-rw-r--r-- 1 robot robot 24196 Dec 24 11:40 fsl-ls1046a.dtsi

第 5 节
scripts为编译工具脚本文件,列出重要的脚本及功能
例如patch-kernel.sh封装了patch命令,在编译时,首先将 patches 目录下的所有补丁文件打上,并且判断如果打补丁失败将退出编译过程
download.pl 为下载源代码的工具脚本,封装下载工具 wget 的选项以及设置从哪里下载

如下表:

脚 本 文 件 含 义
scripts/download.pl 下载编译软件包源代码
scripts/patch-kernel.sh 打补丁脚本,并且判断如果打补丁失败将退出编译过程
scripts/feeds 收集扩展软件包的工具,用于下载和安装编译扩展软件包工具
scripts/diffconfig.sh 收集和默认配置不同之处的工具
scripts/kconfig.pl 处理内核配置
scripts/deptest.sh 自动 OpenWrt 的软件包依赖项检查
scripts/metadata.pl 检查 metadata
scritps/rstrp.sh 丢弃目标文件中的符号,这样就将执行文件和动态库变小
scripts/timestamp.pl 生成文件的时间戳
scripts/ipkg-make-index.sh 生成软件包的 ipkg 索引,在使用 opkg 安装软件时使用
scripts/ext-toolchain.sh 工具链
scripts/strip-kmod.sh 删除内核模块的符号信息,使文件变小

第 6 节
toolchain 编译工具链生成配置规则,如下:

├── binutils
│   ├── Config.in
│   ├── Config.version
│   ├── Makefile
│   └── patches
├── Config.in
├── fortify-headers
│   └── Makefile
├── gcc
│   ├── common.mk
│   ├── Config.in
│   ├── Config.version
│   ├── exclude-testsuite
│   ├── files
│   ├── final
│   ├── initial
│   ├── minimal
│   └── patches
├── gdb
│   ├── Makefile
│   └── patches
├── glibc
│   ├── common.mk
│   ├── headers
│   ├── include
│   ├── Makefile
│   └── patches
├── info.mk
├── kernel-headers
│   └── Makefile
├── Makefile
├── musl
│   ├── common.mk
│   ├── Config.in
│   ├── include
│   ├── Makefile
│   └── patches
├── nasm
│   └── Makefile
├── uClibc
│   ├── common.mk
│   ├── config
│   ├── Config.in
│   ├── headers
│   ├── Makefile
│   └── utils
└── wrapper
    └── Makefile

第 7 节
tools 为系统的工具包内容;如图:
在这里插入图片描述
第 8 节
小结:openwrt 源码解压后文件夹内容:

openwrt-master$ ls -l
total 84
-rw-rw-r--  1 laoli laoli   179 Dec 17 05:11 BSDmakefile
drwxrwxr-x  2 laoli laoli  4096 Dec 17 05:11 config
-rw-rw-r--  1 laoli laoli   576 Dec 17 05:11 Config.in
-rw-rw-r--  1 laoli laoli   579 Dec 17 05:11 feeds.conf.default
drwxrwxr-x  3 laoli laoli  4096 Dec 17 05:11 include
-rw-rw-r--  1 laoli laoli 18092 Dec 17 05:11 LICENSE
-rw-rw-r--  1 laoli laoli  3999 Dec 17 05:11 Makefile
drwxrwxr-x 11 laoli laoli  4096 Dec 17 05:11 package
-rw-rw-r--  1 laoli laoli  3460 Dec 17 05:11 README.md
-rw-rw-r--  1 laoli laoli 13733 Dec 17 05:11 rules.mk
drwxrwxr-x  4 laoli laoli  4096 Dec 17 05:11 scripts
drwxrwxr-x  6 laoli laoli  4096 Dec 17 05:11 target
drwxrwxr-x 12 laoli laoli  4096 Dec 17 05:11 toolchain
drwxrwxr-x 61 laoli laoli  4096 Dec 17 05:11 tools

参考资源:
OpenWrt官方网站(https://openwrt.org/)
江南、董少博客:
https://dongshao.blog.csdn.net/article/details/102509299

猜你喜欢

转载自blog.csdn.net/weixin_38387929/article/details/111723748