hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
/*
 * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <malloc.h>
#include <asm/arch/vendor.h>
#include <boot_rkimg.h>
#include <nand.h>
#include <part.h>
#include <fdt_support.h>
 
/* tag for vendor check */
#define VENDOR_TAG        0x524B5644
/* The Vendor partition contains the number of Vendor blocks */
#define MTD_VENDOR_PART_NUM    1
#define NAND_VENDOR_PART_NUM    2
#define VENDOR_PART_NUM        4
/* align to 64 bytes */
#define VENDOR_BTYE_ALIGN    0x3F
#define VENDOR_BLOCK_SIZE    512
 
#define PAGE_ALGIN_SIZE        (4096uL)
#define PAGE_ALGIN_MASK        (~(PAGE_ALGIN_SIZE - 1))
 
/* --- Emmc define --- */
/* Starting address of the Vendor in memory. */
#define EMMC_VENDOR_PART_OFFSET        (1024 * 7)
/*
 * The number of memory blocks used by each
 * Vendor structure(128 * 512B = 64KB)
 */
#define EMMC_VENDOR_PART_BLKS        128
/* The maximum number of items in each Vendor block */
#define EMMC_VENDOR_ITEM_NUM        126
 
/* --- Spi Nand/SLC/MLC large capacity case define --- */
/* The Vendor partition contains the number of Vendor blocks */
#define NAND_VENDOR_PART_OFFSET        0
/*
 * The number of memory blocks used by each
 * Vendor structure(8 * 512B = 4KB)
 */
#define NAND_VENDOR_PART_BLKS        128
/* The maximum number of items in each Vendor block */
#define NAND_VENDOR_ITEM_NUM        126
 
/* --- Spi/Spi Nand/SLC/MLC small capacity case define --- */
/* The Vendor partition contains the number of Vendor blocks */
#define    FLASH_VENDOR_PART_OFFSET    8
/*
 * The number of memory blocks used by each
 * Vendor structure(8 * 512B = 4KB)
 */
#define FLASH_VENDOR_PART_BLKS        8
/* The maximum number of items in each Vendor block */
#define FLASH_VENDOR_ITEM_NUM        62
 
/* Vendor uinit test define */
int vendor_storage_test(void);
 
struct vendor_hdr {
   u32    tag;
   u32    version;
   u16    next_index;
   u16    item_num;
   u16    free_offset; /* Free space offset */
   u16    free_size; /* Free space size */
};
 
/*
 * Different types of Flash vendor info are different.
 * EMMC:EMMC_VENDOR_PART_BLKS * BLOCK_SIZE(512) = 64KB;
 * Spi Nor/Spi Nand/SLC/MLC: FLASH_VENDOR_PART_BLKS *
 * BLOCK_SIZE(512) = 4KB.
 * hash: For future expansion.
 * version2: Together with hdr->version, it is used to
 * ensure the current Vendor block content integrity.
 *   (version2 == hdr->version):Data valid;
 *   (version2 != hdr->version):Data invalid.
 */
struct vendor_info {
   struct vendor_hdr *hdr;
   struct vendor_item *item;
   u8 *data;
   u32 *hash;
   u32 *version2;
};
 
struct mtd_flash_info {
   u32 part_offset;
   u32 part_size;
   u32 blk_offset;
   u32 page_offset;
   u32 version;
   u32 ops_size;
   u32 blk_size;
};
 
/*
 * Calculate the offset of each field for emmc.
 * Emmc vendor info size: 64KB
 */
#define EMMC_VENDOR_INFO_SIZE    (EMMC_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
#define EMMC_VENDOR_DATA_OFFSET    (sizeof(struct vendor_hdr) + EMMC_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
#define EMMC_VENDOR_HASH_OFFSET (EMMC_VENDOR_INFO_SIZE - 8)
#define EMMC_VENDOR_VERSION2_OFFSET (EMMC_VENDOR_INFO_SIZE - 4)
 
/*
 * Calculate the offset of each field for spi nand/slc/mlc large capacity case.
 * Flash vendor info size: 4KB
 */
#define NAND_VENDOR_INFO_SIZE    (NAND_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
#define NAND_VENDOR_DATA_OFFSET    (sizeof(struct vendor_hdr) + NAND_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
#define NAND_VENDOR_HASH_OFFSET (NAND_VENDOR_INFO_SIZE - 8)
#define NAND_VENDOR_VERSION2_OFFSET (NAND_VENDOR_INFO_SIZE - 4)
 
/*
 * Calculate the offset of each field for spi nor/spi nand/slc/mlc large small capacity case.
 * Flash vendor info size: 4KB
 */
