3 # Copyright (C) Michal Sojka <sojkam1@fel.cvut.cz>
5 # Includes pieces from gen_initramfs_list.sh:
7 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
8 # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
10 # Released under the terms of the GNU GPL
12 # Usage: gen_cpio rootfs.cpio "myapp->/bin/" "start.sh->/etc/init.d/S90startmyapp"
13 # gen_cpio --mkimage="-T ramdisk -A powerpc -O linux" ...
23 $0 [--mkimage="<mkimage params>"] [-u <uid>] [-g <gid>] {<cpio_source>}...
24 --mkimage=... Pack the resulting image with U-Boot's "mkimage ..."
25 -u <uid> User ID to map to user ID 0 (root).
26 <uid> is only meaningful if <cpio_source> is a
27 directory. "squash" forces all files to uid 0.
28 Default: $root_uid (you)
29 -g <gid> Group ID to map to group ID 0 (root).
30 <gid> is only meaningful if <cpio_source> is a
31 directory. "squash" forces all files to gid 0.
32 Default: $root_gid (you)
34 - *.cpio: Included directly into the output
35 - directory: Content of the directory is included
37 - src/file->/dst/file: src/file appear in the output
39 - src/file->/dst/dir/: src/file appear in the output
42 All options except --mkimage may be repeated and are interpreted
43 sequentially and immediately. -u and -g states are preserved across
44 <cpio_source> options so an explicit "-u 0 -g 0" is required to reset
45 the root/group mapping.
50 while [ $# -gt 0 ]; do
53 mkimage=${1#--mkimage=}
64 if [ -n "$mkimage" ]; then
69 gen_cpio_from_list() {
70 [ "$cpio_list" ] || return 0
71 echo "$cpio_list" | LANG=C sort -u | gen_init_cpio -
75 # awk style field access
76 # $1 - field number; rest is argument string
84 # symlink test must come before file test
85 if [ -L "${argv1}" ]; then
87 elif [ -f "${argv1}" ]; then
89 elif [ -d "${argv1}" ]; then
91 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
93 elif [ -p "${argv1}" ]; then
95 elif [ -S "${argv1}" ]; then
103 # for each file print a line in following format
104 # <filetype> <name> <path to file> <octal mode> <uid> <gid>
105 # for links, devices etc the format differs. See gen_init_cpio for details
108 local name="/${location#${srcdir}}"
109 # change '//' into '/'
110 name=$(echo "$name" | sed -e 's://*:/:g')
114 local ftype=$(filetype "${location}")
115 # remap uid/gid to 0 if necessary
116 [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
117 [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
118 local str="${mode} ${uid} ${gid}"
120 [ "${ftype}" = "invalid" ] && return 0
121 [ "${location}" = "${srcdir}" ] && return 0
125 str="${ftype} ${name} ${location} ${str}"
128 local dev=`LC_ALL=C ls -l "${location}"`
129 local maj=`field 5 ${dev}`
130 local min=`field 6 ${dev}`
133 [ -b "${location}" ] && dev="b" || dev="c"
135 str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
138 local target=`readlink "${location}"`
139 str="${ftype} ${name} ${target} ${str}"
142 str="${ftype} ${name} ${str}"
151 # process one directory (incl sub-directories)
153 srcdir=$(echo "$1" | sed -e 's://*:/:g')
154 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n")
156 # If $dirlist is only one line, then the directory is empty
157 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
158 echo "${dirlist}" | \
166 ends_with() { [ x"${1}" != x"${1%$2}" ]; }
172 if ends_with "$dst" "/"; then
173 dst="$dst$(basename $src)"
175 local dir=$(dirname $dst)
176 while [ -n "$dir" -a "$dir" != "." -a "$dir" != "/" ]; do
177 echo "dir $dir 0755 0 0"
180 echo "file $dst $src $(find $src -printf '%m') 0 0"
183 while [ $# -gt 0 ]; do
187 "-u") # map $1 to uid=0 (root)
191 "-g") # map $1 to gid=0 (root)
198 cpio_list=$(echo "$cpio_list"; gen_filelist $src $dst)
201 if [ -d "$arg" ]; then
202 cpio_list="$cpio_list$(gen_dirlist $arg)"
203 elif [ -f "$arg" ]; then
204 case "$(file -L $arg)" in
210 echo >&2 "Unsuported file format: $arg"
216 echo >&2 "No file: $arg"
224 if [ -n "$mkimage" ]; then
227 mkimage $mkimage -d $tmp $tmp2 >&2