]> git.proxmox.com Git - mirror_qemu.git/blame - block/vvfat.c
qapi: Drop superfluous qapi_enum_parse() parameter max
[mirror_qemu.git] / block / vvfat.c
CommitLineData
7ad9be64 1/* vim:set shiftwidth=4 ts=4: */
de167e41
FB
2/*
3 * QEMU Block driver for virtual VFAT (shadows a local directory)
5fafdf24 4 *
a046433a 5 * Copyright (c) 2004,2005 Johannes E. Schindelin
5fafdf24 6 *
de167e41
FB
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
80c71a24 25#include "qemu/osdep.h"
de167e41 26#include <dirent.h>
da34e65c 27#include "qapi/error.h"
737e150e 28#include "block/block_int.h"
1de7afc9 29#include "qemu/module.h"
58369e22 30#include "qemu/bswap.h"
795c40b8 31#include "migration/blocker.h"
7ad9be64 32#include "qapi/qmp/qbool.h"
d49b6836 33#include "qapi/qmp/qstring.h"
f348b6d1 34#include "qemu/cutils.h"
de167e41 35
a046433a
FB
36#ifndef S_IWGRP
37#define S_IWGRP 0
38#endif
39#ifndef S_IWOTH
40#define S_IWOTH 0
41#endif
42
43/* TODO: add ":bootsector=blabla.img:" */
44/* LATER TODO: add automatic boot sector generation from
45 BOOTEASY.ASM and Ranish Partition Manager
5fafdf24 46 Note that DOS assumes the system files to be the first files in the
a046433a
FB
47 file system (test if the boot sector still relies on that fact)! */
48/* MAYBE TODO: write block-visofs.c */
49/* TODO: call try_commit() only after a timeout */
50
51/* #define DEBUG */
52
53#ifdef DEBUG
54
55#define DLOG(a) a
56
3f47aa8c 57static void checkpoint(void);
de167e41 58
a046433a
FB
59#ifdef __MINGW32__
60void nonono(const char* file, int line, const char* msg) {
61 fprintf(stderr, "Nonono! %s:%d %s\n", file, line, msg);
62 exit(-5);
63}
64#undef assert
6bcb76c3 65#define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0)
a046433a
FB
66#endif
67
68#else
69
70#define DLOG(a)
71
72#endif
de167e41 73
63d261cb
HP
74/* bootsector OEM name. see related compatibility problems at:
75 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
76 * http://seasip.info/Misc/oemid.html
77 */
78#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
79
8c4517fd
HP
80#define DIR_DELETED 0xe5
81#define DIR_KANJI DIR_DELETED
82#define DIR_KANJI_FAKE 0x05
83#define DIR_FREE 0x00
84
de167e41 85/* dynamic array functions */
c227f099 86typedef struct array_t {
de167e41
FB
87 char* pointer;
88 unsigned int size,next,item_size;
c227f099 89} array_t;
de167e41 90
c227f099 91static inline void array_init(array_t* array,unsigned int item_size)
de167e41 92{
511d2b14 93 array->pointer = NULL;
de167e41
FB
94 array->size=0;
95 array->next=0;
96 array->item_size=item_size;
97}
98
c227f099 99static inline void array_free(array_t* array)
de167e41 100{
ce137829 101 g_free(array->pointer);
de167e41
FB
102 array->size=array->next=0;
103}
104
a046433a 105/* does not automatically grow */
c227f099 106static inline void* array_get(array_t* array,unsigned int index) {
a046433a
FB
107 assert(index < array->next);
108 return array->pointer + index * array->item_size;
109}
110
c227f099 111static inline int array_ensure_allocated(array_t* array, int index)
a046433a
FB
112{
113 if((index + 1) * array->item_size > array->size) {
d6a7e54e
HP
114 int new_size = (index + 32) * array->item_size;
115 array->pointer = g_realloc(array->pointer, new_size);
116 if (!array->pointer)
117 return -1;
f80256b7 118 memset(array->pointer + array->size, 0, new_size - array->size);
d6a7e54e
HP
119 array->size = new_size;
120 array->next = index + 1;
de167e41 121 }
a046433a
FB
122
123 return 0;
de167e41
FB
124}
125
c227f099 126static inline void* array_get_next(array_t* array) {
a046433a 127 unsigned int next = array->next;
a046433a
FB
128
129 if (array_ensure_allocated(array, next) < 0)
d6a7e54e 130 return NULL;
a046433a
FB
131
132 array->next = next + 1;
9be38598 133 return array_get(array, next);
de167e41
FB
134}
135
c227f099 136static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
de167e41 137 if((array->next+count)*array->item_size>array->size) {
d6a7e54e
HP
138 int increment=count*array->item_size;
139 array->pointer=g_realloc(array->pointer,array->size+increment);
140 if(!array->pointer)
511d2b14 141 return NULL;
d6a7e54e 142 array->size+=increment;
de167e41
FB
143 }
144 memmove(array->pointer+(index+count)*array->item_size,
d6a7e54e
HP
145 array->pointer+index*array->item_size,
146 (array->next-index)*array->item_size);
de167e41
FB
147 array->next+=count;
148 return array->pointer+index*array->item_size;
149}
150
151/* this performs a "roll", so that the element which was at index_from becomes
152 * index_to, but the order of all other elements is preserved. */
c227f099 153static inline int array_roll(array_t* array,int index_to,int index_from,int count)
de167e41
FB
154{
155 char* buf;
156 char* from;
157 char* to;
158 int is;
159
160 if(!array ||
d6a7e54e
HP
161 index_to<0 || index_to>=array->next ||
162 index_from<0 || index_from>=array->next)
163 return -1;
3b46e624 164
de167e41 165 if(index_to==index_from)
d6a7e54e 166 return 0;
de167e41
FB
167
168 is=array->item_size;
169 from=array->pointer+index_from*is;
170 to=array->pointer+index_to*is;
7267c094 171 buf=g_malloc(is*count);
de167e41
FB
172 memcpy(buf,from,is*count);
173
174 if(index_to<index_from)
d6a7e54e 175 memmove(to+is*count,to,from-to);
de167e41 176 else
d6a7e54e 177 memmove(from,from+is*count,to-from);
3b46e624 178
de167e41
FB
179 memcpy(to,buf,is*count);
180
ce137829 181 g_free(buf);
de167e41
FB
182
183 return 0;
184}
185
c227f099 186static inline int array_remove_slice(array_t* array,int index, int count)
de167e41 187{
a046433a
FB
188 assert(index >=0);
189 assert(count > 0);
190 assert(index + count <= array->next);
191 if(array_roll(array,array->next-1,index,count))
d6a7e54e 192 return -1;
a046433a 193 array->next -= count;
de167e41
FB
194 return 0;
195}
196
c227f099 197static int array_remove(array_t* array,int index)
a046433a
FB
198{
199 return array_remove_slice(array, index, 1);
200}
201
202/* return the index for a given member */
c227f099 203static int array_index(array_t* array, void* pointer)
a046433a
FB
204{
205 size_t offset = (char*)pointer - array->pointer;
a046433a
FB
206 assert((offset % array->item_size) == 0);
207 assert(offset/array->item_size < array->next);
208 return offset/array->item_size;
209}
210
de167e41 211/* These structures are used to fake a disk and the VFAT filesystem.
541dc0d4 212 * For this reason we need to use QEMU_PACKED. */
de167e41 213
c227f099 214typedef struct bootsector_t {
de167e41
FB
215 uint8_t jump[3];
216 uint8_t name[8];
217 uint16_t sector_size;
218 uint8_t sectors_per_cluster;
219 uint16_t reserved_sectors;
220 uint8_t number_of_fats;
221 uint16_t root_entries;
a046433a 222 uint16_t total_sectors16;
de167e41
FB
223 uint8_t media_type;
224 uint16_t sectors_per_fat;
225 uint16_t sectors_per_track;
226 uint16_t number_of_heads;
227 uint32_t hidden_sectors;
228 uint32_t total_sectors;
229 union {
230 struct {
d6a7e54e 231 uint8_t drive_number;
92e28d82 232 uint8_t reserved1;
d6a7e54e
HP
233 uint8_t signature;
234 uint32_t id;
235 uint8_t volume_label[11];
92e28d82
HP
236 uint8_t fat_type[8];
237 uint8_t ignored[0x1c0];
d6a7e54e
HP
238 } QEMU_PACKED fat16;
239 struct {
240 uint32_t sectors_per_fat;
241 uint16_t flags;
242 uint8_t major,minor;
92e28d82 243 uint32_t first_cluster_of_root_dir;
d6a7e54e
HP
244 uint16_t info_sector;
245 uint16_t backup_boot_sector;
92e28d82
HP
246 uint8_t reserved[12];
247 uint8_t drive_number;
248 uint8_t reserved1;
249 uint8_t signature;
250 uint32_t id;
251 uint8_t volume_label[11];
252 uint8_t fat_type[8];
253 uint8_t ignored[0x1a4];
d6a7e54e 254 } QEMU_PACKED fat32;
de167e41 255 } u;
de167e41 256 uint8_t magic[2];
541dc0d4 257} QEMU_PACKED bootsector_t;
de167e41 258
b570094d
TS
259typedef struct {
260 uint8_t head;
261 uint8_t sector;
262 uint8_t cylinder;
c227f099 263} mbr_chs_t;
b570094d 264
c227f099 265typedef struct partition_t {
de167e41 266 uint8_t attributes; /* 0x80 = bootable */
c227f099 267 mbr_chs_t start_CHS;
b570094d 268 uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
c227f099 269 mbr_chs_t end_CHS;
de167e41 270 uint32_t start_sector_long;
b570094d 271 uint32_t length_sector_long;
541dc0d4 272} QEMU_PACKED partition_t;
de167e41 273
c227f099 274typedef struct mbr_t {
b570094d
TS
275 uint8_t ignored[0x1b8];
276 uint32_t nt_id;
277 uint8_t ignored2[2];
c227f099 278 partition_t partition[4];
de167e41 279 uint8_t magic[2];
541dc0d4 280} QEMU_PACKED mbr_t;
de167e41 281
c227f099 282typedef struct direntry_t {
f671d173 283 uint8_t name[8 + 3];
de167e41
FB
284 uint8_t attributes;
285 uint8_t reserved[2];
286 uint16_t ctime;
287 uint16_t cdate;
288 uint16_t adate;
289 uint16_t begin_hi;
290 uint16_t mtime;
291 uint16_t mdate;
292 uint16_t begin;
293 uint32_t size;
541dc0d4 294} QEMU_PACKED direntry_t;
de167e41
FB
295
296/* this structure are used to transparently access the files */
297
c227f099 298typedef struct mapping_t {
a046433a
FB
299 /* begin is the first cluster, end is the last+1 */
300 uint32_t begin,end;
de167e41
FB
301 /* as s->directory is growable, no pointer may be used here */
302 unsigned int dir_index;
a046433a
FB
303 /* the clusters of a file may be in any order; this points to the first */
304 int first_mapping_index;
305 union {
d6a7e54e
HP
306 /* offset is
307 * - the offset in the file (in clusters) for a file, or
ad05b318 308 * - the next cluster of the directory for a directory
d6a7e54e
HP
309 */
310 struct {
311 uint32_t offset;
312 } file;
313 struct {
314 int parent_mapping_index;
315 int first_dir_index;
316 } dir;
a046433a
FB
317 } info;
318 /* path contains the full path, i.e. it always starts with s->path */
319 char* path;
320
ad05b318
HP
321 enum {
322 MODE_UNDEFINED = 0,
323 MODE_NORMAL = 1,
324 MODE_MODIFIED = 2,
325 MODE_DIRECTORY = 4,
326 MODE_DELETED = 8,
327 } mode;
a046433a 328 int read_only;
c227f099 329} mapping_t;
de167e41 330
a046433a 331#ifdef DEBUG
c227f099
AL
332static void print_direntry(const struct direntry_t*);
333static void print_mapping(const struct mapping_t* mapping);
a046433a 334#endif
de167e41
FB
335
336/* here begins the real VVFAT driver */
337
338typedef struct BDRVVVFATState {
848c66e8 339 CoMutex lock;
a046433a 340 BlockDriverState* bs; /* pointer to parent */
de167e41 341 unsigned char first_sectors[0x40*0x200];
3b46e624 342
de167e41 343 int fat_type; /* 16 or 32 */
c227f099 344 array_t fat,directory,mapping;
d5941dda 345 char volume_label[11];
3b46e624 346
4dc705dc
HP
347 uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
348
de167e41
FB
349 unsigned int cluster_size;
350 unsigned int sectors_per_cluster;
351 unsigned int sectors_per_fat;
a046433a 352 uint32_t last_cluster_of_root_directory;
6817efea
HP
353 /* how many entries are available in root directory (0 for FAT32) */
354 uint16_t root_entries;
de167e41
FB
355 uint32_t sector_count; /* total number of sectors of the partition */
356 uint32_t cluster_count; /* total number of clusters of this partition */
de167e41 357 uint32_t max_fat_value;
4dc705dc
HP
358 uint32_t offset_to_fat;
359 uint32_t offset_to_root_dir;
3b46e624 360
de167e41 361 int current_fd;
c227f099 362 mapping_t* current_mapping;
a046433a
FB
363 unsigned char* cluster; /* points to current cluster */
364 unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
de167e41
FB
365 unsigned int current_cluster;
366
367 /* write support */
a046433a 368 char* qcow_filename;
eecc7747 369 BdrvChild* qcow;
a046433a
FB
370 void* fat2;
371 char* used_clusters;
c227f099 372 array_t commits;
a046433a
FB
373 const char* path;
374 int downcase_short_names;
3397f0cb
KW
375
376 Error *migration_blocker;
de167e41
FB
377} BDRVVVFATState;
378
b570094d
TS
379/* take the sector position spos and convert it to Cylinder/Head/Sector position
380 * if the position is outside the specified geometry, fill maximum value for CHS
381 * and return 1 to signal overflow.
382 */
4480e0f9
MA
383static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
384{
b570094d 385 int head,sector;
4480e0f9
MA
386 sector = spos % secs; spos /= secs;
387 head = spos % heads; spos /= heads;
388 if (spos >= cyls) {
b570094d
TS
389 /* Overflow,
390 it happens if 32bit sector positions are used, while CHS is only 24bit.
391 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
392 chs->head = 0xFF;
393 chs->sector = 0xFF;
394 chs->cylinder = 0xFF;
395 return 1;
396 }
397 chs->head = (uint8_t)head;
398 chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
399 chs->cylinder = (uint8_t)spos;
400 return 0;
401}
de167e41 402
4480e0f9 403static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
de167e41
FB
404{
405 /* TODO: if the files mbr.img and bootsect.img exist, use them */
c227f099
AL
406 mbr_t* real_mbr=(mbr_t*)s->first_sectors;
407 partition_t* partition = &(real_mbr->partition[0]);
b570094d 408 int lba;
de167e41
FB
409
410 memset(s->first_sectors,0,512);
3b46e624 411
b570094d
TS
412 /* Win NT Disk Signature */
413 real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
414
de167e41 415 partition->attributes=0x80; /* bootable */
b570094d
TS
416
417 /* LBA is used when partition is outside the CHS geometry */
4dc705dc 418 lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
4480e0f9
MA
419 cyls, heads, secs);
420 lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
421 cyls, heads, secs);
b570094d
TS
422
423 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
4dc705dc 424 partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
f91cbefe 425 partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
4dc705dc 426 - s->offset_to_bootsector);
b570094d 427
a046433a 428 /* FAT12/FAT16/FAT32 */
b570094d
TS
429 /* DOS uses different types when partition is LBA,
430 probably to prevent older versions from using CHS on them */
5f5b29df
HP
431 partition->fs_type = s->fat_type == 12 ? 0x1 :
432 s->fat_type == 16 ? (lba ? 0xe : 0x06) :
433 /*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
de167e41
FB
434
435 real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
436}
437
a046433a
FB
438/* direntry functions */
439
09ec4119 440static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
de167e41 441{
09ec4119
HP
442 int number_of_entries, i;
443 glong length;
444 direntry_t *entry;
445
446 gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
447 if (!longname) {
448 fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
449 return NULL;
de167e41 450 }
de167e41 451
78ee96de 452 number_of_entries = DIV_ROUND_UP(length * 2, 26);
de167e41
FB
453
454 for(i=0;i<number_of_entries;i++) {
d6a7e54e
HP
455 entry=array_get_next(&(s->directory));
456 entry->attributes=0xf;
457 entry->reserved[0]=0;
458 entry->begin=0;
459 entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
de167e41 460 }
1e080d5d 461 for(i=0;i<26*number_of_entries;i++) {
d6a7e54e
HP
462 int offset=(i%26);
463 if(offset<10) offset=1+offset;
464 else if(offset<22) offset=14+offset-10;
465 else offset=28+offset-22;
466 entry=array_get(&(s->directory),s->directory.next-1-(i/26));
09ec4119
HP
467 if (i >= 2 * length + 2) {
468 entry->name[offset] = 0xff;
469 } else if (i % 2 == 0) {
470 entry->name[offset] = longname[i / 2] & 0xff;
471 } else {
472 entry->name[offset] = longname[i / 2] >> 8;
473 }
de167e41 474 }
09ec4119 475 g_free(longname);
de167e41
FB
476 return array_get(&(s->directory),s->directory.next-number_of_entries);
477}
478
c227f099 479static char is_free(const direntry_t* direntry)
a046433a 480{
8c4517fd 481 return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
a046433a
FB
482}
483
c227f099 484static char is_volume_label(const direntry_t* direntry)
a046433a
FB
485{
486 return direntry->attributes == 0x28;
487}
488
c227f099 489static char is_long_name(const direntry_t* direntry)
a046433a
FB
490{
491 return direntry->attributes == 0xf;
492}
493
c227f099 494static char is_short_name(const direntry_t* direntry)
a046433a
FB
495{
496 return !is_volume_label(direntry) && !is_long_name(direntry)
d6a7e54e 497 && !is_free(direntry);
a046433a
FB
498}
499
c227f099 500static char is_directory(const direntry_t* direntry)
a046433a 501{
8c4517fd 502 return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
a046433a
FB
503}
504
c227f099 505static inline char is_dot(const direntry_t* direntry)
a046433a
FB
506{
507 return is_short_name(direntry) && direntry->name[0] == '.';
508}
509
c227f099 510static char is_file(const direntry_t* direntry)
a046433a
FB
511{
512 return is_short_name(direntry) && !is_directory(direntry);
513}
514
c227f099 515static inline uint32_t begin_of_direntry(const direntry_t* direntry)
a046433a
FB
516{
517 return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
518}
519
c227f099 520static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
a046433a
FB
521{
522 return le32_to_cpu(direntry->size);
523}
524
c227f099 525static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
a046433a
FB
526{
527 direntry->begin = cpu_to_le16(begin & 0xffff);
528 direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
529}
530
0c36111f
HP
531static uint8_t to_valid_short_char(gunichar c)
532{
533 c = g_unichar_toupper(c);
534 if ((c >= '0' && c <= '9') ||
535 (c >= 'A' && c <= 'Z') ||
536 strchr("$%'-_@~`!(){}^#&", c) != 0) {
537 return c;
538 } else {
539 return 0;
540 }
541}
542
543static direntry_t *create_short_filename(BDRVVVFATState *s,
339cebcc
HP
544 const char *filename,
545 unsigned int directory_start)
0c36111f 546{
339cebcc 547 int i, j = 0;
0c36111f
HP
548 direntry_t *entry = array_get_next(&(s->directory));
549 const gchar *p, *last_dot = NULL;
550 gunichar c;
551 bool lossy_conversion = false;
7c8730d4 552 char tail[8];
0c36111f
HP
553
554 if (!entry) {
555 return NULL;
556 }
557 memset(entry->name, 0x20, sizeof(entry->name));
558
559 /* copy filename and search last dot */
560 for (p = filename; ; p = g_utf8_next_char(p)) {
561 c = g_utf8_get_char(p);
562 if (c == '\0') {
563 break;
564 } else if (c == '.') {
565 if (j == 0) {
566 /* '.' at start of filename */
567 lossy_conversion = true;
568 } else {
569 if (last_dot) {
570 lossy_conversion = true;
571 }
572 last_dot = p;
573 }
574 } else if (!last_dot) {
575 /* first part of the name; copy it */
576 uint8_t v = to_valid_short_char(c);
577 if (j < 8 && v) {
578 entry->name[j++] = v;
579 } else {
580 lossy_conversion = true;
581 }
582 }
583 }
584
585 /* copy extension (if any) */
586 if (last_dot) {
587 j = 0;
588 for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
589 c = g_utf8_get_char(p);
590 if (c == '\0') {
591 break;
592 } else {
593 /* extension; copy it */
594 uint8_t v = to_valid_short_char(c);
595 if (j < 3 && v) {
596 entry->name[8 + (j++)] = v;
597 } else {
598 lossy_conversion = true;
599 }
600 }
601 }
602 }
339cebcc 603
8c4517fd
HP
604 if (entry->name[0] == DIR_KANJI) {
605 entry->name[0] = DIR_KANJI_FAKE;
78f002c9
HP
606 }
607
339cebcc
HP
608 /* numeric-tail generation */
609 for (j = 0; j < 8; j++) {
610 if (entry->name[j] == ' ') {
611 break;
612 }
613 }
614 for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
615 direntry_t *entry1;
616 if (i > 0) {
7c8730d4
HR
617 int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
618 assert(len <= 7);
339cebcc
HP
619 memcpy(entry->name + MIN(j, 8 - len), tail, len);
620 }
621 for (entry1 = array_get(&(s->directory), directory_start);
622 entry1 < entry; entry1++) {
623 if (!is_long_name(entry1) &&
624 !memcmp(entry1->name, entry->name, 11)) {
625 break; /* found dupe */
626 }
627 }
628 if (entry1 == entry) {
629 /* no dupe found */
630 return entry;
631 }
632 }
633 return NULL;
0c36111f
HP
634}
635
de167e41
FB
636/* fat functions */
637
c227f099 638static inline uint8_t fat_chksum(const direntry_t* entry)
de167e41
FB
639{
640 uint8_t chksum=0;
641 int i;
642
f671d173
SW
643 for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
644 chksum = (((chksum & 0xfe) >> 1) |
645 ((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
5606c220 646 }
3b46e624 647
de167e41
FB
648 return chksum;
649}
650
651/* if return_time==0, this returns the fat_date, else the fat_time */
652static uint16_t fat_datetime(time_t time,int return_time) {
653 struct tm* t;
de167e41 654 struct tm t1;
6ab00cee 655 t = &t1;
de167e41 656 localtime_r(&time,t);
de167e41 657 if(return_time)
d6a7e54e 658 return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
de167e41
FB
659 return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
660}
661
662static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
663{
a046433a 664 if(s->fat_type==32) {
d6a7e54e
HP
665 uint32_t* entry=array_get(&(s->fat),cluster);
666 *entry=cpu_to_le32(value);
de167e41 667 } else if(s->fat_type==16) {
d6a7e54e
HP
668 uint16_t* entry=array_get(&(s->fat),cluster);
669 *entry=cpu_to_le16(value&0xffff);
de167e41 670 } else {
d6a7e54e
HP
671 int offset = (cluster*3/2);
672 unsigned char* p = array_get(&(s->fat), offset);
a046433a 673 switch (cluster&1) {
d6a7e54e
HP
674 case 0:
675 p[0] = value&0xff;
676 p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
677 break;
678 case 1:
679 p[0] = (p[0]&0xf) | ((value&0xf)<<4);
680 p[1] = (value>>4);
681 break;
682 }
de167e41
FB
683 }
684}
685
686static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
687{
a046433a 688 if(s->fat_type==32) {
d6a7e54e
HP
689 uint32_t* entry=array_get(&(s->fat),cluster);
690 return le32_to_cpu(*entry);
de167e41 691 } else if(s->fat_type==16) {
d6a7e54e
HP
692 uint16_t* entry=array_get(&(s->fat),cluster);
693 return le16_to_cpu(*entry);
de167e41 694 } else {
d6a7e54e
HP
695 const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
696 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
de167e41
FB
697 }
698}
699
700static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
701{
702 if(fat_entry>s->max_fat_value-8)
d6a7e54e 703 return -1;
de167e41
FB
704 return 0;
705}
706
707static inline void init_fat(BDRVVVFATState* s)
708{
a046433a 709 if (s->fat_type == 12) {
d6a7e54e
HP
710 array_init(&(s->fat),1);
711 array_ensure_allocated(&(s->fat),
712 s->sectors_per_fat * 0x200 * 3 / 2 - 1);
a046433a 713 } else {
d6a7e54e
HP
714 array_init(&(s->fat),(s->fat_type==32?4:2));
715 array_ensure_allocated(&(s->fat),
716 s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
a046433a 717 }
de167e41 718 memset(s->fat.pointer,0,s->fat.size);
3b46e624 719
de167e41 720 switch(s->fat_type) {
d6a7e54e
HP
721 case 12: s->max_fat_value=0xfff; break;
722 case 16: s->max_fat_value=0xffff; break;
723 case 32: s->max_fat_value=0x0fffffff; break;
724 default: s->max_fat_value=0; /* error... */
de167e41
FB
725 }
726
727}
728
c227f099 729static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
d6a7e54e 730 unsigned int directory_start, const char* filename, int is_dot)
de167e41 731{
0c36111f 732 int long_index = s->directory.next;
c227f099
AL
733 direntry_t* entry = NULL;
734 direntry_t* entry_long = NULL;
de167e41
FB
735
736 if(is_dot) {
d6a7e54e 737 entry=array_get_next(&(s->directory));
f671d173 738 memset(entry->name, 0x20, sizeof(entry->name));
d6a7e54e
HP
739 memcpy(entry->name,filename,strlen(filename));
740 return entry;
de167e41 741 }
3b46e624 742
de167e41 743 entry_long=create_long_filename(s,filename);
339cebcc 744 entry = create_short_filename(s, filename, directory_start);
de167e41
FB
745
746 /* calculate checksum; propagate to long name */
747 if(entry_long) {
748 uint8_t chksum=fat_chksum(entry);
749
d6a7e54e
HP
750 /* calculate anew, because realloc could have taken place */
751 entry_long=array_get(&(s->directory),long_index);
752 while(entry_long<entry && is_long_name(entry_long)) {
753 entry_long->reserved[1]=chksum;
754 entry_long++;
755 }
de167e41
FB
756 }
757
758 return entry;
759}
760
a046433a
FB
761/*
762 * Read a directory. (the index of the corresponding mapping must be passed).
763 */
764static int read_directory(BDRVVVFATState* s, int mapping_index)
de167e41 765{
c227f099
AL
766 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
767 direntry_t* direntry;
a046433a
FB
768 const char* dirname = mapping->path;
769 int first_cluster = mapping->begin;
770 int parent_index = mapping->info.dir.parent_mapping_index;
c227f099 771 mapping_t* parent_mapping = (mapping_t*)
511d2b14 772 (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
a046433a 773 int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
de167e41
FB
774
775 DIR* dir=opendir(dirname);
776 struct dirent* entry;
de167e41
FB
777 int i;
778
a046433a
FB
779 assert(mapping->mode & MODE_DIRECTORY);
780
781 if(!dir) {
d6a7e54e
HP
782 mapping->end = mapping->begin;
783 return -1;
a046433a 784 }
3b46e624 785
a046433a 786 i = mapping->info.dir.first_dir_index =
d6a7e54e 787 first_cluster == 0 ? 0 : s->directory.next;
a046433a 788
f82d92bb
HP
789 if (first_cluster != 0) {
790 /* create the top entries of a subdirectory */
791 (void)create_short_and_long_name(s, i, ".", 1);
792 (void)create_short_and_long_name(s, i, "..", 1);
793 }
794
5fafdf24 795 /* actually read the directory, and allocate the mappings */
de167e41 796 while((entry=readdir(dir))) {
d6a7e54e 797 unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
de167e41 798 char* buffer;
d6a7e54e 799 direntry_t* direntry;
a046433a 800 struct stat st;
d6a7e54e
HP
801 int is_dot=!strcmp(entry->d_name,".");
802 int is_dotdot=!strcmp(entry->d_name,"..");
de167e41 803
6817efea
HP
804 if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
805 fprintf(stderr, "Too many entries in root directory\n");
806 closedir(dir);
807 return -2;
808 }
809
d6a7e54e
HP
810 if(first_cluster == 0 && (is_dotdot || is_dot))
811 continue;
5fafdf24 812
d6a7e54e
HP
813 buffer = g_malloc(length);
814 snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
de167e41 815
d6a7e54e 816 if(stat(buffer,&st)<0) {
ce137829 817 g_free(buffer);
de167e41 818 continue;
d6a7e54e
HP
819 }
820
821 /* create directory entry for this file */
f82d92bb
HP
822 if (!is_dot && !is_dotdot) {
823 direntry = create_short_and_long_name(s, i, entry->d_name, 0);
824 } else {
825 direntry = array_get(&(s->directory), is_dot ? i : i + 1);
826 }
d6a7e54e
HP
827 direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
828 direntry->reserved[0]=direntry->reserved[1]=0;
829 direntry->ctime=fat_datetime(st.st_ctime,1);
830 direntry->cdate=fat_datetime(st.st_ctime,0);
831 direntry->adate=fat_datetime(st.st_atime,0);
832 direntry->begin_hi=0;
833 direntry->mtime=fat_datetime(st.st_mtime,1);
834 direntry->mdate=fat_datetime(st.st_mtime,0);
835 if(is_dotdot)
836 set_begin_of_direntry(direntry, first_cluster_of_parent);
837 else if(is_dot)
838 set_begin_of_direntry(direntry, first_cluster);
839 else
840 direntry->begin=0; /* do that later */
a046433a 841 if (st.st_size > 0x7fffffff) {
d6a7e54e 842 fprintf(stderr, "File %s is larger than 2GB\n", buffer);
ce137829 843 g_free(buffer);
08089edc 844 closedir(dir);
d6a7e54e 845 return -2;
a046433a 846 }
d6a7e54e
HP
847 direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
848
849 /* create mapping for this file */
850 if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
851 s->current_mapping = array_get_next(&(s->mapping));
852 s->current_mapping->begin=0;
853 s->current_mapping->end=st.st_size;
854 /*
855 * we get the direntry of the most recent direntry, which
856 * contains the short name and all the relevant information.
857 */
858 s->current_mapping->dir_index=s->directory.next-1;
859 s->current_mapping->first_mapping_index = -1;
860 if (S_ISDIR(st.st_mode)) {
861 s->current_mapping->mode = MODE_DIRECTORY;
862 s->current_mapping->info.dir.parent_mapping_index =
863 mapping_index;
864 } else {
865 s->current_mapping->mode = MODE_UNDEFINED;
866 s->current_mapping->info.file.offset = 0;
867 }
868 s->current_mapping->path=buffer;
869 s->current_mapping->read_only =
870 (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
b122c3b6
MA
871 } else {
872 g_free(buffer);
873 }
de167e41
FB
874 }
875 closedir(dir);
876
877 /* fill with zeroes up to the end of the cluster */
878 while(s->directory.next%(0x10*s->sectors_per_cluster)) {
d6a7e54e
HP
879 direntry_t* direntry=array_get_next(&(s->directory));
880 memset(direntry,0,sizeof(direntry_t));
de167e41
FB
881 }
882
6817efea
HP
883 if (s->fat_type != 32 &&
884 mapping_index == 0 &&
885 s->directory.next < s->root_entries) {
d6a7e54e
HP
886 /* root directory */
887 int cur = s->directory.next;
6817efea
HP
888 array_ensure_allocated(&(s->directory), s->root_entries - 1);
889 s->directory.next = s->root_entries;
d6a7e54e 890 memset(array_get(&(s->directory), cur), 0,
6817efea 891 (s->root_entries - cur) * sizeof(direntry_t));
de167e41 892 }
5fafdf24 893
5f5b29df 894 /* re-get the mapping, since s->mapping was possibly realloc()ed */
d4df3dbc 895 mapping = array_get(&(s->mapping), mapping_index);
a046433a 896 first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
d6a7e54e 897 * 0x20 / s->cluster_size;
a046433a
FB
898 mapping->end = first_cluster;
899
d4df3dbc 900 direntry = array_get(&(s->directory), mapping->dir_index);
a046433a 901 set_begin_of_direntry(direntry, mapping->begin);
3b46e624 902
a046433a
FB
903 return 0;
904}
de167e41 905
a046433a
FB
906static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
907{
4dc705dc 908 return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
a046433a 909}
de167e41 910
a046433a
FB
911static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
912{
4dc705dc 913 return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
a046433a 914}
de167e41 915
a046433a 916static int init_directories(BDRVVVFATState* s,
d11c8917
MA
917 const char *dirname, int heads, int secs,
918 Error **errp)
de167e41 919{
c227f099
AL
920 bootsector_t* bootsector;
921 mapping_t* mapping;
de167e41
FB
922 unsigned int i;
923 unsigned int cluster;
924
925 memset(&(s->first_sectors[0]),0,0x40*0x200);
926
de167e41 927 s->cluster_size=s->sectors_per_cluster*0x200;
7267c094 928 s->cluster_buffer=g_malloc(s->cluster_size);
a046433a
FB
929
930 /*
931 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
932 * where sc is sector_count,
933 * spf is sectors_per_fat,
934 * spc is sectors_per_clusters, and
935 * fat_type = 12, 16 or 32.
936 */
937 i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
938 s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
3b46e624 939
4dc705dc
HP
940 s->offset_to_fat = s->offset_to_bootsector + 1;
941 s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
942
c227f099
AL
943 array_init(&(s->mapping),sizeof(mapping_t));
944 array_init(&(s->directory),sizeof(direntry_t));
de167e41
FB
945
946 /* add volume label */
947 {
d6a7e54e
HP
948 direntry_t* entry=array_get_next(&(s->directory));
949 entry->attributes=0x28; /* archive | volume label */
d5941dda 950 memcpy(entry->name, s->volume_label, sizeof(entry->name));
de167e41
FB
951 }
952
de167e41
FB
953 /* Now build FAT, and write back information into directory */
954 init_fat(s);
955
6817efea
HP
956 /* TODO: if there are more entries, bootsector has to be adjusted! */
957 s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
a046433a
FB
958 s->cluster_count=sector2cluster(s, s->sector_count);
959
960 mapping = array_get_next(&(s->mapping));
961 mapping->begin = 0;
962 mapping->dir_index = 0;
963 mapping->info.dir.parent_mapping_index = -1;
964 mapping->first_mapping_index = -1;
7267c094 965 mapping->path = g_strdup(dirname);
a046433a
FB
966 i = strlen(mapping->path);
967 if (i > 0 && mapping->path[i - 1] == '/')
d6a7e54e 968 mapping->path[i - 1] = '\0';
a046433a
FB
969 mapping->mode = MODE_DIRECTORY;
970 mapping->read_only = 0;
971 s->path = mapping->path;
972
973 for (i = 0, cluster = 0; i < s->mapping.next; i++) {
d6a7e54e
HP
974 /* MS-DOS expects the FAT to be 0 for the root directory
975 * (except for the media byte). */
976 /* LATER TODO: still true for FAT32? */
977 int fix_fat = (i != 0);
978 mapping = array_get(&(s->mapping), i);
a046433a
FB
979
980 if (mapping->mode & MODE_DIRECTORY) {
d6a7e54e
HP
981 mapping->begin = cluster;
982 if(read_directory(s, i)) {
d11c8917
MA
983 error_setg(errp, "Could not read directory %s",
984 mapping->path);
d6a7e54e
HP
985 return -1;
986 }
987 mapping = array_get(&(s->mapping), i);
988 } else {
989 assert(mapping->mode == MODE_UNDEFINED);
990 mapping->mode=MODE_NORMAL;
991 mapping->begin = cluster;
992 if (mapping->end > 0) {
993 direntry_t* direntry = array_get(&(s->directory),
994 mapping->dir_index);
995
996 mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
997 set_begin_of_direntry(direntry, mapping->begin);
998 } else {
999 mapping->end = cluster + 1;
1000 fix_fat = 0;
1001 }
1002 }
1003
1004 assert(mapping->begin < mapping->end);
1005
1006 /* next free cluster */
1007 cluster = mapping->end;
1008
1009 if(cluster > s->cluster_count) {
d11c8917
MA
1010 error_setg(errp,
1011 "Directory does not fit in FAT%d (capacity %.2f MB)",
1012 s->fat_type, s->sector_count / 2000.0);
1013 return -1;
d6a7e54e 1014 }
8ce0f869 1015
d6a7e54e
HP
1016 /* fix fat for entry */
1017 if (fix_fat) {
1018 int j;
1019 for(j = mapping->begin; j < mapping->end - 1; j++)
1020 fat_set(s, j, j+1);
1021 fat_set(s, mapping->end - 1, s->max_fat_value);
1022 }
de167e41
FB
1023 }
1024
a046433a 1025 mapping = array_get(&(s->mapping), 0);
a046433a
FB
1026 s->last_cluster_of_root_directory = mapping->end;
1027
1028 /* the FAT signature */
1029 fat_set(s,0,s->max_fat_value);
1030 fat_set(s,1,s->max_fat_value);
de167e41 1031
a046433a
FB
1032 s->current_mapping = NULL;
1033
4dc705dc
HP
1034 bootsector = (bootsector_t *)(s->first_sectors
1035 + s->offset_to_bootsector * 0x200);
de167e41
FB
1036 bootsector->jump[0]=0xeb;
1037 bootsector->jump[1]=0x3e;
1038 bootsector->jump[2]=0x90;
63d261cb 1039 memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
de167e41
FB
1040 bootsector->sector_size=cpu_to_le16(0x200);
1041 bootsector->sectors_per_cluster=s->sectors_per_cluster;
1042 bootsector->reserved_sectors=cpu_to_le16(1);
1043 bootsector->number_of_fats=0x2; /* number of FATs */
6817efea 1044 bootsector->root_entries = cpu_to_le16(s->root_entries);
a046433a 1045 bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
4dc705dc
HP
1046 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1047 bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
a046433a 1048 s->fat.pointer[0] = bootsector->media_type;
de167e41 1049 bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
4480e0f9
MA
1050 bootsector->sectors_per_track = cpu_to_le16(secs);
1051 bootsector->number_of_heads = cpu_to_le16(heads);
4dc705dc 1052 bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
a046433a 1053 bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
de167e41 1054
a046433a 1055 /* LATER TODO: if FAT32, this is wrong */
4dc705dc
HP
1056 /* drive_number: fda=0, hda=0x80 */
1057 bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
de167e41
FB
1058 bootsector->u.fat16.signature=0x29;
1059 bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
1060
d5941dda
WB
1061 memcpy(bootsector->u.fat16.volume_label, s->volume_label,
1062 sizeof(bootsector->u.fat16.volume_label));
92e28d82
HP
1063 memcpy(bootsector->u.fat16.fat_type,
1064 s->fat_type == 12 ? "FAT12 " : "FAT16 ", 8);
de167e41
FB
1065 bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
1066
1067 return 0;
1068}
1069
83f64091 1070#ifdef DEBUG
a046433a 1071static BDRVVVFATState *vvv = NULL;
83f64091 1072#endif
a046433a 1073
eecc7747 1074static int enable_write_target(BlockDriverState *bs, Error **errp);
a046433a
FB
1075static int is_consistent(BDRVVVFATState *s);
1076
7ad9be64
KW
1077static QemuOptsList runtime_opts = {
1078 .name = "vvfat",
1079 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1080 .desc = {
1081 {
1082 .name = "dir",
1083 .type = QEMU_OPT_STRING,
1084 .help = "Host directory to map to the vvfat device",
1085 },
1086 {
1087 .name = "fat-type",
1088 .type = QEMU_OPT_NUMBER,
1089 .help = "FAT type (12, 16 or 32)",
1090 },
1091 {
1092 .name = "floppy",
1093 .type = QEMU_OPT_BOOL,
1094 .help = "Create a floppy rather than a hard disk image",
1095 },
d5941dda
WB
1096 {
1097 .name = "label",
1098 .type = QEMU_OPT_STRING,
1099 .help = "Use a volume label other than QEMU VVFAT",
1100 },
7ad9be64
KW
1101 {
1102 .name = "rw",
1103 .type = QEMU_OPT_BOOL,
1104 .help = "Make the image writable",
1105 },
1106 { /* end of list */ }
1107 },
1108};
1109
1110static void vvfat_parse_filename(const char *filename, QDict *options,
1111 Error **errp)
1112{
1113 int fat_type = 0;
1114 bool floppy = false;
1115 bool rw = false;
1116 int i;
1117
1118 if (!strstart(filename, "fat:", NULL)) {
1119 error_setg(errp, "File name string must start with 'fat:'");
1120 return;
1121 }
1122
1123 /* Parse options */
1124 if (strstr(filename, ":32:")) {
1125 fat_type = 32;
1126 } else if (strstr(filename, ":16:")) {
1127 fat_type = 16;
1128 } else if (strstr(filename, ":12:")) {
1129 fat_type = 12;
1130 }
1131
1132 if (strstr(filename, ":floppy:")) {
1133 floppy = true;
1134 }
1135
1136 if (strstr(filename, ":rw:")) {
1137 rw = true;
1138 }
1139
1140 /* Get the directory name without options */
1141 i = strrchr(filename, ':') - filename;
1142 assert(i >= 3);
1143 if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
1144 /* workaround for DOS drive names */
1145 filename += i - 1;
1146 } else {
1147 filename += i + 1;
1148 }
1149
1150 /* Fill in the options QDict */
46f5ac20
EB
1151 qdict_put_str(options, "dir", filename);
1152 qdict_put_int(options, "fat-type", fat_type);
1153 qdict_put_bool(options, "floppy", floppy);
1154 qdict_put_bool(options, "rw", rw);
7ad9be64
KW
1155}
1156
015a1036
HR
1157static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
1158 Error **errp)
de167e41
FB
1159{
1160 BDRVVVFATState *s = bs->opaque;
7ad9be64
KW
1161 int cyls, heads, secs;
1162 bool floppy;
d5941dda 1163 const char *dirname, *label;
7ad9be64
KW
1164 QemuOpts *opts;
1165 Error *local_err = NULL;
1166 int ret;
de167e41 1167
83f64091 1168#ifdef DEBUG
a046433a 1169 vvv = s;
83f64091 1170#endif
a046433a 1171
87ea75d5 1172 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
7ad9be64 1173 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 1174 if (local_err) {
c0f92b52 1175 error_propagate(errp, local_err);
7ad9be64
KW
1176 ret = -EINVAL;
1177 goto fail;
1178 }
1179
1180 dirname = qemu_opt_get(opts, "dir");
1181 if (!dirname) {
c0f92b52 1182 error_setg(errp, "vvfat block driver requires a 'dir' option");
7ad9be64
KW
1183 ret = -EINVAL;
1184 goto fail;
1185 }
1186
1187 s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
1188 floppy = qemu_opt_get_bool(opts, "floppy", false);
1189
d5941dda
WB
1190 memset(s->volume_label, ' ', sizeof(s->volume_label));
1191 label = qemu_opt_get(opts, "label");
1192 if (label) {
1193 size_t label_length = strlen(label);
1194 if (label_length > 11) {
1195 error_setg(errp, "vvfat label cannot be longer than 11 bytes");
1196 ret = -EINVAL;
1197 goto fail;
1198 }
1199 memcpy(s->volume_label, label, label_length);
d208c50d
KW
1200 } else {
1201 memcpy(s->volume_label, "QEMU VVFAT", 10);
d5941dda
WB
1202 }
1203
7ad9be64
KW
1204 if (floppy) {
1205 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1206 if (!s->fat_type) {
1207 s->fat_type = 12;
1208 secs = 36;
1209 s->sectors_per_cluster = 2;
1210 } else {
1211 secs = s->fat_type == 12 ? 18 : 36;
1212 s->sectors_per_cluster = 1;
1213 }
7ad9be64
KW
1214 cyls = 80;
1215 heads = 2;
1216 } else {
1217 /* 32MB or 504MB disk*/
1218 if (!s->fat_type) {
1219 s->fat_type = 16;
1220 }
4dc705dc 1221 s->offset_to_bootsector = 0x3f;
7ad9be64
KW
1222 cyls = s->fat_type == 12 ? 64 : 1024;
1223 heads = 16;
1224 secs = 63;
1225 }
1226
1227 switch (s->fat_type) {
1228 case 32:
d6a7e54e 1229 fprintf(stderr, "Big fat greek warning: FAT32 has not been tested. "
7ad9be64
KW
1230 "You are welcome to do so!\n");
1231 break;
1232 case 16:
1233 case 12:
1234 break;
1235 default:
c0f92b52 1236 error_setg(errp, "Valid FAT types are only 12, 16 and 32");
7ad9be64
KW
1237 ret = -EINVAL;
1238 goto fail;
1239 }
1240
1241
a046433a
FB
1242 s->bs = bs;
1243
a046433a 1244 /* LATER TODO: if FAT32, adjust */
a046433a 1245 s->sectors_per_cluster=0x10;
de167e41
FB
1246
1247 s->current_cluster=0xffffffff;
de167e41 1248
eecc7747 1249 s->qcow = NULL;
a046433a
FB
1250 s->qcow_filename = NULL;
1251 s->fat2 = NULL;
1252 s->downcase_short_names = 1;
3b46e624 1253
4480e0f9
MA
1254 fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
1255 dirname, cyls, heads, secs);
a046433a 1256
4dc705dc 1257 s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
5a742b55 1258
7ad9be64 1259 if (qemu_opt_get_bool(opts, "rw", false)) {
e2b8247a
JC
1260 if (!bdrv_is_read_only(bs)) {
1261 ret = enable_write_target(bs, errp);
1262 if (ret < 0) {
1263 goto fail;
1264 }
1265 } else {
1266 ret = -EPERM;
1267 error_setg(errp,
1268 "Unable to set VVFAT to 'rw' when drive is read-only");
1269 goto fail;
1270 }
1271 } else {
1272 /* read only is the default for safety */
1273 ret = bdrv_set_read_only(bs, true, &local_err);
78f27bd0 1274 if (ret < 0) {
e2b8247a 1275 error_propagate(errp, local_err);
7ad9be64
KW
1276 goto fail;
1277 }
b570094d
TS
1278 }
1279
4480e0f9 1280 bs->total_sectors = cyls * heads * secs;
b570094d 1281
d11c8917 1282 if (init_directories(s, dirname, heads, secs, errp)) {
7ad9be64
KW
1283 ret = -EIO;
1284 goto fail;
4480e0f9 1285 }
de167e41 1286
4dc705dc
HP
1287 s->sector_count = s->offset_to_root_dir
1288 + s->sectors_per_cluster * s->cluster_count;
b570094d 1289
3397f0cb
KW
1290 /* Disable migration when vvfat is used rw */
1291 if (s->qcow) {
81e5f78a
AG
1292 error_setg(&s->migration_blocker,
1293 "The vvfat (rw) format used by node '%s' "
1294 "does not support live migration",
1295 bdrv_get_device_or_node_name(bs));
fe44dc91
AA
1296 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1297 if (local_err) {
1298 error_propagate(errp, local_err);
1299 error_free(s->migration_blocker);
1300 goto fail;
1301 }
3397f0cb
KW
1302 }
1303
4dc705dc 1304 if (s->offset_to_bootsector > 0) {
fe44dc91
AA
1305 init_mbr(s, cyls, heads, secs);
1306 }
1307
1308 qemu_co_mutex_init(&s->lock);
1309
7ad9be64
KW
1310 ret = 0;
1311fail:
1312 qemu_opts_del(opts);
1313 return ret;
de167e41
FB
1314}
1315
a6506481
EB
1316static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
1317{
a5b8dd2c 1318 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
a6506481
EB
1319}
1320
de167e41
FB
1321static inline void vvfat_close_current_file(BDRVVVFATState *s)
1322{
1323 if(s->current_mapping) {
d6a7e54e
HP
1324 s->current_mapping = NULL;
1325 if (s->current_fd) {
1326 qemu_close(s->current_fd);
1327 s->current_fd = 0;
1328 }
de167e41 1329 }
a046433a 1330 s->current_cluster = -1;
de167e41
FB
1331}
1332
1333/* mappings between index1 and index2-1 are supposed to be ordered
1334 * return value is the index of the last mapping for which end>cluster_num
1335 */
1336static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
1337{
de167e41 1338 while(1) {
88bf7950 1339 int index3;
d6a7e54e
HP
1340 mapping_t* mapping;
1341 index3=(index1+index2)/2;
1342 mapping=array_get(&(s->mapping),index3);
1343 assert(mapping->begin < mapping->end);
1344 if(mapping->begin>=cluster_num) {
1345 assert(index2!=index3 || index2==0);
1346 if(index2==index3)
1347 return index1;
1348 index2=index3;
1349 } else {
1350 if(index1==index3)
1351 return mapping->end<=cluster_num ? index2 : index1;
1352 index1=index3;
1353 }
1354 assert(index1<=index2);
1355 DLOG(mapping=array_get(&(s->mapping),index1);
1356 assert(mapping->begin<=cluster_num);
1357 assert(index2 >= s->mapping.next ||
1358 ((mapping = array_get(&(s->mapping),index2)) &&
1359 mapping->end>cluster_num)));
de167e41
FB
1360 }
1361}
1362
c227f099 1363static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
de167e41
FB
1364{
1365 int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
c227f099 1366 mapping_t* mapping;
de167e41 1367 if(index>=s->mapping.next)
511d2b14 1368 return NULL;
de167e41
FB
1369 mapping=array_get(&(s->mapping),index);
1370 if(mapping->begin>cluster_num)
511d2b14 1371 return NULL;
a046433a 1372 assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
de167e41
FB
1373 return mapping;
1374}
1375
c227f099 1376static int open_file(BDRVVVFATState* s,mapping_t* mapping)
de167e41
FB
1377{
1378 if(!mapping)
d6a7e54e 1379 return -1;
de167e41 1380 if(!s->current_mapping ||
d6a7e54e
HP
1381 strcmp(s->current_mapping->path,mapping->path)) {
1382 /* open file */
1383 int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
1384 if(fd<0)
1385 return -1;
1386 vvfat_close_current_file(s);
1387 s->current_fd = fd;
1388 s->current_mapping = mapping;
de167e41
FB
1389 }
1390 return 0;
1391}
1392
1393static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
1394{
1395 if(s->current_cluster != cluster_num) {
d6a7e54e
HP
1396 int result=0;
1397 off_t offset;
1398 assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
1399 if(!s->current_mapping
1400 || s->current_mapping->begin>cluster_num
1401 || s->current_mapping->end<=cluster_num) {
1402 /* binary search of mappings for file */
1403 mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
1404
1405 assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
1406
1407 if (mapping && mapping->mode & MODE_DIRECTORY) {
1408 vvfat_close_current_file(s);
1409 s->current_mapping = mapping;
a046433a 1410read_cluster_directory:
d6a7e54e
HP
1411 offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
1412 s->cluster = (unsigned char*)s->directory.pointer+offset
1413 + 0x20*s->current_mapping->info.dir.first_dir_index;
1414 assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
1415 assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
1416 s->current_cluster = cluster_num;
1417 return 0;
1418 }
1419
1420 if(open_file(s,mapping))
1421 return -2;
1422 } else if (s->current_mapping->mode & MODE_DIRECTORY)
1423 goto read_cluster_directory;
1424
1425 assert(s->current_fd);
1426
1427 offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
1428 if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
1429 return -3;
1430 s->cluster=s->cluster_buffer;
1431 result=read(s->current_fd,s->cluster,s->cluster_size);
1432 if(result<0) {
1433 s->current_cluster = -1;
1434 return -1;
1435 }
1436 s->current_cluster = cluster_num;
de167e41
FB
1437 }
1438 return 0;
1439}
1440
a046433a 1441#ifdef DEBUG
c227f099 1442static void print_direntry(const direntry_t* direntry)
de167e41 1443{
a046433a
FB
1444 int j = 0;
1445 char buffer[1024];
1446
3e89cb04 1447 fprintf(stderr, "direntry %p: ", direntry);
de167e41 1448 if(!direntry)
d6a7e54e 1449 return;
a046433a 1450 if(is_long_name(direntry)) {
d6a7e54e
HP
1451 unsigned char* c=(unsigned char*)direntry;
1452 int i;
1453 for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
3891b370 1454#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
d6a7e54e
HP
1455 ADD_CHAR(c[i]);
1456 for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
1457 ADD_CHAR(c[i]);
1458 for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
1459 ADD_CHAR(c[i]);
1460 buffer[j] = 0;
1461 fprintf(stderr, "%s\n", buffer);
de167e41 1462 } else {
d6a7e54e
HP
1463 int i;
1464 for(i=0;i<11;i++)
1465 ADD_CHAR(direntry->name[i]);
1466 buffer[j] = 0;
1467 fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
1468 buffer,
1469 direntry->attributes,
1470 begin_of_direntry(direntry),le32_to_cpu(direntry->size));
de167e41
FB
1471 }
1472}
1473
c227f099 1474static void print_mapping(const mapping_t* mapping)
de167e41 1475{
3e89cb04
KW
1476 fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1477 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1478 mapping, mapping->begin, mapping->end, mapping->dir_index,
1479 mapping->first_mapping_index, mapping->path, mapping->mode);
1480
a046433a 1481 if (mapping->mode & MODE_DIRECTORY)
d6a7e54e 1482 fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index);
a046433a 1483 else
d6a7e54e 1484 fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
de167e41 1485}
a046433a 1486#endif
de167e41 1487
5fafdf24 1488static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
a046433a 1489 uint8_t *buf, int nb_sectors)
de167e41 1490{
a046433a 1491 BDRVVVFATState *s = bs->opaque;
de167e41 1492 int i;
de167e41 1493
a046433a 1494 for(i=0;i<nb_sectors;i++,sector_num++) {
d6a7e54e
HP
1495 if (sector_num >= bs->total_sectors)
1496 return -1;
1497 if (s->qcow) {
d6a644bb 1498 int64_t n;
6f712ee0 1499 int ret;
d6a644bb
EB
1500 ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
1501 (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
6f712ee0
EB
1502 if (ret < 0) {
1503 return ret;
1504 }
1505 if (ret) {
d6a644bb
EB
1506 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
1507 " allocated\n", sector_num,
1508 n >> BDRV_SECTOR_BITS));
1509 if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
1510 n >> BDRV_SECTOR_BITS)) {
7704df98
KW
1511 return -1;
1512 }
d6a644bb
EB
1513 i += (n >> BDRV_SECTOR_BITS) - 1;
1514 sector_num += (n >> BDRV_SECTOR_BITS) - 1;
7704df98
KW
1515 continue;
1516 }
d6a644bb
EB
1517 DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
1518 sector_num));
d6a7e54e 1519 }
4dc705dc
HP
1520 if (sector_num < s->offset_to_root_dir) {
1521 if (sector_num < s->offset_to_fat) {
1522 memcpy(buf + i * 0x200,
1523 &(s->first_sectors[sector_num * 0x200]),
1524 0x200);
1525 } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
1526 memcpy(buf + i * 0x200,
1527 &(s->fat.pointer[(sector_num
1528 - s->offset_to_fat) * 0x200]),
1529 0x200);
1530 } else if (sector_num < s->offset_to_root_dir) {
1531 memcpy(buf + i * 0x200,
1532 &(s->fat.pointer[(sector_num - s->offset_to_fat
1533 - s->sectors_per_fat) * 0x200]),
1534 0x200);
1535 }
d6a7e54e 1536 } else {
4dc705dc 1537 uint32_t sector = sector_num - s->offset_to_root_dir,
d6a7e54e
HP
1538 sector_offset_in_cluster=(sector%s->sectors_per_cluster),
1539 cluster_num=sector/s->sectors_per_cluster;
1540 if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
1541 /* LATER TODO: strict: return -1; */
1542 memset(buf+i*0x200,0,0x200);
1543 continue;
1544 }
1545 memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
1546 }
de167e41 1547 }
de167e41
FB
1548 return 0;
1549}
1550
4575eb49
KW
1551static int coroutine_fn
1552vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1553 QEMUIOVector *qiov, int flags)
2914caa0
PB
1554{
1555 int ret;
1556 BDRVVVFATState *s = bs->opaque;
4575eb49
KW
1557 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
1558 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1559 void *buf;
1560
1561 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1562 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1563
1564 buf = g_try_malloc(bytes);
1565 if (bytes && buf == NULL) {
1566 return -ENOMEM;
1567 }
1568
2914caa0
PB
1569 qemu_co_mutex_lock(&s->lock);
1570 ret = vvfat_read(bs, sector_num, buf, nb_sectors);
1571 qemu_co_mutex_unlock(&s->lock);
4575eb49
KW
1572
1573 qemu_iovec_from_buf(qiov, 0, buf, bytes);
1574 g_free(buf);
1575
2914caa0
PB
1576 return ret;
1577}
1578
a046433a 1579/* LATER TODO: statify all functions */
de167e41 1580
a046433a
FB
1581/*
1582 * Idea of the write support (use snapshot):
de167e41 1583 *
a046433a
FB
1584 * 1. check if all data is consistent, recording renames, modifications,
1585 * new files and directories (in s->commits).
de167e41 1586 *
a046433a 1587 * 2. if the data is not consistent, stop committing
de167e41 1588 *
a046433a
FB
1589 * 3. handle renames, and create new files and directories (do not yet
1590 * write their contents)
de167e41 1591 *
a046433a
FB
1592 * 4. walk the directories, fixing the mapping and direntries, and marking
1593 * the handled mappings as not deleted
de167e41 1594 *
a046433a 1595 * 5. commit the contents of the files
de167e41 1596 *
a046433a 1597 * 6. handle deleted files and directories
de167e41
FB
1598 *
1599 */
1600
c227f099 1601typedef struct commit_t {
a046433a
FB
1602 char* path;
1603 union {
d6a7e54e
HP
1604 struct { uint32_t cluster; } rename;
1605 struct { int dir_index; uint32_t modified_offset; } writeout;
1606 struct { uint32_t first_cluster; } new_file;
1607 struct { uint32_t cluster; } mkdir;
a046433a
FB
1608 } param;
1609 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1610 enum {
d6a7e54e 1611 ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
a046433a 1612 } action;
c227f099 1613} commit_t;
de167e41 1614
a046433a 1615static void clear_commits(BDRVVVFATState* s)
de167e41
FB
1616{
1617 int i;
a046433a
FB
1618DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
1619 for (i = 0; i < s->commits.next; i++) {
d6a7e54e
HP
1620 commit_t* commit = array_get(&(s->commits), i);
1621 assert(commit->path || commit->action == ACTION_WRITEOUT);
1622 if (commit->action != ACTION_WRITEOUT) {
1623 assert(commit->path);
ce137829 1624 g_free(commit->path);
d6a7e54e
HP
1625 } else
1626 assert(commit->path == NULL);
de167e41 1627 }
a046433a 1628 s->commits.next = 0;
de167e41
FB
1629}
1630
a046433a 1631static void schedule_rename(BDRVVVFATState* s,
d6a7e54e 1632 uint32_t cluster, char* new_path)
de167e41 1633{
c227f099 1634 commit_t* commit = array_get_next(&(s->commits));
a046433a
FB
1635 commit->path = new_path;
1636 commit->param.rename.cluster = cluster;
1637 commit->action = ACTION_RENAME;
de167e41
FB
1638}
1639
a046433a 1640static void schedule_writeout(BDRVVVFATState* s,
d6a7e54e 1641 int dir_index, uint32_t modified_offset)
de167e41 1642{
c227f099 1643 commit_t* commit = array_get_next(&(s->commits));
a046433a
FB
1644 commit->path = NULL;
1645 commit->param.writeout.dir_index = dir_index;
1646 commit->param.writeout.modified_offset = modified_offset;
1647 commit->action = ACTION_WRITEOUT;
de167e41
FB
1648}
1649
a046433a 1650static void schedule_new_file(BDRVVVFATState* s,
d6a7e54e 1651 char* path, uint32_t first_cluster)
de167e41 1652{
c227f099 1653 commit_t* commit = array_get_next(&(s->commits));
a046433a
FB
1654 commit->path = path;
1655 commit->param.new_file.first_cluster = first_cluster;
1656 commit->action = ACTION_NEW_FILE;
1657}
1658
1659static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
1660{
c227f099 1661 commit_t* commit = array_get_next(&(s->commits));
a046433a
FB
1662 commit->path = path;
1663 commit->param.mkdir.cluster = cluster;
1664 commit->action = ACTION_MKDIR;
1665}
1666
1667typedef struct {
64eaabda
TS
1668 /*
1669 * Since the sequence number is at most 0x3f, and the filename
1670 * length is at most 13 times the sequence number, the maximal
1671 * filename length is 0x3f * 13 bytes.
1672 */
1673 unsigned char name[0x3f * 13 + 1];
e03da26b 1674 gunichar2 name2[0x3f * 13 + 1];
a046433a
FB
1675 int checksum, len;
1676 int sequence_number;
1677} long_file_name;
1678
1679static void lfn_init(long_file_name* lfn)
1680{
1681 lfn->sequence_number = lfn->len = 0;
1682 lfn->checksum = 0x100;
1683}
1684
1685/* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1686static int parse_long_name(long_file_name* lfn,
d6a7e54e 1687 const direntry_t* direntry)
a046433a
FB
1688{
1689 int i, j, offset;
1690 const unsigned char* pointer = (const unsigned char*)direntry;
1691
1692 if (!is_long_name(direntry))
d6a7e54e 1693 return 1;
a046433a
FB
1694
1695 if (pointer[0] & 0x40) {
e03da26b 1696 /* first entry; do some initialization */
d6a7e54e
HP
1697 lfn->sequence_number = pointer[0] & 0x3f;
1698 lfn->checksum = pointer[13];
1699 lfn->name[0] = 0;
1700 lfn->name[lfn->sequence_number * 13] = 0;
e03da26b
HP
1701 } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
1702 /* not the expected sequence number */
d6a7e54e 1703 return -1;
e03da26b
HP
1704 } else if (pointer[13] != lfn->checksum) {
1705 /* not the expected checksum */
d6a7e54e 1706 return -2;
e03da26b
HP
1707 } else if (pointer[12] || pointer[26] || pointer[27]) {
1708 /* invalid zero fields */
d6a7e54e 1709 return -3;
e03da26b 1710 }
a046433a
FB
1711
1712 offset = 13 * (lfn->sequence_number - 1);
1713 for (i = 0, j = 1; i < 13; i++, j+=2) {
d6a7e54e
HP
1714 if (j == 11)
1715 j = 14;
1716 else if (j == 26)
1717 j = 28;
a046433a 1718
e03da26b
HP
1719 if (pointer[j] == 0 && pointer[j + 1] == 0) {
1720 /* end of long file name */
1721 break;
1722 }
1723 gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
1724 lfn->name2[offset + i] = c;
de167e41 1725 }
a046433a 1726
e03da26b
HP
1727 if (pointer[0] & 0x40) {
1728 /* first entry; set len */
1729 lfn->len = offset + i;
1730 }
1731 if ((pointer[0] & 0x3f) == 0x01) {
1732 /* last entry; finalize entry */
1733 glong olen;
1734 gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
1735 if (!utf8) {
1736 return -4;
1737 }
1738 lfn->len = olen;
1739 memcpy(lfn->name, utf8, olen + 1);
1740 g_free(utf8);
1741 }
a046433a 1742
de167e41
FB
1743 return 0;
1744}
1745
a046433a
FB
1746/* returns 0 if successful, >0 if no short_name, and <0 on error */
1747static int parse_short_name(BDRVVVFATState* s,
d6a7e54e 1748 long_file_name* lfn, direntry_t* direntry)
de167e41 1749{
a046433a 1750 int i, j;
de167e41 1751
a046433a 1752 if (!is_short_name(direntry))
d6a7e54e 1753 return 1;
a046433a
FB
1754
1755 for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
1756 for (i = 0; i <= j; i++) {
e03da26b
HP
1757 uint8_t c = direntry->name[i];
1758 if (c != to_valid_short_char(c)) {
d6a7e54e 1759 return -1;
e03da26b 1760 } else if (s->downcase_short_names) {
d6a7e54e 1761 lfn->name[i] = qemu_tolower(direntry->name[i]);
e03da26b 1762 } else {
d6a7e54e 1763 lfn->name[i] = direntry->name[i];
e03da26b 1764 }
de167e41
FB
1765 }
1766
f671d173
SW
1767 for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
1768 }
a046433a 1769 if (j >= 0) {
d6a7e54e
HP
1770 lfn->name[i++] = '.';
1771 lfn->name[i + j + 1] = '\0';
1772 for (;j >= 0; j--) {
f671d173 1773 uint8_t c = direntry->name[8 + j];
e03da26b 1774 if (c != to_valid_short_char(c)) {
f671d173
SW
1775 return -2;
1776 } else if (s->downcase_short_names) {
1777 lfn->name[i + j] = qemu_tolower(c);
1778 } else {
1779 lfn->name[i + j] = c;
1780 }
d6a7e54e 1781 }
a046433a 1782 } else
d6a7e54e 1783 lfn->name[i + j + 1] = '\0';
a046433a 1784
8c4517fd
HP
1785 if (lfn->name[0] == DIR_KANJI_FAKE) {
1786 lfn->name[0] = DIR_KANJI;
78f002c9 1787 }
ffe8ab83 1788 lfn->len = strlen((char*)lfn->name);
a046433a
FB
1789
1790 return 0;
de167e41
FB
1791}
1792
a046433a 1793static inline uint32_t modified_fat_get(BDRVVVFATState* s,
d6a7e54e 1794 unsigned int cluster)
de167e41 1795{
a046433a 1796 if (cluster < s->last_cluster_of_root_directory) {
d6a7e54e
HP
1797 if (cluster + 1 == s->last_cluster_of_root_directory)
1798 return s->max_fat_value;
1799 else
1800 return cluster + 1;
a046433a
FB
1801 }
1802
1803 if (s->fat_type==32) {
1804 uint32_t* entry=((uint32_t*)s->fat2)+cluster;
1805 return le32_to_cpu(*entry);
1806 } else if (s->fat_type==16) {
1807 uint16_t* entry=((uint16_t*)s->fat2)+cluster;
1808 return le16_to_cpu(*entry);
1809 } else {
1810 const uint8_t* x=s->fat2+cluster*3/2;
1811 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
1812 }
1813}
1814
6f712ee0
EB
1815static inline bool cluster_was_modified(BDRVVVFATState *s,
1816 uint32_t cluster_num)
a046433a
FB
1817{
1818 int was_modified = 0;
d6a644bb 1819 int i;
a046433a 1820
eecc7747
KW
1821 if (s->qcow == NULL) {
1822 return 0;
1823 }
a046433a 1824
eecc7747
KW
1825 for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
1826 was_modified = bdrv_is_allocated(s->qcow->bs,
d6a644bb
EB
1827 (cluster2sector(s, cluster_num) +
1828 i) * BDRV_SECTOR_SIZE,
1829 BDRV_SECTOR_SIZE, NULL);
eecc7747 1830 }
a046433a 1831
6f712ee0
EB
1832 /*
1833 * Note that this treats failures to learn allocation status the
1834 * same as if an allocation has occurred. It's as safe as
1835 * anything else, given that a failure to learn allocation status
1836 * will probably result in more failures.
1837 */
1838 return !!was_modified;
de167e41
FB
1839}
1840
a046433a 1841static const char* get_basename(const char* path)
de167e41 1842{
a046433a
FB
1843 char* basename = strrchr(path, '/');
1844 if (basename == NULL)
d6a7e54e 1845 return path;
a046433a 1846 else
d6a7e54e 1847 return basename + 1; /* strip '/' */
de167e41
FB
1848}
1849
a046433a
FB
1850/*
1851 * The array s->used_clusters holds the states of the clusters. If it is
1852 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1853 * was modified, bit 3 is set.
1854 * If any cluster is allocated, but not part of a file or directory, this
1855 * driver refuses to commit.
1856 */
1857typedef enum {
1858 USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
c227f099 1859} used_t;
de167e41 1860
a046433a
FB
1861/*
1862 * get_cluster_count_for_direntry() not only determines how many clusters
1863 * are occupied by direntry, but also if it was renamed or modified.
1864 *
1865 * A file is thought to be renamed *only* if there already was a file with
1866 * exactly the same first cluster, but a different name.
1867 *
1868 * Further, the files/directories handled by this function are
1869 * assumed to be *not* deleted (and *only* those).
1870 */
1871static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
d6a7e54e 1872 direntry_t* direntry, const char* path)
de167e41 1873{
a046433a
FB
1874 /*
1875 * This is a little bit tricky:
1876 * IF the guest OS just inserts a cluster into the file chain,
1877 * and leaves the rest alone, (i.e. the original file had clusters
1878 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1879 *
1880 * - do_commit will write the cluster into the file at the given
1881 * offset, but
1882 *
1883 * - the cluster which is overwritten should be moved to a later
1884 * position in the file.
1885 *
1886 * I am not aware that any OS does something as braindead, but this
1887 * situation could happen anyway when not committing for a long time.
1888 * Just to be sure that this does not bite us, detect it, and copy the
1889 * contents of the clusters to-be-overwritten into the qcow.
1890 */
1891 int copy_it = 0;
1892 int was_modified = 0;
1893 int32_t ret = 0;
1894
1895 uint32_t cluster_num = begin_of_direntry(direntry);
1896 uint32_t offset = 0;
1897 int first_mapping_index = -1;
c227f099 1898 mapping_t* mapping = NULL;
a046433a 1899 const char* basename2 = NULL;
de167e41 1900
a046433a 1901 vvfat_close_current_file(s);
de167e41 1902
a046433a
FB
1903 /* the root directory */
1904 if (cluster_num == 0)
d6a7e54e 1905 return 0;
de167e41 1906
a046433a
FB
1907 /* write support */
1908 if (s->qcow) {
d6a7e54e 1909 basename2 = get_basename(path);
de167e41 1910
d6a7e54e 1911 mapping = find_mapping_for_cluster(s, cluster_num);
a046433a 1912
d6a7e54e
HP
1913 if (mapping) {
1914 const char* basename;
da2414e9 1915
d6a7e54e
HP
1916 assert(mapping->mode & MODE_DELETED);
1917 mapping->mode &= ~MODE_DELETED;
a046433a 1918
d6a7e54e 1919 basename = get_basename(mapping->path);
a046433a 1920
d6a7e54e 1921 assert(mapping->mode & MODE_NORMAL);
a046433a 1922
d6a7e54e
HP
1923 /* rename */
1924 if (strcmp(basename, basename2))
1925 schedule_rename(s, cluster_num, g_strdup(path));
1926 } else if (is_file(direntry))
1927 /* new file */
1928 schedule_new_file(s, g_strdup(path), cluster_num);
1929 else {
43dc2a64 1930 abort();
d6a7e54e
HP
1931 return 0;
1932 }
de167e41
FB
1933 }
1934
a046433a 1935 while(1) {
d6a7e54e
HP
1936 if (s->qcow) {
1937 if (!copy_it && cluster_was_modified(s, cluster_num)) {
1938 if (mapping == NULL ||
1939 mapping->begin > cluster_num ||
1940 mapping->end <= cluster_num)
1941 mapping = find_mapping_for_cluster(s, cluster_num);
de167e41 1942
a046433a 1943
d6a7e54e
HP
1944 if (mapping &&
1945 (mapping->mode & MODE_DIRECTORY) == 0) {
a046433a 1946
d6a7e54e
HP
1947 /* was modified in qcow */
1948 if (offset != mapping->info.file.offset + s->cluster_size
1949 * (cluster_num - mapping->begin)) {
1950 /* offset of this cluster in file chain has changed */
43dc2a64 1951 abort();
d6a7e54e
HP
1952 copy_it = 1;
1953 } else if (offset == 0) {
1954 const char* basename = get_basename(mapping->path);
a046433a 1955
d6a7e54e
HP
1956 if (strcmp(basename, basename2))
1957 copy_it = 1;
1958 first_mapping_index = array_index(&(s->mapping), mapping);
1959 }
a046433a 1960
d6a7e54e
HP
1961 if (mapping->first_mapping_index != first_mapping_index
1962 && mapping->info.file.offset > 0) {
43dc2a64 1963 abort();
d6a7e54e
HP
1964 copy_it = 1;
1965 }
1966
1967 /* need to write out? */
1968 if (!was_modified && is_file(direntry)) {
1969 was_modified = 1;
1970 schedule_writeout(s, mapping->dir_index, offset);
1971 }
1972 }
1973 }
1974
1975 if (copy_it) {
d6a644bb 1976 int i;
d6a7e54e
HP
1977 /*
1978 * This is horribly inefficient, but that is okay, since
1979 * it is rarely executed, if at all.
1980 */
1981 int64_t offset = cluster2sector(s, cluster_num);
1982
1983 vvfat_close_current_file(s);
7704df98 1984 for (i = 0; i < s->sectors_per_cluster; i++) {
eecc7747
KW
1985 int res;
1986
d6a644bb
EB
1987 res = bdrv_is_allocated(s->qcow->bs,
1988 (offset + i) * BDRV_SECTOR_SIZE,
1989 BDRV_SECTOR_SIZE, NULL);
6f712ee0
EB
1990 if (res < 0) {
1991 return -1;
1992 }
eecc7747
KW
1993 if (!res) {
1994 res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
1995 if (res) {
7704df98
KW
1996 return -1;
1997 }
18d51c4b 1998 res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
eecc7747 1999 if (res) {
7704df98
KW
2000 return -2;
2001 }
2002 }
2003 }
d6a7e54e
HP
2004 }
2005 }
a046433a 2006
d6a7e54e
HP
2007 ret++;
2008 if (s->used_clusters[cluster_num] & USED_ANY)
2009 return 0;
2010 s->used_clusters[cluster_num] = USED_FILE;
a046433a 2011
d6a7e54e 2012 cluster_num = modified_fat_get(s, cluster_num);
a046433a 2013
d6a7e54e
HP
2014 if (fat_eof(s, cluster_num))
2015 return ret;
2016 else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
2017 return -1;
a046433a 2018
d6a7e54e 2019 offset += s->cluster_size;
a046433a 2020 }
de167e41
FB
2021}
2022
a046433a 2023/*
5fafdf24 2024 * This function looks at the modified data (qcow).
a046433a
FB
2025 * It returns 0 upon inconsistency or error, and the number of clusters
2026 * used by the directory, its subdirectories and their files.
2027 */
2028static int check_directory_consistency(BDRVVVFATState *s,
d6a7e54e 2029 int cluster_num, const char* path)
de167e41 2030{
a046433a 2031 int ret = 0;
7267c094 2032 unsigned char* cluster = g_malloc(s->cluster_size);
c227f099
AL
2033 direntry_t* direntries = (direntry_t*)cluster;
2034 mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
a046433a
FB
2035
2036 long_file_name lfn;
2037 int path_len = strlen(path);
0d460d6f 2038 char path2[PATH_MAX + 1];
a046433a
FB
2039
2040 assert(path_len < PATH_MAX); /* len was tested before! */
363a37d5 2041 pstrcpy(path2, sizeof(path2), path);
a046433a
FB
2042 path2[path_len] = '/';
2043 path2[path_len + 1] = '\0';
2044
2045 if (mapping) {
d6a7e54e
HP
2046 const char* basename = get_basename(mapping->path);
2047 const char* basename2 = get_basename(path);
a046433a 2048
d6a7e54e 2049 assert(mapping->mode & MODE_DIRECTORY);
a046433a 2050
d6a7e54e
HP
2051 assert(mapping->mode & MODE_DELETED);
2052 mapping->mode &= ~MODE_DELETED;
a046433a 2053
d6a7e54e
HP
2054 if (strcmp(basename, basename2))
2055 schedule_rename(s, cluster_num, g_strdup(path));
a046433a 2056 } else
d6a7e54e
HP
2057 /* new directory */
2058 schedule_mkdir(s, cluster_num, g_strdup(path));
3b46e624 2059
a046433a
FB
2060 lfn_init(&lfn);
2061 do {
d6a7e54e
HP
2062 int i;
2063 int subret = 0;
a046433a 2064
d6a7e54e 2065 ret++;
a046433a 2066
d6a7e54e
HP
2067 if (s->used_clusters[cluster_num] & USED_ANY) {
2068 fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
6262bbd3 2069 goto fail;
d6a7e54e
HP
2070 }
2071 s->used_clusters[cluster_num] = USED_DIRECTORY;
a046433a
FB
2072
2073DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
d6a7e54e
HP
2074 subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
2075 s->sectors_per_cluster);
2076 if (subret) {
2077 fprintf(stderr, "Error fetching direntries\n");
2078 fail:
ce137829 2079 g_free(cluster);
d6a7e54e
HP
2080 return 0;
2081 }
a046433a 2082
d6a7e54e
HP
2083 for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
2084 int cluster_count = 0;
a046433a 2085
b2bedb21 2086DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
d6a7e54e
HP
2087 if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
2088 is_free(direntries + i))
2089 continue;
2090
2091 subret = parse_long_name(&lfn, direntries + i);
2092 if (subret < 0) {
2093 fprintf(stderr, "Error in long name\n");
2094 goto fail;
2095 }
2096 if (subret == 0 || is_free(direntries + i))
2097 continue;
2098
2099 if (fat_chksum(direntries+i) != lfn.checksum) {
2100 subret = parse_short_name(s, &lfn, direntries + i);
2101 if (subret < 0) {
2102 fprintf(stderr, "Error in short name (%d)\n", subret);
2103 goto fail;
2104 }
2105 if (subret > 0 || !strcmp((char*)lfn.name, ".")
2106 || !strcmp((char*)lfn.name, ".."))
2107 continue;
2108 }
2109 lfn.checksum = 0x100; /* cannot use long name twice */
2110
2111 if (path_len + 1 + lfn.len >= PATH_MAX) {
2112 fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
2113 goto fail;
2114 }
363a37d5
BS
2115 pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
2116 (char*)lfn.name);
a046433a 2117
d6a7e54e
HP
2118 if (is_directory(direntries + i)) {
2119 if (begin_of_direntry(direntries + i) == 0) {
2120 DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
2121 goto fail;
2122 }
2123 cluster_count = check_directory_consistency(s,
2124 begin_of_direntry(direntries + i), path2);
2125 if (cluster_count == 0) {
2126 DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
2127 goto fail;
2128 }
2129 } else if (is_file(direntries + i)) {
2130 /* check file size with FAT */
2131 cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
2132 if (cluster_count !=
13385ae1 2133 DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
d6a7e54e
HP
2134 DLOG(fprintf(stderr, "Cluster count mismatch\n"));
2135 goto fail;
2136 }
2137 } else
43dc2a64 2138 abort(); /* cluster_count = 0; */
a046433a 2139
d6a7e54e
HP
2140 ret += cluster_count;
2141 }
de167e41 2142
d6a7e54e 2143 cluster_num = modified_fat_get(s, cluster_num);
a046433a 2144 } while(!fat_eof(s, cluster_num));
de167e41 2145
ce137829 2146 g_free(cluster);
a046433a
FB
2147 return ret;
2148}
2149
2150/* returns 1 on success */
2151static int is_consistent(BDRVVVFATState* s)
2152{
2153 int i, check;
2154 int used_clusters_count = 0;
2155
2156DLOG(checkpoint());
2157 /*
2158 * - get modified FAT
2159 * - compare the two FATs (TODO)
2160 * - get buffer for marking used clusters
2161 * - recurse direntries from root (using bs->bdrv_read to make
2162 * sure to get the new data)
2163 * - check that the FAT agrees with the size
2164 * - count the number of clusters occupied by this directory and
2165 * its files
2166 * - check that the cumulative used cluster count agrees with the
2167 * FAT
2168 * - if all is fine, return number of used clusters
2169 */
2170 if (s->fat2 == NULL) {
d6a7e54e
HP
2171 int size = 0x200 * s->sectors_per_fat;
2172 s->fat2 = g_malloc(size);
2173 memcpy(s->fat2, s->fat.pointer, size);
a046433a
FB
2174 }
2175 check = vvfat_read(s->bs,
4dc705dc 2176 s->offset_to_fat, s->fat2, s->sectors_per_fat);
a046433a 2177 if (check) {
d6a7e54e
HP
2178 fprintf(stderr, "Could not copy fat\n");
2179 return 0;
a046433a
FB
2180 }
2181 assert (s->used_clusters);
2182 for (i = 0; i < sector2cluster(s, s->sector_count); i++)
d6a7e54e 2183 s->used_clusters[i] &= ~USED_ANY;
a046433a
FB
2184
2185 clear_commits(s);
2186
2187 /* mark every mapped file/directory as deleted.
2188 * (check_directory_consistency() will unmark those still present). */
2189 if (s->qcow)
d6a7e54e
HP
2190 for (i = 0; i < s->mapping.next; i++) {
2191 mapping_t* mapping = array_get(&(s->mapping), i);
2192 if (mapping->first_mapping_index < 0)
2193 mapping->mode |= MODE_DELETED;
2194 }
a046433a
FB
2195
2196 used_clusters_count = check_directory_consistency(s, 0, s->path);
2197 if (used_clusters_count <= 0) {
d6a7e54e
HP
2198 DLOG(fprintf(stderr, "problem in directory\n"));
2199 return 0;
de167e41
FB
2200 }
2201
a046433a
FB
2202 check = s->last_cluster_of_root_directory;
2203 for (i = check; i < sector2cluster(s, s->sector_count); i++) {
d6a7e54e
HP
2204 if (modified_fat_get(s, i)) {
2205 if(!s->used_clusters[i]) {
2206 DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
2207 return 0;
2208 }
2209 check++;
2210 }
a046433a 2211
d6a7e54e
HP
2212 if (s->used_clusters[i] == USED_ALLOCATED) {
2213 /* allocated, but not used... */
2214 DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
2215 return 0;
2216 }
a046433a
FB
2217 }
2218
2219 if (check != used_clusters_count)
d6a7e54e 2220 return 0;
a046433a
FB
2221
2222 return used_clusters_count;
2223}
2224
2225static inline void adjust_mapping_indices(BDRVVVFATState* s,
d6a7e54e 2226 int offset, int adjust)
a046433a
FB
2227{
2228 int i;
2229
2230 for (i = 0; i < s->mapping.next; i++) {
d6a7e54e 2231 mapping_t* mapping = array_get(&(s->mapping), i);
a046433a
FB
2232
2233#define ADJUST_MAPPING_INDEX(name) \
d6a7e54e
HP
2234 if (mapping->name >= offset) \
2235 mapping->name += adjust
a046433a 2236
d6a7e54e
HP
2237 ADJUST_MAPPING_INDEX(first_mapping_index);
2238 if (mapping->mode & MODE_DIRECTORY)
2239 ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
de167e41 2240 }
a046433a
FB
2241}
2242
2243/* insert or update mapping */
c227f099 2244static mapping_t* insert_mapping(BDRVVVFATState* s,
d6a7e54e 2245 uint32_t begin, uint32_t end)
a046433a
FB
2246{
2247 /*
2248 * - find mapping where mapping->begin >= begin,
2249 * - if mapping->begin > begin: insert
2250 * - adjust all references to mappings!
2251 * - else: adjust
2252 * - replace name
2253 */
2254 int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
c227f099
AL
2255 mapping_t* mapping = NULL;
2256 mapping_t* first_mapping = array_get(&(s->mapping), 0);
a046433a
FB
2257
2258 if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
d6a7e54e
HP
2259 && mapping->begin < begin) {
2260 mapping->end = begin;
2261 index++;
2262 mapping = array_get(&(s->mapping), index);
a046433a
FB
2263 }
2264 if (index >= s->mapping.next || mapping->begin > begin) {
d6a7e54e
HP
2265 mapping = array_insert(&(s->mapping), index, 1);
2266 mapping->path = NULL;
2267 adjust_mapping_indices(s, index, +1);
a046433a
FB
2268 }
2269
2270 mapping->begin = begin;
2271 mapping->end = end;
de167e41 2272
c227f099 2273DLOG(mapping_t* next_mapping;
a046433a
FB
2274assert(index + 1 >= s->mapping.next ||
2275((next_mapping = array_get(&(s->mapping), index + 1)) &&
2276 next_mapping->begin >= end)));
2277
c227f099 2278 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
d6a7e54e
HP
2279 s->current_mapping = array_get(&(s->mapping),
2280 s->current_mapping - first_mapping);
a046433a
FB
2281
2282 return mapping;
2283}
2284
2285static int remove_mapping(BDRVVVFATState* s, int mapping_index)
2286{
c227f099
AL
2287 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
2288 mapping_t* first_mapping = array_get(&(s->mapping), 0);
a046433a
FB
2289
2290 /* free mapping */
ce137829
SW
2291 if (mapping->first_mapping_index < 0) {
2292 g_free(mapping->path);
2293 }
a046433a
FB
2294
2295 /* remove from s->mapping */
2296 array_remove(&(s->mapping), mapping_index);
2297
2298 /* adjust all references to mappings */
2299 adjust_mapping_indices(s, mapping_index, -1);
2300
c227f099 2301 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
d6a7e54e
HP
2302 s->current_mapping = array_get(&(s->mapping),
2303 s->current_mapping - first_mapping);
de167e41 2304
de167e41
FB
2305 return 0;
2306}
2307
a046433a
FB
2308static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
2309{
2310 int i;
2311 for (i = 0; i < s->mapping.next; i++) {
d6a7e54e
HP
2312 mapping_t* mapping = array_get(&(s->mapping), i);
2313 if (mapping->dir_index >= offset)
2314 mapping->dir_index += adjust;
2315 if ((mapping->mode & MODE_DIRECTORY) &&
2316 mapping->info.dir.first_dir_index >= offset)
2317 mapping->info.dir.first_dir_index += adjust;
a046433a
FB
2318 }
2319}
de167e41 2320
c227f099 2321static direntry_t* insert_direntries(BDRVVVFATState* s,
d6a7e54e 2322 int dir_index, int count)
de167e41 2323{
a046433a
FB
2324 /*
2325 * make room in s->directory,
2326 * adjust_dirindices
2327 */
c227f099 2328 direntry_t* result = array_insert(&(s->directory), dir_index, count);
a046433a 2329 if (result == NULL)
d6a7e54e 2330 return NULL;
a046433a 2331 adjust_dirindices(s, dir_index, count);
de167e41
FB
2332 return result;
2333}
2334
a046433a
FB
2335static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
2336{
2337 int ret = array_remove_slice(&(s->directory), dir_index, count);
2338 if (ret)
d6a7e54e 2339 return ret;
a046433a
FB
2340 adjust_dirindices(s, dir_index, -count);
2341 return 0;
2342}
de167e41 2343
a046433a
FB
2344/*
2345 * Adapt the mappings of the cluster chain starting at first cluster
2346 * (i.e. if a file starts at first_cluster, the chain is followed according
2347 * to the modified fat, and the corresponding entries in s->mapping are
2348 * adjusted)
2349 */
2350static int commit_mappings(BDRVVVFATState* s,
d6a7e54e 2351 uint32_t first_cluster, int dir_index)
de167e41 2352{
c227f099
AL
2353 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2354 direntry_t* direntry = array_get(&(s->directory), dir_index);
a046433a
FB
2355 uint32_t cluster = first_cluster;
2356
2357 vvfat_close_current_file(s);
2358
2359 assert(mapping);
2360 assert(mapping->begin == first_cluster);
2361 mapping->first_mapping_index = -1;
2362 mapping->dir_index = dir_index;
2363 mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
d6a7e54e 2364 MODE_DIRECTORY : MODE_NORMAL;
a046433a
FB
2365
2366 while (!fat_eof(s, cluster)) {
d6a7e54e
HP
2367 uint32_t c, c1;
2368
2369 for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
2370 c = c1, c1 = modified_fat_get(s, c1));
2371
2372 c++;
2373 if (c > mapping->end) {
2374 int index = array_index(&(s->mapping), mapping);
2375 int i, max_i = s->mapping.next - index;
2376 for (i = 1; i < max_i && mapping[i].begin < c; i++);
2377 while (--i > 0)
2378 remove_mapping(s, index + 1);
2379 }
2380 assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
2381 || mapping[1].begin >= c);
2382 mapping->end = c;
2383
2384 if (!fat_eof(s, c1)) {
2385 int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
2386 mapping_t* next_mapping = i >= s->mapping.next ? NULL :
2387 array_get(&(s->mapping), i);
2388
2389 if (next_mapping == NULL || next_mapping->begin > c1) {
2390 int i1 = array_index(&(s->mapping), mapping);
2391
2392 next_mapping = insert_mapping(s, c1, c1+1);
2393
2394 if (c1 < c)
2395 i1++;
2396 mapping = array_get(&(s->mapping), i1);
2397 }
2398
2399 next_mapping->dir_index = mapping->dir_index;
2400 next_mapping->first_mapping_index =
2401 mapping->first_mapping_index < 0 ?
2402 array_index(&(s->mapping), mapping) :
2403 mapping->first_mapping_index;
2404 next_mapping->path = mapping->path;
2405 next_mapping->mode = mapping->mode;
2406 next_mapping->read_only = mapping->read_only;
2407 if (mapping->mode & MODE_DIRECTORY) {
2408 next_mapping->info.dir.parent_mapping_index =
2409 mapping->info.dir.parent_mapping_index;
2410 next_mapping->info.dir.first_dir_index =
2411 mapping->info.dir.first_dir_index +
2412 0x10 * s->sectors_per_cluster *
2413 (mapping->end - mapping->begin);
2414 } else
2415 next_mapping->info.file.offset = mapping->info.file.offset +
2416 mapping->end - mapping->begin;
2417
2418 mapping = next_mapping;
2419 }
2420
2421 cluster = c1;
a046433a 2422 }
de167e41 2423
de167e41
FB
2424 return 0;
2425}
2426
a046433a 2427static int commit_direntries(BDRVVVFATState* s,
d6a7e54e 2428 int dir_index, int parent_mapping_index)
de167e41 2429{
c227f099 2430 direntry_t* direntry = array_get(&(s->directory), dir_index);
a046433a 2431 uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
c227f099 2432 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
a046433a
FB
2433
2434 int factor = 0x10 * s->sectors_per_cluster;
2435 int old_cluster_count, new_cluster_count;
2436 int current_dir_index = mapping->info.dir.first_dir_index;
2437 int first_dir_index = current_dir_index;
2438 int ret, i;
2439 uint32_t c;
2440
2441DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index));
2442
2443 assert(direntry);
2444 assert(mapping);
2445 assert(mapping->begin == first_cluster);
2446 assert(mapping->info.dir.first_dir_index < s->directory.next);
2447 assert(mapping->mode & MODE_DIRECTORY);
2448 assert(dir_index == 0 || is_directory(direntry));
2449
2450 mapping->info.dir.parent_mapping_index = parent_mapping_index;
2451
2452 if (first_cluster == 0) {
d6a7e54e
HP
2453 old_cluster_count = new_cluster_count =
2454 s->last_cluster_of_root_directory;
a046433a 2455 } else {
d6a7e54e
HP
2456 for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2457 c = fat_get(s, c))
2458 old_cluster_count++;
de167e41 2459
d6a7e54e
HP
2460 for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2461 c = modified_fat_get(s, c))
2462 new_cluster_count++;
a046433a 2463 }
de167e41 2464
a046433a 2465 if (new_cluster_count > old_cluster_count) {
d6a7e54e
HP
2466 if (insert_direntries(s,
2467 current_dir_index + factor * old_cluster_count,
2468 factor * (new_cluster_count - old_cluster_count)) == NULL)
2469 return -1;
a046433a 2470 } else if (new_cluster_count < old_cluster_count)
d6a7e54e
HP
2471 remove_direntries(s,
2472 current_dir_index + factor * new_cluster_count,
2473 factor * (old_cluster_count - new_cluster_count));
a046433a
FB
2474
2475 for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
ebb72c9f 2476 direntry_t *first_direntry;
d6a7e54e
HP
2477 void* direntry = array_get(&(s->directory), current_dir_index);
2478 int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
2479 s->sectors_per_cluster);
2480 if (ret)
2481 return ret;
ebb72c9f
KW
2482
2483 /* The first directory entry on the filesystem is the volume name */
2484 first_direntry = (direntry_t*) s->directory.pointer;
2485 assert(!memcmp(first_direntry->name, s->volume_label, 11));
2486
d6a7e54e 2487 current_dir_index += factor;
a046433a 2488 }
de167e41 2489
a046433a
FB
2490 ret = commit_mappings(s, first_cluster, dir_index);
2491 if (ret)
d6a7e54e 2492 return ret;
a046433a
FB
2493
2494 /* recurse */
2495 for (i = 0; i < factor * new_cluster_count; i++) {
d6a7e54e
HP
2496 direntry = array_get(&(s->directory), first_dir_index + i);
2497 if (is_directory(direntry) && !is_dot(direntry)) {
2498 mapping = find_mapping_for_cluster(s, first_cluster);
2499 assert(mapping->mode & MODE_DIRECTORY);
2500 ret = commit_direntries(s, first_dir_index + i,
2501 array_index(&(s->mapping), mapping));
2502 if (ret)
2503 return ret;
2504 }
a046433a 2505 }
de167e41 2506
a046433a
FB
2507 return 0;
2508}
de167e41 2509
a046433a
FB
2510/* commit one file (adjust contents, adjust mapping),
2511 return first_mapping_index */
2512static int commit_one_file(BDRVVVFATState* s,
d6a7e54e 2513 int dir_index, uint32_t offset)
a046433a 2514{
c227f099 2515 direntry_t* direntry = array_get(&(s->directory), dir_index);
a046433a
FB
2516 uint32_t c = begin_of_direntry(direntry);
2517 uint32_t first_cluster = c;
c227f099 2518 mapping_t* mapping = find_mapping_for_cluster(s, c);
a046433a 2519 uint32_t size = filesize_of_direntry(direntry);
7267c094 2520 char* cluster = g_malloc(s->cluster_size);
a046433a
FB
2521 uint32_t i;
2522 int fd = 0;
2523
2524 assert(offset < size);
2525 assert((offset % s->cluster_size) == 0);
2526
2527 for (i = s->cluster_size; i < offset; i += s->cluster_size)
d6a7e54e 2528 c = modified_fat_get(s, c);
a046433a 2529
6165f4d8 2530 fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
a046433a 2531 if (fd < 0) {
d6a7e54e
HP
2532 fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
2533 strerror(errno), errno);
ce137829 2534 g_free(cluster);
d6a7e54e 2535 return fd;
de167e41 2536 }
ce137829
SW
2537 if (offset > 0) {
2538 if (lseek(fd, offset, SEEK_SET) != offset) {
2e1e79da 2539 qemu_close(fd);
ce137829
SW
2540 g_free(cluster);
2541 return -3;
2542 }
2543 }
a046433a
FB
2544
2545 while (offset < size) {
d6a7e54e
HP
2546 uint32_t c1;
2547 int rest_size = (size - offset > s->cluster_size ?
2548 s->cluster_size : size - offset);
2549 int ret;
a046433a 2550
d6a7e54e 2551 c1 = modified_fat_get(s, c);
a046433a 2552
d6a7e54e
HP
2553 assert((size - offset == 0 && fat_eof(s, c)) ||
2554 (size > offset && c >=2 && !fat_eof(s, c)));
a046433a 2555
d6a7e54e 2556 ret = vvfat_read(s->bs, cluster2sector(s, c),
78ee96de 2557 (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
a046433a 2558
ce137829 2559 if (ret < 0) {
2e1e79da 2560 qemu_close(fd);
ce137829
SW
2561 g_free(cluster);
2562 return ret;
2563 }
a046433a 2564
ce137829 2565 if (write(fd, cluster, rest_size) < 0) {
2e1e79da 2566 qemu_close(fd);
ce137829
SW
2567 g_free(cluster);
2568 return -2;
2569 }
a046433a 2570
d6a7e54e
HP
2571 offset += rest_size;
2572 c = c1;
a046433a
FB
2573 }
2574
2dedf83e
KS
2575 if (ftruncate(fd, size)) {
2576 perror("ftruncate()");
2e1e79da 2577 qemu_close(fd);
ce137829 2578 g_free(cluster);
2dedf83e
KS
2579 return -4;
2580 }
2e1e79da 2581 qemu_close(fd);
ce137829 2582 g_free(cluster);
a046433a
FB
2583
2584 return commit_mappings(s, first_cluster, dir_index);
2585}
2586
2587#ifdef DEBUG
2588/* test, if all mappings point to valid direntries */
2589static void check1(BDRVVVFATState* s)
2590{
2591 int i;
2592 for (i = 0; i < s->mapping.next; i++) {
d6a7e54e
HP
2593 mapping_t* mapping = array_get(&(s->mapping), i);
2594 if (mapping->mode & MODE_DELETED) {
2595 fprintf(stderr, "deleted\n");
2596 continue;
2597 }
2598 assert(mapping->dir_index < s->directory.next);
2599 direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
2600 assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
2601 if (mapping->mode & MODE_DIRECTORY) {
2602 assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
2603 assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
2604 }
de167e41 2605 }
de167e41
FB
2606}
2607
a046433a
FB
2608/* test, if all direntries have mappings */
2609static void check2(BDRVVVFATState* s)
de167e41 2610{
de167e41 2611 int i;
a046433a 2612 int first_mapping = -1;
de167e41 2613
a046433a 2614 for (i = 0; i < s->directory.next; i++) {
d6a7e54e
HP
2615 direntry_t* direntry = array_get(&(s->directory), i);
2616
2617 if (is_short_name(direntry) && begin_of_direntry(direntry)) {
2618 mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
2619 assert(mapping);
2620 assert(mapping->dir_index == i || is_dot(direntry));
2621 assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
2622 }
2623
2624 if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
2625 /* cluster start */
2626 int j, count = 0;
2627
2628 for (j = 0; j < s->mapping.next; j++) {
2629 mapping_t* mapping = array_get(&(s->mapping), j);
2630 if (mapping->mode & MODE_DELETED)
2631 continue;
2632 if (mapping->mode & MODE_DIRECTORY) {
2633 if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
2634 assert(++count == 1);
2635 if (mapping->first_mapping_index == -1)
2636 first_mapping = array_index(&(s->mapping), mapping);
2637 else
2638 assert(first_mapping == mapping->first_mapping_index);
2639 if (mapping->info.dir.parent_mapping_index < 0)
2640 assert(j == 0);
2641 else {
2642 mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
2643 assert(parent->mode & MODE_DIRECTORY);
2644 assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
2645 }
2646 }
2647 }
2648 }
2649 if (count == 0)
2650 first_mapping = -1;
2651 }
a046433a
FB
2652 }
2653}
2654#endif
de167e41 2655
a046433a
FB
2656static int handle_renames_and_mkdirs(BDRVVVFATState* s)
2657{
2658 int i;
de167e41 2659
a046433a
FB
2660#ifdef DEBUG
2661 fprintf(stderr, "handle_renames\n");
2662 for (i = 0; i < s->commits.next; i++) {
d6a7e54e
HP
2663 commit_t* commit = array_get(&(s->commits), i);
2664 fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action);
a046433a
FB
2665 }
2666#endif
2667
2668 for (i = 0; i < s->commits.next;) {
d6a7e54e
HP
2669 commit_t* commit = array_get(&(s->commits), i);
2670 if (commit->action == ACTION_RENAME) {
2671 mapping_t* mapping = find_mapping_for_cluster(s,
2672 commit->param.rename.cluster);
2673 char* old_path = mapping->path;
2674
2675 assert(commit->path);
2676 mapping->path = commit->path;
2677 if (rename(old_path, mapping->path))
2678 return -2;
2679
2680 if (mapping->mode & MODE_DIRECTORY) {
2681 int l1 = strlen(mapping->path);
2682 int l2 = strlen(old_path);
2683 int diff = l1 - l2;
2684 direntry_t* direntry = array_get(&(s->directory),
2685 mapping->info.dir.first_dir_index);
2686 uint32_t c = mapping->begin;
2687 int i = 0;
2688
2689 /* recurse */
2690 while (!fat_eof(s, c)) {
2691 do {
2692 direntry_t* d = direntry + i;
2693
2694 if (is_file(d) || (is_directory(d) && !is_dot(d))) {
2695 mapping_t* m = find_mapping_for_cluster(s,
2696 begin_of_direntry(d));
2697 int l = strlen(m->path);
2698 char* new_path = g_malloc(l + diff + 1);
2699
2700 assert(!strncmp(m->path, mapping->path, l2));
a046433a 2701
363a37d5
BS
2702 pstrcpy(new_path, l + diff + 1, mapping->path);
2703 pstrcpy(new_path + l1, l + diff + 1 - l1,
2704 m->path + l2);
a046433a 2705
d6a7e54e
HP
2706 schedule_rename(s, m->begin, new_path);
2707 }
2708 i++;
2709 } while((i % (0x10 * s->sectors_per_cluster)) != 0);
2710 c = fat_get(s, c);
2711 }
2712 }
de167e41 2713
ce137829 2714 g_free(old_path);
d6a7e54e
HP
2715 array_remove(&(s->commits), i);
2716 continue;
2717 } else if (commit->action == ACTION_MKDIR) {
2718 mapping_t* mapping;
2719 int j, parent_path_len;
a046433a 2720
48c2f068
FB
2721#ifdef __MINGW32__
2722 if (mkdir(commit->path))
2723 return -5;
2724#else
2725 if (mkdir(commit->path, 0755))
2726 return -5;
2727#endif
a046433a 2728
d6a7e54e
HP
2729 mapping = insert_mapping(s, commit->param.mkdir.cluster,
2730 commit->param.mkdir.cluster + 1);
2731 if (mapping == NULL)
2732 return -6;
2733
2734 mapping->mode = MODE_DIRECTORY;
2735 mapping->read_only = 0;
2736 mapping->path = commit->path;
2737 j = s->directory.next;
2738 assert(j);
2739 insert_direntries(s, s->directory.next,
2740 0x10 * s->sectors_per_cluster);
2741 mapping->info.dir.first_dir_index = j;
2742
2743 parent_path_len = strlen(commit->path)
2744 - strlen(get_basename(commit->path)) - 1;
2745 for (j = 0; j < s->mapping.next; j++) {
2746 mapping_t* m = array_get(&(s->mapping), j);
2747 if (m->first_mapping_index < 0 && m != mapping &&
2748 !strncmp(m->path, mapping->path, parent_path_len) &&
2749 strlen(m->path) == parent_path_len)
2750 break;
2751 }
2752 assert(j < s->mapping.next);
2753 mapping->info.dir.parent_mapping_index = j;
2754
2755 array_remove(&(s->commits), i);
2756 continue;
2757 }
2758
2759 i++;
a046433a
FB
2760 }
2761 return 0;
2762}
2763
2764/*
2765 * TODO: make sure that the short name is not matching *another* file
2766 */
2767static int handle_commits(BDRVVVFATState* s)
2768{
2769 int i, fail = 0;
2770
2771 vvfat_close_current_file(s);
2772
2773 for (i = 0; !fail && i < s->commits.next; i++) {
d6a7e54e
HP
2774 commit_t* commit = array_get(&(s->commits), i);
2775 switch(commit->action) {
2776 case ACTION_RENAME: case ACTION_MKDIR:
43dc2a64 2777 abort();
d6a7e54e
HP
2778 fail = -2;
2779 break;
2780 case ACTION_WRITEOUT: {
a6c6f76c
BS
2781#ifndef NDEBUG
2782 /* these variables are only used by assert() below */
d6a7e54e
HP
2783 direntry_t* entry = array_get(&(s->directory),
2784 commit->param.writeout.dir_index);
2785 uint32_t begin = begin_of_direntry(entry);
2786 mapping_t* mapping = find_mapping_for_cluster(s, begin);
a6c6f76c 2787#endif
a046433a 2788
d6a7e54e
HP
2789 assert(mapping);
2790 assert(mapping->begin == begin);
2791 assert(commit->path == NULL);
2792
2793 if (commit_one_file(s, commit->param.writeout.dir_index,
2794 commit->param.writeout.modified_offset))
2795 fail = -3;
2796
2797 break;
2798 }
2799 case ACTION_NEW_FILE: {
2800 int begin = commit->param.new_file.first_cluster;
2801 mapping_t* mapping = find_mapping_for_cluster(s, begin);
2802 direntry_t* entry;
2803 int i;
2804
2805 /* find direntry */
2806 for (i = 0; i < s->directory.next; i++) {
2807 entry = array_get(&(s->directory), i);
2808 if (is_file(entry) && begin_of_direntry(entry) == begin)
2809 break;
2810 }
2811
2812 if (i >= s->directory.next) {
2813 fail = -6;
2814 continue;
2815 }
2816
2817 /* make sure there exists an initial mapping */
2818 if (mapping && mapping->begin != begin) {
2819 mapping->end = begin;
2820 mapping = NULL;
2821 }
2822 if (mapping == NULL) {
2823 mapping = insert_mapping(s, begin, begin+1);
2824 }
2825 /* most members will be fixed in commit_mappings() */
2826 assert(commit->path);
2827 mapping->path = commit->path;
2828 mapping->read_only = 0;
2829 mapping->mode = MODE_NORMAL;
2830 mapping->info.file.offset = 0;
2831
2832 if (commit_one_file(s, i, 0))
2833 fail = -7;
2834
2835 break;
2836 }
2837 default:
43dc2a64 2838 abort();
d6a7e54e 2839 }
a046433a
FB
2840 }
2841 if (i > 0 && array_remove_slice(&(s->commits), 0, i))
d6a7e54e 2842 return -1;
a046433a
FB
2843 return fail;
2844}
2845
2846static int handle_deletes(BDRVVVFATState* s)
2847{
2848 int i, deferred = 1, deleted = 1;
2849
2850 /* delete files corresponding to mappings marked as deleted */
2851 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2852 while (deferred && deleted) {
d6a7e54e
HP
2853 deferred = 0;
2854 deleted = 0;
2855
2856 for (i = 1; i < s->mapping.next; i++) {
2857 mapping_t* mapping = array_get(&(s->mapping), i);
2858 if (mapping->mode & MODE_DELETED) {
2859 direntry_t* entry = array_get(&(s->directory),
2860 mapping->dir_index);
2861
2862 if (is_free(entry)) {
2863 /* remove file/directory */
2864 if (mapping->mode & MODE_DIRECTORY) {
2865 int j, next_dir_index = s->directory.next,
2866 first_dir_index = mapping->info.dir.first_dir_index;
2867
2868 if (rmdir(mapping->path) < 0) {
2869 if (errno == ENOTEMPTY) {
2870 deferred++;
2871 continue;
2872 } else
2873 return -5;
2874 }
2875
2876 for (j = 1; j < s->mapping.next; j++) {
2877 mapping_t* m = array_get(&(s->mapping), j);
2878 if (m->mode & MODE_DIRECTORY &&
2879 m->info.dir.first_dir_index >
2880 first_dir_index &&
2881 m->info.dir.first_dir_index <
2882 next_dir_index)
2883 next_dir_index =
2884 m->info.dir.first_dir_index;
2885 }
2886 remove_direntries(s, first_dir_index,
2887 next_dir_index - first_dir_index);
2888
2889 deleted++;
2890 }
2891 } else {
2892 if (unlink(mapping->path))
2893 return -4;
2894 deleted++;
2895 }
2896 DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
2897 remove_mapping(s, i);
2898 }
2899 }
de167e41 2900 }
a046433a
FB
2901
2902 return 0;
2903}
2904
2905/*
2906 * synchronize mapping with new state:
2907 *
2908 * - copy FAT (with bdrv_read)
2909 * - mark all filenames corresponding to mappings as deleted
2910 * - recurse direntries from root (using bs->bdrv_read)
2911 * - delete files corresponding to mappings marked as deleted
2912 */
2913static int do_commit(BDRVVVFATState* s)
2914{
2915 int ret = 0;
2916
2917 /* the real meat are the commits. Nothing to do? Move along! */
2918 if (s->commits.next == 0)
d6a7e54e 2919 return 0;
a046433a
FB
2920
2921 vvfat_close_current_file(s);
2922
2923 ret = handle_renames_and_mkdirs(s);
2924 if (ret) {
d6a7e54e 2925 fprintf(stderr, "Error handling renames (%d)\n", ret);
43dc2a64 2926 abort();
d6a7e54e 2927 return ret;
a046433a
FB
2928 }
2929
5fafdf24 2930 /* copy FAT (with bdrv_read) */
a046433a
FB
2931 memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
2932
2933 /* recurse direntries from root (using bs->bdrv_read) */
2934 ret = commit_direntries(s, 0, -1);
2935 if (ret) {
d6a7e54e 2936 fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
43dc2a64 2937 abort();
d6a7e54e 2938 return ret;
a046433a
FB
2939 }
2940
2941 ret = handle_commits(s);
2942 if (ret) {
d6a7e54e 2943 fprintf(stderr, "Error handling commits (%d)\n", ret);
43dc2a64 2944 abort();
d6a7e54e 2945 return ret;
a046433a
FB
2946 }
2947
2948 ret = handle_deletes(s);
2949 if (ret) {
d6a7e54e 2950 fprintf(stderr, "Error deleting\n");
43dc2a64 2951 abort();
d6a7e54e 2952 return ret;
a046433a
FB
2953 }
2954
eecc7747
KW
2955 if (s->qcow->bs->drv->bdrv_make_empty) {
2956 s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
7704df98 2957 }
a046433a
FB
2958
2959 memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
2960
2961DLOG(checkpoint());
2962 return 0;
2963}
2964
2965static int try_commit(BDRVVVFATState* s)
2966{
2967 vvfat_close_current_file(s);
2968DLOG(checkpoint());
2969 if(!is_consistent(s))
d6a7e54e 2970 return -1;
a046433a
FB
2971 return do_commit(s);
2972}
2973
5fafdf24 2974static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
a046433a
FB
2975 const uint8_t *buf, int nb_sectors)
2976{
5fafdf24 2977 BDRVVVFATState *s = bs->opaque;
a046433a
FB
2978 int i, ret;
2979
2980DLOG(checkpoint());
2981
ac48e389
KW
2982 /* Check if we're operating in read-only mode */
2983 if (s->qcow == NULL) {
2984 return -EACCES;
2985 }
2986
a046433a
FB
2987 vvfat_close_current_file(s);
2988
2989 /*
2990 * Some sanity checks:
2991 * - do not allow writing to the boot sector
a046433a
FB
2992 */
2993
4dc705dc 2994 if (sector_num < s->offset_to_fat)
d6a7e54e 2995 return -1;
a046433a
FB
2996
2997 for (i = sector2cluster(s, sector_num);
d6a7e54e
HP
2998 i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
2999 mapping_t* mapping = find_mapping_for_cluster(s, i);
3000 if (mapping) {
3001 if (mapping->read_only) {
3002 fprintf(stderr, "Tried to write to write-protected file %s\n",
3003 mapping->path);
3004 return -1;
3005 }
3006
3007 if (mapping->mode & MODE_DIRECTORY) {
3008 int begin = cluster2sector(s, i);
3009 int end = begin + s->sectors_per_cluster, k;
3010 int dir_index;
3011 const direntry_t* direntries;
3012 long_file_name lfn;
3013
3014 lfn_init(&lfn);
3015
3016 if (begin < sector_num)
3017 begin = sector_num;
3018 if (end > sector_num + nb_sectors)
3019 end = sector_num + nb_sectors;
3020 dir_index = mapping->dir_index +
3021 0x10 * (begin - mapping->begin * s->sectors_per_cluster);
3022 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
3023
3024 for (k = 0; k < (end - begin) * 0x10; k++) {
d6a7e54e 3025 /* no access to the direntry of a read-only file */
e03da26b 3026 if (is_short_name(direntries + k) &&
d6a7e54e
HP
3027 (direntries[k].attributes & 1)) {
3028 if (memcmp(direntries + k,
3029 array_get(&(s->directory), dir_index + k),
3030 sizeof(direntry_t))) {
3031 fprintf(stderr, "Warning: tried to write to write-protected file\n");
3032 return -1;
3033 }
3034 }
3035 }
3036 }
3037 i = mapping->end;
3038 } else
3039 i++;
a046433a
FB
3040 }
3041
3042 /*
3043 * Use qcow backend. Commit later.
3044 */
3045DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
18d51c4b 3046 ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
a046433a 3047 if (ret < 0) {
d6a7e54e
HP
3048 fprintf(stderr, "Error writing to qcow backend\n");
3049 return ret;
a046433a
FB
3050 }
3051
3052 for (i = sector2cluster(s, sector_num);
d6a7e54e
HP
3053 i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
3054 if (i >= 0)
3055 s->used_clusters[i] |= USED_ALLOCATED;
a046433a
FB
3056
3057DLOG(checkpoint());
3058 /* TODO: add timeout */
3059 try_commit(s);
3060
3061DLOG(checkpoint());
3062 return 0;
3063}
3064
4575eb49
KW
3065static int coroutine_fn
3066vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3067 QEMUIOVector *qiov, int flags)
e183ef75
PB
3068{
3069 int ret;
3070 BDRVVVFATState *s = bs->opaque;
4575eb49
KW
3071 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
3072 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
3073 void *buf;
3074
3075 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3076 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
3077
3078 buf = g_try_malloc(bytes);
3079 if (bytes && buf == NULL) {
3080 return -ENOMEM;
3081 }
3082 qemu_iovec_to_buf(qiov, 0, buf, bytes);
3083
e183ef75
PB
3084 qemu_co_mutex_lock(&s->lock);
3085 ret = vvfat_write(bs, sector_num, buf, nb_sectors);
3086 qemu_co_mutex_unlock(&s->lock);
4575eb49
KW
3087
3088 g_free(buf);
3089
e183ef75
PB
3090 return ret;
3091}
3092
b6b8a333 3093static int64_t coroutine_fn vvfat_co_get_block_status(BlockDriverState *bs,
d6a7e54e 3094 int64_t sector_num, int nb_sectors, int *n, BlockDriverState **file)
a046433a 3095{
139921aa 3096 *n = bs->total_sectors - sector_num;
4bc74be9
PB
3097 if (*n > nb_sectors) {
3098 *n = nb_sectors;
3099 } else if (*n < 0) {
3100 return 0;
3101 }
3102 return BDRV_BLOCK_DATA;
a046433a
FB
3103}
3104
4575eb49
KW
3105static int coroutine_fn
3106write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3107 QEMUIOVector *qiov, int flags)
3108{
254aee4d
PB
3109 int ret;
3110
9217e26f 3111 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
254aee4d
PB
3112 qemu_co_mutex_lock(&s->lock);
3113 ret = try_commit(s);
3114 qemu_co_mutex_unlock(&s->lock);
3115
3116 return ret;
a046433a
FB
3117}
3118
3119static void write_target_close(BlockDriverState *bs) {
9217e26f 3120 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
eecc7747 3121 bdrv_unref_child(s->bs, s->qcow);
ce137829 3122 g_free(s->qcow_filename);
a046433a
FB
3123}
3124
3125static BlockDriver vvfat_write_target = {
f9e96436 3126 .format_name = "vvfat_write_target",
a8a4d15c 3127 .instance_size = sizeof(void*),
4575eb49 3128 .bdrv_co_pwritev = write_target_commit,
f9e96436 3129 .bdrv_close = write_target_close,
a046433a
FB
3130};
3131
eecc7747
KW
3132static void vvfat_qcow_options(int *child_flags, QDict *child_options,
3133 int parent_flags, QDict *parent_options)
a046433a 3134{
f87a0e29
AG
3135 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
3136 *child_flags = BDRV_O_NO_FLUSH;
eecc7747
KW
3137}
3138
3139static const BdrvChildRole child_vvfat_qcow = {
3140 .inherit_options = vvfat_qcow_options,
3141};
3142
3143static int enable_write_target(BlockDriverState *bs, Error **errp)
3144{
3145 BDRVVVFATState *s = bs->opaque;
facdbb02 3146 BlockDriver *bdrv_qcow = NULL;
5db15a57 3147 BlockDriverState *backing;
facdbb02 3148 QemuOpts *opts = NULL;
a655211a 3149 int ret;
a046433a 3150 int size = sector2cluster(s, s->sector_count);
e6641719
HR
3151 QDict *options;
3152
a046433a
FB
3153 s->used_clusters = calloc(size, 1);
3154
c227f099 3155 array_init(&(s->commits), sizeof(commit_t));
a046433a 3156
9a29e18f
JC
3157 s->qcow_filename = g_malloc(PATH_MAX);
3158 ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
eba25057 3159 if (ret < 0) {
68c70af1 3160 error_setg_errno(errp, -ret, "can't create temporary file");
78f27bd0 3161 goto err;
eba25057 3162 }
91a073a9
KW
3163
3164 bdrv_qcow = bdrv_find_format("qcow");
1bcb15cf
HR
3165 if (!bdrv_qcow) {
3166 error_setg(errp, "Failed to locate qcow driver");
3167 ret = -ENOENT;
3168 goto err;
3169 }
3170
c282e1fd 3171 opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
39101f25
MA
3172 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
3173 &error_abort);
f43e47db 3174 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
91a073a9 3175
c282e1fd 3176 ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
facdbb02 3177 qemu_opts_del(opts);
78f27bd0
FZ
3178 if (ret < 0) {
3179 goto err;
3180 }
a655211a 3181
e6641719 3182 options = qdict_new();
46f5ac20 3183 qdict_put_str(options, "write-target.driver", "qcow");
eecc7747
KW
3184 s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
3185 &child_vvfat_qcow, false, errp);
c4b48bfd 3186 QDECREF(options);
5b363937
HR
3187 if (!s->qcow) {
3188 ret = -EINVAL;
78f27bd0 3189 goto err;
d6e9098e 3190 }
a046433a
FB
3191
3192#ifndef _WIN32
3193 unlink(s->qcow_filename);
3194#endif
3195
a8a4d15c
KW
3196 backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
3197 &error_abort);
3198 *(void**) backing->opaque = s;
3199
12fa4af6 3200 bdrv_set_backing_hd(s->bs, backing, &error_abort);
5db15a57
KW
3201 bdrv_unref(backing);
3202
de167e41 3203 return 0;
78f27bd0
FZ
3204
3205err:
3206 g_free(s->qcow_filename);
3207 s->qcow_filename = NULL;
3208 return ret;
de167e41
FB
3209}
3210
91ef3825
KW
3211static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
3212 const BdrvChildRole *role,
3213 uint64_t perm, uint64_t shared,
3214 uint64_t *nperm, uint64_t *nshared)
3215{
3216 BDRVVVFATState *s = bs->opaque;
3217
3218 assert(c == s->qcow || role == &child_backing);
3219
3220 if (c == s->qcow) {
3221 /* This is a private node, nobody should try to attach to it */
3222 *nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
3223 *nshared = BLK_PERM_WRITE_UNCHANGED;
3224 } else {
3225 /* The backing file is there so 'commit' can use it. vvfat doesn't
3226 * access it in any way. */
3227 *nperm = 0;
3228 *nshared = BLK_PERM_ALL;
3229 }
3230}
3231
de167e41
FB
3232static void vvfat_close(BlockDriverState *bs)
3233{
3234 BDRVVVFATState *s = bs->opaque;
3235
3236 vvfat_close_current_file(s);
3237 array_free(&(s->fat));
3238 array_free(&(s->directory));
3239 array_free(&(s->mapping));
ce137829 3240 g_free(s->cluster_buffer);
3397f0cb
KW
3241
3242 if (s->qcow) {
3243 migrate_del_blocker(s->migration_blocker);
3244 error_free(s->migration_blocker);
3245 }
de167e41
FB
3246}
3247
5efa9d5a 3248static BlockDriver bdrv_vvfat = {
7ad9be64
KW
3249 .format_name = "vvfat",
3250 .protocol_name = "fat",
3251 .instance_size = sizeof(BDRVVVFATState),
3252
3253 .bdrv_parse_filename = vvfat_parse_filename,
3254 .bdrv_file_open = vvfat_open,
a6506481 3255 .bdrv_refresh_limits = vvfat_refresh_limits,
7ad9be64 3256 .bdrv_close = vvfat_close,
91ef3825 3257 .bdrv_child_perm = vvfat_child_perm,
7ad9be64 3258
4575eb49
KW
3259 .bdrv_co_preadv = vvfat_co_preadv,
3260 .bdrv_co_pwritev = vvfat_co_pwritev,
b6b8a333 3261 .bdrv_co_get_block_status = vvfat_co_get_block_status,
de167e41
FB
3262};
3263
5efa9d5a
AL
3264static void bdrv_vvfat_init(void)
3265{
3266 bdrv_register(&bdrv_vvfat);
3267}
3268
3269block_init(bdrv_vvfat_init);
3270
a046433a 3271#ifdef DEBUG
3f47aa8c 3272static void checkpoint(void) {
c227f099 3273 assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
a046433a
FB
3274 check1(vvv);
3275 check2(vvv);
3276 assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
3277#if 0
c227f099 3278 if (((direntry_t*)vvv->directory.pointer)[1].attributes != 0xf)
d6a7e54e 3279 fprintf(stderr, "Nonono!\n");
c227f099
AL
3280 mapping_t* mapping;
3281 direntry_t* direntry;
a046433a
FB
3282 assert(vvv->mapping.size >= vvv->mapping.item_size * vvv->mapping.next);
3283 assert(vvv->directory.size >= vvv->directory.item_size * vvv->directory.next);
3284 if (vvv->mapping.next<47)
d6a7e54e 3285 return;
a046433a
FB
3286 assert((mapping = array_get(&(vvv->mapping), 47)));
3287 assert(mapping->dir_index < vvv->directory.next);
3288 direntry = array_get(&(vvv->directory), mapping->dir_index);
3289 assert(!memcmp(direntry->name, "USB H ", 11) || direntry->name[0]==0);
3290#endif
a046433a
FB
3291}
3292#endif