Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69963 > unrolled thread

Unpacking U-Boot image file

Started by"trewio" <laguna-mc@mail.com>
First post2014-04-09 12:06 -0400
Last post2014-04-11 16:02 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Unpacking U-Boot image file "trewio" <laguna-mc@mail.com> - 2014-04-09 12:06 -0400
    Re: Unpacking U-Boot image file Rustom Mody <rustompmody@gmail.com> - 2014-04-09 10:18 -0700
      Re: Unpacking U-Boot image file Adnan Sadzak <sadzak@gmail.com> - 2014-04-09 19:33 +0200
      Re: Unpacking U-Boot image file "Rhodri James" <rhodri@wildebst.org.uk> - 2014-04-10 23:01 +0100
        Re: Unpacking U-Boot image file Rustom Mody <rustompmody@gmail.com> - 2014-04-10 20:27 -0700
        Re: Unpacking U-Boot image file Grant Edwards <invalid@invalid.invalid> - 2014-04-11 16:02 +0000

#69963 — Unpacking U-Boot image file

From"trewio" <laguna-mc@mail.com>
Date2014-04-09 12:06 -0400
SubjectUnpacking U-Boot image file
Message-ID<mailman.9083.1397060494.18130.python-list@python.org>
How to extract files from U-Boot image file, LZMA-compressed?

Is there a Python script that can do this properly?

[toc] | [next] | [standalone]


#69967

FromRustom Mody <rustompmody@gmail.com>
Date2014-04-09 10:18 -0700
Message-ID<6ed343e7-fa02-426a-a75f-3e1d164a6a61@googlegroups.com>
In reply to#69963
On Wednesday, April 9, 2014 9:36:40 PM UTC+5:30, trewio wrote:
> How to extract files from U-Boot image file, LZMA-compressed?
> 
> Is there a Python script that can do this properly?

For lzma theres this (recent) python library
https://docs.python.org/dev/library/lzma.html

Though you might just be better off with the command-line xz unxz etc

After that.. whats the U-boot format?

[toc] | [prev] | [next] | [standalone]


#69969

FromAdnan Sadzak <sadzak@gmail.com>
Date2014-04-09 19:33 +0200
Message-ID<mailman.9086.1397064809.18130.python-list@python.org>
In reply to#69967

[Multipart message — attachments visible in raw view] — view raw

I belive you are trying to decompress some router images or router
backup/config files?
Check if maybe LZS.

Rustom gave You starting point.



On Wed, Apr 9, 2014 at 7:18 PM, Rustom Mody <rustompmody@gmail.com> wrote:

> On Wednesday, April 9, 2014 9:36:40 PM UTC+5:30, trewio wrote:
> > How to extract files from U-Boot image file, LZMA-compressed?
> >
> > Is there a Python script that can do this properly?
>
> For lzma theres this (recent) python library
> https://docs.python.org/dev/library/lzma.html
>
> Though you might just be better off with the command-line xz unxz etc
>
> After that.. whats the U-boot format?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#70065

From"Rhodri James" <rhodri@wildebst.org.uk>
Date2014-04-10 23:01 +0100
Message-ID<op.xd4fhcfm5079vu@gnudebeest>
In reply to#69967
On Wed, 09 Apr 2014 18:18:56 +0100, Rustom Mody <rustompmody@gmail.com>  
wrote:

> On Wednesday, April 9, 2014 9:36:40 PM UTC+5:30, trewio wrote:
>> How to extract files from U-Boot image file, LZMA-compressed?
>>
>> Is there a Python script that can do this properly?
>
> For lzma theres this (recent) python library
> https://docs.python.org/dev/library/lzma.html
>
> Though you might just be better off with the command-line xz unxz etc
>
> After that.. whats the U-boot format?

