image-android.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 checksum(const unsigned char *buffer, ulong size)
  18. {
  19. ulong sum = 0;
  20. for (ulong i = 0; i < size; i++)
  21. sum += buffer[i];
  22. return sum;
  23. }
  24. static bool is_trailer_present(ulong bootconfig_end_addr)
  25. {
  26. return !strncmp((char *)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE),
  27. BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
  28. }
  29. static ulong add_trailer(ulong bootconfig_start_addr, ulong bootconfig_size)
  30. {
  31. ulong end;
  32. ulong sum;
  33. if (!bootconfig_start_addr)
  34. return -1;
  35. if (!bootconfig_size)
  36. return 0;
  37. end = bootconfig_start_addr + bootconfig_size;
  38. if (is_trailer_present(end))
  39. return 0;
  40. memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE);
  41. sum = checksum((unsigned char *)bootconfig_start_addr, bootconfig_size);
  42. memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum,
  43. BOOTCONFIG_CHECKSUM_SIZE);
  44. memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE),
  45. BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
  46. return BOOTCONFIG_TRAILER_SIZE;
  47. }
  48. static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
  49. struct andr_image_data *data)
  50. {
  51. ulong end;
  52. data->kcmdline = hdr->cmdline;
  53. data->header_version = hdr->header_version;
  54. data->ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
  55. /*
  56. * The header takes a full page, the remaining components are aligned
  57. * on page boundary.
  58. */
  59. end = (ulong)hdr;
  60. end += ANDR_GKI_PAGE_SIZE;
  61. data->kernel_ptr = end;
  62. data->kernel_size = hdr->kernel_size;
  63. end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
  64. data->ramdisk_size = hdr->ramdisk_size;
  65. data->boot_ramdisk_size = hdr->ramdisk_size;
  66. end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
  67. if (hdr->header_version > 3)
  68. end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
  69. data->boot_img_total_size = end - (ulong)hdr;
  70. }
  71. static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
  72. *hdr, struct andr_image_data *data)
  73. {
  74. ulong end;
  75. /*
  76. * The header takes a full page, the remaining components are aligned
  77. * on page boundary.
  78. */
  79. data->kcmdline_extra = hdr->cmdline;
  80. data->tags_addr = hdr->tags_addr;
  81. data->image_name = hdr->name;
  82. data->kernel_addr = hdr->kernel_addr;
  83. data->ramdisk_addr = hdr->ramdisk_addr;
  84. data->dtb_load_addr = hdr->dtb_addr;
  85. data->bootconfig_size = hdr->bootconfig_size;
  86. end = (ulong)hdr;
  87. end += hdr->page_size;
  88. if (hdr->vendor_ramdisk_size) {
  89. data->vendor_ramdisk_ptr = end;
  90. data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
  91. data->ramdisk_size += hdr->vendor_ramdisk_size;
  92. end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
  93. }
  94. data->dtb_ptr = end;
  95. data->dtb_size = hdr->dtb_size;
  96. end += ALIGN(hdr->dtb_size, hdr->page_size);
  97. end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
  98. data->bootconfig_addr = end;
  99. if (hdr->bootconfig_size) {
  100. data->bootconfig_size += add_trailer(data->bootconfig_addr,
  101. data->bootconfig_size);
  102. data->ramdisk_size += data->bootconfig_size;
  103. }
  104. end += ALIGN(data->bootconfig_size, hdr->page_size);
  105. data->vendor_boot_img_total_size = end - (ulong)hdr;
  106. }
  107. static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
  108. struct andr_image_data *data)
  109. {
  110. ulong end;
  111. data->image_name = hdr->name;
  112. data->kcmdline = hdr->cmdline;
  113. data->kernel_addr = hdr->kernel_addr;
  114. data->ramdisk_addr = hdr->ramdisk_addr;
  115. data->header_version = hdr->header_version;
  116. data->dtb_load_addr = hdr->dtb_addr;
  117. end = (ulong)hdr;
  118. /*
  119. * The header takes a full page, the remaining components are aligned
  120. * on page boundary
  121. */
  122. end += hdr->page_size;
  123. data->kernel_ptr = end;
  124. data->kernel_size = hdr->kernel_size;
  125. end += ALIGN(hdr->kernel_size, hdr->page_size);
  126. data->ramdisk_ptr = end;
  127. data->ramdisk_size = hdr->ramdisk_size;
  128. end += ALIGN(hdr->ramdisk_size, hdr->page_size);
  129. data->second_ptr = end;
  130. data->second_size = hdr->second_size;
  131. end += ALIGN(hdr->second_size, hdr->page_size);
  132. if (hdr->header_version >= 1) {
  133. data->recovery_dtbo_ptr = end;
  134. data->recovery_dtbo_size = hdr->recovery_dtbo_size;
  135. end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  136. }
  137. if (hdr->header_version >= 2) {
  138. data->dtb_ptr = end;
  139. data->dtb_size = hdr->dtb_size;
  140. end += ALIGN(hdr->dtb_size, hdr->page_size);
  141. }
  142. data->boot_img_total_size = end - (ulong)hdr;
  143. }
  144. bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
  145. struct andr_image_data *data)
  146. {
  147. if (!boot_hdr || !data) {
  148. printf("boot_hdr or data params can't be NULL\n");
  149. return false;
  150. }
  151. if (!is_android_boot_image_header(boot_hdr)) {
  152. printf("Incorrect boot image header\n");
  153. return false;
  154. }
  155. if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
  156. if (!vendor_boot_hdr) {
  157. printf("For boot header v3+ vendor boot image has to be provided\n");
  158. return false;
  159. }
  160. if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
  161. printf("Incorrect vendor boot image header\n");
  162. return false;
  163. }
  164. android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
  165. android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
  166. } else {
  167. android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
  168. }
  169. return true;
  170. }
  171. static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
  172. {
  173. /*
  174. * All the Android tools that generate a boot.img use this
  175. * address as the default.
  176. *
  177. * Even though it doesn't really make a lot of sense, and it
  178. * might be valid on some platforms, we treat that adress as
  179. * the default value for this field, and try to execute the
  180. * kernel in place in such a case.
  181. *
  182. * Otherwise, we will return the actual value set by the user.
  183. */
  184. if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
  185. return img_data->kernel_ptr;
  186. /*
  187. * abootimg creates images where all load addresses are 0
  188. * and we need to fix them.
  189. */
  190. if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
  191. return env_get_ulong("kernel_addr_r", 16, 0);
  192. return img_data->kernel_addr;
  193. }
  194. /**
  195. * android_image_get_kernel() - processes kernel part of Android boot images
  196. * @hdr: Pointer to boot image header, which is at the start
  197. * of the image.
  198. * @vendor_boot_img: Pointer to vendor boot image header, which is at the
  199. * start of the image.
  200. * @verify: Checksum verification flag. Currently unimplemented.
  201. * @os_data: Pointer to a ulong variable, will hold os data start
  202. * address.
  203. * @os_len: Pointer to a ulong variable, will hold os data length.
  204. *
  205. * This function returns the os image's start address and length. Also,
  206. * it appends the kernel command line to the bootargs env variable.
  207. *
  208. * Return: Zero, os start address and length on success,
  209. * otherwise on failure.
  210. */
  211. int android_image_get_kernel(const void *hdr,
  212. const void *vendor_boot_img, int verify,
  213. ulong *os_data, ulong *os_len)
  214. {
  215. struct andr_image_data img_data = {0};
  216. u32 kernel_addr;
  217. const struct legacy_img_hdr *ihdr;
  218. if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
  219. return -EINVAL;
  220. kernel_addr = android_image_get_kernel_addr(&img_data);
  221. ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
  222. /*
  223. * Not all Android tools use the id field for signing the image with
  224. * sha1 (or anything) so we don't check it. It is not obvious that the
  225. * string is null terminated so we take care of this.
  226. */
  227. strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
  228. andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
  229. if (strlen(andr_tmp_str))
  230. printf("Android's image name: %s\n", andr_tmp_str);
  231. printf("Kernel load addr 0x%08x size %u KiB\n",
  232. kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
  233. int len = 0;
  234. if (*img_data.kcmdline) {
  235. printf("Kernel command line: %s\n", img_data.kcmdline);
  236. len += strlen(img_data.kcmdline);
  237. }
  238. if (img_data.kcmdline_extra) {
  239. printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
  240. len += strlen(img_data.kcmdline_extra);
  241. }
  242. char *bootargs = env_get("bootargs");
  243. if (bootargs)
  244. len += strlen(bootargs);
  245. char *newbootargs = malloc(len + 2);
  246. if (!newbootargs) {
  247. puts("Error: malloc in android_image_get_kernel failed!\n");
  248. return -ENOMEM;
  249. }
  250. *newbootargs = '\0';
  251. if (bootargs) {
  252. strcpy(newbootargs, bootargs);
  253. strcat(newbootargs, " ");
  254. }
  255. if (*img_data.kcmdline)
  256. strcat(newbootargs, img_data.kcmdline);
  257. if (img_data.kcmdline_extra) {
  258. strcat(newbootargs, " ");
  259. strcat(newbootargs, img_data.kcmdline_extra);
  260. }
  261. env_set("bootargs", newbootargs);
  262. if (os_data) {
  263. if (image_get_magic(ihdr) == IH_MAGIC) {
  264. *os_data = image_get_data(ihdr);
  265. } else {
  266. *os_data = img_data.kernel_ptr;
  267. }
  268. }
  269. if (os_len) {
  270. if (image_get_magic(ihdr) == IH_MAGIC)
  271. *os_len = image_get_data_size(ihdr);
  272. else
  273. *os_len = img_data.kernel_size;
  274. }
  275. return 0;
  276. }
  277. bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
  278. {
  279. return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
  280. }
  281. bool is_android_boot_image_header(const void *hdr)
  282. {
  283. return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
  284. }
  285. ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
  286. const void *vendor_boot_img)
  287. {
  288. struct andr_image_data img_data;
  289. if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
  290. return -EINVAL;
  291. if (img_data.header_version > 2)
  292. return 0;
  293. return img_data.boot_img_total_size;
  294. }
  295. ulong android_image_get_kload(const void *hdr,
  296. const void *vendor_boot_img)
  297. {
  298. struct andr_image_data img_data;
  299. if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
  300. return -EINVAL;
  301. return android_image_get_kernel_addr(&img_data);
  302. }
  303. ulong android_image_get_kcomp(const void *hdr,
  304. const void *vendor_boot_img)
  305. {
  306. struct andr_image_data img_data;
  307. const void *p;
  308. if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
  309. return -EINVAL;
  310. p = (const void *)img_data.kernel_ptr;
  311. if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
  312. return image_get_comp((struct legacy_img_hdr *)p);
  313. else if (get_unaligned_le32(p) == LZ4F_MAGIC)
  314. return IH_COMP_LZ4;
  315. else
  316. return image_decomp_type(p, sizeof(u32));
  317. }
  318. int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
  319. ulong *rd_data, ulong *rd_len)
  320. {
  321. struct andr_image_data img_data = {0};
  322. ulong ramdisk_ptr;
  323. if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
  324. return -EINVAL;
  325. if (!img_data.ramdisk_size) {
  326. *rd_data = *rd_len = 0;
  327. return -1;
  328. }
  329. if (img_data.header_version > 2) {
  330. ramdisk_ptr = img_data.ramdisk_ptr;
  331. memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
  332. img_data.vendor_ramdisk_size);
  333. memcpy((void *)(ramdisk_ptr + img_data.vendor_ramdisk_size),
  334. (void *)img_data.ramdisk_ptr,
  335. img_data.boot_ramdisk_size);
  336. if (img_data.bootconfig_size) {
  337. memcpy((void *)
  338. (ramdisk_ptr + img_data.vendor_ramdisk_size +
  339. img_data.boot_ramdisk_size),
  340. (void *)img_data.bootconfig_addr,
  341. img_data.bootconfig_size);
  342. }
  343. }
  344. printf("RAM disk load addr 0x%08lx size %u KiB\n",
  345. img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
  346. *rd_data = img_data.ramdisk_ptr;
  347. *rd_len = img_data.ramdisk_size;
  348. return 0;
  349. }
  350. int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
  351. {
  352. struct andr_image_data img_data;
  353. if (!android_image_get_data(hdr, NULL, &img_data))
  354. return -EINVAL;
  355. if (img_data.header_version > 2) {
  356. printf("Second stage bootloader is only supported for boot image version <= 2\n");
  357. return -EOPNOTSUPP;
  358. }
  359. if (!img_data.second_size) {
  360. *second_data = *second_len = 0;
  361. return -1;
  362. }
  363. *second_data = img_data.second_ptr;
  364. printf("second address is 0x%lx\n",*second_data);
  365. *second_len = img_data.second_size;
  366. return 0;
  367. }
  368. /**
  369. * android_image_get_dtbo() - Get address and size of recovery DTBO image.
  370. * @hdr_addr: Boot image header address
  371. * @addr: If not NULL, will contain address of recovery DTBO image
  372. * @size: If not NULL, will contain size of recovery DTBO image
  373. *
  374. * Get the address and size of DTBO image in "Recovery DTBO" area of Android
  375. * Boot Image in RAM. The format of this image is Android DTBO (see
  376. * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
  377. * the address is obtained from this function, one can use 'adtimg' U-Boot
  378. * command or android_dt_*() functions to extract desired DTBO blob.
  379. *
  380. * This DTBO (included in boot image) is only needed for non-A/B devices, and it
  381. * only can be found in recovery image. On A/B devices we can always rely on
  382. * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
  383. * AOSP documentation for details.
  384. *
  385. * Return: true on success or false on error.
  386. */
  387. bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
  388. {
  389. const struct andr_boot_img_hdr_v0 *hdr;
  390. ulong dtbo_img_addr;
  391. bool ret = true;
  392. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  393. if (!is_android_boot_image_header(hdr)) {
  394. printf("Error: Boot Image header is incorrect\n");
  395. ret = false;
  396. goto exit;
  397. }
  398. if (hdr->header_version != 1 && hdr->header_version != 2) {
  399. printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
  400. ret = false;
  401. goto exit;
  402. }
  403. if (hdr->recovery_dtbo_size == 0) {
  404. printf("Error: recovery_dtbo_size is 0\n");
  405. ret = false;
  406. goto exit;
  407. }
  408. /* Calculate the address of DTB area in boot image */
  409. dtbo_img_addr = hdr_addr;
  410. dtbo_img_addr += hdr->page_size;
  411. dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
  412. dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
  413. dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
  414. if (addr)
  415. *addr = dtbo_img_addr;
  416. if (size)
  417. *size = hdr->recovery_dtbo_size;
  418. exit:
  419. unmap_sysmem(hdr);
  420. return ret;
  421. }
  422. /**
  423. * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
  424. * @hdr_addr: Boot image header address
  425. * @vhdr_addr: Vendor Boot image header address
  426. * @addr: Will contain the address of DTB area in boot image
  427. *
  428. * Return: true on success or false on fail.
  429. */
  430. static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
  431. {
  432. const struct andr_boot_img_hdr_v0 *hdr;
  433. const struct andr_vnd_boot_img_hdr *v_hdr;
  434. ulong dtb_img_addr;
  435. bool ret = true;
  436. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  437. if (!is_android_boot_image_header(hdr)) {
  438. printf("Error: Boot Image header is incorrect\n");
  439. ret = false;
  440. goto exit;
  441. }
  442. if (hdr->header_version < 2) {
  443. printf("Error: header_version must be >= 2 to get dtb\n");
  444. ret = false;
  445. goto exit;
  446. }
  447. if (hdr->header_version == 2) {
  448. if (!hdr->dtb_size) {
  449. printf("Error: dtb_size is 0\n");
  450. ret = false;
  451. goto exit;
  452. }
  453. /* Calculate the address of DTB area in boot image */
  454. dtb_img_addr = hdr_addr;
  455. dtb_img_addr += hdr->page_size;
  456. dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
  457. dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
  458. dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
  459. dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  460. *addr = dtb_img_addr;
  461. }
  462. if (hdr->header_version > 2) {
  463. v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
  464. if (!v_hdr->dtb_size) {
  465. printf("Error: dtb_size is 0\n");
  466. ret = false;
  467. unmap_sysmem(v_hdr);
  468. goto exit;
  469. }
  470. /* Calculate the address of DTB area in boot image */
  471. dtb_img_addr = vhdr_addr;
  472. dtb_img_addr += v_hdr->page_size;
  473. if (v_hdr->vendor_ramdisk_size)
  474. dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
  475. *addr = dtb_img_addr;
  476. unmap_sysmem(v_hdr);
  477. goto exit;
  478. }
  479. exit:
  480. unmap_sysmem(hdr);
  481. return ret;
  482. }
  483. /**
  484. * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
  485. * @hdr_addr: Boot image header address
  486. * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
  487. * @index: Index of desired DTB in DTB area (starting from 0)
  488. * @addr: If not NULL, will contain address to specified DTB
  489. * @size: If not NULL, will contain size of specified DTB
  490. *
  491. * Get the address and size of DTB blob by its index in DTB area of Android
  492. * Boot Image in RAM.
  493. *
  494. * Return: true on success or false on error.
  495. */
  496. bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
  497. u32 index, ulong *addr, u32 *size)
  498. {
  499. struct andr_image_data img_data;
  500. const struct andr_boot_img_hdr_v0 *hdr;
  501. const struct andr_vnd_boot_img_hdr *vhdr;
  502. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  503. if (vendor_boot_img != -1)
  504. vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
  505. if (!android_image_get_data(hdr, vhdr, &img_data)) {
  506. if (vendor_boot_img != -1)
  507. unmap_sysmem(vhdr);
  508. unmap_sysmem(hdr);
  509. return false;
  510. }
  511. if (vendor_boot_img != -1)
  512. unmap_sysmem(vhdr);
  513. unmap_sysmem(hdr);
  514. ulong dtb_img_addr; /* address of DTB part in boot image */
  515. u32 dtb_img_size; /* size of DTB payload in boot image */
  516. ulong dtb_addr; /* address of DTB blob with specified index */
  517. u32 i; /* index iterator */
  518. android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
  519. /* Check if DTB area of boot image is in DTBO format */
  520. if (android_dt_check_header(dtb_img_addr)) {
  521. return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
  522. size);
  523. }
  524. /* Find out the address of DTB with specified index in concat blobs */
  525. dtb_img_size = img_data.dtb_size;
  526. i = 0;
  527. dtb_addr = dtb_img_addr;
  528. while (dtb_addr < dtb_img_addr + dtb_img_size) {
  529. const struct fdt_header *fdt;
  530. u32 dtb_size;
  531. fdt = map_sysmem(dtb_addr, sizeof(*fdt));
  532. if (fdt_check_header(fdt) != 0) {
  533. unmap_sysmem(fdt);
  534. printf("Error: Invalid FDT header for index %u\n", i);
  535. return false;
  536. }
  537. dtb_size = fdt_totalsize(fdt);
  538. unmap_sysmem(fdt);
  539. if (i == index) {
  540. if (size)
  541. *size = dtb_size;
  542. if (addr)
  543. *addr = dtb_addr;
  544. return true;
  545. }
  546. dtb_addr += dtb_size;
  547. ++i;
  548. }
  549. printf("Error: Index is out of bounds (%u/%u)\n", index, i);
  550. return false;
  551. }
  552. #if !defined(CONFIG_SPL_BUILD)
  553. /**
  554. * android_print_contents - prints out the contents of the Android format image
  555. * @hdr: pointer to the Android format image header
  556. *
  557. * android_print_contents() formats a multi line Android image contents
  558. * description.
  559. * The routine prints out Android image properties
  560. *
  561. * returns:
  562. * no returned results
  563. */
  564. void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
  565. {
  566. if (hdr->header_version >= 3) {
  567. printf("Content print is not supported for boot image header version > 2");
  568. return;
  569. }
  570. const char * const p = IMAGE_INDENT_STRING;
  571. /* os_version = ver << 11 | lvl */
  572. u32 os_ver = hdr->os_version >> 11;
  573. u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
  574. printf("%skernel size: %x\n", p, hdr->kernel_size);
  575. printf("%skernel address: %x\n", p, hdr->kernel_addr);
  576. printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
  577. printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
  578. printf("%ssecond size: %x\n", p, hdr->second_size);
  579. printf("%ssecond address: %x\n", p, hdr->second_addr);
  580. printf("%stags address: %x\n", p, hdr->tags_addr);
  581. printf("%spage size: %x\n", p, hdr->page_size);
  582. /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
  583. * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
  584. printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
  585. p, hdr->os_version,
  586. (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
  587. (os_lvl >> 4) + 2000, os_lvl & 0x0F);
  588. printf("%sname: %s\n", p, hdr->name);
  589. printf("%scmdline: %s\n", p, hdr->cmdline);
  590. printf("%sheader_version: %d\n", p, hdr->header_version);
  591. if (hdr->header_version >= 1) {
  592. printf("%srecovery dtbo size: %x\n", p,
  593. hdr->recovery_dtbo_size);
  594. printf("%srecovery dtbo offset: %llx\n", p,
  595. hdr->recovery_dtbo_offset);
  596. printf("%sheader size: %x\n", p,
  597. hdr->header_size);
  598. }
  599. if (hdr->header_version == 2) {
  600. printf("%sdtb size: %x\n", p, hdr->dtb_size);
  601. printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
  602. }
  603. }
  604. /**
  605. * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
  606. * @fdt: DTB header
  607. * @index: Number of DTB blob in DTB area.
  608. *
  609. * Return: true on success or false on error.
  610. */
  611. static bool android_image_print_dtb_info(const struct fdt_header *fdt,
  612. u32 index)
  613. {
  614. int root_node_off;
  615. u32 fdt_size;
  616. const char *model;
  617. const char *compatible;
  618. root_node_off = fdt_path_offset(fdt, "/");
  619. if (root_node_off < 0) {
  620. printf("Error: Root node not found\n");
  621. return false;
  622. }
  623. fdt_size = fdt_totalsize(fdt);
  624. compatible = fdt_getprop(fdt, root_node_off, "compatible",
  625. NULL);
  626. model = fdt_getprop(fdt, root_node_off, "model", NULL);
  627. printf(" - DTB #%u:\n", index);
  628. printf(" (DTB)size = %d\n", fdt_size);
  629. printf(" (DTB)model = %s\n", model ? model : "(unknown)");
  630. printf(" (DTB)compatible = %s\n",
  631. compatible ? compatible : "(unknown)");
  632. return true;
  633. }
  634. /**
  635. * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
  636. * @hdr_addr: Boot image header address
  637. *
  638. * DTB payload in Android Boot Image v2+ can be in one of following formats:
  639. * 1. Concatenated DTB blobs
  640. * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
  641. *
  642. * This function does next:
  643. * 1. Prints out the format used in DTB area
  644. * 2. Iterates over all DTB blobs in DTB area and prints out the info for
  645. * each blob.
  646. *
  647. * Return: true on success or false on error.
  648. */
  649. bool android_image_print_dtb_contents(ulong hdr_addr)
  650. {
  651. const struct andr_boot_img_hdr_v0 *hdr;
  652. bool res;
  653. ulong dtb_img_addr; /* address of DTB part in boot image */
  654. u32 dtb_img_size; /* size of DTB payload in boot image */
  655. ulong dtb_addr; /* address of DTB blob with specified index */
  656. u32 i; /* index iterator */
  657. res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
  658. if (!res)
  659. return false;
  660. /* Check if DTB area of boot image is in DTBO format */
  661. if (android_dt_check_header(dtb_img_addr)) {
  662. printf("## DTB area contents (DTBO format):\n");
  663. android_dt_print_contents(dtb_img_addr);
  664. return true;
  665. }
  666. printf("## DTB area contents (concat format):\n");
  667. /* Iterate over concatenated DTB blobs */
  668. hdr = map_sysmem(hdr_addr, sizeof(*hdr));
  669. dtb_img_size = hdr->dtb_size;
  670. unmap_sysmem(hdr);
  671. i = 0;
  672. dtb_addr = dtb_img_addr;
  673. while (dtb_addr < dtb_img_addr + dtb_img_size) {
  674. const struct fdt_header *fdt;
  675. u32 dtb_size;
  676. fdt = map_sysmem(dtb_addr, sizeof(*fdt));
  677. if (fdt_check_header(fdt) != 0) {
  678. unmap_sysmem(fdt);
  679. printf("Error: Invalid FDT header for index %u\n", i);
  680. return false;
  681. }
  682. res = android_image_print_dtb_info(fdt, i);
  683. if (!res) {
  684. unmap_sysmem(fdt);
  685. return false;
  686. }
  687. dtb_size = fdt_totalsize(fdt);
  688. unmap_sysmem(fdt);
  689. dtb_addr += dtb_size;
  690. ++i;
  691. }
  692. return true;
  693. }
  694. #endif