OpenWrt on IO DATA WN-G300DGR

下記のサイトを参考にして、WN-G300DGR用のOpenWrtをコンパイルしてみた。
https://tetsupc.wordpress.com/2017/11/22/lede-project%E3%82%92wn-g300dgr%E3%81%AB%E5%AF%BE%E5%BF%9C%E3%81%95%E3%81%9B%E3%81%A6%E3%81%BF%E3%82%8B/

ホストとしてArch Linux(2022/05/04)を利用した。
LEDE用であるためか、そのままではコンパイルできなかったので、openwrt v21.02.3のSitecom WL-351にパッチを当ててコンパイルした。
./target/linux/ramips/dts/rt3052_iodata_wn-g300dgr.dts
./target/linux/ramips/image/rt305x.mk
./target/linux/ramips/rt305x/base-files/etc/board.d/02_network

WN-G300DGR用のイメージはでき、TFTPで送信して実行することはできたが、ROMに書き込みためには極端に機能制限して、イメージサイズを圧縮する必要があるため、実用的ではなかった。
; account except root
sudo pacman -S git base-devel unzip wget subversion rsync inetutils python2
git clone https://git.openwrt.org/openwrt/openwrt.git openwrt
cd openwrt
git pull
git branch -a
git checkout v21.02.3
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig
; target, subtarget, profile
make download
; for CPU has 4 cores
make -j5 V=sw

Windows機のrloginでArchLinux機に接続し、sftpを利用して、./bin/targets/ramips/rt305x/openwrt-ramips-rt305x-iodarta_wn-g300dgr-initramfs-kernel.binを転送する。
Windows機でtftpサーバ(tftpd64)を起動する。Directoryを転送したbinファイルのある場所に変更。
Windows機と同じネットワークにルーターをケーブルで接続する。
firmwareを消去した時は、WAN側は接続されていないので必ずLAN側に接続する。
WN-G300DGRを分解して、シリアル接続のピンを基板に半田付けする。USB変換(FTDI232)を介して、ルーターをWindows機にUSB接続。
rloginでシリアルコンソール(57600bps, 8N1)を開いた後に、ルーターを電源オン。u-bootのメッセージが表示されたら、すかさず1を入力すると、プロンプトでる。ルーターのIP(tftpサーバと同じネットワークになるように入力)、Windows機のIP、binファイル名を入力し、initramfs-kernel.binを送って実行する
タイミングによってはエラーになることもあり、そのときは^cで中断して再入力。

実行できたら、LuCI、System、Backup/Flash Firmware、Save mtdblock contentsから、firmware(mtd3)をDownloadして保存しておく。
これを保存しておかないと、メーカーファームに戻れない。
古いバージョンだとLuCIからmtdをバックアップできないので、ssh接続で保存する。
起動時に2を入力してFirmwareを書き込むこともできるが、WebUIからsquashfs-sysupgrade.binをFlash imageしたほうが確実。
起動時にはBad Magic Numberが表示されるが問題ない。

1: Load system code to SDRAM via TFTP.
2: System Load Linux Kernel then write to Flash via TFTP.
4: System Enter Boot Command Line Interface.
5: System Load uCos to SDRAM via TFTP.

CLIのprintenvで環境変数の一覧が出力される。
setenv op_mode 1
saveenv
メーカーfirmwareのブートの際に詳細なログが出力される。

ROMに書き込むためには、Firmwareのサイズを0x3b0000(3866624byte)以下にする必要がある。
https://openwrt.org/docs/guide-user/additional-software/saving_space
USBストレージやluciを使用することは難しい。
RAMが32MBしかないので、パッケージを入れすぎても、ブート時にOut of memory, Kernel panicとなる。
Imageを小さくするための設定。
Global build settings, Enable IPv6 off
Global build settings, Strip unnecessary exports on
Global build settings, Strip unnecessary functions on
Base system, opkg off
LuCI, Modules, luci-mod-admin-full on
LuCI, Applications, luci-app-firewall on
LuCI, Themes, luci-theme-bootstrap on
Network, Firewall, iptables off
Network, Web Servers/Proxies, uhttpd on uhttpd-mod-ubus on
Network, ppp off, odhcpd off

timezoneの初期設定
vi package/base-files/files/bin/config_generate
set system.@system[-1].timezone=’UTC’ -> ‘JST-9’
set system.@system[-1].zonename=’Asia/Tokyo’

wifiの初期設定
vi package/kernel/mac80211/files/lib/wifi/mac80211.sh
config wifi-device
option channel ‘auto’
option htmode ‘HT40’
option txpower ’20’
config wifi-iface
option encryption ‘sae-mixd’

ファイルのやり取りは、scpを利用できる。
CONFIG_DROPBEAR_DBCLINET=y
CONFIG_DROPBEAR_SCP=y

firmwareのバックアップ
cat /dev/mtd3 | ssh user@xx.xx.xx.xx dd of=mtd3

メーカーファームウェアに戻す場合はダウンロードしたイメージをデコード
staging_dir/host/bin/mksenaofw -d iodata.dlf -o iodata.dec
firmwareの前に、0xb0000のブランクブロックを追加。
dd of=dummy if=/dev/zero bs=1 count=720896
cat dummy iodata.dec > iodata.bin
WinSCPで、LAN側に接続したWindows機からbinファイルを/tmpに転送。
mtd write /tmp/iodata.bin firmware
再起動して確認する。
mtd erase firmwareを実行した場合、少し時間がかかる。
書き込みに失敗した場合は、基板へのconsole接続からやり直し。
/tmp以外に転送すると、Out of memoryのエラーで実行できない。
mtd3のバックアップがある場合は、mtd write mtd3.bin firmwareで復旧できる。
hexdup -n 256 /dev/mtd3で確認できる。

openwrtでのパーティション構成は
cat /proc/mtdで確認できる。
dev: size erasesize name
mtd0: 00030000 00010000 “u-boot”        0x00000- 30000
mtd1: 00010000 00010000 “u-boot-env”  0x30000- 40000
mtd2: 00010000 00010000 “factory”        0x40000- 50000
mtd3: 003b0000 00010000 “firmware”     0x50000-400000
もともとのパーティションは、firmwareの前にkernelのようなものがあるが、潰してfirmwareの領域を広げている。
mtd3: 000b0000 00010000 “kernel”        0x050000-100000
mtd4: 00300000 00010000 “firmware”    0x100000-400000

楽天IPv6の接続できるが、ルーターの能力の制限のためか遅い。
ネットワークの接続を変更したら、service network restartで更新。
/etc/config/network
config interface ‘wan’
option proto ‘dslite’
option peeraddr ‘dgw.xpass.jp’
option mtu ‘1460’

config interface ‘wan6’
option device ‘eth0.2’
option proto ‘dhcpv6’

USB storageを利用する場合の設定
block-mount
kmod-fs-ext4
kmod-usb-dwc2
kmod-usb-storage

sshで接続できる。パスワードを設定していなくても、rootで接続できる。
hostから、scp openwrt-ramips-rt305x-wn-g300dgr-squashfs-sysupgrade.bin root@192.168.xx.xx:/tmp
ルータで、sysupgrade -v /tmp/openwrt-ramips-rt305x-wn-g300dgr-squashfs-sysupgrade.bin
sshは切断されが、設定は保存されているので、再起動の時間を十分空けて、再接続して確認する。

当初の目的はWiFi converterの設定。下記リンクの通りだが、joinするときにreplace boxをcheckするだけ。
https://openwrt.org/docs/guide-user/network/wifi/connect_client_wifi

コメント