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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2025, Google LLC.
* Pasha Tatashin <pasha.tatashin@soleen.com>
*/
#define pr_fmt(fmt) KBUILD_MODNAME " test: " fmt
#include <linux/cleanup.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/liveupdate.h>
#include <linux/module.h>
#include "../../kernel/liveupdate/luo_internal.h"
static const struct liveupdate_flb_ops test_flb_ops;
#define DEFINE_TEST_FLB(i) { \
.ops = &test_flb_ops, \
.compatible = LIVEUPDATE_TEST_FLB_COMPATIBLE(i), \
}
/* Number of Test FLBs to register with every file handler */
#define TEST_NFLBS 3
static struct liveupdate_flb test_flbs[TEST_NFLBS] = {
DEFINE_TEST_FLB(0),
DEFINE_TEST_FLB(1),
DEFINE_TEST_FLB(2),
};
#define TEST_FLB_MAGIC_BASE 0xFEEDF00DCAFEBEE0ULL
static int test_flb_preserve(struct liveupdate_flb_op_args *argp)
{
ptrdiff_t index = argp->flb - test_flbs;
pr_info("%s: preserve was triggered\n", argp->flb->compatible);
argp->data = TEST_FLB_MAGIC_BASE + index;
return 0;
}
static void test_flb_unpreserve(struct liveupdate_flb_op_args *argp)
{
pr_info("%s: unpreserve was triggered\n", argp->flb->compatible);
}
static int test_flb_retrieve(struct liveupdate_flb_op_args *argp)
{
ptrdiff_t index = argp->flb - test_flbs;
u64 expected_data = TEST_FLB_MAGIC_BASE + index;
if (argp->data == expected_data) {
pr_info("%s: found flb data from the previous boot\n",
argp->flb->compatible);
argp->obj = (void *)argp->data;
} else {
pr_err("%s: ERROR - incorrect data handle: %llx, expected %llx\n",
argp->flb->compatible, argp->data, expected_data);
return -EINVAL;
}
return 0;
}
static void test_flb_finish(struct liveupdate_flb_op_args *argp)
{
ptrdiff_t index = argp->flb - test_flbs;
void *expected_obj = (void *)(TEST_FLB_MAGIC_BASE + index);
if (argp->obj == expected_obj) {
pr_info("%s: finish was triggered\n", argp->flb->compatible);
} else {
pr_err("%s: ERROR - finish called with invalid object\n",
argp->flb->compatible);
}
}
static const struct liveupdate_flb_ops test_flb_ops = {
.preserve = test_flb_preserve,
.unpreserve = test_flb_unpreserve,
.retrieve = test_flb_retrieve,
.finish = test_flb_finish,
.owner = THIS_MODULE,
};
static void liveupdate_test_init(void)
{
static DEFINE_MUTEX(init_lock);
static bool initialized;
int i;
guard(mutex)(&init_lock);
if (initialized)
return;
for (i = 0; i < TEST_NFLBS; i++) {
struct liveupdate_flb *flb = &test_flbs[i];
void *obj;
int err;
err = liveupdate_flb_get_incoming(flb, &obj);
if (err && err != -ENODATA && err != -ENOENT) {
pr_err("liveupdate_flb_get_incoming for %s failed: %pe\n",
flb->compatible, ERR_PTR(err));
}
}
initialized = true;
}
void liveupdate_test_register(struct liveupdate_file_handler *fh)
{
int err, i;
liveupdate_test_init();
for (i = 0; i < TEST_NFLBS; i++) {
struct liveupdate_flb *flb = &test_flbs[i];
err = liveupdate_register_flb(fh, flb);
if (err) {
pr_err("Failed to register %s %pe\n",
flb->compatible, ERR_PTR(err));
}
}
err = liveupdate_register_flb(fh, &test_flbs[0]);
if (!err || err != -EEXIST) {
pr_err("Failed: %s should be already registered, but got err: %pe\n",
test_flbs[0].compatible, ERR_PTR(err));
}
pr_info("Registered %d FLBs with file handler: [%s]\n",
TEST_NFLBS, fh->compatible);
}
void liveupdate_test_unregister(struct liveupdate_file_handler *fh)
{
int err, i;
for (i = 0; i < TEST_NFLBS; i++) {
struct liveupdate_flb *flb = &test_flbs[i];
err = liveupdate_unregister_flb(fh, flb);
if (err) {
pr_err("Failed to unregister %s %pe\n",
flb->compatible, ERR_PTR(err));
}
}
pr_info("Unregistered %d FLBs from file handler: [%s]\n",
TEST_NFLBS, fh->compatible);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pasha Tatashin <pasha.tatashin@soleen.com>");
MODULE_DESCRIPTION("In-kernel test for LUO mechanism");
|