Skip to content

Commit b50be13

Browse files
committed
Add fake logger library
This library used LD_PRELOAD injection to redirect logcat symbols to kernel logger since logcat is not available during early boot. Very useful while debugging.
1 parent 4a4c3c7 commit b50be13

File tree

10 files changed

+61
-27
lines changed

10 files changed

+61
-27
lines changed

install_zip/Android.mk

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ifeq ($(MR_ENCRYPTION),true)
3232
multirom_cp_enc_libs += libmultirom_fake_properties.so
3333
multirom_extra_dep += libmultirom_fake_propertywait
3434
multirom_cp_enc_libs += libmultirom_fake_propertywait.so
35+
multirom_extra_dep += libmultirom_fake_logger
36+
multirom_cp_enc_libs += libmultirom_fake_logger.so
3537
endif
3638
else
3739
MR_ENCRYPTION := false
@@ -103,7 +105,7 @@ $(MULTIROM_ZIP_TARGET): multirom trampoline signapk bbootimg mrom_kexec_static m
103105
@if [ -n "$(MR_INFOS)" ]; then cp -vr $(PWD)/$(MR_INFOS)/* $(MULTIROM_INST_DIR)/multirom/infos/; fi
104106
@echo Copying scripts
105107
@cp -a $(TARGET_OUT_OPTIONAL_EXECUTABLES)/bbootimg $(MULTIROM_INST_DIR)/scripts/
106-
@cp $(PWD)/$(MR_FSTAB) $(MULTIROM_INST_DIR)/multirom/mrom.fstab
108+
@cp $(PWD)/$(MR_FSTAB) $(MULTIROM_INST_DIR)/multirom/mrom_fsbat
107109
@echo Preparing installer script
108110
@$(install_zip_path)/extract_boot_dev.sh $(PWD)/$(MR_FSTAB) $(MULTIROM_INST_DIR)/scripts/bootdev
109111
@$(install_zip_path)/make_updater_script.sh "$(MR_DEVICES)" $(MULTIROM_INST_DIR)/META-INF/com/google/android "Installing MultiROM for"

lib/Android.mk

+9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ include $(multirom_local_path)/device_defines.mk
9191

9292
include $(BUILD_STATIC_LIBRARY)
9393

94+
include $(CLEAR_VARS)
95+
96+
LOCAL_MODULE := libmultirom_fake_logger
97+
LOCAL_MODULE_TAGS := optional
9498

99+
LOCAL_SRC_FILES := fake_logger.c
100+
LOCAL_SRC_FILES += klog.c
101+
102+
103+
include $(BUILD_SHARED_LIBRARY)
95104

96105
include $(CLEAR_VARS)
97106

lib/fake_logger.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdlib.h>
2+
3+
#define LOG_BUF_MAX 51200
4+
extern void multirom_klog_write(int level, const char* fmt, ...);
5+
extern void multirom_klog_set_level(int level);
6+
#define INFO(tag, fmt, ...) multirom_klog_write(6, "<6>%s: " fmt, tag, ##__VA_ARGS__)
7+
8+
int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
9+
10+
char buf[LOG_BUF_MAX];
11+
va_list ap;
12+
va_start(ap, fmt);
13+
vsnprintf(buf, sizeof(buf), fmt, ap);
14+
va_end(ap);
15+
buf[LOG_BUF_MAX - 1] = 0;
16+
INFO(tag, "%s", buf);
17+
return 0;
18+
}

lib/klog.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ void multirom_klog_set_level(int level) {
4141
void multirom_klog_init(void) {
4242
if (klog_fd >= 0) return; /* Already initialized */
4343

44-
klog_fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
44+
/*klog_fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
4545
if (klog_fd >= 0) {
4646
return;
47-
}
47+
}*/
4848

49-
static const char* name = "/dev/__kmsg__";
49+
static const char* name = "/__kmsg__";
5050
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
5151
klog_fd = open(name, O_WRONLY | O_CLOEXEC);
5252
unlink(name);
5353
}
5454
}
5555

56+
void multirom_klog_close() {
57+
close(klog_fd);
58+
}
59+
5660
#define LOG_BUF_MAX 51200
5761

