From: Lucas De Marchi <lucas.demarchi@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Gustavo Sousa <gustavo.sousa@intel.com>,
Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>,
Kamil Konieczny <kamil.konieczny@linux.intel.com>,
Lucas De Marchi <lucas.demarchi@intel.com>
Subject: [PATCH i-g-t v2 3/6] tools/intel-gfx-fw-info: Embed fw decoding
Date: Tue, 20 Aug 2024 16:29:25 -0700 [thread overview]
Message-ID: <20240820232928.458248-4-lucas.demarchi@intel.com> (raw)
In-Reply-To: <20240820232928.458248-1-lucas.demarchi@intel.com>
Use a factory class method to decide what class to use depending on the
magic and embed the calls to cstruct in each of them. Main motivation is
that in future the decode method will need to seek the binary as it
can't really know the version just checking the beginning of the file.
For now, keep the open/close of the file in the main function, but
eventually can be migrated as well.
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
---
tools/intel-gfx-fw-info | 73 ++++++++++++++++++++++++++++-------------
1 file changed, 51 insertions(+), 22 deletions(-)
diff --git a/tools/intel-gfx-fw-info b/tools/intel-gfx-fw-info
index 3105144b7..0f1da8bff 100755
--- a/tools/intel-gfx-fw-info
+++ b/tools/intel-gfx-fw-info
@@ -105,8 +105,38 @@ def FIELD_GET(mask: int, value: int) -> int:
class Fw(abc.ABC):
- def __init__(self, fw):
- self.fw = fw
+ def __init__(self, cparser, f):
+ self.f = f
+ self.cparser = cparser
+ self.fw = None
+
+ def checksum(self):
+ self.f.seek(0, 0)
+ return hashlib.sha256(self.f.read()).hexdigest()
+
+ def dump(self, **kw):
+ cstruct.dumpstruct(self.fw, kw)
+
+ @classmethod
+ def create(cls, f):
+ cparser = cstruct.cstruct()
+ cparser.load(CDEF)
+
+ magic = cparser.magic(f).data
+ default = None
+
+ for s in cls.__subclasses__():
+ if s.MAGIC is None:
+ default = s
+ elif s.MAGIC == magic:
+ f.seek(0, 0)
+ return s(cparser, f)
+
+ if default:
+ f.seek(0, 0)
+ return default(cparser, f)
+
+ return None
@abc.abstractmethod
def decode(self):
@@ -114,8 +144,13 @@ class Fw(abc.ABC):
class FwCss(Fw):
+ MAGIC = None
+
def decode(self):
- data = []
+ self.f.seek(0, 0)
+ self.fw = self.cparser.uc_css_header(self.f)
+
+ data = ["header-type: CSS"]
CSS_SW_VERSION_UC_MAJOR = 0xFF << 16
CSS_SW_VERSION_UC_MINOR = 0xFF << 8
@@ -137,8 +172,13 @@ class FwCss(Fw):
class FwGsc(Fw):
+ MAGIC = b"$CPD"
+
def decode(self):
- data = []
+ self.f.seek(0, 0)
+ self.fw = self.cparser.uc_huc_gsc_header(self.f)
+
+ data = ["header-type: GSC"]
HUC_GSC_MINOR_VER_HI_MASK = 0xFF << 16
HUC_GSC_MAJOR_VER_HI_MASK = 0xFF
@@ -167,40 +207,29 @@ def parse_args(argv: typing.List[str]) -> argparse.Namespace:
return parser.parse_args(argv)
-def calculate_checksum(f: typing.BinaryIO) -> str:
- return hashlib.sha256(f.read()).hexdigest()
-
-
def main(argv: typing.List[str]) -> int:
args = parse_args(argv)
- cparser = cstruct.cstruct()
- cparser.load(CDEF)
-
checksum = None
try:
with open(args.filename, mode="rb") as f:
- magic = cparser.magic(f)
- f.seek(0, 0)
- if magic.data == b"$CPD":
- fw = FwGsc(cparser.uc_huc_gsc_header(f))
- else:
- fw = FwCss(cparser.uc_css_header(f))
+ fw = Fw.create(f)
+ if not fw:
+ logging.fatal("Unknown firmware type in '{args.filename}'")
+ decoded_data = fw.decode()
if args.checksum:
- f.seek(0, 0)
- checksum = calculate_checksum(f)
-
+ checksum = fw.checksum()
except FileNotFoundError as e:
logging.fatal(e)
return 1
- print(*fw.decode(), sep="\n")
+ print(*decoded_data, sep="\n")
if args.raw:
print("raw dump:", end="")
- cstruct.dumpstruct(fw.fw, color=sys.stdout.isatty())
+ fw.dump(color=sys.stdout.isatty())
if checksum:
print(f"checksum: {checksum}")
--
2.43.0
next prev parent reply other threads:[~2024-08-20 23:29 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-20 23:29 [PATCH i-g-t v2 0/6] Parse GSC-based HuC firmware layout Lucas De Marchi
2024-08-20 23:29 ` [PATCH i-g-t v2 1/6] tools/intel-gfx-fw-info: Re-format with black Lucas De Marchi
2024-08-21 11:29 ` Kamil Konieczny
2024-08-21 12:35 ` Lucas De Marchi
2024-08-21 17:11 ` Kamil Konieczny
2024-08-21 16:46 ` Gustavo Sousa
2024-08-20 23:29 ` [PATCH i-g-t v2 2/6] tools/intel-gfx-fw-info: Annotate abstract method Lucas De Marchi
2024-08-21 16:47 ` Gustavo Sousa
2024-08-20 23:29 ` Lucas De Marchi [this message]
2024-08-21 17:06 ` [PATCH i-g-t v2 3/6] tools/intel-gfx-fw-info: Embed fw decoding Gustavo Sousa
2024-08-21 18:17 ` Lucas De Marchi
2024-08-20 23:29 ` [PATCH i-g-t v2 4/6] tools/intel-gfx-fw-info: Override kernel types Lucas De Marchi
2024-08-21 17:18 ` Gustavo Sousa
2024-08-21 18:37 ` Lucas De Marchi
2024-08-20 23:29 ` [PATCH i-g-t v2 5/6] tools/intel-gfx-fw-info: Add proper HuC parser Lucas De Marchi
2024-08-20 23:29 ` [PATCH i-g-t v2 6/6] tools/intel-gfx-fw-info: do not use bare 'except' Lucas De Marchi
2024-08-21 17:19 ` Gustavo Sousa
2024-08-20 23:54 ` ✓ CI.xeBAT: success for Parse GSC-based HuC firmware layout (rev2) Patchwork
2024-08-21 0:11 ` ✓ Fi.CI.BAT: " Patchwork
2024-08-21 3:24 ` ✗ Fi.CI.IGT: failure " Patchwork
2024-08-21 7:57 ` ✓ CI.xeFULL: success " Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240820232928.458248-4-lucas.demarchi@intel.com \
--to=lucas.demarchi@intel.com \
--cc=daniele.ceraolospurio@intel.com \
--cc=gustavo.sousa@intel.com \
--cc=igt-dev@lists.freedesktop.org \
--cc=kamil.konieczny@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox