1 /*
2 * osal_file.c
3 *
4 * osal driver
5 *
6 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19 #include "osal_file.h"
20 #include <linux/fs.h>
21 #include <asm/uaccess.h>
22 #include "hdf_log.h"
23
24 #define HDF_LOG_TAG osal_file
25
OsalFileOpen(OsalFile * file,const char * path,int flags,uint32_t rights)26 int32_t OsalFileOpen(OsalFile *file, const char *path, int flags, uint32_t rights)
27 {
28 struct file *fp = NULL;
29
30 if (file == NULL || path == NULL) {
31 HDF_LOGE("%s invalid param", __func__);
32 return HDF_ERR_INVALID_PARAM;
33 }
34
35 file->realFile = (void *)fp;
36
37 fp = filp_open(path, flags, rights);
38 if (IS_ERR_OR_NULL(fp)) {
39 HDF_LOGE("%s open file fail %d %u", __func__, flags, rights);
40 return HDF_FAILURE;
41 }
42
43 file->realFile = (void *)fp;
44 return HDF_SUCCESS;
45 }
46 EXPORT_SYMBOL(OsalFileOpen);
47
OsalFileWrite(OsalFile * file,const void * string,uint32_t length)48 ssize_t OsalFileWrite(OsalFile *file, const void *string, uint32_t length)
49 {
50 ssize_t ret;
51 loff_t pos;
52 mm_segment_t org_fs;
53 struct file *fp = NULL;
54
55 if (file == NULL || IS_ERR_OR_NULL(file->realFile) || string == NULL) {
56 HDF_LOGE("%s invalid param", __func__);
57 return HDF_ERR_INVALID_PARAM;
58 }
59 fp = (struct file *)file->realFile;
60 pos = fp->f_pos;
61 org_fs = get_fs();
62 set_fs(KERNEL_DS);
63
64 ret = vfs_write(fp, string, length, &pos);
65 set_fs(org_fs);
66 if (ret < 0) {
67 HDF_LOGE("%s write file length %u fail %d", __func__, length, ret);
68 return HDF_FAILURE;
69 }
70
71 return ret;
72 }
73 EXPORT_SYMBOL(OsalFileWrite);
74
OsalFileClose(OsalFile * file)75 void OsalFileClose(OsalFile *file)
76 {
77 struct file *fp = NULL;
78
79 if (file == NULL || IS_ERR_OR_NULL(file->realFile)) {
80 HDF_LOGE("%s invalid param", __func__);
81 return;
82 }
83 fp = (struct file *)file->realFile;
84 filp_close(fp, NULL);
85 file->realFile = NULL;
86 }
87 EXPORT_SYMBOL(OsalFileClose);
88
OsalFileRead(OsalFile * file,void * buf,uint32_t length)89 ssize_t OsalFileRead(OsalFile *file, void *buf, uint32_t length)
90 {
91 ssize_t ret;
92 mm_segment_t org_fs;
93 loff_t pos;
94 struct file *fp = NULL;
95
96 if (file == NULL || IS_ERR_OR_NULL(file->realFile) || buf == NULL) {
97 HDF_LOGE("%s invalid param", __func__);
98 return HDF_ERR_INVALID_PARAM;
99 }
100 fp = (struct file *)file->realFile;
101 org_fs = get_fs();
102 set_fs(KERNEL_DS);
103 pos = fp->f_pos;
104
105 ret = vfs_read(fp, buf, length, &pos);
106 set_fs(org_fs);
107 if (ret < 0) {
108 HDF_LOGE("%s read file length %u fail %d", __func__, length, ret);
109 return HDF_FAILURE;
110 }
111
112 return ret;
113 }
114 EXPORT_SYMBOL(OsalFileRead);
115
OsalFileLseek(OsalFile * file,off_t offset,int32_t whence)116 off_t OsalFileLseek(OsalFile *file, off_t offset, int32_t whence)
117 {
118 off_t ret;
119 struct file *fp = NULL;
120
121 if (file == NULL || IS_ERR_OR_NULL(file->realFile)) {
122 HDF_LOGE("%s invalid param", __func__);
123 return HDF_ERR_INVALID_PARAM;
124 }
125 fp = (struct file *)file->realFile;
126 ret = vfs_llseek(fp, offset, whence);
127 if (ret < 0) {
128 HDF_LOGE("%s lseek file fail %ld %d", __func__, offset, whence);
129 return HDF_FAILURE;
130 }
131
132 return ret;
133 }
134 EXPORT_SYMBOL(OsalFileLseek);
135
136