• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 #include "hdf_base.h"
9 #include "hdf_log.h"
10 #include "platform_core.h"
11 #include "storage_block.h"
12 
13 #define HDF_LOG_TAG storage_block_c
14 
15 #define MAX_BLOCK_COUNT 3
16 
17 static struct StorageBlock *g_blocks[MAX_BLOCK_COUNT];
18 
StorageBlockErase(struct StorageBlock * sb,size_t secStart,size_t secNr)19 ssize_t StorageBlockErase(struct StorageBlock *sb, size_t secStart, size_t secNr)
20 {
21     if (sb == NULL) {
22         return HDF_ERR_INVALID_OBJECT;
23     }
24     if (sb->ops == NULL || sb->ops->erase == NULL) {
25         return HDF_ERR_NOT_SUPPORT;
26     }
27     return sb->ops->erase(sb, secStart, secNr);
28 }
29 
StorageBlockWrite(struct StorageBlock * sb,const uint8_t * buf,size_t secStart,size_t secNr)30 ssize_t StorageBlockWrite(struct StorageBlock *sb, const uint8_t *buf, size_t secStart, size_t secNr)
31 {
32     if (sb == NULL) {
33         return HDF_ERR_INVALID_OBJECT;
34     }
35     if (sb->ops == NULL || sb->ops->write == NULL) {
36         return HDF_ERR_NOT_SUPPORT;
37     }
38     return sb->ops->write(sb, buf, secStart, secNr);
39 }
40 
StorageBlockRead(struct StorageBlock * sb,uint8_t * buf,size_t secStart,size_t secNr)41 ssize_t StorageBlockRead(struct StorageBlock *sb, uint8_t *buf, size_t secStart, size_t secNr)
42 {
43     if (sb == NULL) {
44         return HDF_ERR_INVALID_OBJECT;
45     }
46     if (sb->ops == NULL || sb->ops->read == NULL) {
47         return HDF_ERR_NOT_SUPPORT;
48     }
49     return sb->ops->read(sb, buf, secStart, secNr);
50 }
51 
StorageBlockGetAuSize(struct StorageBlock * sb,uint32_t * auSize)52 int32_t StorageBlockGetAuSize(struct StorageBlock *sb, uint32_t *auSize)
53 {
54     if (sb == NULL) {
55         return HDF_ERR_INVALID_OBJECT;
56     }
57     if (sb->ops == NULL || sb->ops->getAuSize == NULL) {
58         return HDF_ERR_NOT_SUPPORT;
59     }
60     *auSize = sb->ops->getAuSize(sb);
61     return HDF_SUCCESS;
62 }
63 
StorageBlockGetCapacity(struct StorageBlock * sb)64 size_t StorageBlockGetCapacity(struct StorageBlock *sb)
65 {
66     return (sb == NULL) ? 0 : sb->capacity;
67 }
68 
StorageBlockIsPresent(struct StorageBlock * sb)69 bool StorageBlockIsPresent(struct StorageBlock *sb)
70 {
71     return (sb != NULL && sb->ops != NULL &&
72         sb->ops->isPresent != NULL && sb->ops->isPresent(sb));
73 }
74 
StorageBlockAdd(struct StorageBlock * sb)75 int32_t StorageBlockAdd(struct StorageBlock *sb)
76 {
77     int i;
78     int32_t ret;
79 
80     if (sb == NULL) {
81         return HDF_ERR_INVALID_OBJECT;
82     }
83 
84     for (i = 0; i < MAX_BLOCK_COUNT; i++) {
85         if (g_blocks[i] == NULL) {
86             g_blocks[i] = sb;
87             sb->index = i;
88             break;
89         }
90     }
91     if (i >= MAX_BLOCK_COUNT) {
92         return HDF_PLT_ERR_DEV_FULL;
93     }
94 
95     if (sb->secSize == 0) {
96         sb->secSize = STORAGE_SEC_SIZE;
97     }
98 
99     ret = StorageBlockOsInit(sb);
100     if (ret != HDF_SUCCESS) {
101         g_blocks[i] = NULL;
102         return ret;
103     }
104 
105     return HDF_SUCCESS;
106 }
107 
StorageBlockDel(struct StorageBlock * sb)108 void StorageBlockDel(struct StorageBlock *sb)
109 {
110     int i;
111 
112     if (sb == NULL) {
113         return;
114     }
115 
116     for (i = 0; i < MAX_BLOCK_COUNT; i++) {
117         if (g_blocks[i] == sb) {
118             g_blocks[i] = NULL;
119         }
120     }
121 
122     StorageBlockOsUninit(sb);
123 }
124 
StorageBlockFromNumber(uint32_t number)125 struct StorageBlock *StorageBlockFromNumber(uint32_t number)
126 {
127     if (number >= MAX_BLOCK_COUNT) {
128         HDF_LOGE("StorageBlockFromNumber: invalid number:%d", number);
129         return NULL;
130     }
131     return g_blocks[number];
132 }
133