forked from ventoy/Ventoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtoyloader.c
175 lines (147 loc) · 4.04 KB
/
vtoyloader.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/******************************************************************************
* vtoyloader.c ---- ventoy loader (wapper for binary loader)
*
* Copyright (c) 2020, longpanda <[email protected]>
*
* 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 <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAX_EXT_PARAM 256
#define CMDLINE_BUF_LEN (1024 * 1024 * 1)
#define EXEC_PATH_FILE "/ventoy/loader_exec_file"
#define CMDLINE_FILE "/ventoy/loader_exec_cmdline"
#define HOOK_CMD_FILE "/ventoy/loader_hook_cmd"
#define DEBUG_FLAG_FILE "/ventoy/loader_debug"
static int verbose = 0;
#define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
static char g_exec_file[512];
static char g_hook_cmd[512];
static int vtoy_read_file_to_buf(const char *file, void *buf, int buflen)
{
FILE *fp;
fp = fopen(file, "r");
if (!fp)
{
fprintf(stderr, "Failed to open file %s err:%d\n", file, errno);
return 1;
}
fread(buf, 1, buflen, fp);
fclose(fp);
return 0;
}
int vtoyloader_main(int argc, char **argv)
{
int i;
int len;
int rc;
char *pos;
char *cmdline;
char **cmdlist;
if (access(DEBUG_FLAG_FILE, F_OK) >= 0)
{
verbose = 1;
}
debug("ventoy loader ...\n");
rc = vtoy_read_file_to_buf(EXEC_PATH_FILE, g_exec_file, sizeof(g_exec_file) - 1);
if (rc)
{
return rc;
}
if (access(g_exec_file, F_OK) < 0)
{
fprintf(stderr, "File %s not exist\n", g_exec_file);
return 1;
}
if (access(HOOK_CMD_FILE, F_OK) >= 0)
{
rc = vtoy_read_file_to_buf(HOOK_CMD_FILE, g_hook_cmd, sizeof(g_hook_cmd) - 1);
debug("g_hook_cmd=<%s>\n", g_hook_cmd);
}
cmdline = (char *)malloc(CMDLINE_BUF_LEN);
if (!cmdline)
{
fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
return 1;
}
memset(cmdline, 0, CMDLINE_BUF_LEN);
if (access(CMDLINE_FILE, F_OK) >= 0)
{
rc = vtoy_read_file_to_buf(CMDLINE_FILE, cmdline, CMDLINE_BUF_LEN - 1);
if (rc)
{
return rc;
}
}
len = (int)((argc + MAX_EXT_PARAM) * sizeof(char *));
cmdlist = (char **)malloc(len);
if (!cmdlist)
{
free(cmdline);
fprintf(stderr, "Failed to alloc memory err:%d\n", errno);
return 1;
}
memset(cmdlist, 0, len);
for (i = 0; i < argc; i++)
{
cmdlist[i] = argv[i];
}
cmdlist[0] = g_exec_file;
debug("g_exec_file=<%s>\n", g_exec_file);
pos = cmdline;
while ((*pos) && i < MAX_EXT_PARAM)
{
cmdlist[i++] = pos;
while (*pos)
{
if (*pos == '\r')
{
*pos = ' ';
}
else if (*pos == '\n')
{
*pos = 0;
pos++;
break;
}
pos++;
}
}
debug("execv [%s]...\n", cmdlist[0]);
// call hook script
if (g_hook_cmd[0])
{
rc = system(g_hook_cmd);
debug("system return code =<%d> errno=<%d>\n", rc, errno);
}
execv(cmdlist[0], cmdlist);
return 0;
}
// wrapper main
#ifndef BUILD_VTOY_TOOL
int main(int argc, char **argv)
{
return vtoyloader_main(argc, argv);
}
#endif