hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
/*
 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <image.h>
#include <android_ab.h>
#include <android_bootloader.h>
#include <android_image.h>
#include <malloc.h>
#include <mapmem.h>
#include <errno.h>
#include <boot_rkimg.h>
#include <crypto.h>
#include <sysmem.h>
#include <mp_boot.h>
#include <u-boot/sha1.h>
#ifdef CONFIG_RKIMG_BOOTLOADER
#include <asm/arch/resource_img.h>
#endif
#ifdef CONFIG_RK_AVB_LIBAVB_USER
#include <android_avb/avb_slot_verify.h>
#include <android_avb/avb_ops_user.h>
#include <android_avb/rk_avb_ops_user.h>
#endif
#include <optee_include/OpteeClientInterface.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR    0x10008000
#define ANDROID_PARTITION_VENDOR_BOOT        "vendor_boot"
#define ANDROID_PARTITION_INIT_BOOT        "init_boot"
 
#define BLK_CNT(_num_bytes, _block_size)    \
       ((_num_bytes + _block_size - 1) / _block_size)
 
static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
static u32 android_kernel_comp_type = IH_COMP_NONE;
 
static int android_version_init(void)
{
   struct andr_img_hdr *hdr = NULL;
   struct blk_desc *desc;
   const char *part_name = PART_BOOT;
   disk_partition_t part;
   int os_version;
 
   desc = rockchip_get_bootdev();
   if (!desc) {
       printf("No bootdev\n");
       return -1;
   }
 
#ifdef CONFIG_ANDROID_AB
   part_name = ab_can_find_recovery_part() ? PART_RECOVERY : PART_BOOT;
#endif
   if (part_get_info_by_name(desc, part_name, &part) < 0)
       return -1;
 
   hdr = populate_andr_img_hdr(desc, &part);
   if (!hdr)
       return -1;
 
   os_version = hdr->os_version;
   if (os_version)
       printf("Android %u.%u, Build %u.%u, v%d\n",
              (os_version >> 25) & 0x7f, (os_version >> 18) & 0x7F,
              ((os_version >> 4) & 0x7f) + 2000, os_version & 0x0F,
              hdr->header_version);
   free(hdr);
 
   return (os_version >> 25) & 0x7f;
}
 
u32 android_bcb_msg_sector_offset(void)
{
   static int android_version = -1;    /* static */
 
   /*
    * get android os version:
    *
    * There are two types of misc.img:
    *    Rockchip platforms defines BCB message at the 16KB offset of
    *    misc.img except for the Android version >= 10. Because Google
    *    defines it at 0x00 offset, and from Android-10 it becoms mandary
    *    on Google VTS.
    *
    * So we must get android 'os_version' to identify which type we
    * are using, then we could able to use rockchip_get_boot_mode()
    * which reads BCB from misc.img.
    */
#ifdef CONFIG_RKIMG_BOOTLOADER
   if (android_version < 0)
       android_version = android_version_init();
 
   return (android_version >= 10) ? 0x00 : 0x20;
#else
   return 0x00;
#endif
}
 
#ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
int android_image_init_resource(struct blk_desc *desc,
               disk_partition_t *out_part,
               ulong *out_blk_offset)
{
   struct andr_img_hdr *hdr = NULL;
   const char *part_name = ANDROID_PARTITION_BOOT;
   disk_partition_t part;
   ulong offset;
   int ret = 0;
 
   if (!desc)
       return -ENODEV;
 
#ifndef CONFIG_ANDROID_AB
   if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY)
       part_name = ANDROID_PARTITION_RECOVERY;
#endif
   if (part_get_info_by_name(desc, part_name, &part) < 0)
       return -ENOENT;
 
   hdr = populate_andr_img_hdr(desc, &part);
   if (!hdr)
       return -EINVAL;
 
   if (hdr->header_version >= 2 && hdr->dtb_size)
       env_update("bootargs", "androidboot.dtb_idx=0");
 
   if (hdr->header_version <= 2) {
       offset = hdr->page_size +
           ALIGN(hdr->kernel_size, hdr->page_size) +
           ALIGN(hdr->ramdisk_size, hdr->page_size);
       *out_part = part;
       *out_blk_offset = DIV_ROUND_UP(offset, desc->blksz);
   } else {
       ret = -EINVAL;
   }
   free(hdr);
 
   return ret;
}
#endif
 
static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
{
   /*
    * All the Android tools that generate a boot.img use this
    * address as the default.
    *
    * Even though it doesn't really make a lot of sense, and it
    * might be valid on some platforms, we treat that address as
    * the default value for this field, and try to execute the
    * kernel in place in such a case.
    *
    * Otherwise, we will return the actual value set by the user.
    */
   if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
       return (ulong)hdr + hdr->page_size;
 
#ifdef CONFIG_ARCH_ROCKCHIP
   /*
    * If kernel is compressed, kernel_addr is set as decompressed address
    * after compressed being loaded to ram, so let's use it.
    */
   if (android_kernel_comp_type != IH_COMP_NONE &&
       android_kernel_comp_type != IH_COMP_ZIMAGE)
       return hdr->kernel_addr;
 
   /*
    * Compatble with rockchip legacy packing with kernel/ramdisk/second
    * address base from 0x60000000(SDK versiont < 8.1), these are invalid
    * address, so we calc it by real size.
    */
   return (ulong)hdr + hdr->page_size;
#else
   return hdr->kernel_addr;
#endif
 
}
 
