summaryrefslogtreecommitdiff
path: root/drivers/firmware/coreboot/coreboot-cfr.c
blob: 2d9ea5d213d8242d610bd0d50ef8eca8bc3fb3ec (plain) (blame)
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
// SPDX-License-Identifier: GPL-2.0-only
/*
 * coreboot CFR firmware attributes driver.
 *
 * Parses LB_TAG_CFR_ROOT records from the coreboot table and exposes
 * runtime EFI variable-backed options through the firmware-attributes class.
 */

#include <linux/array_size.h>
#include <linux/bitops.h>
#include <linux/cleanup.h>
#include <linux/compiler_attributes.h>
#include <linux/container_of.h>
#include <linux/crc32.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/device-id/coreboot.h>
#include <linux/efi.h>
#include <linux/err.h>
#include <linux/firmware_attributes.h>
#include <linux/io.h>
#include <linux/kdev_t.h>
#include <linux/kobject.h>
#include <linux/kstrtox.h>
#include <linux/limits.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/types.h>

#include "coreboot_table.h"

#define DRIVER_NAME "coreboot-cfr"

#define LB_TAG_CFR_ROOT		0x47
#define CFR_VERSION		0

enum cfr_tags {
	CFR_TAG_OPTION_FORM		= 1,
	CFR_TAG_ENUM_VALUE		= 2,
	CFR_TAG_OPTION_ENUM		= 3,
	CFR_TAG_OPTION_NUMBER		= 4,
	CFR_TAG_OPTION_BOOL		= 5,
	CFR_TAG_VARCHAR_OPT_NAME	= 7,
	CFR_TAG_VARCHAR_UI_NAME		= 8,
	CFR_TAG_RUNTIME_APPLY		= 13,
};

enum cfr_option_flags {
	CFR_OPTFLAG_READONLY	= BIT(0),
	CFR_OPTFLAG_INACTIVE	= BIT(1),
	CFR_OPTFLAG_SUPPRESS	= BIT(2),
	CFR_OPTFLAG_VOLATILE	= BIT(3),
	CFR_OPTFLAG_RUNTIME	= BIT(4),
};

enum cfr_runtime_apply_method {
	CFR_RUNTIME_APPLY_NONE		= 0,
	CFR_RUNTIME_APPLY_APM_CNT	= 1,
};

struct lb_cfr {
	u32 tag;
	u32 size;
	u32 version;
	u32 checksum;
} __packed;

struct lb_cfr_varbinary {
	u32 tag;
	u32 size;
	u32 data_length;
} __packed;

struct lb_cfr_enum_value {
	u32 tag;
	u32 size;
	u32 value;
} __packed;

struct lb_cfr_runtime_apply {
	u32 tag;
	u32 size;
	u32 method;
	u32 id;
} __packed;

struct lb_cfr_numeric_option {
	u32 tag;
	u32 size;
	cb_u64 object_id;
	cb_u64 dependency_id;
	u32 flags;
	u32 default_value;
	u32 min;
	u32 max;
	u32 step;
	u32 display_flags;
} __packed;

struct lb_cfr_option_form {
	u32 tag;
	u32 size;
	cb_u64 object_id;
	cb_u64 dependency_id;
	u32 flags;
} __packed;

#define COREBOOT_CFR_OPT_SKIP_FLAGS \
	(CFR_OPTFLAG_SUPPRESS | CFR_OPTFLAG_VOLATILE)

#define COREBOOT_CFR_OPT_READ_ONLY_FLAGS \
	(CFR_OPTFLAG_READONLY | CFR_OPTFLAG_INACTIVE)

#define COREBOOT_CFR_EFI_ATTRS \
	(EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | \
	 EFI_VARIABLE_RUNTIME_ACCESS)

#define COREBOOT_CFR_APM_CNT_PORT	0xb2
#define COREBOOT_CFR_APM_STS_PORT	0xb3
#define COREBOOT_CFR_APM_APPLY_CMD	0xe3
#define COREBOOT_CFR_MAX_FORM_DEPTH	16

static efi_guid_t coreboot_cfr_guid = EFI_GUID(0xceae4c1d, 0x335b, 0x4685,
					       0xa4, 0xa0, 0xfc, 0x4a,
					       0x94, 0xee, 0xa0, 0x85);

enum coreboot_cfr_setting_type {
	COREBOOT_CFR_SETTING_ENUM,
	COREBOOT_CFR_SETTING_NUMBER,
	COREBOOT_CFR_SETTING_BOOL,
};

struct coreboot_cfr_enum {
	char *label;
	u32 value;
};

