image-android.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. return end;
  117. }
  118. ulong android_image_get_kload(const struct andr_img_hdr *hdr)
  119. {
  120. return android_image_get_kernel_addr(hdr);
  121. }
  122. ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
  123. {
  124. const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
  125. if (image_get_magic((image_header_t *)p) == IH_MAGIC)
  126. return image_get_comp((image_header_t *)p);
  127. else if (get_unaligned_le32(p) == LZ4F_MAGIC)
  128. return IH_COMP_LZ4;
  129. else
  130. return IH_COMP_NONE;
  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. printf("RAM disk load addr 0x%08x size %u KiB\n",
  140. hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
  141. *rd_data = (unsigned long)hdr;
  142. *rd_data += hdr->page_size;
  143. *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
  144. *rd_len = hdr->ramdisk_size;
  145. return 0;
  146. }
  147. int android_image_get_second(const struct andr_img_hdr *hdr,
  148. ulong *second_data, ulong *second_len)
  149. {
  150. if (!hdr->second_size) {
  151. *second_data = *second_len = 0;
  152. return -1;
  153. }
  154. *second_data = (unsigned long)hdr;
  155. *second_data += hdr->page_size;
  156. *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
  157. *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
  158. printf("second address is 0x%lx\n",*second_data);
  159. *second_len = hdr->second_size;
  160. return 0;
  161. }
  162. #if !defined(CONFIG_SPL_BUILD)
  163. /**
  164. * android_print_contents - prints out the contents of the Android format image
  165. * @hdr: pointer to the Android format image header
  166. *
  167. * android_print_contents() formats a multi line Android image contents
  168. * description.
  169. * The routine prints out Android image properties
  170. *
  171. * returns:
  172. * no returned results
  173. */
  174. void android_print_contents(const struct andr_img_hdr *hdr)
  175. {
  176. const char * const p = IMAGE_INDENT_STRING;
  177. /* os_version = ver << 11 | lvl */
  178. u32 os_ver = hdr->os_version >> 11;
  179. u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
  180. printf("%skernel size: %x\n", p, hdr->kernel_size);
  181. printf("%skernel address: %x\n", p, hdr->kernel_addr);
  182. printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
  183. printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
  184. printf("%ssecond size: %x\n", p, hdr->second_size);
  185. printf("%ssecond address: %x\n", p, hdr->second_addr);
  186. printf("%stags address: %x\n", p, hdr->tags_addr);
  187. printf("%spage size: %x\n", p, hdr->page_size);
  188. /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
  189. * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
  190. printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
  191. p, hdr->os_version,
  192. (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
  193. (os_lvl >> 4) + 2000, os_lvl & 0x0F);
  194. printf("%sname: %s\n", p, hdr->name);
  195. printf("%scmdline: %s\n", p, hdr->cmdline);
  196. }
  197. #endif