3333#include <linux/cdev.h>
3434#include <linux/mutex.h>
3535#include <linux/scatterlist.h>
36+ #include <linux/string.h>
3637#include <linux/string_helpers.h>
3738#include <linux/delay.h>
3839#include <linux/capability.h>
3940#include <linux/compat.h>
4041#include <linux/pm_runtime.h>
4142#include <linux/idr.h>
4243#include <linux/debugfs.h>
44+ #include <linux/rpmb.h>
4345
4446#include <linux/mmc/ioctl.h>
4547#include <linux/mmc/card.h>
@@ -76,6 +78,48 @@ MODULE_ALIAS("mmc:block");
7678#define MMC_EXTRACT_INDEX_FROM_ARG (x ) ((x & 0x00FF0000) >> 16)
7779#define MMC_EXTRACT_VALUE_FROM_ARG (x ) ((x & 0x0000FF00) >> 8)
7880
81+ /**
82+ * struct rpmb_frame - rpmb frame as defined by eMMC 5.1 (JESD84-B51)
83+ *
84+ * @stuff : stuff bytes
85+ * @key_mac : The authentication key or the message authentication
86+ * code (MAC) depending on the request/response type.
87+ * The MAC will be delivered in the last (or the only)
88+ * block of data.
89+ * @data : Data to be written or read by signed access.
90+ * @nonce : Random number generated by the host for the requests
91+ * and copied to the response by the RPMB engine.
92+ * @write_counter: Counter value for the total amount of the successful
93+ * authenticated data write requests made by the host.
94+ * @addr : Address of the data to be programmed to or read
95+ * from the RPMB. Address is the serial number of
96+ * the accessed block (half sector 256B).
97+ * @block_count : Number of blocks (half sectors, 256B) requested to be
98+ * read/programmed.
99+ * @result : Includes information about the status of the write counter
100+ * (valid, expired) and result of the access made to the RPMB.
101+ * @req_resp : Defines the type of request and response to/from the memory.
102+ *
103+ * The stuff bytes and big-endian properties are modeled to fit to the spec.
104+ */
105+ struct rpmb_frame {
106+ u8 stuff [196 ];
107+ u8 key_mac [32 ];
108+ u8 data [256 ];
109+ u8 nonce [16 ];
110+ __be32 write_counter ;
111+ __be16 addr ;
112+ __be16 block_count ;
113+ __be16 result ;
114+ __be16 req_resp ;
115+ } __packed ;
116+
117+ #define RPMB_PROGRAM_KEY 0x1 /* Program RPMB Authentication Key */
118+ #define RPMB_GET_WRITE_COUNTER 0x2 /* Read RPMB write counter */
119+ #define RPMB_WRITE_DATA 0x3 /* Write data to RPMB partition */
120+ #define RPMB_READ_DATA 0x4 /* Read data from RPMB partition */
121+ #define RPMB_RESULT_READ 0x5 /* Read result request (Internal) */
122+
79123static DEFINE_MUTEX (block_mutex );
80124
81125/*
@@ -155,6 +199,7 @@ static const struct bus_type mmc_rpmb_bus_type = {
155199 * @id: unique device ID number
156200 * @part_index: partition index (0 on first)
157201 * @md: parent MMC block device
202+ * @rdev: registered RPMB device
158203 * @node: list item, so we can put this device on a list
159204 */
160205struct mmc_rpmb_data {
@@ -163,6 +208,7 @@ struct mmc_rpmb_data {
163208 int id ;
164209 unsigned int part_index ;
165210 struct mmc_blk_data * md ;
211+ struct rpmb_dev * rdev ;
166212 struct list_head node ;
167213};
168214
@@ -2670,7 +2716,6 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
26702716
26712717 get_device (& rpmb -> dev );
26722718 filp -> private_data = rpmb ;
2673- mmc_blk_get (rpmb -> md -> disk );
26742719
26752720 return nonseekable_open (inode , filp );
26762721}
@@ -2680,7 +2725,6 @@ static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
26802725 struct mmc_rpmb_data * rpmb = container_of (inode -> i_cdev ,
26812726 struct mmc_rpmb_data , chrdev );
26822727
2683- mmc_blk_put (rpmb -> md );
26842728 put_device (& rpmb -> dev );
26852729
26862730 return 0 ;
@@ -2701,10 +2745,165 @@ static void mmc_blk_rpmb_device_release(struct device *dev)
27012745{
27022746 struct mmc_rpmb_data * rpmb = dev_get_drvdata (dev );
27032747
2748+ rpmb_dev_unregister (rpmb -> rdev );
2749+ mmc_blk_put (rpmb -> md );
27042750 ida_free (& mmc_rpmb_ida , rpmb -> id );
27052751 kfree (rpmb );
27062752}
27072753
2754+ static void free_idata (struct mmc_blk_ioc_data * * idata , unsigned int cmd_count )
2755+ {
2756+ unsigned int n ;
2757+
2758+ for (n = 0 ; n < cmd_count ; n ++ )
2759+ kfree (idata [n ]);
2760+ kfree (idata );
2761+ }
2762+
2763+ static struct mmc_blk_ioc_data * * alloc_idata (struct mmc_rpmb_data * rpmb ,
2764+ unsigned int cmd_count )
2765+ {
2766+ struct mmc_blk_ioc_data * * idata ;
2767+ unsigned int n ;
2768+
2769+ idata = kcalloc (cmd_count , sizeof (* idata ), GFP_KERNEL );
2770+ if (!idata )
2771+ return NULL ;
2772+
2773+ for (n = 0 ; n < cmd_count ; n ++ ) {
2774+ idata [n ] = kcalloc (1 , sizeof (* * idata ), GFP_KERNEL );
2775+ if (!idata [n ]) {
2776+ free_idata (idata , n );
2777+ return NULL ;
2778+ }
2779+ idata [n ]-> rpmb = rpmb ;
2780+ }
2781+
2782+ return idata ;
2783+ }
2784+
2785+ static void set_idata (struct mmc_blk_ioc_data * idata , u32 opcode ,
2786+ int write_flag , u8 * buf , unsigned int buf_bytes )
2787+ {
2788+ /*
2789+ * The size of an RPMB frame must match what's expected by the
2790+ * hardware.
2791+ */
2792+ BUILD_BUG_ON (sizeof (struct rpmb_frame ) != 512 );
2793+
2794+ idata -> ic .opcode = opcode ;
2795+ idata -> ic .flags = MMC_RSP_R1 | MMC_CMD_ADTC ;
2796+ idata -> ic .write_flag = write_flag ;
2797+ idata -> ic .blksz = sizeof (struct rpmb_frame );
2798+ idata -> ic .blocks = buf_bytes / idata -> ic .blksz ;
2799+ idata -> buf = buf ;
2800+ idata -> buf_bytes = buf_bytes ;
2801+ }
2802+
2803+ static int mmc_route_rpmb_frames (struct device * dev , u8 * req ,
2804+ unsigned int req_len , u8 * resp ,
2805+ unsigned int resp_len )
2806+ {
2807+ struct rpmb_frame * frm = (struct rpmb_frame * )req ;
2808+ struct mmc_rpmb_data * rpmb = dev_get_drvdata (dev );
2809+ struct mmc_blk_data * md = rpmb -> md ;
2810+ struct mmc_blk_ioc_data * * idata ;
2811+ struct mmc_queue_req * mq_rq ;
2812+ unsigned int cmd_count ;
2813+ struct request * rq ;
2814+ u16 req_type ;
2815+ bool write ;
2816+ int ret ;
2817+
2818+ if (IS_ERR (md -> queue .card ))
2819+ return PTR_ERR (md -> queue .card );
2820+
2821+ if (req_len < sizeof (* frm ))
2822+ return - EINVAL ;
2823+
2824+ req_type = be16_to_cpu (frm -> req_resp );
2825+ switch (req_type ) {
2826+ case RPMB_PROGRAM_KEY :
2827+ if (req_len != sizeof (struct rpmb_frame ) ||
2828+ resp_len != sizeof (struct rpmb_frame ))
2829+ return - EINVAL ;
2830+ write = true;
2831+ break ;
2832+ case RPMB_GET_WRITE_COUNTER :
2833+ if (req_len != sizeof (struct rpmb_frame ) ||
2834+ resp_len != sizeof (struct rpmb_frame ))
2835+ return - EINVAL ;
2836+ write = false;
2837+ break ;
2838+ case RPMB_WRITE_DATA :
2839+ if (req_len % sizeof (struct rpmb_frame ) ||
2840+ resp_len != sizeof (struct rpmb_frame ))
2841+ return - EINVAL ;
2842+ write = true;
2843+ break ;
2844+ case RPMB_READ_DATA :
2845+ if (req_len != sizeof (struct rpmb_frame ) ||
2846+ resp_len % sizeof (struct rpmb_frame ))
2847+ return - EINVAL ;
2848+ write = false;
2849+ break ;
2850+ default :
2851+ return - EINVAL ;
2852+ }
2853+
2854+ if (write )
2855+ cmd_count = 3 ;
2856+ else
2857+ cmd_count = 2 ;
2858+
2859+ idata = alloc_idata (rpmb , cmd_count );
2860+ if (!idata )
2861+ return - ENOMEM ;
2862+
2863+ if (write ) {
2864+ struct rpmb_frame * frm = (struct rpmb_frame * )resp ;
2865+
2866+ /* Send write request frame(s) */
2867+ set_idata (idata [0 ], MMC_WRITE_MULTIPLE_BLOCK ,
2868+ 1 | MMC_CMD23_ARG_REL_WR , req , req_len );
2869+
2870+ /* Send result request frame */
2871+ memset (frm , 0 , sizeof (* frm ));
2872+ frm -> req_resp = cpu_to_be16 (RPMB_RESULT_READ );
2873+ set_idata (idata [1 ], MMC_WRITE_MULTIPLE_BLOCK , 1 , resp ,
2874+ resp_len );
2875+
2876+ /* Read response frame */
2877+ set_idata (idata [2 ], MMC_READ_MULTIPLE_BLOCK , 0 , resp , resp_len );
2878+ } else {
2879+ /* Send write request frame(s) */
2880+ set_idata (idata [0 ], MMC_WRITE_MULTIPLE_BLOCK , 1 , req , req_len );
2881+
2882+ /* Read response frame */
2883+ set_idata (idata [1 ], MMC_READ_MULTIPLE_BLOCK , 0 , resp , resp_len );
2884+ }
2885+
2886+ rq = blk_mq_alloc_request (md -> queue .queue , REQ_OP_DRV_OUT , 0 );
2887+ if (IS_ERR (rq )) {
2888+ ret = PTR_ERR (rq );
2889+ goto out ;
2890+ }
2891+
2892+ mq_rq = req_to_mmc_queue_req (rq );
2893+ mq_rq -> drv_op = MMC_DRV_OP_IOCTL_RPMB ;
2894+ mq_rq -> drv_op_result = - EIO ;
2895+ mq_rq -> drv_op_data = idata ;
2896+ mq_rq -> ioc_count = cmd_count ;
2897+ blk_execute_rq (rq , false);
2898+ ret = req_to_mmc_queue_req (rq )-> drv_op_result ;
2899+
2900+ blk_mq_free_request (rq );
2901+
2902+ out :
2903+ free_idata (idata , cmd_count );
2904+ return ret ;
2905+ }
2906+
27082907static int mmc_blk_alloc_rpmb_part (struct mmc_card * card ,
27092908 struct mmc_blk_data * md ,
27102909 unsigned int part_index ,
@@ -2739,6 +2938,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
27392938 rpmb -> dev .release = mmc_blk_rpmb_device_release ;
27402939 device_initialize (& rpmb -> dev );
27412940 dev_set_drvdata (& rpmb -> dev , rpmb );
2941+ mmc_blk_get (md -> disk );
27422942 rpmb -> md = md ;
27432943
27442944 cdev_init (& rpmb -> chrdev , & mmc_rpmb_fileops );
@@ -3000,6 +3200,42 @@ static void mmc_blk_remove_debugfs(struct mmc_card *card,
30003200
30013201#endif /* CONFIG_DEBUG_FS */
30023202
3203+ static void mmc_blk_rpmb_add (struct mmc_card * card )
3204+ {
3205+ struct mmc_blk_data * md = dev_get_drvdata (& card -> dev );
3206+ struct mmc_rpmb_data * rpmb ;
3207+ struct rpmb_dev * rdev ;
3208+ unsigned int n ;
3209+ u32 cid [4 ];
3210+ struct rpmb_descr descr = {
3211+ .type = RPMB_TYPE_EMMC ,
3212+ .route_frames = mmc_route_rpmb_frames ,
3213+ .reliable_wr_count = card -> ext_csd .enhanced_rpmb_supported ?
3214+ 2 : 32 ,
3215+ .capacity = card -> ext_csd .raw_rpmb_size_mult ,
3216+ .dev_id = (void * )cid ,
3217+ .dev_id_len = sizeof (cid ),
3218+ };
3219+
3220+ /*
3221+ * Provice CID as an octet array. The CID needs to be interpreted
3222+ * when used as input to derive the RPMB key since some fields
3223+ * will change due to firmware updates.
3224+ */
3225+ for (n = 0 ; n < 4 ; n ++ )
3226+ cid [n ] = be32_to_cpu ((__force __be32 )card -> raw_cid [n ]);
3227+
3228+ list_for_each_entry (rpmb , & md -> rpmbs , node ) {
3229+ rdev = rpmb_dev_register (& rpmb -> dev , & descr );
3230+ if (IS_ERR (rdev )) {
3231+ pr_warn ("%s: could not register RPMB device\n" ,
3232+ dev_name (& rpmb -> dev ));
3233+ continue ;
3234+ }
3235+ rpmb -> rdev = rdev ;
3236+ }
3237+ }
3238+
30033239static int mmc_blk_probe (struct mmc_card * card )
30043240{
30053241 struct mmc_blk_data * md ;
@@ -3045,6 +3281,8 @@ static int mmc_blk_probe(struct mmc_card *card)
30453281 pm_runtime_enable (& card -> dev );
30463282 }
30473283
3284+ mmc_blk_rpmb_add (card );
3285+
30483286 return 0 ;
30493287
30503288out :
0 commit comments