#define FLASH_VENDOR_INFO_SIZE    (FLASH_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE)
#define FLASH_VENDOR_DATA_OFFSET (sizeof(struct vendor_hdr) + FLASH_VENDOR_ITEM_NUM * sizeof(struct vendor_item))
#define FLASH_VENDOR_HASH_OFFSET (FLASH_VENDOR_INFO_SIZE - 8)
#define FLASH_VENDOR_VERSION2_OFFSET (FLASH_VENDOR_INFO_SIZE - 4)
 
/* vendor info */
static struct vendor_info vendor_info;
/* The storage type of the device */
static int bootdev_type;
 
#ifdef CONFIG_MTD_BLK
static struct mtd_flash_info s_flash_info;
static const char *vendor_mtd_name = "vnvm";
#endif
 
/* vendor private read write ops*/
static    int (*_flash_read)(struct blk_desc *dev_desc,
              u32 sec,
              u32 n_sec,
              void *buffer);
static    int (*_flash_write)(struct blk_desc *dev_desc,
               u32 sec,
               u32 n_sec,
               void *buffer);
 
int flash_vendor_dev_ops_register(int (*read)(struct blk_desc *dev_desc,
                         u32 sec,
                         u32 n_sec,
                         void *p_data),
                 int (*write)(struct blk_desc *dev_desc,
                          u32 sec,
                          u32 n_sec,
                          void *p_data))
{
   if (!_flash_read) {
       _flash_read = read;
       _flash_write = write;
       return 0;
   }
 
   return -EPERM;
}
 
#ifdef CONFIG_MTD_BLK
static int mtd_vendor_storage_init(struct blk_desc *dev_desc)
{
   struct mtd_info *mtd = (struct mtd_info *)dev_desc->bdev->priv;
   disk_partition_t vnvm_part_info;
   void *buf = vendor_info.hdr;
   int ret, offset;
   int part_num, bad_block_size;
 
   memset(&vnvm_part_info, 0x0, sizeof(vnvm_part_info));
   part_num = part_get_info_by_name(dev_desc, vendor_mtd_name, &vnvm_part_info);
   if (part_num < 0)
       return -EIO;
 
   s_flash_info.part_offset = (u32)vnvm_part_info.start;
   s_flash_info.part_size = (u32)vnvm_part_info.size;
   s_flash_info.page_offset = 0;
   s_flash_info.blk_offset = 0;
   s_flash_info.version = 0;
   /* SPI Nor unified to Support 64KB erase block */
   if (dev_desc->devnum == BLK_MTD_SPI_NOR)
       s_flash_info.blk_size = 0x80;
   else
       s_flash_info.blk_size = mtd->erasesize >> 9;
   s_flash_info.ops_size = roundup(FLASH_VENDOR_INFO_SIZE, mtd->writesize) >> 9;
 
   /* scan bad block and calculate the real size can be used */
   bad_block_size = 0;
   for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) {
       if (mtd_block_isbad(mtd, (s_flash_info.part_offset + offset) << 9))
           bad_block_size += s_flash_info.blk_size;
   }
   s_flash_info.part_size -= bad_block_size;
 
   for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) {
       ret = blk_dread(dev_desc, s_flash_info.part_offset + offset,
               FLASH_VENDOR_INFO_SIZE >> 9,
               (u8 *)buf);
       debug("%s: read %x version = %x\n", __func__,
             s_flash_info.part_offset + offset,
             vendor_info.hdr->version);
       if (ret == (FLASH_VENDOR_INFO_SIZE >> 9) && vendor_info.hdr->tag == VENDOR_TAG &&
           vendor_info.hdr->version == *vendor_info.version2) {
           if (vendor_info.hdr->version > s_flash_info.version) {
               s_flash_info.version = vendor_info.hdr->version;
               s_flash_info.blk_offset = offset;
           }
       }
   }
 
   debug("%s: s_flash_info.version = %x %x\n", __func__, s_flash_info.version, s_flash_info.blk_offset);
   if (s_flash_info.version) {
       for (offset = s_flash_info.blk_size  - s_flash_info.ops_size;
            offset >= 0;
            offset -= s_flash_info.ops_size) {
           ret = blk_dread(dev_desc, s_flash_info.part_offset +
                   s_flash_info.blk_offset + offset,
                   1,
                   (u8 *)buf);
 
           /* the page is not programmed */
           if (ret == 1 && vendor_info.hdr->tag == 0xFFFFFFFF)
               continue;
 
           /* point to the next free page */
           if (s_flash_info.page_offset < offset)
               s_flash_info.page_offset = offset + s_flash_info.ops_size;
 
           if (ret != 1 || vendor_info.hdr->tag != VENDOR_TAG)
               continue;
           ret = blk_dread(dev_desc, s_flash_info.part_offset +
                   s_flash_info.blk_offset + offset,
                   FLASH_VENDOR_INFO_SIZE >> 9,
                   (u8 *)buf);
           debug("%s: read %x version = %x\n", __func__,
                 s_flash_info.part_offset + s_flash_info.blk_offset  + offset,
                 vendor_info.hdr->version);
 
           if (ret == (FLASH_VENDOR_INFO_SIZE >> 9)  && vendor_info.hdr->tag == VENDOR_TAG &&
               vendor_info.hdr->version == *vendor_info.version2) {
               s_flash_info.version = vendor_info.hdr->version;
               break;
           }
       }
   } else {
       memset((u8 *)vendor_info.hdr, 0, FLASH_VENDOR_INFO_SIZE);
       vendor_info.hdr->version = 1;
       vendor_info.hdr->tag = VENDOR_TAG;
       vendor_info.hdr->free_size =
           ((u32)(size_t)vendor_info.hash
           - (u32)(size_t)vendor_info.data);
       *vendor_info.version2 = vendor_info.hdr->version;
   }
 
   return 0;
}
 
