示例#1
0
def test_find_fdt(state: dict, script: str):
    """
    Exercises depthcharge-find-fdt
    """
    output_dir = create_resource_dir(os.path.join(state['test_dir'], 'fdt'))
    dts_file = os.path.join(output_dir, 'test.dts.input')

    with open(dts_file, mode='w') as outfile:
        outfile.write(_DTS)

    args = [_DTC, '-q', '-I', 'dts', '-O', 'dtb', dts_file]
    sub = run_script(args, arch=None, capture_output=True)
    dtb = sub.stdout

    image_file = os.path.join(output_dir, 'image.bin')
    with open(image_file, 'wb') as outfile:
        outfile.write(random_pattern(412, seed=0))
        outfile.write(dtb)
        outfile.write(random_pattern(123, seed=1))
        outfile.write(dtb)
        outfile.write(random_pattern(4500, seed=2))
        outfile.write(dtb)
        outfile.write(random_pattern(150, seed=3))

    # Print only
    args = [script, '-f', image_file]
    run_script(args, stdout=DEVNULL)

    outpfx = os.path.join(output_dir, 'test')

    # Save DTB and DTS files
    args = [script, '-f', image_file, '-o', outpfx, '-a', '0x8000_0000']
    run_script(args, stdout=DEVNULL)

    # Save DTB only
    args = [
        script, '-f', image_file, '-o', outpfx, '-a', '0x9000_0000', '--no-dts'
    ]
    run_script(args, stdout=DEVNULL)

    # Save DTS only
    args = [
        script, '-f', image_file, '-o', outpfx, '-a', '0xa000_0000', '--no-dtb'
    ]
    run_script(args, stdout=DEVNULL)

    # Count results
    results = {'dtb': 0, 'dts': 0}
    for _, _, filenames in os.walk(output_dir):
        for f in filenames:
            if f.endswith('.dts'):
                results['dts'] += 1
            elif f.endswith('.dtb'):
                results['dtb'] += 1

    assert results['dtb'] == 6
    assert results['dts'] == 6
示例#2
0
def test_write_mem__pattern(state: dict, script: str):
    """
    Write a random test pattern to $loadaddr

    Loads state['config_file]'.

    Adds written data in state['write_data'].
    """
    _WRITE_SIZE = 16385

    # Split data into two parts:
    #  1. Data written from a file
    #  2. Data written from a hex string provided on the command-line

    data = random_pattern(_WRITE_SIZE)
    write_data_file = os.path.join(state['test_dir'], 'write_data.bin')

    state['write_data'] = data
    state['write_data_file'] = write_data_file

    save_file(write_data_file, data, 'wb')

    wr_data1 = data[:_WRITE_SIZE-31]
    wr_data2 = data[_WRITE_SIZE-31:]

    wr_addr = int(state['config']['env_vars']['loadaddr'], 0)

    # Part 1 data
    test_file = os.path.join(state['test_dir'], 'write_data.1.bin')

    save_file(test_file, wr_data1, 'wb')

    args = [
        script,
        '-a', hex(wr_addr),
        '-f', test_file,
        '-c', state['config_file'],
        '-X', '_unused_value=foo,_unused_bar',
        '-m', 'file:/dev/null',
        '--op', 'loady,loadx,loadb'
    ]
    run(args, check=True)

    wr_addr += len(wr_data1)

    # Part 2 data - drop the extra unused args just to have a different
    # set of cmdline args
    args = [
        script,
        '-a', hex(wr_addr),
        '-d', wr_data2.hex(),
        '-c', state['config_file'],
    ]
    run(args, check=True)
示例#3
0
import datetime
import os
from os import linesep
from os.path import basename

from depthcharge import log, OperationAlignmentError, Stratagem, StratagemCreationFailed
from depthcharge.cmdline import ArgumentParser, create_depthcharge_ctx
from depthcharge.memory import StratagemMemoryWriter, DataAbortMemoryReader

