1 // vim:shiftwidth=2:expandtab
11 #include "../common/host_fb.h"
12 #include "../common/cmn.h"
14 #define PFX "ging_prep: "
15 #define LOADER_STATIC "ginge_sloader"
16 #define LOADER_DYNAMIC "ginge_dyn.sh"
17 #define LAUNCHER "gp2xmenu"
20 #define WRAP_APP "op_runfbapp "
29 static int fb_x, fb_y;
32 static char *sskip(char *p)
34 while (p && *p && isspace(*p))
39 static char *cskip(char *p)
41 while (p && *p && !isspace(*p))
46 static void fb_text_init(void)
48 int ret = host_video_init(&fb_stride, 1);
50 fb_mem = host_video_flip();
56 static void fb_syms_out(void *fbi, int x, int y, int dotsz, int stride, const char *text, int count)
58 int v = -1, n = 0, *p;
62 fb = (char *)fbi + x * dotsz + y * stride;
64 for (i = 0; i < count; i++)
66 for (l = 0; l < 8; l++)
68 #define pix(fdmask,add) \
69 p = (fontdata8x8[((text[i])*8)+l] & fdmask) ? &v : &n; \
70 memcpy(fb + l*stride + add*dotsz, p, dotsz)
86 static void fb_text_out(char *text)
88 int dotsz = 2, w = 320; // hardcoded for now
100 for (; *p && isspace(*p); p++) {
101 if (*p == '\n' || fb_x + dotsz * 8 > w) {
111 if (fb_x + 8 * l > w) {
115 fb_syms_out(fb_mem, fb_x, fb_y, dotsz, fb_stride, p, l);
121 static void fbprintf(int is_err, const char *format, ...)
126 va_start(ap, format);
127 vsnprintf(buff, sizeof(buff), format, ap);
129 fputs(buff, is_err ? stderr : stdout);
134 #define msg(fmt, ...) fbprintf(0, fmt, #__VA_ARGS__)
135 #define err(fmt, ...) fbprintf(1, fmt, #__VA_ARGS__)
137 static int id_elf(const char *fname)
140 Elf32_Phdr *phdr = NULL;
144 fi = fopen(fname, "rb");
146 err("open %s: ", fname);
151 if (fread(&hdr, 1, sizeof(hdr), fi) != sizeof(hdr))
154 if (memcmp(hdr.e_ident, ELFMAG "\x01\x01", SELFMAG + 2) != 0)
157 if (hdr.e_phentsize != sizeof(Elf32_Phdr) || hdr.e_phnum == 0)
160 phdr = malloc(hdr.e_phnum * hdr.e_phentsize);
164 if (fread(phdr, hdr.e_phentsize, hdr.e_phnum, fi) != hdr.e_phnum)
169 // do what 'file' does - check for PT_INTERP in program headers
170 for (i = 0; i < hdr.e_phnum; i++) {
171 if (phdr[i].p_type == PT_INTERP) {
183 static void dump_args(FILE *fout, int argc, char * const argv[])
188 for (i = 0; i < argc; i++) {
193 for (p = argv[i]; *p != 0; p++) {
203 static char *get_arg(char *d, size_t size, char *p)
212 if (len > size - 1) {
213 err(PFX "get_arg: buff to small: %d/%d\n", len, size);
222 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
224 #define CB_ENTRY(x) { x, sizeof(x) - 1 }
228 } conv_blacklist[] = {
230 CB_ENTRY("modprobe"),
232 CB_ENTRY("./cpuctrl_tiny"),
235 static int cmd_in_blacklist(char *cmd)
240 for (i = 0; i < ARRAY_SIZE(conv_blacklist); i++)
241 if (strncmp(cmd, conv_blacklist[i].name, conv_blacklist[i].len) == 0)
247 int main(int argc, char *argv[])
249 static const char out_script[] = "/tmp/ginge_conv.sh";
250 char root_path[512], cwd[512];
252 int rerun_gp2xmenu = 1;
257 err("usage: %s <script|program> [args]\n", argv[0]);
261 if (strcmp(argv[1], "--cleanup") == 0) {
262 // as loader may crash eny time, restore screen for them menu
263 host_video_init(NULL, 0);
268 if (getcwd(cwd, sizeof(cwd)) == NULL) {
269 err(PFX "failed to get cwd\n");
273 ret = make_local_path(root_path, sizeof(root_path), "");
275 err(PFX "failed to generate root path\n");
279 fout = fopen(out_script, "w");
281 perror("can't open output script");
285 fprintf(fout, "#!/bin/sh\n");
287 ret = id_elf(argv[1]);
288 if (ret == 1 || ret == 2) {
289 if (cmd_in_blacklist(argv[1])) {
290 fprintf(stderr, "blacklisted: %s\n", argv[1]);
300 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
301 dump_args(fout, argc - 1, &argv[1]);
306 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
307 dump_args(fout, argc - 1, &argv[1]);
316 fin = fopen(argv[1], "r");
321 char buff[512], fname[512], *p, *p2;
323 p = fgets(buff, sizeof(buff), fin);
328 if (p[0] == '#' && p[1] == '!')
336 // things we are sure we want to pass
337 if (*p == '#' || strncmp(p, "export ", 7) == 0)
341 if (strncmp(p, "exec ", 5) == 0)
344 // blacklist some stuff
345 if (strncmp(p, "/sbin/", 6) == 0)
347 else if (strncmp(p, "/bin/", 5) == 0)
351 if (strncmp(p2, "mount ", 6) == 0) {
354 if (strstr(p2, "cramfs")) {
357 p2 = sskip(cskip(p2));
358 p2 = sskip(cskip(p2));
361 err(PFX "cramfs: missing mount file in \"%s\"?\n", p);
364 p2 = get_arg(fname, sizeof(fname), p2);
366 err(PFX "cramfs: missing mount point in \"%s\"?\n", p);
369 get_arg(buff, sizeof(buff), p2);
370 fprintf(fout, "if [ `ls %s | wc -l` -eq 0 ]; then\n", buff);
371 fprintf(fout, " rmdir \"%s\"\n", buff); // cramfsck doesn't want it
372 fprintf(fout, " %stools/cramfsck -x \"%s\" \"%s\"\n", root_path, buff, fname);
373 fprintf(fout, "fi\n");
378 if (cmd_in_blacklist(p2))
382 if (strncmp(p, "cd ", 3) == 0) {
383 get_arg(fname, sizeof(fname), p + 3);
384 if (strncmp(fname, "/usr/gp2x", 9) == 0)
393 // trying to run something from cwd?
394 if ((p[0] == '.' && p[1] == '/') || *p == '/') {
395 get_arg(fname, sizeof(fname), p);
396 p2 = strrchr(fname, '/');
397 if (p2 != NULL && strcmp(p2 + 1, "gp2xmenu") == 0)
403 printf(PFX "prefixing as static: %s", p);
404 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
408 printf(PFX "prefixing as dynamic: %s", p);
409 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
425 fprintf(fout, "sync\n");
426 fprintf(fout, "%sginge_prep --cleanup\n", root_path);
428 if (rerun_gp2xmenu) {
429 fprintf(fout, "cd %s\n", root_path);
430 fprintf(fout, "exec %s%s\n", root_path, LAUNCHER);
435 //msg("starting script..\n");
437 msg("\nsome files need to be unpacked, this may tike a few minutes.\n");
439 msg("Please wait at least while SD LED is active.\n");
442 system("echo ---; cat /tmp/ginge_conv.sh; echo ---");
443 chmod(out_script, S_IRWXU|S_IRWXG|S_IRWXO);
445 execlp(out_script, out_script, NULL);
446 perror("run out_script");