static int mtd_vendor_write(struct blk_desc *dev_desc,
               u32 sec,
               u32 n_sec,
               void *buf)
{
   int ret, count = 0, err = 0;
 
re_write:
   debug("[Vendor INFO]:%s page_offset=0x%x count = %x\n", __func__, s_flash_info.part_offset +
         s_flash_info.blk_offset + s_flash_info.page_offset, count);
   if (s_flash_info.page_offset >= s_flash_info.blk_size) {
       s_flash_info.blk_offset += s_flash_info.blk_size;
       if (s_flash_info.blk_offset >= s_flash_info.part_size)
           s_flash_info.blk_offset = 0;
       s_flash_info.page_offset = 0;
       /*
        * The spi NOR driver only erase 4KB while write data, and here need to
        * erase one block for vendor storage request.
        */
       blk_derase(dev_desc, s_flash_info.part_offset + s_flash_info.blk_offset, s_flash_info.blk_size);
   }
 
   dev_desc->op_flag |= BLK_MTD_CONT_WRITE;
   ret = blk_dwrite(dev_desc, s_flash_info.part_offset +
            s_flash_info.blk_offset + s_flash_info.page_offset,
            FLASH_VENDOR_INFO_SIZE >> 9,
            (u8 *)buf);
   dev_desc->op_flag &= ~(BLK_MTD_CONT_WRITE);
 
   s_flash_info.page_offset += s_flash_info.ops_size;
   if (ret != (FLASH_VENDOR_INFO_SIZE >> 9)) {
       err++;
       if (err > 3)
           return -EIO;
       goto re_write;
   }
 
   count++;
   /* write 2 copies for reliability */
   if (count < 2)
       goto re_write;
 
   return ret;
}
#endif
 
/**********************************************************/
/*              vendor API implementation                 */
/**********************************************************/
static int vendor_ops(u8 *buffer, u32 addr, u32 n_sec, int write)
{
   struct blk_desc *dev_desc;
   unsigned int lba = 0;
   int ret = 0;
 
   dev_desc = rockchip_get_bootdev();
   if (!dev_desc) {
       printf("%s: dev_desc is NULL!\n", __func__);
       return -ENODEV;
   }
 
   if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) {
       dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR);
       if (!dev_desc) {
           printf("%s: dev_desc is NULL!\n", __func__);
           return -ENODEV;
       }
   }
 
   /* Get the offset address according to the device type */
   switch (dev_desc->if_type) {
   case IF_TYPE_MMC:
       /*
        * The location of VendorStorage in Flash is shown in the
        * following figure. The starting address of the VendorStorage
        * partition offset is 3.5MB(EMMC_VENDOR_PART_OFFSET*BLOCK_SIZE(512)),
        * and the partition size is 256KB.
        * ----------------------------------------------------
        * |   3.5MB    |  VendorStorage  |                   |
        * ----------------------------------------------------
        */
       lba = EMMC_VENDOR_PART_OFFSET;
       debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
       break;
   case IF_TYPE_RKNAND:
   case IF_TYPE_SPINAND:
       /*
        * The location of VendorStorage in Flash is shown in the
        * following figure. The starting address of the VendorStorage
        * partition offset is 0KB in FTL vendor block,
        * and the partition size is 128KB.
        * ----------------------------------------------------
        * |  VendorStorage  |                     |
        * ----------------------------------------------------
        */
       lba = NAND_VENDOR_PART_OFFSET;
       debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
       break;
   case IF_TYPE_SPINOR:
       /*
        * The location of VendorStorage in Flash is shown in the
        * following figure. The starting address of the VendorStorage
        * partition offset is 4KB (FLASH_VENDOR_PART_OFFSET * BLOCK_SIZE),
        * and the partition size is 16KB.
        * ----------------------------------------------------
        * |   4KB    |  VendorStorage  |                     |
        * ----------------------------------------------------
        */
       lba = FLASH_VENDOR_PART_OFFSET;
       debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba);
       break;
