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_exit(void)
55 static void fb_text_init(void)
57 int ret = host_video_init(&fb_stride, 1);
59 fb_mem = host_video_flip();
66 static void fb_syms_out(void *fbi, int x, int y, int dotsz, int stride, const char *text, int count)
68 int v = -1, n = 0, *p;
72 fb = (char *)fbi + x * dotsz + y * stride;
74 for (i = 0; i < count; i++)
76 for (l = 0; l < 8; l++)
78 #define pix(fdmask,add) \
79 p = (fontdata8x8[((text[i])*8)+l] & fdmask) ? &v : &n; \
80 memcpy(fb + l*stride + add*dotsz, p, dotsz)
96 static void fb_text_out(char *text)
98 int dotsz = 2, w = 320; // hardcoded for now
110 for (; *p && isspace(*p); p++) {
111 if (*p == '\n' || fb_x + dotsz * 8 > w) {
121 if (fb_x + 8 * l > w) {
125 fb_syms_out(fb_mem, fb_x, fb_y, dotsz, fb_stride, p, l);
131 static void fbprintf(int is_err, const char *format, ...)
136 va_start(ap, format);
137 vsnprintf(buff, sizeof(buff), format, ap);
139 fputs(buff, is_err ? stderr : stdout);
144 #define msg(fmt, ...) fbprintf(0, fmt, ##__VA_ARGS__)
145 #define err(fmt, ...) fbprintf(1, fmt, ##__VA_ARGS__)
147 static int id_elf(const char *fname)
150 Elf32_Phdr *phdr = NULL;
154 fi = fopen(fname, "rb");
156 err("open %s: ", fname);
161 if (fread(&hdr, 1, sizeof(hdr), fi) != sizeof(hdr))
164 if (memcmp(hdr.e_ident, ELFMAG "\x01\x01", SELFMAG + 2) != 0)
167 if (hdr.e_phentsize != sizeof(Elf32_Phdr) || hdr.e_phnum == 0)
170 phdr = malloc(hdr.e_phnum * hdr.e_phentsize);
174 if (fread(phdr, hdr.e_phentsize, hdr.e_phnum, fi) != hdr.e_phnum)
179 // do what 'file' does - check for PT_INTERP in program headers
180 for (i = 0; i < hdr.e_phnum; i++) {
181 if (phdr[i].p_type == PT_INTERP) {
193 static void dump_args(FILE *fout, char * const argv[])
198 for (i = 0; argv[i] != NULL; i++) {
203 for (p = argv[i]; *p != 0; p++) {
213 static char *get_arg(char *d, size_t size, char *p)
222 if (len > size - 1) {
223 err(PFX "get_arg: buff to small: %d/%d\n", len, size);
232 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
234 #define CB_ENTRY(x) { x, sizeof(x) - 1 }
238 } conv_blacklist[] = {
240 CB_ENTRY("modprobe"),
242 CB_ENTRY("./cpuctrl_tiny"),
245 static int cmd_in_blacklist(char *cmd)
250 for (i = 0; i < ARRAY_SIZE(conv_blacklist); i++)
251 if (strncmp(cmd, conv_blacklist[i].name, conv_blacklist[i].len) == 0)
257 int main(int argc, char *argv[])
259 static const char out_script[] = "/tmp/ginge_conv.sh";
260 char root_path[512], cwd[512];
261 char **argv_app = NULL;
263 int rerun_gp2xmenu = 1;
264 int quit_if_no_app = 0;
268 for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] == '-'; i++) {
269 if (strcmp(argv[i], "--cleanup") == 0) {
270 // as loader may crash eny time, restore screen for the menu
271 host_video_init(NULL, 1);
276 if (strcmp(argv[i], "--nomenu") == 0) {
280 if (strcmp(argv[i], "--") == 0) {
285 fprintf(stderr, PFX "ignoring unknown option \"%s\"\n", argv[i]);
291 err("usage: %s [opts] <script|program> [args]\n", argv[0]);
292 err(" --cleanup - restore framebuffer state\n");
293 err(" --nomenu - don't run menu on exit\n");
298 if (getcwd(cwd, sizeof(cwd)) == NULL) {
299 err(PFX "failed to get cwd\n");
303 ret = make_local_path(root_path, sizeof(root_path), "");
305 err(PFX "failed to generate root path\n");
309 fout = fopen(out_script, "w");
311 perror("can't open output script");
315 fprintf(fout, "#!/bin/sh\n");
317 ret = id_elf(argv_app[0]);
318 if (ret == 1 || ret == 2) {
319 if (cmd_in_blacklist(argv_app[0])) {
320 fprintf(stderr, "blacklisted: %s\n", argv_app[0]);
330 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
331 dump_args(fout, argv_app);
336 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
337 dump_args(fout, argv_app);
346 fin = fopen(argv_app[0], "r");
351 char buff[512], fname[512], *p, *p2;
353 p = fgets(buff, sizeof(buff), fin);
358 if (p[0] == '#' && p[1] == '!')
366 // things we are sure we want to pass
367 if (*p == '#' || strncmp(p, "export ", 7) == 0)
371 if (strncmp(p, "exec ", 5) == 0)
374 // blacklist some stuff
375 if (strncmp(p, "/sbin/", 6) == 0)
377 else if (strncmp(p, "/bin/", 5) == 0)
381 if (strncmp(p2, "mount ", 6) == 0) {
384 if (strstr(p2, "cramfs")) {
387 p2 = sskip(cskip(p2));
388 p2 = sskip(cskip(p2));
391 err(PFX "cramfs: missing mount file in \"%s\"?\n", p);
394 p2 = get_arg(fname, sizeof(fname), p2);
396 err(PFX "cramfs: missing mount point in \"%s\"?\n", p);
399 get_arg(buff, sizeof(buff), p2);
400 fprintf(fout, "if [ `ls %s | wc -l` -eq 0 ]; then\n", buff);
401 fprintf(fout, " rmdir \"%s\"\n", buff); // cramfsck doesn't want it
402 fprintf(fout, " %stools/cramfsck -x \"%s\" \"%s\"\n", root_path, buff, fname);
403 fprintf(fout, "fi\n");
408 if (cmd_in_blacklist(p2))
412 if (strncmp(p, "cd ", 3) == 0) {
413 get_arg(fname, sizeof(fname), p + 3);
414 if (strncmp(fname, "/usr/gp2x", 9) == 0)
423 // trying to run something from cwd?
424 if ((p[0] == '.' && p[1] == '/') || *p == '/') {
425 get_arg(fname, sizeof(fname), p);
426 p2 = strrchr(fname, '/');
427 if (p2 != NULL && strcmp(p2 + 1, "gp2xmenu") == 0)
433 printf(PFX "prefixing as static: %s", p);
434 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
438 printf(PFX "prefixing as dynamic: %s", p);
439 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
455 fprintf(fout, "sync\n");
456 // since we don't know if loader manages to do proper cleanup,
457 // need to wait for it's threads to die
458 fprintf(fout, "sleep 1\n");
459 fprintf(fout, "%sginge_prep --cleanup\n", root_path);
461 if (rerun_gp2xmenu) {
462 fprintf(fout, "cd %s\n", root_path);
463 fprintf(fout, "exec %s%s\n", root_path, LAUNCHER);
468 //msg("starting script..\n");
470 msg("\nsome files need to be unpacked, this may tike a few minutes.\n");
472 msg("Please wait at least while SD LED is active.\n");
475 system("echo ---; cat /tmp/ginge_conv.sh; echo ---");
476 chmod(out_script, S_IRWXU|S_IRWXG|S_IRWXO);
479 execlp(out_script, out_script, NULL);
480 perror("run out_script");