1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *    http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <sys/stat.h>
17#include "stdlib.h"
18#include "ohos_mem_pool.h"
19#include "hiview_log.h"
20#include "los_debug.h"
21#if (LOSCFG_SUPPORT_FATFS == 1)
22#include "fatfs.h"
23#endif /* LOSCFG_SUPPORT_FATFS == 1 */
24#if (LOSCFG_SUPPORT_LITTLEFS == 1)
25#include "lfs.h"
26#endif /* LOSCFG_SUPPORT_LITTLEFS == 1 */
27
28/**
29 * @brief implement for hilog_lite featured/mini
30 * @notice hilog_lite mini converts '%s' to '%p'
31 */
32boolean HilogProc_Impl(const HiLogContent *hilogContent, uint32 len)
33{
34    char tempOutStr[LOG_FMT_MAX_LEN] = {0};
35    if (LogContentFmt(tempOutStr, sizeof(tempOutStr), hilogContent) > 0) {
36        printf(tempOutStr);
37    }
38    return TRUE;
39}
40
41#if (LOSCFG_SUPPORT_FATFS == 1)
42void SetupDefaultVolToPartTable(void)
43{
44    int i = 0;
45    PARTITION defaultVToP[FF_VOLUMES] = {
46        {0, 1, 1},     /* "0:" ==> Physical drive 1, 1st partition */
47        {1, 1, 2},     /* "1:" ==> Physical drive 1, 2nd partition */
48        {2, 1, 3},     /* "2:" ==> Physical drive 1, 3rd partition */
49        {3, 1, 4}      /* "3:" ==> Physical drive 1, 4th partition */
50    };
51
52    for (i = 0; i < FF_VOLUMES; i++) {
53        SetupVolToPartTable(i, defaultVToP[i], GetCfiBlkOps());
54    }
55}
56#endif /* LOSCFG_SUPPORT_FATFS == 1 */
57
58void _init(void) {}
59void _fini(void) {}
60extern void __libc_fini_array (void);
61
62void SystemAdapterInit(void)
63{
64    UINT32 ret = 0;
65
66    /* _start in crt0.S have been replaced by los_start.S in riscv32_virt,
67     * so call this functions here for initialization and destruction of c++ global var
68     */
69    if (atexit(__libc_fini_array) != 0) {
70        PRINT_ERR("register exit proc error!\n");
71    };
72    __libc_init_array();
73
74#if (LOSCFG_SUPPORT_LITTLEFS == 1)
75    if (LittlefsDriverInit(1) == 0) {
76        struct lfs_config *littlefsConfig = GetCfiLfsCfg();
77        ret = mount(NULL, "/data", "littlefs", 0, littlefsConfig);
78        if (ret != LOS_OK) {
79            PRINT_ERR("Mount Littlefs error:0x%x\n", ret);
80        }
81    }
82#elif (LOSCFG_SUPPORT_FATFS == 1)
83    SetupDefaultVolToPartTable();
84    /* mount fs in fat.img,for font.ttf used by graphic */
85    if (ret = mount("p0", "/data", "vfat", 0, NULL)) {
86        PRINT_ERR("Mount Fatfs error:0x%x\n", ret);
87    }
88#endif /* LOSCFG_SUPPORT_FATFS == 1 */
89
90    /* bootstrap init */
91    OHOS_SystemInit();
92
93    /* register hilog output func for mini */
94    HiviewRegisterHilogProc(HilogProc_Impl);
95}
96
97/**
98 * @brief implement for ohos_mem_pool.h
99 */
100void *OhosMalloc(MemType type, uint32 size)
101{
102    if (size == 0) {
103        return NULL;
104    }
105    return malloc(size);
106}
107
108void OhosFree(void *ptr)
109{
110    free(ptr);
111}
112
113/* implementation for js_app_host.h: LP_TaskBegin */
114void LP_TaskBegin(void)
115{
116}
117
118/* implementation for js_app_host.h: LP_TaskEnd */
119void LP_TaskEnd(void)
120{
121}
122
123/**
124 * @brief adapter for js_ability.cpp
125 * #ifdef OHOS_ACELITE_PRODUCT_WATCH
126 */
127void RestoreSystemWrapper(const char *crashMessage)
128{
129    printf("%s\n", crashMessage);
130}
131