1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 #include <common.h> 7 #include <adc.h> 8 #include <asm/io.h> 9 #include <malloc.h> 10 #include <sysmem.h> 11 #include <linux/list.h> 12 #include <asm/arch/resource_img.h> 13 #include <boot_rkimg.h> 14 #include <dm/ofnode.h> 15 #ifdef CONFIG_ANDROID_AB 16 #include <android_avb/libavb_ab.h> 17 #include <android_avb/rk_avb_ops_user.h> 18 #endif 19 #ifdef CONFIG_ANDROID_BOOT_IMAGE 20 #include <android_bootloader.h> 21 #include <android_image.h> 22 #endif 23 24 DECLARE_GLOBAL_DATA_PTR; 25 26 #define PART_RESOURCE "resource" 27 #define RESOURCE_MAGIC "RSCE" 28 #define RESOURCE_MAGIC_SIZE 4 29 #define RESOURCE_VERSION 0 30 #define CONTENT_VERSION 0 31 #define ENTRY_TAG "ENTR" 32 #define ENTRY_TAG_SIZE 4 33 #define MAX_FILE_NAME_LEN 256 34 35 /* 36 * resource image structure 37 * ---------------------------------------------- 38 * | | 39 * | header (1 block) | 40 * | | 41 * ---------------------------------------------| 42 * | | | 43 * | entry0 (1 block) | | 44 * | | | 45 * ------------------------ | 46 * | | | 47 * | entry1 (1 block) | contents (n blocks) | 48 * | | | 49 * ------------------------ | 50 * | ...... | | 51 * ------------------------ | 52 * | | | 53 * | entryn (1 block) | | 54 * | | | 55 * ---------------------------------------------- 56 * | | 57 * | file0 (x blocks) | 58 * | | 59 * ---------------------------------------------- 60 * | | 61 * | file1 (y blocks) | 62 * | | 63 * ---------------------------------------------- 64 * | ...... | 65 * |--------------------------------------------- 66 * | | 67 * | filen (z blocks) | 68 * | | 69 * ---------------------------------------------- 70 */ 71 72 /** 73 * struct resource_image_header 74 * 75 * @magic: should be "RSCE" 76 * @version: resource image version, current is 0 77 * @c_version: content version, current is 0 78 * @blks: the size of the header ( 1 block = 512 bytes) 79 * @c_offset: contents offset(by block) in the image 80 * @e_blks: the size(by block) of the entry in the contents 81 * @e_num: numbers of the entrys. 82 */ 83 84 struct resource_img_hdr { 85 char magic[4]; 86 uint16_t version; 87 uint16_t c_version; 88 uint8_t blks; 89 uint8_t c_offset; 90 uint8_t e_blks; 91 uint32_t e_nums; 92 }; 93 94 struct resource_entry { 95 char tag[4]; 96 char name[MAX_FILE_NAME_LEN]; 97 uint32_t f_offset; 98 uint32_t f_size; 99 }; 100 101 struct resource_file { 102 char name[MAX_FILE_NAME_LEN]; 103 uint32_t f_offset; 104 uint32_t f_size; 105 struct list_head link; 106 uint32_t rsce_base; /* Base addr of resource */ 107 }; 108 109 static LIST_HEAD(entrys_head); 110 111 static int resource_image_check_header(const struct resource_img_hdr *hdr) 112 { 113 int ret; 114 115 ret = memcmp(RESOURCE_MAGIC, hdr->magic, RESOURCE_MAGIC_SIZE); 116 if (ret) { 117 printf("bad resource image magic: %s\n", 118 hdr->magic ? hdr->magic : "none"); 119 ret = -EINVAL; 120 } 121 debug("resource image header:\n"); 122 debug("magic:%s\n", hdr->magic); 123 debug("version:%d\n", hdr->version); 124 debug("c_version:%d\n", hdr->c_version); 125 debug("blks:%d\n", hdr->blks); 126 debug("c_offset:%d\n", hdr->c_offset); 127 debug("e_blks:%d\n", hdr->e_blks); 128 debug("e_num:%d\n", hdr->e_nums); 129 130 return ret; 131 } 132 133 static int add_file_to_list(struct resource_entry *entry, int rsce_base) 134 { 135 struct resource_file *file; 136 137 if (memcmp(entry->tag, ENTRY_TAG, ENTRY_TAG_SIZE)) { 138 printf("invalid entry tag\n"); 139 return -ENOENT; 140 } 141 file = malloc(sizeof(*file)); 142 if (!file) { 143 printf("out of memory\n"); 144 return -ENOMEM; 145 } 146 strcpy(file->name, entry->name); 147 file->rsce_base = rsce_base; 148 file->f_offset = entry->f_offset; 149 file->f_size = entry->f_size; 150 list_add_tail(&file->link, &entrys_head); 151 debug("entry:%p %s offset:%d size:%d\n", 152 entry, file->name, file->f_offset, file->f_size); 153 154 return 0; 155 } 156 157 static int init_resource_list(struct resource_img_hdr *hdr) 158 { 159 struct resource_entry *entry; 160 void *content; 161 int size; 162 int ret; 163 int e_num; 164 int offset = 0; 165 int resource_found = 0; 166 struct blk_desc *dev_desc; 167 disk_partition_t part_info; 168 char *boot_partname = PART_BOOT; 169 170 /* 171 * Primary detect AOSP format image, try to get resource image from 172 * boot/recovery partition. If not, it's an RK format image and try 173 * to get from resource partition. 174 */ 175 #ifdef CONFIG_ANDROID_BOOT_IMAGE 176 struct andr_img_hdr *andr_hdr; 177 #endif 178 179 if (hdr) { 180 if (resource_image_check_header(hdr)) 181 return -EEXIST; 182 183 content = (void *)((char *)hdr 184 + (hdr->c_offset) * RK_BLK_SIZE); 185 for (e_num = 0; e_num < hdr->e_nums; e_num++) { 186 size = e_num * hdr->e_blks * RK_BLK_SIZE; 187 entry = (struct resource_entry *)(content + size); 188 add_file_to_list(entry, offset); 189 } 190 return 0; 191 } 192 193 dev_desc = rockchip_get_bootdev(); 194 if (!dev_desc) { 195 printf("%s: dev_desc is NULL!\n", __func__); 196 return -ENODEV; 197 } 198 hdr = memalign(ARCH_DMA_MINALIGN, RK_BLK_SIZE); 199 if (!hdr) { 200 printf("%s: out of memory!\n", __func__); 201 return -ENOMEM; 202 } 203 204 #ifdef CONFIG_ANDROID_BOOT_IMAGE 205 /* Get boot mode from misc */ 206 #ifndef CONFIG_ANDROID_AB 207 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 208 boot_partname = PART_RECOVERY; 209 #endif 210 211 /* Read boot/recovery and chenc if this is an AOSP img */ 212 #ifdef CONFIG_ANDROID_AB 213 char slot_suffix[3] = {0}; 214 215 if (rk_avb_get_current_slot(slot_suffix)) 216 goto out; 217 boot_partname = android_str_append(boot_partname, slot_suffix); 218 if (boot_partname == NULL) 219 goto out; 220 #endif 221 ret = part_get_info_by_name(dev_desc, boot_partname, &part_info); 222 if (ret < 0) { 223 printf("%s: failed to get %s part, ret=%d\n", 224 __func__, boot_partname, ret); 225 /* RKIMG can support part table without 'boot' */ 226 goto next; 227 } 228 229 /* 230 * Only read header and check magic, is a AOSP format image? 231 * If so, get resource image from second part. 232 */ 233 andr_hdr = (void *)hdr; 234 ret = blk_dread(dev_desc, part_info.start, 1, andr_hdr); 235 if (ret != 1) { 236 printf("%s: failed to read %s hdr, ret=%d\n", 237 __func__, part_info.name, ret); 238 goto out; 239 } 240 ret = android_image_check_header(andr_hdr); 241 if (!ret) { 242 debug("%s: Load resource from %s second pos\n", 243 __func__, part_info.name); 244 /* Read resource from second offset */ 245 offset = part_info.start * RK_BLK_SIZE; 246 offset += andr_hdr->page_size; 247 offset += ALIGN(andr_hdr->kernel_size, andr_hdr->page_size); 248 offset += ALIGN(andr_hdr->ramdisk_size, andr_hdr->page_size); 249 offset = offset / RK_BLK_SIZE; 250 251 resource_found = 1; 252 } 253 next: 254 #endif 255 /* 256 * If not found resource image in AOSP format images(boot/recovery part), 257 * try to read RK format images(resource part). 258 */ 259 if (!resource_found) { 260 debug("%s: Load resource from resource part\n", __func__); 261 /* Read resource from Rockchip Resource partition */ 262 boot_partname = PART_RESOURCE; 263 ret = part_get_info_by_name(dev_desc, boot_partname, &part_info); 264 if (ret < 0) { 265 printf("%s: failed to get resource part, ret=%d\n", 266 __func__, ret); 267 goto out; 268 } 269 offset = part_info.start; 270 } 271 272 /* Only read header and check magic */ 273 ret = blk_dread(dev_desc, offset, 1, hdr); 274 if (ret != 1) { 275 printf("%s: failed to read resource hdr, ret=%d\n", 276 __func__, ret); 277 goto out; 278 } 279 280 ret = resource_image_check_header(hdr); 281 if (ret < 0) 282 goto out; 283 284 content = memalign(ARCH_DMA_MINALIGN, 285 hdr->e_blks * hdr->e_nums * RK_BLK_SIZE); 286 if (!content) { 287 printf("%s: failed to alloc memory for content\n", __func__); 288 goto out; 289 } 290 291 /* Read all entries from resource image */ 292 ret = blk_dread(dev_desc, offset + hdr->c_offset, 293 hdr->e_blks * hdr->e_nums, content); 294 if (ret != (hdr->e_blks * hdr->e_nums)) { 295 printf("%s: failed to read resource entries, ret=%d\n", 296 __func__, ret); 297 goto err; 298 } 299 300 for (e_num = 0; e_num < hdr->e_nums; e_num++) { 301 size = e_num * hdr->e_blks * RK_BLK_SIZE; 302 entry = (struct resource_entry *)(content + size); 303 add_file_to_list(entry, offset); 304 } 305 306 printf("Load FDT from %s part\n", boot_partname); 307 err: 308 free(content); 309 out: 310 free(hdr); 311 312 return 0; 313 } 314 315 static struct resource_file *get_file_info(struct resource_img_hdr *hdr, 316 const char *name) 317 { 318 struct resource_file *file; 319 struct list_head *node; 320 321 if (list_empty(&entrys_head)) 322 init_resource_list(hdr); 323 324 list_for_each(node, &entrys_head) { 325 file = list_entry(node, struct resource_file, link); 326 if (!strcmp(file->name, name)) 327 return file; 328 } 329 330 return NULL; 331 } 332 333 int rockchip_get_resource_file_offset(void *resc_hdr, const char *name) 334 { 335 struct resource_file *file; 336 337 file = get_file_info(resc_hdr, name); 338 if (!file) 339 return -ENFILE; 340 341 return file->f_offset; 342 } 343 344 int rockchip_get_resource_file_size(void *resc_hdr, const char *name) 345 { 346 struct resource_file *file; 347 348 file = get_file_info(resc_hdr, name); 349 if (!file) 350 return -ENFILE; 351 352 return file->f_size; 353 } 354 355 /* 356 * read file from resource partition 357 * @buf: destination buf to store file data; 358 * @name: file name 359 * @offset: blocks offset in the file, 1 block = 512 bytes 360 * @len: the size(by bytes) of file to read. 361 */ 362 int rockchip_read_resource_file(void *buf, const char *name, 363 int offset, int len) 364 { 365 struct resource_file *file; 366 int ret = 0; 367 int blks; 368 struct blk_desc *dev_desc; 369 370 file = get_file_info(NULL, name); 371 if (!file) { 372 printf("Can't find file:%s\n", name); 373 return -ENOENT; 374 } 375 376 if (len <= 0 || len > file->f_size) 377 len = file->f_size; 378 blks = DIV_ROUND_UP(len, RK_BLK_SIZE); 379 dev_desc = rockchip_get_bootdev(); 380 if (!dev_desc) { 381 printf("%s: dev_desc is NULL!\n", __func__); 382 return -ENODEV; 383 } 384 ret = blk_dread(dev_desc, file->rsce_base + file->f_offset + offset, 385 blks, buf); 386 if (ret != blks) 387 ret = -EIO; 388 else 389 ret = len; 390 391 return ret; 392 } 393 394 #define is_digit(c) ((c) >= '0' && (c) <= '9') 395 #define is_abcd(c) ((c) >= 'a' && (c) <= 'd') 396 #define is_equal(c) ((c) == '=') 397 398 #define DTB_FILE "rk-kernel.dtb" 399 #define KEY_WORDS_ADC_CTRL "#_" 400 #define KEY_WORDS_ADC_CH "_ch" 401 #define KEY_WORDS_GPIO "#gpio" 402 #define GPIO_EXT_PORT 0x50 403 #define MAX_ADC_CH_NR 10 404 #define MAX_GPIO_NR 10 405 406 #ifdef CONFIG_ADC 407 /* 408 * How to make it works ? 409 * 410 * 1. pack dtb into rockchip resource.img, require: 411 * (1) file name end with ".dtb"; 412 * (2) file name contains key words, like: ...#_[controller]_ch[channel]=[value]...dtb 413 * @controller: adc controller name in dts, eg. "saradc", ...; 414 * @channel: adc channel; 415 * @value: adc value; 416 * eg: ...#_saradc_ch1=223#_saradc_ch2=650....dtb 417 * 418 * 2. U-Boot dtsi about adc controller node: 419 * (1) enable "u-boot,dm-pre-reloc;"; 420 * (2) must set status "okay"; 421 */ 422 static int rockchip_read_dtb_by_adc(const char *file_name) 423 { 424 static int cached_v[MAX_ADC_CH_NR]; 425 int offset_ctrl = strlen(KEY_WORDS_ADC_CTRL); 426 int offset_ch = strlen(KEY_WORDS_ADC_CH); 427 int ret, channel, len = 0, found = 0, margin = 30; 428 uint32_t raw_adc; 429 unsigned long dtb_adc; 430 char *stradc, *strch, *p; 431 char adc_v_string[10]; 432 char dev_name[32]; 433 434 debug("%s: %s\n", __func__, file_name); 435 436 /* Invalid format ? */ 437 stradc = strstr(file_name, KEY_WORDS_ADC_CTRL); 438 while (stradc) { 439 debug(" - substr: %s\n", stradc); 440 441 /* Parse controller name */ 442 strch = strstr(stradc, KEY_WORDS_ADC_CH); 443 len = strch - (stradc + offset_ctrl); 444 strlcpy(dev_name, stradc + offset_ctrl, len + 1); 445 446 /* Parse adc channel */ 447 p = strch + offset_ch; 448 if (is_digit(*p) && is_equal(*(p + 1))) { 449 channel = *p - '0'; 450 } else { 451 debug(" - invalid format: %s\n", stradc); 452 return -EINVAL; 453 } 454 455 /* 456 * Read raw adc value 457 * 458 * It doesn't need to read adc value every loop, reading once 459 * is enough. We use cached_v[] to save what we have read, zero 460 * means not read before. 461 */ 462 if (cached_v[channel] == 0) { 463 ret = adc_channel_single_shot(dev_name, 464 channel, &raw_adc); 465 if (ret) { 466 debug(" - failed to read adc, ret=%d\n", ret); 467 return ret; 468 } 469 cached_v[channel] = raw_adc; 470 } 471 472 /* Parse dtb adc value */ 473 p = strch + offset_ch + 2; /* 2: channel and '=' */ 474 while (*p && is_digit(*p)) { 475 len++; 476 p++; 477 } 478 strlcpy(adc_v_string, strch + offset_ch + 2, len + 1); 479 dtb_adc = simple_strtoul(adc_v_string, NULL, 10); 480 481 if (abs(dtb_adc - cached_v[channel]) <= margin) { 482 found = 1; 483 stradc = strstr(p, KEY_WORDS_ADC_CTRL); 484 } else { 485 found = 0; 486 break; 487 } 488 489 debug(" - parse: controller=%s, channel=%d, dtb_adc=%ld, read=%d %s\n", 490 dev_name, channel, dtb_adc, cached_v[channel], found ? "(Y)" : ""); 491 } 492 493 return found ? 0 : -ENOENT; 494 } 495 #else 496 static int rockchip_read_dtb_by_adc(const char *file_name) 497 { 498 return -ENOENT; 499 } 500 #endif 501 502 static int gpio_parse_base_address(fdt_addr_t *gpio_base_addr) 503 { 504 static int initial; 505 ofnode parent, node; 506 int i = 0; 507 508 if (initial) 509 return 0; 510 511 parent = ofnode_path("/pinctrl"); 512 if (!ofnode_valid(parent)) { 513 debug(" - Can't find pinctrl node\n"); 514 return -EINVAL; 515 } 516 517 ofnode_for_each_subnode(node, parent) { 518 if (!ofnode_get_property(node, "gpio-controller", NULL)) { 519 debug(" - Can't find gpio-controller\n"); 520 continue; 521 } 522 523 gpio_base_addr[i++] = ofnode_get_addr(node); 524 debug(" - gpio%d: 0x%x\n", i - 1, (uint32_t)gpio_base_addr[i - 1]); 525 } 526 527 if (i == 0) { 528 debug(" - parse gpio address failed\n"); 529 return -EINVAL; 530 } 531 532 initial = 1; 533 534 return 0; 535 } 536 537 /* 538 * How to make it works ? 539 * 540 * 1. pack dtb into rockchip resource.img, require: 541 * (1) file name end with ".dtb"; 542 * (2) file name contains key words, like: ...#gpio[pin]=[value]...dtb 543 * @pin: gpio name, eg. 0a2 means GPIO0A2; 544 * @value: gpio level, 0 or 1; 545 * eg: ...#gpio0a6=1#gpio1c2=0....dtb 546 * 547 * 2. U-Boot dtsi about gpio node: 548 * (1) enable "u-boot,dm-pre-reloc;" for all gpio node; 549 * (2) set all gpio status "disabled"(Because we just want their property); 550 */ 551 static int rockchip_read_dtb_by_gpio(const char *file_name) 552 { 553 static uint32_t cached_v[MAX_GPIO_NR]; 554 fdt_addr_t gpio_base_addr[MAX_GPIO_NR]; 555 int ret, found = 0, offset = strlen(KEY_WORDS_GPIO); 556 uint8_t port, pin, bank, lvl, val; 557 char *strgpio, *p; 558 uint32_t bit; 559 560 debug("%s\n", file_name); 561 562 strgpio = strstr(file_name, KEY_WORDS_GPIO); 563 while (strgpio) { 564 debug(" - substr: %s\n", strgpio); 565 566 p = strgpio + offset; 567 568 /* Invalid format ? */ 569 if (!(is_digit(*(p + 0)) && is_abcd(*(p + 1)) && 570 is_digit(*(p + 2)) && is_equal(*(p + 3)) && 571 is_digit(*(p + 4)))) { 572 debug(" - invalid format: %s\n", strgpio); 573 return -EINVAL; 574 } 575 576 /* Parse gpio address */ 577 ret = gpio_parse_base_address(gpio_base_addr); 578 if (ret) { 579 debug(" - Can't parse gpio base address: %d\n", ret); 580 return ret; 581 } 582 583 /* Read gpio value */ 584 port = *(p + 0) - '0'; 585 bank = *(p + 1) - 'a'; 586 pin = *(p + 2) - '0'; 587 lvl = *(p + 4) - '0'; 588 589 /* 590 * It doesn't need to read gpio value every loop, reading once 591 * is enough. We use cached_v[] to save what we have read, zero 592 * means not read before. 593 */ 594 if (cached_v[port] == 0) 595 cached_v[port] = 596 readl(gpio_base_addr[port] + GPIO_EXT_PORT); 597 598 /* Verify result */ 599 bit = bank * 8 + pin; 600 val = cached_v[port] & (1 << bit) ? 1 : 0; 601 602 if (val == !!lvl) { 603 found = 1; 604 strgpio = strstr(p, KEY_WORDS_GPIO); 605 } else { 606 found = 0; 607 break; 608 } 609 610 debug(" - parse: gpio%d%c%d=%d, read=%d %s\n", 611 port, bank + 'a', pin, lvl, val, found ? "(Y)" : ""); 612 } 613 614 return found ? 0 : -ENOENT; 615 } 616 617 int rockchip_read_dtb_file(void *fdt_addr) 618 { 619 struct resource_file *file; 620 struct list_head *node; 621 char *dtb_name = DTB_FILE; 622 int ret, size; 623 624 if (list_empty(&entrys_head)) 625 init_resource_list(NULL); 626 627 list_for_each(node, &entrys_head) { 628 file = list_entry(node, struct resource_file, link); 629 if (!strstr(file->name, ".dtb")) 630 continue; 631 632 if (strstr(file->name, KEY_WORDS_ADC_CTRL) && 633 strstr(file->name, KEY_WORDS_ADC_CH) && 634 !rockchip_read_dtb_by_adc(file->name)) { 635 dtb_name = file->name; 636 break; 637 } else if (strstr(file->name, KEY_WORDS_GPIO) && 638 !rockchip_read_dtb_by_gpio(file->name)) { 639 dtb_name = file->name; 640 break; 641 } 642 } 643 644 printf("DTB: %s\n", dtb_name); 645 646 size = rockchip_get_resource_file_size((void *)fdt_addr, dtb_name); 647 if (size < 0) 648 return size; 649 650 if (!sysmem_alloc_base("fdt", (phys_addr_t)fdt_addr, 651 ALIGN(size, RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD)) 652 return -ENOMEM; 653 654 ret = rockchip_read_resource_file((void *)fdt_addr, dtb_name, 0, 0); 655 if (ret < 0) 656 return ret; 657 658 #if defined(CONFIG_CMD_DTIMG) && defined(CONFIG_OF_LIBFDT_OVERLAY) 659 android_fdt_overlay_apply((void *)fdt_addr); 660 #endif 661 662 return ret; 663 } 664