Install Debian Server on Xiaomi Phone

红米note4x刷成debian服务器

参考链接:

Pmbootstrap 相关:
https://wiki.postmarketos.org/wiki/Xiaomi_Redmi_Note_4_(xiaomi-mido)
https://wiki.postmarketos.org/wiki/Installation
https://wiki.postmarketos.org/wiki/Pmbootstrap

参考博客:
https://blog.csdn.net/github_38345754/article/details/114291930
https://forum.renegade-project.tech/t/835-845-855-uefi-archlinux/2497
https://forum.renegade-project.tech/t/835-845-855-uefi-ubuntu-rootfs/2891
https://www.knightli.com/2023/08/09/随身wif-idebian-固件编译/

参考项目:
https://github.com/umeiko/KlipperPhonesLinux
https://gitee.com/meiziyang2023/hm2-ubuntu-ports

参考视频:
https://www.bilibili.com/video/BV15N4y127mN/
https://www.bilibili.com/video/BV13G411Y7u4/

名词解释:
https://wiki.postmarketos.org/wiki/Glossary

源码下载

  1. mainline

https://github.com/msm8953-mainline/linux

注意下载打包后的, 而不是main仓库, 版本选择 6.7.5-r0

  1. 内核编译配置文件

https://gitlab.com/postmarketOS/pmaports/-/blob/master/device/community/linux-postmarketos-qcom-msm8953/config-postmarketos-qcom-msm8953.aarch64?ref_type=heads

或者 https://github.com/umeiko/KlipperPhonesLinux/tree/main/LinuxKernels/msm8953

  1. 开源驱动

https://gitlab.com/postmarketOS/pmaports/-/blob/master/device/community/firmware-xiaomi-mido/APKBUILD?ref_type=heads

找到这个下载地址

https://github.com/Kiciuk/proprietary_firmware_mido

或者从pom中提取

或者 https://github.com/umeiko/KlipperPhonesLinux/tree/main/LinuxKernels/msm8953

依赖安装

1
2
3
sudo apt-get install cpio
sudo apt install avrdude gcc-avr binutils-avr avr-libc stm32flash libnewlib-arm-none-eabi gcc-arm-none-eabi binutils-arm-none-eabi pkg-config
sudo apt install binfmt-support qemu-user-static gcc-10-aarch64-linux-gnu kernel-package fakeroot simg2img img2simg mkbootimg bison

编译内核

内核编译配置

下载 config-postmarketos-qcom-msm8953.aarch64, 用check-kernel-config.sh脚本进行处理

1
2
3
4
5
6
7
8
9
mv config-postmarketos-qcom-msm8953.aarch64 config
./check-kernel-config.sh config -w

# 复制到linux文件夹中
cp config linux/.config

# 设置环境变量
source ./env.sh
echo $ARCH
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# check-kernel-config.sh
#!/bin/bash

FILE=$1

[ -f "$FILE" ] || {
	echo "Provide a config file as argument"
	exit
}

write=false

if [ "$2" = "-w" ]; then
	write=true
fi