The Fine Manual (http://www.denx.de/wiki/view/DULG/UBootImages) isn't very  
forthcoming, sadly;

"U-Boot operates on "image" files which can be basically anything,  
preceeded by a special header; see the definitions in include/image.h for  
details; basically, the header defines the following image properties

* Target Operating System
* Target CPU Architecture
* Compression Type
* Load Address
* Entry Point
* Image Name
* Image Timestamp"

I suspect taking this apart may be a little involved.

-- 
Rhodri James *-* Wildebeest Herder to the Masses

[toc] | [prev] | [next] | [standalone]


#70087

FromRustom Mody <rustompmody@gmail.com>
Date2014-04-10 20:27 -0700
Message-ID<f8acc98d-f330-4c49-be1e-9b79a5e91af2@googlegroups.com>
In reply to#70065
On Friday, April 11, 2014 3:31:50 AM UTC+5:30, Rhodri James wrote:
> On Wed, 09 Apr 2014 18:18:56 +0100, Rustom Mody  
> > After that.. whats the U-boot format?
> 
> The Fine Manual (http://www.denx.de/wiki/view/DULG/UBootImages) isn't very  
> forthcoming, sadly;
> 
> "U-Boot operates on "image" files which can be basically anything,  
> preceeded by a special header; see the definitions in include/image.h for  
> details; basically, the header defines the following image properties
> 
> * Target Operating System
> * Target CPU Architecture
> * Compression Type
> * Load Address
> * Entry Point
> * Image Name
> * Image Timestamp"
> 
> 
> I suspect taking this apart may be a little involved.

If its a binary format, you (OP) may want to look at 
https://pypi.python.org/pypi/construct/2.5.1

Of course you will have to read/get the C struct and write the corresponding
"Construct"

You may want to ask on a U-boot forum for more details.

[toc] | [prev] | [next] | [standalone]


#70153

FromGrant Edwards <invalid@invalid.invalid>
Date2014-04-11 16:02 +0000
Message-ID<li93mr$4du$1@reader1.panix.com>
In reply to#70065
On 2014-04-10, Rhodri James <rhodri@wildebst.org.uk> wrote:
> On Wed, 09 Apr 2014 18:18:56 +0100, Rustom Mody <rustompmody@gmail.com> wrote:
>> On Wednesday, April 9, 2014 9:36:40 PM UTC+5:30, trewio wrote:
>>
>>> How to extract files from U-Boot image file, LZMA-compressed?
>>>
>>> Is there a Python script that can do this properly?
>>
>> For lzma theres this (recent) python library
>> https://docs.python.org/dev/library/lzma.html
>>
>> Though you might just be better off with the command-line xz unxz etc
>>
>> After that.. whats the U-boot format?
>
> The Fine Manual (http://www.denx.de/wiki/view/DULG/UBootImages) isn't very  
> forthcoming, sadly;
>
> "U-Boot operates on "image" files which can be basically anything,  
> preceeded by a special header; see the definitions in include/image.h for  
> details; basically, the header defines the following image properties
>
> * Target Operating System
> * Target CPU Architecture
> * Compression Type
> * Load Address
> * Entry Point
> * Image Name
> * Image Timestamp"
>
> I suspect taking this apart may be a little involved.

Not really.  For the format you're talking about (single-file uImage),
you can extract the data like so:

  data = open("foobar.uImage","rb").read()[64:]

Then just uncompress 'data' as desired.

If you want to look at individual fields in the heaer, here's the
structure:   

#define IH_MAGIC    0x27051956    /* Image Magic Number     */
#define IH_NMLEN    32            /* Image Name Length      */

typedef struct image_header 
{
  uint32_t        ih_magic;       /* Image Header Magic Number    */
  uint32_t        ih_hcrc;        /* Image Header CRC Checksum    */
  uint32_t        ih_time;        /* Image Creation Timestamp     */
  uint32_t        ih_size;        /* Image Data Size              */
  uint32_t        ih_load;        /* Data  Load  Address          */
  uint32_t        ih_ep;          /* Entry Point Address          */
  uint32_t        ih_dcrc;        /* Image Data CRC Checksum      */
  uint8_t         ih_os;          /* Operating System             */
  uint8_t         ih_arch;        /* CPU architecture             */
  uint8_t         ih_type;        /* Image Type                   */
  uint8_t         ih_comp;        /* Compression Type             */
  uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
} image_header_t;

Header fields are all in network byte order (bit-endian).

However, there's also a "multi-file" uImage format that I suspect is
what the OP is referring to (since he says he needs to extract "files"
from a uImage file).

http://www.isysop.com/unpacking-and-repacking-u-boot-uimage-files/

In that case, the "Image Type" field will be set to 4, and the 'data'
field starts with a list of 32-bit image size values (network byte
order, terminated by 0x00000000).  That list is then followed by the
image data.

Here's a Python 2.7 program that reads the uImage header, prints some
stuff and then reads the data for the image(s).  What to _do_ with the
data for the image(s) is left as an exercise for the gentle reader.

-------------------------------------8<-------------------------------------
#!/usr/bin/python

import sys,struct

f = open(sys.argv[1],"rb")

(ih_magic, ih_hcrc, ih_time, ih_size, ih_load, ih_ep, ih_dcrc, 
 ih_os, ih_arch, ih_type, ih_comp, ih_name) \
    = struct.unpack("!IIIIIIIBBBB32s",f.read(64))

print "magic=%08x type=%d size=%d name='%s'" % (ih_magic, ih_type, ih_size, ih_name)

if ih_type == 4:
    # multi-file: read image lengths from data
    imagesizes = []
    while 1:
        size, = struct.unpack("!I",f.read(4))
        if size == 0:
            break
        imagesizes.append(size)
else:
    # single-file
    imagesizes = [ih_size]

print imagesizes

images = [f.read(size) for size in imagesizes]

print [len(image) for image in images]

extradata = f.read() 

if extradata:
    print "%d bytes of extra data" % len(extradata)

-------------------------------------8<-------------------------------------










-- 
Grant Edwards               grant.b.edwards        Yow! The Korean War must
                                  at               have been fun.
                              gmail.com            

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web