• 主页
  • 归档
  • 软件
所有文章 友情链接 关于我

  • 主页
  • 归档
  • 软件

Android boot.img 文件的解包、修改与重打包小记

2023-12-24

本文记录 Andoird 的 boot.img 文件的组成、解包、修改以及重打包方法。

Android 的文件由以下内容组成:

- kernel,文件名一般为 zImage
- ramdisk,文件名一般为 initrd.gz。该文件解压之后是一个 cpio archive 文件(亦是 cramfs 文件系统?)
- second,这是第二阶段启动文件系统

此外,上述文件的地址偏移量也是在打包的时候必须提供的信息。

使用 abootimg 可以轻松地解压。

https://github.com/ggrandou/abootimg

处理过程

- 使用 abootimg 将 boot.img 解包,得到 kernel、ramdisk、second 等文件
- 使用 cpio 将 ramdisk 文件解包
- 修改 ramdisk 中的文件
- 使用 cpio 将修改好的文件重新打包成新的 ramdisk 文件
- 使用 abootimg 将新的 ramdisk 文件更新到 boot.img 中
- 使用 fastboot 刷入 abootimg

注意,cpio 解包和打包过程均应使用 root 用户。

使用 cpio 解包 ramdisk

1
2
3
4
gzip -d boot.img-ramdisk.gz
mkdir ramdisk
cd ramdisk
cpio -i -d -H newc -F ../boot.img-ramdisk --no-absolute-filenames

使用 cpio 制作 ramdisk

1
2
# 在 ramdisk 目录下
sudo find . | sudo cpio --create --format='newc' >../new-boot.img-ramdisk

使用 abootimg 将新的 ramdisk 更新到 boot.img 中

1
abootimg -u boot.img -r new-boot.img-ramdisk

摘录——Android 系统在连接电源时自动开机的方法

https://android.stackexchange.com/questions/20021/automatically-power-on-android-when-the-charger-is-connected

fastboot oem off-mode-charge 0 is the genuine method if your device supports. It’s Google’s recommended method but not all OEMs/vendors implement the command in bootloader. Or on some devices it’s reset on next reboot. If off-mode-charge is disabled, bootloader won’t pass androidboot.mode=charger commandline parameter to kernel when charger is inserted, so device boots normally.

Otherwise when ro.bootmode property is set to charger on boot, init doesn’t continue the normal boot process. Instead limited number of services are started and charging animation is displayed. So you can instruct init to reboot the device whenever charger mode is detected. Create a new .rc file or edit any existing one:

/system/etc/init/off_mode_charge.rc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
on charger
setprop sys.powerctl reboot,leaving-off-mode-charging
Or execute reboot binary:

on charger
exec - -- /system/bin/reboot leaving-off-mode-charging
But if SELinux is enforcing, stock policy may not let init execute /system/bin/reboot. So use Magisk's context (or whatever rooting solution you use):

on charger
exec u:r:magisk:s0 -- /system/bin/reboot
Don't forget to set permissions on *.rc file (chown 0.0, chmod 0644, chcon u:object_r:system_file:s0).

It's also possible to continue boot process instead of restarting the device by replacing class_start charger with trigger late-init in /init.rc file:

on charger
#class_start charger
trigger late-init
Or by setting property sys.boot_from_charger_mode:

on charger
setprop sys.boot_from_charger_mode 1

This method should work on all devices irrespective of OEM as it doesn’t depend on vendor-specific charging binaries like playlpm, battery_charging, chargeonlymode, zchgd, kpoc_charger and so on.

Also replacing binaries of important services like healthd - which take care of a lot of things related to battery, storage etc. - is not a good idea. In this case if the service runs both in charger and normal mode, device may get into bootloop.

On non-System-as-Root devices it’s not necessary to modify /system partition (e.g. if you don’t want to break dm-verity for OTA updates to work). Simply unpack boot.img and edit /init.rc file in ramdisk.



-------------本文结束感谢您的阅读-------------



  • linux
  • android

扫一扫,分享到微信

