summaryrefslogtreecommitdiff
path: root/util/cbfstool/cse_serger.c
blob: 9d74a120c3a55ecb6bc5665d99a0ea77a74c3d0d (plain)
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
/* SPDX-License-Identifier: GPL-2.0-only */
/* CSE Serger - Tool for stitching Intel CSE components */

#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "cse_serger.h"

#define NO_PARTITION_TYPE	(-1)

static struct params {
	bool print_sub_parts;
	const char *partition_name;
	int partition_type;
	const char *output_dir;
	const char *image_name;
	const char *version_str;
	const char *input_file;
	struct region layout_regions[BP_TOTAL];
	const char *layout_files[BP_TOTAL];
} params;

static const struct {
	const char *version_str;
	const struct bpdt_ops *ops;
} bpdt_ops_table[] = {
	{ "1.6", &bpdt_1_6_ops },
	{ "1.7", &bpdt_1_7_ops },
};

static const struct {
	enum subpart_hdr_version version;
	const struct subpart_hdr_ops *ops;
} subpart_hdr_ops_table[] = {
	{ SUBPART_HDR_VERSION_1, &subpart_hdr_1_ops },
	{ SUBPART_HDR_VERSION_2, &subpart_hdr_2_ops },
};

static const struct {
	enum subpart_entry_version version;
	const struct subpart_entry_ops *ops;
} subpart_entry_ops_table[] = {
	{ SUBPART_ENTRY_VERSION_1, &subpart_entry_1_ops },
};

enum bpdt_entry_type {
	SMIP = 0,
	CSE_RBE = 1,
	CSE_BUP = 2,
	UCODE = 3,
	IBB = 4,
	S_BPDT = 5,
	OBB = 6,
	CSE_MAIN = 7,
	ISH = 8,
	CSE_IDLM = 9,
	IFP_OVERRIDE = 10,
	UTOK = 11,
	UFS_PHY = 12,
	UFS_GPP = 13,
	PMC = 14,
	IUNIT = 15,
	NVM_CFG = 16,
	UEP = 17,
	OEM_KM = 20,
	PAVP = 22,
	IOM_FW = 23,
	NPHY_FW = 24,
	TBT_FW = 25,
	ICC = 32,

	MAX_SUBPARTS,
};

static struct {
	struct buffer input_buff;

	const struct bpdt_ops *bpdt_ops;
	const struct subpart_hdr_ops *subpart_hdr_ops;
	const struct subpart_entry_ops *subpart_entry_ops;

	bpdt_hdr_ptr bpdt_hdr;
	cse_layout_ptr cse_layout;
	struct bpdt_entry bpdt_entries[MAX_SUBPARTS];
	struct buffer subpart_buff[MAX_SUBPARTS];
	bool repack;
	size_t file_end_offset;
} ifwi;

#define SUBPART_WITH_ALT(_index, _rname, _name, _aname)	\
				[_index] = { _rname, _name, _aname }
#define SUBPART(_index, _rname, _name)				\
				SUBPART_WITH_ALT(_index, _rname, _name, "")

