static int run_core(void) { int res = -1; struct fstab *fstab = NULL; if(wait_for_file("/dev/graphics/fb0", 5) < 0) { ERROR("Waiting too long for fb0"); goto exit; } #ifdef MR_POPULATE_BY_NAME_PATH Populate_ByName_using_emmc(); #endif fstab = fstab_auto_load(); if(!fstab) goto exit; #if 0 fstab_dump(fstab); //debug #endif // mount and run multirom from sdcard res = mount_and_run(fstab); if(res < 0 && mrom_is_second_boot()) { ERROR("This is second boot and we couldn't mount /data, reboot!\n"); sync(); //android_reboot(ANDROID_RB_RESTART, 0, 0); android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); // favour reboot to recovery, to avoid possible bootlooping while(1) sleep(1); } if(access(KEEP_REALDATA, F_OK) < 0) { umount(REALDATA); rmdir(REALDATA); encryption_destroy(); } encryption_cleanup(); exit: if(fstab) fstab_destroy(fstab); return res; }
// functions used by workaround char *nokexec_find_boot_mmcblk_path(struct multirom_status *s) { struct fstab_part *boot = NULL; INFO(NO_KEXEC_LOG_TEXT ": locating boot partition...\n"); if (!s) { INFO(NO_KEXEC_LOG_TEXT ": 'multirom_status' not set up yet, fstab_auto_load ourselves\n"); struct fstab *fstab; fstab = fstab_auto_load(); if (fstab) { boot = fstab_find_first_by_path(fstab, "/boot"); fstab_destroy(fstab); } } else if (s->fstab) boot = fstab_find_first_by_path(s->fstab, "/boot"); if (boot) INFO(NO_KEXEC_LOG_TEXT ": found boot at '%s'\n", boot->device); else { INFO(NO_KEXEC_LOG_TEXT ": not found in fstab, try looking at mrom.fstab...\n"); struct fstab *mrom_fstab; char path_mrom_fstab[256]; sprintf(path_mrom_fstab, "%s/%s", mrom_dir(), "mrom.fstab"); mrom_fstab = fstab_load(path_mrom_fstab, 1); if (!mrom_fstab) { ERROR(NO_KEXEC_LOG_TEXT ": couldn't load mrom.fstab '%s'\n", path_mrom_fstab); return NULL; } boot = fstab_find_first_by_path(mrom_fstab, "/boot"); if (boot) INFO(NO_KEXEC_LOG_TEXT ": found boot (using mrom.fstab) at '%s'\n", boot->device); else return NULL; } return strdup(boot->device); }
int main(int argc, char *argv[]) { int i, res; static char *const cmd[] = { "/init", NULL }; struct fstab *fstab = NULL; for(i = 1; i < argc; ++i) { if(strcmp(argv[i], "-v") == 0) { printf("%d\n", VERSION_TRAMPOLINE); fflush(stdout); return 0; } } umask(000); // Init only the little we need, leave the rest for real init mkdir("/dev", 0755); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mkdir("/proc", 0755); mkdir("/sys", 0755); mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); mount("devpts", "/dev/pts", "devpts", 0, NULL); mount("proc", "/proc", "proc", 0, NULL); mount("sysfs", "/sys", "sysfs", 0, NULL); klog_init(); ERROR("Running trampoline v%d\n", VERSION_TRAMPOLINE); if(is_charger_mode()) { ERROR("Charger mode detected, skipping multirom\n"); goto run_main_init; } #if MR_DEVICE_HOOKS >= 3 tramp_hook_before_device_init(); #endif ERROR("Initializing devices..."); devices_init(); ERROR("Done initializing"); if(wait_for_file("/dev/graphics/fb0", 5) < 0) { ERROR("Waiting too long for fb0"); goto exit; } fstab = fstab_auto_load(); if(!fstab) goto exit; #if 0 fstab_dump(fstab); //debug #endif // mount and run multirom from sdcard mount_and_run(fstab); exit: if(fstab) fstab_destroy(fstab); // close and destroy everything devices_close(); run_main_init: if(access(KEEP_REALDATA, F_OK) < 0) { umount(REALDATA); umount("/dev/pts"); umount("/dev"); rmdir("/dev/pts"); rmdir("/dev/socket"); rmdir("/dev"); rmdir(REALDATA); } umount("/proc"); umount("/sys"); rmdir("/proc"); rmdir("/sys"); ERROR("Running main_init\n"); fixup_symlinks(); chmod("/main_init", EXEC_MASK); rename("/main_init", "/init"); res = execve(cmd[0], cmd, NULL); ERROR("execve returned %d %d %s\n", res, errno, strerror(errno)); return 0; }
int main(int argc, char *argv[]) { int i, res; static char *const cmd[] = { "/init", NULL }; struct fstab *fstab = NULL; char *inject_path = NULL; char *mrom_dir = NULL; int force_inject = 0; for(i = 1; i < argc; ++i) { if(strcmp(argv[i], "-v") == 0) { printf("%d\n", VERSION_TRAMPOLINE); fflush(stdout); return 0; } else if(strstartswith(argv[i], "--inject=")) inject_path = argv[i] + strlen("--inject="); else if(strstartswith(argv[i], "--mrom_dir=")) mrom_dir = argv[i] + strlen("--mrom_dir="); else if(strcmp(argv[i], "-f") == 0) force_inject = 1; } if(inject_path) { if(!mrom_dir) { printf("--mrom_dir=[path to multirom's data dir] needs to be specified!\n"); fflush(stdout); return 1; } mrom_set_dir(mrom_dir); mrom_set_log_tag("trampoline_inject"); return inject_bootimg(inject_path, force_inject); } umask(000); // Init only the little we need, leave the rest for real init mkdir("/dev", 0755); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mkdir("/proc", 0755); mkdir("/sys", 0755); mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); mount("devpts", "/dev/pts", "devpts", 0, NULL); mount("proc", "/proc", "proc", 0, NULL); mount("sysfs", "/sys", "sysfs", 0, NULL); mount("pstore", "/sys/fs/pstore", "pstore", 0, NULL); #if MR_USE_DEBUGFS_MOUNT // Mount the debugfs kernel sysfs mkdir("/sys/kernel/debug", 0755); mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL); #endif klog_init(); // output all messages to dmesg, // but it is possible to filter out INFO messages klog_set_level(6); mrom_set_log_tag("trampoline"); INFO("Running trampoline v%d\n", VERSION_TRAMPOLINE); if(is_charger_mode()) { INFO("Charger mode detected, skipping multirom\n"); goto run_main_init; } #if MR_DEVICE_HOOKS >= 3 tramp_hook_before_device_init(); #endif INFO("Initializing devices...\n"); devices_init(); INFO("Done initializing\n"); if(wait_for_file("/dev/graphics/fb0", 5) < 0) { ERROR("Waiting too long for fb0"); goto exit; } #ifdef MR_POPULATE_BY_NAME_PATH //nkk71 M7 hack Populate_ByName_using_emmc(); #endif fstab = fstab_auto_load(); if(!fstab) goto exit; #if 0 fstab_dump(fstab); //debug #endif // mount and run multirom from sdcard if(mount_and_run(fstab) < 0 && mrom_is_second_boot()) { ERROR("This is second boot and we couldn't mount /data, reboot!\n"); sync(); android_reboot(ANDROID_RB_RESTART, 0, 0); while(1) sleep(1); } exit: if(fstab) fstab_destroy(fstab); // close and destroy everything devices_close(); run_main_init: umount("/dev/pts"); rmdir("/dev/pts"); rmdir("/dev/socket"); if(access(KEEP_REALDATA, F_OK) < 0) { umount(REALDATA); umount("/dev"); rmdir(REALDATA); encryption_destroy(); } encryption_cleanup(); #if MR_USE_DEBUGFS_MOUNT umount("/sys/kernel/debug"); #endif umount("/proc"); umount("/sys/fs/pstore"); umount("/sys"); INFO("Running main_init\n"); fixup_symlinks(); chmod("/main_init", EXEC_MASK); rename("/main_init", "/init"); res = execve(cmd[0], cmd, NULL); ERROR("execve returned %d %d %s\n", res, errno, strerror(errno)); return 0; }
struct fstab *fstab_load(const char *path) { FILE *f = fopen(path, "r"); if(!f) { ERROR("Failed to open fstab %s\n", path); return NULL; } struct fstab *t = mzalloc(sizeof(struct fstab)); t->version = -1; const char *delim = " \t"; char *saveptr = NULL; char *p; char line[1024]; int len; struct fstab_part *part = NULL; while((p = fgets(line, sizeof(line), f))) { len = strlen(line); if(line[len-1] == '\n') line[len-1] = 0; while(isspace(*p)) ++p; if(*p == '#' || *p == 0) continue; part = mzalloc(sizeof(struct fstab_part)); if(!(p = strtok_r(line, delim, &saveptr))) { ERROR("Error first token\n"); goto fail; } if(t->version == -1) { if(strstr(p, "/dev/") == p) t->version = 2; else t->version = 1; } if(t->version == 2) part->device = readlink_recursive(p); else part->path = strdup (p); if(!(p = strtok_r(NULL, delim, &saveptr))) { ERROR("Error second token\n"); goto fail; } if(t->version == 2) part->path = strdup(p); else part->type = strdup(p); if(!(p = strtok_r(NULL, delim, &saveptr))) { ERROR("Error third token\n"); goto fail; } if(t->version == 2) part->type = strdup(p); else part->device = readlink_recursive(p); if((p = strtok_r(NULL, delim, &saveptr))) fstab_parse_options(p, part); if((p = strtok_r(NULL, delim, &saveptr))) part->options2 = strdup(p); // Check device if(!part->device) { if (strcmp(part->path, "/data") == 0 || strcmp(part->path, "/system") == 0 || strcmp(part->path, "/boot") == 0 || strcmp(part->path, "/cache") == 0) { ERROR("fstab: device for part %s does not exist!\n", part->path); } fstab_destroy_part(part); part = NULL; continue; } list_add(part, &t->parts); ++t->count; part = NULL; } fclose(f); return t; fail: fclose(f); free(part); fstab_destroy(t); return NULL; }
int main(int argc, char *argv[]) { int i; int res = 1; int cmd = CMD_NONE; int stdout_fd; char footer_location[256]; struct fstab *fstab; struct fstab_part *p; char *argument = NULL; klog_init(); // output all messages to dmesg, // but it is possible to filter out INFO messages klog_set_level(6); mrom_set_log_tag("trampoline_encmnt"); mrom_set_dir("/mrom_enc/"); for(i = 1; i < argc; ++i) { if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_help(argv); return 0; } else if(cmd == CMD_NONE) { if(strcmp(argv[i], "decrypt") == 0) cmd = CMD_DECRYPT; else if(strcmp(argv[i], "remove") == 0) cmd = CMD_REMOVE; else if(strcmp(argv[i], "pwtype") == 0) cmd = CMD_PWTYPE; } else if(!argument) { argument = argv[i]; } } if(argc == 1 || cmd == CMD_NONE) { print_help(argv); return 0; } fstab = fstab_auto_load(); if(!fstab) { ERROR("Failed to load fstab!"); return 1; } p = fstab_find_first_by_path(fstab, "/data"); if(!p) { ERROR("Failed to find /data partition in fstab\n"); goto exit; } strcpy(footer_location, ""); if(p->options != NULL && get_footer_from_opts(footer_location, sizeof(footer_location), p->options) < 0) goto exit; if(p->options2 != NULL && strcmp(footer_location, "") == 0 && get_footer_from_opts(footer_location, sizeof(footer_location), p->options2) < 0) goto exit; INFO("Setting encrypted partition data to %s %s %s\n", p->device, footer_location, p->type); set_partition_data(p->device, footer_location, p->type); // cryptfs prints informations, we don't want that stdout_fd = dup(1); freopen("/dev/null", "ae", stdout); switch(cmd) { case CMD_PWTYPE: if(handle_pwtype(stdout_fd) < 0) goto exit; break; case CMD_DECRYPT: if(handle_decrypt(stdout_fd, argument) < 0) goto exit; break; case CMD_REMOVE: if(handle_remove() < 0) goto exit; break; } res = 0; exit: fstab_destroy(fstab); return res; }