summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/liveupdate/init.c
blob: fb08bd58b9b965cf1ca5033f1796161f4a0102fa (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (c) 2025, Google LLC.
 * Pasha Tatashin <pasha.tatashin@soleen.com>
 */
#include <fcntl.h>
#include <linux/kexec.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>

#define COMMAND_LINE_SIZE 2048
#define KERNEL_IMAGE "/kernel"
#define INITRD_IMAGE "/initrd.img"
#define TEST_BINARY "/test_binary"

static int mount_filesystems(void)
{
	if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL) < 0) {
		fprintf(stderr, "INIT: Warning: Failed to mount devtmpfs\n");
		return -1;
	}

	if (mount("debugfs", "/debugfs", "debugfs", 0, NULL) < 0) {
		fprintf(stderr, "INIT: Failed to mount debugfs\n");
		return -1;
	}

	if (mount("proc", "/proc", "proc", 0, NULL) < 0) {
		fprintf(stderr, "INIT: Failed to mount proc\n");
		return -1;
	}

	return 0;
}

static long kexec_file_load(int kernel_fd, int initrd_fd,
			    unsigned long cmdline_len, const char *cmdline,
			    unsigned long flags)
{
	return syscall(__NR_kexec_file_load, kernel_fd, initrd_fd, cmdline_len,
		       cmdline, flags);
}

static int kexec_load(void)
{
	char cmdline[COMMAND_LINE_SIZE];
	int kernel_fd, initrd_fd, err;
	ssize_t len;
	int fd;

	fd = open("/proc/cmdline", O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "INIT: Failed to read /proc/cmdline\n");

		return -1;
	}

	len = read(fd, cmdline, sizeof(cmdline) - 1);
	close(fd);
	if (len < 0)
		return -1;

	cmdline[len] = 0;
	if (len > 0 && cmdline[len - 1] == '\n')
		cmdline[len - 1] = 0;

	strncat(cmdline, " luo_stage=2", sizeof(cmdline) - strlen(cmdline) - 1);

	kernel_fd = open(KERNEL_IMAGE, O_RDONLY);
	if (kernel_fd < 0) {
		fprintf(stderr, "INIT: Failed to open kernel image\n");
		return -1;
	}

	initrd_fd = open(INITRD_IMAGE, O_RDONLY);
	if (initrd_fd < 0) {
		fprintf(stderr, "INIT: Failed to open initrd image\n");
		close(kernel_fd);
		return -1;
	}

	err = kexec_file_load(kernel_fd, initrd_fd, strlen(cmdline) + 1,
			      cmdline, 0);

	close(initrd_fd);
	close(kernel_fd);

	return err;
}

static int run_test(int stage)
{
	char stage_arg[32];
	int status;
	pid_t pid;

	snprintf(stage_arg, sizeof(stage_arg), "%d", stage);

	pid = fork();
	if (pid < 0)
		return -1;

	if (!pid) {
		char *const argv[] = {TEST_BINARY, "-s", stage_arg, NULL};

		execve(TEST_BINARY, argv, NULL);
		fprintf(stderr, "INIT: execve failed\n");
		_exit(1);
	}

	waitpid(pid, &status, 0);

	return (WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : -1;
}

static int get_current_stage(void)
{
	char cmdline[COMMAND_LINE_SIZE];
	ssize_t len;
	int fd;

	fd = open("/proc/cmdline", O_RDONLY);
	if (fd < 0)
		return -1;

	len = read(fd, cmdline, sizeof(cmdline) - 1);
	close(fd);

	if (len < 0)
		return -1;

	cmdline[len] = 0;

	return strstr(cmdline, "luo_stage=2") ? 2 : 1;
}

int main(int argc, char *argv[])
{
	int current_stage;
	int err;

	if (mount_filesystems())
		goto err_reboot;

	current_stage = get_current_stage();
	if (current_stage < 0) {
		fprintf(stderr, "INIT: Failed to read cmdline");
		goto err_reboot;
	}

	printf("INIT: Starting Stage %d\n", current_stage);

	if (current_stage == 1 && kexec_load()) {
		fprintf(stderr, "INIT: Failed to load kexec kernel\n");
		goto err_reboot;
	}

	if (run_test(current_stage)) {
		fprintf(stderr, "INIT: Test binary returned failure\n");
		goto err_reboot;
	}

	printf("INIT: Stage %d completed successfully.\n", current_stage);
	reboot(current_stage == 1 ? RB_KEXEC : RB_AUTOBOOT);

	return 0;

err_reboot:
	reboot(RB_AUTOBOOT);

	return -1;
}