From d33ed7ef55527a27aadfb48eefe0ca68e9a28755 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Fri, 11 Oct 2019 16:08:41 +0200 Subject: [PATCH] Prepare parsing RGN files. --- grmn/__init__.py | 1 + grmn/rgn.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++ grmn/rgnbin.py | 26 +++++++++ rgnstruct.py | 18 +++++++ 4 files changed, 183 insertions(+) create mode 100644 grmn/rgn.py create mode 100644 grmn/rgnbin.py create mode 100644 rgnstruct.py diff --git a/grmn/__init__.py b/grmn/__init__.py index faab907..8fca0f9 100644 --- a/grmn/__init__.py +++ b/grmn/__init__.py @@ -1,4 +1,5 @@ """Library for firmware reading/writing.""" from .gcd import * +from .rgn import * from .chksum import * diff --git a/grmn/rgn.py b/grmn/rgn.py new file mode 100644 index 0000000..8e22c62 --- /dev/null +++ b/grmn/rgn.py @@ -0,0 +1,138 @@ +# -*- coding: utf-8 -*- +# Thanks to Herbert Oppmann (herby) for all your work! + +from .chksum import ChkSum +from .rgnbin import RgnBin +from struct import unpack +import configparser + +RGN_SIG = b"KpGr" + +# RGN structure might be: RGN > BIN or RGN > RGN > BIN +# RGN = outside hull +# BIN = firmware + hwid + checksum + +class ParseException(Exception): + pass + +class Rgn: + def __init__(self, filename: str=None): + self.filename = filename + self.struct = [] + if filename is not None: + self.load() + + def load(self): + if self.filename is None: + return False + last_tlv6 = None + last_tlv7 = None + with open(self.filename, "rb") as f: + sig = f.read(4) + if sig != RGN_SIG: + raise ParseException("Signature mismatch ({}, should be {})!".format(repr(sig), repr(RGN_SIG))) + self.version = unpack(" BIN or RGN > RGN > BIN +# RGN = outside hull +# BIN = firmware + hwid + checksum + +class ParseException(Exception): + pass + +class RgnBin: + def __init__(self, filename: str=None): + self.filename = filename + self.struct = [] + if filename is not None: + self.load() + + def load(self): + if self.filename is None: + return False + with open(self.filename, "rb") as f: + f.close() diff --git a/rgnstruct.py b/rgnstruct.py new file mode 100644 index 0000000..ae0b140 --- /dev/null +++ b/rgnstruct.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Prints out the structure of the given RGN file. +""" + +from grmn import Rgn +import sys + +FILE = sys.argv[1] + +print("Opening {}".format(FILE)) + +rgn = Rgn(FILE) + +#rgn.print_struct() +#rgn.validate(True)