]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoyfat/vtoyfat_linux.c
Add files via upload
[Ventoy.git] / vtoyfat / vtoyfat_linux.c
1 /******************************************************************************
2 * vtoyfat.c ---- Parse fat file system
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 <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <fat_filelib.h>
29
30 static int g_disk_fd = 0;
31
32 static int vtoy_disk_read(uint32 sector, uint8 *buffer, uint32 sector_count)
33 {
34 lseek(g_disk_fd, sector * 512, SEEK_SET);
35 read(g_disk_fd, buffer, sector_count * 512);
36
37 return 1;
38 }
39
40 static int get_ventoy_version(void)
41 {
42 int rc = 1;
43 int size = 0;
44 char *buf = NULL;
45 char *pos = NULL;
46 char *end = NULL;
47 void *flfile = NULL;
48
49 flfile = fl_fopen("/grub/grub.cfg", "rb");
50 if (flfile)
51 {
52 fl_fseek(flfile, 0, SEEK_END);
53 size = (int)fl_ftell(flfile);
54
55 fl_fseek(flfile, 0, SEEK_SET);
56
57 buf = malloc(size + 1);
58 if (buf)
59 {
60 fl_fread(buf, 1, size, flfile);
61 buf[size] = 0;
62
63 pos = strstr(buf, "VENTOY_VERSION=");
64 if (pos)
65 {
66 pos += strlen("VENTOY_VERSION=");
67 if (*pos == '"')
68 {
69 pos++;
70 }
71
72 end = pos;
73 while (*end != 0 && *end != '"' && *end != '\r' && *end != '\n')
74 {
75 end++;
76 }
77
78 *end = 0;
79
80 printf("%s\n", pos);
81 rc = 0;
82 }
83 free(buf);
84 }
85
86 fl_fclose(flfile);
87 }
88
89 return rc;
90 }
91
92 int main(int argc, char **argv)
93 {
94 int rc = 1;
95
96 if (argc != 2)
97 {
98 printf("Usage: vtoyfat /dev/sdb \n");
99 return 1;
100 }
101
102 if (argv[1][0] == '-' && argv[1][1] == 'T')
103 {
104 return 0;
105 }
106
107 g_disk_fd = open(argv[1], O_RDONLY);
108 if (g_disk_fd < 0)
109 {
110 printf("Failed to open %s\n", argv[1]);
111 return 1;
112 }
113
114 fl_init();
115
116 if (0 == fl_attach_media(vtoy_disk_read, NULL))
117 {
118 rc = get_ventoy_version();
119 }
120
121 fl_shutdown();
122
123 close(g_disk_fd);
124
125 return rc;
126 }
127