void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp)
{
   android_kernel_comp_type = comp;
}
 
u32 android_image_get_comp(const struct andr_img_hdr *hdr)
{
   return android_kernel_comp_type;
}
 
int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr)
{
   ulong kaddr = android_image_get_kernel_addr(hdr);
   return bootm_parse_comp((const unsigned char *)kaddr);
}
 
/**
 * android_image_get_kernel() - processes kernel part of Android boot images
 * @hdr:    Pointer to image header, which is at the start
 *            of the image.
 * @verify:    Checksum verification flag. Currently unimplemented.
 * @os_data:    Pointer to a ulong variable, will hold os data start
 *            address.
 * @os_len:    Pointer to a ulong variable, will hold os data length.
 *
 * This function returns the os image's start address and length. Also,
 * it appends the kernel command line to the bootargs env variable.
 *
 * Return: Zero, os start address and length on success,
 *        otherwise on failure.
 */
int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
                ulong *os_data, ulong *os_len)
{
   u32 kernel_addr = android_image_get_kernel_addr(hdr);
   const char *cmdline = hdr->header_version < 3 ?
                 hdr->cmdline : hdr->total_cmdline;
   /*
    * Not all Android tools use the id field for signing the image with
    * sha1 (or anything) so we don't check it. It is not obvious that the
    * string is null terminated so we take care of this.
    */
   strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
   andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
   if (strlen(andr_tmp_str))
       printf("Android's image name: %s\n", andr_tmp_str);
 
   printf("Kernel: 0x%08x - 0x%08x (%u KiB)\n",
          kernel_addr, kernel_addr + hdr->kernel_size,
          DIV_ROUND_UP(hdr->kernel_size, 1024));
 
   int len = 0;
   if (cmdline) {
       debug("Kernel command line: %s\n", cmdline);
       len += strlen(cmdline);
   }
 
   char *bootargs = env_get("bootargs");
   if (bootargs)
       len += strlen(bootargs);
 
   char *newbootargs = malloc(len + 2);
   if (!newbootargs) {
       puts("Error: malloc in android_image_get_kernel failed!\n");
       return -ENOMEM;
   }
   *newbootargs = '\0';
 
   if (bootargs) {
       strcpy(newbootargs, bootargs);
       strcat(newbootargs, " ");
   }
   if (cmdline)
       strcat(newbootargs, cmdline);
 
   env_set("bootargs", newbootargs);
 
   if (os_data) {
       *os_data = (ulong)hdr;
       *os_data += hdr->page_size;
   }
   if (os_len)
       *os_len = hdr->kernel_size;
   return 0;
}
 
int android_image_check_header(const struct andr_img_hdr *hdr)
{
   return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
}
 
ulong android_image_get_end(const struct andr_img_hdr *hdr)
{
   ulong end;
   /*
    * The header takes a full page, the remaining components are aligned
    * on page boundary
    */
   end = (ulong)hdr;
   if (hdr->header_version < 3) {
       end += hdr->page_size;
       end += ALIGN(hdr->kernel_size, hdr->page_size);
       end += ALIGN(hdr->ramdisk_size, hdr->page_size);
       end += ALIGN(hdr->second_size, hdr->page_size);
       if (hdr->header_version == 1) {
           end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
       } else if (hdr->header_version == 2) {
           end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
           end += ALIGN(hdr->dtb_size, hdr->page_size);
       }
   } else {
       /* boot_img_hdr_v34 */
       end += hdr->page_size;
       end += ALIGN(hdr->kernel_size, hdr->page_size);
       end += ALIGN(hdr->ramdisk_size, hdr->page_size);
   }
 
   return end;
}
 
u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
{
   return hdr->kernel_size;
}
 
void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
{
   hdr->kernel_addr = load_address;
}
 
ulong android_image_get_kload(const struct andr_img_hdr *hdr)
{
   return android_image_get_kernel_addr(hdr);
}
 
int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
                 ulong *rd_data, ulong *rd_len)
{
   ulong ramdisk_addr_r;
   ulong start, end;
 
   if (!hdr->ramdisk_size) {
       *rd_data = *rd_len = 0;
       return -1;
   }
 
   /* Have been loaded by android_image_load_separate() on ramdisk_addr_r */
   ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
   if (!ramdisk_addr_r) {
       printf("No Found Ramdisk Load Address.\n");
       return -1;
   }
 
   *rd_data = ramdisk_addr_r;
   *rd_len = hdr->ramdisk_size;
   if (hdr->header_version >= 3)
       *rd_len += hdr->vendor_ramdisk_size;
   if (hdr->header_version >= 4) {
        *rd_len += hdr->vendor_bootconfig_size +
         ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
   }
 
   /* just for print msg */
   start = ramdisk_addr_r;
   if (hdr->header_version >= 3) {
       end = start + (ulong)hdr->vendor_ramdisk_size;
       printf("v-ramdisk:  0x%08lx - 0x%08lx (%u KiB)\n",
              start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024));
       start = end;
   }
   {
       end = start + (ulong)hdr->ramdisk_size;
       printf("ramdisk:    0x%08lx - 0x%08lx (%u KiB)\n",
              start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
       start = end;
   }
   if (hdr->header_version >= 4) {
       end = start + (ulong)hdr->vendor_bootconfig_size;
       printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n",
              start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024));
       start = end;
       end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
       printf("bootparams: 0x%08lx - 0x%08lx\n", start, end);
   }
 
   return 0;
}
 