微信分享二维码
OPPO Reno6 ColorOS13.1内核源码编译记录
twrp设备树从入门到放弃
  1. 1. 处理过程
    1. 1.1. 使用 cpio 解包 ramdisk
    2. 1.2. 使用 cpio 制作 ramdisk
    3. 1.3. 使用 abootimg 将新的 ramdisk 更新到 boot.img 中
    4. 1.4. 摘录——Android 系统在连接电源时自动开机的方法
  • /system/etc/init/off_mode_charge.rc

  • 预览:

    0  字
    2 评论
    • Latest
    • Oldest
    • Hottest
    好2024-09-08
    Firefox129.0Android 13

    另:我感觉这个博客比不上hugo+paperwhite

    当然不是说内容

    好2024-09-08
    Firefox129.0Android 13

    编译内核一般需要gcc,那么除非你有arm64架构的服务器,你就得用交叉编译工具链了。有人说linero的gcc好用,我也不晓得,不过,有一个编译低版本(注意!是低版本)内核绝对能用的就是谷歌的gcc。在这里:https://android.googlesource.com/platform/prebuilts/gcc/
    一般选择https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/ 和 https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/
    其他看着办,比如macos就不能用这两个,要选darwin的
    谷歌由于不知道为啥,停更gcc,删除工具,可是git的commit没删,所以,克隆仓库,git reset --hard (最后一个没删除工具的commit的完整sha1值) 就能完美恢复。

    如果编译出来是有image.lz4(不带-dtb),要使用原厂boot.img打包自定义kernel的boot.img,直接重命名那个lz4为boot.img-kernel,然后再使用mkbootimg打包就行。

    另:吐槽一下,这些个工具挺难找,找到了也不知道是不是正规的,版本是不是新的。abootimg的仓库也404了,目前推荐组合:mkbootimg+unpackbootimg,还有,有些时候编译要mkdtimg,mkdtimg在谷歌的仓库里,是一个python脚本,叫mkdtboimg.py,直接ln一个硬链接为mkdtimg(不要扩展名)然后export到PATH就行。

    https://android.googlesource.com/platform/system/libufdt/
    克隆,mkdtimg在utils/src下

    https://github.com/osm0sis/mkbootimg
    Mkbootimg和unpackbootimg,只有源代码,要make一下,好在make非常快
    注:我懒得用命令行界面。所以我找了一个图形界面的工具,效率++:
    https://github.com/GameTheory-/mktool
    java编写,使用简单,但是要装openjdk

    注:最坑的是编译一半由于非常非常简单的东西出现报错,比如:bc没装:(

    本来想写一篇博客的,后来想想没什么好写的,发个评论算数。
    可能会补充,毕竟这些个东西对小白来说也听不懂,对大佬来说太简单weibo_smile

    Powered by Waline v2.15.8
    © 2025 dpkg123
    本站已苟活 2 年 52 天 21 小时 50 分钟 16 秒
    Hexo Theme Yilia by Litten
    萌ICP备20231955号
    • 所有文章
    • 友情链接
    • 关于我

    tag:

    • 3DS
    • linux
    • buildroot
    • kernel
    • android
    • dtb
    • dtc
    • OPPO
    • hexo
    • web前端
    • github
    • shell
    • 刷机工具
    • 二次元
    • 刷机
    • debian
    • 手机
    • Android
    • Root
    • Github
    • CI
    • Linux
    • 虚拟化 Android
    • twrp
    • build
    • 归档
    • 番剧
    • 别当欧尼酱了
    • 哔哩哔哩
    • av10492
    • 猎奇
    • 内核
    • 编译
    • 速通
    • 教程
    • 奇技淫巧
    • TypeScript
    • Github Actions
    • 杂谈
    • 网页搭建
    • 联发科
    • KernelSU
    • kvm
    • android kernel
    • gki
    • alpine
    • 软件源
    • vps
    • 网页加速
    • fastboot
    • PostmarketOS
    • ROM移植
    • kernelSU
    • 移植
    • 内核编译
    • Kernel
    • miui
    • coloros
    • makefile
    • 9008

      缺失模块。
      1、请确保node版本大于6.2
      2、在博客根目录(注意不是yilia根目录)执行以下命令:
      npm i hexo-generator-json-content --save

      3、在根目录_config.yml里添加配置:

        jsonContent:
          meta: false
          pages: false
          posts:
            title: true
            date: true
            path: true
            text: false
            raw: false
            content: false
            slug: false
            updated: false
            comments: false
            link: false
            permalink: false
            excerpt: false
            categories: false
            tags: true
      

    • 关于高通gunyah虚拟化的一些研究

      2025-03-03

      #虚拟化 Android

    • 安卓内核编译速通

      2025-02-13

      #Android#内核#编译#速通#教程#奇技淫巧

    • 使用github工作流全自动构建postmarketos刷机包

      2025-02-13

      #刷机#手机#Android#Root#Github#CI#Linux

    • PostmarketOS移植常见问题

      2024-08-12

      #linux#android#刷机#PostmarketOS#ROM移植

    • 本地搭建第三方alpine repo

      2024-03-22

      #linux#alpine#软件源

    • 折腾ts action遇到的其中一个问题

      2024-02-11

      #TypeScript#Github Actions

    • dtc编译教程

      2024-01-29

      #linux#android#dtb#dtc

    • OPPO Reno6 ColorOS13.1内核源码编译记录

      2024-01-15

      #kernel#android#OPPO

    • Android boot.img 文件的解包、修改与重打包小记

      2023-12-24

      #linux#android

    • twrp设备树从入门到放弃

      2023-12-10

      #linux#android#twrp#build

    • 3DS折腾linux小记

      2023-10-06

      #3DS#linux#buildroot#kernel

    • 暂停更新博客通知

      2023-08-08

      #杂谈

    • Makefile入门

      2023-08-04

      #linux#makefile

    • linux电脑给手机进行9008刷机

      2023-07-28

      #linux#刷机#9008

    • 浅谈安卓内核的碎片化

      2023-07-19

      #kernel#android#android kernel#gki

    • 哔哩哔哩危险地带部分补档

      2023-06-28

      #哔哩哔哩#av10492#猎奇

    • 使用dh_make快速构建deb

      2023-06-22

      #linux#debian

    • linux解压payload.bin和转换system.new.dat.br

      2023-06-18

      #linux#android#刷机

    • 浅谈联发科设备玩机的可能性

      2023-05-15

      #刷机#联发科#KernelSU#kvm

    • 给OPPO Reno6移植kernelSU

      2023-05-10

      #linux#kernelSU#移植#内核编译#Kernel

    • 别当欧尼酱了12集归档

      2023-05-02

      #归档#番剧#别当欧尼酱了

    • 珍爱生命,远离小众云

      2023-05-01

      #vps

    • fdt转dtb

      2023-04-30

      #android#dtb

    • coloros和miui

      2023-04-30

      #android#手机#miui#coloros

    • linux刷机工具箱

      2023-04-29

      #linux#shell#刷机工具

    • 简单写一个shell刷机脚本之进阶篇

      2023-04-29

      #linux#shell#刷机#fastboot

    • 简单写一个shell刷机脚本

      2023-04-29

      #linux#shell#刷机

    • linux娘

      2023-04-12

      #linux#二次元

    • 使用netlify加快博客访问速度

      2023-04-12

      #hexo#github#网页加速

    • hexo博客源码备份

      2023-04-10

      #hexo#web前端#github

    • 新博客

      2023-04-10

      #hexo#网页搭建

    • Hello World

      1970-01-01

    • note现在暂不考虑申请新的友链
    • 图床
    • 留言板
    • 原来的博客
    • Adminzhangの个人博客
    • cyp0633的blog
    • 小码同学
    • U.M.R-Powered-Blog
    • 秋澪Akimio
    • ialtone的小站
    • PiCpo的阁楼
    • artiga033
    • 柏园猫のBlog
    • 欠陥電気の摸鱼小池
    • 洛仙璃の幻梦
    • 木屐落在水洼了
    • 湛蓝的调色板
    • SakuraKooi的Blog
    • 新世界的大门
    • 沨鸾的小窝
    • Revincx
    • JIPA233の小窝
    • Mufanc
    • Fika
    • Pinpe的物语
    • 残夜的小博客
    透明小菜鸡一枚<br><br>目前正在学习C++和C<br>正在移植postmarketOS到k30pro上<br>本博客使用github-page作为网页托管平台,netlify提供cdn加速服务。<br>不知道为啥yilia主题改不了下面的文字。<br>总之欢迎来到这里!