-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow parsing of RAUC files using `PySquashFsImage`. DB still needs migration.
- Loading branch information
1 parent
f7a0ce7
commit c0e60fd
Showing
8 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
|
||
import aiofiles | ||
import httpx | ||
from anyio import Path, open_file | ||
from . import rauc, swu | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def parse_remote(url: str): | ||
async with httpx.AsyncClient() as c: | ||
file = await c.get(url) | ||
Check failure Code scanning / CodeQL Full server-side request forgery Critical
The full URL of this request depends on a
user-provided value Error loading related location Loading The full URL of this request depends on a user-provided value Error loading related location Loading |
||
async with aiofiles.tempfile.NamedTemporaryFile("w+b") as f: | ||
await f.write(file.content) | ||
return await parse_file(Path(str(f.name))) | ||
|
||
|
||
async def parse_file(file: Path): | ||
async with await open_file(file, "r+b") as f: | ||
magic = await f.read(4) | ||
if magic == swu.MAGIC: | ||
image_format = "SWU" | ||
attributes = await swu.parse_file(file) | ||
elif magic == rauc.MAGIC: | ||
image_format = "RAUC" | ||
attributes = await rauc.parse_file(file) | ||
else: | ||
logger.warning(f"Unknown file format, magic={magic}") | ||
raise ValueError(f"Unknown file format, magic={magic}") | ||
attributes["image_format"] = image_format | ||
return attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import hashlib | ||
|
||
from anyio import AsyncFile | ||
|
||
|
||
async def sha1_hash_file(fileobj: AsyncFile): | ||
last = await fileobj.tell() | ||
await fileobj.seek(0) | ||
sha1_hash = hashlib.sha1() | ||
buf = bytearray(2**18) | ||
view = memoryview(buf) | ||
while True: | ||
size = await fileobj.readinto(buf) | ||
if size == 0: | ||
break | ||
sha1_hash.update(view[:size]) | ||
|
||
await fileobj.seek(last) | ||
return sha1_hash.hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import configparser | ||
import logging | ||
|
||
import semver | ||
from PySquashfsImage import SquashFsImage | ||
from anyio import Path, open_file | ||
|
||
from .func import sha1_hash_file | ||
|
||
MAGIC = b"hsqs" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def parse_file(file: Path): | ||
async with await open_file(file, "r+b") as f: | ||
image_data = await f.read() | ||
|
||
image = SquashFsImage.from_bytes(image_data) | ||
manifest = image.select("manifest.raucm") | ||
manifest_str = manifest.read_bytes().decode("utf-8") | ||
config = configparser.ConfigParser() | ||
config.read_string(manifest_str) | ||
swdesc_attrs = parse_descriptor(config) | ||
|
||
stat = await file.stat() | ||
swdesc_attrs["size"] = stat.st_size | ||
swdesc_attrs["hash"] = await sha1_hash_file(f) | ||
return swdesc_attrs | ||
|
||
|
||
def parse_descriptor(manifest: configparser.ConfigParser): | ||
swdesc_attrs = {} | ||
try: | ||
# specified as optional in the RAUC docs | ||
swdesc_attrs["version"] = semver.Version.parse(manifest["update"].get("version") + ".0") | ||
swdesc_attrs["compatibility"] = [{"hw_model": "default", "hw_revision": manifest["update"]["compatible"]}] | ||
except KeyError as e: | ||
logger.warning(f"Parsing RAUC descriptor failed, error={e}") | ||
raise ValueError("Parsing RAUC descriptor failed", e) | ||
|
||
return swdesc_attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters