/* * This file is part of the flashrom project. * * Copyright 2020 Google LLC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include "programmer.h" #include "flashchips.h" #include "chipdrivers.h" #include "spi.h" struct flashchip mock_chip = { .vendor = "Generic", .name = "unknown SPI chip (RDID)", .bustype = BUS_SPI, .manufacture_id = GENERIC_MANUF_ID, .model_id = GENERIC_DEVICE_ID, .total_size = 0, .page_size = 256, .tested = TEST_BAD_PREW, .probe = probe_spi_rdid, .write = NULL, }; /* * This declaration is needed for visibility, so that wrap below could * redirect to real function. */ int __real_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr); int __wrap_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) { if (flash->chip != &mock_chip) /* * Caller is some other test, redirecting to real function. * This test is the only one which uses wrap of spi_send_command, * all other tests use real function. */ return __real_spi_send_command(flash, writecnt, readcnt, writearr, readarr); check_expected_ptr(flash); assert_int_equal(writecnt, mock_type(int)); assert_int_equal(writearr[0], mock_type(int)); int rcnt = mock_type(int); assert_int_equal(readcnt, rcnt); for (int i = 0; i < rcnt; i++) readarr[i] = i; return 0; } void spi_write_enable_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_WREN_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_WREN); will_return(__wrap_spi_send_command, JEDEC_WREN_INSIZE); assert_int_equal(0, spi_write_enable(&flashctx)); } void spi_write_disable_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_WRDI_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_WRDI); will_return(__wrap_spi_send_command, JEDEC_WRDI_INSIZE); assert_int_equal(0, spi_write_disable(&flashctx)); } void probe_spi_rdid_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_RDID); will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE); assert_int_equal(0, probe_spi_rdid(&flashctx)); } void probe_spi_rdid4_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_RDID); will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE + 1); assert_int_equal(0, probe_spi_rdid4(&flashctx)); } void probe_spi_rems_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_REMS_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_REMS); will_return(__wrap_spi_send_command, JEDEC_REMS_INSIZE); assert_int_equal(0, probe_spi_rems(&flashctx)); } void probe_spi_res1_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_RES); will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); assert_int_equal(0, probe_spi_res2(&flashctx)); } void probe_spi_res2_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ clear_spi_id_cache(); struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_RES); will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); assert_int_equal(0, probe_spi_res2(&flashctx)); } void probe_spi_res3_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); will_return(__wrap_spi_send_command, JEDEC_RES); will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 2); assert_int_equal(0, probe_spi_res3(&flashctx)); } void probe_spi_at25f_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); will_return(__wrap_spi_send_command, AT25F_RDID_OUTSIZE); will_return(__wrap_spi_send_command, AT25F_RDID); will_return(__wrap_spi_send_command, AT25F_RDID_INSIZE); assert_int_equal(0, probe_spi_at25f(&flashctx)); } /* spi95.c */ void probe_spi_st95_test_success(void **state) { (void) state; /* unused */ /* setup initial test state. */ struct flashctx flashctx = { .chip = &mock_chip }; expect_memory(__wrap_spi_send_command, flash, &flashctx, sizeof(flashctx)); /* chip total size < 64K. */ uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address will_return(__wrap_spi_send_command, rdid_outsize); will_return(__wrap_spi_send_command, ST_M95_RDID); will_return(__wrap_spi_send_command, ST_M95_RDID_INSIZE); assert_int_equal(0, probe_spi_st95(&flashctx)); } ='n112' href='#n112'>112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
from libghdl import libghdl
from ctypes import (c_char_p, c_int32, c_int, c_bool, sizeof, c_void_p,
                    POINTER, Structure)
import libghdl.iirs as iirs
import libghdl.nodes_meta as nodes_meta
from libghdl.nodes_meta import (Attr, types)
# from libghdl_defs import (fields, Iir_Kind, types, Attr)

assert sizeof(c_bool) == 1

# libghdl

_set_option = libghdl.libghdl__set_option
_analyze_file = libghdl.libghdl__analyze_file


def set_option(opt):
    return _set_option(c_char_p(opt), len(opt))


def analyze_init():
    return libghdl.libghdl__analyze_init()


def analyze_file(filename):
    return _analyze_file(c_char_p(filename), len(filename))


# Lists


class Lists:
    List_Type = c_int32

    class Iterator(Structure):
        _fields_ = [("chunk", c_int32),
                    ("chunk_idx", c_int32),
                    ("remain", c_int32)]

    Iterate = libghdl.lists__iterate
    Iterate.argstype = [List_Type]
    Iterate.restype = Iterator

    Is_Valid = libghdl.lists__is_valid
    Is_Valid.argstype = [POINTER(Iterator)]
    Is_Valid.restype = c_bool

    Next = libghdl.lists__next
    Next.argstype = [POINTER(Iterator)]
    Next.restype = None

    Get_Element = libghdl.lists__get_element
    Get_Element.argstype = [POINTER(Iterator)]
    Get_Element.restype = c_int32

    Get_Nbr_Elements = libghdl.lists__get_nbr_elements
    Get_Nbr_Elements.argtype = [List_Type]
    Get_Nbr_Elements.restype = c_int32

    Create_Iir_List = libghdl.lists__create_list

    Destroy_Iir_List = libghdl.lists__destroy_list