CONFIGS_ON="
CONFIG_IKCONFIG
CONFIG_CPUSETS
CONFIG_AUTOFS4_FS
CONFIG_TMPFS_XATTR
CONFIG_TMPFS_POSIX_ACL
CONFIG_CGROUP_DEVICE
CONFIG_CGROUP_MEM_RES_CTLR
CONFIG_CGROUP_MEM_RES_CTLR_SWAP
CONFIG_CGROUP_MEM_RES_CTLR_KMEM
CONFIG_RTC_DRV_CMOS
CONFIG_BLK_CGROUP
CONFIG_CGROUP_PERF
CONFIG_IKCONFIG_PROC
CONFIG_SYSVIPC
CONFIG_CGROUPS
CONFIG_CGROUP_FREEZER
CONFIG_NAMESPACES
CONFIG_UTS_NS
CONFIG_IPC_NS
CONFIG_USER_NS
CONFIG_PID_NS
CONFIG_NET_NS
CONFIG_AUDIT
CONFIG_AUDITSYSCALL
CONFIG_AUDIT_TREE
CONFIG_AUDIT_WATCH
CONFIG_CC_STACKPROTECTOR
CONFIG_DEBUG_RODATA
CONFIG_DEVTMPFS
CONFIG_DEVTMPFS_MOUNT
CONFIG_DEVPTS_MULTIPLE_INSTANCES
CONFIG_ECRYPT_FS
CONFIG_ECRYPT_FS_MESSAGING
CONFIG_ENCRYPTED_KEYS
CONFIG_EXT4_FS_POSIX_ACL
CONFIG_EXT4_FS_SECURITY
CONFIG_FSNOTIFY
CONFIG_DNOTIFY
CONFIG_INOTIFY_USER
CONFIG_FANOTIFY
CONFIG_FANOTIFY_ACCESS_PERMISSIONS
CONFIG_KEYS
CONFIG_SWAP
CONFIG_VT
CONFIG_VT_CONSOLE
CONFIG_SECCOMP
CONFIG_STRICT_DEVMEM
CONFIG_SYN_COOKIES
CONFIG_BT
CONFIG_BT_RFCOMM
CONFIG_BT_RFCOMM_TTY
CONFIG_BT_BNEP
CONFIG_BT_BNEP_MC_FILTER
CONFIG_BT_BNEP_PROTO_FILTER
CONFIG_BT_HIDP
CONFIG_XFRM_USER
CONFIG_NET_KEY
CONFIG_INET
CONFIG_IP_ADVANCED_ROUTER
CONFIG_IP_MULTIPLE_TABLES
CONFIG_INET_AH
CONFIG_INET_ESP
CONFIG_INET_IPCOMP
CONFIG_INET_XFRM_MODE_TRANSPORT
CONFIG_INET_XFRM_MODE_TUNNEL
CONFIG_INET_XFRM_MODE_BEET
CONFIG_IPV6
CONFIG_INET6_AH
CONFIG_INET6_ESP
CONFIG_INET6_IPCOMP
CONFIG_INET6_XFRM_MODE_TRANSPORT
CONFIG_INET6_XFRM_MODE_TUNNEL
CONFIG_INET6_XFRM_MODE_BEET
CONFIG_IPV6_MULTIPLE_TABLES
CONFIG_NETFILTER
CONFIG_NETFILTER_ADVANCED
CONFIG_NETFILTER_NETLINK
CONFIG_NETFILTER_NETLINK_ACCT
CONFIG_NETFILTER_NETLINK_LOG
CONFIG_NETFILTER_NETLINK_QUEUE
CONFIG_NETFILTER_TPROXY
CONFIG_NETFILTER_XTABLES
CONFIG_NETFILTER_XT_CONNMARK
CONFIG_NETFILTER_XT_MARK
CONFIG_NETFILTER_XT_MATCH_ADDRTYPE
CONFIG_NETFILTER_XT_MATCH_CLUSTER
CONFIG_NETFILTER_XT_MATCH_COMMENT
CONFIG_NETFILTER_XT_MATCH_CONNBYTES
CONFIG_NETFILTER_XT_MATCH_CONNLIMIT
CONFIG_NETFILTER_XT_MATCH_CONNMARK
CONFIG_NETFILTER_XT_MATCH_CONNTRACK
CONFIG_NETFILTER_XT_MATCH_CPU
CONFIG_NETFILTER_XT_MATCH_DCCP
CONFIG_NETFILTER_XT_MATCH_DEVGROUP
CONFIG_NETFILTER_XT_MATCH_DSCP
CONFIG_NETFILTER_XT_MATCH_ECN
CONFIG_NETFILTER_XT_MATCH_ESP
CONFIG_NETFILTER_XT_MATCH_HASHLIMIT
CONFIG_NETFILTER_XT_MATCH_HELPER
CONFIG_NETFILTER_XT_MATCH_HL
CONFIG_NETFILTER_XT_MATCH_IPRANGE
CONFIG_NETFILTER_XT_MATCH_LENGTH
CONFIG_NETFILTER_XT_MATCH_LIMIT
CONFIG_NETFILTER_XT_MATCH_MAC
CONFIG_NETFILTER_XT_MATCH_MARK
CONFIG_NETFILTER_XT_MATCH_MULTIPORT
CONFIG_NETFILTER_XT_MATCH_NFACCT
CONFIG_NETFILTER_XT_MATCH_OSF
CONFIG_NETFILTER_XT_MATCH_OWNER
CONFIG_NETFILTER_XT_MATCH_PKTTYPE
CONFIG_NETFILTER_XT_MATCH_POLICY
CONFIG_NETFILTER_XT_MATCH_QUOTA
CONFIG_NETFILTER_XT_MATCH_QUOTA2
CONFIG_NETFILTER_XT_MATCH_RATEEST
CONFIG_NETFILTER_XT_MATCH_REALM
CONFIG_NETFILTER_XT_MATCH_RECENT
CONFIG_NETFILTER_XT_MATCH_SCTP
CONFIG_NETFILTER_XT_MATCH_SOCKET
CONFIG_NETFILTER_XT_MATCH_STATE
CONFIG_NETFILTER_XT_MATCH_STATISTIC
CONFIG_NETFILTER_XT_MATCH_STRING
CONFIG_NETFILTER_XT_MATCH_TCPMSS
CONFIG_NETFILTER_XT_MATCH_TIME
CONFIG_NETFILTER_XT_MATCH_U32
CONFIG_NETFILTER_XT_TARGET_AUDIT
CONFIG_NETFILTER_XT_TARGET_CHECKSUM
CONFIG_NETFILTER_XT_TARGET_CLASSIFY
CONFIG_NETFILTER_XT_TARGET_CONNMARK
CONFIG_NETFILTER_XT_TARGET_CONNSECMARK
CONFIG_NETFILTER_XT_TARGET_CT
CONFIG_NETFILTER_XT_TARGET_DSCP
CONFIG_NETFILTER_XT_TARGET_HL
CONFIG_NETFILTER_XT_TARGET_IDLETIMER
CONFIG_NETFILTER_XT_TARGET_LED
CONFIG_NETFILTER_XT_TARGET_LOG
CONFIG_NETFILTER_XT_TARGET_MARK
CONFIG_NETFILTER_XT_TARGET_NFLOG
CONFIG_NETFILTER_XT_TARGET_NFQUEUE
CONFIG_NETFILTER_XT_TARGET_NOTRACK
CONFIG_NETFILTER_XT_TARGET_RATEEST
CONFIG_NETFILTER_XT_TARGET_SECMARK
CONFIG_NETFILTER_XT_TARGET_TCPMSS
CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP
CONFIG_NETFILTER_XT_TARGET_TEE
CONFIG_NETFILTER_XT_TARGET_TPROXY
CONFIG_NETFILTER_XT_TARGET_TRACE
CONFIG_NF_CONNTRACK_ZONES
CONFIG_IP6_NF_FILTER
CONFIG_IP6_NF_IPTABLES
CONFIG_IP6_NF_MANGLE
CONFIG_IP6_NF_MATCH_AH
CONFIG_IP6_NF_MATCH_EUI64
CONFIG_IP6_NF_MATCH_FRAG
CONFIG_IP6_NF_MATCH_HL
CONFIG_IP6_NF_MATCH_IPV6HEADER
CONFIG_IP6_NF_MATCH_MH
CONFIG_IP6_NF_MATCH_OPTS
CONFIG_IP6_NF_MATCH_RPFILTER
CONFIG_IP6_NF_MATCH_RT
CONFIG_IP6_NF_QUEUE
CONFIG_IP6_NF_RAW
CONFIG_IP6_NF_SECURITY
CONFIG_IP6_NF_TARGET_HL
CONFIG_IP6_NF_TARGET_REJECT
CONFIG_IP6_NF_TARGET_REJECT_SKERR
CONFIG_DNS_RESOLVER
CONFIG_IOSCHED_DEADLINE
CONFIG_SUSPEND_TIME
CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
CONFIG_CONSOLE_TRANSLATIONS
CONFIG_EVM
CONFIG_INTEGRITY_SIGNATURE
CONFIG_FHANDLE
CONFIG_EPOLL
CONFIG_SIGNALFD
CONFIG_TIMERFD
CONFIG_TMPFS_POSIX_ACL
"

