掌機 - Miyoo Mini Plus - C/C++ - Print U-Boot Environment Variables



main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#define ENV_OFFSET 0x5f004
#define ENV_LENGTH (0x1000 - 4)

static char buf[512 * 1024] = {0};

void dump_mtd(void)
{
    system("dd if=/dev/mtdblock0 of=m0.backup");
    system("dd if=/dev/mtdblock1 of=m1.backup");
    system("dd if=/dev/mtdblock2 of=m2.backup");
    system("dd if=/dev/mtdblock3 of=m3.backup");
    system("dd if=/dev/mtdblock4 of=m4.backup");
    system("dd if=/dev/mtdblock5 of=m5.backup");
    system("dd if=/dev/mtdblock6 of=m6.backup");
    system("dd if=/dev/mtdblock7 of=m7.backup");
}

int parse_string(const char *p)
{
    while (*p) {
        printf("\"%s\"\n", p);
        p += strlen(p) + 1;
    }
    return 0;
}

unsigned int crc32b(unsigned char *buf, int len)
{
   int i = 0, j = 0;
   unsigned int byte = 0, crc = 0, mask = 0;

   crc = 0xffffffff;
   for (i=0; i<len; i++) {
      byte = buf[i];
      crc = crc ^ byte;
      for (j = 7; j >= 0; j--) {
         mask = -(crc & 1);
         crc = (crc >> 1) ^ (0xedb88320 & mask);
      }
   }
   return ~crc;
}

int main(int argc, char **argv)
{
    dump_mtd();
    int len = 0, fd = open("m0.backup", O_RDONLY);
    if(fd < 0){
        printf("failed to open m0.backup\n");
        return -1;
    }
    len = read(fd, buf, sizeof(buf));
    close(fd);
    parse_string(&buf[ENV_OFFSET]);

    printf("crc32: 0x%x\n", crc32b(&buf[ENV_OFFSET], ENV_LENGTH));
    return 0;
}

編譯

$ /opt/miyoo/bin/arm-linux-gcc main.c -o dump -static

執行

# ./dump
    "SdUpgradeImage=miyoo354_fw.img"
    "baudrate=115200"
    "bootargs=console=ttyS0,115200 root=/dev/mtdblock4 rootfstype=squashfs ro init=/linuxrc LX_MEM=0x7f00000 mma_heap=mma_heap_name0,miu=0,sz=0x1500000 mma_memblock_remove=1 highres=off mmap_reserved=fb,miu=0,sz=0x300000,max_start_off=0x7C00000,max_end_off=0x7F00000"
    "bootcmd=gpio output 85 1; bootlogo 0 0 0 0 0; mw 1f001cc0 11; gpio out 8 0; sf probe 0;sf read 0x22000000 ${sf_kernel_start} ${sf_kernel_size}; gpio out 8 1; sleepms 1000; gpio output 4 1; bootm 0x22000000"
    "bootdelay=0"
    "cpu_part_start=14270000"
    "dispout=K101_IM2BVL"
    "ethact=sstar_emac"
    "ethaddr=00:30:1b:ba:02:db"
    "filesize=1774c"
    "miyoo_version=202303262339"
    "sf_kernel_size=200000"
    "sf_kernel_start=60000"
    "sf_part_size=20000"
    "sf_part_start=270000"
    "stderr=serial"
    "stdin=serial"
    "stdout=serial"
    "usb_folder=images"
    crc32: 0x47a7e93c