xref: /kernel/liteos_a/fs/patchfs/los_patchfs.c (revision 0d163575)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31#include "los_patchfs.h"
32#include "los_partition_utils.h"
33
34#include "sys/mount.h"
35#include "vnode.h"
36
37#ifdef LOSCFG_PLATFORM_PATCHFS
38
39INT32 OsMountPatchFs(VOID)
40{
41    INT32 ret;
42    struct PartitionInfo partInfo = {
43        PATCHPART_NAME, PATCH_CMDLINE_ARGNAME,
44        PATCH_STORAGE_ARGNAME, NULL,
45        PATCH_FSTYPE_ARGNAME, NULL,
46        PATCH_ADDR_ARGNAME, -1,
47        PATCH_SIZE_ARGNAME, -1,
48        NULL, PATCH_PARTITIONNUM
49    };
50
51#ifdef LOSCFG_SECURITY_BOOT
52    partInfo.storageType = strdup(STORAGE_TYPE);
53    if (partInfo.storageType == NULL) {
54        return LOS_NOK;
55    }
56    partInfo.fsType      = strdup(FS_TYPE);
57    if (partInfo.fsType == NULL) {
58        ret = LOS_NOK;
59        goto EXIT;
60    }
61    partInfo.startAddr   = PATCHFS_FLASH_ADDR;
62    partInfo.partSize    = PATCHFS_FLASH_SIZE;
63#else
64    ret = GetPartitionInfo(&partInfo);
65    if (ret != LOS_OK) {
66        goto EXIT;
67    }
68    partInfo.startAddr = (partInfo.startAddr >= 0) ? partInfo.startAddr : PATCHFS_FLASH_ADDR;
69    partInfo.partSize = (partInfo.partSize >= 0) ? partInfo.partSize : PATCHFS_FLASH_SIZE;
70#endif
71
72    ret = LOS_NOK;
73    partInfo.devName = strdup(PATCH_FLASH_DEV_NAME);
74    if (partInfo.devName == NULL) {
75        goto EXIT;
76    }
77    const CHAR *devName = GetDevNameOfPartition(&partInfo);
78    if (devName != NULL) {
79        ret = mkdir(PATCHFS_MOUNT_POINT, 0);
80        if (ret == LOS_OK) {
81            ret = mount(devName, PATCHFS_MOUNT_POINT, partInfo.fsType, MS_RDONLY, NULL);
82            if (ret != LOS_OK) {
83                int err = get_errno();
84                PRINT_ERR("Failed to mount %s, errno %d: %s\n", partInfo.partName, err, strerror(err));
85            }
86        } else {
87            int err = get_errno();
88            PRINT_ERR("Failed to mkdir %s, errno %d: %s\n", PATCHFS_MOUNT_POINT, err, strerror(err));
89        }
90    }
91
92    if ((ret != LOS_OK) && (devName != NULL)) {
93        ResetDevNameofPartition(&partInfo);
94    }
95
96EXIT:
97    free(partInfo.devName);
98    partInfo.devName = NULL;
99    free(partInfo.storageType);
100    partInfo.storageType = NULL;
101    free(partInfo.fsType);
102    partInfo.fsType = NULL;
103    return ret;
104}
105
106#endif // LOSCFG_PLATFORM_PATCHFS
107