openwrt 芯片扩容修改固件步骤

youxiaojie

Nov 2022

../../../staging_dir/host/bin/lzma d tplink_tl-wdr4310-v1-kernel.bin 4310.unlzma
2.
using hexedit software okteta edit 4310.unlzma three offsets need modified (after A4):
0072:5258 replace hex 00020000 007d0000 with 00200000 00fd0000
0073:5272 replace string 7f with ff
0073:5298 replace hex 007f0000 00010000 with 00ff0000 00010000
3.
../../../staging_dir/host/bin/lzma e -lc1 -lp2 4310.unlzma tplink_tl-wdr4310-v1-kernel.bin
4.

 
modify openwrt-imagebuilder-22.03.2-ath79-generic.Linux-x86_64/target/linux/ath79/image/generic-tp-link.mk
define Device/tplink_tl-wdr4310-v1
  $(Device/tplink-8mlzma) ---> changing to 16mlzma
  SOC := ar9344
  DEVICE_MODEL := TL-WDR4310
  DEVICE_VARIANT := v1
  DEVICE_PACKAGES := kmod-usb2 kmod-usb-ledtrig-usbport
  TPLINK_HWID := 0x43100001
  SUPPORTED_DEVICES += tl-wdr4300
endef
TARGET_DEVICES += tplink_tl-wdr4310-v1

make image PROFILE=tplink_tl-wdr4310-v1 PACKAGES="uhttpd luci luci-ssl"

youxiaojie

youxiaojie

Nov 2022

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "Taras Gavrylenko"
__copyright__ = "Copyright 2020, The OpenWrt Project"
__version__ = "0.9"
__email__ = "gavrylenko.taras@gmail.com"
__status__ = "Beta"

"""
OpenWrt TP-Link kernel DTS patcher allows to use release 19.07 kernel
and packages on hardware modded devices with increased flash chip size.

Set environment variable $LZMA_UTIL_PATH with path to LZMA utility
Recommended LZMA 4.65 : Igor Pavlov : Public domain : 2009-02-03
from Openwrt originals

Be sure you have Python interpreter 3.8 or higher
Run: python3.8 kernel_patcher.py <path_to_kernel_file>

Script stages:
1. Rename original kernel to *.bak
2. Unpack original kernel
3. Patch DTS partition table
4. Pack new kernel
"""

import argparse
import logging
import os
import subprocess

from pathlib import Path

logging.basicConfig(
    format='%(asctime)s %(levelname)s %(message)s',
    level=logging.INFO)
logger = logging.getLogger(__name__)


def run_command(cmd: str):
    return subprocess.call(cmd, shell=True)


def compress_lzma(lzma_util: str, raw_path: Path, lzma_path: Path) -> int:
    cmd = f'{lzma_util} e {raw_path} {lzma_path} -lc1 -lp2'
    status = run_command(cmd)
    if status != 0:
        raise RuntimeError(f'Failed to compress {raw_path}')
    return lzma_path.stat().st_size


def decompress_lzma(lzma_util: str, lzma_path: Path, raw_path: Path) -> int:
    cmd = f'{lzma_util} d {lzma_path} {raw_path}'
    status = run_command(cmd)
    if status != 0:
        raise RuntimeError(f'Failed to decompress {lzma_path}')
    return raw_path.stat().st_size


def resize_kernel_dts(krnl_path: Path):
    chunk_num = 0
    chunk_size = 64 * 1024
    with krnl_path.open(mode='rb+') as f_out:
        while data := f_out.read(chunk_size):
            if (offset := data.find(b'partition@20000')) > 0:
                ofs_part = chunk_num * chunk_size + offset
                logger.info('Found DTS partition table at: '
                            f'0x{ofs_part:08x}')
                break
            chunk_num += 1
        else:
            raise RuntimeError('Could not find DTS partition table')
        f_out.seek(ofs_part)
        data = bytearray(f_out.read(0x100))
        if (offset := data.find(b'\xaa\0\x02\0\0\0\x7d\0\0')) > 0:
            data[offset+6] |= 0xff
        else:
            raise RuntimeError('Partition pattern not found')
        if (offset := data.find(b'partition@7f0000')) > 0:
            data[offset+10] = ord('f')
        else:
            raise RuntimeError('Partition pattern not found')
        if (offset := data.find(b'\xaa\0\x7f\0\0\0\x01\0\0')) > 0:
            data[offset+2] |= 0xff
        else:
            raise RuntimeError('Partition pattern not found')
        f_out.seek(ofs_part)
        f_out.write(data)


