1 /*
2 * Copyright (C) 2018 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 #pragma once
18
19 #include <stdint.h>
20
21 #include "rpmb.h" /* For struct rpmb_key */
22
23 #define MMC_READ_MULTIPLE_BLOCK 18
24 #define MMC_WRITE_MULTIPLE_BLOCK 25
25 #define MMC_RELIABLE_WRITE_FLAG (1 << 31)
26
27 #define MMC_RSP_PRESENT (1 << 0)
28 #define MMC_RSP_CRC (1 << 2)
29 #define MMC_RSP_OPCODE (1 << 4)
30 #define MMC_CMD_ADTC (1 << 5)
31 #define MMC_RSP_SPI_S1 (1 << 7)
32 #define MMC_RSP_R1 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
33 #define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
34
35 struct rpmb_nonce {
36 uint8_t byte[16];
37 };
38
39 struct rpmb_u16 {
40 uint8_t byte[2];
41 };
42
43 struct rpmb_u32 {
44 uint8_t byte[4];
45 };
46
47 #define RPMB_PACKET_DATA_SIZE (256)
48 #define RPMB_WRITE_COUNTER_EXPIRED_VALUE (~0U)
49
50 struct rpmb_packet {
51 uint8_t pad[196];
52 struct rpmb_key key_mac;
53 uint8_t data[RPMB_PACKET_DATA_SIZE];
54 struct rpmb_nonce nonce;
55 struct rpmb_u32 write_counter;
56 struct rpmb_u16 address;
57 struct rpmb_u16 block_count;
58 struct rpmb_u16 result;
59 struct rpmb_u16 req_resp;
60 };
61
62 enum rpmb_request {
63 RPMB_REQ_PROGRAM_KEY = 0x0001,
64 RPMB_REQ_GET_COUNTER = 0x0002,
65 RPMB_REQ_DATA_WRITE = 0x0003,
66 RPMB_REQ_DATA_READ = 0x0004,
67 RPMB_REQ_RESULT_READ = 0x0005,
68 };
69
70 enum rpmb_response {
71 RPMB_RESP_PROGRAM_KEY = 0x0100,
72 RPMB_RESP_GET_COUNTER = 0x0200,
73 RPMB_RESP_DATA_WRITE = 0x0300,
74 RPMB_RESP_DATA_READ = 0x0400,
75 };
76
77 enum rpmb_result {
78 RPMB_RES_OK = 0x0000,
79 RPMB_RES_GENERAL_FAILURE = 0x0001,
80 RPMB_RES_AUTH_FAILURE = 0x0002,
81 RPMB_RES_COUNT_FAILURE = 0x0003,
82 RPMB_RES_ADDR_FAILURE = 0x0004,
83 RPMB_RES_WRITE_FAILURE = 0x0005,
84 RPMB_RES_READ_FAILURE = 0x0006,
85 RPMB_RES_NO_AUTH_KEY = 0x0007,
86
87 RPMB_RES_WRITE_COUNTER_EXPIRED = 0x0080,
88 };
89
rpmb_u16(uint16_t val)90 static inline struct rpmb_u16 rpmb_u16(uint16_t val) {
91 struct rpmb_u16 ret = {{
92 (uint8_t)(val >> 8),
93 (uint8_t)(val >> 0),
94 }};
95 return ret;
96 }
97
rpmb_u32(uint32_t val)98 static inline struct rpmb_u32 rpmb_u32(uint32_t val) {
99 struct rpmb_u32 ret = {{
100 (uint8_t)(val >> 24),
101 (uint8_t)(val >> 16),
102 (uint8_t)(val >> 8),
103 (uint8_t)(val >> 0),
104 }};
105 return ret;
106 }
107
rpmb_get_u16(struct rpmb_u16 u16)108 static inline uint16_t rpmb_get_u16(struct rpmb_u16 u16) {
109 size_t i;
110 uint16_t val;
111
112 val = 0;
113 for (i = 0; i < sizeof(u16.byte); i++)
114 val = val << 8 | u16.byte[i];
115
116 return val;
117 }
118
rpmb_get_u32(struct rpmb_u32 u32)119 static inline uint32_t rpmb_get_u32(struct rpmb_u32 u32) {
120 size_t i;
121 uint32_t val;
122
123 val = 0;
124 for (i = 0; i < sizeof(u32.byte); i++)
125 val = val << 8 | u32.byte[i];
126
127 return val;
128 }
129