struct coreboot_cfr_setting {
	struct kobject kobj;
	struct list_head node;
	struct coreboot_cfr_drvdata *drvdata;
	enum coreboot_cfr_setting_type type;
	char *name;
	char *display_name;
	struct coreboot_cfr_enum *values;
	unsigned int n_values;
	u32 default_value;
	u32 min;
	u32 max;
	u32 step;
	u32 runtime_apply_method;
	u32 runtime_apply_id;
	bool read_only;
};

struct coreboot_cfr_drvdata {
	struct device *class_dev;
	struct kset *attrs_kset;
	struct list_head settings;
	/* Serializes EFI variable writes and the matching runtime apply hook. */
	struct mutex lock;
	bool efi_writes_supported;
	bool pending_reboot;
};

struct coreboot_cfr_iterator {
	const u8 *cursor;
	size_t remaining;
};

static struct coreboot_cfr_setting *to_coreboot_cfr_setting(struct kobject *kobj)
{
	return container_of(kobj, struct coreboot_cfr_setting, kobj);
}

static bool coreboot_cfr_string_is_valid_name(const char *name)
{
	return name && name[0] && !strchr(name, '/') &&
		strcmp(name, "pending_reboot") != 0;
}

static bool coreboot_cfr_string_is_valid_label(const char *label)
{
	return label && !strchr(label, ';') && !strchr(label, '\n');
}

static char *coreboot_cfr_string_dup(const struct lb_cfr_varbinary *str)
{
	const char *data = (const char *)(str + 1);
	size_t len = str->data_length;

	if (len && !data[len - 1])
		len--;

	return kmemdup_nul(data, len, GFP_KERNEL);
}

static const struct coreboot_table_entry *
coreboot_cfr_next_entry(struct coreboot_cfr_iterator *iterator)
{
	const struct coreboot_table_entry *entry;

	if (!iterator->remaining)
		return NULL;

	if (iterator->remaining < sizeof(*entry))
		return ERR_PTR(-EINVAL);

	entry = (const struct coreboot_table_entry *)iterator->cursor;
	if (entry->size < sizeof(*entry) || entry->size > iterator->remaining)
		return ERR_PTR(-EINVAL);

	iterator->cursor += entry->size;
	iterator->remaining -= entry->size;

	return entry;
}

static const struct coreboot_table_entry *
coreboot_cfr_child_entry(const void *base, size_t len, u32 tag)
{
	struct coreboot_cfr_iterator iterator = {
		.cursor = base,
		.remaining = len,
	};
	const struct coreboot_table_entry *entry;

	for (;;) {
		entry = coreboot_cfr_next_entry(&iterator);
		if (IS_ERR_OR_NULL(entry))
			return entry;

		if (entry->tag == tag)
			return entry;
	}
}

static const struct lb_cfr_varbinary *
coreboot_cfr_child_string(const void *base, size_t len, u32 tag)
{
	const struct lb_cfr_varbinary *str;
	const struct coreboot_table_entry *entry;

	entry = coreboot_cfr_child_entry(base, len, tag);
	if (IS_ERR(entry))
		return ERR_CAST(entry);
	if (!entry)
		return NULL;

	if (entry->size < sizeof(*str))
		return ERR_PTR(-EINVAL);

	str = (const struct lb_cfr_varbinary *)entry;
	if (str->data_length > entry->size - sizeof(*str))
		return ERR_PTR(-EINVAL);

	return str;
}

static const struct lb_cfr_runtime_apply *
coreboot_cfr_child_runtime_apply(const void *base, size_t len)
{
	const struct lb_cfr_runtime_apply *runtime_apply;
	const struct coreboot_table_entry *entry;

	entry = coreboot_cfr_child_entry(base, len, CFR_TAG_RUNTIME_APPLY);
	if (IS_ERR(entry))
		return ERR_CAST(entry);
	if (!entry)
		return NULL;

	if (entry->size < sizeof(*runtime_apply))
		return ERR_PTR(-EINVAL);

	runtime_apply = (const struct lb_cfr_runtime_apply *)entry;
	if (runtime_apply->method == CFR_RUNTIME_APPLY_APM_CNT &&
	    runtime_apply->id > U8_MAX)
		return ERR_PTR(-EINVAL);

	return runtime_apply;
}

