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