GNU General Public License v3.0

项目介绍

R0rt1z2/lkpatcher 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 GNU General Public License v3.0 许可证,目前获得 411 个 Star、86 次 Fork。

仓库全名
R0rt1z2/lkpatcher
开源许可证
GNU General Public License v3.0
Star 数
411
Fork 数
86
未关闭 Issue
23

README 摘要

lkpatcher lkpatcher is a comprehensive Python tool and module designed for modifying and patching LK (Little Kernel) bootloader images. The utility offers flexibility along with an intuitive API for maximum control. To use the library, Python 3.10 or higher is required. An alternative method is available via the web version, which is not open-source but offers the same functionality with a GUI. Installation [!NOTE]…

完整内容见 GitHub 原始 README 或本页 。

常见问题

lkpatcher 用什么语言开发?

主要使用 Python 编写,采用 GNU General Public License v3.0 许可证,目前有 411 个 Star。

lkpatcher 可以免费商用吗?

仓库标注的许可证为 GNU General Public License v3.0,商用前请先阅读仓库 LICENSE 文件确认授权范围。

如何获取 lkpatcher 的源码?

可以直接克隆仓库:git clone https://github.com/R0rt1z2/lkpatcher.git,默认分支为 master;安装与运行步骤见仓库 README。

lkpatcher 最近还在维护吗?

仓库最近一次更新为 4 天前,当前有 23 个未关闭 Issue;Star 数为 411,可作为社区活跃度的参考。

关于

Streamline tool and module to patch bootloader (LK) images of MTK device(s).

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
597
价值分
272
活跃状态
活跃

语言占比

Nix
Python
Shell

README

lkpatcher

License GitHub Issues

lkpatcher is a comprehensive Python tool and module designed for modifying and patching LK (Little Kernel) bootloader images. The utility offers flexibility along with an intuitive API for maximum control.

To use the library, Python 3.10 or higher is required. An alternative method is available via the web version, which is not open-source but offers the same functionality with a GUI.

Installation

sudo apt install python3-pip # If you don't have pip installed.
pip3 install --upgrade pip   # If pip hasn't been updated yet.
pip3 install --upgrade git+https://github.com/R0rt1z2/lkpatcher

[!NOTE] Windows users should omit the first two command(s) and install Python manually from here.

Basic Usage

python3 -m lkpatcher [-h] [options] bootloader_image

Command-line Options

Main Options:
-o, --output FILE           Path to the output patched image
-c, --config FILE           Path to configuration file (JSON)
-j, --json-patches FILE     Path to JSON file with custom patches
Operational Modes:
-l, --list-partitions       List all partitions in the bootloader image
-d, --dump-partition NAME   Dump partition with NAME to a file
-i, --partition-info NAME   Display detailed information about partition NAME
--analyze-policies          Analyze security policies in the LK partition
--dry-run                   Perform a dry run without writing changes
--export-config FILE        Export default configuration to FILE and exit
Patch Control:
--category CATEGORY         Patch category to apply (can be used multiple times)
--exclude CATEGORY          Patch category to exclude (can be used multiple times)
--patch-policies            Patch security policies to disable verification
--cert-bypass [MODE]        Re-sign a patched image with the cert bypass.
                            MODE is 'override' (default) or 'wrap'
Partition Management:
--add-partition NAME FILE   Add new partition with NAME from FILE
--replace-partition NAME FILE Replace partition with NAME from FILE
--remove-partition NAME     Remove partition with NAME
--add-certificate PART FILE Add certificate from FILE to partition PART
--partition-address ADDR    Memory address for new or replaced partition (hex or decimal)
--partition-legacy          Use legacy header format for new partitions
--cert-type TYPE            Certificate type for --add-certificate (cert1, cert2)
Backup Options:
--backup                    Create a backup before patching
--backup-dir DIR            Directory to store backups
Logging Options:
--log-level LEVEL           Set logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--log-file FILE             Log to specified file in addition to console
Miscellaneous:
-v, --version               Show version information and exit
-h, --help                  Show help message and exit

Examples:

# Basic patching with default settings
python3 -m lkpatcher lk.img

# Specify output file
python3 -m lkpatcher lk.img -o patched.img

# Use custom configuration and patches
python3 -m lkpatcher lk.img -c config.json -j patches.json

# List partitions in an image
python3 -m lkpatcher lk.img --list-partitions

# Dump a specific partition
python3 -m lkpatcher lk.img -d lk

# Apply only specific patch categories
python3 -m lkpatcher lk.img --category fastboot --category dm_verity

# Export default configuration
python3 -m lkpatcher --export-config config.json

# Analyze security policies
python3 -m lkpatcher lk.img --analyze-policies

# Add a new partition
python3 -m lkpatcher lk.img --add-partition custom data.bin --partition-address 0x41000000

# Replace an existing partition
python3 -m lkpatcher lk.img --replace-partition lk u-boot.bin --cert-bypass wrap

# Remove an existing partition
python3 -m lkpatcher lk.img --remove-partition unwanted

# Add certificate to a partition
python3 -m lkpatcher lk.img --add-certificate lk cert1.der --cert-type cert1

# Re-sign an already-patched image
python3 -m lkpatcher patched-lk.img --cert-bypass

# Same, but using the wrap strategy
python3 -m lkpatcher patched-lk.img --cert-bypass wrap

Using as a Library

You can use lkpatcher in your own projects:

from lkpatcher.patcher import LkPatcher
from lkpatcher.config import PatcherConfig

# Create a custom configuration (optional)
config = PatcherConfig()
config.log_level = "DEBUG"
config.backup = True
config.patch_categories = {"fastboot", "dm_verity"}  # Only apply these categories

# Create the patcher object
patcher = LkPatcher(
    image='lk.img',
    patches='patches.json',
    config=config
)

# List available partitions
partitions = patcher.image.get_partition_list()
print(f"Available partitions: {partitions}")

# Apply patches and save the result
output_path = patcher.patch(output='lk-patched.bin')

# Extract all partitions to a directory
patcher.extract_all_partitions("partitions_dir")

# Analyze the image
analysis = patcher.analyze_image()
print(analysis)

Patch Files

Patch files are JSON files containing categories of binary patches to apply. Each patch consists of a “needle” (bytes to find) and a “patch” (replacement bytes).

Example patch file:

{
    "mode": "update",
    "fastboot": {
        "2de9f04fadf5ac5d": "00207047"
    },
    "dm_verity": {
        "30b583b002ab0022": "00207047"
    }
}

The mode can be:

  • update: Add/update patches while keeping defaults (default)
  • replace: Replace all default patches with only those in the file

Configuration

You can customize behavior through a JSON configuration file:

{
    "log_level": "INFO",
    "backup": true,
    "backup_dir": "./backups",
    "verify_patch": true,
    "allow_incomplete": true,
    "dry_run": false,
    "patch_categories": ["fastboot", "dm_verity"],
    "exclude_categories": []
}

Important Notes

  • To successfully boot patched images, unlock seccfg using mtkclient.
  • Create backups of your existing images before patching. You are responsible for any damage to your device.
  • If your device doesn’t boot after unlocking with mtkclient, it likely uses SBC and is not supported.
  • For debugging issues, dump the expdb partition with mtkclient and open a new issue with the dump attached.
  • For device-specific patch requests, open a new issue with your LK image and the generated image.txt file.
  • To learn how these patches work and create your own, see the guide.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

评论

暂无评论