def main():
    logger.info('OpenWrt Imagebuilder DTS patcher v0.9')

    lzma_util_path = os.getenv('LZMA_UTIL_PATH')
    if lzma_util_path is None:
        logger.error('Compressor utility path is not set: LZMA_UTIL_PATH')
        exit(1)

    parser = argparse.ArgumentParser()
    parser.add_argument('image', help='OpenWrt kernel image file')
    args = parser.parse_args()

    krnl_path = Path(args.image)
    if krnl_path.is_file() is False:
        logger.error(f'Kernel image file does not exist: {krnl_path}')
        exit(1)
    krnl_size = krnl_path.stat().st_size
    logger.info(f'Processing kernel image file: {krnl_path}')
    logger.info(f'Origin kernel size: 0x{krnl_size:08x}')
    kernel_raw_path = Path(krnl_path.with_suffix('.raw'))
    logger.info('Decompressing kernel...')
    krnl_raw_size = decompress_lzma(
        lzma_util_path, krnl_path, kernel_raw_path)
    logger.info(f'Decompressed kernel size: 0x{krnl_raw_size:08x}')
    logger.info('Resizing kernel DTS...')
    resize_kernel_dts(kernel_raw_path)
    krnl_path.rename(krnl_path.with_suffix('.bak'))
    logger.info('Compressing kernel...')
    krnl_lzma_size = compress_lzma(
        lzma_util_path, kernel_raw_path, krnl_path)
    logger.info(f'Compressed kernel size: 0x{krnl_lzma_size:08x}')
    if krnl_lzma_size > krnl_size:
        logger.warning('Re-compressed kernel is bigger than original.')
    logger.info('Done')


if __name__ == '__main__':
    main()
### OpenWrt 存储扩容方法 #### 一、理解OpenWrt存储结构 OpenWrt设备通常采用嵌入式文件系统,初始分配给用户的根文件系统空间有限。当使用eMMC或其他大容量存储介质时,默认情况下可能只有一小部分被分配给了用户可用的空间[^2]。 #### 二、准备工作 为了能够顺利地执行存储扩容操作,需先通过SSH方式登录到OpenWrt设备上,并确认当前系统的磁盘使用情况以及是否存在未使用的额外存储区域。可以利用`df -h`命令查看现有分区大小及剩余空间状况;同时借助`fdisk -l`来获取整个硬盘的信息,包括是否有未划分的空白扇区可供扩展[^3]。 #### 三、具体实施步骤 ##### 修改分区表并调整文件系统大小 对于某些特定型号或版本的OpenWrt来说,可以直接在线修改内核中的分区定义参数实现自动识别全部物理存储器容量的目的。这一步骤涉及到编辑配置文件 `/etc/config/fstab` 或者直接更改 U-Boot 中的相关设置项以适应新的布局需求[^1]。 如果上述简易手段无法满足,则需要更深入的操作——即手动重新规划整个磁盘上的逻辑卷分布: 1. 使用 `parted /dev/mmcblk0 mklabel msdos` 创建一个新的DOS标签(假设mmcblk0为目标驱动器) 2. 添加新主分区覆盖所有可用空间:`parted /dev/mmcblk0 mkpart primary ext4 2048s 100%` 3. 扩展已存在的ext4格式化过的根目录至最大尺寸:`resize2fs /dev/mmcblk0p1` 以上过程均应在确保数据安全的前提下谨慎行事,建议提前做好重要资料备份工作以防万一发生意外丢失现象。 ```bash # 查看当前磁盘状态 df -h fdisk -l # 如果支持动态调整则尝试此法 uci set fstab.@mount[-1].enabled=1 uci commit fstab reboot # 否则按照传统流程处理 parted /dev/mmcblk0 mklabel msdos parted /dev/mmcblk0 mkpart primary ext4 2048s 100% resize2fs /dev/mmcblk0p1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值