04-08-2012, 03:25 AM | #1 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
getkernels - "get kernel image files" script
UPDATE: 1.4 adds the 64-byte flash header length to the file length before rounding to the next 1024-byte block size. Now we need to remake affected kernel images in pastebin (such as 4.0.1).
getkernels Use this script to make backup copies of kernel images from an mmcblk0.img backup file, or directly from the mmc device. You can flash these images with fastboot. usage: ./getkernels mmcblk0.img : get kernels from file output:./getkernels /dev/mmcblk0 : get kernels from mmc device main_kernel.img and diags_kernel.img supported devices: DX/DXG/K3/K4/Touch. K3 and earlier do not have diags_kernel. You can see the iterative design process, from linux "one-liner" scripts, through the current version, by clicking the "Show" buttons and comparing the code below. getkernels v1.4 Spoiler:
getkernels v1.3 Spoiler:
getkernels v1.1 Spoiler:
getkernels v1.0 Spoiler:
getkernels v0.1 Spoiler:
Enjoy and learn! (Only tested on Linux host PC, K3, K4 and Touch at this time, but should work on all of the eink kindles.) Last edited by geekmaster; 08-20-2012 at 11:45 PM. Reason: add new and old versions |
04-08-2012, 09:38 AM | #2 |
Addict
Posts: 251
Karma: 183457
Join Date: Jan 2012
Device: k3G, KDXG, AuraHD
|
Thanks for the handy script
|
Advert | |
|
04-08-2012, 12:57 PM | #3 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
Updated first post to include new "speed-enhanced" version 1.1, and old version 0.1 (to show how I create these things). Also added MIT license. Research shows that License by URL is acceptable, and in some cases preferred (unless you want specific license version lock-in). This makes these scripts a LOT smaller than when I embedded license text in previous scripts (which I may go back and replace).
|
04-08-2012, 03:27 PM | #4 |
BLAM!
Posts: 13,477
Karma: 26012492
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
|
FWIW, I'm also using the 'License by URL' trick in the credits file of the fonts/ss hacks.
|
04-08-2012, 11:46 PM | #5 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
getkernels v1.2 now supports K3 (and earlier) kindles, and has significant speed and error-handling enhancements (see first post in this thread).
Last edited by geekmaster; 04-09-2012 at 08:50 AM. |
Advert | |
|
04-09-2012, 08:54 AM | #6 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
The code and scripts that I post in this forum are formatted to fit an entire "concept" into the mobileread "code" window, without needing to scroll that window.
I would like some feedback on my old-school "condensed coding style". For me, the condensed code is actually MORE readable. Of course, if you get paid for "lines of code" instead of "getting the job done", you might want to stick with the popular white-space laden "fluffy" coding style. Last edited by geekmaster; 04-09-2012 at 09:18 AM. |
04-11-2012, 03:56 PM | #7 |
wannabe developer
Posts: 192
Karma: 156548
Join Date: Mar 2011
Device: Kindle: 2xKeyboard, Classic, 2xTouch, 2xPW, PW2; Onyx: Boox M92
|
@geekmaster, you might consider including this code in your script. It works as expected :
Code:
#!/bin/bash DEBUG= TEMP_DIR=/tmp KERNEL_FILE=kernel KERNEL_GZIP_FILE=kernel.gz INITRAMFS_FILE=initramfs.cpio INITRAMFS_DIR=initramfs_root # DO NOT MODIFY BELOW THIS LINE [ -z $1 ] && exit 1 || zImage=$1 [ ! -e $1 ] && exit 1 #GET CURRENT DIR CURRENT_DIR=`pwd` function pre_clean() { [ -z $DEBUG ] || echo "-D- Function: pre_clean()" [ -e $INITRAMFS_FILE ] && ( [ -z $DEBUG ] || echo "-D- Deleting $INITRAMFS_FILE"; rm -f $INITRAMFS_FILE ) [ -e $INITRAMFS_DIR ] && ( [ -z $DEBUG ] || echo "-D- Deleting $INITRAMFS_DIR"; rm -rf $INITRAMFS_DIR ) [ -z $DEBUG ] || echo } function ungzip_kernel() { #======================================================== # find start of gziped kernel object in the zImage file: #======================================================== [ -z $DEBUG ] || echo "-D- Function: ungzip_kernel()" pos=`grep -P -a -b -m 1 --only-matching '\x1F\x8B\x08' $zImage | cut -f 1 -d :` echo "-I- Extracting gzip'd kernel image from file: $zImage (start = $pos)" if [ ! -z $pos ]; then dd if=$zImage of=$TEMP_DIR/$KERNEL_GZIP_FILE bs=1 skip=$pos 2>/dev/null >/dev/null gunzip -qf $TEMP_DIR/$KERNEL_GZIP_FILE else echo "-E- Compressed kernel image not found"; exit 1 fi [ -z $DEBUG ] || echo } function search_cpio() { #======================================================== # Determine cpio compression type: #======================================================== [ -z $DEBUG ] || echo "-D- Function: search_cpio()" for x in gzip bzip lzma none; do case $x in bzip) csig='\x{31}\x{41}\x{59}\x{26}\x{53}\x{59}' ucmd='bunzip2 -q' fext='.bz2' ;; gzip) csig='\x1F\x8B\x08' ucmd='gunzip -q' fext='.gz' ;; lzma) csig='\x{5D}\x{00}\x..\x{FF}\x{FF}\x{FF}\x{FF}\x{FF}\x{FF}' ucmd='unlzma -q' fext='.lzma' ;; none) csig='070701' ucmd= fext= ;; esac #======================================================================== # Search for compressed cpio archive #======================================================================== search=`grep -P -a -b -m 1 --only-matching $csig $TEMP_DIR/$KERNEL_FILE | cut -f 1 -d : | head -1` pos=${search:-0} if [ ${pos} -gt 0 ]; then if [ ${pos} -le ${cpio_compressed_start:-0} ] || [ -z $cpio_compressed_start ];then cpio_compressed_start=$pos compression_name=$x compression_signature=$csig uncompress_cmd=$ucmd file_ext=$fext [ -z $DEBUG ] || echo "-D- Checking for compression type: $compression_name | signature: $compression_signature | in file: $TEMP_DIR/$KERNEL_FILE | offset = $pos" fi fi done [ $compression_name = "bzip" ] && cpio_compressed_start=$((cpio_compressed_start - 4)) echo "-I- CPIO compression type detected = $compression_name | offset = $cpio_compressed_start" [ -z $DEBUG ] || echo } function extract_cpio() { [ -z $DEBUG ] || echo "-D- Function: extract_cpio()" if [ ! $compression_name = "none" ]; then echo "-I- Extracting $compression_name'd compressed CPIO image from kernel image (offset = $cpio_compressed_start)" [ -z $DEBUG ] || echo "-D- dd if=$TEMP_DIR/$KERNEL_FILE of=$TEMP_DIR/$INITRAMFS_FILE$file_ext bs=1 skip=$cpio_compressed_start; $uncompress_cmd $TEMP_DIR/$INITRAMFS_FILE$file_ext" dd if=$TEMP_DIR/$KERNEL_FILE of=$TEMP_DIR/$INITRAMFS_FILE$file_ext bs=1 skip=$cpio_compressed_start 2>/dev/null >/dev/null $uncompress_cmd -q $TEMP_DIR/$INITRAMFS_FILE$file_ext else echo "-I- Extracting non-compressed CPIO image from kernel image (offset = $cpio_compressed_start)" [ -z $DEBUG ] || echo "-D- dd if=$TEMP_DIR/$KERNEL_FILE of=$TEMP_DIR/${INITRAMFS_FILE}${file_ext} bs=1 skip=$cpio_compressed_start 2>/dev/null >/dev/null" dd if=$TEMP_DIR/$KERNEL_FILE of=$TEMP_DIR/${INITRAMFS_FILE}${file_ext} bs=1 skip=$cpio_compressed_start 2>/dev/null >/dev/null fi [ -z $DEBUG ] || echo } function uncompress_cpio() { #========================================================================== # find start and end of the "cpio" initramfs image inside the kernel object: # ASCII cpio header starts with '070701' # The end of the cpio archive is marked with an empty file named TRAILER!!! #========================================================================== [ -z $DEBUG ] || echo "-D- Function: uncompress_cpio()" if [ ! $compress_type = "none" ]; then start=`grep -a -b -m 1 --only-matching '070701' $TEMP_DIR/$INITRAMFS_FILE | head -1 | cut -f 1 -d :` end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' $TEMP_DIR/$INITRAMFS_FILE | head -1 | cut -f 1 -d :` if [ ! -z $start ] || [ ! -z $end ]; then #11 bytes = length of TRAILER!!! zero terminated string, fixes premature end of file warning in CPIO end=$((end + 14)) [ -z $DEBUG ] || echo "-D- Kernel start = $start" [ -z $DEBUG ] || echo "-D- Kernel end = $end" count=$((end - start)) if (($count < 0)); then echo "-E- Couldn't match start/end of the initramfs image." exit 1 fi echo "-I- Extracting initramfs image from file: $inputfile (start = $start, end = $end)" dd if=$TEMP_DIR/$INITRAMFS_FILE of=$CURRENT_DIR/$INITRAMFS_FILE bs=1 skip=$start count=$count 2>/dev/null >/dev/null INITRAMFS_FILE=$CURRENT_DIR/$INITRAMFS_FILE else echo "-E- No CPIO image found in $inputfile." fi else echo "-I- CPIO already uncompressed." fi [ -z $DEBUG ] || echo } function expand_cpio_archive() { [ -z $DEBUG ] || echo "-D- Function: expand_cpio_archive()" echo "-I- Expanding CPIO archive: $INITRAMFS_FILE to $INITRAMFS_DIR." if [ -e $TEMP_DIR/$INITRAMFS_FILE ]; then mkdir $INITRAMFS_DIR cd $INITRAMFS_DIR cpio --quiet -i --make-directories --preserve-modification-time --no-absolute-filenames -F $TEMP_DIR/$INITRAMFS_FILE 2>/dev/null fi [ -z $DEBUG ] || echo } function clean_up() { [ -z $DEBUG ] || echo "-D- Deleting $TEMP_DIR/$KERNEL_FILE"; rm -f $TEMP_DIR/$KERNEL_FILE [ -z $DEBUG ] || echo "-D- Deleting $TEMP_DIR/$INITRAMFS_FILE"; rm -f $TEMP_DIR/$INITRAMFS_FILE [ -z $DEBUG ] || echo "-D- Deleting $TEMP_DIR/$INITRAMFS_FILE$file_ext"; rm -f $TEMP_DIR/$INITRAMFS_FILE$file_ext } reset pre_clean ungzip_kernel search_cpio extract_cpio uncompress_cpio expand_cpio_archive clean_up Last edited by seaniko7; 04-11-2012 at 03:59 PM. |
04-11-2012, 05:38 PM | #8 | |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
Quote:
Do any of the kindles use gzipped kernels? Of course, if not now, they may later. It is nice to be prepared for the future. Of course, this script is loaded with white-space "pollution" that needs a lot of vertical scrolling just to view it, and those long variable names require plenty of horizontal scrolling too, but after it is on the search path, who cares really? And a delete key can fix a lot of that. Where did you find this little gem? Last edited by geekmaster; 04-11-2012 at 05:42 PM. |
|
04-11-2012, 05:50 PM | #9 |
wannabe developer
Posts: 192
Karma: 156548
Join Date: Mar 2011
Device: Kindle: 2xKeyboard, Classic, 2xTouch, 2xPW, PW2; Onyx: Boox M92
|
I found it at xda forums, while I was preparing custom recovery kernel for 3.3.
|
04-13-2012, 05:27 PM | #10 |
Junior Member
Posts: 7
Karma: 10
Join Date: Feb 2012
Device: k touch
|
some one share k4 nt diags_kernel.img
in "simple kindle touch (and k4nt) debricking method",there was only main_kernel of k4 nt there.so someone plz share the diags_kernel one.
|
04-13-2012, 05:54 PM | #11 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
|
08-20-2012, 10:03 AM | #12 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
The getkernels script has been updated to version 1.4 to fix an intermittent bug that could cause up to 64 bytes to be truncated from the end of a kernel image in some cases (such as the 4.0.1 kernels).
It now adds 64 bytes (the length of the flash header) to the image length extracted from the flash header, before rounding up to the next 1024-byte block size. Some comments were also added to the script to make it less "obfuscated". EDIT: Sorry about the delay getting this updated. I thought I updated it a long time ago. That can happen when you have too many projects "in progress". Last edited by geekmaster; 08-20-2012 at 10:15 AM. |
08-20-2012, 04:11 PM | #13 |
BLAM!
Posts: 13,477
Karma: 26012492
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
|
@geekmaster: Thanks! I can provide 5.1.0 diags/main kernels if the current ones are borked.
|
08-20-2012, 04:53 PM | #14 |
Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
They should only be "borked" if their length was a multiple of 1024 plus 1 to 64 extra bytes, and that extra was not just padding. I think most kernels are okay, but they can be checked by extracting with the new getkernels and see if the new ones are larger than the old ones. If not larger, the old ones are fine.
Last edited by geekmaster; 08-20-2012 at 11:34 PM. |
10-21-2012, 09:03 AM | #15 |
Junior Member
Posts: 8
Karma: 5516
Join Date: Oct 2012
Device: Kindle Keyboard 3G, Kindle ($69 Black)
|
Sorry for my ignorance, but I don't know how to use this script.
I placed getkernels-1.0.tar.gz in the kindle root and tried the following in ssh: ./getkernels /dev/mmcblk0 cd /mnt/us && ./getkernels /dev/mmcblk0 && cd - getkernels /dev/mmcblk0 they all reply -sh: getkernels: not found Also, the outputs main_kernel.img and diags_kernel.img actually corresponds to mmcblk0p1.img and mmcblk0p2.img right? Thanks! |
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
extract initramfs from kindle dx kernel image | chinaet | Kindle Developer's Corner | 16 | 04-19-2013 12:16 PM |
boot kindle from kernel image on USB drive | geekmaster | Kindle Developer's Corner | 40 | 10-07-2012 01:46 PM |
Missing files from kernel tree | FDD | Onyx Boox | 0 | 06-03-2012 02:05 PM |
Script to scrape page for a cover image for recipe? | adoucette | Recipes | 12 | 02-29-2012 06:24 PM |
Reflow script for txt files | neurocyp | PocketBook | 6 | 11-19-2011 04:57 AM |