int android_image_get_fdt(const struct andr_img_hdr *hdr,
                 ulong *rd_data)
{
   ulong fdt_addr_r;
 
   if (!hdr->second_size) {
       *rd_data = 0;
       return -1;
   }
 
   /* Have been loaded by android_image_load_separate() on fdt_addr_r */
   fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
   if (!fdt_addr_r) {
       printf("No Found FDT Load Address.\n");
       return -1;
   }
 
   *rd_data = fdt_addr_r;
 
   debug("FDT load addr 0x%08x size %u KiB\n",
         hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
 
   return 0;
}
 
#ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
static void print_hash(const char *label, u8 *hash, int len)
{
   int i;
 
   printf("%s:\n    0x", label ? : "Hash");
   for (i = 0; i < len; i++)
       printf("%02x", hash[i]);
   printf("\n");
}
#endif
 
typedef enum {
   IMG_KERNEL,
   IMG_RAMDISK,    /* within boot.img or init_boot.img(Android-13 or later) */
   IMG_SECOND,
   IMG_RECOVERY_DTBO,
   IMG_RK_DTB,    /* within resource.img in second position */
   IMG_DTB,
   IMG_VENDOR_RAMDISK,
   IMG_BOOTCONFIG,
   IMG_MAX,
} img_t;
 
#if defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) && !defined(CONFIG_DM_CRYPTO)
static sha1_context sha1_ctx;
#endif
 
static int image_load(img_t img, struct andr_img_hdr *hdr,
             ulong blkstart, void *ram_base,
             struct udevice *crypto)
{
   struct blk_desc *desc = rockchip_get_bootdev();
   disk_partition_t part_vendor_boot;
   disk_partition_t part_init_boot;
   __maybe_unused u32 typesz;
   u32 andr_version = (hdr->os_version >> 25) & 0x7f;
   ulong pgsz = hdr->page_size;
   ulong blksz = desc->blksz;
   ulong blkcnt, blkoff;
   ulong memmove_dst = 0;
   ulong bsoffs = 0;
   ulong extra = 0;
   ulong length;
   void *buffer;
   void *tmp = NULL;
   int ret = 0;
 
   switch (img) {
   case IMG_KERNEL:
       bsoffs = 0; /* include a page_size(image header) */
       length = hdr->kernel_size + pgsz;
       buffer = (void *)env_get_ulong("android_addr_r", 16, 0);
       blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz);
       typesz = sizeof(hdr->kernel_size);
       if (!sysmem_alloc_base(MEM_KERNEL,
           (phys_addr_t)buffer, blkcnt * blksz))
           return -ENOMEM;
       break;
   case IMG_VENDOR_RAMDISK:
       if (hdr->vendor_boot_buf) {
           ram_base = hdr->vendor_boot_buf;
       } else {
           if (part_get_info_by_name(desc,
                         ANDROID_PARTITION_VENDOR_BOOT,
                         &part_vendor_boot) < 0) {
               printf("No vendor boot partition\n");
               return -ENOENT;
           }
           ram_base = 0;
       }
 
       blkstart = part_vendor_boot.start;
       pgsz = hdr->vendor_page_size;
       bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz);
       length = hdr->vendor_ramdisk_size;
       buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
       blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz);
       typesz = sizeof(hdr->vendor_ramdisk_size);
       /*
        * Add extra memory for generic ramdisk space.
        *
        * In case of unaligned vendor ramdisk size, reserve
        * 1 more blksz.
        *
        * Reserve 8KB for bootloader cmdline.
        */
       if (hdr->header_version >= 3)
           extra += ALIGN(hdr->ramdisk_size, blksz) + blksz;
       if (hdr->header_version >= 4)
           extra += ALIGN(hdr->vendor_bootconfig_size, blksz) +
                ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
       if (length && !sysmem_alloc_base(MEM_RAMDISK,
           (phys_addr_t)buffer, blkcnt * blksz + extra))
           return -ENOMEM;
       break;
   case IMG_RAMDISK:
       /* get ramdisk from init_boot.img ? */
       if (hdr->header_version >= 4 && andr_version >= 13) {
           if (hdr->init_boot_buf) {
               ram_base = hdr->init_boot_buf;
           } else {
               if (part_get_info_by_name(desc,
                   ANDROID_PARTITION_INIT_BOOT, &part_init_boot) < 0) {
                   printf("No init boot partition\n");
                   return -ENOENT;
               }
               blkstart = part_init_boot.start;
               ram_base = 0;
           }
           bsoffs = pgsz;
       } else {
           /* get ramdisk from generic boot.img */
           bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz);
       }
 
       length = hdr->ramdisk_size;
       buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
       blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz);
       typesz = sizeof(hdr->ramdisk_size);
 
       /*
        * ramdisk_addr_r v012:
        *    |----------------|
        *    |    ramdisk     |
        *    |----------------|
        *
        * ramdisk_addr_r v3 (Android-11 and later):
        *    |----------------|---------|
        *    | vendor-ramdisk | ramdisk |
        *    |----------------|---------|
        *
        * ramdisk_addr_r v4 (Android-12 and later):
        *    |----------------|---------|------------|------------|
        *    | vendor-ramdisk | ramdisk | bootconfig | bootparams |
        *    |----------------|---------|------------|------------|
        *
        * ramdisk_addr_r v4 + init_boot(Android-13 and later):
        *    |----------------|----------------|------------|------------|
        *    | vendor-ramdisk | (init_)ramdisk | bootconfig | bootparams |
        *    |----------------|----------------|------------|------------|
        */
       if (hdr->header_version >= 3) {
           buffer += hdr->vendor_ramdisk_size;
           if (!IS_ALIGNED((ulong)buffer, blksz)) {
               memmove_dst = (ulong)buffer;
               buffer = (void *)ALIGN(memmove_dst, blksz);
           }
       }
       /* sysmem has been alloced by vendor ramdisk */
       if (hdr->header_version < 3) {
           if (length && !sysmem_alloc_base(MEM_RAMDISK,
               (phys_addr_t)buffer, blkcnt * blksz))
               return -ENOMEM;
       }
       break;
   case IMG_BOOTCONFIG:
       if (hdr->header_version < 4)
           return 0;
 
       if (hdr->vendor_boot_buf) {
           ram_base = hdr->vendor_boot_buf;
       } else {
           if (part_get_info_by_name(desc,
                         ANDROID_PARTITION_VENDOR_BOOT,
                         &part_vendor_boot) < 0) {
               printf("No vendor boot partition\n");
               return -ENOENT;
           }
           ram_base = 0;
       }
       blkstart = part_vendor_boot.start;
       pgsz = hdr->vendor_page_size;
       bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) +
            ALIGN(hdr->vendor_ramdisk_size, pgsz) +
            ALIGN(hdr->dtb_size, pgsz) +
            ALIGN(hdr->vendor_ramdisk_table_size, pgsz);
       length = hdr->vendor_bootconfig_size;
       buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
       blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz);
       typesz = sizeof(hdr->vendor_bootconfig_size);
 
       buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size;
       if (!IS_ALIGNED((ulong)buffer, blksz)) {
           memmove_dst = (ulong)buffer;
           buffer = (void *)ALIGN(memmove_dst, blksz);
       }
       break;
   case IMG_SECOND:
       bsoffs = pgsz +
            ALIGN(hdr->kernel_size, pgsz) +
            ALIGN(hdr->ramdisk_size, pgsz);
       length = hdr->second_size;
       blkcnt = DIV_ROUND_UP(hdr->second_size, blksz);
       buffer = tmp = malloc(blkcnt * blksz);
       typesz = sizeof(hdr->second_size);
       break;
   case IMG_RECOVERY_DTBO:
       bsoffs = pgsz +
            ALIGN(hdr->kernel_size, pgsz) +
            ALIGN(hdr->ramdisk_size, pgsz) +
            ALIGN(hdr->second_size, pgsz);
       length = hdr->recovery_dtbo_size;
       blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz);
       buffer = tmp = malloc(blkcnt * blksz);
       typesz = sizeof(hdr->recovery_dtbo_size);
       break;
   case IMG_DTB:
       bsoffs = pgsz +
            ALIGN(hdr->kernel_size, pgsz) +
            ALIGN(hdr->ramdisk_size, pgsz) +
            ALIGN(hdr->second_size, pgsz) +
            ALIGN(hdr->recovery_dtbo_size, pgsz);
       length = hdr->dtb_size;
       blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz);
       buffer = tmp = malloc(blkcnt * blksz);
       typesz = sizeof(hdr->dtb_size);
       break;
   case IMG_RK_DTB:
