]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoydump.c
1.0.10 release
[Ventoy.git] / VtoyTool / vtoydump.c
1 /******************************************************************************
2 * vtoydump.c ---- Dump ventoy os parameters
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <linux/fs.h>
33 #include <dirent.h>
34
35 #define IS_DIGIT(x) ((x) >= '0' && (x) <= '9')
36
37 #ifndef USE_DIET_C
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;
42 #endif
43
44 #define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
45
46 typedef enum ventoy_fs_type
47 {
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
54 ventoy_fs_max
55 }ventoy_fs_type;
56
57 #pragma pack(1)
58
59 typedef struct ventoy_guid
60 {
61 uint32_t data1;
62 uint16_t data2;
63 uint16_t data3;
64 uint8_t data4[8];
65 }ventoy_guid;
66
67
68 typedef struct ventoy_image_disk_region
69 {
70 uint32_t image_sector_count; /* image sectors contained in this region */
71 uint32_t image_start_sector; /* image sector start */
72 uint64_t disk_start_sector; /* disk sector start */
73 }ventoy_image_disk_region;
74
75 typedef struct ventoy_image_location
76 {
77 ventoy_guid guid;
78
79 /* image sector size, currently this value is always 2048 */
80 uint32_t image_sector_size;
81
82 /* disk sector size, normally the value is 512 */
83 uint32_t disk_sector_size;
84
85 uint32_t region_count;
86
87 /*
88 * disk region data
89 * If the image file has more than one fragments in disk,
90 * there will be more than one region data here.
91 * You can calculate the region count by
92 */
93 ventoy_image_disk_region regions[1];
94
95 /* ventoy_image_disk_region regions[2~region_count-1] */
96 }ventoy_image_location;
97
98 typedef struct ventoy_os_param
99 {
100 ventoy_guid guid; // VENTOY_GUID
101 uint8_t chksum; // checksum
102
103 uint8_t vtoy_disk_guid[16];
104 uint64_t vtoy_disk_size; // disk size in bytes
105 uint16_t vtoy_disk_part_id; // begin with 1
106 uint16_t vtoy_disk_part_type; // 0:exfat 1:ntfs other: reserved
107 char vtoy_img_path[384]; // It seems to be enough, utf-8 format
108 uint64_t vtoy_img_size; // image file size in bytes
109
110 /*
111 * Ventoy will write a copy of ventoy_image_location data into runtime memory
112 * this is the physically address and length of that memory.
113 * Address 0 means no such data exist.
114 * Address will be aligned by 4KB.
115 *
116 */
117 uint64_t vtoy_img_location_addr;
118 uint32_t vtoy_img_location_len;
119
120 uint64_t vtoy_reserved[4]; // Internal use by ventoy
121
122 uint8_t reserved[31];
123 }ventoy_os_param;
124
125 #pragma pack()
126
127 #ifndef O_BINARY
128 #define O_BINARY 0
129 #endif
130 #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
131 #define MMAP_FLAGS MAP_SHARED
132 #else
133 #define MMAP_FLAGS MAP_PRIVATE
134 #endif
135
136 #define SEARCH_MEM_START 0x80000
137 #define SEARCH_MEM_LEN 0x1c000
138
139 static int verbose = 0;
140 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
141
142 static ventoy_guid vtoy_guid = VENTOY_GUID;
143
144 static const char *g_ventoy_fs[ventoy_fs_max] =
145 {
146 "exfat", "ntfs", "ext*", "xfs", "udf"
147 };
148
149 static int vtoy_check_os_param(ventoy_os_param *param)
150 {
151 uint32_t i;
152 uint8_t chksum = 0;
153 uint8_t *buf = (uint8_t *)param;
154
155 if (memcmp(&param->guid, &vtoy_guid, sizeof(ventoy_guid)))
156 {
157 return 1;
158 }
159
160 for (i = 0; i < sizeof(ventoy_os_param); i++)
161 {
162 chksum += buf[i];
163 }
164
165 if (chksum)
166 {
167 debug("Invalid checksum 0x%02x\n", chksum);
168 return 1;
169 }
170
171 return 0;
172 }
173
174 static int vtoy_os_param_from_file(const char *filename, ventoy_os_param *param)
175 {
176 int fd = 0;
177 int rc = 0;
178
179 fd = open(filename, O_RDONLY | O_BINARY);
180 if (fd < 0)
181 {
182 fprintf(stderr, "Failed to open file %s error %d\n", filename, errno);
183 return errno;
184 }
185
186 read(fd, param, sizeof(ventoy_os_param));
187
188 if (vtoy_check_os_param(param) == 0)
189 {
190 debug("find ventoy os param in file %s\n", filename);
191 }
192 else
193 {
194 debug("ventoy os pararm NOT found in file %s\n", filename);
195 rc = 1;
196 }
197
198 close(fd);
199 return rc;
200 }
201
202 static void vtoy_dump_os_param(ventoy_os_param *param)
203 {
204 printf("################# dump os param ################\n");
205
206 printf("param->chksum = 0x%x\n", param->chksum);
207 printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
208 param->vtoy_disk_guid[0], param->vtoy_disk_guid[1],
209 param->vtoy_disk_guid[2], param->vtoy_disk_guid[3]);
210 printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param->vtoy_disk_size);
211 printf("param->vtoy_disk_part_id = %u\n", param->vtoy_disk_part_id);
212 printf("param->vtoy_disk_part_type = %u\n", param->vtoy_disk_part_type);
213 printf("param->vtoy_img_path = <%s>\n", param->vtoy_img_path);
214 printf("param->vtoy_img_size = <%llu>\n", (unsigned long long)param->vtoy_img_size);
215 printf("param->vtoy_img_location_addr = <0x%llx>\n", (unsigned long long)param->vtoy_img_location_addr);
216 printf("param->vtoy_img_location_len = <%u>\n", param->vtoy_img_location_len);
217 printf("param->vtoy_reserved[0] = 0x%llx\n", (unsigned long long)param->vtoy_reserved[0]);
218 printf("param->vtoy_reserved[1] = 0x%llx\n", (unsigned long long)param->vtoy_reserved[1]);
219
220 printf("\n");
221 }
222
223 static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid)
224 {
225 int i = 0;
226 int fd = 0;
227 char devdisk[128] = {0};
228
229 snprintf(devdisk, sizeof(devdisk) - 1, "/dev/%s", diskname);
230
231 fd = open(devdisk, O_RDONLY | O_BINARY);
232 if (fd >= 0)
233 {
234 lseek(fd, 0x180, SEEK_SET);
235 read(fd, vtguid, 16);
236 close(fd);
237
238 debug("GUID for %s: <", devdisk);
239 for (i = 0; i < 16; i++)
240 {
241 debug("%02x", vtguid[i]);
242 }
243 debug(">\n");
244
245 return 0;
246 }
247 else
248 {
249 debug("failed to open %s %d\n", devdisk, errno);
250 return errno;
251 }
252 }
253
254 static unsigned long long vtoy_get_disk_size_in_byte(const char *disk)
255 {
256 int fd;
257 int rc;
258 unsigned long long size = 0;
259 char diskpath[256] = {0};
260 char sizebuf[64] = {0};
261
262 // Try 1: get size from sysfs
263 snprintf(diskpath, sizeof(diskpath) - 1, "/sys/block/%s/size", disk);
264 if (access(diskpath, F_OK) >= 0)
265 {
266 debug("get disk size from sysfs for %s\n", disk);
267
268 fd = open(diskpath, O_RDONLY | O_BINARY);
269 if (fd >= 0)
270 {
271 read(fd, sizebuf, sizeof(sizebuf));
272 size = strtoull(sizebuf, NULL, 10);
273 close(fd);
274 return (size * 512);
275 }
276 }
277 else
278 {
279 debug("%s not exist \n", diskpath);
280 }
281
282 // Try 2: get size from ioctl
283 snprintf(diskpath, sizeof(diskpath) - 1, "/dev/%s", disk);
284 fd = open(diskpath, O_RDONLY);
285 if (fd >= 0)
286 {
287 debug("get disk size from ioctl for %s\n", disk);
288 rc = ioctl(fd, BLKGETSIZE64, &size);
289 if (rc == -1)
290 {
291 size = 0;
292 debug("failed to ioctl %d\n", rc);
293 }
294 close(fd);
295 }
296 else
297 {
298 debug("failed to open %s %d\n", diskpath, errno);
299 }
300
301 debug("disk %s size %llu bytes\n", disk, (unsigned long long)size);
302 return size;
303 }
304
305 static int vtoy_is_possible_blkdev(const char *name)
306 {
307 if (name[0] == '.')
308 {
309 return 0;
310 }
311
312 /* /dev/ramX */
313 if (name[0] == 'r' && name[1] == 'a' && name[2] == 'm')
314 {
315 return 0;
316 }
317
318 /* /dev/loopX */
319 if (name[0] == 'l' && name[1] == 'o' && name[2] == 'o' && name[3] == 'p')
320 {
321 return 0;
322 }
323
324 /* /dev/dm-X */
325 if (name[0] == 'd' && name[1] == 'm' && name[2] == '-' && IS_DIGIT(name[3]))
326 {
327 return 0;
328 }
329
330 /* /dev/srX */
331 if (name[0] == 's' && name[1] == 'r' && IS_DIGIT(name[2]))
332 {
333 return 0;
334 }
335
336 return 1;
337 }
338
339 static int vtoy_find_disk_by_size(unsigned long long size, char *diskname)
340 {
341 unsigned long long cursize = 0;
342 DIR* dir = NULL;
343 struct dirent* p = NULL;
344 int rc = 0;
345
346 dir = opendir("/sys/block");
347 if (!dir)
348 {
349 return 0;
350 }
351
352 while ((p = readdir(dir)) != NULL)
353 {
354 if (!vtoy_is_possible_blkdev(p->d_name))
355 {
356 debug("disk %s is filted by name\n", p->d_name);
357 continue;
358 }
359
360 cursize = vtoy_get_disk_size_in_byte(p->d_name);
361 debug("disk %s size %llu\n", p->d_name, (unsigned long long)cursize);
362 if (cursize == size)
363 {
364 sprintf(diskname, "%s", p->d_name);
365 rc++;
366 }
367 }
368 closedir(dir);
369 return rc;
370 }
371
372 static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname)
373 {
374 int rc = 0;
375 int count = 0;
376 DIR* dir = NULL;
377 struct dirent* p = NULL;
378 uint8_t vtguid[16];
379
380 dir = opendir("/sys/block");
381 if (!dir)
382 {
383 return 0;
384 }
385
386 while ((p = readdir(dir)) != NULL)
387 {
388 if (!vtoy_is_possible_blkdev(p->d_name))
389 {
390 debug("disk %s is filted by name\n", p->d_name);
391 continue;
392 }
393
394 memset(vtguid, 0, sizeof(vtguid));
395 rc = vtoy_get_disk_guid(p->d_name, vtguid);
396 if (rc == 0 && memcmp(vtguid, guid, 16) == 0)
397 {
398 sprintf(diskname, "%s", p->d_name);
399 count++;
400 }
401 }
402 closedir(dir);
403
404 return count;
405 }
406
407 static int vtoy_printf_iso_path(ventoy_os_param *param)
408 {
409 printf("%s\n", param->vtoy_img_path);
410 return 0;
411 }
412
413 static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
414 {
415 int cnt = 0;
416 char *path = param->vtoy_img_path;
417 const char *fs;
418
419 cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
420 if (cnt > 1)
421 {
422 cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
423 }
424 else if (cnt == 0)
425 {
426 cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
427 debug("find 0 disk by size, try with guid cnt=%d...\n", cnt);
428 }
429
430 if (param->vtoy_disk_part_type < ventoy_fs_max)
431 {
432 fs = g_ventoy_fs[param->vtoy_disk_part_type];
433 }
434 else
435 {
436 fs = "unknown";
437 }
438
439 if (1 == cnt)
440 {
441 printf("/dev/%s#%s#%s\n", diskname, fs, path);
442 return 0;
443 }
444 else
445 {
446 return 1;
447 }
448 }
449
450 static int vtoy_check_device(ventoy_os_param *param, const char *device)
451 {
452 unsigned long long size;
453 uint8_t vtguid[16] = {0};
454
455 debug("vtoy_check_device for <%s>\n", device);
456
457 size = vtoy_get_disk_size_in_byte(device);
458 vtoy_get_disk_guid(device, vtguid);
459
460 debug("param->vtoy_disk_size=%llu size=%llu\n",
461 (unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
462
463 if ((param->vtoy_disk_size == size || param->vtoy_disk_size == size + 512) &&
464 memcmp(vtguid, param->vtoy_disk_guid, 16) == 0)
465 {
466 debug("<%s> is right ventoy disk\n", device);
467 return 0;
468 }
469 else
470 {
471 debug("<%s> is NOT right ventoy disk\n", device);
472 return 1;
473 }
474 }
475
476 /*
477 * Find disk and image path from ventoy runtime data.
478 * By default data is read from phymem(legacy bios) or efivar(UEFI), if -f is input, data is read from file.
479 *
480 * -f datafile os param data file.
481 * -c /dev/xxx check ventoy disk
482 * -v be verbose
483 * -l also print image disk location
484 */
485 int vtoydump_main(int argc, char **argv)
486 {
487 int rc;
488 int ch;
489 int print_path = 0;
490 char filename[256] = {0};
491 char diskname[256] = {0};
492 char device[64] = {0};
493 ventoy_os_param *param = NULL;
494
495 while ((ch = getopt(argc, argv, "c:f:p:v::")) != -1)
496 {
497 if (ch == 'f')
498 {
499 strncpy(filename, optarg, sizeof(filename) - 1);
500 }
501 else if (ch == 'v')
502 {
503 verbose = 1;
504 }
505 else if (ch == 'c')
506 {
507 strncpy(device, optarg, sizeof(device) - 1);
508 }
509 else if (ch == 'p')
510 {
511 print_path = 1;
512 strncpy(filename, optarg, sizeof(filename) - 1);
513 }
514 else
515 {
516 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
517 return 1;
518 }
519 }
520
521 if (filename[0] == 0)
522 {
523 fprintf(stderr, "Usage: %s -f datafile [ -v ] \n", argv[0]);
524 return 1;
525 }
526
527 param = malloc(sizeof(ventoy_os_param));
528 if (NULL == param)
529 {
530 fprintf(stderr, "failed to alloc memory with size %d error %d\n",
531 (int)sizeof(ventoy_os_param), errno);
532 return 1;
533 }
534
535 memset(param, 0, sizeof(ventoy_os_param));
536
537 debug("get os pararm from file %s\n", filename);
538 rc = vtoy_os_param_from_file(filename, param);
539 if (rc)
540 {
541 debug("ventoy os param not found %d\n", rc);
542 goto end;
543 }
544
545 if (verbose)
546 {
547 vtoy_dump_os_param(param);
548 }
549
550 if (print_path)
551 {
552 rc = vtoy_printf_iso_path(param);
553 }
554 else if (device[0])
555 {
556 rc = vtoy_check_device(param, device);
557 }
558 else
559 {
560 // print os param, you can change the output format in the function
561 rc = vtoy_print_os_param(param, diskname);
562 }
563
564 end:
565 if (param)
566 {
567 free(param);
568 }
569 return rc;
570 }
571
572 // wrapper main
573 #ifndef BUILD_VTOY_TOOL
574 int main(int argc, char **argv)
575 {
576 return vtoydump_main(argc, argv);
577 }
578 #endif
579