1153f0114SJamey Sharp#!/bin/sh
21da177e4SLinus Torvalds# Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
3f6112ec2SOleg Verych# Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
41da177e4SLinus Torvalds#
5d39a206bSSam Ravnborg# Released under the terms of the GNU GPL
61da177e4SLinus Torvalds#
7d39a206bSSam Ravnborg# Generate a cpio packed initramfs. It uses gen_init_cpio to generate
865e00e04SMasahiro Yamada# the cpio archive.
9d39a206bSSam Ravnborg# This script assumes that gen_init_cpio is located in usr/ directory
10d39a206bSSam Ravnborg
11d39a206bSSam Ravnborg# error out on errors
12d39a206bSSam Ravnborgset -e
13d39a206bSSam Ravnborg
14d39a206bSSam Ravnborgusage() {
15d39a206bSSam Ravnborgcat << EOF
16d39a206bSSam RavnborgUsage:
1796680975SMasahiro Yamada$0 [-o <file>] [-l <dep_list>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
1865e00e04SMasahiro Yamada	-o <file>      Create initramfs file named <file> by using gen_init_cpio
1996680975SMasahiro Yamada	-l <dep_list>  Create dependency list named <dep_list>
20d39a206bSSam Ravnborg	-u <uid>       User ID to map to user ID 0 (root).
214c6f2eb9SMike Frysinger		       <uid> is only meaningful if <cpio_source> is a
224c6f2eb9SMike Frysinger		       directory.  "squash" forces all files to uid 0.
23d39a206bSSam Ravnborg	-g <gid>       Group ID to map to group ID 0 (root).
244c6f2eb9SMike Frysinger		       <gid> is only meaningful if <cpio_source> is a
254c6f2eb9SMike Frysinger		       directory.  "squash" forces all files to gid 0.
26d39a206bSSam Ravnborg	<cpio_source>  File list or directory for cpio archive.
27f6112ec2SOleg Verych		       If <cpio_source> is a .cpio file it will be used
28d39a206bSSam Ravnborg		       as direct input to initramfs.
29d39a206bSSam Ravnborg
30d39a206bSSam RavnborgAll options except -o and -l may be repeated and are interpreted
31d39a206bSSam Ravnborgsequentially and immediately.  -u and -g states are preserved across
32d39a206bSSam Ravnborg<cpio_source> options so an explicit "-u 0 -g 0" is required
33d39a206bSSam Ravnborgto reset the root/group mapping.
34d39a206bSSam RavnborgEOF
35d39a206bSSam Ravnborg}
36d39a206bSSam Ravnborg
37f6112ec2SOleg Verych# awk style field access
38f6112ec2SOleg Verych# $1 - field number; rest is argument string
39f6112ec2SOleg Verychfield() {
40f6112ec2SOleg Verych	shift $1 ; echo $1
41f6112ec2SOleg Verych}
42f6112ec2SOleg Verych
431da177e4SLinus Torvaldsfiletype() {
441da177e4SLinus Torvalds	local argv1="$1"
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds	# symlink test must come before file test
471da177e4SLinus Torvalds	if [ -L "${argv1}" ]; then
481da177e4SLinus Torvalds		echo "slink"
491da177e4SLinus Torvalds	elif [ -f "${argv1}" ]; then
501da177e4SLinus Torvalds		echo "file"
511da177e4SLinus Torvalds	elif [ -d "${argv1}" ]; then
521da177e4SLinus Torvalds		echo "dir"
531da177e4SLinus Torvalds	elif [ -b "${argv1}" -o -c "${argv1}" ]; then
541da177e4SLinus Torvalds		echo "nod"
551da177e4SLinus Torvalds	elif [ -p "${argv1}" ]; then
561da177e4SLinus Torvalds		echo "pipe"
571da177e4SLinus Torvalds	elif [ -S "${argv1}" ]; then
581da177e4SLinus Torvalds		echo "sock"
591da177e4SLinus Torvalds	else
601da177e4SLinus Torvalds		echo "invalid"
611da177e4SLinus Torvalds	fi
621da177e4SLinus Torvalds	return 0
631da177e4SLinus Torvalds}
641da177e4SLinus Torvalds
651da177e4SLinus Torvaldsprint_mtime() {
661da177e4SLinus Torvalds	local my_mtime="0"
671da177e4SLinus Torvalds
68d39a206bSSam Ravnborg	if [ -e "$1" ]; then
69d39a206bSSam Ravnborg		my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
701da177e4SLinus Torvalds	fi
71d39a206bSSam Ravnborg
72469e87e8SMasahiro Yamada	echo "# Last modified: ${my_mtime}" >> $cpio_list
73469e87e8SMasahiro Yamada	echo "" >> $cpio_list
74d39a206bSSam Ravnborg}
75d39a206bSSam Ravnborg
76d39a206bSSam Ravnborglist_parse() {
7796680975SMasahiro Yamada	if [ -z "$dep_list" -o -L "$1" ]; then
78590abbddSMichal Marek		return
79590abbddSMichal Marek	fi
8096680975SMasahiro Yamada	echo "$1" | sed 's/:/\\:/g; s/$/ \\/' >> $dep_list
811da177e4SLinus Torvalds}
821da177e4SLinus Torvalds
83d39a206bSSam Ravnborg# for each file print a line in following format
84d39a206bSSam Ravnborg# <filetype> <name> <path to file> <octal mode> <uid> <gid>
85d39a206bSSam Ravnborg# for links, devices etc the format differs. See gen_init_cpio for details
861da177e4SLinus Torvaldsparse() {
871da177e4SLinus Torvalds	local location="$1"
88153f0114SJamey Sharp	local name="/${location#${srcdir}}"
891da177e4SLinus Torvalds	# change '//' into '/'
90153f0114SJamey Sharp	name=$(echo "$name" | sed -e 's://*:/:g')
911da177e4SLinus Torvalds	local mode="$2"
921da177e4SLinus Torvalds	local uid="$3"
931da177e4SLinus Torvalds	local gid="$4"
941da177e4SLinus Torvalds	local ftype=$(filetype "${location}")
951da177e4SLinus Torvalds	# remap uid/gid to 0 if necessary
964c6f2eb9SMike Frysinger	[ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
974c6f2eb9SMike Frysinger	[ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
981da177e4SLinus Torvalds	local str="${mode} ${uid} ${gid}"
991da177e4SLinus Torvalds
100153f0114SJamey Sharp	[ "${ftype}" = "invalid" ] && return 0
101153f0114SJamey Sharp	[ "${location}" = "${srcdir}" ] && return 0
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds	case "${ftype}" in
1041da177e4SLinus Torvalds		"file")
1051da177e4SLinus Torvalds			str="${ftype} ${name} ${location} ${str}"
1061da177e4SLinus Torvalds			;;
1071da177e4SLinus Torvalds		"nod")
108cc976614SMasahiro Yamada			local dev="`LC_ALL=C ls -l "${location}"`"
109f6112ec2SOleg Verych			local maj=`field 5 ${dev}`
110f6112ec2SOleg Verych			local min=`field 6 ${dev}`
111f6112ec2SOleg Verych			maj=${maj%,}
112f6112ec2SOleg Verych
113f6112ec2SOleg Verych			[ -b "${location}" ] && dev="b" || dev="c"
114f6112ec2SOleg Verych
115f6112ec2SOleg Verych			str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
1161da177e4SLinus Torvalds			;;
1171da177e4SLinus Torvalds		"slink")
118b5a5e4c7SFelix Fietkau			local target=`readlink "${location}"`
1191da177e4SLinus Torvalds			str="${ftype} ${name} ${target} ${str}"
1201da177e4SLinus Torvalds			;;
1211da177e4SLinus Torvalds		*)
1221da177e4SLinus Torvalds			str="${ftype} ${name} ${str}"
1231da177e4SLinus Torvalds			;;
1241da177e4SLinus Torvalds	esac
1251da177e4SLinus Torvalds
126469e87e8SMasahiro Yamada	echo "${str}" >> $cpio_list
1271da177e4SLinus Torvalds
1281da177e4SLinus Torvalds	return 0
1291da177e4SLinus Torvalds}
1301da177e4SLinus Torvalds
131d39a206bSSam Ravnborgunknown_option() {
132d39a206bSSam Ravnborg	printf "ERROR: unknown option \"$arg\"\n" >&2
133d39a206bSSam Ravnborg	printf "If the filename validly begins with '-', " >&2
134d39a206bSSam Ravnborg	printf "then it must be prefixed\n" >&2
135d39a206bSSam Ravnborg	printf "by './' so that it won't be interpreted as an option." >&2
136d39a206bSSam Ravnborg	printf "\n" >&2
137d39a206bSSam Ravnborg	usage >&2
138d39a206bSSam Ravnborg	exit 1
139d39a206bSSam Ravnborg}
140d39a206bSSam Ravnborg
141d39a206bSSam Ravnborgheader() {
142469e87e8SMasahiro Yamada	printf "\n#####################\n# $1\n" >> $cpio_list
143d39a206bSSam Ravnborg}
144d39a206bSSam Ravnborg
145d39a206bSSam Ravnborg# process one directory (incl sub-directories)
146d39a206bSSam Ravnborgdir_filelist() {
14796680975SMasahiro Yamada	header "$1"
148d39a206bSSam Ravnborg
149d39a206bSSam Ravnborg	srcdir=$(echo "$1" | sed -e 's://*:/:g')
15077a88274SMasahiro Yamada	dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" | LC_ALL=C sort)
151d39a206bSSam Ravnborg
152d39a206bSSam Ravnborg	# If $dirlist is only one line, then the directory is empty
153d39a206bSSam Ravnborg	if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
15496680975SMasahiro Yamada		print_mtime "$1"
155d39a206bSSam Ravnborg
156d39a206bSSam Ravnborg		echo "${dirlist}" | \
157d39a206bSSam Ravnborg		while read x; do
15896680975SMasahiro Yamada			list_parse $x
15996680975SMasahiro Yamada			parse $x
160d39a206bSSam Ravnborg		done
161d39a206bSSam Ravnborg	fi
1621da177e4SLinus Torvalds}
1631da177e4SLinus Torvalds
164d39a206bSSam Ravnborginput_file() {
165d39a206bSSam Ravnborg	source="$1"
166d39a206bSSam Ravnborg	if [ -f "$1" ]; then
16765e00e04SMasahiro Yamada		# If a regular file is specified, assume it is in
16865e00e04SMasahiro Yamada		# gen_init_cpio format
16996680975SMasahiro Yamada		header "$1"
170469e87e8SMasahiro Yamada		print_mtime "$1" >> $cpio_list
171469e87e8SMasahiro Yamada		cat "$1"         >> $cpio_list
17296680975SMasahiro Yamada		if [ -n "$dep_list" ]; then
17396680975SMasahiro Yamada		        echo "$1 \\"  >> $dep_list
17446ed981dSSam Ravnborg			cat "$1" | while read type dir file perm ; do
175153f0114SJamey Sharp				if [ "$type" = "file" ]; then
17696680975SMasahiro Yamada					echo "$file \\" >> $dep_list
17746ed981dSSam Ravnborg				fi
17846ed981dSSam Ravnborg			done
1791da177e4SLinus Torvalds		fi
180d39a206bSSam Ravnborg	elif [ -d "$1" ]; then
18165e00e04SMasahiro Yamada		# If a directory is specified then add all files in it to fs
182d39a206bSSam Ravnborg		dir_filelist "$1"
1831da177e4SLinus Torvalds	else
184d39a206bSSam Ravnborg		echo "  ${prog}: Cannot open '$1'" >&2
1851da177e4SLinus Torvalds		exit 1
1861da177e4SLinus Torvalds	fi
1871da177e4SLinus Torvalds}
1881da177e4SLinus Torvalds
189d39a206bSSam Ravnborgprog=$0
1901da177e4SLinus Torvaldsroot_uid=0
1911da177e4SLinus Torvaldsroot_gid=0
192d39a206bSSam Ravnborgdep_list=
193469e87e8SMasahiro Yamadacpio_list=$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)
194d39a206bSSam Ravnborgoutput="/dev/stdout"
1951da177e4SLinus Torvalds
1967168965eSMasahiro Yamadatrap "rm -f $cpio_list" EXIT
1977168965eSMasahiro Yamada
1981da177e4SLinus Torvaldswhile [ $# -gt 0 ]; do
1991da177e4SLinus Torvalds	arg="$1"
2001da177e4SLinus Torvalds	shift
2011da177e4SLinus Torvalds	case "$arg" in
20296680975SMasahiro Yamada		"-l")	# files included in initramfs - used by kbuild
20396680975SMasahiro Yamada			dep_list="$1"
20496680975SMasahiro Yamada			echo "deps_initramfs := \\" > $dep_list
20596680975SMasahiro Yamada			shift
20696680975SMasahiro Yamada			;;
20765e00e04SMasahiro Yamada		"-o")	# generate cpio image named $1
20865e00e04SMasahiro Yamada			output="$1"
20996680975SMasahiro Yamada			shift
21096680975SMasahiro Yamada			;;
211d39a206bSSam Ravnborg		"-u")	# map $1 to uid=0 (root)
2121da177e4SLinus Torvalds			root_uid="$1"
213595a22acSRob Landley			[ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0)
2141da177e4SLinus Torvalds			shift
2151da177e4SLinus Torvalds			;;
216d39a206bSSam Ravnborg		"-g")	# map $1 to gid=0 (root)
2171da177e4SLinus Torvalds			root_gid="$1"
218595a22acSRob Landley			[ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0)
2191da177e4SLinus Torvalds			shift
2201da177e4SLinus Torvalds			;;
2211da177e4SLinus Torvalds		"-h")
2221da177e4SLinus Torvalds			usage
2231da177e4SLinus Torvalds			exit 0
2241da177e4SLinus Torvalds			;;
2251da177e4SLinus Torvalds		*)
2261da177e4SLinus Torvalds			case "$arg" in
2271da177e4SLinus Torvalds				"-"*)
228d39a206bSSam Ravnborg					unknown_option
2291da177e4SLinus Torvalds					;;
230d39a206bSSam Ravnborg				*)	# input file/dir - process it
23165e00e04SMasahiro Yamada					input_file "$arg"
2321da177e4SLinus Torvalds					;;
2331da177e4SLinus Torvalds			esac
2341da177e4SLinus Torvalds			;;
2351da177e4SLinus Torvalds	esac
2361da177e4SLinus Torvaldsdone
2371da177e4SLinus Torvalds
23865e00e04SMasahiro Yamada# If output_file is set we will generate cpio archive
23925985edcSLucas De Marchi# we are careful to delete tmp files
24065e00e04SMasahiro Yamadatimestamp=
24165e00e04SMasahiro Yamadaif test -n "$KBUILD_BUILD_TIMESTAMP"; then
24265e00e04SMasahiro Yamada	timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
24365e00e04SMasahiro Yamada	if test -n "$timestamp"; then
24465e00e04SMasahiro Yamada		timestamp="-t $timestamp"
245c299ec2dSAlex Landau	fi
246d39a206bSSam Ravnborgfi
24765e00e04SMasahiro Yamadausr/gen_init_cpio $timestamp $cpio_list > $output
248