Reboot when trampoline can't mount /data on second boot

* On second boot, we have secondary ROM's kernel running and don't
  want to boot the primary ROM with it.
This commit is contained in:
Vojtech Bocek
2015-03-06 21:05:23 +01:00
parent 6d492ff870
commit 30f5316b7b
5 changed files with 75 additions and 58 deletions

View File

@@ -42,3 +42,48 @@ const char *mrom_dir(void)
{
return multirom_dir;
}
int mrom_is_second_boot(void)
{
int i;
int res = 0;
FILE *f = NULL;
char buff[2048];
static const char *kmsg_paths[] = {
"/proc/last_kmsg",
"/sys/fs/pstore/console-ramoops",
NULL,
};
f = fopen("/proc/cmdline", "re");
if(f)
{
if(fgets(buff, sizeof(buff), f) && strstr(buff, "mrom_kexecd=1"))
{
res = 1;
goto exit;
}
fclose(f);
}
for(i = 0; !f && kmsg_paths[i]; ++i)
f = fopen(kmsg_paths[i], "re");
if(!f)
return 0;
while(fgets(buff, sizeof(buff), f))
{
if(strstr(buff, SECOND_BOOT_KMESG))
{
res = 1;
goto exit;
}
}
exit:
fclose(f);
return res;
}

View File

@@ -18,10 +18,13 @@
#ifndef MROM_DATA_H
#define MROM_DATA_H
#define SECOND_BOOT_KMESG "MultiromSaysNextBootShouldBeSecondMagic108\n"
void mrom_set_dir(const char *mrom_dir);
void mrom_set_log_tag(const char *tag);
const char *mrom_log_tag(void);
const char *mrom_dir(void);
int mrom_is_second_boot(void);
#endif

View File

@@ -60,7 +60,6 @@
#define INTERNAL_ROM_NAME "Internal"
#define MAX_ROM_NAME_LEN 26
#define LAYOUT_VERSION "/data/.layout_version"
#define SECOND_BOOT_KMESG "MultiromSaysNextBootShouldBeSecondMagic108\n"
#define BATTERY_CAP "/sys/class/power_supply/battery/capacity"
@@ -473,6 +472,9 @@ int multirom_load_status(struct multirom_status *s)
multirom_default_status(s);
if(mrom_is_second_boot())
s->is_second_boot = 1;
char arg[256];
sprintf(arg, "%s/multirom.ini", mrom_dir());
@@ -490,19 +492,6 @@ int multirom_load_status(struct multirom_status *s)
char name[64];
char *pch;
if(multirom_search_last_kmsg(SECOND_BOOT_KMESG) == 0)
s->is_second_boot = 1;
else
{
FILE *cmdline = fopen("/proc/cmdline", "re");
if(cmdline)
{
if(fgets(line, sizeof(line), cmdline) && strstr(line, "mrom_kexecd=1"))
s->is_second_boot = 1;
fclose(cmdline);
}
}
while((fgets(line, sizeof(line), f)))
{
pch = strtok (line, "=\n");
@@ -2438,38 +2427,6 @@ struct usb_partition *multirom_get_partition(struct multirom_status *s, char *uu
return NULL;
}
int multirom_search_last_kmsg(const char *expr)
{
int i;
int res = -1;
FILE *f = NULL;
char buff[2048];
static const char *kmsg_paths[] = {
"/proc/last_kmsg",
"/sys/fs/pstore/console-ramoops",
NULL,
};
for(i = 0; !f && kmsg_paths[i]; ++i)
f = fopen(kmsg_paths[i], "re");
if(!f)
return -1;
while(fgets(buff, sizeof(buff), f))
{
if(strstr(buff, expr))
{
res = 0;
break;
}
}
fclose(f);
return res;
}
int multirom_get_battery(void)
{
char buff[4];

View File

@@ -155,7 +155,6 @@ int multirom_copy_log(char *klog, const char *dest_path_relative);
int multirom_scan_partition_for_roms(struct multirom_status *s, struct usb_partition *p);
struct usb_partition *multirom_get_partition(struct multirom_status *s, char *uuid);
int multirom_path_exists(char *base, char *filename);
int multirom_search_last_kmsg(const char *expr);
struct rom_info *multirom_parse_rom_info(struct multirom_status *s, struct multirom_rom *rom);
void multirom_destroy_rom_info(struct rom_info *info);
char **multirom_get_rom_info_str(struct rom_info *info, char *key);

View File

@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <cutils/android_reboot.h>
#include "devices.h"
#include "../lib/log.h"
@@ -146,19 +147,23 @@ static int try_mount_all_entries(struct fstab *fstab, struct fstab_part *first_d
return -1;
}
static void mount_and_run(struct fstab *fstab)
static int mount_and_run(struct fstab *fstab)
{
struct fstab_part *datap = fstab_find_first_by_path(fstab, "/data");
if(!datap)
{
ERROR("Failed to find /data partition in fstab\n");
return;
return -1;
}
if(wait_for_file(datap->device, 5) < 0)
if(access(datap->device, R_OK) < 0)
{
ERROR("Waiting too long for dev %s\n", datap->device);
return;
INFO("Waiting for %s\n", datap->device);
if(wait_for_file(datap->device, 5) < 0)
{
ERROR("Waiting too long for dev %s\n", datap->device);
return -1;
}
}
mkdir(REALDATA, 0755);
@@ -167,23 +172,23 @@ static void mount_and_run(struct fstab *fstab)
{
#ifndef MR_ENCRYPTION
ERROR("Failed to mount /data with all possible filesystems!\n");
return;
return -1;
#else
INFO("Failed to mount /data, trying encryption...\n");
switch(encryption_before_mount(fstab))
{
case ENC_RES_ERR:
ERROR("/data decryption failed!\n");
return;
return -1;
case ENC_RES_BOOT_INTERNAL:
return;
return 0;
default:
case ENC_RES_OK:
{
if(try_mount_all_entries(fstab, datap) < 0)
{
ERROR("Failed to mount decrypted /data with all possible filesystems!\n");
return;
return -1;
}
break;
}
@@ -194,12 +199,13 @@ static void mount_and_run(struct fstab *fstab)
if(find_multirom() == -1)
{
ERROR("Could not find multirom folder!\n");
return;
return -1;
}
adb_init(path_multirom);
run_multirom();
adb_quit();
return 0;
}
static int is_charger_mode(void)
@@ -339,7 +345,14 @@ int main(int argc, char *argv[])
#endif
// mount and run multirom from sdcard
mount_and_run(fstab);
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)