Last active
January 3, 2016 05:19
-
-
Save Gnurou/8414813 to your computer and use it in GitHub Desktop.
Simple script that boots a kernel using tegrarcm by creating an image containing U-boot and the kernel concatenated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
# Simple script that boots a kernel using tegrarcm by creating an image | |
# containing U-boot and the kernel concatenated. | |
# | |
# Usage: boot.py board.bct zImage tegraxx-board.dtb | |
# | |
# The files listed below must be present in the working directory and | |
# can be obtained from U-boot. | |
uboot_img = "u-boot-nodtb-tegra.bin" | |
uboot_dtb = "u-boot.dtb" | |
import sys | |
import os | |
outfile = "u-boot-linux.img" | |
loadaddr = 0x80108000 | |
# Adapt these to suit your need, e.g. for mounting a root FS on a SD card | |
kernel_args = 'cma=384M console=ttyS0,115200n8 console=tty1 rootwait rw' | |
bootcmd = 'setenv bootargs ' + kernel_args + ';' | |
bct = sys.argv[1] | |
zImage = sys.argv[2] | |
zImage_dtb = sys.argv[3] | |
if len(sys.argv) > 4: | |
ramfs = sys.argv[4] | |
else: | |
ramfs = None | |
class Padder: | |
def __init__(self, out): | |
self.out = out | |
self.pos = 0 | |
def cat(self, f): | |
fin = open(f, 'rb') | |
content = fin.read() | |
self.pos += len(content) | |
self.out.write(content) | |
fin.close() | |
def pad(self, to): | |
if to < self.pos: | |
raise Exception("output file position too high") | |
pad = to - self.pos | |
self.out.write((' ' * pad).encode('ascii')) | |
self.pos = to | |
def align(self, mask): | |
to = (self.pos + (mask - 1)) & ~(mask - 1) | |
self.pad(to) | |
outf = open(outfile, 'wb') | |
padder = Padder(outf) | |
padder.cat(uboot_img) | |
# Compute the future offset of the kernel and its DTB. We reserve 128KB for U-boot's DTB and heap. It may need to be increased further in the future. | |
#kernoffset = (padder.pos + 0x100000 + (0x10000 - 1)) & ~(0x10000 - 1) | |
#dtboffset = kernoffset + os.stat(zImage).st_size | |
# Above doesn't work anymore. Just set large enough addresses so the kernel won't screw itself while uncompressing. | |
kernoffset = 0x1000000 | |
dtboffset = (kernoffset + os.stat(zImage).st_size + (0x10000 - 1)) & ~(0x10000 - 1) | |
if ramfs: | |
ramfsoffset = (dtboffset + 0x10000 + (0x1000 - 1)) & ~(0x1000 - 1) | |
ramfsaddr = hex(loadaddr + ramfsoffset) | |
else: | |
ramfsaddr = '-' | |
bootcmd += ' bootz ' + hex(loadaddr + kernoffset) + ' ' + ramfsaddr + ' ' + hex(loadaddr + dtboffset) | |
os.system('fdtput -p -t s ' + uboot_dtb + ' /config bootcmd "' + bootcmd + '"') | |
print(bootcmd) | |
padder.cat(uboot_dtb) | |
padder.pad(kernoffset) | |
padder.cat(zImage) | |
padder.pad(dtboffset) | |
padder.cat(zImage_dtb) | |
if ramfs: | |
padder.pad(ramfsoffset) | |
padder.cat(ramfs) | |
outf.close() | |
os.system('tegrarcm --bct ' + bct + ' --bootloader=' + outfile + ' --loadaddr=' + hex(loadaddr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment