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 #ifdef CONFIG_RKIMG_BOOTLOADER 14 #include <asm/arch/resource_img.h> 15 #endif 16 17 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 18 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb" 19 20 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; 21 static u32 android_kernel_comp_type = IH_COMP_NONE; 22 23 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) 24 { 25 /* 26 * All the Android tools that generate a boot.img use this 27 * address as the default. 28 * 29 * Even though it doesn't really make a lot of sense, and it 30 * might be valid on some platforms, we treat that adress as 31 * the default value for this field, and try to execute the 32 * kernel in place in such a case. 33 * 34 * Otherwise, we will return the actual value set by the user. 35 */ 36 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) 37 return (ulong)hdr + hdr->page_size; 38 39 return hdr->kernel_addr; 40 } 41 42 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp) 43 { 44 android_kernel_comp_type = comp; 45 } 46 47 u32 android_image_get_comp(const struct andr_img_hdr *hdr) 48 { 49 return android_kernel_comp_type; 50 } 51 52 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr) 53 { 54 ulong kaddr = android_image_get_kernel_addr(hdr); 55 return bootm_parse_comp((const unsigned char *)kaddr); 56 } 57 58 /** 59 * android_image_get_kernel() - processes kernel part of Android boot images 60 * @hdr: Pointer to image header, which is at the start 61 * of the image. 62 * @verify: Checksum verification flag. Currently unimplemented. 63 * @os_data: Pointer to a ulong variable, will hold os data start 64 * address. 65 * @os_len: Pointer to a ulong variable, will hold os data length. 66 * 67 * This function returns the os image's start address and length. Also, 68 * it appends the kernel command line to the bootargs env variable. 69 * 70 * Return: Zero, os start address and length on success, 71 * otherwise on failure. 72 */ 73 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, 74 ulong *os_data, ulong *os_len) 75 { 76 u32 kernel_addr = android_image_get_kernel_addr(hdr); 77 78 /* 79 * Not all Android tools use the id field for signing the image with 80 * sha1 (or anything) so we don't check it. It is not obvious that the 81 * string is null terminated so we take care of this. 82 */ 83 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 84 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 85 if (strlen(andr_tmp_str)) 86 printf("Android's image name: %s\n", andr_tmp_str); 87 88 printf("Kernel load addr 0x%08x size %u KiB\n", 89 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024)); 90 91 int len = 0; 92 if (*hdr->cmdline) { 93 printf("Kernel command line: %s\n", hdr->cmdline); 94 len += strlen(hdr->cmdline); 95 } 96 97 char *bootargs = env_get("bootargs"); 98 if (bootargs) 99 len += strlen(bootargs); 100 101 char *newbootargs = malloc(len + 2); 102 if (!newbootargs) { 103 puts("Error: malloc in android_image_get_kernel failed!\n"); 104 return -ENOMEM; 105 } 106 *newbootargs = '\0'; 107 108 if (bootargs) { 109 strcpy(newbootargs, bootargs); 110 strcat(newbootargs, " "); 111 } 112 if (*hdr->cmdline) 113 strcat(newbootargs, hdr->cmdline); 114 115 env_set("bootargs", newbootargs); 116 117 if (os_data) { 118 *os_data = (ulong)hdr; 119 *os_data += hdr->page_size; 120 } 121 if (os_len) 122 *os_len = hdr->kernel_size; 123 return 0; 124 } 125 126 int android_image_check_header(const struct andr_img_hdr *hdr) 127 { 128 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); 129 } 130 131 ulong android_image_get_end(const struct andr_img_hdr *hdr) 132 { 133 ulong end; 134 /* 135 * The header takes a full page, the remaining components are aligned 136 * on page boundary 137 */ 138 end = (ulong)hdr; 139 end += hdr->page_size; 140 end += ALIGN(hdr->kernel_size, hdr->page_size); 141 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 142 end += ALIGN(hdr->second_size, hdr->page_size); 143 144 if (hdr->header_version >= 1) 145 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 146 147 return end; 148 } 149 150 u32 android_image_get_ksize(const struct andr_img_hdr *hdr) 151 { 152 return hdr->kernel_size; 153 } 154 155 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address) 156 { 157 hdr->kernel_addr = load_address; 158 } 159 160 ulong android_image_get_kload(const struct andr_img_hdr *hdr) 161 { 162 return android_image_get_kernel_addr(hdr); 163 } 164 165 int android_image_get_ramdisk(const struct andr_img_hdr *hdr, 166 ulong *rd_data, ulong *rd_len) 167 { 168 if (!hdr->ramdisk_size) { 169 *rd_data = *rd_len = 0; 170 return -1; 171 } 172 173 printf("RAM disk load addr 0x%08x size %u KiB\n", 174 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 175 176 *rd_data = (unsigned long)hdr; 177 *rd_data += hdr->page_size; 178 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); 179 180 *rd_len = hdr->ramdisk_size; 181 return 0; 182 } 183 184 int android_image_get_fdt(const struct andr_img_hdr *hdr, 185 ulong *rd_data) 186 { 187 if (!hdr->second_size) { 188 *rd_data = 0; 189 return -1; 190 } 191 192 printf("FDT load addr 0x%08x size %u KiB\n", 193 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 194 195 *rd_data = (unsigned long)hdr; 196 *rd_data += hdr->page_size; 197 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); 198 *rd_data += ALIGN(hdr->ramdisk_size, hdr->page_size); 199 #ifdef CONFIG_RKIMG_BOOTLOADER 200 *rd_data += (rockchip_get_resource_file((void *)*rd_data, 201 ANDROID_ARG_FDT_FILENAME)) 202 * 512; 203 #endif 204 return 0; 205 } 206 207 long android_image_load(struct blk_desc *dev_desc, 208 const disk_partition_t *part_info, 209 unsigned long load_address, 210 unsigned long max_size) { 211 void *buf; 212 long blk_cnt = 0; 213 long blk_read = 0; 214 u32 comp; 215 u32 kload_addr; 216 217 if (max_size < part_info->blksz) 218 return -1; 219 220 /* We don't know the size of the Android image before reading the header 221 * so we don't limit the size of the mapped memory. 222 */ 223 buf = map_sysmem(load_address, 0 /* size */); 224 225 /* Read the Android boot.img header and a few parts of 226 * the head of kernel image. 227 */ 228 if (blk_dread(dev_desc, part_info->start, 8, buf) != 8) 229 blk_read = -1; 230 231 if (!blk_read && android_image_check_header(buf) != 0) { 232 printf("** Invalid Android Image header **\n"); 233 blk_read = -1; 234 } 235 236 237 if (!blk_read) { 238 blk_cnt = (android_image_get_end(buf) - (ulong)buf + 239 part_info->blksz - 1) / part_info->blksz; 240 comp = android_image_parse_kernel_comp(buf); 241 /* 242 * We should load a compressed kernel Image 243 * to high memory 244 */ 245 if (comp != IH_COMP_NONE) { 246 load_address += android_image_get_ksize(buf) * 3; 247 load_address = env_get_ulong("kernel_addr_c", 16, load_address); 248 unmap_sysmem(buf); 249 buf = map_sysmem(load_address, 0 /* size */); 250 } 251 252 if (blk_cnt * part_info->blksz > max_size) { 253 debug("Android Image too big (%lu bytes, max %lu)\n", 254 android_image_get_end(buf) - (ulong)buf, 255 max_size); 256 blk_read = -1; 257 } else { 258 debug("Loading Android Image (%lu blocks) to 0x%lx... ", 259 blk_cnt, load_address); 260 blk_read = blk_dread(dev_desc, part_info->start, 261 blk_cnt, buf); 262 } 263 264 /* 265 * zImage is not need to decompress 266 * kernel will handle decompress itself 267 */ 268 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 269 kload_addr = env_get_ulong("kernel_addr_r", 16, 0x02080000); 270 android_image_set_kload(buf, kload_addr); 271 android_image_set_comp(buf, comp); 272 } else { 273 android_image_set_comp(buf, IH_COMP_NONE); 274 } 275 276 } 277 278 unmap_sysmem(buf); 279 280 debug("%lu blocks read: %s\n", 281 blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR"); 282 if (blk_read != blk_cnt) 283 return -1; 284 285 return load_address; 286 } 287 288 #if !defined(CONFIG_SPL_BUILD) 289 /** 290 * android_print_contents - prints out the contents of the Android format image 291 * @hdr: pointer to the Android format image header 292 * 293 * android_print_contents() formats a multi line Android image contents 294 * description. 295 * The routine prints out Android image properties 296 * 297 * returns: 298 * no returned results 299 */ 300 void android_print_contents(const struct andr_img_hdr *hdr) 301 { 302 const char * const p = IMAGE_INDENT_STRING; 303 /* os_version = ver << 11 | lvl */ 304 u32 os_ver = hdr->os_version >> 11; 305 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 306 u32 header_version = hdr->header_version; 307 308 printf("%skernel size: %x\n", p, hdr->kernel_size); 309 printf("%skernel address: %x\n", p, hdr->kernel_addr); 310 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 311 printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr); 312 printf("%ssecond size: %x\n", p, hdr->second_size); 313 printf("%ssecond address: %x\n", p, hdr->second_addr); 314 printf("%stags address: %x\n", p, hdr->tags_addr); 315 printf("%spage size: %x\n", p, hdr->page_size); 316 printf("%sheader_version: %x\n", p, header_version); 317 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 318 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 319 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 320 p, hdr->os_version, 321 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 322 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 323 printf("%sname: %s\n", p, hdr->name); 324 printf("%scmdline: %s\n", p, hdr->cmdline); 325 326 if (header_version >= 1) { 327 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 328 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 329 printf("%sheader size: %x\n", p, hdr->header_size); 330 } 331 } 332 #endif 333