xref: /openbmc/openbmc/poky/meta/recipes-core/initrdscripts/initramfs-framework/init (revision 8460358c3d24c71d9d38fd126c745854a6301564)
1#!/bin/sh
2# Copyright (C) 2011 O.S. Systems Software LTDA.
3# Licensed on MIT
4#
5# Provides the API to be used by the initramfs modules
6#
7# Modules need to provide the following functions:
8#
9# <module>_enabled : check if the module ought to run (return 1 to skip)
10# <module>_run     : do what is need
11#
12# Boot parameters are available on environment in the as:
13#
14# 'foo=value' as 'bootparam_foo=value'
15# 'foo' as 'bootparam_foo=true'
16# 'foo.bar[=value] as 'foo_bar=[value|true]'
17
18# Register a function to be called before running a module
19# The hook is called as:
20#   <function> pre <module>
21add_module_pre_hook() {
22	MODULE_PRE_HOOKS="$MODULE_PRE_HOOKS $1"
23}
24
25# Register a function to be called after running a module
26# The hook is called as:
27#   <function> post <module>
28add_module_post_hook() {
29	MODULE_POST_HOOKS="$MODULE_POST_HOOKS $1"
30}
31
32# Load kernel module
33load_kernel_module() {
34	if modprobe $1 >/dev/null 2>&1; then
35		info "Loaded module $1"
36	else
37		debug "Failed to load module $1"
38	fi
39}
40
41# Prints information
42msg() {
43	echo "$@" >/dev/console
44}
45
46# Prints information if verbose bootparam is used
47info() {
48	[ -n "$bootparam_verbose" ] && echo "$@" >/dev/console
49}
50
51# Prints information if debug bootparam is used
52debug() {
53	[ -n "$bootparam_debug" ] && echo "DEBUG: $@" >/dev/console
54}
55
56# Prints a message and start a endless loop
57# Force reboot if init_fatal_reboot bootparam is set
58fatal() {
59	echo $1 >/dev/console
60	echo >/dev/console
61
62	if [ -n "$bootparam_init_fatal_reboot" ]; then
63		reboot -f
64	elif [ -n "$bootparam_init_fatal_sh" ]; then
65		sh
66	else
67		while [ "true" ]; do
68			sleep 3600
69		done
70	fi
71}
72
73# Variables shared among modules
74ROOTFS_DIR="/rootfs" # where to do the switch root
75MODULE_PRE_HOOKS=""  # functions to call before running each module
76MODULE_POST_HOOKS="" # functions to call after running each module
77MODULES_DIR=/init.d  # place to look for modules
78EFI_DIR=/sys/firmware/efi  # place to store device firmware information
79
80# make mount stop complaining about missing /etc/fstab
81touch /etc/fstab
82
83# initialize /proc, /sys, /run/lock and /var/lock
84mkdir -p /proc /sys /run/lock /var/lock
85mount -t proc proc /proc
86mount -t sysfs sysfs /sys
87
88if [ -d $EFI_DIR ];then
89	mount -t efivarfs none /sys/firmware/efi/efivars
90fi
91
92# populate bootparam environment
93for p in `cat /proc/cmdline`; do
94	if [ -n "$quoted" ]; then
95		value="$value $p"
96		if [ "`echo $p | sed -e 's/\"$//'`" != "$p" ]; then
97			eval "bootparam_${quoted}=${value}"
98			unset quoted
99		fi
100		continue
101	fi
102
103	opt=`echo $p | cut -d'=' -f1`
104	opt=`echo $opt | sed -e 'y/.-/__/'`
105	if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
106		eval "bootparam_${opt}=true"
107	else
108		value="`echo $p | cut -d'=' -f2-`"
109		if [ "`echo $value | sed -e 's/^\"//'`" != "$value" ]; then
110			quoted=${opt}
111			continue
112		fi
113		eval "bootparam_${opt}=\"${value}\""
114	fi
115done
116
117# use /dev with devtmpfs
118if grep -q devtmpfs /proc/filesystems; then
119	mkdir -p /dev
120	mount -t devtmpfs devtmpfs /dev
121else
122	if [ ! -d /dev ]; then
123		fatal "ERROR: /dev doesn't exist and kernel doesn't have devtmpfs enabled."
124	fi
125fi
126
127mkdir $ROOTFS_DIR
128
129# Load and run modules
130for m in $MODULES_DIR/*; do
131	# Skip backup files
132	if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
133		continue
134	fi
135
136	module=`basename $m | cut -d'-' -f 2`
137	debug "Loading module $module"
138
139	# pre hooks
140	for h in $MODULE_PRE_HOOKS; do
141		debug "Calling module hook (pre): $h"
142		eval "$h pre $module"
143		debug "Finished module hook (pre): $h"
144	done
145
146	# process module
147	. $m
148
149	if ! eval "${module}_enabled"; then
150		debug "Skipping module $module"
151		continue
152	fi
153
154	debug "Running ${module}_run"
155	eval "${module}_run"
156
157	# post hooks
158	for h in $MODULE_POST_HOOKS; do
159		debug "Calling module hook (post): $h"
160		eval "$h post $module"
161		debug "Finished module hook (post): $h"
162	done
163done
164
165# Catch all
166fatal "ERROR: Initramfs failed to initialize the system."
167