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