image-android.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <image-android-dt.h>
  9. #include <android_image.h>
  10. #include <malloc.h>
  11. #include <errno.h>
  12. #include <asm/unaligned.h>
  13. #include <mapmem.h>
  14. #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
  15. static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
  16. static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
  17. {
  18. /*
  19. * All the Android tools that generate a boot.img use this
  20. * address as the default.
  21. *
  22. * Even though it doesn't really make a lot of sense, and it
  23. * might be valid on some platforms, we treat that adress as
  24. * the default value for this field, and try to execute the
  25. * kernel in place in such a case.
  26. *
  27. * Otherwise, we will return the actual value set by the user.
  28. */
  29. if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
  30. return (ulong)hdr + hdr->page_size;
  31. return hdr->kernel_addr;
  32. }
  33. /**
  34. * android_image_get_kernel() - processes kernel part of Android boot images
  35. * @hdr: Pointer to image header, which is at the start
  36. * of the image.
  37. * @verify: Checksum verification flag. Currently unimplemented.
  38. * @os_data: Pointer to a ulong variable, will hold os data start
  39. * address.
  40. * @os_len: Pointer to a ulong variable, will hold os data length.
  41. *
  42. * This function returns the os image's start address and length. Also,
  43. * it appends the kernel command line to the bootargs env variable.
  44. *
  45. * Return: Zero, os start address and length on success,
  46. * otherwise on failure.
  47. */
  48. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  49. ulong *os_data, ulong *os_len)
  50. {
  51. u32 kernel_addr = android_image_get_kernel_addr(hdr);
  52. const struct image_header *ihdr = (const struct image_header *)
  53. ((uintptr_t)hdr + hdr->page_size);
  54. /*
  55. * Not all Android tools use the id field for signing the image with
  56. * sha1 (or anything) so we don't check it. It is not obvious that the
  57. * string is null terminated so we take care of this.
  58. */
  59. strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
  60. andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
  61. if (strlen(andr_tmp_str))
  62. printf("Android's image name: %s\n", andr_tmp_str);
  63. printf("Kernel load addr 0x%08x size %u KiB\n",
  64. kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
  65. int len = 0;
  66. if (*hdr->cmdline) {
  67. printf("Kernel command line: %s\n", hdr->cmdline);
  68. len += strlen(hdr->cmdline);
  69. }
  70. char *bootargs = env_get("bootargs");
  71. if (bootargs)
  72. len += strlen(bootargs);
  73. char *newbootargs = malloc(len + 2);
  74. if (!newbootargs) {
  75. puts("Error: malloc in android_image_get_kernel failed!\n");
  76. return -ENOMEM;
  77. }
  78. *newbootargs = '\0';
  79. if (bootargs) {
  80. strcpy(newbootargs, bootargs);
  81. strcat(newbootargs, " ");
  82. }
  83. if (*hdr->cmdline)
  84. strcat(newbootargs, hdr->cmdline);
  85. env_set("bootargs", newbootargs);
  86. if (os_data) {
  87. if (image_get_magic(ihdr) == IH_MAGIC) {
  88. *os_data = image_get_data(ihdr);
  89. } else {
  90. *os_data = (ulong)hdr;
  91. *os_data += hdr->page_size;
  92. }
  93. }
  94. if (os_len) {
  95. if (image_get_magic(ihdr) == IH_MAGIC)
  96. *os_len = image_get_data_size(ihdr);
  97. else
  98. *os_len = hdr->kernel_size;
  99. }
  100. return 0;
  101. }
  102. int android_image_check_header(const struct andr_img_hdr *hdr)
  103. {
  104. return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
  105. }
  106. ulong android_image_get_end(const struct andr_img_hdr *hdr)
  107. {
  108. ulong end;
  109. /*
  110. * The header takes a full page, the remaining components are aligned
  111. * on page boundary
  112. */
  113. end = (ulong)hdr;
  114. end += hdr->page_size;
  115. end += ALIGN(hdr->kernel_size, hdr->page_size);
  116. end += ALIGN(hdr->ramdisk_size, hdr->page_size);
  117. end += ALIGN(hdr->second_size, hdr->page_size);
  118. if (hdr->header_version >= 1)
  119. end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  120. if (hdr->header_version >= 2)
  121. end += ALIGN(hdr->dtb_size, hdr->page_size);
  122. return end;
  123. }
  124. ulong android_image_get_kload(const struct andr_img_hdr *hdr)
  125. {
  126. return android_image_get_kernel_addr(hdr);
  127. }
  128. ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
  129. {
  130. const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
  131. if (image_get_magic((image_header_t *)p) == IH_MAGIC)
  132. return image_get_comp((image_header_t *)p);
  133. else if (get_unaligned_le32(p) == LZ4F_MAGIC)
  134. return IH_COMP_LZ4;
  135. else
  136. return IH_COMP_NONE;
  137. }
  138. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  139. ulong *rd_data, ulong *rd_len)
  140. {
  141. if (!hdr->ramdisk_size) {
  142. *rd_data = *rd_len = 0;
  143. return -1;
  144. }
  145. printf("RAM disk load addr 0x%08x size %u KiB\n",
  146. hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
  147. *rd_data = (unsigned long)hdr;
  148. *rd_data += hdr->page_size;
  149. *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
  150. *rd_len = hdr->ramdisk_size;
  151. return 0;
  152. }
  153. int android_image_get_second(const struct andr_img_hdr *hdr,
  154. ulong *second_data, ulong *second_len)
  155. {
  156. if (!hdr->second_size) {
  157. *second_data = *second_len = 0;
  158. return -1;
  159. }
  160. *second_data = (unsigned long)hdr;
  161. *second_data += hdr->page_size;
  162. *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
  163. *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
  164. printf("second address is 0x%lx\n",*second_data);
  165. *second_len = hdr->second_size;
  166. return 0;
  167. }
  168. /**
  169. * android_image_get_dtbo() - Get address and size of recovery DTBO image.
  170. * @hdr_addr: Boot image header address
  171. * @addr: If not NULL, will contain address of recovery DTBO image
  172. * @size: If not NULL, will contain size of recovery DTBO image
  173. *
  174. * Get the address and size of DTBO image in "Recovery DTBO" area of Android
  175. * Boot Image in RAM. The format of this image is Android DTBO (see
  176. * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
  177. * the address is obtained from this function, one can use 'adtimg' U-Boot
  178. * command or android_dt_*() functions to extract desired DTBO blob.
  179. *
  180. * This DTBO (included in boot image) is only needed for non-A/B devices, and it
  181. * only can be found in recovery image. On A/B devices we can always rely on
  182. * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
  183. * AOSP documentation for details.
  184. *
  185. * Return: true on success or false on error.
  186. */
  187. bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
  188. {
  189. const struct andr_img_hdr *hdr;
  190. ulong dtbo_img_addr;
  191. bool ret = true;
  192. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  193. if (android_image_check_header(hdr)) {
  194. printf("Error: Boot Image header is incorrect\n");
  195. ret = false;
  196. goto exit;
  197. }
  198. if (hdr->header_version < 1) {
  199. printf("Error: header_version must be >= 1 to get dtbo\n");
  200. ret = false;
  201. goto exit;
  202. }
  203. if (hdr->recovery_dtbo_size == 0) {
  204. printf("Error: recovery_dtbo_size is 0\n");
  205. ret = false;
  206. goto exit;
  207. }
  208. /* Calculate the address of DTB area in boot image */
  209. dtbo_img_addr = hdr_addr;
  210. dtbo_img_addr += hdr->page_size;
  211. dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
  212. dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
  213. dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
  214. if (addr)
  215. *addr = dtbo_img_addr;
  216. if (size)
  217. *size = hdr->recovery_dtbo_size;
  218. exit:
  219. unmap_sysmem(hdr);
  220. return ret;
  221. }
  222. /**
  223. * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
  224. * @hdr_addr: Boot image header address
  225. * @addr: Will contain the address of DTB area in boot image
  226. *
  227. * Return: true on success or false on fail.
  228. */
  229. static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
  230. {
  231. const struct andr_img_hdr *hdr;
  232. ulong dtb_img_addr;
  233. bool ret = true;
  234. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  235. if (android_image_check_header(hdr)) {
  236. printf("Error: Boot Image header is incorrect\n");
  237. ret = false;
  238. goto exit;
  239. }
  240. if (hdr->header_version < 2) {
  241. printf("Error: header_version must be >= 2 to get dtb\n");
  242. ret = false;
  243. goto exit;
  244. }
  245. if (hdr->dtb_size == 0) {
  246. printf("Error: dtb_size is 0\n");
  247. ret = false;
  248. goto exit;
  249. }
  250. /* Calculate the address of DTB area in boot image */
  251. dtb_img_addr = hdr_addr;
  252. dtb_img_addr += hdr->page_size;
  253. dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
  254. dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
  255. dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
  256. dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  257. *addr = dtb_img_addr;
  258. exit:
  259. unmap_sysmem(hdr);
  260. return ret;
  261. }
  262. /**
  263. * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
  264. * @hdr_addr: Boot image header address
  265. * @index: Index of desired DTB in DTB area (starting from 0)
  266. * @addr: If not NULL, will contain address to specified DTB
  267. * @size: If not NULL, will contain size of specified DTB
  268. *
  269. * Get the address and size of DTB blob by its index in DTB area of Android
  270. * Boot Image in RAM.
  271. *
  272. * Return: true on success or false on error.
  273. */
  274. bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
  275. u32 *size)
  276. {
  277. const struct andr_img_hdr *hdr;
  278. bool res;
  279. ulong dtb_img_addr; /* address of DTB part in boot image */
  280. u32 dtb_img_size; /* size of DTB payload in boot image */
  281. ulong dtb_addr; /* address of DTB blob with specified index */
  282. u32 i; /* index iterator */
  283. res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
  284. if (!res)
  285. return false;
  286. /* Check if DTB area of boot image is in DTBO format */
  287. if (android_dt_check_header(dtb_img_addr)) {
  288. return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
  289. size);
  290. }
  291. /* Find out the address of DTB with specified index in concat blobs */
  292. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  293. dtb_img_size = hdr->dtb_size;
  294. unmap_sysmem(hdr);
  295. i = 0;
  296. dtb_addr = dtb_img_addr;
  297. while (dtb_addr < dtb_img_addr + dtb_img_size) {
  298. const struct fdt_header *fdt;
  299. u32 dtb_size;
  300. fdt = map_sysmem(dtb_addr, sizeof(*fdt));
  301. if (fdt_check_header(fdt) != 0) {
  302. unmap_sysmem(fdt);
  303. printf("Error: Invalid FDT header for index %u\n", i);
  304. return false;
  305. }
  306. dtb_size = fdt_totalsize(fdt);
  307. unmap_sysmem(fdt);
  308. if (i == index) {
  309. if (size)
  310. *size = dtb_size;
  311. if (addr)
  312. *addr = dtb_addr;
  313. return true;
  314. }
  315. dtb_addr += dtb_size;
  316. ++i;
  317. }
  318. printf("Error: Index is out of bounds (%u/%u)\n", index, i);
  319. return false;
  320. }
  321. #if !defined(CONFIG_SPL_BUILD)
  322. /**
  323. * android_print_contents - prints out the contents of the Android format image
  324. * @hdr: pointer to the Android format image header
  325. *
  326. * android_print_contents() formats a multi line Android image contents
  327. * description.
  328. * The routine prints out Android image properties
  329. *
  330. * returns:
  331. * no returned results
  332. */
  333. void android_print_contents(const struct andr_img_hdr *hdr)
  334. {
  335. const char * const p = IMAGE_INDENT_STRING;
  336. /* os_version = ver << 11 | lvl */
  337. u32 os_ver = hdr->os_version >> 11;
  338. u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
  339. printf("%skernel size: %x\n", p, hdr->kernel_size);
  340. printf("%skernel address: %x\n", p, hdr->kernel_addr);
  341. printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
  342. printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
  343. printf("%ssecond size: %x\n", p, hdr->second_size);
  344. printf("%ssecond address: %x\n", p, hdr->second_addr);
  345. printf("%stags address: %x\n", p, hdr->tags_addr);
  346. printf("%spage size: %x\n", p, hdr->page_size);
  347. /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
  348. * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
  349. printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
  350. p, hdr->os_version,
  351. (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
  352. (os_lvl >> 4) + 2000, os_lvl & 0x0F);
  353. printf("%sname: %s\n", p, hdr->name);
  354. printf("%scmdline: %s\n", p, hdr->cmdline);
  355. printf("%sheader_version: %d\n", p, hdr->header_version);
  356. if (hdr->header_version >= 1) {
  357. printf("%srecovery dtbo size: %x\n", p,
  358. hdr->recovery_dtbo_size);
  359. printf("%srecovery dtbo offset: %llx\n", p,
  360. hdr->recovery_dtbo_offset);
  361. printf("%sheader size: %x\n", p,
  362. hdr->header_size);
  363. }
  364. if (hdr->header_version >= 2) {
  365. printf("%sdtb size: %x\n", p, hdr->dtb_size);
  366. printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
  367. }
  368. }
  369. /**
  370. * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
  371. * @fdt: DTB header
  372. * @index: Number of DTB blob in DTB area.
  373. *
  374. * Return: true on success or false on error.
  375. */
  376. static bool android_image_print_dtb_info(const struct fdt_header *fdt,
  377. u32 index)
  378. {
  379. int root_node_off;
  380. u32 fdt_size;
  381. const char *model;
  382. const char *compatible;
  383. root_node_off = fdt_path_offset(fdt, "/");
  384. if (root_node_off < 0) {
  385. printf("Error: Root node not found\n");
  386. return false;
  387. }
  388. fdt_size = fdt_totalsize(fdt);
  389. compatible = fdt_getprop(fdt, root_node_off, "compatible",
  390. NULL);
  391. model = fdt_getprop(fdt, root_node_off, "model", NULL);
  392. printf(" - DTB #%u:\n", index);
  393. printf(" (DTB)size = %d\n", fdt_size);
  394. printf(" (DTB)model = %s\n", model ? model : "(unknown)");
  395. printf(" (DTB)compatible = %s\n",
  396. compatible ? compatible : "(unknown)");
  397. return true;
  398. }
  399. /**
  400. * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
  401. * @hdr_addr: Boot image header address
  402. *
  403. * DTB payload in Android Boot Image v2+ can be in one of following formats:
  404. * 1. Concatenated DTB blobs
  405. * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
  406. *
  407. * This function does next:
  408. * 1. Prints out the format used in DTB area
  409. * 2. Iterates over all DTB blobs in DTB area and prints out the info for
  410. * each blob.
  411. *
  412. * Return: true on success or false on error.
  413. */
  414. bool android_image_print_dtb_contents(ulong hdr_addr)
  415. {
  416. const struct andr_img_hdr *hdr;
  417. bool res;
  418. ulong dtb_img_addr; /* address of DTB part in boot image */
  419. u32 dtb_img_size; /* size of DTB payload in boot image */
  420. ulong dtb_addr; /* address of DTB blob with specified index */
  421. u32 i; /* index iterator */
  422. res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
  423. if (!res)
  424. return false;
  425. /* Check if DTB area of boot image is in DTBO format */
  426. if (android_dt_check_header(dtb_img_addr)) {
  427. printf("## DTB area contents (DTBO format):\n");
  428. android_dt_print_contents(dtb_img_addr);
  429. return true;
  430. }
  431. printf("## DTB area contents (concat format):\n");
  432. /* Iterate over concatenated DTB blobs */
  433. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  434. dtb_img_size = hdr->dtb_size;
  435. unmap_sysmem(hdr);
  436. i = 0;
  437. dtb_addr = dtb_img_addr;
  438. while (dtb_addr < dtb_img_addr + dtb_img_size) {
  439. const struct fdt_header *fdt;
  440. u32 dtb_size;
  441. fdt = map_sysmem(dtb_addr, sizeof(*fdt));
  442. if (fdt_check_header(fdt) != 0) {
  443. unmap_sysmem(fdt);
  444. printf("Error: Invalid FDT header for index %u\n", i);
  445. return false;
  446. }
  447. res = android_image_print_dtb_info(fdt, i);
  448. if (!res) {
  449. unmap_sysmem(fdt);
  450. return false;
  451. }
  452. dtb_size = fdt_totalsize(fdt);
  453. unmap_sysmem(fdt);
  454. dtb_addr += dtb_size;
  455. ++i;
  456. }
  457. return true;
  458. }
  459. #endif