blob: 91304583a19311231459accae4e73e14a30ce609 [file] [log] [blame]
Michael Ryleev7a2bc372016-03-08 15:16:11 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/ioctl.h>
24
25#include <linux/major.h>
26#include <linux/mmc/ioctl.h>
27
28#include "ipc.h"
29#include "log.h"
30#include "rpmb.h"
31#include "storage.h"
32
33#define MMC_READ_MULTIPLE_BLOCK 18
34#define MMC_WRITE_MULTIPLE_BLOCK 25
35#define MMC_RELIABLE_WRITE_FLAG (1 << 31)
36
37#define MMC_RSP_PRESENT (1 << 0)
38#define MMC_RSP_CRC (1 << 2)
39#define MMC_RSP_OPCODE (1 << 4)
40#define MMC_CMD_ADTC (1 << 5)
41#define MMC_RSP_SPI_S1 (1 << 7)
42#define MMC_RSP_R1 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
43#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
44
45#define MMC_WRITE_FLAG_R 0
46#define MMC_WRITE_FLAG_W 1
47#define MMC_WRITE_FLAG_RELW (MMC_WRITE_FLAG_W | MMC_RELIABLE_WRITE_FLAG)
48
49#define MMC_BLOCK_SIZE 512
50
51static int rpmb_fd = -1;
52static uint8_t read_buf[4096];
53
54#ifdef RPMB_DEBUG
55
56static void print_buf(const char *prefix, const uint8_t *buf, size_t size)
57{
58 size_t i;
59
60 printf("%s @%p [%zu]", prefix, buf, size);
61 for (i = 0; i < size; i++) {
62 if (i && i % 32 == 0)
63 printf("\n%*s", (int) strlen(prefix), "");
64 printf(" %02x", buf[i]);
65 }
66 printf("\n");
67 fflush(stdout);
68}
69
70#endif
71
72
73int rpmb_send(struct storage_msg *msg, const void *r, size_t req_len)
74{
75 int rc;
76 struct {
77 struct mmc_ioc_multi_cmd multi;
78 struct mmc_ioc_cmd cmd_buf[3];
79 } mmc = {};
80 struct mmc_ioc_cmd *cmd = mmc.multi.cmds;
81 const struct storage_rpmb_send_req *req = r;
82
83 if (req_len < sizeof(*req)) {
84 ALOGW("malformed rpmb request: invalid length (%zu < %zu)\n",
85 req_len, sizeof(*req));
86 msg->result = STORAGE_ERR_NOT_VALID;
87 goto err_response;
88 }
89
90 size_t expected_len =
91 sizeof(*req) + req->reliable_write_size + req->write_size;
92 if (req_len != expected_len) {
93 ALOGW("malformed rpmb request: invalid length (%zu != %zu)\n",
94 req_len, expected_len);
95 msg->result = STORAGE_ERR_NOT_VALID;
96 goto err_response;
97 }
98
99 const uint8_t *write_buf = req->payload;
100 if (req->reliable_write_size) {
101 if ((req->reliable_write_size % MMC_BLOCK_SIZE) != 0) {
102 ALOGW("invalid reliable write size %u\n", req->reliable_write_size);
103 msg->result = STORAGE_ERR_NOT_VALID;
104 goto err_response;
105 }
106
107 cmd->write_flag = MMC_WRITE_FLAG_RELW;
108 cmd->opcode = MMC_WRITE_MULTIPLE_BLOCK;
109 cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
110 cmd->blksz = MMC_BLOCK_SIZE;
111 cmd->blocks = req->reliable_write_size / MMC_BLOCK_SIZE;
112 mmc_ioc_cmd_set_data((*cmd), write_buf);
113#ifdef RPMB_DEBUG
114 ALOGI("opcode: 0x%x, write_flag: 0x%x\n", cmd->opcode, cmd->write_flag);
115 print_buf("request: ", write_buf, req->reliable_write_size);
116#endif
117 write_buf += req->reliable_write_size;
118 mmc.multi.num_of_cmds++;
119 cmd++;
120 }
121
122 if (req->write_size) {
123 if ((req->write_size % MMC_BLOCK_SIZE) != 0) {
124 ALOGW("invalid write size %u\n", req->write_size);
125 msg->result = STORAGE_ERR_NOT_VALID;
126 goto err_response;
127 }
128
129 cmd->write_flag = MMC_WRITE_FLAG_W;
130 cmd->opcode = MMC_WRITE_MULTIPLE_BLOCK;
131 cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
132 cmd->blksz = MMC_BLOCK_SIZE;
133 cmd->blocks = req->write_size / MMC_BLOCK_SIZE;
134 mmc_ioc_cmd_set_data((*cmd), write_buf);
135#ifdef RPMB_DEBUG
136 ALOGI("opcode: 0x%x, write_flag: 0x%x\n", cmd->opcode, cmd->write_flag);
137 print_buf("request: ", write_buf, req->write_size);
138#endif
139 write_buf += req->write_size;
140 mmc.multi.num_of_cmds++;
141 cmd++;
142 }
143
144 if (req->read_size) {
145 if (req->read_size % MMC_BLOCK_SIZE != 0 ||
146 req->read_size > sizeof(read_buf)) {
147 ALOGE("%s: invalid read size %u\n", __func__, req->read_size);
148 msg->result = STORAGE_ERR_NOT_VALID;
149 goto err_response;
150 }
151
152 cmd->write_flag = MMC_WRITE_FLAG_R;
153 cmd->opcode = MMC_READ_MULTIPLE_BLOCK;
154 cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC,
155 cmd->blksz = MMC_BLOCK_SIZE;
156 cmd->blocks = req->read_size / MMC_BLOCK_SIZE;
157 mmc_ioc_cmd_set_data((*cmd), read_buf);
158#ifdef RPMB_DEBUG
159 ALOGI("opcode: 0x%x, write_flag: 0x%x\n", cmd->opcode, cmd->write_flag);
160#endif
161 mmc.multi.num_of_cmds++;
162 cmd++;
163 }
164
165 rc = ioctl(rpmb_fd, MMC_IOC_MULTI_CMD, &mmc.multi);
166 if (rc < 0) {
167 ALOGE("%s: mmc ioctl failed: %d, %s\n", __func__, rc, strerror(errno));
168 msg->result = STORAGE_ERR_GENERIC;
169 goto err_response;
170 }
171#ifdef RPMB_DEBUG
172 if (req->read_size)
173 print_buf("response: ", read_buf, req->read_size);
174#endif
175
176 if (msg->flags & STORAGE_MSG_FLAG_POST_COMMIT) {
177 /*
178 * Nothing todo for post msg commit request as MMC_IOC_MULTI_CMD
179 * is fully synchronous in this implementation.
180 */
181 }
182
183 msg->result = STORAGE_NO_ERROR;
184 return ipc_respond(msg, read_buf, req->read_size);
185
186err_response:
187 return ipc_respond(msg, NULL, 0);
188}
189
190
191int rpmb_open(const char *rpmb_devname)
192{
193 int rc;
194
195 rc = open(rpmb_devname, O_RDWR, 0);
196 if (rc < 0) {
197 ALOGE("unable (%d) to open rpmb device '%s': %s\n",
198 errno, rpmb_devname, strerror(errno));
199 return rc;
200 }
201 rpmb_fd = rc;
202 return 0;
203}
204
205void rpmb_close(void)
206{
207 close(rpmb_fd);
208 rpmb_fd = -1;
209}
210