CONFIGS_OFF="
"
CONFIGS_EQ="
"

ered() {
	echo -e "\033[31m" $@
}

egreen() {
	echo -e "\033[32m" $@
}

ewhite() {
	echo -e "\033[37m" $@
}

echo -e "\n\nChecking config file for Halium specific config options.\n\n"

errors=0
fixes=0

for c in $CONFIGS_ON $CONFIGS_OFF;do
	cnt=`grep -w -c $c $FILE`
	if [ $cnt -gt 1 ];then
		ered "$c appears more than once in the config file, fix this"
		errors=$((errors+1))
	fi

	if [ $cnt -eq 0 ];then
		if $write ; then
			ewhite "Creating $c"
			echo "# $c is not set" >> "$FILE"
			fixes=$((fixes+1))
		else
			ered "$c is neither enabled nor disabled in the config file"
			errors=$((errors+1))
		fi
	fi
done

for c in $CONFIGS_ON;do
	if grep "$c=y\|$c=m" "$FILE" >/dev/null;then
		egreen "$c is already set"
	else
		if $write ; then
			ewhite "Setting $c"
			sed  -i "s,# $c is not set,$c=y," "$FILE"
			fixes=$((fixes+1))
		else
			ered "$c is not set, set it"
			errors=$((errors+1))
		fi
	fi
done

