2019-08-22 01:43:17 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Turris Mox rWTM firmware driver
|
|
|
|
*
|
2024-07-15 11:59:10 +00:00
|
|
|
* Copyright (C) 2019, 2024 Marek Behún <kabel@kernel.org>
|
2019-08-22 01:43:17 +00:00
|
|
|
*/
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
#include <crypto/sha2.h>
|
2024-08-31 09:20:49 +00:00
|
|
|
#include <linux/align.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
#include <linux/armada-37xx-rwtm-mailbox.h>
|
|
|
|
#include <linux/completion.h>
|
2024-08-31 09:20:39 +00:00
|
|
|
#include <linux/container_of.h>
|
2020-06-01 21:00:49 +00:00
|
|
|
#include <linux/debugfs.h>
|
2024-08-31 09:20:39 +00:00
|
|
|
#include <linux/device.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
2024-08-31 09:20:39 +00:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/fs.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
#include <linux/hw_random.h>
|
2024-08-31 09:20:36 +00:00
|
|
|
#include <linux/if_ether.h>
|
2024-08-31 09:20:39 +00:00
|
|
|
#include <linux/kobject.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
#include <linux/mailbox_client.h>
|
2024-08-31 09:20:35 +00:00
|
|
|
#include <linux/minmax.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/platform_device.h>
|
2024-08-31 09:20:35 +00:00
|
|
|
#include <linux/sizes.h>
|
2024-08-31 09:20:39 +00:00
|
|
|
#include <linux/sysfs.h>
|
2024-08-31 09:20:37 +00:00
|
|
|
#include <linux/types.h>
|
2019-08-22 01:43:17 +00:00
|
|
|
|
|
|
|
#define DRIVER_NAME "turris-mox-rwtm"
|
|
|
|
|
2024-08-31 09:20:35 +00:00
|
|
|
#define RWTM_DMA_BUFFER_SIZE SZ_4K
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
/*
|
|
|
|
* The macros and constants below come from Turris Mox's rWTM firmware code.
|
|
|
|
* This firmware is open source and it's sources can be found at
|
|
|
|
* https://gitlab.labs.nic.cz/turris/mox-boot-builder/tree/master/wtmi.
|
|
|
|
*/
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
#define MOX_ECC_NUMBER_WORDS 17
|
|
|
|
#define MOX_ECC_NUMBER_LEN (MOX_ECC_NUMBER_WORDS * sizeof(u32))
|
|
|
|
|
|
|
|
#define MOX_ECC_SIGNATURE_WORDS (2 * MOX_ECC_NUMBER_WORDS)
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
#define MBOX_STS_SUCCESS (0 << 30)
|
|
|
|
#define MBOX_STS_FAIL (1 << 30)
|
|
|
|
#define MBOX_STS_BADCMD (2 << 30)
|
|
|
|
#define MBOX_STS_ERROR(s) ((s) & (3 << 30))
|
|
|
|
#define MBOX_STS_VALUE(s) (((s) >> 10) & 0xfffff)
|
|
|
|
#define MBOX_STS_CMD(s) ((s) & 0x3ff)
|
|
|
|
|
|
|
|
enum mbox_cmd {
|
|
|
|
MBOX_CMD_GET_RANDOM = 1,
|
|
|
|
MBOX_CMD_BOARD_INFO = 2,
|
|
|
|
MBOX_CMD_ECDSA_PUB_KEY = 3,
|
|
|
|
MBOX_CMD_HASH = 4,
|
|
|
|
MBOX_CMD_SIGN = 5,
|
|
|
|
MBOX_CMD_VERIFY = 6,
|
|
|
|
|
|
|
|
MBOX_CMD_OTP_READ = 7,
|
|
|
|
MBOX_CMD_OTP_WRITE = 8,
|
|
|
|
};
|
|
|
|
|
2024-11-11 09:53:57 +00:00
|
|
|
/**
|
|
|
|
* struct mox_rwtm - driver private data structure
|
|
|
|
* @mbox_client: rWTM mailbox client
|
|
|
|
* @mbox: rWTM mailbox channel
|
|
|
|
* @hwrng: RNG driver structure
|
|
|
|
* @reply: last mailbox reply, filled in receive callback
|
|
|
|
* @buf: DMA buffer
|
|
|
|
* @buf_phys: physical address of the DMA buffer
|
|
|
|
* @busy: mutex to protect mailbox command execution
|
|
|
|
* @cmd_done: command done completion
|
|
|
|
* @has_board_info: whether board information is present
|
|
|
|
* @serial_number: serial number of the device
|
|
|
|
* @board_version: board version / revision of the device
|
|
|
|
* @ram_size: RAM size of the device
|
|
|
|
* @mac_address1: first MAC address of the device
|
|
|
|
* @mac_address2: second MAC address of the device
|
|
|
|
* @has_pubkey: whether board ECDSA public key is present
|
|
|
|
* @pubkey: board ECDSA public key
|
|
|
|
* @last_sig: last ECDSA signature generated with board ECDSA private key
|
|
|
|
* @last_sig_done: whether the last ECDSA signing is complete
|
|
|
|
*/
|
2019-08-22 01:43:17 +00:00
|
|
|
struct mox_rwtm {
|
|
|
|
struct mbox_client mbox_client;
|
|
|
|
struct mbox_chan *mbox;
|
|
|
|
struct hwrng hwrng;
|
|
|
|
|
|
|
|
struct armada_37xx_rwtm_rx_msg reply;
|
|
|
|
|
|
|
|
void *buf;
|
|
|
|
dma_addr_t buf_phys;
|
|
|
|
|
|
|
|
struct mutex busy;
|
|
|
|
struct completion cmd_done;
|
|
|
|
|
2024-08-31 09:20:37 +00:00
|
|
|
bool has_board_info;
|
2019-08-22 01:43:17 +00:00
|
|
|
u64 serial_number;
|
|
|
|
int board_version, ram_size;
|
2024-08-31 09:20:36 +00:00
|
|
|
u8 mac_address1[ETH_ALEN], mac_address2[ETH_ALEN];
|
2019-08-22 01:43:17 +00:00
|
|
|
|
2024-08-31 09:20:37 +00:00
|
|
|
bool has_pubkey;
|
2019-08-22 01:43:17 +00:00
|
|
|
u8 pubkey[135];
|
2020-06-01 21:00:49 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
/*
|
|
|
|
* Signature process. This is currently done via debugfs, because it
|
|
|
|
* does not conform to the sysfs standard "one file per attribute".
|
|
|
|
* It should be rewritten via crypto API once akcipher API is available
|
|
|
|
* from userspace.
|
|
|
|
*/
|
2024-08-31 09:20:38 +00:00
|
|
|
u32 last_sig[MOX_ECC_SIGNATURE_WORDS];
|
2024-08-31 09:20:37 +00:00
|
|
|
bool last_sig_done;
|
2020-06-01 21:00:49 +00:00
|
|
|
#endif
|
2019-08-22 01:43:17 +00:00
|
|
|
};
|
|
|
|
|
2024-08-31 09:20:45 +00:00
|
|
|
static inline struct device *rwtm_dev(struct mox_rwtm *rwtm)
|
|
|
|
{
|
|
|
|
return rwtm->mbox_client.dev;
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
#define MOX_ATTR_RO(name, format, cat) \
|
|
|
|
static ssize_t \
|
2024-08-31 09:20:41 +00:00
|
|
|
name##_show(struct device *dev, struct device_attribute *a, \
|
2019-08-22 01:43:17 +00:00
|
|
|
char *buf) \
|
|
|
|
{ \
|
2024-08-31 09:20:41 +00:00
|
|
|
struct mox_rwtm *rwtm = dev_get_drvdata(dev); \
|
2019-08-22 01:43:17 +00:00
|
|
|
if (!rwtm->has_##cat) \
|
|
|
|
return -ENODATA; \
|
2024-08-31 09:20:40 +00:00
|
|
|
return sysfs_emit(buf, format, rwtm->name); \
|
2019-08-22 01:43:17 +00:00
|
|
|
} \
|
2024-08-31 09:20:41 +00:00
|
|
|
static DEVICE_ATTR_RO(name)
|
2019-08-22 01:43:17 +00:00
|
|
|
|
|
|
|
MOX_ATTR_RO(serial_number, "%016llX\n", board_info);
|
|
|
|
MOX_ATTR_RO(board_version, "%i\n", board_info);
|
|
|
|
MOX_ATTR_RO(ram_size, "%i\n", board_info);
|
|
|
|
MOX_ATTR_RO(mac_address1, "%pM\n", board_info);
|
|
|
|
MOX_ATTR_RO(mac_address2, "%pM\n", board_info);
|
|
|
|
MOX_ATTR_RO(pubkey, "%s\n", pubkey);
|
|
|
|
|
2024-08-31 09:20:41 +00:00
|
|
|
static struct attribute *turris_mox_rwtm_attrs[] = {
|
|
|
|
&dev_attr_serial_number.attr,
|
|
|
|
&dev_attr_board_version.attr,
|
|
|
|
&dev_attr_ram_size.attr,
|
|
|
|
&dev_attr_mac_address1.attr,
|
|
|
|
&dev_attr_mac_address2.attr,
|
|
|
|
&dev_attr_pubkey.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(turris_mox_rwtm);
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
static int mox_get_status(enum mbox_cmd cmd, u32 retval)
|
|
|
|
{
|
2021-05-20 11:35:17 +00:00
|
|
|
if (MBOX_STS_CMD(retval) != cmd)
|
2019-08-22 01:43:17 +00:00
|
|
|
return -EIO;
|
|
|
|
else if (MBOX_STS_ERROR(retval) == MBOX_STS_FAIL)
|
|
|
|
return -(int)MBOX_STS_VALUE(retval);
|
2021-05-20 11:35:17 +00:00
|
|
|
else if (MBOX_STS_ERROR(retval) == MBOX_STS_BADCMD)
|
2024-08-31 09:20:48 +00:00
|
|
|
return -EOPNOTSUPP;
|
2021-05-20 11:35:17 +00:00
|
|
|
else if (MBOX_STS_ERROR(retval) != MBOX_STS_SUCCESS)
|
|
|
|
return -EIO;
|
2019-08-22 01:43:17 +00:00
|
|
|
else
|
|
|
|
return MBOX_STS_VALUE(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mox_rwtm_rx_callback(struct mbox_client *cl, void *data)
|
|
|
|
{
|
|
|
|
struct mox_rwtm *rwtm = dev_get_drvdata(cl->dev);
|
|
|
|
struct armada_37xx_rwtm_rx_msg *msg = data;
|
|
|
|
|
2024-07-15 11:59:10 +00:00
|
|
|
if (completion_done(&rwtm->cmd_done))
|
|
|
|
return;
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
rwtm->reply = *msg;
|
|
|
|
complete(&rwtm->cmd_done);
|
|
|
|
}
|
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
static int mox_rwtm_exec(struct mox_rwtm *rwtm, enum mbox_cmd cmd,
|
|
|
|
struct armada_37xx_rwtm_tx_msg *msg,
|
|
|
|
bool interruptible)
|
|
|
|
{
|
|
|
|
struct armada_37xx_rwtm_tx_msg _msg = {};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
msg = &_msg;
|
|
|
|
|
|
|
|
msg->command = cmd;
|
|
|
|
|
|
|
|
ret = mbox_send_message(rwtm->mbox, msg);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (interruptible) {
|
|
|
|
ret = wait_for_completion_interruptible(&rwtm->cmd_done);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
if (!wait_for_completion_timeout(&rwtm->cmd_done, HZ / 2))
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mox_get_status(cmd, rwtm->reply.retval);
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
static void reply_to_mac_addr(u8 *mac, u32 t1, u32 t2)
|
|
|
|
{
|
|
|
|
mac[0] = t1 >> 8;
|
|
|
|
mac[1] = t1;
|
|
|
|
mac[2] = t2 >> 24;
|
|
|
|
mac[3] = t2 >> 16;
|
|
|
|
mac[4] = t2 >> 8;
|
|
|
|
mac[5] = t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mox_get_board_info(struct mox_rwtm *rwtm)
|
|
|
|
{
|
2024-08-31 09:20:45 +00:00
|
|
|
struct device *dev = rwtm_dev(rwtm);
|
2019-08-22 01:43:17 +00:00
|
|
|
struct armada_37xx_rwtm_rx_msg *reply = &rwtm->reply;
|
|
|
|
int ret;
|
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
ret = mox_rwtm_exec(rwtm, MBOX_CMD_BOARD_INFO, NULL, false);
|
2021-05-20 11:35:18 +00:00
|
|
|
if (ret == -ENODATA) {
|
2024-08-31 09:20:45 +00:00
|
|
|
dev_warn(dev,
|
2019-08-22 01:43:17 +00:00
|
|
|
"Board does not have manufacturing information burned!\n");
|
2024-08-31 09:20:48 +00:00
|
|
|
} else if (ret == -EOPNOTSUPP) {
|
2024-08-31 09:20:45 +00:00
|
|
|
dev_notice(dev,
|
2021-05-20 11:35:18 +00:00
|
|
|
"Firmware does not support the BOARD_INFO command\n");
|
|
|
|
} else if (ret < 0) {
|
|
|
|
return ret;
|
2019-08-22 01:43:17 +00:00
|
|
|
} else {
|
|
|
|
rwtm->serial_number = reply->status[1];
|
|
|
|
rwtm->serial_number <<= 32;
|
|
|
|
rwtm->serial_number |= reply->status[0];
|
2019-12-11 14:47:10 +00:00
|
|
|
rwtm->board_version = reply->status[2];
|
2019-08-22 01:43:17 +00:00
|
|
|
rwtm->ram_size = reply->status[3];
|
|
|
|
reply_to_mac_addr(rwtm->mac_address1, reply->status[4],
|
|
|
|
reply->status[5]);
|
|
|
|
reply_to_mac_addr(rwtm->mac_address2, reply->status[6],
|
|
|
|
reply->status[7]);
|
2024-08-31 09:20:37 +00:00
|
|
|
rwtm->has_board_info = true;
|
2019-08-22 01:43:17 +00:00
|
|
|
|
|
|
|
pr_info("Turris Mox serial number %016llX\n",
|
|
|
|
rwtm->serial_number);
|
|
|
|
pr_info(" board version %i\n", rwtm->board_version);
|
|
|
|
pr_info(" burned RAM size %i MiB\n", rwtm->ram_size);
|
|
|
|
}
|
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
ret = mox_rwtm_exec(rwtm, MBOX_CMD_ECDSA_PUB_KEY, NULL, false);
|
2021-05-20 11:35:18 +00:00
|
|
|
if (ret == -ENODATA) {
|
2024-08-31 09:20:45 +00:00
|
|
|
dev_warn(dev, "Board has no public key burned!\n");
|
2024-08-31 09:20:48 +00:00
|
|
|
} else if (ret == -EOPNOTSUPP) {
|
2024-08-31 09:20:45 +00:00
|
|
|
dev_notice(dev,
|
2021-05-20 11:35:18 +00:00
|
|
|
"Firmware does not support the ECDSA_PUB_KEY command\n");
|
|
|
|
} else if (ret < 0) {
|
|
|
|
return ret;
|
2019-08-22 01:43:17 +00:00
|
|
|
} else {
|
|
|
|
u32 *s = reply->status;
|
|
|
|
|
2024-08-31 09:20:37 +00:00
|
|
|
rwtm->has_pubkey = true;
|
2019-08-22 01:43:17 +00:00
|
|
|
sprintf(rwtm->pubkey,
|
|
|
|
"%06x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
|
|
|
|
ret, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
|
|
|
|
s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:35:19 +00:00
|
|
|
static int check_get_random_support(struct mox_rwtm *rwtm)
|
|
|
|
{
|
2024-08-31 09:20:50 +00:00
|
|
|
struct armada_37xx_rwtm_tx_msg msg = {
|
|
|
|
.args = { 1, rwtm->buf_phys, 4 },
|
|
|
|
};
|
2021-05-20 11:35:19 +00:00
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
return mox_rwtm_exec(rwtm, MBOX_CMD_GET_RANDOM, &msg, false);
|
2021-05-20 11:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
static int mox_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
|
|
|
|
{
|
2024-08-31 09:20:47 +00:00
|
|
|
struct mox_rwtm *rwtm = container_of(rng, struct mox_rwtm, hwrng);
|
2024-08-31 09:20:50 +00:00
|
|
|
struct armada_37xx_rwtm_tx_msg msg = {
|
|
|
|
.args = { 1, rwtm->buf_phys, ALIGN(max, 4) },
|
|
|
|
};
|
2019-08-22 01:43:17 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-08-31 09:20:35 +00:00
|
|
|
max = min(max, RWTM_DMA_BUFFER_SIZE);
|
2019-08-22 01:43:17 +00:00
|
|
|
|
|
|
|
if (!wait) {
|
|
|
|
if (!mutex_trylock(&rwtm->busy))
|
|
|
|
return -EBUSY;
|
|
|
|
} else {
|
|
|
|
mutex_lock(&rwtm->busy);
|
|
|
|
}
|
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
ret = mox_rwtm_exec(rwtm, MBOX_CMD_GET_RANDOM, &msg, true);
|
2019-08-22 01:43:17 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto unlock_mutex;
|
|
|
|
|
|
|
|
memcpy(data, rwtm->buf, max);
|
|
|
|
ret = max;
|
|
|
|
|
|
|
|
unlock_mutex:
|
|
|
|
mutex_unlock(&rwtm->busy);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:00:49 +00:00
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
static int rwtm_debug_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
file->private_data = inode->i_private;
|
|
|
|
|
|
|
|
return nonseekable_open(inode, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t do_sign_read(struct file *file, char __user *buf, size_t len,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct mox_rwtm *rwtm = file->private_data;
|
|
|
|
ssize_t ret;
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
/* only allow one read, of whole signature, from position 0 */
|
2020-06-01 21:00:49 +00:00
|
|
|
if (*ppos != 0)
|
|
|
|
return 0;
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
if (len < sizeof(rwtm->last_sig))
|
2020-06-01 21:00:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!rwtm->last_sig_done)
|
|
|
|
return -ENODATA;
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
ret = simple_read_from_buffer(buf, len, ppos, rwtm->last_sig,
|
|
|
|
sizeof(rwtm->last_sig));
|
2024-08-31 09:20:37 +00:00
|
|
|
rwtm->last_sig_done = false;
|
2020-06-01 21:00:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t do_sign_write(struct file *file, const char __user *buf,
|
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct mox_rwtm *rwtm = file->private_data;
|
|
|
|
struct armada_37xx_rwtm_tx_msg msg;
|
|
|
|
loff_t dummy = 0;
|
|
|
|
ssize_t ret;
|
|
|
|
|
2024-08-31 09:20:38 +00:00
|
|
|
if (len != SHA512_DIGEST_SIZE)
|
2020-06-01 21:00:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* if last result is not zero user has not read that information yet */
|
|
|
|
if (rwtm->last_sig_done)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (!mutex_trylock(&rwtm->busy))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we have to send:
|
|
|
|
* 1. Address of the input to sign.
|
|
|
|
* The input is an array of 17 32-bit words, the first (most
|
|
|
|
* significat) is 0, the rest 16 words are copied from the SHA-512
|
|
|
|
* hash given by the user and converted from BE to LE.
|
|
|
|
* 2. Address of the buffer where ECDSA signature value R shall be
|
|
|
|
* stored by the rWTM firmware.
|
|
|
|
* 3. Address of the buffer where ECDSA signature value S shall be
|
|
|
|
* stored by the rWTM firmware.
|
|
|
|
*/
|
2024-08-31 09:20:38 +00:00
|
|
|
memset(rwtm->buf, 0, sizeof(u32));
|
|
|
|
ret = simple_write_to_buffer(rwtm->buf + sizeof(u32),
|
|
|
|
SHA512_DIGEST_SIZE, &dummy, buf, len);
|
2020-06-01 21:00:49 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto unlock_mutex;
|
2024-08-31 09:20:38 +00:00
|
|
|
be32_to_cpu_array(rwtm->buf, rwtm->buf, MOX_ECC_NUMBER_WORDS);
|
2020-06-01 21:00:49 +00:00
|
|
|
|
|
|
|
msg.args[0] = 1;
|
|
|
|
msg.args[1] = rwtm->buf_phys;
|
2024-08-31 09:20:38 +00:00
|
|
|
msg.args[2] = rwtm->buf_phys + MOX_ECC_NUMBER_LEN;
|
|
|
|
msg.args[3] = rwtm->buf_phys + 2 * MOX_ECC_NUMBER_LEN;
|
2020-06-01 21:00:49 +00:00
|
|
|
|
2024-08-31 09:20:50 +00:00
|
|
|
ret = mox_rwtm_exec(rwtm, MBOX_CMD_SIGN, &msg, true);
|
2020-06-01 21:00:49 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto unlock_mutex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we read the R and S values of the ECDSA signature
|
|
|
|
* computed by the rWTM firmware and convert their words from
|
|
|
|
* LE to BE.
|
|
|
|
*/
|
2024-08-31 09:20:38 +00:00
|
|
|
memcpy(rwtm->last_sig, rwtm->buf + MOX_ECC_NUMBER_LEN,
|
|
|
|
sizeof(rwtm->last_sig));
|
|
|
|
cpu_to_be32_array(rwtm->last_sig, rwtm->last_sig,
|
|
|
|
MOX_ECC_SIGNATURE_WORDS);
|
2024-08-31 09:20:37 +00:00
|
|
|
rwtm->last_sig_done = true;
|
2020-06-01 21:00:49 +00:00
|
|
|
|
|
|
|
mutex_unlock(&rwtm->busy);
|
|
|
|
return len;
|
|
|
|
unlock_mutex:
|
|
|
|
mutex_unlock(&rwtm->busy);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations do_sign_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = rwtm_debug_open,
|
|
|
|
.read = do_sign_read,
|
|
|
|
.write = do_sign_write,
|
|
|
|
};
|
|
|
|
|
2024-08-31 09:20:42 +00:00
|
|
|
static void rwtm_debugfs_release(void *root)
|
2020-06-01 21:00:49 +00:00
|
|
|
{
|
|
|
|
debugfs_remove_recursive(root);
|
|
|
|
}
|
|
|
|
|
2024-08-31 09:20:42 +00:00
|
|
|
static void rwtm_register_debugfs(struct mox_rwtm *rwtm)
|
2020-06-01 21:00:49 +00:00
|
|
|
{
|
2024-08-31 09:20:42 +00:00
|
|
|
struct dentry *root;
|
|
|
|
|
|
|
|
root = debugfs_create_dir("turris-mox-rwtm", NULL);
|
|
|
|
|
|
|
|
debugfs_create_file_unsafe("do_sign", 0600, root, rwtm, &do_sign_fops);
|
|
|
|
|
2024-08-31 09:20:45 +00:00
|
|
|
devm_add_action_or_reset(rwtm_dev(rwtm), rwtm_debugfs_release, root);
|
2020-06-01 21:00:49 +00:00
|
|
|
}
|
|
|
|
#else
|
2024-08-31 09:20:42 +00:00
|
|
|
static inline void rwtm_register_debugfs(struct mox_rwtm *rwtm)
|
2020-06-01 21:00:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-08-31 09:20:43 +00:00
|
|
|
static void rwtm_devm_mbox_release(void *mbox)
|
|
|
|
{
|
|
|
|
mbox_free_channel(mbox);
|
|
|
|
}
|
|
|
|
|
2024-08-31 09:20:41 +00:00
|
|
|
static void rwtm_firmware_symlink_drop(void *parent)
|
|
|
|
{
|
|
|
|
sysfs_remove_link(parent, DRIVER_NAME);
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
static int turris_mox_rwtm_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct mox_rwtm *rwtm;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
rwtm = devm_kzalloc(dev, sizeof(*rwtm), GFP_KERNEL);
|
|
|
|
if (!rwtm)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2024-08-31 09:20:35 +00:00
|
|
|
rwtm->buf = dmam_alloc_coherent(dev, RWTM_DMA_BUFFER_SIZE,
|
|
|
|
&rwtm->buf_phys, GFP_KERNEL);
|
2019-08-22 01:43:17 +00:00
|
|
|
if (!rwtm->buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, rwtm);
|
|
|
|
|
2024-08-31 09:20:46 +00:00
|
|
|
ret = devm_mutex_init(dev, &rwtm->busy);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2024-07-15 11:59:12 +00:00
|
|
|
init_completion(&rwtm->cmd_done);
|
2019-08-22 01:43:17 +00:00
|
|
|
|
|
|
|
rwtm->mbox_client.dev = dev;
|
|
|
|
rwtm->mbox_client.rx_callback = mox_rwtm_rx_callback;
|
|
|
|
|
|
|
|
rwtm->mbox = mbox_request_channel(&rwtm->mbox_client, 0);
|
2024-08-31 09:20:44 +00:00
|
|
|
if (IS_ERR(rwtm->mbox))
|
|
|
|
return dev_err_probe(dev, PTR_ERR(rwtm->mbox),
|
|
|
|
"Cannot request mailbox channel!\n");
|
2019-08-22 01:43:17 +00:00
|
|
|
|
2024-08-31 09:20:43 +00:00
|
|
|
ret = devm_add_action_or_reset(dev, rwtm_devm_mbox_release, rwtm->mbox);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
ret = mox_get_board_info(rwtm);
|
|
|
|
if (ret < 0)
|
|
|
|
dev_warn(dev, "Cannot read board information: %i\n", ret);
|
|
|
|
|
2021-05-20 11:35:19 +00:00
|
|
|
ret = check_get_random_support(rwtm);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_notice(dev,
|
|
|
|
"Firmware does not support the GET_RANDOM command\n");
|
2024-08-31 09:20:43 +00:00
|
|
|
return ret;
|
2021-05-20 11:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
rwtm->hwrng.name = DRIVER_NAME "_hwrng";
|
|
|
|
rwtm->hwrng.read = mox_hwrng_read;
|
|
|
|
|
|
|
|
ret = devm_hwrng_register(dev, &rwtm->hwrng);
|
2024-08-31 09:20:44 +00:00
|
|
|
if (ret)
|
|
|
|
return dev_err_probe(dev, ret, "Cannot register HWRNG!\n");
|
2019-08-22 01:43:17 +00:00
|
|
|
|
2024-08-31 09:20:42 +00:00
|
|
|
rwtm_register_debugfs(rwtm);
|
2020-06-01 21:00:49 +00:00
|
|
|
|
2021-05-20 11:35:20 +00:00
|
|
|
dev_info(dev, "HWRNG successfully registered\n");
|
|
|
|
|
2024-08-31 09:20:41 +00:00
|
|
|
/*
|
|
|
|
* For sysfs ABI compatibility, create symlink
|
|
|
|
* /sys/firmware/turris-mox-rwtm to this device's sysfs directory.
|
|
|
|
*/
|
|
|
|
ret = sysfs_create_link(firmware_kobj, &dev->kobj, DRIVER_NAME);
|
|
|
|
if (!ret)
|
|
|
|
devm_add_action_or_reset(dev, rwtm_firmware_symlink_drop,
|
|
|
|
firmware_kobj);
|
|
|
|
|
2019-08-22 01:43:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id turris_mox_rwtm_match[] = {
|
|
|
|
{ .compatible = "cznic,turris-mox-rwtm", },
|
2021-05-20 11:38:43 +00:00
|
|
|
{ .compatible = "marvell,armada-3700-rwtm-firmware", },
|
2019-08-22 01:43:17 +00:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, turris_mox_rwtm_match);
|
|
|
|
|
|
|
|
static struct platform_driver turris_mox_rwtm_driver = {
|
|
|
|
.probe = turris_mox_rwtm_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = DRIVER_NAME,
|
|
|
|
.of_match_table = turris_mox_rwtm_match,
|
2024-08-31 09:20:41 +00:00
|
|
|
.dev_groups = turris_mox_rwtm_groups,
|
2019-08-22 01:43:17 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(turris_mox_rwtm_driver);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_DESCRIPTION("Turris Mox rWTM firmware driver");
|
2021-04-09 20:27:04 +00:00
|
|
|
MODULE_AUTHOR("Marek Behun <kabel@kernel.org>");
|