static const struct {
	const char *readable_name;
	const char *name;
	const char *alt_name;
} subparts[] = {
	SUBPART(SMIP, "OEM SMIP", "SMIP"),
	SUBPART(CSE_RBE, "CSE RBE", "RBEP"),
	SUBPART_WITH_ALT(CSE_BUP, "CSE BUP", "FTPR", "MFTP"),
	SUBPART(UCODE, "Microcode", "UCOD"),
	SUBPART(IBB, "Initial Boot Block", "IBBP"),
	SUBPART(S_BPDT, "Secondary BPDT", "SBDT"),
	SUBPART(OBB, "OEM Boot Block", "OBBP"),
	SUBPART(CSE_MAIN, "CSE Main", "NFTP"),
	SUBPART(ISH, "ISH Firmware", "ISHP"),
	SUBPART(CSE_IDLM, "CSE IDLM", "DLMP"),
	SUBPART(IFP_OVERRIDE, "IFP override", "IFPP"),
	SUBPART(UTOK, "Debug tokens", "UTOK"),
	SUBPART(UFS_PHY, "UFS Phy", "UFSP"),
	SUBPART(UFS_GPP, "UFS GPP", "UFSG"),
	SUBPART(PMC, "PMC Firmware", "PMCP"),
	SUBPART(IUNIT, "IUNIT Firmware", "IUNP"),
	SUBPART(NVM_CFG, "NVM CFG", "NVMC"),
	SUBPART(UEP, "UEP", "UEPP"),
	SUBPART(OEM_KM, "OEM Key Manifest", "OEMP"),
	SUBPART(PAVP, "PAVP", "PAVP"),
	SUBPART(IOM_FW, "IOM Firmware", "IOMP"),
	SUBPART(NPHY_FW, "NPHY Firmware", "NPHY"),
	SUBPART(TBT_FW, "TBT Firmware", "TBTP"),
	SUBPART(ICC, "ICC Firmware", "PCHC"),
};

static const char *subpart_readable_name(enum bpdt_entry_type type)
{
	return subparts[type].readable_name;
}

static const char *subpart_name(enum bpdt_entry_type type)
{
	return subparts[type].name;
}

static const char *subpart_alt_name(enum bpdt_entry_type type)
{
	return subparts[type].alt_name;
}

static struct buffer *subpart_buff(int type)
{
	return &ifwi.subpart_buff[type];
}

static int subpart_get_type_from_name(const char *name)
{
	int i;

	for (i = 0; i < MAX_SUBPARTS; i++) {
		if (subpart_name(i) == NULL)
			continue;

		if (!strcmp(subpart_name(i), name))
			return i;

		if (!strcmp(subpart_alt_name(i), name))
			return i;
	}

	return -1;
}

static const struct bpdt_ops *get_bpdt_ops(const struct buffer *buff)
{
	assert(buff || params.version_str);

	for (size_t i = 0; i < ARRAY_SIZE(bpdt_ops_table); i++) {
		if (params.version_str) {
			if (!strcmp(params.version_str, bpdt_ops_table[i].version_str))
				return bpdt_ops_table[i].ops;
			else
				continue;
		}
		if (bpdt_ops_table[i].ops->match_version(buff))
			return bpdt_ops_table[i].ops;
	}

	return NULL;
}

static const struct subpart_hdr_ops *get_subpart_hdr_ops(void)
{
	for (size_t i = 0; i < ARRAY_SIZE(subpart_hdr_ops_table); i++) {
		if (subpart_hdr_ops_table[i].version == ifwi.bpdt_ops->subpart_hdr_version)
			return subpart_hdr_ops_table[i].ops;
	}

	return NULL;
}

static const struct subpart_entry_ops *get_subpart_entry_ops(void)
{
	for (size_t i = 0; i < ARRAY_SIZE(subpart_entry_ops_table); i++) {
		if (subpart_entry_ops_table[i].version == ifwi.bpdt_ops->subpart_entry_version)
			return subpart_entry_ops_table[i].ops;
	}

	return NULL;
}

static int subpart_read(struct buffer *input_buff)
{
	size_t input_size = buffer_size(input_buff);
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];
	struct buffer *buff;

	for (size_t i = 0; i < ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr); i++, e++) {
		if (e->size == 0)
			continue;

		if (e->type >= MAX_SUBPARTS) {
			ERROR("Invalid part type(%d)\n", e->type);
			return -1;
		}

		if (e->offset + e->size > input_size) {
			ERROR("Part(%d) exceeds file size. Part offset=0x%x, Part size = 0x%x, File size = 0x%zx\n",
			      e->type, e->offset, e->size, input_size);
			return -1;
		}

		buff = subpart_buff(e->type);
		if (buffer_size(buff) != 0) {
			ERROR("Multiple subparts of same type(%d %s)!\n",
			      e->type, subpart_name(e->type));
			return -1;
		}

		buffer_splice(buff, input_buff, e->offset, e->size);
	}

	return 0;
}

