| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright (C) 2015-2019 Intel Corp. All rights reserved |
| 4 | * Copyright (C) 2021-2022 Linaro Ltd |
| 5 | */ |
| 6 | #ifndef __RPMB_H__ |
| 7 | #define __RPMB_H__ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/types.h> |
| 11 | |
| 12 | /** |
| 13 | * enum rpmb_type - type of underlying storage technology |
| 14 | * |
| 15 | * @RPMB_TYPE_EMMC : emmc (JESD84-B50.1) |
| 16 | * @RPMB_TYPE_UFS : UFS (JESD220) |
| 17 | * @RPMB_TYPE_NVME : NVM Express |
| 18 | */ |
| 19 | enum rpmb_type { |
| 20 | RPMB_TYPE_EMMC, |
| 21 | RPMB_TYPE_UFS, |
| 22 | RPMB_TYPE_NVME, |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * struct rpmb_descr - RPMB description provided by the underlying block device |
| 27 | * |
| 28 | * @type : block device type |
| 29 | * @route_frames : routes frames to and from the RPMB device |
| 30 | * @dev_id : unique device identifier read from the hardware |
| 31 | * @dev_id_len : length of unique device identifier |
| 32 | * @reliable_wr_count: number of sectors that can be written in one access |
| 33 | * @capacity : capacity of the device in units of 128K |
| 34 | * |
| 35 | * @dev_id is intended to be used as input when deriving the authenticaion key. |
| 36 | */ |
| 37 | struct rpmb_descr { |
| 38 | enum rpmb_type type; |
| 39 | int (*route_frames)(struct device *dev, u8 *req, unsigned int req_len, |
| 40 | u8 *resp, unsigned int resp_len); |
| 41 | u8 *dev_id; |
| 42 | size_t dev_id_len; |
| 43 | u16 reliable_wr_count; |
| 44 | u16 capacity; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * struct rpmb_dev - device which can support RPMB partition |
| 49 | * |
| 50 | * @dev : device |
| 51 | * @id : device_id |
| 52 | * @list_node : linked list node |
| 53 | * @descr : RPMB description |
| 54 | */ |
| 55 | struct rpmb_dev { |
| 56 | struct device dev; |
| 57 | int id; |
| 58 | struct list_head list_node; |
| 59 | struct rpmb_descr descr; |
| 60 | }; |
| 61 | |
| 62 | #define to_rpmb_dev(x) container_of((x), struct rpmb_dev, dev) |
| 63 | |
| 64 | /** |
| 65 | * struct rpmb_frame - RPMB frame structure for authenticated access |
| 66 | * |
| 67 | * @stuff : stuff bytes, a padding/reserved area of 196 bytes at the |
| 68 | * beginning of the RPMB frame. They don’t carry meaningful |
| 69 | * data but are required to make the frame exactly 512 bytes. |
| 70 | * @key_mac : The authentication key or the message authentication |
| 71 | * code (MAC) depending on the request/response type. |
| 72 | * The MAC will be delivered in the last (or the only) |
| 73 | * block of data. |
| 74 | * @data : Data to be written or read by signed access. |
| 75 | * @nonce : Random number generated by the host for the requests |
| 76 | * and copied to the response by the RPMB engine. |
| 77 | * @write_counter: Counter value for the total amount of the successful |
| 78 | * authenticated data write requests made by the host. |
| 79 | * @addr : Address of the data to be programmed to or read |
| 80 | * from the RPMB. Address is the serial number of |
| 81 | * the accessed block (half sector 256B). |
| 82 | * @block_count : Number of blocks (half sectors, 256B) requested to be |
| 83 | * read/programmed. |
| 84 | * @result : Includes information about the status of the write counter |
| 85 | * (valid, expired) and result of the access made to the RPMB. |
| 86 | * @req_resp : Defines the type of request and response to/from the memory. |
| 87 | * |
| 88 | * The stuff bytes and big-endian properties are modeled to fit to the spec. |
| 89 | */ |
| 90 | struct rpmb_frame { |
| 91 | u8 stuff[196]; |
| 92 | u8 key_mac[32]; |
| 93 | u8 data[256]; |
| 94 | u8 nonce[16]; |
| 95 | __be32 write_counter; |
| 96 | __be16 addr; |
| 97 | __be16 block_count; |
| 98 | __be16 result; |
| 99 | __be16 req_resp; |
| 100 | }; |
| 101 | |
| 102 | #define RPMB_PROGRAM_KEY 0x1 /* Program RPMB Authentication Key */ |
| 103 | #define RPMB_GET_WRITE_COUNTER 0x2 /* Read RPMB write counter */ |
| 104 | #define RPMB_WRITE_DATA 0x3 /* Write data to RPMB partition */ |
| 105 | #define RPMB_READ_DATA 0x4 /* Read data from RPMB partition */ |
| 106 | #define RPMB_RESULT_READ 0x5 /* Read result request (Internal) */ |
| 107 | |
| 108 | #if IS_ENABLED(CONFIG_RPMB) |
| 109 | struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev); |
| 110 | void rpmb_dev_put(struct rpmb_dev *rdev); |
| 111 | struct rpmb_dev *rpmb_dev_find_device(const void *data, |
| 112 | const struct rpmb_dev *start, |
| 113 | int (*match)(struct device *dev, |
| 114 | const void *data)); |
| 115 | int rpmb_interface_register(struct class_interface *intf); |
| 116 | void rpmb_interface_unregister(struct class_interface *intf); |
| 117 | struct rpmb_dev *rpmb_dev_register(struct device *dev, |
| 118 | struct rpmb_descr *descr); |
| 119 | int rpmb_dev_unregister(struct rpmb_dev *rdev); |
| 120 | |
| 121 | int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req, |
| 122 | unsigned int req_len, u8 *resp, unsigned int resp_len); |
| 123 | |
| 124 | #else |
| 125 | static inline struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev) |
| 126 | { |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | static inline void rpmb_dev_put(struct rpmb_dev *rdev) { } |
| 131 | |
| 132 | static inline struct rpmb_dev * |
| 133 | rpmb_dev_find_device(const void *data, const struct rpmb_dev *start, |
| 134 | int (*match)(struct device *dev, const void *data)) |
| 135 | { |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static inline int rpmb_interface_register(struct class_interface *intf) |
| 140 | { |
| 141 | return -EOPNOTSUPP; |
| 142 | } |
| 143 | |
| 144 | static inline void rpmb_interface_unregister(struct class_interface *intf) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | static inline struct rpmb_dev * |
| 149 | rpmb_dev_register(struct device *dev, struct rpmb_descr *descr) |
| 150 | { |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | static inline int rpmb_dev_unregister(struct rpmb_dev *dev) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static inline int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req, |
| 160 | unsigned int req_len, u8 *resp, |
| 161 | unsigned int resp_len) |
| 162 | { |
| 163 | return -EOPNOTSUPP; |
| 164 | } |
| 165 | #endif /* CONFIG_RPMB */ |
| 166 | |
| 167 | #endif /* __RPMB_H__ */ |
| 168 | |