]> rtime.felk.cvut.cz Git - novaboot.git/blob - contrib/gen_cpio
Document how to use gen_cpio to produce uBoot images
[novaboot.git] / contrib / gen_cpio
1 #!/bin/sh
2 #
3 # Copyright (C) Michal Sojka <sojkam1@fel.cvut.cz>
4 #
5 # Includes pieces from gen_initramfs_list.sh:
6 #
7 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
8 # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
9 #
10 # Released under the terms of the GNU GPL
11
12 # Usage: gen_cpio rootfs.cpio myapp->/bin/ start.sh->/etc/init.d/S90startmyapp
13 #        gen_cpio --mkimage="-T ramdisk -A powerpc -O linux" ...
14
15 set -e
16
17 while [ $# -gt 0 ]; do
18     case "$1" in
19         --mkimage=*) mkimage=${1#--mkimage=};;
20         *) break;;
21     esac
22     shift
23 done
24
25 if [ -n "$mkimage" ]; then
26     tmp=$(mktemp)
27     exec 3<&1 1>$tmp
28 fi
29
30 gen_cpio_from_list() {
31     [ "$cpio_list" ] || return 0
32     echo "$cpio_list" | LANG=C sort -u | gen_init_cpio -
33     cpio_list=
34 }
35
36 filetype() {
37     local argv1="$1"
38
39     # symlink test must come before file test
40     if [ -L "${argv1}" ]; then
41         echo "slink"
42     elif [ -f "${argv1}" ]; then
43         echo "file"
44     elif [ -d "${argv1}" ]; then
45         echo "dir"
46     elif [ -b "${argv1}" -o -c "${argv1}" ]; then
47         echo "nod"
48     elif [ -p "${argv1}" ]; then
49         echo "pipe"
50     elif [ -S "${argv1}" ]; then
51         echo "sock"
52     else
53         echo "invalid"
54     fi
55     return 0
56 }
57
58 ends_with() { [ x"${1}" != x"${1#$2}" ]; }
59
60 gen_list() {
61     local src=$1
62     local dst=$2
63
64     if ends_with "$dst" "/"; then
65         dst="$dst$(basename $src)"
66     fi
67     local dir=$(dirname $dst)
68     while [ -n "$dir" -a "$dir" != "." -a "$dir" != "/" ]; do
69         echo "dir $dir 0755 0 0"
70         dir=$(dirname $dir)
71     done
72     echo "$(filetype $src) $dst $src $(find $src -printf '%m') 0 0"
73 }
74
75 while [ $# -gt 0 ]; do
76     arg="$1"
77     shift
78     if [ -f "$arg" ]; then
79         case "$(file $arg)" in
80             *"cpio archive"*)
81                 gen_cpio_from_list
82                 cat $arg
83                 ;;
84             *)
85                 echo >&2 "Unsuported file format: $arg"
86                 exit 1
87                 ;;
88         esac
89     else
90         case "$arg" in
91             *"->"*)
92                 src=${arg%%->*}
93                 dst=${arg#*->}
94                 cpio_list="$cpio_list$(gen_list $src $dst)"
95                 ;;
96             *)
97                 echo >&2 "No file: $arg"
98                 exit 1
99         esac
100     fi
101 done
102
103 gen_cpio_from_list
104
105 if [ -n "$mkimage" ]; then
106     exec 1<&3
107     tmp2=$(mktemp)
108     mkimage $mkimage -d $tmp $tmp2 >&2
109     cat $tmp2
110     rm $tmp $tmp2
111 fi