#ifdef CONFIG_RKIMG_BOOTLOADER
       /* No going further, it handles DTBO, HW-ID, etc */
       buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0);
       if (gd->fdt_blob != (void *)buffer)
           ret = rockchip_read_dtb_file(buffer);
#endif
       return ret < 0 ? ret : 0;
   default:
       return -EINVAL;
   }
 
   if (!buffer) {
       printf("No memory for image(%d)\n", img);
       return -ENOMEM;
   }
 
   if (!blksz || !length)
       goto crypto_calc;
 
   /* load */
   if (ram_base) {
       memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length);
   } else {
       blkoff = DIV_ROUND_UP(bsoffs, blksz);
       ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer);
       if (ret != blkcnt) {
           printf("Failed to read img(%d), ret=%d\n", img, ret);
           return -EIO;
       }
   }
 
   if (memmove_dst)
       memmove((char *)memmove_dst, buffer, length);
 
crypto_calc:
   if (img == IMG_KERNEL) {
       buffer += pgsz;
       length -= pgsz;
   }
 
   /* sha1 */
   if (hdr->header_version < 3) {
#ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
#ifdef CONFIG_DM_CRYPTO
       if (crypto) {
           crypto_sha_update(crypto, (u32 *)buffer, length);
           crypto_sha_update(crypto, (u32 *)&length, typesz);
       }
#else
       sha1_update(&sha1_ctx, (void *)buffer, length);
       sha1_update(&sha1_ctx, (void *)&length, typesz);
#endif
#endif
   }
 
   if (tmp)
       free(tmp);
 
   return 0;
}
 
static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start,
                 void *ram_base, struct udevice *crypto)
{
   /* load, never change order ! */
   if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto))
       return -1;
   if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto))
       return -1;
   if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto))
       return -1;
   if (hdr->header_version > 0) {
       if (image_load(IMG_RECOVERY_DTBO, hdr, part_start,
                  ram_base, crypto))
           return -1;
   }
   if (hdr->header_version > 1) {
       if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto))
           return -1;
   }
 
   return 0;
}
 
/*
 * @ram_base: !NULL means require memcpy for an exist full android image.
 */
