最近在将 OpenWRT 移植到一个商业无线路由器,中间遇到的一些问题,在此记录下。
首先是商业路由器和对应的小米路由器的硬件列表:
硬件 | 商业路由器 | Xiaomi Router 3 Pro | Xiaomi MiWiFi 3G v2 |
---|---|---|---|
CPU | mt7621 | mt7621 | mt7621 |
无线模块 | mt7615e | mt7615e | MT7603E MT7612E |
switch | mt7621内置 (mt7530) | mt7621内置 (mt7530) | mt7621内置 (mt7530) |
Flash | w25q128 (16M bytes) | Nand 256M Bytes | Nor 16M Bytes |
内存 | 256M bytes | 512M bytes | 128M bytes |
串口波特率 | 57600 | 115200 | 115200 |
USB | 无 | 有 | 无 |
最重要的是 cpu + 无线模块,需要找到一款硬件类似并支持 OpenWRT 的路由器,在此基础上进行移植,这样工作量最低。我选了小米路由器3 Pro(该路由器已停产,可以买二手的),CPU 和无线模块是相同的。另外选了小米 3G V2,Flash 比较相似。
查看 OpenWRT 所有设备的 WLAN信息可查阅这个链接 https://openwrt.org/toh/views/toh_admin_wlandriver,通过该信息可以找到与自己设备相类似的 OpenWRT 路由器。
移植主要是两个步骤:
首先通过 uboot 下载 initramfs-kernel.bin 文件,可以调试大多数的硬件移植。(网口、无线部分)
网口移植好后,通过 initramfs-kernel.bin 的网页,升级 squashfs-sysupgrade.bin 文件,调试升级。
移植一:波特率
商业路由器的波特率是57600,通过 tftp 下载运行 openwrt-ramips-mt7621-xiaomi_mi-router-3-pro-initramfs-kernel.bin,串口由于波特率问题,变成乱码,修改下面的文件:target/linux/ramips/dts/mt7621_xiaomi_mi-router-3-pro.dts
- bootargs = "console=ttyS0,115200n8";
+ bootargs = "console=ttyS0,57600n8";
波特率即完成移植。(详细步骤见我的另一篇博客文章OpenWRT 设备修改串口波特率)
移植二:Flash
Flash 不同于 Xiaomi route 3 pro 而类似于 Xiaomi MiWiFi 3G v2,需要将 Nand的 flash 的硬件去掉。即删除 target/linux/ramips/dts/mt7621_xiaomi_mi-router-3-pro.dts 中的 nand 部分。
&nand {
status = "okay";
partitions {
......
};
};
添加 Nor Flash 部分:
+&spi0 {
+ status = "okay";
+ flash@0 {
+ compatible = "w25q128", "jedec,spi-nor";
+ reg = <0>;
+ spi-max-frequency = <10000000>;
+
+ partitions {
+ compatible = "fixed-partitions";
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ partition@0 {
+ label = "Bootloader";
+ reg = <0x0 0x30000>;
+ read-only;
+ };
+
+ partition@30000 {
+ label = "Config";
+ reg = <0x30000 0x10000>;
+ read-only;
+ };
+
+ factory: partition@40000 {
+ label = "Factory";
+ reg = <0x40000 0x10000>;
+ read-only;
+ };
+
+ partition@50000 {
+ compatible = "denx,uimage";
+ label = "firmware";
+ reg = <0x50000 0xfb0000>;
+ };
+ };
+ };
+ };
非常重要的一点是:在移植前,先查看下商业路由器串口打印的 MTD 信息。Flash 的内容要和商业路由器的 MTD 信息一致。Booter、配置、无线校准数据有三个独立的分区,位置和大小不要随意修改。
另外,"Factory" 分区是无线校准部分的数据(包含 mac 地址),所以在 “partition” 前有添加 "factory: "。这部分出错的话,无线部分即使正常工作,性能也会有影响。
移植三:Switch 部分
MT7621 有两个千兆口,一个一般用来做 WAN 口,另一个接 switch 做 LAN 口。而商业路由器只用了一个千兆口接 switch,将 switch 的两个口,一个做 WAN,一个做 LAN(牺牲带宽降成本的做法)。
需要将一个千兆口删除掉:
-&gmac1 {
- status = "okay";
- label = "wan";
- phy-handle = <ðphy4>;
-
- nvmem-cells = <&macaddr_factory_e000>;
- nvmem-cell-names = "mac-address";
-};
另一个配置成 lan wan 两个口:
&switch0 {
ports {
port@1 {
status = "okay";
label = "lan1";
};
port@0 {
status = "okay";
label = "wan";
nvmem-cells = <&macaddr_factory_e000>;
nvmem-cell-names = "mac-address";
};
};
};
移植四:无线部分
这部分小米 3 Pro 和商业路由器是一致的,无需修改。(商业路由器使用的是 MTK 某个版本的SDK(非OpenWRT),但产测的数据结构是相同的)
移植五:升级配置
小米的使用 NAND,商业路由器使用 NOR,NAND Flash 的 kernel 分区是单独的,NOR Flash 的kenel 和 rootfs 是拼接在一起的,因此升级文件的制作和升级过程是不一样的。(如果把小米 3 Pro 的 sysupgrade.bin 文件后缀修改为 tar,可以解压成三个文件,但小米 Route 3G V2 是不行的)
如果这部分没有修改,升级后,启动串口会打印
## Booting image at bc050000 ...
Bad Magic Number,73797375
需要修改的两个文件即可:
target/linux/ramips/image/mt7621.mk
define Device/xiaomi_mi-router-3-pro
- $(Device/nand)
+ $(Device/dsa-migration)
$(Device/uimage-lzma-loader)
- IMAGE_SIZE := 255488k
+ IMAGE_SIZE := 14848k
DEVICE_VENDOR := Xiaomi
DEVICE_MODEL := Mi Router 3 Pro
- IMAGES += factory.bin
- IMAGE/factory.bin := append-kernel | pad-to $$(KERNEL_SIZE) | append-ubi | \
- check-size
DEVICE_PACKAGES := kmod-mt7615-firmware kmod-usb3 kmod-usb-ledtrig-usbport
SUPPORTED_DEVICES += xiaomi,mir3p
endef
target/linux/ramips/mt7621/base-files/lib/upgrade/platform.sh
@@ -111,7 +111,6 @@ platform_do_upgrade() {
tplink,ec330-g5u-v1|\
wifire,s1500-nbn|\
xiaomi,mi-router-3g|\
- xiaomi,mi-router-3-pro|\
xiaomi,mi-router-4|\
xiaomi,mi-router-ac2100|\
xiaomi,mi-router-cr6606|\
这两个文件的修改思路,就是比较小米 3 Pro 和小米 3G V2 的这部分的差别,修改成小米 3G V2 即可。
总结:移植时,可以借助 OpenWRT 的大量已支持的设备,在此基础上开发,可以大幅减小开发工作量。