static struct bpdt_entry *find_bpdt_entry(uint32_t type)
{
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];

	for (size_t i = 0; i < ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr); i++, e++) {
		if (e->type == type)
			return e;
	}

	return NULL;
}

static struct bpdt_entry *new_bpdt_entry(void)
{
	size_t count = ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr);
	if (count == MAX_SUBPARTS) {
		ERROR("No space for new BPDT entry!\n");
		return NULL;
	}

	ifwi.bpdt_ops->inc_entry_count(ifwi.bpdt_hdr);

	return &ifwi.bpdt_entries[count];
}

static void set_file_end_offset(struct buffer *buff)
{
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];
	size_t end_offset;
	size_t count = ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr);

	ifwi.file_end_offset = ALIGN_UP(buffer_offset(buff), BUFF_SIZE_ALIGN);

	for (size_t i = 0; i < count; i++, e++) {
		end_offset = e->offset + e->size;
		if (end_offset > ifwi.file_end_offset)
			ifwi.file_end_offset = end_offset;

	}
}

static void read_bpdt_entries(struct buffer *buff)
{
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];
	size_t count = ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr);

	for (size_t i = 0; i < count; i++, e++) {
		READ_MEMBER(buff, e->type);
		READ_MEMBER(buff, e->offset);
		READ_MEMBER(buff, e->size);
	}
}

static int write_bpdt_entries(struct buffer *buff)
{
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];
	size_t count = ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr);

	if (buffer_size(buff) < count * sizeof(*e)) {
		ERROR("Not enough buffer space for bpdt entries!\n");
		return -1;
	}

	for (size_t i = 0; i < count; i++, e++) {
		WRITE_MEMBER(buff, e->type);
		WRITE_MEMBER(buff, e->offset);
		WRITE_MEMBER(buff, e->size);
	}

	return 0;
}

static void print_bpdt_entries(void)
{
	const size_t count = ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr);

	if (count == 0)
		return;

	const struct bpdt_entry *e = &ifwi.bpdt_entries[0];

	printf("\n * BPDT entries\n");

	printf("%-25s%-25s%-25s%-25s%-25s%-25s\n", "Entry #",
	       "Partition Name", "Human readable name", "Type", "Offset", "Size");

	printf("===================================================================="
	       "====================================================================\n");

	for (size_t i = 0; i < count; i++) {
		printf("%-25zd%-25s%-25s%-25d0x%-23x0x%-23x"
		       "\n", i+1, subpart_name(e[i].type), subpart_readable_name(e[i].type),
		       e[i].type, e[i].offset, e[i].size);
	}

	printf("===================================================================="
	       "====================================================================\n");
}

static int ifwi_parse(const char *image_name)
{
	struct buffer *input_buff = &ifwi.input_buff;
	struct buffer bpdt_buff;

	if (buffer_from_file(input_buff, image_name)) {
		ERROR("Failed to read input file %s\n", image_name);
		return -1;
	}

	buffer_clone(&bpdt_buff, input_buff);

	ifwi.bpdt_ops = get_bpdt_ops(&bpdt_buff);
	if (!ifwi.bpdt_ops) {
		ERROR("No matching bpdt_ops!\n");
		return -1;
	}

	ifwi.bpdt_hdr = ifwi.bpdt_ops->read_hdr(&bpdt_buff);
	if (ifwi.bpdt_hdr == NULL)
		return -1;

	read_bpdt_entries(&bpdt_buff);
	set_file_end_offset(&bpdt_buff);

	if (!ifwi.bpdt_ops->validate_checksum(ifwi.bpdt_hdr, &ifwi.bpdt_entries[0])) {
		ERROR("Checksum failed!\n");
		return -1;
	}

	ifwi.subpart_hdr_ops = get_subpart_hdr_ops();
	if (ifwi.subpart_hdr_ops == NULL) {
		ERROR("No matching subpart_hdr_ops for given BPDT!\n");
		return -1;
	}

	ifwi.subpart_entry_ops = get_subpart_entry_ops();
	if (ifwi.subpart_entry_ops == NULL) {
		ERROR("No matching subpart_entry_ops for given BPDT!\n");
		return -1;
	}

	return subpart_read(&ifwi.input_buff);
}

static int subpart_write(struct buffer *buff)
{
	struct bpdt_entry *e;
	struct buffer *s_buff;

	for (size_t i = 0; i < ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr); i++) {
		e = &ifwi.bpdt_entries[i];

		if (e->size == 0)
			continue;

		if (e->offset + e->size > buffer_size(buff)) {
			ERROR("Subpart end(0x%x) overflows buffer size(0x%zx)\n",
			      e->offset + e->size, buffer_size(buff));
			return -1;
		}

		s_buff = subpart_buff(e->type);

		if (buffer_size(s_buff) != e->size) {
			ERROR("Subpart buffer size does not match BPDT entry size!\n");
			return -1;
		}

		memcpy(buffer_get(buff) + e->offset, buffer_get(s_buff), e->size);
	}

	return 0;
}

static int ifwi_repack(void)
{
	if (!ifwi.repack)
		return 0;

	struct buffer output_buff;
	const size_t size = ifwi.file_end_offset;
	struct buffer bpdt_buff;

	if (buffer_create(&output_buff, size, "Output IFWI")) {
		ERROR("Unable to allocate output buff!\n");
		return -1;
	}

	buffer_clone(&bpdt_buff, &output_buff);

	ifwi.bpdt_ops->update_checksum(ifwi.bpdt_hdr, &ifwi.bpdt_entries[0]);

	if (ifwi.bpdt_ops->write_hdr(&bpdt_buff, ifwi.bpdt_hdr))
		return -1;

	if (write_bpdt_entries(&bpdt_buff))
		return -1;

	subpart_write(&output_buff);

	if (buffer_write_file(&output_buff, params.image_name)) {
		ERROR("File write error!\n");
		return -1;
	}

	printf("Image written successfully to %s.\n", params.image_name);
	return 0;
}

static bool should_process_partition(int type)
{
	if (params.partition_name) {
		const char *name = subpart_name(type);

		if (!name)
			return false;

		if (strcmp(params.partition_name, name))
			return false;
	} else if (params.partition_type != NO_PARTITION_TYPE) {
		if (params.partition_type != type)
			return false;
	}

	return true;
}

static int process_entries(int (*fn)(const struct bpdt_entry *e))
{
	struct bpdt_entry *e = &ifwi.bpdt_entries[0];
	bool found = false;

	for (size_t i = 0; i < ifwi.bpdt_ops->get_entry_count(ifwi.bpdt_hdr); i++, e++) {
		if (e->size == 0)
			continue;

		if (!should_process_partition(e->type))
			continue;

		if (fn(e))
			return -1;

		found = true;
	}

	if (!found && params.partition_name) {
		ERROR("Partition %s not found!\n", params.partition_name);
		return -1;
	}

	if (!found && params.partition_type != NO_PARTITION_TYPE) {
		ERROR("Partition type %d not found!\n", params.partition_type);
		return -1;
	}

	return 0;
}

static int print_subpart(const struct bpdt_entry *e)
{
	struct buffer buff;
	subpart_hdr_ptr hdr;

	printf("\n\n * Subpart entry #%d(%s)\n", e->type, subpart_readable_name(e->type));

	buffer_clone(&buff, subpart_buff(e->type));
	hdr = ifwi.subpart_hdr_ops->read(&buff);
	if (!hdr) {
		ERROR("Failed to read subpart header!\n");
		return -1;
	}

	ifwi.subpart_hdr_ops->print(hdr);
	ifwi.subpart_entry_ops->print(&buff, ifwi.subpart_hdr_ops->get_entry_count(hdr));
	ifwi.subpart_hdr_ops->free(hdr);

	return 0;
}

