Advertisement
buonaseva_fatelo

unpackisf.py

May 7th, 2024
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import sys
  2.  
  3. from construct import *
  4. from subprocess import PIPE, Popen
  5.  
  6. # http://www.qnx.com/developers/docs/660/index.jsp?topic=%2Fcom.qnx.doc.neutrino.building%2Ftopic%2Fload_process_Boot_header.html
  7.  
  8. boot_header = Struct("boot_header",
  9.   Enum(ULInt32("signature"),
  10.         CORRECT = 0x00FF7EEB,
  11.         _default_ = Pass,
  12.     ),
  13.   ULInt16("version"),
  14.   BitStruct("flags1",
  15.         Flag("STARTUP_HDR_FLAGS1_VIRTUAL"),
  16.         Flag("STARTUP_HDR_FLAGS1_BIGENDIAN"),
  17.         Enum(BitField("Compression", 4),
  18.             STARTUP_HDR_FLAGS1_COMPRESS_NONE = 0,
  19.             STARTUP_HDR_FLAGS1_COMPRESS_ZLIB = 0x01,
  20.             STARTUP_HDR_FLAGS1_COMPRESS_LZO  = 0x02,
  21.             STARTUP_HDR_FLAGS1_COMPRESS_UCL  = 0x03,
  22.           _default_ = Pass
  23.         ),
  24.         Padding(2),
  25.   ),
  26.   ULInt8("flags2_unused"),
  27.   ULInt16("header_size"),
  28.   Enum(ULInt16("machine"),
  29.         EM_NONE = 0,
  30.         EM_M32 = 1,                 # /* AT&T WE 32100 */
  31.         EM_SPARC = 2,               # /* Sun SPARC */
  32.         EM_386 = 3,                 # /* Intel 80386 */
  33.         EM_68k = 4,                 # /* Motorola 68000 */
  34.         EM_88k = 5,                 # /* Motorola 88000 */
  35.         EM_486 = 6,                 # /* Intel 80486 */
  36.         EM_860 = 7,                 # /* Intel i860 */
  37.         EM_MIPS = 8,                # /* MIPS RS3000 Big-Endian */
  38.         EM_S370 = 9,                # /* IBM system 370 */
  39.         EM_MIPS_RS3_LE = 10, # /* MIPS RS3000 Little-Endian */
  40.         EM_RS6000 = 11,              # /* RS6000 */
  41.         EM_UNKNOWN12 = 12,
  42.         EM_UNKNOWN13 = 13,
  43.         EM_UNKNOWN14 = 14,
  44.         EM_PA_RISC = 15,             # /* PA_RISC */
  45.         EM_nCUBE = 16,               # /* nCUBE */
  46.         EM_VPP500 = 17,              # /* Fujitsu VPP500 */
  47.         EM_SPARC32PLUS = 18, # /* Sun Sparc 32+ */
  48.         EM_UNKNOWN19 = 19,
  49.         EM_PPC = 20,                 # /* Power PC */
  50.         EM_ARM = 40,    # /* ARM */
  51.         EM_SH = 42,             # /* Hitachi SH */
  52.         _default_ = Pass
  53.     ),
  54.   ULInt32("startup_vaddr"),
  55.   ULInt32("paddr_bias"),
  56.   ULInt32("image_paddr"),
  57.   ULInt32("ram_paddr"),
  58.   ULInt32("ram_size"),
  59.   ULInt32("startup_size"),
  60.   ULInt32("stored_size"),
  61.   ULInt32("imagefs_paddr"),
  62.   ULInt32("imagefs_size"),
  63.   ULInt16("preboot_size"),
  64.   ULInt16("zero0"),
  65.   Array(3, ULInt32("zero3"))
  66. )
  67.  
  68. input = open(sys.argv[1], "rb")
  69. data = input.read()
  70. input.close()
  71.  
  72. header = boot_header.parse(data)
  73. print header
  74. if header.stored_size == len(data):
  75.     print "stored_size OK"
  76. else:
  77.     print "stored_size NOK"
  78.  
  79. stored_imagefs = header.stored_size - header.startup_size
  80. if stored_imagefs == header.imagefs_size:
  81.     print "imagefs_size OK, uncompressed"
  82. elif stored_imagefs < header.imagefs_size:
  83.     print "imagefs_size OK, compressed %d -> %d" % (header.imagefs_size, stored_imagefs)
  84. else:
  85.     print "imagefs_size NOK: imagefs %d, stored %d" % (header.imagefs_size, stored_imagefs)
  86.  
  87. startup = open(sys.argv[1] + ".startup", "wb")
  88. startup.write(data[:header.startup_size])
  89. print "Wrote " + str(startup)
  90. startup.close()
  91.  
  92. imagefs = open(sys.argv[1] + ".imagefs", "wb")
  93. imagefs_data = data[header.startup_size:]
  94. imagefs.write(imagefs_data)
  95. print "Wrote " + str(imagefs)
  96. imagefs.close()
  97.  
  98. imagefs_struct = GreedyRange(PascalString("imagefs", length_field = UBInt16("length")))
  99. imagefs_parsed = imagefs_struct.parse(imagefs_data)
  100. print "Found %d LZO blocks" % len(imagefs_parsed)
  101.  
  102. imagefs = open(sys.argv[1] + ".imagefs.decompressed", "wb")
  103. for i in range(len(imagefs_parsed)):
  104.     sys.stdout.write("\rDecompress: %d/%d " % (i, len(imagefs_parsed)))
  105.     if len(imagefs_parsed[i]) == 0:
  106.         continue
  107.     lzod = Popen("./lzod", stdin=PIPE, stdout=imagefs, stderr=PIPE)
  108.     lzod_result = lzod.communicate(imagefs_parsed[i])
  109.     if (lzod.returncode != 0):
  110.         print "Failed: %d" % lzod.returncode
  111.         print lzod_result
  112.         raise Exception
  113.  
  114. print "\nWrote " + str(imagefs)
  115. imagefs.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement