blob: 204f2379a5a05c933346ca84eb59a0abbf2774f0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
57fatal() {
58 echo $1 >/dev/console
59 echo >/dev/console
60
Patrick Williamsf1e5d692016-03-30 15:21:19 -050061 if [ -n "$bootparam_init_fatal_sh" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 sh
63 else
64 while [ "true" ]; do
65 sleep 3600
66 done
67 fi
68}
69
70# Variables shared amoung modules
71ROOTFS_DIR="/rootfs" # where to do the switch root
72MODULE_PRE_HOOKS="" # functions to call before running each module
73MODULE_POST_HOOKS="" # functions to call after running each module
74MODULES_DIR=/init.d # place to look for modules
75
76# make mount stop complaining about missing /etc/fstab
77touch /etc/fstab
78
79# initialize /proc, /sys and /var/lock
80mkdir -p /proc /sys /var/lock
81mount -t proc proc /proc
82mount -t sysfs sysfs /sys
83
84# populate bootparam environment
85for p in `cat /proc/cmdline`; do
86 opt=`echo $p | cut -d'=' -f1`
87 opt=`echo $opt | tr '.-' '__'`
88 if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
89 eval "bootparam_${opt}=true"
90 else
91 value="`echo $p | cut -d'=' -f2-`"
92 eval "bootparam_${opt}=\"${value}\""
93 fi
94done
95
96# use /dev with devtmpfs
97if grep -q devtmpfs /proc/filesystems; then
98 mkdir -p /dev
99 mount -t devtmpfs devtmpfs /dev
100else
101 if [ ! -d /dev ]; then
102 fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
103 fi
104fi
105
106mkdir $ROOTFS_DIR
107
108# Load and run modules
109for m in $MODULES_DIR/*; do
110 # Skip backup files
111 if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
112 continue
113 fi
114
115 module=`basename $m | cut -d'-' -f 2`
116 debug "Loading module $module"
117
118 # pre hooks
119 for h in $MODULE_PRE_HOOKS; do
120 debug "Calling module hook (pre): $h"
121 eval "$h pre $module"
122 debug "Finished module hook (pre): $h"
123 done
124
125 # process module
126 . $m
127
128 if ! eval "${module}_enabled"; then
129 debug "Skipping module $module"
130 continue
131 fi
132
133 debug "Running ${module}_run"
134 eval "${module}_run"
135
136 # post hooks
137 for h in $MODULE_POST_HOOKS; do
138 debug "Calling module hook (post): $h"
139 eval "$h post $module"
140 debug "Finished module hook (post): $h"
141 done
142done
143
144# Catch all
145fatal "ERROR: Initramfs failed to initialize the system."