前情

bring up android S的时候开机失败,卡第一屏,adb没有起来,无法获取日志

奇巧

只要init起来了,就可以通过在init中加入指定服务的方式来把日志打印到指定的地方

init.logcat.rc

#
# Take logs even when adb is not working.
# Thanks: markakash
#
# Usage: Copy this file to /vendor/etc/init/hw.
#        Add this line to your init.$(ro.hardware).rc file:
#        import /vendor/etc/init/hw/init.logcat.rc
#

on fs
    rm /cache/boot_lc_main.txt
    rm /cache/boot_dmesg.txt

    start boot_lc_main
    start boot_dmesg

service boot_lc_main /system/bin/logcat -f /cache/boot_lc_main.txt
    class main
    user root
    group root system
    disabled
    oneshot

service boot_dmesg /system/bin/sh -c "dmesg -w > /cache/boot_dmesg.txt"
    class main
    user root
    group root system
    disabled
    oneshot

之后,在某个rc中import它即可

不过,一加的设备上并没有cache分区,但是我们可以采用神奇的op2分区
官方系统本身也会把一些启动日志塞进这个分区中,正好现在为我所用

完整diff

diff --git a/common.mk b/common.mk
index fe145b1..d7ad22f 100644
--- a/common.mk
+++ b/common.mk
@@ -218,7 +218,8 @@ PRODUCT_PACKAGES += \
     init.target.rc \
     ueventd.qcom.rc \
     vendor.oem_ftm.rc \
-    vendor.oem_ftm_svc_disable.rc
+    vendor.oem_ftm_svc_disable.rc \
+    init.logcat.rc
 
 # Component overrides
 PRODUCT_COPY_FILES += \
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 6519319..5b21f65 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -241,3 +241,11 @@ LOCAL_MODULE_CLASS := ETC
 LOCAL_SRC_FILES    := ueventd.rc
 LOCAL_MODULE_PATH  := $(TARGET_OUT_VENDOR)
 include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE       := init.logcat.rc
+LOCAL_MODULE_TAGS  := optional
+LOCAL_MODULE_CLASS := ETC
+LOCAL_SRC_FILES    := etc/init.logcat.rc
+LOCAL_MODULE_PATH  := $(TARGET_OUT_VENDOR_ETC)/init/hw
+include $(BUILD_PREBUILT)
diff --git a/rootdir/etc/init.logcat.rc b/rootdir/etc/init.logcat.rc
new file mode 100644
index 0000000..fa3ba98
--- /dev/null
+++ b/rootdir/etc/init.logcat.rc
@@ -0,0 +1,29 @@
+#
+# Take logs even when adb is not working.
+# Thanks: markakash
+#
+# Usage: Copy this file to /vendor/etc/init/hw.
+#        Add this line to your init.$(ro.hardware).rc file:
+#        import /vendor/etc/init/hw/init.logcat.rc
+#
+
+on fs
+    rm /mnt/vendor/op2/boot_lc_main.txt
+    rm /mnt/vendor/op2/boot_dmesg.txt
+
+    start boot_lc_main
+    start boot_dmesg
+
+service boot_lc_main /system/bin/logcat -f /mnt/vendor/op2/boot_lc_main.txt
+    class main
+    user root
+    group root system
+    disabled
+    oneshot
+
+service boot_dmesg /system/bin/sh -c "dmesg -w > /mnt/vendor/op2/boot_dmesg.txt"
+    class main
+    user root
+    group root system
+    disabled
+    oneshot
diff --git a/rootdir/etc/init.qcom.rc b/rootdir/etc/init.qcom.rc
index 3190568..452e820 100644
--- a/rootdir/etc/init.qcom.rc
+++ b/rootdir/etc/init.qcom.rc
@@ -36,6 +36,8 @@ import /vendor/etc/init/hw/init.target.rc
 import /vendor/etc/init/hw/init.qcom.factory.rc
 import /vendor/etc/init/hw/init.qcom.test.rc
 
+import /vendor/etc/init/hw/init.logcat.rc
+
 on early-init
     mount debugfs debugfs /sys/kernel/debug
     chmod 0755 /sys/kernel/debug

之后在rec中启用adb
mkdir /op2
mount /dev/block/bootdevice/by-name/op2 /op2
cat /op2/boot_lc_main.txt
cat /op2/boot_dmesg.txt
即可获取日志

Credit

https://github.com/KujouYuko/scripts/blob/master/android/init.logcat.rc