第二课--OpenWRT 固件定制

来自Microduino Wikipedia
Wp.southwolf@gmail.com讨论 | 贡献2015年4月4日 (六) 03:58的版本 (Customize firmware - build option)
跳转至: 导航搜索

OpenWRT 最新的版本已经提供了 MT7620A 芯片的支持,所以只要是相同芯片的固件,在 MicroWRT 上基本都能直接使用。但由于每款路由器/开发板的接口、配置不尽相同,为了用好 MicroWRT, 还需要对固件进行相应的定制。

本教程基于 OpenWRT 最新版。可以用 git clone git://git.openwrt.org/openwrt.git 获取最新的 OpenWRT 源码。


加入 MicroWRT 硬件描述

参考: 第十三课--MicroWRT_内核树介绍

DTS 是对每一块板上硬件的描述。对于 MicroWRT ,我们需要定制的内容主要包括板卡名称、串口波特率、Flash分区、GPIO、按键、SPI、PCI-E、I2C、网口等硬件接口。将以下内容保存为 target/linux/ramips/dts/MicroWRT.dts

   /dts-v1/;
   
   /include/ "mt7620a.dtsi"
   
   / {
     compatible = "microwrt", "ralink,mt7620a-soc";
     model = "MicroWRT";
   
     chosen {
       bootargs = "console=ttyS0,115200";
     };
   
     palmbus@10000000 {
       gpio2: gpio@660 {
         status = "okay";
       };
   
       gpio3: gpio@688 {
         status = "okay";
       };
   
       spi@b00 {
         status = "okay";
   
         m25p80@0 {
           #address-cells = <1>;
           #size-cells = <1>;
           compatible = "w25q128";
           reg = <0 0>;
           linux,modalias = "m25p80", "w25q128";
           spi-max-frequency = <10000000>;
   
           partition@0 {
             label = "u-boot";
             reg = <0x0 0x30000>;
             read-only;
           };
   
           partition@30000 {
             label = "u-boot-env";
             reg = <0x30000 0x10000>;
             read-only;
           };
   
           factory: partition@40000 {
             label = "factory";
             reg = <0x40000 0x10000>;
             read-only;
           };
   
           partition@50000 {
             label = "firmware";
             reg = <0x50000 0xfb0000>;
           };
         };
       };
     };
   
     ehci@101c0000 {
       status = "okay";
     };
   
     ohci@101c1000 {
       status = "okay";
     };
   
     pcie@10140000 {
       status = "okay";
   
       pcie-bridge {
         mt76@0,0 {
           reg = <0x0000 0 0 0 0>;
           device_type = "pci";
           mediatek,mtd-eeprom = <&factory 0x8000>;
           mediatek,2ghz = <0>;
         };
       };
     };
   
     ethernet@10100000 {
       pinctrl-names = "default";
       pinctrl-0 = <&ephy_pins>;
       mtd-mac-address = <&factory 0x4>;
       ralink,port-map = "llllw";
     };
   
     wmac@10180000 {
       ralink,mtd-eeprom = <&factory 0>;
     };
   
     pinctrl {
       state_default: pinctrl0 {
         default {
           ralink,group = "wled", "i2c", "wdt", "uartf";
           ralink,function = "gpio";
         };
       };
     };
   
     gpio-keys-polled {
       compatible = "gpio-keys-polled";
       #address-cells = <1>;
       #size-cells = <0>;
       poll-interval = <20>;
       reset {
         label = "reset";
         gpios = <&gpio0 1 1>;
         linux,code = <0x198>;
       };
       wps {
         label = "wps";
         gpios = <&gpio0 2 1>;
         linux,code = <0x211>;
       };
     };
   };
   

加入 MicroWRT 编译选项

有了 MicroWRT 的硬件描述,我们只需要通知 OpenWRT 编译工具进行编译,就可以生成适合 MicroWRT 的固件了。修改 target/linux/ramips/image/Makefile 找到

ifeq ($(SUBTARGET),mt7620)
   define Image/Build/Profile/Default

在下面加入

   $(call Image/Build/Profile/MicroWRT,$(1))

然后在这个函数上面加入

   Image/Build/Profile/MicroWRT=$(call BuildFirmware/Default16M/$(1),$(1),microwrt,MicroWRT)

即可在编译时自动生成 MicroWRT 固件。

网络接口定制(VLAN)

参考: 第九课--MicroWRT_VLAN_配置

MT7620 内置了支持 VLAN 的交换机,通过划分不同的 VLAN 就可以实现内外网的切换。MicroWRT 扩展板提供了一个一以太网口,连接到 Port 4。通常我们会使用这个网口连接外网。此时需要修改固件源码中的 /target/linux/ramips/base-files/etc/board.d/02_network。 找到如下段落:

   wt1520 | \
   xiaomi-miwifi-mini |\
   y1)
       ucidef_set_interfaces_lan_wan "eth0.1" "eth0.2"
       ucidef_add_switch "switch0" "1" "1"
       ucidef_add_switch_vlan "switch0" "1" "0 1 2 3 6t"
       ucidef_add_switch_vlan "switch0" "2" "4 6t"
       ;;

这一段就是将 Port 4 配置到 VLAN 2 的代码。 只要把 microwrt 也加进去就好了。

   wt1520 | \
   xiaomi-miwifi-mini | \
   microwrt | \
   y1)
       ucidef_set_interfaces_lan_wan "eth0.1" "eth0.2"
       ucidef_add_switch "switch0" "1" "1"
       ucidef_add_switch_vlan "switch0" "1" "0 1 2 3 6t"
       ucidef_add_switch_vlan "switch0" "2" "4 6t"
       ;;