OpenWrt编译自定义固件(更新21.02)

1、编译环境搭建

OpenWrt官网
https://openwrt.org/

OpenWrt源码地址——有基本的编译步骤
https://github.com/openwrt/openwrt

*需要注意的是:
22.03以上版本取消了iptables作为默认防火墙

18.06依赖库安装
binutils bzip2 diff find flex gawk gcc-6+ getopt grep install libc-dev libz-dev
make4.1+ perl python3.6+ rsync subversion unzip which

21.02依赖库安装

binutils bzip2 diff find flex gawk gcc-6+ getopt grep install libc-dev libz-dev
make4.1+ perl python3.7+ rsync subversion unzip which

获取源码

git clone -b openwrt-18.06 https://github.com/openwrt/openwrt

git clone -b openwrt-22.07 https://github.com/openwrt/openwrt

其中-b后参数为代码版本,根据branches和tags进行指定版本拉取

*建议直接github网页中选择对应版本下载

2、自定义固件

以编译mt7621为例,操作目录为

./target/linux/ramips/

其他平台类似

2.1添加机型mymodle.dts文件

文件地址:
./target/linux/ramips/dts/mymodle.dts

./target/linux/ramips/dts/mt7621-mymodle.dts

*dts文件前加上对应板子的前缀,这里是"mt7621-"

复制已有的型号dts为mymodle.dts并根据硬件参数修改,要特别注意以下参数

compatible = "mycompany,mymodle", "mediatek,mt7621-soc";

model = "MyModle";

chosen {
		bootargs = "console=ttyS0,115200";
	};

*找你所用的板子抄一下

其中:
mycompany为生产厂商
mymodle为生产型号
MyModle为系统中显示的设备型号
console=ttyS0,115200为串口波特率设置

2.2、添加设备编译mt7621.mk文件

文件地址:

./target/linux/ramips/image/mt7621.mk

添加设备:

define Device/mymodle
  DTS := mydts
  IMAGE_SIZE := $(ralink_default_fw_size_16M)
  DEVICE_TITLE := MYCOMPANY MYMODLE
  DEVICE_PACKAGES := \
	kmod-ata-core kmod-ata-ahci kmod-mt76x2 kmod-sdhci-mt7620 kmod-usb3 \
	kmod-usb-ledtrig-usbport wpad-mini
endef
TARGET_DEVICES += mymodle

*找你所用的板子抄一下

2.2、添加升级支持

为网页升级添加支持,防止出现以下错误

不支持所上传的映像文件格式,请选择适合当前平台的通用映像文件。

修改platform.sh文件

文件地址:

./target/linux/ramips/base-files/lib/upgrade/platform.sh

添加机型:

*"MyModle")
	name="mymodle"
	;;

Mymodlemymodle需与2.1中提到的参数对应

修改ramips.sh文件

文件地址:

./target/linux/ramips/base-files/lib/ramips.sh

添加设备:

case "$board" in
	mymodle|\

mymodle需与2.1中提到的参数对应

3、添加系统文件

固化自己的文件如:配置文件、脚本、程序等到系统中
在编译目录新建文件夹

mkdir ./files

此文件夹的文件将会固化到系统文件根目录

注意:编译前需要为文件赋予权限,否则文件权限不够,固化后系统无法执行

chmod 777 ./files/*

4、编译

4.1、更新插件库

./scripts/feeds update -a
./scripts/feeds install -a

4.2、编译设置

执行编译配置:

make menuconfig

选择所需要安装的插件,建议的基本插件如下:

Libraries
	libuci-lua

Luci
	Collections
		luci
	Modules
		translations
			chinese
Network
	SSH
		openssh-sftp-server

默认编译配置文件为

.config

你也可以备份编译配置为

myconf.config

开始编译,执行:

make -j2 V=s

其中-j2为系统核心数,可以加快编译速度;V=s为显示编译日志。

最后生成的固件在目录

./bin/targets/ramips/mt7621/

5、升级Uboot

除了在uboot启动界面升级
可以在编译时,将dts的uboot分区只读属性去掉

partition@0 {
	label = "u-boot";
	reg = <0x0 0x30000>;
	read-only;##将readonly去掉
};

编译完成后,将uboot.bin文件传入/tmp目录下
执行

mtd -r write /tmp/uboot.bin u-boot