]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/Edk2/Capsule/FmpPayloadHeader.py
0ed51752d3eae575013ad4eb92ab56a3d915fa79
[mirror_edk2.git] / BaseTools / Source / Python / Common / Edk2 / Capsule / FmpPayloadHeader.py
1 ## @file
2 # Module that encodes and decodes a FMP_PAYLOAD_HEADER with a payload.
3 # The FMP_PAYLOAD_HEADER is processed by the FmpPayloadHeaderLib in the
4 # FmpDevicePkg.
5 #
6 # Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
7 # This program and the accompanying materials
8 # are licensed and made available under the terms and conditions of the BSD License
9 # which accompanies this distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15
16 '''
17 FmpPayloadHeader
18 '''
19
20 import struct
21
22 def _SIGNATURE_32 (A, B, C, D):
23 return struct.unpack ('=I',bytearray (A + B + C + D, 'ascii'))[0]
24
25 def _SIGNATURE_32_TO_STRING (Signature):
26 return struct.pack ("<I", Signature).decode ()
27
28 class FmpPayloadHeaderClass (object):
29 #
30 # typedef struct {
31 # UINT32 Signature;
32 # UINT32 HeaderSize;
33 # UINT32 FwVersion;
34 # UINT32 LowestSupportedVersion;
35 # } FMP_PAYLOAD_HEADER;
36 #
37 # #define FMP_PAYLOAD_HEADER_SIGNATURE SIGNATURE_32 ('M', 'S', 'S', '1')
38 #
39 _StructFormat = '<IIII'
40 _StructSize = struct.calcsize (_StructFormat)
41
42 _FMP_PAYLOAD_HEADER_SIGNATURE = _SIGNATURE_32 ('M', 'S', 'S', '1')
43
44 def __init__ (self):
45 self._Valid = False
46 self.Signature = self._FMP_PAYLOAD_HEADER_SIGNATURE
47 self.HeaderSize = self._StructSize
48 self.FwVersion = 0x00000000
49 self.LowestSupportedVersion = 0x00000000
50 self.Payload = b''
51
52 def Encode (self):
53 FmpPayloadHeader = struct.pack (
54 self._StructFormat,
55 self.Signature,
56 self.HeaderSize,
57 self.FwVersion,
58 self.LowestSupportedVersion
59 )
60 self._Valid = True
61 return FmpPayloadHeader + self.Payload
62
63 def Decode (self, Buffer):
64 if len (Buffer) < self._StructSize:
65 raise ValueError
66 (Signature, HeaderSize, FwVersion, LowestSupportedVersion) = \
67 struct.unpack (
68 self._StructFormat,
69 Buffer[0:self._StructSize]
70 )
71 if Signature != self._FMP_PAYLOAD_HEADER_SIGNATURE:
72 raise ValueError
73 if HeaderSize < self._StructSize:
74 raise ValueError
75 self.Signature = Signature
76 self.HeaderSize = HeaderSize
77 self.FwVersion = FwVersion
78 self.LowestSupportedVersion = LowestSupportedVersion
79 self.Payload = Buffer[self.HeaderSize:]
80
81 self._Valid = True
82 return self.Payload
83
84 def DumpInfo (self):
85 if not self._Valid:
86 raise ValueError
87 print ('FMP_PAYLOAD_HEADER.Signature = {Signature:08X} ({SignatureString})'.format (Signature = self.Signature, SignatureString = _SIGNATURE_32_TO_STRING (self.Signature)))
88 print ('FMP_PAYLOAD_HEADER.HeaderSize = {HeaderSize:08X}'.format (HeaderSize = self.HeaderSize))
89 print ('FMP_PAYLOAD_HEADER.FwVersion = {FwVersion:08X}'.format (FwVersion = self.FwVersion))
90 print ('FMP_PAYLOAD_HEADER.LowestSupportedVersion = {LowestSupportedVersion:08X}'.format (LowestSupportedVersion = self.LowestSupportedVersion))
91 print ('sizeof (Payload) = {Size:08X}'.format (Size = len (self.Payload)))