-
Notifications
You must be signed in to change notification settings - Fork 13
/
mkfs.ubifs.c
3036 lines (2648 loc) · 77 KB
/
mkfs.ubifs.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) 2008 Nokia Corporation.
* Copyright (C) 2008 University of Szeged, Hungary
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: Adrian Hunter
* Artem Bityutskiy
* Zoltan Sogor
*/
#define _XOPEN_SOURCE 500 /* For realpath() */
#include "mkfs.ubifs.h"
#include <crc32.h>
#include "common.h"
#include <sys/types.h>
#ifndef WITHOUT_XATTR
#include <sys/xattr.h>
#endif
#ifdef WITH_SELINUX
#include <selinux/selinux.h>
#include <selinux/label.h>
#endif
#ifndef WITHOUT_ZSTD
#include <zstd.h>
#endif
#include "crypto.h"
#include "fscrypt.h"
/* Size (prime number) of hash table for link counting */
#define HASH_TABLE_SIZE 10099
/* The node buffer must allow for worst case compression */
#define NODE_BUFFER_SIZE (UBIFS_DATA_NODE_SZ + \
UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR)
/* Default time granularity in nanoseconds */
#define DEFAULT_TIME_GRAN 1000000000
#ifdef WITH_SELINUX
#define XATTR_NAME_SELINUX "security.selinux"
static struct selabel_handle *sehnd;
static char *secontext;
#endif
/**
* struct idx_entry - index entry.
* @next: next index entry (NULL at end of list)
* @prev: previous index entry (NULL at beginning of list)
* @key: key
* @name: directory entry name used for sorting colliding keys by name
* @lnum: LEB number
* @offs: offset
* @len: length
* @hash: hash of the node
*
* The index is recorded as a linked list which is sorted and used to create
* the bottom level of the on-flash index tree. The remaining levels of the
* index tree are each built from the level below.
*/
struct idx_entry {
struct idx_entry *next;
struct idx_entry *prev;
union ubifs_key key;
char *name;
int name_len;
int lnum;
int offs;
int len;
uint8_t hash[UBIFS_MAX_HASH_LEN];
};
/**
* struct inum_mapping - inode number mapping for link counting.
* @next: next inum_mapping (NULL at end of list)
* @prev: previous inum_mapping (NULL at beginning of list)
* @dev: source device on which the source inode number resides
* @inum: source inode number of the file
* @use_inum: target inode number of the file
* @use_nlink: number of links
* @path_name: a path name of the file
* @st: struct stat object containing inode attributes which have to be used
* when the inode is being created (actually only UID, GID, access
* mode, major and minor device numbers)
*
* If a file has more than one hard link, then the number of hard links that
* exist in the source directory hierarchy must be counted to exclude the
* possibility that the file is linked from outside the source directory
* hierarchy.
*
* The inum_mappings are stored in a hash_table of linked lists.
*/
struct inum_mapping {
struct inum_mapping *next;
struct inum_mapping *prev;
dev_t dev;
ino_t inum;
ino_t use_inum;
unsigned int use_nlink;
char *path_name;
struct stat st;
};
/*
* Because we copy functions from the kernel, we use a subset of the UBIFS
* file-system description object struct ubifs_info.
*/
struct ubifs_info info_;
static struct ubifs_info *c = &info_;
static libubi_t ubi;
/* Debug levels are: 0 (none), 1 (statistics), 2 (files) ,3 (more details) */
int debug_level;
int verbose;
int yes;
static char *root;
static int root_len;
static struct fscrypt_context *root_fctx;
static struct stat root_st;
static char *output;
static int out_fd;
static int out_ubi;
static int squash_owner;
static int do_create_inum_attr;
static char *context;
static int context_len;
static struct stat context_st;
/* The 'head' (position) which nodes are written */
static int head_lnum;
static int head_offs;
static int head_flags;
/* The index list */
static struct idx_entry *idx_list_first;
static struct idx_entry *idx_list_last;
static size_t idx_cnt;
/* Global buffers */
static void *leb_buf;
static void *node_buf;
static void *block_buf;
/* Hash table for inode link counting */
static struct inum_mapping **hash_table;
/* Inode creation sequence number */
static unsigned long long creat_sqnum;
static const char *optstring = "d:r:m:o:D:yh?vVe:c:g:f:Fp:k:x:X:j:R:l:j:UQqaK:b:P:C:";
enum {
HASH_ALGO_OPTION = CHAR_MAX + 1,
AUTH_KEY_OPTION,
AUTH_CERT_OPTION,
};
static const struct option longopts[] = {
{"root", 1, NULL, 'r'},
{"min-io-size", 1, NULL, 'm'},
{"leb-size", 1, NULL, 'e'},
{"max-leb-cnt", 1, NULL, 'c'},
{"output", 1, NULL, 'o'},
{"devtable", 1, NULL, 'D'},
{"yes", 0, NULL, 'y'},
{"help", 0, NULL, 'h'},
{"verbose", 0, NULL, 'v'},
{"version", 0, NULL, 'V'},
{"debug-level", 1, NULL, 'g'},
{"jrn-size", 1, NULL, 'j'},
{"reserved", 1, NULL, 'R'},
{"compr", 1, NULL, 'x'},
{"favor-percent", 1, NULL, 'X'},
{"fanout", 1, NULL, 'f'},
{"space-fixup", 0, NULL, 'F'},
{"keyhash", 1, NULL, 'k'},
{"log-lebs", 1, NULL, 'l'},
{"orph-lebs", 1, NULL, 'p'},
{"squash-uids" , 0, NULL, 'U'},
{"set-inode-attr", 0, NULL, 'a'},
{"selinux", 1, NULL, 's'},
{"key", 1, NULL, 'K'},
{"key-descriptor", 1, NULL, 'b'},
{"padding", 1, NULL, 'P'},
{"cipher", 1, NULL, 'C'},
{"hash-algo", 1, NULL, HASH_ALGO_OPTION},
{"auth-key", 1, NULL, AUTH_KEY_OPTION},
{"auth-cert", 1, NULL, AUTH_CERT_OPTION},
{NULL, 0, NULL, 0}
};
static const char *helptext =
"Usage: mkfs.ubifs [OPTIONS] target\n"
"Make a UBIFS file system image from an existing directory tree\n\n"
"Examples:\n"
"Build file system from directory /opt/img, writting the result in the ubifs.img file\n"
"\tmkfs.ubifs -m 512 -e 128KiB -c 100 -r /opt/img ubifs.img\n"
"The same, but writting directly to an UBI volume\n"
"\tmkfs.ubifs -r /opt/img /dev/ubi0_0\n"
"Creating an empty UBIFS filesystem on an UBI volume\n"
"\tmkfs.ubifs /dev/ubi0_0\n\n"
"Options:\n"
"-r, -d, --root=DIR build file system from directory DIR\n"
"-m, --min-io-size=SIZE minimum I/O unit size\n"
"-e, --leb-size=SIZE logical erase block size\n"
"-c, --max-leb-cnt=COUNT maximum logical erase block count\n"
"-o, --output=FILE output to FILE\n"
"-j, --jrn-size=SIZE journal size\n"
"-R, --reserved=SIZE how much space should be reserved for the super-user\n"
"-x, --compr=TYPE compression type - \"lzo\", \"favor_lzo\", \"zlib\"\n"
" \"zstd\" or \"none\" (default: \"lzo\")\n"
"-X, --favor-percent may only be used with favor LZO compression and defines\n"
" how many percent better zlib should compress to make\n"
" mkfs.ubifs use zlib instead of LZO (default 20%)\n"
"-f, --fanout=NUM fanout NUM (default: 8)\n"
"-F, --space-fixup file-system free space has to be fixed up on first mount\n"
" (requires kernel version 3.0 or greater)\n"
"-k, --keyhash=TYPE key hash type - \"r5\" or \"test\" (default: \"r5\")\n"
"-p, --orph-lebs=COUNT count of erase blocks for orphans (default: 1)\n"
"-D, --devtable=FILE use device table FILE\n"
"-U, --squash-uids squash owners making all files owned by root\n"
"-l, --log-lebs=COUNT count of erase blocks for the log (used only for\n"
" debugging)\n"
"-y, --yes assume the answer is \"yes\" for all questions\n"
"-v, --verbose verbose operation\n"
"-V, --version display version information\n"
"-g, --debug=LEVEL display debug information (0 - none, 1 - statistics,\n"
" 2 - files, 3 - more details)\n"
"-a, --set-inum-attr create user.image-inode-number extended attribute on files\n"
" added to the image. The attribute will contain the inode\n"
" number the file has in the generated image.\n"
"-s, --selinux=FILE Selinux context file\n"
"-K, --key=FILE load an encryption key from a specified file.\n"
"-b, --key-descriptor=HEX specify the key descriptor as a hex string.\n"
"-P, --padding=NUM specify padding policy for encrypting filenames\n"
" (default = 4).\n"
"-C, --cipher=NAME Specify cipher to use for file level encryption\n"
" (default is \"AES-256-XTS\").\n"
" --hash-algo=NAME hash algorithm to use for signed images\n"
" (Valid options include sha1, sha256, sha512)\n"
" --auth-key=FILE filename or PKCS #11 uri containing the authentication key\n"
" for signing\n"
" --auth-cert=FILE Authentication certificate filename for signing. Unused\n"
" when certificate is provided via PKCS #11\n"
"-h, --help display this help text\n\n"
"Note, SIZE is specified in bytes, but it may also be specified in Kilobytes,\n"
"Megabytes, and Gigabytes if a KiB, MiB, or GiB suffix is used.\n\n"
"If you specify \"lzo\" or \"zlib\" compressors, mkfs.ubifs will use this compressor\n"
"for all data. The \"none\" disables any data compression. The \"favor_lzo\" is not\n"
"really a separate compressor. It is just a method of combining \"lzo\" and \"zlib\"\n"
"compressors. Namely, mkfs.ubifs tries to compress data with both \"lzo\" and \"zlib\"\n"
"compressors, then it compares which compressor is better. If \"zlib\" compresses 20\n"
"or more percent better than \"lzo\", mkfs.ubifs chooses \"zlib\", otherwise it chooses\n"
"\"lzo\". The \"--favor-percent\" may specify arbitrary threshold instead of the\n"
"default 20%.\n\n"
"The -F parameter is used to set the \"fix up free space\" flag in the superblock,\n"
"which forces UBIFS to \"fixup\" all the free space which it is going to use. This\n"
"option is useful to work-around the problem of double free space programming: if the\n"
"flasher program which flashes the UBI image is unable to skip NAND pages containing\n"
"only 0xFF bytes, the effect is that some NAND pages are written to twice - first time\n"
"when flashing the image and the second time when UBIFS is mounted and writes useful\n"
"data there. A proper UBI-aware flasher should skip such NAND pages, though. Note, this\n"
"flag may make the first mount very slow, because the \"free space fixup\" procedure\n"
"takes time. This feature is supported by the Linux kernel starting from version 3.0.\n"
"\n"
"mkfs.ubifs supports building signed images. For this the \"--hash-algo\",\n"
"\"--auth-key\" and \"--auth-cert\" options have to be specified.\n";
static inline uint8_t *ubifs_branch_hash(struct ubifs_branch *br)
{
return (void *)br + sizeof(*br) + c->key_len;
}
/**
* make_path - make a path name from a directory and a name.
* @dir: directory path name
* @name: name
*/
static char *make_path(const char *dir, const char *name)
{
char *s;
xasprintf(&s, "%s%s%s",
dir, dir[strlen(dir) - 1] == '/' ? "" : "/", name);
return s;
}
/**
* is_contained - determine if a file is beneath a directory.
* @file: file path name
* @dir: directory path name
*
* This function returns %1 if @file is accessible from the @dir directory and
* %0 otherwise. In case of error, returns %-1.
*/
static int is_contained(const char *file, const char *dir)
{
char *real_file = NULL;
char *real_dir = NULL;
char *file_base, *copy;
int ret = -1;
/* Make a copy of the file path because 'dirname()' can modify it */
copy = strdup(file);
if (!copy)
return -1;
file_base = dirname(copy);
/* Turn the paths into the canonical form */
real_file = xmalloc(PATH_MAX);
real_dir = xmalloc(PATH_MAX);
if (!realpath(file_base, real_file)) {
perror("Could not canonicalize file path");
goto out_free;
}
if (!realpath(dir, real_dir)) {
perror("Could not canonicalize directory");
goto out_free;
}
ret = !!strstr(real_file, real_dir);
out_free:
free(copy);
free(real_file);
free(real_dir);
return ret;
}
/**
* calc_min_log_lebs - calculate the minimum number of log LEBs needed.
* @max_bud_bytes: journal size (buds only)
*/
static int calc_min_log_lebs(unsigned long long max_bud_bytes)
{
int buds, log_lebs;
unsigned long long log_size;
buds = (max_bud_bytes + c->leb_size - 1) / c->leb_size;
log_size = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
log_size *= buds;
log_size += ALIGN(UBIFS_CS_NODE_SZ +
UBIFS_REF_NODE_SZ * (c->jhead_cnt + 2),
c->min_io_size);
log_lebs = (log_size + c->leb_size - 1) / c->leb_size;
log_lebs += 1;
return log_lebs;
}
/**
* add_space_overhead - add UBIFS overhead.
* @size: flash space which should be visible to the user
*
* UBIFS has overhead, and if we need to reserve @size bytes for the user data,
* we have to reserve more flash space, to compensate the overhead. This
* function calculates and returns the amount of physical flash space which
* should be reserved to provide @size bytes for the user.
*/
static long long add_space_overhead(long long size)
{
int divisor, factor, f, max_idx_node_sz;
/*
* Do the opposite to what the 'ubifs_reported_space()' kernel UBIFS
* function does.
*/
max_idx_node_sz = ubifs_idx_node_sz(c, c->fanout);
f = c->fanout > 3 ? c->fanout >> 1 : 2;
divisor = UBIFS_BLOCK_SIZE;
factor = UBIFS_MAX_DATA_NODE_SZ;
factor += (max_idx_node_sz * 3) / (f - 1);
size *= factor;
return size / divisor;
}
static int validate_options(void)
{
int tmp;
if (!output)
return err_msg("no output file or UBI volume specified");
if (root) {
tmp = is_contained(output, root);
if (tmp < 0)
return err_msg("failed to perform output file root check");
else if (tmp)
return err_msg("output file cannot be in the UBIFS root "
"directory");
}
if (!is_power_of_2(c->min_io_size))
return err_msg("min. I/O unit size should be power of 2");
if (c->leb_size < c->min_io_size)
return err_msg("min. I/O unit cannot be larger than LEB size");
if (c->leb_size < UBIFS_MIN_LEB_SZ)
return err_msg("too small LEB size %d, minimum is %d",
c->leb_size, UBIFS_MIN_LEB_SZ);
if (c->leb_size % c->min_io_size)
return err_msg("LEB should be multiple of min. I/O units");
if (c->leb_size % 8)
return err_msg("LEB size has to be multiple of 8");
if (c->leb_size > UBIFS_MAX_LEB_SZ)
return err_msg("too large LEB size %d, maximum is %d",
c->leb_size, UBIFS_MAX_LEB_SZ);
if (c->max_leb_cnt < UBIFS_MIN_LEB_CNT)
return err_msg("too low max. count of LEBs, minimum is %d",
UBIFS_MIN_LEB_CNT);
if (c->fanout < UBIFS_MIN_FANOUT)
return err_msg("too low fanout, minimum is %d",
UBIFS_MIN_FANOUT);
tmp = c->leb_size - UBIFS_IDX_NODE_SZ;
tmp /= UBIFS_BRANCH_SZ + UBIFS_MAX_KEY_LEN;
if (c->fanout > tmp)
return err_msg("too high fanout, maximum is %d", tmp);
if (c->log_lebs < UBIFS_MIN_LOG_LEBS)
return err_msg("too few log LEBs, minimum is %d",
UBIFS_MIN_LOG_LEBS);
if (c->log_lebs >= c->max_leb_cnt - UBIFS_MIN_LEB_CNT)
return err_msg("too many log LEBs, maximum is %d",
c->max_leb_cnt - UBIFS_MIN_LEB_CNT);
if (c->orph_lebs < UBIFS_MIN_ORPH_LEBS)
return err_msg("too few orphan LEBs, minimum is %d",
UBIFS_MIN_ORPH_LEBS);
if (c->orph_lebs >= c->max_leb_cnt - UBIFS_MIN_LEB_CNT)
return err_msg("too many orphan LEBs, maximum is %d",
c->max_leb_cnt - UBIFS_MIN_LEB_CNT);
tmp = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs;
tmp += c->orph_lebs + 4;
if (tmp > c->max_leb_cnt)
return err_msg("too low max. count of LEBs, expected at "
"least %d", tmp);
tmp = calc_min_log_lebs(c->max_bud_bytes);
if (c->log_lebs < calc_min_log_lebs(c->max_bud_bytes))
return err_msg("too few log LEBs, expected at least %d", tmp);
if (c->rp_size >= ((long long)c->leb_size * c->max_leb_cnt) / 2)
return err_msg("too much reserved space %lld", c->rp_size);
return 0;
}
/**
* get_multiplier - convert size specifier to an integer multiplier.
* @str: the size specifier string
*
* This function parses the @str size specifier, which may be one of
* 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive
* size multiplier in case of success and %-1 in case of failure.
*/
static int get_multiplier(const char *str)
{
if (!str)
return 1;
/* Remove spaces before the specifier */
while (*str == ' ' || *str == '\t')
str += 1;
if (!strcmp(str, "KiB"))
return 1024;
if (!strcmp(str, "MiB"))
return 1024 * 1024;
if (!strcmp(str, "GiB"))
return 1024 * 1024 * 1024;
return -1;
}
/**
* get_bytes - convert a string containing amount of bytes into an
* integer.
* @str: string to convert
*
* This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' size
* specifiers. Returns positive amount of bytes in case of success and %-1 in
* case of failure.
*/
static long long get_bytes(const char *str)
{
char *endp;
long long bytes = strtoull(str, &endp, 0);
if (endp == str || bytes < 0)
return err_msg("incorrect amount of bytes: \"%s\"", str);
if (*endp != '\0') {
int mult = get_multiplier(endp);
if (mult == -1)
return err_msg("bad size specifier: \"%s\" - "
"should be 'KiB', 'MiB' or 'GiB'", endp);
bytes *= mult;
}
return bytes;
}
/**
* open_ubi - open the UBI volume.
* @node: name of the UBI volume character device to fetch information about
*
* Returns %0 in case of success and %-1 in case of failure
*/
static int open_ubi(const char *node)
{
struct stat st;
if (stat(node, &st) || !S_ISCHR(st.st_mode))
return -1;
ubi = libubi_open();
if (!ubi)
return -1;
if (ubi_get_vol_info(ubi, node, &c->vi))
return -1;
if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di))
return -1;
return 0;
}
static void select_default_compr(void)
{
if (c->encrypted) {
c->default_compr = UBIFS_COMPR_NONE;
return;
}
#ifdef WITHOUT_LZO
c->default_compr = UBIFS_COMPR_ZLIB;
#else
c->default_compr = UBIFS_COMPR_LZO;
#endif
}
static int get_options(int argc, char**argv)
{
int opt, i, fscrypt_flags = FS_POLICY_FLAGS_PAD_4;
const char *key_file = NULL, *key_desc = NULL;
const char *tbl_file = NULL;
struct stat st;
char *endp;
#ifdef WITH_CRYPTO
const char *cipher_name;
#endif
c->fanout = 8;
c->orph_lebs = 1;
c->key_hash = key_r5_hash;
c->key_len = UBIFS_SK_LEN;
c->favor_percent = 20;
c->lsave_cnt = 256;
c->leb_size = -1;
c->min_io_size = -1;
c->max_leb_cnt = -1;
c->max_bud_bytes = -1;
c->log_lebs = -1;
c->double_hash = 0;
c->encrypted = 0;
c->default_compr = -1;
while (1) {
opt = getopt_long(argc, argv, optstring, longopts, &i);
if (opt == -1)
break;
switch (opt) {
case 'r':
case 'd':
root_len = strlen(optarg);
root = xmalloc(root_len + 2);
/*
* The further code expects '/' at the end of the root
* UBIFS directory on the host.
*/
memcpy(root, optarg, root_len);
if (root[root_len - 1] != '/')
root[root_len++] = '/';
root[root_len] = 0;
/* Make sure the root directory exists */
if (stat(root, &st))
return sys_err_msg("bad root directory '%s'",
root);
break;
case 'm':
c->min_io_size = get_bytes(optarg);
if (c->min_io_size <= 0)
return err_msg("bad min. I/O size");
break;
case 'e':
c->leb_size = get_bytes(optarg);
if (c->leb_size <= 0)
return err_msg("bad LEB size");
break;
case 'c':
c->max_leb_cnt = get_bytes(optarg);
if (c->max_leb_cnt <= 0)
return err_msg("bad maximum LEB count");
break;
case 'o':
output = xstrdup(optarg);
break;
case 'D':
tbl_file = optarg;
if (stat(tbl_file, &st) < 0)
return sys_err_msg("bad device table file '%s'",
tbl_file);
break;
case 'y':
yes = 1;
break;
case 'h':
printf("%s", helptext);
exit(EXIT_SUCCESS);
case '?':
printf("%s", helptext);
#ifdef WITH_CRYPTO
printf("\n\nSupported ciphers:\n");
list_ciphers(stdout);
#endif
exit(-1);
case 'v':
verbose = 1;
break;
case 'V':
common_print_version();
exit(EXIT_SUCCESS);
case 'g':
debug_level = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
debug_level < 0 || debug_level > 3)
return err_msg("bad debugging level '%s'",
optarg);
break;
case 'f':
c->fanout = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg || c->fanout <= 0)
return err_msg("bad fanout %s", optarg);
break;
case 'F':
c->space_fixup = 1;
break;
case 'l':
c->log_lebs = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg || c->log_lebs <= 0)
return err_msg("bad count of log LEBs '%s'",
optarg);
break;
case 'p':
c->orph_lebs = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
c->orph_lebs <= 0)
return err_msg("bad orphan LEB count '%s'",
optarg);
break;
case 'k':
if (strcmp(optarg, "r5") == 0) {
c->key_hash = key_r5_hash;
c->key_hash_type = UBIFS_KEY_HASH_R5;
} else if (strcmp(optarg, "test") == 0) {
c->key_hash = key_test_hash;
c->key_hash_type = UBIFS_KEY_HASH_TEST;
} else
return err_msg("bad key hash");
break;
case 'x':
if (strcmp(optarg, "none") == 0)
c->default_compr = UBIFS_COMPR_NONE;
else if (strcmp(optarg, "zlib") == 0)
c->default_compr = UBIFS_COMPR_ZLIB;
#ifndef WITHOUT_ZSTD
else if (strcmp(optarg, "zstd") == 0)
c->default_compr = UBIFS_COMPR_ZSTD;
#endif
#ifndef WITHOUT_LZO
else if (strcmp(optarg, "favor_lzo") == 0) {
c->default_compr = UBIFS_COMPR_LZO;
c->favor_lzo = 1;
} else if (strcmp(optarg, "lzo") == 0) {
c->default_compr = UBIFS_COMPR_LZO;
}
#endif
else
return err_msg("bad compressor name");
break;
case 'X':
#ifdef WITHOUT_LZO
return err_msg("built without LZO support");
#else
c->favor_percent = strtol(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg ||
c->favor_percent <= 0 || c->favor_percent >= 100)
return err_msg("bad favor LZO percent '%s'",
optarg);
#endif
break;
case 'j':
c->max_bud_bytes = get_bytes(optarg);
if (c->max_bud_bytes <= 0)
return err_msg("bad maximum amount of buds");
break;
case 'R':
c->rp_size = get_bytes(optarg);
if (c->rp_size < 0)
return err_msg("bad reserved bytes count");
break;
case 'U':
squash_owner = 1;
break;
case 'a':
do_create_inum_attr = 1;
break;
case 's':
context_len = strlen(optarg);
context = (char *) xmalloc(context_len + 1);
if (!context)
return err_msg("xmalloc failed\n");
memcpy(context, optarg, context_len);
/* Make sure root directory exists */
if (stat(context, &context_st))
return sys_err_msg("bad file context %s\n",
context);
break;
case 'K':
if (key_file) {
return err_msg("key file specified more than once");
}
key_file = optarg;
break;
case 'b':
if (key_desc) {
return err_msg("key descriptor specified more than once");
}
key_desc = optarg;
break;
case 'P': {
int error = 0;
unsigned long num;
num = simple_strtoul(optarg, &error);
if (error)
num = -1;
fscrypt_flags &= ~FS_POLICY_FLAGS_PAD_MASK;
switch (num) {
case 4:
fscrypt_flags |= FS_POLICY_FLAGS_PAD_4;
break;
case 8:
fscrypt_flags |= FS_POLICY_FLAGS_PAD_8;
break;
case 16:
fscrypt_flags |= FS_POLICY_FLAGS_PAD_16;
break;
case 32:
fscrypt_flags |= FS_POLICY_FLAGS_PAD_32;
break;
default:
return errmsg("invalid padding policy '%s'",
optarg);
}
break;
}
#ifdef WITH_CRYPTO
case 'C':
cipher_name = optarg;
break;
case HASH_ALGO_OPTION:
c->hash_algo_name = xstrdup(optarg);
break;
case AUTH_KEY_OPTION:
c->auth_key_filename = xstrdup(optarg);
break;
case AUTH_CERT_OPTION:
c->auth_cert_filename = xstrdup(optarg);
break;
}
#else
case 'C':
case HASH_ALGO_OPTION:
case AUTH_KEY_OPTION:
case X509_OPTION:
return err_msg("mkfs.ubifs was built without crypto support.");
#endif
}
if (optind != argc && !output)
output = xstrdup(argv[optind]);
if (!output)
return err_msg("not output device or file specified");
out_ubi = !open_ubi(output);
if (out_ubi) {
c->min_io_size = c->di.min_io_size;
c->leb_size = c->vi.leb_size;
if (c->max_leb_cnt == -1)
c->max_leb_cnt = c->vi.rsvd_lebs;
}
if (key_file || key_desc) {
#ifdef WITH_CRYPTO
if (!key_file)
return err_msg("no key file specified");
c->double_hash = 1;
c->encrypted = 1;
if (cipher_name == NULL)
cipher_name = "AES-256-XTS";
root_fctx = init_fscrypt_context(cipher_name, fscrypt_flags,
key_file, key_desc);
if (!root_fctx)
return -1;
#else
return err_msg("mkfs.ubifs was built without crypto support.");
#endif
}
if (c->default_compr == -1)
select_default_compr();
if (c->min_io_size == -1)
return err_msg("min. I/O unit was not specified "
"(use -h for help)");
if (c->leb_size == -1)
return err_msg("LEB size was not specified (use -h for help)");
if (c->max_leb_cnt == -1)
return err_msg("Maximum count of LEBs was not specified "
"(use -h for help)");
if (c->max_bud_bytes == -1) {
int lebs;
lebs = c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
lebs -= c->orph_lebs;
if (c->log_lebs != -1)
lebs -= c->log_lebs;
else
lebs -= UBIFS_MIN_LOG_LEBS;
/*
* We do not know lprops geometry so far, so assume minimum
* count of lprops LEBs.
*/
lebs -= UBIFS_MIN_LPT_LEBS;
/* Make the journal about 12.5% of main area lebs */
c->max_bud_bytes = (lebs / 8) * (long long)c->leb_size;
/* Make the max journal size 8MiB */
if (c->max_bud_bytes > 8 * 1024 * 1024)
c->max_bud_bytes = 8 * 1024 * 1024;
if (c->max_bud_bytes < 4 * c->leb_size)
c->max_bud_bytes = 4 * c->leb_size;
}
if (c->log_lebs == -1) {
c->log_lebs = calc_min_log_lebs(c->max_bud_bytes);
c->log_lebs += 2;
}
if (c->min_io_size < 8)
c->min_io_size = 8;
c->rp_size = add_space_overhead(c->rp_size);
if (verbose) {
printf("mkfs.ubifs\n");
printf("\troot: %s\n", root);
printf("\tmin_io_size: %d\n", c->min_io_size);
printf("\tleb_size: %d\n", c->leb_size);
printf("\tmax_leb_cnt: %d\n", c->max_leb_cnt);
printf("\toutput: %s\n", output);
printf("\tjrn_size: %llu\n", c->max_bud_bytes);
printf("\treserved: %llu\n", c->rp_size);
switch (c->default_compr) {
case UBIFS_COMPR_LZO:
printf("\tcompr: lzo\n");
break;
case UBIFS_COMPR_ZLIB:
printf("\tcompr: zlib\n");
break;
case UBIFS_COMPR_NONE:
printf("\tcompr: none\n");
break;
}
printf("\tkeyhash: %s\n", (c->key_hash == key_r5_hash) ?
"r5" : "test");
printf("\tfanout: %d\n", c->fanout);
printf("\torph_lebs: %d\n", c->orph_lebs);
printf("\tspace_fixup: %d\n", c->space_fixup);
printf("\tselinux file: %s\n", context);
}
if (validate_options())
return -1;
if (tbl_file && parse_devtable(tbl_file))
return err_msg("cannot parse device table file '%s'", tbl_file);
return 0;
}
/**
* prepare_node - fill in the common header.
* @node: node
* @len: node length
*/
static void prepare_node(void *node, int len)
{
uint32_t crc;
struct ubifs_ch *ch = node;
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
ch->len = cpu_to_le32(len);
ch->group_type = UBIFS_NO_NODE_GROUP;
ch->sqnum = cpu_to_le64(++c->max_sqnum);
ch->padding[0] = ch->padding[1] = 0;
crc = mtd_crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
ch->crc = cpu_to_le32(crc);
}
/**
* write_leb - copy the image of a LEB to the output target.
* @lnum: LEB number
* @len: length of data in the buffer
* @buf: buffer (must be at least c->leb_size bytes)
*/
int write_leb(int lnum, int len, void *buf)
{
off_t pos = (off_t)lnum * c->leb_size;
dbg_msg(3, "LEB %d len %d", lnum, len);
memset(buf + len, 0xff, c->leb_size - len);
if (out_ubi)
if (ubi_leb_change_start(ubi, out_fd, lnum, c->leb_size))
return sys_err_msg("ubi_leb_change_start failed");
if (lseek(out_fd, pos, SEEK_SET) != pos)
return sys_err_msg("lseek failed seeking %lld", (long long)pos);
if (write(out_fd, buf, c->leb_size) != c->leb_size)
return sys_err_msg("write failed writing %d bytes at pos %lld",
c->leb_size, (long long)pos);
return 0;
}
/**
* write_empty_leb - copy the image of an empty LEB to the output target.
* @lnum: LEB number
*/
static int write_empty_leb(int lnum)
{
return write_leb(lnum, 0, leb_buf);
}
/**
* do_pad - pad a buffer to the minimum I/O size.
* @buf: buffer
* @len: buffer length
*/
static int do_pad(void *buf, int len)
{
int pad_len, alen = ALIGN(len, 8), wlen = ALIGN(alen, c->min_io_size);
uint32_t crc;
memset(buf + len, 0xff, alen - len);
pad_len = wlen - alen;
dbg_msg(3, "len %d pad_len %d", len, pad_len);
buf += alen;
if (pad_len >= (int)UBIFS_PAD_NODE_SZ) {
struct ubifs_ch *ch = buf;
struct ubifs_pad_node *pad_node = buf;
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
ch->node_type = UBIFS_PAD_NODE;
ch->group_type = UBIFS_NO_NODE_GROUP;
ch->padding[0] = ch->padding[1] = 0;
ch->sqnum = cpu_to_le64(0);
ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
pad_len -= UBIFS_PAD_NODE_SZ;