image-android.c 5.8 KB

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