5862
void multirom_klog_writev(int level, const struct iovec* iov, int iov_count) {

lib/util.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,17 @@ int run_cmd_with_env(char **cmd, char *const *envp)
564564

565565

566566
char* read_file(char* file) {
567-
char* buf = calloc(1, 256);
567+
char* buf = calloc(1, 256000);
568+
int nread = 0;
569+
int offset = 0;
570+
char buff[256];
568571
FILE* fp = fopen(file, "r");
569572
if (fp) {
570-
fread(buf, 1, 256, fp);
573+
while ((nread = fread(buff, 1, 256, fp)) > 0) {
574+
INFO("buf %s\n", buff);
575+
memcpy(buf + offset, buff, nread);
576+
offset += nread;
577+
}
571578
fclose(fp);
572579
} else {
573580
ERROR("cannot open %s %s\n", file, strerror(errno));

multirom.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct multirom_status
131131
struct rcadditions rc;
132132
};
133133

134-
int multirom(const char *rom_to_boot);
134+
int multirom(const char *rom_to_boot, int always_reboot);
135135
int multirom_find_base_dir(void);
136136
void multirom_emergency_reboot(void);
137137
int multirom_default_status(struct multirom_status *s);
@@ -145,7 +145,7 @@ void multirom_import_internal(void);
145145
void multirom_dump_status(struct multirom_status *s);
146146
int multirom_save_status(struct multirom_status *s);
147147
void multirom_fixup_rom_name(struct multirom_rom *rom, char *name, const char *def);
148-
int multirom_prepare_for_boot(struct multirom_status *s, struct multirom_rom *to_boot);
148+
int multirom_prepare_for_boot(struct multirom_status *s, struct multirom_rom *to_boot, int always_reboot);
149149
void multirom_free_status(struct multirom_status *s);
150150
void multirom_free_rom(void *rom);
151151
int multirom_init_fb(int rotation);

trampoline/encryption.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern int e4crypt_install_keyring();
3737
static char encmnt_cmd_arg[64] = { 0 };
3838
static char *const encmnt_cmd[] = { "/mrom_enc/trampoline_encmnt", encmnt_cmd_arg, NULL };
3939
#ifdef MR_ENCRYPTION_FAKE_PROPERTIES
40-
static char *const encmnt_envp[] = { "LD_CONFIG_FILE='/mrom_enc/ld.config.txt'", "LD_LIBRARY_PATH=/mrom_enc/", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so", NULL };
40+
static char *const encmnt_envp[] = { "LD_CONFIG_FILE='/mrom_enc/ld.config.txt'", "LD_LIBRARY_PATH=/mrom_enc/", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so /mrom_enc/libmultirom_fake_logger.so", NULL };
4141
#else
4242
static char *const encmnt_envp[] = { "LD_CONFIG_FILE='/mrom_enc/ld.config.txt'", "LD_LIBRARY_PATH=/mrom_enc/", NULL };
4343
#endif
@@ -98,7 +98,7 @@ int encryption_before_mount(struct fstab *fstab, bool isFbe)
9898
goto exit;
9999
}
100100

101-
if (!isFbe || exit_code != 0) {
101+
if (output != NULL) {
102102
itr = output + strlen(output) - 1;
103103
while(itr >= output && isspace(*itr))
104104
*itr-- = 0;
@@ -146,7 +146,7 @@ int encryption_before_mount(struct fstab *fstab, bool isFbe)
146146
mount("/data", "/realdata", NULL, MS_MOVE, NULL);
147147
mkdir("/data", 0755);
148148
}
149-
free(output);
149+
//free(output);
150150
return res;
151151
}
152152

trampoline_encmnt/Android.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LOCAL_MODULE:= trampoline_encmnt
55
LOCAL_MODULE_TAGS := optional
66
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
77
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
8-
LOCAL_SHARED_LIBRARIES := libcryptfslollipop libcutils libe4crypt
8+
LOCAL_SHARED_LIBRARIES := libcryptfslollipop libcutils libe4crypt libwifikeystorehal libsoftkeymasterdevice android.system.wifi.keystore@1.0
99
LOCAL_STATIC_LIBRARIES := libmultirom_static libext4_utils
1010

1111
LOCAL_ADDITIONAL_DEPENDENCIES += libstdc++
@@ -52,7 +52,7 @@ ifeq ($(MR_ENCRYPTION_FAKE_PROPERTIES),true)
5252
LOCAL_C_INCLUDES += $(multirom_local_path)
5353
LOCAL_C_INCLUDES += system/extras/libbootimg/include
5454

55-
LOCAL_SRC_FILES := fake_properties.c klog.c
55+
LOCAL_SRC_FILES := fake_properties.c
5656
LOCAL_SHARED_LIBRARIES := liblog
5757

5858
ifneq ($(MR_ENCRYPTION_FAKE_PROPERTIES_EXTRAS),)

trampoline_encmnt/fake_properties.c

+7-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <sys/un.h>
2323
#include <unistd.h>
2424
#include <utils/Log.h>
25-
#include "log.h"
25+
//#include "log.h"
2626
#include <sys/wait.h>
2727
#include <sys/types.h>
2828

@@ -105,7 +105,6 @@ static int fork_and_exec(char *cmd, char** env, char** argv)
105105
setenv("LD_LIBRARY_PATH", "/mrom_enc", 1);
106106
setenv("LD_PRELOAD", "/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so", 1);
107107
execve(cmd, argv, environ);
108-
INFO("Failed to exec %s: %s\n", cmd[0], strerror(errno));
109108
_exit(127);
110109
}
111110
return pID;
@@ -120,33 +119,28 @@ int property_set(char* property, char* value) {
120119
int i, s, len;
121120
struct sockaddr_un saun;
122121

123-
char* env[] = {"LD_CONFIG_FILE=/mron_enc/ld.config.txt", "LD_LIBRARY_PATH=/mrom_enc", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so", NULL};
122+
char* env[] = {"LD_CONFIG_FILE=/mron_enc/ld.config.txt", "LD_LIBRARY_PATH=/mrom_enc", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so /mrom_enc/libmultirom_fake_logger.so", NULL};
124123
if (property && value && strstr(property, "ctl.start") && !strcmp(value, "keystore")) {
125124
char* args[] = {"keystore", "/tmp/misc/keystore", NULL};
126125
keystore_pid = fork_and_exec("/mrom_enc/keystore", env, args);
127-
if (keystore_pid == -1)
128-
INFO("Failed to fork for keymaster; should never happen!\n");
129-
else
130-
INFO("keystore started: pid=%d\n", keystore_pid);
126+
if (keystore_pid != -1) {
127+
ALOGE("keystore running %d", keystore_pid);
128+
} else {
129+
ALOGE("keystore failed %d", keystore_pid);
130+
}
131131
return 0;
132132
}
133133

134134
if (property && value && strstr(property, "ctl.start") && !strcmp(value, "keystore_auth")) {
135135
char* args[] = {"keystore_auth", NULL};
136136
keystore_auth_pid = fork_and_exec("/mrom_enc/keystore_auth", env, args);
137-
if (keystore_auth_pid == -1)
138-
INFO("Failed to fork for keymaster; should never happen!\n");
139-
else
140-
INFO("keystore started: pid=%d\n", keystore_auth_pid);
141-
return 0;
142137
}
143138

144139
if (property && value && strstr(property, "ctl.stop") && !strcmp(value, "keystore")) {
145140
if (keystore_pid != -1)
146141
{
147142
kill(-keystore_pid, SIGTERM); // kill the entire process group
148143
waitpid(keystore_pid, NULL, 0);
149-
ERROR("keystore killed %d\n", keystore_pid);
150144
}
151145
return 0;
152146
}

trampoline_encmnt/pw_ui.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void boot_internal_clicked(UNUSED void *data)
8383

8484
// We need to run quirks for primary ROM to prevent
8585
// restorecon breaking everything
86-
rom_quirks_on_initrd_finalized();
86+
//rom_quirks_on_initrd_finalized();
8787

8888
pthread_mutex_lock(&exit_code_mutex);
8989
exit_code = ENCMNT_UIRES_BOOT_INTERNAL;

0 commit comments

Comments
 (0)