static int cmd_print(void)
{
	ifwi.bpdt_ops->print_hdr(ifwi.bpdt_hdr);
	print_bpdt_entries();

	if (!params.print_sub_parts && !params.partition_name &&
	    params.partition_type == NO_PARTITION_TYPE)
		return 0;

	return process_entries(print_subpart);
}

static char *get_file_path(const char *name)
{
	size_t filename_len = strlen(name) + 1;

	/* output_dir name followed by '/' */
	if (params.output_dir)
		filename_len += strlen(params.output_dir) + 1;

	char *filepath = malloc(filename_len);
	if (!filepath)
		return NULL;

	snprintf(filepath, filename_len, "%s%s%s",
		 params.output_dir ? : "",
		 params.output_dir ? "/" : "",
		 name);

	return filepath;
}

static int write_partition_to_file(const struct bpdt_entry *e)
{
	size_t end_offset = e->offset + e->size - 1;

	if (end_offset > buffer_size(&ifwi.input_buff)) {
		ERROR("Offset out of bounds!\n");
		return -1;
	}

	const char *name = subpart_name(e->type);
	char *filepath = get_file_path(name);
	if (!filepath) {
		ERROR("Failed to allocate filepath!\n");
		return -1;
	}

	printf("Dumping %.4s in %s\n", name, filepath);

	struct buffer buff;
	buffer_splice(&buff, &ifwi.input_buff, e->offset, e->size);
	buffer_write_file(&buff, filepath);

	free(filepath);
	return 0;
}

static int cmd_dump(void)
{
	struct stat sb;

	if (params.output_dir && (stat(params.output_dir, &sb) == -1)) {
		ERROR("Failed to stat %s: %s\n", params.output_dir, strerror(errno));
		return -1;
	}

	return process_entries(write_partition_to_file);
}

static int cmd_print_layout(void)
{
	if (params.version_str == NULL) {
		ERROR("No version provided!\n");
		return -1;
	}

	const struct bpdt_ops *ops = get_bpdt_ops(NULL);
	if (!ops) {
		ERROR("No matching bpdt_ops!\n");
		return -1;
	}

	struct buffer buff;
	if (buffer_from_file(&buff, params.image_name)) {
		ERROR("Failed to read input file %s\n", params.image_name);
		return -1;
	}

	ifwi.cse_layout = ops->read_layout(&buff);
	if (!ifwi.cse_layout) {
		ERROR("Failed to read CSE layout!\n");
		return -1;
	}

	ops->print_layout(ifwi.cse_layout);

	return 0;
}

static int allocate_buffer(struct buffer *buff, struct buffer *wbuff, const char *str)
{
	if (params.version_str == NULL) {
		ERROR("No version provided!\n");
		return -1;
	}

	ifwi.bpdt_ops = get_bpdt_ops(NULL);
	if (!ifwi.bpdt_ops)
		return -1;

	if (buffer_create(buff, BUFF_SIZE_ALIGN, str)) {
		ERROR("Buffer creation error!\n");
		return -1;
	}

	void *data = buffer_get(buff);
	memset(data, 0xff, buffer_size(buff));

	buffer_clone(wbuff, buff);

	return 0;
}

static size_t get_cse_region_end_offset(void)
{
	size_t offset = 0;
	size_t end_offset;

	for (size_t i = 0; i < BP_TOTAL; i++) {
		end_offset = region_last(&params.layout_regions[i]) + 1;
		if (end_offset > offset)
			offset = end_offset;
	}

	return offset;
}

static int fill_layout_buffer(struct buffer *buff)
{
	struct buffer wbuff;

	if (allocate_buffer(buff, &wbuff, "CSE layout"))
		return -1;

	ifwi.cse_layout = ifwi.bpdt_ops->create_layout(&params.layout_regions[0]);
	if (!ifwi.cse_layout) {
		ERROR("Failed to create layout!\n");
		return -1;
	}

	if (ifwi.bpdt_ops->write_layout(&wbuff, ifwi.cse_layout)) {
		ERROR("Failed to write CSE layout!\n");
		return -1;
	}

	return 0;
}

