xref: /aosp_15_r20/external/flashrom/tests/realtek_mst_i2c_spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright 2021 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_REALTEK_MST_I2C_SPI == 1
realtek_mst_ioctl(void * state,int fd,unsigned long request,va_list args)19 static int realtek_mst_ioctl(void *state, int fd, unsigned long request, va_list args)
20 {
21 	assert_int_equal(fd, MOCK_FD);
22 	assert_int_equal(request, I2C_SLAVE);
23 	/* Only access to I2C address 0x4a is expected */
24 	unsigned long addr = va_arg(args, unsigned long);
25 	assert_int_equal(addr, 0x4a);
26 
27 	return 0;
28 }
29 
realtek_mst_read(void * state,int fd,void * buf,size_t sz)30 static int realtek_mst_read(void *state, int fd, void *buf, size_t sz)
31 {
32 	assert_int_equal(fd, MOCK_FD);
33 	assert_int_equal(sz, 1);
34 	return sz;
35 }
36 
realtek_mst_write(void * state,int fd,const void * buf,size_t sz)37 static int realtek_mst_write(void *state, int fd, const void *buf, size_t sz)
38 {
39 	assert_int_equal(fd, MOCK_FD);
40 	const LargestIntegralType accepted_sizes[] = {1, 2};
41 	assert_in_set(sz, accepted_sizes, ARRAY_SIZE(accepted_sizes));
42 	return sz;
43 }
44 
realtek_mst_basic_lifecycle_test_success(void ** state)45 void realtek_mst_basic_lifecycle_test_success(void **state)
46 {
47 	struct io_mock_fallback_open_state realtek_mst_fallback_open_state = {
48 		.noc = 0,
49 		.paths = { LOCK_FILE, "/dev/i2c-254", NULL },
50 		.flags = { O_RDWR, O_RDWR },
51 	};
52 	const struct io_mock realtek_mst_io = {
53 		.iom_ioctl = realtek_mst_ioctl,
54 		.iom_read = realtek_mst_read,
55 		.iom_write = realtek_mst_write,
56 		.fallback_open_state = &realtek_mst_fallback_open_state,
57 	};
58 
59 	run_basic_lifecycle(state, &realtek_mst_io, &programmer_realtek_mst_i2c_spi, "bus=254,enter_isp=0,allow_brick=yes");
60 }
61 
realtek_mst_no_allow_brick_test_success(void ** state)62 void realtek_mst_no_allow_brick_test_success(void **state)
63 {
64 	struct io_mock_fallback_open_state realtek_mst_fallback_open_state = {
65 		.noc = 0,
66 		.paths = { LOCK_FILE, "/dev/i2c-254", NULL },
67 		.flags = { O_RDWR, O_RDWR },
68 	};
69 	const struct io_mock realtek_mst_io = {
70 		.fallback_open_state = &realtek_mst_fallback_open_state,
71 	};
72 
73 	run_init_error_path(state, &realtek_mst_io, &programmer_realtek_mst_i2c_spi,
74 				"bus=254,enter_isp=0", SPI_GENERIC_ERROR);
75 }
76 #else
77 	SKIP_TEST(realtek_mst_basic_lifecycle_test_success)
78 	SKIP_TEST(realtek_mst_no_allow_brick_test_success)
79 #endif /* CONFIG_REALTEK_MST_I2C_SPI */
80