static int android_image_separate(struct andr_img_hdr *hdr,
                 const disk_partition_t *part,
                 void *load_address,
                 void *ram_base)
{
   ulong bstart;
   int ret;
 
   if (android_image_check_header(hdr)) {
       printf("Bad android image header\n");
       return -EINVAL;
   }
 
   /* set for image_load(IMG_KERNEL, ...) */
   env_set_hex("android_addr_r", (ulong)load_address);
   bstart = part ? part->start : 0;
 
   /*
    * 1. Load images to their individual target ram position
    *    in order to disable fdt/ramdisk relocation.
    */
 
   /* load rk-kernel.dtb alone */
   if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL))
       return -1;
 
#ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
   int verify = 1;
 
#ifdef CONFIG_MP_BOOT
   verify = mpb_post(3);
#endif
   if (hdr->header_version < 3 && verify) {
       struct udevice *dev = NULL;
       uchar hash[20];
#ifdef CONFIG_DM_CRYPTO
       sha_context ctx;
 
       ctx.length = 0;
       ctx.algo = CRYPTO_SHA1;
       dev = crypto_get_device(ctx.algo);
       if (!dev) {
           printf("Can't find crypto device for SHA1\n");
           return -ENODEV;
       }
 
       /* v1 & v2: requires total length before sha init */
       ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
                 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
                 hdr->second_size + sizeof(hdr->second_size);
       if (hdr->header_version > 0)
           ctx.length += hdr->recovery_dtbo_size +
                       sizeof(hdr->recovery_dtbo_size);
       if (hdr->header_version > 1)
           ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
       crypto_sha_init(dev, &ctx);
#else
       sha1_starts(&sha1_ctx);
#endif
       ret = images_load_verify(hdr, bstart, ram_base, dev);
       if (ret)
           return ret;
 
#ifdef CONFIG_DM_CRYPTO
       crypto_sha_final(dev, &ctx, hash);
#else
       sha1_finish(&sha1_ctx, hash);
#endif
       if (memcmp(hash, hdr->id, 20)) {
           print_hash("Hash from header", (u8 *)hdr->id, 20);
           print_hash("Hash real", (u8 *)hash, 20);
           return -EBADFD;
       } else {
           printf("ANDROID: Hash OK\n");
       }
   } else
#endif
   {
       ret = images_load_verify(hdr, bstart, ram_base, NULL);
       if (ret)
           return ret;
   }
 
   /* 2. Disable fdt/ramdisk relocation, it saves boot time */
   env_set("bootm-no-reloc", "y");
 
   return 0;
}
 
static int android_image_separate_v34(struct andr_img_hdr *hdr,
                     const disk_partition_t *part,
                     void *load_address, void *ram_base)
{
   ulong bstart;
 
   if (android_image_check_header(hdr)) {
       printf("Bad android image header\n");
       return -EINVAL;
   }
 
   /* set for image_load(IMG_KERNEL, ...) */
   env_set_hex("android_addr_r", (ulong)load_address);
   bstart = part ? part->start : 0;
 
   /*
    * 1. Load images to their individual target ram position
    *    in order to disable fdt/ramdisk relocation.
    */
   if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
       return -1;
   if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
       return -1;
   if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL))
       return -1;
   if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
       return -1;
   if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL))
       return -1;
   /*
    * Copy the populated hdr to load address after image_load(IMG_KERNEL)
    *
    * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while
    * vendor_boot_img_hdr_v34 is not included, so fix it here.
    */
   memcpy((char *)load_address, hdr, hdr->page_size);
 
   /* 2. Disable fdt/ramdisk relocation, it saves boot time */
   env_set("bootm-no-reloc", "y");
 
   return 0;
}
 
static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp)
{
   ulong kernel_addr_c;
   ulong load_addr = 0;
 
   kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
 
#ifdef CONFIG_ARM64
   /*
    * On 64-bit kernel, assuming use IMAGE by default.
    *
    * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
    * kernel_addr_r is for IMAGE.
    */
   if (comp != IH_COMP_NONE) {
       ulong comp_addr;
 
       if (kernel_addr_c) {
           comp_addr = kernel_addr_c;
       } else {
           printf("Warn: No \"kernel_addr_c\"\n");
           comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
           env_set_hex("kernel_addr_c", comp_addr);
       }
 
       load_addr = comp_addr - hdr->page_size;
   }
#else
   /*
    * On 32-bit kernel:
    *
    * The input load_addr is from env value: "kernel_addr_r", it has
    * different role depends on whether kernel_addr_c is defined:
    *
    * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
    * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
    */
   if (comp == IH_COMP_NONE) {
       if (kernel_addr_c) {
           /* input load_addr is for Image, nothing to do */
       } else {
           /* input load_addr is for lz4/zImage, set default addr for Image */
           load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
           env_set_hex("kernel_addr_r", load_addr);
 
           load_addr -= hdr->page_size;
       }
   } else {
       if (kernel_addr_c) {
           /* input load_addr is for Image, so use another for lz4/zImage */
           load_addr = kernel_addr_c - hdr->page_size;
       } else {
           /* input load_addr is for lz4/zImage, nothing to do */
       }
   }
#endif
 
   return load_addr;
}
 
void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
{
   ulong kernel_addr_r;
 
   env_set_ulong("os_comp", comp);
 
   /* zImage handles decompress itself */
   if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
       kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
       android_image_set_kload(hdr, kernel_addr_r);
       android_image_set_comp(hdr, comp);
   } else {
       android_image_set_comp(hdr, IH_COMP_NONE);
   }
}
 
static int android_image_load_separate(struct andr_img_hdr *hdr,
                      const disk_partition_t *part,
                      void *load_addr)
{
   if (hdr->header_version < 3)
       return android_image_separate(hdr, part, load_addr, NULL);
   else
       return android_image_separate_v34(hdr, part, load_addr, NULL);
}
 
int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
{
   ulong comp_addr;
   int comp;
 
   comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
   comp_addr = android_image_get_comp_addr(hdr, comp);
 
   /* non-compressed image: already in-place */
   if ((ulong)hdr == *load_addr)
       return 0;
 
   /* compressed image */
   if (comp_addr) {
       *load_addr = comp_addr;
       if ((ulong)hdr == comp_addr)    /* already in-place */
           return 0;
   }
 
   /*
    * The most possible reason to arrive here is:
    *
    * VBoot=1 and AVB load full partition to a temp memory buffer, now we
    * separate(memcpy) subimages from boot.img to where they should be.
    */
   if (hdr->header_version < 3) {
       if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr))
           return -1;
   } else {
       if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr))
           return -1;
   }
 
   android_image_set_decomp((void *)(*load_addr), comp);
 
   return 0;
}
 
long android_image_load(struct blk_desc *dev_desc,
           const disk_partition_t *part_info,
           unsigned long load_address,
           unsigned long max_size) {
   struct andr_img_hdr *hdr;
   ulong comp_addr;
   int comp, ret;
   int blk_off;
 
   if (max_size < part_info->blksz)
       return -1;
 
   hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info);
   if (!hdr) {
       printf("No valid android hdr\n");
       return -1;
   }
 
   /*
    * create the layout:
    *
    * |<- page_size ->|1-blk |
    * |-----|---------|------|-----|
    * | hdr |   ...   |   kernel   |
    * |-----|----- ---|------------|
    *
    * Alloc page_size and 1 more blk for reading kernel image to
    * get it's compression type, then fill the android hdr what
    * we have populated before.
    *
    * Why? see: android_image_get_kernel_addr().
    */
   blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz);
   hdr = (struct andr_img_hdr *)
           realloc(hdr, (blk_off + 1) * dev_desc->blksz);
   if (!hdr)
       return -1;
 
   if (blk_dread(dev_desc, part_info->start + blk_off, 1,
             (char *)hdr + hdr->page_size) != 1) {
       free(hdr);
       return -1;
   }
 
   /* Changed to compressed address ? */
   comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
   comp_addr = android_image_get_comp_addr(hdr, comp);
   if (comp_addr)
       load_address = comp_addr;
   else
       load_address -= hdr->page_size;
 
   ret = android_image_load_separate(hdr, part_info, (void *)load_address);
   if (ret) {
       printf("Failed to load android image\n");
       goto fail;
   }
   android_image_set_decomp((void *)load_address, comp);
 
   debug("Loading Android Image to 0x%08lx\n", load_address);
 
   free(hdr);
   return load_address;
 
fail:
   free(hdr);
   return -1;
}
 
static struct andr_img_hdr *
extract_boot_image_v012_header(struct blk_desc *dev_desc,
                  const disk_partition_t *boot_img)
{
   struct andr_img_hdr *hdr;
   long blk_cnt, blks_read;
 
   blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz);
   hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz);
 
   if (!blk_cnt || !hdr)
       return NULL;
 
   blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr);
   if (blks_read != blk_cnt) {
       debug("boot img header blk cnt is %ld and blks read is %ld\n",
             blk_cnt, blks_read);
       return NULL;
   }
 
   if (android_image_check_header((void *)hdr)) {
       printf("boot header magic is invalid.\n");
       return NULL;
   }
 
   if (hdr->page_size < sizeof(*hdr)) {
       printf("android hdr is over size\n");
       return NULL;
   }
 
   return hdr;
}
 
static struct boot_img_hdr_v34 *
extract_boot_image_v34_header(struct blk_desc *dev_desc,
                 const disk_partition_t *boot_img)
{
   struct boot_img_hdr_v34 *boot_hdr;
   disk_partition_t part;
   long blk_cnt, blks_read;
 
   blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz);
   boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz);
 
   if (!blk_cnt || !boot_hdr)
       return NULL;
 
   blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr);
   if (blks_read != blk_cnt) {
       debug("boot img header blk cnt is %ld and blks read is %ld\n",
             blk_cnt, blks_read);
       return NULL;
   }
 
   if (android_image_check_header((void *)boot_hdr)) {
       printf("boot header magic is invalid.\n");
       return NULL;
   }
 
   if (boot_hdr->header_version < 3) {
       printf("boot header %d, is not >= v3.\n",
              boot_hdr->header_version);
       return NULL;
   }
 
   /* Start from android-13 GKI, it doesn't assign 'os_version' */
   if (boot_hdr->header_version >= 4 && boot_hdr->os_version == 0) {
       if (part_get_info_by_name(dev_desc,
               ANDROID_PARTITION_INIT_BOOT, &part) > 0)
           boot_hdr->os_version = 13 << 25;
   }
 
   return boot_hdr;
}
 
