1 /* 2 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <image.h> 9 #include <android_image.h> 10 #include <malloc.h> 11 #include <mapmem.h> 12 #include <errno.h> 13 14 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 15 16 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; 17 18 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) 19 { 20 /* 21 * All the Android tools that generate a boot.img use this 22 * address as the default. 23 * 24 * Even though it doesn't really make a lot of sense, and it 25 * might be valid on some platforms, we treat that adress as 26 * the default value for this field, and try to execute the 27 * kernel in place in such a case. 28 * 29 * Otherwise, we will return the actual value set by the user. 30 */ 31 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) 32 return (ulong)hdr + hdr->page_size; 33 34 return hdr->kernel_addr; 35 } 36 37 /** 38 * android_image_get_kernel() - processes kernel part of Android boot images 39 * @hdr: Pointer to image header, which is at the start 40 * of the image. 41 * @verify: Checksum verification flag. Currently unimplemented. 42 * @os_data: Pointer to a ulong variable, will hold os data start 43 * address. 44 * @os_len: Pointer to a ulong variable, will hold os data length. 45 * 46 * This function returns the os image's start address and length. Also, 47 * it appends the kernel command line to the bootargs env variable. 48 * 49 * Return: Zero, os start address and length on success, 50 * otherwise on failure. 51 */ 52 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, 53 ulong *os_data, ulong *os_len) 54 { 55 u32 kernel_addr = android_image_get_kernel_addr(hdr); 56 57 /* 58 * Not all Android tools use the id field for signing the image with 59 * sha1 (or anything) so we don't check it. It is not obvious that the 60 * string is null terminated so we take care of this. 61 */ 62 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 63 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 64 if (strlen(andr_tmp_str)) 65 printf("Android's image name: %s\n", andr_tmp_str); 66 67 printf("Kernel load addr 0x%08x size %u KiB\n", 68 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024)); 69 70 int len = 0; 71 if (*hdr->cmdline) { 72 printf("Kernel command line: %s\n", hdr->cmdline); 73 len += strlen(hdr->cmdline); 74 } 75 76 char *bootargs = env_get("bootargs"); 77 if (bootargs) 78 len += strlen(bootargs); 79 80 char *newbootargs = malloc(len + 2); 81 if (!newbootargs) { 82 puts("Error: malloc in android_image_get_kernel failed!\n"); 83 return -ENOMEM; 84 } 85 *newbootargs = '\0'; 86 87 if (bootargs) { 88 strcpy(newbootargs, bootargs); 89 strcat(newbootargs, " "); 90 } 91 if (*hdr->cmdline) 92 strcat(newbootargs, hdr->cmdline); 93 94 env_set("bootargs", newbootargs); 95 96 if (os_data) { 97 *os_data = (ulong)hdr; 98 *os_data += hdr->page_size; 99 } 100 if (os_len) 101 *os_len = hdr->kernel_size; 102 return 0; 103 } 104 105 int android_image_check_header(const struct andr_img_hdr *hdr) 106 { 107 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); 108 } 109 110 ulong android_image_get_end(const struct andr_img_hdr *hdr) 111 { 112 ulong end; 113 /* 114 * The header takes a full page, the remaining components are aligned 115 * on page boundary 116 */ 117 end = (ulong)hdr; 118 end += hdr->page_size; 119 end += ALIGN(hdr->kernel_size, hdr->page_size); 120 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 121 end += ALIGN(hdr->second_size, hdr->page_size); 122 123 return end; 124 } 125 126 ulong android_image_get_kload(const struct andr_img_hdr *hdr) 127 { 128 return android_image_get_kernel_addr(hdr); 129 } 130 131 int android_image_get_ramdisk(const struct andr_img_hdr *hdr, 132 ulong *rd_data, ulong *rd_len) 133 { 134 if (!hdr->ramdisk_size) { 135 *rd_data = *rd_len = 0; 136 return -1; 137 } 138 139 printf("RAM disk load addr 0x%08x size %u KiB\n", 140 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 141 142 *rd_data = (unsigned long)hdr; 143 *rd_data += hdr->page_size; 144 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); 145 146 *rd_len = hdr->ramdisk_size; 147 return 0; 148 } 149 150 long android_image_load(struct blk_desc *dev_desc, 151 const disk_partition_t *part_info, 152 unsigned long load_address, 153 unsigned long max_size) { 154 void *buf; 155 long blk_cnt, blk_read = 0; 156 157 if (max_size < part_info->blksz) 158 return -1; 159 160 /* We don't know the size of the Android image before reading the header 161 * so we don't limit the size of the mapped memory. 162 */ 163 buf = map_sysmem(load_address, 0 /* size */); 164 165 /* Read the Android header first and then read the rest. */ 166 if (blk_dread(dev_desc, part_info->start, 1, buf) != 1) 167 blk_read = -1; 168 169 if (!blk_read && android_image_check_header(buf) != 0) { 170 printf("** Invalid Android Image header **\n"); 171 blk_read = -1; 172 } 173 if (!blk_read) { 174 blk_cnt = (android_image_get_end(buf) - (ulong)buf + 175 part_info->blksz - 1) / part_info->blksz; 176 if (blk_cnt * part_info->blksz > max_size) { 177 debug("Android Image too big (%lu bytes, max %lu)\n", 178 android_image_get_end(buf) - (ulong)buf, 179 max_size); 180 blk_read = -1; 181 } else { 182 debug("Loading Android Image (%lu blocks) to 0x%lx... ", 183 blk_cnt, load_address); 184 blk_read = blk_dread(dev_desc, part_info->start, 185 blk_cnt, buf); 186 } 187 } 188 189 unmap_sysmem(buf); 190 if (blk_read < 0) 191 return blk_read; 192 193 debug("%lu blocks read: %s\n", 194 blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR"); 195 if (blk_read != blk_cnt) 196 return -1; 197 return blk_read; 198 } 199 200 #if !defined(CONFIG_SPL_BUILD) 201 /** 202 * android_print_contents - prints out the contents of the Android format image 203 * @hdr: pointer to the Android format image header 204 * 205 * android_print_contents() formats a multi line Android image contents 206 * description. 207 * The routine prints out Android image properties 208 * 209 * returns: 210 * no returned results 211 */ 212 void android_print_contents(const struct andr_img_hdr *hdr) 213 { 214 const char * const p = IMAGE_INDENT_STRING; 215 /* os_version = ver << 11 | lvl */ 216 u32 os_ver = hdr->os_version >> 11; 217 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 218 219 printf("%skernel size: %x\n", p, hdr->kernel_size); 220 printf("%skernel address: %x\n", p, hdr->kernel_addr); 221 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 222 printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr); 223 printf("%ssecond size: %x\n", p, hdr->second_size); 224 printf("%ssecond address: %x\n", p, hdr->second_addr); 225 printf("%stags address: %x\n", p, hdr->tags_addr); 226 printf("%spage size: %x\n", p, hdr->page_size); 227 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 228 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 229 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 230 p, hdr->os_version, 231 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 232 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 233 printf("%sname: %s\n", p, hdr->name); 234 printf("%scmdline: %s\n", p, hdr->cmdline); 235 } 236 #endif 237