[script] repack-zImage.sh: Unpack and repack a zImage without kernel source, V. 5

Kyuta Syuko

Senior Member
Aug 18, 2009
1,867
660
0
After decompressing the ramdisk I get a bunch of errors that look sort of like this:

Code:
Unpacking initramfs
9923 blocks
cpio: misc: Cannot change mode to rwsrwsrwx: Operation not permitted
cpio: busybox: Cannot symlink to `sbin/chgrp': Operation not permitted
cpio: busybox: Cannot symlink to `sbin/uuencode': Operation not permitted
cpio: busybox: Cannot symlink to `sbin/printenv': Operation not permitted
cpio: busybox: Cannot symlink to `sbin/df': Operation not permitted
cpio: ../init: Cannot symlink to `sbin/ueventd': Operation not permitted
And I was wondering if that's normal or if something's wrong.
 

whiskerp

Senior Member
Mar 22, 2012
273
127
0
repack-zImage.sh is a bash script for Linux which allows you to unpack a kernel image (zImage) for modification and repack it afterwards into a new working kernel image.

You don't need a kernel tree for this program nor a compiler. It should work with any zImage that contains an initramfs, for whatever phone, operating system or CPU architecture you like.

My main purpose when I wrote it was to modify the initramfs of leaked Samsung i5800 firmware for which no kernel source is available.

Usage:
=====

Put the unzipped script into some directory along your $PATH (e.g., /usr/local/bin). Put the unpacked files from initramfs_utils.zip into /usr/local/bin.

Then simply run 'repack-zImage.sh -u' with your zImage in the current directory and it will create a directory named 'zImage_unpacked' which contains the unpacked blocks of your zImage. Refer to the comments near the start of the program to identify which file corresponds to which fragment of the original zImage. (The file name of the zImage should be "zImage". If it isn't, pass it as the only non-option argument. The subdirectory's name will change accordingly.)

Most notably, there will be a directory 'initramfs' in there, which contains all files from the original initramfs in their original tree. You can modify the contents as you like, but keep in mind that your initramfs cannot grow larger than the space reserved for it in the original zImage. So you're restricted to relatively small changes which should, however, satisfy many needs. You always can call a script or executable on some other partition (including the SD card if already mounted) if you need more room for your modifications.

After your modifications are done, cd back to the directory which contains zImage and zImage_unpacked and run 'repack-zImage.sh -p' to start the packing process.

This will create a directory called 'zImage_packing' which contains your new zImage (and a zImage.tar for loaders like ODIN). It will emit (between others) one or two messages about a padding being done and about how many bytes were padded. This number (or the lower number of the two) is an indication about how many compressed bytes are left for further additions to the initrd.

If your initramfs (or some other modified part) grows too large, the script will abort with an appropriate error message.

In initramfs-utils.zip, three programs are provided. They should be copied to /usr/local/bin:
* cpio_set0. This is a slightly modified cpio (compiled for 32 bit Linux). repack-zImage.sh will run without it, but there may be slightly more room in your initramfs if you use the modified one. It sets all file times in the archive to 0 (epoch), thus yielding better and consistent compression results. Else, the size of the compressed initramfs will differ from invocation to invocation due to differing atimes. Put it somewhere along your $PATH (e.g., /usr/local/bin).
* gen_init_cpio and
* gen_initramfs_list.sh. These are utilities copied from a kernel tree and used to support creation of an initramfs (in certain modes).

'repack-zImage.sh --help' will output usage information.


Happy hacking,

mizch


Current Version: 6
2011-05-03
('repack-zImage.sh --version' will output version information.)

- added support for lzma compressed ramdisks (both directions)

Version 4
2011-02-17

- Workaround for ambiguous gunzip result, see post #20
- Some code cleanup + CLI cleanup
- better error detection

Version 3
2011-01-06

- now also works with unzipped initramfs withing gzipped zImage part (i.e., all kinds of zImages)

Version 1
2011-01-05

- initial version. Works only for gzipped initramfs within gzipped zImage (e.g., G3 Eclair kernels)

-----------------------
repack-zImage.zip contains version 4 of the script.
For the newest version, download repack-zImage.v6.zip and initramfs-utils.zip.
Thank you for this utility. I've taken the liberty of making a couple of bugfixes - one to initialise compress_list in the -3 option case, and the other to initialise at_min to 0 rather than blank in gunzipWithTrailer. The first stops the -3 option hanging, and the second is cosmetic and prevents sh complaining.

I can confirm it works with zImage 4.0.4 XWLPT for the Samsung Galaxy S II (i9100).

Peter
 

Attachments

Last edited:

psiva2010

Senior Member
Feb 4, 2011
350
53
0
chennai
Please help : repack-zImage.sh: 100: repack-zImage.sh: Syntax error: "(" unexpected

Hi all,

I am getting the follwing error while executing the extraction script how to resolve this?

# sh repack-zImage.sh -u
repack-zImage.sh: 100: repack-zImage.sh: Syntax error: "(" unexpected

Any please help me..

Siva
 

Phil3759

Inactive Recognized Developer
May 30, 2012
9,557
33,049
0
Thank you for this utility. I've taken the liberty of making a couple of bugfixes - one to initialise compress_list in the -3 option case, and the other to initialise at_min to 0 rather than blank in gunzipWithTrailer. The first stops the -3 option hanging, and the second is cosmetic and prevents sh complaining.

I can confirm it works with zImage 4.0.4 XWLPT for the Samsung Galaxy S II (i9100).

Peter
Thank you a lot, the unpack part works perfect

However:
- it will hang on kernels like CF-Root
- the resulting output zimage, even from a stock fw without modifying the intramfs files will not boot, stuck at Samsung first boot screen

Any help please or a tut how to setup

(using Ubuntu 10.10)
 

whiskerp

Senior Member
Mar 22, 2012
273
127
0
Thank you a lot, the unpack part works perfect

However:
- it will hang on kernels like CF-Root
- the resulting output zimage, even from a stock fw without modifying the intramfs files will not boot, stuck at Samsung first boot screen

Any help please or a tut how to setup

(using Ubuntu 10.10)
It works for XWLPT and XWLPU (unpacking, clearing the MMC_ERASE_CAP byte and repacking) and provides a working safe kernel. However it doesn't seem to work for XWLPX and I don't know why yet. If I unpack and repack the XXLPX zImage, the resultant zImage is binary identical. However changing the single byte from 1 to 0 to make it safe (this results in piggy.gz being one byte smaller and getting padded by one byte) makes the kernel hang. Why?!
 
Last edited:

tux_mind

Senior Member
Mar 3, 2012
199
293
0
hi all,
i'm trying to decompress zImage from TF201-kernel_V10_4_2_15.zip kernel

unpacking the blob file gives me the attached zImage.

so...since it's a lzma zImage your script try to bruteforce the lzma archive size.....well....let it run...all night....but nothing.....
i can see numbers like 6125813, which means about 6MB, but the zImage is only 5MB.....

sincerely i just need to hexedit the kernel.img not the initramfs, so my quest is:

can someone fix this bug? or can someone add an option for extract and repack only the kernel.img?

thanks in advance.

PS:
now i'm trying to modify this script for extract only the kernel.img, update soon.
 

Attachments

tux_mind

Senior Member
Mar 3, 2012
199
293
0
i did it

i make some changes to your code.

now if you provide the the -k option it extract only the kernel.img + initramfs(_gz)+part3.

for repacking you have to re-use the -k option with the -4 one.

hope this helps.

note:
/*************************************************************************************************************************
* this is a work around because the script FAIL to extract my lzma compressed zImage ( look @ my previous post ).
* don't use it unless you have my needs ( modifying the kernel only ). Cheers!
************************************************************************************************************************/
 

Attachments