static int cmd_create_layout(void)
{
	struct buffer buff;

	if (fill_layout_buffer(&buff))
		return -1;

	buffer_write_file(&buff, params.image_name);
	return 0;
}

static int cmd_create_cse_region(void)
{
	size_t file_size = get_cse_region_end_offset();
	struct buffer cse_buff, layout_buff;

	if (fill_layout_buffer(&layout_buff))
		return -1;

	if (file_size == 0)
		file_size = buffer_size(&layout_buff);

	file_size = ALIGN_UP(file_size, BUFF_SIZE_ALIGN);
	if (buffer_create(&cse_buff, file_size, "CSE buff")) {
		ERROR("CSE buffer creation error!\n");
		return -1;
	}

	memset(buffer_get(&cse_buff), 0xff, buffer_size(&cse_buff));
	memcpy(buffer_get(&cse_buff), buffer_get(&layout_buff), buffer_size(&layout_buff));

	for (size_t i = 0; i < BP_TOTAL; i++) {
		struct buffer wbuff, rbuff;

		if (region_sz(&params.layout_regions[i]) == 0)
			continue;

		buffer_clone(&wbuff, &cse_buff);
		buffer_seek(&wbuff, region_offset(&params.layout_regions[i]));

		if (params.layout_files[i] == NULL) {
			if (i == 0) {
				ERROR("File name not provided for DP!\n");
			} else {
				ERROR("File name not provided for BP%zd!\n", i);
			}
			return -1;
		}

		if (buffer_from_file(&rbuff, params.layout_files[i])) {
			ERROR("Failed to read %s\n", params.layout_files[i]);
			return -1;
		}

		assert(buffer_size(&wbuff) >= buffer_size(&rbuff));
		memcpy(buffer_get(&wbuff), buffer_get(&rbuff), buffer_size(&rbuff));
	}

	buffer_write_file(&cse_buff, params.image_name);

	return 0;
}

static int cmd_create_bpdt(void)
{
	struct buffer buff;
	struct buffer wbuff;

	if (allocate_buffer(&buff, &wbuff, "BPDT header"))
		return -1;

	ifwi.bpdt_hdr = ifwi.bpdt_ops->create_hdr();
	if (!ifwi.bpdt_hdr) {
		ERROR("Failed to create BPDT header!\n");
		return -1;
	}

	ifwi.bpdt_ops->update_checksum(ifwi.bpdt_hdr, NULL);

	if (ifwi.bpdt_ops->write_hdr(&wbuff, ifwi.bpdt_hdr)) {
		ERROR("Failed to write BPDT header!\n");
		return -1;
	}

	buffer_write_file(&buff, params.image_name);
	return 0;
}

static int cmd_add(void)
{
	if (!params.partition_name && params.partition_type == NO_PARTITION_TYPE) {
		ERROR("Partition name/type is required for add!\n");
		return -1;
	}

	int type;

	if (params.partition_name) {
		type = subpart_get_type_from_name(params.partition_name);
		if (type == NO_PARTITION_TYPE) {
			ERROR("Invalid partition %s\n", params.partition_name);
			return -1;
		}
	} else {
		type = params.partition_type;
		if (type > MAX_SUBPARTS) {
			ERROR("Invalid type %d\n", type);
			return -1;
		}
	}

	struct bpdt_entry *e = find_bpdt_entry(type);
	if (e) {
		ERROR("Partition %s(%d) already exists!\n", params.partition_name ? : "", type);
		return -1;
	}

	e = new_bpdt_entry();
	if (e == NULL)
		return -1;

	e->type = type;
	e->offset = 0;
	e->size = 0;

	ifwi.repack = true;

	if (params.input_file == NULL)
		return 0;

	struct buffer *buff = subpart_buff(type);
	if (buffer_from_file_aligned_size(buff, params.input_file, BUFF_SIZE_ALIGN)) {
		ERROR("Failed to read input file %s\n", params.input_file);
		return -1;
	}

	e->offset = ALIGN_UP(ifwi.file_end_offset, BUFF_SIZE_ALIGN);
	e->size = buffer_size(buff);

	ifwi.file_end_offset = e->offset + e->size;

	return 0;
}

