Files
Obsidian-Main/21.01. OS/21.01. Linux/Replace custom build module driver.md
Awin Huang 1a181315d8 vault backup: 2025-11-24 15:12:58
Affected files:
21.01. OS/21.01. Linux/Replace custom build module driver.md
21.01. OS/21.01. Linux/modprobe 與 insmod 的差別.md
30. 工作 - Logitech/project - Lanky.md
2025-11-24 15:12:58 +08:00

115 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
tags:
aliases:
date: 2025-11-24
time: 15:05:35
description:
---
This page note how to isntall custom built `.ko` file.
### 版號確認
`uname -a` 查看目前 kernel 版本。
`modinfo tcxxx_pcie_eth.ko` 檢查 vermagic 版本。
ko 版本與 kernel 版本必須一致。
```
1|KM1:/ # uname -r
5.15.78-qki-consolidate-android13-8-gf5cf1391b188
```
```
KM1:/ # modinfo /storage/emulated/0/Documents/tc956x_pcie_eth.ko
filename: /storage/emulated/0/Documents/tc956x_pcie_eth.ko
license: GPL v2
author: Toshiba Electronic Devices & Storage Corporation
description: TC956X PCI Express Ethernet Network Driver
srcversion: 5F197F74D301D4F6ADC6A72
depends:
name: tc956x_pcie_eth
vermagic: 5.15.167-g94a5a0e4b3fa SMP preempt mod_unload modversions aarch64
```
### Unload old driver
#### Old driver is a module
`lsmod | grep tc956x` 確認原本的 module 已安裝。
`rmmod` 移除原本的 module`sudo rmmod tc956x_pcie_eth`
`insmod` 來安裝新的 module`insmod tc956x_pcie_eth.ko`
#### Old module is static link
`lsmod` 找不到東西,但 `find` 卻在 `/sys` 裡看到了一堆 `tc956x` 相關的檔案。這代表原本的驅動程式是**直接編譯進核心 (Static Linked)**,而不是獨立的 `.ko` 檔案。
##### 1. 確認原本的 Module 是否已正確安裝
```shell
ls -l /sys/bus/pci/drivers/tc956x_pci-eth/
```
如果在該目錄下看到類似 `0001:05:00.0` 這樣的 PCI ID 連結Symlink就代表**原本的內建驅動已經抓到裝置並正在控制它**。
##### 2. 解綁原本的內建驅動
```shell
# 解綁 Port 0
echo -n "0001:05:00.0" > /sys/bus/pci/drivers/tc956x_pci-eth/unbind
# 解綁 Port 1 (如果有)
echo -n "0001:05:00.1" > /sys/bus/pci/drivers/tc956x_pci-eth/unbind
```
##### 3. 載入新的 `.ko` 檔
```shell
insmod tc956x_pcie_eth.ko
```
### 可能的錯誤
#### Unbind driver 就 reboot
##### 試著先把 eth 關掉:
```
ip link set eth0 down
```
如果還是會當機,試著查看 log先確認 log 目錄下有哪些檔案:
```shell
ls -l /sys/fs/pstore/
```
假設有發現檔案叫做 `pmsg-ramoops-0`,那可以:
```shell
cat /sys/fs/pstore/pmsg-ramoops-0
```
如果是亂碼,試著:
```shell
logcat -P /sys/fs/pstore/pmsg-ramoops-0
```
或是:
```shell
string /sys/fs/pstore/pmsg-ramoops-0
```
##### 使用 `driver_override` (繞過 Unbind 的崩潰)
1. 先載入新的 KO 檔
1. `insmod /storage/emulated/0/Documents/tc956x_pcie_eth.ko`
2. **設定 Driver Override** 強制指定該 PCI 裝置未來只能使用新的驅動名稱。
1. `echo "tc956x_pcie_eth" > /sys/bus/pci/devices/0001:05:00.0/driver_override`
3. **嘗試替換**
- 不執行 `unbind` (因為會當機),我們嘗試執行 `bind` 給新驅動。但在標準 Linux 中,如果裝置已被佔用,通常需要先 `unbind`
- 但在某些架構下,如果我們無法執行 `unbind`,唯一的路是修改 `boot.img` 的 cmdline加上 `initcall_blacklist` 禁用舊驅動初始化。
#### Unknown symbol
如果 insmod 的訊息是:`tc956x_pcie_eth: Unknown symbol ...` 之類。
這代表您的驅動程式依賴某個核心功能(例如 PHY 介面或 PTP但目前的核心沒有開啟該功能。
#### Version mismatch (版本不符)
核心會說 `disagrees about version of symbol``version magic '...' should be '...'`
這代表您的 `.ko` 檔是給 Kernel A 編譯的,但您的機器跑的是 Kernel B。
#### Initialization failed
初始化失敗 - 這是最像 ENOENT 的原因。
如果驅動程式在載入時(`module_init`)嘗試讀取某個**設定檔**或**Firmware (韌體)**(例如 `/vendor/firmware/tc956x.bin`),而那個檔案不存在,驅動程式會回傳錯誤代碼 `-ENOENT`,導致 `insmod` 顯示「找不到檔案」。
### 題外化
#### [[modprobe 與 insmod 的差別]]
# 參考來源
- [Linux .ko 檔案置換教學](https://gemini.google.com/app/52b1336e11a24217?hl=zh-TW)