[kernel] r17223 - people/benh

Ben Hutchings benh at alioth.debian.org
Fri Apr 15 01:53:16 UTC 2011


Author: benh
Date: Fri Apr 15 01:53:14 2011
New Revision: 17223

Log:
Add script for decompressing vmlinuz

Added:
   people/benh/zcat-linux-image   (contents, props changed)

Added: people/benh/zcat-linux-image
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ people/benh/zcat-linux-image	Fri Apr 15 01:53:14 2011	(r17223)
@@ -0,0 +1,74 @@
+#!/usr/bin/python
+
+# Copyright 2010 Ben Hutchings
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import os, struct, zlib
+
+def zcat_vmlinuz(f):
+    f.seek(0, 0)
+    head = f.read(65536)
+
+    try:
+        # Look for gzip-deflate header
+        off = head.index('\x1f\x8b\x08')
+    except ValueError:
+        pass
+    else:
+        return gzcat(f, head, off)
+
+    # Look for likely lzma header
+    off = head.index('\x00\x00\x00\x02\xff\xff\xff\xff\xff\xff\xff\xff') - 1
+    return lzcat(f, head, off)
+
+def gzcat(f, head, off):
+    # We can't use gzip.GzipFile because that complains if there is
+    # trailing data, which is the case for ELFBoot kernel images.
+    # Use zlib directly.
+    zo = zlib.decompressobj(-15)
+
+    flags = ord(head[off + 3])
+    off += 10			# fixed header
+    if flags & 0x04:		# FEXTRA
+        off += 2 + struct.unpack('<H', head[off:off+2])[0]
+    if flags & 0x08:		# FNAME
+        off = head.index('\0', off) + 1
+    if flags & 0x10:		# FCOMMENT
+        off = head.index('\0', off) + 1
+    if flags & 0x02:		# FHCRC
+        off += 2
+    assert not (flags & 0xe0)	# reserved
+
+    # Decompress following deflate blocks
+    f.seek(off)
+    image = zo.decompress(f.read()) + zo.flush()
+
+    # Verify decompressed data against gzip trailer
+    assert (struct.unpack('<LL', zo.unused_data[:8]) ==
+            (zlib.crc32(image) & 0xffffffffL, len(image)))
+
+    sys.stdout.write(image)
+    sys.stdout.flush()
+
+def lzcat(f, head, off):
+    os.dup2(f.fileno(), 0)
+    f.close()
+    os.lseek(0, off, os.SEEK_SET)
+    os.execvp('lzcat', ['lzcat', '-t'])
+
+if __name__ == '__main__':
+    import sys
+    zcat_vmlinuz(open(sys.argv[1], 'rb'))



More information about the Kernel-svn-changes mailing list