-
Notifications
You must be signed in to change notification settings - Fork 29
/
cpio.sh
executable file
·86 lines (78 loc) · 1.71 KB
/
cpio.sh
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
#!/bin/bash
#
# This file is part of asafw.
# Copyright (c) 2017, Aaron Adams <aaron.adams(at)nccgroup(dot)trust>
# Copyright (c) 2017, Cedric Halbronn <cedric.halbronn(at)nccgroup(dot)trust>
#
# Main script to deal with a rootfs extracted from a firmware
usage()
{
echo "-c Create cpio image"
echo "-d Directory to turn into cpio image"
echo "-e Extract cpio image"
echo "-o Output file"
echo "Examples:"
echo "Create ./cpio.sh -c -d rootfs -o rootfs.img"
echo "Extract ./cpio.sh -e -i rootfs.img"
}
CREATE=
EXTRACT=
OUTPUT=
DIR=
CPIOFILE=
while [ $# -gt 0 ]
do
key="$1"
case $key in
-c|--create)
CREATE="Y"
;;
-e|--extract)
EXTRACT="Y"
;;
-i|--cpio-image)
CPIOFILE="$2"
shift
;;
-d|--dir)
DIR="$2"
shift # past argument
;;
-o|--output)
OUTPUT="$2"
shift # past argument
;;
*)
# unknown option
echo "Unknown option"
usage
;;
esac
shift
done
CPIODIR=$(dirname "$CPIOFILE")
# Checking for . allows us to specify relative paths...
if [[ ${CPIODIR} == '.' ]]; then
CPIODIR=$(pwd)
fi
if [ ! -z "${CREATE}" ]; then
OLDDIR=$(pwd)
cd "${DIR}"
find . | cpio -o -H newc | gzip -9 > "${OUTPUT}"
cd ${OLDDIR}
fi
if [ ! -z "${EXTRACT}" ]; then
OLDDIR=$(pwd)
if [ -z "${DIR}" ]; then
echo "Extraction requires -d to specify dir to extract to"
exit
fi;
if [ -z "${CPIOFILE}" ]; then
echo "Extraction requires -i to specify cpio image to extract"
exit
fi;
mkdir -p "${DIR}"
cd ${DIR}
cpio -id < ${CPIODIR}/${CPIOFILE}
cd ${OLDDIR}
fi