static efi_char16_t *coreboot_cfr_efi_name(const char *name)
{
	size_t len, i;

	len = strlen(name);
	if (len >= EFI_VAR_NAME_LEN)
		return ERR_PTR(-ENAMETOOLONG);

	efi_char16_t *efi_name __free(kfree) =
		kcalloc(len + 1, sizeof(*efi_name), GFP_KERNEL);
	if (!efi_name)
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < len; i++) {
		if (!isascii(name[i]))
			return ERR_PTR(-EINVAL);
		efi_name[i] = name[i];
	}

	return no_free_ptr(efi_name);
}

/* Caller must hold the efivar lock. */
static int coreboot_cfr_read_efi_value(efi_char16_t *efi_name, u32 *value,
				       u32 *attrs)
{
	unsigned long size = sizeof(__le32);
	efi_status_t status;
	__le32 data;
	u32 attr;

	status = efivar_get_variable(efi_name, &coreboot_cfr_guid, &attr,
				     &size, &data);
	if (status != EFI_SUCCESS)
		return efi_status_to_err(status);

	if (size != sizeof(data))
		return -EINVAL;

	if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS))
		return -EOPNOTSUPP;

	*value = le32_to_cpu(data);
	if (attrs)
		*attrs = attr;

	return 0;
}

static int coreboot_cfr_read_value(const struct coreboot_cfr_setting *setting,
				   u32 *value, u32 *attrs)
{
	efi_char16_t *efi_name __free(kfree) =
		coreboot_cfr_efi_name(setting->name);
	int ret;

	if (IS_ERR(efi_name))
		return PTR_ERR(no_free_ptr(efi_name));

	ret = efivar_lock();
	if (ret)
		return ret;

	ret = coreboot_cfr_read_efi_value(efi_name, value, attrs);
	efivar_unlock();

	return ret;
}

/* Caller must hold the efivar lock. */
static int coreboot_cfr_write_efi_value(efi_char16_t *efi_name, u32 value,
					u32 attrs)
{
	efi_status_t status;
	__le32 data;

	if (!efivar_supports_writes())
		return -EROFS;

	data = cpu_to_le32(value);
	status = efivar_set_variable_locked(efi_name, &coreboot_cfr_guid, attrs,
					    sizeof(data), &data, false);
	if (status != EFI_SUCCESS)
		return efi_status_to_err(status);

	return 0;
}

static int coreboot_cfr_apply_runtime(struct coreboot_cfr_setting *setting)
{
	u8 status;

	if (setting->runtime_apply_method != CFR_RUNTIME_APPLY_APM_CNT)
		return -EOPNOTSUPP;

	outb((u8)setting->runtime_apply_id, COREBOOT_CFR_APM_STS_PORT);
	outb(COREBOOT_CFR_APM_APPLY_CMD, COREBOOT_CFR_APM_CNT_PORT);
	status = inb(COREBOOT_CFR_APM_STS_PORT);
	if (status)
		return -EIO;

	return 0;
}

static int coreboot_cfr_write_value(struct coreboot_cfr_setting *setting,
				    u32 value)
{
	efi_char16_t *efi_name;
	u32 attrs;
	u32 old;
	int restore_ret;
	int ret;

	if (setting->read_only)
		return -EACCES;

	efi_name = coreboot_cfr_efi_name(setting->name);
	if (IS_ERR(efi_name))
		return PTR_ERR(efi_name);

	mutex_lock(&setting->drvdata->lock);

	ret = efivar_lock();
	if (ret)
		goto out_unlock_mutex;

	ret = coreboot_cfr_read_efi_value(efi_name, &old, &attrs);
	if (ret)
		goto out_unlock_efi;

	if ((attrs & COREBOOT_CFR_EFI_ATTRS) != COREBOOT_CFR_EFI_ATTRS) {
		ret = -EOPNOTSUPP;
		goto out_unlock_efi;
	}

	if (old == value)
		goto out_unlock_efi;

	ret = coreboot_cfr_write_efi_value(efi_name, value, attrs);
	if (ret)
		goto out_unlock_efi;

	ret = coreboot_cfr_apply_runtime(setting);
	if (ret == -EOPNOTSUPP) {
		/* EFI changed; firmware will consume it after reboot. */
		setting->drvdata->pending_reboot = true;
		ret = 0;
	} else if (ret) {
		restore_ret = coreboot_cfr_write_efi_value(efi_name, old, attrs);
		if (restore_ret) {
			setting->drvdata->pending_reboot = true;
			ret = restore_ret;
		} else {
			goto out_unlock_efi;
		}
	}

	efivar_unlock();
	kobject_uevent(&setting->drvdata->class_dev->kobj, KOBJ_CHANGE);
	mutex_unlock(&setting->drvdata->lock);
	kfree(efi_name);
	return ret;

out_unlock_efi:
	efivar_unlock();
out_unlock_mutex:
	mutex_unlock(&setting->drvdata->lock);
	kfree(efi_name);
	return ret;
}