for c in $CONFIGS_EQ;do
	lhs=$(awk -F= '{ print $1 }' <(echo $c))
	rhs=$(awk -F= '{ print $2 }' <(echo $c))
	if grep "^$c" "$FILE" >/dev/null;then
		egreen "$c is already set correctly."
		continue
	elif grep "^$lhs" "$FILE" >/dev/null;then
		cur=$(awk -F= '{ print $2 }' <(grep "$lhs" "$FILE"))
		ered "$lhs is set, but to $cur not $rhs."
		if $write ; then
			egreen "Setting $c correctly"
			sed -i 's,^'"$lhs"'.*,# '"$lhs"' was '"$cur"'\n'"$c"',' "$FILE"
			fixes=$((fixes+1))
		fi
	else
		if $write ; then
			ewhite "Setting $c"
			echo  "$c" >> "$FILE"
			fixes=$((fixes+1))
		else
			ered "$c is not set"
			errors=$((errors+1))
		fi
	fi
done

for c in $CONFIGS_OFF;do
	if grep "$c=y\|$c=m" "$FILE" >/dev/null;then
		if $write ; then
			ewhite "Unsetting $c"
			sed  -i "s,$c=.*,# $c is not set," $FILE
			fixes=$((fixes+1))
		else
			ered "$c is set, unset it"
			errors=$((errors+1))
		fi
	else
		egreen "$c is already unset"
	fi
done

if [ $errors -eq 0 ];then
	egreen "\n\nConfig file checked, found no errors.\n\n"
else
	ered "\n\nConfig file checked, found $errors errors that I did not fix.\n\n"
fi

if [ $fixes -gt 0 ];then
	egreen "Made $fixes fixes.\n\n"
fi

ewhite " "
1
2
3
4
# env.sh
export CROSS_COMPILE=aarch64-linux-gnu-
export ARCH=arm64
export CC=aarch64-linux-gnu-gcc

开始编译

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
cd linux
# 添加内核组件, 如smb支持的cifs
make menuconfig

# debian 不支持内核模块压缩
Enable loadable module support --> Module compression mode (None) --〉

# 修改名称 LOCALVERSION

# 如果需要smb挂载, 同样需要在内核中开启,搜索 cifs utf8 并开启
# 如果要开启串口登录: CONFIG_USB_GADGET CONFIG_USB_G_SERIAL CONFIG_SERIAL_8250  CONFIG_SERIAL_8250_CONSOLE
# usb 接键盘 CONFIG_USB CONFIG_USB_HID CONFIG_HID CONFIG_HID_GENERIC CONFIG_INPUT_KEYBOARD
# usb otg功能  CONFIG_USB CONFIG_USB_XHCI_HCD CONFIG_USB_EHCI_HCD CONFIG_USB_OHCI_HCD CONFIG_USB_OTG
# btrfs 文件系统 CONFIG_BTRFS_FS CONFIG_BTRFS_FS_COMPRESSION

# 开始编译
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc)
make -j$(nproc)

# 生成deb安装包

# ubuntu 20 可以使用的命令
# fakeroot make-kpkg  --initrd --cross-compile aarch64-linux-gnu- --arch arm64  kernel_image kernel_headers 
# make -j$(nproc) ARCH=arm64 KBUILD_DEBARCH=arm64 KDEB_CHANGELOG_DIST=mobile CROSS_COMPILE=aarch64-linux-gnu- deb-pkg

# Ubuntu 23 命令
make -j$(nproc) bindeb-pkg

# 在上级目录生成 heades image 的deb包
cd ../ && ls

# 如果要修改编译后的deb文件名, 请修改.config里的

# Image.gz msm8953-xiaomi-mido.dtb 复制待用
# cp linux/arch/arm64/boot/Image.gz tmp_mkboot/
# cp linux/arch/arm64/boot/dts/qcom/msm8953-xiaomi-mido.dtb tmp_mkboot/

制作rootfs

debian

生成zip

https://github.com/SdtElectronics/debian-rootfs

1
2
3
4
5
# 修改 multistrap.conf

# ssh 改为 openssh-client
# packages=vim 这行只保留vim
suite=stable
1
2
3
4
sudo ./make-rootfs.sh --help
sudo ./make-rootfs.sh arm64 

# 生成的文件在 build/arm64 里, 带时间戳, 例如 arm64-rootfs-20240308T162556Z
1
2
3
4
5
6
7
# 报错
# The following signatures couldn't be verified because the public key is not available
sudo vim /usr/sbin/multistrap
# 323行, 行末添加分号
$config_str .= " -o Apt::Install-Recommends=false";
# 在下方插入
$config_str .= " -o Acquire::AllowInsecureRepositories=yes"

生成rootfs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
dd if=/dev/zero of=root.img bs=1M count=2048
mkfs.btrfs root.img

