-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkunified_kernel.sh
executable file
·118 lines (90 loc) · 2.77 KB
/
mkunified_kernel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
function prereq()
{
#Install pre-reqs
apt-get install -y binutils # needed for objdump (and objcopy?)
}
function combine_initrd_images()
{
microcode_img=
full_img=
concat_img=/tmp/combined.img
cat "${microcode_img}" "${full_img}" > "${concat_img}"
}
function get_last_section_terminal_address()
{
local file="${1}"
#get the last section in the file's offset and size and sum, then print in hex with '0x' prefix:
printf 0x%x "$(objdump -h "${file}"| tail -2 | head -1 | awk '{ a=sprintf("%d","0x"$3); b=sprintf("%d","0x"$4); print a+b; }')"
}
function get_section_term_address()
{
local base="${1}" #can be dec or hex (hex must be 0x prefixed)
local size="${2}" #can be dec or hex (hex must be 0x prefixed)
#simply add up and convert to a hex number.
printf 0x%x "$(( base + size ))"
}
function file_size_in_bytes()
{
if [[ ! -f "${file}" ]] ; then
echo 0x00
else
#NB: stat -c%s doesn't work for '/proc/cmdline'
printf 0x%x "$(cat "${file}" | wc --bytes)"
fi
}
function mksections()
{
local base="$1"
local file="$2"
local section_name="$3"
shift 3
if [[ -f "${file}" ]] ; then
echo '--add-section .'"${section_name}"'="'"${file}"'" --change-section-vma .'"${section_name}"'="'"${base}"\"
fi
base="$(get_section_term_address "${base}" "$(file_size_in_bytes "${file}" )")"
if [[ ${#*} -ge 2 ]] ; then
mksections "${base}" "${@}"
fi
}
function build_unified_kernel_image()
{
#add the following sections to the systemd stub, and store the result as 'unified_kernal_image'
#cmdline
#os release info
#splash image
#kernel
#initrd
local systemd_stub_file="/usr/lib/systemd/boot/efi/linuxx64.efi.stub"
local cmdline_file="${1}"
local osrel_file="${2}"
local splash_file="${3}"
local kernel_file="${4}"
local initrd_file="${5}"
local unified_kernel_image_file="${6}"
stub_term_address="$(get_last_section_terminal_address "${systemd_stub_file}")"
sections="$(mksections \
"${stub_term_address}" \
"${osrel_file}" "osrel" \
"${cmdline_file}" "cmdline" \
"${splash_file}" "splash" \
"${kernel_file}" "linux" \
"${initrd_file}" "initrd")"
echo "${sections}"
eval objcopy \
${sections} \
"${systemd_stub_file}" "${unified_kernel_image_file}"
}
function all_together()
{
#combine_initrd_images # suspect this isn't needed in Ubuntu?
cat /proc/cmdline > /tmp/cmdline
build_unified_kernel_image \
/tmp/cmdline \
/usr/lib/os-release \
""\
"/boot/vmlinuz-$(uname -r)" \
"/boot/initrd.img-$(uname -r)" \
"test.img"
}
all_together