class Flists:
    Flist_Type = c_int32

    Ffirst = 0
    Flast = libghdl.flists__flast

    Length = libghdl.flists__length

    Get_Nth_Element = libghdl.flists__get_nth_element


# Files

Location_To_File = libghdl.files_map__location_to_file

Location_File_To_Pos = libghdl.files_map__location_file_to_pos

Location_File_To_Line = libghdl.files_map__location_file_to_line

Location_File_Line_To_Col = libghdl.files_map__location_file_line_to_col

Get_File_Name = libghdl.files_map__get_file_name

Get_File_Buffer = libghdl.files_map__get_file_buffer
Get_File_Buffer.restype = c_void_p

Get_File_Length = libghdl.files_map__get_file_length

Read_Source_File = libghdl.files_map__read_source_file

No_Source_File_Entry = 0

No_Location = 0

# Names

Get_Name_Length = libghdl.name_table__get_name_length

Get_Name_Ptr = libghdl.name_table__get_name_ptr
Get_Name_Ptr.restype = c_char_p

_Get_Identifier_With_Len = libghdl.name_table__get_identifier_with_len

def Get_Identifier(s):
    return _Get_Identifier_With_Len(c_char_p(s), len(s))

Null_Identifier = 0

# Ieee

class Ieee:
    Std_Logic_1164_Pkg = c_int.in_dll(
        libghdl, "ieee__std_logic_1164__std_logic_1164_pkg")

    # Get value
    Std_Logic_Type = c_int.in_dll(
        libghdl, "ieee__std_logic_1164__std_logic_type")

    # Get value
    Std_Logic_Vector_Type = c_int.in_dll(
        libghdl, "ieee__std_logic_1164__std_logic_vector_type")

    # Get value
    Rising_Edge = c_int.in_dll(libghdl, "ieee__std_logic_1164__rising_edge")

    # Get value
    Falling_Edge = c_int.in_dll(libghdl, "ieee__std_logic_1164__falling_edge")


# Flags
class Flags:
    Flag_Elocations = c_bool.in_dll(libghdl, "flags__flag_elocations")


# Scanner
class Scanner:
    Set_File = libghdl.scanner__set_file

    Close_File = libghdl.scanner__close_file

    Scan = libghdl.scanner__scan

    # This is a c_int, so you want to use its .value
    Current_Token = c_int.in_dll(libghdl, "scanner__current_token")

    Flag_Comment = c_bool.in_dll(libghdl, "scanner__flag_comment")

    Get_Current_Line = libghdl.scanner__get_current_line

    Get_Token_Column = libghdl.scanner__get_token_column

    Get_Token_Position = libghdl.scanner__get_token_position

    Get_Position = libghdl.scanner__get_position

    Current_Identifier = libghdl.scanner__current_identifier


class Parse:
    Parse_Design_File = libghdl.parse__parse_design_file

    Flag_Parse_Parenthesis = c_bool.in_dll(
        libghdl, "parse__flag_parse_parenthesis")


class Canon:
    Flag_Concurrent_Stmts = c_bool.in_dll(
        libghdl, "canon__canon_flag_concurrent_stmts")

    Flag_Configurations = c_bool.in_dll(
        libghdl, "canon__canon_flag_configurations")

    Flag_Associations = c_bool.in_dll(
        libghdl, "canon__canon_flag_associations")

    Extract_Sequential_Statement_Chain_Sensitivity = \
        libghdl.canon__canon_extract_sequential_statement_chain_sensitivity


# std.standard

# Use .value
Standard_Package = c_int32.in_dll(libghdl, "std_package__standard_package")

# Use .value
Character_Type_Definition = c_int32.in_dll(
    libghdl, "std_package__character_type_definition")

# libraries

Get_Libraries_Chain = libghdl.libraries__get_libraries_chain

Add_Design_Unit_Into_Library = libghdl.libraries__add_design_unit_into_library

Finish_Compilation = libghdl.libraries__finish_compilation

# Use .value
Library_Location = c_int32.in_dll(libghdl, "libraries__library_location")

# Use .value
Work_Library = c_int32.in_dll(libghdl, "libraries__work_library")

Purge_Design_File = libghdl.libraries__purge_design_file

# Disp_Tree

Disp_Iir = libghdl.disp_tree__disp_iir

# Iirs_Utils

class Iirs_Utils:
    Strip_Denoting_Name = libghdl.iirs_utils__strip_denoting_name

    Get_Entity = libghdl.iirs_utils__get_entity

    Is_Second_Subprogram_Specification = \
        libghdl.iirs_utils__is_second_subprogram_specification

    Get_Entity_From_Entity_Aspect = \
        libghdl.iirs_utils__get_entity_from_entity_aspect

    Get_Interface_Of_Formal = \
        libghdl.iirs_utils__get_interface_of_formal

Null_Iir = 0
Null_Iir_List = 0
Iir_List_All = 1

Null_Iir_Flist = 0
Iir_Flist_Others = 1
Iir_Flist_All = 2