/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# 
# Initramfs unpacker - unpacks initramfs images into /tmp
# 
# Copyright © 2013-2019 Teddy Hogeborn
# Copyright © 2013-2019 Björn Påhlsson
# 
# This file is part of Mandos.
#
# Mandos is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#     Mandos is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mandos.  If not, see
# <http://www.gnu.org/licenses/>.
# 
# Contact the authors at <mandos@recompile.se>.

cpio="cpio --extract --make-directories --unconditional --preserve-modification-time"

if [ -z "$*" ]; then
    set -- /boot/initrd.img-*
fi

for imgfile in "$@"; do
    if ! [ -f "$imgfile" ]; then
	echo "Error: Not an existing file: $imgfile" >&2
	continue
    fi
    imgdir="${TMPDIR:-/tmp}/${imgfile##*/}"
    if [ -d "$imgdir" ]; then
	rm --recursive -- "$imgdir"
    fi
    mkdir --parents "$imgdir"
    # Does this image contain microcode?
    if $cpio --quiet --list --file="$imgfile" >/dev/null 2>&1; then
	# Number of bytes to skip to get to the compressed archive
	skip=$(($(LANG=C $cpio --io-size=1 --list --file="$imgfile" 2>&1 \
		      | sed --quiet \
			    --expression='s/^\([0-9]\+\) blocks$/\1/p')+8))
	if [ -x /usr/lib/dracut/skipcpio ]; then
	    catimg="/usr/lib/dracut/skipcpio $imgfile"
	else
	    catimg="dd if=$imgfile bs=$skip skip=1 status=noxfer"
	fi
    else
	echo "No microcode detected"
	catimg="cat -- $imgfile"
    fi
    while :; do
	# Determine the compression method
	if { $catimg 2>/dev/null | zcat --test >/dev/null 2>&1;
	     [ ${PIPESTATUS[-1]} -eq 0 ]; }; then
	    decomp="zcat"
	elif { $catimg 2>/dev/null | bzip2 --test >/dev/null 2>&1;
	       [ ${PIPESTATUS[-1]} -eq 0 ]; }; then
	    decomp="bzip2 --stdout --decompress"
	elif { $catimg 2>/dev/null | lzop --test >/dev/null 2>&1;
	       [ ${PIPESTATUS[-1]} -eq 0 ]; }; then
	    decomp="lzop --stdout --decompress"
	elif { $catimg 2>/dev/null | zstd --test >/dev/null 2>&1;
	       [ ${PIPESTATUS[-1]} -eq 0 ]; }; then
	    decomp="zstdcat --stdout --decompress"
	else
	    skip=$((${skip}+1))
	    echo "Could not determine compression of ${imgfile}; trying to skip ${skip} bytes" >&2
	    catimg="dd if=$imgfile bs=$skip skip=1 status=noxfer"
	    continue
	fi
	break
    done
    case "$catimg" in
	*skipcpio*) echo "Microcode detected, skipping";;
	*) echo "Microcode detected, skipping ${skip} bytes";;
    esac
    $catimg 2>/dev/null | $decomp | ( cd -- "$imgdir" && $cpio --quiet )
    if [ ${PIPESTATUS[-1]} -eq 0 ]; then
	echo "$imgfile unpacked into $imgdir"
    fi
done