vtoyfat_linux.c 3.32 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/******************************************************************************
 * vtoyfat.c  ---- Parse fat file system
 *
 * Copyright (c) 2020, longpanda <admin@ventoy.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h> 
#include <fcntl.h>

#include <fat_filelib.h>

static int g_disk_fd = 0;

static int vtoy_disk_read(uint32 sector, uint8 *buffer, uint32 sector_count)
{
    lseek(g_disk_fd, sector * 512, SEEK_SET);
    read(g_disk_fd, buffer, sector_count * 512);
    
    return 1;
}

longpanda's avatar
longpanda committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
static int check_secure_boot(void)
{
    void *flfile = NULL;
    
    flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
    if (flfile)
    {
        fl_fclose(flfile);
        return 0;
    }
    
    return 1;
}

longpanda's avatar
longpanda committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static int get_ventoy_version(void)
{
    int rc = 1;
    int size = 0;
    char *buf = NULL;
    char *pos = NULL;
    char *end = NULL;
    void *flfile = NULL;

    flfile = fl_fopen("/grub/grub.cfg", "rb");
    if (flfile)
    {
        fl_fseek(flfile, 0, SEEK_END);
        size = (int)fl_ftell(flfile);

        fl_fseek(flfile, 0, SEEK_SET);

        buf = malloc(size + 1);
        if (buf)
        {
            fl_fread(buf, 1, size, flfile);
            buf[size] = 0;

            pos = strstr(buf, "VENTOY_VERSION=");
            if (pos)
            {
                pos += strlen("VENTOY_VERSION=");
                if (*pos == '"')
                {
                    pos++;
                }

                end = pos;
                while (*end != 0 && *end != '"' && *end != '\r' && *end != '\n')
                {
                    end++;
                }

                *end = 0;

                printf("%s\n", pos);
                rc = 0;
            }
            free(buf);
        }

        fl_fclose(flfile);
    }

    return rc;
}

int main(int argc, char **argv)
{
longpanda's avatar
longpanda committed
108
    int op = 0;
longpanda's avatar
longpanda committed
109
    int rc = 1;
longpanda's avatar
longpanda committed
110
    char *disk;
longpanda's avatar
longpanda committed
111

longpanda's avatar
longpanda committed
112
    if (argc != 2 && argc != 3)
longpanda's avatar
longpanda committed
113
    {   
longpanda's avatar
longpanda committed
114
115
        printf("Usage: vtoyfat /dev/sdbs \n");
        printf("Usage: vtoyfat -s /dev/sdbs \n");
longpanda's avatar
longpanda committed
116
117
118
119
120
121
122
123
        return 1;
    }
    
    if (argv[1][0] == '-' && argv[1][1] == 'T')
    {
        return 0;
    }
    
longpanda's avatar
longpanda committed
124
125
126
127
128
129
130
131
    disk = argv[1];
    if (argv[1][0] == '-' && argv[1][1] == 's')
    {
        op = 1;
        disk = argv[2];
    } 
    
    g_disk_fd = open(disk, O_RDONLY);
longpanda's avatar
longpanda committed
132
133
    if (g_disk_fd < 0)
    {
longpanda's avatar
longpanda committed
134
        printf("Failed to open %s\n", disk);
longpanda's avatar
longpanda committed
135
136
137
138
139
140
141
        return 1;
    }

    fl_init();

    if (0 == fl_attach_media(vtoy_disk_read, NULL))
    {
longpanda's avatar
longpanda committed
142
143
144
145
146
147
148
149
        if (op == 0)
        {
            rc = get_ventoy_version();
        }
        else
        {
            rc = check_secure_boot();
        }        
longpanda's avatar
longpanda committed
150
151
152
153
154
155
156
157
158
    }

    fl_shutdown();

    close(g_disk_fd);

    return rc;
}