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