Adding OpenWrt support for Senao like Devices

Hey, I was looking at some commits for similar boards and found this hint, it might work for this one

NOTE: The U-Boot won't boot with the serial attached.
Boot the device without serial attached and attach it
after 3 seconds.

edit: never mind I read it wrong
however, see if UART works while openwrt is booted?

1 Like

No problem...

Yes. I tested with... without serial connected. Just garbage :cry:

1 Like

Hi @mpratt14
I´m installing dev environment here to try build something...
Any instructions to build this image? I´m following initially https://openwrt.org/docs/guide-developer/quickstart-build-images

1 Like

yeah that is a good place to start, make sure you installed dependencies first

You can practice building for another device while I finish what I'm testing...

1 Like

I Use a MAC, but already fixed most of problems with apple make, dependencies, etc... Now able to make menuconfig.

1 Like

@akornow Hi, sorry for the wait...I'm done testing my changes

the gist has one last test image

if the log looks good I will give you the rest

https://gist.github.com/mpratt14/ae08223fbb9bb9a639197452d215bffb

2 Likes

Thanks. Here's dmesg.

[    0.000000] Linux version 5.4.97 (pi@SDDesk) (gcc version 8.4.0 (OpenWrt GCC 8.4.0 r13719+835-66e04abbb6)) #0 Mon Feb 15 02:44:21 2021
[    0.000000] printk: bootconsole [early0] enabled
[    0.000000] CPU0 revision is: 00019750 (MIPS 74Kc)
[    0.000000] MIPS: machine is WatchGuard AP300
[    0.000000] SoC: Qualcomm Atheros QCA9558 ver 1 rev 0
[    0.000000] Initrd not found or empty - disabling initrd
[    0.000000] Primary instruction cache 64kB, VIPT, 4-way, linesize 32 bytes.
[    0.000000] Primary data cache 32kB, 4-way, VIPT, cache aliases, linesize 32 bytes
[    0.000000] Zone ranges:
[    0.000000]   Normal   [mem 0x0000000000000000-0x0000000007ffffff]
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000000000000-0x0000000007ffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000000000000-0x0000000007ffffff]
[    0.000000] On node 0 totalpages: 32768
[    0.000000]   Normal zone: 288 pages used for memmap
[    0.000000]   Normal zone: 0 pages reserved
[    0.000000]   Normal zone: 32768 pages, LIFO batch:7
[    0.000000] pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768
[    0.000000] pcpu-alloc: [0] 0 
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 32480
[    0.000000] Kernel command line: console=ttyS0,115200n8 rootfstype=squashfs,jffs2
[    0.000000] Dentry cache hash table entries: 16384 (order: 4, 65536 bytes, linear)
[    0.000000] Inode-cache hash table entries: 8192 (order: 3, 32768 bytes, linear)
[    0.000000] Writing ErrCtl register=00000000
[    0.000000] Readback ErrCtl register=00000000
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 112396K/131072K available (4916K kernel code, 184K rwdata, 1124K rodata, 10920K init, 196K bss, 18676K reserved, 0K cma-reserved)
[    0.000000] SLUB: HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] NR_IRQS: 51
[    0.000000] random: get_random_bytes called from start_kernel+0x32c/0x520 with crng_init=0
[    0.000000] CPU clock: 720.000 MHz
[    0.000000] clocksource: MIPS: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 5309056796 ns
[    0.000007] sched_clock: 32 bits at 360MHz, resolution 2ns, wraps every 5965232126ns
[    0.008284] Calibrating delay loop... 359.42 BogoMIPS (lpj=718848)
[    0.042878] pid_max: default: 32768 minimum: 301
[    0.047927] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.055710] Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.067940] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
[    0.078358] futex hash table entries: 256 (order: -1, 3072 bytes, linear)
[    0.085697] pinctrl core: initialized pinctrl subsystem
[    0.092090] NET: Registered protocol family 16
[    0.121613] workqueue: max_active 576 requested for napi_workq is out of range, clamping between 1 and 512
[    0.134765] clocksource: Switched to clocksource MIPS
[    0.141195] NET: Registered protocol family 2
[    0.146515] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 4096 bytes, linear)
[    0.155479] TCP established hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.163648] TCP bind hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.171165] TCP: Hash tables configured (established 1024 bind 1024)
[    0.178051] UDP hash table entries: 256 (order: 0, 4096 bytes, linear)
[    0.185059] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes, linear)
[    0.192753] NET: Registered protocol family 1
[    0.197444] PCI: CLS 0 bytes, default 32
[    0.373185] workingset: timestamp_bits=14 max_order=15 bucket_order=1
[    0.385971] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    0.392215] jffs2: version 2.2 (NAND) (SUMMARY) (LZMA) (RTIME) (CMODE_PRIORITY) (c) 2001-2006 Red Hat, Inc.
[    0.416315] pinctrl-single 1804002c.pinmux: 544 pins, size 68
[    0.423412] Serial: 8250/16550 driver, 1 ports, IRQ sharing disabled
[    0.430867] printk: console [ttyS0] disabled
[    0.435469] 18020000.uart: ttyS0 at MMIO 0x18020000 (irq = 9, base_baud = 2500000) is a 16550A
[    0.444655] printk: console [ttyS0] enabled
[    0.453554] printk: bootconsole [early0] disabled
[    0.469470] spi-nor spi0.0: s25fl256s1 (32768 Kbytes)
[    0.474689] 9 fixed-partitions partitions found on MTD device spi0.0
[    0.481148] Creating 9 MTD partitions on "spi0.0":
[    0.486026] 0x000000000000-0x000000040000 : "u-boot"
[    0.491872] 0x000000040000-0x000000050000 : "u-boot-env"
[    0.498086] 0x000000050000-0x0000000a0000 : "custom"
[    0.503956] 0x0000000a0000-0x0000000b0000 : "loader"
[    0.509798] 0x0000000b0000-0x000000230000 : "fwconcat1"
[    0.515916] 0x000000230000-0x000000d70000 : "fwconcat0"
[    0.522038] 0x000000d70000-0x000000ff0000 : "failsafe"
[    0.528070] 0x000000ff0000-0x000001ff0000 : "fwconcat2"
[    0.534191] 0x000001ff0000-0x000002000000 : "art"
[    0.540014] Concatenating MTD devices:
[    0.543869] (0): "fwconcat0"
[    0.546797] (1): "fwconcat1"
[    0.549716] (2): "fwconcat2"
[    0.552637] into device "virtual_flash"
[    0.556558] 1 fixed-partitions partitions found on MTD device virtual_flash
[    0.563627] Creating 1 MTD partitions on "virtual_flash":
[    0.569112] 0x000000000000-0x000001cc0000 : "firmware"
[    0.611499] libphy: Fixed MDIO Bus: probed
[    0.944556] libphy: ag71xx_mdio: probed
[    0.966014] ag71xx 19000000.eth: connected to PHY at mdio.0:05 [uid=004dd072, driver=Atheros 8035 ethernet]
[    0.976444] eth0: Atheros AG71xx at 0xb9000000, irq 4, mode: rgmii-id
[    0.983199] i2c /dev entries driver
[    0.988531] NET: Registered protocol family 10
[    0.997309] Segment Routing with IPv6
[    1.001160] NET: Registered protocol family 17
[    1.005755] 8021q: 802.1Q VLAN Support v1.8
[    1.010851] PCI host bridge /ahb/pcie-controller@180c0000 ranges:
[    1.017072]  MEM 0x0000000010000000..0x0000000011ffffff
[    1.022382]   IO 0x0000000000000000..0x0000000000000000
[    1.027839] PCI host bridge to bus 0000:00
[    1.032030] pci_bus 0000:00: root bus resource [mem 0x10000000-0x11ffffff]
[    1.039016] pci_bus 0000:00: root bus resource [io  0x0000]
[    1.044678] pci_bus 0000:00: root bus resource [??? 0x00000000 flags 0x0]
[    1.051568] pci_bus 0000:00: No busn resource found for root bus, will use [bus 00-ff]
[    1.059637] pci 0000:00:00.0: [168c:003c] type 00 class 0x028000
[    1.065783] pci 0000:00:00.0: reg 0x10: [mem 0x00000000-0x001fffff 64bit]
[    1.072728] pci 0000:00:00.0: reg 0x30: [mem 0x00000000-0x0000ffff pref]
[    1.079606] pci 0000:00:00.0: supports D1
[    1.083682] pci 0000:00:00.0: PME# supported from D0 D1 D3hot
[    1.090535] pci_bus 0000:00: busn_res: [bus 00-ff] end is updated to 00
[    1.097293] pci 0000:00:00.0: BAR 0: assigned [mem 0x10000000-0x101fffff 64bit]
[    1.104737] pci 0000:00:00.0: BAR 6: assigned [mem 0x10200000-0x1020ffff pref]
[    1.152980] Freeing unused kernel memory: 10920K
[    1.157690] This architecture does not have kernel memory protection.
[    1.164223] Run /init as init process
[    1.182793] random: fast init done
[    1.620511] init: Console is alive
[    1.624251] init: - watchdog -
[    1.644182] kmodloader: loading kernel modules from /etc/modules-boot.d/*
[    1.653298] kmodloader: done loading kernel modules from /etc/modules-boot.d/*
[    1.671595] init: - preinit -
[    1.927273] random: jshn: uninitialized urandom read (4 bytes read)
[    2.037752] random: jshn: uninitialized urandom read (4 bytes read)
[    2.084687] random: jshn: uninitialized urandom read (4 bytes read)
[    5.352132] eth0: link up (1000Mbps/Full duplex)
[    5.356867] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[    6.412136] eth0: link down
[    6.425524] procd: - early -
[    6.428613] procd: - watchdog -
[    6.986867] procd: - watchdog -
[    6.990386] procd: - ubus -
[    7.000593] urandom_read: 5 callbacks suppressed
[    7.000600] random: ubusd: uninitialized urandom read (4 bytes read)
[    7.044883] random: ubusd: uninitialized urandom read (4 bytes read)
[    7.054288] procd: - init -
[    7.599676] kmodloader: loading kernel modules from /etc/modules.d/*
[    7.633738] Loading modules backported from Linux version v5.8.18-0-gab435ce49bd1
[    7.641381] Backport generated by backports.git v5.8.18-1-0-ga630fd46
[    7.699434] xt_time: kernel timezone is -0000
[    7.820341] PPP generic driver version 2.4.2
[    7.835511] NET: Registered protocol family 24
[    7.858717] urngd: v1.0.2 started.
[    7.915123] ath10k 5.8 driver, optimized for CT firmware, probing pci device: 0x3c.
[    7.931509] ath10k_pci 0000:00:00.0: enabling device (0000 -> 0002)
[    7.940166] ath10k_pci 0000:00:00.0: pci irq legacy oper_irq_mode 1 irq_mode 0 reset_mode 0
[    8.083196] random: crng init done
[   11.419551] ath10k_pci 0000:00:00.0: qca988x hw2.0 target 0x4100016c chip_id 0x043202ff sub 0000:0000
[   11.428962] ath10k_pci 0000:00:00.0: kconfig debug 0 debugfs 1 tracing 0 dfs 1 testmode 0
[   11.441198] ath10k_pci 0000:00:00.0: firmware ver 10.1-ct-8x-__fW-022-ecad3248 api 2 features wmi-10.x,has-wmi-mgmt-tx,mfp,txstatus-noack,wmi-10.x-CT,ratemask-CT,txrate-CT,get-temp-CT,tx-rc-CT,cust-stats-CT,retry-gt2-CT,txrate2-CT,beacon-cb-CT,wmi-block-ack-CT crc32 3e4cf97f
[   11.774728] ath10k_pci 0000:00:00.0: board_file api 1 bmi_id N/A crc32 bebc7c08
[   12.700463] ath10k_pci 0000:00:00.0: 10.1 wmi init: vdevs: 16  peers: 127  tid: 256
[   12.717760] ath10k_pci 0000:00:00.0: wmi print 'P 128 V 8 T 410'
[   12.724085] ath10k_pci 0000:00:00.0: wmi print 'msdu-desc: 1424  sw-crypt: 0 ct-sta: 0'
[   12.732235] ath10k_pci 0000:00:00.0: wmi print 'alloc rem: 24984 iram: 38672'
[   12.786110] ath10k_pci 0000:00:00.0: htt-ver 2.1 wmi-op 2 htt-op 2 cal file max-sta 128 raw 0 hwcrypto 1
[   12.800388] ath10k_pci 0000:00:00.0: NOTE:  Firmware DBGLOG output disabled in debug_mask: 0x10000000
[   12.910049] ath: EEPROM regdomain sanitized
[   12.910058] ath: EEPROM regdomain: 0x64
[   12.910062] ath: EEPROM indicates we should expect a direct regpair map
[   12.910079] ath: Country alpha2 being used: 00
[   12.910083] ath: Regpair used: 0x64
[   12.988801] ath: EEPROM regdomain sanitized
[   12.988811] ath: EEPROM regdomain: 0x64
[   12.988815] ath: EEPROM indicates we should expect a direct regpair map
[   12.988833] ath: Country alpha2 being used: 00
[   12.988836] ath: Regpair used: 0x64
[   13.005885] ieee80211 phy1: Selected rate control algorithm 'minstrel_ht'
[   13.007645] ieee80211 phy1: Atheros AR9550 Rev:0 mem=0xb8100000, irq=12
[   13.051170] kmodloader: done loading kernel modules from /etc/modules.d/*
[   73.497355] br-lan: port 1(eth0) entered blocking state
[   73.502703] br-lan: port 1(eth0) entered disabled state
[   73.508287] device eth0 entered promiscuous mode
[   76.552121] eth0: link up (1000Mbps/Full duplex)
[   76.557000] br-lan: port 1(eth0) entered blocking state
[   76.562332] br-lan: port 1(eth0) entered forwarding state
[   76.569513] IPv6: ADDRCONF(NETDEV_CHANGE): br-lan: link becomes ready
[  206.412990] br-lan: port 1(eth0) entered disabled state
[  206.439165] device eth0 left promiscuous mode
[  206.443712] br-lan: port 1(eth0) entered disabled state
[  206.456308] eth0: link down
[  206.479253] br-lan: port 1(eth0) entered blocking state
[  206.484603] br-lan: port 1(eth0) entered disabled state
[  206.490179] device eth0 entered promiscuous mode
[  206.513841] br-lan: port 1(eth0) entered blocking state
[  206.519189] br-lan: port 1(eth0) entered forwarding state
[  207.463355] br-lan: port 1(eth0) entered disabled state
[  209.544165] eth0: link up (1000Mbps/Full duplex)
[  209.554840] br-lan: port 1(eth0) entered blocking state
[  209.560161] br-lan: port 1(eth0) entered forwarding state
[  297.606907] eth0: link down
[  297.610296] br-lan: port 1(eth0) entered disabled state
[  305.800112] eth0: link up (1000Mbps/Full duplex)
[  305.804998] br-lan: port 1(eth0) entered blocking state
[  305.810323] br-lan: port 1(eth0) entered forwarding state
[  344.723175] br-lan: port 1(eth0) entered disabled state
[  344.739922] device eth0 left promiscuous mode
[  344.744470] br-lan: port 1(eth0) entered disabled state
[  344.758151] eth0: link down
[  344.783056] br-lan: port 1(eth0) entered blocking state
[  344.788404] br-lan: port 1(eth0) entered disabled state
[  344.793977] device eth0 entered promiscuous mode
[  344.818523] br-lan: port 1(eth0) entered blocking state
[  344.823869] br-lan: port 1(eth0) entered forwarding state
[  345.767347] br-lan: port 1(eth0) entered disabled state
[  346.033743] device eth0 left promiscuous mode
[  346.038287] br-lan: port 1(eth0) entered disabled state
[  346.110682] br-lan: port 1(eth0) entered blocking state
[  346.116035] br-lan: port 1(eth0) entered disabled state
[  346.121606] device eth0 entered promiscuous mode
[  349.160110] eth0: link up (1000Mbps/Full duplex)
[  349.164992] br-lan: port 1(eth0) entered blocking state
[  349.170324] br-lan: port 1(eth0) entered forwarding state
[  349.177463] IPv6: ADDRCONF(NETDEV_CHANGE): br-lan: link becomes ready

By the way.

Not getting success on make... Seems that have some issues building on MAC...
/Volumes/OpenWrt/openwrt/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/lib/gcc/mips-openwrt-linux-musl/8.4.0/../../../../mips-openwrt-linux-musl/bin/ld: cannot find -lgcc_s

1 Like

I am not familiar with building on a Mac so I'm not sure how to help you there, but you can google it :smiley:

however I found this

1 Like

OK the gist is updated with all files

Use factory.bin with the original "firmware upgrade" page

after flashing factory.bin you must do a sysupgrade with sysupgrade.bin

1 Like

Original firmware is not allowing to upgrade.
I found this script that do some checks on etc and lib..
Do you think we need to change some of these files to ignore checks and continue using original page or is better to try to upgrade using initiramfs that is already working?

/etc/fwupgrade.sh

cat /etc/fwupgrade.sh 
#!/bin/sh

. /lib/upgrade/common.sh

firmware="/tmp/firmware.img"
tmpdir="/tmp/_upgrade"
output="/dev/ttyS0"
fw_env_config="/etc/fw_env.config"
sectorsize="$([ -f "${fw_env_config}" ] && grep "/dev/mtd1 " "${fw_env_config}" | sed -e "s/^\([^ \t]*[ \t]*\)\{4\}.*/\1/g")"
if [ -f "/etc/modelname" ]; then
	modelname="$(cat /etc/modelname)"
else
	modelname="$(cat /proc/sys/kernel/hostname | tr [A-Z] [a-z])"
fi
before_local="/etc/before-upgradelocal.sh"
after_local="/etc/after-upgradelocal.sh"
before="before-upgrade.sh"
after="after-upgrade.sh"
flag_disable_umount="/tmp/flag_disable_umount"

get_wgsum1()
{
    local image=$1;
    local image_size="`ls -l1 ${image} | tr -s ' ' | cut -d ' ' -f3 `"
    local  fw_size="`expr ${image_size} - 32`"
    echo `(dd if=${image} bs=${fw_size} count=1 2>/dev/null; echo -n "82kdlzk2" ) | md5sum | cut -d' ' -f1`
}

get_wgsum2()
{
	local image=$1;
	echo `tail -c 32 $image`;
}

check_image(){
	local image=$1
	local sum1="$(get_wgsum1 ${image})"
	local sum2="$(get_wgsum2 ${image})"

	if [ "${sum1}" == "${sum2}" ]; then
		echo 1
	else
		echo 0
	fi
}

create_mtdaddr() {
	addr="0"
	cat "/proc/mtd" | grep "mtd[0-9]*: " | grep -v "\"rootfs_data\"" | cut -d " " -f 2,4 | \
	while read line; do
		printf "0xbf%06x $(echo "${line}" | cut -d " " -f 2)\n" "${addr}"
		addr="$((${addr} + 0x$(echo "${line}" | cut -d " " -f 1)))"
	done
}

