Commit fed3889e authored by Dorota Czaplejewicz's avatar Dorota Czaplejewicz

Merge branch 'verify-checksum' into 'master'

Verify checksum and get image size from meta data

See merge request Librem5/librem5-devkit-tools!14
parents ba36e962 bc170625
......@@ -44,6 +44,7 @@ Depends:
python3-jenkins,
python3-requests,
python3-tqdm,
python3-yaml,
usbutils,
uuu,
Description: Tools for the librem5 devkit (host side)
......
......@@ -2,6 +2,7 @@
import argparse
import datetime
import hashlib
import jenkins
import logging
import lzma
......@@ -12,6 +13,7 @@ import subprocess
import sys
import tempfile
import tqdm
import yaml
try:
import coloredlogs
......@@ -26,6 +28,7 @@ JENKINS = 'https://arm01.puri.sm'
TYPE = 'devkit'
DIST = 'buster+ci'
IMAGE = 'devkit.img'
META_YAML = 'files/meta.yml'
IMAGE_JOB_NAME = 'Images/Image Build'
UBOOT = 'u-boot-devkit-recovery.imx'
UBOOT_JOB_NAME = 'u-boot_builds/devkit-recovery_uboot_build'
......@@ -47,20 +50,61 @@ FB: flash -raw2sparse all {image}
FB: Done
'''
BLOCK_SIZE = 8192
# TODO store in image metadata
UNCOMPRESSED_SIZE = 3600000000
class VerifyImageException(Exception):
pass
def verify_image(image, meta):
m = hashlib.sha256()
size = int(meta['image']['size'])
hexdigest = meta['image']['sha256sum']
logging.info("Calculating sha256sum of {}".format(image))
bar = tqdm.tqdm(total=size,
desc='Checking',
leave=False)
with open(image, 'rb') as f:
while True:
data = f.read(BLOCK_SIZE)
if data:
m.update(data)
bar.update(n=len(data))
else:
break
bar.close()
if m.hexdigest() != hexdigest:
raise VerifyImageException("Checksum of image {} "
"does not match {}".format(m.hexdigest(), hexdigest))
def download_image(url, target):
decomp = lzma.LZMADecompressor()
logging.info("Downloading image from {}".format(url))
# We expect metadata to be right next to the image
meta_yml_url = "{}/{}".format(url.rsplit('/', 1)[0], META_YAML)
resp = requests.get(meta_yml_url)
# No meta data is no yet fatal
try:
resp.raise_for_status()
meta = yaml.safe_load(resp.text)
uncompressed_size = int(meta['image']['size'])
logging.debug("Image size is %d", uncompressed_size)
except requests.exceptions.HTTPError:
logging.warning("No meta data found, skipping integrity check")
meta = None
uncompressed_size = UNCOMPRESSED_SIZE
resp = requests.get(url, stream=True)
resp.raise_for_status()
ts = int(resp.headers.get('content-length', 0))
download_bar = tqdm.tqdm(total=ts,
desc='Download',
leave=False)
decompress_bar = tqdm.tqdm(total=UNCOMPRESSED_SIZE,
decompress_bar = tqdm.tqdm(total=uncompressed_size,
desc='Decompr.',
leave=False)
with open(target, 'wb+') as f:
......@@ -72,6 +116,8 @@ def download_image(url, target):
download_bar.update(n=len(data))
download_bar.close()
decompress_bar.close()
if meta:
verify_image(target, meta)
def find_image(jobname, type, dist):
......@@ -189,6 +235,9 @@ def main():
uboot_target)
write_uuu_script(uuu_target, image_target, uboot_target)
flash_image(uuu_target)
except VerifyImageException as e:
logging.error(e)
return 1
except KeyboardInterrupt:
logging.error("CTRL-C pressed.")
return 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment