10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci * conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci * provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci * permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "osTest.h"
330d163575Sopenharmony_ci#include <sys/syscall.h>
340d163575Sopenharmony_ci#include <unistd.h>
350d163575Sopenharmony_ci
360d163575Sopenharmony_ciUINT32 g_shellTestQueueID;
370d163575Sopenharmony_ciINT32 g_iCunitErrCode = 0;
380d163575Sopenharmony_ciINT32 g_iCunitErrLineNo = 0;
390d163575Sopenharmony_ci
400d163575Sopenharmony_ciUINT32 g_testTskHandle;
410d163575Sopenharmony_ciUINT32 g_testCount;
420d163575Sopenharmony_ciUINT32 g_flowcheck = 0;
430d163575Sopenharmony_ciUINT32 g_failResult = 0;
440d163575Sopenharmony_ciUINT32 g_passResult = 0;
450d163575Sopenharmony_ci
460d163575Sopenharmony_ci#ifdef TEST1980
470d163575Sopenharmony_ciUINT32 g_testhwiFlag;
480d163575Sopenharmony_ciUINT32 g_testCpuMask;
490d163575Sopenharmony_ci#endif
500d163575Sopenharmony_ci
510d163575Sopenharmony_ciUINT32 g_testCount1;
520d163575Sopenharmony_ciUINT32 g_testCount2;
530d163575Sopenharmony_ciUINT32 g_testCount3;
540d163575Sopenharmony_ciUINT32 g_testTaskID01;
550d163575Sopenharmony_ciUINT32 g_testTaskID02;
560d163575Sopenharmony_ciUINT32 g_testTaskID03;
570d163575Sopenharmony_ciUINT32 g_testTaskID04;
580d163575Sopenharmony_ciUINT32 g_hwiNum1;
590d163575Sopenharmony_ciUINT32 g_hwiNum2;
600d163575Sopenharmony_ciUINT32 g_semID;
610d163575Sopenharmony_ciUINT32 g_semID2;
620d163575Sopenharmony_ciUINT32 g_mutexTest;
630d163575Sopenharmony_ciUINT32 g_cpupTestCount;
640d163575Sopenharmony_ciUINT32 g_waitTestCount;
650d163575Sopenharmony_ciUINT32 g_testPeriod;
660d163575Sopenharmony_ci
670d163575Sopenharmony_ciUINT16 g_swTmrID;
680d163575Sopenharmony_ciUINT32 g_testQueueID01;
690d163575Sopenharmony_ciUINT32 g_testQueueID02;
700d163575Sopenharmony_ciUINT32 g_testQueueID03;
710d163575Sopenharmony_ciUINT32 g_leavingTaskNum;
720d163575Sopenharmony_ciUINT32 g_mAuwTestTaskID[32] = {0};
730d163575Sopenharmony_ciUINT32 g_getTickConsume = 0;
740d163575Sopenharmony_ciCHAR g_libcPathname[50] = "/usr/jffs0";
750d163575Sopenharmony_ciUINT32 g_testCircleCount = 0;
760d163575Sopenharmony_ci
770d163575Sopenharmony_ciUINT32 g_fatFilesystem;
780d163575Sopenharmony_ciUINT8 g_mUsIndex;
790d163575Sopenharmony_ci
800d163575Sopenharmony_ci#if TEST_MODULE_CHECK == 1
810d163575Sopenharmony_ci
820d163575Sopenharmony_ciextern UINT32 g_FailModelResult[];
830d163575Sopenharmony_ciextern UINT32 g_PassModelResult[];
840d163575Sopenharmony_ciextern UINT32 g_ExecutModelNum[];
850d163575Sopenharmony_ci#endif
860d163575Sopenharmony_ciextern char *StrLayer[];
870d163575Sopenharmony_ciextern char *StrLevel[];
880d163575Sopenharmony_ciextern char *StrType[];
890d163575Sopenharmony_ci
900d163575Sopenharmony_ciextern char *StrModule[];
910d163575Sopenharmony_ciextern UINT32 g_ModelNum;
920d163575Sopenharmony_ci
930d163575Sopenharmony_ci#ifdef LOSCFG_USER_TEST_FS_FAT
940d163575Sopenharmony_ci#define TEST_FAT32 0x02
950d163575Sopenharmony_ci#define TEST_EXFAT 0x04
960d163575Sopenharmony_ci#endif
970d163575Sopenharmony_ci
980d163575Sopenharmony_ciBOOL g_isSpinorInit = FALSE;
990d163575Sopenharmony_ciBOOL g_isSdInit = FALSE;
1000d163575Sopenharmony_ciBOOL g_isUartDevInit = FALSE;
1010d163575Sopenharmony_ciBOOL g_isTcpipInit = FALSE;
1020d163575Sopenharmony_ciBOOL g_isInitSerial = FALSE;
1030d163575Sopenharmony_ciUINT32 g_vfsCyclesCount = 0;
1040d163575Sopenharmony_ciINT32 g_serialInitFlag = -1;
1050d163575Sopenharmony_ciBOOL g_isAddArray = TRUE;
1060d163575Sopenharmony_ciBOOL g_isUsbInit = FALSE;
1070d163575Sopenharmony_ciBOOL g_isIpcGmacInit = FALSE;
1080d163575Sopenharmony_ci
1090d163575Sopenharmony_ciBOOL g_isDriversRandomInit = FALSE;
1100d163575Sopenharmony_ci
1110d163575Sopenharmony_ciBOOL g_isHisiEthSetPhyModeInit = FALSE;
1120d163575Sopenharmony_ci
1130d163575Sopenharmony_ciBOOL g_isVfsInit = FALSE;
1140d163575Sopenharmony_ciBOOL g_isProcInit = FALSE;
1150d163575Sopenharmony_ci
1160d163575Sopenharmony_ciINT32 g_libFilesystem = -1;
1170d163575Sopenharmony_cienum {
1180d163575Sopenharmony_ci    LIB_USE_FAT = 1,
1190d163575Sopenharmony_ci    LIB_USE_JFFS2,
1200d163575Sopenharmony_ci};
1210d163575Sopenharmony_ci#ifdef LOSCFG_DRIVERS_USB
1220d163575Sopenharmony_ciVOID test_init_usb(controller_type ctype, device_type dtype);
1230d163575Sopenharmony_ci#endif
1240d163575Sopenharmony_ciVOID test_init_ipc_gmac(VOID);
1250d163575Sopenharmony_ciVOID test_init_proc(VOID);
1260d163575Sopenharmony_ciVOID test_init_sd(VOID);
1270d163575Sopenharmony_ciVOID TestInitVfs(VOID);
1280d163575Sopenharmony_ciVOID test_init_spinor(VOID);
1290d163575Sopenharmony_ciVOID test_deinit_jffs(VOID);
1300d163575Sopenharmony_ciVOID test_mtd_jffs(VOID);
1310d163575Sopenharmony_ci
1320d163575Sopenharmony_ciUINT32 PrepareFileEnv(CHAR *pathList[], CHAR *streamList[], INT32 streamLen[], INT32 listCnt)
1330d163575Sopenharmony_ci{
1340d163575Sopenharmony_ci    CHAR nameBuf[256] = {0};
1350d163575Sopenharmony_ci    for (UINT32 i = 0; i < listCnt; i++) {
1360d163575Sopenharmony_ci        UINT32 ret = access(pathList[i], 0);
1370d163575Sopenharmony_ci        if (ret == 0) {
1380d163575Sopenharmony_ci            ret = memcpy_s(nameBuf, sizeof(nameBuf), pathList[i], strlen(pathList[i]) + 1);
1390d163575Sopenharmony_ci            if (ret != 0) {
1400d163575Sopenharmony_ci                return -1;
1410d163575Sopenharmony_ci            }
1420d163575Sopenharmony_ci            ret = strcat_s(nameBuf, sizeof(nameBuf), "_bak_for_test");
1430d163575Sopenharmony_ci            if (ret != 0) {
1440d163575Sopenharmony_ci                return -1;
1450d163575Sopenharmony_ci            }
1460d163575Sopenharmony_ci            ret = rename(pathList[i], nameBuf);
1470d163575Sopenharmony_ci            if (ret != 0) {
1480d163575Sopenharmony_ci                return -1;
1490d163575Sopenharmony_ci            }
1500d163575Sopenharmony_ci        }
1510d163575Sopenharmony_ci        FILE *fp = fopen(pathList[i], "w");
1520d163575Sopenharmony_ci        if (fp == NULL) {
1530d163575Sopenharmony_ci            return -1;
1540d163575Sopenharmony_ci        }
1550d163575Sopenharmony_ci        ret = fwrite(streamList[i], 1, streamLen[i], fp);
1560d163575Sopenharmony_ci        if (ret != streamLen[i]) {
1570d163575Sopenharmony_ci            (VOID)fclose(fp);
1580d163575Sopenharmony_ci            return -1;
1590d163575Sopenharmony_ci        }
1600d163575Sopenharmony_ci        ret = fclose(fp);
1610d163575Sopenharmony_ci        if (ret != 0) {
1620d163575Sopenharmony_ci            printf("%d\n", errno);
1630d163575Sopenharmony_ci        }
1640d163575Sopenharmony_ci    }
1650d163575Sopenharmony_ci    return 0;
1660d163575Sopenharmony_ci}
1670d163575Sopenharmony_ci
1680d163575Sopenharmony_ciUINT32 RecoveryFileEnv(CHAR *pathList[], INT32 listCnt)
1690d163575Sopenharmony_ci{
1700d163575Sopenharmony_ci    UINT32 ret;
1710d163575Sopenharmony_ci    CHAR nameBuf[256] = {0};
1720d163575Sopenharmony_ci    for (UINT32 i = 0; i < listCnt; i++) {
1730d163575Sopenharmony_ci        ret = remove(pathList[i]);
1740d163575Sopenharmony_ci        if (ret != 0) {
1750d163575Sopenharmony_ci            printf("ret = %d, err = %d\n", ret, errno);
1760d163575Sopenharmony_ci        }
1770d163575Sopenharmony_ci        ret = memcpy_s(nameBuf, sizeof(nameBuf), pathList[i], strlen(pathList[i]) + 1);
1780d163575Sopenharmony_ci        if (ret != 0) {
1790d163575Sopenharmony_ci            return -1;
1800d163575Sopenharmony_ci        }
1810d163575Sopenharmony_ci        ret = strcat_s(nameBuf, sizeof(nameBuf), "_bak_for_test");
1820d163575Sopenharmony_ci        if (ret != 0) {
1830d163575Sopenharmony_ci            return -1;
1840d163575Sopenharmony_ci        }
1850d163575Sopenharmony_ci        ret = access(nameBuf, 0);
1860d163575Sopenharmony_ci        if (ret == 0) {
1870d163575Sopenharmony_ci            ret = rename(nameBuf, pathList[i]);
1880d163575Sopenharmony_ci            if (ret != 0) {
1890d163575Sopenharmony_ci                return -1;
1900d163575Sopenharmony_ci            }
1910d163575Sopenharmony_ci        }
1920d163575Sopenharmony_ci    }
1930d163575Sopenharmony_ci    return 0;
1940d163575Sopenharmony_ci}
1950d163575Sopenharmony_ci
1960d163575Sopenharmony_ciVOID Wfi(VOID)
1970d163575Sopenharmony_ci{
1980d163575Sopenharmony_ci    __asm__ __volatile__("wfi" : : : "memory");
1990d163575Sopenharmony_ci}
2000d163575Sopenharmony_ci
2010d163575Sopenharmony_ciVOID Dmb(VOID)
2020d163575Sopenharmony_ci{
2030d163575Sopenharmony_ci    __asm__ __volatile__("dmb" : : : "memory");
2040d163575Sopenharmony_ci}
2050d163575Sopenharmony_ci
2060d163575Sopenharmony_ciVOID Dsb(VOID)
2070d163575Sopenharmony_ci{
2080d163575Sopenharmony_ci    __asm__ __volatile__("dsb" : : : "memory");
2090d163575Sopenharmony_ci}
2100d163575Sopenharmony_ci
2110d163575Sopenharmony_ci__attribute__((weak)) int Gettid()
2120d163575Sopenharmony_ci{
2130d163575Sopenharmony_ci    return syscall(SYS_gettid);
2140d163575Sopenharmony_ci}
2150d163575Sopenharmony_ci
2160d163575Sopenharmony_ciUINT32 LosCurTaskIDGet()
2170d163575Sopenharmony_ci{
2180d163575Sopenharmony_ci    return Gettid();
2190d163575Sopenharmony_ci}
2200d163575Sopenharmony_ci
2210d163575Sopenharmony_ci
2220d163575Sopenharmony_ciUINT32 LosTaskDelay(UINT32 tick)
2230d163575Sopenharmony_ci{
2240d163575Sopenharmony_ci    return usleep(10 * tick * 1000);
2250d163575Sopenharmony_ci}
2260d163575Sopenharmony_ci
2270d163575Sopenharmony_ciVOID TestExtraTaskDelay(UINT32 uwTick)
2280d163575Sopenharmony_ci{
2290d163575Sopenharmony_ci#ifdef LOSCFG_KERNEL_SMP
2300d163575Sopenharmony_ci    // trigger task schedule may occor on another core
2310d163575Sopenharmony_ci    // needs adding delay and checking status later
2320d163575Sopenharmony_ci    LosTaskDelay(uwTick);
2330d163575Sopenharmony_ci#else
2340d163575Sopenharmony_ci    // do nothing
2350d163575Sopenharmony_ci#endif
2360d163575Sopenharmony_ci}
2370d163575Sopenharmony_ci
2380d163575Sopenharmony_ciextern volatile UINT64 g_tickCount[];
2390d163575Sopenharmony_ciUINT64 TestTickCountGet(VOID)
2400d163575Sopenharmony_ci{
2410d163575Sopenharmony_ci    /* not use LOS_TickCountGet for now,
2420d163575Sopenharmony_ci       cause every timer is not match with others.
2430d163575Sopenharmony_ci       use cpu0 timer instead. */
2440d163575Sopenharmony_ci    return clock();
2450d163575Sopenharmony_ci}
2460d163575Sopenharmony_ci
2470d163575Sopenharmony_ciUINT64 TestTickCountByCurrCpuid(VOID)
2480d163575Sopenharmony_ci{
2490d163575Sopenharmony_ci    return clock();
2500d163575Sopenharmony_ci}
2510d163575Sopenharmony_ci
2520d163575Sopenharmony_ci/*
2530d163575Sopenharmony_ci * different from calling LOS_TaskDelay,
2540d163575Sopenharmony_ci * this func will not yield this task to another one.
2550d163575Sopenharmony_ci */
2560d163575Sopenharmony_ciVOID TestBusyTaskDelay(UINT32 tick)
2570d163575Sopenharmony_ci{
2580d163575Sopenharmony_ci    UINT64 runtime = 0;
2590d163575Sopenharmony_ci
2600d163575Sopenharmony_ci    runtime = TestTickCountByCurrCpuid() + tick;
2610d163575Sopenharmony_ci    while (1) {
2620d163575Sopenharmony_ci        if (runtime <= TestTickCountByCurrCpuid()) {
2630d163575Sopenharmony_ci            break;
2640d163575Sopenharmony_ci        }
2650d163575Sopenharmony_ci        Wfi();
2660d163575Sopenharmony_ci    }
2670d163575Sopenharmony_ci}
2680d163575Sopenharmony_ci
2690d163575Sopenharmony_ciVOID TestAssertBusyTaskDelay(UINT32 timeout, UINT32 flag)
2700d163575Sopenharmony_ci{
2710d163575Sopenharmony_ci    UINT64 runtime = 0;
2720d163575Sopenharmony_ci
2730d163575Sopenharmony_ci    runtime = TestTickCountGet() + timeout;
2740d163575Sopenharmony_ci    while (1) {
2750d163575Sopenharmony_ci        if ((runtime <= TestTickCountGet()) || (g_testCount == flag)) {
2760d163575Sopenharmony_ci            break;
2770d163575Sopenharmony_ci        }
2780d163575Sopenharmony_ci        Wfi();
2790d163575Sopenharmony_ci    }
2800d163575Sopenharmony_ci}
2810d163575Sopenharmony_ci
2820d163575Sopenharmony_ciVOID TestAssertWaitDelay(UINT32 *testCount, UINT32 flag)
2830d163575Sopenharmony_ci{
2840d163575Sopenharmony_ci    while (*testCount != flag) {
2850d163575Sopenharmony_ci    usleep(1);
2860d163575Sopenharmony_ci    }
2870d163575Sopenharmony_ci}
2880d163575Sopenharmony_ci
2890d163575Sopenharmony_ciUINT32 PosixPthreadInit(pthread_attr_t *attr, int pri)
2900d163575Sopenharmony_ci{
2910d163575Sopenharmony_ci    UINT32 uwRet = 0;
2920d163575Sopenharmony_ci    struct sched_param sp;
2930d163575Sopenharmony_ci
2940d163575Sopenharmony_ci    uwRet = pthread_attr_init(attr);
2950d163575Sopenharmony_ci    ICUNIT_GOTO_EQUAL(uwRet, 0, uwRet, NOK);
2960d163575Sopenharmony_ci
2970d163575Sopenharmony_ci    uwRet = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
2980d163575Sopenharmony_ci    ICUNIT_GOTO_EQUAL(uwRet, 0, uwRet, NOK);
2990d163575Sopenharmony_ci
3000d163575Sopenharmony_ci    sp.sched_priority = pri;
3010d163575Sopenharmony_ci    uwRet = pthread_attr_setschedparam(attr, &sp);
3020d163575Sopenharmony_ci    ICUNIT_GOTO_EQUAL(uwRet, 0, uwRet, NOK);
3030d163575Sopenharmony_ci
3040d163575Sopenharmony_ci    return LOS_OK;
3050d163575Sopenharmony_ciNOK:
3060d163575Sopenharmony_ci    return LOS_NOK;
3070d163575Sopenharmony_ci}
3080d163575Sopenharmony_ci
3090d163575Sopenharmony_ciUINT32 PosixPthreadDestroy(pthread_attr_t *attr, pthread_t thread)
3100d163575Sopenharmony_ci{
3110d163575Sopenharmony_ci    UINT32 uwRet = 0;
3120d163575Sopenharmony_ci
3130d163575Sopenharmony_ci    uwRet = pthread_join(thread, NULL);
3140d163575Sopenharmony_ci    ICUNIT_GOTO_EQUAL(uwRet, 0, uwRet, NOK);
3150d163575Sopenharmony_ci
3160d163575Sopenharmony_ci    uwRet = pthread_attr_destroy(attr);
3170d163575Sopenharmony_ci    ICUNIT_GOTO_EQUAL(uwRet, 0, uwRet, NOK);
3180d163575Sopenharmony_ci
3190d163575Sopenharmony_ci    return LOS_OK;
3200d163575Sopenharmony_ciNOK:
3210d163575Sopenharmony_ci    return LOS_NOK;
3220d163575Sopenharmony_ci}
3230d163575Sopenharmony_ci
3240d163575Sopenharmony_ciVOID TestInitVfs(VOID)
3250d163575Sopenharmony_ci{
3260d163575Sopenharmony_ci#if defined(LOSCFG_FS_VFS)
3270d163575Sopenharmony_ci    if (g_isVfsInit) {
3280d163575Sopenharmony_ci        return;
3290d163575Sopenharmony_ci    }
3300d163575Sopenharmony_ci
3310d163575Sopenharmony_ci    extern VOID los_vfs_init(VOID);
3320d163575Sopenharmony_ci    los_vfs_init();
3330d163575Sopenharmony_ci    g_isVfsInit = TRUE;
3340d163575Sopenharmony_ci
3350d163575Sopenharmony_ci#endif
3360d163575Sopenharmony_ci}
3370d163575Sopenharmony_ci
3380d163575Sopenharmony_ciVOID TestInitDriversRandom(VOID)
3390d163575Sopenharmony_ci{
3400d163575Sopenharmony_ci    if (g_isDriversRandomInit) {
3410d163575Sopenharmony_ci        return;
3420d163575Sopenharmony_ci    }
3430d163575Sopenharmony_ci
3440d163575Sopenharmony_ci#if defined(LOSCFG_DRIVERS_RANDOM)
3450d163575Sopenharmony_ci
3460d163575Sopenharmony_ci    printf("random init ...\n");
3470d163575Sopenharmony_ci    extern int ran_dev_register(VOID);
3480d163575Sopenharmony_ci    ran_dev_register();
3490d163575Sopenharmony_ci
3500d163575Sopenharmony_ci#endif
3510d163575Sopenharmony_ci
3520d163575Sopenharmony_ci#if defined(LOSCFG_HW_RANDOM_ENABLE)
3530d163575Sopenharmony_ci
3540d163575Sopenharmony_ci    extern int random_hw_dev_register(VOID);
3550d163575Sopenharmony_ci    printf("random_hw init ...\n");
3560d163575Sopenharmony_ci    if (random_hw_dev_register() != 0) {
3570d163575Sopenharmony_ci        printf("Failed!\n");
3580d163575Sopenharmony_ci    }
3590d163575Sopenharmony_ci
3600d163575Sopenharmony_ci#endif
3610d163575Sopenharmony_ci
3620d163575Sopenharmony_ci    g_isDriversRandomInit = TRUE;
3630d163575Sopenharmony_ci}
3640d163575Sopenharmony_ci
3650d163575Sopenharmony_ciVOID TestInitUartDev(VOID) {}
3660d163575Sopenharmony_ci
3670d163575Sopenharmony_ci/* ****************************************
3680d163575Sopenharmony_ciFunction:Test_PartInit
3690d163575Sopenharmony_ciDescription: create a partition for testing,partition num is 0,mount point is jffs0
3700d163575Sopenharmony_ciInput:
3710d163575Sopenharmony_ci [1]type: "spinor"
3720d163575Sopenharmony_ci [2]start_addr: the partition start address
3730d163575Sopenharmony_ci [3]length: the partition length
3740d163575Sopenharmony_ciOutput: None
3750d163575Sopenharmony_ciReturn: None
3760d163575Sopenharmony_ci***************************************** */
3770d163575Sopenharmony_ciVOID TestPartInit(char *type, UINT32 start_addr, UINT32 length)
3780d163575Sopenharmony_ci{
3790d163575Sopenharmony_ci#if defined(LOSCFG_FS_JFFS)
3800d163575Sopenharmony_ci    int uwRet = 0;
3810d163575Sopenharmony_ci
3820d163575Sopenharmony_ci    if ((uwRet = add_mtd_partition(type, start_addr, length, 0)) != 0)
3830d163575Sopenharmony_ci        PRINT_ERR("add %s partition failed, return %d\n", type, uwRet);
3840d163575Sopenharmony_ci    else {
3850d163575Sopenharmony_ci        printf("[OK] add %s partition successful\n", type);
3860d163575Sopenharmony_ci        if (strcmp(type, "spinor") == 0) {
3870d163575Sopenharmony_ci            if ((uwRet = mount("/dev/spinorblk0", "/jffs0", "jffs", 0, NULL)) != 0)
3880d163575Sopenharmony_ci                PRINT_ERR("mount jffs0 failed,err %d\n", uwRet);
3890d163575Sopenharmony_ci            else
3900d163575Sopenharmony_ci                printf("[OK] mount jffs0 successful\n");
3910d163575Sopenharmony_ci        }
3920d163575Sopenharmony_ci    }
3930d163575Sopenharmony_ci#endif
3940d163575Sopenharmony_ci    return;
3950d163575Sopenharmony_ci}
3960d163575Sopenharmony_ci
3970d163575Sopenharmony_ci/* ****************************************
3980d163575Sopenharmony_ciFunction:Test_PartDelete
3990d163575Sopenharmony_ciDescription: delete the partition for test
4000d163575Sopenharmony_ciInput:
4010d163575Sopenharmony_ci [1]type: "spinor"
4020d163575Sopenharmony_ciOutput: None
4030d163575Sopenharmony_ciReturn: None
4040d163575Sopenharmony_ci***************************************** */
4050d163575Sopenharmony_ciVOID TestPartDelete(char *type)
4060d163575Sopenharmony_ci{
4070d163575Sopenharmony_ci#if defined(LOSCFG_FS_JFFS)
4080d163575Sopenharmony_ci
4090d163575Sopenharmony_ci    int uwRet = 0;
4100d163575Sopenharmony_ci    char *point = "";
4110d163575Sopenharmony_ci
4120d163575Sopenharmony_ci    if (strcmp(type, "spinor") == 0) {
4130d163575Sopenharmony_ci        point = "/jffs0";
4140d163575Sopenharmony_ci    }
4150d163575Sopenharmony_ci
4160d163575Sopenharmony_ci    if ((uwRet = umount(point)) != 0) {
4170d163575Sopenharmony_ci        PRINT_ERR("umount %s failed,err %d.\n", point, uwRet);
4180d163575Sopenharmony_ci    } else {
4190d163575Sopenharmony_ci        printf("[OK] umount %s OK.\n", point);
4200d163575Sopenharmony_ci        if ((uwRet = delete_mtd_partition(0, type)) != 0)
4210d163575Sopenharmony_ci            PRINT_ERR("delete %s partition failed, return %d\n", type, uwRet);
4220d163575Sopenharmony_ci        else
4230d163575Sopenharmony_ci            printf("[OK] delete %s partition OK.\n", type);
4240d163575Sopenharmony_ci    }
4250d163575Sopenharmony_ci#endif
4260d163575Sopenharmony_ci    return;
4270d163575Sopenharmony_ci}
4280d163575Sopenharmony_ci
4290d163575Sopenharmony_ci/* *
4300d163575Sopenharmony_ci * dir: what you want to delete force
4310d163575Sopenharmony_ci */
4320d163575Sopenharmony_ciint RemoveDir(const char *dir)
4330d163575Sopenharmony_ci{
4340d163575Sopenharmony_ci    char cur_dir[] = ".";
4350d163575Sopenharmony_ci    char up_dir[] = "..";
4360d163575Sopenharmony_ci    char dir_name[128] = { 0 };
4370d163575Sopenharmony_ci    DIR *dirp = NULL;
4380d163575Sopenharmony_ci    struct dirent *dp = NULL;
4390d163575Sopenharmony_ci    struct stat dir_stat;
4400d163575Sopenharmony_ci    int ret;
4410d163575Sopenharmony_ci
4420d163575Sopenharmony_ci    if (access(dir, F_OK) != 0) {
4430d163575Sopenharmony_ci        return 0;
4440d163575Sopenharmony_ci    }
4450d163575Sopenharmony_ci
4460d163575Sopenharmony_ci    if (stat(dir, &dir_stat) < 0) {
4470d163575Sopenharmony_ci        perror("get directory stat error");
4480d163575Sopenharmony_ci        return -1;
4490d163575Sopenharmony_ci    }
4500d163575Sopenharmony_ci
4510d163575Sopenharmony_ci    if (S_ISREG(dir_stat.st_mode)) {
4520d163575Sopenharmony_ci        remove(dir);
4530d163575Sopenharmony_ci    } else if (S_ISDIR(dir_stat.st_mode)) {
4540d163575Sopenharmony_ci        dirp = opendir(dir);
4550d163575Sopenharmony_ci        while ((dp = readdir(dirp)) != NULL) {
4560d163575Sopenharmony_ci            if ((strcmp(cur_dir, dp->d_name) == 0) || (strcmp(up_dir, dp->d_name) == 0)) {
4570d163575Sopenharmony_ci                continue;
4580d163575Sopenharmony_ci            }
4590d163575Sopenharmony_ci
4600d163575Sopenharmony_ci            ret = sprintf_s(dir_name, sizeof(dir_name), "%s/%s", dir, dp->d_name);
4610d163575Sopenharmony_ci            if (ret < 0) {
4620d163575Sopenharmony_ci                perror("sprintf dir_name error");
4630d163575Sopenharmony_ci                closedir(dirp);
4640d163575Sopenharmony_ci                return -1;
4650d163575Sopenharmony_ci            }
4660d163575Sopenharmony_ci            RemoveDir(dir_name);
4670d163575Sopenharmony_ci        }
4680d163575Sopenharmony_ci        closedir(dirp);
4690d163575Sopenharmony_ci
4700d163575Sopenharmony_ci        rmdir(dir); /* now dir is empty */
4710d163575Sopenharmony_ci    } else {
4720d163575Sopenharmony_ci        perror("unknown file type!");
4730d163575Sopenharmony_ci    }
4740d163575Sopenharmony_ci    return 0;
4750d163575Sopenharmony_ci}
476