]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoydump.c
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>
39 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
40 #define MMAP_FLAGS MAP_SHARED
42 #define MMAP_FLAGS MAP_PRIVATE
45 #define SEARCH_MEM_START 0x80000
46 #define SEARCH_MEM_LEN 0x1c000
48 static int verbose
= 0;
49 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
51 static ventoy_guid vtoy_guid
= VENTOY_GUID
;
53 static const char *g_ventoy_fs
[ventoy_fs_max
] =
55 "exfat", "ntfs", "ext*", "xfs", "udf", "fat"
58 static int vtoy_check_os_param(ventoy_os_param
*param
)
62 uint8_t *buf
= (uint8_t *)param
;
64 if (memcmp(¶m
->guid
, &vtoy_guid
, sizeof(ventoy_guid
)))
66 uint8_t *data1
= (uint8_t *)(¶m
->guid
);
67 uint8_t *data2
= (uint8_t *)(&vtoy_guid
);
69 for (i
= 0; i
< 16; i
++)
71 if (data1
[i
] != data2
[i
])
73 debug("guid not equal i = %u, 0x%02x, 0x%02x\n", i
, data1
[i
], data2
[i
]);
79 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
86 debug("Invalid checksum 0x%02x\n", chksum
);
93 static int vtoy_os_param_from_file(const char *filename
, ventoy_os_param
*param
)
98 fd
= open(filename
, O_RDONLY
| O_BINARY
);
101 fprintf(stderr
, "Failed to open file %s error %d\n", filename
, errno
);
105 read(fd
, param
, sizeof(ventoy_os_param
));
107 if (vtoy_check_os_param(param
) == 0)
109 debug("find ventoy os param in file %s\n", filename
);
113 debug("ventoy os pararm NOT found in file %s\n", filename
);
121 static void vtoy_dump_os_param(ventoy_os_param
*param
)
123 printf("################# dump os param ################\n");
125 printf("param->chksum = 0x%x\n", param
->chksum
);
126 printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
127 param
->vtoy_disk_guid
[0], param
->vtoy_disk_guid
[1],
128 param
->vtoy_disk_guid
[2], param
->vtoy_disk_guid
[3]);
130 printf("param->vtoy_disk_signature = %02x %02x %02x %02x\n",
131 param
->vtoy_disk_signature
[0], param
->vtoy_disk_signature
[1],
132 param
->vtoy_disk_signature
[2], param
->vtoy_disk_signature
[3]);
134 printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param
->vtoy_disk_size
);
135 printf("param->vtoy_disk_part_id = %u\n", param
->vtoy_disk_part_id
);
136 printf("param->vtoy_disk_part_type = %u\n", param
->vtoy_disk_part_type
);
137 printf("param->vtoy_img_path = <%s>\n", param
->vtoy_img_path
);
138 printf("param->vtoy_img_size = <%llu>\n", (unsigned long long)param
->vtoy_img_size
);
139 printf("param->vtoy_img_location_addr = <0x%llx>\n", (unsigned long long)param
->vtoy_img_location_addr
);
140 printf("param->vtoy_img_location_len = <%u>\n", param
->vtoy_img_location_len
);
141 printf("param->vtoy_reserved = %02x %02x %02x %02x %02x %02x %02x %02x\n",
142 param
->vtoy_reserved
[0],
143 param
->vtoy_reserved
[1],
144 param
->vtoy_reserved
[2],
145 param
->vtoy_reserved
[3],
146 param
->vtoy_reserved
[4],
147 param
->vtoy_reserved
[5],
148 param
->vtoy_reserved
[6],
149 param
->vtoy_reserved
[7]
155 static int vtoy_get_disk_guid(const char *diskname
, uint8_t *vtguid
, uint8_t *vtsig
)
159 char devdisk
[128] = {0};
161 snprintf(devdisk
, sizeof(devdisk
) - 1, "/dev/%s", diskname
);
163 fd
= open(devdisk
, O_RDONLY
| O_BINARY
);
166 lseek(fd
, 0x180, SEEK_SET
);
167 read(fd
, vtguid
, 16);
169 lseek(fd
, 0x1b8, SEEK_SET
);
173 debug("GUID for %s: <", devdisk
);
174 for (i
= 0; i
< 16; i
++)
176 debug("%02x", vtguid
[i
]);
184 debug("failed to open %s %d\n", devdisk
, errno
);
189 static unsigned long long vtoy_get_disk_size_in_byte(const char *disk
)
193 unsigned long long size
= 0;
194 char diskpath
[256] = {0};
195 char sizebuf
[64] = {0};
197 // Try 1: get size from sysfs
198 snprintf(diskpath
, sizeof(diskpath
) - 1, "/sys/block/%s/size", disk
);
199 if (access(diskpath
, F_OK
) >= 0)
201 debug("get disk size from sysfs for %s\n", disk
);
203 fd
= open(diskpath
, O_RDONLY
| O_BINARY
);
206 read(fd
, sizebuf
, sizeof(sizebuf
));
207 size
= strtoull(sizebuf
, NULL
, 10);
214 debug("%s not exist \n", diskpath
);
217 // Try 2: get size from ioctl
218 snprintf(diskpath
, sizeof(diskpath
) - 1, "/dev/%s", disk
);
219 fd
= open(diskpath
, O_RDONLY
);
222 debug("get disk size from ioctl for %s\n", disk
);
223 rc
= ioctl(fd
, BLKGETSIZE64
, &size
);
227 debug("failed to ioctl %d\n", rc
);
233 debug("failed to open %s %d\n", diskpath
, errno
);
236 debug("disk %s size %llu bytes\n", disk
, (unsigned long long)size
);
240 static int vtoy_is_possible_blkdev(const char *name
)
248 if (name
[0] == 'r' && name
[1] == 'a' && name
[2] == 'm')
254 if (name
[0] == 'l' && name
[1] == 'o' && name
[2] == 'o' && name
[3] == 'p')
260 if (name
[0] == 'd' && name
[1] == 'm' && name
[2] == '-' && IS_DIGIT(name
[3]))
266 if (name
[0] == 's' && name
[1] == 'r' && IS_DIGIT(name
[2]))
274 static int vtoy_find_disk_by_size(unsigned long long size
, char *diskname
)
276 unsigned long long cursize
= 0;
278 struct dirent
* p
= NULL
;
281 dir
= opendir("/sys/block");
287 while ((p
= readdir(dir
)) != NULL
)
289 if (!vtoy_is_possible_blkdev(p
->d_name
))
291 debug("disk %s is filted by name\n", p
->d_name
);
295 cursize
= vtoy_get_disk_size_in_byte(p
->d_name
);
296 debug("disk %s size %llu\n", p
->d_name
, (unsigned long long)cursize
);
299 sprintf(diskname
, "%s", p
->d_name
);
307 int vtoy_find_disk_by_guid(ventoy_os_param
*param
, char *diskname
)
312 struct dirent
* p
= NULL
;
316 dir
= opendir("/sys/block");
322 while ((p
= readdir(dir
)) != NULL
)
324 if (!vtoy_is_possible_blkdev(p
->d_name
))
326 debug("disk %s is filted by name\n", p
->d_name
);
330 memset(vtguid
, 0, sizeof(vtguid
));
331 memset(vtsig
, 0, sizeof(vtsig
));
332 rc
= vtoy_get_disk_guid(p
->d_name
, vtguid
, vtsig
);
333 if (rc
== 0 && memcmp(vtguid
, param
->vtoy_disk_guid
, 16) == 0 &&
334 memcmp(vtsig
, param
->vtoy_disk_signature
, 4) == 0)
336 sprintf(diskname
, "%s", p
->d_name
);
345 static int vtoy_find_disk_by_sig(uint8_t *sig
, char *diskname
)
350 struct dirent
* p
= NULL
;
354 dir
= opendir("/sys/block");
360 while ((p
= readdir(dir
)) != NULL
)
362 if (!vtoy_is_possible_blkdev(p
->d_name
))
364 debug("disk %s is filted by name\n", p
->d_name
);
368 memset(vtguid
, 0, sizeof(vtguid
));
369 memset(vtsig
, 0, sizeof(vtsig
));
370 rc
= vtoy_get_disk_guid(p
->d_name
, vtguid
, vtsig
);
371 if (rc
== 0 && memcmp(vtsig
, sig
, 4) == 0)
373 sprintf(diskname
, "%s", p
->d_name
);
382 static int vtoy_printf_iso_path(ventoy_os_param
*param
)
384 printf("%s\n", param
->vtoy_img_path
);
388 static int vtoy_printf_fs(ventoy_os_param
*param
)
392 "exfat", "ntfs", "ext", "xfs", "udf", "fat"
395 if (param
->vtoy_disk_part_type
< 6)
397 printf("%s\n", fs
[param
->vtoy_disk_part_type
]);
406 static int vtoy_vlnk_printf(ventoy_os_param
*param
, char *diskname
)
413 uint8_t check
[8] = { 0x56, 0x54, 0x00, 0x47, 0x65, 0x00, 0x48, 0x44 };
415 memcpy(disk_sig
, param
->vtoy_reserved
+ 7, 4);
417 debug("vlnk disk sig: %02x %02x %02x %02x \n", disk_sig
[0], disk_sig
[1], disk_sig
[2], disk_sig
[3]);
419 cnt
= vtoy_find_disk_by_sig(disk_sig
, diskname
);
422 snprintf(diskpath
, sizeof(diskpath
), "/dev/%s", diskname
);
423 fd
= open(diskpath
, O_RDONLY
| O_BINARY
);
426 memset(mbr
, 0, sizeof(mbr
));
427 read(fd
, mbr
, sizeof(mbr
));
430 if (memcmp(mbr
+ 0x190, check
, 8) == 0)
432 printf("/dev/%s", diskname
);
437 debug("check data failed /dev/%s\n", diskname
);
442 debug("find count=%d\n", cnt
);
447 static int vtoy_check_device(ventoy_os_param
*param
, const char *device
)
449 unsigned long long size
;
450 uint8_t vtguid
[16] = {0};
451 uint8_t vtsig
[4] = {0};
453 debug("vtoy_check_device for <%s>\n", device
);
455 size
= vtoy_get_disk_size_in_byte(device
);
456 vtoy_get_disk_guid(device
, vtguid
, vtsig
);
458 debug("param->vtoy_disk_size=%llu size=%llu\n",
459 (unsigned long long)param
->vtoy_disk_size
, (unsigned long long)size
);
461 if (memcmp(vtguid
, param
->vtoy_disk_guid
, 16) == 0 &&
462 memcmp(vtsig
, param
->vtoy_disk_signature
, 4) == 0)
464 debug("<%s> is right ventoy disk\n", device
);
469 debug("<%s> is NOT right ventoy disk\n", device
);
474 static int vtoy_print_os_param(ventoy_os_param
*param
, char *diskname
)
478 char *path
= param
->vtoy_img_path
;
480 char diskpath
[256] = {0};
481 char sizebuf
[64] = {0};
483 cnt
= vtoy_find_disk_by_size(param
->vtoy_disk_size
, diskname
);
484 debug("find disk by size %llu, cnt=%d...\n", (unsigned long long)param
->vtoy_disk_size
, cnt
);
487 if (vtoy_check_device(param
, diskname
) != 0)
494 cnt
= vtoy_find_disk_by_guid(param
, diskname
);
495 debug("find disk by guid cnt=%d...\n", cnt
);
498 if (param
->vtoy_disk_part_type
< ventoy_fs_max
)
500 fs
= g_ventoy_fs
[param
->vtoy_disk_part_type
];
509 if (strstr(diskname
, "nvme") || strstr(diskname
, "mmc") || strstr(diskname
, "nbd"))
511 snprintf(diskpath
, sizeof(diskpath
) - 1, "/sys/class/block/%sp2/size", diskname
);
515 snprintf(diskpath
, sizeof(diskpath
) - 1, "/sys/class/block/%s2/size", diskname
);
518 if (param
->vtoy_reserved
[6] == 0 && access(diskpath
, F_OK
) >= 0)
520 debug("get part size from sysfs for %s\n", diskpath
);
522 fd
= open(diskpath
, O_RDONLY
| O_BINARY
);
525 read(fd
, sizebuf
, sizeof(sizebuf
));
526 size
= (int)strtoull(sizebuf
, NULL
, 10);
528 if ((size
!= (64 * 1024)) && (size
!= (8 * 1024)))
530 debug("sizebuf=<%s> size=%d\n", sizebuf
, size
);
537 debug("%s not exist \n", diskpath
);
540 printf("/dev/%s#%s#%s\n", diskname
, fs
, path
);
550 * Find disk and image path from ventoy runtime data.
551 * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
553 * -f datafile os param data file.
554 * -c /dev/xxx check ventoy disk
556 * -l also print image disk location
558 int vtoydump_main(int argc
, char **argv
)
565 char filename
[256] = {0};
566 char diskname
[256] = {0};
567 char device
[64] = {0};
568 ventoy_os_param
*param
= NULL
;
570 while ((ch
= getopt(argc
, argv
, "c:f:p:t:s:v::")) != -1)
574 strncpy(filename
, optarg
, sizeof(filename
) - 1);
582 strncpy(device
, optarg
, sizeof(device
) - 1);
587 strncpy(filename
, optarg
, sizeof(filename
) - 1);
592 strncpy(filename
, optarg
, sizeof(filename
) - 1);
597 strncpy(filename
, optarg
, sizeof(filename
) - 1);
601 fprintf(stderr
, "Usage: %s -f datafile [ -v ] \n", argv
[0]);
606 if (filename
[0] == 0)
608 fprintf(stderr
, "Usage: %s -f datafile [ -v ] \n", argv
[0]);
612 param
= malloc(sizeof(ventoy_os_param
));
615 fprintf(stderr
, "failed to alloc memory with size %d error %d\n",
616 (int)sizeof(ventoy_os_param
), errno
);
620 memset(param
, 0, sizeof(ventoy_os_param
));
622 debug("get os pararm from file %s\n", filename
);
623 rc
= vtoy_os_param_from_file(filename
, param
);
626 debug("ventoy os param not found %d %d\n", rc
, ENOENT
);
629 debug("now try with file %s\n", "/ventoy/ventoy_os_param");
630 rc
= vtoy_os_param_from_file("/ventoy/ventoy_os_param", param
);
644 vtoy_dump_os_param(param
);
649 rc
= vtoy_printf_iso_path(param
);
653 rc
= vtoy_printf_fs(param
);
657 rc
= vtoy_vlnk_printf(param
, diskname
);
661 rc
= vtoy_check_device(param
, device
);
665 // print os param, you can change the output format in the function
666 rc
= vtoy_print_os_param(param
, diskname
);
678 #ifndef BUILD_VTOY_TOOL
679 int main(int argc
, char **argv
)
681 return vtoydump_main(argc
, argv
);