forked from rauc/rauc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_handler.c
1965 lines (1693 loc) · 53.3 KB
/
update_handler.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
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gunixoutputstream.h>
#include <mtd/ubi-user.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "context.h"
#include "mount.h"
#include "signature.h"
#include "update_handler.h"
#include "emmc.h"
#include "mbr.h"
#include "gpt.h"
#include "utils.h"
#define R_SLOT_HOOK_PRE_INSTALL "slot-pre-install"
#define R_SLOT_HOOK_POST_INSTALL "slot-post-install"
#define R_SLOT_HOOK_INSTALL "slot-install"
#define CLEAR_BLOCK_SIZE 1024
GQuark r_update_error_quark(void)
{
return g_quark_from_static_string("r_update_error_quark");
}
/* the fd will only live as long as the returned output stream */
static GUnixOutputStream* open_slot_device(RaucSlot *slot, int *fd, GError **error)
{
GUnixOutputStream *outstream = NULL;
GError *ierror = NULL;
int fd_out;
g_return_val_if_fail(slot, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
fd_out = g_open(slot->device, O_WRONLY | O_EXCL);
if (fd_out == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Opening output device %s failed: %s", slot->device, strerror(errno));
return NULL;
}
outstream = G_UNIX_OUTPUT_STREAM(g_unix_output_stream_new(fd_out, TRUE));
if (outstream == NULL) {
g_propagate_prefixed_error(error, ierror,
"Failed to open file for writing: ");
return NULL;
}
if (fd != NULL)
*fd = fd_out;
return outstream;
}
#if ENABLE_EMMC_BOOT_SUPPORT == 1
static gboolean clear_slot(RaucSlot *slot, GError **error)
{
GError *ierror = NULL;
static gchar zerobuf[CLEAR_BLOCK_SIZE] = {};
g_autoptr(GOutputStream) outstream = NULL;
gint write_count = 0;
outstream = G_OUTPUT_STREAM(open_slot_device(slot, NULL, &ierror));
if (outstream == NULL) {
g_propagate_error(error, ierror);
return FALSE;
}
while (write_count != -1) {
write_count = g_output_stream_write(outstream, zerobuf, CLEAR_BLOCK_SIZE, NULL,
&ierror);
/*
* G_IO_ERROR_NO_SPACE is expected here, because the block
* device is cleared completely
*/
if (write_count == -1 &&
!g_error_matches(ierror, G_IO_ERROR, G_IO_ERROR_NO_SPACE)) {
g_propagate_prefixed_error(error, ierror,
"failed clearing block device: ");
return FALSE;
}
}
if (!g_output_stream_close(outstream, NULL, &ierror)) {
g_propagate_error(error, ierror);
return FALSE;
}
return TRUE;
}
#endif
/**
* Clear the the memory area defined in dest_partition.
*
* @param device dev path (/dev/mmcblkX)
* @param dest_partition partition to be cleared (start & size) *
* @param error return location for a GError, or NULL
*
* @return True if succeeded, False if failed
*/
static gboolean clear_boot_switch_partition(const gchar *device,
const struct boot_switch_partition *dest_partition,
GError **error)
{
gboolean res = FALSE;
static gchar zerobuf[512] = {};
gint clear_size = sizeof(zerobuf);
guint clear_count = 0;
gint tmp_count = 0;
gint fd;
g_return_val_if_fail(device, FALSE);
g_return_val_if_fail(dest_partition, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fd = g_open(device, O_RDWR);
if (fd == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Opening device failed: %s",
g_strerror(errno));
goto out;
}
if (lseek(fd, dest_partition->start, SEEK_SET) !=
(off_t)dest_partition->start) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Failed to set file to position %"G_GUINT64_FORMAT ": %s",
dest_partition->start, g_strerror(errno));
goto out;
}
while (clear_count < dest_partition->size) {
if ((dest_partition->size - clear_count) < sizeof(zerobuf))
clear_size = dest_partition->size - clear_count;
tmp_count = write(fd, zerobuf, clear_size);
if (tmp_count < 0)
break;
clear_count += tmp_count;
}
if (clear_count != dest_partition->size) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Failed to clear partition: %s",
g_strerror(errno));
goto out;
}
res = TRUE;
out:
if (fd >= 0)
g_close(fd, NULL);
return res;
}
static gboolean ubifs_ioctl(RaucImage *image, int fd, GError **error)
{
int ret;
gint64 size = image->checksum.size;
/* set up ubi volume for image copy */
ret = ioctl(fd, UBI_IOCVOLUP, &size);
if (ret == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"ubi volume update failed: %s", strerror(errno));
return FALSE;
}
return TRUE;
}
static gboolean copy_raw_image(RaucImage *image, GUnixOutputStream *outstream, GError **error)
{
GError *ierror = NULL;
gssize size;
goffset seeksize;
g_autoptr(GFile) srcimagefile = g_file_new_for_path(image->filename);
int out_fd = g_unix_output_stream_get_fd(outstream);
g_autoptr(GInputStream) instream = G_INPUT_STREAM(g_file_read(srcimagefile, NULL, &ierror));
if (instream == NULL) {
g_propagate_prefixed_error(error, ierror,
"Failed to open file for reading: ");
return FALSE;
}
/* Do not close fd automatically to give us the chance to call fsync() on it before closing */
g_unix_output_stream_set_close_fd(outstream, FALSE);
size = g_output_stream_splice(G_OUTPUT_STREAM(outstream), instream,
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
NULL,
&ierror);
if (size == -1) {
g_propagate_prefixed_error(error, ierror,
"Failed splicing data: ");
return FALSE;
}
seeksize = g_seekable_tell(G_SEEKABLE(instream));
if (seeksize != (goffset)image->checksum.size) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED,
"Written size (%"G_GOFFSET_FORMAT ") != image size (%"G_GOFFSET_FORMAT ")", seeksize, image->checksum.size);
return FALSE;
}
if (!g_input_stream_close(instream, NULL, &ierror)) {
g_propagate_error(error, ierror);
return FALSE;
}
/* flush to block device before closing to assure content is written to disk */
if (fsync(out_fd) == -1) {
close(out_fd); /* Silent attempt to close as we failed, anyway */
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, "Syncing content to disk failed: %s", strerror(errno));
return FALSE;
}
if (close(out_fd) == -1) {
g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, "Closing output device failed: %s", strerror(errno));
return FALSE;
}
return TRUE;
}
static gboolean casync_extract(RaucImage *image, gchar *dest, int out_fd, const gchar *seed, const gchar *store, const gchar *tmpdir, GError **error)
{
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(5, g_free);
g_ptr_array_add(args, g_strdup("casync"));
g_ptr_array_add(args, g_strdup("extract"));
if (seed) {
g_ptr_array_add(args, g_strdup("--seed"));
g_ptr_array_add(args, g_strdup(seed));
}
if (store) {
g_ptr_array_add(args, g_strdup("--store"));
g_ptr_array_add(args, g_strdup(store));
}
g_ptr_array_add(args, g_strdup("--seed-output=no"));
g_ptr_array_add(args, g_strdup(image->filename));
g_ptr_array_add(args, g_strdup(out_fd >= 0 ? "-" : dest));
g_ptr_array_add(args, NULL);
launcher = g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_NONE);
if (out_fd >= 0)
g_subprocess_launcher_take_stdout_fd(launcher, out_fd);
if (tmpdir)
g_subprocess_launcher_setenv(launcher, "TMPDIR", tmpdir, TRUE);
sproc = r_subprocess_launcher_spawnv(launcher, args, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start casync extract: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run casync extract: ");
goto out;
}
out:
return res;
}
static RaucSlot *get_active_slot_class_member(gchar *slotclass)
{
RaucSlot *iterslot;
GHashTableIter iter;
g_return_val_if_fail(slotclass, NULL);
g_hash_table_iter_init(&iter, r_context()->config->slots);
while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&iterslot)) {
if (iterslot->state == ST_INACTIVE)
continue;
if (g_strcmp0(iterslot->sclass, slotclass) == 0) {
return iterslot;
}
}
return NULL;
}
static gboolean casync_extract_image(RaucImage *image, gchar *dest, int out_fd, GError **error)
{
GError *ierror = NULL;
gboolean res = FALSE;
RaucSlot *seedslot = NULL;
g_autofree gchar *seed = NULL;
gchar *store = NULL;
gchar *tmpdir = NULL;
gboolean seed_mounted = FALSE;
/* Prepare Seed */
seedslot = get_active_slot_class_member(image->slotclass);
if (!seedslot) {
g_warning("No seed slot available for %s", image->slotclass);
goto extract;
}
if (g_str_has_suffix(image->filename, ".caidx" )) {
/* We need to have the seed slot (bind) mounted to a distinct
* path to allow seeding. E.g. using mount path '/' for the
* rootfs slot seed is inaproppriate as it contains virtual
* file systems, additional mounts, etc. */
if (!seedslot->mount_point) {
g_debug("Mounting %s to use as seed", seedslot->device);
res = r_mount_slot(seedslot, &ierror);
if (!res) {
g_warning("Failed mounting for seeding: %s", ierror->message);
g_clear_error(&ierror);
goto extract;
}
seed_mounted = TRUE;
}
g_debug("Adding as casync directory tree seed: %s", seedslot->mount_point);
seed = g_strdup(seedslot->mount_point);
} else {
GStatBuf seedstat;
/* For the moment do not utilize UBI volumes as seed because they are
* character devices - additional logic is needed to (temporarily) map
* them to UBIBLOCK devices which are suitable for that purpose */
if (g_stat(seedslot->device, &seedstat) < 0 || S_ISCHR(seedstat.st_mode))
goto extract;
g_debug("Adding as casync blob seed: %s", seedslot->device);
seed = g_strdup(seedslot->device);
}
extract:
/* Set store */
store = r_context()->install_info->mounted_bundle->storepath;
g_debug("Using store path: '%s'", store);
/* Set temporary directory */
tmpdir = r_context()->config->tmp_path;
if (tmpdir)
g_debug("Using tmp path: '%s'", tmpdir);
/* Call casync to extract */
res = casync_extract(image, dest, out_fd, seed, store, tmpdir, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
/* Cleanup seed */
if (seed_mounted) {
res = r_umount_slot(seedslot, &ierror);
if (!res) {
g_propagate_prefixed_error(error, ierror, "Failed unmounting seed slot: ");
goto out;
}
}
res = TRUE;
out:
return res;
}
static gboolean copy_raw_image_to_dev(RaucImage *image, RaucSlot *slot, GError **error)
{
g_autoptr(GUnixOutputStream) outstream = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
/* open */
g_message("opening slot device %s", slot->device);
outstream = open_slot_device(slot, NULL, &ierror);
if (outstream == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
goto out;
}
/* copy */
g_message("writing data to device %s", slot->device);
res = copy_raw_image(image, outstream, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
out:
return res;
}
static gboolean write_image_to_dev(RaucImage *image, RaucSlot *slot, GError **error)
{
GError *ierror = NULL;
gboolean res = FALSE;
/* Handle casync index file */
if (g_str_has_suffix(image->filename, ".caibx")) {
g_message("Extracting %s to %s", image->filename, slot->device);
/* Extract caibx to device */
res = casync_extract_image(image, slot->device, -1, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
} else {
res = copy_raw_image_to_dev(image, slot, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
}
out:
return res;
}
static gboolean ubifs_format_slot(RaucSlot *dest_slot, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(3, g_free);
g_ptr_array_add(args, g_strdup("mkfs.ubifs"));
g_ptr_array_add(args, g_strdup("-y"));
g_ptr_array_add(args, g_strdup(dest_slot->device));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start mkfs.ubifs: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run mkfs.ubifs: ");
goto out;
}
out:
return res;
}
static gboolean ext4_resize_slot(RaucSlot *dest_slot, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(3, g_free);
g_ptr_array_add(args, g_strdup("resize2fs"));
g_ptr_array_add(args, g_strdup(dest_slot->device));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"Failed to start resize2fs: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"Failed to run resize2fs: ");
goto out;
}
out:
return res;
}
static gboolean ext4_format_slot(RaucSlot *dest_slot, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(4, g_free);
g_ptr_array_add(args, g_strdup("mkfs.ext4"));
g_ptr_array_add(args, g_strdup("-F"));
if (strlen(dest_slot->name) <= 16) {
g_ptr_array_add(args, g_strdup("-L"));
g_ptr_array_add(args, g_strdup(dest_slot->name));
}
g_ptr_array_add(args, g_strdup("-I256"));
g_ptr_array_add(args, g_strdup(dest_slot->device));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start mkfs.ext4: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run mkfs.ext4: ");
goto out;
}
out:
return res;
}
/**
* Create valid / safe vfat label names.
*
* mkfs.vfat label requirements:
*
* | mkfs.fat: Warning: lowercase labels might not work properly on some systems
* | mkfs.vfat: Labels with characters *?.,;:/\|+=<>[]" are not allowed
* | mkfs.vfat: Label can be no longer than 11 characters
*
* This cuts input at 11 characters, makes all characters uppercase and
* replaces all invalid characters by '_' (underscore)
*
* @param name input name to create vfat label from
* @return newly-allocated string to be used as "-n" argument for mkfs.vfat
*/
static gchar* vfat_label_generator(const gchar *name)
{
gchar *label_name;
const gchar *invalid_chars = "*?.,;:/\\|+=<>[]\"";
g_return_val_if_fail(name, NULL);
/* limit label length to 11 characters */
if (strlen(name) > 11)
label_name = g_strndup(name, 11);
else
label_name = g_strdup(name);
for (gchar *c = label_name; *c != '\0'; c++) {
/* make chars uppercase */
if (g_ascii_islower(*c))
*c = g_ascii_toupper(*c);
/* replace invalid chars */
for (size_t i = 0; i < strlen(invalid_chars); i++) {
g_message("r: %c", invalid_chars[i]);
if (*c == invalid_chars[i]) {
*c = '_';
break;
}
}
}
return label_name;
}
static gboolean vfat_format_slot(RaucSlot *dest_slot, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(4, g_free);
g_ptr_array_add(args, g_strdup("mkfs.vfat"));
g_ptr_array_add(args, g_strdup("-n"));
g_ptr_array_add(args, vfat_label_generator(dest_slot->name));
g_ptr_array_add(args, g_strdup(dest_slot->device));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start mkfs.vfat: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run mkfs.vfat: ");
goto out;
}
out:
return res;
}
static gboolean nor_write_slot(const gchar *image, const gchar *device, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(5, g_free);
g_ptr_array_add(args, g_strdup("flashcp"));
g_ptr_array_add(args, g_strdup(image));
g_ptr_array_add(args, g_strdup(device));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start flashcp: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run flashcp: ");
goto out;
}
out:
return res;
}
static gboolean flash_format_slot(const gchar *device, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(5, g_free);
g_ptr_array_add(args, g_strdup("flash_erase"));
g_ptr_array_add(args, g_strdup("--quiet"));
g_ptr_array_add(args, g_strdup(device));
g_ptr_array_add(args, g_strdup("0"));
g_ptr_array_add(args, g_strdup("0"));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start flash_erase: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run flash_erase: ");
goto out;
}
out:
return res;
}
static gboolean nand_write_slot(const gchar *image, const gchar *device, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(5, g_free);
g_ptr_array_add(args, g_strdup("nandwrite"));
g_ptr_array_add(args, g_strdup("--pad"));
g_ptr_array_add(args, g_strdup("--quiet"));
g_ptr_array_add(args, g_strdup(device));
g_ptr_array_add(args, g_strdup(image));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start nandwrite: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run nandwrite: ");
goto out;
}
out:
return res;
}
static gboolean untar_image(RaucImage *image, gchar *dest, GError **error)
{
g_autoptr(GSubprocess) sproc = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
g_autoptr(GPtrArray) args = g_ptr_array_new_full(5, g_free);
g_ptr_array_add(args, g_strdup("tar"));
g_ptr_array_add(args, g_strdup("xf"));
g_ptr_array_add(args, g_strdup(image->filename));
g_ptr_array_add(args, g_strdup("-C"));
g_ptr_array_add(args, g_strdup(dest));
g_ptr_array_add(args, g_strdup("--numeric-owner"));
g_ptr_array_add(args, NULL);
sproc = r_subprocess_newv(args, G_SUBPROCESS_FLAGS_NONE, &ierror);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start tar extract: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run tar extract: ");
goto out;
}
out:
return res;
}
static gboolean unpack_archive(RaucImage *image, gchar *dest, GError **error)
{
if (g_str_has_suffix(image->filename, ".caidx" ))
return casync_extract_image(image, dest, -1, error);
else if (g_str_has_suffix(image->filename, ".catar" ))
return casync_extract_image(image, dest, -1, error);
else
return untar_image(image, dest, error);
}
/**
* Executes the per-slot hook script with extra environment variables.
*
* @param hook_name file name of the hook script
* @param hook_cmd first argument to the hook script
* @param image image to be installed (optional)
* @param slot target slot
* @param error return location for a GError, or NULL
*
* @return TRUE on success, FALSE if an error occurred
*/
static gboolean run_slot_hook_extra_env(const gchar *hook_name, const gchar *hook_cmd, RaucImage *image, RaucSlot *slot, GHashTable *variables, GError **error)
{
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) sproc = NULL;
g_autofree gchar* image_size = NULL;
GError *ierror = NULL;
gboolean res = FALSE;
RaucBundle *bundle;
g_assert_nonnull(slot);
g_assert_nonnull(slot->name);
g_assert_nonnull(slot->sclass);
g_message("Running slot hook %s for %s", hook_cmd, slot->name);
launcher = g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_NONE);
g_subprocess_launcher_setenv(launcher, "RAUC_SYSTEM_COMPATIBLE", r_context()->config->system_compatible ?: "", TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SYSTEM_VARIANT", r_context()->config->system_variant ?: "", TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_NAME", slot->name, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_STATE", r_slot_slotstate_to_str(slot->state), TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_CLASS", slot->sclass, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_TYPE", slot->type, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_DEVICE", slot->device, TRUE);
if (slot->parent) {
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_BOOTNAME", slot->parent->bootname ?: "", TRUE);
} else {
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_BOOTNAME", slot->bootname ?: "", TRUE);
}
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_PARENT", slot->parent ? slot->parent->name : "", TRUE);
if (slot->mount_point) {
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_MOUNT_POINT", slot->mount_point, TRUE);
} else if (slot->ext_mount_point) {
g_subprocess_launcher_setenv(launcher, "RAUC_SLOT_MOUNT_POINT", slot->ext_mount_point, TRUE);
}
if (image) {
image_size = g_strdup_printf("%" G_GOFFSET_FORMAT, image->checksum.size);
g_subprocess_launcher_setenv(launcher, "RAUC_IMAGE_NAME", image->filename, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_IMAGE_SIZE", image_size, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_IMAGE_DIGEST", image->checksum.digest, TRUE);
g_subprocess_launcher_setenv(launcher, "RAUC_IMAGE_CLASS", image->slotclass, TRUE);
}
g_subprocess_launcher_setenv(launcher, "RAUC_MOUNT_PREFIX", r_context()->config->mount_prefix, TRUE);
bundle = r_context()->install_info->mounted_bundle;
if (bundle) {
gchar **hashes = NULL;
gchar *string = NULL;
hashes = get_pubkey_hashes(bundle->verified_chain);
string = g_strjoinv(" ", hashes);
g_strfreev(hashes);
g_subprocess_launcher_setenv(launcher, "RAUC_BUNDLE_SPKI_HASHES", string, TRUE);
g_free(string);
g_subprocess_launcher_setenv(launcher, "RAUC_BUNDLE_MOUNT_POINT", bundle->mount_point, TRUE);
}
if (variables) {
GHashTableIter iter;
gchar *key = NULL;
gchar *value = NULL;
/* copy the variables from the hashtable and add them to the
subprocess environment */
g_hash_table_iter_init(&iter, variables);
while (g_hash_table_iter_next(&iter, (gpointer*) &key, (gpointer*) &value)) {
g_subprocess_launcher_setenv(launcher, g_strdup(key), g_strdup(value), TRUE);
}
}
sproc = g_subprocess_launcher_spawn(
launcher, &ierror,
hook_name,
hook_cmd,
NULL);
if (sproc == NULL) {
g_propagate_prefixed_error(
error,
ierror,
"failed to start slot hook: ");
goto out;
}
res = g_subprocess_wait_check(sproc, NULL, &ierror);
if (!res) {
g_propagate_prefixed_error(
error,
ierror,
"failed to run slot hook: ");
goto out;
}
out:
return res;
}
/**
* Executes the per-slot hook script without setting extra environment variables.
*
* @param hook_name file name of the hook script
* @param hook_cmd first argument to the hook script
* @param image image to be installed (optional)
* @param slot target slot
* @param error return location for a GError, or NULL
*
* @return TRUE on success, FALSE if an error occurred
*/
static gboolean run_slot_hook(const gchar *hook_name, const gchar *hook_cmd, RaucImage *image, RaucSlot *slot, GError **error)
{
return run_slot_hook_extra_env(hook_name, hook_cmd, image, slot, NULL, error);
}
static gboolean mount_and_run_slot_hook(const gchar *hook_name, const gchar *hook_cmd, RaucImage *image, RaucSlot *slot, GError **error)
{
GError *ierror = NULL;
gboolean res = FALSE;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
g_assert_nonnull(hook_name);
g_assert_nonnull(hook_cmd);
/* mount slot */
g_message("Mounting slot %s", slot->device);
res = r_mount_slot(slot, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
/* run slot install hook */
g_message("Running slot '%s' hook for %s", hook_cmd, slot->name);
res = run_slot_hook(hook_name, hook_cmd, image, slot, &ierror);
if (!res) {
g_propagate_error(error, ierror);
}
/* finally umount slot */
g_message("Unmounting slot %s", slot->device);
ierror = NULL; /* any previous error was propagated already */
if (!r_umount_slot(slot, &ierror)) {
res = FALSE;
if (error && *error) {
/* the slot hook error is more relevant here */
g_warning("Ignoring umount error after slot hook error: %s", ierror->message);
g_clear_error(&ierror);
} else {
g_propagate_error(error, ierror);
}
}
out:
return res;
}
static gboolean img_to_ubivol_handler(RaucImage *image, RaucSlot *dest_slot, const gchar *hook_name, GError **error)
{
g_autoptr(GUnixOutputStream) outstream = NULL;
GError *ierror = NULL;
int out_fd;
gboolean res = FALSE;
/* run slot pre install hook if enabled */
if (hook_name && image->hooks.pre_install) {
res = run_slot_hook(hook_name, R_SLOT_HOOK_PRE_INSTALL, image, dest_slot, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
}
/* open */
g_message("opening slot device %s", dest_slot->device);
outstream = open_slot_device(dest_slot, &out_fd, &ierror);
if (outstream == NULL) {
res = FALSE;
g_propagate_error(error, ierror);
goto out;
}
/* ubifs ioctl */
res = ubifs_ioctl(image, out_fd, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
/* Handle casync index file */
if (g_str_has_suffix(image->filename, ".caibx")) {
g_message("Extracting %s to %s", image->filename, dest_slot->device);
res = casync_extract_image(image, NULL, out_fd, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
} else {
/* copy */
res = copy_raw_image(image, outstream, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}
}