1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <trusty/rpmb.h>
26 #include <trusty/trusty_dev.h>
27 #include <trusty/util.h>
28
29 #include <common.h>
30 #include <memalign.h>
31 #include <mmc.h>
32
rpmb_storage_get_ctx(void)33 void* rpmb_storage_get_ctx(void) {
34 /* Unused for U-boot */
35 return NULL;
36 }
37
rpmb_storage_put_ctx(void * dev)38 void rpmb_storage_put_ctx(void* dev) {}
39
rpmb_storage_send(void * rpmb_dev,const void * rel_write_data,size_t rel_write_size,const void * write_data,size_t write_size,void * read_buf,size_t read_size)40 int rpmb_storage_send(void* rpmb_dev,
41 const void* rel_write_data,
42 size_t rel_write_size,
43 const void* write_data,
44 size_t write_size,
45 void* read_buf,
46 size_t read_size) {
47 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_rel_write_data, rel_write_size);
48 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_write_data, write_size);
49 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, rpmb_read_data, read_size);
50 int ret = TRUSTY_ERR_NONE;
51 struct mmc* mmc = find_mmc_device(mmc_get_env_dev());
52 char original_part = mmc->block_dev.hwpart;
53
54 /* Switch to RPMB partition */
55 if (mmc->block_dev.hwpart != MMC_PART_RPMB) {
56 ret = mmc_switch_part(mmc, MMC_PART_RPMB);
57 if (ret) {
58 trusty_error("failed to switch to RPMB partition\n");
59 ret = TRUSTY_ERR_GENERIC;
60 goto end;
61 }
62 mmc->block_dev.hwpart = MMC_PART_RPMB;
63 }
64
65 if (rel_write_size) {
66 if (rel_write_size % MMC_BLOCK_SIZE) {
67 trusty_error(
68 "rel_write_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
69 rel_write_size);
70 ret = TRUSTY_ERR_INVALID_ARGS;
71 goto end;
72 }
73 trusty_memcpy(rpmb_rel_write_data, rel_write_data, rel_write_size);
74 ret = mmc_rpmb_request(mmc, (const struct s_rpmb*)rpmb_rel_write_data,
75 rel_write_size / MMC_BLOCK_SIZE, true);
76 if (ret) {
77 trusty_error("failed to execute rpmb reliable write\n");
78 goto end;
79 }
80 }
81 if (write_size) {
82 if (write_size % MMC_BLOCK_SIZE) {
83 trusty_error("write_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
84 write_size);
85 ret = TRUSTY_ERR_INVALID_ARGS;
86 goto end;
87 }
88 trusty_memcpy(rpmb_write_data, write_data, write_size);
89 ret = mmc_rpmb_request(mmc, (const struct s_rpmb*)rpmb_write_data,
90 write_size / MMC_BLOCK_SIZE, false);
91 if (ret) {
92 trusty_error("failed to execute rpmb write\n");
93 goto end;
94 }
95 }
96 if (read_size) {
97 if (read_size % MMC_BLOCK_SIZE) {
98 trusty_error("read_size is not a multiple of MMC_BLOCK_SIZE: %d\n",
99 read_size);
100 ret = TRUSTY_ERR_INVALID_ARGS;
101 goto end;
102 }
103 ret = mmc_rpmb_response(mmc, (struct s_rpmb*)rpmb_read_data,
104 read_size / MMC_BLOCK_SIZE, 0);
105 trusty_memcpy((void*)read_buf, rpmb_read_data, read_size);
106 if (ret < 0) {
107 trusty_error("failed to execute rpmb read\n");
108 }
109 }
110
111 end:
112 /* Return to original partition */
113 if (mmc->block_dev.hwpart != original_part) {
114 if (mmc_switch_part(mmc, original_part) != 0) {
115 trusty_error("failed to switch back to original partition\n");
116 return TRUSTY_ERR_GENERIC;
117 }
118 mmc->block_dev.hwpart = original_part;
119 }
120 return ret;
121 }
122