How to use clear_app method in Airtest

Best Python code snippet using Airtest

generate_secureboot_assets.py

Source:generate_secureboot_assets.py Github

copy

Full Screen

1#!/usr/bin/env python32import argparse3import sys4import os5# This key table has to match the one in bootloader6keyTbl = [0xDEADBEEF, 0xAAAAAAAA, 0x11111111, 0x00000000, 0xFFFFFFFF, 0x55555555, 0xA5A5A5A5, 0x66666666]7#******************************************************************************8#9# Main function10#11#******************************************************************************12def main():13 # Read the binary file from the command line.14 with open(args.binfile, mode='rb') as binfile:15 clear_application= binfile.read()16 print('Loading Clear application {} bytes from {}...'.format(len(clear_application), args.binfile), flush=True)17 18 plaintext = pad_to_block_size(clear_application, 4)19 ivVal = word_from_bytes(os.urandom(4), 0)20 print("Initialization Vector")21 print(hex(ivVal))22 application = encrypt_app(args.keyidxVal, plaintext, ivVal)23 trailer = sec_trailer(args.keyidxVal, plaintext, ivVal, int(args.protectionVal, 0))24 print('Saving encrypted image {} bytes to {}...'.format(len(application), args.encimagefile), flush=True)25 with open(args.encimagefile, mode='wb') as encimagefile:26 encimagebytearray = bytearray(application)27 encimagefile.write(encimagebytearray)28 print('Saving security trailer {} bytes to {}...'.format(len(trailer), args.sectrailerfile), flush=True)29 with open(args.sectrailerfile, mode='wb') as sectrailerfile:30 trailerbytearray = bytearray(trailer)31 sectrailerfile.write(trailerbytearray)32 print('Done.')33#******************************************************************************34#35# Turn a 32-bit number into a series of bytes for transmission.36#37# This command will split a 32-bit integer into an array of bytes, ordered38# LSB-first for transmission over the UART.39#40#******************************************************************************41def int_to_bytes(n):42 A = [n & 0xFF,43 (n >> 8) & 0xFF,44 (n >> 16) & 0xFF,45 (n >> 24) & 0xFF]46 return A47#******************************************************************************48#49# Extract a word from a byte array50#51#******************************************************************************52def word_from_bytes(B, n):53 return (B[n] + (B[n + 1] << 8) + (B[n + 2] << 16) + (B[n + 3] << 24))54#******************************************************************************55#56# CRC function that matches the CRC used by the Apollo bootloader.57#58#******************************************************************************59poly32 = 0x1EDC6F4160def crc32(L):61 rem = 062 for b in L:63 rem = rem ^ (b << 24)64 for i in range(8):65 if rem & 0x80000000:66 rem = ((rem << 1) ^ poly32)67 else:68 rem = (rem << 1)69 rem = rem & 0xFFFFFFFF70 return rem71def pad_to_block_size(text, block_size):72 text_length = len(text)73 amount_to_pad = block_size - (text_length % block_size)74 if amount_to_pad == 0:75 amount_to_pad = block_size76 for i in range(0, amount_to_pad, 1):77 text += bytes(chr(amount_to_pad), 'ascii')78 return text79def encrypt_app(keyidx, clear_app, iv):80 key32 = keyTbl[keyidx]81 applen = len(clear_app)82 enc_app = []83 for i in range(0, applen, 4):84 word = word_from_bytes(clear_app, i)85 word = (word ^ iv) ^ key3286 iv = word87 enc_app.extend(int_to_bytes(word))88 return enc_app89def sec_trailer(keyidx, clear_app, iv, protection):90 key32 = keyTbl[keyidx]91 secTrailer = []92 secTrailer.extend(int_to_bytes(keyidx))93 secTrailer.extend(int_to_bytes(protection))94 applen = len(clear_app)95 secTrailer.extend(int_to_bytes(applen))96 crc = crc32(clear_app)97 sig = key32 ^ crc98 secTrailer.extend(int_to_bytes(sig))99 secTrailer.extend(int_to_bytes(iv))100 # Trailer Signature101 secTrailerSig = crc32(secTrailer) ^ key32102 secTrailer.extend(int_to_bytes(secTrailerSig))103 return secTrailer104#******************************************************************************105#106# Main program flow107#108#******************************************************************************109if __name__ == '__main__':110 parser = argparse.ArgumentParser(description =111 'Secure Image generation utility for Apollo or Apollo2')112 parser.add_argument('binfile',113 help = 'Binary file to program into the target device')114 parser.add_argument('keyidxVal', default=0, type=int, help = 'encryption key index')115 116 parser.add_argument('protectionVal', default=0, help = 'Image Protection Value (hex)')117 parser.add_argument('encimagefile', help = 'Destination file for Encrypted image')118 parser.add_argument('sectrailerfile', help = 'Destination file for security trailer')119 args = parser.parse_args()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful