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 int android_image_get_fdt(const struct andr_img_hdr *hdr, 151 ulong *rd_data) 152 { 153 if (!hdr->second_size) { 154 *rd_data = 0; 155 return -1; 156 } 157 158 printf("FDT load addr 0x%08x size %u KiB\n", 159 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 160 161 *rd_data = (unsigned long)hdr; 162 *rd_data += hdr->page_size; 163 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); 164 *rd_data += ALIGN(hdr->ramdisk_size, hdr->page_size); 165 166 #ifdef CONFIG_ROCKCHIP_BOOTLOADER 167 rockchip_get_resource_file(rd_data, "rk-kernel.dtb"); 168 #endif 169 return 0; 170 } 171 172 long android_image_load(struct blk_desc *dev_desc, 173 const disk_partition_t *part_info, 174 unsigned long load_address, 175 unsigned long max_size) { 176 void *buf; 177 long blk_cnt, blk_read = 0; 178 179 if (max_size < part_info->blksz) 180 return -1; 181 182 /* We don't know the size of the Android image before reading the header 183 * so we don't limit the size of the mapped memory. 184 */ 185 buf = map_sysmem(load_address, 0 /* size */); 186 187 /* Read the Android header first and then read the rest. */ 188 if (blk_dread(dev_desc, part_info->start, 1, buf) != 1) 189 blk_read = -1; 190 191 if (!blk_read && android_image_check_header(buf) != 0) { 192 printf("** Invalid Android Image header **\n"); 193 blk_read = -1; 194 } 195 if (!blk_read) { 196 blk_cnt = (android_image_get_end(buf) - (ulong)buf + 197 part_info->blksz - 1) / part_info->blksz; 198 if (blk_cnt * part_info->blksz > max_size) { 199 debug("Android Image too big (%lu bytes, max %lu)\n", 200 android_image_get_end(buf) - (ulong)buf, 201 max_size); 202 blk_read = -1; 203 } else { 204 debug("Loading Android Image (%lu blocks) to 0x%lx... ", 205 blk_cnt, load_address); 206 blk_read = blk_dread(dev_desc, part_info->start, 207 blk_cnt, buf); 208 } 209 } 210 211 unmap_sysmem(buf); 212 if (blk_read < 0) 213 return blk_read; 214 215 debug("%lu blocks read: %s\n", 216 blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR"); 217 if (blk_read != blk_cnt) 218 return -1; 219 return blk_read; 220 } 221 222 #if !defined(CONFIG_SPL_BUILD) 223 /** 224 * android_print_contents - prints out the contents of the Android format image 225 * @hdr: pointer to the Android format image header 226 * 227 * android_print_contents() formats a multi line Android image contents 228 * description. 229 * The routine prints out Android image properties 230 * 231 * returns: 232 * no returned results 233 */ 234 void android_print_contents(const struct andr_img_hdr *hdr) 235 { 236 const char * const p = IMAGE_INDENT_STRING; 237 /* os_version = ver << 11 | lvl */ 238 u32 os_ver = hdr->os_version >> 11; 239 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 240 241 printf("%skernel size: %x\n", p, hdr->kernel_size); 242 printf("%skernel address: %x\n", p, hdr->kernel_addr); 243 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 244 printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr); 245 printf("%ssecond size: %x\n", p, hdr->second_size); 246 printf("%ssecond address: %x\n", p, hdr->second_addr); 247 printf("%stags address: %x\n", p, hdr->tags_addr); 248 printf("%spage size: %x\n", p, hdr->page_size); 249 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 250 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 251 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 252 p, hdr->os_version, 253 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 254 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 255 printf("%sname: %s\n", p, hdr->name); 256 printf("%scmdline: %s\n", p, hdr->cmdline); 257 } 258 #endif 259