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 (getcwd(cwd, sizeof(cwd)) == NULL) {
262 err(PFX "failed to get cwd\n");
266 ret = make_local_path(root_path, sizeof(root_path), "");
268 err(PFX "failed to generate root path\n");
272 fout = fopen(out_script, "w");
274 perror("can't open output script");
278 fprintf(fout, "#!/bin/sh\n");
280 ret = id_elf(argv[1]);
281 if (ret == 1 || ret == 2) {
282 if (cmd_in_blacklist(argv[1])) {
283 fprintf(stderr, "blacklisted: %s\n", argv[1]);
293 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
294 dump_args(fout, argc - 1, &argv[1]);
299 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
300 dump_args(fout, argc - 1, &argv[1]);
309 fin = fopen(argv[1], "r");
314 char buff[512], fname[512], *p, *p2;
316 p = fgets(buff, sizeof(buff), fin);
321 if (p[0] == '#' && p[1] == '!')
329 // things we are sure we want to pass
330 if (*p == '#' || strncmp(p, "export ", 7) == 0)
334 if (strncmp(p, "exec ", 5) == 0)
337 // blacklist some stuff
338 if (strncmp(p, "/sbin/", 6) == 0)
340 else if (strncmp(p, "/bin/", 5) == 0)
344 if (strncmp(p2, "mount ", 6) == 0) {
347 if (strstr(p2, "cramfs")) {
350 p2 = sskip(cskip(p2));
351 p2 = sskip(cskip(p2));
354 err(PFX "cramfs: missing mount file in \"%s\"?\n", p);
357 p2 = get_arg(fname, sizeof(fname), p2);
359 err(PFX "cramfs: missing mount point in \"%s\"?\n", p);
362 get_arg(buff, sizeof(buff), p2);
363 fprintf(fout, "if [ `ls %s | wc -l` -eq 0 ]; then\n", buff);
364 fprintf(fout, " rmdir \"%s\"\n", buff); // cramfsck doesn't want it
365 fprintf(fout, " %stools/cramfsck -x \"%s\" \"%s\"\n", root_path, buff, fname);
366 fprintf(fout, "fi\n");
371 if (cmd_in_blacklist(p2))
375 if (strncmp(p, "cd ", 3) == 0) {
376 get_arg(fname, sizeof(fname), p + 3);
377 if (strncmp(fname, "/usr/gp2x", 9) == 0)
386 // trying to run something from cwd?
387 if ((p[0] == '.' && p[1] == '/') || *p == '/') {
388 get_arg(fname, sizeof(fname), p);
389 p2 = strrchr(fname, '/');
390 if (p2 != NULL && strcmp(p2 + 1, "gp2xmenu") == 0)
396 printf(PFX "prefixing as static: %s", p);
397 fprintf(fout, WRAP_APP "%s%s ", root_path, LOADER_STATIC);
401 printf(PFX "prefixing as dynamic: %s", p);
402 fprintf(fout, WRAP_APP "%s%s \"%s\" ", root_path, LOADER_DYNAMIC, root_path);
417 if (rerun_gp2xmenu) {
418 fprintf(fout, "cd %s\n", root_path);
419 fprintf(fout, "exec %s%s\n", root_path, LAUNCHER);
424 //msg("starting script..\n");
426 msg("\nsome files need to be unpacked, this may tike a few minutes.\n");
428 msg("Please wait at least while SD LED is active.\n");
431 system("echo ---; cat /tmp/ginge_conv.sh; echo ---");
432 chmod(out_script, S_IRWXU|S_IRWXG|S_IRWXO);
434 execlp(out_script, out_script, NULL);
435 perror("run out_script");