1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#include <linux/fs.h>
#include <asm/segment.h>
#include <linux/uaccess.h>
#include <linux/buffer_head.h>
#include "tz_fileio.h"
/** FILE_Open
* Open a file for read or write
*
* @param path File path to open
* @param flags Access flag see open(2)
* @param mode The mode/permission for created file, see open(2)
* @retval File handle
*/
struct file *FILE_Open(const char *path, int flags, int mode)
{
struct file *filp = NULL;
mm_segment_t oldfs;
int err = 0;
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open(path, flags, mode);
set_fs(oldfs);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
/** FILE_Read
* Read data from a file.
*
* @param file Opened file handle
* @param offset Pointer to begin offset to read.
* @param data user's buffer address.
* @param size readdata length in bytes.
* @retval >0 SUCCESS, return actual bytes read.
* @retval <0 Fail, errno
*/
int FILE_Read(struct file *file, unsigned char *data, unsigned int size, unsigned long long *offset)
{
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_read(file, data, size, offset);
set_fs(oldfs);
return ret;
}
/** FILE_Write
* Write data from a file.
*
* @param file Opened file handle
* @param offset Pointer to begin offset to Write.
* @param data user's buffer address.
* @param size Writedata length in bytes.
* @retval >0 SUCCESS, return actual bytes Write.
* @retval <0 Fail, errno
*/
int FILE_Write(struct file *file, unsigned char *data, unsigned int size, unsigned long long *offset)
{
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_write(file, data, size, offset);
set_fs(oldfs);
return ret;
}
/** FILE_ReadData
* Read data from a file.
*
* @param path File path name to read.
* @param u4Offset begin offset to read.
* @param pData user's buffer address.
* @param i4Length readdata length in bytes.
* @retval >0 SUCCESS, return actual bytes read.
* @retval <0 Fail, errno
*/
int FILE_ReadData(const char *path, unsigned int u4Offset, char *pData, int i4Length)
{
struct file *file = NULL;
UINT64 u8Offset = (UINT64)u4Offset;
file = FILE_Open(path, O_RDONLY, 0);
if (!file)
return -EFAULT;
i4Length = FILE_Read(file, (void *)pData, i4Length, &u8Offset);
filp_close(file, NULL);
return i4Length;
}
/** FILE_WriteData
* Write data to a file.
*
* @param path File path name to write.
* @param u4Offset begin offset to write.
* @param pData user's buffer address.
* @param i4Length writedata length in bytes.
* @retval >0 SUCCESS, return actual bytes writen.
* @retval <0 Fail, errno
*/
int FILE_WriteData(const char *path, unsigned int u4Offset, char *pData, int i4Length)
{
struct file *file = NULL;
UINT64 u8Offset = (UINT64)u4Offset;
file = FILE_Open(path, O_WRONLY|O_CREAT, 0644);
if (!file)
return -EFAULT;
i4Length = FILE_Write(file, (void *)pData, i4Length, &u8Offset);
filp_close(file, NULL);
return i4Length;
}
|