2 * QEMU Block driver for virtual VFAT (shadows a local directory)
4 * Copyright (c) 2004 Johannes E. Schindelin
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "block_int.h"
32 // TODO: make root directory larger
33 // TODO: make directory clusters connected, so they are reserved anyway... add a member which tells how many clusters are reserved after a directory
34 // TODO: introduce another member in mapping_t which says where the directory resides in s->directory (for mkdir and rmdir)
35 // in _read and _write, before treating direntries or file contents, get_mapping to know what it is.
39 // TODO: when commit_data'ing a direntry and is_consistent, commit_remove
40 // TODO: reset MODE_MODIFIED when commit_remove'ing
44 /* dynamic array functions */
45 typedef struct array_t
{
47 unsigned int size
,next
,item_size
;
50 static inline void array_init(array_t
* array
,unsigned int item_size
)
55 array
->item_size
=item_size
;
58 static inline void array_free(array_t
* array
)
62 array
->size
=array
->next
=0;
65 /* make sure that memory is reserved at pointer[index*item_size] */
66 static inline void* array_get(array_t
* array
,unsigned int index
) {
67 if((index
+1)*array
->item_size
>array
->size
) {
68 int new_size
=(index
+32)*array
->item_size
;
69 array
->pointer
=realloc(array
->pointer
,new_size
);
75 return array
->pointer
+index
*array
->item_size
;
78 static inline void* array_get_next(array_t
* array
) {
79 unsigned int next
=array
->next
;
80 void* result
=array_get(array
,next
);
85 static inline void* array_insert(array_t
* array
,unsigned int index
,unsigned int count
) {
86 if((array
->next
+count
)*array
->item_size
>array
->size
) {
87 int increment
=count
*array
->item_size
;
88 array
->pointer
=realloc(array
->pointer
,array
->size
+increment
);
91 array
->size
+=increment
;
93 memmove(array
->pointer
+(index
+count
)*array
->item_size
,
94 array
->pointer
+index
*array
->item_size
,
95 (array
->next
-index
)*array
->item_size
);
97 return array
->pointer
+index
*array
->item_size
;
100 /* this performs a "roll", so that the element which was at index_from becomes
101 * index_to, but the order of all other elements is preserved. */
102 static inline int array_roll(array_t
* array
,int index_to
,int index_from
,int count
)
110 index_to
<0 || index_to
>=array
->next
||
111 index_from
<0 || index_from
>=array
->next
)
114 if(index_to
==index_from
)
118 from
=array
->pointer
+index_from
*is
;
119 to
=array
->pointer
+index_to
*is
;
120 buf
=malloc(is
*count
);
121 memcpy(buf
,from
,is
*count
);
123 if(index_to
<index_from
)
124 memmove(to
+is
*count
,to
,from
-to
);
126 memmove(from
,from
+is
*count
,to
-from
);
128 memcpy(to
,buf
,is
*count
);
135 int array_remove(array_t
* array
,int index
)
137 if(array_roll(array
,array
->next
-1,index
,1))
143 /* These structures are used to fake a disk and the VFAT filesystem.
144 * For this reason we need to use __attribute__((packed)). */
146 typedef struct bootsector_t
{
149 uint16_t sector_size
;
150 uint8_t sectors_per_cluster
;
151 uint16_t reserved_sectors
;
152 uint8_t number_of_fats
;
153 uint16_t root_entries
;
156 uint16_t sectors_per_fat
;
157 uint16_t sectors_per_track
;
158 uint16_t number_of_heads
;
159 uint32_t hidden_sectors
;
160 uint32_t total_sectors
;
163 uint8_t drive_number
;
164 uint8_t current_head
;
167 uint8_t volume_label
[11];
168 } __attribute__((packed
)) fat16
;
170 uint32_t sectors_per_fat
;
173 uint32_t first_cluster_of_root_directory
;
174 uint16_t info_sector
;
175 uint16_t backup_boot_sector
;
177 } __attribute__((packed
)) fat32
;
180 uint8_t ignored
[0x1c0];
182 } __attribute__((packed
)) bootsector_t
;
184 typedef struct partition_t
{
185 uint8_t attributes
; /* 0x80 = bootable */
187 uint8_t start_sector
;
188 uint8_t start_cylinder
;
189 uint8_t fs_type
; /* 0x6 = FAT16, 0xb = FAT32 */
192 uint8_t end_cylinder
;
193 uint32_t start_sector_long
;
194 uint32_t end_sector_long
;
195 } __attribute__((packed
)) partition_t
;
197 typedef struct mbr_t
{
198 uint8_t ignored
[0x1be];
199 partition_t partition
[4];
201 } __attribute__((packed
)) mbr_t
;
203 typedef struct direntry_t
{
205 uint8_t extension
[3];
216 } __attribute__((packed
)) direntry_t
;
218 /* this structure are used to transparently access the files */
220 typedef struct mapping_t
{
221 /* begin is the first cluster, end is the last+1,
222 * offset is the offset in the file in clusters of this slice */
223 off_t begin
,end
,offset
;
226 /* as s->directory is growable, no pointer may be used here */
227 unsigned int dir_index
;
228 enum { MODE_NORMAL
,MODE_UNDEFINED
,MODE_MODIFIED
,MODE_DELETED
,MODE_DIRECTORY
} mode
;
231 /* this structure is used to hold sectors which need to be written, but it's
232 * not known yet where to write them. */
234 typedef struct commit_t
{
235 uint32_t cluster_num
;
239 /* write support exists for fat, direntry and file contents */
241 WRITE_UNDEFINED
,WRITE_FAT
,WRITE_DIRENTRY
,WRITE_DATA
244 /* here begins the real VVFAT driver */
246 typedef struct BDRVVVFATState
{
247 unsigned int first_sectors_number
; /* 1 for a single partition, 0x40 for a disk with partition table */
248 unsigned char first_sectors
[0x40*0x200];
250 int fat_type
; /* 16 or 32 */
251 array_t fat
,directory
,mapping
;
253 unsigned int cluster_size
;
254 unsigned int sectors_per_cluster
;
255 unsigned int sectors_per_fat
;
256 unsigned int sectors_of_root_directory
;
257 unsigned int sectors_for_directory
;
258 unsigned int faked_sectors
; /* how many sectors are faked before file data */
259 uint32_t sector_count
; /* total number of sectors of the partition */
260 uint32_t cluster_count
; /* total number of clusters of this partition */
261 unsigned int first_file_mapping
; /* index of the first mapping which is not a directory, but a file */
262 uint32_t max_fat_value
;
265 char current_fd_is_writable
; /* =0 if read only, !=0 if read/writable */
266 mapping_t
* current_mapping
;
267 unsigned char* cluster
;
268 unsigned int current_cluster
;
272 /* for each file, the file contents, the direntry, and the fat entries are
273 * written, but not necessarily in that order */
274 write_action_t action
[3];
278 static int vvfat_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
280 if (strstart(filename
, "fat:", NULL
) ||
281 strstart(filename
, "fatrw:", NULL
))
286 static void init_mbr(BDRVVVFATState
* s
)
288 /* TODO: if the files mbr.img and bootsect.img exist, use them */
289 mbr_t
* real_mbr
=(mbr_t
*)s
->first_sectors
;
290 partition_t
* partition
=&(real_mbr
->partition
[0]);
292 memset(s
->first_sectors
,0,512);
294 partition
->attributes
=0x80; /* bootable */
295 partition
->start_head
=1;
296 partition
->start_sector
=1;
297 partition
->start_cylinder
=0;
298 partition
->fs_type
=(s
->fat_type
==16?0x6:0xb); /* FAT16/FAT32 */
299 partition
->end_head
=0xf;
300 partition
->end_sector
=0xff; /* end sector & upper 2 bits of cylinder */;
301 partition
->end_cylinder
=0xff; /* lower 8 bits of end cylinder */;
302 partition
->start_sector_long
=cpu_to_le32(0x3f);
303 partition
->end_sector_long
=cpu_to_le32(s
->sector_count
);
305 real_mbr
->magic
[0]=0x55; real_mbr
->magic
[1]=0xaa;
308 /* dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26 */
309 static inline int short2long_name(unsigned char* dest
,const char* src
)
312 for(i
=0;i
<129 && src
[i
];i
++) {
316 dest
[2*i
]=dest
[2*i
+1]=0;
317 for(i
=2*i
+2;(i
%26);i
++)
322 static inline direntry_t
* create_long_filename(BDRVVVFATState
* s
,const char* filename
)
325 int length
=short2long_name(buffer
,filename
),
326 number_of_entries
=(length
+25)/26,i
;
329 for(i
=0;i
<number_of_entries
;i
++) {
330 entry
=array_get_next(&(s
->directory
));
331 entry
->attributes
=0xf;
332 entry
->reserved
[0]=0;
334 entry
->name
[0]=(number_of_entries
-i
)|(i
==0?0x40:0);
336 for(i
=0;i
<length
;i
++) {
338 if(offset
<10) offset
=1+offset
;
339 else if(offset
<22) offset
=14+offset
-10;
340 else offset
=28+offset
-22;
341 entry
=array_get(&(s
->directory
),s
->directory
.next
-1-(i
/26));
342 entry
->name
[offset
]=buffer
[i
];
344 return array_get(&(s
->directory
),s
->directory
.next
-number_of_entries
);
349 static inline uint8_t fat_chksum(direntry_t
* entry
)
355 chksum
=(((chksum
&0xfe)>>1)|((chksum
&0x01)?0x80:0))
356 +(unsigned char)entry
->name
[i
];
361 /* if return_time==0, this returns the fat_date, else the fat_time */
362 static uint16_t fat_datetime(time_t time
,int return_time
) {
365 t
=localtime(&time
); /* this is not thread safe */
369 localtime_r(&time
,t
);
372 return cpu_to_le16((t
->tm_sec
/2)|(t
->tm_min
<<5)|(t
->tm_hour
<<11));
373 return cpu_to_le16((t
->tm_mday
)|((t
->tm_mon
+1)<<5)|((t
->tm_year
-80)<<9));
376 static inline void fat_set(BDRVVVFATState
* s
,unsigned int cluster
,uint32_t value
)
378 if(s
->fat_type
==12) {
379 assert(0); /* TODO */
380 } else if(s
->fat_type
==16) {
381 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
382 *entry
=cpu_to_le16(value
&0xffff);
384 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
385 *entry
=cpu_to_le32(value
);
389 static inline uint32_t fat_get(BDRVVVFATState
* s
,unsigned int cluster
)
391 //fprintf(stderr,"want to get fat for cluster %d\n",cluster);
392 if(s
->fat_type
==12) {
393 const uint8_t* x
=s
->fat
.pointer
+cluster
*3/2;
394 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
395 } else if(s
->fat_type
==16) {
396 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
397 return le16_to_cpu(*entry
);
399 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
400 return le32_to_cpu(*entry
);
404 static inline int fat_eof(BDRVVVFATState
* s
,uint32_t fat_entry
)
406 if(fat_entry
>s
->max_fat_value
-8)
411 static inline void init_fat(BDRVVVFATState
* s
)
415 array_init(&(s
->fat
),(s
->fat_type
==32?4:2));
416 array_get(&(s
->fat
),s
->sectors_per_fat
*0x200/s
->fat
.item_size
-1);
417 memset(s
->fat
.pointer
,0,s
->fat
.size
);
418 fat_set(s
,0,0x7ffffff8);
420 for(i
=1;i
<s
->sectors_for_directory
/s
->sectors_per_cluster
-1;i
++)
422 fat_set(s
,i
,0x7fffffff);
424 switch(s
->fat_type
) {
425 case 12: s
->max_fat_value
=0xfff; break;
426 case 16: s
->max_fat_value
=0xffff; break;
427 case 32: s
->max_fat_value
=0xfffffff; break;
428 default: s
->max_fat_value
=0; /* error... */
433 static inline int long2unix_name(unsigned char* dest
,int dest_size
,direntry_t
* direntry_short
) {
435 int chksum
=fat_chksum(direntry_short
);
437 char* buf
=(char*)(direntry_short
+i
);
438 if((buf
[0]&0x3f)!=-i
|| direntry_short
[i
].reserved
[1]!=chksum
||
439 direntry_short
[i
].attributes
!=0xf) {
442 /* take short name */
443 for(j
=7;j
>0 && direntry_short
->name
[j
]==' ';j
--);
446 strncpy(dest
,direntry_short
->name
,j
+1);
447 dest
+=j
+1; dest_size
-=j
+1;
448 for(j
=2;j
>=0 && direntry_short
->extension
[j
]==' ';j
--);
453 strncpy(dest
+1,direntry_short
->extension
,j
+1);
461 dest
[0]=buf
[2*j
+((j
<5)?1:(j
<11)?4:6)];
462 if(dest
[0]==0 && (buf
[0]&0x40)!=0)
466 /* last entry, but no trailing \0? */
473 static inline direntry_t
* create_short_filename(BDRVVVFATState
* s
,unsigned int directory_start
,const char* filename
,int is_dot
)
475 int i
,long_index
=s
->directory
.next
;
477 direntry_t
* entry_long
=0;
480 entry
=array_get_next(&(s
->directory
));
481 memset(entry
->name
,0x20,11);
482 memcpy(entry
->name
,filename
,strlen(filename
));
486 for(i
=1;i
<8 && filename
[i
] && filename
[i
]!='.';i
++);
488 entry_long
=create_long_filename(s
,filename
);
490 entry
=array_get_next(&(s
->directory
));
491 memset(entry
->name
,0x20,11);
492 strncpy(entry
->name
,filename
,i
);
495 int len
=strlen(filename
);
496 for(i
=len
;i
>0 && filename
[i
-1]!='.';i
--);
498 memcpy(entry
->extension
,filename
+i
,(len
-i
>3?3:len
-i
));
501 /* upcase & remove unwanted characters */
503 if(i
==10 || i
==7) for(;i
>1 && entry
->name
[i
]==' ';i
--);
504 if(entry
->name
[i
]<=' ' || entry
->name
[i
]>0x7f
505 || strchr("*?<>|\":/\\[];,+='",entry
->name
[i
]))
507 else if(entry
->name
[i
]>='a' && entry
->name
[i
]<='z')
508 entry
->name
[i
]+='A'-'a';
511 /* mangle duplicates */
513 direntry_t
* entry1
=array_get(&(s
->directory
),directory_start
);
516 for(;entry1
<entry
;entry1
++)
517 if(!(entry1
->attributes
&0xf) && !memcmp(entry1
->name
,entry
->name
,11))
518 break; /* found dupe */
519 if(entry1
==entry
) /* no dupe found */
522 /* use all 8 characters of name */
523 if(entry
->name
[7]==' ') {
525 for(j
=6;j
>0 && entry
->name
[j
]==' ';j
--)
529 /* increment number */
530 for(j
=7;j
>0 && entry
->name
[j
]=='9';j
--)
533 if(entry
->name
[j
]<'0' || entry
->name
[j
]>'9')
540 /* calculate checksum; propagate to long name */
542 uint8_t chksum
=fat_chksum(entry
);
544 /* calculate anew, because realloc could have taken place */
545 entry_long
=array_get(&(s
->directory
),long_index
);
546 while(entry_long
<entry
547 && entry_long
->attributes
==0xf) {
548 entry_long
->reserved
[1]=chksum
;
556 static int read_directory(BDRVVVFATState
* s
,const char* dirname
,
557 int first_cluster_of_parent
)
560 DIR* dir
=opendir(dirname
);
561 struct dirent
* entry
;
563 unsigned int start_of_directory
=s
->directory
.next
;
564 /* mappings before first_file_mapping are directories */
565 unsigned int first_directory_mapping
=s
->first_file_mapping
;
566 unsigned int first_cluster
=(start_of_directory
/0x10/s
->sectors_per_cluster
);
572 while((entry
=readdir(dir
))) {
573 unsigned int length
=strlen(dirname
)+2+strlen(entry
->d_name
);
575 direntry_t
* direntry
;
576 int is_dot
=!strcmp(entry
->d_name
,".");
577 int is_dotdot
=!strcmp(entry
->d_name
,"..");
579 if(start_of_directory
==1 && (is_dotdot
|| is_dot
))
582 buffer
=(char*)malloc(length
);
583 snprintf(buffer
,length
,"%s/%s",dirname
,entry
->d_name
);
585 if(stat(buffer
,&st
)<0) {
590 /* create directory entry for this file */
591 //fprintf(stderr,"create direntry at %d (cluster %d) for %s\n",s->directory.next,s->directory.next/0x10/s->sectors_per_cluster,entry->d_name);
592 direntry
=create_short_filename(s
,start_of_directory
,entry
->d_name
,is_dot
||is_dotdot
);
593 direntry
->attributes
=(S_ISDIR(st
.st_mode
)?0x10:0x20);
594 direntry
->reserved
[0]=direntry
->reserved
[1]=0;
595 direntry
->ctime
=fat_datetime(st
.st_ctime
,1);
596 direntry
->cdate
=fat_datetime(st
.st_ctime
,0);
597 direntry
->adate
=fat_datetime(st
.st_atime
,0);
598 direntry
->begin_hi
=0;
599 direntry
->mtime
=fat_datetime(st
.st_mtime
,1);
600 direntry
->mdate
=fat_datetime(st
.st_mtime
,0);
602 direntry
->begin
=cpu_to_le16(first_cluster_of_parent
);
604 direntry
->begin
=cpu_to_le16(first_cluster
);
606 direntry
->begin
=cpu_to_le16(0); /* do that later */
607 direntry
->size
=cpu_to_le32(st
.st_size
);
609 /* create mapping for this file */
610 if(!is_dot
&& !is_dotdot
) {
611 if(S_ISDIR(st
.st_mode
))
612 s
->current_mapping
=(mapping_t
*)array_insert(&(s
->mapping
),s
->first_file_mapping
++,1);
614 s
->current_mapping
=(mapping_t
*)array_get_next(&(s
->mapping
));
615 s
->current_mapping
->begin
=0;
616 s
->current_mapping
->end
=st
.st_size
;
617 s
->current_mapping
->offset
=0;
618 s
->current_mapping
->filename
=buffer
;
619 s
->current_mapping
->dir_index
=s
->directory
.next
-1;
620 s
->current_mapping
->mode
=(S_ISDIR(st
.st_mode
)?MODE_DIRECTORY
:MODE_UNDEFINED
);
625 /* fill with zeroes up to the end of the cluster */
626 while(s
->directory
.next
%(0x10*s
->sectors_per_cluster
)) {
627 direntry_t
* direntry
=array_get_next(&(s
->directory
));
628 memset(direntry
,0,sizeof(direntry_t
));
631 /* reserve next cluster also (for new files) */
632 for(i
=0;i
<0x10*s
->sectors_per_cluster
;i
++) {
633 direntry_t
* direntry
=array_get_next(&(s
->directory
));
634 memset(direntry
,0,sizeof(direntry_t
));
637 /* was it the first directory? */
638 if(start_of_directory
==1) {
639 mapping_t
* mapping
=array_insert(&(s
->mapping
),0,1);
640 mapping
->filename
=strdup(dirname
);
641 mapping
->mode
=MODE_DIRECTORY
;
645 mapping
->dir_index
=0xffffffff;
646 s
->sectors_of_root_directory
=s
->directory
.next
/0x10;
649 /* recurse directories */
653 //fprintf(stderr,"iterating subdirectories of %s (first cluster %d): %d to %d\n",dirname,first_cluster,first_directory_mapping,last_directory_mapping);
654 for(i
=first_directory_mapping
;i
<s
->first_file_mapping
;i
++) {
655 mapping_t
* mapping
=array_get(&(s
->mapping
),i
);
656 direntry_t
* direntry
=array_get(&(s
->directory
),mapping
->dir_index
);
657 /* the directory to be read can add more subdirectories */
658 int last_dir_mapping
=s
->first_file_mapping
;
660 assert(mapping
->mode
==MODE_DIRECTORY
);
661 /* first, tell the mapping where the directory will start */
662 mapping
->begin
=s
->directory
.next
/0x10/s
->sectors_per_cluster
;
664 mapping
[-1].end
=mapping
->begin
;
665 assert(mapping
[-1].begin
<mapping
->begin
);
667 /* then tell the direntry */
668 direntry
->begin
=cpu_to_le16(mapping
->begin
);
669 //fprintf(stderr,"read directory %s (begin %d)\n",mapping->filename,(int)mapping->begin);
671 if(read_directory(s
,mapping
->filename
,first_cluster
))
674 if(last_dir_mapping
!=s
->first_file_mapping
) {
675 int diff
=s
->first_file_mapping
-last_dir_mapping
;
678 if(last_dir_mapping
!=i
+1) {
679 int count
=last_dir_mapping
-i
-1;
680 int to
=s
->first_file_mapping
-count
;
684 array_roll(&(s
->mapping
),to
,i
+1,count
);
685 /* could have changed due to realloc */
686 mapping
=array_get(&(s
->mapping
),i
);
687 mapping
->end
=mapping
[1].begin
;
697 static int init_directory(BDRVVVFATState
* s
,const char* dirname
)
699 bootsector_t
* bootsector
=(bootsector_t
*)&(s
->first_sectors
[(s
->first_sectors_number
-1)*0x200]);
701 unsigned int cluster
;
703 memset(&(s
->first_sectors
[0]),0,0x40*0x200);
705 /* TODO: if FAT32, this is probably wrong */
706 s
->sectors_per_fat
=0xfc;
707 s
->sectors_per_cluster
=0x10;
708 s
->cluster_size
=s
->sectors_per_cluster
*0x200;
709 s
->cluster
=malloc(s
->cluster_size
);
711 array_init(&(s
->mapping
),sizeof(mapping_t
));
712 array_init(&(s
->directory
),sizeof(direntry_t
));
713 array_init(&(s
->commit
),sizeof(commit_t
));
715 /* add volume label */
717 direntry_t
* entry
=array_get_next(&(s
->directory
));
718 entry
->attributes
=0x28; /* archive | volume label */
719 snprintf(entry
->name
,11,"QEMU VVFAT");
722 if(read_directory(s
,dirname
,0))
725 /* make sure that the number of directory entries is multiple of 0x200/0x20 (to fit the last sector exactly) */
726 s
->sectors_for_directory
=s
->directory
.next
/0x10;
728 s
->faked_sectors
=s
->first_sectors_number
+s
->sectors_per_fat
*2+s
->sectors_for_directory
;
729 s
->cluster_count
=(s
->sector_count
-s
->faked_sectors
)/s
->sectors_per_cluster
;
731 /* Now build FAT, and write back information into directory */
734 cluster
=s
->sectors_for_directory
/s
->sectors_per_cluster
;
735 assert(s
->sectors_for_directory
%s
->sectors_per_cluster
==0);
737 /* set the end of the last read directory */
738 if(s
->first_file_mapping
>0) {
739 mapping_t
* mapping
=array_get(&(s
->mapping
),s
->first_file_mapping
-1);
740 mapping
->end
=cluster
;
743 for(i
=1;i
<s
->mapping
.next
;i
++) {
744 mapping_t
* mapping
=array_get(&(s
->mapping
),i
);
745 direntry_t
* direntry
=array_get(&(s
->directory
),mapping
->dir_index
);
746 if(mapping
->mode
==MODE_DIRECTORY
) {
750 fprintf(stderr
,"assert: %s %d < %d\n",mapping
->filename
,(int)mapping
->begin
,(int)mapping
->end
);
752 assert(mapping
->begin
<mapping
->end
);
753 for(i
=mapping
->begin
;i
<mapping
->end
-1;i
++)
755 fat_set(s
,i
,0x7fffffff);
757 /* as the space is virtual, we can be sloppy about it */
758 unsigned int end_cluster
=cluster
+mapping
->end
/s
->cluster_size
;
760 if(end_cluster
>=s
->cluster_count
) {
761 fprintf(stderr
,"Directory does not fit in FAT%d\n",s
->fat_type
);
764 mapping
->begin
=cluster
;
765 mapping
->mode
=MODE_NORMAL
;
767 direntry
->size
=cpu_to_le32(mapping
->end
);
768 if(direntry
->size
==0) {
770 mapping
->end
=cluster
;
774 direntry
->begin
=cpu_to_le16(cluster
);
775 mapping
->end
=end_cluster
+1;
776 for(;cluster
<end_cluster
;cluster
++)
777 fat_set(s
,cluster
,cluster
+1);
778 fat_set(s
,cluster
,0x7fffffff);
783 s
->current_mapping
=0;
785 bootsector
->jump
[0]=0xeb;
786 bootsector
->jump
[1]=0x3e;
787 bootsector
->jump
[2]=0x90;
788 memcpy(bootsector
->name
,"QEMU ",8);
789 bootsector
->sector_size
=cpu_to_le16(0x200);
790 bootsector
->sectors_per_cluster
=s
->sectors_per_cluster
;
791 bootsector
->reserved_sectors
=cpu_to_le16(1);
792 bootsector
->number_of_fats
=0x2; /* number of FATs */
793 bootsector
->root_entries
=cpu_to_le16(s
->sectors_of_root_directory
*0x10);
795 bootsector
->media_type
=(s
->first_sectors_number
==1?0xf0:0xf8); /* media descriptor */
796 bootsector
->sectors_per_fat
=cpu_to_le16(s
->sectors_per_fat
);
797 bootsector
->sectors_per_track
=cpu_to_le16(0x3f);
798 bootsector
->number_of_heads
=cpu_to_le16(0x10);
799 bootsector
->hidden_sectors
=cpu_to_le32(s
->first_sectors_number
==1?0:0x3f);
800 /* TODO: if FAT32, adjust */
801 bootsector
->total_sectors
=cpu_to_le32(s
->sector_count
);
803 /* TODO: if FAT32, this is wrong */
804 bootsector
->u
.fat16
.drive_number
=0x80; /* assume this is hda (TODO) */
805 bootsector
->u
.fat16
.current_head
=0;
806 bootsector
->u
.fat16
.signature
=0x29;
807 bootsector
->u
.fat16
.id
=cpu_to_le32(0xfabe1afd);
809 memcpy(bootsector
->u
.fat16
.volume_label
,"QEMU VVFAT ",11);
810 memcpy(bootsector
->fat_type
,(s
->fat_type
==12?"FAT12 ":s
->fat_type
==16?"FAT16 ":"FAT32 "),8);
811 bootsector
->magic
[0]=0x55; bootsector
->magic
[1]=0xaa;
816 static int vvfat_open(BlockDriverState
*bs
, const char* dirname
)
818 BDRVVVFATState
*s
= bs
->opaque
;
821 /* TODO: automatically determine which FAT type */
823 s
->sector_count
=0xec04f;
825 s
->current_cluster
=0xffffffff;
826 s
->first_file_mapping
=0;
828 /* TODO: if simulating a floppy, this is 1, because there is no partition table */
829 s
->first_sectors_number
=0x40;
831 if (strstart(dirname
, "fat:", &dirname
)) {
832 /* read only is the default for safety */
834 } else if (strstart(dirname
, "fatrw:", &dirname
)) {
835 /* development only for now */
840 if(init_directory(s
,dirname
))
843 if(s
->first_sectors_number
==0x40)
846 /* TODO: this could be wrong for FAT32 */
847 bs
->cyls
=1023; bs
->heads
=15; bs
->secs
=63;
848 bs
->total_sectors
=bs
->cyls
*bs
->heads
*bs
->secs
;
852 s
->action
[i
]=WRITE_UNDEFINED
;
856 static inline void vvfat_close_current_file(BDRVVVFATState
*s
)
858 if(s
->current_mapping
) {
859 s
->current_mapping
= 0;
860 close(s
->current_fd
);
864 /* mappings between index1 and index2-1 are supposed to be ordered
865 * return value is the index of the last mapping for which end>cluster_num
867 static inline int find_mapping_for_cluster_aux(BDRVVVFATState
* s
,int cluster_num
,int index1
,int index2
)
870 //fprintf(stderr,"find_aux: cluster_num=%d, index1=%d,index2=%d\n",cluster_num,index1,index2);
873 index3
=(index1
+index2
)/2;
874 mapping
=array_get(&(s
->mapping
),index3
);
875 //fprintf(stderr,"index3: %d = (%d+%d)/2, end: %d\n",index3,index1,index2,(int)mapping->end);
876 if(mapping
->end
>cluster_num
) {
877 assert(index2
!=index3
|| index2
==0);
886 assert(index1
<=index2
);
890 static inline mapping_t
* find_mapping_for_cluster(BDRVVVFATState
* s
,int cluster_num
)
892 int index
=find_mapping_for_cluster_aux(s
,cluster_num
,0,s
->mapping
.next
);
894 if(index
>=s
->mapping
.next
)
896 mapping
=array_get(&(s
->mapping
),index
);
897 if(mapping
->begin
>cluster_num
)
902 static int open_file(BDRVVVFATState
* s
,mapping_t
* mapping
,int flags
)
906 assert(flags
==O_RDONLY
|| flags
==O_RDWR
);
907 if(!s
->current_mapping
||
908 strcmp(s
->current_mapping
->filename
,mapping
->filename
) ||
909 (flags
==O_RDWR
&& !s
->current_fd_is_writable
)) {
911 int fd
= open(mapping
->filename
, flags
| O_BINARY
| O_LARGEFILE
);
914 vvfat_close_current_file(s
);
916 s
->current_fd_is_writable
= (flags
==O_RDWR
?-1:0);
917 s
->current_mapping
= mapping
;
922 static inline int read_cluster(BDRVVVFATState
*s
,int cluster_num
)
924 if(s
->current_cluster
!= cluster_num
) {
927 if(!s
->current_mapping
928 || s
->current_mapping
->begin
>cluster_num
929 || s
->current_mapping
->end
<=cluster_num
) {
930 /* binary search of mappings for file */
931 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
932 if(open_file(s
,mapping
,O_RDONLY
))
936 offset
=s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
+s
->current_mapping
->offset
);
937 if(lseek(s
->current_fd
, offset
, SEEK_SET
)!=offset
)
939 result
=read(s
->current_fd
,s
->cluster
,s
->cluster_size
);
941 s
->current_cluster
= -1;
944 s
->current_cluster
= cluster_num
;
949 static int vvfat_read(BlockDriverState
*bs
, int64_t sector_num
,
950 uint8_t *buf
, int nb_sectors
)
952 BDRVVVFATState
*s
= bs
->opaque
;
955 // fprintf(stderr,"vvfat_read: sector %d+%d\n",(int)sector_num,nb_sectors);
957 for(i
=0;i
<nb_sectors
;i
++,sector_num
++) {
958 if(sector_num
<s
->faked_sectors
) {
959 if(sector_num
<s
->first_sectors_number
)
960 memcpy(buf
+i
*0x200,&(s
->first_sectors
[sector_num
*0x200]),0x200);
961 else if(sector_num
-s
->first_sectors_number
<s
->sectors_per_fat
)
962 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
)*0x200]),0x200);
963 else if(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
<s
->sectors_per_fat
)
964 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
)*0x200]),0x200);
965 else if(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
*2<s
->sectors_for_directory
)
966 memcpy(buf
+i
*0x200,&(s
->directory
.pointer
[(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
*2)*0x200]),0x200);
968 uint32_t sector
=sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
*2,
969 sector_offset_in_cluster
=(sector
%s
->sectors_per_cluster
),
970 cluster_num
=sector
/s
->sectors_per_cluster
;
971 if(read_cluster(s
, cluster_num
) != 0) {
972 //fprintf(stderr,"failed to read cluster %d\n",(int)cluster_num);
973 // TODO: strict: return -1;
974 memset(buf
+i
*0x200,0,0x200);
977 memcpy(buf
+i
*0x200,s
->cluster
+sector_offset_in_cluster
*0x200,0x200);
983 static void print_direntry(direntry_t
* direntry
)
987 if(direntry
->attributes
==0xf) {
988 unsigned char* c
=(unsigned char*)direntry
;
990 for(i
=1;i
<11 && c
[i
] && c
[i
]!=0xff;i
+=2)
992 for(i
=14;i
<26 && c
[i
] && c
[i
]!=0xff;i
+=2)
994 for(i
=28;i
<32 && c
[i
] && c
[i
]!=0xff;i
+=2)
1000 fputc(direntry
->name
[i
],stderr
);
1001 fprintf(stderr
,"attributes=0x%02x begin=%d size=%d\n",
1002 direntry
->attributes
,
1003 direntry
->begin
,direntry
->size
);
1007 static void print_changed_sector(BlockDriverState
*bs
,int64_t sector_num
,const uint8_t *buf
)
1009 BDRVVVFATState
*s
= bs
->opaque
;
1011 if(sector_num
<s
->first_sectors_number
)
1013 if(sector_num
<s
->first_sectors_number
+s
->sectors_per_fat
*2) {
1014 int first
=((sector_num
-s
->first_sectors_number
)%s
->sectors_per_fat
);
1015 int first_fat_entry
=first
*0x200/2;
1018 fprintf(stderr
, "fat:\n");
1019 for(i
=0;i
<0x200;i
+=2) {
1020 uint16_t* f
=array_get(&(s
->fat
),first_fat_entry
+i
/2);
1021 if(memcmp(buf
+i
,f
,2))
1022 fprintf(stderr
,"%d(%d->%d) ",first_fat_entry
+i
/2,*f
,*(uint16_t*)(buf
+i
));
1024 fprintf(stderr
, "\n");
1025 } else if(sector_num
<s
->faked_sectors
) {
1026 direntry_t
* d
=(direntry_t
*)buf
;
1028 fprintf(stderr
, "directory:\n");
1029 for(i
=0;i
<0x200/sizeof(direntry_t
);i
++) {
1030 direntry_t
* d_old
=(direntry_t
*)(s
->directory
.pointer
+0x200*(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
*2)+i
*sizeof(direntry_t
));
1031 if(memcmp(d
+i
,d_old
,sizeof(direntry_t
))) {
1032 fprintf(stderr
, "old: "); print_direntry(d_old
);
1033 fprintf(stderr
, "new: "); print_direntry(d
+i
);
1034 fprintf(stderr
, "\n");
1038 int sec
=(sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
);
1039 fprintf(stderr
, "\tcluster: %d(+%d sectors)\n",sec
/s
->sectors_per_cluster
,sec
%s
->sectors_per_cluster
);
1043 char direntry_is_free(const direntry_t
* direntry
)
1045 return direntry
->name
[0]==0 || direntry
->name
[0]==0xe5;
1048 /* TODO: use this everywhere */
1049 static inline uint32_t begin_of_direntry(direntry_t
* direntry
)
1051 return le16_to_cpu(direntry
->begin
)|(le16_to_cpu(direntry
->begin_hi
)<<16);
1054 int consistency_check1(BDRVVVFATState
*s
) {
1055 /* check all mappings */
1057 for(i
=0;i
<s
->mapping
.next
;i
++) {
1058 mapping_t
* mapping
=array_get(&(s
->mapping
),i
);
1060 for(j
=mapping
->begin
;j
<mapping
->end
-1;j
++)
1061 assert(fat_get(s
,j
)==j
+1);
1062 assert(fat_get(s
,j
)==(0x7fffffff&s
->max_fat_value
));
1067 int consistency_check2(BDRVVVFATState
*s
) {
1068 /* check fat entries: consecutive fat entries should be mapped in one mapping */
1070 /* TODO: i=0 (mappings for direntries have to be sorted) */
1071 for(i
=s
->sectors_for_directory
/s
->sectors_per_cluster
;i
<s
->fat
.next
-1;i
++) {
1072 uint32_t j
=fat_get(s
,i
);
1073 if(j
!=i
+1 && j
!=0 && !fat_eof(s
,j
)) {
1074 mapping_t
* mapping
=find_mapping_for_cluster(s
,i
+1);
1075 assert(mapping
->begin
==i
+1);
1081 int consistency_check3(BDRVVVFATState
*s
) {
1082 /* check that for each file there is exactly one mapping per cluster */
1083 int i
,count_non_next
=0;
1084 for(i
=0;i
<s
->mapping
.next
;i
++) {
1085 mapping_t
* mapping
=array_get(&(s
->mapping
),i
);
1086 /* TODO: when directories are correctly adapted, add them here */
1087 assert(mapping
->begin
<mapping
->end
);
1088 if(mapping
->mode
==MODE_NORMAL
) {
1089 int j
,count
=0,count_next
=0;
1090 for(j
=0;j
<s
->mapping
.next
;j
++) {
1091 mapping_t
* other
=array_get(&(s
->mapping
),j
);
1092 if(mapping
->begin
<other
->end
&&mapping
->end
>other
->begin
)
1094 if(mapping
->end
==other
->begin
)
1097 assert(count
==1); /* no overlapping mappings */
1098 assert(count_next
==1 || count_next
==0); /* every mapping except the last one has a successor */
1103 assert(count_non_next
==1); /* only one last mapping */
1107 static inline commit_t
* commit_get_next(BDRVVVFATState
* s
)
1109 commit_t
* commit
=array_get_next(&(s
->commit
));
1110 if((commit
->buf
=malloc(s
->cluster_size
))==0) {
1118 int commit_remove(BDRVVVFATState
* s
,commit_t
* commit
)
1120 int index
=commit
-(commit_t
*)s
->commit
.pointer
;
1122 if(array_roll(&(s
->commit
),s
->commit
.next
-1,index
,1))
1128 /* TODO: the plan for write support:
1130 * it seems that the direntries are written first, then the data is committed
1131 * to the free sectors, then fat 1 is updated, then fat2.
1133 * Plan: when sectors are written, do the following:
1135 * - if they are in a directory, check if the entry has changed. if yes,
1136 * look what has changed (different strategies for name, begin & size).
1138 * if it is new (old entry is only 0's or has E5 at the start), create it,
1139 * and also create mapping, but in a special mode "undefined" (TODO),
1140 * because we cannot know which clusters belong to it yet.
1142 * if it is zeroed, or has E5 at the start, look if has just moved. If yes,
1143 * copy the entry to the new position. If no, delete the file.
1145 * - if they are in data, and the cluster is undefined, add it to the commit
1146 * list. if the cluster is defined (find_mapping), then write it into the
1147 * corresponding file.
1149 * If it is the last cluster (TODO: add a function
1150 * fat_get(s,cluster); ), make sure not to write a complete cluster_size.
1152 * If the data is in current_cluster, update s->cluster.
1154 * - if they are in fat 1, update mappings, look in the commit list
1155 * (assertions!) and if the cluster is now known (or changed from undefined
1156 * state to defined state, like when begin or size changed in a direntry),
1159 * - if they are in fat 2, make sure they match with current fat.
1163 void mapping_modify_from_direntry(BDRVVVFATState
* s
,mapping_t
* mapping
,direntry_t
* direntry
)
1165 int begin
=le16_to_cpu(direntry
->begin
),
1166 end
=begin
+le32_to_cpu(direntry
->size
)/s
->cluster_size
+1,
1168 mapping
->mode
= MODE_MODIFIED
;
1169 /* TODO: what if begin==0 (size==0)? */
1170 mapping
->begin
= begin
;
1171 /* TODO: why not just mapping->end = begin+1 ? */
1172 for(i
=begin
+1;i
<end
&& (fat_get(s
,i
)==0 || fat_get(s
,i
)==i
+1);i
++);
1176 mapping_t
* find_mapping_for_direntry(BDRVVVFATState
* s
,direntry_t
* direntry
)
1179 int dir_index
=direntry
-((direntry_t
*)s
->directory
.pointer
);
1181 /* TODO: support allocation for new clusters for directories (new/larger directory */
1182 assert(dir_index
<0x200/0x20*s
->sectors_for_directory
);
1184 for(i
=0;i
<s
->mapping
.next
;i
++) {
1185 mapping_t
* mapping
=array_get(&(s
->mapping
),i
);
1186 if(mapping
->dir_index
==dir_index
&& mapping
->offset
==0 &&
1187 mapping
->mode
!=MODE_UNDEFINED
)
1193 static inline uint32_t sector2cluster(BDRVVVFATState
* s
,off_t sector_num
)
1195 return (sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
)/s
->sectors_per_cluster
;
1198 static inline uint32_t sector_offset_in_cluster(BDRVVVFATState
* s
,off_t sector_num
)
1200 return (sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
)%s
->sectors_per_cluster
;
1203 static commit_t
* get_commit_for_cluster(BDRVVVFATState
* s
,uint32_t cluster_num
)
1206 for(i
=0;i
<s
->commit
.next
;i
++) {
1207 commit_t
* commit
=array_get(&(s
->commit
),i
);
1208 if(commit
->cluster_num
==cluster_num
)
1214 static inline commit_t
* create_or_get_commit_for_sector(BDRVVVFATState
* s
,off_t sector_num
)
1218 uint32_t cluster_num
=sector2cluster(s
,sector_num
);
1220 for(i
=0;i
<s
->commit
.next
;i
++) {
1221 commit
=array_get(&(s
->commit
),i
);
1222 if(commit
->cluster_num
==cluster_num
)
1226 commit
=commit_get_next(s
);
1227 commit
->cluster_num
=cluster_num
;
1228 /* we can ignore read errors here */
1229 read_cluster(s
,cluster_num
);
1230 memcpy(commit
->buf
,s
->cluster
,s
->cluster_size
);
1234 static direntry_t
* get_direntry_for_mapping(BDRVVVFATState
* s
,mapping_t
* mapping
)
1236 if(mapping
->mode
==MODE_UNDEFINED
)
1238 if(mapping
->dir_index
>=0x200/0x20*s
->sectors_for_directory
)
1240 return (direntry_t
*)(s
->directory
.pointer
+sizeof(direntry_t
)*mapping
->dir_index
);
1243 static void print_mappings(BDRVVVFATState
* s
)
1246 fprintf(stderr
,"mapping:\n");
1247 for(i
=0;i
<s
->mapping
.next
;i
++) {
1248 mapping_t
* m
=array_get(&(s
->mapping
),i
);
1249 direntry_t
* d
=get_direntry_for_mapping(s
,m
);
1250 fprintf(stderr
,"%02d %d-%d (%d) %s (dir: %d)",i
,(int)m
->begin
,(int)m
->end
,(int)m
->offset
,m
->filename
,m
->dir_index
);
1252 fprintf(stderr
,"\n");
1254 fprintf(stderr
,"mappings end.\n");
1257 /* TODO: statify all functions */
1259 /* This function is only meant for file contents.
1260 * It will return an error if used for other sectors. */
1261 static int write_cluster(BDRVVVFATState
* s
,uint32_t cluster_num
,const uint8_t* buf
)
1263 /* sector_offset is the sector_num relative to the first cluster */
1264 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
1265 direntry_t
* direntry
;
1266 int next_cluster
,write_size
,last_cluster
;
1269 /* if this cluster is free, return error */
1270 next_cluster
=fat_get(s
,cluster_num
);
1274 /* TODO: MODE_DIRECTORY */
1275 if(!mapping
|| mapping
->mode
==MODE_UNDEFINED
|| mapping
->mode
==MODE_DIRECTORY
)
1277 direntry
=get_direntry_for_mapping(s
,mapping
);
1281 /* get size to write */
1282 last_cluster
=fat_eof(s
,next_cluster
);
1283 write_size
=!last_cluster
?s
->cluster_size
:
1284 (le32_to_cpu(direntry
->size
)%s
->cluster_size
);
1287 //fprintf(stderr,"next_cluster: %d (%d), write_size: %d, %d, %d\n",next_cluster,s->max_fat_value-8,write_size,direntry->size,s->cluster_size);
1289 if(open_file(s
,mapping
,O_RDWR
))
1292 offset
=(cluster_num
-mapping
->begin
+mapping
->offset
)*s
->cluster_size
;
1293 if(lseek(s
->current_fd
,offset
,SEEK_SET
)!=offset
)
1295 if(write(s
->current_fd
,buf
,write_size
)!=write_size
) {
1296 lseek(s
->current_fd
,0,SEEK_END
);
1297 vvfat_close_current_file(s
);
1301 /* seek to end of file, so it doesn't get truncated */
1303 lseek(s
->current_fd
,0,SEEK_END
);
1305 ftruncate(s
->current_fd
,le32_to_cpu(direntry
->size
));
1306 vvfat_close_current_file(s
);
1309 /* update s->cluster if necessary */
1310 if(cluster_num
==s
->current_cluster
&& s
->cluster
!=buf
)
1311 memcpy(s
->cluster
,buf
,s
->cluster_size
);
1316 /* this function returns !=0 on error */
1317 int mapping_is_consistent(BDRVVVFATState
* s
,mapping_t
* mapping
)
1319 direntry_t
* direntry
=get_direntry_for_mapping(s
,mapping
);
1320 uint32_t cluster_count
=0;
1321 int commit_count
=0; /* number of commits for this file (we also write incomplete files; think "append") */
1322 //fprintf(stderr,"check direntry for %s\n",mapping->filename);
1325 assert(mapping
->begin
<mapping
->end
);
1326 for(i
=mapping
->begin
;i
<mapping
->end
-1;i
++) {
1327 if(i
<=0 || fat_get(s
,i
)!=i
+1) {
1328 /*fprintf(stderr,"the fat mapping of %d is not %d, but %d\n",
1329 i,i+1,fat_get(s,i));*/
1332 if(get_commit_for_cluster(s
,i
))
1335 if(get_commit_for_cluster(s
,i
))
1338 cluster_count
+=mapping
->end
-mapping
->begin
;
1340 i
=fat_get(s
,mapping
->end
-1);
1344 mapping
=find_mapping_for_cluster(s
,i
);
1346 //fprintf(stderr,"No mapping found for %d\n",i);
1352 if(cluster_count
!=(le32_to_cpu(direntry
->size
)+s
->cluster_size
-1)/s
->cluster_size
) {
1353 //fprintf(stderr,"cluster_count is %d, but size is %d\n",cluster_count,le32_to_cpu(direntry->size));
1360 //fprintf(stderr,"okay\n");
1364 /* TODO: remember what comes third, and what's first in this OS:
1365 * FAT, direntry or data.
1366 * If the last written sector is either last in cluster or sector_num+nb_sectors-1,
1367 * - commit every cluster for this file if mapping_is_consistent()==0
1368 * - if the last written sector is first_action, and last_action=third_action, clear commit
1371 static int commit_cluster_aux(BDRVVVFATState
* s
,commit_t
* commit
)
1373 int result
=write_cluster(s
,commit
->cluster_num
,commit
->buf
);
1378 static int commit_cluster(BDRVVVFATState
* s
,uint32_t cluster_num
)
1382 /* commit the sectors of this cluster */
1383 commit
=get_commit_for_cluster(s
,cluster_num
);
1385 return commit_cluster_aux(s
,commit
);
1389 /* this function checks the consistency for the direntry which belongs to
1390 * the mapping. if everything is found consistent, the data is committed.
1391 * this returns 0 if no error occurred (even if inconsistencies were found) */
1392 static inline int commit_data_if_consistent(BDRVVVFATState
* s
,mapping_t
* mapping
,write_action_t action
)
1394 direntry_t
* direntry
;
1399 //fprintf(stderr,"7\n");
1400 #define d(x) fprintf(stderr,#x "\n")
1401 direntry
=get_direntry_for_mapping(s
,mapping
);
1405 assert(action
==WRITE_FAT
|| action
==WRITE_DIRENTRY
|| action
==WRITE_DATA
);
1408 //fprintf(stderr,"mapping: 0x%x s=0x%x\n",(uint32_t)mapping,(uint32_t)s);
1409 /*fprintf(stderr,"commit? file=%s, action=%s\n",
1410 mapping->filename,action==WRITE_FAT?"fat":action==WRITE_DIRENTRY?"direntry":"data");*/
1413 if(s
->action
[2]==WRITE_UNDEFINED
) {
1415 for(i
=2;i
>0 && s
->action
[i
-1]==WRITE_UNDEFINED
;i
--);
1416 if(i
>0 && action
!=s
->action
[i
-1])
1417 s
->action
[i
]=action
;
1418 assert(i
<2 || s
->action
[0]!=s
->action
[2]);
1422 if(mapping_is_consistent(s
,mapping
)==0) {
1423 uint32_t cluster_num
=begin_of_direntry(direntry
);
1424 off_t remaining_bytes
=le32_to_cpu(direntry
->size
);
1425 //fprintf(stderr,"the data for %s was found consistent\n",mapping->filename);
1426 while(remaining_bytes
>0) {
1427 commit_t
* commit
=get_commit_for_cluster(s
,cluster_num
);
1431 //fprintf(stderr,"commit_cluster %d (%d), remaining: %d\n",cluster_num,s->max_fat_value-15,(int)remaining_bytes);
1432 assert(cluster_num
>1);
1433 assert(cluster_num
<s
->max_fat_value
-15);
1434 if(commit_cluster(s
,cluster_num
)) {
1435 fprintf(stderr
,"error committing cluster %d\n",cluster_num
);
1438 cluster_num
=fat_get(s
,cluster_num
);
1439 remaining_bytes
-=s
->cluster_size
;
1440 /* TODO: if(action==s->action[2]) {
1441 commit_t* commit=get_commit_for_cluster(s,cluster_num);
1442 commit_remove(s,commit);
1446 //print_mappings(s);
1447 //fprintf(stderr,"finish vvfat_write\n");
1451 static int vvfat_write(BlockDriverState
*bs
, int64_t sector_num
,
1452 const uint8_t *buf
, int nb_sectors
)
1454 BDRVVVFATState
*s
= bs
->opaque
;
1457 /* fprintf(stderr,"vvfat_write %d+%d (%s)\n",(int)sector_num,nb_sectors,
1458 (sector_num>=s->faked_sectors?"data":
1459 (sector_num>=s->first_sectors_number+2*s->sectors_per_fat?"directory":
1460 (sector_num>=s->first_sectors_number+s->sectors_per_fat?"fat 2":
1461 (sector_num>=s->first_sectors_number?"fat 1":"boot sector"))))); */
1463 for(i
=0;i
<nb_sectors
;i
++,sector_num
++,buf
+=0x200) {
1464 print_changed_sector(bs
,sector_num
,buf
);
1466 if(sector_num
<s
->first_sectors_number
) {
1467 /* change the bootsector or partition table? no! */
1469 } else if(sector_num
<s
->first_sectors_number
+s
->sectors_per_fat
) {
1471 int fat_entries_per_cluster
=s
->cluster_size
*8/s
->fat_type
;
1472 int first_cluster
=(sector_num
-s
->first_sectors_number
)*fat_entries_per_cluster
,i
;
1473 mapping_t
* mapping
=0;
1476 memcpy(s
->fat
.pointer
+0x200*(sector_num
-s
->first_sectors_number
),
1479 /* for each changed FAT entry, */
1480 for(i
=0;i
<fat_entries_per_cluster
;i
++) {
1483 /* TODO: MODE_DIRENTRY */
1484 if(first_cluster
+i
<s
->sectors_for_directory
/s
->sectors_per_cluster
)
1487 new_value
=fat_get(s
,first_cluster
+i
);
1489 /* check the current fat entry */
1490 if(new_value
<2 || (new_value
>=s
->max_fat_value
-0xf && !fat_eof(s
,new_value
))) {
1491 /* free, reserved or bad cluster */
1492 mapping
=find_mapping_for_cluster(s
,first_cluster
+i
);
1493 //assert(!mapping || mapping->mode==MODE_DELETED);
1494 if(mapping
&& mapping
->mode
==MODE_DELETED
&&
1495 first_cluster
+i
+1==mapping
->end
)
1496 array_remove(&(s
->mapping
),mapping
-(mapping_t
*)s
->mapping
.pointer
);
1501 /* get the mapping for the current entry */
1502 if(!mapping
|| mapping
->begin
>new_value
|| mapping
->end
<=new_value
) {
1503 mapping
=find_mapping_for_cluster(s
,first_cluster
+i
);
1507 fprintf(stderr
,"fat_get(%d)=%d\n",first_cluster
+i
,new_value
);
1508 /* TODO: what if there's no mapping? this is valid. */
1509 /* TODO: refactor the rest of this clause so it can be called when the direntry changes, too */
1512 if(new_value
>1 && new_value
<s
->max_fat_value
-0xf) {
1513 /* the cluster new_value points to is valid */
1515 if(first_cluster
+i
+1==new_value
) {
1516 /* consecutive cluster */
1517 if(mapping
->end
<=new_value
)
1518 mapping
->end
=new_value
+1;
1520 mapping_t
* next_mapping
;
1522 /* the current mapping ends here */
1523 mapping
->end
=first_cluster
+i
+1;
1525 /* the next mapping */
1526 next_mapping
=find_mapping_for_cluster(s
,new_value
);
1528 assert(mapping
!=next_mapping
);
1529 /* assert next mapping's filename is the same */
1530 assert(next_mapping
->filename
==mapping
->filename
);
1531 assert(next_mapping
->dir_index
==mapping
->dir_index
);
1532 /* assert next mapping is MODIFIED or UNDEFINED */
1533 assert(next_mapping
->mode
==MODE_MODIFIED
|| next_mapping
->mode
==MODE_UNDEFINED
);
1535 int index
=find_mapping_for_cluster_aux(s
,new_value
,0,s
->mapping
.next
);
1536 next_mapping
=array_insert(&(s
->mapping
),index
,1);
1537 next_mapping
->filename
=mapping
->filename
;
1538 next_mapping
->dir_index
=mapping
->dir_index
;
1539 next_mapping
->mode
=MODE_MODIFIED
;
1540 next_mapping
->begin
=0;
1542 /* adjust offset of next mapping */
1543 next_mapping
->offset
=mapping
->offset
+mapping
->end
-mapping
->begin
;
1544 /* set begin and possible end */
1545 if(next_mapping
->begin
!=new_value
) {
1546 next_mapping
->begin
=new_value
;
1547 next_mapping
->end
=new_value
+1;
1549 if(commit_data_if_consistent(s
,mapping
,WRITE_FAT
))
1553 } else if(fat_eof(s
,new_value
)) {
1554 /* the last cluster of the file */
1555 mapping
->end
=first_cluster
+i
+1;
1556 if(commit_data_if_consistent(s
,mapping
,WRITE_FAT
))
1561 } else if(sector_num
<s
->first_sectors_number
+2*s
->sectors_per_fat
) {
1562 /* FAT 2: check if it is the same as FAT 1 */
1563 if(memcmp(array_get(&(s
->fat
),sector_num
-s
->first_sectors_number
),buf
,0x200))
1564 return -1; /* mismatch */
1565 } else if(sector_num
<s
->faked_sectors
) {
1567 /* - if they are in a directory, check if the entry has changed.
1568 * if yes, look what has changed (different strategies for name,
1571 * if it is new (old entry is only 0's or has E5 at the start),
1572 * create it, and also create mapping, but in a special mode
1573 * "undefined", because we cannot know which clusters belong
1576 * if it is zeroed, or has E5 at the start, look if has just
1577 * moved. If yes, copy the entry to the new position. If no,
1580 mapping_t
* dir_mapping
=find_mapping_for_cluster(s
,sector2cluster(s
,sector_num
));
1581 direntry_t
*original
=array_get(&(s
->directory
),sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
);
1582 direntry_t
*new_
=(direntry_t
*)buf
;
1583 int first_dir_index
=(sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
)*0x200/0x20;
1587 fprintf(stderr
,"direntry: consistency check\n");
1589 if(s
->commit
.next
==0) {
1590 consistency_check1(s
);
1591 consistency_check2(s
);
1592 consistency_check3(s
);
1596 assert(sizeof(direntry_t
)==0x20);
1598 for(j
=0;j
<0x200/0x20;j
++) {
1599 //fprintf(stderr,"compare direntry %d: 0x%x,0x%x\n",j,(uint32_t)original+j,(uint32_t)new_+j);
1600 if(memcmp(original
+j
,new_
+j
,sizeof(direntry_t
))) {
1601 //fprintf(stderr,"different\n");
1602 /* TODO: in create_short_filename, 0xe5->0x05 is not yet handled! */
1603 if(direntry_is_free(original
+j
)) {
1608 if(new_
[j
].attributes
==0xf)
1609 continue; /* long entry */
1612 //fprintf(stderr,"sector: %d cluster: %d\n",(int)sector_num,(int)sector2cluster(s,sector_num));
1614 /* construct absolute path */
1615 strncpy(buffer
,dir_mapping
->filename
,4096);
1620 if(long2unix_name(buffer
+i
+1,4096-i
-1,new_
+j
))
1623 /* new file/directory */
1624 if(new_
[j
].attributes
&0x10) {
1626 #define SEVENFIVEFIVE
1628 #define SEVENFIVEFIVE ,0755
1630 if(mkdir(buffer SEVENFIVEFIVE
))
1632 /* TODO: map direntry.begin as directory, together with new array_t direntries */
1635 fd
=open(buffer
,O_CREAT
|O_EXCL
,0644);
1641 /* create mapping */
1642 i
=find_mapping_for_cluster_aux(s
,begin_of_direntry(new_
+j
),0,s
->mapping
.next
);
1643 mapping
=array_insert(&(s
->mapping
),i
,1);
1644 mapping
->filename
=strdup(buffer
);
1646 /* back pointer to direntry */
1647 mapping
->dir_index
=first_dir_index
+j
;
1648 /* set mode to modified */
1649 mapping
->mode
=MODE_MODIFIED
;
1650 /* set begin to direntry.begin */
1651 mapping
->begin
=begin_of_direntry(new_
+j
);
1652 /* set end to begin+1 */
1653 mapping
->end
=mapping
->begin
+1;
1654 /* commit file contents */
1655 if(commit_data_if_consistent(s
,mapping
,WRITE_DIRENTRY
)) {
1656 fprintf(stderr
,"error committing file contents for new file %s!\n",buffer
);
1659 } else if(direntry_is_free(new_
+j
)) {
1661 /* TODO: delete file */
1662 /* TODO: write direntry */
1663 /* TODO: modify mapping: set mode=deleted */
1666 mapping_t
* mapping
=0;
1667 /* if direntry.begin has changed,
1668 * set mode to modified,
1671 /* TODO: handle rename */
1672 assert(!memcmp(new_
[j
].name
,original
[j
].name
,11));
1673 //fprintf(stderr,"1\n");
1674 if(new_
[j
].begin
!=original
[j
].begin
|| new_
[j
].size
/s
->cluster_size
!=original
[j
].size
/s
->cluster_size
) {
1675 //fprintf(stderr,"2\n");
1676 mapping
= find_mapping_for_direntry(s
,original
+j
);
1677 //fprintf(stderr,"3\n");
1678 if(!mapping
) /* this should never happen! */
1680 mapping_modify_from_direntry(s
,mapping
,new_
+j
);
1681 //fprintf(stderr,"4\n");
1682 if(commit_data_if_consistent(s
,mapping
,WRITE_DIRENTRY
)) {
1683 fprintf(stderr
,"big error\n");
1687 /* TODO: handle modified times and other attributes */
1689 //fprintf(stderr,"5: mapping=0x%x, s=0x%x, s->mapping.pointer=0x%x\n",(uint32_t)mapping,(uint32_t)s,(uint32_t)s->mapping.pointer);
1690 //fprintf(stderr,"6\n");
1694 /* write back direntries */
1695 memcpy(original
,new_
,0x200);
1698 off_t sector
=sector_num
-s
->first_sectors_number
-2*s
->sectors_per_fat
;
1699 off_t cluster
=sector
/s
->sectors_per_cluster
;
1700 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster
);
1701 if(mapping
&& mapping
->mode
==MODE_DELETED
)
1702 return -3; /* this is an error: no writes to these clusters before committed */
1704 /* as of yet, undefined: put into commits */
1705 commit_t
* commit
=create_or_get_commit_for_sector(s
,sector_num
);
1708 return -1; /* out of memory */
1709 memcpy(commit
->buf
+0x200*sector_offset_in_cluster(s
,sector_num
),buf
,0x200);
1711 //fprintf(stderr,"mapping: 0x%x\n",(uint32_t)mapping);
1712 if(commit_data_if_consistent(s
,mapping
,WRITE_DATA
))
1720 static void vvfat_close(BlockDriverState
*bs
)
1722 BDRVVVFATState
*s
= bs
->opaque
;
1724 vvfat_close_current_file(s
);
1725 array_free(&(s
->fat
));
1726 array_free(&(s
->directory
));
1727 array_free(&(s
->mapping
));
1732 BlockDriver bdrv_vvfat
= {
1734 sizeof(BDRVVVFATState
),