image-android.c 15 KB

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