diff --git a/devicemodel/hw/pci/virtio/virtio_rpmb.c b/devicemodel/hw/pci/virtio/virtio_rpmb.c index 08b0d82cd..44a9c1b0e 100644 --- a/devicemodel/hw/pci/virtio/virtio_rpmb.c +++ b/devicemodel/hw/pci/virtio/virtio_rpmb.c @@ -56,6 +56,7 @@ #include #include #include +#include #define VIRTIO_RPMB_RINGSZ 64 @@ -63,6 +64,8 @@ static int virtio_rpmb_debug; #define DPRINTF(params) do { if (virtio_rpmb_debug) printf params; } while (0) #define WPRINTF(params) (printf params) +#define SEQ_CMD_MAX 3 /*support up to 3 cmds*/ + /* * virtio-rpmb struct */ @@ -77,16 +80,238 @@ struct virtio_rpmb { int vmid; }; +struct virtio_rpmb_ioctl_cmd { + unsigned int cmd; /*ioctl cmd*/ + int result; /* result for ioctl cmd*/ +}; + +struct virtio_rpmb_ioc_seq_cmd { + struct virtio_rpmb_ioctl_cmd ioc; + union { + __u64 num_of_cmds; /*num of seq cmds*/ + __u64 seq_base; + }; +}; + +struct virtio_rpmb_ioc_seq_data { + struct virtio_rpmb_ioc_seq_cmd seq_cmd; + struct rpmb_cmd cmds[SEQ_CMD_MAX + 1]; +}; + +/* + * get start address of seq cmds. + */ +static struct rpmb_cmd * +virtio_rpmb_get_seq_cmds(void *pdata) +{ + struct virtio_rpmb_ioc_seq_data *seq_data; + + if (!pdata) { + DPRINTF(("error, invalid args!\n")); + return NULL; + } + + seq_data = (struct virtio_rpmb_ioc_seq_data *)pdata; + + return (struct rpmb_cmd *)&seq_data->cmds[0]; +} + +/* + * get address of seq specail frames. + * index: the index of cmds. + */ +static struct rpmb_frame * +virtio_rpmb_get_seq_frames(void *pdata, __u32 index) +{ + struct virtio_rpmb_ioc_seq_data *seq_data; + struct rpmb_frame *frames; + struct rpmb_cmd *cmds, *cmd; + __u64 ncmds; + __u32 offset = 0; + __u32 num = 0; + + if (!pdata || index > SEQ_CMD_MAX) { + DPRINTF(("error, invalid args!\n")); + return NULL; + } + + seq_data = (struct virtio_rpmb_ioc_seq_data *)pdata; + + /* get number of cmds.*/ + ncmds = seq_data->seq_cmd.num_of_cmds; + if (ncmds > SEQ_CMD_MAX) { + DPRINTF(("error, ncmds(%llu) > max\n", ncmds)); + return NULL; + } + + /* get start address of cmds.*/ + cmds = virtio_rpmb_get_seq_cmds(pdata); + if (!cmds) { + DPRINTF(("fail to get seq cmds.\n")); + return NULL; + } + + /* get start address of frames.*/ + frames = (struct rpmb_frame *)&seq_data->cmds[ncmds + 1]; + if (!frames) { + DPRINTF(("error, invalid frames ptr.\n")); + return NULL; + } + + for (num = 0; num < index; num++) { + cmd = &cmds[num]; + if (!cmd) { + DPRINTF(("error, invalid cmd ptr.\n")); + return NULL; + } + offset += cmd->nframes; + } + + return (struct rpmb_frame *)&frames[offset]; +} + +/* + * get address of all seq data. + */ +static void * +virtio_rpmb_get_seq_data(void *pdata) +{ + struct virtio_rpmb_ioc_seq_data *seq_data; + + if (!pdata) { + DPRINTF(("error, invalid args!\n")); + return NULL; + } + + seq_data = (struct virtio_rpmb_ioc_seq_data *)pdata; + + return (void *)&seq_data->seq_cmd.seq_base; +} + +/* + * Address space is different between + * SOS VBS-U and UOS kernel. + * Update frames_ptr to current space. + */ +static int +virtio_rpmb_map_seq_frames(struct iovec *iov) +{ + struct virtio_rpmb_ioc_seq_cmd *seq_cmd = NULL; + struct rpmb_cmd *cmds, *cmd; + __u64 index; + + if (!iov) { + DPRINTF(("error, invalid arg!\n")); + return -1; + } + + seq_cmd = (struct virtio_rpmb_ioc_seq_cmd *)(iov->iov_base); + if (!seq_cmd || seq_cmd->num_of_cmds > SEQ_CMD_MAX) { + DPRINTF(("found invalid data.\n")); + return -1; + } + + /* get start address of cmds.*/ + cmds = virtio_rpmb_get_seq_cmds(iov->iov_base); + if (!cmds) { + DPRINTF(("fail to get seq cmds.\n")); + return -1; + } + + /* set frames_ptr to VBS-U space.*/ + for (index = 0; index < seq_cmd->num_of_cmds; index++) { + cmd = &cmds[index]; + if (!cmd) { + DPRINTF(("error, invalid cmd ptr.\n")); + return -1; + } + /* set frames address.*/ + cmd->frames = virtio_rpmb_get_seq_frames(iov->iov_base, index); + if (!cmd->frames) { + DPRINTF(("fail to get frames[%llu] ptr!\n", index)); + return -1; + } + } + + return 0; +} + +static int +virtio_rpmb_seq_handler(struct virtio_rpmb *rpmb, struct iovec *iov) +{ + struct virtio_rpmb_ioctl_cmd *ioc = NULL; + int fd; + void *pdata; + int rc; + + if (!rpmb || !iov) { + DPRINTF(("found invalid args!!!\n")); + return -1; + } + + /* update frames_ptr to curent space*/ + rc = virtio_rpmb_map_seq_frames(iov); + if (rc) { + DPRINTF(("fail to map seq frames\n")); + return rc; + } + + ioc = (struct virtio_rpmb_ioctl_cmd *)(iov->iov_base); + if (!ioc) { + DPRINTF(("error, get ioc is NULL!\n")); + return -1; + } + + pdata = virtio_rpmb_get_seq_data(iov->iov_base); + if (!pdata) { + DPRINTF(("fail to get seq data\n")); + return -1; + } + + /* open rpmb device.*/ + rc = open("/dev/rpmb0", O_RDWR | O_NONBLOCK); + if (rc < 0) { + DPRINTF(("failed to open /dev/rpmb0.\n")); + return rc; + } + fd = rc; + + /* send ioctl cmd.*/ + rc = ioctl(fd, ioc->cmd, pdata); + if (rc) + DPRINTF(("seq ioctl cmd failed(%d).\n", rc)); + + /* close rpmb device.*/ + close(fd); + + return rc; +} + static void virtio_rpmb_notify(void *base, struct virtio_vq_info *vq) { struct iovec iov; int len; uint16_t idx; + struct virtio_rpmb *rpmb = (struct virtio_rpmb *)base; + struct virtio_rpmb_ioctl_cmd *ioc; while (vq_has_descs(vq)) { vq_getchain(vq, &idx, &iov, 1, NULL); + ioc = (struct virtio_rpmb_ioctl_cmd *)(iov.iov_base); + + switch (ioc->cmd) { + case RPMB_IOC_SEQ_CMD: + ioc->result = virtio_rpmb_seq_handler(rpmb, &iov); + break; + + default: + DPRINTF(("found unsupported ioctl(%u)!\n", ioc->cmd)); + ioc->result = -1; + break; + } + len = iov.iov_len; assert(len > 0); diff --git a/devicemodel/include/rpmb.h b/devicemodel/include/rpmb.h new file mode 100644 index 000000000..d2a1b2488 --- /dev/null +++ b/devicemodel/include/rpmb.h @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2018 Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Contact Information: weideng + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +#ifndef __RPMB_H__ +#define __RPMB_H__ + +#include + +/** + * struct rpmb_cmd: rpmb access command + * + * @flags: command flags + * 0 - read command + * 1 - write command RPMB_F_WRITE + * 2 - reliable write RPMB_F_REL_WRITE + * @nframes: number of rpmb frames in the command + * @frames: list of rpmb frames + */ +struct rpmb_cmd { + __u32 flags; + __u32 nframes; + struct rpmb_frame *frames; +}; + +/** + * struct rpmb_data - rpmb data be transmitted in RPMB request + * + * @req_type: request type (program key, read, write, write counter) + * @icmd: list of input frames + * @ocmd: list of result frames + */ +struct rpmb_data { + __u16 req_type; + struct rpmb_cmd icmd; + struct rpmb_cmd ocmd; +}; + +/** + * struct rpmb_frame - rpmb frame as defined by specs + * + * @stuff : stuff bytes + * @key_mac : The authentication key or the message authentication + * code (MAC) depending on the request/response type. + * The MAC will be delivered in the last (or the only) + * block of data. + * @data : Data to be written or read by signed access. + * @nonce : Random number generated by the host for the requests + * and copied to the response by the RPMB engine. + * @write_counter: Counter value for the total amount of the successful + * authenticated data write requests made by the host. + * @addr : Address of the data to be programmed to or read + * from the RPMB. Address is the serial number of + * the accessed block (half sector 256B). + * @block_count : Number of blocks (half sectors, 256B) requested to be + * read/programmed. + * @result : Includes information about the status of the write counter + * (valid, expired) and result of the access made to the RPMB. + * @req_resp : Defines the type of request and response to/from the memory. + */ +struct rpmb_frame { + __u8 stuff[196]; + __u8 key_mac[32]; + __u8 data[256]; + __u8 nonce[16]; + __be32 write_counter; + __be16 addr; + __be16 block_count; + __be16 result; + __be16 req_resp; +} __attribute__((packed)); + +/** + * struct rpmb_cmd - rpmb access command + * + * @flags: command flags + * 0 - read command + * 1 - write commnad RPMB_F_WRITE + * 2 - reliable write RPMB_F_REL_WRITE + * @nframes: number of rpmb frames in the command + * @frames_ptr: a pointer to the list of rpmb frames + */ +struct rpmb_ioc_cmd { + __u32 flags; + __u32 nframes; + __aligned_u64 frames_ptr; +}; + +/** + * struct rpmb_ioc_req_cmd - rpmb operation request command + * + * @req_type: request type: must match the in frame req_resp + * program key + * get write counter + * write data + * read data + * @icmd: input command + * @ocmd: output/result command + */ +struct rpmb_ioc_req_cmd { + __u64 req_type; + struct rpmb_ioc_cmd icmd; + struct rpmb_ioc_cmd ocmd; +}; + +/** + * struct rpmb_ioc_seq_cmd - rpmb command sequence + * + * @num_of_cmds: number of commands + * @cmds: list of rpmb commands + */ +struct rpmb_ioc_seq_cmd { + __u64 num_of_cmds; + struct rpmb_ioc_cmd cmds[0]; +}; + +#define RPMB_IOC_REQ_CMD _IOWR(0xB5, 80, struct rpmb_ioc_req_cmd) +#define RPMB_IOC_SEQ_CMD _IOWR(0xB5, 81, struct rpmb_ioc_seq_cmd) + +#endif /* __RPMB_H__ */