hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
 
/* Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd */
 
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/soc/rockchip/rk_vendor_storage.h>
#include <linux/uaccess.h>
#include <misc/rkflash_vendor_storage.h>
 
#include "flash_vendor_storage.h"
 
#define FLASH_VENDOR_TEST    0
#define DRM_DEBUG        1
 
#if DRM_DEBUG
#define DLOG(fmt, args...)    pr_info(fmt, ##args)
#else
#define DLOG(x...)
#endif
 
#define FLASH_VENDOR_PART_START        8
#define FLASH_VENDOR_PART_NUM        4
#define FLASH_VENDOR_TAG        VENDOR_HEAD_TAG
 
static int (*_flash_read)(u32 sec, u32 n_sec, void *p_data);
static int (*_flash_write)(u32 sec, u32 n_sec, void *p_data);
static struct flash_vendor_info *g_vendor;
 
int flash_vendor_dev_ops_register(int (*read)(u32 sec,
                         u32 n_sec,
                         void *p_data),
                 int (*write)(u32 sec,
                          u32 n_sec,
                          void *p_data))
{
   if (!_flash_read) {
       _flash_read = read;
       _flash_write = write;
       return 0;
   }
   return -1;
}
 
static u32 flash_vendor_init(void)
{
   u32 i, max_ver, max_index;
 
   if (!_flash_read)
       return -EPERM;
 
   g_vendor = kmalloc(sizeof(*g_vendor), GFP_KERNEL | GFP_DMA);
   if (!g_vendor)
       return 0;
 
   max_ver = 0;
   max_index = 0;
   for (i = 0; i < FLASH_VENDOR_PART_NUM; i++) {
       _flash_read(FLASH_VENDOR_PART_START +
               FLASH_VENDOR_PART_SIZE * i,
               FLASH_VENDOR_PART_SIZE,
               g_vendor);
       if (g_vendor->tag == FLASH_VENDOR_TAG &&
           g_vendor->version == g_vendor->version2) {
           if (max_ver < g_vendor->version) {
               max_index = i;
               max_ver = g_vendor->version;
           }
       }
   }
   /* DLOG("max_ver = %d\n",max_ver); */
   if (max_ver) {
       _flash_read(FLASH_VENDOR_PART_START +
               FLASH_VENDOR_PART_SIZE * max_index,
               FLASH_VENDOR_PART_SIZE,
       g_vendor);
   } else {
       memset(g_vendor, 0, sizeof(*g_vendor));
       g_vendor->version = 1;
       g_vendor->tag = FLASH_VENDOR_TAG;
       g_vendor->version2 = g_vendor->version;
       g_vendor->free_offset = 0;
       g_vendor->free_size = sizeof(g_vendor->data);
   }
   /* rknand_print_hex("vendor:", g_vendor, 4, 1024); */
 
   return 0;
}
 
static int flash_vendor_read(u32 id, void *pbuf, u32 size)
{
   u32 i;
 
   if (!g_vendor)
       return -1;
 
   for (i = 0; i < g_vendor->item_num; i++) {
       if (g_vendor->item[i].id == id) {
           if (size > g_vendor->item[i].size)
               size = g_vendor->item[i].size;
           memcpy(pbuf,
                  &g_vendor->data[g_vendor->item[i].offset],
                  size);
           return size;
       }
   }
   return (-1);
}
 
static int flash_vendor_write(u32 id, void *pbuf, u32 size)
{
   u32 i, j, next_index, align_size, alloc_size, item_num;
   u32 offset, next_size;
   u8 *p_data;
   struct vendor_item *item;
   struct vendor_item *next_item;
 
   if (!g_vendor)
       return -1;
 
   p_data = g_vendor->data;
   item_num = g_vendor->item_num;
   align_size = ALIGN(size, 0x40); /* align to 64 bytes*/
   next_index = g_vendor->next_index;
   for (i = 0; i < item_num; i++) {
       item = &g_vendor->item[i];
       if (item->id == id) {
           alloc_size = ALIGN(item->size, 0x40);
           if (size > alloc_size) {
               if (g_vendor->free_size < align_size)
                   return -1;
               offset = item->offset;
               for (j = i; j < item_num - 1; j++) {
                   item = &g_vendor->item[j];
                   next_item = &g_vendor->item[j + 1];
                   item->id = next_item->id;
                   item->size = next_item->size;
                   item->offset = offset;
                   next_size = ALIGN(next_item->size,
                             0x40);
                   memcpy(&p_data[offset],
                          &p_data[next_item->offset],
                          next_size);
                   offset += next_size;
               }
               item = &g_vendor->item[j];
               item->id = id;
               item->offset = offset;
               item->size = size;
               memcpy(&p_data[item->offset], pbuf, size);
               g_vendor->free_offset = offset + align_size;
               g_vendor->free_size -= (align_size -
                           alloc_size);
           } else {
               memcpy(&p_data[item->offset],
                      pbuf,
                      size);
               g_vendor->item[i].size = size;
           }
           g_vendor->version++;
           g_vendor->version2 = g_vendor->version;
           g_vendor->next_index++;
           if (g_vendor->next_index >= FLASH_VENDOR_PART_NUM)
               g_vendor->next_index = 0;
           _flash_write(FLASH_VENDOR_PART_START +
                   FLASH_VENDOR_PART_SIZE * next_index,
                   FLASH_VENDOR_PART_SIZE,
                   g_vendor);
           return 0;
       }
   }
 
   if (g_vendor->free_size >= align_size) {
       item = &g_vendor->item[g_vendor->item_num];
       item->id = id;
       item->offset = g_vendor->free_offset;
       item->size = align_size;
       item->size = size;
       g_vendor->free_offset += align_size;
       g_vendor->free_size -= align_size;
       memcpy(&g_vendor->data[item->offset], pbuf, size);
       g_vendor->item_num++;
       g_vendor->version++;
       g_vendor->next_index++;
       g_vendor->version2 = g_vendor->version;
       if (g_vendor->next_index >= FLASH_VENDOR_PART_NUM)
           g_vendor->next_index = 0;
       _flash_write(FLASH_VENDOR_PART_START +
               FLASH_VENDOR_PART_SIZE * next_index,
               FLASH_VENDOR_PART_SIZE,
           g_vendor);
       return 0;
   }
 
   return(-1);
}
 
