• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
49 struct rpmb_packet {
50     uint8_t pad[196];
51     struct rpmb_key key_mac;
52     uint8_t data[RPMB_PACKET_DATA_SIZE];
53     struct rpmb_nonce nonce;
54     struct rpmb_u32 write_counter;
55     struct rpmb_u16 address;
56     struct rpmb_u16 block_count;
57     struct rpmb_u16 result;
58     struct rpmb_u16 req_resp;
59 };
60 
61 enum rpmb_request {
62     RPMB_REQ_PROGRAM_KEY = 0x0001,
63     RPMB_REQ_GET_COUNTER = 0x0002,
64     RPMB_REQ_DATA_WRITE = 0x0003,
65     RPMB_REQ_DATA_READ = 0x0004,
66     RPMB_REQ_RESULT_READ = 0x0005,
67 };
68 
69 enum rpmb_response {
70     RPMB_RESP_PROGRAM_KEY = 0x0100,
71     RPMB_RESP_GET_COUNTER = 0x0200,
72     RPMB_RESP_DATA_WRITE = 0x0300,
73     RPMB_RESP_DATA_READ = 0x0400,
74 };
75 
76 enum rpmb_result {
77     RPMB_RES_OK = 0x0000,
78     RPMB_RES_GENERAL_FAILURE = 0x0001,
79     RPMB_RES_AUTH_FAILURE = 0x0002,
80     RPMB_RES_COUNT_FAILURE = 0x0003,
81     RPMB_RES_ADDR_FAILURE = 0x0004,
82     RPMB_RES_WRITE_FAILURE = 0x0005,
83     RPMB_RES_READ_FAILURE = 0x0006,
84     RPMB_RES_NO_AUTH_KEY = 0x0007,
85 
86     RPMB_RES_WRITE_COUNTER_EXPIRED = 0x0080,
87 };
88 
rpmb_u16(uint16_t val)89 static inline struct rpmb_u16 rpmb_u16(uint16_t val) {
90     struct rpmb_u16 ret = {{
91             (uint8_t)(val >> 8),
92             (uint8_t)(val >> 0),
93     }};
94     return ret;
95 }
96 
rpmb_u32(uint32_t val)97 static inline struct rpmb_u32 rpmb_u32(uint32_t val) {
98     struct rpmb_u32 ret = {{
99             (uint8_t)(val >> 24),
100             (uint8_t)(val >> 16),
101             (uint8_t)(val >> 8),
102             (uint8_t)(val >> 0),
103     }};
104     return ret;
105 }
106 
rpmb_get_u16(struct rpmb_u16 u16)107 static inline uint16_t rpmb_get_u16(struct rpmb_u16 u16) {
108     size_t i;
109     uint16_t val;
110 
111     val = 0;
112     for (i = 0; i < sizeof(u16.byte); i++)
113         val = val << 8 | u16.byte[i];
114 
115     return val;
116 }
117 
rpmb_get_u32(struct rpmb_u32 u32)118 static inline uint32_t rpmb_get_u32(struct rpmb_u32 u32) {
119     size_t i;
120     uint32_t val;
121 
122     val = 0;
123     for (i = 0; i < sizeof(u32.byte); i++)
124         val = val << 8 | u32.byte[i];
125 
126     return val;
127 }
128