-
Notifications
You must be signed in to change notification settings - Fork 2
/
IfrStructures.cs
2869 lines (2620 loc) · 98.4 KB
/
IfrStructures.cs
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
//MIT License
//
//Copyright(c) 2016-2020 Peter Kirmeier
//
//Permission Is hereby granted, free Of charge, to any person obtaining a copy
//of this software And associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, And/Or sell
//copies of the Software, And to permit persons to whom the Software Is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice And this permission notice shall be included In all
//copies Or substantial portions of the Software.
//
//THE SOFTWARE Is PROVIDED "AS IS", WITHOUT WARRANTY Of ANY KIND, EXPRESS Or
//IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES Of MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE And NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS Or COPYRIGHT HOLDERS BE LIABLE For ANY CLAIM, DAMAGES Or OTHER
//LIABILITY, WHETHER In AN ACTION Of CONTRACT, TORT Or OTHERWISE, ARISING FROM,
//OUT OF Or IN CONNECTION WITH THE SOFTWARE Or THE USE Or OTHER DEALINGS IN THE
//SOFTWARE.
// This file contains the EDKII structures of "UefiInternalFormRepresentation.h"
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using static IFR.IFRHelper;
/// <summary>
/// This namespace contains the Internal Form Representation definisions
/// (see: EDKII structures of "UefiInternalFormRepresentation.h")
/// </summary>
namespace IFR
{
#region 1:1 type assignments between C <-> C#
using UINT8 = Byte;
using UINT16 = UInt16;
using UINT32 = UInt32;
using UINT64 = UInt64;
using CHAR16 = Char;
using EFI_IMAGE_ID = UInt16;
using EFI_QUESTION_ID = UInt16;
using EFI_STRING_ID = UInt16;
using EFI_FORM_ID = UInt16;
using EFI_VARSTORE_ID = UInt16;
using EFI_ANIMATION_ID = UInt16;
using EFI_DEFAULT_ID = UInt16;
using EFI_HII_FONT_STYLE = UInt32;
/// <summary>
/// Wrapper for EFI_GUID
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 16)]
struct EFI_GUID
{
public Guid Guid;
};
/// <summary>
/// Wrapper for UINT16
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 1)]
struct IfrTypeUINT8
{
public UINT8 u8;
};
/// <summary>
/// Wrapper for UINT16
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 2)]
struct IfrTypeUINT16
{
public UINT16 u16;
};
/// <summary>
/// Wrapper for UINT16
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 4)]
struct IfrTypeUINT32
{
public UINT32 u32;
};
/// <summary>
/// Wrapper for UINT16
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 8)]
struct IfrTypeUINT64
{
public UINT64 u64;
};
/// <summary>
/// Wrapper for BOOLEAN
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 1)]
struct IfrTypeBOOLEAN
{
/// <summary>
/// 0 = false, 1 = true
/// </summary>
public UINT8 b;
};
/// <summary>
/// Wrapper for EFI_STRING_ID
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 2)]
struct IfrTypeEFI_STRING_ID
{
public UINT16 stringid; // = "string" but not working in C#
};
#endregion
#region Common stuff (not related to UEFI source)
#region Interfaces
public interface IEfiIfrNumericValue
{
EFI_IFR_NUMERIC_SIZE_e Flags_DataSize { get; }
}
public interface IEfiIfrType
{
EFI_IFR_TYPE_e Type { get; }
}
#endregion
/// <summary>
/// Wrapper to access raw data directly as single memory source
/// </summary>
public class IfrRawDataBlock
{
public byte[] Bytes;
public uint Offset;
public uint Length;
#region Constructors
/// <summary>
/// Creates new IFR raw data block.
/// Sets initially Offset to 0 and Length according to given raw data length
/// </summary>
/// <param name="raw">Data this block should refer</param>
public IfrRawDataBlock(byte[] raw)
{
Bytes = raw;
Offset = 0;
Length = (uint)raw.Length;
}
/// <summary>
/// Creates new IFR raw data block.
/// Sets initially Offset to 0 and Length according to given raw data length
/// </summary>
/// <param name="raw">Data this block should refer</param>
/// <param name="offset">Starting offset where payload resides</param>
/// <param name="length">Amount of paylod's data bytes</param>
public IfrRawDataBlock(byte[] raw, uint offset, uint length)
{
Bytes = raw;
Offset = offset;
Length = length;
}
/// <summary>
/// Creates new IFR raw data block.
/// Copy constructur
/// </summary>
/// <param name="origin">Instance from which data is copied to this new instance</param>
public IfrRawDataBlock(IfrRawDataBlock origin)
{
Bytes = origin.Bytes;
Offset = origin.Offset;
Length = origin.Length;
}
#endregion
#region CopyOf[type] methods
/// <summary>
/// Copies the whole selected buffer at the current data position
/// </summary>
/// <returns>Byte array</returns>
public byte[] CopyOfSelectedBytes { get { return Bytes.SubArray((int)Offset, (int)Length); } }
/// <summary>
/// Retrieves a null terminated ASCII string from the current data position
/// </summary>
/// <returns>Managed string filled with corresponding raw data (without NULL)</returns>
public string CopyOfAsciiNullTerminatedString
{
get
{
const int startindex = 0;
int IdxStart = (int)(Offset + startindex);
int IdxEnd = IdxStart + (int)Length;
int IdxNull = -1;
for (int i = IdxStart; i < IdxEnd; i++)
{
if (Bytes[i] == '\0') // null terminated string
{
IdxNull = i;
break;
}
}
if (IdxNull == -1)
throw new Exception("Expected string is not NULL terminated!");
return System.Text.Encoding.ASCII.GetString(Bytes.SubArray(IdxStart, IdxNull - IdxStart)); ;
}
}
/// <summary>
/// Retrieves a null terminated Unicode string from the current data position
/// </summary>
/// <returns>Managed string filled with corresponding raw data (without NULL)</returns>
public string CopyOfUnicodeNullTerminatedString
{
get
{
const int startindex = 0;
int IdxStart = (int)(Offset + startindex);
int IdxEnd = IdxStart + (int)Length;
int IdxNull = -1;
for (int i = IdxStart; i < IdxEnd - 1; i += 2)
{
if ((Bytes[i] == '\0') && (Bytes[i+1] == '\0')) // null terminated string
{
IdxNull = i;
break;
}
}
if (IdxNull == -1)
throw new Exception("Expected string is not NULL terminated!");
return System.Text.Encoding.Unicode.GetString(Bytes.SubArray(IdxStart, IdxNull - IdxStart)); ;
}
}
#endregion
#region Other methods
/// <summary>
/// Changes Offset and Length accordingly. Cheks for out of bounds.
/// </summary>
/// <param name="amount">Amount of bytes to skip</param>
public void IncreaseOffset(uint amount)
{
if (Length < amount)
throw new Exception("Out of bounds!");
Offset += amount;
Length -= amount;
}
/// <summary>
/// Creates a managed object at the current data position
/// </summary>
/// <typeparam name="T">Type of managed structure</typeparam>
/// <param name="startindex">Additional starting offset</param>
/// <returns>Managed structure filled with corresponding raw data</returns>
public T ToIfrType<T>(uint startindex = 0)
{
// Sanity check of broken structs..
if (0 == typeof(T).StructLayoutAttribute.Size)
throw new Exception("Hey dev, assign structure size for \"" + typeof(T).ToString() + "\" in order to allow size checking!");
// Pin the buffer and copy structure into managed type..
int PtrOffset = (int)(Offset + startindex);
GCHandle handle = GCHandle.Alloc(Bytes, GCHandleType.Pinned);
T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + PtrOffset, typeof(T));
handle.Free();
// Check if casted structure is within our memory
if (Length < typeof(T).StructLayoutAttribute.Size)
throw new Exception("Data has " + Length + " bytes left. Casting to " + typeof(T).StructLayoutAttribute.Size + " bytes long \"" + typeof(T).ToString() + "\" failed!");
return result;
}
#endregion
#region Debug methods
/// <summary>
/// Dumps the selection of IFR raw data block to log window
/// </summary>
/// <param name="title">Firendly name of the dumped object</param>
/// <param name="bytesPerLine">Amount of bytes shown at a single line</param>
/// <returns>Generated message</returns>
public string GenerateAndLogDump(string title = "Unnamed", uint bytesPerLine = 16)
{
string message = "Data \"" + title + "\" dumped (Offset=" + Offset + ", Length=" + Length + "):" + Environment.NewLine + CopyOfSelectedBytes.HexDump(bytesPerLine);
IFRHelper.CreateLogEntry(LogSeverity.INFO, "Debug", message);
return message;
}
#endregion
}
/// <summary>
/// Severity for console messages
/// </summary>
public enum LogSeverity { INFO, SUCCESS, STATUS, WARNING, ERROR, UNIMPLEMENTED };
static class IFRHelper
{
#region Methods for generic types
public static T[] SubArray<T>(this T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
public static uint GetPhysSize<T>(this T s) where T : struct
{
uint result = (uint)s.GetType().StructLayoutAttribute.Size;
// Sanity check of broken structs..
if (0 == result)
throw new Exception("Hey dev, get your structure size of \"" + typeof(T).ToString() + "\" fixed!");
return result;
}
#endregion
#region Methods specific for byte arrays
/// <summary>
/// Dumps a byte array to debugger console
/// </summary>
/// <param name="data">Data that shall be dumped</param>
/// <param name="bytesPerLine">Amount of bytes shown at a single line</param>
public static void DumpToDebugConsole(this byte[] bytes, uint bytesPerLine = 16)
{
System.Console.WriteLine(bytes.HexDump(bytesPerLine));
}
/// <summary>
/// Dumps a byte array
/// (see: http://stackoverflow.com/questions/26206257/packet-dump-hex-view-format-for-byte-to-text-file)
/// </summary>
/// <param name="bytes">Byte array to be dumped</param>
/// <param name="bytesPerLine">Amount of bytes shown at a single line</param>
/// <returns>String holding the hexdump</returns>
public static string HexDump(this byte[] bytes, uint BytesPerLine = 16)
{
int bytesPerLine = (int)BytesPerLine;
if (bytes == null) return "<null>";
int bytesLength = bytes.Length;
char[] HexChars = "0123456789ABCDEF".ToCharArray();
int firstHexColumn =
8 // 8 characters for the address
+ 3; // 3 spaces
int firstCharColumn = firstHexColumn
+ bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
+ 2; // 2 spaces
int lineLength = firstCharColumn
+ bytesPerLine // - characters to show the ascii value
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
char[] line = (new String(' ', lineLength - 2) + Environment.NewLine).ToCharArray();
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
System.Text.StringBuilder result = new System.Text.StringBuilder(expectedLines * lineLength);
for (int i = 0; i < bytesLength; i += bytesPerLine)
{
line[0] = HexChars[(i >> 28) & 0xF];
line[1] = HexChars[(i >> 24) & 0xF];
line[2] = HexChars[(i >> 20) & 0xF];
line[3] = HexChars[(i >> 16) & 0xF];
line[4] = HexChars[(i >> 12) & 0xF];
line[5] = HexChars[(i >> 8) & 0xF];
line[6] = HexChars[(i >> 4) & 0xF];
line[7] = HexChars[(i >> 0) & 0xF];
int hexColumn = firstHexColumn;
int charColumn = firstCharColumn;
for (int j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
byte b = bytes[i + j];
line[hexColumn] = HexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = HexChars[b & 0xF];
line[charColumn] = (b < 32 ? '·' : (char)b);
}
hexColumn += 3;
charColumn++;
}
result.Append(line);
}
return result.ToString();
}
#endregion
#region Methods specific for numeric bitmasks
private static T _internal_GetBits<T>(UINT64 Value, UINT64 BitMask = UINT64.MaxValue, UINT8 ShiftedOffset = 0)
{
return (T)(dynamic)((Value >> ShiftedOffset) & BitMask);
}
#region GetBits[Type] wrappers
/// <summary>
/// Gets a typecasted value from a bit mask represented by the object's numeric value.
/// Origin's value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type for typecasted value</typeparam>
/// <param name="Value">The objects numeric value</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
/// <returns>Value of selected bits</returns>
public static T GetBits<T>(this UINT64 Value, UINT64 BitMask = UINT64.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_GetBits<T>(Value, BitMask, ShiftedOffset);
}
/// <summary>
/// Gets a typecasted value from a bit mask represented by the object's numeric value.
/// Origin's value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type for typecasted value</typeparam>
/// <param name="Value">The objects numeric value</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
/// <returns>Value of selected bits</returns>
public static T GetBits<T>(this UINT32 Value, UINT32 BitMask = UINT32.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_GetBits<T>(Value, BitMask, ShiftedOffset);
}
/// <summary>
/// Gets a typecasted value from a bit mask represented by the object's numeric value.
/// Origin's value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type for typecasted value</typeparam>
/// <param name="Value">The objects numeric value</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
/// <returns>Value of selected bits</returns>
public static T GetBits<T>(this UINT16 Value, UINT16 BitMask = UINT16.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_GetBits<T>(Value, BitMask, ShiftedOffset);
}
/// <summary>
/// Gets a typecasted value from a bit mask represented by the object's numeric value.
/// Origin's value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type for typecasted value</typeparam>
/// <param name="Value">The objects numeric value</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
/// <returns>Value of selected bits</returns>
public static T GetBits<T>(this UINT8 Value, UINT8 BitMask = UINT8.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_GetBits<T>(Value, BitMask, ShiftedOffset);
}
#endregion
private static T _internal_SetBits<T>(UINT64 Value, UINT64 NewValue, UINT64 BitMask = UINT64.MaxValue, UINT8 ShiftedOffset = 0)
{
return (T)(dynamic)((Value & ~(BitMask << ShiftedOffset)) | ((NewValue & BitMask) << ShiftedOffset));
}
#region SetBits[Type] wrappers
/// <summary>
/// Sets bits of the object's numeric value to a typecasted numeric value in the masked bitfield.
/// Given value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type of given numeric value</typeparam>
/// <param name="OldValue">The objects numeric value</param>
/// <param name="NewValue">Value of given type to set</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
public static UINT64 SetBits<T>(UINT64 OldValue, T NewValue, UINT64 BitMask = UINT64.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_SetBits<UINT64>(OldValue, (UINT64)(dynamic)NewValue, BitMask, ShiftedOffset);
}
/// <summary>
/// Sets bits of the object's numeric value to a typecasted numeric value in the masked bitfield.
/// Given value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type of given numeric value</typeparam>
/// <param name="OldValue">The objects numeric value</param>
/// <param name="NewValue">Value of given type to set</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
public static UINT32 SetBits<T>(UINT32 OldValue, T NewValue, UINT32 BitMask = UINT32.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_SetBits<UINT32>(OldValue, (UINT64)(dynamic)NewValue, BitMask, ShiftedOffset);
}
/// <summary>
/// Sets bits of the object's numeric value to a typecasted numeric value in the masked bitfield.
/// Given value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type of given numeric value</typeparam>
/// <param name="OldValue">The objects numeric value</param>
/// <param name="NewValue">Value of given type to set</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
public static UINT16 SetBits<T>(UINT16 OldValue, T NewValue, UINT16 BitMask = UINT16.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_SetBits<UINT16>(OldValue, (UINT64)(dynamic)NewValue, BitMask, ShiftedOffset);
}
/// <summary>
/// Sets bits of the object's numeric value to a typecasted numeric value in the masked bitfield.
/// Given value gets shifted first and then masked.
/// If you plan to keep bit position alive, simply use BitMask only.
/// </summary>
/// <typeparam name="T">Type of given numeric value</typeparam>
/// <param name="OldValue">The objects numeric value</param>
/// <param name="NewValue">Value of given type to set</param>
/// <param name="BitMask">Mask of relevant bits for value, default = 0xFFFFFFFFFFFFFFFF</param>
/// <param name="ShiftedOffset">Amount of bits shifted, default = 0</param>
public static UINT8 SetBits<T>(UINT8 OldValue, T NewValue, UINT8 BitMask = UINT8.MaxValue, UINT8 ShiftedOffset = 0) where T : IConvertible
{
return _internal_SetBits<UINT8>(OldValue, (UINT64)(dynamic)NewValue, BitMask, ShiftedOffset);
}
#endregion
#endregion
#region Debug and console functions
/// <summary>
/// Debug window which is used to print debug messages
/// </summary>
public static DataGridView log = null;
/// <summary>
/// Generates a logged message and adds it to the logging window
/// </summary>
/// <param name="severity">Severity of message</param>
/// <param name="origin">Short origin name of message (to group messges)</param>
/// <param name="msg">Message string</param>
/// <param name="bShowMsgBox">Shows message box when true</param>
public static void CreateLogEntry(LogSeverity severity, string origin, string msg, bool bShowMsgBox = false)
{
string typename = severity.ToString();
Color color;
MessageBoxIcon Icon;
switch (severity)
{
case LogSeverity.SUCCESS: color = Color.LimeGreen; Icon = MessageBoxIcon.None; break;
case LogSeverity.STATUS: color = Color.LightGreen; Icon = MessageBoxIcon.Information; break;
case LogSeverity.WARNING: color = Color.LightCoral; Icon = MessageBoxIcon.Warning; break;
case LogSeverity.ERROR: color = Color.OrangeRed; Icon = MessageBoxIcon.Error; break;
case LogSeverity.UNIMPLEMENTED: color = Color.HotPink; Icon = MessageBoxIcon.Exclamation; break;
//case LogSeverity.INFO:
default: color = Color.White; Icon = MessageBoxIcon.None; break;
}
//Is debug window connected?
if (log != null)
{
// print debug message and assign color
log.Rows[log.Rows.Add(new object[]{ typename, origin, msg.Replace(Environment.NewLine, Environment.NewLine + " > ") })].SetRowBackgroundColor(color);
log.AutoResizeColumns();
log.AutoResizeRow(log.Rows.Count - 1);
}
// Show error in message box?
if (bShowMsgBox) MessageBox.Show(msg, origin, MessageBoxButtons.OK, Icon);
}
public static void SetRowBackgroundColor(this DataGridViewRow row, Color color)
{
foreach (DataGridViewCell col in row.Cells)
col.Style.BackColor = color;
}
#endregion
}
#endregion
#region Definitions for Package Lists and Package Headers (Section 27.3.1)
/*
///
/// The header found at the start of each package list.
///
typedef struct {
EFI_GUID PackageListGuid;
UINT32 PackageLength;
}
EFI_HII_PACKAGE_LIST_HEADER;
*/
/// <summary>
/// The header found at the start of each package.
/// </summary>
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack=1, Size=4)]
struct EFI_HII_PACKAGE_HEADER
{
[FieldOffset(0)]
private UINT32 _Length;
[FieldOffset(3)]
private UINT8 _Type;
// UINT8 Data[...];
public UINT32 Length { get { return _Length.GetBits<UINT32>(0x00FFFFFF); } set { _Length = SetBits<UINT32>(_Length, value, 0x00FFFFFF); } }
public EFI_HII_PACKAGE_e Type { get { return _Type.GetBits<EFI_HII_PACKAGE_e>(); } set { _Type = SetBits(_Type, value); } }
};
/// <summary>
/// Value of HII package type
/// </summary>
public enum EFI_HII_PACKAGE_e
{
EFI_HII_PACKAGE_TYPE_ALL = 0x00,
EFI_HII_PACKAGE_TYPE_GUID = 0x01,
EFI_HII_PACKAGE_FORMS = 0x02,
EFI_HII_PACKAGE_STRINGS = 0x04,
EFI_HII_PACKAGE_FONTS = 0x05,
EFI_HII_PACKAGE_IMAGES = 0x06,
EFI_HII_PACKAGE_SIMPLE_FONTS = 0x07,
EFI_HII_PACKAGE_DEVICE_PATH = 0x08,
EFI_HII_PACKAGE_KEYBOARD_LAYOUT = 0x09,
EFI_HII_PACKAGE_ANIMATIONS = 0x0A,
EFI_HII_PACKAGE_END = 0xDF,
EFI_HII_PACKAGE_TYPE_SYSTEM_BEGIN = 0xE0,
EFI_HII_PACKAGE_TYPE_SYSTEM_END = 0xFF,
};
#endregion
#region Definitions for Simplified Font Package
/*
///
/// Contents of EFI_NARROW_GLYPH.Attributes.
///@{
#define EFI_GLYPH_NON_SPACING 0x01
#define EFI_GLYPH_WIDE 0x02
#define EFI_GLYPH_HEIGHT 19
#define EFI_GLYPH_WIDTH 8
///@}
///
/// The EFI_NARROW_GLYPH has a preferred dimension (w x h) of 8 x 19 pixels.
///
typedef struct {
///
/// The Unicode representation of the glyph. The term weight is the
/// technical term for a character code.
///
CHAR16 UnicodeWeight;
///
/// The data element containing the glyph definitions.
///
UINT8 Attributes;
///
/// The column major glyph representation of the character. Bits
/// with values of one indicate that the corresponding pixel is to be
/// on when normally displayed; those with zero are off.
///
UINT8 GlyphCol1[EFI_GLYPH_HEIGHT];
} EFI_NARROW_GLYPH;
///
/// The EFI_WIDE_GLYPH has a preferred dimension (w x h) of 16 x 19 pixels, which is large enough
/// to accommodate logographic characters.
///
typedef struct {
///
/// The Unicode representation of the glyph. The term weight is the
/// technical term for a character code.
///
CHAR16 UnicodeWeight;
///
/// The data element containing the glyph definitions.
///
UINT8 Attributes;
///
/// The column major glyph representation of the character. Bits
/// with values of one indicate that the corresponding pixel is to be
/// on when normally displayed; those with zero are off.
///
UINT8 GlyphCol1[EFI_GLYPH_HEIGHT];
///
/// The column major glyph representation of the character. Bits
/// with values of one indicate that the corresponding pixel is to be
/// on when normally displayed; those with zero are off.
///
UINT8 GlyphCol2[EFI_GLYPH_HEIGHT];
///
/// Ensures that sizeof (EFI_WIDE_GLYPH) is twice the
/// sizeof (EFI_NARROW_GLYPH). The contents of Pad must
/// be zero.
///
UINT8 Pad[3];
} EFI_WIDE_GLYPH;
///
/// A simplified font package consists of a font header
/// followed by a series of glyph structures.
///
typedef struct _EFI_HII_SIMPLE_FONT_PACKAGE_HDR
{
EFI_HII_PACKAGE_HEADER Header;
UINT16 NumberOfNarrowGlyphs;
UINT16 NumberOfWideGlyphs;
// EFI_NARROW_GLYPH NarrowGlyphs[];
// EFI_WIDE_GLYPH WideGlyphs[];
}
EFI_HII_SIMPLE_FONT_PACKAGE_HDR;
//
// Definitions for Font Package
// Section 27.3.3
//
//
// Value for font style
//
#define EFI_HII_FONT_STYLE_NORMAL 0x00000000
#define EFI_HII_FONT_STYLE_BOLD 0x00000001
#define EFI_HII_FONT_STYLE_ITALIC 0x00000002
#define EFI_HII_FONT_STYLE_EMBOSS 0x00010000
#define EFI_HII_FONT_STYLE_OUTLINE 0x00020000
#define EFI_HII_FONT_STYLE_SHADOW 0x00040000
#define EFI_HII_FONT_STYLE_UNDERLINE 0x00080000
#define EFI_HII_FONT_STYLE_DBL_UNDER 0x00100000
typedef struct _EFI_HII_GLYPH_INFO
{
UINT16 Width;
UINT16 Height;
INT16 OffsetX;
INT16 OffsetY;
INT16 AdvanceX;
}
EFI_HII_GLYPH_INFO;
///
/// The fixed header consists of a standard record header,
/// then the character values in this section, the flags
/// (including the encoding method) and the offsets of the glyph
/// information, the glyph bitmaps and the character map.
///
typedef struct _EFI_HII_FONT_PACKAGE_HDR
{
EFI_HII_PACKAGE_HEADER Header;
UINT32 HdrSize;
UINT32 GlyphBlockOffset;
EFI_HII_GLYPH_INFO Cell;
EFI_HII_FONT_STYLE FontStyle;
CHAR16 FontFamily[1];
}
EFI_HII_FONT_PACKAGE_HDR;
//
// Value of different glyph info block types
//
#define EFI_HII_GIBT_END 0x00
#define EFI_HII_GIBT_GLYPH 0x10
#define EFI_HII_GIBT_GLYPHS 0x11
#define EFI_HII_GIBT_GLYPH_DEFAULT 0x12
#define EFI_HII_GIBT_GLYPHS_DEFAULT 0x13
#define EFI_HII_GIBT_DUPLICATE 0x20
#define EFI_HII_GIBT_SKIP2 0x21
#define EFI_HII_GIBT_SKIP1 0x22
#define EFI_HII_GIBT_DEFAULTS 0x23
#define EFI_HII_GIBT_EXT1 0x30
#define EFI_HII_GIBT_EXT2 0x31
#define EFI_HII_GIBT_EXT4 0x32
typedef struct _EFI_HII_GLYPH_BLOCK
{
UINT8 BlockType;
}
EFI_HII_GLYPH_BLOCK;
//
// Definition of different glyph info block types
//
typedef struct _EFI_HII_GIBT_DEFAULTS_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
EFI_HII_GLYPH_INFO Cell;
}
EFI_HII_GIBT_DEFAULTS_BLOCK;
typedef struct _EFI_HII_GIBT_DUPLICATE_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
CHAR16 CharValue;
}
EFI_HII_GIBT_DUPLICATE_BLOCK;
typedef struct _EFI_GLYPH_GIBT_END_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
}
EFI_GLYPH_GIBT_END_BLOCK;
typedef struct _EFI_HII_GIBT_EXT1_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT8 BlockType2;
UINT8 Length;
}
EFI_HII_GIBT_EXT1_BLOCK;
typedef struct _EFI_HII_GIBT_EXT2_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT8 BlockType2;
UINT16 Length;
}
EFI_HII_GIBT_EXT2_BLOCK;
typedef struct _EFI_HII_GIBT_EXT4_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT8 BlockType2;
UINT32 Length;
}
EFI_HII_GIBT_EXT4_BLOCK;
typedef struct _EFI_HII_GIBT_GLYPH_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
EFI_HII_GLYPH_INFO Cell;
UINT8 BitmapData[1];
}
EFI_HII_GIBT_GLYPH_BLOCK;
typedef struct _EFI_HII_GIBT_GLYPHS_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
EFI_HII_GLYPH_INFO Cell;
UINT16 Count;
UINT8 BitmapData[1];
}
EFI_HII_GIBT_GLYPHS_BLOCK;
typedef struct _EFI_HII_GIBT_GLYPH_DEFAULT_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT8 BitmapData[1];
}
EFI_HII_GIBT_GLYPH_DEFAULT_BLOCK;
typedef struct _EFI_HII_GIBT_GLYPHS_DEFAULT_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT16 Count;
UINT8 BitmapData[1];
}
EFI_HII_GIBT_GLYPHS_DEFAULT_BLOCK;
typedef struct _EFI_HII_GIBT_SKIP1_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT8 SkipCount;
}
EFI_HII_GIBT_SKIP1_BLOCK;
typedef struct _EFI_HII_GIBT_SKIP2_BLOCK
{
EFI_HII_GLYPH_BLOCK Header;
UINT16 SkipCount;
}
EFI_HII_GIBT_SKIP2_BLOCK;
*/
#endregion
#region Definitions for Device Path Package (Section 27.3.4)
/*
///
/// The device path package is used to carry a device path
/// associated with the package list.
///
typedef struct _EFI_HII_DEVICE_PATH_PACKAGE_HDR
{
EFI_HII_PACKAGE_HEADER Header;
// EFI_DEVICE_PATH_PROTOCOL DevicePath[];
}
EFI_HII_DEVICE_PATH_PACKAGE_HDR;
*/
#endregion
#region Definitions for GUID Package (Section 27.3.5)
/*
///
/// The GUID package is used to carry data where the format is defined by a GUID.
///
typedef struct _EFI_HII_GUID_PACKAGE_HDR
{
EFI_HII_PACKAGE_HEADER Header;
EFI_GUID Guid;
// Data per GUID definition may follow
}
EFI_HII_GUID_PACKAGE_HDR;
*/
#endregion
#region Definitions for String Package (Section 27.3.6)
/*
#define UEFI_CONFIG_LANG "x-UEFI"
#define UEFI_CONFIG_LANG_2 "x-i-UEFI"
*/
/// <summary>
/// The fixed header consists of a standard record header and then the string identifiers
/// contained in this section and the offsets of the string and language information.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 46)]
struct EFI_HII_STRING_PACKAGE_HDR
{
public EFI_HII_PACKAGE_HEADER Header;
public UINT32 HdrSize;
public UINT32 StringInfoOffset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public CHAR16[] LanguageWindow;
public EFI_STRING_ID LanguageName;
// CHAR8 Language[...];
// EFI_HII_STRING_BLOCK Blocks[...];
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 1)]
struct EFI_HII_STRING_BLOCK
{
private UINT8 _BlockType;
// UINT8 BlockBody[...];
public EFI_HII_SIBT_e BlockType { get { return _BlockType.GetBits<EFI_HII_SIBT_e>(); } set { _BlockType = SetBits(_BlockType, value); } }
}
/// <summary>
/// Value of different string information block types
/// </summary>
enum EFI_HII_SIBT_e
{
EFI_HII_SIBT_END = 0x00,
EFI_HII_SIBT_STRING_SCSU = 0x10,
EFI_HII_SIBT_STRING_SCSU_FONT = 0x11,
EFI_HII_SIBT_STRINGS_SCSU = 0x12,
EFI_HII_SIBT_STRINGS_SCSU_FONT = 0x13,
EFI_HII_SIBT_STRING_UCS2 = 0x14,
EFI_HII_SIBT_STRING_UCS2_FONT = 0x15,
EFI_HII_SIBT_STRINGS_UCS2 = 0x16,
EFI_HII_SIBT_STRINGS_UCS2_FONT = 0x17,
EFI_HII_SIBT_DUPLICATE = 0x20,
EFI_HII_SIBT_SKIP2 = 0x21,
EFI_HII_SIBT_SKIP1 = 0x22,
EFI_HII_SIBT_EXT1 = 0x30,
EFI_HII_SIBT_EXT2 = 0x31,
EFI_HII_SIBT_EXT4 = 0x32,
EFI_HII_SIBT_FONT = 0x40,
};
/*
//
// Definition of different string information block types
//
typedef struct _EFI_HII_SIBT_DUPLICATE_BLOCK
{
EFI_HII_STRING_BLOCK Header;
EFI_STRING_ID StringId;
}
EFI_HII_SIBT_DUPLICATE_BLOCK;
*/
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 3)]
struct EFI_HII_SIBT_EXT1_BLOCK
{
public EFI_HII_STRING_BLOCK Header;
public UINT8 BlockType2;
public UINT8 Length;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 4)]
struct EFI_HII_SIBT_EXT2_BLOCK
{
public EFI_HII_STRING_BLOCK Header;
public UINT8 BlockType2;
public UINT16 Length;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 6)]
struct EFI_HII_SIBT_EXT4_BLOCK
{
public EFI_HII_STRING_BLOCK Header;
public UINT8 BlockType2;
public UINT32 Length;
}
/*
typedef struct _EFI_HII_SIBT_FONT_BLOCK
{
EFI_HII_SIBT_EXT2_BLOCK Header;
UINT8 FontId;
UINT16 FontSize;
EFI_HII_FONT_STYLE FontStyle;
CHAR16 FontName[1];
}
EFI_HII_SIBT_FONT_BLOCK;