X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/05a1b863a66bf72b26e5d87570c4e0e61b9736cd..6bd0463d3403c5c3dfe8ef5efe4ff74ceb86eeda:/VtoyTool/vtoydump.c diff --git a/VtoyTool/vtoydump.c b/VtoyTool/vtoydump.c index 003fd1a..34c1ee7 100644 --- a/VtoyTool/vtoydump.c +++ b/VtoyTool/vtoydump.c @@ -35,7 +35,9 @@ #define IS_DIGIT(x) ((x) >= '0' && (x) <= '9') #ifndef USE_DIET_C +#ifndef __mips__ typedef unsigned long long uint64_t; +#endif typedef unsigned int uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; @@ -43,6 +45,18 @@ typedef unsigned char uint8_t; #define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }} +typedef enum ventoy_fs_type +{ + ventoy_fs_exfat = 0, /* 0: exfat */ + ventoy_fs_ntfs, /* 1: NTFS */ + ventoy_fs_ext, /* 2: ext2/ext3/ext4 */ + ventoy_fs_xfs, /* 3: XFS */ + ventoy_fs_udf, /* 4: UDF */ + ventoy_fs_fat, /* 5: FAT */ + + ventoy_fs_max +}ventoy_fs_type; + #pragma pack(1) typedef struct ventoy_guid @@ -108,7 +122,9 @@ typedef struct ventoy_os_param uint64_t vtoy_reserved[4]; // Internal use by ventoy - uint8_t reserved[31]; + uint8_t vtoy_disk_signature[4]; + + uint8_t reserved[27]; }ventoy_os_param; #pragma pack() @@ -130,6 +146,11 @@ static int verbose = 0; static ventoy_guid vtoy_guid = VENTOY_GUID; +static const char *g_ventoy_fs[ventoy_fs_max] = +{ + "exfat", "ntfs", "ext*", "xfs", "udf", "fat" +}; + static int vtoy_check_os_param(ventoy_os_param *param) { uint32_t i; @@ -138,6 +159,16 @@ static int vtoy_check_os_param(ventoy_os_param *param) if (memcmp(¶m->guid, &vtoy_guid, sizeof(ventoy_guid))) { + uint8_t *data1 = (uint8_t *)(¶m->guid); + uint8_t *data2 = (uint8_t *)(&vtoy_guid); + + for (i = 0; i < 16; i++) + { + if (data1[i] != data2[i]) + { + debug("guid not equal i = %u, 0x%02x, 0x%02x\n", i, data1[i], data2[i]); + } + } return 1; } @@ -191,6 +222,11 @@ static void vtoy_dump_os_param(ventoy_os_param *param) printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n", param->vtoy_disk_guid[0], param->vtoy_disk_guid[1], param->vtoy_disk_guid[2], param->vtoy_disk_guid[3]); + + printf("param->vtoy_disk_signature = %02x %02x %02x %02x\n", + param->vtoy_disk_signature[0], param->vtoy_disk_signature[1], + param->vtoy_disk_signature[2], param->vtoy_disk_signature[3]); + printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param->vtoy_disk_size); printf("param->vtoy_disk_part_id = %u\n", param->vtoy_disk_part_id); printf("param->vtoy_disk_part_type = %u\n", param->vtoy_disk_part_type); @@ -204,7 +240,7 @@ static void vtoy_dump_os_param(ventoy_os_param *param) printf("\n"); } -static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid) +static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid, uint8_t *vtsig) { int i = 0; int fd = 0; @@ -217,6 +253,9 @@ static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid) { lseek(fd, 0x180, SEEK_SET); read(fd, vtguid, 16); + + lseek(fd, 0x1b8, SEEK_SET); + read(fd, vtsig, 4); close(fd); debug("GUID for %s: <", devdisk); @@ -328,6 +367,11 @@ static int vtoy_find_disk_by_size(unsigned long long size, char *diskname) int rc = 0; dir = opendir("/sys/block"); + if (!dir) + { + return 0; + } + while ((p = readdir(dir)) != NULL) { if (!vtoy_is_possible_blkdev(p->d_name)) @@ -348,15 +392,21 @@ static int vtoy_find_disk_by_size(unsigned long long size, char *diskname) return rc; } -static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname) +static int vtoy_find_disk_by_guid(ventoy_os_param *param, char *diskname) { int rc = 0; int count = 0; DIR* dir = NULL; struct dirent* p = NULL; uint8_t vtguid[16]; + uint8_t vtsig[16]; dir = opendir("/sys/block"); + if (!dir) + { + return 0; + } + while ((p = readdir(dir)) != NULL) { if (!vtoy_is_possible_blkdev(p->d_name)) @@ -366,8 +416,9 @@ static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname) } memset(vtguid, 0, sizeof(vtguid)); - rc = vtoy_get_disk_guid(p->d_name, vtguid); - if (rc == 0 && memcmp(vtguid, guid, 16) == 0) + rc = vtoy_get_disk_guid(p->d_name, vtguid, vtsig); + if (rc == 0 && memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 && + memcmp(vtsig, param->vtoy_disk_signature, 4) == 0) { sprintf(diskname, "%s", p->d_name); count++; @@ -378,6 +429,39 @@ static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname) return count; } +static int vtoy_printf_iso_path(ventoy_os_param *param) +{ + printf("%s\n", param->vtoy_img_path); + return 0; +} + +static int vtoy_check_device(ventoy_os_param *param, const char *device) +{ + unsigned long long size; + uint8_t vtguid[16] = {0}; + uint8_t vtsig[4] = {0}; + + debug("vtoy_check_device for <%s>\n", device); + + size = vtoy_get_disk_size_in_byte(device); + vtoy_get_disk_guid(device, vtguid, vtsig); + + debug("param->vtoy_disk_size=%llu size=%llu\n", + (unsigned long long)param->vtoy_disk_size, (unsigned long long)size); + + if (memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 && + memcmp(vtsig, param->vtoy_disk_signature, 4) == 0) + { + debug("<%s> is right ventoy disk\n", device); + return 0; + } + else + { + debug("<%s> is NOT right ventoy disk\n", device); + return 1; + } +} + static int vtoy_print_os_param(ventoy_os_param *param, char *diskname) { int cnt = 0; @@ -385,18 +469,23 @@ static int vtoy_print_os_param(ventoy_os_param *param, char *diskname) const char *fs; cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname); - if (cnt > 1) + debug("find disk by size %llu, cnt=%d...\n", (unsigned long long)param->vtoy_disk_size, cnt); + if (1 == cnt) { - cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname); + if (vtoy_check_device(param, diskname) != 0) + { + cnt = 0; + } } - - if (param->vtoy_disk_part_type == 0) + else { - fs = "exfat"; + cnt = vtoy_find_disk_by_guid(param, diskname); + debug("find disk by guid cnt=%d...\n", cnt); } - else if (param->vtoy_disk_part_type == 0) + + if (param->vtoy_disk_part_type < ventoy_fs_max) { - fs = "ntfs"; + fs = g_ventoy_fs[param->vtoy_disk_part_type]; } else { @@ -414,32 +503,6 @@ static int vtoy_print_os_param(ventoy_os_param *param, char *diskname) } } -static int vtoy_check_device(ventoy_os_param *param, const char *device) -{ - unsigned long long size; - uint8_t vtguid[16] = {0}; - - debug("vtoy_check_device for <%s>\n", device); - - size = vtoy_get_disk_size_in_byte(device); - vtoy_get_disk_guid(device, vtguid); - - debug("param->vtoy_disk_size=%llu size=%llu\n", - (unsigned long long)param->vtoy_disk_size, (unsigned long long)size); - - if (param->vtoy_disk_size == size && - memcmp(vtguid, param->vtoy_disk_guid, 16) == 0) - { - debug("<%s> is right ventoy disk\n", device); - return 0; - } - else - { - debug("<%s> is NOT right ventoy disk\n", device); - return 1; - } -} - /* * Find disk and image path from ventoy runtime data. * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file. @@ -453,12 +516,13 @@ int vtoydump_main(int argc, char **argv) { int rc; int ch; + int print_path = 0; char filename[256] = {0}; char diskname[256] = {0}; char device[64] = {0}; ventoy_os_param *param = NULL; - while ((ch = getopt(argc, argv, "c:f:v::")) != -1) + while ((ch = getopt(argc, argv, "c:f:p:v::")) != -1) { if (ch == 'f') { @@ -472,6 +536,11 @@ int vtoydump_main(int argc, char **argv) { strncpy(device, optarg, sizeof(device) - 1); } + else if (ch == 'p') + { + print_path = 1; + strncpy(filename, optarg, sizeof(filename) - 1); + } else { fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]); @@ -499,8 +568,20 @@ int vtoydump_main(int argc, char **argv) rc = vtoy_os_param_from_file(filename, param); if (rc) { - debug("ventoy os param not found %d\n", rc); - goto end; + debug("ventoy os param not found %d %d\n", rc, ENOENT); + if (ENOENT == rc) + { + debug("now try with file %s\n", "/ventoy/ventoy_os_param"); + rc = vtoy_os_param_from_file("/ventoy/ventoy_os_param", param); + if (rc) + { + goto end; + } + } + else + { + goto end; + } } if (verbose) @@ -508,7 +589,11 @@ int vtoydump_main(int argc, char **argv) vtoy_dump_os_param(param); } - if (device[0]) + if (print_path) + { + rc = vtoy_printf_iso_path(param); + } + else if (device[0]) { rc = vtoy_check_device(param, device); }