static struct vendor_boot_img_hdr_v34 *
extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc,
                    const disk_partition_t *part_vendor_boot)
{
   struct vendor_boot_img_hdr_v34 *vboot_hdr;
   long blk_cnt, blks_read;
 
   blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34),
               part_vendor_boot->blksz);
   vboot_hdr = (struct vendor_boot_img_hdr_v34 *)
               malloc(blk_cnt * part_vendor_boot->blksz);
 
   if (!blk_cnt || !vboot_hdr)
       return NULL;
 
   blks_read = blk_dread(dev_desc, part_vendor_boot->start,
                 blk_cnt, vboot_hdr);
   if (blks_read != blk_cnt) {
       debug("vboot img header blk cnt is %ld and blks read is %ld\n",
             blk_cnt, blks_read);
       return NULL;
   }
 
   if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic,
           VENDOR_BOOT_MAGIC_SIZE)) {
       printf("vendor boot header is invalid.\n");
       return NULL;
   }
 
   if (vboot_hdr->header_version < 3) {
       printf("vendor boot header %d, is not >= v3.\n",
              vboot_hdr->header_version);
       return NULL;
   }
 
   return vboot_hdr;
}
 
int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr,
              const struct vendor_boot_img_hdr_v34 *vendor_boot_hdr,
              const struct boot_img_hdr_v34 *init_boot_hdr,
              struct andr_img_hdr *hdr, bool save_hdr)
{
   memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE);
   memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE);
 
   hdr->kernel_size = boot_hdr->kernel_size;
   /* don't use vendor_boot_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */
   hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR;
 
   /*
    * generic ramdisk: immediately following the vendor ramdisk.
    * It can be from init_boot.img or boot.img.
    */
   if (init_boot_hdr)
       hdr->ramdisk_size = init_boot_hdr->ramdisk_size;
   else
       hdr->ramdisk_size = boot_hdr->ramdisk_size;
 
   /* actually, useless */
   hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0);
 
   /* removed in v3 */
   hdr->second_size = 0;
   hdr->second_addr = 0;
 
   hdr->tags_addr = vendor_boot_hdr->tags_addr;
 
   /* fixed in v3 */
   hdr->page_size = 4096;
   hdr->header_version = boot_hdr->header_version;
   hdr->os_version = boot_hdr->os_version;
 
   memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE);
   strncpy(hdr->name, (const char *)vendor_boot_hdr->name, ANDR_BOOT_NAME_SIZE);
 
   /* removed in v3 */
   memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE);
   memset(hdr->id, 0, 32);
   memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE);
   hdr->recovery_dtbo_size = 0;
   hdr->recovery_dtbo_offset = 0;
 
   hdr->header_size = boot_hdr->header_size;
   hdr->dtb_size = vendor_boot_hdr->dtb_size;
   hdr->dtb_addr = vendor_boot_hdr->dtb_addr;
 
   /* boot_img_hdr_v34 fields */
   hdr->vendor_ramdisk_size = vendor_boot_hdr->vendor_ramdisk_size;
   hdr->vendor_page_size = vendor_boot_hdr->page_size;
   hdr->vendor_header_version = vendor_boot_hdr->header_version;
   hdr->vendor_header_size = vendor_boot_hdr->header_size;
 
   hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE);
   if (!hdr->total_cmdline)
       return -ENOMEM;
   strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline,
       sizeof(boot_hdr->cmdline));
   strncat(hdr->total_cmdline, " ", 1);
   strncat(hdr->total_cmdline, (const char *)vendor_boot_hdr->cmdline,
       sizeof(vendor_boot_hdr->cmdline));
 
   /* new for header v4 */
   if (vendor_boot_hdr->header_version >= 4) {
       hdr->vendor_ramdisk_table_size =
               vendor_boot_hdr->vendor_ramdisk_table_size;
       hdr->vendor_ramdisk_table_entry_num =
               vendor_boot_hdr->vendor_ramdisk_table_entry_num;
       hdr->vendor_ramdisk_table_entry_size =
               vendor_boot_hdr->vendor_ramdisk_table_entry_size;
       /*
        * If we place additional "androidboot.xxx" parameters after
        * bootconfig, this field value should be increased,
        * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE.
        */
       hdr->vendor_bootconfig_size =
               vendor_boot_hdr->vendor_bootconfig_size;
   } else {
       hdr->vendor_ramdisk_table_size = 0;
       hdr->vendor_ramdisk_table_entry_num = 0;
       hdr->vendor_ramdisk_table_entry_size = 0;
       hdr->vendor_bootconfig_size = 0;
   }
 
   hdr->init_boot_buf = save_hdr ? (void *)init_boot_hdr : 0;
   hdr->vendor_boot_buf = save_hdr ? (void *)vendor_boot_hdr : 0;
 
   if (hdr->page_size < sizeof(*hdr)) {
       printf("android hdr is over size\n");
       return -EINVAL;
   }
 
   return 0;
}
 
/*
 * The possible cases of boot.img + recovery.img:
 *
 * [N]: 0, 1, 2
 * [M]: 0, 1, 2, 3, 4
 *
 * |--------------------|---------------------|
 * |   boot.img         |    recovery.img     |
 * |--------------------|---------------------|
 * | boot_img_hdr_v[N]  |  boot_img_hdr_v[N]  | <= if A/B is not required
 * |--------------------|---------------------|
 * | boot_img_hdr_v34   |  boot_img_hdr_v2    | <= if A/B is not required
 * |------------------------------------------|
 * | boot_img_hdr_v[M], no recovery.img       | <= if A/B is required
 * |------------------------------------------|
 */
struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
                      disk_partition_t *part_boot)
{
   disk_partition_t part_vendor_boot;
   disk_partition_t part_init_boot;
   struct vendor_boot_img_hdr_v34 *vboot_hdr = NULL;
   struct boot_img_hdr_v34 *iboot_hdr = NULL;
   struct boot_img_hdr_v34 *boot_hdr = NULL;
   struct andr_img_hdr *andr_hdr = NULL;
   int header_version;
   int andr_version;
 
   if (!dev_desc || !part_boot)
       return NULL;
 
   andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz);
   if (!andr_hdr)
       return NULL;
 
   if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) {
       free(andr_hdr);
       return NULL;
   }
 
   if (android_image_check_header(andr_hdr)) {
       free(andr_hdr);
       return NULL;
   }
 
   header_version = andr_hdr->header_version;
   free(andr_hdr);
   andr_hdr = NULL;
 
   if (header_version < 3) {
       return extract_boot_image_v012_header(dev_desc, part_boot);
   } else {
       if (part_get_info_by_name(dev_desc,
                     ANDROID_PARTITION_VENDOR_BOOT,
                     &part_vendor_boot) < 0) {
           printf("No vendor boot partition\n");
           return NULL;
       }
       boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot);
       vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc,
                           &part_vendor_boot);
       if (!boot_hdr || !vboot_hdr)
           goto image_load_exit;
 
       andr_version = (boot_hdr->os_version >> 25) & 0x7f;
       if (header_version >= 4 && andr_version >= 13) {
           if (part_get_info_by_name(dev_desc,
                         ANDROID_PARTITION_INIT_BOOT,
                         &part_init_boot) < 0) {
               printf("No init boot partition\n");
               return NULL;
           }
           iboot_hdr = extract_boot_image_v34_header(dev_desc, &part_init_boot);
           if (!iboot_hdr)
               goto image_load_exit;
           if (!iboot_hdr->ramdisk_size) {
               printf("No ramdisk in init boot partition\n");
               goto image_load_exit;
           }
       }
 
       andr_hdr = (struct andr_img_hdr *)
               malloc(sizeof(struct andr_img_hdr));
       if (!andr_hdr) {
           printf("No memory for andr hdr\n");
           goto image_load_exit;
       }
 
       if (populate_boot_info(boot_hdr, vboot_hdr,
                      iboot_hdr, andr_hdr, false)) {
           printf("populate boot info failed\n");
           goto image_load_exit;
       }
 
image_load_exit:
       if (boot_hdr)
           free(boot_hdr);
       if (iboot_hdr)
           free(iboot_hdr);
       if (vboot_hdr)
           free(vboot_hdr);
 
       return andr_hdr;
   }
 
   return NULL;
}
 
#if !defined(CONFIG_SPL_BUILD)
/**
 * android_print_contents - prints out the contents of the Android format image
 * @hdr: pointer to the Android format image header
 *
 * android_print_contents() formats a multi line Android image contents
 * description.
 * The routine prints out Android image properties
 *
 * returns:
 *     no returned results
 */
void android_print_contents(const struct andr_img_hdr *hdr)
{
   const char * const p = IMAGE_INDENT_STRING;
   /* os_version = ver << 11 | lvl */
   u32 os_ver = hdr->os_version >> 11;
   u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
   u32 header_version = hdr->header_version;
 
   printf("%skernel size:      %x\n", p, hdr->kernel_size);
   printf("%skernel address:   %x\n", p, hdr->kernel_addr);
   printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
   printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
   printf("%ssecond size:      %x\n", p, hdr->second_size);
   printf("%ssecond address:   %x\n", p, hdr->second_addr);
   printf("%stags address:     %x\n", p, hdr->tags_addr);
   printf("%spage size:        %x\n", p, hdr->page_size);
   printf("%sheader_version:   %x\n", p, header_version);
   /* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
    * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
   printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
          p, hdr->os_version,
          (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
          (os_lvl >> 4) + 2000, os_lvl & 0x0F);
   printf("%sname:             %s\n", p, hdr->name);
   printf("%scmdline:          %s\n", p, hdr->cmdline);
 
   if (header_version == 1 || header_version == 2) {
       printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
       printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
       printf("%sheader size:           %x\n", p, hdr->header_size);
   }
 
   if (header_version == 2 || header_version == 3) {
       printf("%sdtb size:              %x\n", p, hdr->dtb_size);
       printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
   }
 
   if (header_version >= 3) {
       printf("%scmdline:               %s\n", p, hdr->total_cmdline);
       printf("%svendor ramdisk size:   %x\n", p, hdr->vendor_ramdisk_size);
       printf("%svendor page size:      %x\n", p, hdr->vendor_page_size);
       printf("%svendor header version: %d\n", p, hdr->vendor_header_version);
       printf("%svendor header size:    %x\n", p, hdr->vendor_header_size);
   }
 
   if (header_version >= 4) {
       printf("%svendor ramdisk table size:        %x\n",
              p, hdr->vendor_ramdisk_table_size);
       printf("%svendor ramdisk table entry num:   %x\n",
              p, hdr->vendor_ramdisk_table_entry_num);
       printf("%svendor ramdisk table entry size:  %x\n",
              p, hdr->vendor_ramdisk_table_entry_size);
       printf("%svendor bootconfig size:           %d\n",
              p, hdr->vendor_bootconfig_size);
   }
}
#endif