static int parse_region(struct region *r, char *arg)
{
	char *tok;

	tok = strtok(arg, ":");
	unsigned long offset = strtoul(tok, NULL, 0);

	tok = strtok(NULL, ":");
	unsigned long size = strtoul(tok, NULL, 0);

	if (region_create_untrusted(r, offset, size) != CB_SUCCESS) {
		ERROR("Invalid region: %lx:%lx\n", offset, size);
		return -1;
	}

	return 0;
}

static struct command {
	const char *name;
	const char *optstring;
	int (*cb)(void);
	bool parse_ifwi;
} commands[] = {
	{ "print", "n:st:?", cmd_print, true },
	{ "dump", "n:o:t:?", cmd_dump, true },
	{ "create-layout", "v:?", cmd_create_layout, false },
	{ "print-layout", "v:?", cmd_print_layout, false },
	{ "create-bpdt", "v:?", cmd_create_bpdt, false },
	{ "add", "f:n:t:v:?", cmd_add, true },
	{ "create-cse-region", "v:?", cmd_create_cse_region, false },
};

enum {
	LONGOPT_START = 256,
	LONGOPT_BP1 = LONGOPT_START,
	LONGOPT_BP2,
	LONGOPT_BP3,
	LONGOPT_BP4,
	LONGOPT_DATA,
	LONGOPT_BP1_FILE,
	LONGOPT_BP2_FILE,
	LONGOPT_BP3_FILE,
	LONGOPT_BP4_FILE,
	LONGOPT_DATA_FILE,

	LONGOPT_END,
};

static struct option long_options[] = {
	{"help",	    required_argument, 0, 'h'},
	{"parition_name",   required_argument, 0, 'n'},
	{"output_dir",      required_argument, 0, 'o'},
	{"sub_partition",   no_argument,       0, 's'},
	{"version",         required_argument, 0, 'v'},
	{"bp1",             required_argument, 0, LONGOPT_BP1},
	{"bp1_file",        required_argument, 0, LONGOPT_BP1_FILE},
	{"bp2",             required_argument, 0, LONGOPT_BP2},
	{"bp2_file",        required_argument, 0, LONGOPT_BP2_FILE},
	{"bp3",             required_argument, 0, LONGOPT_BP3},
	{"bp3_file",        required_argument, 0, LONGOPT_BP3_FILE},
	{"bp4",             required_argument, 0, LONGOPT_BP4},
	{"bp4_file",        required_argument, 0, LONGOPT_BP4_FILE},
	{"dp",              required_argument, 0, LONGOPT_DATA},
	{"dp_file",         required_argument, 0, LONGOPT_DATA_FILE},
	{NULL,		    0,                 0,  0 }
};

static bool valid_opt(size_t i, int c)
{
	/* Check if it is one of the optstrings supported by the command. */
	if (strchr(commands[i].optstring, c))
		return true;

	/*
	 * Check if it is one of the non-ASCII characters. Currently, the
	 * non-ASCII characters are only checked against the valid list
	 * irrespective of the command.
	 */
	return c >= LONGOPT_START && c < LONGOPT_END;
}