static const char *
coreboot_cfr_label_from_value(const struct coreboot_cfr_setting *setting,
			      u32 value)
{
	unsigned int i;

	for (i = 0; i < setting->n_values; i++) {
		if (setting->values[i].value == value)
			return setting->values[i].label;
	}

	return NULL;
}

static int coreboot_cfr_parse_value(struct coreboot_cfr_setting *setting,
				    const char *label, u32 *value_out)
{
	unsigned int i;

	for (i = 0; i < setting->n_values; i++) {
		if (!sysfs_streq(label, setting->values[i].label))
			continue;

		*value_out = setting->values[i].value;
		return 0;
	}

	return kstrtou32(label, 0, value_out);
}

static bool coreboot_cfr_value_is_valid(struct coreboot_cfr_setting *setting,
					u32 value)
{
	u32 delta;

	if (setting->type != COREBOOT_CFR_SETTING_NUMBER) {
		/* Enum and bool values are valid only if they have a label. */
		return coreboot_cfr_label_from_value(setting, value);
	}

	if (value < setting->min || value > setting->max)
		return false;

	if (!setting->step)
		return true;

	delta = value - setting->min;
	return delta % setting->step == 0;
}

static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	if (setting->type == COREBOOT_CFR_SETTING_NUMBER)
		return sysfs_emit(buf, "integer\n");

	return sysfs_emit(buf, "enumeration\n");
}

static ssize_t display_name_language_code_show(struct kobject *kobj,
					       struct kobj_attribute *attr,
					       char *buf)
{
	return sysfs_emit(buf, "en_US.UTF-8\n");
}

static ssize_t display_name_show(struct kobject *kobj,
				 struct kobj_attribute *attr, char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	return sysfs_emit(buf, "%s\n", setting->display_name);
}

static ssize_t possible_values_show(struct kobject *kobj,
				    struct kobj_attribute *attr, char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);
	ssize_t len = 0;
	unsigned int i;

	for (i = 0; i < setting->n_values; i++) {
		len += sysfs_emit_at(buf, len, "%s%s", i ? ";" : "",
				     setting->values[i].label);
	}

	len += sysfs_emit_at(buf, len, "\n");
	return len;
}

static ssize_t min_value_show(struct kobject *kobj, struct kobj_attribute *attr,
			      char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	return sysfs_emit(buf, "%u\n", setting->min);
}

static ssize_t max_value_show(struct kobject *kobj, struct kobj_attribute *attr,
			      char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	return sysfs_emit(buf, "%u\n", setting->max);
}

static ssize_t scalar_increment_show(struct kobject *kobj,
				     struct kobj_attribute *attr, char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	return sysfs_emit(buf, "%u\n", setting->step);
}

static ssize_t default_value_show(struct kobject *kobj,
				  struct kobj_attribute *attr, char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);
	const char *label;

	if (setting->type == COREBOOT_CFR_SETTING_NUMBER)
		return sysfs_emit(buf, "%u\n", setting->default_value);

	label = coreboot_cfr_label_from_value(setting, setting->default_value);
	if (!label)
		return sysfs_emit(buf, "%u\n", setting->default_value);

	return sysfs_emit(buf, "%s\n", label);
}

static ssize_t current_value_show(struct kobject *kobj,
				  struct kobj_attribute *attr, char *buf)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);
	const char *label;
	u32 value;
	int ret;

	ret = coreboot_cfr_read_value(setting, &value, NULL);
	if (ret)
		return ret;

	if (setting->type == COREBOOT_CFR_SETTING_NUMBER)
		return sysfs_emit(buf, "%u\n", value);

	label = coreboot_cfr_label_from_value(setting, value);
	if (!label)
		return -EINVAL;

	return sysfs_emit(buf, "%s\n", label);
}

static ssize_t current_value_store(struct kobject *kobj,
				   struct kobj_attribute *attr,
				   const char *buf, size_t count)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);
	u32 value;
	int ret;

	ret = coreboot_cfr_parse_value(setting, buf, &value);
	if (ret)
		return ret;

	if (!coreboot_cfr_value_is_valid(setting, value))
		return -EINVAL;

	ret = coreboot_cfr_write_value(setting, value);
	if (ret)
		return ret;

	return count;
}

static struct kobj_attribute type_attr = __ATTR_RO(type);
static struct kobj_attribute display_name_language_code_attr =
	__ATTR_RO(display_name_language_code);
