image-android.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <image.h>
  8. #include <android_image.h>
  9. #include <malloc.h>
  10. #include <errno.h>
  11. #include <asm/unaligned.h>
  12. #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
  13. static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
  14. static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
  15. {
  16. /*
  17. * All the Android tools that generate a boot.img use this
  18. * address as the default.
  19. *
  20. * Even though it doesn't really make a lot of sense, and it
  21. * might be valid on some platforms, we treat that adress as
  22. * the default value for this field, and try to execute the
  23. * kernel in place in such a case.
  24. *
  25. * Otherwise, we will return the actual value set by the user.
  26. */
  27. if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
  28. return (ulong)hdr + hdr->page_size;
  29. return hdr->kernel_addr;
  30. }
  31. /**
  32. * android_image_get_kernel() - processes kernel part of Android boot images
  33. * @hdr: Pointer to image header, which is at the start
  34. * of the image.
  35. * @verify: Checksum verification flag. Currently unimplemented.
  36. * @os_data: Pointer to a ulong variable, will hold os data start
  37. * address.
  38. * @os_len: Pointer to a ulong variable, will hold os data length.
  39. *
  40. * This function returns the os image's start address and length. Also,
  41. * it appends the kernel command line to the bootargs env variable.
  42. *
  43. * Return: Zero, os start address and length on success,
  44. * otherwise on failure.
  45. */
  46. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  47. ulong *os_data, ulong *os_len)
  48. {
  49. u32 kernel_addr = android_image_get_kernel_addr(hdr);
  50. const struct image_header *ihdr = (const struct image_header *)
  51. ((uintptr_t)hdr + hdr->page_size);
  52. /*
  53. * Not all Android tools use the id field for signing the image with
  54. * sha1 (or anything) so we don't check it. It is not obvious that the
  55. * string is null terminated so we take care of this.
  56. */
  57. strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
  58. andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
  59. if (strlen(andr_tmp_str))
  60. printf("Android's image name: %s\n", andr_tmp_str);
  61. printf("Kernel load addr 0x%08x size %u KiB\n",
  62. kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
  63. int len = 0;
  64. if (*hdr->cmdline) {
  65. printf("Kernel command line: %s\n", hdr->cmdline);
  66. len += strlen(hdr->cmdline);
  67. }
  68. char *bootargs = env_get("bootargs");
  69. if (bootargs)
  70. len += strlen(bootargs);
  71. char *newbootargs = malloc(len + 2);
  72. if (!newbootargs) {
  73. puts("Error: malloc in android_image_get_kernel failed!\n");
  74. return -ENOMEM;
  75. }
  76. *newbootargs = '\0';
  77. if (bootargs) {
  78. strcpy(newbootargs, bootargs);
  79. strcat(newbootargs, " ");
  80. }
  81. if (*hdr->cmdline)
  82. strcat(newbootargs, hdr->cmdline);
  83. env_set("bootargs", newbootargs);
  84. if (os_data) {
  85. if (image_get_magic(ihdr) == IH_MAGIC) {
  86. *os_data = image_get_data(ihdr);
  87. } else {
  88. *os_data = (ulong)hdr;
  89. *os_data += hdr->page_size;
  90. }
  91. }
  92. if (os_len) {
  93. if (image_get_magic(ihdr) == IH_MAGIC)
  94. *os_len = image_get_data_size(ihdr);
  95. else
  96. *os_len = hdr->kernel_size;
  97. }
  98. return 0;
  99. }
  100. int android_image_check_header(const struct andr_img_hdr *hdr)
  101. {
  102. return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
  103. }
  104. ulong android_image_get_end(const struct andr_img_hdr *hdr)
  105. {
  106. ulong end;
  107. /*
  108. * The header takes a full page, the remaining components are aligned
  109. * on page boundary
  110. */
  111. end = (ulong)hdr;
  112. end += hdr->page_size;
  113. end += ALIGN(hdr->kernel_size, hdr->page_size);
  114. end += ALIGN(hdr->ramdisk_size, hdr->page_size);
  115. end += ALIGN(hdr->second_size, hdr->page_size);
  116. if (hdr->header_version >= 1)
  117. end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  118. if (hdr->header_version >= 2)
  119. end += ALIGN(hdr->dtb_size, hdr->page_size);
  120. return end;
  121. }
  122. ulong android_image_get_kload(const struct andr_img_hdr *hdr)
  123. {
  124. return android_image_get_kernel_addr(hdr);
  125. }
  126. ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
  127. {
  128. const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
  129. if (image_get_magic((image_header_t *)p) == IH_MAGIC)
  130. return image_get_comp((image_header_t *)p);
  131. else if (get_unaligned_le32(p) == LZ4F_MAGIC)
  132. return IH_COMP_LZ4;
  133. else
  134. return IH_COMP_NONE;
  135. }
  136. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  137. ulong *rd_data, ulong *rd_len)
  138. {
  139. if (!hdr->ramdisk_size) {
  140. *rd_data = *rd_len = 0;
  141. return -1;
  142. }
  143. printf("RAM disk load addr 0x%08x size %u KiB\n",
  144. hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
  145. *rd_data = (unsigned long)hdr;
  146. *rd_data += hdr->page_size;
  147. *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
  148. *rd_len = hdr->ramdisk_size;
  149. return 0;
  150. }
  151. int android_image_get_second(const struct andr_img_hdr *hdr,
  152. ulong *second_data, ulong *second_len)
  153. {
  154. if (!hdr->second_size) {
  155. *second_data = *second_len = 0;
  156. return -1;
  157. }
  158. *second_data = (unsigned long)hdr;
  159. *second_data += hdr->page_size;
  160. *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
  161. *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
  162. printf("second address is 0x%lx\n",*second_data);
  163. *second_len = hdr->second_size;
  164. return 0;
  165. }
  166. #if !defined(CONFIG_SPL_BUILD)
  167. /**
  168. * android_print_contents - prints out the contents of the Android format image
  169. * @hdr: pointer to the Android format image header
  170. *
  171. * android_print_contents() formats a multi line Android image contents
  172. * description.
  173. * The routine prints out Android image properties
  174. *
  175. * returns:
  176. * no returned results
  177. */
  178. void android_print_contents(const struct andr_img_hdr *hdr)
  179. {
  180. const char * const p = IMAGE_INDENT_STRING;
  181. /* os_version = ver << 11 | lvl */
  182. u32 os_ver = hdr->os_version >> 11;
  183. u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
  184. printf("%skernel size: %x\n", p, hdr->kernel_size);
  185. printf("%skernel address: %x\n", p, hdr->kernel_addr);
  186. printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
  187. printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
  188. printf("%ssecond size: %x\n", p, hdr->second_size);
  189. printf("%ssecond address: %x\n", p, hdr->second_addr);
  190. printf("%stags address: %x\n", p, hdr->tags_addr);
  191. printf("%spage size: %x\n", p, hdr->page_size);
  192. /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
  193. * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
  194. printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
  195. p, hdr->os_version,
  196. (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
  197. (os_lvl >> 4) + 2000, os_lvl & 0x0F);
  198. printf("%sname: %s\n", p, hdr->name);
  199. printf("%scmdline: %s\n", p, hdr->cmdline);
  200. printf("%sheader_version: %d\n", p, hdr->header_version);
  201. if (hdr->header_version >= 1) {
  202. printf("%srecovery dtbo size: %x\n", p,
  203. hdr->recovery_dtbo_size);
  204. printf("%srecovery dtbo offset: %llx\n", p,
  205. hdr->recovery_dtbo_offset);
  206. printf("%sheader size: %x\n", p,
  207. hdr->header_size);
  208. }
  209. if (hdr->header_version >= 2) {
  210. printf("%sdtb size: %x\n", p, hdr->dtb_size);
  211. printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
  212. }
  213. }
  214. #endif