blob: cff6cfef3deadae5cda048e3da430dff479b5353 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <assert.h>
4#include <errno.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <sys/ioctl.h>
9#include <string.h>
10#include <unistd.h>
11#include <linux/cpnv.h>
12#include "cfg_api.h"
13#include "libcpnv.h"
14
15#define CP_NV_DEV "/dev/cpnv"
16static char *cpnv_fs_mount_point[] = {"/mnt/imagefs", "/mnt/resource", "/mnt/nvrofs"};
17
18static int cpnv_dev_fd = -1;
19
20static int lib_init(void)
21{
22 int fd = open(CP_NV_DEV, O_RDWR);
23 if (fd < 0) {
24 perror("open cpnv dev fd fail\n");
25 assert(fd > 0);
26 }
27
28 return fd;
29}
30
31static unsigned int cpnv_write(int direction, unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen)
32{
33 int ret;
34 unsigned char buffer[128];
35 unsigned char *tmpbuf = buffer;
36 size_t tmpbuf_len = sizeof(buffer);
37 struct cpnv_readwrite_head *phead;
38
39 //assert(NvItemID > 0);
40 assert(NvItemData != NULL);
41 assert(NvItemLen > 0);
42
43 if (cpnv_dev_fd < 0)
44 cpnv_dev_fd = lib_init();
45
46 if (NvItemLen + sizeof(struct cpnv_readwrite_head) > sizeof(buffer)) {
47 tmpbuf_len = NvItemLen + sizeof(struct cpnv_readwrite_head);
48 tmpbuf = malloc(tmpbuf_len);
49 if (tmpbuf == NULL)
50 return CPNV_ERROR;
51 }
52 phead = (struct cpnv_readwrite_head *)tmpbuf;
53 phead->direction = direction;
54 phead->NvItemID = NvItemID;
55 phead->NvItemLen = NvItemLen;
56 memcpy(phead->NvItemData, NvItemData, NvItemLen);
57
58 ret = write(cpnv_dev_fd, tmpbuf, tmpbuf_len);
59 if (ret == tmpbuf_len) {
60 ret = CPNV_OK;
61 } else {
62 perror("cpnv write error\n");
63 ret = CPNV_ERROR;
64 }
65
66 if (tmpbuf != buffer)
67 free(tmpbuf);
68
69 return ret;
70}
71
72unsigned int cpnv_NvItemWrite(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen)
73{
74 return cpnv_write(TO_NVRW, NvItemID, NvItemData, NvItemLen);
75}
76
77void cpnv_NvItemWriteFactory(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen)
78{
79 cpnv_write(TO_NVFAC, NvItemID, NvItemData, NvItemLen);
80}
81
82unsigned int cpnv_NvItemWriteNvro(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen)
83{
84 return cpnv_write(TO_NVRO, NvItemID, NvItemData, NvItemLen);
85}
86
87unsigned int cpnv_NvItemRead(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen)
88{
89 int ret;
90 unsigned char *tmpbuf = NvItemData;
91 int tmpbuf_len = NvItemLen;
92
93 //assert(NvItemID > 0);
94 assert(NvItemData != NULL);
95 assert(NvItemLen > 0);
96
97 if (cpnv_dev_fd < 0)
98 cpnv_dev_fd = lib_init();
99
100 if (NvItemLen < sizeof(NvItemID)) {
101 tmpbuf_len = sizeof(NvItemID);
102 tmpbuf = malloc(tmpbuf_len);
103 if (tmpbuf == NULL)
104 return CPNV_ERROR;
105 }
106
107 memcpy(tmpbuf, &NvItemID, sizeof(NvItemID));
108 ret = read(cpnv_dev_fd, tmpbuf, tmpbuf_len);
109 if (ret > 0) {
110 memcpy(NvItemData, tmpbuf, NvItemLen);
111 ret = CPNV_OK;
112 } else {
113 perror("cpnv read error\n");
114 bzero(NvItemData, NvItemLen);
115 ret = CPNV_ERROR;
116 }
117
118 if (tmpbuf != NvItemData)
119 free(tmpbuf);
120
121 return ret;
122}
123
124unsigned int cpnv_NvramFlush(void)
125{
126 int ret = CPNV_OK;
127
128 if (cpnv_dev_fd < 0)
129 cpnv_dev_fd = lib_init();
130
131 ret = ioctl(cpnv_dev_fd, CPNV_IOIOCTL_FLUSH);
132 if (ret < 0) {
133 perror("cpnv ioctrl error\n");
134 ret = CPNV_ERROR;
135 }
136
137 return ret;
138}
139
140unsigned int cpnv_ResetNVFactory(void)
141{
142 int ret = CPNV_OK;
143
144 if (cpnv_dev_fd < 0)
145 cpnv_dev_fd = lib_init();
146
147 ret = ioctl(cpnv_dev_fd, CPNV_IOIOCTL_RESETNVFACTORY);
148 if (ret < 0) {
149 perror("cpnv ioctrl error\n");
150 ret = CPNV_ERROR;
151 }
152
153 return (unsigned int)ret;
154}
155
156unsigned int cpnv_ChangeFsPartitionAttr(int partition_no, int writable)
157{
158 char cmd[64] = {0};
159 int ret = 0;
160
161 if(partition_no >= FS_PARTITION_NO_MAX)
162 return CPNV_ERROR;
163
164 if(writable)
165 snprintf(cmd, 64, "/bin/mount -o remount,%s %s", "rw", cpnv_fs_mount_point[partition_no]);
166 else
167 snprintf(cmd, 64, "/bin/mount -o remount,%s %s", "ro", cpnv_fs_mount_point[partition_no]);
168
169 ret = zxic_system(cmd);
170 if(0 == ret)
171 return CPNV_OK;
172 else
173 return CPNV_ERROR;
174}
175
176unsigned int cpnv_ChangeNvRoAttr(int writable)
177{
178 return cpnv_ChangeFsPartitionAttr(FS_NVROFS, writable);
179}
180
181unsigned int cpnv_FsGcWait(int partition_no)
182{
183 int ret = CPNV_ERROR;
184
185 switch(partition_no)
186 {
187 case FS_IMAGEFS:
188 case FS_RESOURCEFS:
189 case FS_NVROFS:
190 syscall(SYSCALL_jffs2_quick_gc_wait_done, partition_no);
191 ret = CPNV_OK;
192 break;
193 default:
194 ret = CPNV_ERROR;
195 }
196
197 return ret;
198}
199