mirror of
https://github.com/dikeckaan/mu300-linux.git
synced 2026-12-06 00:03:00 +00:00
install.sh and install.ps1 refused a device whose Android runs from slot b, which after an OTA update is common, although the boot image, the init, the system tools and the Magisk installer already support Linux on slot a. They now pick the Linux slot the way the Magisk installer does: the slot Android is not on, from ro.boot.slot_suffix (_a gives b, _b gives a, anything else is refused). The Linux image is built from Android's own boot image with --linux-slot, written to boot_<Linux slot> and armed with that slot's trial block; Android's boot partition is never written. Right before writing, the slot suffix is read again and the image's manifest must name the Linux slot, so a device that switched slots in between, or an image built for the other slot, is refused with nothing written. boot_a and boot_b resolving to the same partition is refused too. Linux on slot a needs systems that follow the slot the initramfs names, so a release older than v2026.10.10 is refused for it. build-boot-image.py now puts the trial block of the image's own slot next to it (it always put the slot b one there), so nothing can arm Android's slot from that file. uninstall.sh and uninstall.ps1 had the same slot a check: they now restore the slot Android runs from in misc and copy Android's boot image over the Linux slot's, whichever that is. The "(slot a)" in the reboot-into-Android message is gone, and the README says boot_a is the partition written when Android runs from b. Tests: the slot block of install.sh runs against fake partitions for an Android on a, on b, an unknown suffix, a slot change before writing, an image for the wrong slot and a shared boot partition; uninstall's misc block is checked for both slots, and build-boot-image's trial block for both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DqKKtBQZCfxYnHuBCuDqqn
255 lines
12 KiB
Python
255 lines
12 KiB
Python
"""boot/build-boot-image.py: the generic ramdisk segment (what every release and mu300-update ship) holds init,
|
|
the modules of boot/module-order.txt and, for another device, its own modules, order and kernel release."""
|
|
import json
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zlib
|
|
from pathlib import Path
|
|
|
|
from helpers import TOP
|
|
|
|
try:
|
|
import lz4.block
|
|
except ImportError:
|
|
lz4 = None
|
|
HAVE_LZ4 = lz4 is not None or shutil.which('lz4') is not None
|
|
|
|
BOOT = TOP / 'boot'
|
|
RELEASE = '5.4.254-gtest'
|
|
|
|
|
|
def unlz4_legacy(data):
|
|
"""Concatenated LZ4 legacy frames (magic 0x184C2102, then blocks of <u32 size><data>) -> bytes."""
|
|
if lz4 is None:
|
|
return subprocess.run(['lz4', '-dc'], input=data, capture_output=True, check=True).stdout
|
|
out, i = b'', 0
|
|
while i < len(data):
|
|
assert data[i:i + 4] == b'\x02\x21\x4c\x18', 'not an LZ4 legacy frame'
|
|
i += 4
|
|
while i + 4 <= len(data) and data[i:i + 4] != b'\x02\x21\x4c\x18':
|
|
n = int.from_bytes(data[i:i + 4], 'little')
|
|
i += 4
|
|
out += lz4.block.decompress(data[i:i + n], uncompressed_size=8 << 20)
|
|
i += n
|
|
return out
|
|
|
|
|
|
def cpio_files(data):
|
|
"""newc cpio -> {name: (mode, data)}"""
|
|
files, i = {}, 0
|
|
while True:
|
|
h = data[i:i + 110]
|
|
assert h[:6] == b'070701', 'not newc'
|
|
mode = int(h[14:22], 16)
|
|
size = int(h[54:62], 16)
|
|
nlen = int(h[94:102], 16)
|
|
name = data[i + 110:i + 110 + nlen - 1].decode()
|
|
i = (i + 110 + nlen + 3) & ~3
|
|
body = data[i:i + size]
|
|
i = (i + size + 3) & ~3
|
|
if name == 'TRAILER!!!':
|
|
return files
|
|
files[name] = (mode, body)
|
|
|
|
|
|
class Fixtures(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = Path(tempfile.mkdtemp(prefix='mu300-bootimg-'))
|
|
self.mods = self.tmp / 'modules'
|
|
self.u30 = self.tmp / 'modules-u30air'
|
|
for d in (self.mods, self.u30):
|
|
d.mkdir()
|
|
base = (BOOT / 'module-order.txt').read_text().split()
|
|
u30 = (BOOT / 'module-order-u30air.txt').read_text().split()
|
|
for n in set(base) | set(u30):
|
|
(self.mods / n).write_bytes(b'\x7fELF base ' + n.encode() + b'\0vermagic=' + RELEASE.encode() + b' SMP\0')
|
|
# the U30 Air set: the modules it builds differently plus the ones the F50 order does not have
|
|
self.u30_only = sorted(set(u30) - set(base)) + ['sprd-charger-manager.ko']
|
|
for n in self.u30_only:
|
|
(self.u30 / n).write_bytes(b'\x7fELF u30 ' + n.encode() + b'\0vermagic=' + RELEASE.encode() + b' SMP\0')
|
|
for f in ('busybox', 'logdw'):
|
|
(self.tmp / f).write_bytes(b'\x7fELF ' + f.encode())
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.tmp, ignore_errors=True)
|
|
|
|
def build(self, *extra):
|
|
out = self.tmp / 'ramdisk-generic.lz4'
|
|
r = subprocess.run([sys.executable, str(BOOT / 'build-boot-image.py'), '--generic-ramdisk',
|
|
'--modules', str(self.mods), '--busybox', str(self.tmp / 'busybox'),
|
|
'--logdw', str(self.tmp / 'logdw'),
|
|
'--ueventd-perms', str(TOP / 'android-vendor' / 'ueventd-perms.sh'),
|
|
'--out', str(out), *extra], capture_output=True, text=True)
|
|
return r, out
|
|
|
|
|
|
@unittest.skipIf(not HAVE_LZ4, 'neither the lz4 command nor the lz4 Python module is installed')
|
|
class GenericRamdisk(Fixtures):
|
|
def test_base(self):
|
|
r, out = self.build()
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
files = cpio_files(unlz4_legacy(out.read_bytes()))
|
|
self.assertEqual(files['init'][1], (BOOT / 'init').read_bytes())
|
|
self.assertTrue(files['init'][0] & 0o111, 'init is not executable')
|
|
self.assertEqual(files['etc/module-order'][1], (BOOT / 'module-order.txt').read_bytes())
|
|
for n in (BOOT / 'module-order.txt').read_text().split():
|
|
self.assertIn('linux-modules/' + n, files)
|
|
self.assertNotIn('etc/mu300-device', files)
|
|
# an empty trial guard: disarms one an experiment left in the device segment that mu300-update keeps
|
|
self.assertEqual(files['etc/mu300-trial-guard'][1], b'')
|
|
self.assertFalse([f for f in files if f.startswith('linux-modules/u30air/')])
|
|
|
|
def test_device_modules(self):
|
|
r, out = self.build('--device-modules', f'u30air={self.u30}')
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
files = cpio_files(unlz4_legacy(out.read_bytes()))
|
|
self.assertEqual(files['etc/module-order-u30air'][1], (BOOT / 'module-order-u30air.txt').read_bytes())
|
|
for n in self.u30_only:
|
|
self.assertEqual(files['linux-modules/u30air/' + n][1], (self.u30 / n).read_bytes())
|
|
# the U30 Air's modules are used only with the kernel they were built for
|
|
self.assertEqual(files['linux-modules/u30air/kernel.release'][1].decode().strip(), RELEASE)
|
|
# every module the U30 Air order names is in the image, in its own set or the base one
|
|
for n in (BOOT / 'module-order-u30air.txt').read_text().split():
|
|
self.assertTrue('linux-modules/u30air/' + n in files or 'linux-modules/' + n in files, n)
|
|
|
|
def test_crlf_checkout(self):
|
|
# a Windows clone with core.autocrlf=true (issue #7): the device's shell must still get LF
|
|
crlf = {}
|
|
for name, src in (('init', BOOT / 'init'), ('perms', TOP / 'android-vendor' / 'ueventd-perms.sh'),
|
|
('order', BOOT / 'module-order.txt')):
|
|
crlf[name] = self.tmp / name
|
|
crlf[name].write_bytes(src.read_bytes().replace(b'\n', b'\r\n'))
|
|
r, out = self.build('--init', str(crlf['init']), '--ueventd-perms', str(crlf['perms']),
|
|
'--module-order', str(crlf['order']))
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
files = cpio_files(unlz4_legacy(out.read_bytes()))
|
|
self.assertEqual(files['init'][1], (BOOT / 'init').read_bytes())
|
|
self.assertEqual(files['etc/ueventd-perms.sh'][1], (TOP / 'android-vendor' / 'ueventd-perms.sh').read_bytes())
|
|
self.assertEqual(files['etc/module-order'][1], (BOOT / 'module-order.txt').read_bytes())
|
|
|
|
def test_generic_refuses_device_settings(self):
|
|
# the generic segment is the same for every device: it must not name one, nor carry a trial guard
|
|
for extra in (['--device', 'u30air'], ['--trial-guard', '300']):
|
|
r, _ = self.build(*extra)
|
|
self.assertNotEqual(r.returncode, 0, extra)
|
|
|
|
def test_missing_module(self):
|
|
(self.mods / (BOOT / 'module-order.txt').read_text().split()[0]).unlink()
|
|
r, _ = self.build()
|
|
self.assertNotEqual(r.returncode, 0)
|
|
self.assertIn('missing module', r.stderr)
|
|
|
|
|
|
def fake_stock(path, size=4 << 20, vbmeta_offset=1 << 20, vbmeta=b'V' * 2304):
|
|
"""An Android boot image header v4 with an AVB footer, as far as build-boot-image.py and mu300-update read it."""
|
|
img = bytearray(size)
|
|
img[0:8] = b'ANDROID!'
|
|
struct.pack_into('<I', img, 40, 4)
|
|
img[4096:8192] = b'k' * 4096
|
|
img[vbmeta_offset:vbmeta_offset + len(vbmeta)] = vbmeta
|
|
footer = bytearray(64)
|
|
footer[0:4] = b'AVBf'
|
|
struct.pack_into('>IIQQQ', footer, 4, 1, 0, vbmeta_offset, vbmeta_offset, len(vbmeta))
|
|
img[-64:] = footer
|
|
path.write_bytes(bytes(img))
|
|
|
|
|
|
# the slot-a block of the F50 test board: AOSP bootloader_control, nothing device-specific in it
|
|
LIVE_A = bytes.fromhex('5f61000042434142010200009f001e000000000000000000000000000be17146')
|
|
|
|
|
|
def fake_misc(path, live=LIVE_A):
|
|
path.write_bytes(bytes(0x800) + live + bytes(4096 - 0x800 - 32))
|
|
|
|
|
|
def with_slots(live, suffix, a, b):
|
|
x = bytearray(live)
|
|
x[0:4] = suffix
|
|
x[12] = a
|
|
x[14] = b
|
|
x[28:32] = struct.pack('<I', zlib.crc32(bytes(x[:28])))
|
|
return bytes(x)
|
|
|
|
|
|
@unittest.skipIf(not HAVE_LZ4, 'neither the lz4 command nor the lz4 Python module is installed')
|
|
class SlotBlocks(Fixtures):
|
|
def image(self, *extra):
|
|
fake_stock(self.tmp / 'stock.img')
|
|
fake_misc(self.tmp / 'misc.bin')
|
|
(self.tmp / 'Image').write_bytes(b'\x7fkernel' * 100)
|
|
out = self.tmp / 'boot.img'
|
|
r = subprocess.run([sys.executable, str(BOOT / 'build-boot-image.py'), '--stock-boot', str(self.tmp / 'stock.img'),
|
|
'--misc-head', str(self.tmp / 'misc.bin'), '--kernel', str(self.tmp / 'Image'),
|
|
'--modules', str(self.mods), '--busybox', str(self.tmp / 'busybox'),
|
|
'--logdw', str(self.tmp / 'logdw'), '--device', 'f50',
|
|
'--ueventd-perms', str(TOP / 'android-vendor' / 'ueventd-perms.sh'),
|
|
'--out', str(out), *extra], capture_output=True, text=True)
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
img = out.read_bytes()
|
|
ksz, rsz = struct.unpack_from('<II', img, 8)
|
|
roff = 4096 + (ksz + 4095) // 4096 * 4096
|
|
return cpio_files(unlz4_legacy(img[roff:roff + rsz]))
|
|
|
|
def test_all_four_blocks(self):
|
|
files = self.image()
|
|
want = {'etc/misc-bc-slot-a.bin': with_slots(LIVE_A, b'_a\0\0', 0x9f, 0x1e),
|
|
'etc/misc-bc-slot-b-trial.bin': with_slots(LIVE_A, b'_b\0\0', 0x9e, 0x2f),
|
|
'etc/misc-bc-slot-b.bin': with_slots(LIVE_A, b'_b\0\0', 0x1e, 0x9f),
|
|
'etc/misc-bc-slot-a-trial.bin': with_slots(LIVE_A, b'_a\0\0', 0x2f, 0x9e)}
|
|
for name, data in want.items():
|
|
self.assertEqual(files[name][1], data, name)
|
|
|
|
def test_slot_default_and_choice(self):
|
|
self.assertEqual(self.image()['etc/mu300-linux-slot'][1], b'b\n')
|
|
self.assertEqual(self.image('--linux-slot', 'a')['etc/mu300-linux-slot'][1], b'a\n')
|
|
|
|
def test_trial_block_next_to_the_image_is_the_one_of_its_slot(self):
|
|
# install.sh writes the block beside the image to misc: for Linux on a it must arm a, and no block that
|
|
# arms b (the slot Android runs from then) may lie next to it
|
|
for slot, other, block in (('b', 'a', with_slots(LIVE_A, b'_b\0\0', 0x9e, 0x2f)),
|
|
('a', 'b', with_slots(LIVE_A, b'_a\0\0', 0x2f, 0x9e))):
|
|
for f in self.tmp.glob('boot.misc-slot-*'):
|
|
f.unlink()
|
|
self.image('--linux-slot', slot)
|
|
self.assertEqual((self.tmp / f'boot.misc-slot-{slot}-trial.bin').read_bytes(), block, slot)
|
|
self.assertFalse((self.tmp / f'boot.misc-slot-{other}-trial.bin').exists(), slot)
|
|
self.assertEqual(json.loads((self.tmp / 'boot.json').read_text())['linux_slot'], slot)
|
|
|
|
def test_header_says_linux(self):
|
|
# loglevel=5 in the header's command line is how the Magisk switch tells our image from another Android
|
|
# (after an OTA the other slot holds the previous one); mu300-update keeps the header page as it is
|
|
for extra in ((), ('--linux-slot', 'a')):
|
|
self.image(*extra)
|
|
hdr = (self.tmp / 'boot.img').read_bytes()[:4096]
|
|
self.assertEqual(hdr[44:55], b'loglevel=5\0', extra)
|
|
|
|
def test_generic_has_no_slot(self):
|
|
r, _ = self.build('--linux-slot', 'a')
|
|
self.assertNotEqual(r.returncode, 0)
|
|
|
|
|
|
class ModuleOrder(unittest.TestCase):
|
|
def test_u30air_order(self):
|
|
base = (BOOT / 'module-order.txt').read_text().split()
|
|
u30 = (BOOT / 'module-order-u30air.txt').read_text().split()
|
|
self.assertEqual(len(u30), len(set(u30)), 'a module is listed twice')
|
|
# the F50's charger driver is replaced, not loaded next to the SQC one
|
|
self.assertNotIn('bq2560x-charger.ko', u30)
|
|
for n in ('sqc_bq2560x.ko', 'sqc_charger.ko', 'charger_policy_service.ko', 'zte_power_supply.ko'):
|
|
self.assertIn(n, u30)
|
|
# dependencies first: the fuel gauge needs the charger manager, which needs the SQC stack
|
|
for a, b in (('zte_power_supply.ko', 'charger_policy_service.ko'), ('sqc_charger.ko', 'sqc_bq2560x.ko'),
|
|
('sprd-charger-manager.ko', 'sc27xx_fuel_gauge.ko'), ('sprd_tcpm.ko', 'sc27xx_pd.ko')):
|
|
self.assertLess(u30.index(a), u30.index(b), f'{a} must load before {b}')
|
|
# everything else keeps the F50's order
|
|
common = [n for n in base if n in u30]
|
|
self.assertEqual(common, [n for n in u30 if n in common])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|