1 /* 2 * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <malloc.h> 9 #include <asm/arch/vendor.h> 10 #include <boot_rkimg.h> 11 #include <nand.h> 12 #include <part.h> 13 14 /* tag for vendor check */ 15 #define VENDOR_TAG 0x524B5644 16 /* The Vendor partition contains the number of Vendor blocks */ 17 #define MTD_VENDOR_PART_NUM 1 18 #define NAND_VENDOR_PART_NUM 2 19 #define VENDOR_PART_NUM 4 20 /* align to 64 bytes */ 21 #define VENDOR_BTYE_ALIGN 0x3F 22 #define VENDOR_BLOCK_SIZE 512 23 24 /* --- Emmc define --- */ 25 /* Starting address of the Vendor in memory. */ 26 #define EMMC_VENDOR_PART_OFFSET (1024 * 7) 27 /* 28 * The number of memory blocks used by each 29 * Vendor structure(128 * 512B = 64KB) 30 */ 31 #define EMMC_VENDOR_PART_BLKS 128 32 /* The maximum number of items in each Vendor block */ 33 #define EMMC_VENDOR_ITEM_NUM 126 34 35 /* --- Spi Nand/SLC/MLC large capacity case define --- */ 36 /* The Vendor partition contains the number of Vendor blocks */ 37 #define NAND_VENDOR_PART_OFFSET 0 38 /* 39 * The number of memory blocks used by each 40 * Vendor structure(8 * 512B = 4KB) 41 */ 42 #define NAND_VENDOR_PART_BLKS 128 43 /* The maximum number of items in each Vendor block */ 44 #define NAND_VENDOR_ITEM_NUM 126 45 46 /* --- Spi/Spi Nand/SLC/MLC small capacity case define --- */ 47 /* The Vendor partition contains the number of Vendor blocks */ 48 #define FLASH_VENDOR_PART_OFFSET 8 49 /* 50 * The number of memory blocks used by each 51 * Vendor structure(8 * 512B = 4KB) 52 */ 53 #define FLASH_VENDOR_PART_BLKS 8 54 /* The maximum number of items in each Vendor block */ 55 #define FLASH_VENDOR_ITEM_NUM 62 56 57 /* Vendor uinit test define */ 58 int vendor_storage_test(void); 59 60 struct vendor_hdr { 61 u32 tag; 62 u32 version; 63 u16 next_index; 64 u16 item_num; 65 u16 free_offset; /* Free space offset */ 66 u16 free_size; /* Free space size */ 67 }; 68 69 /* 70 * Different types of Flash vendor info are different. 71 * EMMC:EMMC_VENDOR_PART_BLKS * BLOCK_SIZE(512) = 64KB; 72 * Spi Nor/Spi Nand/SLC/MLC: FLASH_VENDOR_PART_BLKS * 73 * BLOCK_SIZE(512) = 4KB. 74 * hash: For future expansion. 75 * version2: Together with hdr->version, it is used to 76 * ensure the current Vendor block content integrity. 77 * (version2 == hdr->version):Data valid; 78 * (version2 != hdr->version):Data invalid. 79 */ 80 struct vendor_info { 81 struct vendor_hdr *hdr; 82 struct vendor_item *item; 83 u8 *data; 84 u32 *hash; 85 u32 *version2; 86 }; 87 88 struct mtd_flash_info { 89 u32 part_offset; 90 u32 part_size; 91 u32 blk_offset; 92 u32 page_offset; 93 u32 version; 94 u32 ops_size; 95 u32 blk_size; 96 }; 97 98 /* 99 * Calculate the offset of each field for emmc. 100 * Emmc vendor info size: 64KB 101 */ 102 #define EMMC_VENDOR_INFO_SIZE (EMMC_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE) 103 #define EMMC_VENDOR_DATA_OFFSET (sizeof(struct vendor_hdr) + EMMC_VENDOR_ITEM_NUM * sizeof(struct vendor_item)) 104 #define EMMC_VENDOR_HASH_OFFSET (EMMC_VENDOR_INFO_SIZE - 8) 105 #define EMMC_VENDOR_VERSION2_OFFSET (EMMC_VENDOR_INFO_SIZE - 4) 106 107 /* 108 * Calculate the offset of each field for spi nand/slc/mlc large capacity case. 109 * Flash vendor info size: 4KB 110 */ 111 #define NAND_VENDOR_INFO_SIZE (NAND_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE) 112 #define NAND_VENDOR_DATA_OFFSET (sizeof(struct vendor_hdr) + NAND_VENDOR_ITEM_NUM * sizeof(struct vendor_item)) 113 #define NAND_VENDOR_HASH_OFFSET (NAND_VENDOR_INFO_SIZE - 8) 114 #define NAND_VENDOR_VERSION2_OFFSET (NAND_VENDOR_INFO_SIZE - 4) 115 116 /* 117 * Calculate the offset of each field for spi nor/spi nand/slc/mlc large small capacity case. 118 * Flash vendor info size: 4KB 119 */ 120 #define FLASH_VENDOR_INFO_SIZE (FLASH_VENDOR_PART_BLKS * VENDOR_BLOCK_SIZE) 121 #define FLASH_VENDOR_DATA_OFFSET (sizeof(struct vendor_hdr) + FLASH_VENDOR_ITEM_NUM * sizeof(struct vendor_item)) 122 #define FLASH_VENDOR_HASH_OFFSET (FLASH_VENDOR_INFO_SIZE - 8) 123 #define FLASH_VENDOR_VERSION2_OFFSET (FLASH_VENDOR_INFO_SIZE - 4) 124 125 /* vendor info */ 126 static struct vendor_info vendor_info; 127 /* The storage type of the device */ 128 static int bootdev_type; 129 130 #ifdef CONFIG_MTD_BLK 131 static struct mtd_flash_info s_flash_info; 132 static const char *vendor_mtd_name = "vnvm"; 133 #endif 134 135 /* vendor private read write ops*/ 136 static int (*_flash_read)(struct blk_desc *dev_desc, 137 u32 sec, 138 u32 n_sec, 139 void *buffer); 140 static int (*_flash_write)(struct blk_desc *dev_desc, 141 u32 sec, 142 u32 n_sec, 143 void *buffer); 144 145 int flash_vendor_dev_ops_register(int (*read)(struct blk_desc *dev_desc, 146 u32 sec, 147 u32 n_sec, 148 void *p_data), 149 int (*write)(struct blk_desc *dev_desc, 150 u32 sec, 151 u32 n_sec, 152 void *p_data)) 153 { 154 if (!_flash_read) { 155 _flash_read = read; 156 _flash_write = write; 157 return 0; 158 } 159 160 return -EPERM; 161 } 162 163 #ifdef CONFIG_MTD_BLK 164 static int mtd_vendor_storage_init(struct blk_desc *dev_desc) 165 { 166 struct mtd_info *mtd = (struct mtd_info *)dev_desc->bdev->priv; 167 disk_partition_t vnvm_part_info; 168 void *buf = vendor_info.hdr; 169 int ret, offset; 170 int part_num, bad_block_size; 171 172 memset(&vnvm_part_info, 0x0, sizeof(vnvm_part_info)); 173 part_num = part_get_info_by_name(dev_desc, vendor_mtd_name, &vnvm_part_info); 174 if (part_num < 0) 175 return -EIO; 176 177 s_flash_info.part_offset = (u32)vnvm_part_info.start; 178 s_flash_info.part_size = (u32)vnvm_part_info.size; 179 s_flash_info.page_offset = 0; 180 s_flash_info.blk_offset = 0; 181 s_flash_info.version = 0; 182 /* SPI Nor unified to Support 64KB erase block */ 183 if (dev_desc->devnum == BLK_MTD_SPI_NOR) 184 s_flash_info.blk_size = 0x80; 185 else 186 s_flash_info.blk_size = mtd->erasesize >> 9; 187 s_flash_info.ops_size = roundup(FLASH_VENDOR_INFO_SIZE, mtd->writesize) >> 9; 188 189 /* scan bad block and calculate the real size can be used */ 190 bad_block_size = 0; 191 for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) { 192 if (mtd_block_isbad(mtd, (s_flash_info.part_offset + offset) << 9)) 193 bad_block_size += s_flash_info.blk_size; 194 } 195 s_flash_info.part_size -= bad_block_size; 196 197 for (offset = 0; offset < s_flash_info.part_size; offset += s_flash_info.blk_size) { 198 ret = blk_dread(dev_desc, s_flash_info.part_offset + offset, 199 FLASH_VENDOR_INFO_SIZE >> 9, 200 (u8 *)buf); 201 debug("%s: read %x version = %x\n", __func__, 202 s_flash_info.part_offset + offset, 203 vendor_info.hdr->version); 204 if (ret == (FLASH_VENDOR_INFO_SIZE >> 9) && vendor_info.hdr->tag == VENDOR_TAG && 205 vendor_info.hdr->version == *vendor_info.version2) { 206 if (vendor_info.hdr->version > s_flash_info.version) { 207 s_flash_info.version = vendor_info.hdr->version; 208 s_flash_info.blk_offset = offset; 209 } 210 } 211 } 212 213 debug("%s: s_flash_info.version = %x %x\n", __func__, s_flash_info.version, s_flash_info.blk_offset); 214 if (s_flash_info.version) { 215 for (offset = s_flash_info.blk_size - s_flash_info.ops_size; 216 offset >= 0; 217 offset -= s_flash_info.ops_size) { 218 ret = blk_dread(dev_desc, s_flash_info.part_offset + 219 s_flash_info.blk_offset + offset, 220 1, 221 (u8 *)buf); 222 223 /* the page is not programmed */ 224 if (ret == 1 && vendor_info.hdr->tag == 0xFFFFFFFF) 225 continue; 226 227 /* point to the next free page */ 228 if (s_flash_info.page_offset < offset) 229 s_flash_info.page_offset = offset + s_flash_info.ops_size; 230 231 if (ret != 1 || vendor_info.hdr->tag != VENDOR_TAG) 232 continue; 233 ret = blk_dread(dev_desc, s_flash_info.part_offset + 234 s_flash_info.blk_offset + offset, 235 FLASH_VENDOR_INFO_SIZE >> 9, 236 (u8 *)buf); 237 debug("%s: read %x version = %x\n", __func__, 238 s_flash_info.part_offset + s_flash_info.blk_offset + offset, 239 vendor_info.hdr->version); 240 241 if (ret == (FLASH_VENDOR_INFO_SIZE >> 9) && vendor_info.hdr->tag == VENDOR_TAG && 242 vendor_info.hdr->version == *vendor_info.version2) { 243 s_flash_info.version = vendor_info.hdr->version; 244 break; 245 } 246 } 247 } else { 248 memset((u8 *)vendor_info.hdr, 0, FLASH_VENDOR_INFO_SIZE); 249 vendor_info.hdr->version = 1; 250 vendor_info.hdr->tag = VENDOR_TAG; 251 vendor_info.hdr->free_size = 252 ((u32)(size_t)vendor_info.hash 253 - (u32)(size_t)vendor_info.data); 254 *vendor_info.version2 = vendor_info.hdr->version; 255 } 256 257 return 0; 258 } 259 260 static int mtd_vendor_write(struct blk_desc *dev_desc, 261 u32 sec, 262 u32 n_sec, 263 void *buf) 264 { 265 int ret, count = 0, err = 0; 266 267 re_write: 268 debug("[Vendor INFO]:%s page_offset=0x%x count = %x\n", __func__, s_flash_info.part_offset + 269 s_flash_info.blk_offset + s_flash_info.page_offset, count); 270 if (s_flash_info.page_offset >= s_flash_info.blk_size) { 271 s_flash_info.blk_offset += s_flash_info.blk_size; 272 if (s_flash_info.blk_offset >= s_flash_info.part_size) 273 s_flash_info.blk_offset = 0; 274 s_flash_info.page_offset = 0; 275 } 276 277 dev_desc->op_flag |= BLK_MTD_CONT_WRITE; 278 ret = blk_dwrite(dev_desc, s_flash_info.part_offset + 279 s_flash_info.blk_offset + s_flash_info.page_offset, 280 FLASH_VENDOR_INFO_SIZE >> 9, 281 (u8 *)buf); 282 dev_desc->op_flag &= ~(BLK_MTD_CONT_WRITE); 283 284 s_flash_info.page_offset += s_flash_info.ops_size; 285 if (ret != (FLASH_VENDOR_INFO_SIZE >> 9)) { 286 err++; 287 if (err > 3) 288 return -EIO; 289 goto re_write; 290 } 291 292 count++; 293 /* write 2 copies for reliability */ 294 if (count < 2) 295 goto re_write; 296 297 return ret; 298 } 299 #endif 300 301 /**********************************************************/ 302 /* vendor API implementation */ 303 /**********************************************************/ 304 static int vendor_ops(u8 *buffer, u32 addr, u32 n_sec, int write) 305 { 306 struct blk_desc *dev_desc; 307 unsigned int lba = 0; 308 int ret = 0; 309 310 dev_desc = rockchip_get_bootdev(); 311 if (!dev_desc) { 312 printf("%s: dev_desc is NULL!\n", __func__); 313 return -ENODEV; 314 } 315 316 if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) { 317 dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR); 318 if (!dev_desc) { 319 printf("%s: dev_desc is NULL!\n", __func__); 320 return -ENODEV; 321 } 322 } 323 324 /* Get the offset address according to the device type */ 325 switch (dev_desc->if_type) { 326 case IF_TYPE_MMC: 327 /* 328 * The location of VendorStorage in Flash is shown in the 329 * following figure. The starting address of the VendorStorage 330 * partition offset is 3.5MB(EMMC_VENDOR_PART_OFFSET*BLOCK_SIZE(512)), 331 * and the partition size is 256KB. 332 * ---------------------------------------------------- 333 * | 3.5MB | VendorStorage | | 334 * ---------------------------------------------------- 335 */ 336 lba = EMMC_VENDOR_PART_OFFSET; 337 debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba); 338 break; 339 case IF_TYPE_RKNAND: 340 case IF_TYPE_SPINAND: 341 /* 342 * The location of VendorStorage in Flash is shown in the 343 * following figure. The starting address of the VendorStorage 344 * partition offset is 0KB in FTL vendor block, 345 * and the partition size is 128KB. 346 * ---------------------------------------------------- 347 * | VendorStorage | | 348 * ---------------------------------------------------- 349 */ 350 lba = NAND_VENDOR_PART_OFFSET; 351 debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba); 352 break; 353 case IF_TYPE_SPINOR: 354 /* 355 * The location of VendorStorage in Flash is shown in the 356 * following figure. The starting address of the VendorStorage 357 * partition offset is 4KB (FLASH_VENDOR_PART_OFFSET * BLOCK_SIZE), 358 * and the partition size is 16KB. 359 * ---------------------------------------------------- 360 * | 4KB | VendorStorage | | 361 * ---------------------------------------------------- 362 */ 363 lba = FLASH_VENDOR_PART_OFFSET; 364 debug("[Vendor INFO]:VendorStorage offset address=0x%x\n", lba); 365 break; 366 #ifdef CONFIG_MTD_BLK 367 case IF_TYPE_MTD: 368 /* 369 * The location of VendorStorage in NAND FLASH or SPI NAND partition "vnvm" 370 * is shown in the following figure. The partition size is at least 4 371 * NAND FLASH blocks. 372 * ---------------------------------------------------- 373 * | ..... | vnvm | ....... | 374 * ---------------------------------------------------- 375 */ 376 lba = 0; 377 break; 378 #endif 379 default: 380 printf("[Vendor ERROR]:Boot device type is invalid!\n"); 381 return -ENODEV; 382 } 383 if (write) { 384 if (_flash_write) 385 ret = _flash_write(dev_desc, lba + addr, n_sec, buffer); 386 else 387 ret = blk_dwrite(dev_desc, lba + addr, n_sec, buffer); 388 } else { 389 if (_flash_read) 390 ret = _flash_read(dev_desc, lba + addr, n_sec, buffer); 391 else 392 ret = blk_dread(dev_desc, lba + addr, n_sec, buffer); 393 } 394 395 debug("[Vendor INFO]:op=%s, ret=%d\n", write ? "write" : "read", ret); 396 397 return ret; 398 } 399 400 /* 401 * The VendorStorage partition is divided into four parts 402 * (vendor 0-3) and its structure is shown in the following figure. 403 * The init function is used to select the latest and valid vendor. 404 * 405 * |******************** FLASH ********************| 406 * ------------------------------------------------- 407 * | vendor0 | vendor1 | vendor2 | vendor3 | 408 * ------------------------------------------------- 409 * Notices: 410 * 1. "version" and "version2" are used to verify that the vendor 411 * is valid (equal is valid). 412 * 2. the "version" value is larger, indicating that the current 413 * verndor data is new. 414 */ 415 int vendor_storage_init(void) 416 { 417 int ret = 0; 418 int ret_size; 419 u8 *buffer; 420 u32 size, i; 421 u32 max_ver = 0; 422 u32 max_index = 0; 423 u16 data_offset, hash_offset, part_num; 424 u16 version2_offset, part_size; 425 struct blk_desc *dev_desc; 426 427 dev_desc = rockchip_get_bootdev(); 428 if (!dev_desc) { 429 printf("[Vendor ERROR]:Invalid boot device type(%d)\n", 430 bootdev_type); 431 return -ENODEV; 432 } 433 434 if (dev_desc->if_type == IF_TYPE_NVME || dev_desc->if_type == IF_TYPE_SCSI) { 435 dev_desc = blk_get_devnum_by_type(IF_TYPE_MTD, BLK_MTD_SPI_NOR); 436 if (!dev_desc) { 437 printf("%s: dev_desc is NULL!\n", __func__); 438 return -ENODEV; 439 } 440 } 441 442 switch (dev_desc->if_type) { 443 case IF_TYPE_MMC: 444 size = EMMC_VENDOR_INFO_SIZE; 445 part_size = EMMC_VENDOR_PART_BLKS; 446 data_offset = EMMC_VENDOR_DATA_OFFSET; 447 hash_offset = EMMC_VENDOR_HASH_OFFSET; 448 version2_offset = EMMC_VENDOR_VERSION2_OFFSET; 449 part_num = VENDOR_PART_NUM; 450 break; 451 case IF_TYPE_RKNAND: 452 case IF_TYPE_SPINAND: 453 size = NAND_VENDOR_INFO_SIZE; 454 part_size = NAND_VENDOR_PART_BLKS; 455 data_offset = NAND_VENDOR_DATA_OFFSET; 456 hash_offset = NAND_VENDOR_HASH_OFFSET; 457 version2_offset = NAND_VENDOR_VERSION2_OFFSET; 458 part_num = NAND_VENDOR_PART_NUM; 459 break; 460 case IF_TYPE_SPINOR: 461 size = FLASH_VENDOR_INFO_SIZE; 462 part_size = FLASH_VENDOR_PART_BLKS; 463 data_offset = FLASH_VENDOR_DATA_OFFSET; 464 hash_offset = FLASH_VENDOR_HASH_OFFSET; 465 version2_offset = FLASH_VENDOR_VERSION2_OFFSET; 466 part_num = VENDOR_PART_NUM; 467 break; 468 #ifdef CONFIG_MTD_BLK 469 case IF_TYPE_MTD: 470 size = FLASH_VENDOR_INFO_SIZE; 471 part_size = FLASH_VENDOR_PART_BLKS; 472 data_offset = FLASH_VENDOR_DATA_OFFSET; 473 hash_offset = FLASH_VENDOR_HASH_OFFSET; 474 version2_offset = FLASH_VENDOR_VERSION2_OFFSET; 475 part_num = MTD_VENDOR_PART_NUM; 476 _flash_write = mtd_vendor_write; 477 break; 478 #endif 479 default: 480 debug("[Vendor ERROR]:Boot device type is invalid!\n"); 481 ret = -ENODEV; 482 break; 483 } 484 /* Invalid bootdev type */ 485 if (ret) 486 return ret; 487 488 /* Initialize */ 489 bootdev_type = dev_desc->if_type; 490 491 /* Always use, no need to release */ 492 buffer = (u8 *)malloc(size); 493 if (!buffer) { 494 printf("[Vendor ERROR]:Malloc failed!\n"); 495 ret = -ENOMEM; 496 goto out; 497 } 498 /* Pointer initialization */ 499 vendor_info.hdr = (struct vendor_hdr *)buffer; 500 vendor_info.item = (struct vendor_item *)(buffer + sizeof(struct vendor_hdr)); 501 vendor_info.data = buffer + data_offset; 502 vendor_info.hash = (u32 *)(buffer + hash_offset); 503 vendor_info.version2 = (u32 *)(buffer + version2_offset); 504 505 #ifdef CONFIG_MTD_BLK 506 if (dev_desc->if_type == IF_TYPE_MTD) { 507 ret = mtd_vendor_storage_init(dev_desc); 508 goto out; 509 } 510 #endif 511 512 /* Find valid and up-to-date one from (vendor0 - vendor3) */ 513 for (i = 0; i < part_num; i++) { 514 ret_size = vendor_ops((u8 *)vendor_info.hdr, 515 part_size * i, part_size, 0); 516 if (ret_size != part_size) { 517 ret = -EIO; 518 goto out; 519 } 520 521 if ((vendor_info.hdr->tag == VENDOR_TAG) && 522 (*(vendor_info.version2) == vendor_info.hdr->version)) { 523 if (max_ver < vendor_info.hdr->version) { 524 max_index = i; 525 max_ver = vendor_info.hdr->version; 526 } 527 } 528 } 529 530 if (max_ver) { 531 debug("[Vendor INFO]:max_ver=%d, vendor_id=%d.\n", max_ver, max_index); 532 /* 533 * Keep vendor_info the same as the largest 534 * version of vendor 535 */ 536 if (max_index != (part_num - 1)) { 537 ret_size = vendor_ops((u8 *)vendor_info.hdr, 538 part_size * max_index, part_size, 0); 539 if (ret_size != part_size) { 540 ret = -EIO; 541 goto out; 542 } 543 } 544 } else { 545 debug("[Vendor INFO]:Reset vendor info...\n"); 546 memset((u8 *)vendor_info.hdr, 0, size); 547 vendor_info.hdr->version = 1; 548 vendor_info.hdr->tag = VENDOR_TAG; 549 /* data field length */ 550 vendor_info.hdr->free_size = 551 ((u32)(size_t)vendor_info.hash 552 - (u32)(size_t)vendor_info.data); 553 *(vendor_info.version2) = vendor_info.hdr->version; 554 } 555 debug("[Vendor INFO]:ret=%d.\n", ret); 556 557 out: 558 if (ret) 559 bootdev_type = 0; 560 561 return ret; 562 } 563 564 /* 565 * @id: item id, first 4 id is occupied: 566 * VENDOR_SN_ID 567 * VENDOR_WIFI_MAC_ID 568 * VENDOR_LAN_MAC_ID 569 * VENDOR_BLUETOOTH_ID 570 * @pbuf: read data buffer; 571 * @size: read bytes; 572 * 573 * return: bytes equal to @size is success, other fail; 574 */ 575 int vendor_storage_read(u16 id, void *pbuf, u16 size) 576 { 577 int ret = 0; 578 u32 i; 579 u16 offset; 580 struct vendor_item *item; 581 582 /* init vendor storage */ 583 if (!bootdev_type) { 584 ret = vendor_storage_init(); 585 if (ret < 0) 586 return ret; 587 } 588 589 item = vendor_info.item; 590 for (i = 0; i < vendor_info.hdr->item_num; i++) { 591 if ((item + i)->id == id) { 592 debug("[Vendor INFO]:Find the matching item, id=%d\n", id); 593 /* Correct the size value */ 594 if (size > (item + i)->size) 595 size = (item + i)->size; 596 offset = (item + i)->offset; 597 memcpy(pbuf, (vendor_info.data + offset), size); 598 return size; 599 } 600 } 601 debug("[Vendor ERROR]:No matching item, id=%d\n", id); 602 603 return -EINVAL; 604 } 605 606 /* 607 * @id: item id, first 4 id is occupied: 608 * VENDOR_SN_ID 609 * VENDOR_WIFI_MAC_ID 610 * VENDOR_LAN_MAC_ID 611 * VENDOR_BLUETOOTH_ID 612 * @pbuf: write data buffer; 613 * @size: write bytes; 614 * 615 * return: bytes equal to @size is success, other fail; 616 */ 617 int vendor_storage_write(u16 id, void *pbuf, u16 size) 618 { 619 int cnt, ret = 0; 620 u32 i, next_index, align_size; 621 struct vendor_item *item; 622 u16 part_size, max_item_num, offset, part_num; 623 624 /* init vendor storage */ 625 if (!bootdev_type) { 626 ret = vendor_storage_init(); 627 if (ret < 0) 628 return ret; 629 } 630 631 switch (bootdev_type) { 632 case IF_TYPE_MMC: 633 part_size = EMMC_VENDOR_PART_BLKS; 634 max_item_num = EMMC_VENDOR_ITEM_NUM; 635 part_num = VENDOR_PART_NUM; 636 break; 637 case IF_TYPE_RKNAND: 638 case IF_TYPE_SPINAND: 639 part_size = NAND_VENDOR_PART_BLKS; 640 max_item_num = NAND_VENDOR_ITEM_NUM; 641 part_num = NAND_VENDOR_PART_NUM; 642 break; 643 case IF_TYPE_SPINOR: 644 part_size = FLASH_VENDOR_PART_BLKS; 645 max_item_num = FLASH_VENDOR_ITEM_NUM; 646 part_num = VENDOR_PART_NUM; 647 break; 648 #ifdef CONFIG_MTD_BLK 649 case IF_TYPE_MTD: 650 part_size = FLASH_VENDOR_PART_BLKS; 651 max_item_num = FLASH_VENDOR_ITEM_NUM; 652 part_num = MTD_VENDOR_PART_NUM; 653 break; 654 #endif 655 default: 656 ret = -ENODEV; 657 break; 658 } 659 /* Invalid bootdev? */ 660 if (ret < 0) 661 return ret; 662 663 next_index = vendor_info.hdr->next_index; 664 /* algin to 64 bytes*/ 665 align_size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN); 666 if (size > align_size) 667 return -EINVAL; 668 669 item = vendor_info.item; 670 /* If item already exist, update the item data */ 671 for (i = 0; i < vendor_info.hdr->item_num; i++) { 672 if ((item + i)->id == id) { 673 debug("[Vendor INFO]:Find the matching item, id=%d\n", id); 674 offset = (item + i)->offset; 675 memcpy((vendor_info.data + offset), pbuf, size); 676 (item + i)->size = size; 677 vendor_info.hdr->version++; 678 *(vendor_info.version2) = vendor_info.hdr->version; 679 vendor_info.hdr->next_index++; 680 if (vendor_info.hdr->next_index >= part_num) 681 vendor_info.hdr->next_index = 0; 682 cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1); 683 return (cnt == part_size) ? size : -EIO; 684 } 685 } 686 /* 687 * If item does not exist, and free size is enough, 688 * creat a new one 689 */ 690 if ((vendor_info.hdr->item_num < max_item_num) && 691 (vendor_info.hdr->free_size >= align_size)) { 692 debug("[Vendor INFO]:Create new Item, id=%d\n", id); 693 item = vendor_info.item + vendor_info.hdr->item_num; 694 item->id = id; 695 item->offset = vendor_info.hdr->free_offset; 696 item->size = size; 697 698 vendor_info.hdr->free_offset += align_size; 699 vendor_info.hdr->free_size -= align_size; 700 memcpy((vendor_info.data + item->offset), pbuf, size); 701 vendor_info.hdr->item_num++; 702 vendor_info.hdr->version++; 703 vendor_info.hdr->next_index++; 704 *(vendor_info.version2) = vendor_info.hdr->version; 705 if (vendor_info.hdr->next_index >= part_num) 706 vendor_info.hdr->next_index = 0; 707 708 cnt = vendor_ops((u8 *)vendor_info.hdr, part_size * next_index, part_size, 1); 709 return (cnt == part_size) ? size : -EIO; 710 } 711 debug("[Vendor ERROR]:Vendor has no space left!\n"); 712 713 return -ENOMEM; 714 } 715 716 /**********************************************************/ 717 /* vendor API uinit test */ 718 /**********************************************************/ 719 /* Reset the vendor storage space to the initial state */ 720 static void vendor_test_reset(void) 721 { 722 u16 i, part_size, part_num; 723 u32 size; 724 725 switch (bootdev_type) { 726 case IF_TYPE_MMC: 727 size = EMMC_VENDOR_INFO_SIZE; 728 part_size = EMMC_VENDOR_PART_BLKS; 729 part_num = VENDOR_PART_NUM; 730 break; 731 case IF_TYPE_RKNAND: 732 case IF_TYPE_SPINAND: 733 size = NAND_VENDOR_INFO_SIZE; 734 part_size = NAND_VENDOR_PART_BLKS; 735 part_num = NAND_VENDOR_PART_NUM; 736 break; 737 case IF_TYPE_SPINOR: 738 size = FLASH_VENDOR_INFO_SIZE; 739 part_size = FLASH_VENDOR_PART_BLKS; 740 part_num = VENDOR_PART_NUM; 741 break; 742 default: 743 size = 0; 744 part_size = 0; 745 break; 746 } 747 /* Invalid bootdev? */ 748 if (!size) 749 return; 750 751 memset((u8 *)vendor_info.hdr, 0, size); 752 vendor_info.hdr->version = 1; 753 vendor_info.hdr->tag = VENDOR_TAG; 754 /* data field length */ 755 vendor_info.hdr->free_size = (unsigned long)vendor_info.hash - 756 (unsigned long)vendor_info.data; 757 *(vendor_info.version2) = vendor_info.hdr->version; 758 /* write to flash. */ 759 for (i = 0; i < part_num; i++) 760 vendor_ops((u8 *)vendor_info.hdr, part_size * i, part_size, 1); 761 } 762 763 /* 764 * A total of four tests 765 * 1.All items test. 766 * 2.Overrides the maximum number of items test. 767 * 3.Single Item memory overflow test. 768 * 4.Total memory overflow test. 769 */ 770 int vendor_storage_test(void) 771 { 772 u16 id, size, j, item_num; 773 u32 total_size; 774 u8 *buffer = NULL; 775 int ret = 0; 776 777 if (!bootdev_type) { 778 ret = vendor_storage_init(); 779 if (ret) { 780 printf("%s: vendor storage init failed, ret=%d\n", 781 __func__, ret); 782 return ret; 783 } 784 } 785 786 /* 787 * Calculate the maximum number of items and the maximum 788 * allocable memory for each item. 789 */ 790 switch (bootdev_type) { 791 case IF_TYPE_MMC: 792 item_num = EMMC_VENDOR_ITEM_NUM; 793 total_size = (unsigned long)vendor_info.hash - 794 (unsigned long)vendor_info.data; 795 size = total_size / item_num; 796 break; 797 case IF_TYPE_RKNAND: 798 case IF_TYPE_SPINAND: 799 item_num = NAND_VENDOR_ITEM_NUM; 800 total_size = (unsigned long)vendor_info.hash - 801 (unsigned long)vendor_info.data; 802 size = total_size / item_num; 803 break; 804 case IF_TYPE_SPINOR: 805 case IF_TYPE_MTD: 806 item_num = FLASH_VENDOR_ITEM_NUM; 807 total_size = (unsigned long)vendor_info.hash - 808 (unsigned long)vendor_info.data; 809 size = total_size / item_num; 810 break; 811 default: 812 item_num = 0; 813 total_size = 0; 814 size = 0; 815 break; 816 } 817 /* Invalid bootdev? */ 818 if (!total_size) 819 return -ENODEV; 820 /* 64 bytes are aligned and rounded down */ 821 if (size > 64) 822 size = (size / 64) * 64; 823 /* malloc memory */ 824 buffer = (u8 *)malloc(size); 825 if (!buffer) { 826 printf("[Vendor Test]:Malloc failed(size=%d)!\n", size); 827 return -ENOMEM; 828 } 829 printf("[Vendor Test]:Test Start...\n"); 830 printf("[Vendor Test]:Before Test, Vendor Resetting.\n"); 831 if (bootdev_type != IF_TYPE_MTD) 832 vendor_test_reset(); 833 834 /* FIRST TEST: test all items can be used correctly */ 835 printf("[Vendor Test]:<All Items Used> Test Start...\n"); 836 printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size); 837 /* 838 * Write data, then read the data, and compare the 839 * data consistency 840 */ 841 for (id = 0; id < item_num; id++) { 842 memset(buffer, id, size); 843 ret = vendor_storage_write(id, buffer, size); 844 if (ret < 0) { 845 printf("[Vendor Test]:vendor write failed(id=%d)!\n", id); 846 free(buffer); 847 return ret; 848 } 849 } 850 /* Read data */ 851 for (id = 0; id < item_num; id++) { 852 memset(buffer, 0, size); 853 ret = vendor_storage_read(id, buffer, size); 854 if (ret < 0) { 855 printf("[Vendor Test]:vendor read failed(id=%d)!\n", id); 856 free(buffer); 857 return ret; 858 } 859 /* check data Correctness */ 860 for (j = 0; j < size; j++) { 861 if (*(buffer + j) != id) { 862 printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id); 863 printf("the data content is:\n"); 864 print_buffer(0, buffer, 1, size, 16); 865 866 free(buffer); 867 return -1; 868 } 869 } 870 debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size); 871 } 872 printf("[Vendor Test]:<All Items Used> Test End,States:OK\n"); 873 874 printf("[Vendor Test]:<All Items Used> re init,States:OK\n"); 875 ret = vendor_storage_init(); 876 /* Read data */ 877 for (id = 0; id < item_num; id++) { 878 memset(buffer, 0, size); 879 ret = vendor_storage_read(id, buffer, size); 880 if (ret < 0) { 881 printf("[Vendor Test]:vendor read failed(id=%d)!\n", id); 882 free(buffer); 883 return ret; 884 } 885 /* check data Correctness */ 886 for (j = 0; j < size; j++) { 887 if (*(buffer + j) != id) { 888 printf("[Vendor Test]:Unexpected error occurs(id=%d)\n", id); 889 printf("the data content is:\n"); 890 print_buffer(0, buffer, 1, size, 16); 891 892 free(buffer); 893 return -1; 894 } 895 } 896 debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size); 897 } 898 printf("[Vendor Test]:<All Items Used> Test End,States:OK\n"); 899 #ifdef CONFIG_MTD_BLK 900 if (bootdev_type == IF_TYPE_MTD) 901 return 0; 902 #endif 903 /* 904 * SECOND TEST: Overrides the maximum number of items to see if the 905 * return value matches the expectation 906 */ 907 printf("[Vendor Test]:<Overflow Items Cnt> Test Start...\n"); 908 /* Any id value that was not used before */ 909 id = item_num; 910 printf("[Vendor Test]:id=%d, size=%d.\n", id, size); 911 ret = vendor_storage_write(id, buffer, size); 912 if (ret == -ENOMEM) 913 printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:OK\n"); 914 else 915 printf("[Vendor Test]:<Overflow Items Cnt> Test End,States:Failed\n"); 916 917 /* free buffer, remalloc later */ 918 free(buffer); 919 buffer = NULL; 920 /* 921 * remalloc memory and recalculate size to test memory overflow 922 * (1) item_num > 10: Memory is divided into 10 blocks, 923 * 11th memory will overflow. 924 * (2) 10 > item_num > 1: Memory is divided into item_num-1 925 * blocks. item_num block, memory will overflow. 926 * (3) item_num = 1: size = total_size + 512 Bytes, The first 927 * block, memory will overflow. 928 * The reason to do so is to minimize the size of the memory, 929 * making malloc easier to perform successfully. 930 */ 931 item_num = (item_num > 10) ? 10 : (item_num - 1); 932 size = item_num ? (total_size / item_num) : (total_size + 512); 933 size = (size + VENDOR_BTYE_ALIGN) & (~VENDOR_BTYE_ALIGN); 934 /* Find item_num value that can make the memory overflow */ 935 for (id = 0; id <= item_num; id++) { 936 if (((id + 1) * size) > total_size) { 937 item_num = id; 938 break; 939 } 940 } 941 /* malloc */ 942 buffer = (u8 *)malloc(size); 943 if (buffer == NULL) { 944 printf("[Vendor Test]:Malloc failed(size=%d)!\n", size); 945 return -ENOMEM; 946 } 947 948 /* THIRD TEST: Single Item memory overflow test */ 949 printf("[Vendor Test]:<Single Item Memory Overflow> Test Start...\n"); 950 /* The value can be arbitrary */ 951 memset(buffer, 'a', size); 952 /* Any id value that was used before */ 953 id = 0; 954 printf("[Vendor Test]:id=%d, size=%d.\n", id, size); 955 ret = vendor_storage_write(id, buffer, size); 956 if (ret == size) 957 printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:OK\n"); 958 else 959 printf("[Vendor Test]:<Single Item Memory Overflow> Test End, States:Failed\n"); 960 961 /* FORTH TEST: Total memory overflow test */ 962 printf("[Vendor Test]:<Total memory overflow> Test Start...\n"); 963 printf("[Vendor Test]:item_num=%d, size=%d.\n", item_num, size); 964 965 vendor_test_reset(); 966 for (id = 0; id < item_num; id++) { 967 memset(buffer, id, size); 968 ret = vendor_storage_write(id, buffer, size); 969 if (ret < 0) { 970 if ((id == item_num) && (ret == -ENOMEM)) { 971 printf("[Vendor Test]:<Total memory overflow> Test End, States:OK\n"); 972 break; 973 } else { 974 printf("[Vendor Test]:<Total memory overflow> Test End, States:Failed\n"); 975 break; 976 } 977 } 978 debug("\t#id=%03d success,data=0x%02x,size=%d.\n", id, *buffer, size); 979 } 980 981 /* Test end */ 982 printf("[Vendor Test]:After Test, Vendor Resetting...\n"); 983 vendor_test_reset(); 984 printf("[Vendor Test]:Test End.\n"); 985 free(buffer); 986 987 return 0; 988 } 989