image-android.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <image.h>
  8. #include <android_image.h>
  9. #include <malloc.h>
  10. #include <errno.h>
  11. static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
  12. /**
  13. * android_image_get_kernel() - processes kernel part of Android boot images
  14. * @hdr: Pointer to image header, which is at the start
  15. * of the image.
  16. * @verify: Checksum verification flag. Currently unimplemented.
  17. * @os_data: Pointer to a ulong variable, will hold os data start
  18. * address.
  19. * @os_len: Pointer to a ulong variable, will hold os data length.
  20. *
  21. * This function returns the os image's start address and length. Also,
  22. * it appends the kernel command line to the bootargs env variable.
  23. *
  24. * Return: Zero, os start address and length on success,
  25. * otherwise on failure.
  26. */
  27. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  28. ulong *os_data, ulong *os_len)
  29. {
  30. /*
  31. * Not all Android tools use the id field for signing the image with
  32. * sha1 (or anything) so we don't check it. It is not obvious that the
  33. * string is null terminated so we take care of this.
  34. */
  35. strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
  36. andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
  37. if (strlen(andr_tmp_str))
  38. printf("Android's image name: %s\n", andr_tmp_str);
  39. printf("Kernel load addr 0x%08x size %u KiB\n",
  40. hdr->kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
  41. int len = 0;
  42. if (*hdr->cmdline) {
  43. printf("Kernel command line: %s\n", hdr->cmdline);
  44. len += strlen(hdr->cmdline);
  45. }
  46. char *bootargs = getenv("bootargs");
  47. if (bootargs)
  48. len += strlen(bootargs);
  49. char *newbootargs = malloc(len + 2);
  50. if (!newbootargs) {
  51. puts("Error: malloc in android_image_get_kernel failed!\n");
  52. return -ENOMEM;
  53. }
  54. *newbootargs = '\0';
  55. if (bootargs) {
  56. strcpy(newbootargs, bootargs);
  57. strcat(newbootargs, " ");
  58. }
  59. if (*hdr->cmdline)
  60. strcat(newbootargs, hdr->cmdline);
  61. setenv("bootargs", newbootargs);
  62. if (os_data) {
  63. *os_data = (ulong)hdr;
  64. *os_data += hdr->page_size;
  65. }
  66. if (os_len)
  67. *os_len = hdr->kernel_size;
  68. return 0;
  69. }
  70. int android_image_check_header(const struct andr_img_hdr *hdr)
  71. {
  72. return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
  73. }
  74. ulong android_image_get_end(const struct andr_img_hdr *hdr)
  75. {
  76. ulong end;
  77. /*
  78. * The header takes a full page, the remaining components are aligned
  79. * on page boundary
  80. */
  81. end = (ulong)hdr;
  82. end += hdr->page_size;
  83. end += ALIGN(hdr->kernel_size, hdr->page_size);
  84. end += ALIGN(hdr->ramdisk_size, hdr->page_size);
  85. end += ALIGN(hdr->second_size, hdr->page_size);
  86. return end;
  87. }
  88. ulong android_image_get_kload(const struct andr_img_hdr *hdr)
  89. {
  90. return hdr->kernel_addr;
  91. }
  92. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  93. ulong *rd_data, ulong *rd_len)
  94. {
  95. if (!hdr->ramdisk_size)
  96. return -1;
  97. printf("RAM disk load addr 0x%08x size %u KiB\n",
  98. hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
  99. *rd_data = (unsigned long)hdr;
  100. *rd_data += hdr->page_size;
  101. *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
  102. *rd_len = hdr->ramdisk_size;
  103. return 0;
  104. }