#ifdef CONFIG_MTD_BLK
   case IF_TYPE_MTD:
       /*
        * The location of VendorStorage in NAND FLASH or SPI NAND partition "vnvm"
        * is shown in the following figure. The partition size is at least  4
        * NAND FLASH blocks.
        * ----------------------------------------------------
        * |   .....    |  vnvm  |  .......                   |
        * ----------------------------------------------------
        */
       lba = 0;
       break;
#endif
   default:
       printf("[Vendor ERROR]:Boot device type is invalid!\n");
       return -ENODEV;
   }
   if (write) {
       if (_flash_write)
           ret = _flash_write(dev_desc, lba + addr, n_sec, buffer);
       else
           ret = blk_dwrite(dev_desc, lba + addr, n_sec, buffer);
   } else {
       if (_flash_read)
           ret = _flash_read(dev_desc, lba + addr, n_sec, buffer);
       else
           ret = blk_dread(dev_desc, lba + addr, n_sec, buffer);
   }
 
   debug("[Vendor INFO]:op=%s, ret=%d\n", write ? "write" : "read", ret);
 
   return ret;
}
 
/*
 * The VendorStorage partition is divided into four parts
 * (vendor 0-3) and its structure is shown in the following figure.
 * The init function is used to select the latest and valid vendor.
 *
 * |******************** FLASH ********************|
 * -------------------------------------------------
 * |  vendor0  |  vendor1  |  vendor2  |  vendor3  |
 * -------------------------------------------------
 * Notices:
 *   1. "version" and "version2" are used to verify that the vendor
 *      is valid (equal is valid).
 *   2. the "version" value is larger, indicating that the current
 *      verndor data is new.
 */
int vendor_storage_init(void)
{
   int ret = 0;
   int ret_size;
   u8 *buffer;
   u32 size, i;
   u32 max_ver = 0;
   u32 max_index = 0;
   u16 data_offset, hash_offset, part_num;
   u16 version2_offset, part_size;
   struct blk_desc *dev_desc;
 
   dev_desc = rockchip_get_bootdev();
   if (!dev_desc) {
       printf("[Vendor ERROR]:Invalid boot device type(%d)\n",
              bootdev_type);
       return -ENODEV;
   }
 
   if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) {
       dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR);
       if (!dev_desc) {
           printf("%s: dev_desc is NULL!\n", __func__);
           return -ENODEV;
       }
   }
 
   switch (dev_desc->if_type) {
   case IF_TYPE_MMC:
       size = EMMC_VENDOR_INFO_SIZE;
       part_size = EMMC_VENDOR_PART_BLKS;
       data_offset = EMMC_VENDOR_DATA_OFFSET;
       hash_offset = EMMC_VENDOR_HASH_OFFSET;
       version2_offset = EMMC_VENDOR_VERSION2_OFFSET;
       part_num = VENDOR_PART_NUM;
       break;
   case IF_TYPE_RKNAND:
   case IF_TYPE_SPINAND:
       size = NAND_VENDOR_INFO_SIZE;
       part_size = NAND_VENDOR_PART_BLKS;
       data_offset = NAND_VENDOR_DATA_OFFSET;
       hash_offset = NAND_VENDOR_HASH_OFFSET;
       version2_offset = NAND_VENDOR_VERSION2_OFFSET;
       part_num = NAND_VENDOR_PART_NUM;
       break;
   case IF_TYPE_SPINOR:
       size = FLASH_VENDOR_INFO_SIZE;
       part_size = FLASH_VENDOR_PART_BLKS;
       data_offset = FLASH_VENDOR_DATA_OFFSET;
       hash_offset = FLASH_VENDOR_HASH_OFFSET;
       version2_offset = FLASH_VENDOR_VERSION2_OFFSET;
       part_num = VENDOR_PART_NUM;
       break;
#ifdef CONFIG_MTD_BLK
   case IF_TYPE_MTD:
       size = FLASH_VENDOR_INFO_SIZE;
       part_size = FLASH_VENDOR_PART_BLKS;
       data_offset = FLASH_VENDOR_DATA_OFFSET;
       hash_offset = FLASH_VENDOR_HASH_OFFSET;
       version2_offset = FLASH_VENDOR_VERSION2_OFFSET;
       part_num = MTD_VENDOR_PART_NUM;
       _flash_write = mtd_vendor_write;
       break;
#endif
   default:
       debug("[Vendor ERROR]:Boot device type is invalid!\n");
       ret = -ENODEV;
       break;
   }
   /* Invalid bootdev type */
   if (ret)
       return ret;
 
   /* Initialize */
   bootdev_type = dev_desc->if_type;
 
   /* Always use, no need to release, align to page size for kerenl reserved memory */
   buffer = (u8 *)memalign(PAGE_ALGIN_SIZE, size);
   if (!buffer) {
       printf("[Vendor ERROR]:Malloc failed!\n");
       ret = -ENOMEM;
       goto out;
   }
 
   /* Pointer initialization */
   vendor_info.hdr = (struct vendor_hdr *)buffer;
   vendor_info.item = (struct vendor_item *)(buffer + sizeof(struct vendor_hdr));
   vendor_info.data = buffer + data_offset;
   vendor_info.hash = (u32 *)(buffer + hash_offset);
   vendor_info.version2 = (u32 *)(buffer + version2_offset);
 