doupgrade() {
	mtdaddr="$(create_mtdaddr)"

	get_start_addr() {
		echo -e "${mtdaddr}" | grep "\"$1\"" | cut -d " " -f 1
	}

	get_size() {
		if [ "$1" = "kernel" ]; then
			echo "0x$(printf "%08x" "$(ls -al "${kernel}" | sed -e "s/^\([^ ]* *\)\{4\}\([0-9]\+\).*$/\2/g")")"
		elif [ "$1" = "rootfs" ]; then
			if [ "$(get_magic_word "${rootfs}")" = "6873" ]; then
				len=""
				for var in $(hexdump "${rootfs}" -s 40 -n 4 -e '/1 "%02x "'); do
					len="${var}${len}"
				done
				len="$((0x${len}))"
			else
				len="$(hexdump "${rootfs}" -s 67 -n 4 -e '"%d"')"
			fi
			printf "0x%08x\n" "$((((${len} - 1) / ${sectorsize} + 1) * ${sectorsize}))"
		fi
	}

	get_checksum() {
		if [ "$1" = "kernel" ]; then
			md5sum "${kernel}"
		elif [ "$1" = "rootfs" ]; then
			dd if="${rootfs}" bs="${sectorsize}" count="$(($(get_size "rootfs") / ${sectorsize}))" 2>/dev/null | md5sum -
		fi | cut -d " " -f 1
	}

	[ -f "${before_local}" ] && chmod a+x "${before_local}" && . "${before_local}"
	[ -f "${before}" ] && chmod a+x "${before}" && . "${before}"
	[ ! -f "${flag_disable_umount}" ] && {
		rootfs_mtd="$(cat /proc/mtd | grep \"rootfs\" | cut -d : -f 1)"
		rootfs_size="$(($( ( fw_printenv | grep ^rootfs_size= | cut -d = -f 2 ) 2>&- )))"
		if [ ! -f "/rom/note" -a -n "${rootfs_mtd}" -a ${rootfs_size} -gt 0 ]; then
			. /lib/functions/boot.sh && 
			umount -l /jffs && 
			pivot /rom /mnt && 
			umount -l /mnt && 
			{
				dd if=/dev/${rootfs_mtd} of=/tmp/root.squashfs bs=${rootfs_size} count=1 && 
				mount /tmp/root.squashfs /mnt && 
				pivot /mnt /rom && 
				umount -l /rom
			} 2>&- || true && 
			ramoverlay
		fi
	}
	[ -n "${kernel}" -a -f "${kernel}" ] && 
		[ "${magic_word_kernel}" = "2705" ] && {
		echo "Writing kernel..." >"${output}"
		fw_setenv vmlinux_start_addr "$(get_start_addr "kernel")"
		fw_setenv vmlinux_size       "$(get_size "kernel")"
		fw_setenv vmlinux_checksum   "$(get_checksum "kernel")"
		mtd write "${kernel}" "kernel"
	}
	[ -n "${rootfs}" -a -f "${rootfs}" ] && 
		[ "${magic_word_rootfs}" = "7371" -o "${magic_word_rootfs}" = "6873" ] && {
		echo "Writing rootfs..." >"${output}"
		fw_setenv rootfs_start_addr "$(get_start_addr "rootfs")"
		fw_setenv rootfs_size       "$(get_size "rootfs")"
		fw_setenv rootfs_checksum   "$(get_checksum "rootfs")"
		mtd -j "/tmp/_sys/sysupgrade.tgz" write "${rootfs}" "rootfs"
	}
	[ -f "${after_local}" ] && chmod a+x "${after_local}" && . "${after_local}"
	[ -f "${after}" ] && chmod a+x "${after}" && . "${after}"
	ask_bool 1 "Reboot"

	#WG: kill watchdog.sh to make sure system reboot.
	killall -9 watchdog.sh
	echo "Upgrade completed, rebooting system..." >"${output}"
}

