X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/4f840ed6736c4622c246bdaba3671e5820b79657..HEAD:/VtoyTool/vtoydm.c diff --git a/VtoyTool/vtoydm.c b/VtoyTool/vtoydm.c index ec0cb95..46de172 100644 --- a/VtoyTool/vtoydm.c +++ b/VtoyTool/vtoydm.c @@ -30,18 +30,22 @@ #include #include #include +#include #include "biso.h" #include "biso_list.h" #include "biso_util.h" #include "biso_plat.h" #include "biso_9660.h" +#include "vtoytool.h" #ifndef O_BINARY #define O_BINARY 0 #endif #ifndef USE_DIET_C +#ifndef __mips__ typedef unsigned long long uint64_t; +#endif typedef unsigned int uint32_t; #endif @@ -64,6 +68,7 @@ static int verbose = 0; #define CMD_DUMP_ISO_INFO 3 #define CMD_EXTRACT_ISO_FILE 4 #define CMD_PRINT_EXTRACT_ISO_FILE 5 +#define CMD_PRINT_RAW_TABLE 6 static uint64_t g_iso_file_size; static char g_disk_name[128]; @@ -482,7 +487,7 @@ static int vtoydm_print_extract_iso -static int vtoydm_print_linear_table(const char *img_map_file, const char *diskname) +static int vtoydm_print_linear_table(const char *img_map_file, const char *diskname, int part, uint64_t offset) { int i; int len; @@ -507,17 +512,17 @@ static int vtoydm_print_linear_table(const char *img_map_file, const char *diskn (sector_start << 2), disk_sector_num, diskname, (unsigned long long)chunk[i].disk_start_sector); #else - if (strstr(diskname, "nvme") || strstr(diskname, "mmc")) + if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd")) { - printf("%u %u linear %sp1 %llu\n", + printf("%u %u linear %sp%d %llu\n", (sector_start << 2), disk_sector_num, - diskname, (unsigned long long)chunk[i].disk_start_sector - 2048); + diskname, part, (unsigned long long)chunk[i].disk_start_sector - offset); } else { - printf("%u %u linear %s1 %llu\n", + printf("%u %u linear %s%d %llu\n", (sector_start << 2), disk_sector_num, - diskname, (unsigned long long)chunk[i].disk_start_sector - 2048); + diskname, part, (unsigned long long)chunk[i].disk_start_sector - offset); } #endif } @@ -537,17 +542,162 @@ static int vtoydm_print_help(FILE *fp) return 0; } +static uint64_t vtoydm_get_part_start(const char *diskname, int part) +{ + int fd; + unsigned long long size = 0; + char diskpath[256] = {0}; + char sizebuf[64] = {0}; + + if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd")) + { + snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%sp%d/start", diskname, part); + } + else + { + snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%s%d/start", diskname, part); + } + + if (access(diskpath, F_OK) >= 0) + { + debug("get part start from sysfs for %s %d\n", diskname, part); + + fd = open(diskpath, O_RDONLY | O_BINARY); + if (fd >= 0) + { + read(fd, sizebuf, sizeof(sizebuf)); + size = strtoull(sizebuf, NULL, 10); + close(fd); + return size; + } + } + else + { + debug("%s not exist \n", diskpath); + } + + return size; +} + +static uint64_t vtoydm_get_part_secnum(const char *diskname, int part) +{ + int fd; + unsigned long long size = 0; + char diskpath[256] = {0}; + char sizebuf[64] = {0}; + + diskname += 5; /* skip /dev/ */ + + if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd")) + { + snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%sp%d/size", diskname, part); + } + else + { + snprintf(diskpath, sizeof(diskpath) - 1, "/sys/class/block/%s%d/size", diskname, part); + } + + if (access(diskpath, F_OK) >= 0) + { + debug("get part size from sysfs for %s %d\n", diskname, part); + + fd = open(diskpath, O_RDONLY | O_BINARY); + if (fd >= 0) + { + read(fd, sizebuf, sizeof(sizebuf)); + size = strtoull(sizebuf, NULL, 10); + close(fd); + return size; + } + } + else + { + debug("%s not exist \n", diskpath); + } + + return size; +} + +static int vtoydm_vlnk_convert(char *disk, int len, int *part, uint64_t *offset) +{ + int rc = 1; + int cnt = 0; + int rdlen; + FILE *fp = NULL; + ventoy_os_param param; + char diskname[128] = {0}; + + fp = fopen("/ventoy/ventoy_os_param", "rb"); + if (!fp) + { + debug("dm vlnk convert not exist %d\n", errno); + goto end; + } + + memset(¶m, 0, sizeof(param)); + rdlen = (int)fread(¶m, 1, sizeof(param), fp); + if (rdlen != (int)sizeof(param)) + { + debug("fread failed %d %d\n", rdlen, errno); + goto end; + } + + debug("dm vlnk convert vtoy_reserved=%d\n", param.vtoy_reserved[6]); + + if (param.vtoy_reserved[6]) + { + cnt = vtoy_find_disk_by_guid(¶m, diskname); + debug("vtoy_find_disk_by_guid cnt=%d\n", cnt); + if (cnt == 1) + { + *part = param.vtoy_disk_part_id; + *offset = vtoydm_get_part_start(diskname, *part); + + debug("VLNK <%s> <%s> <%llu>\n", disk, diskname, *part, (unsigned long long)(*offset)); + + snprintf(disk, len, "/dev/%s", diskname); + + rc = 0; + } + } + +end: + if (fp) + fclose(fp); + return rc; +} + +static int vtoydm_print_raw_linear_table(const char *img_map_file, const char *diskname, int part) +{ + uint64_t disk_sector_num; + + disk_sector_num = vtoydm_get_part_secnum(diskname, part); + + if (strstr(diskname, "nvme") || strstr(diskname, "mmc") || strstr(diskname, "nbd")) + { + printf("0 %lu linear %sp%d 0\n", (unsigned long)disk_sector_num, diskname, part); + } + else + { + printf("0 %lu linear %s%d 0\n", (unsigned long)disk_sector_num, diskname, part); + } + + return 0; +} + int vtoydm_main(int argc, char **argv) { int ch; int cmd = 0; + int part = 1; + uint64_t offset = 2048; unsigned long first_sector = 0; unsigned long long file_size = 0; char diskname[128] = {0}; char filepath[300] = {0}; char outfile[300] = {0}; - while ((ch = getopt(argc, argv, "s:l:o:d:f:v::i::p::c::h::e::E::")) != -1) + while ((ch = getopt(argc, argv, "s:l:o:d:f:v::i::p::r::c::h::e::E::")) != -1) { if (ch == 'd') { @@ -561,6 +711,10 @@ int vtoydm_main(int argc, char **argv) { cmd = CMD_PRINT_TABLE; } + else if (ch == 'r') + { + cmd = CMD_PRINT_RAW_TABLE; + } else if (ch == 'c') { cmd = CMD_CREATE_DM; @@ -613,11 +767,17 @@ int vtoydm_main(int argc, char **argv) debug("cmd=%d file=<%s> disk=<%s> first_sector=%lu file_size=%llu\n", cmd, filepath, diskname, first_sector, file_size); + vtoydm_vlnk_convert(diskname, sizeof(diskname), &part, &offset); + switch (cmd) { case CMD_PRINT_TABLE: { - return vtoydm_print_linear_table(filepath, diskname); + return vtoydm_print_linear_table(filepath, diskname, part, offset); + } + case CMD_PRINT_RAW_TABLE: + { + return vtoydm_print_raw_linear_table(filepath, diskname, part); } case CMD_CREATE_DM: {