2021-02-05 09:30:43 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-06-10 10:40:48 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
import sys, pathlib
|
2021-07-01 18:21:36 +00:00
|
|
|
import serial
|
2021-06-10 10:40:48 +00:00
|
|
|
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
|
2021-01-23 13:35:55 +00:00
|
|
|
|
2021-02-05 13:47:40 +00:00
|
|
|
import argparse, pathlib
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='(Linux) kernel loader for m1n1')
|
2021-02-06 06:07:44 +00:00
|
|
|
parser.add_argument('payload', type=pathlib.Path)
|
|
|
|
parser.add_argument('dtb', type=pathlib.Path)
|
2021-02-05 13:47:40 +00:00
|
|
|
parser.add_argument('initramfs', nargs='?', type=pathlib.Path)
|
2021-02-05 14:39:09 +00:00
|
|
|
parser.add_argument('--compression', choices=['auto', 'none', 'gz', 'xz'], default='auto')
|
2021-02-05 15:23:25 +00:00
|
|
|
parser.add_argument('-b', '--bootargs', type=str, metavar='"boot arguments"')
|
2021-04-17 08:42:16 +00:00
|
|
|
parser.add_argument('-t', '--tty', type=str)
|
2021-04-17 09:10:37 +00:00
|
|
|
parser.add_argument('-u', '--u-boot', type=pathlib.Path, help="load u-boot before linux")
|
2021-02-05 13:47:40 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-06-10 10:40:48 +00:00
|
|
|
from m1n1.setup import *
|
2021-01-23 13:35:55 +00:00
|
|
|
|
2021-02-05 14:39:09 +00:00
|
|
|
if args.compression == 'auto':
|
2021-02-06 06:07:44 +00:00
|
|
|
suffix = args.payload.suffix
|
2021-02-05 14:39:09 +00:00
|
|
|
if suffix == '.gz':
|
|
|
|
args.compression = 'gz'
|
|
|
|
elif suffix == '.xz':
|
|
|
|
args.compression = 'xz'
|
|
|
|
else:
|
|
|
|
raise ValueError('unknown compression for {}'.format(args.payload))
|
|
|
|
|
2021-04-17 09:10:37 +00:00
|
|
|
if args.tty is not None:
|
|
|
|
tty_dev = serial.Serial(args.tty)
|
|
|
|
tty_dev.reset_input_buffer()
|
|
|
|
tty_dev.baudrate = 1500000
|
|
|
|
else:
|
2021-05-15 19:25:15 +00:00
|
|
|
tty_dev = None
|
2021-02-05 14:39:09 +00:00
|
|
|
|
2021-02-06 06:07:44 +00:00
|
|
|
payload = args.payload.read_bytes()
|
|
|
|
dtb = args.dtb.read_bytes()
|
2021-02-05 13:47:40 +00:00
|
|
|
if args.initramfs is not None:
|
2021-02-06 06:07:44 +00:00
|
|
|
initramfs = args.initramfs.read_bytes()
|
2021-01-23 13:35:55 +00:00
|
|
|
initramfs_size = len(initramfs)
|
|
|
|
else:
|
|
|
|
initramfs = None
|
|
|
|
initramfs_size = 0
|
|
|
|
|
2021-02-05 15:23:25 +00:00
|
|
|
if args.bootargs is not None:
|
|
|
|
print('Setting boot args: "{}"'.format(args.bootargs))
|
2021-04-15 14:14:41 +00:00
|
|
|
p.kboot_set_bootargs(args.bootargs)
|
2021-02-05 15:23:25 +00:00
|
|
|
|
2021-02-05 14:39:09 +00:00
|
|
|
if args.compression != 'none':
|
|
|
|
compressed_size = len(payload)
|
|
|
|
compressed_addr = u.malloc(compressed_size)
|
2021-01-23 13:35:55 +00:00
|
|
|
|
2021-02-05 14:39:09 +00:00
|
|
|
print("Loading %d bytes to 0x%x..0x%x..." % (compressed_size, compressed_addr, compressed_addr + compressed_size))
|
|
|
|
iface.writemem(compressed_addr, payload, True)
|
2021-01-23 13:35:55 +00:00
|
|
|
|
2021-02-05 14:39:09 +00:00
|
|
|
dtb_addr = u.malloc(len(dtb))
|
2021-01-23 13:35:55 +00:00
|
|
|
print("Loading DTB to 0x%x..." % dtb_addr)
|
|
|
|
|
|
|
|
iface.writemem(dtb_addr, dtb)
|
|
|
|
|
2021-03-07 16:33:43 +00:00
|
|
|
kernel_size = 512 * 1024 * 1024
|
2021-01-29 16:36:45 +00:00
|
|
|
kernel_base = u.memalign(2 * 1024 * 1024, kernel_size)
|
2021-04-17 09:10:37 +00:00
|
|
|
boot_addr = kernel_base
|
2021-01-29 16:36:45 +00:00
|
|
|
|
|
|
|
print("Kernel_base: 0x%x" % kernel_base)
|
|
|
|
|
|
|
|
assert not (kernel_base & 0xffff)
|
|
|
|
|
2021-01-23 13:35:55 +00:00
|
|
|
if initramfs is not None:
|
2021-01-29 16:36:45 +00:00
|
|
|
initramfs_base = u.memalign(65536, initramfs_size)
|
2021-01-23 13:35:55 +00:00
|
|
|
print("Loading %d initramfs bytes to 0x%x..." % (initramfs_size, initramfs_base))
|
|
|
|
iface.writemem(initramfs_base, initramfs, True)
|
2021-01-29 16:36:45 +00:00
|
|
|
p.kboot_set_initrd(initramfs_base, initramfs_size)
|
|
|
|
|
2021-04-17 09:10:37 +00:00
|
|
|
|
|
|
|
if args.u_boot:
|
|
|
|
uboot = bytearray(args.u_boot.read_bytes())
|
|
|
|
uboot_size = len(uboot)
|
|
|
|
uboot_addr = u.memalign(2*1024*1024, len(uboot))
|
|
|
|
print("Loading u-boot to 0x%x..." % uboot_addr)
|
|
|
|
|
|
|
|
bootenv_start = uboot.find(b"bootcmd=run distro_bootcmd")
|
|
|
|
bootenv_len = uboot[bootenv_start:].find(b"\x00\x00")
|
|
|
|
bootenv_old = uboot[bootenv_start:bootenv_start+bootenv_len]
|
|
|
|
bootenv = str(bootenv_old, "ascii").split("\x00")
|
|
|
|
bootenv = list(filter(lambda x: not (x.startswith("baudrate") or x.startswith("boot_") or x.startswith("distro_bootcmd")), bootenv))
|
|
|
|
|
|
|
|
if initramfs is not None:
|
|
|
|
bootcmd = "distro_bootcmd=booti 0x%x 0x%x:0x%x 0x%x" % (kernel_base, initramfs_base, initramfs_size, dtb_addr)
|
|
|
|
else:
|
|
|
|
bootcmd = "distro_bootcmd=booti 0x%x - 0x%x" % (kernel_base, dtb_addr)
|
|
|
|
|
|
|
|
bootenv.append("baudrate=%d" % tty_dev.baudrate)
|
|
|
|
bootenv.append(bootcmd)
|
|
|
|
if args.bootargs is not None:
|
|
|
|
bootenv.append("bootargs=" + args.bootargs)
|
|
|
|
|
|
|
|
bootenv_new = b"\x00".join(map(lambda x: bytes(x, "ascii"), bootenv))
|
|
|
|
bootenv_new = bootenv_new.ljust(len(bootenv_old), b"\x00")
|
|
|
|
|
|
|
|
if len(bootenv_new) > len(bootenv_old):
|
|
|
|
raise Exception("New bootenv cannot be larger than original bootenv")
|
|
|
|
uboot[bootenv_start:bootenv_start+bootenv_len] = bootenv_new
|
|
|
|
|
|
|
|
u.compressed_writemem(uboot_addr, uboot, True)
|
|
|
|
p.dc_cvau(uboot_addr, uboot_size)
|
|
|
|
p.ic_ivau(uboot_addr, uboot_size)
|
|
|
|
|
|
|
|
boot_addr = uboot_addr
|
|
|
|
|
2021-02-04 16:09:27 +00:00
|
|
|
p.smp_start_secondaries()
|
|
|
|
|
2021-01-29 16:36:45 +00:00
|
|
|
if p.kboot_prepare_dt(dtb_addr):
|
|
|
|
print("DT prepare failed")
|
|
|
|
sys.exit(1)
|
2021-01-23 13:35:55 +00:00
|
|
|
|
|
|
|
iface.dev.timeout = 40
|
|
|
|
|
2021-02-05 14:39:09 +00:00
|
|
|
if args.compression == 'none':
|
|
|
|
kernel_size = len(payload)
|
|
|
|
print("Loading %d bytes to 0x%x..0x%x..." % (kernel_size, kernel_base, kernel_base + kernel_size))
|
|
|
|
iface.writemem(kernel_base, payload, True)
|
|
|
|
elif args.compression == 'gz':
|
|
|
|
print("Uncompressing gz ...")
|
|
|
|
kernel_size = p.gzdec(compressed_addr, compressed_size, kernel_base, kernel_size)
|
|
|
|
elif args.compression == 'xz':
|
|
|
|
print("Uncompressing xz ...")
|
|
|
|
kernel_size = p.xzdec(compressed_addr, compressed_size, kernel_base, kernel_size)
|
|
|
|
else:
|
|
|
|
raise ValueError('unsupported compression {}'.format(args.compression))
|
|
|
|
|
2021-01-23 13:35:55 +00:00
|
|
|
print(kernel_size)
|
|
|
|
|
|
|
|
if kernel_size < 0:
|
|
|
|
raise Exception("Decompression error!")
|
|
|
|
|
|
|
|
print("Decompress OK...")
|
|
|
|
|
|
|
|
p.dc_cvau(kernel_base, kernel_size)
|
|
|
|
p.ic_ivau(kernel_base, kernel_size)
|
|
|
|
|
|
|
|
print("Ready to boot")
|
|
|
|
|
|
|
|
daif = u.mrs(DAIF)
|
2021-02-21 02:55:11 +00:00
|
|
|
daif = 0xc0
|
2021-01-23 13:35:55 +00:00
|
|
|
u.msr(DAIF, daif)
|
|
|
|
print("DAIF: %x" % daif)
|
|
|
|
|
2021-04-17 09:10:37 +00:00
|
|
|
p.kboot_boot(boot_addr)
|
2021-04-17 08:42:16 +00:00
|
|
|
|
|
|
|
iface.ttymode(tty_dev)
|