#ifdef CONFIG_MTD_BLK
   if (dev_desc->if_type == IF_TYPE_MTD) {
       ret = mtd_vendor_storage_init(dev_desc);
       goto out;
   }
#endif
 
   /* Find valid and up-to-date one from (vendor0 - vendor3) */
   for (i = 0; i < part_num; i++) {
       ret_size = vendor_ops((u8 *)vendor_info.hdr,
                     part_size * i, part_size, 0);
       if (ret_size != part_size) {
           ret = -EIO;
           goto out;
       }
 
       if ((vendor_info.hdr->tag == VENDOR_TAG) &&
           (*(vendor_info.version2) == vendor_info.hdr->version)) {
           if (max_ver < vendor_info.hdr->version) {
               max_index = i;
               max_ver = vendor_info.hdr->version;
           }
       }
   }
 
   if (max_ver) {
       debug("[Vendor INFO]:max_ver=%d, vendor_id=%d.\n", max_ver, max_index);
       /*
        * Keep vendor_info the same as the largest
        * version of vendor
        */
       if (max_index != (part_num - 1)) {
           ret_size = vendor_ops((u8 *)vendor_info.hdr,
                          part_size * max_index, part_size, 0);
           if (ret_size != part_size) {
               ret = -EIO;
               goto out;
           }
       }
   } else {
       debug("[Vendor INFO]:Reset vendor info...\n");
       memset((u8 *)vendor_info.hdr, 0, size);
       vendor_info.hdr->version = 1;
       vendor_info.hdr->tag = VENDOR_TAG;
       /* data field length */
       vendor_info.hdr->free_size =
           ((u32)(size_t)vendor_info.hash
           - (u32)(size_t)vendor_info.data);
       *(vendor_info.version2) = vendor_info.hdr->version;
   }
   debug("[Vendor INFO]:ret=%d.\n", ret);
 
out:
   if (ret)
       bootdev_type = 0;
 
   return ret;
}
 
void vendor_storage_fixup(void *blob)
{
   unsigned long size;
   unsigned long start;
   ulong offset;
 
   /* init vendor storage */
   if (!bootdev_type) {
       if (vendor_storage_init() < 0)
           return;
   }
 
   offset = fdt_node_offset_by_compatible(blob, 0, "rockchip,vendor-storage-rm");
   if (offset >= 0) {
       start = (unsigned long)vendor_info.hdr;
       size = (unsigned long)((void *)vendor_info.version2 - (void *)vendor_info.hdr);
       size += 4;
       fdt_update_reserved_memory(blob, "rockchip,vendor-storage-rm",
                      (u64)start,
                      (u64)size);
   }
}
 
/*
 * @id: item id, first 4 id is occupied:
 *    VENDOR_SN_ID
 *    VENDOR_WIFI_MAC_ID
 *    VENDOR_LAN_MAC_ID
 *    VENDOR_BLUETOOTH_ID
 * @pbuf: read data buffer;
 * @size: read bytes;
 *
 * return: bytes equal to @size is success, other fail;
 */