if [ -f "${firmware}" ]; then
#!/bin/sh

. /lib/upgrade/common.sh

firmware="/tmp/firmware.img"
                if [ ! -f "/rom/note" -a -n "${rootfs_mtd}" -a ${rootfs_size} -gt 0 ]; then                                          
                        . /lib/functions/boot.sh &&                                                                                  
                        umount -l /jffs &&                                                                                           
                        pivot /rom /mnt &&                                                                                           
                        umount -l /mnt &&                                                                                            
                        {                                                                                                            
                                dd if=/dev/${rootfs_mtd} of=/tmp/root.squashfs bs=${rootfs_size} count=1 &&                          
                                mount /tmp/root.squashfs /mnt &&                                                                     
                                pivot /mnt /rom &&                                                                                   
                                umount -l /rom                                                                                       
                        } 2>&- || true &&                                                                                            
                        ramoverlay                                                                                                   
                fi                                                                                                                   
        }                                                                                                                            
        [ -n "${kernel}" -a -f "${kernel}" ] &&                                                                                      
                [ "${magic_word_kernel}" = "2705" ] && {                                                                             
                echo "Writing kernel..." >"${output}"                                                                                
                fw_setenv vmlinux_start_addr "$(get_start_addr "kernel")"                                                            
                fw_setenv vmlinux_size       "$(get_size "kernel")"                                                                  
                fw_setenv vmlinux_checksum   "$(get_checksum "kernel")"                                                              
                mtd write "${kernel}" "kernel"                                                                                       
        }                                                                                                                            
        [ -n "${rootfs}" -a -f "${rootfs}" ] &&                                                                                      
                [ "${magic_word_rootfs}" = "7371" -o "${magic_word_rootfs}" = "6873" ] && {                                          
                echo "Writing rootfs..." >"${output}"                                                                                
                fw_setenv rootfs_start_addr "$(get_start_addr "rootfs")"                                                             
                fw_setenv rootfs_size       "$(get_size "rootfs")"                                                                   
                fw_setenv rootfs_checksum   "$(get_checksum "rootfs")"                                                               
                mtd -j "/tmp/_sys/sysupgrade.tgz" write "${rootfs}" "rootfs"                                                         
        }                                                                                                                            
        [ -f "${after_local}" ] && chmod a+x "${after_local}" && . "${after_local}"                                                  
        [ -f "${after}" ] && chmod a+x "${after}" && . "${after}"                                                                    
        ask_bool 1 "Reboot"                                                                                                          
                                                                                                                                     
        #WG: kill watchdog.sh to make sure system reboot.                                                                            
        killall -9 watchdog.sh                                                                                                       
        echo "Upgrade completed, rebooting system..." >"${output}"                                                                   
}                                                                                                                                    
                                                                                                                                     
