1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include "esp_log.h"
18 #include "esp_vfs.h"
19 #include "esp_vfs_fat.h"
20 #include "vfs_fat_internal.h"
21 #include "driver/sdspi_host.h"
22 #include "sdmmc_cmd.h"
23 #include "diskio_impl.h"
24 #include "diskio_sdmmc.h"
25 #include "soc/soc_caps.h"
26 #include "driver/sdmmc_defs.h"
27
28 #if SOC_SDMMC_HOST_SUPPORTED
29 #include "driver/sdmmc_host.h"
30 #endif
31
32 static const char* TAG = "vfs_fat_sdmmc";
33 static sdmmc_card_t* s_card = NULL;
34 static uint8_t s_pdrv = FF_DRV_NOT_USED;
35 static char * s_base_path = NULL;
36
37 #define CHECK_EXECUTE_RESULT(err, str) do { \
38 if ((err) !=ESP_OK) { \
39 ESP_LOGE(TAG, str" (0x%x).", err); \
40 goto cleanup; \
41 } \
42 } while(0)
43
44 static void call_host_deinit(const sdmmc_host_t *host_config);
45 static esp_err_t partition_card(const esp_vfs_fat_mount_config_t *mount_config,
46 const char *drv, sdmmc_card_t *card, BYTE pdrv);
47
mount_prepare_mem(const char * base_path,BYTE * out_pdrv,char ** out_dup_path,sdmmc_card_t ** out_card)48 static esp_err_t mount_prepare_mem(const char *base_path,
49 BYTE *out_pdrv,
50 char **out_dup_path,
51 sdmmc_card_t** out_card)
52 {
53 esp_err_t err = ESP_OK;
54 char* dup_path = NULL;
55 sdmmc_card_t* card = NULL;
56
57 // connect SDMMC driver to FATFS
58 BYTE pdrv = FF_DRV_NOT_USED;
59 if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED) {
60 ESP_LOGD(TAG, "the maximum count of volumes is already mounted");
61 return ESP_ERR_NO_MEM;
62
63 }
64
65 // not using ff_memalloc here, as allocation in internal RAM is preferred
66 card = (sdmmc_card_t*)malloc(sizeof(sdmmc_card_t));
67 if (card == NULL) {
68 ESP_LOGD(TAG, "could not locate new sdmmc_card_t");
69 err = ESP_ERR_NO_MEM;
70 goto cleanup;
71 }
72
73 dup_path = strdup(base_path);
74 if(!dup_path){
75 ESP_LOGD(TAG, "could not copy base_path");
76 err = ESP_ERR_NO_MEM;
77 goto cleanup;
78 }
79
80 *out_card = card;
81 *out_pdrv = pdrv;
82 *out_dup_path = dup_path;
83 return ESP_OK;
84 cleanup:
85 free(card);
86 free(dup_path);
87 return err;
88 }
89
mount_to_vfs_fat(const esp_vfs_fat_mount_config_t * mount_config,sdmmc_card_t * card,uint8_t pdrv,const char * base_path)90 static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t *card, uint8_t pdrv,
91 const char *base_path)
92 {
93 FATFS* fs = NULL;
94 esp_err_t err;
95 ff_diskio_register_sdmmc(pdrv, card);
96 ESP_LOGD(TAG, "using pdrv=%i", pdrv);
97 char drv[3] = {(char)('0' + pdrv), ':', 0};
98
99 // connect FATFS to VFS
100 err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
101 if (err == ESP_ERR_INVALID_STATE) {
102 // it's okay, already registered with VFS
103 } else if (err != ESP_OK) {
104 ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", err);
105 goto fail;
106 }
107
108 // Try to mount partition
109 FRESULT res = f_mount(fs, drv, 1);
110 if (res != FR_OK) {
111 err = ESP_FAIL;
112 ESP_LOGW(TAG, "failed to mount card (%d)", res);
113 if (!((res == FR_NO_FILESYSTEM || res == FR_INT_ERR)
114 && mount_config->format_if_mount_failed)) {
115 goto fail;
116 }
117
118 err = partition_card(mount_config, drv, card, pdrv);
119 if (err != ESP_OK) {
120 goto fail;
121 }
122
123 ESP_LOGW(TAG, "mounting again");
124 res = f_mount(fs, drv, 0);
125 if (res != FR_OK) {
126 err = ESP_FAIL;
127 ESP_LOGD(TAG, "f_mount failed after formatting (%d)", res);
128 goto fail;
129 }
130 }
131 return ESP_OK;
132
133 fail:
134 if (fs) {
135 f_mount(NULL, drv, 0);
136 }
137 esp_vfs_fat_unregister_path(base_path);
138 ff_diskio_unregister(pdrv);
139 return err;
140 }
141
partition_card(const esp_vfs_fat_mount_config_t * mount_config,const char * drv,sdmmc_card_t * card,BYTE pdrv)142 static esp_err_t partition_card(const esp_vfs_fat_mount_config_t *mount_config,
143 const char *drv, sdmmc_card_t *card, BYTE pdrv)
144 {
145 FRESULT res = FR_OK;
146 esp_err_t err;
147 const size_t workbuf_size = 4096;
148 void* workbuf = NULL;
149 ESP_LOGW(TAG, "partitioning card");
150
151 workbuf = ff_memalloc(workbuf_size);
152 if (workbuf == NULL) {
153 return ESP_ERR_NO_MEM;
154 }
155
156 DWORD plist[] = {100, 0, 0, 0};
157 res = f_fdisk(pdrv, plist, workbuf);
158 if (res != FR_OK) {
159 err = ESP_FAIL;
160 ESP_LOGD(TAG, "f_fdisk failed (%d)", res);
161 goto fail;
162 }
163 size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
164 card->csd.sector_size,
165 mount_config->allocation_unit_size);
166 ESP_LOGW(TAG, "formatting card, allocation unit size=%d", alloc_unit_size);
167 res = f_mkfs(drv, FM_ANY, alloc_unit_size, workbuf, workbuf_size);
168 if (res != FR_OK) {
169 err = ESP_FAIL;
170 ESP_LOGD(TAG, "f_mkfs failed (%d)", res);
171 goto fail;
172 }
173
174 free(workbuf);
175 return ESP_OK;
176 fail:
177 free(workbuf);
178 return err;
179 }
180
181 #if SOC_SDMMC_HOST_SUPPORTED
init_sdmmc_host(int slot,const void * slot_config,int * out_slot)182 static esp_err_t init_sdmmc_host(int slot, const void *slot_config, int *out_slot)
183 {
184 *out_slot = slot;
185 return sdmmc_host_init_slot(slot, (const sdmmc_slot_config_t*) slot_config);
186 }
187
init_sdspi_host_deprecated(int slot,const void * slot_config,int * out_slot)188 static esp_err_t init_sdspi_host_deprecated(int slot, const void *slot_config, int *out_slot)
189 {
190 *out_slot = slot;
191 return sdspi_host_init_slot(slot, (const sdspi_slot_config_t*) slot_config);
192 }
193
esp_vfs_fat_sdmmc_mount(const char * base_path,const sdmmc_host_t * host_config,const void * slot_config,const esp_vfs_fat_mount_config_t * mount_config,sdmmc_card_t ** out_card)194 esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path,
195 const sdmmc_host_t* host_config,
196 const void* slot_config,
197 const esp_vfs_fat_mount_config_t* mount_config,
198 sdmmc_card_t** out_card)
199 {
200 esp_err_t err;
201 int card_handle = -1; //uninitialized
202 sdmmc_card_t* card = NULL;
203 BYTE pdrv = FF_DRV_NOT_USED;
204 char* dup_path = NULL;
205 bool host_inited = false;
206
207 err = mount_prepare_mem(base_path, &pdrv, &dup_path, &card);
208 if (err != ESP_OK) {
209 ESP_LOGE(TAG, "mount_prepare failed");
210 return err;
211 }
212
213 if (host_config->flags == SDMMC_HOST_FLAG_SPI) {
214 //Deprecated API
215 //the init() function is usually empty, doesn't require any deinit to revert it
216 err = (*host_config->init)();
217 CHECK_EXECUTE_RESULT(err, "host init failed");
218 err = init_sdspi_host_deprecated(host_config->slot, slot_config, &card_handle);
219 CHECK_EXECUTE_RESULT(err, "slot init failed");
220 //Set `host_inited` to true to indicate that host_config->deinit() needs
221 //to be called to revert `init_sdspi_host_deprecated`; set `card_handle`
222 //to -1 to indicate that no other deinit is required.
223 host_inited = true;
224 card_handle = -1;
225 } else {
226 err = (*host_config->init)();
227 CHECK_EXECUTE_RESULT(err, "host init failed");
228 //deinit() needs to be called to revert the init
229 host_inited = true;
230 //If this failed (indicated by card_handle != -1), slot deinit needs to called()
231 //leave card_handle as is to indicate that (though slot deinit not implemented yet.
232 err = init_sdmmc_host(host_config->slot, slot_config, &card_handle);
233 CHECK_EXECUTE_RESULT(err, "slot init failed");
234 }
235
236 // probe and initialize card
237 err = sdmmc_card_init(host_config, card);
238 CHECK_EXECUTE_RESULT(err, "sdmmc_card_init failed");
239
240 err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path);
241 CHECK_EXECUTE_RESULT(err, "mount_to_vfs failed");
242
243 if (out_card != NULL) {
244 *out_card = card;
245 }
246 if (s_card == NULL) {
247 //store the ctx locally to be back-compatible
248 s_card = card;
249 s_pdrv = pdrv;
250 s_base_path = dup_path;
251 } else {
252 free(dup_path);
253 }
254 return ESP_OK;
255 cleanup:
256 if (host_inited) {
257 call_host_deinit(host_config);
258 }
259 free(card);
260 free(dup_path);
261 return err;
262 }
263 #endif
264
init_sdspi_host(int slot,const void * slot_config,int * out_slot)265 static esp_err_t init_sdspi_host(int slot, const void *slot_config, int *out_slot)
266 {
267 esp_err_t err = sdspi_host_init_device((const sdspi_device_config_t*)slot_config, out_slot);
268 if (err != ESP_OK) {
269 ESP_LOGE(TAG,
270 "Failed to attach sdspi device onto an SPI bus (rc=0x%x), please initialize the \
271 bus first and check the device parameters."
272 , err);
273 }
274 return err;
275 }
276
esp_vfs_fat_sdspi_mount(const char * base_path,const sdmmc_host_t * host_config_input,const sdspi_device_config_t * slot_config,const esp_vfs_fat_mount_config_t * mount_config,sdmmc_card_t ** out_card)277 esp_err_t esp_vfs_fat_sdspi_mount(const char* base_path,
278 const sdmmc_host_t* host_config_input,
279 const sdspi_device_config_t* slot_config,
280 const esp_vfs_fat_mount_config_t* mount_config,
281 sdmmc_card_t** out_card)
282 {
283 const sdmmc_host_t* host_config = host_config_input;
284 esp_err_t err;
285 int card_handle = -1; //uninitialized
286 bool host_inited = false;
287 BYTE pdrv = FF_DRV_NOT_USED;
288 sdmmc_card_t* card = NULL;
289 char* dup_path = NULL;
290
291 err = mount_prepare_mem(base_path, &pdrv, &dup_path, &card);
292 if (err != ESP_OK) {
293 ESP_LOGE(TAG, "mount_prepare failed");
294 return err;
295 }
296
297 //the init() function is usually empty, doesn't require any deinit to revert it
298 err = (*host_config->init)();
299 CHECK_EXECUTE_RESULT(err, "host init failed");
300
301 err = init_sdspi_host(host_config->slot, slot_config, &card_handle);
302 CHECK_EXECUTE_RESULT(err, "slot init failed");
303 //Set `host_inited` to true to indicate that host_config->deinit() needs
304 //to be called to revert `init_sdspi_host`
305 host_inited = true;
306
307 /*
308 * The `slot` argument inside host_config should be replaced by the SD SPI handled returned
309 * above. But the input pointer is const, so create a new variable.
310 */
311 sdmmc_host_t new_config;
312 if (card_handle != host_config->slot) {
313 new_config = *host_config_input;
314 host_config = &new_config;
315 new_config.slot = card_handle;
316 }
317
318 // probe and initialize card
319 err = sdmmc_card_init(host_config, card);
320 CHECK_EXECUTE_RESULT(err, "sdmmc_card_init failed");
321
322 err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path);
323 CHECK_EXECUTE_RESULT(err, "mount_to_vfs failed");
324
325 if (out_card != NULL) {
326 *out_card = card;
327 }
328 if (s_card == NULL) {
329 //store the ctx locally to be back-compatible
330 s_card = card;
331 s_pdrv = pdrv;
332 s_base_path = dup_path;
333 } else {
334 free(dup_path);
335 }
336 return ESP_OK;
337
338 cleanup:
339 if (host_inited) {
340 call_host_deinit(host_config);
341 }
342 free(card);
343 free(dup_path);
344 return err;
345
346 }
347
local_card_remove(void)348 static void local_card_remove(void)
349 {
350 s_card = NULL;
351 free(s_base_path);
352 s_base_path = NULL;
353 s_pdrv = FF_DRV_NOT_USED;
354 }
355
call_host_deinit(const sdmmc_host_t * host_config)356 static void call_host_deinit(const sdmmc_host_t *host_config)
357 {
358 if (host_config->flags & SDMMC_HOST_FLAG_DEINIT_ARG) {
359 host_config->deinit_p(host_config->slot);
360 } else {
361 host_config->deinit();
362 }
363 }
364
unmount_card_core(const char * base_path,sdmmc_card_t * card)365 static esp_err_t unmount_card_core(const char *base_path, sdmmc_card_t *card)
366 {
367 BYTE pdrv = ff_diskio_get_pdrv_card(card);
368 if (pdrv == 0xff) {
369 return ESP_ERR_INVALID_ARG;
370 }
371
372 // unmount
373 char drv[3] = {(char)('0' + pdrv), ':', 0};
374 f_mount(0, drv, 0);
375 // release SD driver
376 ff_diskio_unregister(pdrv);
377
378 call_host_deinit(&card->host);
379 free(card);
380
381 esp_err_t err = esp_vfs_fat_unregister_path(base_path);
382 return err;
383 }
384
esp_vfs_fat_sdmmc_unmount(void)385 esp_err_t esp_vfs_fat_sdmmc_unmount(void)
386 {
387 sdmmc_card_t* card = s_card;
388 esp_err_t err = unmount_card_core(s_base_path, card);
389 local_card_remove();
390 return err;
391 }
392
esp_vfs_fat_sdcard_unmount(const char * base_path,sdmmc_card_t * card)393 esp_err_t esp_vfs_fat_sdcard_unmount(const char *base_path, sdmmc_card_t *card)
394 {
395 esp_err_t err = unmount_card_core(base_path, card);
396 if (s_card == card) {
397 local_card_remove();
398 }
399 return err;
400 }
401