记一次对W527的第三方内核编写(二)

SoraNeko 发布于 22 天前 96 次阅读


然后 开机了

可是他目前有:

亮度炸了 音频炸了 摄像头炸了 wifi炸了 蓝牙炸了 重力传感器炸了 充电炸了 触摸炸了 电量炸了 老天 全身上下没一个是好的 几乎全死了 可以说是一个木乃伊 所以 开始一个个修bug吧

那么 这里是第二部分!也就是 让一些原厂模块得以加载!

我们看目前的报错内容

[    8.810332] [ASoC:BOARD] asoc_sprd_card_dai_link_of: parse for codec failed. Go to use a dummy codec.
[    8.840362] [ASoC:BOARD] asoc_sprd_card_dai_link_of: parse for codec failed. Go to use a dummy codec.
[    8.849804] vbc-rxpx-codec-sc27xx sound@0: \x09name : FE_TEST_CODEC
[    8.856025] vbc-rxpx-codec-sc27xx sound@0: \x09stream_name : FE_ST_TEST_CODEC
[    8.863157] vbc-rxpx-codec-sc27xx sound@0: \x09name : BE_TEST_CODEC
[    8.869350] vbc-rxpx-codec-sc27xx sound@0: \x09stream_name : BE_TEST_CODEC
[    8.876205] [ASoC:BOARD] ERR: asoc_sprd_card_sub_parse_of parse phandle 'codec' failed!(-2)
[    8.884756] [ASoC:BOARD] asoc_sprd_card_dai_link_of: parse for codec failed. Go to use a dummy codec.
[    8.894199] vbc-rxpx-codec-sc27xx sound@0: \x09name : BE_FAST_BTSCO
[    8.900418] vbc-rxpx-codec-sc27xx sound@0: \x09stream_name : BE_ST_FAST_BTSCO
[    8.907533] [ASoC:BOARD] ERR: asoc_sprd_card_sub_parse_of parse phandle 'codec' failed!(-2)
[    8.916080] [ASoC:BOARD] asoc_sprd_card_dai_link_of: parse for codec failed. Go to use a dummy codec.
[    8.925521] vbc-rxpx-codec-sc27xx sound@0: \x09name : BE_NORMAL_AP01_BTSCO
[    8.932352] vbc-rxpx-codec-sc27xx sound@0: \x09stream_name : BE_ST_NORMAL_AP01_BTSCO
[    8.940073] [ASoC:BOARD] ERR: asoc_sprd_card_sub_parse_of get dai name for 'cpu' failed!(-22)
[    8.948809] vbc-rxpx-codec-sc27xx sound@0: asoc_sprd_card_parse_of: Parsing dai link 53 failed(-22)!
[    8.958142] vbc-rxpx-codec-sc27xx sound@0: parse error -22
[    8.963853] [ASoC:BOARD] ERR: vbc_rxpx_codec_sc27xx_probe, asoc_sprd_card_probe failed!
[    8.972092] vbc-rxpx-codec-sc27xx: probe of sound@0 failed with error -22
[    8.979181] ALSA device list:
[    8.982315] No soundcards found.
[    9.460104] init: [libfs_mgr]Filesystem on /dev/block/platform/soc/soc:ap-apb/71400000.sdio/by-name/vendor was not cleanly shutdown; state flags: 0x0, incompat feature flags: 0x42
[    9.487344] EXT4-fs (mmcblk0p37): warning: mounting unchecked fs, running e2fsck is recommended
[    9.581733] e2fsck: e2fsck terminated by exit(255)\x0a
[    9.588639] init: [libfs_mgr]e2fsck returned status 0xff00
[   16.949896] power_supply sc27xx-fgu: driver failed to report `status' property: -22
[   16.958327] device: 'sc27xx-fgu': dev_uevent: class uevent() returned -22
[   16.958331] synth uevent: /devices/platform/soc/soc:aon/32100000.spi/spi_master/spi4/spi4.0/sc27xx-fgu/power_supply/sc27xx-fgu: failed to send uevent
[   16.958335] power_supply sc27xx-fgu: uevent: failed to send synthetic uevent
[   17.970370] init: Command 'insmod /vendor/lib/modules/sprd_sensor.ko' action=early-init (/vendor/etc/init/camera.rc:2) took 3ms and failed: finit_module for "/vendor/lib/modules/sprd_sensor.ko" failed: Exec format error
[   17.991254] init: Command 'insmod /vendor/lib/modules/sprd_flash_drv.ko' action=early-init (/vendor/etc/init/camera.rc:3) took 1ms and failed: finit_module for "/vendor/lib/modules/sprd_flash_drv.ko" failed: Exec format error
[   18.071226] init: Command 'insmod /vendor/lib/modules/sprd_camera.ko' action=early-init (/vendor/etc/init/camera.rc:4) took 59ms and failed: finit_module for "/vendor/lib/modules/sprd_camera.ko" failed: Exec format error
[   18.093582] init: Command 'insmod /vendor/lib/modules/sprd_cpp.ko' action=early-init (/vendor/etc/init/camera.rc:5) took 2ms and failed: finit_module for "/vendor/lib/modules/sprd_cpp.ko" failed: Exec format error
[   18.115131] init: Command 'insmod /vendor/lib/modules/sprd_fd.ko' action=early-init (/vendor/etc/init/camera.rc:6) took 2ms and failed: finit_module for "/vendor/lib/modules/sprd_fd.ko" failed: Exec format error
[   18.138210] init: Command 'insmod /vendor/lib/modules/mmdvfs.ko' action=early-init (/vendor/etc/init/camera.rc:7) took 4ms and failed: finit_module for "/vendor/lib/modules/mmdvfs.ko" failed: Exec format error
[   21.428372] init: Command 'insmod /vendor/lib/modules/flash_ic_sc2703.ko' action=sprd-flash-ko (/vendor/etc/init/init.sprd_flash.rc:2) took 2ms and failed: finit_module for "/vendor/lib/modules/flash_ic_sc2703.ko" failed: Exec format error
[   21.451101] init: Command 'insmod /vendor/lib/modules/flash_ic_ocp8137.ko' action=sprd-flash-ko (/vendor/etc/init/init.sprd_flash.rc:3) took 1ms and failed: finit_module for "/vendor/lib/modules/flash_ic_ocp8137.ko" failed: Exec format error
[   21.474278] init: Command 'insmod /vendor/lib/modules/flash_ic_aw3642.ko' action=sprd-flash-ko (/vendor/etc/init/init.sprd_flash.rc:4) took 1ms and failed: finit_module for "/vendor/lib/modules/flash_ic_aw3642.ko" failed: Exec format error
[   21.647685] fsck.f2fs: Info: Fix the reported corruption.
[   22.746537] fsck.f2fs: Info: Corrupted valid nat_bits in checkpoint
[   59.293312] fsck.f2fs: [FSCK] other corrupted bugs                           [Fail]
[   64.613150] init: [libfs_mgr]Filesystem on /dev/block/platform/soc/soc:ap-apb/71400000.sdio/by-name/prodnv was not cleanly shutdown; state flags: 0x1, incompat feature flags: 0x246
[   64.834665] init: [libfs_mgr]Filesystem on /dev/block/platform/soc/soc:ap-apb/71400000.sdio/by-name/customer was not cleanly shutdown; state flags: 0x0, incompat feature flags: 0x242
[   64.852248] EXT4-fs (mmcblk0p35): warning: mounting unchecked fs, running e2fsck is recommended
[   64.998562] e2fsck: e2fsck terminated by exit(1)\x0a==
[   65.005230] init: [libfs_mgr]e2fsck returned status 0x100
[   66.511619] init: Command 'insmod /vendor/lib/modules/cyttsp5_i2c_ts.ko' action=post-fs (/vendor/etc/init/hw/init.uws6152_1h10_go.rc:14) took 3ms and failed: finit_module for "/vendor/lib/modules/cyttsp5_i2c_ts.ko" failed: Exec format error
[   66.672810] init: Command 'insmod vendor/lib/modules/pvrsrvkm.ko' action=post-fs (/vendor/etc/init/hw/init.common.rc:69) took 116ms and failed: finit_module for "vendor/lib/modules/pvrsrvkm.ko" failed: Exec format error
[   67.385574] EXT4-fs (mmcblk0p33): warning: mounting unchecked fs, running e2fsck is recommended
[   71.080500] init: Command 'insmod /vendor/lib/modules/sc27xx-vibra.ko' action=post-fs (/vendor/etc/init/init.vibdrv.rc:2) took 0ms and failed: open("/vendor/lib/modules/sc27xx-vibra.ko") failed: No such file or directory

我们分析一下呀

[   18.115131] init: Command 'insmod /vendor/lib/modules/sprd_fd.ko' action=early-init (/vendor/etc/init/camera.rc:6) took 2ms and failed: finit_module for "/vendor/lib/modules/sprd_fd.ko" failed: Exec format error

看到这样的字了嘛 这就是模块加载被内核一脚踹出去了 出现了Exec format error报错

为什么呢? 内核为了防第三方乱入 也防符号缺失之类导致kernel panic 所以加载 .ko 的时候会检测好多东西

下面就是要检查的东西:

1 ELF 格式检查 他会先看是不是正经内核模块:e_type 得是 ET_REL 架构要对,section header 大小也得对

不对就 -ENOEXEC 用户态看到的就是 Exec format error

所以别一看到 Exec format error 就只想到 vermagic ELF 头本身也可能有问题 不过至于可能性 约等于0?可能没那么大 除非是什么特别垃圾的山寨机

也不太可能 是个正常人大概都不会犯这个问题吧

2 内核魔术字(vermagic) 模块 .modinfo 段里有个 vermagic=,必须和当前内核的 VERMAGIC_STRING 对得上。

版本、SMP、PREEMPT、架构,全在里面。对不上也是 -ENOEXEC

这个就是我们说的“内核魔术字”,这货也阻止我们修改自己的内核版本名喵

所以 我们上面的报错就可能是这两种捏

3 符号 CRC(CONFIG_MODVERSIONS) 模块的 __versions 段里记着它引用的每个内核符号的 CRC。

kcrctab 一个一个对,对不上就报 disagrees about version of symbol

 原版 check_version() 这时候返回 0,符号解析就会挂。

这个就是符号 CRC,但注意:只有开了 CONFIG_MODVERSIONS 的内核才玩这套,没开就没这段

当然 保险一点 全改了吧

4 module_layout 布局 CRC

struct module 是内核描述模块的核心结构体,模块和内核必须对这个结构体的内存布局理解完全一致,不然引用计数、链表、sysfs 全乱套。

setup_load_info() 里有个 check_modstruct_version(),专门查 module_layout 符号的 CRC。 不一致,直接 -ENOEXEC。

这关不受 MODULE_INIT_IGNORE_MODVERSIONS 和 MODULE_INIT_IGNORE_VERMAGIC 影响,想绕只能改代码

所以这货必须要改 作用对于我们这些做第三方内核的用途堪比gay片里的套

不过也就是这样 万一啥时候用到了 头文件布局不对 他就彻底死翘翘了 直接内核恐慌

5 符号解析 前面都过了,才真正开始找符号。

simplify_symbols() 里遇到 SHN_UNDEF,就去 resolve_symbol_wait()。

找不到,又不是弱符号,就报 Unknown symbol,返回 -ENOENT

注意:符号找不到不会报 Exec format error,会明确告诉你哪个符号没找到。

这个的确没办法 必须得补齐做stub函数或者占位 但是我更建议你去找开源的东西

拼好核不是不能用 我的内核里面就有无数个拼好包

所以日志里这些:

sprd_sensor.ko
sprd_flash_drv.ko
sprd_camera.ko
sprd_cpp.ko
sprd_fd.ko
mmdvfs.ko
flash_ic_sc2703.ko
flash_ic_ocp8137.ko
flash_ic_aw3642.ko
cyttsp5_i2c_ts.ko
pvrsrvkm.ko

全是 Exec format error,说明它们卡在了门外面,至少没过 ELF / vermagic / module_layout 这几关。

我们要改的那几个校验,就是在这几道门上动手脚

毕竟如你所见 我的内核叫4.14.98-KawaiiSoraNeko 我也不打算改掉这个可爱的名字

毕竟SoraNeko总是可爱的!

而且即使内核字符变回去也没吊用 总有问题要改

当然 改这些主要是为了让pvrsrvkm正常 其他的我们都会重新编译

因为呀 我们的内核很多头文件和原版不一样

毕竟我是懒人x 其实pvrsrvkm也有源码 在这里感谢jingpad 开源的紫光bsp真的帮大忙了

骂一下realme 故意写wifi那边有个寄存器不开5g

我们观察linux的源码 其中负责校验的正是Modules.c

和他关联的有几个内核设置项

1.

在load_modules结构体中 是这样的

static int load_module(struct load_info *info, const char __user *uargs,
               int flags)
{
    struct module *mod;
    long err;
    char *after_dashes;

    err = module_sig_check(info, flags);
    if (err)
        goto free_copy;

然后 加上

    flags |= MODULE_INIT_IGNORE_MODVERSIONS;
    flags |= MODULE_INIT_IGNORE_VERMAGIC;

就好了 也算是用了官方的一部分东西 就像这样

static int load_module(struct load_info *info, const char __user *uargs,
               int flags)
{
    struct module *mod;
    long err;
    char *after_dashes;

    flags |= MODULE_INIT_IGNORE_MODVERSIONS;
    flags |= MODULE_INIT_IGNORE_VERMAGIC;

    err = module_sig_check(info, flags);
    if (err)
        goto free_copy;

2.

在setup_load_info() 跳过 module_layout 校验 原版是这个样子

    info->index.pcpu = find_pcpusec(info);

    /* Check module struct version now, before we try to use module. */
    if (!check_modstruct_version(info, mod))
        return ERR_PTR(-ENOEXEC);

    return mod;
}

能看懂C的都知道 如果在这里校验不通过就会返回enoexec 其实这货也挺重要

但是先删了吧

不让他return enoexec就行 我是打了个prwarn方便自己找

就像这样

    info->index.pcpu = find_pcpusec(info);

    /* Check module struct version now, before we try to use module. */
    if (!check_modstruct_version(info, mod))
        pr_warn("%s: module_layout version mismatch, FORCING load (DANGEROUS!)\n",
                info->name);

    return mod;
}

3.

在check_version() 的 bad_version 分支返回 1 这货是真的南通篇里的套 没屌用 真浪费

bad_version:
    pr_warn("%s: disagrees about version of symbol %s\n",
           info->name, symname);
    return 0;
}

原版存在的意义其实挺明显 防止一些预制的脏东西被加载 不过在这真的很碍事

直接让他return 1就行 不让他解析失败

bad_version:
    pr_warn("%s: disagrees about version of symbol %s\n",
           info->name, symname);
    return 1;
}

没记错的话就是改这三个就ok了来着 虽然我总记得要改四个

这样.ko模块加载的时候就不会有问题了

发现缺漏欢迎留言喵 下一个讲适配屏幕

此作者没有提供个人介绍。
最后更新于 2026-09-28