if [ -f "${firmware}" ]; then                                                                                                        
        if [ "${1}" != "force" ] && [ $(check_image ${firmware}) -ne 1 ]; then                                                       
                echo "Not WatchGuard firmware, refuse to upgrade" > "${output}"                                                      
                return 1;                                                                                                            
        fi                                                                                                                           
        if [ -n "${sectorsize}" ]; then                                                                                              
                sectorsize="$((${sectorsize}))"                                                                                      
        else                                                                                                                         
                echo "sectorsize Not defined." >"${output}"                                                                          
                return 3                                                                                                             
        fi                                                                                                                           
        [ -e "${tmpdir}" ] && rm -rf "${tmpdir}"                                                                                     
        mkdir -p "${tmpdir}" && cd "${tmpdir}" && tar zxf "${firmware}" && {                                                         
                errcode="1"                                                                                                          
                kernel="$(ls -1 | grep "^openwrt\-.*\-${modelname}\-uImage\-lzma\.bin$")"                                            
                rootfs="$(ls -1 | grep "^openwrt\-.*\-${modelname}\-root\.squashfs$")"                                               
                [ -n "${kernel}" -a -f "${kernel}" -a -n "${rootfs}" -a -f "${rootfs}" ] && {                                        
                        magic_word_kernel="$(get_magic_word "${kernel}")"                                                            
                        magic_word_rootfs="$(get_magic_word "${rootfs}")"                                                            
                        [ "${magic_word_kernel}" = "2705" ] &&                                                                       
                                [ "${magic_word_rootfs}" = "7371" -o "${magic_word_rootfs}" = "6873" ] &&                            
                                errcode="0"                                                                                          
                }                                                                                                                    
                #pass check when upload with full image file                                                                         
                [ "${errcode}" -eq "1" ] && [ -f failsafe.bin ] && errcode="0"                                                       
                if [ "${errcode}" -eq "0" ] && [ -f "${before}" -o -f "${after}" ]; then                                             
                [ "$1" = "test" ] || {                                                                                               
                                echo doupgrade >"${output}"                                                                          
                                rm -rf "${firmware}"                                                                                 
                                doupgrade                                                                                            
                        }                                                                                                            
                else                                                                                                                 
                        echo "Firmware invalid format." >"${output}"                                                                 
                        return 1                                                                                                     
                fi                                                                                                                   
                return                                                                                                               
        } || {                                                                                                                       
                echo "Firmware invalid format." >"${output}"                                                                         
                return 1                                                                                                             
        }                                                                                                                            
fi                                                                                                                                   
echo "$firmware Not existed." >"${output}"                                                                                           
return 2                                                     

/lib/upgrade.common.sh

cat  /lib/upgrade/common.sh
#!/bin/sh

RAM_ROOT=/tmp/root

ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
libs() { ldd $* | awk '{print $3}'; }

install_file() { # <file> [ <file> ... ]
	for file in "$@"; do
		dest="$RAM_ROOT/$file"
		[ -f $file -a ! -f $dest ] && {
			dir="$(dirname $dest)"
			mkdir -p "$dir"
			cp $file $dest
		}
	done
}

install_bin() { # <file> [ <symlink> ... ]
	src=$1
	files=$1
	[ -x "$src" ] && files="$src $(libs $src)"
	install_file $files
	[ -e /lib/ld-linux.so.3 ] && {
		install_file /lib/ld-linux.so.3
	}
	shift
	for link in "$@"; do {
		dest="$RAM_ROOT/$link"
		dir="$(dirname $dest)"
		mkdir -p "$dir"
		[ -f "$dest" ] || ln -s $src $dest
	}; done
}

pivot() { # <new_root> <old_root>
	mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
	mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/jffs && \
	mount -o move /proc $1/proc && \
	pivot_root $1 $1$2 || {
        umount $1 $1
		return 1
	}
	mount -o move $2/dev /dev
	mount -o move $2/tmp /tmp
	mount -o move $2/jffs /jffs 2>&-
	return 0
}

