]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoyfat/vtoyfat_linux.c
Fix French language (#2850)
[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 <unistd.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #include <fat_filelib.h>
30
31 static int g_disk_fd = 0;
32
33 static int vtoy_disk_read(uint32 sector, uint8 *buffer, uint32 sector_count)
34 {
35 lseek(g_disk_fd, sector * 512, SEEK_SET);
36 read(g_disk_fd, buffer, sector_count * 512);
37
38 return 1;
39 }
40
41 static int check_secure_boot(void)
42 {
43 void *flfile = NULL;
44
45 flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
46 if (flfile)
47 {
48 fl_fclose(flfile);
49 return 0;
50 }
51
52 return 1;
53 }
54
55 static int get_ventoy_version(void)
56 {
57 int rc = 1;
58 int size = 0;
59 char *buf = NULL;
60 char *pos = NULL;
61 char *end = NULL;
62 void *flfile = NULL;
63
64 flfile = fl_fopen("/grub/grub.cfg", "rb");
65 if (flfile)
66 {
67 fl_fseek(flfile, 0, SEEK_END);
68 size = (int)fl_ftell(flfile);
69
70 fl_fseek(flfile, 0, SEEK_SET);
71
72 buf = malloc(size + 1);
73 if (buf)
74 {
75 fl_fread(buf, 1, size, flfile);
76 buf[size] = 0;
77
78 pos = strstr(buf, "VENTOY_VERSION=");
79 if (pos)
80 {
81 pos += strlen("VENTOY_VERSION=");
82 if (*pos == '"')
83 {
84 pos++;
85 }
86
87 end = pos;
88 while (*end != 0 && *end != '"' && *end != '\r' && *end != '\n')
89 {
90 end++;
91 }
92
93 *end = 0;
94
95 printf("%s\n", pos);
96 rc = 0;
97 }
98 free(buf);
99 }
100
101 fl_fclose(flfile);
102 }
103
104 return rc;
105 }
106
107 int main(int argc, char **argv)
108 {
109 int op = 0;
110 int rc = 1;
111 char *disk;
112
113 if (argc != 2 && argc != 3)
114 {
115 printf("Usage: vtoyfat /dev/sdbs \n");
116 printf("Usage: vtoyfat -s /dev/sdbs \n");
117 return 1;
118 }
119
120 if (argv[1][0] == '-' && argv[1][1] == 'T')
121 {
122 return 0;
123 }
124
125 disk = argv[1];
126 if (argv[1][0] == '-' && argv[1][1] == 's')
127 {
128 op = 1;
129 disk = argv[2];
130 }
131
132 g_disk_fd = open(disk, O_RDONLY);
133 if (g_disk_fd < 0)
134 {
135 printf("Failed to open %s\n", disk);
136 return 1;
137 }
138
139 fl_init();
140
141 if (0 == fl_attach_media(vtoy_disk_read, NULL))
142 {
143 if (op == 0)
144 {
145 rc = get_ventoy_version();
146 }
147 else
148 {
149 rc = check_secure_boot();
150 }
151 }
152
153 fl_shutdown();
154
155 close(g_disk_fd);
156
157 return rc;
158 }
159