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"
23 static int fb_x, fb_y;
25 static char *sskip(char *p)
27 while (p && *p && isspace(*p))
32 static char *cskip(char *p)
34 while (p && *p && !isspace(*p))
39 static void fb_syms_out(void *fbi, int x, int y, int dotsz, int stride, const char *text, int count)
41 int v = -1, n = 0, *p;
45 fb = (char *)fbi + x * dotsz + y * stride;
47 for (i = 0; i < count; i++)
49 for (l = 0; l < 8; l++)
51 #define pix(fdmask,add) \
52 p = (fontdata8x8[((text[i])*8)+l] & fdmask) ? &v : &n; \
53 memcpy(fb + l*stride + add*dotsz, p, dotsz)
69 static void fb_text_out(char *text)
71 int dotsz = 2, w = 320; // hardcoded for now
80 for (; *p && isspace(*p); p++) {
81 if (*p == '\n' || fb_x + dotsz * 8 > w) {
91 if (fb_x + 8 * l > w) {
95 fb_syms_out(fb_mem, fb_x, fb_y, dotsz, fb_stride, p, l);
101 static void fb_text_init(void)
103 int ret = host_video_init(&fb_stride, 1);
105 fb_mem = host_video_flip();
110 static void fbprintf(int is_err, const char *format, ...)
115 va_start(ap, format);
116 vsnprintf(buff, sizeof(buff), format, ap);
118 fputs(buff, is_err ? stderr : stdout);
123 #define msg(fmt, ...) fbprintf(0, fmt, #__VA_ARGS__)
124 #define err(fmt, ...) fbprintf(1, fmt, #__VA_ARGS__)
126 static int id_elf(const char *fname)
129 Elf32_Phdr *phdr = NULL;
133 fi = fopen(fname, "rb");
135 err("open %s: ", fname);
140 if (fread(&hdr, 1, sizeof(hdr), fi) != sizeof(hdr))
143 if (memcmp(hdr.e_ident, ELFMAG "\x01\x01", SELFMAG + 2) != 0)
146 if (hdr.e_phentsize != sizeof(Elf32_Phdr) || hdr.e_phnum == 0)
149 phdr = malloc(hdr.e_phnum * hdr.e_phentsize);
153 if (fread(phdr, hdr.e_phentsize, hdr.e_phnum, fi) != hdr.e_phnum)
158 // do what 'file' does - check for PT_INTERP in program headers
159 for (i = 0; i < hdr.e_phnum; i++) {
160 if (phdr[i].p_type == PT_INTERP) {
172 static void dump_args(FILE *fout, int argc, char * const argv[])
177 for (i = 0; i < argc; i++) {
182 for (p = argv[i]; *p != 0; p++) {
192 static char *get_arg(char *d, size_t size, char *p)
201 if (len > size - 1) {
202 err(PFX "get_arg: buff to small: %d/%d\n", len, size);
211 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
213 #define CB_ENTRY(x) { x, sizeof(x) - 1 }
217 } conv_blacklist[] = {
219 CB_ENTRY("modprobe"),
221 CB_ENTRY("./cpuctrl_tiny"),
224 static int cmd_in_blacklist(char *cmd)
229 for (i = 0; i < ARRAY_SIZE(conv_blacklist); i++)
230 if (strncmp(cmd, conv_blacklist[i].name, conv_blacklist[i].len) == 0)
236 int main(int argc, char *argv[])
238 static const char out_script[] = "/tmp/ginge_conv.sh";
239 char root_path[512], cwd[512];
241 int rerun_gp2xmenu = 1;
248 err("usage: %s <script|program> [args]\n", argv[0]);
252 if (getcwd(cwd, sizeof(cwd)) == NULL) {
253 err(PFX "failed to get cwd\n");
257 ret = make_local_path(root_path, sizeof(root_path), "");
259 err(PFX "failed to generate root path\n");
263 fout = fopen(out_script, "w");
265 perror("can't open output script");
269 fprintf(fout, "#!/bin/sh\n");
271 ret = id_elf(argv[1]);
272 if (ret == 1 || ret == 2) {
273 if (cmd_in_blacklist(argv[1])) {
274 fprintf(stderr, "blacklisted: %s\n", argv[1]);
284 fprintf(fout, "op_runfbapp %s%s ", root_path, LOADER_STATIC);
285 dump_args(fout, argc - 1, &argv[1]);
290 fprintf(fout, "op_runfbapp %s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
291 dump_args(fout, argc - 1, &argv[1]);
300 fin = fopen(argv[1], "r");
305 char buff[512], fname[512], *p, *p2;
307 p = fgets(buff, sizeof(buff), fin);
312 if (p[0] == '#' && p[1] == '!')
320 // things we are sure we want to pass
321 if (*p == '#' || strncmp(p, "export ", 7) == 0)
325 if (strncmp(p, "exec ", 5) == 0)
328 // blacklist some stuff
329 if (strncmp(p, "/sbin/", 6) == 0)
331 else if (strncmp(p, "/bin/", 5) == 0)
335 if (strncmp(p2, "mount ", 6) == 0) {
338 if (strstr(p2, "cramfs")) {
341 p2 = sskip(cskip(p2));
342 p2 = sskip(cskip(p2));
345 err(PFX "cramfs: missing mount file in \"%s\"?\n", p);
348 p2 = get_arg(fname, sizeof(fname), p2);
350 err(PFX "cramfs: missing mount point in \"%s\"?\n", p);
353 get_arg(buff, sizeof(buff), p2);
354 fprintf(fout, "if [ `ls %s | wc -l` -eq 0 ]; then\n", buff);
355 fprintf(fout, " rmdir \"%s\"\n", buff); // cramfsck doesn't want it
356 fprintf(fout, " %stools/cramfsck -x \"%s\" \"%s\"\n", root_path, buff, fname);
357 fprintf(fout, "fi\n");
362 if (cmd_in_blacklist(p2))
366 if (strncmp(p, "cd ", 3) == 0) {
367 get_arg(fname, sizeof(fname), p + 3);
368 if (strncmp(fname, "/usr/gp2x", 9) == 0)
377 // trying to run something from cwd?
378 if ((p[0] == '.' && p[1] == '/') || *p == '/') {
379 get_arg(fname, sizeof(fname), p);
380 p2 = strrchr(fname, '/');
381 if (p2 != NULL && strcmp(p2 + 1, "gp2xmenu") == 0)
387 printf(PFX "prefixing as static: %s", p);
388 fprintf(fout, "op_runfbapp %s%s ", root_path, LOADER_STATIC);
392 printf(PFX "prefixing as dynamic: %s", p);
393 fprintf(fout, "op_runfbapp %s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
408 if (rerun_gp2xmenu) {
409 fprintf(fout, "cd %s\n", root_path);
410 fprintf(fout, "exec %s%s\n", root_path, LAUNCHER);
415 msg("starting script..\n");
417 msg("\nsome files need to be unpacked, this may tike a few minutes.\n"
418 "Please wait at least while SD LED is active.\n");
419 system("echo ---; cat /tmp/ginge_conv.sh; echo ---");
420 chmod(out_script, S_IRWXU|S_IRWXG|S_IRWXO);
422 execlp(out_script, out_script, NULL);
423 perror("run out_script");