]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyloader.c
1 /******************************************************************************
2 * vtoyloader.c ---- ventoy loader (wapper for binary loader)
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
33 #define MAX_EXT_PARAM 256
34 #define CMDLINE_BUF_LEN (1024 * 1024 * 1)
35 #define EXEC_PATH_FILE "/ventoy/loader_exec_file"
36 #define CMDLINE_FILE "/ventoy/loader_exec_cmdline"
37 #define HOOK_CMD_FILE "/ventoy/loader_hook_cmd"
38 #define DEBUG_FLAG_FILE "/ventoy/loader_debug"
40 static int verbose
= 0;
41 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
43 static char g_exec_file
[512];
44 static char g_hook_cmd
[512];
46 static int vtoy_read_file_to_buf(const char *file
, void *buf
, int buflen
)
50 fp
= fopen(file
, "r");
53 fprintf(stderr
, "Failed to open file %s err:%d\n", file
, errno
);
56 fread(buf
, 1, buflen
, fp
);
62 int vtoyloader_main(int argc
, char **argv
)
71 if (access(DEBUG_FLAG_FILE
, F_OK
) >= 0)
76 debug("ventoy loader ...\n");
78 rc
= vtoy_read_file_to_buf(EXEC_PATH_FILE
, g_exec_file
, sizeof(g_exec_file
) - 1);
84 if (access(g_exec_file
, F_OK
) < 0)
86 fprintf(stderr
, "File %s not exist\n", g_exec_file
);
90 if (access(HOOK_CMD_FILE
, F_OK
) >= 0)
92 rc
= vtoy_read_file_to_buf(HOOK_CMD_FILE
, g_hook_cmd
, sizeof(g_hook_cmd
) - 1);
93 debug("g_hook_cmd=<%s>\n", g_hook_cmd
);
96 rc
= system(g_hook_cmd
);
97 debug("system return code =<%d> errno=<%d>\n", rc
, errno
);
100 cmdline
= (char *)malloc(CMDLINE_BUF_LEN
);
103 fprintf(stderr
, "Failed to alloc memory err:%d\n", errno
);
106 memset(cmdline
, 0, CMDLINE_BUF_LEN
);
108 if (access(CMDLINE_FILE
, F_OK
) >= 0)
110 rc
= vtoy_read_file_to_buf(CMDLINE_FILE
, cmdline
, CMDLINE_BUF_LEN
- 1);
117 len
= (int)((argc
+ MAX_EXT_PARAM
) * sizeof(char *));
118 cmdlist
= (char **)malloc(len
);
122 fprintf(stderr
, "Failed to alloc memory err:%d\n", errno
);
125 memset(cmdlist
, 0, len
);
127 for (i
= 0; i
< argc
; i
++)
129 cmdlist
[i
] = argv
[i
];
132 cmdlist
[0] = g_exec_file
;
133 debug("g_exec_file=<%s>\n", g_exec_file
);
136 while ((*pos
) && i
< MAX_EXT_PARAM
)
146 else if (*pos
== '\n')
157 debug("execv [%s]...\n", cmdlist
[0]);
159 execv(cmdlist
[0], cmdlist
);
166 #ifndef BUILD_VTOY_TOOL
167 int main(int argc
, char **argv
)
169 return vtoyloader_main(argc
, argv
);