forked from intel/kernelflinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.c
1350 lines (1159 loc) · 33.5 KB
/
installer.c
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
/*
* Copyright (c) 2015, Intel Corporation
* All rights reserved.
*
* Author: Jeremy Compostella <jeremy.compostella@intel.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <efi.h>
#include <efiapi.h>
#include <efilib.h>
#include <stdio.h>
#include <transport.h>
#include <version.h>
#include "lib.h"
#include "uefi_utils.h"
#include "protocol.h"
#include "flash.h"
#include "gpt.h"
#include "sparse.h"
#include "sparse_format.h"
#include "fastboot.h"
#include "fastboot_oem.h"
#include "text_parser.h"
#include "android.h"
#include "slot.h"
#include "security.h"
#include "security_interface.h"
#include "security_efi.h"
#ifdef USE_TPM
#include "tpm2_security.h"
#endif
static BOOLEAN last_cmd_succeeded;
static fastboot_handle fastboot_flash_cmd;
static fastboot_handle fastboot_erase_cmd;
static EFI_FILE_IO_INTERFACE *file_io_interface;
static data_callback_t fastboot_rx_cb, fastboot_tx_cb;
static BOOLEAN need_tx_cb;
static char *fastboot_cmd_buf;
static UINTN fastboot_cmd_buf_len;
static char command_buffer[256]; /* Large enough to fit long filename
on flash command. */
static struct download_buffer *dl;
#define inst_perror(ret, x, ...) do { \
fastboot_fail(x ": %r", ##__VA_ARGS__, ret); \
} while (0)
#define MAX_LABEL_LEN 64
static void flush_tx_buffer(void)
{
while (need_tx_cb) {
need_tx_cb = FALSE;
fastboot_tx_cb(NULL, 0);
}
}
static void do_erase(INTN argc, CHAR8 **argv)
{
fastboot_erase_cmd(argc, argv);
flush_tx_buffer();
}
static EFI_STATUS find_partition(CHAR8 *target)
{
EFI_STATUS ret;
CHAR16 *target16;
struct gpt_partition_interface gparti;
target16 = stra_to_str(target);
if (!target16) {
fastboot_fail("Failed to convert target to CHAR16");
return EFI_OUT_OF_RESOURCES;
}
ret = gpt_get_partition_by_label(target16, &gparti, LOGICAL_UNIT_USER);
FreePool(target16);
return ret;
}
static CHAR8 *get_target(CHAR8 *target)
{
EFI_STATUS ret;
CHAR16 *target16;
const CHAR16 *label16;
static CHAR8 label[MAX_LABEL_LEN];
CHAR8 *ret_target = NULL;
ret = find_partition(target);
if (!EFI_ERROR(ret))
return target;
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND)
return NULL;
target16 = stra_to_str(target);
if (!target16)
return NULL;
label16 = slot_label(target16);
if (!label16) {
ret_target = target;
goto out;
}
if (StrLen(label16) + 1 > sizeof(label)) {
fastboot_fail("Label name is too long");
goto out;
}
ret = str_to_stra(label, label16, sizeof(label));
if (!EFI_ERROR(ret))
ret_target = label;
out:
if (target16)
FreePool(target16);
return ret_target;
}
static void installer_erase(INTN argc, CHAR8 **argv)
{
if (argc != 2) {
fastboot_fail("Erase command requires exactly 2 arguments");
return;
}
argv[1] = get_target(argv[1]);
if (!argv[1])
return;
do_erase(argc, argv);
}
static void installer_flash_buffer(void *data, unsigned size,
INTN argc, CHAR8 **argv)
{
void *data_save = dl->data;
dl->data = data;
dl->size = size;
fastboot_flash_cmd(argc, argv);
flush_tx_buffer();
dl->data = data_save;
dl->size = 0;
}
static EFI_STATUS read_file(EFI_FILE *file, UINTN size, void *data)
{
EFI_STATUS ret;
UINTN nsize = size;
ret = uefi_call_wrapper(file->Read, 3, file, &nsize, data);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to read file");
return ret;
}
if (size != nsize) {
fastboot_fail("Failed to read %d bytes (only %d read)",
size, nsize);
return EFI_INVALID_PARAMETER;
}
return ret;
}
typedef struct flash_buffer {
struct sparse_header sph;
struct chunk_header skip_ckh;
union {
struct chunk_header ckh;
char data[1];
} d;
char ckh_data[1];
} __attribute__((__packed__)) flash_buffer_t;
/*when flash big chunks and in two image should add read_flags*/
static EFI_STATUS installer_flash_big_chunk_multiple(EFI_FILE **file, UINTN *read_flags,
UINTN file_size, UINTN *remaining_data, flash_buffer_t *fb, UINTN argc, CHAR8 **argv)
{
EFI_STATUS ret = EFI_INVALID_PARAMETER;
UINTN payload_size, read_size, already_read, ckh_blks, data_size;
const UINTN MAX_DATA_SIZE = dl->max_size - offsetof(flash_buffer_t, ckh_data);
const UINTN MAX_BLKS = MAX_DATA_SIZE / fb->sph.blk_sz;
const UINTN HEADER_SIZE = offsetof(flash_buffer_t, d);
struct chunk_header *ckh;
void *read_ptr;
ckh = &fb->d.ckh;
payload_size = ckh->total_sz - sizeof(*ckh);
fb->sph.total_chunks = 2; /* skip and data chunks. */
for (ckh_blks = ckh->chunk_sz; ckh_blks; ckh_blks -= ckh->chunk_sz) {
ckh->chunk_sz = min(MAX_BLKS, ckh_blks);
data_size = ckh->chunk_sz * fb->sph.blk_sz;
ckh->total_sz = sizeof(*ckh) + data_size;
fb->sph.total_blks = fb->skip_ckh.chunk_sz + ckh->chunk_sz;
installer_flash_buffer(fb, ckh->total_sz + HEADER_SIZE, argc, argv);
if (!last_cmd_succeeded)
return EFI_INVALID_PARAMETER;
payload_size -= data_size;
if (!payload_size)
break;
/* Move the incomplete data from the end to the
beginning of the buffer. */
read_ptr = fb->ckh_data;
read_size = min(payload_size, MAX_DATA_SIZE);
if (data_size < MAX_DATA_SIZE) {
already_read = MAX_DATA_SIZE - data_size;
ret = memcpy_s(read_ptr, MAX_DATA_SIZE, fb->d.data + ckh->total_sz, already_read);
if (EFI_ERROR(ret))
return ret;
read_size -= already_read;
read_ptr += already_read;
if (read_size > file_size) {
ret = read_file(file[*read_flags], file_size, read_ptr);
if (EFI_ERROR(ret))
return ret;
read_size -= file_size;
read_ptr += file_size;
*remaining_data -= file_size;
(*read_flags)++;
}
}
ret = read_file(file[*read_flags], read_size, read_ptr);
if (EFI_ERROR(ret))
return ret;
*remaining_data -= read_size;
fb->skip_ckh.chunk_sz += ckh->chunk_sz;
}
if (payload_size)
return EFI_INVALID_PARAMETER;
return EFI_SUCCESS;
}
/* This function splits a chunk too large to fit into a
dl->max_size buffer into smaller chunks and flash them. */
static EFI_STATUS installer_flash_big_chunk(EFI_FILE *file, UINTN *remaining_data,
flash_buffer_t *fb, UINTN argc, CHAR8 **argv)
{
EFI_STATUS ret = EFI_INVALID_PARAMETER;
UINTN payload_size, read_size, already_read, ckh_blks, data_size;
const UINTN MAX_DATA_SIZE = dl->max_size - offsetof(flash_buffer_t, ckh_data);
const UINTN MAX_BLKS = MAX_DATA_SIZE / fb->sph.blk_sz;
const UINTN HEADER_SIZE = offsetof(flash_buffer_t, d);
struct chunk_header *ckh;
void *read_ptr;
ckh = &fb->d.ckh;
payload_size = ckh->total_sz - sizeof(*ckh);
fb->sph.total_chunks = 2; /* skip and data chunks. */
for (ckh_blks = ckh->chunk_sz; ckh_blks; ckh_blks -= ckh->chunk_sz) {
ckh->chunk_sz = min(MAX_BLKS, ckh_blks);
data_size = ckh->chunk_sz * fb->sph.blk_sz;
ckh->total_sz = sizeof(*ckh) + data_size;
fb->sph.total_blks = fb->skip_ckh.chunk_sz + ckh->chunk_sz;
installer_flash_buffer(fb, ckh->total_sz + HEADER_SIZE, argc, argv);
if (!last_cmd_succeeded)
return EFI_INVALID_PARAMETER;
payload_size -= data_size;
if (!payload_size)
break;
/* Move the incomplete data from the end to the
beginning of the buffer. */
read_ptr = fb->ckh_data;
read_size = min(payload_size, MAX_DATA_SIZE);
if (data_size < MAX_DATA_SIZE) {
already_read = MAX_DATA_SIZE - data_size;
ret = memcpy_s(read_ptr, MAX_DATA_SIZE, fb->d.data + ckh->total_sz, already_read);
if (EFI_ERROR(ret))
return ret;
read_size -= already_read;
read_ptr += already_read;
}
ret = read_file(file, read_size, read_ptr);
if (EFI_ERROR(ret))
return ret;
*remaining_data -= read_size;
fb->skip_ckh.chunk_sz += ckh->chunk_sz;
}
if (payload_size)
return EFI_INVALID_PARAMETER;
return EFI_SUCCESS;
}
/*this function splits a huge sparse file and put together the two image*/
static void installer_split_and_joint_flash(CHAR16 **filename,
UINTN *size, UINTN num, UINTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
flash_buffer_t *fb;
UINTN read_flags = 0;
struct sparse_header sph;
struct chunk_header *ckh;
UINTN read_size, flash_size, already_read = 0, remaining_data = 0;
void *read_ptr;
INTN nb_chunks;
EFI_FILE *file[num];
UINT32 blk_count;
memset_s(file, sizeof(file), 0, sizeof(file));
const UINTN HEADER_SIZE = offsetof(flash_buffer_t, d);
const UINTN MAX_DATA_SIZE = dl->max_size - HEADER_SIZE;
for (UINTN i = 0; i < num; i++) {
remaining_data = remaining_data + size[i];
ret = uefi_open_file(file_io_interface, filename[i], &file[i]);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to open %s file", filename[i]);
return;
}
}
ret = read_file(file[0], sizeof(sph), &sph);
if (EFI_ERROR(ret))
return;
remaining_data -= sizeof(sph);
size[read_flags] -= sizeof(sph);
if (!is_sparse_image((void *) &sph, sizeof(sph))) {
fastboot_fail("sparse file expected");
return;
}
fb = dl->data;
ret = memcpy_s(&fb->sph, sizeof(fb->sph), &sph, sizeof(sph));
if (EFI_ERROR(ret))
return;
/* Sparse skip chunk. */
fb->skip_ckh.chunk_type = CHUNK_TYPE_DONT_CARE;
fb->skip_ckh.total_sz = sizeof(fb->skip_ckh);
nb_chunks = sph.total_chunks;
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
blk_count = 0;
while (nb_chunks > 0 && remaining_data > 0) {
fb->sph.total_chunks = 1;
fb->sph.total_blks = fb->skip_ckh.chunk_sz = blk_count;
if (remaining_data < read_size)
read_size = remaining_data;
/* Read a new piece of the input sparse file. */
ret = read_file(file[read_flags], read_size, read_ptr);
if (EFI_ERROR(ret))
goto exit;
remaining_data -= read_size;
size[read_flags] -= read_size;
/* Process the loaded chunks to build the new header
and the skip chunk. */
flash_size = HEADER_SIZE;
ckh = &fb->d.ckh;
while ((void *)ckh + sizeof(*ckh) <= read_ptr + read_size &&
(void *)ckh + ckh->total_sz <= read_ptr + read_size) {
if (nb_chunks == 0)
goto exit;
flash_size += ckh->total_sz;
fb->sph.total_blks += ckh->chunk_sz;
blk_count += ckh->chunk_sz;
fb->sph.total_chunks++;
nb_chunks--;
ckh = (void *)ckh + ckh->total_sz;
}
/* chunk is too big to fit in the download buffer. */
if (flash_size == HEADER_SIZE) {
if (ckh->chunk_type != CHUNK_TYPE_RAW ||
remaining_data < ckh->total_sz - MAX_DATA_SIZE) {
fastboot_fail("Corrupted sparse file");
goto exit;
}
blk_count += ckh->chunk_sz;
nb_chunks--;
ret = installer_flash_big_chunk_multiple(file, &read_flags,
size[read_flags], &remaining_data, fb, argc, argv);
if (EFI_ERROR(ret))
goto exit;
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
continue;
}
installer_flash_buffer(dl->data, flash_size, argc, argv);
if (!last_cmd_succeeded)
goto exit;
/* Move the incomplete chunk from the end to the
beginning of the buffer. */
if (dl->data + flash_size < read_ptr + read_size) {
already_read = read_ptr + read_size - (void *)ckh;
ret = memcpy_s(fb->d.data, MAX_DATA_SIZE, ckh, already_read);
if (EFI_ERROR(ret))
goto exit;
read_size = MAX_DATA_SIZE - already_read;
read_ptr = fb->d.data + already_read;
} else {
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
}
if ((read_size > size[read_flags]) && (read_flags != (num - 1))) {
ret = read_file(file[read_flags], size[read_flags], read_ptr);
if (EFI_ERROR(ret))
goto exit;
ret = memcpy_s(fb->d.data+already_read, MAX_DATA_SIZE, read_ptr, size[read_flags]);
if (EFI_ERROR(ret))
goto exit;
read_size = read_size - size[read_flags];
remaining_data -= size[read_flags];
read_ptr = fb->d.data+already_read+size[read_flags];
read_flags++;
}
}
exit:
for (UINTN i = 0; i < num; i++)
uefi_call_wrapper(file[i]->Close, 1, file[i]);
}
/* This function splits a huge sparse file into smaller ones and flash
them. */
static void installer_split_and_flash(CHAR16 *filename, UINTN size,
UINTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
flash_buffer_t *fb;
struct sparse_header sph;
struct chunk_header *ckh;
UINTN read_size, flash_size, already_read, remaining_data = size;
void *read_ptr;
INTN nb_chunks;
EFI_FILE *file;
UINT32 blk_count;
const UINTN HEADER_SIZE = offsetof(flash_buffer_t, d);
const UINTN MAX_DATA_SIZE = dl->max_size - HEADER_SIZE;
ret = uefi_open_file(file_io_interface, filename, &file);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to open %s file", filename);
return;
}
ret = read_file(file, sizeof(sph), &sph);
if (EFI_ERROR(ret))
return;
remaining_data -= sizeof(sph);
if (!is_sparse_image((void *) &sph, sizeof(sph))) {
fastboot_fail("sparse file expected");
return;
}
fb = dl->data;
/* New sparse header. */
ret = memcpy_s(&fb->sph, sizeof(fb->sph), &sph, sizeof(sph));
if (EFI_ERROR(ret))
return;
/* Sparse skip chunk. */
fb->skip_ckh.chunk_type = CHUNK_TYPE_DONT_CARE;
fb->skip_ckh.total_sz = sizeof(fb->skip_ckh);
nb_chunks = sph.total_chunks;
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
blk_count = 0;
while (nb_chunks > 0 && remaining_data > 0) {
fb->sph.total_chunks = 1;
fb->sph.total_blks = fb->skip_ckh.chunk_sz = blk_count;
if (remaining_data < read_size)
read_size = remaining_data;
/* Read a new piece of the input sparse file. */
ret = read_file(file, read_size, read_ptr);
if (EFI_ERROR(ret))
goto exit;
remaining_data -= read_size;
/* Process the loaded chunks to build the new header
and the skip chunk. */
flash_size = HEADER_SIZE;
ckh = &fb->d.ckh;
while ((void *)ckh + sizeof(*ckh) <= read_ptr + read_size &&
(void *)ckh + ckh->total_sz <= read_ptr + read_size) {
if (nb_chunks == 0) {
fastboot_fail("Corrupted sparse file: too many chunks");
goto exit;
}
flash_size += ckh->total_sz;
fb->sph.total_blks += ckh->chunk_sz;
blk_count += ckh->chunk_sz;
fb->sph.total_chunks++;
nb_chunks--;
ckh = (void *)ckh + ckh->total_sz;
}
/* chunk is too big to fit in the download buffer. */
if (flash_size == HEADER_SIZE) {
if (ckh->chunk_type != CHUNK_TYPE_RAW ||
remaining_data < ckh->total_sz - MAX_DATA_SIZE) {
fastboot_fail("Corrupted sparse file");
goto exit;
}
blk_count += ckh->chunk_sz;
nb_chunks--;
ret = installer_flash_big_chunk(file, &remaining_data,
fb, argc, argv);
if (EFI_ERROR(ret))
goto exit;
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
continue;
}
installer_flash_buffer(dl->data, flash_size, argc, argv);
if (!last_cmd_succeeded)
goto exit;
/* Move the incomplete chunk from the end to the
beginning of the buffer. */
if (dl->data + flash_size < read_ptr + read_size) {
already_read = read_ptr + read_size - (void *)ckh;
ret = memcpy_s(fb->d.data, MAX_DATA_SIZE, ckh, already_read);
if (EFI_ERROR(ret))
goto exit;
read_size = MAX_DATA_SIZE - already_read;
read_ptr = fb->d.data + already_read;
} else {
read_size = MAX_DATA_SIZE;
read_ptr = fb->d.data;
}
}
exit:
uefi_call_wrapper(file->Close, 1, file);
}
static void installer_flash_cmd(INTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
CHAR16 *filename;
INTN num = argc - 2;
CHAR16 *numname[num];
void *data;
UINTN size;
UINTN numsize[num];
if (argc < 3) {
fastboot_fail("Flash command requires exactly more then 3 arguments");
return;
}
for (int i = 0; i < num; i++)
numname[i] = NULL;
if (argc > 3) {
argc = 2;
for (int i = 0; i < num; i++) {
numname[i] = stra_to_str(argv[i+2]);
if (!numname[i]) {
fastboot_fail("Failed to convert CHAR8 numname to CHAR16");
return;
}
ret = uefi_get_file_size(file_io_interface, numname[i], &numsize[i]);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to get %s file size", numname[i]);
goto exit;
}
}
if (get_current_state() == LOCKED) {
error(L"Installer: Flash %a is prohibited in %a state.", argv[1],
get_current_state_string());
fastboot_fail("Installer: Prohibited command in %a state.",
get_current_state_string());
return;
}
argv[1] = get_target(argv[1]);
if (!argv[1])
goto exit;
ret = find_partition(argv[1]);
switch (ret) {
case EFI_SUCCESS:
do_erase(argc, argv);
if (!last_cmd_succeeded)
goto exit;
break;
case EFI_NOT_FOUND:
break;
default:
inst_perror(ret, "Failed to get partition information");
goto exit;
}
installer_split_and_joint_flash(numname, numsize, num, argc, argv);
} else {
/* The fastboot flash command does not want the file parameter. */
argc--;
filename = stra_to_str(argv[2]);
if (!filename) {
fastboot_fail("Failed to convert CHAR8 filename to CHAR16");
return;
}
if (get_current_state() == LOCKED) {
error(L"Installer: Flash %a is prohibited in %a state.", argv[1],
get_current_state_string());
fastboot_fail("Installer: Prohibited command in %a state.",
get_current_state_string());
return;
}
ret = uefi_get_file_size(file_io_interface, filename, &size);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to get %s file size", filename);
goto exit;
}
argv[1] = get_target(argv[1]);
if (!argv[1])
goto exit;
ret = find_partition(argv[1]);
switch (ret) {
case EFI_SUCCESS:
do_erase(argc, argv);
if (!last_cmd_succeeded)
goto exit;
break;
case EFI_NOT_FOUND:
break;
default:
inst_perror(ret, "Failed to get partition information");
goto exit;
}
if (size > dl->max_size) {
installer_split_and_flash(filename, size, argc, argv);
goto exit;
}
ret = uefi_read_file(file_io_interface, filename, &data, &size);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Unable to read file %s", filename);
goto exit;
}
installer_flash_buffer(data, size, argc, argv);
FreePool(data);
}
exit:
if (num == 1) {
FreePool(filename);
} else {
for (INTN i = 0; i < num; i++)
if (numname[i])
FreePool(numname[i]);
}
}
#define SIZE_OF_FILENAME (label_length + 5)
static CHAR16 *get_format_image_filename(CHAR8 *label)
{
EFI_STATUS ret;
CHAR8 *filename;
CHAR16 *filename16;
UINTN label_length;
if (!strcmp(label, (CHAR8 *)"data"))
label = (CHAR8 *)"userdata";
label_length = strlena(label);
filename = AllocateZeroPool(SIZE_OF_FILENAME);
if (!filename) {
fastboot_fail("Unable to allocate CHAR8 filename buffer");
return NULL;
}
ret = memcpy_s(filename, SIZE_OF_FILENAME, label, label_length);
if (EFI_ERROR(ret)) {
filename16 = NULL;
goto out;
}
ret = memcpy_s(filename + label_length, SIZE_OF_FILENAME - label_length, ".img", 4);
if (EFI_ERROR(ret)) {
filename16 = NULL;
goto out;
}
filename16 = stra_to_str(filename);
if (!filename16)
fastboot_fail("Unable to allocate CHAR16 filename buffer");
out:
FreePool(filename);
return filename16;
}
/* Simulate the fastboot host format command:
1. get a filesystem image from a file;
2. erase the partition;
3. flash the filesystem image; */
static void installer_format(INTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
void *data = NULL;
UINTN size;
CHAR16 *filename;
if (argc != 2) {
fastboot_fail("Format command requires exactly 2 arguments");
return;
}
filename = get_format_image_filename(argv[1]);
if (!filename)
return;
ret = uefi_read_file(file_io_interface, filename, &data, &size);
if (ret == EFI_NOT_FOUND && !StrCmp(L"userdata.img", filename)) {
fastboot_info("userdata.img is missing, cannot format %a", argv[1]);
fastboot_info("Android fs_mgr will manage this");
} else if (EFI_ERROR(ret)) {
inst_perror(ret, "Unable to read file %s", filename);
goto free_filename;
}
argv[1] = get_target(argv[1]);
if (!argv[1])
goto free_data;
do_erase(argc, argv);
if (!last_cmd_succeeded)
goto free_data;
if (data)
installer_flash_buffer(data, size, argc, argv);
free_data:
FreePool(data);
free_filename:
FreePool(filename);
}
static void installer_boot(INTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
VOID *bootimage;
UINTN size;
CHAR16 *filename;
if (argc != 2) {
fastboot_fail("boot command takes one parameter");
return;
}
filename = stra_to_str((CHAR8 *)argv[1]);
if (!filename) {
fastboot_fail("Failed to convert filename to CHAR16");
return;
}
ret = uefi_read_file(file_io_interface, filename, &bootimage, &size);
FreePool(filename);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to read %a file", argv[1]);
return;
}
ret = android_image_start_buffer(g_parent_image, bootimage, NULL,
NORMAL_BOOT, BOOT_STATE_ORANGE, NULL,
NULL, NULL);
if (EFI_ERROR(ret))
inst_perror(ret, "Failed to start %s image", filename);
else
fastboot_okay("");
}
static struct command {
BOOLEAN optional;
char *cmd;
} *commands;
static UINTN command_nb;
static UINTN current_command;
static void free_commands(void)
{
UINTN i;
if (!commands)
return;
for (i = 0; i < command_nb; i++)
if (commands[i].cmd)
FreePool(commands[i].cmd);
FreePool(commands);
commands = NULL;
command_nb = 0;
current_command = 0;
}
static EFI_STATUS create_new_command(struct command *command, char *str)
{
char *cmd = str;
command->optional = FALSE;
if (*str == '[') {
str++;
cmd = (char *)strchr((CHAR8 *)str, ']');
if (!cmd)
return EFI_INVALID_PARAMETER;
*cmd++ = '\0';
while (*str) {
switch (*str++) {
case 'o':
command->optional = TRUE;
break;
default:
return EFI_INVALID_PARAMETER;
}
}
while (*cmd && isspace(*cmd))
cmd++;
if (!*cmd)
return EFI_INVALID_PARAMETER;
}
command->cmd = strdup(cmd);
if (!command->cmd)
return EFI_OUT_OF_RESOURCES;
return EFI_SUCCESS;
}
#define SIZE_OF_NEW_COMMANDS ((command_nb + 1) * sizeof(*new_commands))
static EFI_STATUS store_command(char *command, VOID *context _unused)
{
EFI_STATUS ret;
struct command *new_commands;
new_commands = AllocatePool(SIZE_OF_NEW_COMMANDS);
if (!new_commands) {
free_commands();
return EFI_OUT_OF_RESOURCES;
}
ret = memcpy_s(new_commands, SIZE_OF_NEW_COMMANDS, commands, command_nb * sizeof(*commands));
if (EFI_ERROR(ret)) {
free_commands();
return ret;
}
ret = create_new_command(&new_commands[command_nb], command);
if (EFI_ERROR(ret)) {
free_commands();
return ret;
}
if (commands)
FreePool(commands);
commands = new_commands;
command_nb++;
return EFI_SUCCESS;
}
static char *next_command()
{
if (command_nb == current_command) {
free_commands();
return NULL;
}
return commands[current_command++].cmd;
}
static void batch(INTN argc, CHAR8 **argv)
{
EFI_STATUS ret;
void *data;
UINTN size;
CHAR16 *filename;
if (argc != 2) {
fastboot_fail("Batch command takes one parameter");
return;
}
filename = stra_to_str(argv[1]);
if (!filename) {
fastboot_fail("Failed to convert CHAR8 filename to CHAR16");
return;
}
ret = uefi_read_file(file_io_interface, filename, &data, &size);
if (EFI_ERROR(ret)) {
inst_perror(ret, "Failed to read %s file", filename);
FreePool(filename);
return;
}
FreePool(filename);
ret = parse_text_buffer(data, size, store_command, NULL);
FreePool(data);
if (EFI_ERROR(ret))
inst_perror(ret, "Failed to parse batch file");
else
fastboot_okay("");
}
static CHAR8 *build_default_options()
{
EFI_STATUS ret;
static CHAR8 options[64];
const char cmd_prefix[] = "--batch installer";
const char file_suffix[] = ".cmd";
char *str;
if (*options)
return options;
ret = memcpy_s(options, sizeof(options), cmd_prefix, sizeof(cmd_prefix) - 1);
if (EFI_ERROR(ret))
return NULL;
str = (char *)options + strlen(options);
#ifdef HAL_AUTODETECT
char *device = get_property_device();
if (device) {
*str++ = '_';
while (*device)
*str++ = *device++;
}
#endif
ret = memcpy_s(str, sizeof(file_suffix) - 1, file_suffix, sizeof(file_suffix) - 1);
if (EFI_ERROR(ret))
return NULL;
return options;
}
static void usage(__attribute__((__unused__)) INTN argc,
__attribute__((__unused__)) CHAR8 **argv)
{
Print(L"Usage: installer [OPTIONS | COMMANDS]\n");
Print(L" installer is an EFI application acting like the fastboot command.\n\n");
Print(L" COMMANDS fastboot commands (cf. the fastboot manual page)\n");
Print(L" -i include the device which is loaded from, must be the first option\n");
Print(L" --help, -h print this help and exit\n");
Print(L" --version, -v print Installer version and exit\n");
Print(L" --batch, -b FILE run all the fastboot commands of FILE\n");
Print(L"If no option is provided, the installer assumes '%a'\n", build_default_options());
Print(L"Note: 'update', 'flash-raw' and 'flashall' commands are NOT supported\n");
fastboot_okay("");
}