From 40f7f9e08f61a26d76b320caae0d150640b2ce13 Mon Sep 17 00:00:00 2001 From: "Haoran.Wang" Date: Mon, 28 Aug 2017 18:30:06 +0800 Subject: [PATCH] MLK-18591-5 android: iot: Export eMMC RPMB interface for Secure Storage proxy Secure Storage service in Trusty OS will compute the encrypted mmc frame and the rpmb proxy inject the frame to driver directly. So that need to export RPMB related interface for Secure Storage proxy use. Change-Id: I7f69831a20a440f597d323b610fa615fd4344d05 Signed-off-by: Haoran.Wang (cherry picked from commit 4d2c1873ce8221e35874265e41dc42a6df169659) (cherry picked from commit ce4e9dc35ff89a2429224ae6d0ffb4109cb42e28) (cherry picked from commit 412ece12209e2f916616053ad65b421e95c07955) (cherry picked from commit 24d2c13f7e1f7ff5908cc9547abc8f781928f64f) --- drivers/mmc/rpmb.c | 40 ++++++++++------------------------------ include/mmc.h | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/drivers/mmc/rpmb.c b/drivers/mmc/rpmb.c index ea7e506666..0aafc7e95b 100644 --- a/drivers/mmc/rpmb.c +++ b/drivers/mmc/rpmb.c @@ -41,12 +41,6 @@ #define RPMB_ERR_CNT_EXPIRED 0x80 #define RPMB_ERR_MSK 0x7 -/* Sizes of RPMB data frame */ -#define RPMB_SZ_STUFF 196 -#define RPMB_SZ_MAC 32 -#define RPMB_SZ_DATA 256 -#define RPMB_SZ_NONCE 16 - #define SHA256_BLOCK_SIZE 64 /* Error messages */ @@ -61,20 +55,6 @@ static const char * const rpmb_err_msg[] = { "Authentication key not yet programmed", }; - -/* Structure of RPMB data frame. */ -struct s_rpmb { - unsigned char stuff[RPMB_SZ_STUFF]; - unsigned char mac[RPMB_SZ_MAC]; - unsigned char data[RPMB_SZ_DATA]; - unsigned char nonce[RPMB_SZ_NONCE]; - unsigned int write_counter; - unsigned short address; - unsigned short block_count; - unsigned short result; - unsigned short request; -}; - static int mmc_set_blockcount(struct mmc *mmc, unsigned int blockcount, bool is_rel_write) { @@ -88,7 +68,7 @@ static int mmc_set_blockcount(struct mmc *mmc, unsigned int blockcount, return mmc_send_cmd(mmc, &cmd, NULL); } -static int mmc_rpmb_request(struct mmc *mmc, const struct s_rpmb *s, +int mmc_rpmb_request(struct mmc *mmc, const struct s_rpmb *s, unsigned int count, bool is_rel_write) { struct mmc_cmd cmd = {0}; @@ -112,7 +92,7 @@ static int mmc_rpmb_request(struct mmc *mmc, const struct s_rpmb *s, cmd.resp_type = MMC_RSP_R1; data.src = (const char *)s; - data.blocks = 1; + data.blocks = count; data.blocksize = MMC_MAX_BLOCK_LEN; data.flags = MMC_DATA_WRITE; @@ -125,14 +105,14 @@ static int mmc_rpmb_request(struct mmc *mmc, const struct s_rpmb *s, } return 0; } -static int mmc_rpmb_response(struct mmc *mmc, struct s_rpmb *s, - unsigned short expected) +int mmc_rpmb_response(struct mmc *mmc, struct s_rpmb *s, + unsigned int count, unsigned short expected) { struct mmc_cmd cmd = {0}; struct mmc_data data; int ret; - ret = mmc_set_blockcount(mmc, 1, false); + ret = mmc_set_blockcount(mmc, count, false); if (ret) { #ifdef CONFIG_MMC_RPMB_TRACE printf("%s:mmc_set_blockcount-> %d\n", __func__, ret); @@ -144,7 +124,7 @@ static int mmc_rpmb_response(struct mmc *mmc, struct s_rpmb *s, cmd.resp_type = MMC_RSP_R1; data.dest = (char *)s; - data.blocks = 1; + data.blocks = count; data.blocksize = MMC_MAX_BLOCK_LEN; data.flags = MMC_DATA_READ; @@ -156,7 +136,7 @@ static int mmc_rpmb_response(struct mmc *mmc, struct s_rpmb *s, return -1; } /* Check the response and the status */ - if (be16_to_cpu(s->request) != expected) { + if (expected && be16_to_cpu(s->request) != expected) { #ifdef CONFIG_MMC_RPMB_TRACE printf("%s:response= %x\n", __func__, be16_to_cpu(s->request)); @@ -183,7 +163,7 @@ static int mmc_rpmb_status(struct mmc *mmc, unsigned short expected) return -1; /* Read the result */ - return mmc_rpmb_response(mmc, rpmb_frame, expected); + return mmc_rpmb_response(mmc, rpmb_frame, 1, expected); } static void rpmb_hmac(unsigned char *key, unsigned char *buff, int len, unsigned char *output) @@ -241,7 +221,7 @@ int mmc_rpmb_get_counter(struct mmc *mmc, unsigned long *pcounter) return -1; /* Read the result */ - ret = mmc_rpmb_response(mmc, rpmb_frame, RPMB_RESP_WCOUNTER); + ret = mmc_rpmb_response(mmc, rpmb_frame, 1, RPMB_RESP_WCOUNTER); if (ret) return ret; @@ -277,7 +257,7 @@ int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk, break; /* Read the result */ - if (mmc_rpmb_response(mmc, rpmb_frame, RPMB_RESP_READ_DATA)) + if (mmc_rpmb_response(mmc, rpmb_frame, 1, RPMB_RESP_READ_DATA)) break; /* Check the HMAC if key is provided */ diff --git a/include/mmc.h b/include/mmc.h index 5f92622752..66f28e94f7 100644 --- a/include/mmc.h +++ b/include/mmc.h @@ -876,6 +876,24 @@ int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode); /* Function to modify the RST_n_FUNCTION field of EXT_CSD */ int mmc_set_rst_n_function(struct mmc *mmc, u8 enable); /* Functions to read / write the RPMB partition */ +/* Sizes of RPMB data frame */ +#define RPMB_SZ_STUFF 196 +#define RPMB_SZ_MAC 32 +#define RPMB_SZ_DATA 256 +#define RPMB_SZ_NONCE 16 + +/* Structure of RPMB data frame. */ +struct s_rpmb { + unsigned char stuff[RPMB_SZ_STUFF]; + unsigned char mac[RPMB_SZ_MAC]; + unsigned char data[RPMB_SZ_DATA]; + unsigned char nonce[RPMB_SZ_NONCE]; + unsigned long write_counter; + unsigned short address; + unsigned short block_count; + unsigned short result; + unsigned short request; +}; int mmc_rpmb_set_key(struct mmc *mmc, void *key); int mmc_rpmb_get_counter(struct mmc *mmc, unsigned long *counter); int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk, @@ -901,6 +919,11 @@ int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk, int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen, void *rsp, unsigned long rsplen); +int mmc_rpmb_request(struct mmc *mmc, const struct s_rpmb *s, + unsigned int count, bool is_rel_write); +int mmc_rpmb_response(struct mmc *mmc, struct s_rpmb *s, + unsigned int count, unsigned short expected); + #ifdef CONFIG_CMD_BKOPS_ENABLE int mmc_set_bkops_enable(struct mmc *mmc); #endif -- 2.17.1