-
Notifications
You must be signed in to change notification settings - Fork 0
/
vvfat.c
3289 lines (2842 loc) · 102 KB
/
vvfat.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
/* vim:set shiftwidth=4 ts=4: */
/*
* QEMU Block driver for virtual VFAT (shadows a local directory)
*
* Copyright (c) 2004,2005 Johannes E. Schindelin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include <dirent.h>
#include <glib/gstdio.h>
#include "qapi/error.h"
#include "block/block_int.h"
#include "block/qdict.h"
#include "qemu/module.h"
#include "qemu/option.h"
#include "qemu/bswap.h"
#include "migration/blocker.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qemu/ctype.h"
#include "qemu/cutils.h"
#include "qemu/error-report.h"
#ifndef S_IWGRP
#define S_IWGRP 0
#endif
#ifndef S_IWOTH
#define S_IWOTH 0
#endif
/* TODO: add ":bootsector=blabla.img:" */
/* LATER TODO: add automatic boot sector generation from
BOOTEASY.ASM and Ranish Partition Manager
Note that DOS assumes the system files to be the first files in the
file system (test if the boot sector still relies on that fact)! */
/* MAYBE TODO: write block-visofs.c */
/* TODO: call try_commit() only after a timeout */
/* #define DEBUG */
#ifdef DEBUG
#define DLOG(a) a
static void checkpoint(void);
#else
#define DLOG(a)
#endif
/* bootsector OEM name. see related compatibility problems at:
* https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
* http://seasip.info/Misc/oemid.html
*/
#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
#define DIR_DELETED 0xe5
#define DIR_KANJI DIR_DELETED
#define DIR_KANJI_FAKE 0x05
#define DIR_FREE 0x00
/* dynamic array functions */
typedef struct array_t {
char* pointer;
unsigned int size,next,item_size;
} array_t;
static inline void array_init(array_t* array,unsigned int item_size)
{
array->pointer = NULL;
array->size=0;
array->next=0;
array->item_size=item_size;
}
static inline void array_free(array_t* array)
{
g_free(array->pointer);
array->size=array->next=0;
}
/* does not automatically grow */
static inline void* array_get(array_t* array,unsigned int index) {
assert(index < array->next);
assert(array->pointer);
return array->pointer + index * array->item_size;
}
static inline void array_ensure_allocated(array_t *array, int index)
{
if((index + 1) * array->item_size > array->size) {
int new_size = (index + 32) * array->item_size;
array->pointer = g_realloc(array->pointer, new_size);
assert(array->pointer);
memset(array->pointer + array->size, 0, new_size - array->size);
array->size = new_size;
array->next = index + 1;
}
}
static inline void* array_get_next(array_t* array) {
unsigned int next = array->next;
array_ensure_allocated(array, next);
array->next = next + 1;
return array_get(array, next);
}
static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
if((array->next+count)*array->item_size>array->size) {
int increment=count*array->item_size;
array->pointer=g_realloc(array->pointer,array->size+increment);
if(!array->pointer)
return NULL;
array->size+=increment;
}
memmove(array->pointer+(index+count)*array->item_size,
array->pointer+index*array->item_size,
(array->next-index)*array->item_size);
array->next+=count;
return array->pointer+index*array->item_size;
}
static inline int array_remove_slice(array_t* array,int index, int count)
{
assert(index >=0);
assert(count > 0);
assert(index + count <= array->next);
memmove(array->pointer + index * array->item_size,
array->pointer + (index + count) * array->item_size,
(array->next - index - count) * array->item_size);
array->next -= count;
return 0;
}
static int array_remove(array_t* array,int index)
{
return array_remove_slice(array, index, 1);
}
/* return the index for a given member */
static int array_index(array_t* array, void* pointer)
{
size_t offset = (char*)pointer - array->pointer;
assert((offset % array->item_size) == 0);
assert(offset/array->item_size < array->next);
return offset/array->item_size;
}
/* These structures are used to fake a disk and the VFAT filesystem.
* For this reason we need to use QEMU_PACKED. */
typedef struct bootsector_t {
uint8_t jump[3];
uint8_t name[8];
uint16_t sector_size;
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t number_of_fats;
uint16_t root_entries;
uint16_t total_sectors16;
uint8_t media_type;
uint16_t sectors_per_fat;
uint16_t sectors_per_track;
uint16_t number_of_heads;
uint32_t hidden_sectors;
uint32_t total_sectors;
union {
struct {
uint8_t drive_number;
uint8_t reserved1;
uint8_t signature;
uint32_t id;
uint8_t volume_label[11];
uint8_t fat_type[8];
uint8_t ignored[0x1c0];
} QEMU_PACKED fat16;
struct {
uint32_t sectors_per_fat;
uint16_t flags;
uint8_t major,minor;
uint32_t first_cluster_of_root_dir;
uint16_t info_sector;
uint16_t backup_boot_sector;
uint8_t reserved[12];
uint8_t drive_number;
uint8_t reserved1;
uint8_t signature;
uint32_t id;
uint8_t volume_label[11];
uint8_t fat_type[8];
uint8_t ignored[0x1a4];
} QEMU_PACKED fat32;
} u;
uint8_t magic[2];
} QEMU_PACKED bootsector_t;
typedef struct {
uint8_t head;
uint8_t sector;
uint8_t cylinder;
} mbr_chs_t;
typedef struct partition_t {
uint8_t attributes; /* 0x80 = bootable */
mbr_chs_t start_CHS;
uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
mbr_chs_t end_CHS;
uint32_t start_sector_long;
uint32_t length_sector_long;
} QEMU_PACKED partition_t;
typedef struct mbr_t {
uint8_t ignored[0x1b8];
uint32_t nt_id;
uint8_t ignored2[2];
partition_t partition[4];
uint8_t magic[2];
} QEMU_PACKED mbr_t;
typedef struct direntry_t {
uint8_t name[8 + 3];
uint8_t attributes;
uint8_t reserved[2];
uint16_t ctime;
uint16_t cdate;
uint16_t adate;
uint16_t begin_hi;
uint16_t mtime;
uint16_t mdate;
uint16_t begin;
uint32_t size;
} QEMU_PACKED direntry_t;
/* this structure are used to transparently access the files */
typedef struct mapping_t {
/* begin is the first cluster, end is the last+1 */
uint32_t begin,end;
/* as s->directory is growable, no pointer may be used here */
unsigned int dir_index;
/* the clusters of a file may be in any order; this points to the first */
int first_mapping_index;
union {
/* offset is
* - the offset in the file (in clusters) for a file, or
* - the next cluster of the directory for a directory
*/
struct {
uint32_t offset;
} file;
struct {
int parent_mapping_index;
int first_dir_index;
} dir;
} info;
/* path contains the full path, i.e. it always starts with s->path */
char* path;
enum {
MODE_UNDEFINED = 0,
MODE_NORMAL = 1,
MODE_MODIFIED = 2,
MODE_DIRECTORY = 4,
MODE_DELETED = 8,
} mode;
int read_only;
} mapping_t;
#ifdef DEBUG
static void print_direntry(const struct direntry_t*);
static void print_mapping(const struct mapping_t* mapping);
#endif
/* here begins the real VVFAT driver */
typedef struct BDRVVVFATState {
CoMutex lock;
BlockDriverState* bs; /* pointer to parent */
unsigned char first_sectors[0x40*0x200];
int fat_type; /* 16 or 32 */
array_t fat,directory,mapping;
char volume_label[11];
uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
unsigned int cluster_size;
unsigned int sectors_per_cluster;
unsigned int sectors_per_fat;
uint32_t last_cluster_of_root_directory;
/* how many entries are available in root directory (0 for FAT32) */
uint16_t root_entries;
uint32_t sector_count; /* total number of sectors of the partition */
uint32_t cluster_count; /* total number of clusters of this partition */
uint32_t max_fat_value;
uint32_t offset_to_fat;
uint32_t offset_to_root_dir;
int current_fd;
mapping_t* current_mapping;
unsigned char* cluster; /* points to current cluster */
unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
unsigned int current_cluster;
/* write support */
char* qcow_filename;
BdrvChild* qcow;
void* fat2;
char* used_clusters;
array_t commits;
const char* path;
int downcase_short_names;
Error *migration_blocker;
} BDRVVVFATState;
/* take the sector position spos and convert it to Cylinder/Head/Sector position
* if the position is outside the specified geometry, fill maximum value for CHS
* and return 1 to signal overflow.
*/
static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
{
int head,sector;
sector = spos % secs; spos /= secs;
head = spos % heads; spos /= heads;
if (spos >= cyls) {
/* Overflow,
it happens if 32bit sector positions are used, while CHS is only 24bit.
Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
chs->head = 0xFF;
chs->sector = 0xFF;
chs->cylinder = 0xFF;
return 1;
}
chs->head = (uint8_t)head;
chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
chs->cylinder = (uint8_t)spos;
return 0;
}
static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
{
/* TODO: if the files mbr.img and bootsect.img exist, use them */
mbr_t* real_mbr=(mbr_t*)s->first_sectors;
partition_t* partition = &(real_mbr->partition[0]);
int lba;
memset(s->first_sectors,0,512);
/* Win NT Disk Signature */
real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
partition->attributes=0x80; /* bootable */
/* LBA is used when partition is outside the CHS geometry */
lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
cyls, heads, secs);
lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
cyls, heads, secs);
/*LBA partitions are identified only by start/length_sector_long not by CHS*/
partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
- s->offset_to_bootsector);
/* FAT12/FAT16/FAT32 */
/* DOS uses different types when partition is LBA,
probably to prevent older versions from using CHS on them */
partition->fs_type = s->fat_type == 12 ? 0x1 :
s->fat_type == 16 ? (lba ? 0xe : 0x06) :
/*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
}
/* direntry functions */
static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
{
int number_of_entries, i;
glong length;
direntry_t *entry;
gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
if (!longname) {
fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
return NULL;
}
number_of_entries = DIV_ROUND_UP(length * 2, 26);
for(i=0;i<number_of_entries;i++) {
entry=array_get_next(&(s->directory));
entry->attributes=0xf;
entry->reserved[0]=0;
entry->begin=0;
entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
}
for(i=0;i<26*number_of_entries;i++) {
int offset=(i%26);
if(offset<10) offset=1+offset;
else if(offset<22) offset=14+offset-10;
else offset=28+offset-22;
entry=array_get(&(s->directory),s->directory.next-1-(i/26));
if (i >= 2 * length + 2) {
entry->name[offset] = 0xff;
} else if (i % 2 == 0) {
entry->name[offset] = longname[i / 2] & 0xff;
} else {
entry->name[offset] = longname[i / 2] >> 8;
}
}
g_free(longname);
return array_get(&(s->directory),s->directory.next-number_of_entries);
}
static char is_free(const direntry_t* direntry)
{
return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
}
static char is_volume_label(const direntry_t* direntry)
{
return direntry->attributes == 0x28;
}
static char is_long_name(const direntry_t* direntry)
{
return direntry->attributes == 0xf;
}
static char is_short_name(const direntry_t* direntry)
{
return !is_volume_label(direntry) && !is_long_name(direntry)
&& !is_free(direntry);
}
static char is_directory(const direntry_t* direntry)
{
return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
}
static inline char is_dot(const direntry_t* direntry)
{
return is_short_name(direntry) && direntry->name[0] == '.';
}
static char is_file(const direntry_t* direntry)
{
return is_short_name(direntry) && !is_directory(direntry);
}
static inline uint32_t begin_of_direntry(const direntry_t* direntry)
{
return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
}
static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
{
return le32_to_cpu(direntry->size);
}
static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
{
direntry->begin = cpu_to_le16(begin & 0xffff);
direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
}
static bool valid_filename(const unsigned char *name)
{
unsigned char c;
if (!strcmp((const char*)name, ".") || !strcmp((const char*)name, "..")) {
return false;
}
for (; (c = *name); name++) {
if (!((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c > 127 ||
strchr(" $%'-_@~`!(){}^#&.+,;=[]", c) != NULL))
{
return false;
}
}
return true;
}
static uint8_t to_valid_short_char(gunichar c)
{
c = g_unichar_toupper(c);
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
strchr("$%'-_@~`!(){}^#&", c) != NULL) {
return c;
} else {
return 0;
}
}
static direntry_t *create_short_filename(BDRVVVFATState *s,
const char *filename,
unsigned int directory_start)
{
int i, j = 0;
direntry_t *entry = array_get_next(&(s->directory));
const gchar *p, *last_dot = NULL;
gunichar c;
bool lossy_conversion = false;
char tail[8];
if (!entry) {
return NULL;
}
memset(entry->name, 0x20, sizeof(entry->name));
/* copy filename and search last dot */
for (p = filename; ; p = g_utf8_next_char(p)) {
c = g_utf8_get_char(p);
if (c == '\0') {
break;
} else if (c == '.') {
if (j == 0) {
/* '.' at start of filename */
lossy_conversion = true;
} else {
if (last_dot) {
lossy_conversion = true;
}
last_dot = p;
}
} else if (!last_dot) {
/* first part of the name; copy it */
uint8_t v = to_valid_short_char(c);
if (j < 8 && v) {
entry->name[j++] = v;
} else {
lossy_conversion = true;
}
}
}
/* copy extension (if any) */
if (last_dot) {
j = 0;
for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
c = g_utf8_get_char(p);
if (c == '\0') {
break;
} else {
/* extension; copy it */
uint8_t v = to_valid_short_char(c);
if (j < 3 && v) {
entry->name[8 + (j++)] = v;
} else {
lossy_conversion = true;
}
}
}
}
if (entry->name[0] == DIR_KANJI) {
entry->name[0] = DIR_KANJI_FAKE;
}
/* numeric-tail generation */
for (j = 0; j < 8; j++) {
if (entry->name[j] == ' ') {
break;
}
}
for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
direntry_t *entry1;
if (i > 0) {
int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
assert(len <= 7);
memcpy(entry->name + MIN(j, 8 - len), tail, len);
}
for (entry1 = array_get(&(s->directory), directory_start);
entry1 < entry; entry1++) {
if (!is_long_name(entry1) &&
!memcmp(entry1->name, entry->name, 11)) {
break; /* found dupe */
}
}
if (entry1 == entry) {
/* no dupe found */
return entry;
}
}
return NULL;
}
/* fat functions */
static inline uint8_t fat_chksum(const direntry_t* entry)
{
uint8_t chksum=0;
int i;
for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
chksum = (((chksum & 0xfe) >> 1) |
((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
}
return chksum;
}
/* if return_time==0, this returns the fat_date, else the fat_time */
static uint16_t fat_datetime(time_t time,int return_time) {
struct tm* t;
struct tm t1;
t = &t1;
localtime_r(&time,t);
if(return_time)
return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
}
static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
{
if(s->fat_type==32) {
uint32_t* entry=array_get(&(s->fat),cluster);
*entry=cpu_to_le32(value);
} else if(s->fat_type==16) {
uint16_t* entry=array_get(&(s->fat),cluster);
*entry=cpu_to_le16(value&0xffff);
} else {
int offset = (cluster*3/2);
unsigned char* p = array_get(&(s->fat), offset);
switch (cluster&1) {
case 0:
p[0] = value&0xff;
p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
break;
case 1:
p[0] = (p[0]&0xf) | ((value&0xf)<<4);
p[1] = (value>>4);
break;
}
}
}
static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
{
if(s->fat_type==32) {
uint32_t* entry=array_get(&(s->fat),cluster);
return le32_to_cpu(*entry);
} else if(s->fat_type==16) {
uint16_t* entry=array_get(&(s->fat),cluster);
return le16_to_cpu(*entry);
} else {
const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
}
}
static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
{
if(fat_entry>s->max_fat_value-8)
return -1;
return 0;
}
static inline void init_fat(BDRVVVFATState* s)
{
if (s->fat_type == 12) {
array_init(&(s->fat),1);
array_ensure_allocated(&(s->fat),
s->sectors_per_fat * 0x200 * 3 / 2 - 1);
} else {
array_init(&(s->fat),(s->fat_type==32?4:2));
array_ensure_allocated(&(s->fat),
s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
}
memset(s->fat.pointer,0,s->fat.size);
switch(s->fat_type) {
case 12: s->max_fat_value=0xfff; break;
case 16: s->max_fat_value=0xffff; break;
case 32: s->max_fat_value=0x0fffffff; break;
default: s->max_fat_value=0; /* error... */
}
}
static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
unsigned int directory_start, const char* filename, int is_dot)
{
int long_index = s->directory.next;
direntry_t* entry = NULL;
direntry_t* entry_long = NULL;
if(is_dot) {
entry=array_get_next(&(s->directory));
memset(entry->name, 0x20, sizeof(entry->name));
memcpy(entry->name,filename,strlen(filename));
return entry;
}
entry_long=create_long_filename(s,filename);
entry = create_short_filename(s, filename, directory_start);
/* calculate checksum; propagate to long name */
if(entry_long) {
uint8_t chksum=fat_chksum(entry);
/* calculate anew, because realloc could have taken place */
entry_long=array_get(&(s->directory),long_index);
while(entry_long<entry && is_long_name(entry_long)) {
entry_long->reserved[1]=chksum;
entry_long++;
}
}
return entry;
}
/*
* Read a directory. (the index of the corresponding mapping must be passed).
*/
static int read_directory(BDRVVVFATState* s, int mapping_index)
{
mapping_t* mapping = array_get(&(s->mapping), mapping_index);
direntry_t* direntry;
const char* dirname = mapping->path;
int first_cluster = mapping->begin;
int parent_index = mapping->info.dir.parent_mapping_index;
mapping_t* parent_mapping = (mapping_t*)
(parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
DIR* dir=opendir(dirname);
struct dirent* entry;
int i;
assert(mapping->mode & MODE_DIRECTORY);
if(!dir) {
mapping->end = mapping->begin;
return -1;
}
i = mapping->info.dir.first_dir_index =
first_cluster == 0 ? 0 : s->directory.next;
if (first_cluster != 0) {
/* create the top entries of a subdirectory */
(void)create_short_and_long_name(s, i, ".", 1);
(void)create_short_and_long_name(s, i, "..", 1);
}
/* actually read the directory, and allocate the mappings */
while((entry=readdir(dir))) {
unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
char* buffer;
direntry_t* direntry;
struct stat st;
int is_dot=!strcmp(entry->d_name,".");
int is_dotdot=!strcmp(entry->d_name,"..");
if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
fprintf(stderr, "Too many entries in root directory\n");
closedir(dir);
return -2;
}
if(first_cluster == 0 && (is_dotdot || is_dot))
continue;
buffer = g_malloc(length);
snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
if(stat(buffer,&st)<0) {
g_free(buffer);
continue;
}
/* create directory entry for this file */
if (!is_dot && !is_dotdot) {
direntry = create_short_and_long_name(s, i, entry->d_name, 0);
} else {
direntry = array_get(&(s->directory), is_dot ? i : i + 1);
}
direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
direntry->reserved[0]=direntry->reserved[1]=0;
direntry->ctime=fat_datetime(st.st_ctime,1);
direntry->cdate=fat_datetime(st.st_ctime,0);
direntry->adate=fat_datetime(st.st_atime,0);
direntry->begin_hi=0;
direntry->mtime=fat_datetime(st.st_mtime,1);
direntry->mdate=fat_datetime(st.st_mtime,0);
if(is_dotdot)
set_begin_of_direntry(direntry, first_cluster_of_parent);
else if(is_dot)
set_begin_of_direntry(direntry, first_cluster);
else
direntry->begin=0; /* do that later */
if (st.st_size > 0x7fffffff) {
fprintf(stderr, "File %s is larger than 2GB\n", buffer);
g_free(buffer);
closedir(dir);
return -2;
}
direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
/* create mapping for this file */
if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
s->current_mapping = array_get_next(&(s->mapping));
s->current_mapping->begin=0;
s->current_mapping->end=st.st_size;
/*
* we get the direntry of the most recent direntry, which
* contains the short name and all the relevant information.
*/
s->current_mapping->dir_index=s->directory.next-1;
s->current_mapping->first_mapping_index = -1;
if (S_ISDIR(st.st_mode)) {
s->current_mapping->mode = MODE_DIRECTORY;
s->current_mapping->info.dir.parent_mapping_index =
mapping_index;
} else {
s->current_mapping->mode = MODE_UNDEFINED;
s->current_mapping->info.file.offset = 0;
}
s->current_mapping->path=buffer;
s->current_mapping->read_only =
(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
} else {
g_free(buffer);
}
}
closedir(dir);
/* fill with zeroes up to the end of the cluster */
while(s->directory.next%(0x10*s->sectors_per_cluster)) {
direntry_t* direntry=array_get_next(&(s->directory));
memset(direntry,0,sizeof(direntry_t));
}
if (s->fat_type != 32 &&
mapping_index == 0 &&
s->directory.next < s->root_entries) {
/* root directory */
int cur = s->directory.next;
array_ensure_allocated(&(s->directory), s->root_entries - 1);
s->directory.next = s->root_entries;
memset(array_get(&(s->directory), cur), 0,
(s->root_entries - cur) * sizeof(direntry_t));
}
/* re-get the mapping, since s->mapping was possibly realloc()ed */
mapping = array_get(&(s->mapping), mapping_index);
first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
* 0x20 / s->cluster_size;
mapping->end = first_cluster;
direntry = array_get(&(s->directory), mapping->dir_index);
set_begin_of_direntry(direntry, mapping->begin);
return 0;
}
static inline int32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
{
return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
}
static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
{
return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
}
static int init_directories(BDRVVVFATState* s,
const char *dirname, int heads, int secs,
Error **errp)
{
bootsector_t* bootsector;
mapping_t* mapping;
unsigned int i;
unsigned int cluster;
memset(&(s->first_sectors[0]),0,0x40*0x200);
s->cluster_size=s->sectors_per_cluster*0x200;
s->cluster_buffer=g_malloc(s->cluster_size);
/*
* The formula: sc = spf+1+spf*spc*(512*8/fat_type),
* where sc is sector_count,
* spf is sectors_per_fat,
* spc is sectors_per_clusters, and
* fat_type = 12, 16 or 32.
*/
i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
s->offset_to_fat = s->offset_to_bootsector + 1;
s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
array_init(&(s->mapping),sizeof(mapping_t));
array_init(&(s->directory),sizeof(direntry_t));
/* add volume label */
{
direntry_t* entry=array_get_next(&(s->directory));
entry->attributes=0x28; /* archive | volume label */
memcpy(entry->name, s->volume_label, sizeof(entry->name));
}
/* Now build FAT, and write back information into directory */
init_fat(s);
/* TODO: if there are more entries, bootsector has to be adjusted! */
s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
s->cluster_count=sector2cluster(s, s->sector_count);
mapping = array_get_next(&(s->mapping));
mapping->begin = 0;
mapping->dir_index = 0;
mapping->info.dir.parent_mapping_index = -1;
mapping->first_mapping_index = -1;
mapping->path = g_strdup(dirname);
i = strlen(mapping->path);
if (i > 0 && mapping->path[i - 1] == '/')
mapping->path[i - 1] = '\0';
mapping->mode = MODE_DIRECTORY;
mapping->read_only = 0;
s->path = mapping->path;
for (i = 0, cluster = 0; i < s->mapping.next; i++) {
/* MS-DOS expects the FAT to be 0 for the root directory
* (except for the media byte). */
/* LATER TODO: still true for FAT32? */
int fix_fat = (i != 0);
mapping = array_get(&(s->mapping), i);
if (mapping->mode & MODE_DIRECTORY) {
char *path = mapping->path;
mapping->begin = cluster;
if(read_directory(s, i)) {
error_setg(errp, "Could not read directory %s", path);
return -1;
}
mapping = array_get(&(s->mapping), i);
} else {
assert(mapping->mode == MODE_UNDEFINED);
mapping->mode=MODE_NORMAL;
mapping->begin = cluster;
if (mapping->end > 0) {
direntry_t* direntry = array_get(&(s->directory),
mapping->dir_index);
mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
set_begin_of_direntry(direntry, mapping->begin);
} else {
mapping->end = cluster + 1;
fix_fat = 0;
}
}
assert(mapping->begin < mapping->end);
/* next free cluster */
cluster = mapping->end;
if(cluster > s->cluster_count) {
error_setg(errp,
"Directory does not fit in FAT%d (capacity %.2f MB)",
s->fat_type, s->sector_count / 2000.0);
return -1;
}
/* fix fat for entry */
if (fix_fat) {
int j;
for(j = mapping->begin; j < mapping->end - 1; j++)
fat_set(s, j, j+1);