sudo mount -t btrfs -o compress-force=zstd root.img rootfs
sudo cp -a rootfsrootfs-20240308T162556Z/* rootfs/

sudo cp /etc/resolv.conf rootfs/etc/
sudo cp /etc/hosts rootfs/etc/

# 复制开源驱动
sudo chmod -R +x firmware
sudo chown -R root:root firmware
sudo mkdir -p rootfs/lib/firmware/
sudo cp -a firmware/* rootfs/lib/firmware/

# 复制内核文件
sudo cp *.deb rootfs/tmp

sudo chroot rootfs /usr/bin/sh

btrfs设置压缩比

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
blkid
# 输出可能类似于:
/dev/loop0: UUID="4982f187-6831-40c4-9cc2-9a9258aefe91" UUID_SUB="7b2911ad-be44-4537-866c-efe2ba2ea1b0" BLOCK_SIZE="4096" TYPE="btrfs"
/dev/zram0: UUID="0b775f09-4d6b-42bc-a1ee-7cb3da12877c" TYPE="swap"
/dev/vdc: UUID="c2971f04-c415-4b34-8334-cb8b7e32146b" TYPE="swap"

cat > /etc/fstab << EOF
# /etc/fstab: static file system information
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
UUID=4982f187-6831-40c4-9cc2-9a9258aefe91 /               btrfs   rw,relatime,compress-force=zstd:3        0       1
EOF

安装内核并设置环境

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
# debian换源
apt install apt-transport-https ca-certificates

vim /etc/apt/sources.list

# https://mirrors.tuna.tsinghua.edu.cn/help/debian/
===
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
===

apt update

# 删除自带内核
cd /tmp
dpkg --get-selections | grep linux
dpkg -l | grep -E "linux-headers|linux-image" |awk '{print $2}'|xargs dpkg -P
# 手动删除
# dpkg -P 包名

# 自动生成initram
apt install initramfs-tools zstd
# btrfs
apt install btrfs-progs

# 安装内核
dpkg -i *.deb

# 修改主机名
vim /etc/hostname
vim /etc/hosts

# 设置locales
apt install locales
dpkg-reconfigure locales
update-locale LANG=en_US.UTF-8
# cat /etc/default/locale

# 更改密码
passwd

# 中文字体
apt install fonts-wqy-microhei

# ssh
apt install openssh-server
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
systemctl enable ssh

# 时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 时间同步
apt install systemd-timesyncd
systemctl enable systemd-timesyncd

# 常用软件
apt install sudo net-tools network-manager iputils-ping kmod linux-base vim -y

systemctl enable NetworkManager

# 自动创建内存一半的zram
apt install zram-tools
systemctl enable zramswap.service
vim /etc/default/zramswap
# 取消注释
# ALGO=lz4
# PERCENT=50
# PRIORITY=100

# 可选
# btrfs压缩率分析, compsize /path
apt install btrfs-compsize

# 添加默认WiFi连接,参考后面的内容

# 添加串口登录,参考后面的内容

# 添加rc.local开机脚本,参考后面的内容

# 查看可更新的包
apt list --upgradable
# 更新
apt upgrade

# 清理
rm /tmp/*
apt autoremove
apt clean
history -c

# 退出
exit

开机扩容

方法一: 开机后手动执行命令

1
btrfs filesystem resize max /

方法二: 添加自启服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
cat > /etc/systemd/system/resizefs.service << 'EOF'
[Unit]
Description=Expand root filesystem to fill partition
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c 'btrfs filesystem resize max /'
ExecStartPost=/usr/bin/systemctl disable resizefs.service
RemainAfterExit=true

[Install]
WantedBy=default.target
EOF

systemctl daemon-reload && systemctl enable resizefs.service

rc.local开机脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
EOF

cat > /etc/rc.local <<EOF 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#

exit 0
EOF

chmod +x /etc/rc.local
1
2
systemctl enable rc-local.service
systemctl is-enabled rc-local.service

开启串口登录

内核需要开启

1
2
3
4
5
6
7
CONFIG_USB_GADGET

CONFIG_USB_G_SERIAL

CONFIG_SERIAL_8250

CONFIG_SERIAL_8250_CONSOLE
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
cat > /etc/systemd/system/serial-getty@ttyGS0.service << EOF
[Unit]
Description=Serial Console Service on ttyGS0

[Service]
ExecStart=-/usr/sbin/agetty -L 115200 ttyGS0 xterm+256color
Type=idle
Restart=always
RestartSec=0

[Install]
WantedBy=multi-user.target
EOF

systemctl enable serial-getty@ttyGS0.service
#如果串口登录失效,可能是g_serial模块没有加载
echo g_serial >> /etc/modules

添加默认wifi连接

以note4x为例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd /etc/NetworkManager/system-connections/
touch wifi.nmconnection
chmod 600 wifi.nmconnection

cat  > wifi.nmconnection << EOF
[connection]
id=wifi
uuid=0d100c6b-4149-402d-8ec7-f09166e3624b
type=wifi
interface-name=wlan0

[wifi]
mode=infrastructure
ssid=wifi

[ipv4]
method=auto

[ipv6]
addr-gen-mode=stable-privacy
method=auto

[proxy]
EOF

note2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd /etc/NetworkManager/system-connections/
touch wifi.nmconnection
chmod 600 wifi.nmconnection

cat  > wifi.nmconnection << EOF
[connection]
id=wifi
uuid=5936f33a-b85c-4ccb-a651-56d5aa464a90
type=wifi
interface-name=wlp1s0

[wifi]
mode=infrastructure
ssid=wifi

[ipv4]
method=auto

[ipv6]
addr-gen-mode=stable-privacy
method=auto

[proxy]
EOF
  • 手机开热点查看ip ip neigh

ubuntu(btrfs)

1
2
# https://cdimage.ubuntu.com/ubuntu-base/
wget https://cdimage.ubuntu.com/ubuntu-base/releases/22.04.4/release/ubuntu-base-22.04.4-base-arm64.tar.gz

docker启动不了

ubuntu的问题, 降级iptable解决

1
2
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

arch

1
http://os.archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz

移植其它设备的rootfs

例如 高通410随身wifi的

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 把rootfs.img模板转ext4
simg2img root_temp.img root_temp.ext4
mkdir root_temp

# 挂载模板
sudo mount root_temp.ext4 root_temp

# 新建
mkdir rootfs
dd if=/dev/zero of=root.img bs=1M count=2048
mkfs.ext4 root.img
sudo mount root.img rootfs

# 复制
sudo cp -a root_temp/* rootfs/
sudo umount root_temp

# 复制开源驱动
sudo chmod -R +x firmware
sudo chown -R root:root firmware
sudo cp -a firmware/* rootfs/lib/firmware/

# chroot
./mount_rootfs.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# mount_rootfs.sh
sudo mount root.img rootfs

sudo mount --bind /proc rootfs/proc
sudo mount --bind /dev rootfs/dev
sudo mount --bind /dev/pts rootfs/dev/pts
sudo mount --bind /sys rootfs/sys

sudo cp /etc/resolv.conf rootfs/etc/
sudo cp /etc/hosts rootfs/etc/

sudo rm rootfs/tmp/linux-*.deb 
sudo cp *.deb rootfs/tmp

sudo chroot rootfs /usr/bin/bash

制作boot

复制boot所需的文件

1
2
3
4
5
# 需要rootfs里的 initrd.img 和 内核编译时生成的 Image.gz dtb
./get_kernel_files.sh

# 复制完就可以卸载rootfs了
./umount_rootfs.sh
1
2
3
4
5
# get_kernel_files.sh
mkdir ./tmp_mkboot
cp ./linux/arch/arm64/boot/dts/qcom/*mido*.dtb ./tmp_mkboot/dtb
cp ./linux/arch/arm64/boot/Image.gz ./tmp_mkboot/
cp rootfs/boot/initrd* ./tmp_mkboot/initrd.img
1
2
3
4
5
6
# umount_rootfs.sh
sudo umount rootfs/proc
sudo umount rootfs/dev/pts
sudo umount rootfs/dev
sudo umount rootfs/sys
sudo umount rootfs

生成boot和rootf安卓刷机包

1
2
3
4
# 需要 initrd.img Image.gz dtb
file root.img
# 修改 mkboot.sh 里的 UUID
./mkboot.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# mkboot.sh
rm ./tmp_mkboot/kernel-dtb
cat ./tmp_mkboot/Image.gz ./tmp_mkboot/dtb > ./tmp_mkboot/kernel-dtb
mkbootimg --base 0x80000000 \
        --kernel_offset 0x00008000 \
        --ramdisk_offset 0x01000000 \
        --tags_offset 0x00000100 \
        --pagesize 2048 \
        --second_offset 0x00f00000 \
        --ramdisk ./tmp_mkboot/initrd.img \
        --cmdline "console=tty0 root=UUID=239798af-b82a-4547-b4d4-448ab8335552 rw loglevel=3"\
        --kernel ./tmp_mkboot/kernel-dtb -o ./tmp_mkboot/boot.img
img2simg ./root.img ./tmp_mkboot/rootfs.img

刷机

  1. 首先是小米官方解bl
  2. 刷入 pmos 的 lk2nd
  3. 重启, 手机震动后长按音量减, 进入 lk2nd 引导的 rec
  4. 用fastboot 刷入 boot rootfs
1
2
3
4
5
6
7
8
9

fastboot erase userdata
fastboot erase boot
fastboot flash boot lk2nd.img
fastboot reboot

fastboot flash boot boot.img
fastboot flash userdata rootfs.img
fastboot reboot

关闭屏幕灯光

1
2
3
4
echo 1 > /sys/class/backlight/backlight/bl_power

# 小米note2
echo 1 > /sys/class/backlight/994000.dsi.0/bl_power
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash
file_path="/sys/class/backlight/backlight/bl_power"

current_value=$(cat "$file_path")

if [ "$current_value" -eq 0 ]; then
    new_value=1
else
    new_value=0
fi

echo $new_value > $file_path

查看电流

1
/sys/class/power_supply/qcom-battery/current_now

查看电量

1
2
apt install upower
upower -i /org/freedesktop/UPower/devices/battery_qcom_battery

查看频率

1
cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq

骁龙625超频

查看当前支持的cpu频率

1
2
3
4
5
cd /sys/devices/system/cpu/cpufreq/policy0/
# 频率范围
cat scaling_available_frequencies
# 最大频率
cat scaling_max_freq 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
drivers/clk/qcom/a53-pll.c

# 默认
===
static const struct pll_freq_tbl a53pll_freq[] = {
	{  998400000, 52, 0x0, 0x1, 0 },
	{ 1094400000, 57, 0x0, 0x1, 0 },
	{ 1152000000, 62, 0x0, 0x1, 0 },
	{ 1209600000, 63, 0x0, 0x1, 0 },
	{ 1248000000, 65, 0x0, 0x1, 0 },
	{ 1363200000, 71, 0x0, 0x1, 0 },
	{ 1401600000, 73, 0x0, 0x1, 0 },
	{ }
};
===
1
2
# 频率补充
drivers/soc/qcom/cpr3pd.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 设备树
# 把0x81改为0x85
arch/arm64/boot/dts/qcom/msm8953.dtsi
===
	cpu0_opp: cpu0-opp-table {
		compatible = "operating-points-v2-qcom-cpu";
		nvmem-cells = <&cpu_speed_bin>;
		opp-shared;

		opp-652800000 {
			opp-hz = /bits/ 64 <652800000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp1>;
			opp-suspend;
		};
	
		opp-1036800000 {
			opp-hz = /bits/ 64 <1036800000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp2>;
		};
	
		opp-1248000000 {
			opp-hz = /bits/ 64 <1248000000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp3>;
		};
	
		opp-1401600000 {
			opp-hz = /bits/ 64 <1401600000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp4>;
		};
	
		opp-1689600000 {
			opp-hz = /bits/ 64 <1689600000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp5>;
		};
	
		opp-1804800000 {
			opp-hz = /bits/ 64 <1804800000>;
			opp-supported-hw = <0xC5>;
			required-opps = <&cpr_opp6>;
		};
	
		opp-1958400000 {
			opp-hz = /bits/ 64 <1958400000>;
			opp-supported-hw = <0x85>;
			required-opps = <&cpr_opp7>;
		};
	
		opp-2016000000 {
			opp-hz = /bits/ 64 <2016000000>;
			opp-supported-hw = <0x85>;
			required-opps = <&cpr_opp8>;
		};
	
		opp-2150400000 {
			opp-hz = /bits/ 64 <2150400000>;
			opp-supported-hw = <0x81>;
			required-opps = <&cpr_opp9>;
		};
	
		opp-2208000000 {
			opp-hz = /bits/ 64 <2208000000>;
			opp-supported-hw = <0x81>;
			required-opps = <&cpr_opp10>;
		};
	};
===

扩大分区

如果发现分区大小和rootfs一样大

ext4:

方法一:

1
2
3
4
5
#!/bin/bash

resize2fs $(df / | awk '$NF=="/"{print $1}')
# resize2fs /dev/mmcblk0p30
# resize2fs /dev/mmcblk1p30

方法二:

制作rootfs时

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
cat > /etc/systemd/system/resizefs.service << EOF
[Unit]
Description=Expand root filesystem to fill partition
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c 'exec /usr/sbin/resize2fs $(findmnt -nvo SOURCE /)'
ExecStartPost=/usr/bin/systemctl disable resizefs.service
RemainAfterExit=true

[Install]
WantedBy=default.target
EOF

systemctl enable resizefs.service

定量充电脚本

note4x支持去掉电池直接供电, 需要5V2A电源和长度较短的数据线

小米note2拆电池无法启动, 可以使用定充脚本, 或者拆电池小板改直供电

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash

# 小米note2

# 是否在连接usb
USB_ONLINE="/sys/class/power_supply/qcom-smbchg-usb/online"
# USB充电电流限制文件 微安
USB_CURRENT_LIMIT_FILE="/sys/class/power_supply/qcom-smbchg-usb/input_current_limit"
# USB最大充电电流 微安
USB_MAX_CURRENT_FILE="/sys/class/power_supply/qcom-smbchg-usb/constant_charge_current_max"
# 电池百分比
BATTERY_CAPACITY_FILE="/sys/class/power_supply/qcom-battery/capacity"
# 电池电流 微安
BATTERY_CURRENT_FILE="/sys/class/power_supply/qcom-battery/current_now"
# 电池温度
BATTERY_TEMP_FILE="/sys/class/power_supply/qcom-battery/temp"
# 记录充电记录
CHARGING_LOG_FILE="/root/charging.log"

# 调整电流的变量 微安, 最小刻度为100ma
DISABLE_CHARGE=300000      # 电池放电时的usb电流
TRICKLE_CHARGE=700000      # 电池充电时的usb电流

# 开启放电的电量百分比
DISABLE_START=90
# 开启充电的电量百分比
TRICKLE_START=20

# 检测是否在充电,如果不在充电,退出
if [ "$(cat $USB_ONLINE)" != "1" ]; then
  echo "当前未连接usb,退出..."
  exit 0
fi

# 获取电池电量百分比
battery_capacity=$(cat $BATTERY_CAPACITY_FILE)
# 获取当前usb电流
current_usb_current=$(cat $USB_CURRENT_LIMIT_FILE)
# 时间戳
formatted_time=$(date +"%Y-%m-%d %H:%M:%S")

if [ $battery_capacity -ge $DISABLE_START ]; then
  if [ "$current_usb_current" != "$DISABLE_CHARGE" ]; then
    echo "电量大于等于$DISABLE_START%,设置USB电流为$DISABLE_CHARGE μA"
    echo $DISABLE_CHARGE > $USB_CURRENT_LIMIT_FILE
    echo "$formatted_time: 开始放电" >> $CHARGING_LOG_FILE
  else
    echo "电量大于等于$DISABLE_START%,USB电流已设置为$DISABLE_CHARGE μA,无需重复设置"
  fi
elif [ $battery_capacity -le $TRICKLE_START ]; then
  if [ "$current_usb_current" != "$TRICKLE_CHARGE" ]; then
    echo "电量小于等于$TRICKLE_START%,设置USB电流为$TRICKLE_CHARGE μA"
    echo $TRICKLE_CHARGE > $USB_CURRENT_LIMIT_FILE
    echo "$formatted_time: 开始充电" >> $CHARGING_LOG_FILE
  else
    echo "电量小于等于$TRICKLE_START%,USB电流已设置为$TRICKLE_CHARGE μA,无需重复设置"
  fi
else
  echo "电量在$TRICKLE_START%和$DISABLE_START%之间,保持当前USB电流"
fi

获取mkbootimg 参数

https://wiki.postmarketos.org/wiki/Huawei_Honor_6X_(huawei-berlin)

https://github.com/anestisb/android-unpackbootimg

https://github.com/OpenStick/OpenStick/issues/7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
git clone https://github.com/anestisb/android-unpackbootimg
make -C android-unpackbootimg

./android-unpackbootimg/unpackbootimg -i boot.img -o boot/
# 打印出上面的mkbootimg 参数

===
BOARD_KERNEL_CMDLINE PMOS_NO_OUTPUT_REDIRECT  pmos_boot_uuid=518e9aa3-4de5-42c3-b6ec-6f1d8384030a pmos_root_uuid=19d04306-f22f-4009-ad75-a6afd390fba8
BOARD_KERNEL_BASE 80000000
BOARD_NAME 
BOARD_PAGE_SIZE 4096
BOARD_HASH_TYPE sha1
BOARD_KERNEL_OFFSET 00008000
BOARD_RAMDISK_OFFSET 01000000
BOARD_SECOND_OFFSET 00f00000
BOARD_TAGS_OFFSET 00000100
===

切换OTG主从模式

1
2
3
4
5
# 625可以这样切主从
# 主机模式:
echo host > /sys/kernel/debug/usb/7000000.usb/mode
# 从设备模式
echo device > /sys/kernel/debug/usb/7000000.usb/mode

测试稳定性

1
wget -qO- https://raw.githubusercontent.com/LemonBench/LemonBench/main/LemonBench.sh | bash -s -- --fast
1
stress-ng --cpu 8 --timeout 120s --perf

btrfs 查看压缩信息

1
2
3
4
btrfs filesystem usage /

# 整理碎片
btrfs filesystem defragment -r /
build with Hugo, theme Stack, visits 0