Skip to content

Instantly share code, notes, and snippets.

@mbikovitsky
Created August 17, 2022 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbikovitsky/2f76bacb3f31c8a730c7f54adf43c6a5 to your computer and use it in GitHub Desktop.
Save mbikovitsky/2f76bacb3f31c8a730c7f54adf43c6a5 to your computer and use it in GitHub Desktop.
Script for patching a Raspberry Pi bootloader to support a larger kernel
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# MIT License
#
# Copyright (c) 2022 Michael Bikovitsky
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import re
import sys
def _main():
args = _parse_command_line()
# https://source.denx.de/u-boot/u-boot/-/blob/157861e6afa5c26134639e8a8f34540141672a0a/include/configs/rpi.h#L116
DEFAULT_ENV = (
b"kernel_addr_r=0x00080000\0"
+ b"scriptaddr=0x02400000\0"
+ b"pxefile_addr_r=0x02500000\0"
+ b"fdt_addr_r=0x02600000\0"
+ b"ramdisk_addr_r=0x02700000\0"
)
kernel_addr_r = 0x00080000
scriptaddr = kernel_addr_r + args.size
pxefile_addr_r = scriptaddr + 0x100000
fdt_addr_r = pxefile_addr_r + 0x100000
ramdisk_addr_r = fdt_addr_r + 0x100000
new_env = (
f"kernel_addr_r={kernel_addr_r:#010x}\0"
+ f"scriptaddr={scriptaddr:#010x}\0"
+ f"pxefile_addr_r={pxefile_addr_r:#010x}\0"
+ f"fdt_addr_r={fdt_addr_r:#010x}\0"
+ f"ramdisk_addr_r={ramdisk_addr_r:#010x}\0"
).encode("ASCII")
if len(new_env) != len(DEFAULT_ENV):
raise ValueError("Specified size is too big")
with open(args.input, mode="rb") as f:
bootloader = f.read()
if len(re.findall(DEFAULT_ENV, bootloader)) != 1:
raise ValueError("Unsupported bootloader memory configuration")
new_bootloader = re.sub(DEFAULT_ENV, new_env, bootloader)
with open(args.output, mode="wb") as f:
f.write(new_bootloader)
def _parse_command_line() -> argparse.Namespace:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
def int_any_base(string: str):
return int(string, base=0)
parser.add_argument("-i", "--input", required=True, help="Input filename")
parser.add_argument("-o", "--output", required=True, help="Output filename")
parser.add_argument(
"-s",
"--size",
type=int_any_base,
default=66584576,
help="Required memory for kernel decompression, in bytes",
)
return parser.parse_args()
if __name__ == "__main__":
sys.exit(_main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment