blob: c71ce0ce8ccef079168019f64a69e4ac0d60af86 [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
Brad Bishop19323692019-04-05 15:28:33 -040075EFI_DIR=/sys/firmware/efi # place to store device firmware information
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
77# make mount stop complaining about missing /etc/fstab
78touch /etc/fstab
79
Patrick Williamsc0f7c042017-02-23 20:41:17 -060080# initialize /proc, /sys, /run/lock and /var/lock
81mkdir -p /proc /sys /run/lock /var/lock
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082mount -t proc proc /proc
83mount -t sysfs sysfs /sys
84
Brad Bishop19323692019-04-05 15:28:33 -040085if [ -d $EFI_DIR ];then
86 mount -t efivarfs none /sys/firmware/efi/efivars
87fi
88
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089# populate bootparam environment
90for p in `cat /proc/cmdline`; do
91 opt=`echo $p | cut -d'=' -f1`
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080092 opt=`echo $opt | sed -e 'y/.-/__/'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
94 eval "bootparam_${opt}=true"
95 else
96 value="`echo $p | cut -d'=' -f2-`"
97 eval "bootparam_${opt}=\"${value}\""
98 fi
99done
100
101# use /dev with devtmpfs
102if grep -q devtmpfs /proc/filesystems; then
103 mkdir -p /dev
104 mount -t devtmpfs devtmpfs /dev
105else
106 if [ ! -d /dev ]; then
107 fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
108 fi
109fi
110
111mkdir $ROOTFS_DIR
112
113# Load and run modules
114for m in $MODULES_DIR/*; do
115 # Skip backup files
116 if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then
117 continue
118 fi
119
120 module=`basename $m | cut -d'-' -f 2`
121 debug "Loading module $module"
122
123 # pre hooks
124 for h in $MODULE_PRE_HOOKS; do
125 debug "Calling module hook (pre): $h"
126 eval "$h pre $module"
127 debug "Finished module hook (pre): $h"
128 done
129
130 # process module
131 . $m
132
133 if ! eval "${module}_enabled"; then
134 debug "Skipping module $module"
135 continue
136 fi
137
138 debug "Running ${module}_run"
139 eval "${module}_run"
140
141 # post hooks
142 for h in $MODULE_POST_HOOKS; do
143 debug "Calling module hook (post): $h"
144 eval "$h post $module"
145 debug "Finished module hook (post): $h"
146 done
147done
148
149# Catch all
150fatal "ERROR: Initramfs failed to initialize the system."