1/* 2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include "vfs_partition.h" 31#include "vfs_operations.h" 32#include "los_fs.h" 33#include "los_list.h" 34#include "vfs_maps.h" 35#include "vfs_mount.h" 36#include "securec.h" 37#include "stdlib.h" 38#include "string.h" 39 40static struct DeviceDesc *g_deviceList = NULL; 41 42int GetPartIdByPartName(const char *partName) 43{ 44 if (partName == NULL) { 45 return (int)LOS_NOK; 46 } 47 48 /* the character next to p is the partId */ 49 char *p = strrchr(partName, 'p'); 50 if (p != NULL) { 51 return atoi(p + 1); 52 } 53 54 return (int)LOS_NOK; 55} 56 57int GetDevIdByDevName(const char *dev) 58{ 59 if (dev == NULL) { 60 return (int)LOS_NOK; 61 } 62 63 /* last character is the deviceId */ 64 char *p = (char *)dev + strlen(dev) - 1; 65 if (p != NULL) { 66 return atoi(p); 67 } 68 69 return (int)LOS_NOK; 70} 71 72struct DeviceDesc *getDeviceList(VOID) 73{ 74 return g_deviceList; 75} 76 77static int AddDevice(const char *dev, const char *fsType, int *lengthArray, int *addrArray, 78 int partNum) 79{ 80 size_t len; 81 struct DeviceDesc *prev = NULL; 82 for (prev = g_deviceList; prev != NULL; prev = prev->dNext) { 83 if (strcmp(prev->dDev, dev) == 0) { 84 errno = -EEXIST; 85 return (int)LOS_NOK; 86 } 87 } 88 89 if (addrArray == NULL) { 90 errno = -EFAULT; 91 return (int)LOS_NOK; 92 } 93 94 prev = (struct DeviceDesc *)LOSCFG_FS_MALLOC_HOOK(sizeof(struct DeviceDesc)); 95 if (prev == NULL) { 96 errno = -ENOMEM; 97 return (int)LOS_NOK; 98 } 99 len = strlen(dev) + 1; 100 prev->dDev = LOSCFG_FS_MALLOC_HOOK(len); 101 len = strlen(fsType) + 1; 102 prev->dFsType = LOSCFG_FS_MALLOC_HOOK(len); 103 prev->dAddrArray = (int *)LOSCFG_FS_MALLOC_HOOK(partNum * sizeof(int)); 104 if (prev->dDev == NULL || prev->dFsType == NULL || prev->dAddrArray == NULL) { 105 goto errout; 106 } 107 (void)strcpy_s((char *)prev->dDev, len, dev); 108 (void)strcpy_s((char *)prev->dFsType, len, fsType); 109 (void)memcpy_s(prev->dAddrArray, partNum * sizeof(int), addrArray, partNum * sizeof(int)); 110 111 if (lengthArray != NULL) { 112 prev->dLengthArray = (int *)LOSCFG_FS_MALLOC_HOOK(partNum * sizeof(int)); 113 if (prev->dLengthArray == NULL) { 114 goto errout; 115 } 116 (void)memcpy_s(prev->dLengthArray, partNum * sizeof(int), lengthArray, partNum * sizeof(int)); 117 } 118 119 prev->dNext = g_deviceList; 120 prev->dPartNum = partNum; 121 g_deviceList = prev; 122 return LOS_OK; 123errout: 124 if (prev->dDev != NULL) { 125 LOSCFG_FS_FREE_HOOK((void *)prev->dDev); 126 } 127 if (prev->dFsType != NULL) { 128 LOSCFG_FS_FREE_HOOK((void *)prev->dFsType); 129 } 130 if (prev->dAddrArray != NULL) { 131 LOSCFG_FS_FREE_HOOK((void *)prev->dAddrArray); 132 } 133 if (prev->dLengthArray != NULL) { 134 LOSCFG_FS_FREE_HOOK((void *)prev->dLengthArray); 135 } 136 137 LOSCFG_FS_FREE_HOOK(prev); 138 errno = -ENOMEM; 139 return (int)LOS_NOK; 140} 141 142 143int LOS_DiskPartition(const char *dev, const char *fsType, int *lengthArray, int *addrArray, 144 int partNum) 145{ 146 int ret = (int)LOS_NOK; 147 struct FsMap *fMap = VfsFsMapGet(fsType); 148 if ((fMap != NULL) && (fMap->fsMgt != NULL) && 149 (fMap->fsMgt->fdisk != NULL)) { 150 ret = fMap->fsMgt->fdisk(dev, lengthArray, partNum); 151 return ret; 152 } 153 154 ret = AddDevice(dev, fsType, lengthArray, addrArray, partNum); 155 return ret; 156} 157 158int LOS_PartitionFormat(const char *partName, char *fsType, void *data) 159{ 160 int ret = (int)LOS_NOK; 161 162 /* check if the device is mounted by iterate the mp list 163 format is not allowed when the device has been mounted. */ 164 struct MountPoint *iter = NULL; 165 LOS_MP_FOR_EACH_ENTRY(iter) { 166 if ((iter->mPath != NULL) && (strcmp(iter->mPath, partName) == 0)) { 167 errno = EBUSY; 168 return (int)LOS_NOK; 169 } 170 } 171 172 struct FsMap *fMap = VfsFsMapGet(fsType); 173 if ((fMap != NULL) && (fMap->fsMgt != NULL) && 174 (fMap->fsMgt->format != NULL)) { 175 ret = fMap->fsMgt->format(partName, data); 176 } 177 178 return ret; 179} 180