summaryrefslogtreecommitdiff
path: root/drivers/test_rs/update_capsule.c
blob: b856215958af625e9b3e6306c24d1699788ed500 (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
/*
 * Test Update Capsule module.
 *
 * Copyright (C) 2014 Linaro
 * Author: Sam Protsenko <semen.protsenko@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/efi.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/sysfs.h>

#define TRS_DIR_NAME		"test_uefi_rs"

static ssize_t tuc_show(struct kobject *kobj, struct kobj_attribute *attr,
		char *buf);

static struct kobject *trs_kobj;
static struct kobj_attribute tuc_attribute = __ATTR_RO(tuc);
static struct attribute *tuc_attrs[] = {
	&tuc_attribute.attr,
	NULL
};
static struct attribute_group tuc_attr_group = {
	.attrs = tuc_attrs
};
static DEFINE_MUTEX(tuc_mutex);

static int invoke_update_capsule(void *fw_buf)
{
	typedef struct {
		u32 data1;
		u16 data2;
		u16 data3;
		u8 data4[8];
	} efi_guid2_t;

	efi_capsule_header_t *capsules;
	int i, j;
	efi_guid2_t guid[2];
	efi_status_t status;

	capsules = kzalloc(2 * sizeof(efi_capsule_header_t), GFP_KERNEL);
	if (capsules == NULL) {
		pr_err("tuc: Unable to allocate memory for capsules\n");
		return -ENOMEM;
	}
	for (i = 0; i < 2; ++i) {
		guid[i].data1 = 111;
		guid[i].data2 = 222;
		guid[i].data3 = 333;
		for (j = 0; j < 8; ++j)
			guid[i].data4[j] = j + (i+1)*10;

		capsules[i].guid = *(efi_guid_t *)(&(guid[i]));
		capsules[i].headersize = sizeof(efi_capsule_header_t);
		capsules[i].imagesize = 0;
	}
	capsules[0].flags = (u32)(u64)fw_buf;
	capsules[1].flags = (u32)(((u64)fw_buf >> 32));

	/* UEFI RS invokation */
	status = efi.update_capsule(&capsules, 2, 0);
	if (status != EFI_SUCCESS)
		pr_err("tuc: error runtime service status: %lu\n", status);

	kfree(capsules);

	return 0;
}

/* Display info from this sysfs entry (cat ...) */
static ssize_t tuc_show(struct kobject *kobj, struct kobj_attribute *attr,
		char *buf)
{
	void *fw_buf;
	int len;
	int ret;

	mutex_lock(&tuc_mutex);
	fw_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (fw_buf == NULL) {
		pr_err("tuc: Unable to allocate memory for shared memory\n");
		mutex_unlock(&tuc_mutex);
		return -ENOMEM;
	}

	ret = invoke_update_capsule(fw_buf);
	if (ret) {
		len = -ENOMEM;
		goto err;
	}

	len = sprintf(buf, "%s", (char *)fw_buf);
	if (len <= 0)
		pr_err("tuc: Invalid sprintf len: %d\n", len);

err:
	kfree(fw_buf);
	mutex_unlock(&tuc_mutex);
	return len;
}

static int __init tuc_init_sysfs(void)
{
	int ret = 0;

	trs_kobj = kobject_create_and_add(TRS_DIR_NAME, trs_kobj);
	if (!trs_kobj) {
		pr_err("tuc: unable to allocate memory for sysfs kobj\n");
		return -ENOMEM;
	}

	ret = sysfs_create_group(trs_kobj, &tuc_attr_group);
	if (ret) {
		pr_err("tuc: sysfs creation failed\n");
		kobject_put(trs_kobj);
	}

	return ret;
}

static int __init tuc_init(void)
{
	int ret;

	ret = tuc_init_sysfs();
	if (ret) {
		pr_err("tuc: unable to initialize sysfs\n");
		return ret;
	}

	pr_info("tuc: successfully loaded\n");
	return 0;
}

static void __exit tuc_exit(void)
{
	kobject_put(trs_kobj);
	pr_info("tuc: unloaded\n");
}

module_init(tuc_init);
module_exit(tuc_exit);

MODULE_DESCRIPTION("Test Update Capsule Module");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sam Protsenko <semen.protsenko@linaro.org>");