summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/DriverSampleDxe
diff options
context:
space:
mode:
authorBi, Dandan <dandan.bi@intel.com>2018-02-08 22:28:39 +0800
committerLiming Gao <liming.gao@intel.com>2018-02-09 17:04:15 +0800
commit6b86f6f336b9ccfdc27b718100c31b2aed5d7bb1 (patch)
treef2b3b8eec76bad1d46398085cc3505de0b9bf443 /MdeModulePkg/Universal/DriverSampleDxe
parentce2697abeb37cf9fabab15327385415599e8fb0c (diff)
downloadedk2-6b86f6f336b9ccfdc27b718100c31b2aed5d7bb1.tar.gz
edk2-6b86f6f336b9ccfdc27b718100c31b2aed5d7bb1.tar.bz2
edk2-6b86f6f336b9ccfdc27b718100c31b2aed5d7bb1.zip
MdeModulePkg/DriverSampleDxe: Make bit fields aligned in C structure
For a structure with a series of bit fields and used as a storage in vfr file, and if the bit fields do not add up to the size of the defined type.In the C code use sizeof() to get size of the structure, the results may vary form the compiler(VS,GCC...). But the size of the storage calculated by VfrCompiler is fixed (calculate with alignment).To avoid the issue cased by above case, we need to make the total width of the bit fields in the structure aligned with the size of the defined type for these bit fields. Cc: Eric Dong <eric.dong@intel.com> Cc: Liming Gao <liming.gao@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Dandan Bi <dandan.bi@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com>
Diffstat (limited to 'MdeModulePkg/Universal/DriverSampleDxe')
-rw-r--r--MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
index 40fb3d0964..6f092de8b6 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
+++ b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
@@ -35,6 +35,14 @@ Revision History:
#pragma pack(1)
+//
+// !!! For a structure with a series of bit fields and used as a storage in vfr file, and if the bit fields do not add up to the size of the defined type.
+// In the C code use sizeof() to get the size the strucure, the results may vary form the compiler(VS,GCC...).
+// But the size of the storage calculated by VfrCompiler is fixed (calculate with alignment).
+// To avoid above case, we need to make the total bit width in the structure aligned with the size of the defined type for these bit fields. We can:
+// 1. Add bit field (with/without name) with remianing with for padding.
+// 2. Add unnamed bit field with 0 for padding, the amount of padding is determined by the alignment characteristics of the members of the structure.
+//
typedef struct {
UINT16 NestByteField;
UINT8 : 1; // unamed field can be used for padding
@@ -84,7 +92,9 @@ typedef struct {
UINT8 Match2;
UINT8 GetDefaultValueFromCallBackForOrderedList[3];
UINT8 BitCheckbox : 1;
+ UINT8 ReservedBits: 7; // Reserved bit fields for padding.
UINT16 BitOneof : 6;
+ UINT16 : 0; // Width 0 used to force alignment.
UINT16 BitNumeric : 12;
MY_BITS_DATA MyBitData;
MY_EFI_UNION_DATA MyUnionData;
@@ -109,6 +119,7 @@ typedef struct {
UINT32 EfiBitNumeric : 4;
UINT32 EfiBitOneof : 10;
UINT32 EfiBitCheckbox : 1;
+ UINT32 : 0; // Width 0 used to force alignment.
} MY_EFI_BITS_VARSTORE_DATA;
//