summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-kernel/linux/linux-aspeed/0104-Add-chip-unique-id-reading-interface.patch
blob: f366287f1c3fa58c14a3c6a292f6862da257293e (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
From 61fd1c976a0867deec8607183849969e2d96aef7 Mon Sep 17 00:00:00 2001
From: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Date: Fri, 27 Mar 2020 14:42:05 -0700
Subject: [PATCH] Add chip unique id reading interface

This commit adds an interface for reading chip unique id value.
Optionally, the id can be encrypted using a dts-supplied hash data.

Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
---
 drivers/soc/aspeed/aspeed-bmc-misc.c | 118 ++++++++++++++++++++++++---
 1 file changed, 105 insertions(+), 13 deletions(-)

diff --git a/drivers/soc/aspeed/aspeed-bmc-misc.c b/drivers/soc/aspeed/aspeed-bmc-misc.c
index 04d97ab17274..c80b2e71f254 100644
--- a/drivers/soc/aspeed/aspeed-bmc-misc.c
+++ b/drivers/soc/aspeed/aspeed-bmc-misc.c
@@ -7,15 +7,18 @@
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
 #include <linux/mfd/syscon.h>
+#include <crypto/hash.h>
 
 #define DEVICE_NAME "aspeed-bmc-misc"
 
 struct aspeed_bmc_ctrl {
 	const char *name;
 	u32 offset;
-	u32 mask;
+	u64 mask;
 	u32 shift;
 	bool read_only;
+	u32 reg_width;
+	const char *hash_data;
 	struct regmap *map;
 	struct kobj_attribute attr;
 };
@@ -31,6 +34,7 @@ static int aspeed_bmc_misc_parse_dt_child(struct device_node *child,
 					  struct aspeed_bmc_ctrl *ctrl)
 {
 	int rc;
+	u32 mask;
 
 	/* Example child:
 	 *
@@ -38,6 +42,7 @@ static int aspeed_bmc_misc_parse_dt_child(struct device_node *child,
 	 *     offset = <0x80>;
 	 *     bit-mask = <0x1>;
 	 *     bit-shift = <6>;
+	 *     reg-width = <64>;
 	 *     label = "foo";
 	 * }
 	 */
@@ -48,9 +53,22 @@ static int aspeed_bmc_misc_parse_dt_child(struct device_node *child,
 	if (rc < 0)
 		return rc;
 
-	rc = of_property_read_u32(child, "bit-mask", &ctrl->mask);
-	if (rc < 0)
-		return rc;
+	/* optional reg-width, default to 32 */
+	rc = of_property_read_u32(child, "reg-width", &ctrl->reg_width);
+	if (rc < 0 || ctrl->reg_width != 64)
+		ctrl->reg_width = 32;
+
+	if (ctrl->reg_width == 32) {
+		rc = of_property_read_u32(child, "bit-mask", &mask);
+		if (rc < 0)
+			return rc;
+		ctrl->mask = mask;
+	} else {
+		rc = of_property_read_u64(child, "bit-mask", &ctrl->mask);
+		if (rc < 0)
+			return rc;
+	}
+	ctrl->mask <<= ctrl->shift;
 
 	rc = of_property_read_u32(child, "bit-shift", &ctrl->shift);
 	if (rc < 0)
@@ -58,7 +76,9 @@ static int aspeed_bmc_misc_parse_dt_child(struct device_node *child,
 
 	ctrl->read_only = of_property_read_bool(child, "read-only");
 
-	ctrl->mask <<= ctrl->shift;
+	/* optional hash_data for obfuscating reads */
+	if (of_property_read_string(child, "hash-data", &ctrl->hash_data))
+		ctrl->hash_data = NULL;
 
 	return 0;
 }
@@ -88,22 +108,94 @@ static int aspeed_bmc_misc_parse_dt(struct aspeed_bmc_misc *bmc,
 	return 0;
 }
 
+#define SHA256_DIGEST_LEN 32
+static int hmac_sha256(u8 *key, u8 ksize, const char *plaintext, u8 psize,
+		u8 *output)
+{
+	struct crypto_shash *tfm;
+	struct shash_desc *shash;
+	int ret;
+
+	if (!ksize)
+		return -EINVAL;
+
+	tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
+	if (IS_ERR(tfm)) {
+		return -ENOMEM;
+	}
+
+	ret = crypto_shash_setkey(tfm, key, ksize);
+	if (ret)
+		goto failed;
+
+	shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm), GFP_KERNEL);
+	if (!shash) {
+		ret = -ENOMEM;
+		goto failed;
+	}
+
+	shash->tfm = tfm;
+	ret = crypto_shash_digest(shash, plaintext, psize, output);
+
+	kfree(shash);
+
+failed:
+	crypto_free_shash(tfm);
+	return ret;
+}
+
 static ssize_t aspeed_bmc_misc_show(struct kobject *kobj,
 				    struct kobj_attribute *attr, char *buf)
 {
 	struct aspeed_bmc_ctrl *ctrl;
-	unsigned int val;
+	u32 val;
+	u64 val64;
 	int rc;
+	u8 *binbuf;
+	size_t buf_len;
+	u8 hashbuf[SHA256_DIGEST_LEN];
 
 	ctrl = container_of(attr, struct aspeed_bmc_ctrl, attr);
+
+	if (ctrl->reg_width == 32) {
+		rc = regmap_read(ctrl->map, ctrl->offset, &val);
+		if (rc)
+			return rc;
+		val &= (u32)ctrl->mask;
+		val >>= ctrl->shift;
+
+		return sprintf(buf, "%u\n", val);
+	}
 	rc = regmap_read(ctrl->map, ctrl->offset, &val);
 	if (rc)
 		return rc;
+	val64 = val;
+	rc = regmap_read(ctrl->map, ctrl->offset + sizeof(u32), &val);
+	if (rc)
+		return rc;
+	/* aspeed puts 64-bit regs as L, H in address space */
+	val64 |= (u64)val << 32;
+	val64 &= ctrl->mask;
+	val64 >>= ctrl->shift;
+	buf_len = sizeof(val64);
+
+	if (ctrl->hash_data) {
+		rc = hmac_sha256((u8*)&val64, buf_len, ctrl->hash_data,
+				strlen(ctrl->hash_data), hashbuf);
+		if (rc)
+			return rc;
+		buf_len = SHA256_DIGEST_LEN;
+		binbuf = hashbuf;
+	} else {
+		binbuf = (u8*)&val64;
+		buf_len = sizeof(val64);
+	}
+	bin2hex(buf, binbuf, buf_len);
+	buf[buf_len * 2] = '\n';
+	rc = buf_len * 2 + 1;
 
-	val &= ctrl->mask;
-	val >>= ctrl->shift;
+	return rc;
 
-	return sprintf(buf, "%u\n", val);
 }
 
 static ssize_t aspeed_bmc_misc_store(struct kobject *kobj,
@@ -114,15 +206,15 @@ static ssize_t aspeed_bmc_misc_store(struct kobject *kobj,
 	long val;
 	int rc;
 
-	rc = kstrtol(buf, 0, &val);
-	if (rc)
-		return rc;
-
 	ctrl = container_of(attr, struct aspeed_bmc_ctrl, attr);
 
 	if (ctrl->read_only)
 		return -EROFS;
 
+	rc = kstrtol(buf, 0, &val);
+	if (rc)
+		return rc;
+
 	val <<= ctrl->shift;
 	rc = regmap_update_bits(ctrl->map, ctrl->offset, ctrl->mask, val);
 
-- 
2.17.1