from test_utils import (load_resource, save_resource, create_resource_dir,
                        random_pattern, decrementing_pattern, incrementing_pattern)

SIZES           = (4, 32, 33, 34, 36, 128)
PATTERNS        = (incrementing_pattern, decrementing_pattern, random_pattern)

STRATAGEM_DATA          = random_pattern(16384, seed=1)
STRATAGEM_DATA_OFF      = 1 * 1024 * 1024
_STRATAGEM_DATA_LOADED  = False


def test_cases():
    pattern_idx = 0
    for size in SIZES:
        pattern = PATTERNS[pattern_idx]
        pattern_idx = (pattern_idx + 1) % len(PATTERNS)
        data = pattern(size)
        yield (data, pattern.__name__)


def setup_stratagem(ctx, data, pattern_name, writer):
    global _STRATAGEM_DATA_LOADED
示例#4
0
def test_find_env(state: dict, script: str):
    """
    Locate environments created by test_mkenv in a binar blob.
    """

    output_dir = create_resource_dir(os.path.join(state['test_dir'], 'env'))
    blobfile = os.path.join(output_dir, 'blob.bin')
    with open(blobfile, 'wb') as outfile:
        outfile.write(random_pattern(31, seed=0) + b'\x00')

        with open(os.path.join(output_dir, 'env_no_hdr.1.bin'), 'rb') as infile:
            data = infile.read()
            outfile.write(data)

        outfile.write(random_pattern(63, seed=1) + b'\x00')

        with open(os.path.join(output_dir, 'env_no_hdr.2.bin'), 'rb') as infile:
            data = infile.read()
            outfile.write(data)

        outfile.write(random_pattern(1023, seed=2) + b'\x00')

        with open(os.path.join(output_dir, 'env_flags_0xa.bin'), 'rb') as infile:
            data = infile.read()
            outfile.write(data)

        outfile.write(random_pattern(3, seed=3) + b'\x00')
        with open(os.path.join(output_dir, 'env.bin'), 'rb') as infile:
            data = infile.read()
            outfile.write(data)

        outfile.write(random_pattern(64, seed=4) + b'\x00')

    test_dir = create_resource_dir(os.path.join(state['test_dir'], 'find_env'))
    filename_pfx = os.path.join(test_dir, 'env')

    # Expanded text
    args = [
        script,
        '-A', 'arm',
        '-a', '0x8004_0000',
        '-E',
        '-f', blobfile,
        '-o', filename_pfx,
    ]
    run(args, stdout=DEVNULL, check=True)

    # Expanded text dup, default arch and addr
    args = [
        script,
        '--expand',
        '-f', blobfile,
        '-o', filename_pfx,
    ]
    run(args, stdout=DEVNULL, check=True)

    # Text
    args = [
        script,
        '-f', blobfile,
        '-o', filename_pfx,
    ]
    run(args, stdout=DEVNULL, check=True)

    args = [
        script,
        '-B',
        '-f', blobfile,
        '-o', filename_pfx,
    ]
    run(args, stdout=DEVNULL, check=True)

    # Dup of the above, just long-form args
    args = [
        script,
        '--binary',
        '--file', blobfile,
        '--outfile', filename_pfx,
    ]
    run(args, stdout=DEVNULL, check=True)

    results = {
        'high_exp_text': 0,
        'low_exp_text':  0,
        'text': 0,
        'bin': 0
    }

    for root, _, filenames in os.walk(test_dir):
        for filename in filenames:
            if filename.endswith('.bin'):
                results['bin'] += 1
            elif filename.endswith('exp.txt'):
                if '0x8' in filename:
                    results['high_exp_text'] += 1
                else:
                    results['low_exp_text'] += 1
            elif filename.endswith('.txt'):
                results['text'] += 1
            else:
                raise RuntimeError('Unexpected condition')

    for key, value in results.items():
        if value != 4:
            msg = '{:s}: Expected 4, got {:d}'.format(key, value)
            raise ValueError(msg)