run_ramfs() { # <command> [...]
	install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump /bin/sleep /bin/zcat /usr/bin/bzcat
	install_bin /sbin/mtd
	for file in $RAMFS_COPY_BIN; do
		install_bin $file
	done
	install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA

	pivot $RAM_ROOT /mnt || {
		echo "Failed to switch over to ramfs. Please reboot."
		exit 1
	}

	mount -o remount,ro /mnt
	umount -l /mnt

	grep /jffs /proc/mounts > /dev/null && {
		mount -o remount,ro /jffs
		umount -l /jffs
	}

	# spawn a new shell from ramdisk to reduce the probability of cache issues
	exec /bin/busybox ash -c "$*"
}

run_hooks() {
	local arg="$1"; shift
	for func in "$@"; do
		eval "$func $arg"
	done
}

ask_bool() {
	local default="$1"; shift;
	local answer="$default"

	[ "$INTERACTIVE" -eq 1 ] && {
		case "$default" in
			0) echo -n "$* (y/N): ";;
			*) echo -n "$* (Y/n): ";;
		esac
		read answer
		case "$answer" in
			y*) answer=1;;
			n*) answer=0;;
			*) answer="$default";;
		esac
	}
	[ "$answer" -gt 0 ]
}

v() {
	[ "$VERBOSE" -ge 1 ] && echo "$@"
}

