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 /* Read raw adc value */ 424 if (cached_v[channel] == 0) { 425 ret = adc_channel_single_shot(dev_name, 426 channel, &raw_adc); 427 if (ret) { 428 debug(" - failed to read adc, ret=%d\n", ret); 429 return ret; 430 } 431 cached_v[channel] = raw_adc; 432 } 433 434 /* Parse dtb adc value */ 435 p = strch + offset_ch + 2; /* 2: channel and '=' */ 436 while (*p && is_digit(*p)) { 437 len++; 438 p++; 439 } 440 strlcpy(adc_v_string, strch + offset_ch + 2, len + 1); 441 dtb_adc = simple_strtoul(adc_v_string, NULL, 10); 442 443 if (abs(dtb_adc - cached_v[channel]) <= margin) { 444 found = 1; 445 stradc = strstr(p, KEY_WORDS_ADC_CTRL); 446 } else { 447 found = 0; 448 break; 449 } 450 451 debug(" - parse: controller=%s, channel=%d, dtb_adc=%ld, read=%d %s\n", 452 dev_name, channel, dtb_adc, cached_v[channel], found ? "(Y)" : ""); 453 } 454 455 return found ? 0 : -ENOENT; 456 } 457 458 static int gpio_parse_base_address(fdt_addr_t *gpio_base_addr) 459 { 460 static int initial; 461 ofnode parent, node; 462 int i = 0; 463 464 if (initial) 465 return 0; 466 467 parent = ofnode_path("/pinctrl"); 468 if (!ofnode_valid(parent)) { 469 debug(" - Can't find pinctrl node\n"); 470 return -EINVAL; 471 } 472 473 ofnode_for_each_subnode(node, parent) { 474 if (!ofnode_get_property(node, "gpio-controller", NULL)) { 475 debug(" - Can't find gpio-controller\n"); 476 continue; 477 } 478 479 gpio_base_addr[i++] = ofnode_get_addr(node); 480 debug(" - gpio%d: 0x%x\n", i - 1, (uint32_t)gpio_base_addr[i - 1]); 481 } 482 483 if (i == 0) { 484 debug(" - parse gpio address failed\n"); 485 return -EINVAL; 486 } 487 488 initial = 1; 489 490 return 0; 491 } 492 493 /* 494 * How to make it works ? 495 * 496 * 1. pack dtb into rockchip resource.img, require: 497 * (1) file name end with ".dtb"; 498 * (2) file name contains key words, like: ...#gpio[pin]=[value]...dtb 499 * @pin: gpio name, eg. 0a2 means GPIO0A2; 500 * @value: gpio level, 0 or 1; 501 * eg: ...#gpio0a6=1#gpio1c2=0....dtb 502 * 503 * 2. U-Boot dtsi about gpio node: 504 * (1) enable "u-boot,dm-pre-reloc;" for all gpio node; 505 * (2) set all gpio status "disabled"(Because we just want their property); 506 */ 507 static int rockchip_read_dtb_by_gpio(const char *file_name) 508 { 509 static uint32_t cached_v[MAX_GPIO_NR]; 510 fdt_addr_t gpio_base_addr[MAX_GPIO_NR]; 511 int ret, found = 0, offset = strlen(KEY_WORDS_GPIO); 512 uint8_t port, pin, bank, lvl, val; 513 char *strgpio, *p; 514 uint32_t bit; 515 516 debug("%s\n", file_name); 517 518 strgpio = strstr(file_name, KEY_WORDS_GPIO); 519 while (strgpio) { 520 debug(" - substr: %s\n", strgpio); 521 522 p = strgpio + offset; 523 524 /* Invalid format ? */ 525 if (!(is_digit(*(p + 0)) && is_abcd(*(p + 1)) && 526 is_digit(*(p + 2)) && is_equal(*(p + 3)) && 527 is_digit(*(p + 4)))) { 528 debug(" - invalid format: %s\n", strgpio); 529 return -EINVAL; 530 } 531 532 /* Parse gpio address */ 533 ret = gpio_parse_base_address(gpio_base_addr); 534 if (ret) { 535 debug(" - Can't parse gpio base address: %d\n", ret); 536 return ret; 537 } 538 539 /* Read gpio value */ 540 port = *(p + 0) - '0'; 541 bank = *(p + 1) - 'a'; 542 pin = *(p + 2) - '0'; 543 lvl = *(p + 4) - '0'; 544 545 if (cached_v[port] == 0) 546 cached_v[port] = 547 readl(gpio_base_addr[port] + GPIO_EXT_PORT); 548 549 /* Verify result */ 550 bit = bank * 32 + pin; 551 val = cached_v[port] & (1 << bit) ? 1 : 0; 552 553 if (val == !!lvl) { 554 found = 1; 555 strgpio = strstr(p, KEY_WORDS_GPIO); 556 } else { 557 found = 0; 558 break; 559 } 560 561 debug(" - parse: gpio%d%c%d=%d, read=%d %s\n", 562 port, bank + 'a', pin, lvl, val, found ? "(Y)" : ""); 563 } 564 565 return found ? 0 : -ENOENT; 566 } 567 568 int rockchip_read_dtb_file(void *fdt_addr) 569 { 570 struct resource_file *file; 571 struct list_head *node; 572 char *dtb_name = DTB_FILE; 573 int ret; 574 575 if (list_empty(&entrys_head)) 576 init_resource_list(NULL); 577 578 list_for_each(node, &entrys_head) { 579 file = list_entry(node, struct resource_file, link); 580 if (!strstr(file->name, ".dtb")) 581 continue; 582 583 if (strstr(file->name, KEY_WORDS_ADC_CTRL) && 584 strstr(file->name, KEY_WORDS_ADC_CH) && 585 !rockchip_read_dtb_by_adc(file->name)) { 586 dtb_name = file->name; 587 break; 588 } else if (strstr(file->name, KEY_WORDS_GPIO) && 589 !rockchip_read_dtb_by_gpio(file->name)) { 590 dtb_name = file->name; 591 break; 592 } 593 } 594 595 printf("DTB: %s\n", dtb_name); 596 597 ret = rockchip_read_resource_file((void *)fdt_addr, dtb_name, 0, 0); 598 if (ret < 0) 599 return ret; 600 601 #if defined(CONFIG_CMD_DTIMG) && \ 602 defined(CONFIG_OF_LIBFDT_OVERLAY) && defined(CONFIG_USING_KERNEL_DTB) 603 android_fdt_overlay_apply((void *)fdt_addr); 604 #endif 605 606 return ret; 607 } 608