lin
2025-02-25 a02983e50ab34c3e7366b27cdeca427a327faebd
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// SPDX-License-Identifier: GPL-2.0
 
#define pr_fmt(fmt) "sunxi-spinand: " fmt
 
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/spi/spi.h>
#include <linux/mtd/aw-spinand.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/mtd.h>
 
#define SEC_STO_MAGIC 0x5CAA
#define SEC_STO_ITEM_BYTES (4096)
 
struct sec_sto_oob {
   __u8 badflag;
   __u16 magic;
   __u32 checksum;
} __packed;
 
static inline bool is_init_end(struct aw_spinand_sec_sto *sec_sto)
{
   return !!sec_sto->init_end;
}
 
static int is_secure_storage_block(struct aw_spinand_chip *chip,
       unsigned int blk, unsigned int page)
{
   struct aw_spinand_chip_request req = {0};
   struct aw_spinand_chip_ops *ops = chip->ops;
   struct sec_sto_oob oob;
   int ret;
 
   req.block = blk;
   req.page = page;
   req.oobbuf = &oob;
   req.ooblen = sizeof(struct sec_sto_oob);
   ret = ops->phy_read_page(chip, &req);
   if (ret) {
       pr_err("check valid secure storage failed with %d back\n", ret);
       return ret;
   }
 
   return le16_to_cpu(oob.magic) == (u16)SEC_STO_MAGIC;
}
 
static int get_blocks_for_secure_storage(struct aw_spinand_sec_sto *sec_sto)
{
   struct aw_spinand_chip *chip = sec_sto->chip;
   struct aw_spinand_chip_ops *ops = chip->ops;
   struct aw_spinand_chip_request req = {0};
   unsigned int i = 0, blk[2] = {0};
   unsigned int start, end;
   int ret;
 
   start = sec_sto->startblk;
   end = sec_sto->endblk;
   /* find the frist two valid blocks */
   for (; start < end; start++) {
       req.block = start;
 
       ret = ops->phy_is_bad(chip, &req);
       if (ret == true)
           continue;
 
       blk[i++] = start;
       if (i >= 2)
           break;
   }
   if (i < 2) {
       pr_err("no enough good blk between [%u %u) for secure storage\n",
               start, end);
       return -ENOSPC;
   }
 
   sec_sto->blk[0] = blk[0];
   sec_sto->blk[1] = blk[1];
   return 0;
}
 
static unsigned int secure_storage_checksum(char *_buf, unsigned int len)
{
   unsigned int sum = 0;
   unsigned int i;
   unsigned char *buf = (unsigned char *)_buf;
 
   for (i = 0; i < len; i++)
       sum += buf[i];
   return sum;
}
 
static int read_check_secure_storage_page(struct aw_spinand_chip *chip,
       unsigned int blk, int page, char *mbuf, unsigned int len,
       struct sec_sto_oob *oob)
{
   struct aw_spinand_chip_ops *ops = chip->ops;
   struct aw_spinand_chip_request req = {0};
   struct sec_sto_oob _oob;
   unsigned int checksum;
   int ret;
 
   req.block = blk;
   req.page = page;
   req.databuf = mbuf;
   req.datalen = len;
   req.oobbuf = oob ? oob : &_oob;
   req.ooblen = sizeof(struct sec_sto_oob);
 
   ret = ops->phy_read_page(chip, &req);
   if (ret)
       return ret;
 
   checksum = secure_storage_checksum(mbuf, len);
   if (oob)
       return (unsigned int)oob->checksum != checksum;
   else
       return (unsigned int)_oob.checksum != checksum;
}
 
static int write_check_secure_storage_page(struct aw_spinand_chip *chip,
       unsigned int blk, int page, char *mbuf, unsigned int len,
       struct sec_sto_oob *oob)
{
   struct aw_spinand_chip_ops *ops = chip->ops;
   struct aw_spinand_chip_request req = {0};
   int ret;
 
   req.block = blk;
   req.page = page;
   req.databuf = mbuf;
   req.datalen = len;
   req.oobbuf = oob;
   req.ooblen = oob ? sizeof(struct sec_sto_oob) : 0;
 
   if (req.page == 0) {
       ret = ops->phy_erase_block(chip, &req);
       if (ret)
           return ret;
   }
 
   ret = ops->phy_write_page(chip, &req);
   if (ret)
       pr_err("write secure storage failed: %d\n", ret);
 
