Skip to content

Instantly share code, notes, and snippets.

@pawitp
Created May 20, 2014 10:54
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 pawitp/bc896a9c005900af1c7c to your computer and use it in GitHub Desktop.
Save pawitp/bc896a9c005900af1c7c to your computer and use it in GitHub Desktop.
Script to extract cpio ramdisk from LZMA compressed kernels
#!/usr/bin/env python
# Copyright (C) 2014 Pawit Pornkitprasan
#
# Script to extract cpio ramdisk from LZMA compressed kernels
# Tested on galaxysmtd boot.img from CM11
# Config
fileIn = "/path/to/boot.img"
fileOut = "whole_disk.cpio"
# Code
from subprocess import Popen, PIPE, STDOUT
import struct
def align(x, length):
return ((x + length - 1) / length) * length
print "Reading zImage..."
zImage = open(fileIn, "rb").read()
lzmaStart = zImage.find('\x5D\0\0\0')
p = Popen(['unlzma'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
piggy = p.communicate(zImage[lzmaStart:])[0]
# Retroactively find end from size
lzmaEnd = zImage.find(struct.pack('<I', len(piggy)))
cpioStart = piggy.find("0707")
cpioEnd = piggy.rfind("00TRAILER!!!\0") + len("00TRAILER!!!\0")
# Align
cpioEnd = align(cpioEnd, 4)
out = open(fileOut, 'w')
out.write(piggy[cpioStart:cpioEnd])
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment