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 9 #ifndef STORAGE_BLOCK_H 10 #define STORAGE_BLOCK_H 11 12 #include "storage.h" 13 14 #ifdef __cplusplus 15 #if __cplusplus 16 extern "C" { 17 #endif 18 #endif /* __cplusplus */ 19 20 #define STORAGE_SEC_SIZE 512 21 #define STORAGE_SEC_SHIFT 9 22 #define STORAGE_MAX_SEC_NR (STORAGE_MAX_BYTES >> STORAGE_SEC_SHIFT) 23 24 #define STORAGE_SEC_PARAM_INVALID(s, n) \ 25 (s >= STORAGE_MAX_SEC_NR || \ 26 n >= STORAGE_MAX_SEC_NR || \ 27 (STORAGE_MAX_SEC_NR - n) <= s) 28 29 #define BLOCK_NAME_LEN 32 30 31 struct StorageBlock { 32 char name[BLOCK_NAME_LEN]; /* name of the block device */ 33 int32_t index; 34 enum StorageType type; /* indicate which type of media is used */ 35 bool removeable; 36 void *media; /* media device of the block */ 37 size_t capacity; /* sized by sector */ 38 size_t secSize; /* sized by bytes */ 39 uint32_t errCnt; /* err count on io transfer */ 40 struct StorageBlockMethod *ops; /* storage oparations provided by specific media */ 41 void *bops; /* block operations of specific os */ 42 }; 43 44 struct StorageBlockMethod { 45 ssize_t (*read)(struct StorageBlock *sb, uint8_t *buf, size_t secStart, size_t secNr); 46 ssize_t (*write)(struct StorageBlock *sb, const uint8_t *buf, size_t secStart, size_t secNr); 47 ssize_t (*erase)(struct StorageBlock *sb, size_t secStart, size_t secNr); 48 size_t (*getCapacity)(struct StorageBlock *sb); 49 bool (*isPresent)(struct StorageBlock *sb); 50 uint32_t (*getAuSize)(struct StorageBlock *sb); 51 }; 52 53 ssize_t StorageBlockRead(struct StorageBlock *sb, uint8_t *buf, size_t secStart, size_t secNr); 54 ssize_t StorageBlockWrite(struct StorageBlock *sb, const uint8_t *buf, size_t secStart, size_t secNr); 55 ssize_t StorageBlockErase(struct StorageBlock *sb, size_t secStart, size_t secNr); 56 size_t StorageBlockGetCapacity(struct StorageBlock *sb); 57 bool StorageBlockIsPresent(struct StorageBlock *sb); 58 int32_t StorageBlockGetAuSize(struct StorageBlock *sb, uint32_t *auSize); 59 60 int32_t StorageBlockAdd(struct StorageBlock *sb); 61 void StorageBlockDel(struct StorageBlock *sb); 62 63 /* these two functions gona implemented by specific os */ 64 int32_t StorageBlockOsInit(struct StorageBlock *sb); 65 void StorageBlockOsUninit(struct StorageBlock *sb); 66 67 ssize_t StorageBlockMmcErase(uint32_t blockId, size_t secStart, size_t secNr); 68 struct StorageBlock *StorageBlockFromNumber(uint32_t number); 69 70 71 #ifdef __cplusplus 72 #if __cplusplus 73 } 74 #endif 75 #endif /* __cplusplus */ 76 77 #endif /* STORAGE_BLOCK_H */ 78