   return ret;
}
 
#define BOTH_SEC_BLKS_GOOD 0
#define FIRST_SEC_BLK_GOOD 1
#define SECOND_SEC_BLK_GOOD 2
static int check_secure_storage(struct aw_spinand_sec_sto *sec_sto)
{
   struct aw_spinand_chip *chip = sec_sto->chip;
   struct aw_spinand_info *info = chip->info;
   int ret, ret1, ret2, i, blk1, blk2;
   unsigned int pagecnt;
   char *buf;
 
   buf = kmalloc(info->phy_page_size(chip), GFP_KERNEL);
   if (!buf)
       return -ENOMEM;
 
   blk1 = sec_sto->blk[0];
   blk2 = sec_sto->blk[1];
   pagecnt = info->phy_block_size(chip) / info->phy_page_size(chip);
   for (i = 0; i < pagecnt; i++) {
       ret1 = read_check_secure_storage_page(chip, blk1, i, buf,
               info->phy_page_size(chip), NULL);
       ret2 = read_check_secure_storage_page(chip, blk2, i, buf,
               info->phy_page_size(chip), NULL);
       if (ret1 != 0 || ret2 != 0)
           break;
   }
 
   /*
    * retX negative: read failed
    * retX 0: read and check good
    * retX 1: read good but check failed
    */
   if (ret1 == 0 && ret2 == 0)
       ret = BOTH_SEC_BLKS_GOOD;
   else if (ret1 == 0 && ret2 != 0)
       ret = FIRST_SEC_BLK_GOOD;
   else if (ret2 == 0 && ret1 != 0)
       ret = SECOND_SEC_BLK_GOOD;
   else if (ret1 == 1 && ret2 == 1)
       ret = BOTH_SEC_BLKS_GOOD;
   else
       ret = -EINVAL;
 
   if (ret < 0)
       pr_err("nand secure storage check page %u fail\n", i);
   kfree(buf);
   return ret;
}
 
static int repair_secure_storage(struct aw_spinand_sec_sto *sec_sto,
       int good_blk_index)
{
   struct aw_spinand_chip *chip = sec_sto->chip;
   struct aw_spinand_chip_ops *ops = chip->ops;
   unsigned int from, to;
   int ret;
 
   if (good_blk_index == FIRST_SEC_BLK_GOOD) {
       from = sec_sto->blk[0];
       to = sec_sto->blk[1];
   } else {
       from = sec_sto->blk[1];
       to = sec_sto->blk[0];
   }
 
   ret = ops->phy_copy_block(chip, from, to);
   if (ret)
       pr_err("copy phy block from %u to %u failed: %d\n", from, to, ret);
   return ret;
}
 
static int init_secure_storage(struct aw_spinand_sec_sto *sec_sto, int update)
{
   int ret;
 
   ret = get_blocks_for_secure_storage(sec_sto);
   if (ret)
       return ret;
   pr_info("spinand secure storage ok for phy blk %u and %u\n",
           sec_sto->blk[0], sec_sto->blk[1]);
 
   /*
    * both of blk1 and blk2 has no valid data, it may be no any data had
    * been writen to secure storage.
    */
   ret = is_secure_storage_block(sec_sto->chip, sec_sto->blk[0], 0);
   if (ret == false) {
       ret = is_secure_storage_block(sec_sto->chip, sec_sto->blk[1], 0);
       if (ret == false) {
           pr_info("secure storage blks have never used before\n");
           goto end;
       }
   }
 
   ret = check_secure_storage(sec_sto);
   if (ret < 0) {
       pr_err("check secure storage failed with %d\n", ret);
       return ret;
   } else if (ret > 0) {
       pr_info("try to repair secure storage from block %u\n",
               sec_sto->blk[ret - 1]);
       ret = repair_secure_storage(sec_sto, ret);
       if (ret) {
           pr_err("repair secure storage failed with %d\n", ret);
           return ret;
       }
   }
end:
   pr_debug("init secure storage ok\n");
   sec_sto->init_end = true;
   return 0;
}
 
int aw_spinand_secure_storage_read(struct aw_spinand_sec_sto *sec_sto,
       int item, char *buf, unsigned int len)
{
   struct aw_spinand_chip *chip = sec_sto->chip;
   struct aw_spinand_info *info = chip->info;
   int ret = 0, pages_cnt_per_item, minlen, i;
   char *pagebuf;
   unsigned int page, pagesize;
 
   pr_debug("try to read item %d with len %d\n", item, len);
   if (!sec_sto || !sec_sto->chip)
       return -EINVAL;
 
   if (len % 1024) {
       pr_err("secure storage need len (%d) align to 1024B\n", len);
       return -EINVAL;
   }
 
   if (!is_init_end(sec_sto)) {
       ret = init_secure_storage(sec_sto, 1);
       if (ret)
           return ret;
   }
 
   pagebuf = kzalloc(info->phy_page_size(chip), GFP_KERNEL);
   if (!pagebuf) {
       pr_err("no memory for reading secure storage page\n");
       return -ENOMEM;
   }
 
   pagesize = info->phy_page_size(chip);
   pages_cnt_per_item = DIV_ROUND_UP(len, pagesize);
   for (i = 0; i < pages_cnt_per_item; i++) {
       page = item * pages_cnt_per_item + i;
 
       ret = read_check_secure_storage_page(chip, sec_sto->blk[0],
               page, pagebuf, pagesize, NULL);
       if (ret)
           ret = read_check_secure_storage_page(chip,
                   sec_sto->blk[1], page, pagebuf,
                   pagesize, NULL);
 
       if (ret < 0) {
           pr_err("read secure storage page failed with %d back\n", ret);
           break;
       } else if (ret == true) {
           pr_info("secure storage has no valid data on item %d\n", item);
           break;
       }
 
       minlen = min(len, pagesize);
       memcpy(buf + i * pagesize, pagebuf, minlen);
       len -= minlen;
   }
 
   kfree(pagebuf);
   return ret;
}
EXPORT_SYMBOL(aw_spinand_secure_storage_read);
 
int aw_spinand_secure_storage_write(struct aw_spinand_sec_sto *sec_sto,
       int item, char *buf, unsigned int len)
{
   struct aw_spinand_chip *chip = sec_sto->chip;
   struct aw_spinand_info *info = chip->info;
   int ret = 0, pages_cnt_per_item, minlen;
   char *pagebuf;
   struct sec_sto_oob oob;
   unsigned int pagenum, pagesize, pagecnt;
 
   pr_debug("try to write item %d with len %d\n", item, len);
   if (!sec_sto || !sec_sto->chip)
       return -EINVAL;
 
   if (len % 1024) {
       pr_err("secure storage need len (%d) align to 1024B\n", len);
       return -EINVAL;
   }
 
   if (!is_init_end(sec_sto)) {
       ret = init_secure_storage(sec_sto, 1);
       if (ret)
           return ret;
   }
 
   pagesize = info->phy_page_size(chip);
   pagecnt = info->phy_block_size(chip) / info->phy_page_size(chip);
   pagebuf = kzalloc(pagesize, GFP_KERNEL);
   if (!pagebuf)
       return -ENOMEM;
 
   pages_cnt_per_item = DIV_ROUND_UP(len, pagesize);
   for (pagenum = 0; pagenum < pagecnt; pagenum++) {
       if (pagenum / pages_cnt_per_item == item) {
           unsigned int off;
 
           memset(pagebuf, 0xFF, pagesize);
           minlen = min(len, pagesize);
           off = pagesize * (pagenum % pages_cnt_per_item);
           len -= minlen;
 
           memcpy(pagebuf, buf + off, minlen);
 
           oob.badflag = 0xFF;
           oob.magic = cpu_to_le16(SEC_STO_MAGIC);
           oob.checksum = secure_storage_checksum(pagebuf, pagesize);
       } else {
           memset(pagebuf, 0, pagesize);
           ret = read_check_secure_storage_page(chip,
                   sec_sto->blk[0], pagenum,
                   pagebuf, pagesize, &oob);
           if (ret < 0)
               goto out;
       }
       ret = write_check_secure_storage_page(chip, sec_sto->blk[1],
               pagenum, pagebuf, pagesize, &oob);
       if (ret)
           goto out;
   }
 
   ret = repair_secure_storage(sec_sto, SECOND_SEC_BLK_GOOD);
   if (!ret)
       pr_info("write secure storage itme %d ok\n", item);
out:
   kfree(pagebuf);
   return ret;
}
EXPORT_SYMBOL(aw_spinand_secure_storage_write);