image-android.c 15 KB

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