From nobody Mon Dec 23 09:26:15 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1518100139903400.4852600139284; Thu, 8 Feb 2018 06:28:59 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E236021F0DA6E; Thu, 8 Feb 2018 06:23:12 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 4C84D22361E43 for ; Thu, 8 Feb 2018 06:23:11 -0800 (PST) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Feb 2018 06:28:52 -0800 Received: from shwdeopenpsi114.ccr.corp.intel.com ([10.239.157.135]) by orsmga004.jf.intel.com with ESMTP; 08 Feb 2018 06:28:51 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.31; helo=mga06.intel.com; envelope-from=dandan.bi@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,479,1511856000"; d="scan'208";a="172730224" From: Dandan Bi To: edk2-devel@lists.01.org Date: Thu, 8 Feb 2018 22:28:39 +0800 Message-Id: <1518100119-6448-1-git-send-email-dandan.bi@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.1 Subject: [edk2] [patch] MdeModulePkg/DriverSampleDxe: Make bit fields aligned in C structure X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Dong , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Dandan Bi Reviewed-by: Liming Gao --- MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h b/MdeModu= lePkg/Universal/DriverSampleDxe/NVDataStruc.h index 40fb3d0..af3d4bc 100644 --- a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h +++ b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h @@ -33,10 +33,18 @@ Revision History: #define CONFIGURATION_VARSTORE_ID 0x1234 #define BITS_VARSTORE_ID 0x2345 =20 #pragma pack(1) =20 +// +// !!! For a structure with a series of bit fields and used as a storage i= n 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 ma= y vary form the compiler(VS,GCC...). +// But the size of the storage calculated by VfrCompiler is fixed (calcula= te with alignment). +// To avoid above case, we need to make the total bit width in the structu= re 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 d= etermined by the alignment characteristics of the members of the structure. +// typedef struct { UINT16 NestByteField; UINT8 : 1; // unamed field can be used for padding UINT8 NestBitCheckbox : 1; UINT8 NestBitOneof : 2; @@ -82,11 +90,13 @@ typedef struct { EFI_HII_TIME Time; UINT8 RefreshGuidCount; 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; } DRIVER_SAMPLE_CONFIGURATION; =20 @@ -107,10 +117,11 @@ typedef struct { MY_BITS_DATA BitsData; UINT32 EfiBitGrayoutTest : 5; UINT32 EfiBitNumeric : 4; UINT32 EfiBitOneof : 10; UINT32 EfiBitCheckbox : 1; + UINT32 : 0; // Width 0 used to force alignment. } MY_EFI_BITS_VARSTORE_DATA; =20 // // Labels definition // --=20 1.9.5.msysgit.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel