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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
/*
* Exynos FMP driver
*
* Copyright (C) 2015 Samsung Electronics Co., Ltd.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*/
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/smc.h>
#include <linux/crypto.h>
#include <crypto/fmp.h>
#include "fmp_lib.h"
#include "fmp_fips_main.h"
#include "fmp_dev.h"
#include "fmp_derive_iv.h"
#include "fmp_version.h"
#include "fmp_fips_func_test.h"
static LIST_HEAD(fmp_devices);
#define EXYNOS_HOST_LABEL "exynos-host"
#define EXYNOS_HOST_TYPE_LABEL "exynos,host-type"
static inline int is_set_fmp_disk_key(struct exynos_fmp *fmp)
{
return (fmp->status_disk_key == KEY_SET) ? TRUE : FALSE;
}
static inline int is_stored_fmp_disk_key(struct exynos_fmp *fmp)
{
return (fmp->status_disk_key == KEY_STORED) ? TRUE : FALSE;
}
static int fmpdev_set_disk_cfg(struct exynos_fmp *fmp,
struct fmp_data_setting *data,
enum fmp_crypto_enc_mode enc_mode)
{
int ret = 0;
struct fmp_table_setting *table = data->table;
struct fmp_crypto_setting *crypto = &data->disk;
ret = fmplib_set_algo_mode(fmp, table, crypto, enc_mode,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP encryption mode\n",
__func__);
goto err;
}
if (crypto->algo_mode == EXYNOS_FMP_BYPASS_MODE)
return 0;
if (is_stored_fmp_disk_key(fmp)) {
exynos_smc(SMC_CMD_FMP_DISK_KEY_SET, 0, 0, 0);
fmp->status_disk_key = KEY_SET;
} else if (!is_set_fmp_disk_key(fmp)) {
dev_err(fmp->dev, "%s: Cannot configure FMP because disk key is clear\n",
__func__);
ret = -EINVAL;
goto err;
}
ret = fmplib_set_key_size(fmp, table, crypto, enc_mode,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP key size\n", __func__);
goto err;
}
ret = fmplib_derive_iv(fmp, crypto, enc_mode);
if (ret) {
dev_err(fmp->dev, "%s: Fail to derive FMP IV\n", __func__);
goto err;
}
ret = fmplib_set_iv(fmp, table, crypto, enc_mode);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP IV\n", __func__);
goto err;
}
err:
return ret;
}
static int fmpdev_set_file_cfg(struct exynos_fmp *fmp,
struct fmp_data_setting *data,
enum fmp_crypto_enc_mode enc_mode)
{
int ret = 0;
struct fmp_table_setting *table = data->table;
struct fmp_crypto_setting *crypto = &data->file;
ret = fmplib_set_algo_mode(fmp, table, crypto, enc_mode,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP encryption mode\n",
__func__);
goto err;
}
if (crypto->algo_mode == EXYNOS_FMP_BYPASS_MODE)
return 0;
ret = fmplib_set_key(fmp, table, crypto, enc_mode);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP key\n", __func__);
goto err;
}
ret = fmplib_set_key_size(fmp, table, crypto, enc_mode,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP key size\n", __func__);
goto err;
}
ret = fmplib_derive_iv(fmp, crypto, enc_mode);
if (ret) {
dev_err(fmp->dev, "%s: Fail to derive FMP IV\n", __func__);
goto err;
}
ret = fmplib_set_iv(fmp, table, crypto, enc_mode);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP IV\n", __func__);
goto err;
}
err:
return ret;
}
static int fmpdev_set_test_cfg(struct exynos_fmp *fmp,
struct fmp_data_setting *data)
{
int ret = 0;
struct fmp_crypto_setting *crypto;
if (!fmp->fips_data) {
dev_err(fmp->dev, "%s: Invalid test data\n", __func__);
goto err;
}
crypto = &fmp->fips_data->crypto;
if (!data || !data->table) {
dev_err(fmp->dev, "%s: Invalid data or table data\n", __func__);
goto err;
}
ret = fmplib_set_algo_mode(fmp, data->table, crypto, EXYNOS_FMP_FILE_ENC,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP encryption mode\n",
__func__);
goto err;
}
if (crypto->algo_mode == EXYNOS_FMP_BYPASS_MODE)
return 0;
ret = fmplib_clear(fmp, data->table);
if (ret) {
pr_err("%s: Fail to clear FMP table\n", __func__);
goto err;
}
ret = fmplib_set_key(fmp, data->table, crypto, EXYNOS_FMP_FILE_ENC);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP key\n", __func__);
goto err;
}
ret = fmplib_set_key_size(fmp, data->table, crypto, EXYNOS_FMP_FILE_ENC,
data->cmdq_enabled);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP key size\n", __func__);
goto err;
}
ret = fmplib_set_iv(fmp, data->table, crypto, EXYNOS_FMP_FILE_ENC);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP IV\n", __func__);
goto err;
}
err:
return ret;
}
static int exynos_fmp_config(struct platform_device *pdev,
struct fmp_data_setting *data)
{
int ret = 0;
struct exynos_fmp *fmp;
if (!pdev || !data) {
pr_err("%s: Invalid fmp pdev or data\n", __func__);
ret = -EINVAL;
goto err;
}
fmp = dev_get_drvdata(&pdev->dev);
if (!fmp) {
pr_err("%s: Invalid fmp driver platform data\n", __func__);
ret = -ENODEV;
goto err;
}
if (unlikely(in_fmp_fips_err())) {
dev_err(fmp->dev, "%s: Fail to work fmp config due to fips in error.\n",
__func__);
return -EPERM;
}
if (fmp->test_mode) {
ret = fmpdev_set_test_cfg(fmp, data);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP test configuration\n",
__func__);
ret = -EPERM;
goto err;
} else
goto err;
}
ret = fmpdev_set_file_cfg(fmp, data, EXYNOS_FMP_FILE_ENC);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP file configuration\n",
__func__);
ret = -EPERM;
goto err;
}
ret = fmpdev_set_disk_cfg(fmp, data, EXYNOS_FMP_DISK_ENC);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP disk configuration\n",
__func__);
ret = -EPERM;
goto err;
}
err:
return ret;
}
static int exynos_fmp_set_disk_key(struct platform_device *pdev,
struct fmp_data_setting *data)
{
int ret = 0;
struct device *dev = &pdev->dev;
struct exynos_fmp *fmp;
struct fmp_crypto_setting *crypto;
struct fmp_table_setting *table;
if (!data) {
pr_err("%s: Invalid fmp data\n", __func__);
ret = -EINVAL;
goto err;
}
table = data->table;
crypto = &data->disk;
fmp = dev_get_drvdata(dev);
if (!fmp) {
pr_err("%s: Invalid fmp driver platform data\n", __func__);
ret = -ENODEV;
goto err;
}
ret = fmplib_set_key(fmp, table, crypto, EXYNOS_FMP_DISK_ENC);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP disk key\n", __func__);
goto err;
}
err:
return ret;
}
static int exynos_fmp_clear_disk_key(struct platform_device *pdev)
{
int ret = 0;
struct device *dev = &pdev->dev;
struct exynos_fmp *fmp;
fmp = dev_get_drvdata(dev);
if (!fmp) {
pr_err("%s: Invalid fmp driver platform data\n", __func__);
ret = -ENODEV;
goto err;
}
ret = fmplib_clear_disk_key(fmp);
if (ret) {
dev_err(fmp->dev, "%s: Fail to clear FMP disk key\n", __func__);
goto err;
}
err:
return ret;
}
static int exynos_fmp_clear(struct platform_device *pdev,
struct fmp_data_setting *data)
{
int ret = 0;
struct device *dev = &pdev->dev;
struct exynos_fmp *fmp;
struct fmp_table_setting *table;
if (!data) {
pr_err("%s: Invalid fmp data\n", __func__);
ret = -EINVAL;
goto err;
}
table = data->table;
fmp = dev_get_drvdata(dev);
if (!fmp) {
pr_err("%s: Invalid fmp driver platform data\n", __func__);
ret = -ENODEV;
goto err;
}
ret = fmplib_clear(fmp, table);
if (ret) {
dev_err(fmp->dev, "%s: Fail to clear FMP disk key\n", __func__);
goto err;
}
err:
return ret;
}
const struct exynos_fmp_variant_ops exynos_fmp_ops = {
.name = "exynos",
.config = exynos_fmp_config,
.set_disk_key = exynos_fmp_set_disk_key,
.clear_disk_key = exynos_fmp_clear_disk_key,
.clear = exynos_fmp_clear,
};
static struct of_device_id exynos_fmp_match[] = {
{
.compatible = "samsung,exynos-fmp",
.data = (void *)&exynos_fmp_ops,
},
{},
};
MODULE_DEVICE_TABLE(of, exynos_fmp_match);
struct platform_device *exynos_fmp_get_pdevice(struct device_node *node)
{
struct platform_device *fmp_pdev = NULL;
struct exynos_fmp *fmp = NULL;
if (!node) {
pr_err("%s: Invalid node\n", __func__);
goto out;
}
if (!of_device_is_available(node)) {
pr_err("%s: Unavailable device\n", __func__);
goto out;
}
if (list_empty(&fmp_devices)) {
pr_err("%s: Invalie device list\n", __func__);
fmp_pdev = ERR_PTR(-EPROBE_DEFER);
goto out;
}
list_for_each_entry(fmp, &fmp_devices, list) {
if (fmp->dev->of_node == node) {
pr_info("%s: Found FMP device\n", __func__);
break;
}
}
fmp_pdev = to_platform_device(fmp->dev);
pr_info("%s: Matching platform device\n", __func__);
out:
return fmp_pdev;
}
EXPORT_SYMBOL(exynos_fmp_get_pdevice);
struct exynos_fmp_variant_ops *exynos_fmp_get_variant_ops(struct device_node *node)
{
if (node) {
const struct of_device_id *match;
match = of_match_node(exynos_fmp_match, node);
if (match)
return (struct exynos_fmp_variant_ops *)(match->data);
pr_err("%s, Error matching\n", __func__);
} else {
pr_err("%s, Invalid node\n", __func__);
}
return NULL;
}
EXPORT_SYMBOL(exynos_fmp_get_variant_ops);
static struct platform_device *exynos_fmp_host_get_pdevice(struct device *dev)
{
struct device_node *node;
struct platform_device *host_pdev = NULL;
node = of_parse_phandle(dev->of_node, EXYNOS_HOST_LABEL, 0);
if (!node) {
dev_err(dev, "%s: exynos-host property not specified\n",
__func__);
goto out;
}
host_pdev = of_find_device_by_node(node);
out:
return host_pdev;
}
static int exynos_fmp_get_host_type(struct platform_device *pdev,
struct exynos_fmp *fmp)
{
int ret = 0;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
const char *type;
ret = of_property_read_string_index(node, EXYNOS_HOST_TYPE_LABEL, 0, &type);
if (ret) {
dev_err(fmp->dev, "%s: Could not get FMP host type\n", __func__);
ret = -EINVAL;
goto err;
}
strlcpy(fmp->host_type, type, FMP_HOST_TYPE_NAME_LEN);
err:
return ret;
}
static int exynos_fmp_host_get_dev(struct platform_device *pdev)
{
int ret = 0;
struct exynos_fmp *fmp;
struct platform_device *host_pdev;
struct exynos_fmp_variant_ops *fmp_vops;
fmp = dev_get_drvdata(&pdev->dev);
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp context or device\n", __func__);
ret = -EINVAL;
goto err;
}
host_pdev = exynos_fmp_host_get_pdevice(fmp->dev);
fmp_vops = exynos_fmp_get_variant_ops(fmp->dev->of_node);
if (host_pdev == ERR_PTR(-EPROBE_DEFER)) {
dev_err(fmp->dev, "%s: Host device not probed yet\n", __func__);
ret = -EPROBE_DEFER;
goto err;
}
if (!host_pdev || !fmp_vops) {
dev_err(fmp->dev, "%s: invalid platform device %p or vops %p\n",
__func__, host_pdev, fmp_vops);
ret = -ENODEV;
goto err;
}
ret = exynos_fmp_get_host_type(pdev, fmp);
if (ret) {
dev_err(fmp->dev, "%s: invalid Host type. ret(%d)\n", __func__, ret);
goto err;
}
fmp->host_pdev = host_pdev;
if (!strncmp(fmp->host_type, "ufs", sizeof("ufs")))
return exynos_ufs_fmp_host_set_device(host_pdev, pdev, fmp_vops);
else if (!strncmp(fmp->host_type, "mmc", sizeof("mmc")))
return exynos_mmc_fmp_host_set_device(host_pdev, pdev, fmp_vops);
dev_err(fmp->dev, "%s: Not matched Host type(%s)\n", __func__,
fmp->host_type);
ret = -EINVAL;
err:
return ret;
}
static int exynos_fmp_probe(struct platform_device *pdev)
{
int ret = 0;
struct exynos_fmp *fmp;
struct device *dev = &pdev->dev;
if (!pdev) {
pr_err("%s: Invalid platform_device.\n", __func__);
ret = -EINVAL;
goto err_pdev;
}
fmp = kzalloc(sizeof(struct exynos_fmp), GFP_KERNEL);
if (!fmp) {
ret = -ENOMEM;
goto err_mem;
}
fmp->dev = &pdev->dev;
if (!fmp->dev) {
pr_err("%s: Invalid device.\n", __func__);
ret = -EINVAL;
goto err_dev;
}
fmp->status_disk_key = KEY_CLEAR;
dev_set_drvdata(dev, fmp);
list_add_tail(&fmp->list, &fmp_devices);
ret = exynos_fmp_host_get_dev(pdev);
if (ret == -EPROBE_DEFER) {
dev_err(fmp->dev, "%s: Host device not proved yet. ret = %d\n",
__func__, ret);
goto err_dev;
} else if (ret) {
dev_err(fmp->dev, "%s: Fail to get Host device. ret = %d\n",
__func__, ret);
goto err_dev;
}
dev_info(fmp->dev, "Exynos FMP Version: %s\n", FMP_DRV_VERSION);
ret = exynos_fmp_func_test_KAT_case(pdev, fmp,
exynos_fmp_get_variant_ops(fmp->dev->of_node));
if (ret) {
dev_err(fmp->dev, "%s: Fail to test KAT case. ret(%d)",
__func__, ret);
goto err_dev;
}
ret = exynos_fmp_fips_init(fmp);
if (ret) {
dev_err(fmp->dev, "%s: Fail to initialize fmp fips. ret(%d)",
__func__, ret);
exynos_fmp_fips_exit(fmp);
goto err_dev;
}
dev_info(fmp->dev, "%s: Exynos FMP driver is proved\n", __func__);
return ret;
err_dev:
kzfree(fmp);
err_mem:
err_pdev:
return ret;
}
static int exynos_fmp_remove(struct platform_device *pdev)
{
struct exynos_fmp *fmp;
struct device *dev = &pdev->dev;
fmp = dev_get_drvdata(dev);
if (!fmp)
return 0;
exynos_fmp_fips_exit(fmp);
kzfree(fmp);
return 0;
}
static struct platform_driver exynos_fmp_driver = {
.driver = {
.name = "exynos-fmp",
.owner = THIS_MODULE,
.pm = NULL,
.of_match_table = exynos_fmp_match,
},
.probe = exynos_fmp_probe,
.remove = exynos_fmp_remove,
};
static int __init fmp_init(void)
{
return platform_driver_register(&exynos_fmp_driver);
}
late_initcall(fmp_init);
static void __exit fmp_exit(void)
{
platform_driver_unregister(&exynos_fmp_driver);
}
module_exit(fmp_exit);
MODULE_DESCRIPTION("Exynos Spedific FMP(Flash Memory Protector) driver");
|