static void usage(const char *name)
{
	printf("%s: Utility for stitching CSE components\n"
	       "USAGE:\n"
	       " %s FILE COMMAND\n\n"
	       "COMMANDs:\n"
	       " print [-s][-n NAME][-t TYPE]\n"
	       " dump [-o DIR][-n NAME]\n"
	       " create-layout --dp <offset:size> --bp* <offset:size> -v VERSION\n"
	       " create-cse-region --dp <offset:size> --dp_file <FILE> --bp* <offset:size>"
	       " --bp*_file <FILE> -v VERSION\n"
	       " print-layout -v VERSION\n"
	       " create-bpdt -v VERSION\n"
	       " add [-n NAME][-t TYPE][-f INPUT_FILE]\n"
	       "\nOPTIONS:\n"
	       " -f INPUT_FILE       : Input file\n"
	       " -n NAME             : Sub-partition name\n"
	       " -o DIR              : Output directory\n"
	       " -s                  : Print sub-partition info\n"
	       " -t TYPE             : Sub-partition type\n"
	       " -v VERSION          : BPDT version\n"
	       " --dp <offset:size>  : Offset and size of data partition\n"
	       " --dp_file <FILE>    : File for data partition\n"
	       " --bp1 <offset:size> : Offset and size of BP1\n"
	       " --bp1_file <FILE>   : File for BP1 partition\n"
	       " --bp2 <offset:size> : Offset and size of BP2\n"
	       " --bp2_file <FILE>   : File for BP2 partition\n"
	       " --bp3 <offset:size> : Offset and size of BP3\n"
	       " --bp3_file <FILE>   : File for BP3 partition\n"
	       " --bp4 <offset:size> : Offset and size of BP4\n"
	       " --bp4_file <FILE>   : File for BP4 partition\n"
	       "\n",
	       name, name);
}

int main(int argc, char **argv)
{
	if (argc < 3) {
		printf("Incorrect number of args(%d)!\n", argc);
		usage(argv[0]);
		return 1;
	}

	const char *prog_name = argv[0];
	const char *image_name = argv[1];
	const char *cmd = argv[2];

	size_t i;

	params.partition_type = NO_PARTITION_TYPE;
	params.image_name = image_name;

	for (i = 0; i < ARRAY_SIZE(commands); i++) {
		if (strcmp(cmd, commands[i].name))
			continue;

		int c;
		int option_index;

		while (1) {
			c = getopt_long(argc, argv, commands[i].optstring,
					long_options, &option_index);

			if (c == -1)
				break;

			if (!valid_opt(i, c)) {
				if (c < LONGOPT_START) {
					ERROR("Invalid option -- '%c'\n", c);
				} else {
					ERROR("Invalid option -- '%d'\n", c);
				}
				usage(prog_name);
				return 1;
			}

			switch (c) {
			case 'f':
				params.input_file = optarg;
				break;
			case 'n':
				params.partition_name = optarg;
				break;
			case 'o':
				params.output_dir = optarg;
				break;
			case 's':
				params.print_sub_parts = true;
				break;
			case 'v':
				params.version_str = optarg;
				break;
			case 't':
				params.partition_type = atoi(optarg);
				break;
			case LONGOPT_BP1:
				if (parse_region(&params.layout_regions[BP1], optarg))
					return 1;
				break;
			case LONGOPT_BP2:
				if (parse_region(&params.layout_regions[BP2], optarg))
					return 1;
				break;
			case LONGOPT_BP3:
				if (parse_region(&params.layout_regions[BP3], optarg))
					return 1;
				break;
			case LONGOPT_BP4:
				if (parse_region(&params.layout_regions[BP4], optarg))
					return 1;
				break;
			case LONGOPT_DATA:
				if (parse_region(&params.layout_regions[DP], optarg))
					return 1;
				break;
			case LONGOPT_BP1_FILE:
				params.layout_files[BP1] = optarg;
				break;
			case LONGOPT_BP2_FILE:
				params.layout_files[BP2] = optarg;
				break;
			case LONGOPT_BP3_FILE:
				params.layout_files[BP3] = optarg;
				break;
			case LONGOPT_BP4_FILE:
				params.layout_files[BP4] = optarg;
				break;
			case LONGOPT_DATA_FILE:
				params.layout_files[DP] = optarg;
				break;
			case 'h':
			case '?':
			default:
				usage(prog_name);
				return 1;
			}
		}

		break;
	}

	if (i == ARRAY_SIZE(commands)) {
		printf("No command match %s!\n", cmd);
		usage(prog_name);
		return 1;
	}

	if (commands[i].parse_ifwi && ifwi_parse(image_name))
		return 1;

	if (commands[i].cb())
		return 1;

	return ifwi_repack();
}