In: Computer Science
Flashing Firmware
You have been given a task to write a firmware flashing tool that will read the header of a firmware file, check the integrity and extract the contents and write a memory mapped file on the system.
You will need to use the python language to solve this problem. Below outlines the file format of the firmware file.
magic: 4bytes, integer, First 4 bytes of a file, needs to contain the following number 0x22225555
version: 4bytes, Integer, you will need to divide the version value by 1000 and take the leading. eg 1400, will be 1.4 and therefore the version is 1.4
sha256: 64bytes, String (char array), hex string within the file
data: N bytes, you will need to verify the data using hashlib sha256 function, if it matches to what is expected, flash it to WRITE_PATH.
Your program will be given a filename as a command line argument to extract the contents from.
python flash.py firmware.bin
What is a hash function?
This transforms arbitrary length data into a fixed size representation. You can think of it as a way to provide a unique identifier for data. Although you can get collisions in hash functions (meaning, two different data sets can potentially match up to the same has value), it is very unlikely to happen.
How can I use a hash function? (sha256)
You will use a library called hashlib which you will need to import into your flash.py code.
import hashlib # opened file as binary file data = f.read() result = hashlib.sha256(data) # Computes sha256 hex = result.hexdigest() # Gets Hex String Representation
What does my application need to print out?
Your program will need to print out the following
Version
Size Of Firmware
Verification output of magic, hash and write output.
You can use this as the format for your program.
MAGIC Verification... <Status: PASSED or FAILED, if FAILED, program should exit> Hash Verification... <Status: PASSED or FAILED, if FAILED, program should exit> Firmware Version: <Version Number> Size of Firmware: <Number of Bytes> Bytes Writing Data... FINISHED
Example of Successful Write
MAGIC Verification... PASSED Hash Verification... PASSED Firmware Version: 1.4 Size of Firmware: 1024 Bytes Writing Data... FINISHED
Example of Failed Magic Verification
MAGIC Verification... FAILED
Example of Failed Hash Verification
MAGIC Verification... PASSED Hash Verification... FAILED
import hashlib #imported as mentioned in the question
import sys #this module is used to get the command line
argument
from pathlib import Path # this is used to get the size of
file
fileName = sys.argv[1] # python script has script name as first
command line argument so using second argument to read the name of
the file
file = open(fileName, 'rb')
magic = file.read(4)
magic_number = int.from_bytes(magic, byteorder = 'big', signed =
False)
print("Magic verification...")
if magic_number != 0x22225555:
print("<STATUS: FAILED>")
return
else:
print("<STATUS: FAILED>")
version_byte = file.read(4)
version_number = int.from_bytes(version_byte, byteorder='big',
signed = False)
actual_version = version_number/1000;
print()
sha = file.read(64)
data = file.read()
file.close()
result = hashlib.sha256(data) # as mentioned in the question used
the same
hex = result.hexdigest() # as mentioned in the question used the
same
print("Hash Verification... ")
if hex == sha:
print ("<STATUS: PASSED>")
print("Firmware Version : ", actual_version)
firmware_size = Path(fileName).stat().st_size - 72
print("Size Of Firmware : ", firmware_size)
writeFile = open(WRITE_PATH, rb+)
writeFile.write(data)
writeFile.close()
print("Write Successfull")
else:
print ("<STATUS: FAILED>")