1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/i2c.h>
8 #include <string.h>
9 #include <zephyr/internal/syscall_handler.h>
10 
z_vrfy_i2c_configure(const struct device * dev,uint32_t dev_config)11 static inline int z_vrfy_i2c_configure(const struct device *dev,
12 				       uint32_t dev_config)
13 {
14 	K_OOPS(K_SYSCALL_DRIVER_I2C(dev, configure));
15 	return z_impl_i2c_configure((const struct device *)dev, dev_config);
16 }
17 #include <zephyr/syscalls/i2c_configure_mrsh.c>
18 
z_vrfy_i2c_get_config(const struct device * dev,uint32_t * dev_config)19 static inline int z_vrfy_i2c_get_config(const struct device *dev,
20 					uint32_t *dev_config)
21 {
22 	K_OOPS(K_SYSCALL_DRIVER_I2C(dev, get_config));
23 	K_OOPS(K_SYSCALL_MEMORY_WRITE(dev_config, sizeof(uint32_t)));
24 
25 	return z_impl_i2c_get_config(dev, dev_config);
26 }
27 #include <zephyr/syscalls/i2c_get_config_mrsh.c>
28 
copy_msgs_and_transfer(const struct device * dev,const struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)29 static uint32_t copy_msgs_and_transfer(const struct device *dev,
30 				       const struct i2c_msg *msgs,
31 				       uint8_t num_msgs,
32 				       uint16_t addr)
33 {
34 	struct i2c_msg copy[num_msgs];
35 	uint8_t i;
36 
37 	/* Use a local copy to avoid switcheroo attacks. */
38 	memcpy(copy, msgs, num_msgs * sizeof(*msgs));
39 
40 	/* Validate the buffers in each message struct. Read options require
41 	 * that the target buffer be writable
42 	 */
43 	for (i = 0U; i < num_msgs; i++) {
44 		K_OOPS(K_SYSCALL_MEMORY(copy[i].buf, copy[i].len,
45 					copy[i].flags & I2C_MSG_READ));
46 	}
47 
48 	return z_impl_i2c_transfer(dev, copy, num_msgs, addr);
49 }
50 
z_vrfy_i2c_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)51 static inline int z_vrfy_i2c_transfer(const struct device *dev,
52 				      struct i2c_msg *msgs, uint8_t num_msgs,
53 				      uint16_t addr)
54 {
55 	K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_I2C));
56 
57 	/* copy_msgs_and_transfer() will allocate a copy on the stack using
58 	 * VLA, so ensure this won't blow the stack.  Most functions defined
59 	 * in i2c.h use only a handful of messages, so up to 32 messages
60 	 * should be more than sufficient.
61 	 */
62 	K_OOPS(K_SYSCALL_VERIFY(num_msgs >= 1 && num_msgs < 32));
63 
64 	/* We need to be able to read the overall array of messages */
65 	K_OOPS(K_SYSCALL_MEMORY_ARRAY_READ(msgs, num_msgs,
66 					   sizeof(struct i2c_msg)));
67 
68 	return copy_msgs_and_transfer((const struct device *)dev,
69 				      (struct i2c_msg *)msgs,
70 				      (uint8_t)num_msgs, (uint16_t)addr);
71 }
72 #include <zephyr/syscalls/i2c_transfer_mrsh.c>
73 
z_vrfy_i2c_target_driver_register(const struct device * dev)74 static inline int z_vrfy_i2c_target_driver_register(const struct device *dev)
75 {
76 	K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_I2C));
77 	return z_impl_i2c_target_driver_register(dev);
78 }
79 #include <zephyr/syscalls/i2c_target_driver_register_mrsh.c>
80 
z_vrfy_i2c_target_driver_unregister(const struct device * dev)81 static inline int z_vrfy_i2c_target_driver_unregister(const struct device *dev)
82 {
83 	K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_I2C));
84 	return z_impl_i2c_target_driver_unregister(dev);
85 }
86 #include <zephyr/syscalls/i2c_target_driver_unregister_mrsh.c>
87 
z_vrfy_i2c_recover_bus(const struct device * dev)88 static inline int z_vrfy_i2c_recover_bus(const struct device *dev)
89 {
90 	K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_I2C));
91 	return z_impl_i2c_recover_bus(dev);
92 }
93 #include <zephyr/syscalls/i2c_recover_bus_mrsh.c>
94