rootfs_type() {
	mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
}

get_image() { # <source> [ <command> ]
	local from="$1"
	local conc="$2"
	local cmd

	case "$from" in
		http://*|ftp://*) cmd="wget -O- -q";;
		*) cmd="cat";;
	esac
	if [ -z "$conc" ]; then
		local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
		case "$magic" in
			1f8b) conc="zcat";;
			425a) conc="bzcat";;
		esac
	fi

	eval "$cmd $from ${conc:+| $conc}"
}

get_magic_word() {
	get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
}

refresh_mtd_partitions() {
	mtd refresh rootfs
}

jffs2_copy_config() {
	if grep rootfs_data /proc/mtd >/dev/null; then
		# squashfs+jffs2
		mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
	else
		# jffs2
		mtd jffs2write "$CONF_TAR" rootfs
	fi
}

default_do_upgrade() {
	sync
	if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
		get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
	else
		get_image "$1" | mtd write - "${PART_NAME:-image}"
	fi
}

do_upgrade() {
	v "Performing system upgrade..."
	if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
		platform_do_upgrade "$ARGV"
	else
		default_do_upgrade "$ARGV"
	fi

	[ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
		v "Refreshing partitions"
		if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
			platform_refresh_partitions
		else
			refresh_mtd_partitions
		fi
		if type 'platform_copy_config' >/dev/null 2>/dev/null; then
			platform_copy_config
		else
			jffs2_copy_config
		fi
	}
	v "Upgrade completed"
	[ -n "$DELAY" ] && sleep "$DELAY"
	ask_bool 1 "Reboot" && {
		v "Rebooting system..."
		reboot
		sleep 5
		echo b 2>/dev/null >/proc/sysrq-trigger
	}
}
1 Like

my bad there is an extra image verification step I did not notice

get_wgsum1()
{
    local image=$1;
    local image_size="`ls -l1 ${image} | tr -s ' ' | cut -d ' ' -f3 `"
    local  fw_size="`expr ${image_size} - 32`"
    echo `(dd if=${image} bs=${fw_size} count=1 2>/dev/null; echo -n "82kdlzk2" ) | md5sum | cut -d' ' -f1`
}

get_wgsum2()
{
	local image=$1;
	echo `tail -c 32 $image`;
}

check_image(){
	local image=$1
	local sum1="$(get_wgsum1 ${image})"
	local sum2="$(get_wgsum2 ${image})"

	if [ "${sum1}" == "${sum2}" ]; then
		echo 1
	else
		echo 0
	fi
}
1 Like

I changed a line where sum1 and sum2 are compared and seems to completed.

if [ "${sum1}" == "${sum2}" ]; then

to

if [ "${sum1}" == "${sum1}" ]; then
1 Like

ideally I want to make an image where it is accepted by the original software so people do not have to do this hack

It is not difficult....just a checksum with added magic at the end of the file

so when I figure it out can you test the factory.bin again, by putting it in failsafe mode?

1 Like

sure. no problem. I have another here.

2 Likes

Yeap. I saw this... Give up trying on MAC. Which distro are you using?

1 Like

I build on a Raspberry Pi 4, so Debian 10

1 Like

ok there is a test image on the gist for TFTP, last one hopefully, it can be tested on the first one that you already flashed

1 Like

What changed? Old one seems very stable.
I´m digging using settings that are on gist to build a new image for it but didn't creating bin to ap300.

1 Like

in the gist there is only initramfs-kernel.bin

I have support for this board on top of other changes being reviewed and they made me change some things

I just want to be 100% sure you have ethernet with openwrt before flashing...

1 Like

OK. Will test here.
During my search for another devices here I found some AP100 and AP200 too.
Are you interested in build something to these devices too?

1 Like