Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/sdmmc.h
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2025 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#ifndef _SDMMC_H_
19
#define _SDMMC_H_
20
21
#include <utils/types.h>
22
#include <storage/sd_def.h>
23
#include <storage/sdmmc_driver.h>
24
25
#define SDMMC_CMD_BLOCKSIZE 64
26
#define SDMMC_DAT_BLOCKSIZE 512
27
28
extern u32 sd_power_cycle_time_start;
29
30
typedef enum _sdmmc_type
31
{
32
MMC_SD = 0,
33
MMC_EMMC = 1,
34
35
EMMC_GPP = 0,
36
EMMC_BOOT0 = 1,
37
EMMC_BOOT1 = 2,
38
EMMC_RPMB = 3
39
} sdmmc_type;
40
41
typedef struct _mmc_sandisk_advanced_report_t
42
{
43
u32 power_inits;
44
45
u32 max_erase_cycles_sys;
46
u32 max_erase_cycles_slc;
47
u32 max_erase_cycles_mlc;
48
49
u32 min_erase_cycles_sys;
50
u32 min_erase_cycles_slc;
51
u32 min_erase_cycles_mlc;
52
53
u32 max_erase_cycles_euda;
54
u32 min_erase_cycles_euda;
55
u32 avg_erase_cycles_euda;
56
u32 read_reclaim_cnt_euda;
57
u32 bad_blocks_euda;
58
59
u32 pre_eol_euda;
60
u32 pre_eol_sys;
61
u32 pre_eol_mlc;
62
63
u32 uncorrectable_ecc;
64
65
u32 temperature_now;
66
u32 temperature_min;
67
u32 temperature_max;
68
69
u32 health_pct_euda;
70
u32 health_pct_sys;
71
u32 health_pct_mlc;
72
73
u32 unk0;
74
u32 unk1;
75
u32 unk2;
76
77
u32 reserved[78];
78
} mmc_sandisk_advanced_report_t;
79
80
typedef struct _mmc_sandisk_report_t
81
{
82
u32 avg_erase_cycles_sys;
83
u32 avg_erase_cycles_slc;
84
u32 avg_erase_cycles_mlc;
85
86
u32 read_reclaim_cnt_sys;
87
u32 read_reclaim_cnt_slc;
88
u32 read_reclaim_cnt_mlc;
89
90
u32 bad_blocks_factory;
91
u32 bad_blocks_sys;
92
u32 bad_blocks_slc;
93
u32 bad_blocks_mlc;
94
95
u32 fw_updates_cnt;
96
97
u8 fw_update_date[12];
98
u8 fw_update_time[8];
99
100
u32 total_writes_100mb;
101
u32 vdrops;
102
u32 vdroops;
103
104
u32 vdrops_failed_data_rec;
105
u32 vdrops_data_rec_ops;
106
107
u32 total_writes_slc_100mb;
108
u32 total_writes_mlc_100mb;
109
110
u32 mlc_bigfile_mode_limit_exceeded;
111
u32 avg_erase_cycles_hybrid;
112
113
mmc_sandisk_advanced_report_t advanced;
114
} mmc_sandisk_report_t;
115
116
typedef struct _mmc_cid
117
{
118
u32 manfid;
119
u8 prod_name[8];
120
u32 serial;
121
u16 oemid;
122
u16 year;
123
u8 prv;
124
u8 hwrev;
125
u8 fwrev;
126
u8 month;
127
} mmc_cid_t;
128
129
typedef struct _mmc_csd
130
{
131
u8 structure;
132
u8 mmca_vsn;
133
u16 cmdclass;
134
u32 c_size;
135
u32 r2w_factor;
136
u32 read_blkbits;
137
u32 capacity;
138
u8 write_protect;
139
u16 busspeed;
140
} mmc_csd_t;
141
142
typedef struct _mmc_ext_csd
143
{
144
//u8 bkops; /* background support bit */
145
//u8 bkops_en; /* manual bkops enable bit */
146
//u8 bkops_status; /* 246 */
147
u8 rev;
148
u8 ext_struct; /* 194 */
149
u8 card_type; /* 196 */
150
u8 pre_eol_info;
151
u8 dev_life_est_a;
152
u8 dev_life_est_b;
153
u8 boot_mult;
154
u8 rpmb_mult;
155
u16 dev_version;
156
u32 cache_size;
157
u32 max_enh_mult;
158
} mmc_ext_csd_t;
159
160
typedef struct _sd_scr
161
{
162
u8 sda_vsn;
163
u8 sda_spec3;
164
u8 bus_widths;
165
u8 cmds;
166
} sd_scr_t;
167
168
typedef struct _sd_ssr
169
{
170
u8 bus_width;
171
u8 speed_class;
172
u8 uhs_grade;
173
u8 video_class;
174
u8 app_class;
175
u8 au_size;
176
u8 uhs_au_size;
177
u8 perf_enhance;
178
u32 protected_size;
179
} sd_ssr_t;
180
181
typedef struct _sd_ext_reg_t
182
{
183
u8 cmdq;
184
u8 cmdq_ext;
185
u8 cache;
186
u8 cache_ext;
187
int valid;
188
} sd_ext_reg_t;
189
190
/*! SDMMC storage context. */
191
typedef struct _sdmmc_storage_t
192
{
193
sdmmc_t *sdmmc;
194
int initialized;
195
int is_low_voltage;
196
int has_sector_access;
197
u32 rca;
198
u32 sec_cnt;
199
u32 partition;
200
u32 max_power;
201
u8 raw_cid[0x10];
202
u8 raw_csd[0x10];
203
u8 raw_scr[8];
204
u8 raw_ssr[0x40];
205
mmc_cid_t cid;
206
mmc_csd_t csd;
207
mmc_ext_csd_t ext_csd;
208
sd_scr_t scr;
209
sd_ssr_t ssr;
210
sd_ext_reg_t ser;
211
} sdmmc_storage_t;
212
213
typedef struct _sd_func_modes_t
214
{
215
u16 access_mode;
216
u16 cmd_system;
217
u16 driver_strength;
218
u16 power_limit;
219
} sd_func_modes_t;
220
221
int sdmmc_storage_end(sdmmc_storage_t *storage);
222
int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf);
223
int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf);
224
int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type);
225
int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition);
226
void sdmmc_storage_init_wait_sd();
227
int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type);
228
int sdmmc_storage_init_gc(sdmmc_storage_t *storage, sdmmc_t *sdmmc);
229
230
int sdmmc_storage_execute_vendor_cmd(sdmmc_storage_t *storage, u32 arg);
231
int sdmmc_storage_vendor_sandisk_report(sdmmc_storage_t *storage, void *buf);
232
233
int mmc_storage_get_ext_csd(sdmmc_storage_t *storage, void *buf);
234
235
int sd_storage_get_ext_reg(sdmmc_storage_t *storage, u8 fno, u8 page, u16 offset, u32 len, void *buf);
236
int sd_storage_get_fmodes(sdmmc_storage_t *storage, u8 *buf, sd_func_modes_t *functions);
237
int sd_storage_get_scr(sdmmc_storage_t *storage, u8 *buf);
238
int sd_storage_get_ssr(sdmmc_storage_t *storage, u8 *buf);
239
u32 sd_storage_get_ssr_au(sdmmc_storage_t *storage);
240
241
void sd_storage_get_ext_regs(sdmmc_storage_t *storage, u8 *buf);
242
int sd_storage_parse_perf_enhance(sdmmc_storage_t *storage, u8 fno, u8 page, u16 offset, u8 *buf);
243
244
#endif
245
246