xref: /kernel/liteos_a/fs/fat/os_adapt/format.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
32#include "errno.h"
33#include "fatfs.h"
34#include "unistd.h"
35#include "stdio.h"
36#include "string.h"
37#include "errcode_fat.h"
38#include "integer.h"
39#ifdef LOSCFG_FS_FAT
40
41char FatLabel[LABEL_LEN];
42#define DEV_NAME_SIZE 4
43
44int format(const char *dev, int sectors, int option)
45{
46    struct Vnode *device = NULL;
47    INT err;
48    if (dev == NULL) {
49        set_errno(EINVAL);
50        return -1;
51    }
52
53    if (strncmp(dev, "/dev", DEV_NAME_SIZE) != 0) {
54        PRINTK("Usage  :\n");
55        PRINTK("        format <dev_vnodename> <sectors> <option> <label>\n");
56        PRINTK("        dev_vnodename : the name of dev\n");
57        PRINTK("        sectors       : Size of allocation unit in unit of byte or sector, ");
58        PRINTK("0 instead of default size\n");
59        PRINTK("        options       : Index of filesystem. 1 for FAT filesystem, 2 for FAT32 filesystem, ");
60        PRINTK("7 for any, 8 for erase\n");
61        PRINTK("        label         : The volume of device. It will be emptyed when this parameter is null\n");
62        PRINTK("Example:\n");
63        PRINTK("        format /dev/mmcblk0 128 2\n");
64
65        set_errno(EINVAL);
66        return -1;
67    }
68    VnodeHold();
69    err = VnodeLookup(dev, &device, 0);
70    if (err == -ENOENT || err == -ENOSYS) {
71        VnodeDrop();
72        set_errno(ENODEV);
73        return -1;
74    } else if (err < 0) {
75        VnodeDrop();
76        set_errno(-err);
77        return -1;
78    }
79    err = fatfs_mkfs(device, sectors, option);
80    if (err < 0) {
81        VnodeDrop();
82        set_errno(-err);
83        return -1;
84    }
85#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
86    else if (err >= VIRERR_BASE) {
87        set_errno(err);
88    }
89#endif
90    VnodeDrop();
91    return 0;
92}
93
94void set_label(const char *name)
95{
96    INT len;
97    INT err;
98
99    (void)memset_s(FatLabel, LABEL_LEN, 0, LABEL_LEN);
100
101    if (name == NULL || *name == '\0') {
102        return;
103    }
104
105    len = strlen(name);
106    if (len >= LABEL_LEN) {
107        len = LABEL_LEN - 1;
108    }
109
110    err = strncpy_s(FatLabel, LABEL_LEN, name, len);
111    if (err != EOK) {
112        PRINT_ERR("Fat set_label error");
113    }
114}
115#endif    /* #ifdef CONFIG_FS_FAT */
116