-
Notifications
You must be signed in to change notification settings - Fork 0
/
udf_create.c
2399 lines (1904 loc) · 63.7 KB
/
udf_create.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
/* $NetBSD: udf_create.c,v 1.30 2021/08/20 20:25:26 andvar Exp $ */
/*
* Copyright (c) 2006, 2008 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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 AUTHOR ``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 AUTHOR 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.
*
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#define __RCSID(_s) /* nothing */
#define __SCCSID(_s) /* nothing */
#include <sys/cdefs.h>
__RCSID("$NetBSD: udf_create.c,v 1.30 2021/08/20 20:25:26 andvar Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <err.h>
#include <sys/types.h>
#include <sys/param.h>
#include "unicode.h"
#include "udf_create.h"
#if 0
# ifndef DEBUG
# define DEBUG
# endif
#endif
struct udf_create_context context;
struct udf_disclayout layout;
/*
* NOTE that there is some overlap between this code and the udf kernel fs.
* This is intentially though it might better be factored out one day.
*/
void
udf_init_create_context(void)
{
/* clear */
memset(&context, 0, sizeof(struct udf_create_context));
/* fill with defaults currently known */
context.dscrver = 3;
context.min_udf = 0x0102;
context.max_udf = 0x0260;
context.serialnum = 1; /* default */
context.gmtoff = 0;
context.sector_size = 512; /* minimum for UDF */
context.logvol_name = NULL;
context.primary_name = NULL;
context.volset_name = NULL;
context.fileset_name = NULL;
/* most basic identification */
context.app_name = "*NetBSD";
context.app_version_main = 0;
context.app_version_sub = 0;
context.impl_name = "*NetBSD";
context.vds_seq = 0; /* first one starts with zero */
/* Minimum value of 16 : UDF 3.2.1.1, 3.3.3.4. */
context.unique_id = 0x10;
context.num_files = 0;
context.num_directories = 0;
context.data_part = 0;
context.metadata_part = 0;
context.metadata_alloc_pos = 0;
context.data_alloc_pos = 0;
}
/* version can be specified as 0xabc or a.bc */
static int
parse_udfversion(const char *pos, uint32_t *version) {
int hex = 0;
char c1, c2, c3, c4;
*version = 0;
if (*pos == '0') {
pos++;
/* expect hex format */
hex = 1;
if (*pos++ != 'x')
return 1;
}
c1 = *pos++;
if (c1 < '0' || c1 > '9')
return 1;
c1 -= '0';
c2 = *pos++;
if (!hex) {
if (c2 != '.')
return 1;
c2 = *pos++;
}
if (c2 < '0' || c2 > '9')
return 1;
c2 -= '0';
c3 = *pos++;
if (c3 < '0' || c3 > '9')
return 1;
c3 -= '0';
c4 = *pos++;
if (c4 != 0)
return 1;
*version = c1 * 0x100 + c2 * 0x10 + c3;
return 0;
}
/* parse a given string for an udf version */
int
a_udf_version(const char *s, const char *id_type)
{
uint32_t version;
if (parse_udfversion(s, &version))
errx(1, "unknown %s id %s; specify as hex or float", id_type, s);
return version;
}
static uint32_t
udf_space_bitmap_len(uint32_t part_size)
{
return sizeof(struct space_bitmap_desc)-1 +
part_size/8;
}
static uint32_t
udf_bytes_to_sectors(uint64_t bytes)
{
uint32_t sector_size = layout.sector_size;
return (bytes + sector_size -1) / sector_size;
}
int
udf_calculate_disc_layout(int format_flags, int min_udf,
uint32_t wrtrack_skew,
uint32_t first_lba, uint32_t last_lba,
uint32_t sector_size, uint32_t blockingnr,
uint32_t sparable_blocks, float meta_fract)
{
uint64_t kbsize, bytes;
uint32_t sparable_blockingnr;
uint32_t align_blockingnr;
uint32_t pos, mpos;
/* clear */
memset(&layout, 0, sizeof(layout));
/* fill with parameters */
layout.wrtrack_skew = wrtrack_skew;
layout.first_lba = first_lba;
layout.last_lba = last_lba;
layout.sector_size = sector_size;
layout.blockingnr = blockingnr;
layout.sparable_blocks = sparable_blocks;
/* start disc layouting */
/*
* location of iso9660 vrs is defined as first sector AFTER 32kb,
* minimum `sector size' 2048
*/
layout.iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
+ first_lba;
/* anchor starts at specified offset in sectors */
layout.anchors[0] = first_lba + 256;
if (format_flags & FORMAT_TRACK512)
layout.anchors[0] = first_lba + 512;
layout.anchors[1] = last_lba - 256;
layout.anchors[2] = last_lba;
/* update workable space */
first_lba = layout.anchors[0] + blockingnr;
last_lba = layout.anchors[1] - 1;
/* XXX rest of anchor packet can be added to unallocated space descr */
/* reserve space for VRS and VRS copy and associated tables */
layout.vds_size = MAX(16, blockingnr); /* UDF 2.2.3.1+2 */
layout.vds1 = first_lba;
first_lba += layout.vds_size; /* next packet */
if (format_flags & FORMAT_SEQUENTIAL) {
/* for sequential, append them ASAP */
layout.vds2 = first_lba;
first_lba += layout.vds_size;
} else {
layout.vds2 = layout.anchors[1] - layout.vds_size;
last_lba = layout.vds2 - 1; /* XXX -1 ?? */
}
/* reserve space for logvol integrity sequence */
layout.lvis_size = MAX(8192/sector_size, 2 * blockingnr);
if (format_flags & FORMAT_VAT)
layout.lvis_size = 2;
if (format_flags & FORMAT_WORM)
layout.lvis_size = 64 * blockingnr;
/* TODO skip bad blocks in LVID sequence; for now use f.e. */
//first_lba+=128;
layout.lvis = first_lba;
first_lba += layout.lvis_size;
/* initial guess of UDF partition size */
layout.part_start_lba = first_lba;
layout.part_size_lba = last_lba - layout.part_start_lba;
/* all non sequential media needs an unallocated space bitmap */
layout.alloc_bitmap_dscr_size = 0;
if ((format_flags & (FORMAT_SEQUENTIAL | FORMAT_READONLY)) == 0) {
bytes = udf_space_bitmap_len(layout.part_size_lba);
layout.alloc_bitmap_dscr_size = udf_bytes_to_sectors(bytes);
/* XXX freed space map when applicable */
}
/*
* Note that for (bug) compatibility with version UDF 2.00 (fixed in
* 2.01 and higher) the blocking size needs to be 32 sectors otherwise
* the drive's blockingnr.
*/
sparable_blockingnr = blockingnr;
if (min_udf <= 0x200)
sparable_blockingnr = 32;
align_blockingnr = blockingnr;
if (format_flags & (FORMAT_SPARABLE | FORMAT_META))
align_blockingnr = sparable_blockingnr;
layout.align_blockingnr = align_blockingnr;
layout.sparable_blockingnr = sparable_blockingnr;
/*
* Align partition LBA space to blocking granularity. Not strickly
* necessary for non sparables but safer for the VRS data since it is
* not updated sporadically
*/
if ((format_flags & (FORMAT_SEQUENTIAL | FORMAT_READONLY)) == 0) {
#ifdef DEBUG
printf("Lost %d slack sectors at start\n", UDF_ROUNDUP(
first_lba - wrtrack_skew, align_blockingnr) -
(first_lba - wrtrack_skew));
printf("Lost %d slack sectors at end\n",
(first_lba - wrtrack_skew) - UDF_ROUNDDOWN(
first_lba - wrtrack_skew, align_blockingnr));
#endif
first_lba = UDF_ROUNDUP( first_lba - wrtrack_skew,
align_blockingnr);
last_lba = UDF_ROUNDDOWN(last_lba - wrtrack_skew,
align_blockingnr);
}
if ((format_flags & FORMAT_SPARABLE) == 0)
layout.sparable_blocks = 0;
if (format_flags & FORMAT_SPARABLE) {
layout.sparable_area_size =
layout.sparable_blocks * sparable_blockingnr;
/* a sparing table descriptor is a whole blockingnr sectors */
layout.sparing_table_dscr_lbas = sparable_blockingnr;
/* place the descriptors at the start and end of the area */
layout.spt_1 = first_lba;
first_lba += layout.sparing_table_dscr_lbas;
layout.spt_2 = last_lba - layout.sparing_table_dscr_lbas;
last_lba -= layout.sparing_table_dscr_lbas;
/* allocate sparable section */
layout.sparable_area = first_lba;
first_lba += layout.sparable_area_size;
}
/* update guess of UDF partition size */
layout.part_start_lba = first_lba;
layout.part_size_lba = last_lba - layout.part_start_lba;
/* determine partition selection for data and metadata */
context.data_part = 0;
context.metadata_part = context.data_part;
if ((format_flags & FORMAT_VAT) || (format_flags & FORMAT_META))
context.metadata_part = context.data_part + 1;
/*
* Pick fixed logical space sector numbers for main FSD, rootdir and
* unallocated space. The reason for this pre-allocation is that they
* are referenced in the volume descriptor sequence and hence can't be
* allocated later.
*/
pos = 0;
layout.unalloc_space = pos;
pos += layout.alloc_bitmap_dscr_size;
/* claim metadata descriptors and partition space [UDF 2.2.10] */
if (format_flags & FORMAT_META) {
/* note: all in backing partition space */
layout.meta_file = pos++;
layout.meta_bitmap = pos++;
layout.meta_mirror = layout.part_size_lba-1;
layout.meta_alignment = MAX(blockingnr, sparable_blockingnr);
layout.meta_blockingnr = MAX(layout.meta_alignment, 32);
/* calculate our partition length and store in sectors */
layout.meta_part_size_lba = layout.part_size_lba * meta_fract;
layout.meta_part_size_lba = MAX(layout.meta_part_size_lba, 32);
layout.meta_part_size_lba =
UDF_ROUNDDOWN(layout.meta_part_size_lba, layout.meta_blockingnr);
/* calculate positions */
bytes = udf_space_bitmap_len(layout.meta_part_size_lba);
layout.meta_bitmap_dscr_size = udf_bytes_to_sectors(bytes);
layout.meta_bitmap_space = pos;
pos += layout.meta_bitmap_dscr_size;
layout.meta_part_start_lba = UDF_ROUNDUP(pos, layout.meta_alignment);
}
mpos = (context.metadata_part == context.data_part) ? pos : 0;
layout.fsd = mpos; mpos += 1;
layout.rootdir = mpos; mpos += 1;
layout.vat = mpos; mpos += 1; /* if present */
#if 0
printf("Summary so far\n");
printf("\tiso9660_vrs\t\t%d\n", layout.iso9660_vrs);
printf("\tanchor0\t\t\t%d\n", layout.anchors[0]);
printf("\tanchor1\t\t\t%d\n", layout.anchors[1]);
printf("\tanchor2\t\t\t%d\n", layout.anchors[2]);
printf("\tvds_size\t\t%d\n", layout.vds_size);
printf("\tvds1\t\t\t%d\n", layout.vds1);
printf("\tvds2\t\t\t%d\n", layout.vds2);
printf("\tlvis_size\t\t%d\n", layout.lvis_size);
printf("\tlvis\t\t\t%d\n", layout.lvis);
if (format_flags & FORMAT_SPARABLE) {
printf("\tsparable size\t\t%d\n", layout.sparable_area_size);
printf("\tsparable\t\t%d\n", layout.sparable_area);
}
printf("\tpartition start lba\t%d\n", layout.part_start_lba);
printf("\tpartition size\t\t%d KiB, %d MiB\n",
(layout.part_size_lba * sector_size) / 1024,
(layout.part_size_lba * sector_size) / (1024*1024));
if ((format_flags & FORMAT_SEQUENTIAL) == 0) {
printf("\tpart bitmap start\t%d\n", layout.unalloc_space);
printf("\t\tfor %d lba\n", layout.alloc_bitmap_dscr_size);
}
if (format_flags & FORMAT_META) {
printf("\tmeta blockingnr\t\t%d\n", layout.meta_blockingnr);
printf("\tmeta alignment\t\t%d\n", layout.meta_alignment);
printf("\tmeta size\t\t%d KiB, %d MiB\n",
(layout.meta_part_size_lba * sector_size) / 1024,
(layout.meta_part_size_lba * sector_size) / (1024*1024));
printf("\tmeta file\t\t%d\n", layout.meta_file);
printf("\tmeta mirror\t\t%d\n", layout.meta_mirror);
printf("\tmeta bitmap\t\t%d\n", layout.meta_bitmap);
printf("\tmeta bitmap start\t%d\n", layout.meta_bitmap_space);
printf("\t\tfor %d lba\n", layout.meta_bitmap_dscr_size);
printf("\tmeta space start\t%d\n", layout.meta_part_start_lba);
printf("\t\tfor %d lba\n", layout.meta_part_size_lba);
}
printf("\n");
#endif
kbsize = (uint64_t) last_lba * sector_size;
printf("Total space on this medium approx. %llu KiB, %llu MiB\n",
kbsize/1024, kbsize/(1024*1024));
kbsize = (uint64_t)(layout.part_size_lba - layout.alloc_bitmap_dscr_size
- layout.meta_bitmap_dscr_size) * sector_size;
printf("Free space on this volume approx. %llu KiB, %llu MiB\n\n",
kbsize/1024, kbsize/(1024*1024));
return 0;
}
int
udf_validate_tag_sum(union dscrptr *dscr)
{
struct desc_tag *tag = &dscr->tag;
uint8_t *pos, sum, cnt;
/* calculate TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for(cnt = 0; cnt < 16; cnt++) {
if (cnt != 4) sum += *pos;
pos++;
};
tag->cksum = sum; /* 8 bit */
return 0;
}
/* assumes sector number of descriptor to be allready present */
int
udf_validate_tag_and_crc_sums(union dscrptr *dscr)
{
struct desc_tag *tag = &dscr->tag;
uint16_t crc;
/* check payload CRC if applicable */
if (udf_rw16(tag->desc_crc_len) > 0) {
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH,
udf_rw16(tag->desc_crc_len));
tag->desc_crc = udf_rw16(crc);
};
/* calculate TAG header checksum */
return udf_validate_tag_sum(dscr);
}
void
udf_inittag(struct desc_tag *tag, int tagid, uint32_t loc)
{
tag->id = udf_rw16(tagid);
tag->descriptor_ver = udf_rw16(context.dscrver);
tag->cksum = 0;
tag->reserved = 0;
tag->serial_num = udf_rw16(context.serialnum);
tag->tag_loc = udf_rw32(loc);
}
int
udf_create_anchor(int num)
{
struct anchor_vdp *avdp;
uint32_t vds_extent_len = layout.vds_size * context.sector_size;
if ((avdp = calloc(1, context.sector_size)) == NULL)
return ENOMEM;
udf_inittag(&avdp->tag, TAGID_ANCHOR, layout.anchors[num]);
avdp->main_vds_ex.loc = udf_rw32(layout.vds1);
avdp->main_vds_ex.len = udf_rw32(vds_extent_len);
avdp->reserve_vds_ex.loc = udf_rw32(layout.vds2);
avdp->reserve_vds_ex.len = udf_rw32(vds_extent_len);
/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
avdp->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
context.anchors[num] = avdp;
return 0;
}
void
udf_create_terminator(union dscrptr *dscr, uint32_t loc)
{
memset(dscr, 0, context.sector_size);
udf_inittag(&dscr->tag, TAGID_TERM, loc);
/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
}
void
udf_osta_charset(struct charspec *charspec)
{
memset(charspec, 0, sizeof(*charspec));
charspec->type = 0;
strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
}
void
udf_encode_osta_id(char *osta_id, uint16_t len, char *text)
{
uint16_t u16_name[1024];
uint8_t *pos;
uint16_t *pos16;
memset(osta_id, 0, len);
if (!text || (strlen(text) == 0)) return;
memset(u16_name, 0, sizeof(uint16_t) * 1023);
/* convert ascii to 16 bits unicode */
pos = (uint8_t *) text;
pos16 = u16_name;
while (*pos) {
*pos16 = *pos;
pos++; pos16++;
};
*pos16 = 0;
udf_CompressUnicode(len, 8, (unicode_t *) u16_name, (byte *) osta_id);
/* Ecma 167/7.2.13 states that length is recorded in the last byte */
osta_id[len-1] = strlen(text)+1;
}
/* first call udf_set_regid and then the suffix */
void
udf_set_regid(struct regid *regid, char const *name)
{
memset(regid, 0, sizeof(*regid));
regid->flags = 0; /* not dirty and not protected */
strcpy((char *) regid->id, name);
}
void
udf_add_domain_regid(struct regid *regid)
{
uint16_t *ver;
ver = (uint16_t *) regid->id_suffix;
*ver = udf_rw16(context.min_udf);
}
void
udf_add_udf_regid(struct regid *regid)
{
uint16_t *ver;
ver = (uint16_t *) regid->id_suffix;
*ver = udf_rw16(context.min_udf);
regid->id_suffix[2] = 4; /* unix */
regid->id_suffix[3] = 8; /* NetBSD */
}
void
udf_add_impl_regid(struct regid *regid)
{
regid->id_suffix[0] = 4; /* unix */
regid->id_suffix[1] = 8; /* NetBSD */
}
void
udf_add_app_regid(struct regid *regid)
{
regid->id_suffix[0] = context.app_version_main;
regid->id_suffix[1] = context.app_version_sub;
}
/*
* Fill in timestamp structure based on clock_gettime(). Time is reported back
* as a time_t accompanied with a nano second field.
*
* The husec, usec and csec could be relaxed in type.
*/
static void
udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
{
struct tm tm;
uint64_t husec, usec, csec;
memset(timestamp, 0, sizeof(*timestamp));
gmtime_r(×pec->tv_sec, &tm);
/*
* Time type and time zone : see ECMA 1/7.3, UDF 2., 2.1.4.1, 3.1.1.
*
* Lower 12 bits are two complement signed timezone offset if bit 12
* (method 1) is clear. Otherwise if bit 12 is set, specify timezone
* offset to -2047 i.e. unsigned `zero'
*/
/* set method 1 for CUT/GMT */
timestamp->type_tz = udf_rw16((1<<12) + 0);
timestamp->year = udf_rw16(tm.tm_year + 1900);
timestamp->month = tm.tm_mon + 1; /* `tm' uses 0..11 for months */
timestamp->day = tm.tm_mday;
timestamp->hour = tm.tm_hour;
timestamp->minute = tm.tm_min;
timestamp->second = tm.tm_sec;
usec = (timespec->tv_nsec + 500) / 1000; /* round */
husec = usec / 100;
usec -= husec * 100; /* only 0-99 in usec */
csec = husec / 100; /* only 0-99 in csec */
husec -= csec * 100; /* only 0-99 in husec */
/* in rare cases there is overflow in csec */
csec = MIN(99, csec);
husec = MIN(99, husec);
usec = MIN(99, usec);
timestamp->centisec = csec;
timestamp->hund_usec = husec;
timestamp->usec = usec;
}
void
udf_set_timestamp_now(struct timestamp *timestamp)
{
struct timespec now;
#ifdef CLOCK_REALTIME
(void)clock_gettime(CLOCK_REALTIME, &now);
#else
struct timeval time_of_day;
(void)gettimeofday(&time_of_day, NULL);
now.tv_sec = time_of_day.tv_sec;
now.tv_nsec = time_of_day.tv_usec * 1000;
#endif
udf_timespec_to_timestamp(&now, timestamp);
}
/* some code copied from sys/fs/udf */
static void
udf_set_timestamp(struct timestamp *timestamp, time_t value)
{
struct timespec t;
memset(&t, 0, sizeof(struct timespec));
t.tv_sec = value;
t.tv_nsec = 0;
udf_timespec_to_timestamp(&t, timestamp);
}
static uint32_t
unix_mode_to_udf_perm(mode_t mode)
{
uint32_t perm;
perm = ((mode & S_IRWXO) );
perm |= ((mode & S_IRWXG) << 2);
perm |= ((mode & S_IRWXU) << 4);
perm |= ((mode & S_IWOTH) << 3);
perm |= ((mode & S_IWGRP) << 5);
perm |= ((mode & S_IWUSR) << 7);
return perm;
}
/* end of copied code */
int
udf_create_primaryd(void)
{
struct pri_vol_desc *pri;
uint16_t crclen;
pri = calloc(1, context.sector_size);
if (pri == NULL)
return ENOMEM;
memset(pri, 0, context.sector_size);
udf_inittag(&pri->tag, TAGID_PRI_VOL, /* loc */ 0);
pri->seq_num = udf_rw32(context.vds_seq); context.vds_seq++;
pri->pvd_num = udf_rw32(0); /* default serial */
udf_encode_osta_id(pri->vol_id, 32, context.primary_name);
/* set defaults for single disc volumes as UDF prescribes */
pri->vds_num = udf_rw16(1);
pri->max_vol_seq = udf_rw16(1);
pri->ichg_lvl = udf_rw16(2);
pri->max_ichg_lvl = udf_rw16(3);
pri->flags = udf_rw16(0);
pri->charset_list = udf_rw32(1); /* only CS0 */
pri->max_charset_list = udf_rw32(1); /* only CS0 */
udf_encode_osta_id(pri->volset_id, 128, context.volset_name);
udf_osta_charset(&pri->desc_charset);
udf_osta_charset(&pri->explanatory_charset);
udf_set_regid(&pri->app_id, context.app_name);
udf_add_app_regid(&pri->app_id);
udf_set_regid(&pri->imp_id, context.impl_name);
udf_add_impl_regid(&pri->imp_id);
udf_set_timestamp_now(&pri->time);
crclen = sizeof(struct pri_vol_desc) - UDF_DESC_TAG_LENGTH;
pri->tag.desc_crc_len = udf_rw16(crclen);
context.primary_vol = pri;
return 0;
}
/* XXX no support for unallocated or freed space tables yet (!) */
int
udf_create_partitiond(int part_num, int part_accesstype)
{
struct part_desc *pd;
struct part_hdr_desc *phd;
uint32_t sector_size, bitmap_bytes;
uint16_t crclen;
sector_size = context.sector_size;
bitmap_bytes = layout.alloc_bitmap_dscr_size * sector_size;
if (context.partitions[part_num]) {
printf("Internal error: partition %d allready defined\n",
part_num);
return EINVAL;
}
pd = calloc(1, context.sector_size);
if (pd == NULL)
return ENOMEM;
phd = &pd->_impl_use.part_hdr;
udf_inittag(&pd->tag, TAGID_PARTITION, /* loc */ 0);
pd->seq_num = udf_rw32(context.vds_seq); context.vds_seq++;
pd->flags = udf_rw16(1); /* allocated */
pd->part_num = udf_rw16(part_num); /* only one physical partition */
if (context.dscrver == 2) {
udf_set_regid(&pd->contents, "+NSR02");
} else {
udf_set_regid(&pd->contents, "+NSR03");
}
udf_add_app_regid(&pd->contents);
phd->unalloc_space_bitmap.len = udf_rw32(bitmap_bytes);
phd->unalloc_space_bitmap.lb_num = udf_rw32(layout.unalloc_space);
if (layout.freed_space) {
phd->freed_space_bitmap.len = udf_rw32(bitmap_bytes);
phd->freed_space_bitmap.lb_num = udf_rw32(layout.freed_space);
}
pd->access_type = udf_rw32(part_accesstype);
pd->start_loc = udf_rw32(layout.part_start_lba);
pd->part_len = udf_rw32(layout.part_size_lba);
udf_set_regid(&pd->imp_id, context.impl_name);
udf_add_impl_regid(&pd->imp_id);
crclen = sizeof(struct part_desc) - UDF_DESC_TAG_LENGTH;
pd->tag.desc_crc_len = udf_rw16(crclen);
context.partitions[part_num] = pd;
return 0;
}
int
udf_create_unalloc_spaced(void)
{
struct unalloc_sp_desc *usd;
uint16_t crclen;
usd = calloc(1, context.sector_size);
if (usd == NULL)
return ENOMEM;
udf_inittag(&usd->tag, TAGID_UNALLOC_SPACE, /* loc */ 0);
usd->seq_num = udf_rw32(context.vds_seq); context.vds_seq++;
/* no default entries */
usd->alloc_desc_num = udf_rw32(0); /* no entries */
crclen = sizeof(struct unalloc_sp_desc) - sizeof(struct extent_ad);
crclen -= UDF_DESC_TAG_LENGTH;
usd->tag.desc_crc_len = udf_rw16(crclen);
context.unallocated = usd;
return 0;
}
static int
udf_create_base_logical_dscr(void)
{
struct logvol_desc *lvd;
uint32_t sector_size;
uint16_t crclen;
sector_size = context.sector_size;
lvd = calloc(1, sector_size);
if (lvd == NULL)
return ENOMEM;
udf_inittag(&lvd->tag, TAGID_LOGVOL, /* loc */ 0);
lvd->seq_num = udf_rw32(context.vds_seq); context.vds_seq++;
udf_osta_charset(&lvd->desc_charset);
udf_encode_osta_id(lvd->logvol_id, 128, context.logvol_name);
lvd->lb_size = udf_rw32(context.sector_size);
udf_set_regid(&lvd->domain_id, "*OSTA UDF Compliant");
udf_add_domain_regid(&lvd->domain_id);
/* no partition mappings/entries yet */
lvd->mt_l = udf_rw32(0);
lvd->n_pm = udf_rw32(0);
udf_set_regid(&lvd->imp_id, context.impl_name);
udf_add_impl_regid(&lvd->imp_id);
lvd->integrity_seq_loc.loc = udf_rw32(layout.lvis);
lvd->integrity_seq_loc.len = udf_rw32(layout.lvis_size * sector_size);
/* just one fsd for now */
lvd->lv_fsd_loc.len = udf_rw32(sector_size);
lvd->lv_fsd_loc.loc.part_num = udf_rw32(context.metadata_part);
lvd->lv_fsd_loc.loc.lb_num = udf_rw32(layout.fsd);
crclen = sizeof(struct logvol_desc) - 1 - UDF_DESC_TAG_LENGTH;
lvd->tag.desc_crc_len = udf_rw16(crclen);
context.logical_vol = lvd;
context.vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
context.vtop_offset[UDF_VTOP_RAWPART] = 0;
return 0;
}
static void
udf_add_logvol_part_physical(uint16_t phys_part)
{
struct logvol_desc *logvol = context.logical_vol;
union udf_pmap *pmap;
uint8_t *pmap_pos;
uint16_t crclen;
uint32_t pmap1_size, log_part;
log_part = udf_rw32(logvol->n_pm);
pmap_pos = logvol->maps + udf_rw32(logvol->mt_l);
pmap1_size = sizeof(struct part_map_1);
pmap = (union udf_pmap *) pmap_pos;
pmap->pm1.type = 1;
pmap->pm1.len = sizeof(struct part_map_1);
pmap->pm1.vol_seq_num = udf_rw16(1); /* no multi-volume */
pmap->pm1.part_num = udf_rw16(phys_part);
context.vtop [log_part] = phys_part;
context.vtop_tp [log_part] = UDF_VTOP_TYPE_PHYS;
context.vtop_offset[log_part] = layout.part_start_lba;
context.part_size[log_part] = layout.part_size_lba;
context.part_free[log_part] = layout.part_size_lba;
/* increment number of partitions and length */
logvol->n_pm = udf_rw32(log_part + 1);
logvol->mt_l = udf_rw32(udf_rw32(logvol->mt_l) + pmap1_size);
crclen = udf_rw16(logvol->tag.desc_crc_len) + pmap1_size;
logvol->tag.desc_crc_len = udf_rw16(crclen);
}
static void
udf_add_logvol_part_virtual(uint16_t phys_part)
{
union udf_pmap *pmap;
struct logvol_desc *logvol = context.logical_vol;
uint8_t *pmap_pos;
uint16_t crclen;
uint32_t pmapv_size, log_part;
log_part = udf_rw32(logvol->n_pm);
pmap_pos = logvol->maps + udf_rw32(logvol->mt_l);
pmapv_size = sizeof(struct part_map_2);
pmap = (union udf_pmap *) pmap_pos;
pmap->pmv.type = 2;
pmap->pmv.len = pmapv_size;
udf_set_regid(&pmap->pmv.id, "*UDF Virtual Partition");
udf_add_udf_regid(&pmap->pmv.id);
pmap->pmv.vol_seq_num = udf_rw16(1); /* no multi-volume */
pmap->pmv.part_num = udf_rw16(phys_part);
context.vtop [log_part] = phys_part;
context.vtop_tp [log_part] = UDF_VTOP_TYPE_VIRT;
context.vtop_offset[log_part] = context.vtop_offset[phys_part];
context.part_size[log_part] = 0xffffffff;
context.part_free[log_part] = 0xffffffff;
/* increment number of partitions and length */
logvol->n_pm = udf_rw32(log_part + 1);
logvol->mt_l = udf_rw32(udf_rw32(logvol->mt_l) + pmapv_size);
crclen = udf_rw16(logvol->tag.desc_crc_len) + pmapv_size;
logvol->tag.desc_crc_len = udf_rw16(crclen);
}
/* sparing table size is in bytes */
static void
udf_add_logvol_part_sparable(uint16_t phys_part)
{
union udf_pmap *pmap;
struct logvol_desc *logvol = context.logical_vol;
uint32_t *st_pos, sparable_bytes, pmaps_size;
uint8_t *pmap_pos, num;
uint16_t crclen;
uint32_t log_part;
log_part = udf_rw32(logvol->n_pm);
pmap_pos = logvol->maps + udf_rw32(logvol->mt_l);
pmaps_size = sizeof(struct part_map_2);
sparable_bytes = layout.sparable_area_size * context.sector_size;
pmap = (union udf_pmap *) pmap_pos;
pmap->pms.type = 2;
pmap->pms.len = pmaps_size;
udf_set_regid(&pmap->pmv.id, "*UDF Sparable Partition");
udf_add_udf_regid(&pmap->pmv.id);
pmap->pms.vol_seq_num = udf_rw16(1); /* no multi-volume */
pmap->pms.part_num = udf_rw16(phys_part);
pmap->pms.packet_len = udf_rw16(layout.sparable_blockingnr);
pmap->pms.st_size = udf_rw32(sparable_bytes);
/* enter spare tables */
st_pos = &pmap->pms.st_loc[0];
*st_pos++ = udf_rw32(layout.spt_1);
*st_pos++ = udf_rw32(layout.spt_2);
num = 2;
if (layout.spt_2 == 0) num--;
if (layout.spt_1 == 0) num--;
pmap->pms.n_st = num; /* 8 bit */
/* the vtop_offset needs to explicitly set since there is no phys. */
context.vtop [log_part] = phys_part;
context.vtop_tp [log_part] = UDF_VTOP_TYPE_SPARABLE;
context.vtop_offset[log_part] = layout.part_start_lba;
context.part_size[log_part] = layout.part_size_lba;
context.part_free[log_part] = layout.part_size_lba;
/* increment number of partitions and length */
logvol->n_pm = udf_rw32(log_part + 1);