static struct kobj_attribute display_name_attr = __ATTR_RO(display_name);
static struct kobj_attribute possible_values_attr = __ATTR_RO(possible_values);
static struct kobj_attribute min_value_attr = __ATTR_RO(min_value);
static struct kobj_attribute max_value_attr = __ATTR_RO(max_value);
static struct kobj_attribute scalar_increment_attr = __ATTR_RO(scalar_increment);
static struct kobj_attribute default_value_attr = __ATTR_RO(default_value);
static struct kobj_attribute current_value_attr = __ATTR_RW(current_value);

static struct attribute *coreboot_cfr_setting_attrs[] = {
	&type_attr.attr,
	&display_name_language_code_attr.attr,
	&display_name_attr.attr,
	&possible_values_attr.attr,
	&min_value_attr.attr,
	&max_value_attr.attr,
	&scalar_increment_attr.attr,
	&default_value_attr.attr,
	&current_value_attr.attr,
	NULL,
};

static umode_t coreboot_cfr_attr_is_visible(struct kobject *kobj,
					    struct attribute *attr, int n)
{
	struct coreboot_cfr_setting *setting = to_coreboot_cfr_setting(kobj);

	if (setting->type == COREBOOT_CFR_SETTING_NUMBER &&
	    attr == &possible_values_attr.attr)
		return 0;

	if (setting->type != COREBOOT_CFR_SETTING_NUMBER &&
	    (attr == &min_value_attr.attr || attr == &max_value_attr.attr ||
	     attr == &scalar_increment_attr.attr))
		return 0;

	if (setting->read_only && attr == &current_value_attr.attr)
		return 0444;

	return attr->mode;
}

static const struct attribute_group coreboot_cfr_setting_group = {
	.attrs = coreboot_cfr_setting_attrs,
	.is_visible = coreboot_cfr_attr_is_visible,
};

static void coreboot_cfr_free_setting(struct coreboot_cfr_setting *setting)
{
	unsigned int i;

	for (i = 0; i < setting->n_values; i++)
		kfree(setting->values[i].label);

	kfree(setting->values);
	kfree(setting->display_name);
	kfree(setting->name);
	kfree(setting);
}

static void coreboot_cfr_setting_release(struct kobject *kobj)
{
	coreboot_cfr_free_setting(to_coreboot_cfr_setting(kobj));
}

static const struct kobj_type coreboot_cfr_setting_ktype = {
	.release = coreboot_cfr_setting_release,
	.sysfs_ops = &kobj_sysfs_ops,
};

static ssize_t pending_reboot_show(struct kobject *kobj,
				   struct kobj_attribute *attr, char *buf)
{
	struct device *dev = kobj_to_dev(kobj->parent);
	struct coreboot_cfr_drvdata *data;

	data = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", data->pending_reboot);
}

static struct kobj_attribute pending_reboot_attr = __ATTR_RO(pending_reboot);

static int coreboot_cfr_copy_bool_values(struct coreboot_cfr_setting *setting)
{
	static const struct coreboot_cfr_enum bool_values[] = {
		{ .label = "Disabled", .value = 0 },
		{ .label = "Enabled", .value = 1 },
	};
	unsigned int i;

	setting->values = kcalloc(ARRAY_SIZE(bool_values), sizeof(*setting->values),
				  GFP_KERNEL);
	if (!setting->values)
		return -ENOMEM;

	for (i = 0; i < ARRAY_SIZE(bool_values); i++) {
		setting->values[i].label = kstrdup(bool_values[i].label,
						   GFP_KERNEL);
		if (!setting->values[i].label)
			return -ENOMEM;
		setting->values[i].value = bool_values[i].value;
		setting->n_values++;
	}

	return 0;
}

static int coreboot_cfr_count_enum_values(const void *base, size_t len)
{
	struct coreboot_cfr_iterator iterator = {
		.cursor = base,
		.remaining = len,
	};
	const struct coreboot_table_entry *entry;
	int count = 0;

	for (;;) {
		entry = coreboot_cfr_next_entry(&iterator);
		if (IS_ERR(entry))
			return PTR_ERR(entry);
		if (!entry)
			return count;

		if (entry->tag == CFR_TAG_ENUM_VALUE)
			count++;
	}
}

static int coreboot_cfr_copy_enum_values(struct coreboot_cfr_setting *setting,
					 const void *base, size_t len)
{
	struct coreboot_cfr_iterator iterator = {
		.cursor = base,
		.remaining = len,
	};
	const struct lb_cfr_enum_value *enum_value;
	const struct lb_cfr_varbinary *label;
	const struct coreboot_table_entry *entry;
	struct coreboot_cfr_enum *value;
	int count;

	count = coreboot_cfr_count_enum_values(base, len);
	if (count <= 0)
		return count ?: -EINVAL;

	setting->values = kcalloc(count, sizeof(*setting->values), GFP_KERNEL);
	if (!setting->values)
		return -ENOMEM;

	for (;;) {
		entry = coreboot_cfr_next_entry(&iterator);
		if (IS_ERR(entry))
			return PTR_ERR(entry);
		if (!entry)
			return 0;

		if (entry->tag != CFR_TAG_ENUM_VALUE)
			continue;

		if (entry->size < sizeof(*enum_value))
			return -EINVAL;

		enum_value = (const struct lb_cfr_enum_value *)entry;
		label = coreboot_cfr_child_string(enum_value + 1,
						  enum_value->size - sizeof(*enum_value),
						  CFR_TAG_VARCHAR_UI_NAME);
		if (IS_ERR(label))
			return PTR_ERR(label);
		if (!label)
			return -EINVAL;

		value = &setting->values[setting->n_values];
		value->label = coreboot_cfr_string_dup(label);
		if (!value->label)
			return -ENOMEM;
		if (!coreboot_cfr_string_is_valid_label(value->label)) {
			kfree(value->label);
			value->label = NULL;
			return -EINVAL;
		}

		value->value = enum_value->value;
		setting->n_values++;
	}
}

static bool
coreboot_cfr_possible_values_fit(const struct coreboot_cfr_setting *setting)
{
	size_t len = 1;		/* Trailing newline. */
	size_t label_len;
	unsigned int i;

	for (i = 0; i < setting->n_values; i++) {
		if (len >= PAGE_SIZE)
			return false;

		if (i)
			len++;

		label_len = strlen(setting->values[i].label);
		if (label_len >= PAGE_SIZE - len)
			return false;

		len += label_len;
	}

	return true;
}

static int coreboot_cfr_setting_is_usable(struct coreboot_cfr_setting *setting)
{
	u32 value;
	int ret;

	ret = coreboot_cfr_read_value(setting, &value, NULL);
	if (ret)
		return ret;

	if (!coreboot_cfr_value_is_valid(setting, value))
		return -EINVAL;

	return 0;
}

static int coreboot_cfr_register_setting(struct coreboot_cfr_drvdata *data,
					 struct coreboot_cfr_setting *setting)
{
	int ret;

	ret = kobject_init_and_add(&setting->kobj, &coreboot_cfr_setting_ktype,
				   &data->attrs_kset->kobj, "%s", setting->name);
	if (ret)
		goto err_put_kobj;

	ret = sysfs_create_group(&setting->kobj, &coreboot_cfr_setting_group);
	if (ret)
		goto err_put_kobj;

	list_add_tail(&setting->node, &data->settings);
	return 0;

err_put_kobj:
	kobject_put(&setting->kobj);
	return ret;
}

static int coreboot_cfr_add_numeric_option(struct coreboot_cfr_drvdata *data,
					   const struct lb_cfr_numeric_option *option,
					   bool parent_read_only)
{
	const struct lb_cfr_varbinary *name;
	const struct lb_cfr_varbinary *display_name;
	const struct lb_cfr_runtime_apply *runtime_apply;
	const void *child_base = option + 1;
	struct coreboot_cfr_setting *setting;
	size_t child_len = option->size - sizeof(*option);
	int ret;

	if (!(option->flags & CFR_OPTFLAG_RUNTIME))
		return 0;

	if (option->flags & COREBOOT_CFR_OPT_SKIP_FLAGS)
		return 0;

	if (option->dependency_id)
		return 0;

	setting = kzalloc_obj(*setting, GFP_KERNEL);
	if (!setting)
		return -ENOMEM;

	INIT_LIST_HEAD(&setting->node);
	setting->drvdata = data;
	setting->default_value = option->default_value;
	setting->min = option->min;
	setting->max = option->max;
	setting->step = option->step ?: 1;
	setting->read_only =
		(option->flags & COREBOOT_CFR_OPT_READ_ONLY_FLAGS) ||
		!data->efi_writes_supported || parent_read_only;

	runtime_apply = coreboot_cfr_child_runtime_apply(child_base, child_len);
	if (IS_ERR(runtime_apply)) {
		ret = PTR_ERR(runtime_apply);
		goto err_put_setting;
	}

	if (runtime_apply && runtime_apply->method == CFR_RUNTIME_APPLY_APM_CNT) {
		setting->runtime_apply_method = runtime_apply->method;
		setting->runtime_apply_id = runtime_apply->id;
	}

	name = coreboot_cfr_child_string(child_base, child_len,
					 CFR_TAG_VARCHAR_OPT_NAME);
	if (IS_ERR(name)) {
		ret = PTR_ERR(name);
		goto err_put_setting;
	}
	if (!name) {
		ret = -EINVAL;
		goto err_put_setting;
	}

	setting->name = coreboot_cfr_string_dup(name);
	if (!setting->name) {
		ret = -ENOMEM;
		goto err_put_setting;
	}

	if (!coreboot_cfr_string_is_valid_name(setting->name)) {
		ret = -EINVAL;
		goto err_put_setting;
	}

	display_name = coreboot_cfr_child_string(child_base, child_len,
						 CFR_TAG_VARCHAR_UI_NAME);
	if (IS_ERR(display_name)) {
		ret = PTR_ERR(display_name);
		goto err_put_setting;
	}
	if (display_name)
		setting->display_name = coreboot_cfr_string_dup(display_name);
	else
		setting->display_name = kstrdup(setting->name, GFP_KERNEL);
	if (!setting->display_name) {
		ret = -ENOMEM;
		goto err_put_setting;
	}

	switch (option->tag) {
	case CFR_TAG_OPTION_BOOL:
		setting->type = COREBOOT_CFR_SETTING_BOOL;
		setting->min = 0;
		setting->max = 1;
		setting->step = 1;
		ret = coreboot_cfr_copy_bool_values(setting);
		break;
	case CFR_TAG_OPTION_ENUM:
		setting->type = COREBOOT_CFR_SETTING_ENUM;
		ret = coreboot_cfr_copy_enum_values(setting, child_base,
						    child_len);
		break;
	case CFR_TAG_OPTION_NUMBER:
		setting->type = COREBOOT_CFR_SETTING_NUMBER;
		if (setting->max < setting->min)
			ret = -EINVAL;
		else
			ret = 0;
		break;
	default:
		ret = -EINVAL;
		break;
	}

	if (ret)
		goto err_put_setting;
	/* possible_values must be returned completely in one sysfs read. */
	if (setting->type != COREBOOT_CFR_SETTING_NUMBER &&
	    !coreboot_cfr_possible_values_fit(setting)) {
		ret = 0;
		goto err_put_setting;
	}

	ret = coreboot_cfr_setting_is_usable(setting);
	if (ret) {
		/*
		 * CFR may describe options without a matching, readable runtime
		 * EFI variable. Skip those, but propagate transient EFI failures.
		 */
		if (ret == -ENOENT || ret == -EINVAL || ret == -EOPNOTSUPP ||
		    ret == -ENAMETOOLONG)
			ret = 0;
		goto err_put_setting;
	}

	/* The kobject release callback owns setting after this call. */
	return coreboot_cfr_register_setting(data, setting);

err_put_setting:
	coreboot_cfr_free_setting(setting);
	return ret;
}

static int coreboot_cfr_parse_records(struct coreboot_cfr_drvdata *data,
				      const void *base, size_t len,
				      unsigned int depth,
				      bool parent_read_only)
{
	struct coreboot_cfr_iterator iterator = {
		.cursor = base,
		.remaining = len,
	};
	const struct lb_cfr_option_form *form;
	const struct lb_cfr_numeric_option *option;
	const struct coreboot_table_entry *entry;
	const void *child_base;
	size_t child_len;
	int ret;

	for (;;) {
		entry = coreboot_cfr_next_entry(&iterator);
		if (IS_ERR(entry))
			return PTR_ERR(entry);
		if (!entry)
			return 0;

		switch (entry->tag) {
		case CFR_TAG_OPTION_FORM:
			if (entry->size < sizeof(struct lb_cfr_option_form))
				return -EINVAL;

			form = (const struct lb_cfr_option_form *)entry;
			if (form->dependency_id ||
			    form->flags & CFR_OPTFLAG_SUPPRESS)
				break;

			if (depth >= COREBOOT_CFR_MAX_FORM_DEPTH)
				return -E2BIG;

			child_base = form + 1;
			child_len = entry->size - sizeof(struct lb_cfr_option_form);
			ret = coreboot_cfr_parse_records(data, child_base,
							 child_len, depth + 1,
							 parent_read_only ||
							 (form->flags &
							  COREBOOT_CFR_OPT_READ_ONLY_FLAGS));
			if (ret)
				return ret;
			break;
		case CFR_TAG_OPTION_ENUM:
		case CFR_TAG_OPTION_NUMBER:
		case CFR_TAG_OPTION_BOOL:
			option = (const struct lb_cfr_numeric_option *)entry;
			if (entry->size < sizeof(*option))
				return -EINVAL;
			ret = coreboot_cfr_add_numeric_option(data, option,
							      parent_read_only);
			if (ret)
				return ret;
			break;
		default:
			/* Ignore unsupported, child-only and future record types. */
			break;
		}
	}
}

