在DDwrt下对Firmware操作的一些技巧

[备注]这里是对ddwrt的操作,事实上,对openwrt同样也适用。

基础知识:

1、MTD

MTD是Memory Technology Devices的缩写,它主要提供了一个raw Flash设备和Linux系统之间的抽象层,这确保了即使使用不同的Flash芯片,Linux都是在用同样的API进行操作;对Linux系统来讲,这是对底层硬件的屏蔽。raw Flash设备指像NAND, OneNAND, NOR, AG-AND, ECC'd NOR, SPI Flash等这些。

MTD不支持MMC, eMMC, SD, CompactFlash等块设备,虽然这些设备的基本构成也是Flash,但是他们内部有一个 Flash Translation layer,这一设计让他们用起来像是block device。这些设备属于Linux Block devcies要处理的范畴。

MTD subsystem has the following interfaces:

  • MTD character devices - usually referred to as /dev/mtd0, /dev/mtd1, and so on. These character devices provide I/O access to the raw flash. They support a number of ioctl calls for erasing eraseblocks, marking them as bad or checking if an eraseblock is bad, getting information about MTD devices, etc.
  • The sysfs interface is relatively newer and it provides full information about each MTD device in the system. This interface is easily extensible and developers are encouraged to use the sysfs interface instead of older ioctl or /proc/mtd interfaces, when possible. The sysfs interface for the mtd subsystem is documentated in the kernel, and currently can be found at Documentation/ABI/testing/sysfs-class-mtd.
  • The /proc/mtd proc file system file provides general MTD information. This is a legacy interface and the sysfs interface provides more information.

2、dd命令

功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换。

参数:

if = 输入文件(或输入设备)

of = 输出文件(或输出设备)

操作:

1、用dd来备份firmware

先查看一下当前Firmware中Flash分区状况,

# cat /proc/mtd
dev: size erasesize name
mtd0: 00040000 00010000 "u-boot"
mtd1: 00010000 00010000 "u-boot-env"
mtd2: 00100000 00010000 "kernel"
mtd3: 00660000 00010000 "rootfs"
mtd4: 00040000 00010000 "cfg"
mtd5: 00010000 00010000 "EEPROM"

像上面讲的,/proc/mtd存储了mtd设备的基本情况,那对应的mtd块在/dev目录下有相应字符设备。

比如我们要备份rootfs

# dd if=/dev/mtd3 of=/tmp/rootfs.bin
13056+0 records in
13056+0 records out
# cd /tmp
# ls
boot.txt     rootfs.bin       running.cfg  system.cfg

我们也可以分别备份各块,然后“拼接”出整个firmware

cat uboot.bin u-boot-env.bin kernel.bin rootfs.bin cfg.bin eeprom.bin > fullflash.bin

2、binary的拼接

因为从Flash里面读出来的是Plain Binary,所以,也可以用像Ultra Editor这样的工具进行拼接。需要注意的是修改的位置和并且要计算好偏移。

当然,这样直接拼接的意义可能不大,因为不管是U-boot到kernel也好,app到configure也好,他们都是对格式有特殊要求的。

1)合成烧写Flash需要的bin文件。

2)传递的参数相同,数据格式相同时,修改其中的一部分,可以直接编辑bin文件拼接。

3、Uboot里面烧写Flash操作

ar7240> printenv
ar7240> setenv ipaddr 192.168.1.2
ar7240> setenv serverip 192.168.1.100 
ar7240> tftpboot 0x80000000 xxx.lzma
ar7240> erase 0x9f050000 +0x
ar7240> cp.b 0x80000000 0x9f050000 0x3fffff
原文地址:https://www.cnblogs.com/pied/p/2878944.html