1 /******************************************************************************
2 * vtoydump.c ---- Dump ventoy os parameters
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
35 #define IS_DIGIT(x) ((x) >= '0' && (x) <= '9')
38 typedef unsigned long long uint64_t;
39 typedef unsigned int uint32_t;
40 typedef unsigned short uint16_t;
41 typedef unsigned char uint8_t;
44 #define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
46 typedef enum ventoy_fs_type
48 ventoy_fs_exfat
= 0, /* 0: exfat */
49 ventoy_fs_ntfs
, /* 1: NTFS */
50 ventoy_fs_ext
, /* 2: ext2/ext3/ext4 */
51 ventoy_fs_xfs
, /* 3: XFS */
52 ventoy_fs_udf
, /* 4: UDF */
53 ventoy_fs_fat
, /* 5: FAT */
60 typedef struct ventoy_guid
69 typedef struct ventoy_image_disk_region
71 uint32_t image_sector_count
; /* image sectors contained in this region */
72 uint32_t image_start_sector
; /* image sector start */
73 uint64_t disk_start_sector
; /* disk sector start */
74 }ventoy_image_disk_region
;
76 typedef struct ventoy_image_location
80 /* image sector size, currently this value is always 2048 */
81 uint32_t image_sector_size
;
83 /* disk sector size, normally the value is 512 */
84 uint32_t disk_sector_size
;
86 uint32_t region_count
;
90 * If the image file has more than one fragments in disk,
91 * there will be more than one region data here.
92 * You can calculate the region count by
94 ventoy_image_disk_region regions
[1];
96 /* ventoy_image_disk_region regions[2~region_count-1] */
97 }ventoy_image_location
;
99 typedef struct ventoy_os_param
101 ventoy_guid guid
; // VENTOY_GUID
102 uint8_t chksum
; // checksum
104 uint8_t vtoy_disk_guid
[16];
105 uint64_t vtoy_disk_size
; // disk size in bytes
106 uint16_t vtoy_disk_part_id
; // begin with 1
107 uint16_t vtoy_disk_part_type
; // 0:exfat 1:ntfs other: reserved
108 char vtoy_img_path
[384]; // It seems to be enough, utf-8 format
109 uint64_t vtoy_img_size
; // image file size in bytes
112 * Ventoy will write a copy of ventoy_image_location data into runtime memory
113 * this is the physically address and length of that memory.
114 * Address 0 means no such data exist.
115 * Address will be aligned by 4KB.
118 uint64_t vtoy_img_location_addr
;
119 uint32_t vtoy_img_location_len
;
121 uint64_t vtoy_reserved
[4]; // Internal use by ventoy
123 uint8_t reserved
[31];
131 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
132 #define MMAP_FLAGS MAP_SHARED
134 #define MMAP_FLAGS MAP_PRIVATE
137 #define SEARCH_MEM_START 0x80000
138 #define SEARCH_MEM_LEN 0x1c000
140 static int verbose
= 0;
141 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
143 static ventoy_guid vtoy_guid
= VENTOY_GUID
;
145 static const char *g_ventoy_fs
[ventoy_fs_max
] =
147 "exfat", "ntfs", "ext*", "xfs", "udf", "fat"
150 static int vtoy_check_os_param(ventoy_os_param
*param
)
154 uint8_t *buf
= (uint8_t *)param
;
156 if (memcmp(¶m
->guid
, &vtoy_guid
, sizeof(ventoy_guid
)))
161 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
168 debug("Invalid checksum 0x%02x\n", chksum
);
175 static int vtoy_os_param_from_file(const char *filename
, ventoy_os_param
*param
)
180 fd
= open(filename
, O_RDONLY
| O_BINARY
);
183 fprintf(stderr
, "Failed to open file %s error %d\n", filename
, errno
);
187 read(fd
, param
, sizeof(ventoy_os_param
));
189 if (vtoy_check_os_param(param
) == 0)
191 debug("find ventoy os param in file %s\n", filename
);
195 debug("ventoy os pararm NOT found in file %s\n", filename
);
203 static void vtoy_dump_os_param(ventoy_os_param
*param
)
205 printf("################# dump os param ################\n");
207 printf("param->chksum = 0x%x\n", param
->chksum
);
208 printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
209 param
->vtoy_disk_guid
[0], param
->vtoy_disk_guid
[1],
210 param
->vtoy_disk_guid
[2], param
->vtoy_disk_guid
[3]);
211 printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param
->vtoy_disk_size
);
212 printf("param->vtoy_disk_part_id = %u\n", param
->vtoy_disk_part_id
);
213 printf("param->vtoy_disk_part_type = %u\n", param
->vtoy_disk_part_type
);
214 printf("param->vtoy_img_path = <%s>\n", param
->vtoy_img_path
);
215 printf("param->vtoy_img_size = <%llu>\n", (unsigned long long)param
->vtoy_img_size
);
216 printf("param->vtoy_img_location_addr = <0x%llx>\n", (unsigned long long)param
->vtoy_img_location_addr
);
217 printf("param->vtoy_img_location_len = <%u>\n", param
->vtoy_img_location_len
);
218 printf("param->vtoy_reserved[0] = 0x%llx\n", (unsigned long long)param
->vtoy_reserved
[0]);
219 printf("param->vtoy_reserved[1] = 0x%llx\n", (unsigned long long)param
->vtoy_reserved
[1]);
224 static int vtoy_get_disk_guid(const char *diskname
, uint8_t *vtguid
)
228 char devdisk
[128] = {0};
230 snprintf(devdisk
, sizeof(devdisk
) - 1, "/dev/%s", diskname
);
232 fd
= open(devdisk
, O_RDONLY
| O_BINARY
);
235 lseek(fd
, 0x180, SEEK_SET
);
236 read(fd
, vtguid
, 16);
239 debug("GUID for %s: <", devdisk
);
240 for (i
= 0; i
< 16; i
++)
242 debug("%02x", vtguid
[i
]);
250 debug("failed to open %s %d\n", devdisk
, errno
);
255 static unsigned long long vtoy_get_disk_size_in_byte(const char *disk
)
259 unsigned long long size
= 0;
260 char diskpath
[256] = {0};
261 char sizebuf
[64] = {0};
263 // Try 1: get size from sysfs
264 snprintf(diskpath
, sizeof(diskpath
) - 1, "/sys/block/%s/size", disk
);
265 if (access(diskpath
, F_OK
) >= 0)
267 debug("get disk size from sysfs for %s\n", disk
);
269 fd
= open(diskpath
, O_RDONLY
| O_BINARY
);
272 read(fd
, sizebuf
, sizeof(sizebuf
));
273 size
= strtoull(sizebuf
, NULL
, 10);
280 debug("%s not exist \n", diskpath
);
283 // Try 2: get size from ioctl
284 snprintf(diskpath
, sizeof(diskpath
) - 1, "/dev/%s", disk
);
285 fd
= open(diskpath
, O_RDONLY
);
288 debug("get disk size from ioctl for %s\n", disk
);
289 rc
= ioctl(fd
, BLKGETSIZE64
, &size
);
293 debug("failed to ioctl %d\n", rc
);
299 debug("failed to open %s %d\n", diskpath
, errno
);
302 debug("disk %s size %llu bytes\n", disk
, (unsigned long long)size
);
306 static int vtoy_is_possible_blkdev(const char *name
)
314 if (name
[0] == 'r' && name
[1] == 'a' && name
[2] == 'm')
320 if (name
[0] == 'l' && name
[1] == 'o' && name
[2] == 'o' && name
[3] == 'p')
326 if (name
[0] == 'd' && name
[1] == 'm' && name
[2] == '-' && IS_DIGIT(name
[3]))
332 if (name
[0] == 's' && name
[1] == 'r' && IS_DIGIT(name
[2]))
340 static int vtoy_find_disk_by_size(unsigned long long size
, char *diskname
)
342 unsigned long long cursize
= 0;
344 struct dirent
* p
= NULL
;
347 dir
= opendir("/sys/block");
353 while ((p
= readdir(dir
)) != NULL
)
355 if (!vtoy_is_possible_blkdev(p
->d_name
))
357 debug("disk %s is filted by name\n", p
->d_name
);
361 cursize
= vtoy_get_disk_size_in_byte(p
->d_name
);
362 debug("disk %s size %llu\n", p
->d_name
, (unsigned long long)cursize
);
365 sprintf(diskname
, "%s", p
->d_name
);
373 static int vtoy_find_disk_by_guid(uint8_t *guid
, char *diskname
)
378 struct dirent
* p
= NULL
;
381 dir
= opendir("/sys/block");
387 while ((p
= readdir(dir
)) != NULL
)
389 if (!vtoy_is_possible_blkdev(p
->d_name
))
391 debug("disk %s is filted by name\n", p
->d_name
);
395 memset(vtguid
, 0, sizeof(vtguid
));
396 rc
= vtoy_get_disk_guid(p
->d_name
, vtguid
);
397 if (rc
== 0 && memcmp(vtguid
, guid
, 16) == 0)
399 sprintf(diskname
, "%s", p
->d_name
);
408 static int vtoy_printf_iso_path(ventoy_os_param
*param
)
410 printf("%s\n", param
->vtoy_img_path
);
414 static int vtoy_print_os_param(ventoy_os_param
*param
, char *diskname
)
417 char *path
= param
->vtoy_img_path
;
420 cnt
= vtoy_find_disk_by_size(param
->vtoy_disk_size
, diskname
);
423 cnt
= vtoy_find_disk_by_guid(param
->vtoy_disk_guid
, diskname
);
427 cnt
= vtoy_find_disk_by_guid(param
->vtoy_disk_guid
, diskname
);
428 debug("find 0 disk by size, try with guid cnt=%d...\n", cnt
);
431 if (param
->vtoy_disk_part_type
< ventoy_fs_max
)
433 fs
= g_ventoy_fs
[param
->vtoy_disk_part_type
];
442 printf("/dev/%s#%s#%s\n", diskname
, fs
, path
);
451 static int vtoy_check_device(ventoy_os_param
*param
, const char *device
)
453 unsigned long long size
;
454 uint8_t vtguid
[16] = {0};
456 debug("vtoy_check_device for <%s>\n", device
);
458 size
= vtoy_get_disk_size_in_byte(device
);
459 vtoy_get_disk_guid(device
, vtguid
);
461 debug("param->vtoy_disk_size=%llu size=%llu\n",
462 (unsigned long long)param
->vtoy_disk_size
, (unsigned long long)size
);
464 if ((param
->vtoy_disk_size
== size
|| param
->vtoy_disk_size
== size
+ 512) &&
465 memcmp(vtguid
, param
->vtoy_disk_guid
, 16) == 0)
467 debug("<%s> is right ventoy disk\n", device
);
472 debug("<%s> is NOT right ventoy disk\n", device
);
478 * Find disk and image path from ventoy runtime data.
479 * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
481 * -f datafile os param data file.
482 * -c /dev/xxx check ventoy disk
484 * -l also print image disk location
486 int vtoydump_main(int argc
, char **argv
)
491 char filename
[256] = {0};
492 char diskname
[256] = {0};
493 char device
[64] = {0};
494 ventoy_os_param
*param
= NULL
;
496 while ((ch
= getopt(argc
, argv
, "c:f:p:v::")) != -1)
500 strncpy(filename
, optarg
, sizeof(filename
) - 1);
508 strncpy(device
, optarg
, sizeof(device
) - 1);
513 strncpy(filename
, optarg
, sizeof(filename
) - 1);
517 fprintf(stderr
, "Usage: %s -f datafile [ -v ] \n", argv
[0]);
522 if (filename
[0] == 0)
524 fprintf(stderr
, "Usage: %s -f datafile [ -v ] \n", argv
[0]);
528 param
= malloc(sizeof(ventoy_os_param
));
531 fprintf(stderr
, "failed to alloc memory with size %d error %d\n",
532 (int)sizeof(ventoy_os_param
), errno
);
536 memset(param
, 0, sizeof(ventoy_os_param
));
538 debug("get os pararm from file %s\n", filename
);
539 rc
= vtoy_os_param_from_file(filename
, param
);
542 debug("ventoy os param not found %d\n", rc
);
548 vtoy_dump_os_param(param
);
553 rc
= vtoy_printf_iso_path(param
);
557 rc
= vtoy_check_device(param
, device
);
561 // print os param, you can change the output format in the function
562 rc
= vtoy_print_os_param(param
, diskname
);
574 #ifndef BUILD_VTOY_TOOL
575 int main(int argc
, char **argv
)
577 return vtoydump_main(argc
, argv
);