2  * Copyright (c) 2009, Google Inc.
 
   5  * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
 
   7  * Redistribution and use in source and binary forms, with or without
 
   8  * modification, are permitted provided that the following conditions are met:
 
   9  *     * Redistributions of source code must retain the above copyright
 
  10  *       notice, this list of conditions and the following disclaimer.
 
  11  *     * Redistributions in binary form must reproduce the above copyright
 
  12  *       notice, this list of conditions and the following disclaimer in the
 
  13  *       documentation and/or other materials provided with the distribution.
 
  14  *     * Neither the name of The Linux Foundation nor
 
  15  *       the names of its contributors may be used to endorse or promote
 
  16  *       products derived from this software without specific prior written
 
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
  20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  21  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
  22  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
  23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
  24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
  25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
  26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
  27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
  28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
  29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  39 #include <kernel/thread.h>
 
  42 #include <dev/flash.h>
 
  43 #include <lib/ptable.h>
 
  45 #include <dev/fbcon.h>
 
  49 #include <partition_parser.h>
 
  51 #include <crypto_hash.h>
 
  53 #include <boot_stats.h>
 
  55 #include <platform/iomap.h>
 
  56 #include <boot_device.h>
 
  63 #include "image_verify.h"
 
  67 #include "sparse_format.h"
 
  73 extern  bool target_use_signed_kernel(void);
 
  74 extern void platform_uninit(void);
 
  75 extern void target_uninit(void);
 
  76 extern int get_target_boot_params(const char *cmdline, const char *part,
 
  77                                   char *buf, int buflen);
 
  79 void write_device_info_mmc(device_info *dev);
 
  80 void write_device_info_flash(device_info *dev);
 
  82 #define EXPAND(NAME) #NAME
 
  83 #define TARGET(NAME) EXPAND(NAME)
 
  86 #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
 
  88 #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
 
  92 #define MEMSIZE 1024*1024
 
  95 #define MAX_TAGS_SIZE   1024
 
  97 #define RECOVERY_MODE   0x77665502
 
  98 #define FASTBOOT_MODE   0x77665500
 
 100 /* make 4096 as default size to ensure EFS,EXT4's erasing */
 
 101 #define DEFAULT_ERASE_SIZE  4096
 
 102 #define MAX_PANEL_BUF_SIZE 128
 
 104 #define UBI_MAGIC      "UBI#"
 
 105 #define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
 
 106 #define UBI_MAGIC_SIZE 0x04
 
 107 #define BOOT_DEV_MAX_LEN  64
 
 109 #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
 
 111 #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
 
 114 static const char *emmc_cmdline = " androidboot.bootdevice=";
 
 116 static const char *emmc_cmdline = " androidboot.emmc=true";
 
 118 static const char *usb_sn_cmdline = " androidboot.serialno=";
 
 119 static const char *androidboot_mode = " androidboot.mode=";
 
 120 static const char *loglevel         = " quiet";
 
 121 static const char *battchg_pause = " androidboot.mode=charger";
 
 122 static const char *auth_kernel = " androidboot.authorized_kernel=true";
 
 123 static const char *secondary_gpt_enable = " gpt";
 
 125 static const char *baseband_apq     = " androidboot.baseband=apq";
 
 126 static const char *baseband_msm     = " androidboot.baseband=msm";
 
 127 static const char *baseband_csfb    = " androidboot.baseband=csfb";
 
 128 static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
 
 129 static const char *baseband_mdm     = " androidboot.baseband=mdm";
 
 130 static const char *baseband_mdm2    = " androidboot.baseband=mdm2";
 
 131 static const char *baseband_sglte   = " androidboot.baseband=sglte";
 
 132 static const char *baseband_dsda    = " androidboot.baseband=dsda";
 
 133 static const char *baseband_dsda2   = " androidboot.baseband=dsda2";
 
 134 static const char *baseband_sglte2  = " androidboot.baseband=sglte2";
 
 135 static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
 
 137 static unsigned page_size = 0;
 
 138 static unsigned page_mask = 0;
 
 139 static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
 
 140 static bool boot_into_ffbm;
 
 141 static char target_boot_params[64];
 
 143 /* Assuming unauthorized kernel image by default */
 
 144 static int auth_kernel_img = 0;
 
 146 static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
 
 148 struct atag_ptbl_entry
 
 157  * Partition info, required to be published
 
 160 struct getvar_partition_info {
 
 161         const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
 
 162         char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
 
 163         char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
 
 164         char size_response[MAX_RSP_SIZE];        /* fastboot response for size */
 
 165         char type_response[MAX_RSP_SIZE];        /* fastboot response for type */
 
 169  * Right now, we are publishing the info for only
 
 172 struct getvar_partition_info part_info[] =
 
 174         { "system"  , "partition-size:", "partition-type:", "", "ext4" },
 
 175         { "userdata", "partition-size:", "partition-type:", "", "ext4" },
 
 176         { "cache"   , "partition-size:", "partition-type:", "", "ext4" },
 
 179 char max_download_size[MAX_RSP_SIZE];
 
 180 char charger_screen_enabled[MAX_RSP_SIZE];
 
 182 char display_panel_buf[MAX_PANEL_BUF_SIZE];
 
 183 char panel_display_mode[MAX_RSP_SIZE];
 
 185 extern int emmc_recovery_init(void);
 
 188 extern int fastboot_trigger(void);
 
 191 static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
 
 193         /* overwrite the destination of specified for the project */
 
 194 #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
 
 196                 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
 
 198                 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
 
 199         hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
 
 200         hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
 
 204 static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
 
 206         struct atag_ptbl_entry atag_ptn;
 
 208         memcpy(atag_ptn.name, ptn->name, 16);
 
 209         atag_ptn.name[15] = '\0';
 
 210         atag_ptn.offset = ptn->start;
 
 211         atag_ptn.size = ptn->length;
 
 212         atag_ptn.flags = ptn->flags;
 
 213         memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
 
 214         *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
 
 217 unsigned char *update_cmdline(const char * cmdline)
 
 220         int have_cmdline = 0;
 
 221         unsigned char *cmdline_final = NULL;
 
 222         int pause_at_bootup = 0;
 
 223         bool warm_boot = false;
 
 224         bool gpt_exists = partition_gpt_exists();
 
 225         int have_target_boot_params = 0;
 
 226         char *boot_dev_buf = NULL;
 
 228         if (cmdline && cmdline[0]) {
 
 229                 cmdline_len = strlen(cmdline);
 
 232         if (target_is_emmc_boot()) {
 
 233                 cmdline_len += strlen(emmc_cmdline);
 
 235                 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
 
 236                 ASSERT(boot_dev_buf);
 
 237                 platform_boot_dev_cmdline(boot_dev_buf);
 
 238                 cmdline_len += strlen(boot_dev_buf);
 
 242         cmdline_len += strlen(usb_sn_cmdline);
 
 243         cmdline_len += strlen(sn_buf);
 
 245         if (boot_into_recovery && gpt_exists)
 
 246                 cmdline_len += strlen(secondary_gpt_enable);
 
 248         if (boot_into_ffbm) {
 
 249                 cmdline_len += strlen(androidboot_mode);
 
 250                 cmdline_len += strlen(ffbm_mode_string);
 
 251                 /* reduce kernel console messages to speed-up boot */
 
 252                 cmdline_len += strlen(loglevel);
 
 253         } else if (device.charger_screen_enabled &&
 
 254                         target_pause_for_battery_charge()) {
 
 256                 cmdline_len += strlen(battchg_pause);
 
 259         if(target_use_signed_kernel() && auth_kernel_img) {
 
 260                 cmdline_len += strlen(auth_kernel);
 
 263         if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
 
 266                                    sizeof(target_boot_params)) == 0) {
 
 267                 have_target_boot_params = 1;
 
 268                 cmdline_len += strlen(target_boot_params);
 
 271         /* Determine correct androidboot.baseband to use */
 
 272         switch(target_baseband())
 
 275                         cmdline_len += strlen(baseband_apq);
 
 279                         cmdline_len += strlen(baseband_msm);
 
 283                         cmdline_len += strlen(baseband_csfb);
 
 286                 case BASEBAND_SVLTE2A:
 
 287                         cmdline_len += strlen(baseband_svlte2a);
 
 291                         cmdline_len += strlen(baseband_mdm);
 
 295                         cmdline_len += strlen(baseband_mdm2);
 
 299                         cmdline_len += strlen(baseband_sglte);
 
 302                 case BASEBAND_SGLTE2:
 
 303                         cmdline_len += strlen(baseband_sglte2);
 
 307                         cmdline_len += strlen(baseband_dsda);
 
 311                         cmdline_len += strlen(baseband_dsda2);
 
 316                 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
 
 317                         target_display_panel_node(device.display_panel,
 
 318                         display_panel_buf, MAX_PANEL_BUF_SIZE) &&
 
 319                         strlen(display_panel_buf)) {
 
 320                         cmdline_len += strlen(display_panel_buf);
 
 324         if (target_warm_boot()) {
 
 326                 cmdline_len += strlen(warmboot_cmdline);
 
 329         if (cmdline_len > 0) {
 
 331                 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
 
 334                 /* Save start ptr for debug print */
 
 338                         while ((*dst++ = *src++));
 
 340                 if (target_is_emmc_boot()) {
 
 342                         if (have_cmdline) --dst;
 
 344                         while ((*dst++ = *src++));
 
 347                         if (have_cmdline) --dst;
 
 348                         while ((*dst++ = *src++));
 
 352                 src = usb_sn_cmdline;
 
 353                 if (have_cmdline) --dst;
 
 355                 while ((*dst++ = *src++));
 
 357                 if (have_cmdline) --dst;
 
 359                 while ((*dst++ = *src++));
 
 361                         if (have_cmdline) --dst;
 
 362                         src = warmboot_cmdline;
 
 363                         while ((*dst++ = *src++));
 
 366                 if (boot_into_recovery && gpt_exists) {
 
 367                         src = secondary_gpt_enable;
 
 368                         if (have_cmdline) --dst;
 
 369                         while ((*dst++ = *src++));
 
 372                 if (boot_into_ffbm) {
 
 373                         src = androidboot_mode;
 
 374                         if (have_cmdline) --dst;
 
 375                         while ((*dst++ = *src++));
 
 376                         src = ffbm_mode_string;
 
 377                         if (have_cmdline) --dst;
 
 378                         while ((*dst++ = *src++));
 
 380                         if (have_cmdline) --dst;
 
 381                         while ((*dst++ = *src++));
 
 382                 } else if (pause_at_bootup) {
 
 384                         if (have_cmdline) --dst;
 
 385                         while ((*dst++ = *src++));
 
 388                 if(target_use_signed_kernel() && auth_kernel_img) {
 
 390                         if (have_cmdline) --dst;
 
 391                         while ((*dst++ = *src++));
 
 394                 switch(target_baseband())
 
 398                                 if (have_cmdline) --dst;
 
 399                                 while ((*dst++ = *src++));
 
 404                                 if (have_cmdline) --dst;
 
 405                                 while ((*dst++ = *src++));
 
 410                                 if (have_cmdline) --dst;
 
 411                                 while ((*dst++ = *src++));
 
 414                         case BASEBAND_SVLTE2A:
 
 415                                 src = baseband_svlte2a;
 
 416                                 if (have_cmdline) --dst;
 
 417                                 while ((*dst++ = *src++));
 
 422                                 if (have_cmdline) --dst;
 
 423                                 while ((*dst++ = *src++));
 
 428                                 if (have_cmdline) --dst;
 
 429                                 while ((*dst++ = *src++));
 
 433                                 src = baseband_sglte;
 
 434                                 if (have_cmdline) --dst;
 
 435                                 while ((*dst++ = *src++));
 
 438                         case BASEBAND_SGLTE2:
 
 439                                 src = baseband_sglte2;
 
 440                                 if (have_cmdline) --dst;
 
 441                                 while ((*dst++ = *src++));
 
 446                                 if (have_cmdline) --dst;
 
 447                                 while ((*dst++ = *src++));
 
 451                                 src = baseband_dsda2;
 
 452                                 if (have_cmdline) --dst;
 
 453                                 while ((*dst++ = *src++));
 
 457                 if (strlen(display_panel_buf)) {
 
 458                         src = display_panel_buf;
 
 459                         if (have_cmdline) --dst;
 
 460                         while ((*dst++ = *src++));
 
 463                 if (have_target_boot_params) {
 
 464                         if (have_cmdline) --dst;
 
 465                         src = target_boot_params;
 
 466                         while ((*dst++ = *src++));
 
 474         dprintf(INFO, "cmdline: %s\n", cmdline_final);
 
 475         return cmdline_final;
 
 478 unsigned *atag_core(unsigned *ptr)
 
 488 unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
 
 489                                                            unsigned ramdisk_size)
 
 494                 *ptr++ = (unsigned)ramdisk;
 
 495                 *ptr++ = ramdisk_size;
 
 501 unsigned *atag_ptable(unsigned **ptr_addr)
 
 504         struct ptable *ptable;
 
 506         if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
 
 507                 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
 
 509                 *(*ptr_addr)++ = 0x4d534d70;
 
 510                 for (i = 0; i < ptable->count; ++i)
 
 511                         ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
 
 517 unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
 
 519         int cmdline_length = 0;
 
 523         cmdline_length = strlen((const char*)cmdline);
 
 524         n = (cmdline_length + 4) & (~3);
 
 526         *ptr++ = (n / 4) + 2;
 
 529         while ((*dest++ = *cmdline++));
 
 535 unsigned *atag_end(unsigned *ptr)
 
 544 void generate_atags(unsigned *ptr, const char *cmdline,
 
 545                     void *ramdisk, unsigned ramdisk_size)
 
 548         ptr = atag_core(ptr);
 
 549         ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
 
 550         ptr = target_atag_mem(ptr);
 
 552         /* Skip NAND partition ATAGS for eMMC boot */
 
 553         if (!target_is_emmc_boot()){
 
 554                 ptr = atag_ptable(&ptr);
 
 557         ptr = atag_cmdline(ptr, cmdline);
 
 561 typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
 
 562 void boot_linux(void *kernel, unsigned *tags,
 
 563                 const char *cmdline, unsigned machtype,
 
 564                 void *ramdisk, unsigned ramdisk_size)
 
 566         unsigned char *final_cmdline;
 
 571         void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
 
 572         uint32_t tags_phys = PA((addr_t)tags);
 
 573         struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
 
 575         ramdisk = PA(ramdisk);
 
 577         final_cmdline = update_cmdline((const char*)cmdline);
 
 580         dprintf(INFO, "Updating device tree: start\n");
 
 582         /* Update the Device Tree */
 
 583         ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
 
 586                 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
 
 589         dprintf(INFO, "Updating device tree: done\n");
 
 591         /* Generating the Atags */
 
 592         generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
 
 595         /* Perform target specific cleanup */
 
 598         /* Turn off splash screen if enabled */
 
 599 #if DISPLAY_SPLASH_SCREEN
 
 600         target_display_shutdown();
 
 604         dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
 
 605                 entry, ramdisk, ramdisk_size, tags_phys);
 
 607         enter_critical_section();
 
 609         /* do any platform specific cleanup before kernel entry */
 
 612         arch_disable_cache(UCACHE);
 
 617         bs_set_timestamp(BS_KERNEL_ENTRY);
 
 620                 /* Jump to a 64bit kernel */
 
 621                 scm_elexec_call((paddr_t)kernel, tags_phys);
 
 623                 /* Jump to a 32bit kernel */
 
 624                 entry(0, machtype, (unsigned*)tags_phys);
 
 627 /* Function to check if the memory address range falls within the aboot
 
 629  * start: Start of the memory region
 
 630  * size: Size of the memory region
 
 632 int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
 
 634         /* Check for boundary conditions. */
 
 635         if ((UINT_MAX - start) < size)
 
 638         /* Check for memory overlap. */
 
 639         if ((start < MEMBASE) && ((start + size) <= MEMBASE))
 
 641         else if (start >= (MEMBASE + MEMSIZE))
 
 647 #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
 
 649 BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
 
 651 BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
 
 654 static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
 
 657 #if IMAGE_VERIF_ALGO_SHA1
 
 658         uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
 
 660         uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
 
 663         /* Assume device is rooted at this time. */
 
 664         device.is_tampered = 1;
 
 666         dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
 
 668         ret = image_verify((unsigned char *)bootimg_addr,
 
 669                                            (unsigned char *)(bootimg_addr + bootimg_size),
 
 673         dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
 
 677                 /* Authorized kernel */
 
 678                 device.is_tampered = 0;
 
 683         set_tamper_flag(device.is_tampered);
 
 686         if(device.is_tampered)
 
 688                 write_device_info_mmc(&device);
 
 689         #ifdef TZ_TAMPER_FUSE
 
 690                 set_tamper_fuse_cmd();
 
 692         #ifdef ASSERT_ON_TAMPER
 
 693                 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
 
 699 static bool check_format_bit()
 
 704         struct boot_selection_info *in = NULL;
 
 707         index = partition_get_index("bootselect");
 
 708         if (index == INVALID_PTN)
 
 710                 dprintf(INFO, "Unable to locate /bootselect partition\n");
 
 713         offset = partition_get_offset(index);
 
 716                 dprintf(INFO, "partition /bootselect doesn't exist\n");
 
 719         buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
 
 721         if (mmc_read(offset, (unsigned int *)buf, page_size))
 
 723                 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
 
 727         in = (struct boot_selection_info *) buf;
 
 728         if ((in->signature == BOOTSELECT_SIGNATURE) &&
 
 729                         (in->version == BOOTSELECT_VERSION)) {
 
 730                 if ((in->state_info & BOOTSELECT_FORMAT) &&
 
 731                                 !(in->state_info & BOOTSELECT_FACTORY))
 
 734                 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
 
 735                                 in->signature, in->version);
 
 742 int boot_linux_from_mmc(void)
 
 744         struct boot_img_hdr *hdr = (void*) buf;
 
 745         struct boot_img_hdr *uhdr;
 
 748         unsigned long long ptn = 0;
 
 749         int index = INVALID_PTN;
 
 751         unsigned char *image_addr = 0;
 
 752         unsigned kernel_actual;
 
 753         unsigned ramdisk_actual;
 
 754         unsigned imagesize_actual;
 
 755         unsigned second_actual = 0;
 
 758         struct dt_table *table;
 
 759         struct dt_entry dt_entry;
 
 760         unsigned dt_table_offset;
 
 762         uint32_t dt_hdr_size;
 
 764         BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
 
 765         struct kernel64_hdr *kptr = (void*) kbuf;
 
 767         if (check_format_bit())
 
 768                 boot_into_recovery = 1;
 
 770         if (!boot_into_recovery) {
 
 771                 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
 
 772                 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
 
 774                         boot_into_ffbm = false;
 
 776                                 dprintf(CRITICAL,"failed to get ffbm cookie");
 
 778                         boot_into_ffbm = true;
 
 780                 boot_into_ffbm = false;
 
 781         uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
 
 782         if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
 783                 dprintf(INFO, "Unified boot method!\n");
 
 787         if (!boot_into_recovery) {
 
 788                 index = partition_get_index("boot");
 
 789                 ptn = partition_get_offset(index);
 
 791                         dprintf(CRITICAL, "ERROR: No boot partition found\n");
 
 796                 index = partition_get_index("recovery");
 
 797                 ptn = partition_get_offset(index);
 
 799                         dprintf(CRITICAL, "ERROR: No recovery partition found\n");
 
 804         if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
 
 805                 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
 
 809         if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
 810                 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
 
 814         if (hdr->page_size && (hdr->page_size != page_size)) {
 
 816                 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
 
 817                         dprintf(CRITICAL, "ERROR: Invalid page size\n");
 
 820                 page_size = hdr->page_size;
 
 821                 page_mask = page_size - 1;
 
 824         /* Read the next page to get kernel Image header
 
 825          * which lives in the second page for arm64 targets.
 
 828         if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
 
 829                 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
 
 834          * Update the kernel/ramdisk/tags address if the boot image header
 
 835          * has default values, these default values come from mkbootimg when
 
 836          * the boot image is flashed using fastboot flash:raw
 
 838         update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
 
 840         /* Get virtual addresses since the hdr saves physical addresses. */
 
 841         hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
 
 842         hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
 
 843         hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
 
 845         kernel_actual  = ROUND_TO_PAGE(hdr->kernel_size,  page_mask);
 
 846         ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
 
 848         /* Check if the addresses in the header are valid. */
 
 849         if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
 
 850                 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
 
 852                 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
 
 857         if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
 
 859                 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
 
 864         /* Authenticate Kernel */
 
 865         dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
 
 866                 (int) target_use_signed_kernel(),
 
 870         if(target_use_signed_kernel() && (!device.is_unlocked))
 
 874                 image_addr = (unsigned char *)target_get_scratch_address();
 
 877                 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
 
 878                 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
 
 880                 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
 
 882                         dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
 886                 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
 
 890                 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
 
 891                 bs_set_timestamp(BS_KERNEL_LOAD_START);
 
 893                 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
 
 895                         dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
 
 899                 /* Read image without signature */
 
 900                 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
 
 902                         dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
 
 906                 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
 
 907                 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
 
 909                 offset = imagesize_actual;
 
 911                 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
 
 913                         dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
 
 918                 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
 
 920                         dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
 
 924                 verify_signed_bootimg(image_addr, imagesize_actual);
 
 926                 /* Move kernel, ramdisk and device tree to correct address */
 
 927                 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
 
 928                 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
 
 932                         dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
 
 933                         table = (struct dt_table*) dt_table_offset;
 
 935                         if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
 
 936                                 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
 
 940                         /* Find index of device tree within device tree table */
 
 941                         if(dev_tree_get_entry_info(table, &dt_entry) != 0){
 
 942                                 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
 
 946                         /* Validate and Read device device tree in the "tags_add */
 
 947                         if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
 
 949                                 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
 953                         memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
 
 956                          * If appended dev tree is found, update the atags with
 
 957                          * memory address to the DTB appended location on RAM.
 
 958                          * Else update with the atags address in the kernel header
 
 961                         dtb = dev_tree_appended((void*) hdr->kernel_addr,
 
 963                                                 (void *)hdr->tags_addr);
 
 965                                 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
 
 973                 second_actual  = ROUND_TO_PAGE(hdr->second_size,  page_mask);
 
 975                 dprintf(INFO, "Loading boot image (%d): start\n",
 
 976                                 kernel_actual + ramdisk_actual);
 
 977                 bs_set_timestamp(BS_KERNEL_LOAD_START);
 
 982                 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
 
 983                         dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
 
 986                 offset += kernel_actual;
 
 989                 if(ramdisk_actual != 0)
 
 991                         if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
 
 992                                 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
 
 996                 offset += ramdisk_actual;
 
 998                 dprintf(INFO, "Loading boot image (%d): done\n",
 
 999                                 kernel_actual + ramdisk_actual);
 
1000                 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
 
1002                 if(hdr->second_size != 0) {
 
1003                         offset += second_actual;
 
1004                         /* Second image loading not implemented. */
 
1009                 if(hdr->dt_size != 0) {
 
1010                         /* Read the first page of device tree table into buffer */
 
1011                         if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
 
1012                                 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
 
1015                         table = (struct dt_table*) dt_buf;
 
1017                         if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
 
1018                                 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
 
1022                         table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
 
1026                         /* Read the entire device tree table into buffer */
 
1027                         if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
 
1028                                 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
 
1032                         /* Find index of device tree within device tree table */
 
1033                         if(dev_tree_get_entry_info(table, &dt_entry) != 0){
 
1034                                 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
 
1038                         /* Validate and Read device device tree in the "tags_add */
 
1039                         if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
 
1041                                 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1045                         if(mmc_read(ptn + offset + dt_entry.offset,
 
1046                                                  (void *)hdr->tags_addr, dt_entry.size)) {
 
1047                                 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
 
1050                         #ifdef TZ_SAVE_KERNEL_HASH
 
1051                         aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
 
1052                                        hdr->ramdisk_addr, ramdisk_actual,
 
1053                                        ptn, offset, hdr->dt_size);
 
1054                         #endif /* TZ_SAVE_KERNEL_HASH */
 
1058                         /* Validate the tags_addr */
 
1059                         if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
 
1061                                 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1065                          * If appended dev tree is found, update the atags with
 
1066                          * memory address to the DTB appended location on RAM.
 
1067                          * Else update with the atags address in the kernel header
 
1070                         dtb = dev_tree_appended((void*) hdr->kernel_addr,
 
1072                                                 (void *)hdr->tags_addr);
 
1074                                 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
 
1081         if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
 
1082                 target_load_ssd_keystore();
 
1086         boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
 
1087                    (const char *)hdr->cmdline, board_machtype(),
 
1088                    (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
 
1093 int boot_linux_from_flash(void)
 
1095         struct boot_img_hdr *hdr = (void*) buf;
 
1096         struct ptentry *ptn;
 
1097         struct ptable *ptable;
 
1098         unsigned offset = 0;
 
1100         unsigned char *image_addr = 0;
 
1101         unsigned kernel_actual;
 
1102         unsigned ramdisk_actual;
 
1103         unsigned imagesize_actual;
 
1104         unsigned second_actual;
 
1107         struct dt_table *table;
 
1108         struct dt_entry dt_entry;
 
1110         uint32_t dt_hdr_size;
 
1113         if (target_is_emmc_boot()) {
 
1114                 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
 
1115                 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
1116                         dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
 
1122         ptable = flash_get_ptable();
 
1123         if (ptable == NULL) {
 
1124                 dprintf(CRITICAL, "ERROR: Partition table not found\n");
 
1128         if(!boot_into_recovery)
 
1130                 ptn = ptable_find(ptable, "boot");
 
1133                         dprintf(CRITICAL, "ERROR: No boot partition found\n");
 
1139                 ptn = ptable_find(ptable, "recovery");
 
1141                         dprintf(CRITICAL, "ERROR: No recovery partition found\n");
 
1146         if (flash_read(ptn, offset, buf, page_size)) {
 
1147                 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
 
1151         if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
1152                 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
 
1156         if (hdr->page_size != page_size) {
 
1157                 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
 
1162          * Update the kernel/ramdisk/tags address if the boot image header
 
1163          * has default values, these default values come from mkbootimg when
 
1164          * the boot image is flashed using fastboot flash:raw
 
1166         update_ker_tags_rdisk_addr(hdr, false);
 
1168         /* Get virtual addresses since the hdr saves physical addresses. */
 
1169         hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
 
1170         hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
 
1171         hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
 
1173         kernel_actual  = ROUND_TO_PAGE(hdr->kernel_size,  page_mask);
 
1174         ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
 
1176         /* Check if the addresses in the header are valid. */
 
1177         if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
 
1178                 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
 
1180                 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
 
1185                 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
 
1187                         dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
 
1192         /* Authenticate Kernel */
 
1193         if(target_use_signed_kernel() && (!device.is_unlocked))
 
1195                 image_addr = (unsigned char *)target_get_scratch_address();
 
1199                 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
 
1200                 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
 
1202                 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
 
1204                         dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1208                 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
 
1211                 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
 
1212                 bs_set_timestamp(BS_KERNEL_LOAD_START);
 
1214                 /* Read image without signature */
 
1215                 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
 
1217                         dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
 
1221                 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
 
1222                 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
 
1224                 offset = imagesize_actual;
 
1225                 /* Read signature */
 
1226                 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
 
1228                         dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
 
1232                 verify_signed_bootimg(image_addr, imagesize_actual);
 
1234                 /* Move kernel and ramdisk to correct address */
 
1235                 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
 
1236                 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
 
1238                 /* Validate and Read device device tree in the "tags_add */
 
1239                 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
 
1241                         dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1245                 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
 
1248                 /* Make sure everything from scratch address is read before next step!*/
 
1249                 if(device.is_tampered)
 
1251                         write_device_info_flash(&device);
 
1253 #if USE_PCOM_SECBOOT
 
1254                 set_tamper_flag(device.is_tampered);
 
1261                 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
 
1262                 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
 
1263                 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
 
1265                 dprintf(INFO, "Loading boot image (%d): start\n",
 
1266                                 kernel_actual + ramdisk_actual);
 
1267                 bs_set_timestamp(BS_KERNEL_LOAD_START);
 
1269                 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
 
1270                         dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
 
1273                 offset += kernel_actual;
 
1275                 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
 
1276                         dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
 
1279                 offset += ramdisk_actual;
 
1281                 dprintf(INFO, "Loading boot image (%d): done\n",
 
1282                                 kernel_actual + ramdisk_actual);
 
1283                 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
 
1285                 if(hdr->second_size != 0) {
 
1286                         offset += second_actual;
 
1287                         /* Second image loading not implemented. */
 
1292                 if(hdr->dt_size != 0) {
 
1294                         /* Read the device tree table into buffer */
 
1295                         if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
 
1296                                 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
 
1300                         table = (struct dt_table*) dt_buf;
 
1302                         if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
 
1303                                 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
 
1307                         table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
 
1311                         /* Read the entire device tree table into buffer */
 
1312                         if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
 
1313                                 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
 
1318                         /* Find index of device tree within device tree table */
 
1319                         if(dev_tree_get_entry_info(table, &dt_entry) != 0){
 
1320                                 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
 
1324                         /* Validate and Read device device tree in the "tags_add */
 
1325                         if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
 
1327                                 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1331                         /* Read device device tree in the "tags_add */
 
1332                         if(flash_read(ptn, offset + dt_entry.offset,
 
1333                                                  (void *)hdr->tags_addr, dt_entry.size)) {
 
1334                                 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
 
1343         /* TODO: create/pass atags to kernel */
 
1345         boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
 
1346                    (const char *)hdr->cmdline, board_machtype(),
 
1347                    (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
 
1352 BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
 
1353 void write_device_info_mmc(device_info *dev)
 
1355         struct device_info *info = (void*) info_buf;
 
1356         unsigned long long ptn = 0;
 
1357         unsigned long long size;
 
1358         int index = INVALID_PTN;
 
1362         index = partition_get_index("aboot");
 
1363         ptn = partition_get_offset(index);
 
1369         lun = partition_get_lun(index);
 
1372         size = partition_get_size(index);
 
1374         memcpy(info, dev, sizeof(device_info));
 
1376         blocksize = mmc_get_device_blocksize();
 
1378         if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
 
1380                 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
 
1385 void read_device_info_mmc(device_info *dev)
 
1387         struct device_info *info = (void*) info_buf;
 
1388         unsigned long long ptn = 0;
 
1389         unsigned long long size;
 
1390         int index = INVALID_PTN;
 
1393         index = partition_get_index("aboot");
 
1394         ptn = partition_get_offset(index);
 
1400         size = partition_get_size(index);
 
1402         blocksize = mmc_get_device_blocksize();
 
1404         if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
 
1406                 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
 
1410         if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
 
1412                 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
 
1413                 info->is_unlocked = 0;
 
1414                 info->is_tampered = 0;
 
1415                 info->charger_screen_enabled = 0;
 
1417                 write_device_info_mmc(info);
 
1419         memcpy(dev, info, sizeof(device_info));
 
1422 void write_device_info_flash(device_info *dev)
 
1424         struct device_info *info = (void *) info_buf;
 
1425         struct ptentry *ptn;
 
1426         struct ptable *ptable;
 
1428         ptable = flash_get_ptable();
 
1431                 dprintf(CRITICAL, "ERROR: Partition table not found\n");
 
1435         ptn = ptable_find(ptable, "devinfo");
 
1438                 dprintf(CRITICAL, "ERROR: No boot partition found\n");
 
1442         memcpy(info, dev, sizeof(device_info));
 
1444         if (flash_write(ptn, 0, (void *)info_buf, page_size))
 
1446                 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
 
1451 void read_device_info_flash(device_info *dev)
 
1453         struct device_info *info = (void*) info_buf;
 
1454         struct ptentry *ptn;
 
1455         struct ptable *ptable;
 
1457         ptable = flash_get_ptable();
 
1460                 dprintf(CRITICAL, "ERROR: Partition table not found\n");
 
1464         ptn = ptable_find(ptable, "devinfo");
 
1467                 dprintf(CRITICAL, "ERROR: No boot partition found\n");
 
1471         if (flash_read(ptn, 0, (void *)info_buf, page_size))
 
1473                 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
 
1477         if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
 
1479                 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
 
1480                 info->is_unlocked = 0;
 
1481                 info->is_tampered = 0;
 
1482                 write_device_info_flash(info);
 
1484         memcpy(dev, info, sizeof(device_info));
 
1487 void write_device_info(device_info *dev)
 
1489         if(target_is_emmc_boot())
 
1491                 write_device_info_mmc(dev);
 
1495                 write_device_info_flash(dev);
 
1499 void read_device_info(device_info *dev)
 
1501         if(target_is_emmc_boot())
 
1503                 read_device_info_mmc(dev);
 
1507                 read_device_info_flash(dev);
 
1511 void reset_device_info()
 
1513         dprintf(ALWAYS, "reset_device_info called.");
 
1514         device.is_tampered = 0;
 
1515         write_device_info(&device);
 
1518 void set_device_root()
 
1520         dprintf(ALWAYS, "set_device_root called.");
 
1521         device.is_tampered = 1;
 
1522         write_device_info(&device);
 
1526 int copy_dtb(uint8_t *boot_image_start)
 
1528         uint32 dt_image_offset = 0;
 
1530         struct dt_table *table;
 
1531         struct dt_entry dt_entry;
 
1532         uint32_t dt_hdr_size;
 
1534         struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
 
1536         if(hdr->dt_size != 0) {
 
1538                 /* add kernel offset */
 
1539                 dt_image_offset += page_size;
 
1540                 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
 
1541                 dt_image_offset += n;
 
1543                 /* add ramdisk offset */
 
1544                 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
 
1545                 dt_image_offset += n;
 
1547                 /* add second offset */
 
1548                 if(hdr->second_size != 0) {
 
1549                         n = ROUND_TO_PAGE(hdr->second_size, page_mask);
 
1550                         dt_image_offset += n;
 
1553                 /* offset now point to start of dt.img */
 
1554                 table = (struct dt_table*)(boot_image_start + dt_image_offset);
 
1556                 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
 
1557                         dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
 
1560                 /* Find index of device tree within device tree table */
 
1561                 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
 
1562                         dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
 
1566                 /* Validate and Read device device tree in the "tags_add */
 
1567                 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
 
1569                         dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
 
1573                 /* Read device device tree in the "tags_add */
 
1574                 memmove((void*) hdr->tags_addr,
 
1575                                 boot_image_start + dt_image_offset +  dt_entry.offset,
 
1580         /* Everything looks fine. Return success. */
 
1585 void cmd_boot(const char *arg, void *data, unsigned sz)
 
1587         unsigned kernel_actual;
 
1588         unsigned ramdisk_actual;
 
1589         uint32_t image_actual;
 
1590         uint32_t dt_actual = 0;
 
1591         uint32_t sig_actual = SIGNATURE_SIZE;
 
1592         struct boot_img_hdr *hdr;
 
1593         struct kernel64_hdr *kptr;
 
1594         char *ptr = ((char*) data);
 
1596         uint8_t dtb_copied = 0;
 
1598         if (sz < sizeof(hdr)) {
 
1599                 fastboot_fail("invalid bootimage header");
 
1603         hdr = (struct boot_img_hdr *)data;
 
1605         /* ensure commandline is terminated */
 
1606         hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
 
1608         if(target_is_emmc_boot() && hdr->page_size) {
 
1609                 page_size = hdr->page_size;
 
1610                 page_mask = page_size - 1;
 
1613         kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
 
1614         ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
 
1616         dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
 
1619         image_actual = ADD_OF(page_size, kernel_actual);
 
1620         image_actual = ADD_OF(image_actual, ramdisk_actual);
 
1621         image_actual = ADD_OF(image_actual, dt_actual);
 
1623         if (target_use_signed_kernel() && (!device.is_unlocked))
 
1624                 image_actual = ADD_OF(image_actual, sig_actual);
 
1626         /* sz should have atleast raw boot image */
 
1627         if (image_actual > sz) {
 
1628                 fastboot_fail("bootimage: incomplete or not signed");
 
1632         /* Verify the boot image
 
1633          * device & page_size are initialized in aboot_init
 
1635         if (target_use_signed_kernel() && (!device.is_unlocked))
 
1636                 /* Pass size excluding signature size, otherwise we would try to
 
1637                  * access signature beyond its length
 
1639                 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
 
1642          * Update the kernel/ramdisk/tags address if the boot image header
 
1643          * has default values, these default values come from mkbootimg when
 
1644          * the boot image is flashed using fastboot flash:raw
 
1646         kptr = (struct kernel64_hdr*)((char*) data + page_size);
 
1647         update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
 
1649         /* Get virtual addresses since the hdr saves physical addresses. */
 
1650         hdr->kernel_addr = VA(hdr->kernel_addr);
 
1651         hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
 
1652         hdr->tags_addr = VA(hdr->tags_addr);
 
1654         /* Check if the addresses in the header are valid. */
 
1655         if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
 
1656                 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
 
1658                 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
 
1663         /* find correct dtb and copy it to right location */
 
1664         ret = copy_dtb(data);
 
1666         dtb_copied = !ret ? 1 : 0;
 
1668         if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
 
1670                 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
 
1675         /* Load ramdisk & kernel */
 
1676         memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
 
1677         memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
 
1681          * If dtb is not found look for appended DTB in the kernel.
 
1682          * If appended dev tree is found, update the atags with
 
1683          * memory address to the DTB appended location on RAM.
 
1684          * Else update with the atags address in the kernel header
 
1688                 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
 
1689                                         (void *)hdr->tags_addr);
 
1691                         fastboot_fail("dtb not found");
 
1698         if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
 
1700                 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
 
1708         boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
 
1709                    (const char*) hdr->cmdline, board_machtype(),
 
1710                    (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
 
1713 void cmd_erase(const char *arg, void *data, unsigned sz)
 
1715         struct ptentry *ptn;
 
1716         struct ptable *ptable;
 
1718         ptable = flash_get_ptable();
 
1719         if (ptable == NULL) {
 
1720                 fastboot_fail("partition table doesn't exist");
 
1724         ptn = ptable_find(ptable, arg);
 
1726                 fastboot_fail("unknown partition name");
 
1730         if (flash_erase(ptn)) {
 
1731                 fastboot_fail("failed to erase partition");
 
1738 void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
 
1740         BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
 
1741         unsigned long long ptn = 0;
 
1742         unsigned long long size = 0;
 
1743         int index = INVALID_PTN;
 
1746         index = partition_get_index(arg);
 
1747         ptn = partition_get_offset(index);
 
1748         size = partition_get_size(index);
 
1751                 fastboot_fail("Partition table doesn't exist\n");
 
1755         lun = partition_get_lun(index);
 
1758 #if MMC_SDHCI_SUPPORT
 
1759         if (mmc_erase_card(ptn, size)) {
 
1760                 fastboot_fail("failed to erase partition\n");
 
1764         size = partition_get_size(index);
 
1765         if (size > DEFAULT_ERASE_SIZE)
 
1766                 size = DEFAULT_ERASE_SIZE;
 
1768         /* Simple inefficient version of erase. Just writing
 
1769        0 in first several blocks */
 
1770         if (mmc_write(ptn , size, (unsigned int *)out)) {
 
1771                 fastboot_fail("failed to erase partition");
 
1779 void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
 
1781         unsigned long long ptn = 0;
 
1782         unsigned long long size = 0;
 
1783         int index = INVALID_PTN;
 
1787         bool lun_set = false;
 
1789         token = strtok(arg, ":");
 
1791         token = strtok(NULL, ":");
 
1801                 if (!strcmp(pname, "partition"))
 
1803                         dprintf(INFO, "Attempt to write partition image.\n");
 
1804                         if (write_partition(sz, (unsigned char *) data)) {
 
1805                                 fastboot_fail("failed to write partition");
 
1811                         index = partition_get_index(pname);
 
1812                         ptn = partition_get_offset(index);
 
1814                                 fastboot_fail("partition table doesn't exist");
 
1818                         if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
 
1819                                 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
1820                                         fastboot_fail("image is not a boot image");
 
1827                                 lun = partition_get_lun(index);
 
1831                         size = partition_get_size(index);
 
1832                         if (ROUND_TO_PAGE(sz,511) > size) {
 
1833                                 fastboot_fail("size too large");
 
1836                         else if (mmc_write(ptn , sz, (unsigned int *)data)) {
 
1837                                 fastboot_fail("flash write failure");
 
1846 void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
 
1849         unsigned int chunk_data_sz;
 
1850         uint32_t *fill_buf = NULL;
 
1852         uint32_t chunk_blk_cnt = 0;
 
1853         sparse_header_t *sparse_header;
 
1854         chunk_header_t *chunk_header;
 
1855         uint32_t total_blocks = 0;
 
1856         unsigned long long ptn = 0;
 
1857         unsigned long long size = 0;
 
1858         int index = INVALID_PTN;
 
1862         index = partition_get_index(arg);
 
1863         ptn = partition_get_offset(index);
 
1865                 fastboot_fail("partition table doesn't exist");
 
1869         size = partition_get_size(index);
 
1870         if (ROUND_TO_PAGE(sz,511) > size) {
 
1871                 fastboot_fail("size too large");
 
1875         lun = partition_get_lun(index);
 
1878         /* Read and skip over sparse image header */
 
1879         sparse_header = (sparse_header_t *) data;
 
1880         if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
 
1881                 fastboot_fail("size too large");
 
1885         data += sparse_header->file_hdr_sz;
 
1886         if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
 
1888                 /* Skip the remaining bytes in a header that is longer than
 
1891                 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
 
1894         dprintf (SPEW, "=== Sparse Image Header ===\n");
 
1895         dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
 
1896         dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
 
1897         dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
 
1898         dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
 
1899         dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
 
1900         dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
 
1901         dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
 
1902         dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
 
1904         /* Start processing chunks */
 
1905         for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
 
1907                 /* Read and skip over chunk header */
 
1908                 chunk_header = (chunk_header_t *) data;
 
1909                 data += sizeof(chunk_header_t);
 
1911                 dprintf (SPEW, "=== Chunk Header ===\n");
 
1912                 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
 
1913                 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
 
1914                 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
 
1916                 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
 
1918                         /* Skip the remaining bytes in a header that is longer than
 
1921                         data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
 
1924                 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
 
1925                 switch (chunk_header->chunk_type)
 
1927                         case CHUNK_TYPE_RAW:
 
1928                         if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
 
1931                                 fastboot_fail("Bogus chunk size for chunk type Raw");
 
1935                         if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
 
1937                                                 (unsigned int*)data))
 
1939                                 fastboot_fail("flash write failure");
 
1942                         total_blocks += chunk_header->chunk_sz;
 
1943                         data += chunk_data_sz;
 
1946                         case CHUNK_TYPE_FILL:
 
1947                         if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
 
1950                                 fastboot_fail("Bogus chunk size for chunk type FILL");
 
1954                         fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
 
1957                                 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
 
1961                         fill_val = *(uint32_t *)data;
 
1962                         data = (char *) data + sizeof(uint32_t);
 
1963                         chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
 
1965                         for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
 
1967                                 fill_buf[i] = fill_val;
 
1970                         for (i = 0; i < chunk_blk_cnt; i++)
 
1972                                 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
 
1973                                                         sparse_header->blk_sz,
 
1976                                         fastboot_fail("flash write failure");
 
1987                         case CHUNK_TYPE_DONT_CARE:
 
1988                         total_blocks += chunk_header->chunk_sz;
 
1991                         case CHUNK_TYPE_CRC:
 
1992                         if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
 
1994                                 fastboot_fail("Bogus chunk size for chunk type Dont Care");
 
1997                         total_blocks += chunk_header->chunk_sz;
 
1998                         data += chunk_data_sz;
 
2002                         dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
 
2003                         fastboot_fail("Unknown chunk type");
 
2008         dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
 
2009                                         total_blocks, sparse_header->total_blks);
 
2011         if(total_blocks != sparse_header->total_blks)
 
2013                 fastboot_fail("sparse image write failure");
 
2020 void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
 
2022         sparse_header_t *sparse_header;
 
2023         /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
 
2024         unsigned int *magic_number = (unsigned int *) data;
 
2028         uint32           major_version=0;
 
2029         uint32           minor_version=0;
 
2031         ret = scm_svc_version(&major_version,&minor_version);
 
2034                 if(major_version >= 2)
 
2036                         if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
 
2038                                 ret = encrypt_scm((uint32 **) &data, &sz);
 
2040                                         dprintf(CRITICAL, "ERROR: Encryption Failure\n");
 
2044                                 /* Protect only for SSD */
 
2045                                 if (!strcmp(arg, "ssd")) {
 
2046                                         ret = scm_protect_keystore((uint32 *) data, sz);
 
2048                                                 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
 
2055                                 ret = decrypt_scm_v2((uint32 **) &data, &sz);
 
2058                                         dprintf(CRITICAL,"ERROR: Decryption Failure\n");
 
2065                         if (magic_number[0] == DECRYPT_MAGIC_0 &&
 
2066                         magic_number[1] == DECRYPT_MAGIC_1)
 
2068                                 ret = decrypt_scm((uint32 **) &data, &sz);
 
2070                                         dprintf(CRITICAL, "ERROR: Invalid secure image\n");
 
2074                         else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
 
2075                                 magic_number[1] == ENCRYPT_MAGIC_1)
 
2077                                 ret = encrypt_scm((uint32 **) &data, &sz);
 
2079                                         dprintf(CRITICAL, "ERROR: Encryption Failure\n");
 
2087                 dprintf(CRITICAL,"INVALID SVC Version\n");
 
2090 #endif /* SSD_ENABLE */
 
2092         sparse_header = (sparse_header_t *) data;
 
2093         if (sparse_header->magic != SPARSE_HEADER_MAGIC)
 
2094                 cmd_flash_mmc_img(arg, data, sz);
 
2096                 cmd_flash_mmc_sparse_img(arg, data, sz);
 
2100 void cmd_flash(const char *arg, void *data, unsigned sz)
 
2102         struct ptentry *ptn;
 
2103         struct ptable *ptable;
 
2106         ptable = flash_get_ptable();
 
2107         if (ptable == NULL) {
 
2108                 fastboot_fail("partition table doesn't exist");
 
2112         ptn = ptable_find(ptable, arg);
 
2114                 fastboot_fail("unknown partition name");
 
2118         if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
 
2119                 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
 
2120                         fastboot_fail("image is not a boot image");
 
2125         if (!strcmp(ptn->name, "system")
 
2126                 || !strcmp(ptn->name, "userdata")
 
2127                 || !strcmp(ptn->name, "persist")
 
2128                 || !strcmp(ptn->name, "recoveryfs")
 
2129                 || !strcmp(ptn->name, "modem"))
 
2131                 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
 
2137                 sz = ROUND_TO_PAGE(sz, page_mask);
 
2139         dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
 
2140         if (flash_write(ptn, extra, data, sz)) {
 
2141                 fastboot_fail("flash write failure");
 
2144         dprintf(INFO, "partition '%s' updated\n", ptn->name);
 
2148 void cmd_continue(const char *arg, void *data, unsigned sz)
 
2152         if (target_is_emmc_boot())
 
2154                 boot_linux_from_mmc();
 
2158                 boot_linux_from_flash();
 
2162 void cmd_reboot(const char *arg, void *data, unsigned sz)
 
2164         dprintf(INFO, "rebooting the device\n");
 
2169 void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
 
2171         dprintf(INFO, "rebooting the device\n");
 
2173         reboot_device(FASTBOOT_MODE);
 
2176 void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
 
2178         dprintf(INFO, "Enabling charger screen check\n");
 
2179         device.charger_screen_enabled = 1;
 
2180         write_device_info(&device);
 
2184 void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
 
2186         dprintf(INFO, "Disabling charger screen check\n");
 
2187         device.charger_screen_enabled = 0;
 
2188         write_device_info(&device);
 
2192 void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
 
2194         dprintf(INFO, "Selecting display panel %s\n", arg);
 
2196                 strlcpy(device.display_panel, arg,
 
2197                         sizeof(device.display_panel));
 
2198         write_device_info(&device);
 
2202 void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
 
2204         if(!device.is_unlocked)
 
2206                 device.is_unlocked = 1;
 
2207                 write_device_info(&device);
 
2212 void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
 
2215         snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
 
2216         fastboot_info(response);
 
2217         snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
 
2218         fastboot_info(response);
 
2219         snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
 
2220         fastboot_info(response);
 
2221         snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
 
2222         fastboot_info(response);
 
2226 void cmd_preflash(const char *arg, void *data, unsigned sz)
 
2231 static struct fbimage logo_header = {0};
 
2232 struct fbimage* splash_screen_flash();
 
2234 int splash_screen_check_header(struct fbimage *logo)
 
2236         if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
 
2238         if (logo->header.width == 0 || logo->header.height == 0)
 
2243 struct fbimage* splash_screen_flash()
 
2245         struct ptentry *ptn;
 
2246         struct ptable *ptable;
 
2247         struct fbcon_config *fb_display = NULL;
 
2248         struct fbimage *logo = &logo_header;
 
2251         ptable = flash_get_ptable();
 
2252         if (ptable == NULL) {
 
2253         dprintf(CRITICAL, "ERROR: Partition table not found\n");
 
2256         ptn = ptable_find(ptable, "splash");
 
2258                 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
 
2262         if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
 
2263                 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
 
2267         if (splash_screen_check_header(logo)) {
 
2268                 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
 
2272         fb_display = fbcon_display();
 
2274                 uint8_t *base = (uint8_t *) fb_display->base;
 
2275                 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
 
2276                                 base += LOGO_IMG_OFFSET;
 
2279                 if (flash_read(ptn + sizeof(logo->header), 0,
 
2281                         ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
 
2283                         dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
 
2292 struct fbimage* splash_screen_mmc()
 
2294         int index = INVALID_PTN;
 
2295         unsigned long long ptn = 0;
 
2296         struct fbcon_config *fb_display = NULL;
 
2297         struct fbimage *logo = &logo_header;
 
2299         index = partition_get_index("splash");
 
2301                 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
 
2305         ptn = partition_get_offset(index);
 
2307                 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
 
2311         if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
 
2312                 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
 
2316         if (splash_screen_check_header(logo)) {
 
2317                 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
 
2321         fb_display = fbcon_display();
 
2323                 uint8_t *base = (uint8_t *) fb_display->base;
 
2324                 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
 
2325                                 base += LOGO_IMG_OFFSET;
 
2327                 if (mmc_read(ptn + sizeof(logo->header),
 
2329                         ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
 
2331                         dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
 
2342 struct fbimage* fetch_image_from_partition()
 
2344         if (target_is_emmc_boot()) {
 
2345                 return splash_screen_mmc();
 
2347                 return splash_screen_flash();
 
2351 /* Get the size from partiton name */
 
2352 static void get_partition_size(const char *arg, char *response)
 
2356         int index = INVALID_PTN;
 
2358         index = partition_get_index(arg);
 
2360         if (index == INVALID_PTN)
 
2362                 dprintf(CRITICAL, "Invalid partition index\n");
 
2366         ptn = partition_get_offset(index);
 
2370                 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
 
2374         size = partition_get_size(index);
 
2376         snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
 
2381  * Publish the partition type & size info
 
2382  * fastboot getvar will publish the required information.
 
2383  * fastboot getvar partition_size:<partition_name>: partition size in hex
 
2384  * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
 
2386 static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
 
2390         for (i = 0; i < num_parts; i++) {
 
2391                 get_partition_size(info[i].part_name, info[i].size_response);
 
2393                 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
 
2395                         dprintf(CRITICAL, "partition size name truncated\n");
 
2398                 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
 
2400                         dprintf(CRITICAL, "partition type name truncated\n");
 
2404                 /* publish partition size & type info */
 
2405                 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
 
2406                 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
 
2410 /* register commands and variables for fastboot */
 
2411 void aboot_fastboot_register_commands(void)
 
2413         if (target_is_emmc_boot())
 
2415                 fastboot_register("flash:", cmd_flash_mmc);
 
2416                 fastboot_register("erase:", cmd_erase_mmc);
 
2420                 fastboot_register("flash:", cmd_flash);
 
2421                 fastboot_register("erase:", cmd_erase);
 
2424         fastboot_register("boot",              cmd_boot);
 
2425         fastboot_register("continue",          cmd_continue);
 
2426         fastboot_register("reboot",            cmd_reboot);
 
2427         fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
 
2428         fastboot_register("oem unlock",        cmd_oem_unlock);
 
2429         fastboot_register("oem device-info",   cmd_oem_devinfo);
 
2430         fastboot_register("preflash",          cmd_preflash);
 
2431         fastboot_register("oem enable-charger-screen",
 
2432                         cmd_oem_enable_charger_screen);
 
2433         fastboot_register("oem disable-charger-screen",
 
2434                         cmd_oem_disable_charger_screen);
 
2435         fastboot_register("oem select-display-panel",
 
2436                         cmd_oem_select_display_panel);
 
2437         /* publish variables and their values */
 
2438         fastboot_publish("product",  TARGET(BOARD));
 
2439         fastboot_publish("kernel",   "lk");
 
2440         fastboot_publish("serialno", sn_buf);
 
2443          * partition info is supported only for emmc partitions
 
2444          * Calling this for NAND prints some error messages which
 
2445          * is harmless but misleading. Avoid calling this for NAND
 
2448         if (target_is_emmc_boot())
 
2449                 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
 
2451         /* Max download size supported */
 
2452         snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
 
2453                         target_get_max_flash_size());
 
2454         fastboot_publish("max-download-size", (const char *) max_download_size);
 
2455         /* Is the charger screen check enabled */
 
2456         snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
 
2457                         device.charger_screen_enabled);
 
2458         fastboot_publish("charger-screen-enabled",
 
2459                         (const char *) charger_screen_enabled);
 
2460         snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
 
2461                         device.display_panel);
 
2462         fastboot_publish("display-panel",
 
2463                         (const char *) panel_display_mode);
 
2466 void aboot_init(const struct app_descriptor *app)
 
2468         unsigned reboot_mode = 0;
 
2469         bool boot_into_fastboot = false;
 
2471         /* Setup page size information for nv storage */
 
2472         if (target_is_emmc_boot())
 
2474                 page_size = mmc_page_size();
 
2475                 page_mask = page_size - 1;
 
2479                 page_size = flash_page_size();
 
2480                 page_mask = page_size - 1;
 
2483         ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
 
2485         read_device_info(&device);
 
2487         /* Display splash screen if enabled */
 
2488 #if DISPLAY_SPLASH_SCREEN
 
2489         dprintf(SPEW, "Display Init: Start\n");
 
2490         target_display_init(device.display_panel);
 
2491         dprintf(SPEW, "Display Init: Done\n");
 
2495         target_serialno((unsigned char *) sn_buf);
 
2496         dprintf(SPEW,"serial number: %s\n",sn_buf);
 
2498         memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
 
2500         /* Check if we should do something other than booting up */
 
2501         if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
 
2503                 dprintf(ALWAYS,"dload mode key sequence detected\n");
 
2504                 if (set_download_mode(EMERGENCY_DLOAD))
 
2506                         dprintf(CRITICAL,"dload mode not supported by target\n");
 
2510                         reboot_device(DLOAD);
 
2511                         dprintf(CRITICAL,"Failed to reboot into dload mode\n");
 
2513                 boot_into_fastboot = true;
 
2515         if (!boot_into_fastboot)
 
2517                 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
 
2518                         boot_into_recovery = 1;
 
2519                 if (!boot_into_recovery &&
 
2520                         (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
 
2521                         boot_into_fastboot = true;
 
2523         #if NO_KEYPAD_DRIVER
 
2524         if (fastboot_trigger())
 
2525                 boot_into_fastboot = true;
 
2528         reboot_mode = check_reboot_mode();
 
2529         if (reboot_mode == RECOVERY_MODE) {
 
2530                 boot_into_recovery = 1;
 
2531         } else if(reboot_mode == FASTBOOT_MODE) {
 
2532                 boot_into_fastboot = true;
 
2535         if (!boot_into_fastboot)
 
2537                 if (target_is_emmc_boot())
 
2539                         if(emmc_recovery_init())
 
2540                                 dprintf(ALWAYS,"error in emmc_recovery_init\n");
 
2541                         if(target_use_signed_kernel())
 
2543                                 if((device.is_unlocked) || (device.is_tampered))
 
2545                                 #ifdef TZ_TAMPER_FUSE
 
2546                                         set_tamper_fuse_cmd();
 
2548                                 #if USE_PCOM_SECBOOT
 
2549                                         set_tamper_flag(device.is_tampered);
 
2553                         boot_linux_from_mmc();
 
2558         #if USE_PCOM_SECBOOT
 
2559                 if((device.is_unlocked) || (device.is_tampered))
 
2560                         set_tamper_flag(device.is_tampered);
 
2562                         boot_linux_from_flash();
 
2564                 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
 
2565                         "to fastboot mode.\n");
 
2568         /* We are here means regular boot did not happen. Start fastboot. */
 
2570         /* register aboot specific fastboot commands */
 
2571         aboot_fastboot_register_commands();
 
2573         /* dump partition table for debug info */
 
2576         /* initialize and start fastboot */
 
2577         fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
 
2580 uint32_t get_page_size()
 
2586  * Calculated and save hash (SHA256) for non-signed boot image.
 
2588  * Hash the same data that is checked on the signed boot image.
 
2589  * Kernel and Ramdisk are already read to memory buffers.
 
2590  * Need to read the entire device-tree from mmc
 
2591  * since non-signed image only read the DT tags of the relevant platform.
 
2593  * @param kernel_addr - kernel bufer
 
2594  * @param kernel_actual - kernel size in bytes
 
2595  * @param ramdisk_addr - ramdisk buffer
 
2596  * @param ramdisk_actual - ramdisk size
 
2597  * @param ptn - partition
 
2598  * @param dt_offset - device tree offset on mmc partition
 
2601  * @return int - 0 on success, negative value on failure.
 
2603 int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
 
2604                    void *ramdisk_addr, unsigned ramdisk_actual,
 
2605                    unsigned long long ptn,
 
2606                    unsigned dt_offset, unsigned dt_size)
 
2608         SHA256_CTX sha256_ctx;
 
2609         char digest[32]={0};
 
2610         char *buf = (char *)target_get_scratch_address();
 
2611         unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
 
2612         unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
 
2614         SHA256_Init(&sha256_ctx);
 
2616         /* Read Boot Header */
 
2617         if (mmc_read(ptn, buf, page_size))
 
2619                 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
 
2622         /* Read entire Device Tree */
 
2623         if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
 
2625                 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
 
2628         SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
 
2629         SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
 
2630         SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
 
2631         SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
 
2633         SHA256_Final(digest, &sha256_ctx);
 
2635         save_kernel_hash_cmd(digest);
 
2636         dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);