使用者工具

網站工具


os:linux:編輯16進制檔案

[編輯16進度檔案]

Dump MBR Files

use “dd if=/dev/sda of=sda_mbr bs=512 count=1” to generate the binary file, which contains the mbr data.

# dd if=/dev/sda of=sda_mbr bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000310209 s, 1.7 MB/s

View Hex File

use hexdump to view the hex string

[root@svnserver Eddie]# hexdump sda_mbr
0000000 48eb d090 00bc 8e7c 8ec0 bed8 7c00 00bf
0000010 b906 0200 f3fc 50a4 1c68 cb06 b9fb 0004
0000020 bebd 8007 007e 7c00 0f0b 0e85 8301 10c5
0000030 f1e2 18cd 5688 5500 46c6 0511 46c6 0203
0000040 0080 8000 4ae2 0000 0800 90fa f690 80c2
0000050 0275 80b2 59ea 007c 3100 8ec0 8ed8 bcd0
0000060 2000 a0fb 7c40 ff3c 0274 c288 f652 80c2
0000070 5474 41b4 aabb cd55 5a13 7252 8149 55fb
.
.
.

Edit the hex file:

use “vi sda_mbr” open it and type“:%!xxd”:

0000000: eb48 90d0 bc00 7c8e c08e d8be 007c bf00  .H....|......|..
0000010: 06b9 0002 fcf3 a450 681c 06cb fbb9 0400  .......Ph.......
0000020: bdbe 0780 7e00 007c 0b0f 850e 0183 c510  ....~..|........
0000030: e2f1 cd18 8856 0055 c646 1105 c646 0302  .....V.U.F...F..
0000040: 8000 0080 e24a 0000 0008 fa90 90f6 c280  .....J..........
0000050: 7502 b280 ea59 7c00 0031 c08e d88e d0bc  u....Y|..1......
0000060: 0020 fba0 407c 3cff 7402 88c2 52f6 c280  . ..@|<.t...R...
0000070: 7454 b441 bbaa 55cd 135a 5272 4981 fb55  tT.A..U..ZRrI..U
.
.
.

自行寫 code 修改 flash

通过程序直接读取/dev/mtd5的内容,然后修改对应的位(适用于规模化生产)

int openwrt_mac_write(char* nic, char* value, int count)
{
    int size = 0;
    struct mtd_info_user mtdInfo;
    struct erase_info_user mtdEraseInfo;
    int fd = open(/ dev / mtd5, O_RDWR | O_SYNC);
    unsigned char *buf, *ptr;
 
    int i;
 
    if (fd < 0)
    {
        printf("Could not open mtd device: %s\n", / dev / mtd5);
        return -1;
    }
 
    if (ioctl(fd, MEMGETINFO, &mtdInfo))
    {
        printf("Could not get MTD device info from %s\n", / dev / mtd5);
        close(fd);
        return -1;
    }
 
    mtdEraseInfo.length = size = mtdInfo.erasesize;
    buf = (unsigned char*)malloc(size);
    if (NULL == buf)
    {
        printf("Allocate memory for size failed.\n");
        close(fd);
        return -1;
    }
 
    if (read(fd, buf, size) != size)
    {
        printf("read() %s failed\n", / dev / mtd5);
        goto write_fail;
    }
 
    mtdEraseInfo.start = 0x0;
    for (mtdEraseInfo.start; mtdEraseInfo.start < mtdInfo.size;
         mtdEraseInfo.start += mtdInfo.erasesize)
    {
        ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
        if (ioctl(fd, MEMERASE, &mtdEraseInfo))
        {
            printf("Failed to erase block on %s at 0x%x\n", / dev / mtd5, mtdEraseInfo.start);
            goto write_fail;
        }
    }
 
    if (!strcmp(nic, "wan"))
        ptr = buf + WAN_OFFSET;
    else if (!strcmp(nic, "lan"))
        ptr = buf + LAN_OFFSET;
    else if (!strcmp(nic, "ath0"))
        ptr = buf + WIFI_OFFSET;
    memcpy(ptr, value, count);
 
    lseek(fd, 0, SEEK_SET);
    if (write(fd, buf, size) != size)
    {
        printf("write() %s failed\n", / dev / mtd5);
        goto write_fail;
    }
 
    close(fd);
    free(buf);
    return 0;
write_fail:
    close(fd);
    free(buf);
    return -1;
}
os/linux/編輯16進制檔案.txt · 上一次變更: 2019/11/16 08:12 由 127.0.0.1