Commit 415727cf authored by Guido Gunther's avatar Guido Gunther
Browse files

flash-image: Validate checksum of downloaded image

This also uses the meta data provided with the image.
parent af843f80
Pipeline #72902 failed with stage
in 1 minute and 1 second
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import argparse import argparse
import datetime import datetime
import hashlib
import jenkins import jenkins
import logging import logging
import lzma import lzma
...@@ -52,6 +53,33 @@ BLOCK_SIZE = 8192 ...@@ -52,6 +53,33 @@ BLOCK_SIZE = 8192
UNCOMPRESSED_SIZE = 3600000000 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): def download_image(url, target):
decomp = lzma.LZMADecompressor() decomp = lzma.LZMADecompressor()
logging.info("Downloading image from {}".format(url)) logging.info("Downloading image from {}".format(url))
...@@ -66,7 +94,7 @@ def download_image(url, target): ...@@ -66,7 +94,7 @@ def download_image(url, target):
uncompressed_size = int(meta['image']['size']) uncompressed_size = int(meta['image']['size'])
logging.debug("Image size is %d", uncompressed_size) logging.debug("Image size is %d", uncompressed_size)
except requests.exceptions.HTTPError: except requests.exceptions.HTTPError:
logging.warning("No meta data found, size may be incorrect") logging.warning("No meta data found, skipping integrity check")
meta = None meta = None
uncompressed_size = UNCOMPRESSED_SIZE uncompressed_size = UNCOMPRESSED_SIZE
...@@ -88,6 +116,8 @@ def download_image(url, target): ...@@ -88,6 +116,8 @@ def download_image(url, target):
download_bar.update(n=len(data)) download_bar.update(n=len(data))
download_bar.close() download_bar.close()
decompress_bar.close() decompress_bar.close()
if meta:
verify_image(target, meta)
def find_image(jobname, type, dist): def find_image(jobname, type, dist):
...@@ -205,6 +235,9 @@ def main(): ...@@ -205,6 +235,9 @@ def main():
uboot_target) uboot_target)
write_uuu_script(uuu_target, image_target, uboot_target) write_uuu_script(uuu_target, image_target, uboot_target)
flash_image(uuu_target) flash_image(uuu_target)
except VerifyImageException as e:
logging.error(e)
return 1
except KeyboardInterrupt: except KeyboardInterrupt:
logging.error("CTRL-C pressed.") logging.error("CTRL-C pressed.")
return 1 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