static void coreboot_cfr_unregister_settings(struct coreboot_cfr_drvdata *data)
{
	struct coreboot_cfr_setting *setting, *tmp;

	list_for_each_entry_safe(setting, tmp, &data->settings, node) {
		sysfs_remove_group(&setting->kobj, &coreboot_cfr_setting_group);
		list_del(&setting->node);
		kobject_put(&setting->kobj);
	}
}

static int coreboot_cfr_probe(struct coreboot_device *dev)
{
	const struct lb_cfr *root = (const struct lb_cfr *)dev->raw;
	struct coreboot_cfr_drvdata *data;
	size_t payload_len;
	int ret;

	if (dev->entry.size < sizeof(*root))
		return -EINVAL;

	if (root->tag != LB_TAG_CFR_ROOT || root->version != CFR_VERSION)
		return -EINVAL;

	if (root->size < sizeof(*root) || root->size > dev->entry.size)
		return -EINVAL;

	payload_len = root->size - sizeof(*root);
	if (crc32_be(0, root + 1, payload_len) != root->checksum)
		return -EBADMSG;

	if (!efivar_is_available())
		return -EPROBE_DEFER;

	data = devm_kzalloc(&dev->dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	ret = efivar_lock();
	if (ret)
		return ret;
	data->efi_writes_supported = efivar_supports_writes();
	efivar_unlock();

	INIT_LIST_HEAD(&data->settings);
	ret = devm_mutex_init(&dev->dev, &data->lock);
	if (ret)
		return ret;

	dev_set_drvdata(&dev->dev, data);

	data->class_dev = device_create(&firmware_attributes_class, NULL,
					MKDEV(0, 0), NULL, DRIVER_NAME);
	if (IS_ERR(data->class_dev))
		return PTR_ERR(data->class_dev);
	dev_set_drvdata(data->class_dev, data);

	data->attrs_kset = kset_create_and_add("attributes", NULL,
					       &data->class_dev->kobj);
	if (!data->attrs_kset) {
		ret = -ENOMEM;
		goto err_unregister_dev;
	}

	ret = sysfs_create_file(&data->attrs_kset->kobj,
				&pending_reboot_attr.attr);
	if (ret)
		goto err_unregister_attrs;

	ret = coreboot_cfr_parse_records(data, root + 1, payload_len, 0, false);
	if (ret)
		goto err_unregister_settings;

	if (list_empty(&data->settings)) {
		ret = -ENODEV;
		goto err_unregister_settings;
	}

	return 0;

err_unregister_settings:
	coreboot_cfr_unregister_settings(data);
	sysfs_remove_file(&data->attrs_kset->kobj, &pending_reboot_attr.attr);
err_unregister_attrs:
	kset_unregister(data->attrs_kset);
err_unregister_dev:
	device_unregister(data->class_dev);
	return ret;
}

static void coreboot_cfr_remove(struct coreboot_device *dev)
{
	struct coreboot_cfr_drvdata *data = dev_get_drvdata(&dev->dev);

	coreboot_cfr_unregister_settings(data);
	sysfs_remove_file(&data->attrs_kset->kobj, &pending_reboot_attr.attr);
	kset_unregister(data->attrs_kset);
	device_unregister(data->class_dev);
}

static const struct coreboot_device_id coreboot_cfr_ids[] = {
	{ .tag = LB_TAG_CFR_ROOT },
	{ }
};
MODULE_DEVICE_TABLE(coreboot, coreboot_cfr_ids);

static struct coreboot_driver coreboot_cfr_driver = {
	.probe = coreboot_cfr_probe,
	.remove = coreboot_cfr_remove,
	.drv = {
		.name = DRIVER_NAME,
	},
	.id_table = coreboot_cfr_ids,
};
module_coreboot_driver(coreboot_cfr_driver);

MODULE_AUTHOR("Sean Rhodes <sean@starlabs.systems>");
MODULE_DESCRIPTION("coreboot CFR firmware attributes driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("EFIVAR");