109d48127SCorentin Chary#!/bin/sh 24317cf95SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only 309d48127SCorentin Chary# ---------------------------------------------------------------------- 409d48127SCorentin Chary# extract-vmlinux - Extract uncompressed vmlinux from a kernel image 509d48127SCorentin Chary# 609d48127SCorentin Chary# Inspired from extract-ikconfig 709d48127SCorentin Chary# (c) 2009,2010 Dick Streefland <dick@streefland.net> 809d48127SCorentin Chary# 909d48127SCorentin Chary# (c) 2011 Corentin Chary <corentin.chary@gmail.com> 1009d48127SCorentin Chary# 1109d48127SCorentin Chary# ---------------------------------------------------------------------- 1209d48127SCorentin Chary 13*1e150869SMaxime Thiebautme=${0##*/} 14*1e150869SMaxime Thiebaut 1509d48127SCorentin Charycheck_vmlinux() 1609d48127SCorentin Chary{ 17b9f75396SJeremy Linton if file "$1" | grep -q 'Linux kernel.*boot executable' || 18b9f75396SJeremy Linton readelf -h "$1" > /dev/null 2>&1 19b9f75396SJeremy Linton then 20b9f75396SJeremy Linton cat "$1" 21*1e150869SMaxime Thiebaut echo "$me: Extracted vmlinux using '$2' from offset $3" >&2 2209d48127SCorentin Chary exit 0 23b9f75396SJeremy Linton fi 2409d48127SCorentin Chary} 2509d48127SCorentin Chary 2609d48127SCorentin Charytry_decompress() 2709d48127SCorentin Chary{ 2809d48127SCorentin Chary # The obscure use of the "tr" filter is to work around older versions of 2909d48127SCorentin Chary # "grep" that report the byte offset of the line instead of the pattern. 3009d48127SCorentin Chary 3109d48127SCorentin Chary # Try to find the header ($1) and decompress from here 3209d48127SCorentin Chary for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"` 3309d48127SCorentin Chary do 3409d48127SCorentin Chary pos=${pos%%:*} 3509d48127SCorentin Chary tail -c+$pos "$img" | $3 > $tmp 2> /dev/null 36*1e150869SMaxime Thiebaut check_vmlinux $tmp "$3" $pos 3709d48127SCorentin Chary done 3809d48127SCorentin Chary} 3909d48127SCorentin Chary 4009d48127SCorentin Chary# Check invocation: 4109d48127SCorentin Charyimg=$1 4209d48127SCorentin Charyif [ $# -ne 1 -o ! -s "$img" ] 4309d48127SCorentin Charythen 4409d48127SCorentin Chary echo "Usage: $me <kernel-image>" >&2 4509d48127SCorentin Chary exit 2 4609d48127SCorentin Charyfi 4709d48127SCorentin Chary 4809d48127SCorentin Chary# Prepare temp files: 4909d48127SCorentin Charytmp=$(mktemp /tmp/vmlinux-XXX) 5009d48127SCorentin Charytrap "rm -f $tmp" 0 5109d48127SCorentin Chary 5209d48127SCorentin Chary# That didn't work, so retry after decompression. 5309d48127SCorentin Charytry_decompress '\037\213\010' xy gunzip 5409d48127SCorentin Charytry_decompress '\3757zXZ\000' abcde unxz 5509d48127SCorentin Charytry_decompress 'BZh' xy bunzip2 5609d48127SCorentin Charytry_decompress '\135\0\0\0' xxx unlzma 5709d48127SCorentin Charytry_decompress '\211\114\132' xy 'lzop -d' 5847a18a2dSAdam Borowskitry_decompress '\002!L\030' xxx 'lz4 -d' 5947a18a2dSAdam Borowskitry_decompress '(\265/\375' xxx unzstd 6009d48127SCorentin Chary 61db139d71SHelge Deller# Finally check for uncompressed images or objects: 62*1e150869SMaxime Thiebautcheck_vmlinux "$img" cat 0 63db139d71SHelge Deller 6409d48127SCorentin Chary# Bail out: 6509d48127SCorentin Charyecho "$me: Cannot find vmlinux." >&2 66