int vendor_storage_read(u16 id, void *pbuf, u16 size)
{
   int ret = 0;
   u32 i;
   u16 offset;
   struct vendor_item *item;
 
   /* init vendor storage */
   if (!bootdev_type) {
       ret = vendor_storage_init();
       if (ret < 0)
           return ret;
   }
 
   item = vendor_info.item;
   for (i = 0; i < vendor_info.hdr->item_num; i++) {
       if ((item + i)->id == id) {
           debug("[Vendor INFO]:Find the matching item, id=%d\n", id);
           /* Correct the size value */
           if (size > (item + i)->size)
               size = (item + i)->size;
           offset = (item + i)->offset;
           memcpy(pbuf, (vendor_info.data + offset), size);
           return size;
       }
   }
   debug("[Vendor ERROR]:No matching item, id=%d\n", id);
 
   return -EINVAL;
}
 
/*
 * @id: item id, first 4 id is occupied:
 *    VENDOR_SN_ID
 *    VENDOR_WIFI_MAC_ID
 *    VENDOR_LAN_MAC_ID
 *    VENDOR_BLUETOOTH_ID
 * @pbuf: write data buffer;
 * @size: write bytes;
 *
 * return: bytes equal to @size is success, other fail;
 */
int vendor_storage_write(u16 id, void *pbuf, u16 size)
{
   int cnt, ret = 0;
   u32 i, next_index, align_size;
   struct vendor_item *item;
   u16 part_size, max_item_num, offset, part_num;
 
   /* init vendor storage */
   if (!bootdev_type) {
       ret = vendor_storage_init();
       if (ret < 0)
           return ret;
   }
 
   switch (bootdev_type) {
   case IF_TYPE_MMC:
       part_size = EMMC_VENDOR_PART_BLKS;
       max_item_num = EMMC_VENDOR_ITEM_NUM;
       part_num = VENDOR_PART_NUM;
       break;
   case IF_TYPE_RKNAND:
   case IF_TYPE_SPINAND:
       part_size = NAND_VENDOR_PART_BLKS;
       max_item_num = NAND_VENDOR_ITEM_NUM;
       part_num = NAND_VENDOR_PART_NUM;
       break;
   case IF_TYPE_SPINOR:
       part_size = FLASH_VENDOR_PART_BLKS;
       max_item_num = FLASH_VENDOR_ITEM_NUM;
       part_num = VENDOR_PART_NUM;
       break;
#ifdef CONFIG_MTD_BLK
   case IF_TYPE_MTD:
       part_size = FLASH_VENDOR_PART_BLKS;
       max_item_num = FLASH_VENDOR_ITEM_NUM;
       part_num = MTD_VENDOR_PART_NUM;
       break;
#endif
   default:
       ret = -ENODEV;
       break;
   }
   /* Invalid bootdev? */
   if (ret < 0)
       return ret;
 
   next_index = vendor_info.hdr->next_index;
   /* algin to 64 bytes*/
   align_size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
   if (size > align_size)
       return -EINVAL;
 
   item = vendor_info.item;
   /* If item already exist, update the item data */
   for (i = 0; i < vendor_info.hdr->item_num; i++) {
       if ((item + i)->id == id) {
           debug("[Vendor INFO]:Find the matching item, id=%d\n", id);
           offset = (item + i)->offset;
           memcpy((vendor_info.data + offset), pbuf, size);
           (item + i)->size = size;
           vendor_info.hdr->version++;
           *(vendor_info.version2) = vendor_info.hdr->version;
           vendor_info.hdr->next_index++;
           if (vendor_info.hdr->next_index >= part_num)
               vendor_info.hdr->next_index = 0;
           cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1);
           return (cnt == part_size) ? size : -EIO;
       }
   }
   /*
    * If item does not exist, and free size is enough,
    * creat a new one
    */
   if ((vendor_info.hdr->item_num < max_item_num) &&
       (vendor_info.hdr->free_size >= align_size)) {
       debug("[Vendor INFO]:Create new Item, id=%d\n", id);
       item = vendor_info.item + vendor_info.hdr->item_num;
       item->id = id;
       item->offset = vendor_info.hdr->free_offset;
       item->size = size;
 
       vendor_info.hdr->free_offset += align_size;
       vendor_info.hdr->free_size -= align_size;
       memcpy((vendor_info.data + item->offset), pbuf, size);
       vendor_info.hdr->item_num++;
       vendor_info.hdr->version++;
       vendor_info.hdr->next_index++;
       *(vendor_info.version2) = vendor_info.hdr->version;
       if (vendor_info.hdr->next_index >= part_num)
           vendor_info.hdr->next_index = 0;
 
       cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1);
       return (cnt == part_size) ? size : -EIO;
   }
   debug("[Vendor ERROR]:Vendor has no space left!\n");
 
   return -ENOMEM;
}
 
/**********************************************************/
/*              vendor API uinit test                      */
/**********************************************************/
/* Reset the vendor storage space to the initial state */
static void vendor_test_reset(void)
{
   u16 i, part_size, part_num;
   u32 size;
 
   switch (bootdev_type) {
   case IF_TYPE_MMC:
       size = EMMC_VENDOR_INFO_SIZE;
       part_size = EMMC_VENDOR_PART_BLKS;
       part_num = VENDOR_PART_NUM;
       break;
   case IF_TYPE_RKNAND:
   case IF_TYPE_SPINAND:
       size = NAND_VENDOR_INFO_SIZE;
       part_size = NAND_VENDOR_PART_BLKS;
       part_num = NAND_VENDOR_PART_NUM;
       break;
   case IF_TYPE_SPINOR:
       size = FLASH_VENDOR_INFO_SIZE;
       part_size = FLASH_VENDOR_PART_BLKS;
       part_num = VENDOR_PART_NUM;
       break;
   default:
       size = 0;
       part_size = 0;
       break;
   }
   /* Invalid bootdev? */
   if (!size)
       return;
 
   memset((u8 *)vendor_info.hdr, 0, size);
   vendor_info.hdr->version = 1;
   vendor_info.hdr->tag = VENDOR_TAG;
   /* data field length */
   vendor_info.hdr->free_size = (unsigned long)vendor_info.hash -
                    (unsigned long)vendor_info.data;
   *(vendor_info.version2) = vendor_info.hdr->version;
   /* write to flash. */
   for (i = 0; i < part_num; i++)
       vendor_ops((u8 *)vendor_info.hdr, part_size * i, part_size, 1);
}
 
/*
 * A total of four tests
 * 1.All items test.
 * 2.Overrides the maximum number of items test.
 * 3.Single Item memory overflow test.
 * 4.Total memory overflow test.
 */
int vendor_storage_test(void)
{
   u16 id, size, j, item_num;
   u32 total_size;
   u8 *buffer = NULL;
   int ret = 0;
 
   if (!bootdev_type) {
       ret = vendor_storage_init();
       if (ret) {
           printf("%s: vendor storage init failed, ret=%d\n",
                  __func__, ret);
           return ret;
       }
   }
 
   /*
    * Calculate the maximum number of items and the maximum
    * allocable memory for each item.
    */
   switch (bootdev_type) {
   case IF_TYPE_MMC:
       item_num = EMMC_VENDOR_ITEM_NUM;
       total_size = (unsigned long)vendor_info.hash -
                (unsigned long)vendor_info.data;
       size = total_size / item_num;
       break;
   case IF_TYPE_RKNAND:
   case IF_TYPE_SPINAND:
       item_num = NAND_VENDOR_ITEM_NUM;
       total_size = (unsigned long)vendor_info.hash -
                (unsigned long)vendor_info.data;
       size = total_size / item_num;
       break;
   case IF_TYPE_SPINOR:
   case IF_TYPE_MTD:
       item_num = FLASH_VENDOR_ITEM_NUM;
       total_size = (unsigned long)vendor_info.hash -
                (unsigned long)vendor_info.data;
       size = total_size / item_num;
       break;
   default:
       item_num = 0;
       total_size = 0;
       size = 0;
       break;
   }
   /* Invalid bootdev? */
   if (!total_size)
       return -ENODEV;
   /* 64 bytes are aligned and rounded down */
   if (size > 64)
       size = (size / 64) * 64;
   /* malloc memory */
   buffer = (u8 *)malloc(size);
   if (!buffer) {
       printf("[Vendor Test]:Malloc failed(size=%d)!\n", size);
       return -ENOMEM;
   }
   printf("[Vendor Test]:Test Start...\n");
   printf("[Vendor Test]:Before Test, Vendor Resetting.\n");
   if (bootdev_type != IF_TYPE_MTD)
       vendor_test_reset();
 
   /* FIRST TEST: test all items can be used correctly */
   printf("[Vendor Test]:<All Items Used> Test Start...\n");
   printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size);
   /*
    * Write data, then read the data, and compare the
    * data consistency
    */
   for (id = 0; id < item_num; id++) {
       memset(buffer, id, size);
       ret = vendor_storage_write(id, buffer, size);
       if (ret < 0) {
           printf("[Vendor Test]:vendor write failed(id=%d)!\n", id);
           free(buffer);
           return ret;
       }
   }
   /* Read data */
   for (id = 0; id < item_num; id++) {
       memset(buffer, 0, size);
       ret = vendor_storage_read(id, buffer, size);
       if (ret < 0) {
           printf("[Vendor Test]:vendor read failed(id=%d)!\n", id);
           free(buffer);
           return ret;
       }
       /* check data Correctness */
       for (j = 0; j < size; j++) {
           if (*(buffer + j) != id) {
               printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id);
               printf("the data content is:\n");
               print_buffer(0, buffer, 1, size, 16);
 
               free(buffer);
               return -1;
           }
       }
       debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
   }
   printf("[Vendor Test]:<All Items Used> Test End,States:OK\n");
 
   printf("[Vendor Test]:<All Items Used> re init,States:OK\n");
   ret = vendor_storage_init();
   /* Read data */
   for (id = 0; id < item_num; id++) {
       memset(buffer, 0, size);
       ret = vendor_storage_read(id, buffer, size);
       if (ret < 0) {
           printf("[Vendor Test]:vendor read failed(id=%d)!\n", id);
           free(buffer);
           return ret;
       }
       /* check data Correctness */
       for (j = 0; j < size; j++) {
           if (*(buffer + j) != id) {
               printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id);
               printf("the data content is:\n");
               print_buffer(0, buffer, 1, size, 16);
 
               free(buffer);
               return -1;
           }
       }
       debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
   }
   printf("[Vendor Test]:<All Items Used> Test End,States:OK\n");
