Skip to content

Commit 8303376

Browse files
Merge pull request #15 from acompany-develop/imamura/refactor/autodetection
refactor(types)!: reduce args in detect_processor_model()
2 parents ace4ddd + 451b92e commit 8303376

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

pysnputils/types.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,31 @@
3535

3636
# Helper functions
3737
def get_bit(raw: bytes, bit: int) -> int:
38-
"""Get a bit from a byte array."""
38+
"""
39+
Get a bit from a byte array.
40+
41+
Args:
42+
raw: bytes
43+
bit: int
44+
45+
Returns:
46+
int
47+
"""
3948
return (int.from_bytes(raw, "little") >> bit) & 1
4049

4150

4251
def get_bits(raw: bytes, begin: int, end: int) -> int:
43-
"""Get bits from a byte array."""
52+
"""
53+
Get bits from a byte array.
54+
55+
Args:
56+
raw: bytes
57+
begin: int
58+
end: int
59+
60+
Returns:
61+
int
62+
"""
4463
return (int.from_bytes(raw, "little") >> begin) & ((1 << (end - begin)) - 1)
4564

4665

@@ -81,7 +100,15 @@ class ReportVariant(IntEnum):
81100

82101
# Functions
83102
def report_version_to_variant(version: int) -> ReportVariant:
84-
"""Convert a report version to a report variant."""
103+
"""
104+
Convert a report version to a report variant.
105+
106+
Args:
107+
version: int
108+
109+
Returns:
110+
ReportVariant
111+
"""
85112
if version == 2:
86113
return ReportVariant.V2
87114
if version in [3, 4]:
@@ -91,14 +118,17 @@ def report_version_to_variant(version: int) -> ReportVariant:
91118
raise ValueError(f"invalid or unsupported report version: {version}")
92119

93120

94-
def detect_processor_model(report_version: int, cpuid_fam_id: int | None, cpuid_mod_id: int | None) -> ProcessorModel:
95-
"""Detect the processor model from the CPUID family and model."""
96-
if report_version < 3:
97-
raise ValueError("Processor model could not be determined; update SEV-SNP firmware to bump report version to V3 or later")
98-
if cpuid_fam_id is None:
99-
raise ValueError("missing CPUID family ID")
100-
if cpuid_mod_id is None:
101-
raise ValueError("missing CPUID model ID")
121+
def detect_processor_model(cpuid_fam_id: int, cpuid_mod_id: int) -> ProcessorModel:
122+
"""
123+
Detect the processor model from the CPUID family and model.
124+
125+
Args:
126+
cpuid_fam_id: int
127+
cpuid_mod_id: int
128+
129+
Returns:
130+
ProcessorModel
131+
"""
102132
if cpuid_fam_id == 0x19:
103133
if cpuid_mod_id in range(0x00, 0x10):
104134
return ProcessorModel.MILAN
@@ -630,7 +660,7 @@ def __init__(self, raw: bytes, processor_model: ProcessorModel | None = None):
630660
raise ValueError("Report version must be 3+ or processor model must be specified")
631661
cpuid_fam_id = int.from_bytes(raw[0x188:0x189], "little")
632662
cpuid_mod_id = int.from_bytes(raw[0x189:0x18A], "little")
633-
self.processor_model = detect_processor_model(version, cpuid_fam_id, cpuid_mod_id)
663+
self.processor_model = detect_processor_model(cpuid_fam_id, cpuid_mod_id)
634664
else:
635665
self.processor_model = ProcessorModel(processor_model)
636666

0 commit comments

Comments
 (0)