1f2d4f7b0Sopenharmony_ci/* 2f2d4f7b0Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd. 3f2d4f7b0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f2d4f7b0Sopenharmony_ci * you may not use this file except in compliance with the License. 5f2d4f7b0Sopenharmony_ci * You may obtain a copy of the License at 6f2d4f7b0Sopenharmony_ci * 7f2d4f7b0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f2d4f7b0Sopenharmony_ci * 9f2d4f7b0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f2d4f7b0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f2d4f7b0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f2d4f7b0Sopenharmony_ci * See the License for the specific language governing permissions and 13f2d4f7b0Sopenharmony_ci * limitations under the License. 14f2d4f7b0Sopenharmony_ci */ 15f2d4f7b0Sopenharmony_ci 16f2d4f7b0Sopenharmony_ci#include "app_file.h" 17f2d4f7b0Sopenharmony_ci#include <fcntl.h> 18f2d4f7b0Sopenharmony_ci#include <limits.h> 19f2d4f7b0Sopenharmony_ci#include <stdbool.h> 20f2d4f7b0Sopenharmony_ci#include <string.h> 21f2d4f7b0Sopenharmony_ci#include <sys/mman.h> 22f2d4f7b0Sopenharmony_ci#include <sys/stat.h> 23f2d4f7b0Sopenharmony_ci#include <sys/types.h> 24f2d4f7b0Sopenharmony_ci#include <unistd.h> 25f2d4f7b0Sopenharmony_ci#include "app_centraldirectory.h" 26f2d4f7b0Sopenharmony_ci#include "app_verify_hal.h" 27f2d4f7b0Sopenharmony_ci 28f2d4f7b0Sopenharmony_cistatic int32_t g_memoryPageSize = 0; 29f2d4f7b0Sopenharmony_ciint32_t InitVerify(FileRead *file, const char *filePath, int32_t *handle) 30f2d4f7b0Sopenharmony_ci{ 31f2d4f7b0Sopenharmony_ci if (handle == NULL || file == NULL || filePath == NULL) { 32f2d4f7b0Sopenharmony_ci LOG_ERROR("file open error"); 33f2d4f7b0Sopenharmony_ci return V_ERR_FILE_OPEN; 34f2d4f7b0Sopenharmony_ci } 35f2d4f7b0Sopenharmony_ci RegistHalFunc(); 36f2d4f7b0Sopenharmony_ci char *path = APPV_MALLOC(PATH_MAX + 1); 37f2d4f7b0Sopenharmony_ci if (path == NULL) { 38f2d4f7b0Sopenharmony_ci LOG_ERROR("path malloc error"); 39f2d4f7b0Sopenharmony_ci return V_ERR_MALLOC; 40f2d4f7b0Sopenharmony_ci } 41f2d4f7b0Sopenharmony_ci if ((strlen(filePath) > PATH_MAX) || (NULL == realpath(filePath, path))) { 42f2d4f7b0Sopenharmony_ci APPV_FREE(path); 43f2d4f7b0Sopenharmony_ci LOG_ERROR("file path error"); 44f2d4f7b0Sopenharmony_ci return V_ERR_FILE_OPEN; 45f2d4f7b0Sopenharmony_ci } 46f2d4f7b0Sopenharmony_ci *handle = open(path, O_RDONLY, 0); 47f2d4f7b0Sopenharmony_ci if (*handle < 0) { 48f2d4f7b0Sopenharmony_ci APPV_FREE(path); 49f2d4f7b0Sopenharmony_ci LOG_PRINT_STR("file open error"); 50f2d4f7b0Sopenharmony_ci return V_ERR_FILE_OPEN; 51f2d4f7b0Sopenharmony_ci } 52f2d4f7b0Sopenharmony_ci if (g_memoryPageSize == 0) { 53f2d4f7b0Sopenharmony_ci g_memoryPageSize = sysconf(_SC_PAGESIZE); 54f2d4f7b0Sopenharmony_ci } 55f2d4f7b0Sopenharmony_ci if (g_memoryPageSize <= 0) { 56f2d4f7b0Sopenharmony_ci LOG_ERROR("MAP_FAILED %d", g_memoryPageSize); 57f2d4f7b0Sopenharmony_ci APPV_FREE(path); 58f2d4f7b0Sopenharmony_ci return V_ERR_FILE_STAT; 59f2d4f7b0Sopenharmony_ci } 60f2d4f7b0Sopenharmony_ci file->len = lseek(*handle, 0, SEEK_END); 61f2d4f7b0Sopenharmony_ci file->fp = *handle; 62f2d4f7b0Sopenharmony_ci APPV_FREE(path); 63f2d4f7b0Sopenharmony_ci return V_OK; 64f2d4f7b0Sopenharmony_ci} 65f2d4f7b0Sopenharmony_ci 66f2d4f7b0Sopenharmony_ciint32_t HapMMap(int32_t bufCapacity, int32_t offset, MmapInfo *mmapInfo, const FileRead *file) 67f2d4f7b0Sopenharmony_ci{ 68f2d4f7b0Sopenharmony_ci if (mmapInfo == NULL || file == NULL || bufCapacity <= 0) { 69f2d4f7b0Sopenharmony_ci return MMAP_FAILED; 70f2d4f7b0Sopenharmony_ci } 71f2d4f7b0Sopenharmony_ci mmapInfo->mapAddr = (char*)(MAP_FAILED); 72f2d4f7b0Sopenharmony_ci if (file->fp == FILE_OPEN_FAIL_ERROR_NUM) { 73f2d4f7b0Sopenharmony_ci return FILE_IS_CLOSE; 74f2d4f7b0Sopenharmony_ci } 75f2d4f7b0Sopenharmony_ci if (offset < 0 || offset > file->len - bufCapacity) { 76f2d4f7b0Sopenharmony_ci return READ_OFFSET_OUT_OF_RANGE; 77f2d4f7b0Sopenharmony_ci } 78f2d4f7b0Sopenharmony_ci lseek(file->fp, offset, SEEK_SET); 79f2d4f7b0Sopenharmony_ci if (g_memoryPageSize == 0) { 80f2d4f7b0Sopenharmony_ci return MMAP_FAILED; 81f2d4f7b0Sopenharmony_ci } 82f2d4f7b0Sopenharmony_ci mmapInfo->mmapPosition = (offset / g_memoryPageSize) * g_memoryPageSize; 83f2d4f7b0Sopenharmony_ci mmapInfo->readMoreLen = (int)(offset - mmapInfo->mmapPosition); 84f2d4f7b0Sopenharmony_ci mmapInfo->mmapSize = bufCapacity + mmapInfo->readMoreLen; 85f2d4f7b0Sopenharmony_ci mmapInfo->mapAddr = (char*)(mmap(NULL, mmapInfo->mmapSize, PROT_READ, 86f2d4f7b0Sopenharmony_ci MAP_SHARED, file->fp, mmapInfo->mmapPosition)); 87f2d4f7b0Sopenharmony_ci if (mmapInfo->mapAddr == MAP_FAILED) { 88f2d4f7b0Sopenharmony_ci LOG_ERROR("MAP_FAILED"); 89f2d4f7b0Sopenharmony_ci return MMAP_FAILED; 90f2d4f7b0Sopenharmony_ci } 91f2d4f7b0Sopenharmony_ci return V_OK; 92f2d4f7b0Sopenharmony_ci} 93f2d4f7b0Sopenharmony_ci 94f2d4f7b0Sopenharmony_civoid HapMUnMap(char *mapAddr, int32_t mmapSize) 95f2d4f7b0Sopenharmony_ci{ 96f2d4f7b0Sopenharmony_ci if (mapAddr == NULL || mmapSize <= 0) { 97f2d4f7b0Sopenharmony_ci return; 98f2d4f7b0Sopenharmony_ci } 99f2d4f7b0Sopenharmony_ci munmap(mapAddr, mmapSize); 100f2d4f7b0Sopenharmony_ci} 101