Ath79: Help configuring/setup for factory image similar to stock

Hi.

I'm trying to setup the common-ubnt.mk image for the Amplifi Router HD. I thought this was more a how to/learn post so that's why I did not post it in the Amplifi post.

So by looking at the stock image with binwalk:

# binwalk b450-AmpliFi-3.6.3-834b2a9c26fb47d3b361df2ee92c7676.bin

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             Ubiquiti firmware header, header size: 264 bytes, ~CRC32: 0x80D72CA6, version: "AFi-R.qca956x.v3.6.3.0-g69993faf72"
268           0x10C           Ubiquiti partition header, header size: 56 bytes, name: "PARTu-boot", base address: 0x9F000000, data size: 313830 bytes
154260        0x25A94         Certificate in DER format (x509 v3), header length: 4, sequence length: 64
165796        0x287A4         CRC32 polynomial table, big endian
188983        0x2E237         Copyright string: "Copyright Ubiquiti Networks Inc. 2016"
193508        0x2F3E4         Ubiquiti firmware header, header size: 264 bytes, ~CRC32: 0x0, version: " APP Magic mismatch, addr=%x, magic=%x "
300136        0x49468         JFFS2 filesystem, big endian
300232        0x494C8         CRC32 polynomial table, big endian
314162        0x4CB32         Ubiquiti partition header, header size: 56 bytes, name: "PARTfirmware", base address: 0x9F070000, data size: 11272196 bytes
314218        0x4CB6A         uImage header, header size: 64 bytes, header CRC: 0x7C8505A1, created: 2022-03-30 08:10:32, image size: 1311332 bytes, Data Address: 0x80060000, Entry Point: 0x80060000, data CRC: 0xC5BE642B, OS: Linux, CPU: MIPS, image type: OS Kernel Image, compression type: lzma, image name: "MIPS OpenWrt Linux-4.1.16"
314282        0x4CBAA         LZMA compressed data, properties: 0x6D, dictionary size: 8388608 bytes, uncompressed size: 4157372 bytes
1690474       0x19CB6A        Squashfs filesystem, little endian, version 4.0, compression:xz, size: 9827056 bytes, 2085 inodes, blocksize: 262144 bytes, created: 2022-03-30 08:10:32
11586422      0xB0CB76        Signed Ubiquiti end header, RSA 2048 bit, header size: 264 bytes

they use the name: "PARTfirmware" that I think is messing up my image when I try to install OpenWrt. The respons I get from the amplifi tool is

'rootfs' has incorrect image part size: 00A00000, MTD part size: 009b0000

This is the output from binwalk with OpenWrt factory image:

# binwalk openwrt-ath79-generic-ubnt_amplifi-router-hd-squashfs-factory.bin

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             Ubiquiti firmware header, third party, ~CRC32: 0x785246A6, version: "AFi-R.qca956x.v3.6.3-OpenWrt-r25955-9cef2bc224"
268           0x10C           Ubiquiti partition header, header size: 56 bytes, name: "PARTkernel", base address: 0x9F070000, data size: 1048576 bytes
324           0x144           uImage header, header size: 64 bytes, header CRC: 0xF197E239, created: 2024-04-17 10:08:56, image size: 2456190 bytes, Data Address: 0x80060000, Entry Point: 0x80060000, data CRC: 0x2CE1AC6B, OS: Linux, CPU: MIPS, image type: OS Kernel Image, compression type: lzma, image name: "MIPS OpenWrt Linux-6.1.86"
388           0x184           LZMA compressed data, properties: 0x6D, dictionary size: 8388608 bytes, uncompressed size: 8266605 bytes
1048908       0x10014C        Ubiquiti partition header, header size: 56 bytes, name: "PARTrootfs", base address: 0x9F170000, data size: 5111812 bytes
2490756       0x260184        Squashfs filesystem, little endian, version 4.0, compression:xz, size: 3618330 bytes, 1013 inodes, blocksize: 262144 bytes, created: 2024-04-17 10:08:56
6160784       0x5E0190        Signed Ubiquiti end header, RSA 2048 bit, header size: 264 bytes

I feel like I'm missing something in the image code/setup so the size matches stock etc...
This is my code https://github.com/skramstad/openwrt/commit/68216e1119855b593869253dabcdc53bab4ed7e1#diff-a51c99e55cf3108676ce2e5fcc479dc3f59730f6189c1a1bfc8bf029d91d699b

So by looking at other images I can see a lot of options to generate an image. Like append, JFFS2, pad-to and so on...

Btw, this is my patch for mkfwimage.c

+	{
+		.name = "AFi-R-HD",
+		.fw_layout = {
+			.kern_start	=	0x9f070000,
+			.kern_entry	=	0x80060000,
+			.firmware_max_length=	0x00b00000,
+		},
+		.sign = true,
+	},

So I'm stuck at how to setup an image with the right size for each parts.

If you have any input on how to solve this, please do share. I would really appreciate that :slight_smile:

Edit: This is the parts compared to stock with the built in tool for amplifi

Version: AFi-R.qca956x.v3.6.3-OpenWrt-r25955-9cef2bc224
  - Magic:      OPEN
  - Header CRC: 785246A6
  - Part count: 2
  * Partition 1: 'kernel'
    - Magic: PART
    - Mem   addr: 80060000
    - Base  addr: 9F070000
    - Entry addr: 80060000
    - Data size:  00100000
    - Part size:  00100000
  * Partition 2: 'rootfs'
    - Magic: PART
    - Mem   addr: 00000000
    - Base  addr: 9F170000
    - Entry addr: 00000000
    - Data size:  004D3E00
    - Part size:  00A00000
Version: AFi-R.qca956x.v3.7.1.0-gd7d8bdad31
  - Magic:      UBNT
  - Header CRC: 2EB7D950
  - Part count: 2
  * Partition 1: 'u-boot'
    - Magic: PART
    - Mem   addr: 00000000
    - Base  addr: 9F000000
    - Entry addr: 00000000
    - Data size:  0004C9E6
    - Part size:  00060000
  * Partition 2: 'firmware'
    - Magic: PART
    - Mem   addr: 00000000
    - Base  addr: 9F070000
    - Entry addr: 00000000
    - Data size:  00AC0004
    - Part size:  00B00000