1 /* vim:set shiftwidth=4 ts=8: */
3 * QEMU Block driver for virtual VFAT (shadows a local directory)
5 * Copyright (c) 2004,2005 Johannes E. Schindelin
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu-common.h"
28 #include "block_int.h"
38 /* TODO: add ":bootsector=blabla.img:" */
39 /* LATER TODO: add automatic boot sector generation from
40 BOOTEASY.ASM and Ranish Partition Manager
41 Note that DOS assumes the system files to be the first files in the
42 file system (test if the boot sector still relies on that fact)! */
43 /* MAYBE TODO: write block-visofs.c */
44 /* TODO: call try_commit() only after a timeout */
56 static void checkpoint(void);
59 void nonono(const char* file
, int line
, const char* msg
) {
60 fprintf(stderr
, "Nonono! %s:%d %s\n", file
, line
, msg
);
64 #define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0)
73 /* dynamic array functions */
74 typedef struct array_t
{
76 unsigned int size
,next
,item_size
;
79 static inline void array_init(array_t
* array
,unsigned int item_size
)
81 array
->pointer
= NULL
;
84 array
->item_size
=item_size
;
87 static inline void array_free(array_t
* array
)
89 g_free(array
->pointer
);
90 array
->size
=array
->next
=0;
93 /* does not automatically grow */
94 static inline void* array_get(array_t
* array
,unsigned int index
) {
95 assert(index
< array
->next
);
96 return array
->pointer
+ index
* array
->item_size
;
99 static inline int array_ensure_allocated(array_t
* array
, int index
)
101 if((index
+ 1) * array
->item_size
> array
->size
) {
102 int new_size
= (index
+ 32) * array
->item_size
;
103 array
->pointer
= g_realloc(array
->pointer
, new_size
);
106 array
->size
= new_size
;
107 array
->next
= index
+ 1;
113 static inline void* array_get_next(array_t
* array
) {
114 unsigned int next
= array
->next
;
117 if (array_ensure_allocated(array
, next
) < 0)
120 array
->next
= next
+ 1;
121 result
= array_get(array
, next
);
126 static inline void* array_insert(array_t
* array
,unsigned int index
,unsigned int count
) {
127 if((array
->next
+count
)*array
->item_size
>array
->size
) {
128 int increment
=count
*array
->item_size
;
129 array
->pointer
=g_realloc(array
->pointer
,array
->size
+increment
);
132 array
->size
+=increment
;
134 memmove(array
->pointer
+(index
+count
)*array
->item_size
,
135 array
->pointer
+index
*array
->item_size
,
136 (array
->next
-index
)*array
->item_size
);
138 return array
->pointer
+index
*array
->item_size
;
141 /* this performs a "roll", so that the element which was at index_from becomes
142 * index_to, but the order of all other elements is preserved. */
143 static inline int array_roll(array_t
* array
,int index_to
,int index_from
,int count
)
151 index_to
<0 || index_to
>=array
->next
||
152 index_from
<0 || index_from
>=array
->next
)
155 if(index_to
==index_from
)
159 from
=array
->pointer
+index_from
*is
;
160 to
=array
->pointer
+index_to
*is
;
161 buf
=g_malloc(is
*count
);
162 memcpy(buf
,from
,is
*count
);
164 if(index_to
<index_from
)
165 memmove(to
+is
*count
,to
,from
-to
);
167 memmove(from
,from
+is
*count
,to
-from
);
169 memcpy(to
,buf
,is
*count
);
176 static inline int array_remove_slice(array_t
* array
,int index
, int count
)
180 assert(index
+ count
<= array
->next
);
181 if(array_roll(array
,array
->next
-1,index
,count
))
183 array
->next
-= count
;
187 static int array_remove(array_t
* array
,int index
)
189 return array_remove_slice(array
, index
, 1);
192 /* return the index for a given member */
193 static int array_index(array_t
* array
, void* pointer
)
195 size_t offset
= (char*)pointer
- array
->pointer
;
196 assert((offset
% array
->item_size
) == 0);
197 assert(offset
/array
->item_size
< array
->next
);
198 return offset
/array
->item_size
;
201 /* These structures are used to fake a disk and the VFAT filesystem.
202 * For this reason we need to use QEMU_PACKED. */
204 typedef struct bootsector_t
{
207 uint16_t sector_size
;
208 uint8_t sectors_per_cluster
;
209 uint16_t reserved_sectors
;
210 uint8_t number_of_fats
;
211 uint16_t root_entries
;
212 uint16_t total_sectors16
;
214 uint16_t sectors_per_fat
;
215 uint16_t sectors_per_track
;
216 uint16_t number_of_heads
;
217 uint32_t hidden_sectors
;
218 uint32_t total_sectors
;
221 uint8_t drive_number
;
222 uint8_t current_head
;
225 uint8_t volume_label
[11];
228 uint32_t sectors_per_fat
;
231 uint32_t first_cluster_of_root_directory
;
232 uint16_t info_sector
;
233 uint16_t backup_boot_sector
;
238 uint8_t ignored
[0x1c0];
240 } QEMU_PACKED bootsector_t
;
248 typedef struct partition_t
{
249 uint8_t attributes
; /* 0x80 = bootable */
251 uint8_t fs_type
; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
253 uint32_t start_sector_long
;
254 uint32_t length_sector_long
;
255 } QEMU_PACKED partition_t
;
257 typedef struct mbr_t
{
258 uint8_t ignored
[0x1b8];
261 partition_t partition
[4];
265 typedef struct direntry_t
{
267 uint8_t extension
[3];
278 } QEMU_PACKED direntry_t
;
280 /* this structure are used to transparently access the files */
282 typedef struct mapping_t
{
283 /* begin is the first cluster, end is the last+1 */
285 /* as s->directory is growable, no pointer may be used here */
286 unsigned int dir_index
;
287 /* the clusters of a file may be in any order; this points to the first */
288 int first_mapping_index
;
291 * - the offset in the file (in clusters) for a file, or
292 * - the next cluster of the directory for a directory, and
293 * - the address of the buffer for a faked entry
299 int parent_mapping_index
;
303 /* path contains the full path, i.e. it always starts with s->path */
306 enum { MODE_UNDEFINED
= 0, MODE_NORMAL
= 1, MODE_MODIFIED
= 2,
307 MODE_DIRECTORY
= 4, MODE_FAKED
= 8,
308 MODE_DELETED
= 16, MODE_RENAMED
= 32 } mode
;
313 static void print_direntry(const struct direntry_t
*);
314 static void print_mapping(const struct mapping_t
* mapping
);
317 /* here begins the real VVFAT driver */
319 typedef struct BDRVVVFATState
{
321 BlockDriverState
* bs
; /* pointer to parent */
322 unsigned int first_sectors_number
; /* 1 for a single partition, 0x40 for a disk with partition table */
323 unsigned char first_sectors
[0x40*0x200];
325 int fat_type
; /* 16 or 32 */
326 array_t fat
,directory
,mapping
;
328 unsigned int cluster_size
;
329 unsigned int sectors_per_cluster
;
330 unsigned int sectors_per_fat
;
331 unsigned int sectors_of_root_directory
;
332 uint32_t last_cluster_of_root_directory
;
333 unsigned int faked_sectors
; /* how many sectors are faked before file data */
334 uint32_t sector_count
; /* total number of sectors of the partition */
335 uint32_t cluster_count
; /* total number of clusters of this partition */
336 uint32_t max_fat_value
;
339 mapping_t
* current_mapping
;
340 unsigned char* cluster
; /* points to current cluster */
341 unsigned char* cluster_buffer
; /* points to a buffer to hold temp data */
342 unsigned int current_cluster
;
345 BlockDriverState
* write_target
;
347 BlockDriverState
* qcow
;
352 int downcase_short_names
;
355 /* take the sector position spos and convert it to Cylinder/Head/Sector position
356 * if the position is outside the specified geometry, fill maximum value for CHS
357 * and return 1 to signal overflow.
359 static int sector2CHS(BlockDriverState
* bs
, mbr_chs_t
* chs
, int spos
){
361 sector
= spos
% (bs
->secs
); spos
/= bs
->secs
;
362 head
= spos
% (bs
->heads
); spos
/= bs
->heads
;
363 if(spos
>= bs
->cyls
){
365 it happens if 32bit sector positions are used, while CHS is only 24bit.
366 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
369 chs
->cylinder
= 0xFF;
372 chs
->head
= (uint8_t)head
;
373 chs
->sector
= (uint8_t)( (sector
+1) | ((spos
>>8)<<6) );
374 chs
->cylinder
= (uint8_t)spos
;
378 static void init_mbr(BDRVVVFATState
* s
)
380 /* TODO: if the files mbr.img and bootsect.img exist, use them */
381 mbr_t
* real_mbr
=(mbr_t
*)s
->first_sectors
;
382 partition_t
* partition
= &(real_mbr
->partition
[0]);
385 memset(s
->first_sectors
,0,512);
387 /* Win NT Disk Signature */
388 real_mbr
->nt_id
= cpu_to_le32(0xbe1afdfa);
390 partition
->attributes
=0x80; /* bootable */
392 /* LBA is used when partition is outside the CHS geometry */
393 lba
= sector2CHS(s
->bs
, &partition
->start_CHS
, s
->first_sectors_number
-1);
394 lba
|= sector2CHS(s
->bs
, &partition
->end_CHS
, s
->sector_count
);
396 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
397 partition
->start_sector_long
=cpu_to_le32(s
->first_sectors_number
-1);
398 partition
->length_sector_long
=cpu_to_le32(s
->sector_count
- s
->first_sectors_number
+1);
400 /* FAT12/FAT16/FAT32 */
401 /* DOS uses different types when partition is LBA,
402 probably to prevent older versions from using CHS on them */
403 partition
->fs_type
= s
->fat_type
==12 ? 0x1:
404 s
->fat_type
==16 ? (lba
?0xe:0x06):
405 /*fat_tyoe==32*/ (lba
?0xc:0x0b);
407 real_mbr
->magic
[0]=0x55; real_mbr
->magic
[1]=0xaa;
410 /* direntry functions */
412 /* dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26 */
413 static inline int short2long_name(char* dest
,const char* src
)
417 for(i
=0;i
<129 && src
[i
];i
++) {
422 dest
[2*i
]=dest
[2*i
+1]=0;
423 for(i
=2*i
+2;(i
%26);i
++)
428 static inline direntry_t
* create_long_filename(BDRVVVFATState
* s
,const char* filename
)
431 int length
=short2long_name(buffer
,filename
),
432 number_of_entries
=(length
+25)/26,i
;
435 for(i
=0;i
<number_of_entries
;i
++) {
436 entry
=array_get_next(&(s
->directory
));
437 entry
->attributes
=0xf;
438 entry
->reserved
[0]=0;
440 entry
->name
[0]=(number_of_entries
-i
)|(i
==0?0x40:0);
442 for(i
=0;i
<26*number_of_entries
;i
++) {
444 if(offset
<10) offset
=1+offset
;
445 else if(offset
<22) offset
=14+offset
-10;
446 else offset
=28+offset
-22;
447 entry
=array_get(&(s
->directory
),s
->directory
.next
-1-(i
/26));
448 entry
->name
[offset
]=buffer
[i
];
450 return array_get(&(s
->directory
),s
->directory
.next
-number_of_entries
);
453 static char is_free(const direntry_t
* direntry
)
455 return direntry
->name
[0]==0xe5 || direntry
->name
[0]==0x00;
458 static char is_volume_label(const direntry_t
* direntry
)
460 return direntry
->attributes
== 0x28;
463 static char is_long_name(const direntry_t
* direntry
)
465 return direntry
->attributes
== 0xf;
468 static char is_short_name(const direntry_t
* direntry
)
470 return !is_volume_label(direntry
) && !is_long_name(direntry
)
471 && !is_free(direntry
);
474 static char is_directory(const direntry_t
* direntry
)
476 return direntry
->attributes
& 0x10 && direntry
->name
[0] != 0xe5;
479 static inline char is_dot(const direntry_t
* direntry
)
481 return is_short_name(direntry
) && direntry
->name
[0] == '.';
484 static char is_file(const direntry_t
* direntry
)
486 return is_short_name(direntry
) && !is_directory(direntry
);
489 static inline uint32_t begin_of_direntry(const direntry_t
* direntry
)
491 return le16_to_cpu(direntry
->begin
)|(le16_to_cpu(direntry
->begin_hi
)<<16);
494 static inline uint32_t filesize_of_direntry(const direntry_t
* direntry
)
496 return le32_to_cpu(direntry
->size
);
499 static void set_begin_of_direntry(direntry_t
* direntry
, uint32_t begin
)
501 direntry
->begin
= cpu_to_le16(begin
& 0xffff);
502 direntry
->begin_hi
= cpu_to_le16((begin
>> 16) & 0xffff);
507 static inline uint8_t fat_chksum(const direntry_t
* entry
)
515 c
= (i
< 8) ? entry
->name
[i
] : entry
->extension
[i
-8];
516 chksum
=(((chksum
&0xfe)>>1)|((chksum
&0x01)?0x80:0)) + c
;
522 /* if return_time==0, this returns the fat_date, else the fat_time */
523 static uint16_t fat_datetime(time_t time
,int return_time
) {
526 t
=localtime(&time
); /* this is not thread safe */
530 localtime_r(&time
,t
);
533 return cpu_to_le16((t
->tm_sec
/2)|(t
->tm_min
<<5)|(t
->tm_hour
<<11));
534 return cpu_to_le16((t
->tm_mday
)|((t
->tm_mon
+1)<<5)|((t
->tm_year
-80)<<9));
537 static inline void fat_set(BDRVVVFATState
* s
,unsigned int cluster
,uint32_t value
)
539 if(s
->fat_type
==32) {
540 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
541 *entry
=cpu_to_le32(value
);
542 } else if(s
->fat_type
==16) {
543 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
544 *entry
=cpu_to_le16(value
&0xffff);
546 int offset
= (cluster
*3/2);
547 unsigned char* p
= array_get(&(s
->fat
), offset
);
551 p
[1] = (p
[1]&0xf0) | ((value
>>8)&0xf);
554 p
[0] = (p
[0]&0xf) | ((value
&0xf)<<4);
561 static inline uint32_t fat_get(BDRVVVFATState
* s
,unsigned int cluster
)
563 if(s
->fat_type
==32) {
564 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
565 return le32_to_cpu(*entry
);
566 } else if(s
->fat_type
==16) {
567 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
568 return le16_to_cpu(*entry
);
570 const uint8_t* x
=(uint8_t*)(s
->fat
.pointer
)+cluster
*3/2;
571 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
575 static inline int fat_eof(BDRVVVFATState
* s
,uint32_t fat_entry
)
577 if(fat_entry
>s
->max_fat_value
-8)
582 static inline void init_fat(BDRVVVFATState
* s
)
584 if (s
->fat_type
== 12) {
585 array_init(&(s
->fat
),1);
586 array_ensure_allocated(&(s
->fat
),
587 s
->sectors_per_fat
* 0x200 * 3 / 2 - 1);
589 array_init(&(s
->fat
),(s
->fat_type
==32?4:2));
590 array_ensure_allocated(&(s
->fat
),
591 s
->sectors_per_fat
* 0x200 / s
->fat
.item_size
- 1);
593 memset(s
->fat
.pointer
,0,s
->fat
.size
);
595 switch(s
->fat_type
) {
596 case 12: s
->max_fat_value
=0xfff; break;
597 case 16: s
->max_fat_value
=0xffff; break;
598 case 32: s
->max_fat_value
=0x0fffffff; break;
599 default: s
->max_fat_value
=0; /* error... */
604 /* TODO: in create_short_filename, 0xe5->0x05 is not yet handled! */
605 /* TODO: in parse_short_filename, 0x05->0xe5 is not yet handled! */
606 static inline direntry_t
* create_short_and_long_name(BDRVVVFATState
* s
,
607 unsigned int directory_start
, const char* filename
, int is_dot
)
609 int i
,j
,long_index
=s
->directory
.next
;
610 direntry_t
* entry
= NULL
;
611 direntry_t
* entry_long
= NULL
;
614 entry
=array_get_next(&(s
->directory
));
615 memset(entry
->name
,0x20,11);
616 memcpy(entry
->name
,filename
,strlen(filename
));
620 entry_long
=create_long_filename(s
,filename
);
622 i
= strlen(filename
);
623 for(j
= i
- 1; j
>0 && filename
[j
]!='.';j
--);
629 entry
=array_get_next(&(s
->directory
));
630 memset(entry
->name
,0x20,11);
631 memcpy(entry
->name
, filename
, i
);
634 for (i
= 0; i
< 3 && filename
[j
+1+i
]; i
++)
635 entry
->extension
[i
] = filename
[j
+1+i
];
637 /* upcase & remove unwanted characters */
639 if(i
==10 || i
==7) for(;i
>0 && entry
->name
[i
]==' ';i
--);
640 if(entry
->name
[i
]<=' ' || entry
->name
[i
]>0x7f
641 || strchr(".*?<>|\":/\\[];,+='",entry
->name
[i
]))
643 else if(entry
->name
[i
]>='a' && entry
->name
[i
]<='z')
644 entry
->name
[i
]+='A'-'a';
647 /* mangle duplicates */
649 direntry_t
* entry1
=array_get(&(s
->directory
),directory_start
);
652 for(;entry1
<entry
;entry1
++)
653 if(!is_long_name(entry1
) && !memcmp(entry1
->name
,entry
->name
,11))
654 break; /* found dupe */
655 if(entry1
==entry
) /* no dupe found */
658 /* use all 8 characters of name */
659 if(entry
->name
[7]==' ') {
661 for(j
=6;j
>0 && entry
->name
[j
]==' ';j
--)
665 /* increment number */
666 for(j
=7;j
>0 && entry
->name
[j
]=='9';j
--)
669 if(entry
->name
[j
]<'0' || entry
->name
[j
]>'9')
676 /* calculate checksum; propagate to long name */
678 uint8_t chksum
=fat_chksum(entry
);
680 /* calculate anew, because realloc could have taken place */
681 entry_long
=array_get(&(s
->directory
),long_index
);
682 while(entry_long
<entry
&& is_long_name(entry_long
)) {
683 entry_long
->reserved
[1]=chksum
;
692 * Read a directory. (the index of the corresponding mapping must be passed).
694 static int read_directory(BDRVVVFATState
* s
, int mapping_index
)
696 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
697 direntry_t
* direntry
;
698 const char* dirname
= mapping
->path
;
699 int first_cluster
= mapping
->begin
;
700 int parent_index
= mapping
->info
.dir
.parent_mapping_index
;
701 mapping_t
* parent_mapping
= (mapping_t
*)
702 (parent_index
>= 0 ? array_get(&(s
->mapping
), parent_index
) : NULL
);
703 int first_cluster_of_parent
= parent_mapping
? parent_mapping
->begin
: -1;
705 DIR* dir
=opendir(dirname
);
706 struct dirent
* entry
;
709 assert(mapping
->mode
& MODE_DIRECTORY
);
712 mapping
->end
= mapping
->begin
;
716 i
= mapping
->info
.dir
.first_dir_index
=
717 first_cluster
== 0 ? 0 : s
->directory
.next
;
719 /* actually read the directory, and allocate the mappings */
720 while((entry
=readdir(dir
))) {
721 unsigned int length
=strlen(dirname
)+2+strlen(entry
->d_name
);
723 direntry_t
* direntry
;
725 int is_dot
=!strcmp(entry
->d_name
,".");
726 int is_dotdot
=!strcmp(entry
->d_name
,"..");
728 if(first_cluster
== 0 && (is_dotdot
|| is_dot
))
731 buffer
=(char*)g_malloc(length
);
732 snprintf(buffer
,length
,"%s/%s",dirname
,entry
->d_name
);
734 if(stat(buffer
,&st
)<0) {
739 /* create directory entry for this file */
740 direntry
=create_short_and_long_name(s
, i
, entry
->d_name
,
741 is_dot
|| is_dotdot
);
742 direntry
->attributes
=(S_ISDIR(st
.st_mode
)?0x10:0x20);
743 direntry
->reserved
[0]=direntry
->reserved
[1]=0;
744 direntry
->ctime
=fat_datetime(st
.st_ctime
,1);
745 direntry
->cdate
=fat_datetime(st
.st_ctime
,0);
746 direntry
->adate
=fat_datetime(st
.st_atime
,0);
747 direntry
->begin_hi
=0;
748 direntry
->mtime
=fat_datetime(st
.st_mtime
,1);
749 direntry
->mdate
=fat_datetime(st
.st_mtime
,0);
751 set_begin_of_direntry(direntry
, first_cluster_of_parent
);
753 set_begin_of_direntry(direntry
, first_cluster
);
755 direntry
->begin
=0; /* do that later */
756 if (st
.st_size
> 0x7fffffff) {
757 fprintf(stderr
, "File %s is larger than 2GB\n", buffer
);
762 direntry
->size
=cpu_to_le32(S_ISDIR(st
.st_mode
)?0:st
.st_size
);
764 /* create mapping for this file */
765 if(!is_dot
&& !is_dotdot
&& (S_ISDIR(st
.st_mode
) || st
.st_size
)) {
766 s
->current_mapping
=(mapping_t
*)array_get_next(&(s
->mapping
));
767 s
->current_mapping
->begin
=0;
768 s
->current_mapping
->end
=st
.st_size
;
770 * we get the direntry of the most recent direntry, which
771 * contains the short name and all the relevant information.
773 s
->current_mapping
->dir_index
=s
->directory
.next
-1;
774 s
->current_mapping
->first_mapping_index
= -1;
775 if (S_ISDIR(st
.st_mode
)) {
776 s
->current_mapping
->mode
= MODE_DIRECTORY
;
777 s
->current_mapping
->info
.dir
.parent_mapping_index
=
780 s
->current_mapping
->mode
= MODE_UNDEFINED
;
781 s
->current_mapping
->info
.file
.offset
= 0;
783 s
->current_mapping
->path
=buffer
;
784 s
->current_mapping
->read_only
=
785 (st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) == 0;
790 /* fill with zeroes up to the end of the cluster */
791 while(s
->directory
.next
%(0x10*s
->sectors_per_cluster
)) {
792 direntry_t
* direntry
=array_get_next(&(s
->directory
));
793 memset(direntry
,0,sizeof(direntry_t
));
796 /* TODO: if there are more entries, bootsector has to be adjusted! */
797 #define ROOT_ENTRIES (0x02 * 0x10 * s->sectors_per_cluster)
798 if (mapping_index
== 0 && s
->directory
.next
< ROOT_ENTRIES
) {
800 int cur
= s
->directory
.next
;
801 array_ensure_allocated(&(s
->directory
), ROOT_ENTRIES
- 1);
802 s
->directory
.next
= ROOT_ENTRIES
;
803 memset(array_get(&(s
->directory
), cur
), 0,
804 (ROOT_ENTRIES
- cur
) * sizeof(direntry_t
));
807 /* reget the mapping, since s->mapping was possibly realloc()ed */
808 mapping
= (mapping_t
*)array_get(&(s
->mapping
), mapping_index
);
809 first_cluster
+= (s
->directory
.next
- mapping
->info
.dir
.first_dir_index
)
810 * 0x20 / s
->cluster_size
;
811 mapping
->end
= first_cluster
;
813 direntry
= (direntry_t
*)array_get(&(s
->directory
), mapping
->dir_index
);
814 set_begin_of_direntry(direntry
, mapping
->begin
);
819 static inline uint32_t sector2cluster(BDRVVVFATState
* s
,off_t sector_num
)
821 return (sector_num
-s
->faked_sectors
)/s
->sectors_per_cluster
;
824 static inline off_t
cluster2sector(BDRVVVFATState
* s
, uint32_t cluster_num
)
826 return s
->faked_sectors
+ s
->sectors_per_cluster
* cluster_num
;
829 static int init_directories(BDRVVVFATState
* s
,
832 bootsector_t
* bootsector
;
835 unsigned int cluster
;
837 memset(&(s
->first_sectors
[0]),0,0x40*0x200);
839 s
->cluster_size
=s
->sectors_per_cluster
*0x200;
840 s
->cluster_buffer
=g_malloc(s
->cluster_size
);
843 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
844 * where sc is sector_count,
845 * spf is sectors_per_fat,
846 * spc is sectors_per_clusters, and
847 * fat_type = 12, 16 or 32.
849 i
= 1+s
->sectors_per_cluster
*0x200*8/s
->fat_type
;
850 s
->sectors_per_fat
=(s
->sector_count
+i
)/i
; /* round up */
852 array_init(&(s
->mapping
),sizeof(mapping_t
));
853 array_init(&(s
->directory
),sizeof(direntry_t
));
855 /* add volume label */
857 direntry_t
* entry
=array_get_next(&(s
->directory
));
858 entry
->attributes
=0x28; /* archive | volume label */
859 memcpy(entry
->name
,"QEMU VVF",8);
860 memcpy(entry
->extension
,"AT ",3);
863 /* Now build FAT, and write back information into directory */
866 s
->faked_sectors
=s
->first_sectors_number
+s
->sectors_per_fat
*2;
867 s
->cluster_count
=sector2cluster(s
, s
->sector_count
);
869 mapping
= array_get_next(&(s
->mapping
));
871 mapping
->dir_index
= 0;
872 mapping
->info
.dir
.parent_mapping_index
= -1;
873 mapping
->first_mapping_index
= -1;
874 mapping
->path
= g_strdup(dirname
);
875 i
= strlen(mapping
->path
);
876 if (i
> 0 && mapping
->path
[i
- 1] == '/')
877 mapping
->path
[i
- 1] = '\0';
878 mapping
->mode
= MODE_DIRECTORY
;
879 mapping
->read_only
= 0;
880 s
->path
= mapping
->path
;
882 for (i
= 0, cluster
= 0; i
< s
->mapping
.next
; i
++) {
883 /* MS-DOS expects the FAT to be 0 for the root directory
884 * (except for the media byte). */
885 /* LATER TODO: still true for FAT32? */
886 int fix_fat
= (i
!= 0);
887 mapping
= array_get(&(s
->mapping
), i
);
889 if (mapping
->mode
& MODE_DIRECTORY
) {
890 mapping
->begin
= cluster
;
891 if(read_directory(s
, i
)) {
892 fprintf(stderr
, "Could not read directory %s\n",
896 mapping
= array_get(&(s
->mapping
), i
);
898 assert(mapping
->mode
== MODE_UNDEFINED
);
899 mapping
->mode
=MODE_NORMAL
;
900 mapping
->begin
= cluster
;
901 if (mapping
->end
> 0) {
902 direntry_t
* direntry
= array_get(&(s
->directory
),
905 mapping
->end
= cluster
+ 1 + (mapping
->end
-1)/s
->cluster_size
;
906 set_begin_of_direntry(direntry
, mapping
->begin
);
908 mapping
->end
= cluster
+ 1;
913 assert(mapping
->begin
< mapping
->end
);
915 /* next free cluster */
916 cluster
= mapping
->end
;
918 if(cluster
> s
->cluster_count
) {
919 fprintf(stderr
,"Directory does not fit in FAT%d (capacity %.2f MB)\n",
920 s
->fat_type
, s
->sector_count
/ 2000.0);
924 /* fix fat for entry */
927 for(j
= mapping
->begin
; j
< mapping
->end
- 1; j
++)
929 fat_set(s
, mapping
->end
- 1, s
->max_fat_value
);
933 mapping
= array_get(&(s
->mapping
), 0);
934 s
->sectors_of_root_directory
= mapping
->end
* s
->sectors_per_cluster
;
935 s
->last_cluster_of_root_directory
= mapping
->end
;
937 /* the FAT signature */
938 fat_set(s
,0,s
->max_fat_value
);
939 fat_set(s
,1,s
->max_fat_value
);
941 s
->current_mapping
= NULL
;
943 bootsector
=(bootsector_t
*)(s
->first_sectors
+(s
->first_sectors_number
-1)*0x200);
944 bootsector
->jump
[0]=0xeb;
945 bootsector
->jump
[1]=0x3e;
946 bootsector
->jump
[2]=0x90;
947 memcpy(bootsector
->name
,"QEMU ",8);
948 bootsector
->sector_size
=cpu_to_le16(0x200);
949 bootsector
->sectors_per_cluster
=s
->sectors_per_cluster
;
950 bootsector
->reserved_sectors
=cpu_to_le16(1);
951 bootsector
->number_of_fats
=0x2; /* number of FATs */
952 bootsector
->root_entries
=cpu_to_le16(s
->sectors_of_root_directory
*0x10);
953 bootsector
->total_sectors16
=s
->sector_count
>0xffff?0:cpu_to_le16(s
->sector_count
);
954 bootsector
->media_type
=(s
->first_sectors_number
>1?0xf8:0xf0); /* media descriptor (f8=hd, f0=3.5 fd)*/
955 s
->fat
.pointer
[0] = bootsector
->media_type
;
956 bootsector
->sectors_per_fat
=cpu_to_le16(s
->sectors_per_fat
);
957 bootsector
->sectors_per_track
=cpu_to_le16(s
->bs
->secs
);
958 bootsector
->number_of_heads
=cpu_to_le16(s
->bs
->heads
);
959 bootsector
->hidden_sectors
=cpu_to_le32(s
->first_sectors_number
==1?0:0x3f);
960 bootsector
->total_sectors
=cpu_to_le32(s
->sector_count
>0xffff?s
->sector_count
:0);
962 /* LATER TODO: if FAT32, this is wrong */
963 bootsector
->u
.fat16
.drive_number
=s
->first_sectors_number
==1?0:0x80; /* fda=0, hda=0x80 */
964 bootsector
->u
.fat16
.current_head
=0;
965 bootsector
->u
.fat16
.signature
=0x29;
966 bootsector
->u
.fat16
.id
=cpu_to_le32(0xfabe1afd);
968 memcpy(bootsector
->u
.fat16
.volume_label
,"QEMU VVFAT ",11);
969 memcpy(bootsector
->fat_type
,(s
->fat_type
==12?"FAT12 ":s
->fat_type
==16?"FAT16 ":"FAT32 "),8);
970 bootsector
->magic
[0]=0x55; bootsector
->magic
[1]=0xaa;
976 static BDRVVVFATState
*vvv
= NULL
;
979 static int enable_write_target(BDRVVVFATState
*s
);
980 static int is_consistent(BDRVVVFATState
*s
);
982 static int vvfat_open(BlockDriverState
*bs
, const char* dirname
, int flags
)
984 BDRVVVFATState
*s
= bs
->opaque
;
992 DLOG(if (stderr
== NULL
) {
993 stderr
= fopen("vvfat.log", "a");
994 setbuf(stderr
, NULL
);
1000 /* LATER TODO: if FAT32, adjust */
1001 s
->sectors_per_cluster
=0x10;
1003 bs
->cyls
=1024; bs
->heads
=16; bs
->secs
=63;
1005 s
->current_cluster
=0xffffffff;
1007 s
->first_sectors_number
=0x40;
1008 /* read only is the default for safety */
1010 s
->qcow
= s
->write_target
= NULL
;
1011 s
->qcow_filename
= NULL
;
1013 s
->downcase_short_names
= 1;
1015 if (!strstart(dirname
, "fat:", NULL
))
1018 if (strstr(dirname
, ":floppy:")) {
1021 s
->first_sectors_number
= 1;
1022 s
->sectors_per_cluster
=2;
1023 bs
->cyls
= 80; bs
->heads
= 2; bs
->secs
= 36;
1026 if (strstr(dirname
, ":32:")) {
1027 fprintf(stderr
, "Big fat greek warning: FAT32 has not been tested. You are welcome to do so!\n");
1029 } else if (strstr(dirname
, ":16:")) {
1031 } else if (strstr(dirname
, ":12:")) {
1036 s
->sector_count
=bs
->cyls
*bs
->heads
*bs
->secs
-(s
->first_sectors_number
-1);
1038 if (strstr(dirname
, ":rw:")) {
1039 if (enable_write_target(s
))
1044 i
= strrchr(dirname
, ':') - dirname
;
1046 if (dirname
[i
-2] == ':' && qemu_isalpha(dirname
[i
-1]))
1047 /* workaround for DOS drive names */
1052 bs
->total_sectors
=bs
->cyls
*bs
->heads
*bs
->secs
;
1054 if(init_directories(s
, dirname
))
1057 s
->sector_count
= s
->faked_sectors
+ s
->sectors_per_cluster
*s
->cluster_count
;
1059 if(s
->first_sectors_number
==0x40)
1062 /* for some reason or other, MS-DOS does not like to know about CHS... */
1064 bs
->heads
= bs
->cyls
= bs
->secs
= 0;
1066 // assert(is_consistent(s));
1067 qemu_co_mutex_init(&s
->lock
);
1071 static inline void vvfat_close_current_file(BDRVVVFATState
*s
)
1073 if(s
->current_mapping
) {
1074 s
->current_mapping
= NULL
;
1075 if (s
->current_fd
) {
1076 close(s
->current_fd
);
1080 s
->current_cluster
= -1;
1083 /* mappings between index1 and index2-1 are supposed to be ordered
1084 * return value is the index of the last mapping for which end>cluster_num
1086 static inline int find_mapping_for_cluster_aux(BDRVVVFATState
* s
,int cluster_num
,int index1
,int index2
)
1091 index3
=(index1
+index2
)/2;
1092 mapping
=array_get(&(s
->mapping
),index3
);
1093 assert(mapping
->begin
< mapping
->end
);
1094 if(mapping
->begin
>=cluster_num
) {
1095 assert(index2
!=index3
|| index2
==0);
1101 return mapping
->end
<=cluster_num
? index2
: index1
;
1104 assert(index1
<=index2
);
1105 DLOG(mapping
=array_get(&(s
->mapping
),index1
);
1106 assert(mapping
->begin
<=cluster_num
);
1107 assert(index2
>= s
->mapping
.next
||
1108 ((mapping
= array_get(&(s
->mapping
),index2
)) &&
1109 mapping
->end
>cluster_num
)));
1113 static inline mapping_t
* find_mapping_for_cluster(BDRVVVFATState
* s
,int cluster_num
)
1115 int index
=find_mapping_for_cluster_aux(s
,cluster_num
,0,s
->mapping
.next
);
1117 if(index
>=s
->mapping
.next
)
1119 mapping
=array_get(&(s
->mapping
),index
);
1120 if(mapping
->begin
>cluster_num
)
1122 assert(mapping
->begin
<=cluster_num
&& mapping
->end
>cluster_num
);
1126 static int open_file(BDRVVVFATState
* s
,mapping_t
* mapping
)
1130 if(!s
->current_mapping
||
1131 strcmp(s
->current_mapping
->path
,mapping
->path
)) {
1133 int fd
= open(mapping
->path
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1136 vvfat_close_current_file(s
);
1138 s
->current_mapping
= mapping
;
1143 static inline int read_cluster(BDRVVVFATState
*s
,int cluster_num
)
1145 if(s
->current_cluster
!= cluster_num
) {
1148 assert(!s
->current_mapping
|| s
->current_fd
|| (s
->current_mapping
->mode
& MODE_DIRECTORY
));
1149 if(!s
->current_mapping
1150 || s
->current_mapping
->begin
>cluster_num
1151 || s
->current_mapping
->end
<=cluster_num
) {
1152 /* binary search of mappings for file */
1153 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
1155 assert(!mapping
|| (cluster_num
>=mapping
->begin
&& cluster_num
<mapping
->end
));
1157 if (mapping
&& mapping
->mode
& MODE_DIRECTORY
) {
1158 vvfat_close_current_file(s
);
1159 s
->current_mapping
= mapping
;
1160 read_cluster_directory
:
1161 offset
= s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
);
1162 s
->cluster
= (unsigned char*)s
->directory
.pointer
+offset
1163 + 0x20*s
->current_mapping
->info
.dir
.first_dir_index
;
1164 assert(((s
->cluster
-(unsigned char*)s
->directory
.pointer
)%s
->cluster_size
)==0);
1165 assert((char*)s
->cluster
+s
->cluster_size
<= s
->directory
.pointer
+s
->directory
.next
*s
->directory
.item_size
);
1166 s
->current_cluster
= cluster_num
;
1170 if(open_file(s
,mapping
))
1172 } else if (s
->current_mapping
->mode
& MODE_DIRECTORY
)
1173 goto read_cluster_directory
;
1175 assert(s
->current_fd
);
1177 offset
=s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
)+s
->current_mapping
->info
.file
.offset
;
1178 if(lseek(s
->current_fd
, offset
, SEEK_SET
)!=offset
)
1180 s
->cluster
=s
->cluster_buffer
;
1181 result
=read(s
->current_fd
,s
->cluster
,s
->cluster_size
);
1183 s
->current_cluster
= -1;
1186 s
->current_cluster
= cluster_num
;
1192 static void print_direntry(const direntry_t
* direntry
)
1197 fprintf(stderr
, "direntry %p: ", direntry
);
1200 if(is_long_name(direntry
)) {
1201 unsigned char* c
=(unsigned char*)direntry
;
1203 for(i
=1;i
<11 && c
[i
] && c
[i
]!=0xff;i
+=2)
1204 #define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
1206 for(i
=14;i
<26 && c
[i
] && c
[i
]!=0xff;i
+=2)
1208 for(i
=28;i
<32 && c
[i
] && c
[i
]!=0xff;i
+=2)
1211 fprintf(stderr
, "%s\n", buffer
);
1215 ADD_CHAR(direntry
->name
[i
]);
1217 fprintf(stderr
,"%s attributes=0x%02x begin=%d size=%d\n",
1219 direntry
->attributes
,
1220 begin_of_direntry(direntry
),le32_to_cpu(direntry
->size
));
1224 static void print_mapping(const mapping_t
* mapping
)
1226 fprintf(stderr
, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1227 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1228 mapping
, mapping
->begin
, mapping
->end
, mapping
->dir_index
,
1229 mapping
->first_mapping_index
, mapping
->path
, mapping
->mode
);
1231 if (mapping
->mode
& MODE_DIRECTORY
)
1232 fprintf(stderr
, "parent_mapping_index = %d, first_dir_index = %d\n", mapping
->info
.dir
.parent_mapping_index
, mapping
->info
.dir
.first_dir_index
);
1234 fprintf(stderr
, "offset = %d\n", mapping
->info
.file
.offset
);
1238 static int vvfat_read(BlockDriverState
*bs
, int64_t sector_num
,
1239 uint8_t *buf
, int nb_sectors
)
1241 BDRVVVFATState
*s
= bs
->opaque
;
1244 for(i
=0;i
<nb_sectors
;i
++,sector_num
++) {
1245 if (sector_num
>= bs
->total_sectors
)
1249 if (s
->qcow
->drv
->bdrv_is_allocated(s
->qcow
,
1250 sector_num
, nb_sectors
-i
, &n
)) {
1251 DLOG(fprintf(stderr
, "sectors %d+%d allocated\n", (int)sector_num
, n
));
1252 if (s
->qcow
->drv
->bdrv_read(s
->qcow
, sector_num
, buf
+i
*0x200, n
))
1255 sector_num
+= n
- 1;
1258 DLOG(fprintf(stderr
, "sector %d not allocated\n", (int)sector_num
));
1260 if(sector_num
<s
->faked_sectors
) {
1261 if(sector_num
<s
->first_sectors_number
)
1262 memcpy(buf
+i
*0x200,&(s
->first_sectors
[sector_num
*0x200]),0x200);
1263 else if(sector_num
-s
->first_sectors_number
<s
->sectors_per_fat
)
1264 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
)*0x200]),0x200);
1265 else if(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
<s
->sectors_per_fat
)
1266 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
)*0x200]),0x200);
1268 uint32_t sector
=sector_num
-s
->faked_sectors
,
1269 sector_offset_in_cluster
=(sector
%s
->sectors_per_cluster
),
1270 cluster_num
=sector
/s
->sectors_per_cluster
;
1271 if(cluster_num
> s
->cluster_count
|| read_cluster(s
, cluster_num
) != 0) {
1272 /* LATER TODO: strict: return -1; */
1273 memset(buf
+i
*0x200,0,0x200);
1276 memcpy(buf
+i
*0x200,s
->cluster
+sector_offset_in_cluster
*0x200,0x200);
1282 static coroutine_fn
int vvfat_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1283 uint8_t *buf
, int nb_sectors
)
1286 BDRVVVFATState
*s
= bs
->opaque
;
1287 qemu_co_mutex_lock(&s
->lock
);
1288 ret
= vvfat_read(bs
, sector_num
, buf
, nb_sectors
);
1289 qemu_co_mutex_unlock(&s
->lock
);
1293 /* LATER TODO: statify all functions */
1296 * Idea of the write support (use snapshot):
1298 * 1. check if all data is consistent, recording renames, modifications,
1299 * new files and directories (in s->commits).
1301 * 2. if the data is not consistent, stop committing
1303 * 3. handle renames, and create new files and directories (do not yet
1304 * write their contents)
1306 * 4. walk the directories, fixing the mapping and direntries, and marking
1307 * the handled mappings as not deleted
1309 * 5. commit the contents of the files
1311 * 6. handle deleted files and directories
1315 typedef struct commit_t
{
1318 struct { uint32_t cluster
; } rename
;
1319 struct { int dir_index
; uint32_t modified_offset
; } writeout
;
1320 struct { uint32_t first_cluster
; } new_file
;
1321 struct { uint32_t cluster
; } mkdir
;
1323 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1325 ACTION_RENAME
, ACTION_WRITEOUT
, ACTION_NEW_FILE
, ACTION_MKDIR
1329 static void clear_commits(BDRVVVFATState
* s
)
1332 DLOG(fprintf(stderr
, "clear_commits (%d commits)\n", s
->commits
.next
));
1333 for (i
= 0; i
< s
->commits
.next
; i
++) {
1334 commit_t
* commit
= array_get(&(s
->commits
), i
);
1335 assert(commit
->path
|| commit
->action
== ACTION_WRITEOUT
);
1336 if (commit
->action
!= ACTION_WRITEOUT
) {
1337 assert(commit
->path
);
1338 g_free(commit
->path
);
1340 assert(commit
->path
== NULL
);
1342 s
->commits
.next
= 0;
1345 static void schedule_rename(BDRVVVFATState
* s
,
1346 uint32_t cluster
, char* new_path
)
1348 commit_t
* commit
= array_get_next(&(s
->commits
));
1349 commit
->path
= new_path
;
1350 commit
->param
.rename
.cluster
= cluster
;
1351 commit
->action
= ACTION_RENAME
;
1354 static void schedule_writeout(BDRVVVFATState
* s
,
1355 int dir_index
, uint32_t modified_offset
)
1357 commit_t
* commit
= array_get_next(&(s
->commits
));
1358 commit
->path
= NULL
;
1359 commit
->param
.writeout
.dir_index
= dir_index
;
1360 commit
->param
.writeout
.modified_offset
= modified_offset
;
1361 commit
->action
= ACTION_WRITEOUT
;
1364 static void schedule_new_file(BDRVVVFATState
* s
,
1365 char* path
, uint32_t first_cluster
)
1367 commit_t
* commit
= array_get_next(&(s
->commits
));
1368 commit
->path
= path
;
1369 commit
->param
.new_file
.first_cluster
= first_cluster
;
1370 commit
->action
= ACTION_NEW_FILE
;
1373 static void schedule_mkdir(BDRVVVFATState
* s
, uint32_t cluster
, char* path
)
1375 commit_t
* commit
= array_get_next(&(s
->commits
));
1376 commit
->path
= path
;
1377 commit
->param
.mkdir
.cluster
= cluster
;
1378 commit
->action
= ACTION_MKDIR
;
1383 * Since the sequence number is at most 0x3f, and the filename
1384 * length is at most 13 times the sequence number, the maximal
1385 * filename length is 0x3f * 13 bytes.
1387 unsigned char name
[0x3f * 13 + 1];
1389 int sequence_number
;
1392 static void lfn_init(long_file_name
* lfn
)
1394 lfn
->sequence_number
= lfn
->len
= 0;
1395 lfn
->checksum
= 0x100;
1398 /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1399 static int parse_long_name(long_file_name
* lfn
,
1400 const direntry_t
* direntry
)
1403 const unsigned char* pointer
= (const unsigned char*)direntry
;
1405 if (!is_long_name(direntry
))
1408 if (pointer
[0] & 0x40) {
1409 lfn
->sequence_number
= pointer
[0] & 0x3f;
1410 lfn
->checksum
= pointer
[13];
1412 lfn
->name
[lfn
->sequence_number
* 13] = 0;
1413 } else if ((pointer
[0] & 0x3f) != --lfn
->sequence_number
)
1415 else if (pointer
[13] != lfn
->checksum
)
1417 else if (pointer
[12] || pointer
[26] || pointer
[27])
1420 offset
= 13 * (lfn
->sequence_number
- 1);
1421 for (i
= 0, j
= 1; i
< 13; i
++, j
+=2) {
1427 if (pointer
[j
+1] == 0)
1428 lfn
->name
[offset
+ i
] = pointer
[j
];
1429 else if (pointer
[j
+1] != 0xff || (pointer
[0] & 0x40) == 0)
1432 lfn
->name
[offset
+ i
] = 0;
1435 if (pointer
[0] & 0x40)
1436 lfn
->len
= offset
+ strlen((char*)lfn
->name
+ offset
);
1441 /* returns 0 if successful, >0 if no short_name, and <0 on error */
1442 static int parse_short_name(BDRVVVFATState
* s
,
1443 long_file_name
* lfn
, direntry_t
* direntry
)
1447 if (!is_short_name(direntry
))
1450 for (j
= 7; j
>= 0 && direntry
->name
[j
] == ' '; j
--);
1451 for (i
= 0; i
<= j
; i
++) {
1452 if (direntry
->name
[i
] <= ' ' || direntry
->name
[i
] > 0x7f)
1454 else if (s
->downcase_short_names
)
1455 lfn
->name
[i
] = qemu_tolower(direntry
->name
[i
]);
1457 lfn
->name
[i
] = direntry
->name
[i
];
1460 for (j
= 2; j
>= 0 && direntry
->extension
[j
] == ' '; j
--);
1462 lfn
->name
[i
++] = '.';
1463 lfn
->name
[i
+ j
+ 1] = '\0';
1464 for (;j
>= 0; j
--) {
1465 if (direntry
->extension
[j
] <= ' ' || direntry
->extension
[j
] > 0x7f)
1467 else if (s
->downcase_short_names
)
1468 lfn
->name
[i
+ j
] = qemu_tolower(direntry
->extension
[j
]);
1470 lfn
->name
[i
+ j
] = direntry
->extension
[j
];
1473 lfn
->name
[i
+ j
+ 1] = '\0';
1475 lfn
->len
= strlen((char*)lfn
->name
);
1480 static inline uint32_t modified_fat_get(BDRVVVFATState
* s
,
1481 unsigned int cluster
)
1483 if (cluster
< s
->last_cluster_of_root_directory
) {
1484 if (cluster
+ 1 == s
->last_cluster_of_root_directory
)
1485 return s
->max_fat_value
;
1490 if (s
->fat_type
==32) {
1491 uint32_t* entry
=((uint32_t*)s
->fat2
)+cluster
;
1492 return le32_to_cpu(*entry
);
1493 } else if (s
->fat_type
==16) {
1494 uint16_t* entry
=((uint16_t*)s
->fat2
)+cluster
;
1495 return le16_to_cpu(*entry
);
1497 const uint8_t* x
=s
->fat2
+cluster
*3/2;
1498 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
1502 static inline int cluster_was_modified(BDRVVVFATState
* s
, uint32_t cluster_num
)
1504 int was_modified
= 0;
1507 if (s
->qcow
== NULL
)
1510 for (i
= 0; !was_modified
&& i
< s
->sectors_per_cluster
; i
++)
1511 was_modified
= s
->qcow
->drv
->bdrv_is_allocated(s
->qcow
,
1512 cluster2sector(s
, cluster_num
) + i
, 1, &dummy
);
1514 return was_modified
;
1517 static const char* get_basename(const char* path
)
1519 char* basename
= strrchr(path
, '/');
1520 if (basename
== NULL
)
1523 return basename
+ 1; /* strip '/' */
1527 * The array s->used_clusters holds the states of the clusters. If it is
1528 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1529 * was modified, bit 3 is set.
1530 * If any cluster is allocated, but not part of a file or directory, this
1531 * driver refuses to commit.
1534 USED_DIRECTORY
= 1, USED_FILE
= 2, USED_ANY
= 3, USED_ALLOCATED
= 4
1538 * get_cluster_count_for_direntry() not only determines how many clusters
1539 * are occupied by direntry, but also if it was renamed or modified.
1541 * A file is thought to be renamed *only* if there already was a file with
1542 * exactly the same first cluster, but a different name.
1544 * Further, the files/directories handled by this function are
1545 * assumed to be *not* deleted (and *only* those).
1547 static uint32_t get_cluster_count_for_direntry(BDRVVVFATState
* s
,
1548 direntry_t
* direntry
, const char* path
)
1551 * This is a little bit tricky:
1552 * IF the guest OS just inserts a cluster into the file chain,
1553 * and leaves the rest alone, (i.e. the original file had clusters
1554 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1556 * - do_commit will write the cluster into the file at the given
1559 * - the cluster which is overwritten should be moved to a later
1560 * position in the file.
1562 * I am not aware that any OS does something as braindead, but this
1563 * situation could happen anyway when not committing for a long time.
1564 * Just to be sure that this does not bite us, detect it, and copy the
1565 * contents of the clusters to-be-overwritten into the qcow.
1568 int was_modified
= 0;
1571 uint32_t cluster_num
= begin_of_direntry(direntry
);
1572 uint32_t offset
= 0;
1573 int first_mapping_index
= -1;
1574 mapping_t
* mapping
= NULL
;
1575 const char* basename2
= NULL
;
1577 vvfat_close_current_file(s
);
1579 /* the root directory */
1580 if (cluster_num
== 0)
1585 basename2
= get_basename(path
);
1587 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1590 const char* basename
;
1592 assert(mapping
->mode
& MODE_DELETED
);
1593 mapping
->mode
&= ~MODE_DELETED
;
1595 basename
= get_basename(mapping
->path
);
1597 assert(mapping
->mode
& MODE_NORMAL
);
1600 if (strcmp(basename
, basename2
))
1601 schedule_rename(s
, cluster_num
, g_strdup(path
));
1602 } else if (is_file(direntry
))
1604 schedule_new_file(s
, g_strdup(path
), cluster_num
);
1613 if (!copy_it
&& cluster_was_modified(s
, cluster_num
)) {
1614 if (mapping
== NULL
||
1615 mapping
->begin
> cluster_num
||
1616 mapping
->end
<= cluster_num
)
1617 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1621 (mapping
->mode
& MODE_DIRECTORY
) == 0) {
1623 /* was modified in qcow */
1624 if (offset
!= mapping
->info
.file
.offset
+ s
->cluster_size
1625 * (cluster_num
- mapping
->begin
)) {
1626 /* offset of this cluster in file chain has changed */
1629 } else if (offset
== 0) {
1630 const char* basename
= get_basename(mapping
->path
);
1632 if (strcmp(basename
, basename2
))
1634 first_mapping_index
= array_index(&(s
->mapping
), mapping
);
1637 if (mapping
->first_mapping_index
!= first_mapping_index
1638 && mapping
->info
.file
.offset
> 0) {
1643 /* need to write out? */
1644 if (!was_modified
&& is_file(direntry
)) {
1646 schedule_writeout(s
, mapping
->dir_index
, offset
);
1654 * This is horribly inefficient, but that is okay, since
1655 * it is rarely executed, if at all.
1657 int64_t offset
= cluster2sector(s
, cluster_num
);
1659 vvfat_close_current_file(s
);
1660 for (i
= 0; i
< s
->sectors_per_cluster
; i
++)
1661 if (!s
->qcow
->drv
->bdrv_is_allocated(s
->qcow
,
1662 offset
+ i
, 1, &dummy
)) {
1663 if (vvfat_read(s
->bs
,
1664 offset
, s
->cluster_buffer
, 1))
1666 if (s
->qcow
->drv
->bdrv_write(s
->qcow
,
1667 offset
, s
->cluster_buffer
, 1))
1674 if (s
->used_clusters
[cluster_num
] & USED_ANY
)
1676 s
->used_clusters
[cluster_num
] = USED_FILE
;
1678 cluster_num
= modified_fat_get(s
, cluster_num
);
1680 if (fat_eof(s
, cluster_num
))
1682 else if (cluster_num
< 2 || cluster_num
> s
->max_fat_value
- 16)
1685 offset
+= s
->cluster_size
;
1690 * This function looks at the modified data (qcow).
1691 * It returns 0 upon inconsistency or error, and the number of clusters
1692 * used by the directory, its subdirectories and their files.
1694 static int check_directory_consistency(BDRVVVFATState
*s
,
1695 int cluster_num
, const char* path
)
1698 unsigned char* cluster
= g_malloc(s
->cluster_size
);
1699 direntry_t
* direntries
= (direntry_t
*)cluster
;
1700 mapping_t
* mapping
= find_mapping_for_cluster(s
, cluster_num
);
1703 int path_len
= strlen(path
);
1704 char path2
[PATH_MAX
+ 1];
1706 assert(path_len
< PATH_MAX
); /* len was tested before! */
1707 pstrcpy(path2
, sizeof(path2
), path
);
1708 path2
[path_len
] = '/';
1709 path2
[path_len
+ 1] = '\0';
1712 const char* basename
= get_basename(mapping
->path
);
1713 const char* basename2
= get_basename(path
);
1715 assert(mapping
->mode
& MODE_DIRECTORY
);
1717 assert(mapping
->mode
& MODE_DELETED
);
1718 mapping
->mode
&= ~MODE_DELETED
;
1720 if (strcmp(basename
, basename2
))
1721 schedule_rename(s
, cluster_num
, g_strdup(path
));
1724 schedule_mkdir(s
, cluster_num
, g_strdup(path
));
1733 if (s
->used_clusters
[cluster_num
] & USED_ANY
) {
1734 fprintf(stderr
, "cluster %d used more than once\n", (int)cluster_num
);
1737 s
->used_clusters
[cluster_num
] = USED_DIRECTORY
;
1739 DLOG(fprintf(stderr
, "read cluster %d (sector %d)\n", (int)cluster_num
, (int)cluster2sector(s
, cluster_num
)));
1740 subret
= vvfat_read(s
->bs
, cluster2sector(s
, cluster_num
), cluster
,
1741 s
->sectors_per_cluster
);
1743 fprintf(stderr
, "Error fetching direntries\n");
1749 for (i
= 0; i
< 0x10 * s
->sectors_per_cluster
; i
++) {
1750 int cluster_count
= 0;
1752 DLOG(fprintf(stderr
, "check direntry %d:\n", i
); print_direntry(direntries
+ i
));
1753 if (is_volume_label(direntries
+ i
) || is_dot(direntries
+ i
) ||
1754 is_free(direntries
+ i
))
1757 subret
= parse_long_name(&lfn
, direntries
+ i
);
1759 fprintf(stderr
, "Error in long name\n");
1762 if (subret
== 0 || is_free(direntries
+ i
))
1765 if (fat_chksum(direntries
+i
) != lfn
.checksum
) {
1766 subret
= parse_short_name(s
, &lfn
, direntries
+ i
);
1768 fprintf(stderr
, "Error in short name (%d)\n", subret
);
1771 if (subret
> 0 || !strcmp((char*)lfn
.name
, ".")
1772 || !strcmp((char*)lfn
.name
, ".."))
1775 lfn
.checksum
= 0x100; /* cannot use long name twice */
1777 if (path_len
+ 1 + lfn
.len
>= PATH_MAX
) {
1778 fprintf(stderr
, "Name too long: %s/%s\n", path
, lfn
.name
);
1781 pstrcpy(path2
+ path_len
+ 1, sizeof(path2
) - path_len
- 1,
1784 if (is_directory(direntries
+ i
)) {
1785 if (begin_of_direntry(direntries
+ i
) == 0) {
1786 DLOG(fprintf(stderr
, "invalid begin for directory: %s\n", path2
); print_direntry(direntries
+ i
));
1789 cluster_count
= check_directory_consistency(s
,
1790 begin_of_direntry(direntries
+ i
), path2
);
1791 if (cluster_count
== 0) {
1792 DLOG(fprintf(stderr
, "problem in directory %s:\n", path2
); print_direntry(direntries
+ i
));
1795 } else if (is_file(direntries
+ i
)) {
1796 /* check file size with FAT */
1797 cluster_count
= get_cluster_count_for_direntry(s
, direntries
+ i
, path2
);
1798 if (cluster_count
!=
1799 (le32_to_cpu(direntries
[i
].size
) + s
->cluster_size
1800 - 1) / s
->cluster_size
) {
1801 DLOG(fprintf(stderr
, "Cluster count mismatch\n"));
1805 abort(); /* cluster_count = 0; */
1807 ret
+= cluster_count
;
1810 cluster_num
= modified_fat_get(s
, cluster_num
);
1811 } while(!fat_eof(s
, cluster_num
));
1817 /* returns 1 on success */
1818 static int is_consistent(BDRVVVFATState
* s
)
1821 int used_clusters_count
= 0;
1825 * - get modified FAT
1826 * - compare the two FATs (TODO)
1827 * - get buffer for marking used clusters
1828 * - recurse direntries from root (using bs->bdrv_read to make
1829 * sure to get the new data)
1830 * - check that the FAT agrees with the size
1831 * - count the number of clusters occupied by this directory and
1833 * - check that the cumulative used cluster count agrees with the
1835 * - if all is fine, return number of used clusters
1837 if (s
->fat2
== NULL
) {
1838 int size
= 0x200 * s
->sectors_per_fat
;
1839 s
->fat2
= g_malloc(size
);
1840 memcpy(s
->fat2
, s
->fat
.pointer
, size
);
1842 check
= vvfat_read(s
->bs
,
1843 s
->first_sectors_number
, s
->fat2
, s
->sectors_per_fat
);
1845 fprintf(stderr
, "Could not copy fat\n");
1848 assert (s
->used_clusters
);
1849 for (i
= 0; i
< sector2cluster(s
, s
->sector_count
); i
++)
1850 s
->used_clusters
[i
] &= ~USED_ANY
;
1854 /* mark every mapped file/directory as deleted.
1855 * (check_directory_consistency() will unmark those still present). */
1857 for (i
= 0; i
< s
->mapping
.next
; i
++) {
1858 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
1859 if (mapping
->first_mapping_index
< 0)
1860 mapping
->mode
|= MODE_DELETED
;
1863 used_clusters_count
= check_directory_consistency(s
, 0, s
->path
);
1864 if (used_clusters_count
<= 0) {
1865 DLOG(fprintf(stderr
, "problem in directory\n"));
1869 check
= s
->last_cluster_of_root_directory
;
1870 for (i
= check
; i
< sector2cluster(s
, s
->sector_count
); i
++) {
1871 if (modified_fat_get(s
, i
)) {
1872 if(!s
->used_clusters
[i
]) {
1873 DLOG(fprintf(stderr
, "FAT was modified (%d), but cluster is not used?\n", i
));
1879 if (s
->used_clusters
[i
] == USED_ALLOCATED
) {
1880 /* allocated, but not used... */
1881 DLOG(fprintf(stderr
, "unused, modified cluster: %d\n", i
));
1886 if (check
!= used_clusters_count
)
1889 return used_clusters_count
;
1892 static inline void adjust_mapping_indices(BDRVVVFATState
* s
,
1893 int offset
, int adjust
)
1897 for (i
= 0; i
< s
->mapping
.next
; i
++) {
1898 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
1900 #define ADJUST_MAPPING_INDEX(name) \
1901 if (mapping->name >= offset) \
1902 mapping->name += adjust
1904 ADJUST_MAPPING_INDEX(first_mapping_index
);
1905 if (mapping
->mode
& MODE_DIRECTORY
)
1906 ADJUST_MAPPING_INDEX(info
.dir
.parent_mapping_index
);
1910 /* insert or update mapping */
1911 static mapping_t
* insert_mapping(BDRVVVFATState
* s
,
1912 uint32_t begin
, uint32_t end
)
1915 * - find mapping where mapping->begin >= begin,
1916 * - if mapping->begin > begin: insert
1917 * - adjust all references to mappings!
1921 int index
= find_mapping_for_cluster_aux(s
, begin
, 0, s
->mapping
.next
);
1922 mapping_t
* mapping
= NULL
;
1923 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
1925 if (index
< s
->mapping
.next
&& (mapping
= array_get(&(s
->mapping
), index
))
1926 && mapping
->begin
< begin
) {
1927 mapping
->end
= begin
;
1929 mapping
= array_get(&(s
->mapping
), index
);
1931 if (index
>= s
->mapping
.next
|| mapping
->begin
> begin
) {
1932 mapping
= array_insert(&(s
->mapping
), index
, 1);
1933 mapping
->path
= NULL
;
1934 adjust_mapping_indices(s
, index
, +1);
1937 mapping
->begin
= begin
;
1940 DLOG(mapping_t
* next_mapping
;
1941 assert(index
+ 1 >= s
->mapping
.next
||
1942 ((next_mapping
= array_get(&(s
->mapping
), index
+ 1)) &&
1943 next_mapping
->begin
>= end
)));
1945 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
1946 s
->current_mapping
= array_get(&(s
->mapping
),
1947 s
->current_mapping
- first_mapping
);
1952 static int remove_mapping(BDRVVVFATState
* s
, int mapping_index
)
1954 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
1955 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
1958 if (mapping
->first_mapping_index
< 0) {
1959 g_free(mapping
->path
);
1962 /* remove from s->mapping */
1963 array_remove(&(s
->mapping
), mapping_index
);
1965 /* adjust all references to mappings */
1966 adjust_mapping_indices(s
, mapping_index
, -1);
1968 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
1969 s
->current_mapping
= array_get(&(s
->mapping
),
1970 s
->current_mapping
- first_mapping
);
1975 static void adjust_dirindices(BDRVVVFATState
* s
, int offset
, int adjust
)
1978 for (i
= 0; i
< s
->mapping
.next
; i
++) {
1979 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
1980 if (mapping
->dir_index
>= offset
)
1981 mapping
->dir_index
+= adjust
;
1982 if ((mapping
->mode
& MODE_DIRECTORY
) &&
1983 mapping
->info
.dir
.first_dir_index
>= offset
)
1984 mapping
->info
.dir
.first_dir_index
+= adjust
;
1988 static direntry_t
* insert_direntries(BDRVVVFATState
* s
,
1989 int dir_index
, int count
)
1992 * make room in s->directory,
1995 direntry_t
* result
= array_insert(&(s
->directory
), dir_index
, count
);
1998 adjust_dirindices(s
, dir_index
, count
);
2002 static int remove_direntries(BDRVVVFATState
* s
, int dir_index
, int count
)
2004 int ret
= array_remove_slice(&(s
->directory
), dir_index
, count
);
2007 adjust_dirindices(s
, dir_index
, -count
);
2012 * Adapt the mappings of the cluster chain starting at first cluster
2013 * (i.e. if a file starts at first_cluster, the chain is followed according
2014 * to the modified fat, and the corresponding entries in s->mapping are
2017 static int commit_mappings(BDRVVVFATState
* s
,
2018 uint32_t first_cluster
, int dir_index
)
2020 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2021 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2022 uint32_t cluster
= first_cluster
;
2024 vvfat_close_current_file(s
);
2027 assert(mapping
->begin
== first_cluster
);
2028 mapping
->first_mapping_index
= -1;
2029 mapping
->dir_index
= dir_index
;
2030 mapping
->mode
= (dir_index
<= 0 || is_directory(direntry
)) ?
2031 MODE_DIRECTORY
: MODE_NORMAL
;
2033 while (!fat_eof(s
, cluster
)) {
2036 for (c
= cluster
, c1
= modified_fat_get(s
, c
); c
+ 1 == c1
;
2037 c
= c1
, c1
= modified_fat_get(s
, c1
));
2040 if (c
> mapping
->end
) {
2041 int index
= array_index(&(s
->mapping
), mapping
);
2042 int i
, max_i
= s
->mapping
.next
- index
;
2043 for (i
= 1; i
< max_i
&& mapping
[i
].begin
< c
; i
++);
2045 remove_mapping(s
, index
+ 1);
2047 assert(mapping
== array_get(&(s
->mapping
), s
->mapping
.next
- 1)
2048 || mapping
[1].begin
>= c
);
2051 if (!fat_eof(s
, c1
)) {
2052 int i
= find_mapping_for_cluster_aux(s
, c1
, 0, s
->mapping
.next
);
2053 mapping_t
* next_mapping
= i
>= s
->mapping
.next
? NULL
:
2054 array_get(&(s
->mapping
), i
);
2056 if (next_mapping
== NULL
|| next_mapping
->begin
> c1
) {
2057 int i1
= array_index(&(s
->mapping
), mapping
);
2059 next_mapping
= insert_mapping(s
, c1
, c1
+1);
2063 mapping
= array_get(&(s
->mapping
), i1
);
2066 next_mapping
->dir_index
= mapping
->dir_index
;
2067 next_mapping
->first_mapping_index
=
2068 mapping
->first_mapping_index
< 0 ?
2069 array_index(&(s
->mapping
), mapping
) :
2070 mapping
->first_mapping_index
;
2071 next_mapping
->path
= mapping
->path
;
2072 next_mapping
->mode
= mapping
->mode
;
2073 next_mapping
->read_only
= mapping
->read_only
;
2074 if (mapping
->mode
& MODE_DIRECTORY
) {
2075 next_mapping
->info
.dir
.parent_mapping_index
=
2076 mapping
->info
.dir
.parent_mapping_index
;
2077 next_mapping
->info
.dir
.first_dir_index
=
2078 mapping
->info
.dir
.first_dir_index
+
2079 0x10 * s
->sectors_per_cluster
*
2080 (mapping
->end
- mapping
->begin
);
2082 next_mapping
->info
.file
.offset
= mapping
->info
.file
.offset
+
2083 mapping
->end
- mapping
->begin
;
2085 mapping
= next_mapping
;
2094 static int commit_direntries(BDRVVVFATState
* s
,
2095 int dir_index
, int parent_mapping_index
)
2097 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2098 uint32_t first_cluster
= dir_index
== 0 ? 0 : begin_of_direntry(direntry
);
2099 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2101 int factor
= 0x10 * s
->sectors_per_cluster
;
2102 int old_cluster_count
, new_cluster_count
;
2103 int current_dir_index
= mapping
->info
.dir
.first_dir_index
;
2104 int first_dir_index
= current_dir_index
;
2108 DLOG(fprintf(stderr
, "commit_direntries for %s, parent_mapping_index %d\n", mapping
->path
, parent_mapping_index
));
2112 assert(mapping
->begin
== first_cluster
);
2113 assert(mapping
->info
.dir
.first_dir_index
< s
->directory
.next
);
2114 assert(mapping
->mode
& MODE_DIRECTORY
);
2115 assert(dir_index
== 0 || is_directory(direntry
));
2117 mapping
->info
.dir
.parent_mapping_index
= parent_mapping_index
;
2119 if (first_cluster
== 0) {
2120 old_cluster_count
= new_cluster_count
=
2121 s
->last_cluster_of_root_directory
;
2123 for (old_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2125 old_cluster_count
++;
2127 for (new_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2128 c
= modified_fat_get(s
, c
))
2129 new_cluster_count
++;
2132 if (new_cluster_count
> old_cluster_count
) {
2133 if (insert_direntries(s
,
2134 current_dir_index
+ factor
* old_cluster_count
,
2135 factor
* (new_cluster_count
- old_cluster_count
)) == NULL
)
2137 } else if (new_cluster_count
< old_cluster_count
)
2138 remove_direntries(s
,
2139 current_dir_index
+ factor
* new_cluster_count
,
2140 factor
* (old_cluster_count
- new_cluster_count
));
2142 for (c
= first_cluster
; !fat_eof(s
, c
); c
= modified_fat_get(s
, c
)) {
2143 void* direntry
= array_get(&(s
->directory
), current_dir_index
);
2144 int ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
), direntry
,
2145 s
->sectors_per_cluster
);
2148 assert(!strncmp(s
->directory
.pointer
, "QEMU", 4));
2149 current_dir_index
+= factor
;
2152 ret
= commit_mappings(s
, first_cluster
, dir_index
);
2157 for (i
= 0; i
< factor
* new_cluster_count
; i
++) {
2158 direntry
= array_get(&(s
->directory
), first_dir_index
+ i
);
2159 if (is_directory(direntry
) && !is_dot(direntry
)) {
2160 mapping
= find_mapping_for_cluster(s
, first_cluster
);
2161 assert(mapping
->mode
& MODE_DIRECTORY
);
2162 ret
= commit_direntries(s
, first_dir_index
+ i
,
2163 array_index(&(s
->mapping
), mapping
));
2172 /* commit one file (adjust contents, adjust mapping),
2173 return first_mapping_index */
2174 static int commit_one_file(BDRVVVFATState
* s
,
2175 int dir_index
, uint32_t offset
)
2177 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2178 uint32_t c
= begin_of_direntry(direntry
);
2179 uint32_t first_cluster
= c
;
2180 mapping_t
* mapping
= find_mapping_for_cluster(s
, c
);
2181 uint32_t size
= filesize_of_direntry(direntry
);
2182 char* cluster
= g_malloc(s
->cluster_size
);
2186 assert(offset
< size
);
2187 assert((offset
% s
->cluster_size
) == 0);
2189 for (i
= s
->cluster_size
; i
< offset
; i
+= s
->cluster_size
)
2190 c
= modified_fat_get(s
, c
);
2192 fd
= open(mapping
->path
, O_RDWR
| O_CREAT
| O_BINARY
, 0666);
2194 fprintf(stderr
, "Could not open %s... (%s, %d)\n", mapping
->path
,
2195 strerror(errno
), errno
);
2200 if (lseek(fd
, offset
, SEEK_SET
) != offset
) {
2206 while (offset
< size
) {
2208 int rest_size
= (size
- offset
> s
->cluster_size
?
2209 s
->cluster_size
: size
- offset
);
2212 c1
= modified_fat_get(s
, c
);
2214 assert((size
- offset
== 0 && fat_eof(s
, c
)) ||
2215 (size
> offset
&& c
>=2 && !fat_eof(s
, c
)));
2217 ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
),
2218 (uint8_t*)cluster
, (rest_size
+ 0x1ff) / 0x200);
2225 if (write(fd
, cluster
, rest_size
) < 0) {
2230 offset
+= rest_size
;
2234 if (ftruncate(fd
, size
)) {
2235 perror("ftruncate()");
2243 return commit_mappings(s
, first_cluster
, dir_index
);
2247 /* test, if all mappings point to valid direntries */
2248 static void check1(BDRVVVFATState
* s
)
2251 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2252 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2253 if (mapping
->mode
& MODE_DELETED
) {
2254 fprintf(stderr
, "deleted\n");
2257 assert(mapping
->dir_index
< s
->directory
.next
);
2258 direntry_t
* direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
2259 assert(mapping
->begin
== begin_of_direntry(direntry
) || mapping
->first_mapping_index
>= 0);
2260 if (mapping
->mode
& MODE_DIRECTORY
) {
2261 assert(mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
* (mapping
->end
- mapping
->begin
) <= s
->directory
.next
);
2262 assert((mapping
->info
.dir
.first_dir_index
% (0x10 * s
->sectors_per_cluster
)) == 0);
2267 /* test, if all direntries have mappings */
2268 static void check2(BDRVVVFATState
* s
)
2271 int first_mapping
= -1;
2273 for (i
= 0; i
< s
->directory
.next
; i
++) {
2274 direntry_t
* direntry
= array_get(&(s
->directory
), i
);
2276 if (is_short_name(direntry
) && begin_of_direntry(direntry
)) {
2277 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin_of_direntry(direntry
));
2279 assert(mapping
->dir_index
== i
|| is_dot(direntry
));
2280 assert(mapping
->begin
== begin_of_direntry(direntry
) || is_dot(direntry
));
2283 if ((i
% (0x10 * s
->sectors_per_cluster
)) == 0) {
2287 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2288 mapping_t
* mapping
= array_get(&(s
->mapping
), j
);
2289 if (mapping
->mode
& MODE_DELETED
)
2291 if (mapping
->mode
& MODE_DIRECTORY
) {
2292 if (mapping
->info
.dir
.first_dir_index
<= i
&& mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
> i
) {
2293 assert(++count
== 1);
2294 if (mapping
->first_mapping_index
== -1)
2295 first_mapping
= array_index(&(s
->mapping
), mapping
);
2297 assert(first_mapping
== mapping
->first_mapping_index
);
2298 if (mapping
->info
.dir
.parent_mapping_index
< 0)
2301 mapping_t
* parent
= array_get(&(s
->mapping
), mapping
->info
.dir
.parent_mapping_index
);
2302 assert(parent
->mode
& MODE_DIRECTORY
);
2303 assert(parent
->info
.dir
.first_dir_index
< mapping
->info
.dir
.first_dir_index
);
2315 static int handle_renames_and_mkdirs(BDRVVVFATState
* s
)
2320 fprintf(stderr
, "handle_renames\n");
2321 for (i
= 0; i
< s
->commits
.next
; i
++) {
2322 commit_t
* commit
= array_get(&(s
->commits
), i
);
2323 fprintf(stderr
, "%d, %s (%d, %d)\n", i
, commit
->path
? commit
->path
: "(null)", commit
->param
.rename
.cluster
, commit
->action
);
2327 for (i
= 0; i
< s
->commits
.next
;) {
2328 commit_t
* commit
= array_get(&(s
->commits
), i
);
2329 if (commit
->action
== ACTION_RENAME
) {
2330 mapping_t
* mapping
= find_mapping_for_cluster(s
,
2331 commit
->param
.rename
.cluster
);
2332 char* old_path
= mapping
->path
;
2334 assert(commit
->path
);
2335 mapping
->path
= commit
->path
;
2336 if (rename(old_path
, mapping
->path
))
2339 if (mapping
->mode
& MODE_DIRECTORY
) {
2340 int l1
= strlen(mapping
->path
);
2341 int l2
= strlen(old_path
);
2343 direntry_t
* direntry
= array_get(&(s
->directory
),
2344 mapping
->info
.dir
.first_dir_index
);
2345 uint32_t c
= mapping
->begin
;
2349 while (!fat_eof(s
, c
)) {
2351 direntry_t
* d
= direntry
+ i
;
2353 if (is_file(d
) || (is_directory(d
) && !is_dot(d
))) {
2354 mapping_t
* m
= find_mapping_for_cluster(s
,
2355 begin_of_direntry(d
));
2356 int l
= strlen(m
->path
);
2357 char* new_path
= g_malloc(l
+ diff
+ 1);
2359 assert(!strncmp(m
->path
, mapping
->path
, l2
));
2361 pstrcpy(new_path
, l
+ diff
+ 1, mapping
->path
);
2362 pstrcpy(new_path
+ l1
, l
+ diff
+ 1 - l1
,
2365 schedule_rename(s
, m
->begin
, new_path
);
2368 } while((i
% (0x10 * s
->sectors_per_cluster
)) != 0);
2374 array_remove(&(s
->commits
), i
);
2376 } else if (commit
->action
== ACTION_MKDIR
) {
2378 int j
, parent_path_len
;
2381 if (mkdir(commit
->path
))
2384 if (mkdir(commit
->path
, 0755))
2388 mapping
= insert_mapping(s
, commit
->param
.mkdir
.cluster
,
2389 commit
->param
.mkdir
.cluster
+ 1);
2390 if (mapping
== NULL
)
2393 mapping
->mode
= MODE_DIRECTORY
;
2394 mapping
->read_only
= 0;
2395 mapping
->path
= commit
->path
;
2396 j
= s
->directory
.next
;
2398 insert_direntries(s
, s
->directory
.next
,
2399 0x10 * s
->sectors_per_cluster
);
2400 mapping
->info
.dir
.first_dir_index
= j
;
2402 parent_path_len
= strlen(commit
->path
)
2403 - strlen(get_basename(commit
->path
)) - 1;
2404 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2405 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2406 if (m
->first_mapping_index
< 0 && m
!= mapping
&&
2407 !strncmp(m
->path
, mapping
->path
, parent_path_len
) &&
2408 strlen(m
->path
) == parent_path_len
)
2411 assert(j
< s
->mapping
.next
);
2412 mapping
->info
.dir
.parent_mapping_index
= j
;
2414 array_remove(&(s
->commits
), i
);
2424 * TODO: make sure that the short name is not matching *another* file
2426 static int handle_commits(BDRVVVFATState
* s
)
2430 vvfat_close_current_file(s
);
2432 for (i
= 0; !fail
&& i
< s
->commits
.next
; i
++) {
2433 commit_t
* commit
= array_get(&(s
->commits
), i
);
2434 switch(commit
->action
) {
2435 case ACTION_RENAME
: case ACTION_MKDIR
:
2439 case ACTION_WRITEOUT
: {
2441 /* these variables are only used by assert() below */
2442 direntry_t
* entry
= array_get(&(s
->directory
),
2443 commit
->param
.writeout
.dir_index
);
2444 uint32_t begin
= begin_of_direntry(entry
);
2445 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2449 assert(mapping
->begin
== begin
);
2450 assert(commit
->path
== NULL
);
2452 if (commit_one_file(s
, commit
->param
.writeout
.dir_index
,
2453 commit
->param
.writeout
.modified_offset
))
2458 case ACTION_NEW_FILE
: {
2459 int begin
= commit
->param
.new_file
.first_cluster
;
2460 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2465 for (i
= 0; i
< s
->directory
.next
; i
++) {
2466 entry
= array_get(&(s
->directory
), i
);
2467 if (is_file(entry
) && begin_of_direntry(entry
) == begin
)
2471 if (i
>= s
->directory
.next
) {
2476 /* make sure there exists an initial mapping */
2477 if (mapping
&& mapping
->begin
!= begin
) {
2478 mapping
->end
= begin
;
2481 if (mapping
== NULL
) {
2482 mapping
= insert_mapping(s
, begin
, begin
+1);
2484 /* most members will be fixed in commit_mappings() */
2485 assert(commit
->path
);
2486 mapping
->path
= commit
->path
;
2487 mapping
->read_only
= 0;
2488 mapping
->mode
= MODE_NORMAL
;
2489 mapping
->info
.file
.offset
= 0;
2491 if (commit_one_file(s
, i
, 0))
2500 if (i
> 0 && array_remove_slice(&(s
->commits
), 0, i
))
2505 static int handle_deletes(BDRVVVFATState
* s
)
2507 int i
, deferred
= 1, deleted
= 1;
2509 /* delete files corresponding to mappings marked as deleted */
2510 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2511 while (deferred
&& deleted
) {
2515 for (i
= 1; i
< s
->mapping
.next
; i
++) {
2516 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2517 if (mapping
->mode
& MODE_DELETED
) {
2518 direntry_t
* entry
= array_get(&(s
->directory
),
2519 mapping
->dir_index
);
2521 if (is_free(entry
)) {
2522 /* remove file/directory */
2523 if (mapping
->mode
& MODE_DIRECTORY
) {
2524 int j
, next_dir_index
= s
->directory
.next
,
2525 first_dir_index
= mapping
->info
.dir
.first_dir_index
;
2527 if (rmdir(mapping
->path
) < 0) {
2528 if (errno
== ENOTEMPTY
) {
2535 for (j
= 1; j
< s
->mapping
.next
; j
++) {
2536 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2537 if (m
->mode
& MODE_DIRECTORY
&&
2538 m
->info
.dir
.first_dir_index
>
2540 m
->info
.dir
.first_dir_index
<
2543 m
->info
.dir
.first_dir_index
;
2545 remove_direntries(s
, first_dir_index
,
2546 next_dir_index
- first_dir_index
);
2551 if (unlink(mapping
->path
))
2555 DLOG(fprintf(stderr
, "DELETE (%d)\n", i
); print_mapping(mapping
); print_direntry(entry
));
2556 remove_mapping(s
, i
);
2565 * synchronize mapping with new state:
2567 * - copy FAT (with bdrv_read)
2568 * - mark all filenames corresponding to mappings as deleted
2569 * - recurse direntries from root (using bs->bdrv_read)
2570 * - delete files corresponding to mappings marked as deleted
2572 static int do_commit(BDRVVVFATState
* s
)
2576 /* the real meat are the commits. Nothing to do? Move along! */
2577 if (s
->commits
.next
== 0)
2580 vvfat_close_current_file(s
);
2582 ret
= handle_renames_and_mkdirs(s
);
2584 fprintf(stderr
, "Error handling renames (%d)\n", ret
);
2589 /* copy FAT (with bdrv_read) */
2590 memcpy(s
->fat
.pointer
, s
->fat2
, 0x200 * s
->sectors_per_fat
);
2592 /* recurse direntries from root (using bs->bdrv_read) */
2593 ret
= commit_direntries(s
, 0, -1);
2595 fprintf(stderr
, "Fatal: error while committing (%d)\n", ret
);
2600 ret
= handle_commits(s
);
2602 fprintf(stderr
, "Error handling commits (%d)\n", ret
);
2607 ret
= handle_deletes(s
);
2609 fprintf(stderr
, "Error deleting\n");
2614 s
->qcow
->drv
->bdrv_make_empty(s
->qcow
);
2616 memset(s
->used_clusters
, 0, sector2cluster(s
, s
->sector_count
));
2622 static int try_commit(BDRVVVFATState
* s
)
2624 vvfat_close_current_file(s
);
2626 if(!is_consistent(s
))
2628 return do_commit(s
);
2631 static int vvfat_write(BlockDriverState
*bs
, int64_t sector_num
,
2632 const uint8_t *buf
, int nb_sectors
)
2634 BDRVVVFATState
*s
= bs
->opaque
;
2639 /* Check if we're operating in read-only mode */
2640 if (s
->qcow
== NULL
) {
2644 vvfat_close_current_file(s
);
2647 * Some sanity checks:
2648 * - do not allow writing to the boot sector
2649 * - do not allow to write non-ASCII filenames
2652 if (sector_num
< s
->first_sectors_number
)
2655 for (i
= sector2cluster(s
, sector_num
);
2656 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1);) {
2657 mapping_t
* mapping
= find_mapping_for_cluster(s
, i
);
2659 if (mapping
->read_only
) {
2660 fprintf(stderr
, "Tried to write to write-protected file %s\n",
2665 if (mapping
->mode
& MODE_DIRECTORY
) {
2666 int begin
= cluster2sector(s
, i
);
2667 int end
= begin
+ s
->sectors_per_cluster
, k
;
2669 const direntry_t
* direntries
;
2674 if (begin
< sector_num
)
2676 if (end
> sector_num
+ nb_sectors
)
2677 end
= sector_num
+ nb_sectors
;
2678 dir_index
= mapping
->dir_index
+
2679 0x10 * (begin
- mapping
->begin
* s
->sectors_per_cluster
);
2680 direntries
= (direntry_t
*)(buf
+ 0x200 * (begin
- sector_num
));
2682 for (k
= 0; k
< (end
- begin
) * 0x10; k
++) {
2683 /* do not allow non-ASCII filenames */
2684 if (parse_long_name(&lfn
, direntries
+ k
) < 0) {
2685 fprintf(stderr
, "Warning: non-ASCII filename\n");
2688 /* no access to the direntry of a read-only file */
2689 else if (is_short_name(direntries
+k
) &&
2690 (direntries
[k
].attributes
& 1)) {
2691 if (memcmp(direntries
+ k
,
2692 array_get(&(s
->directory
), dir_index
+ k
),
2693 sizeof(direntry_t
))) {
2694 fprintf(stderr
, "Warning: tried to write to write-protected file\n");
2706 * Use qcow backend. Commit later.
2708 DLOG(fprintf(stderr
, "Write to qcow backend: %d + %d\n", (int)sector_num
, nb_sectors
));
2709 ret
= s
->qcow
->drv
->bdrv_write(s
->qcow
, sector_num
, buf
, nb_sectors
);
2711 fprintf(stderr
, "Error writing to qcow backend\n");
2715 for (i
= sector2cluster(s
, sector_num
);
2716 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1); i
++)
2718 s
->used_clusters
[i
] |= USED_ALLOCATED
;
2721 /* TODO: add timeout */
2728 static coroutine_fn
int vvfat_co_write(BlockDriverState
*bs
, int64_t sector_num
,
2729 const uint8_t *buf
, int nb_sectors
)
2732 BDRVVVFATState
*s
= bs
->opaque
;
2733 qemu_co_mutex_lock(&s
->lock
);
2734 ret
= vvfat_write(bs
, sector_num
, buf
, nb_sectors
);
2735 qemu_co_mutex_unlock(&s
->lock
);
2739 static int vvfat_is_allocated(BlockDriverState
*bs
,
2740 int64_t sector_num
, int nb_sectors
, int* n
)
2742 BDRVVVFATState
* s
= bs
->opaque
;
2743 *n
= s
->sector_count
- sector_num
;
2744 if (*n
> nb_sectors
)
2751 static int write_target_commit(BlockDriverState
*bs
, int64_t sector_num
,
2752 const uint8_t* buffer
, int nb_sectors
) {
2753 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
2754 return try_commit(s
);
2757 static void write_target_close(BlockDriverState
*bs
) {
2758 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
2759 bdrv_delete(s
->qcow
);
2760 g_free(s
->qcow_filename
);
2763 static BlockDriver vvfat_write_target
= {
2764 .format_name
= "vvfat_write_target",
2765 .bdrv_write
= write_target_commit
,
2766 .bdrv_close
= write_target_close
,
2769 static int enable_write_target(BDRVVVFATState
*s
)
2771 BlockDriver
*bdrv_qcow
;
2772 QEMUOptionParameter
*options
;
2774 int size
= sector2cluster(s
, s
->sector_count
);
2775 s
->used_clusters
= calloc(size
, 1);
2777 array_init(&(s
->commits
), sizeof(commit_t
));
2779 s
->qcow_filename
= g_malloc(1024);
2780 get_tmp_filename(s
->qcow_filename
, 1024);
2782 bdrv_qcow
= bdrv_find_format("qcow");
2783 options
= parse_option_parameters("", bdrv_qcow
->create_options
, NULL
);
2784 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, s
->sector_count
* 512);
2785 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, "fat:");
2787 if (bdrv_create(bdrv_qcow
, s
->qcow_filename
, options
) < 0)
2790 s
->qcow
= bdrv_new("");
2791 if (s
->qcow
== NULL
) {
2795 ret
= bdrv_open(s
->qcow
, s
->qcow_filename
,
2796 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, bdrv_qcow
);
2802 unlink(s
->qcow_filename
);
2805 s
->bs
->backing_hd
= calloc(sizeof(BlockDriverState
), 1);
2806 s
->bs
->backing_hd
->drv
= &vvfat_write_target
;
2807 s
->bs
->backing_hd
->opaque
= g_malloc(sizeof(void*));
2808 *(void**)s
->bs
->backing_hd
->opaque
= s
;
2813 static void vvfat_close(BlockDriverState
*bs
)
2815 BDRVVVFATState
*s
= bs
->opaque
;
2817 vvfat_close_current_file(s
);
2818 array_free(&(s
->fat
));
2819 array_free(&(s
->directory
));
2820 array_free(&(s
->mapping
));
2821 g_free(s
->cluster_buffer
);
2824 static BlockDriver bdrv_vvfat
= {
2825 .format_name
= "vvfat",
2826 .instance_size
= sizeof(BDRVVVFATState
),
2827 .bdrv_file_open
= vvfat_open
,
2828 .bdrv_read
= vvfat_co_read
,
2829 .bdrv_write
= vvfat_co_write
,
2830 .bdrv_close
= vvfat_close
,
2831 .bdrv_is_allocated
= vvfat_is_allocated
,
2832 .protocol_name
= "fat",
2835 static void bdrv_vvfat_init(void)
2837 bdrv_register(&bdrv_vvfat
);
2840 block_init(bdrv_vvfat_init
);
2843 static void checkpoint(void) {
2844 assert(((mapping_t
*)array_get(&(vvv
->mapping
), 0))->end
== 2);
2847 assert(!vvv
->current_mapping
|| vvv
->current_fd
|| (vvv
->current_mapping
->mode
& MODE_DIRECTORY
));
2849 if (((direntry_t
*)vvv
->directory
.pointer
)[1].attributes
!= 0xf)
2850 fprintf(stderr
, "Nonono!\n");
2852 direntry_t
* direntry
;
2853 assert(vvv
->mapping
.size
>= vvv
->mapping
.item_size
* vvv
->mapping
.next
);
2854 assert(vvv
->directory
.size
>= vvv
->directory
.item_size
* vvv
->directory
.next
);
2855 if (vvv
->mapping
.next
<47)
2857 assert((mapping
= array_get(&(vvv
->mapping
), 47)));
2858 assert(mapping
->dir_index
< vvv
->directory
.next
);
2859 direntry
= array_get(&(vvv
->directory
), mapping
->dir_index
);
2860 assert(!memcmp(direntry
->name
, "USB H ", 11) || direntry
->name
[0]==0);