1 /*
2  * Copyright (c) 2020-2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "securec.h"
10 
11 #include "hdf_base.h"
12 #include "hdf_log.h"
13 #include "mmc_block.h"
14 
15 #define HDF_LOG_TAG mmc_block_c
16 
MmcBlockInit(struct MmcDevice * mmcDevice)17 int32_t MmcBlockInit(struct MmcDevice *mmcDevice)
18 {
19     int32_t ret;
20     size_t nameSize;
21     struct MmcBlock *mb = NULL;
22 
23     if (mmcDevice == NULL) {
24         HDF_LOGE("MmcBlockInit: mmcDevice is null!");
25         return HDF_ERR_INVALID_OBJECT;
26     }
27 
28     if (MmcDeviceGet(mmcDevice) == NULL) {
29         HDF_LOGE("MmcBlockInit: mmcDevice get fail!");
30         return HDF_PLT_ERR_DEV_GET;
31     }
32 
33     mb = (struct MmcBlock *)OsalMemCalloc(sizeof(*mb));
34     if (mb == NULL) {
35         HDF_LOGE("MmcBlockInit: memcalloc fail!");
36         PlatformDevicePut(&mmcDevice->device);
37         return HDF_ERR_MALLOC_FAIL;
38     }
39 
40     mmcDevice->mb = mb;
41     mb->mmc = mmcDevice;
42     mb->secSize = mmcDevice->secSize;
43     mb->capacity = mmcDevice->capacity;
44     mb->removable = (mmcDevice->state.bits.removable == 0) ? false : true;
45     nameSize = sizeof(mb->name);
46     ret = snprintf_s(mb->name, nameSize, nameSize - 1, "/dev/mmcblk%0d", mmcDevice->cntlr->index);
47     if (ret <= 0) {
48         OsalMemFree(mb);
49         PlatformDevicePut(&mmcDevice->device);
50         HDF_LOGE("MmcBlockInit: format block dev name fail, ret = %d!", ret);
51         return HDF_PLT_ERR_OS_API;
52     }
53 
54     ret = MmcBlockOsInit(mmcDevice);
55     if (ret != HDF_SUCCESS) {
56         HDF_LOGE("MmcBlockInit: mmc block os init fail, ret = %d!", ret);
57         OsalMemFree(mb);
58         PlatformDevicePut(&mmcDevice->device);
59         return ret;
60     }
61     return HDF_SUCCESS;
62 }
63 
MmcBlockUninit(struct MmcDevice * mmcDevice)64 void MmcBlockUninit(struct MmcDevice *mmcDevice)
65 {
66     if (mmcDevice != NULL) {
67         MmcBlockOsUninit(mmcDevice);
68         OsalMemFree(mmcDevice->mb);
69         mmcDevice->mb = NULL;
70         PlatformDevicePut(&mmcDevice->device);
71     }
72 }
73