Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 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> |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <errno.h> |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 12 | |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 13 | #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 |
| 14 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 15 | static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; |
| 16 | |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 17 | static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) |
| 18 | { |
| 19 | /* |
| 20 | * All the Android tools that generate a boot.img use this |
| 21 | * address as the default. |
| 22 | * |
| 23 | * Even though it doesn't really make a lot of sense, and it |
| 24 | * might be valid on some platforms, we treat that adress as |
| 25 | * the default value for this field, and try to execute the |
| 26 | * kernel in place in such a case. |
| 27 | * |
| 28 | * Otherwise, we will return the actual value set by the user. |
| 29 | */ |
| 30 | if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) |
| 31 | return (ulong)hdr + hdr->page_size; |
| 32 | |
| 33 | return hdr->kernel_addr; |
| 34 | } |
| 35 | |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 36 | /** |
| 37 | * android_image_get_kernel() - processes kernel part of Android boot images |
| 38 | * @hdr: Pointer to image header, which is at the start |
| 39 | * of the image. |
| 40 | * @verify: Checksum verification flag. Currently unimplemented. |
| 41 | * @os_data: Pointer to a ulong variable, will hold os data start |
| 42 | * address. |
| 43 | * @os_len: Pointer to a ulong variable, will hold os data length. |
| 44 | * |
| 45 | * This function returns the os image's start address and length. Also, |
| 46 | * it appends the kernel command line to the bootargs env variable. |
| 47 | * |
| 48 | * Return: Zero, os start address and length on success, |
| 49 | * otherwise on failure. |
| 50 | */ |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 51 | int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, |
| 52 | ulong *os_data, ulong *os_len) |
| 53 | { |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 54 | u32 kernel_addr = android_image_get_kernel_addr(hdr); |
| 55 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 56 | /* |
| 57 | * Not all Android tools use the id field for signing the image with |
| 58 | * sha1 (or anything) so we don't check it. It is not obvious that the |
| 59 | * string is null terminated so we take care of this. |
| 60 | */ |
| 61 | strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); |
| 62 | andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; |
| 63 | if (strlen(andr_tmp_str)) |
| 64 | printf("Android's image name: %s\n", andr_tmp_str); |
| 65 | |
| 66 | printf("Kernel load addr 0x%08x size %u KiB\n", |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 67 | kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024)); |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 68 | |
| 69 | int len = 0; |
| 70 | if (*hdr->cmdline) { |
| 71 | printf("Kernel command line: %s\n", hdr->cmdline); |
| 72 | len += strlen(hdr->cmdline); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 73 | } |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 74 | |
| 75 | char *bootargs = getenv("bootargs"); |
| 76 | if (bootargs) |
| 77 | len += strlen(bootargs); |
| 78 | |
| 79 | char *newbootargs = malloc(len + 2); |
| 80 | if (!newbootargs) { |
| 81 | puts("Error: malloc in android_image_get_kernel failed!\n"); |
| 82 | return -ENOMEM; |
| 83 | } |
| 84 | *newbootargs = '\0'; |
| 85 | |
| 86 | if (bootargs) { |
| 87 | strcpy(newbootargs, bootargs); |
| 88 | strcat(newbootargs, " "); |
| 89 | } |
| 90 | if (*hdr->cmdline) |
| 91 | strcat(newbootargs, hdr->cmdline); |
| 92 | |
| 93 | setenv("bootargs", newbootargs); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 94 | |
| 95 | if (os_data) { |
| 96 | *os_data = (ulong)hdr; |
| 97 | *os_data += hdr->page_size; |
| 98 | } |
| 99 | if (os_len) |
| 100 | *os_len = hdr->kernel_size; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int android_image_check_header(const struct andr_img_hdr *hdr) |
| 105 | { |
| 106 | return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); |
| 107 | } |
| 108 | |
| 109 | ulong android_image_get_end(const struct andr_img_hdr *hdr) |
| 110 | { |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 111 | ulong end; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 112 | /* |
| 113 | * The header takes a full page, the remaining components are aligned |
| 114 | * on page boundary |
| 115 | */ |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 116 | end = (ulong)hdr; |
| 117 | end += hdr->page_size; |
| 118 | end += ALIGN(hdr->kernel_size, hdr->page_size); |
| 119 | end += ALIGN(hdr->ramdisk_size, hdr->page_size); |
| 120 | end += ALIGN(hdr->second_size, hdr->page_size); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 121 | |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 122 | return end; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ulong android_image_get_kload(const struct andr_img_hdr *hdr) |
| 126 | { |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 127 | return android_image_get_kernel_addr(hdr); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int android_image_get_ramdisk(const struct andr_img_hdr *hdr, |
| 131 | ulong *rd_data, ulong *rd_len) |
| 132 | { |
Rob Herring | d979878 | 2015-10-05 14:37:07 -0500 | [diff] [blame] | 133 | if (!hdr->ramdisk_size) { |
| 134 | *rd_data = *rd_len = 0; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 135 | return -1; |
Rob Herring | d979878 | 2015-10-05 14:37:07 -0500 | [diff] [blame] | 136 | } |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 137 | |
| 138 | printf("RAM disk load addr 0x%08x size %u KiB\n", |
| 139 | hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); |
| 140 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 141 | *rd_data = (unsigned long)hdr; |
| 142 | *rd_data += hdr->page_size; |
| 143 | *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); |
| 144 | |
| 145 | *rd_len = hdr->ramdisk_size; |
| 146 | return 0; |
| 147 | } |