Question

In: Computer Science

Flashing Firmware You have been given a task to write a firmware flashing tool that will...

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

Solutions

Expert Solution

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>")


Related Solutions

You are an intern with an equity research organization. You have been given the task to...
You are an intern with an equity research organization. You have been given the task to project the values of certain income statement and balance sheet items for the next year (FY2020) for ABC Ltd (Your current time=0 is FY ending 2019). You have collected information about the historical income statement and balance sheet items. The historical information is provided in Table 1. Table 1 PARTICULARS (All values in INR crores) FY2019 FY2018 FY2017 FY2016 Sales Revenue 21118.00 22084.00 17273.00...
You have been given the task of calculating the WACC of ABC Inc. You will use...
You have been given the task of calculating the WACC of ABC Inc. You will use the following information to calculate the WACC. The firm has 3000 coupon paying bonds outstanding. Each coupon-paying bond has a face value of $1000, will mature 10 years from today, and is currently priced at 130% of the face value. The annual coupon rate is 12%, and coupon is paid on an annual basis. The company has 500,000 common shares outstanding, and each share...
You have been given the task of calculating the WACC of ABC Inc. You will use...
You have been given the task of calculating the WACC of ABC Inc. You will use the following information to calculate the WACC. The firm has 3000 coupon paying bonds outstanding. Each coupon-paying bond has a face value of $1000, will mature 10 years from today, and is currently priced at 130% of the face value. The annual coupon rate is 12%, and coupon is paid on an annual basis. The company has 500,000 common shares outstanding, and each share...
5. You have been given the task of determining if the number of board feet of...
5. You have been given the task of determining if the number of board feet of lumber (Volume) from a black cherry tree can be predicted from the diameter of the tree at 54 inches from the ground. You have data from 31 black cherry trees in New Zealand about their volume, height, and diameter. You need to analyze the data and write a couple of sentences discussing the questions below. Follow the steps we use in class to analyze...
You have been given the task to evaluate a market opportunity for a firm. Give specific...
You have been given the task to evaluate a market opportunity for a firm. Give specific examples of the dimensions you would use to determine the attractiveness of a given market. (Strategic Marketing)
Suppose you have been given the task of distilling a mixture of hexane + toluene. Pure...
Suppose you have been given the task of distilling a mixture of hexane + toluene. Pure hexane has a refractive index of 1.375 and pure toluene has a refactive index of 1.497. You collect a distillate sample which has a refractive index of 1.441. Assuming that the refractive index of the hexane + toluene mixture varies linearly with mole fraction, what is the mole fraction of hexane in your sample?
You have been given the task of organizing a poll regarding an upcoming election. The object...
You have been given the task of organizing a poll regarding an upcoming election. The object is to estimate the proportion of the country that will vote to keep the current government in power. You have been told to collect a sample and find a 95% confidence interval for the proportion. This interval is allowed to have a margin of error of 4%. A preliminary investigation suggests that the proportion of people that will vote to keep the current government...
You have been given the task of finding out what proportion of students that enroll in...
You have been given the task of finding out what proportion of students that enroll in a local university actually complete their degree. You have access to first year enrolment records and you decide to randomly sample 115 of those records. You find that 87 of those sampled went on to complete their degree. a)Calculate the proportion of sampled students that complete their degree. Give your answer as a decimal to 3 decimal places. Sample proportion = You decide to...
You have been given the task of organizing a poll regarding an upcoming election. The object...
You have been given the task of organizing a poll regarding an upcoming election. The object is to estimate the proportion of the country that will vote to keep the current government in power. You have been told to collect a sample and find a 95% confidence interval for the proportion. This interval is allowed to have a margin of error of 4%. A preliminary investigation suggests that the proportion of people that will vote to keep the current government...
You have been given the task of finding out what proportion of students that enroll in...
You have been given the task of finding out what proportion of students that enroll in a local university actually complete their degree. You have access to first year enrolment records and you decide to randomly sample 111 of those records. You find that 80 of those sampled went on to complete their degree. a)Calculate the proportion of sampled students that complete their degree. Give your answer as a decimal to 3 decimal places. Sample proportion = You decide to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT