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