forked from microsoft/mu_feature_config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigEditor.py
1411 lines (1195 loc) · 51.7 KB
/
ConfigEditor.py
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
## @ ConfigEditor.py
#
# Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
import os
import sys
import base64
import datetime
import ctypes
from pathlib import Path
sys.dont_write_bytecode = True
import tkinter # noqa: E402
import tkinter.ttk as ttk # noqa: E402
import tkinter.messagebox as messagebox # noqa: E402
import tkinter.filedialog as filedialog # noqa: E402
from GenNCCfgData import CGenNCCfgData # noqa: E402
import WriteConfVarListToUefiVars as uefi_var_write # noqa: E402
import ReadUefiVarsToConfVarList as uefi_var_read # noqa: E402
import BoardMiscInfo # noqa: E402
from VariableList import Schema # noqa: E402
from CommonUtility import ( # noqa: E402
bytes_to_value,
bytes_to_bracket_str,
value_to_bytes,
array_str_to_value,
)
def ask_yes_no(prompt):
result = messagebox.askyesno("Question", prompt)
return result
class create_tool_tip(object):
"""
create a tooltip for a given widget
"""
in_progress = False
def __init__(self, widget, text=""):
self.top_win = None
self.widget = widget
self.text = text
self.widget.bind("<Enter>", self.enter)
self.widget.bind("<Leave>", self.leave)
self.config_xml_path = None
def enter(self, event=None):
if self.in_progress:
return
if self.widget.winfo_class() == "Treeview":
# Only show help when cursor is on row header.
rowid = self.widget.identify_row(event.y)
if rowid != "":
return
else:
x, y, cx, cy = self.widget.bbox("insert")
cursor = self.widget.winfo_pointerxy()
x = self.widget.winfo_rootx() + 35
y = self.widget.winfo_rooty() + 20
if cursor[1] > y and cursor[1] < y + 20:
y += 20
# creates a toplevel window
self.top_win = tkinter.Toplevel(self.widget)
# Leaves only the label and removes the app window
self.top_win.wm_overrideredirect(True)
self.top_win.wm_geometry("+%d+%d" % (x, y))
label = tkinter.Message(
self.top_win,
text=self.text,
justify="left",
background="bisque",
relief="solid",
borderwidth=1,
font=("times", "10", "normal"),
)
label.pack(ipadx=1)
self.in_progress = True
def leave(self, event=None):
if self.top_win:
self.top_win.destroy()
self.in_progress = False
class validating_entry(tkinter.Entry):
def __init__(self, master, **kw):
tkinter.Entry.__init__(*(self, master), **kw)
self.parent = master
self.old_value = ""
self.last_value = ""
self.variable = tkinter.StringVar()
self.variable.trace("w", self.callback)
self.config(textvariable=self.variable)
self.config({"background": "#c0c0c0"})
self.bind("<Return>", self.move_next)
self.bind("<Tab>", self.move_next)
self.bind("<Escape>", self.cancel)
for each in ["BackSpace", "Delete"]:
self.bind("<%s>" % each, self.ignore)
self.display(None)
def ignore(self, even):
return "break"
def move_next(self, event):
if self.row < 0:
return
row, col = self.row, self.col
txt, row_id, col_id = self.parent.get_next_cell(row, col)
self.display(txt, row_id, col_id)
return "break"
def cancel(self, event):
self.variable.set(self.old_value)
self.display(None)
def display(self, txt, row_id="", col_id=""):
if txt is None:
self.row = -1
self.col = -1
self.place_forget()
else:
row = int("0x" + row_id[1:], 0) - 1
col = int(col_id[1:]) - 1
self.row = row
self.col = col
self.old_value = txt
self.last_value = txt
x, y, width, height = self.parent.bbox(row_id, col)
self.place(x=x, y=y, w=width)
self.variable.set(txt)
self.focus_set()
self.icursor(0)
def callback(self, *Args):
cur_val = self.variable.get()
new_val = self.validate(cur_val)
if new_val is not None and self.row >= 0:
self.last_value = new_val
self.parent.set_cell(self.row, self.col, new_val)
self.variable.set(self.last_value)
def validate(self, value):
if len(value) > 0:
try:
int(value, 16)
except Exception:
return None
# Normalize the cell format
self.update()
cell_width = self.winfo_width()
max_len = custom_table.to_byte_length(cell_width) * 2
cur_pos = self.index("insert")
if cur_pos == max_len + 1:
value = value[-max_len:]
else:
value = value[:max_len]
if value == "":
value = "0"
fmt = "%%0%dX" % max_len
return fmt % int(value, 16)
class custom_table(ttk.Treeview):
_Padding = 20
_Char_width = 6
def __init__(self, parent, col_hdr, bins):
cols = len(col_hdr)
col_byte_len = []
for col in range(cols): # Columns
col_byte_len.append(int(col_hdr[col].split(":")[1]))
byte_len = sum(col_byte_len)
rows = (len(bins) + byte_len - 1) // byte_len
self.rows = rows
self.cols = cols
self.col_byte_len = col_byte_len
self.col_hdr = col_hdr
self.size = len(bins)
self.last_dir = ""
style = ttk.Style()
style.configure(
"Custom.Treeview.Heading", font=("calibri", 10, "bold"), foreground="blue"
)
ttk.Treeview.__init__(
self,
parent,
height=rows,
columns=[""] + col_hdr,
show="headings",
style="Custom.Treeview",
selectmode="none",
)
self.bind("<Button-1>", self.click)
self.bind("<FocusOut>", self.focus_out)
self.entry = validating_entry(self, width=4, justify=tkinter.CENTER)
self.heading(0, text="LOAD")
self.column(0, width=60, stretch=0, anchor=tkinter.CENTER)
for col in range(cols): # Columns
text = col_hdr[col].split(":")[0]
byte_len = int(col_hdr[col].split(":")[1])
self.heading(col + 1, text=text)
self.column(
col + 1,
width=self.to_cell_width(byte_len),
stretch=0,
anchor=tkinter.CENTER,
)
idx = 0
for row in range(rows): # Rows
text = "%04X" % (row * len(col_hdr))
vals = ["%04X:" % (cols * row)]
for col in range(cols): # Columns
if idx >= len(bins):
break
byte_len = int(col_hdr[col].split(":")[1])
value = bytes_to_value(bins[idx: idx + byte_len])
hex = ("%%0%dX" % (byte_len * 2)) % value
vals.append(hex)
idx += byte_len
self.insert("", "end", values=tuple(vals))
if idx >= len(bins):
break
@staticmethod
def to_cell_width(byte_len):
return byte_len * 2 * custom_table._Char_width + custom_table._Padding
@staticmethod
def to_byte_length(cell_width):
return (cell_width - custom_table._Padding) // (2 * custom_table._Char_width)
def focus_out(self, event):
self.entry.display(None)
def refresh_bin(self, bins):
if not bins:
return
# Reload binary into widget
bin_len = len(bins)
for row in range(self.rows):
iid = self.get_children()[row]
for col in range(self.cols):
idx = row * sum(self.col_byte_len) + sum(self.col_byte_len[:col])
byte_len = self.col_byte_len[col]
if idx + byte_len <= self.size:
byte_len = int(self.col_hdr[col].split(":")[1])
if idx + byte_len > bin_len:
val = 0
else:
val = bytes_to_value(bins[idx: idx + byte_len])
hex_val = ("%%0%dX" % (byte_len * 2)) % val
self.set(iid, col + 1, hex_val)
def get_cell(self, row, col):
iid = self.get_children()[row]
txt = self.item(iid, "values")[col]
return txt
def get_next_cell(self, row, col):
rows = self.get_children()
col += 1
if col > self.cols:
col = 1
row += 1
cnt = row * sum(self.col_byte_len) + sum(self.col_byte_len[:col])
if cnt > self.size:
# Reached the last cell, so roll back to beginning
row = 0
col = 1
txt = self.get_cell(row, col)
row_id = rows[row]
col_id = "#%d" % (col + 1)
return (txt, row_id, col_id)
def set_cell(self, row, col, val):
iid = self.get_children()[row]
self.set(iid, col, val)
def load_bin(self):
# Load binary from file
path = filedialog.askopenfilename(
initialdir=self.last_dir,
title="Load variable list file",
filetypes=(("variable list files", "*.vl"), ("variable list files", "*.vl")),
)
if path:
self.last_dir = os.path.dirname(path)
fd = open(path, "rb")
bins = bytearray(fd.read())[: self.size]
fd.close()
bins.extend(b"\x00" * (self.size - len(bins)))
return bins
return None
def click(self, event):
row_id = self.identify_row(event.y)
col_id = self.identify_column(event.x)
if row_id == "" and col_id == "#1":
# Clicked on "LOAD" cell
bins = self.load_bin()
self.refresh_bin(bins)
return
if col_id == "#1":
# Clicked on column 1 (Offset column)
return
item = self.identify("item", event.x, event.y)
if not item or not col_id:
# Not clicked on valid cell
return
# Clicked cell
row = int("0x" + row_id[1:], 0) - 1
col = int(col_id[1:]) - 1
if row * self.cols + col > self.size:
return
vals = self.item(item, "values")
if col < len(vals):
txt = self.item(item, "values")[col]
self.entry.display(txt, row_id, col_id)
def get(self):
bins = bytearray()
row_ids = self.get_children()
for row_id in row_ids:
row = int("0x" + row_id[1:], 0) - 1
for col in range(self.cols):
idx = row * sum(self.col_byte_len) + sum(self.col_byte_len[:col])
byte_len = self.col_byte_len[col]
if idx + byte_len > self.size:
break
hex = self.item(row_id, "values")[col + 1]
values = value_to_bytes(
int(hex, 16) & ((1 << byte_len * 8) - 1), byte_len
)
bins.extend(values)
return bins
class state:
def __init__(self):
self.state = False
def set(self, value):
self.state = value
def get(self):
return self.state
class cfg_data:
def __init__(self):
self.cfg_data_obj = None
self.org_cfg_data_bin = None
self.config_type = ''
class application(tkinter.Frame):
def __init__(self, master=None):
root = master
self.debug = True
self.page_id = ""
self.page_list = {}
self.conf_list = {}
self.in_left = state()
self.in_right = state()
self.cfg_data_list = {}
# this maps page id to cfg_data index, needed for when user changes pages
# self.page_cfg_map[page_id] = cfg_data_idx
self.page_cfg_map = {}
# Check if current directory contains a file with a .yaml extension
# if not default self.last_dir to a Platform directory where it is
# easier to locate *BoardPkg\CfgData\*Def.yaml files
self.last_dir = "."
if not any(fname.endswith(".yaml") for fname in os.listdir(".")):
platform_path = (
Path(os.path.realpath(__file__)).parents[2].joinpath("Platform")
)
if platform_path.exists():
self.last_dir = platform_path
tkinter.Frame.__init__(self, master, borderwidth=2)
self.menu_string = [
'Save Full Config Data to Binary',
'Save Config Changes to Binary',
'Load Config Data from Binary',
'Save Full Config Data to SVD File',
'Save Config Changes to SVD File',
'Load Config from SVD File',
'Save Full Config Data to Change File',
'Save Config Changes to Change File',
'Load Config from Change File',
]
self.variable_menu_string = [
'Load Runtime Variables from system',
'Save Runtime Variables to system',
'Delete Runtime Variables to system',
]
self.xml_specific_setting = [
'Save Config Changes to Binary'
]
root.geometry("1200x800")
paned = ttk.Panedwindow(root, orient=tkinter.HORIZONTAL)
paned.pack(fill=tkinter.BOTH, expand=True, padx=(4, 4))
self.status = tkinter.Text(
master, height=8, bd=1, relief=tkinter.SUNKEN, wrap=tkinter.WORD
)
self.status.pack(side=tkinter.BOTTOM, fill=tkinter.X)
frame_left = ttk.Frame(paned, height=800, relief="groove")
self.left = ttk.Treeview(frame_left, show="tree")
# Set up tree HScroller
pady = (10, 10)
self.tree_scroll = ttk.Scrollbar(
frame_left, orient="vertical", command=self.left.yview
)
self.left.configure(yscrollcommand=self.tree_scroll.set)
self.left.bind("<<TreeviewSelect>>", self.on_config_page_select_change)
self.left.bind("<Enter>", lambda e: self.in_left.set(True))
self.left.bind("<Leave>", lambda e: self.in_left.set(False))
self.left.bind("<MouseWheel>", self.on_tree_scroll)
self.left.pack(
side="left", fill=tkinter.BOTH, expand=True, padx=(5, 0), pady=pady
)
self.tree_scroll.pack(side="right", fill=tkinter.Y, pady=pady, padx=(0, 5))
frame_right = ttk.Frame(paned, relief="groove")
self.frame_right = frame_right
self.conf_canvas = tkinter.Canvas(frame_right, highlightthickness=0)
self.page_scroll = ttk.Scrollbar(
frame_right, orient="vertical", command=self.conf_canvas.yview
)
self.right_grid = ttk.Frame(self.conf_canvas)
self.conf_canvas.configure(yscrollcommand=self.page_scroll.set)
self.conf_canvas.pack(
side="left", fill=tkinter.BOTH, expand=True, pady=pady, padx=(5, 0)
)
self.page_scroll.pack(side="right", fill=tkinter.Y, pady=pady, padx=(0, 5))
self.conf_canvas.create_window(0, 0, window=self.right_grid, anchor="nw")
self.conf_canvas.bind("<Enter>", lambda e: self.in_right.set(True))
self.conf_canvas.bind("<Leave>", lambda e: self.in_right.set(False))
self.conf_canvas.bind("<Configure>", self.on_canvas_configure)
self.conf_canvas.bind_all("<MouseWheel>", self.on_page_scroll)
paned.add(frame_left, weight=2)
paned.add(frame_right, weight=10)
style = ttk.Style()
style.layout("Treeview", [("Treeview.treearea", {"sticky": "nswe"})])
menubar = tkinter.Menu(root)
file_menu = tkinter.Menu(menubar, tearoff=0)
file_menu.add_command(
label="Open Config file...", command=self.load_from_ml
)
file_menu.add_command(
label="Open Config file and Clear Old Config", command=self.load_from_ml_and_clear
)
file_menu.add_separator()
file_menu.add_command(
label=self.menu_string[0], command=self.save_to_bin, state="disabled"
)
file_menu.add_command(
label=self.menu_string[1], command=self.save_delta_to_bin, state="disabled"
)
file_menu.add_command(
label=self.menu_string[2], command=self.load_from_bin, state="disabled"
)
file_menu.add_separator()
file_menu.add_command(
label=self.menu_string[3], command=self.save_full_to_svd, state="disabled"
)
file_menu.add_command(
label=self.menu_string[4], command=self.save_delta_to_svd, state="disabled"
)
file_menu.add_command(
label=self.menu_string[5], command=self.load_from_svd, state="disabled"
)
file_menu.add_separator()
file_menu.add_command(
label=self.menu_string[6], command=self.save_full_to_delta, state="disabled"
)
file_menu.add_command(
label=self.menu_string[7], command=self.save_to_delta, state="disabled"
)
file_menu.add_command(
label=self.menu_string[8], command=self.load_from_delta, state="disabled"
)
file_menu.add_separator()
file_menu.add_command(label="About", command=self.about)
menubar.add_cascade(label="File", menu=file_menu)
self.file_menu = file_menu
self.admin_mode = False
if os.name == 'nt' and ctypes.windll.shell32.IsUserAnAdmin():
self.admin_mode = True
elif os.name == 'posix' and os.getuid() == 0:
self.admin_mode = True
if self.admin_mode:
# Variable Menu
variable_menu = tkinter.Menu(menubar, tearoff=0)
variable_menu.add_command(
label=self.variable_menu_string[0], command=self.load_variable_runtime, state="disabled"
)
variable_menu.add_command(
label=self.variable_menu_string[1], command=self.set_variable_runtime, state="disabled"
)
variable_menu.add_command(
label=self.variable_menu_string[2], command=self.del_all_variable_runtime, state="disabled"
)
menubar.add_cascade(label="Variables", menu=variable_menu)
self.variable_menu = variable_menu
root.config(menu=menubar)
# Checking if we are in Manufacturing mode
bios_info_smbios_data = BoardMiscInfo.locate_smbios_entry(0)
# Check if we have the SMBIOS data in the first entry
bios_info_smbios_data = bios_info_smbios_data[0]
if (bios_info_smbios_data != []):
char_ext2_data = bios_info_smbios_data[0x13]
Manufacturing_enabled = (char_ext2_data & (0x1 << 6)) >> 6
print(f"Manufacturing : {Manufacturing_enabled:02X}")
# get mfci policy
mfci_policy_result = BoardMiscInfo.get_mfci_policy()
self.canvas = tkinter.Canvas(master, width=240, height=50, bg=master['bg'], highlightthickness=0)
self.canvas.place(relx=1.0, rely=1.0, x=0, y=0, anchor='se')
self.canvas.create_text(
120, 25,
text=(
f"AdminMode: {self.admin_mode}\n"
f"Manufacturing Mode: {Manufacturing_enabled}\n"
f"Mfci Policy: {mfci_policy_result}"
),
fill="black",
font=("Helvetica", 10, "bold")
)
idx = 0
if len(sys.argv) > 1:
path = sys.argv[1]
if not path.endswith('.xml'):
messagebox.showerror('LOADING ERROR', "Unsupported file '%s' !" % path)
return
else:
self.load_cfg_file(path, idx, False)
for i in range(2, len(sys.argv)):
idx += 1
path = sys.argv[i]
if path.endswith(".csv"):
self.load_delta_file(path)
elif path.endswith(".vl"):
self.load_bin_file(path, True)
elif path.endswith(".xml"):
self.load_cfg_file(path, idx, False)
else:
messagebox.showerror("LOADING ERROR", "Unsupported file '%s' !" % path)
return
if getattr(sys, "frozen", False) and hasattr(sys, '_MEIPASS'):
# The application is frozen, pre-populate collected definition files
print("Running bundled ConfigEditor! Load pre-populated definition files.\n")
bundle_dir = sys._MEIPASS
# The collected definitions will be put under "ConfDefinitions" folder in the bundle directory
for subdir, _, files in os.walk(os.path.join(bundle_dir, "ConfDefinitions")):
for file in files:
sub_path = os.path.join(subdir, file)
if sub_path.endswith(".xml"):
idx += 1
self.load_cfg_file(sub_path, idx, False)
def set_object_name(self, widget, name, file_id):
# associate the name of the widget with the file it came from, in case of name conflicts
self.conf_list[id(widget)] = (name, file_id)
def get_object_name(self, widget):
if id(widget) in self.conf_list:
return self.conf_list[id(widget)]
else:
return None, None
def limit_entry_size(self, variable, limit):
value = variable.get()
if len(value) > limit:
variable.set(value[:limit])
def on_canvas_configure(self, event):
self.right_grid.grid_columnconfigure(0, minsize=event.width)
def on_tree_scroll(self, event):
if not self.in_left.get() and self.in_right.get():
# This prevents scroll event from being handled by both left and
# right frame at the same time.
self.on_page_scroll(event)
return "break"
def on_page_scroll(self, event):
if self.in_right.get():
# Only scroll when it is in active area
min, max = self.page_scroll.get()
if not ((min == 0.0) and (max == 1.0)):
self.conf_canvas.yview_scroll(-1 * int(event.delta / 120), "units")
def update_visibility_for_widget(self, widget, args):
visible = True
item = self.get_config_data_item_from_widget(widget, True)
if item is None:
return visible
elif not item:
return visible
file_id = self.get_object_name(widget)[1]
result = 1
if 'condition' in item and item['condition']:
result = self.evaluate_condition(item, file_id)
if result == 2:
# Gray
if not isinstance(widget, custom_table):
widget.configure(state="disabled")
elif result == 0:
# Hide
visible = False
widget.grid_remove()
else:
# Show
widget.grid()
if not isinstance(widget, custom_table):
widget.configure(state="normal")
return visible
def update_widgets_visibility_on_page(self):
self.walk_widgets_in_layout(self.right_grid, self.update_visibility_for_widget)
def combo_select_changed(self, event):
self.update_config_data_from_widget(event.widget, None)
self.update_widgets_visibility_on_page()
def edit_num_finished(self, event):
widget = event.widget
item = self.get_config_data_item_from_widget(widget)
if not item:
return
parts = item["type"].split(",")
file_id = self.get_object_name(widget)[1]
if len(parts) > 3:
min = parts[2].lstrip()[1:]
max = parts[3].rstrip()[:-1]
min_val = array_str_to_value(min)
max_val = array_str_to_value(max)
text = widget.get()
if "," in text:
text = "{ %s }" % text
try:
value = array_str_to_value(text)
if value < min_val or value > max_val:
raise Exception("Invalid input!")
self.set_config_item_value(item, text, file_id)
except Exception:
pass
text = item["value"].strip("{").strip("}").strip()
widget.delete(0, tkinter.END)
widget.insert(0, text)
self.update_widgets_visibility_on_page()
def update_page_scroll_bar(self):
# Update scrollbar
self.frame_right.update()
self.conf_canvas.config(scrollregion=self.conf_canvas.bbox("all"))
def on_config_page_select_change(self, event):
self.update_config_data_on_page()
sel = self.left.selection()
if len(sel) > 0:
page_id = sel[0]
self.build_config_data_page(page_id)
self.update_widgets_visibility_on_page()
self.update_page_scroll_bar()
def walk_widgets_in_layout(self, parent, callback_function, args=None):
for widget in parent.winfo_children():
callback_function(widget, args)
def clear_widgets_inLayout(self, parent=None):
if parent is None:
parent = self.right_grid
for widget in parent.winfo_children():
widget.destroy()
parent.grid_forget()
self.conf_list.clear()
def build_config_page_tree(self, cfg_page, parent, file_id):
for page in cfg_page["child"]:
page_id = next(iter(page))
# Put CFG items into related page list
self.page_cfg_map[page_id] = file_id
self.page_list[page_id] = self.cfg_data_list[file_id].cfg_data_obj.get_cfg_list(page_id)
self.page_list[page_id].sort(key=lambda x: x["order"])
page_name = self.cfg_data_list[file_id].cfg_data_obj.get_page_title(page_id)
child = self.left.insert(
parent, "end", iid=page_id, text=page_name, value=0
)
if len(page[page_id]) > 0:
self.build_config_page_tree(page[page_id], child, file_id)
def is_config_data_loaded(self):
return True if len(self.page_list) else False
def set_current_config_page(self, page_id):
self.page_id = page_id
def get_current_config_page(self):
return self.page_id
def get_current_config_data(self):
page_id = self.get_current_config_page()
if page_id in self.page_list:
return self.page_list[page_id]
else:
return []
def build_config_data_page(self, page_id):
self.clear_widgets_inLayout()
self.set_current_config_page(page_id)
disp_list = []
for item in self.get_current_config_data():
disp_list.append(item)
row = 0
disp_list.sort(key=lambda x: x["order"])
for item in disp_list:
self.add_config_item(item, row, self.page_cfg_map[page_id])
row += 2
def load_config_data(self, file_name):
if file_name.endswith('.xml'):
gen_cfg_data = CGenNCCfgData(file_name)
if gen_cfg_data.load_xml(file_name) != 0:
raise Exception(gen_cfg_data.get_last_error())
else:
raise Exception('Unsupported file "%s" !' % file_name)
return gen_cfg_data
def about(self):
msg = (
"Configuration Editor\n--------------------------------\nVersion 0.8\n2020"
)
lines = msg.split("\n")
width = 30
text = []
for line in lines:
text.append(line.center(width, " "))
messagebox.showinfo("Config Editor", "\n".join(text))
def update_last_dir(self, path):
self.last_dir = os.path.dirname(path)
def get_open_file_name(self, ftype):
if self.is_config_data_loaded():
if 'csv' in ftype:
question = ""
elif ftype == "vl":
question = ''
elif ftype == 'svd':
question = ''
elif 'xml' in ftype:
question = ''
else:
raise Exception("Unsupported file type !")
if question:
reply = messagebox.askquestion("", question, icon="warning")
if reply == "no":
return None
file_type = ''
file_ext = ''
if 'xml' in ftype:
file_type += ' XML'
file_ext += ' xml'
else:
file_type = ftype.upper()
file_ext = ftype
file_ext = file_ext.split(' ')
file_ext_opt = ['*.' + i for i in file_ext]
path = filedialog.askopenfilename(
initialdir=self.last_dir,
title="Load file",
filetypes=(("%s files" % file_type, file_ext_opt), (
"all files", "*.*")))
if path:
self.update_last_dir(path)
return path
else:
return None
def load_from_delta(self):
path = self.get_open_file_name("csv")
if not path:
return
elif not path.endswith('.csv'):
messagebox.showerror("LOADING ERROR", "Unsupported file type %s" % path)
return
self.load_delta_file(path)
def set_variable_runtime(self):
self.update_config_data_on_page()
if (not ask_yes_no("Do you want to save the variable to the system?\n")):
return
runtime_var_delta_path = "RuntimeVarToWrite.vl"
bin = b''
for idx in self.cfg_data_list:
if self.cfg_data_list[idx].config_type == 'xml':
bin = self.cfg_data_list[idx].cfg_data_obj.generate_binary_array(True)
with open(runtime_var_delta_path, "wb") as fd:
fd.write(bin)
uefi_var_write.set_variable_from_file(runtime_var_delta_path)
self.load_variable_runtime()
self.output_current_status("Settings are set to system and save to RuntimeVar.vl")
def del_all_variable_runtime(self):
if (not ask_yes_no(f"Do you want to delete all variables in {self.config_xml_path} on system?\n")):
return
schema = Schema.load(self.config_xml_path)
for knob in schema.knobs:
self.output_current_status(f"Delete variable {knob.name} with namespace {knob.namespace}")
rc = uefi_var_write.delete_var_by_guid_name(knob.name, knob.namespace)
if rc == 0:
self.output_current_status(f"{knob.name} variable was not deleted from system {rc}")
else:
self.output_current_status(f"{knob.name} variable is deleted from system")
def load_delta_file(self, path):
# assumption is there may be multiple xml files
# so we can only load this delta file if the file name matches to this xml data
updated_knobs = 0
for idx in self.cfg_data_list:
# if loading xml, ensure that knobs GUID + name exist in any loaded XML
try:
updated_knobs += self.cfg_data_list[idx].cfg_data_obj.override_default_value(path)
except Exception as e:
messagebox.showerror("LOADING ERROR", str(e))
return
if path.endswith('.csv'):
if updated_knobs == 0:
messagebox.showerror('CSV Loading Error', 'Loaded CSV did not apply to any loaded config file!')
return
else:
raise Exception('Unsupported file "%s" !' % path)
self.update_last_dir(path)
self.refresh_config_data_page()
def load_from_raw_bin(self):
path = self.get_open_file_name("vl")
if not path:
return
self.load_bin_file(path, False)
def load_from_bin(self):
path = self.get_open_file_name("vl")
if not path:
return
self.load_bin_file(path)
def load_from_svd(self):
path = self.get_open_file_name("svd")
if not path:
return
for idx in self.cfg_data_list:
self.cfg_data_list[idx].cfg_data_obj.load_from_svd(path)
self.refresh_config_data_page()
def load_bin_file(self, path):
with open(path, "rb") as fd:
bin_data = bytearray(fd.read())
try:
for idx in self.cfg_data_list:
self.reload_config_data_from_bin(bin_data, idx, True)
except Exception as e:
messagebox.showerror("LOADING ERROR", str(e))
return
self.output_current_status(f"{path} file is loaded")
def load_cfg_file(self, path, file_id, clear_config):
# Clear out old config if requested
if clear_config is True:
self.clear_widgets_inLayout()
self.left.delete(*self.left.get_children())
self.cfg_data_list = {}
self.cfg_data_list[file_id] = cfg_data()
# Set up the config type to begin with
if path.lower().endswith('.xml'):
self.cfg_data_list[file_id].config_type = 'xml'
else:
raise Exception("Unsupported file format")
self.cfg_data_list[file_id].cfg_data_obj = self.load_config_data(path)
self.update_last_dir(path)
self.cfg_data_list[file_id].org_cfg_data_bin = self.cfg_data_list[file_id].cfg_data_obj.generate_binary_array(
False
)
self.build_config_page_tree(self.cfg_data_list[file_id].cfg_data_obj.get_cfg_page()["root"], "", file_id)
for menu in self.menu_string:
self.file_menu.entryconfig(menu, state="normal")
if self.admin_mode:
for menu in self.variable_menu_string:
self.variable_menu.entryconfig(menu, state="normal")
self.config_xml_path = path
self.output_current_status(f"{path} file is loaded")
return 0
def load_from_ml_and_clear(self):
path = self.get_open_file_name('xml')
if not path:
return
# we are opening a new file and clearing out the other ones, start at 0
file_id = 0
self.load_cfg_file(path, file_id, True)
def load_from_ml(self):
path = self.get_open_file_name('xml')
if not path:
return
# we are opening a new file, so increment the file_id
file_id = len(self.cfg_data_list)
self.load_cfg_file(path, file_id, False)
def load_variable_runtime(self):
status = uefi_var_read.read_all_uefi_vars("RuntimeVar.vl", self.config_xml_path)
info_msg = "Settings are read from system and save to RuntimeVar.vl"
if status == -1:
info_msg = f"No Config Var is found, all the data from from {self.config_xml_path}"
messagebox.showinfo("WARNING", f"No Config Var is found, all the data from from {self.config_xml_path}")
else:
self.load_bin_file("RuntimeVar.vl")