forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kexec.c
114 lines (96 loc) · 2.89 KB
/
kexec.c
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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "kexec.h"
#include "lib/containers.h"
#include "lib/log.h"
#include "lib/util.h"
// kexec --load-hardboot ./zImage --command-line="$(cat /proc/cmdline)" --mem-min=0xA0000000 --initrd=./rd.img
// --mem-min should be somewhere in System RAM (see /proc/iomem). Location just above kernel seems to work fine.
// It must not conflict with vmalloc ram. Vmalloc area seems to be allocated from top of System RAM.
void kexec_init(struct kexec *k, const char *path)
{
k->args = NULL;
kexec_add_arg(k, path);
}
void kexec_destroy(struct kexec *k)
{
list_clear(&k->args, &free);
}
int kexec_load_exec(struct kexec *k)
{
int i, len;
INFO("Loading kexec:\n");
for(i = 0; k->args && k->args[i]; ++i)
{
len = strlen(k->args[i]);
if(len < 480)
INFO(" %s\n", k->args[i]);
else
{
char buff[481];
char *itr;
const char *end = k->args[i]+len;
int chunk = 0;
for(itr = k->args[i]; itr < end; itr += chunk)
{
chunk = imin(480, end - itr);
memcpy(buff, itr, chunk);
buff[chunk] = 0;
INFO(" %s\n", buff);
}
}
}
if(run_cmd(k->args) == 0)
return 0;
else
{
ERROR("kexec call failed, re-running it to get info:\n");
char *r = run_get_stdout(k->args);
if(!r)
ERROR("run_get_stdout returned NULL!\n");
char *p = strtok(r, "\n\r");
while(p)
{
ERROR(" %s\n", p);
p = strtok(NULL, "\n\r");
}
free(r);
return -1;
}
}
void kexec_add_arg(struct kexec *k, const char *arg)
{
list_add(&k->args, strdup(arg));
}
void kexec_add_arg_prefix(struct kexec *k, const char *prefix, const char *value)
{
int len = strlen(prefix) + strlen(value) + 1;
char *arg = malloc(len);
snprintf(arg, len, "%s%s", prefix, value);
list_add(&k->args, arg);
}
void kexec_add_kernel(struct kexec *k, const char *path, int hardboot)
{
if(hardboot)
kexec_add_arg(k, "--load-hardboot");
else
kexec_add_arg(k, "-l");
kexec_add_arg(k, path);
}