#ifdef CONFIG_MTD_BLK
   if (bootdev_type == IF_TYPE_MTD)
       return 0;
#endif
   /*
    * SECOND TEST: Overrides the maximum number of items to see if the
    * return value matches the expectation
    */
   printf("[Vendor Test]:<Overflow Items Cnt> Test Start...\n");
   /* Any id value that was not used before */
   id = item_num;
   printf("[Vendor Test]:id=%d, size=%d.\n", id, size);
   ret = vendor_storage_write(id, buffer, size);
   if (ret == -ENOMEM)
       printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:OK\n");
   else
       printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:Failed\n");
 
   /* free buffer, remalloc later */
   free(buffer);
   buffer = NULL;
   /*
    * remalloc memory and recalculate size to test memory overflow
    * (1) item_num > 10: Memory is divided into 10 blocks,
    * 11th memory will overflow.
    * (2) 10 > item_num > 1: Memory is divided into item_num-1
    * blocks. item_num block, memory will overflow.
    * (3) item_num = 1: size = total_size + 512 Bytes, The first
    * block, memory will overflow.
    * The reason to do so is to minimize the size of the memory,
    * making malloc easier to perform successfully.
    */
   item_num = (item_num > 10) ? 10 : (item_num - 1);
   size = item_num ? (total_size / item_num) : (total_size + 512);
   size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN);
   /* Find item_num value that can make the memory overflow */
   for (id = 0; id <= item_num; id++) {
       if (((id + 1) * size) > total_size) {
           item_num = id;
           break;
       }
   }
   /* malloc */
   buffer = (u8 *)malloc(size);
   if (buffer == NULL) {
       printf("[Vendor Test]:Malloc failed(size=%d)!\n", size);
       return -ENOMEM;
   }
 
   /* THIRD TEST: Single Item memory overflow test */
   printf("[Vendor Test]:<Single Item Memory Overflow> Test Start...\n");
   /* The value can be arbitrary */
   memset(buffer, 'a', size);
   /* Any id value that was used before */
   id = 0;
   printf("[Vendor Test]:id=%d, size=%d.\n", id, size);
   ret = vendor_storage_write(id, buffer, size);
   if (ret == size)
       printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:OK\n");
   else
       printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:Failed\n");
 
   /* FORTH TEST: Total memory overflow test */
   printf("[Vendor Test]:<Total memory overflow> Test Start...\n");
   printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size);
 
   vendor_test_reset();
   for (id = 0; id < item_num; id++) {
       memset(buffer, id, size);
       ret = vendor_storage_write(id, buffer, size);
       if (ret < 0) {
           if ((id == item_num) && (ret == -ENOMEM)) {
               printf("[Vendor Test]:<Total memory overflow> Test End, States:OK\n");
               break;
           } else {
               printf("[Vendor Test]:<Total memory overflow> Test End, States:Failed\n");
               break;
           }
       }
       debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size);
   }
 
   /* Test end */
   printf("[Vendor Test]:After Test, Vendor Resetting...\n");
   vendor_test_reset();
   printf("[Vendor Test]:Test End.\n");
   free(buffer);
 
   return 0;
}