1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2022 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include "lifecycle.h"
17
18 #if CONFIG_PARADE_LSPCON == 1
19
20 /* Same macros as in parade_lspcon.c programmer. */
21 /* FIXME(aklm): should driver register maps be defined in `include/drivers/` for sharing with tests? */
22 #define REGISTER_ADDRESS 0x4a
23 #define SPISTATUS 0x9e
24 #define SPISTATUS_SECTOR_ERASE_FINISHED 0
25 #define SWSPICTL 0x93
26 #define SWSPICTL_ENABLE_READBACK 0x8
27 #define SWSPI_RDATA 0x91
28 /* Macros for test run. */
29 #define DATA_TO_READ 0
30 #define MAX_REG_BUF_LEN 2
31
32 struct parade_lspcon_io_state {
33 unsigned long addr; /* Address to read and write */
34 uint8_t reg_buf[MAX_REG_BUF_LEN]; /* Last value written to the register address */
35 };
36
parade_lspcon_ioctl(void * state,int fd,unsigned long request,va_list args)37 static int parade_lspcon_ioctl(void *state, int fd, unsigned long request, va_list args)
38 {
39 struct parade_lspcon_io_state *io_state = state;
40 if (request == I2C_SLAVE)
41 /* Addr is the next (and the only) argument in the parameters list for this ioctl call. */
42 io_state->addr = va_arg(args, unsigned long);
43
44 return 0;
45 }
46
parade_lspcon_read(void * state,int fd,void * buf,size_t sz)47 static int parade_lspcon_read(void *state, int fd, void *buf, size_t sz)
48 {
49 struct parade_lspcon_io_state *io_state = state;
50
51 /*
52 * Parade_lspcon programmer has operations over register address and
53 * page address. In the current emulation for basic lifecycle we need
54 * to emulate operations over register address. Page address can do
55 * nothing for now, and just return successful return value.
56 *
57 * For future, if this unit test is upgraded to run probing lifecycle,
58 * page address operations might need to be fully emulated.
59 */
60 if (io_state->addr != REGISTER_ADDRESS)
61 return sz;
62
63 assert_int_equal(sz, 1);
64
65 switch (io_state->reg_buf[0]) {
66 case SPISTATUS:
67 memset(buf, SPISTATUS_SECTOR_ERASE_FINISHED, sz);
68 break;
69 case SWSPICTL:
70 memset(buf, SWSPICTL_ENABLE_READBACK, sz);
71 break;
72 case SWSPI_RDATA:
73 memset(buf, DATA_TO_READ, sz);
74 break;
75 default:
76 memset(buf, 0, sz);
77 break;
78 }
79
80 return sz;
81 }
82
parade_lspcon_write(void * state,int fd,const void * buf,size_t sz)83 static int parade_lspcon_write(void *state, int fd, const void *buf, size_t sz)
84 {
85 struct parade_lspcon_io_state *io_state = state;
86
87 /*
88 * Only register address operations are needed to be emulated for basic lifecycle.
89 * See also comment in `parade_lspcon_read`.
90 */
91 if (io_state->addr != REGISTER_ADDRESS)
92 return sz;
93
94 assert_true(sz <= MAX_REG_BUF_LEN);
95
96 memcpy(io_state->reg_buf, buf, sz);
97
98 return sz;
99 }
100
parade_lspcon_basic_lifecycle_test_success(void ** state)101 void parade_lspcon_basic_lifecycle_test_success(void **state)
102 {
103 struct parade_lspcon_io_state parade_lspcon_io_state = { 0 };
104 struct io_mock_fallback_open_state parade_lspcon_fallback_open_state = {
105 .noc = 0,
106 .paths = { LOCK_FILE, "/dev/i2c-254", NULL },
107 .flags = { O_RDWR, O_RDWR },
108 };
109 const struct io_mock parade_lspcon_io = {
110 .state = ¶de_lspcon_io_state,
111 .iom_ioctl = parade_lspcon_ioctl,
112 .iom_read = parade_lspcon_read,
113 .iom_write = parade_lspcon_write,
114 .fallback_open_state = ¶de_lspcon_fallback_open_state,
115 };
116
117 run_basic_lifecycle(state, ¶de_lspcon_io, &programmer_parade_lspcon, "bus=254,allow_brick=yes");
118 }
119
parade_lspcon_no_allow_brick_test_success(void ** state)120 void parade_lspcon_no_allow_brick_test_success(void **state)
121 {
122 struct io_mock_fallback_open_state parade_lspcon_fallback_open_state = {
123 .noc = 0,
124 .paths = { LOCK_FILE, "/dev/i2c-254", NULL },
125 .flags = { O_RDWR, O_RDWR },
126 };
127 const struct io_mock parade_lspcon_io = {
128 .fallback_open_state = ¶de_lspcon_fallback_open_state,
129 };
130
131 run_init_error_path(state, ¶de_lspcon_io, &programmer_parade_lspcon,
132 "bus=254", SPI_GENERIC_ERROR);
133 }
134
135 #else
136 SKIP_TEST(parade_lspcon_basic_lifecycle_test_success)
137 SKIP_TEST(parade_lspcon_no_allow_brick_test_success)
138 #endif /* CONFIG_PARADE_LSPCON */
139