#if (FLASH_VENDOR_TEST)
static void print_hex(char *s, void *buf, int width, int len)
{
   print_hex_dump(KERN_WARNING, s, DUMP_PREFIX_OFFSET,
              16, width, buf, len * width, 0);
}
 
static void flash_vendor_test(void)
{
   u32 i;
   u8 test_buf[512];
 
   memset(test_buf, 0, 512);
   for (i = 0; i < 62; i++) {
       memset(test_buf, i, i + 1);
       flash_vendor_write(i, test_buf, i + 1);
   }
   memset(test_buf, 0, 512);
   for (i = 0; i < 62; i++) {
       flash_vendor_read(i, test_buf, i + 1);
       DLOG("id = %d ,size = %d\n", i, i + 1);
       print_hex("data:", test_buf, 1, i + 1);
   }
   flash_vendor_init();
   memset(test_buf, 0, 512);
   for (i = 0; i < 62; i++) {
       flash_vendor_read(i, test_buf, i + 1);
       DLOG("id = %d ,size = %d\n", i, i + 1);
       print_hex("data:", test_buf, 1, i + 1);
   }
   while (1)
       ;
}
#endif
 
static long vendor_storage_ioctl(struct file *file,
                unsigned int cmd,
                unsigned long arg)
{
   long ret = -EINVAL;
   int size;
   u32 *temp_buf;
   struct RK_VENDOR_REQ *req;
 
   req = kmalloc(sizeof(*req), GFP_KERNEL);
   if (!req)
       return ret;
 
   temp_buf = (u32 *)req;
 
   switch (cmd) {
   case VENDOR_READ_IO:
   {
       if (copy_from_user(temp_buf,
                  (void __user *)arg,
                  sizeof(*req))) {
           DLOG("copy_from_user error\n");
           ret = -EFAULT;
           break;
       }
       if (req->tag == VENDOR_REQ_TAG) {
           size = flash_vendor_read(req->id,
                        req->data,
                        req->len);
           if (size > 0) {
               req->len = size;
               ret = 0;
               if (copy_to_user((void __user *)arg,
                        temp_buf,
                        sizeof(*req)))
                   ret = -EFAULT;
           }
       }
   } break;
   case VENDOR_WRITE_IO:
   {
       if (copy_from_user(temp_buf,
                  (void __user *)arg,
                  sizeof(struct RK_VENDOR_REQ))) {
           DLOG("copy_from_user error\n");
           ret = -EFAULT;
           break;
       }
       if (req->tag == VENDOR_REQ_TAG)
           ret = flash_vendor_write(req->id,
                        req->data,
                        req->len);
   } break;
   default:
       return -EINVAL;
   }
   kfree(temp_buf);
   DLOG("flash_vendor_ioctl cmd=%x ret = %lx\n", cmd, ret);
   return ret;
}
 
static const struct file_operations vendor_storage_fops = {
   .compat_ioctl    = vendor_storage_ioctl,
   .unlocked_ioctl = vendor_storage_ioctl,
};
 
static struct miscdevice vender_storage_dev = {
   .minor = MISC_DYNAMIC_MINOR,
   .name  = "vendor_storage",
   .fops  = &vendor_storage_fops,
};
 
static int vendor_init_thread(void *arg)
{
   int ret;
 
   pr_info("flash %s!\n", __func__);
   ret = flash_vendor_init();
   if (!ret) {
       ret = misc_register(&vender_storage_dev);
       #ifdef CONFIG_ROCKCHIP_VENDOR_STORAGE
       rk_vendor_register(flash_vendor_read, flash_vendor_write);
       #endif
   }
   pr_info("flash vendor storage:20170308 ret = %d\n", ret);
   return ret;
}
 
static int __init vendor_storage_init(void)
{
   kthread_run(vendor_init_thread, (void *)NULL, "vendor_storage_init");
   return 0;
}
 
static __exit void vendor_storage_deinit(void)
{
   if (g_vendor) {
       misc_deregister(&vender_storage_dev);
       kfree(g_vendor);
       g_vendor = NULL;
   }
}
 
device_initcall_sync(vendor_storage_init);
module_exit(vendor_storage_deinit);
MODULE_LICENSE("GPL");