OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / bin / extract-romfs
1 #!/bin/sh
2 # set -x
3 #
4 # take a Snapgear upgrade image (or just an image) and try to extract the
5 # filesystem (minus dev nodes) creating a tar file based on the name of the
6 # original image.  This way we can easily extract proprietry files form a
7 # released image and add them to the romfs directory of a public tree for a
8 # customer to play with
9 #
10 # David McCullough <davidm@snapgear.com>
11 #
12 ############################################################################
13
14 EXE="$0"
15 IMAGE="$1"
16 TARFILE="$2"
17
18 ############################################################################
19
20 usage()
21 {
22         [ "$*" ] && echo "$*"
23         echo "usage: $EXE flash-upgrade-image [optional-output-filename]"
24         exit 1
25 }
26
27 ############################################################################
28
29 transfer()
30 {
31         (
32                 cd $1
33                 find . -depth \( ! -type c -a ! -type b \) -print |
34                         tar cvzfT $TARFILE - --no-recursion
35         )
36 }
37
38 ############################################################################
39
40 handle_fs()
41 {
42         mkdir -p /tmp/image.$$/romfs
43         if mount -o loop $1 /tmp/image.$$/romfs; then
44                 INITRD="`find /tmp/image.$$ -name initrd.gz`"
45                 [ "$INITRD" ] || INITRD="`find /tmp/image.$$ -name initrd`"
46                 if [ -f "$INITRD" ]; then
47                         $EXE $INITRD
48                 else
49                         transfer /tmp/image.$$
50                 fi
51                 umount /tmp/image.$$/romfs
52                 rm -rf /tmp/image.$$
53         else
54                 rm -rf /tmp/image.$$
55                 echo "Failed to mount filesystem"
56                 echo "Ensure loopback and cramfs/romfs/etc work"
57                 return 1
58         fi
59         return 0
60 }
61
62 ############################################################################
63
64 if [ $# -lt 1 -o $# -gt 2 ]; then
65         usage "bad number of arguments"
66 fi
67
68 if [ ! -f "$IMAGE" ]; then
69         usage "image '$IMAGE' does not exist."
70 fi
71
72
73 if [ $# -eq 1 ]
74 then
75         TARFILE="`basename $IMAGE | sed 's/\.[^.]*$//'`_romfs.tar.gz"
76 fi
77
78 case "$TARFILE" in
79 /*) ;;
80 *)  TARFILE="`pwd`/$TARFILE" ;;
81 esac
82
83
84 if [ -f "$TARFILE" ]; then
85         usage "Output file '$TARFILE' already exists, will not overwrite, exiting."
86 fi
87
88 if ! id 2>&1 | fgrep "uid=0(" > /dev/null 2>&1
89 then
90         echo "You must be root to run this script."
91         echo "You may also need to add loopback/cramfs/romfs or other"
92         echo "support to your linux system. Watch for errors."
93         exit 1
94 fi
95
96 echo "Creating '$TARFILE'"
97 echo "From     '$IMAGE' ..."
98
99 TYPE="`file $IMAGE`"
100
101 case "$TYPE" in
102 *"Linux Compressed ROM File System"*)
103         echo "$IMAGE is a CRAMFS image, extracting..."
104         handle_fs $IMAGE
105         exit $?
106         ;;
107 *"romfs filesystem"*)
108         echo "$IMAGE is a ROMFS image, extracting..."
109         handle_fs $IMAGE
110         exit $?
111         ;;
112 *"ISO 9660"*)
113         echo "$IMAGE is a ISO 9660 image, extracting..."
114         handle_fs $IMAGE
115         exit $?
116         ;;
117 *"gzip compressed data"*)
118         echo "$IMAGE is a COMPRESSED image, extracting..."
119         gunzip < $IMAGE > /tmp/image.$$
120         $EXE /tmp/image.$$
121         rm -f /tmp/image.$$
122         exit 0
123         ;;
124 *)
125         # find the romfs (use last one if many)
126         ROFF="`strings -t d $IMAGE | grep -e -rom1fs- |tail -1|awk '{ print $1 }'`"
127         if [ "$ROFF" ]; then
128                 echo "Trying romfs@$ROFF ..."
129                 dd if=$IMAGE bs=$ROFF skip=1 of=/tmp/image.$$
130                 $EXE /tmp/image.$$
131                 OK=$?
132                 rm -f /tmp/image.$$
133                 [ "$OK" -eq 0 ] && exit 0
134         fi
135         echo "$IMAGE is an unknown/unhandled file type: $TYPE"
136         exit 1
137         ;;
138 esac
139
140 exit 0
141
142 ############################################################################