blob: 4a17a49e12823a9a755dcf8aaec186bb18b2eb70 [file] [log] [blame]
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001/* vim:set shiftwidth=4 ts=4: */
bellardde167e42005-04-28 21:15:08 +00002/*
3 * QEMU Block driver for virtual VFAT (shadows a local directory)
ths5fafdf22007-09-16 21:08:06 +00004 *
bellarda0464332005-12-18 18:29:50 +00005 * Copyright (c) 2004,2005 Johannes E. Schindelin
ths5fafdf22007-09-16 21:08:06 +00006 *
bellardde167e42005-04-28 21:15:08 +00007 * 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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010025
Peter Maydell80c71a22016-01-18 18:01:42 +000026#include "qemu/osdep.h"
bellardde167e42005-04-28 21:15:08 +000027#include <dirent.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010029#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010031#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010032#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020033#include "migration/blocker.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010034#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010035#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070037#include "qemu/error-report.h"
bellardde167e42005-04-28 21:15:08 +000038
bellarda0464332005-12-18 18:29:50 +000039#ifndef S_IWGRP
40#define S_IWGRP 0
41#endif
42#ifndef S_IWOTH
43#define S_IWOTH 0
44#endif
bellardde167e42005-04-28 21:15:08 +000045
bellarda0464332005-12-18 18:29:50 +000046/* TODO: add ":bootsector=blabla.img:" */
47/* LATER TODO: add automatic boot sector generation from
48 BOOTEASY.ASM and Ranish Partition Manager
ths5fafdf22007-09-16 21:08:06 +000049 Note that DOS assumes the system files to be the first files in the
bellarda0464332005-12-18 18:29:50 +000050 file system (test if the boot sector still relies on that fact)! */
51/* MAYBE TODO: write block-visofs.c */
52/* TODO: call try_commit() only after a timeout */
bellardde167e42005-04-28 21:15:08 +000053
bellarda0464332005-12-18 18:29:50 +000054/* #define DEBUG */
55
56#ifdef DEBUG
57
58#define DLOG(a) a
59
blueswir13f47aa82008-03-09 06:59:01 +000060static void checkpoint(void);
bellarda0464332005-12-18 18:29:50 +000061
bellarda0464332005-12-18 18:29:50 +000062#else
63
64#define DLOG(a)
65
66#endif
bellardde167e42005-04-28 21:15:08 +000067
Hervé Poussineau63d261c2017-07-15 15:28:39 +020068/* bootsector OEM name. see related compatibility problems at:
69 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
70 * http://seasip.info/Misc/oemid.html
71 */
72#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
73
Hervé Poussineau8c4517f2017-07-15 15:28:38 +020074#define DIR_DELETED 0xe5
75#define DIR_KANJI DIR_DELETED
76#define DIR_KANJI_FAKE 0x05
77#define DIR_FREE 0x00
78
bellardde167e42005-04-28 21:15:08 +000079/* dynamic array functions */
Anthony Liguoric227f092009-10-01 16:12:16 -050080typedef struct array_t {
bellardde167e42005-04-28 21:15:08 +000081 char* pointer;
82 unsigned int size,next,item_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050083} array_t;
bellardde167e42005-04-28 21:15:08 +000084
Anthony Liguoric227f092009-10-01 16:12:16 -050085static inline void array_init(array_t* array,unsigned int item_size)
bellardde167e42005-04-28 21:15:08 +000086{
blueswir1511d2b12009-03-07 15:32:56 +000087 array->pointer = NULL;
bellardde167e42005-04-28 21:15:08 +000088 array->size=0;
89 array->next=0;
90 array->item_size=item_size;
91}
92
Anthony Liguoric227f092009-10-01 16:12:16 -050093static inline void array_free(array_t* array)
bellardde167e42005-04-28 21:15:08 +000094{
Stefan Weilce137822011-09-30 23:29:53 +020095 g_free(array->pointer);
bellardde167e42005-04-28 21:15:08 +000096 array->size=array->next=0;
97}
98
bellarda0464332005-12-18 18:29:50 +000099/* does not automatically grow */
Anthony Liguoric227f092009-10-01 16:12:16 -0500100static inline void* array_get(array_t* array,unsigned int index) {
bellarda0464332005-12-18 18:29:50 +0000101 assert(index < array->next);
102 return array->pointer + index * array->item_size;
103}
104
Anthony Liguoric227f092009-10-01 16:12:16 -0500105static inline int array_ensure_allocated(array_t* array, int index)
bellarda0464332005-12-18 18:29:50 +0000106{
107 if((index + 1) * array->item_size > array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200108 int new_size = (index + 32) * array->item_size;
109 array->pointer = g_realloc(array->pointer, new_size);
110 if (!array->pointer)
111 return -1;
Hervé Poussineauf80256b2017-07-15 15:28:41 +0200112 memset(array->pointer + array->size, 0, new_size - array->size);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200113 array->size = new_size;
114 array->next = index + 1;
bellardde167e42005-04-28 21:15:08 +0000115 }
bellarda0464332005-12-18 18:29:50 +0000116
117 return 0;
bellardde167e42005-04-28 21:15:08 +0000118}
119
Anthony Liguoric227f092009-10-01 16:12:16 -0500120static inline void* array_get_next(array_t* array) {
bellarda0464332005-12-18 18:29:50 +0000121 unsigned int next = array->next;
bellarda0464332005-12-18 18:29:50 +0000122
123 if (array_ensure_allocated(array, next) < 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200124 return NULL;
bellarda0464332005-12-18 18:29:50 +0000125
126 array->next = next + 1;
Eduardo Habkost9be38592016-06-13 18:57:58 -0300127 return array_get(array, next);
bellardde167e42005-04-28 21:15:08 +0000128}
129
Anthony Liguoric227f092009-10-01 16:12:16 -0500130static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
bellardde167e42005-04-28 21:15:08 +0000131 if((array->next+count)*array->item_size>array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200132 int increment=count*array->item_size;
133 array->pointer=g_realloc(array->pointer,array->size+increment);
134 if(!array->pointer)
blueswir1511d2b12009-03-07 15:32:56 +0000135 return NULL;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200136 array->size+=increment;
bellardde167e42005-04-28 21:15:08 +0000137 }
138 memmove(array->pointer+(index+count)*array->item_size,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200139 array->pointer+index*array->item_size,
140 (array->next-index)*array->item_size);
bellardde167e42005-04-28 21:15:08 +0000141 array->next+=count;
142 return array->pointer+index*array->item_size;
143}
144
145/* this performs a "roll", so that the element which was at index_from becomes
146 * index_to, but the order of all other elements is preserved. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500147static inline int array_roll(array_t* array,int index_to,int index_from,int count)
bellardde167e42005-04-28 21:15:08 +0000148{
149 char* buf;
150 char* from;
151 char* to;
152 int is;
153
154 if(!array ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200155 index_to<0 || index_to>=array->next ||
156 index_from<0 || index_from>=array->next)
157 return -1;
ths3b46e622007-09-17 08:09:54 +0000158
bellardde167e42005-04-28 21:15:08 +0000159 if(index_to==index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200160 return 0;
bellardde167e42005-04-28 21:15:08 +0000161
162 is=array->item_size;
163 from=array->pointer+index_from*is;
164 to=array->pointer+index_to*is;
Anthony Liguori7267c092011-08-20 22:09:37 -0500165 buf=g_malloc(is*count);
bellardde167e42005-04-28 21:15:08 +0000166 memcpy(buf,from,is*count);
167
168 if(index_to<index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200169 memmove(to+is*count,to,from-to);
bellardde167e42005-04-28 21:15:08 +0000170 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200171 memmove(from,from+is*count,to-from);
ths3b46e622007-09-17 08:09:54 +0000172
bellardde167e42005-04-28 21:15:08 +0000173 memcpy(to,buf,is*count);
174
Stefan Weilce137822011-09-30 23:29:53 +0200175 g_free(buf);
bellardde167e42005-04-28 21:15:08 +0000176
177 return 0;
178}
179
Anthony Liguoric227f092009-10-01 16:12:16 -0500180static inline int array_remove_slice(array_t* array,int index, int count)
bellarda0464332005-12-18 18:29:50 +0000181{
182 assert(index >=0);
183 assert(count > 0);
184 assert(index + count <= array->next);
185 if(array_roll(array,array->next-1,index,count))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200186 return -1;
bellarda0464332005-12-18 18:29:50 +0000187 array->next -= count;
188 return 0;
189}
190
Anthony Liguoric227f092009-10-01 16:12:16 -0500191static int array_remove(array_t* array,int index)
bellardde167e42005-04-28 21:15:08 +0000192{
bellarda0464332005-12-18 18:29:50 +0000193 return array_remove_slice(array, index, 1);
194}
195
196/* return the index for a given member */
Anthony Liguoric227f092009-10-01 16:12:16 -0500197static int array_index(array_t* array, void* pointer)
bellarda0464332005-12-18 18:29:50 +0000198{
199 size_t offset = (char*)pointer - array->pointer;
bellarda0464332005-12-18 18:29:50 +0000200 assert((offset % array->item_size) == 0);
201 assert(offset/array->item_size < array->next);
202 return offset/array->item_size;
bellardde167e42005-04-28 21:15:08 +0000203}
204
205/* These structures are used to fake a disk and the VFAT filesystem.
Stefan Weil541dc0d2011-08-31 12:38:01 +0200206 * For this reason we need to use QEMU_PACKED. */
bellardde167e42005-04-28 21:15:08 +0000207
Anthony Liguoric227f092009-10-01 16:12:16 -0500208typedef struct bootsector_t {
bellardde167e42005-04-28 21:15:08 +0000209 uint8_t jump[3];
210 uint8_t name[8];
211 uint16_t sector_size;
212 uint8_t sectors_per_cluster;
213 uint16_t reserved_sectors;
214 uint8_t number_of_fats;
215 uint16_t root_entries;
bellarda0464332005-12-18 18:29:50 +0000216 uint16_t total_sectors16;
bellardde167e42005-04-28 21:15:08 +0000217 uint8_t media_type;
218 uint16_t sectors_per_fat;
219 uint16_t sectors_per_track;
220 uint16_t number_of_heads;
221 uint32_t hidden_sectors;
222 uint32_t total_sectors;
223 union {
224 struct {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200225 uint8_t drive_number;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200226 uint8_t reserved1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200227 uint8_t signature;
228 uint32_t id;
229 uint8_t volume_label[11];
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200230 uint8_t fat_type[8];
231 uint8_t ignored[0x1c0];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200232 } QEMU_PACKED fat16;
233 struct {
234 uint32_t sectors_per_fat;
235 uint16_t flags;
236 uint8_t major,minor;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200237 uint32_t first_cluster_of_root_dir;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200238 uint16_t info_sector;
239 uint16_t backup_boot_sector;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200240 uint8_t reserved[12];
241 uint8_t drive_number;
242 uint8_t reserved1;
243 uint8_t signature;
244 uint32_t id;
245 uint8_t volume_label[11];
246 uint8_t fat_type[8];
247 uint8_t ignored[0x1a4];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200248 } QEMU_PACKED fat32;
bellardde167e42005-04-28 21:15:08 +0000249 } u;
bellardde167e42005-04-28 21:15:08 +0000250 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200251} QEMU_PACKED bootsector_t;
bellardde167e42005-04-28 21:15:08 +0000252
thsb5700942007-09-25 14:47:03 +0000253typedef struct {
254 uint8_t head;
255 uint8_t sector;
256 uint8_t cylinder;
Anthony Liguoric227f092009-10-01 16:12:16 -0500257} mbr_chs_t;
thsb5700942007-09-25 14:47:03 +0000258
Anthony Liguoric227f092009-10-01 16:12:16 -0500259typedef struct partition_t {
bellardde167e42005-04-28 21:15:08 +0000260 uint8_t attributes; /* 0x80 = bootable */
Anthony Liguoric227f092009-10-01 16:12:16 -0500261 mbr_chs_t start_CHS;
thsb5700942007-09-25 14:47:03 +0000262 uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
Anthony Liguoric227f092009-10-01 16:12:16 -0500263 mbr_chs_t end_CHS;
bellardde167e42005-04-28 21:15:08 +0000264 uint32_t start_sector_long;
thsb5700942007-09-25 14:47:03 +0000265 uint32_t length_sector_long;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200266} QEMU_PACKED partition_t;
bellardde167e42005-04-28 21:15:08 +0000267
Anthony Liguoric227f092009-10-01 16:12:16 -0500268typedef struct mbr_t {
thsb5700942007-09-25 14:47:03 +0000269 uint8_t ignored[0x1b8];
270 uint32_t nt_id;
271 uint8_t ignored2[2];
Anthony Liguoric227f092009-10-01 16:12:16 -0500272 partition_t partition[4];
bellardde167e42005-04-28 21:15:08 +0000273 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200274} QEMU_PACKED mbr_t;
bellardde167e42005-04-28 21:15:08 +0000275
Anthony Liguoric227f092009-10-01 16:12:16 -0500276typedef struct direntry_t {
Stefan Weilf671d172013-12-11 21:37:11 +0100277 uint8_t name[8 + 3];
bellardde167e42005-04-28 21:15:08 +0000278 uint8_t attributes;
279 uint8_t reserved[2];
280 uint16_t ctime;
281 uint16_t cdate;
282 uint16_t adate;
283 uint16_t begin_hi;
284 uint16_t mtime;
285 uint16_t mdate;
286 uint16_t begin;
287 uint32_t size;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200288} QEMU_PACKED direntry_t;
bellardde167e42005-04-28 21:15:08 +0000289
290/* this structure are used to transparently access the files */
291
Anthony Liguoric227f092009-10-01 16:12:16 -0500292typedef struct mapping_t {
bellarda0464332005-12-18 18:29:50 +0000293 /* begin is the first cluster, end is the last+1 */
294 uint32_t begin,end;
bellardde167e42005-04-28 21:15:08 +0000295 /* as s->directory is growable, no pointer may be used here */
296 unsigned int dir_index;
bellarda0464332005-12-18 18:29:50 +0000297 /* the clusters of a file may be in any order; this points to the first */
298 int first_mapping_index;
299 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200300 /* offset is
301 * - the offset in the file (in clusters) for a file, or
Hervé Poussineauad05b312017-05-22 23:11:56 +0200302 * - the next cluster of the directory for a directory
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200303 */
304 struct {
305 uint32_t offset;
306 } file;
307 struct {
308 int parent_mapping_index;
309 int first_dir_index;
310 } dir;
bellarda0464332005-12-18 18:29:50 +0000311 } info;
312 /* path contains the full path, i.e. it always starts with s->path */
313 char* path;
314
Hervé Poussineauad05b312017-05-22 23:11:56 +0200315 enum {
316 MODE_UNDEFINED = 0,
317 MODE_NORMAL = 1,
318 MODE_MODIFIED = 2,
319 MODE_DIRECTORY = 4,
320 MODE_DELETED = 8,
321 } mode;
bellarda0464332005-12-18 18:29:50 +0000322 int read_only;
Anthony Liguoric227f092009-10-01 16:12:16 -0500323} mapping_t;
bellardde167e42005-04-28 21:15:08 +0000324
bellarda0464332005-12-18 18:29:50 +0000325#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -0500326static void print_direntry(const struct direntry_t*);
327static void print_mapping(const struct mapping_t* mapping);
bellarda0464332005-12-18 18:29:50 +0000328#endif
bellardde167e42005-04-28 21:15:08 +0000329
330/* here begins the real VVFAT driver */
331
332typedef struct BDRVVVFATState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200333 CoMutex lock;
bellarda0464332005-12-18 18:29:50 +0000334 BlockDriverState* bs; /* pointer to parent */
bellardde167e42005-04-28 21:15:08 +0000335 unsigned char first_sectors[0x40*0x200];
ths3b46e622007-09-17 08:09:54 +0000336
bellardde167e42005-04-28 21:15:08 +0000337 int fat_type; /* 16 or 32 */
Anthony Liguoric227f092009-10-01 16:12:16 -0500338 array_t fat,directory,mapping;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200339 char volume_label[11];
ths3b46e622007-09-17 08:09:54 +0000340
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200341 uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
342
bellardde167e42005-04-28 21:15:08 +0000343 unsigned int cluster_size;
344 unsigned int sectors_per_cluster;
345 unsigned int sectors_per_fat;
bellarda0464332005-12-18 18:29:50 +0000346 uint32_t last_cluster_of_root_directory;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200347 /* how many entries are available in root directory (0 for FAT32) */
348 uint16_t root_entries;
bellardde167e42005-04-28 21:15:08 +0000349 uint32_t sector_count; /* total number of sectors of the partition */
350 uint32_t cluster_count; /* total number of clusters of this partition */
bellardde167e42005-04-28 21:15:08 +0000351 uint32_t max_fat_value;
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200352 uint32_t offset_to_fat;
353 uint32_t offset_to_root_dir;
ths3b46e622007-09-17 08:09:54 +0000354
bellardde167e42005-04-28 21:15:08 +0000355 int current_fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500356 mapping_t* current_mapping;
bellarda0464332005-12-18 18:29:50 +0000357 unsigned char* cluster; /* points to current cluster */
358 unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
bellardde167e42005-04-28 21:15:08 +0000359 unsigned int current_cluster;
360
361 /* write support */
bellarda0464332005-12-18 18:29:50 +0000362 char* qcow_filename;
Kevin Wolfeecc7742016-05-30 17:13:09 +0200363 BdrvChild* qcow;
bellarda0464332005-12-18 18:29:50 +0000364 void* fat2;
365 char* used_clusters;
Anthony Liguoric227f092009-10-01 16:12:16 -0500366 array_t commits;
bellarda0464332005-12-18 18:29:50 +0000367 const char* path;
368 int downcase_short_names;
Kevin Wolf3397f0c2011-11-22 16:52:13 +0100369
370 Error *migration_blocker;
bellardde167e42005-04-28 21:15:08 +0000371} BDRVVVFATState;
372
thsb5700942007-09-25 14:47:03 +0000373/* take the sector position spos and convert it to Cylinder/Head/Sector position
374 * if the position is outside the specified geometry, fill maximum value for CHS
375 * and return 1 to signal overflow.
376 */
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200377static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
378{
thsb5700942007-09-25 14:47:03 +0000379 int head,sector;
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200380 sector = spos % secs; spos /= secs;
381 head = spos % heads; spos /= heads;
382 if (spos >= cyls) {
thsb5700942007-09-25 14:47:03 +0000383 /* Overflow,
384 it happens if 32bit sector positions are used, while CHS is only 24bit.
385 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
386 chs->head = 0xFF;
387 chs->sector = 0xFF;
388 chs->cylinder = 0xFF;
389 return 1;
390 }
391 chs->head = (uint8_t)head;
392 chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
393 chs->cylinder = (uint8_t)spos;
394 return 0;
395}
bellardde167e42005-04-28 21:15:08 +0000396
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200397static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
bellardde167e42005-04-28 21:15:08 +0000398{
399 /* TODO: if the files mbr.img and bootsect.img exist, use them */
Anthony Liguoric227f092009-10-01 16:12:16 -0500400 mbr_t* real_mbr=(mbr_t*)s->first_sectors;
401 partition_t* partition = &(real_mbr->partition[0]);
thsb5700942007-09-25 14:47:03 +0000402 int lba;
bellardde167e42005-04-28 21:15:08 +0000403
404 memset(s->first_sectors,0,512);
ths3b46e622007-09-17 08:09:54 +0000405
thsb5700942007-09-25 14:47:03 +0000406 /* Win NT Disk Signature */
407 real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
408
bellardde167e42005-04-28 21:15:08 +0000409 partition->attributes=0x80; /* bootable */
thsb5700942007-09-25 14:47:03 +0000410
411 /* LBA is used when partition is outside the CHS geometry */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200412 lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200413 cyls, heads, secs);
414 lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
415 cyls, heads, secs);
thsb5700942007-09-25 14:47:03 +0000416
417 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200418 partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
Markus Armbrusterf91cbef2012-07-10 11:12:28 +0200419 partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200420 - s->offset_to_bootsector);
thsb5700942007-09-25 14:47:03 +0000421
bellarda0464332005-12-18 18:29:50 +0000422 /* FAT12/FAT16/FAT32 */
thsb5700942007-09-25 14:47:03 +0000423 /* DOS uses different types when partition is LBA,
424 probably to prevent older versions from using CHS on them */
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200425 partition->fs_type = s->fat_type == 12 ? 0x1 :
426 s->fat_type == 16 ? (lba ? 0xe : 0x06) :
427 /*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
bellardde167e42005-04-28 21:15:08 +0000428
429 real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
430}
431
bellarda0464332005-12-18 18:29:50 +0000432/* direntry functions */
433
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200434static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
bellardde167e42005-04-28 21:15:08 +0000435{
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200436 int number_of_entries, i;
437 glong length;
438 direntry_t *entry;
bellardde167e42005-04-28 21:15:08 +0000439
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200440 gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
441 if (!longname) {
442 fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
443 return NULL;
444 }
445
Marc-André Lureau78ee96d2017-06-22 13:04:16 +0200446 number_of_entries = DIV_ROUND_UP(length * 2, 26);
bellardde167e42005-04-28 21:15:08 +0000447
448 for(i=0;i<number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200449 entry=array_get_next(&(s->directory));
450 entry->attributes=0xf;
451 entry->reserved[0]=0;
452 entry->begin=0;
453 entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
bellardde167e42005-04-28 21:15:08 +0000454 }
balrog1e080d52007-12-24 13:26:04 +0000455 for(i=0;i<26*number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200456 int offset=(i%26);
457 if(offset<10) offset=1+offset;
458 else if(offset<22) offset=14+offset-10;
459 else offset=28+offset-22;
460 entry=array_get(&(s->directory),s->directory.next-1-(i/26));
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200461 if (i >= 2 * length + 2) {
462 entry->name[offset] = 0xff;
463 } else if (i % 2 == 0) {
464 entry->name[offset] = longname[i / 2] & 0xff;
465 } else {
466 entry->name[offset] = longname[i / 2] >> 8;
467 }
bellardde167e42005-04-28 21:15:08 +0000468 }
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200469 g_free(longname);
bellardde167e42005-04-28 21:15:08 +0000470 return array_get(&(s->directory),s->directory.next-number_of_entries);
471}
472
Anthony Liguoric227f092009-10-01 16:12:16 -0500473static char is_free(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000474{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200475 return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
bellarda0464332005-12-18 18:29:50 +0000476}
477
Anthony Liguoric227f092009-10-01 16:12:16 -0500478static char is_volume_label(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000479{
480 return direntry->attributes == 0x28;
481}
482
Anthony Liguoric227f092009-10-01 16:12:16 -0500483static char is_long_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000484{
485 return direntry->attributes == 0xf;
486}
487
Anthony Liguoric227f092009-10-01 16:12:16 -0500488static char is_short_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000489{
490 return !is_volume_label(direntry) && !is_long_name(direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200491 && !is_free(direntry);
bellarda0464332005-12-18 18:29:50 +0000492}
493
Anthony Liguoric227f092009-10-01 16:12:16 -0500494static char is_directory(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000495{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200496 return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
bellarda0464332005-12-18 18:29:50 +0000497}
498
Anthony Liguoric227f092009-10-01 16:12:16 -0500499static inline char is_dot(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000500{
501 return is_short_name(direntry) && direntry->name[0] == '.';
502}
503
Anthony Liguoric227f092009-10-01 16:12:16 -0500504static char is_file(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000505{
506 return is_short_name(direntry) && !is_directory(direntry);
507}
508
Anthony Liguoric227f092009-10-01 16:12:16 -0500509static inline uint32_t begin_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000510{
511 return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
512}
513
Anthony Liguoric227f092009-10-01 16:12:16 -0500514static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000515{
516 return le32_to_cpu(direntry->size);
517}
518
Anthony Liguoric227f092009-10-01 16:12:16 -0500519static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
bellarda0464332005-12-18 18:29:50 +0000520{
521 direntry->begin = cpu_to_le16(begin & 0xffff);
522 direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
523}
524
Hervé Poussineau0c361112017-05-22 23:12:01 +0200525static uint8_t to_valid_short_char(gunichar c)
526{
527 c = g_unichar_toupper(c);
528 if ((c >= '0' && c <= '9') ||
529 (c >= 'A' && c <= 'Z') ||
530 strchr("$%'-_@~`!(){}^#&", c) != 0) {
531 return c;
532 } else {
533 return 0;
534 }
535}
536
537static direntry_t *create_short_filename(BDRVVVFATState *s,
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200538 const char *filename,
539 unsigned int directory_start)
Hervé Poussineau0c361112017-05-22 23:12:01 +0200540{
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200541 int i, j = 0;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200542 direntry_t *entry = array_get_next(&(s->directory));
543 const gchar *p, *last_dot = NULL;
544 gunichar c;
545 bool lossy_conversion = false;
Max Reitz7c8730d2017-07-17 17:12:07 +0200546 char tail[8];
Hervé Poussineau0c361112017-05-22 23:12:01 +0200547
548 if (!entry) {
549 return NULL;
550 }
551 memset(entry->name, 0x20, sizeof(entry->name));
552
553 /* copy filename and search last dot */
554 for (p = filename; ; p = g_utf8_next_char(p)) {
555 c = g_utf8_get_char(p);
556 if (c == '\0') {
557 break;
558 } else if (c == '.') {
559 if (j == 0) {
560 /* '.' at start of filename */
561 lossy_conversion = true;
562 } else {
563 if (last_dot) {
564 lossy_conversion = true;
565 }
566 last_dot = p;
567 }
568 } else if (!last_dot) {
569 /* first part of the name; copy it */
570 uint8_t v = to_valid_short_char(c);
571 if (j < 8 && v) {
572 entry->name[j++] = v;
573 } else {
574 lossy_conversion = true;
575 }
576 }
577 }
578
579 /* copy extension (if any) */
580 if (last_dot) {
581 j = 0;
582 for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
583 c = g_utf8_get_char(p);
584 if (c == '\0') {
585 break;
586 } else {
587 /* extension; copy it */
588 uint8_t v = to_valid_short_char(c);
589 if (j < 3 && v) {
590 entry->name[8 + (j++)] = v;
591 } else {
592 lossy_conversion = true;
593 }
594 }
595 }
596 }
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200597
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200598 if (entry->name[0] == DIR_KANJI) {
599 entry->name[0] = DIR_KANJI_FAKE;
Hervé Poussineau78f002c2017-05-22 23:12:04 +0200600 }
601
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200602 /* numeric-tail generation */
603 for (j = 0; j < 8; j++) {
604 if (entry->name[j] == ' ') {
605 break;
606 }
607 }
608 for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
609 direntry_t *entry1;
610 if (i > 0) {
Max Reitz7c8730d2017-07-17 17:12:07 +0200611 int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
612 assert(len <= 7);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200613 memcpy(entry->name + MIN(j, 8 - len), tail, len);
614 }
615 for (entry1 = array_get(&(s->directory), directory_start);
616 entry1 < entry; entry1++) {
617 if (!is_long_name(entry1) &&
618 !memcmp(entry1->name, entry->name, 11)) {
619 break; /* found dupe */
620 }
621 }
622 if (entry1 == entry) {
623 /* no dupe found */
624 return entry;
625 }
626 }
627 return NULL;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200628}
629
bellardde167e42005-04-28 21:15:08 +0000630/* fat functions */
631
Anthony Liguoric227f092009-10-01 16:12:16 -0500632static inline uint8_t fat_chksum(const direntry_t* entry)
bellardde167e42005-04-28 21:15:08 +0000633{
634 uint8_t chksum=0;
635 int i;
636
Stefan Weilf671d172013-12-11 21:37:11 +0100637 for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
638 chksum = (((chksum & 0xfe) >> 1) |
639 ((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
Aurelien Jarno5606c222009-04-25 00:08:05 +0200640 }
ths3b46e622007-09-17 08:09:54 +0000641
bellardde167e42005-04-28 21:15:08 +0000642 return chksum;
643}
644
645/* if return_time==0, this returns the fat_date, else the fat_time */
646static uint16_t fat_datetime(time_t time,int return_time) {
647 struct tm* t;
bellardde167e42005-04-28 21:15:08 +0000648 struct tm t1;
Michael S. Tsirkin6ab00ce2009-09-30 19:43:31 +0200649 t = &t1;
bellardde167e42005-04-28 21:15:08 +0000650 localtime_r(&time,t);
bellardde167e42005-04-28 21:15:08 +0000651 if(return_time)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200652 return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
bellardde167e42005-04-28 21:15:08 +0000653 return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
654}
655
656static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
657{
bellarda0464332005-12-18 18:29:50 +0000658 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200659 uint32_t* entry=array_get(&(s->fat),cluster);
660 *entry=cpu_to_le32(value);
bellardde167e42005-04-28 21:15:08 +0000661 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200662 uint16_t* entry=array_get(&(s->fat),cluster);
663 *entry=cpu_to_le16(value&0xffff);
bellardde167e42005-04-28 21:15:08 +0000664 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200665 int offset = (cluster*3/2);
666 unsigned char* p = array_get(&(s->fat), offset);
bellarda0464332005-12-18 18:29:50 +0000667 switch (cluster&1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200668 case 0:
669 p[0] = value&0xff;
670 p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
671 break;
672 case 1:
673 p[0] = (p[0]&0xf) | ((value&0xf)<<4);
674 p[1] = (value>>4);
675 break;
676 }
bellardde167e42005-04-28 21:15:08 +0000677 }
678}
679
680static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
681{
bellarda0464332005-12-18 18:29:50 +0000682 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200683 uint32_t* entry=array_get(&(s->fat),cluster);
684 return le32_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000685 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200686 uint16_t* entry=array_get(&(s->fat),cluster);
687 return le16_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000688 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200689 const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
690 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
bellardde167e42005-04-28 21:15:08 +0000691 }
692}
693
694static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
695{
696 if(fat_entry>s->max_fat_value-8)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200697 return -1;
bellardde167e42005-04-28 21:15:08 +0000698 return 0;
699}
700
701static inline void init_fat(BDRVVVFATState* s)
702{
bellarda0464332005-12-18 18:29:50 +0000703 if (s->fat_type == 12) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200704 array_init(&(s->fat),1);
705 array_ensure_allocated(&(s->fat),
706 s->sectors_per_fat * 0x200 * 3 / 2 - 1);
bellarda0464332005-12-18 18:29:50 +0000707 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200708 array_init(&(s->fat),(s->fat_type==32?4:2));
709 array_ensure_allocated(&(s->fat),
710 s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
bellarda0464332005-12-18 18:29:50 +0000711 }
bellardde167e42005-04-28 21:15:08 +0000712 memset(s->fat.pointer,0,s->fat.size);
ths3b46e622007-09-17 08:09:54 +0000713
bellardde167e42005-04-28 21:15:08 +0000714 switch(s->fat_type) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200715 case 12: s->max_fat_value=0xfff; break;
716 case 16: s->max_fat_value=0xffff; break;
717 case 32: s->max_fat_value=0x0fffffff; break;
718 default: s->max_fat_value=0; /* error... */
bellardde167e42005-04-28 21:15:08 +0000719 }
720
721}
722
Anthony Liguoric227f092009-10-01 16:12:16 -0500723static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200724 unsigned int directory_start, const char* filename, int is_dot)
bellardde167e42005-04-28 21:15:08 +0000725{
Hervé Poussineau0c361112017-05-22 23:12:01 +0200726 int long_index = s->directory.next;
Anthony Liguoric227f092009-10-01 16:12:16 -0500727 direntry_t* entry = NULL;
728 direntry_t* entry_long = NULL;
bellardde167e42005-04-28 21:15:08 +0000729
730 if(is_dot) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200731 entry=array_get_next(&(s->directory));
Stefan Weilf671d172013-12-11 21:37:11 +0100732 memset(entry->name, 0x20, sizeof(entry->name));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200733 memcpy(entry->name,filename,strlen(filename));
734 return entry;
bellardde167e42005-04-28 21:15:08 +0000735 }
ths3b46e622007-09-17 08:09:54 +0000736
bellardde167e42005-04-28 21:15:08 +0000737 entry_long=create_long_filename(s,filename);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200738 entry = create_short_filename(s, filename, directory_start);
bellardde167e42005-04-28 21:15:08 +0000739
740 /* calculate checksum; propagate to long name */
741 if(entry_long) {
742 uint8_t chksum=fat_chksum(entry);
743
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200744 /* calculate anew, because realloc could have taken place */
745 entry_long=array_get(&(s->directory),long_index);
746 while(entry_long<entry && is_long_name(entry_long)) {
747 entry_long->reserved[1]=chksum;
748 entry_long++;
749 }
bellardde167e42005-04-28 21:15:08 +0000750 }
751
752 return entry;
753}
754
bellarda0464332005-12-18 18:29:50 +0000755/*
756 * Read a directory. (the index of the corresponding mapping must be passed).
757 */
758static int read_directory(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +0000759{
Anthony Liguoric227f092009-10-01 16:12:16 -0500760 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
761 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000762 const char* dirname = mapping->path;
763 int first_cluster = mapping->begin;
764 int parent_index = mapping->info.dir.parent_mapping_index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500765 mapping_t* parent_mapping = (mapping_t*)
blueswir1511d2b12009-03-07 15:32:56 +0000766 (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
bellarda0464332005-12-18 18:29:50 +0000767 int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
bellardde167e42005-04-28 21:15:08 +0000768
769 DIR* dir=opendir(dirname);
770 struct dirent* entry;
bellardde167e42005-04-28 21:15:08 +0000771 int i;
772
bellarda0464332005-12-18 18:29:50 +0000773 assert(mapping->mode & MODE_DIRECTORY);
774
775 if(!dir) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200776 mapping->end = mapping->begin;
777 return -1;
bellarda0464332005-12-18 18:29:50 +0000778 }
ths3b46e622007-09-17 08:09:54 +0000779
bellarda0464332005-12-18 18:29:50 +0000780 i = mapping->info.dir.first_dir_index =
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200781 first_cluster == 0 ? 0 : s->directory.next;
bellarda0464332005-12-18 18:29:50 +0000782
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200783 if (first_cluster != 0) {
784 /* create the top entries of a subdirectory */
785 (void)create_short_and_long_name(s, i, ".", 1);
786 (void)create_short_and_long_name(s, i, "..", 1);
787 }
788
ths5fafdf22007-09-16 21:08:06 +0000789 /* actually read the directory, and allocate the mappings */
bellardde167e42005-04-28 21:15:08 +0000790 while((entry=readdir(dir))) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200791 unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000792 char* buffer;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200793 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000794 struct stat st;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200795 int is_dot=!strcmp(entry->d_name,".");
796 int is_dotdot=!strcmp(entry->d_name,"..");
bellardde167e42005-04-28 21:15:08 +0000797
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200798 if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
799 fprintf(stderr, "Too many entries in root directory\n");
800 closedir(dir);
801 return -2;
802 }
803
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200804 if(first_cluster == 0 && (is_dotdot || is_dot))
805 continue;
ths5fafdf22007-09-16 21:08:06 +0000806
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200807 buffer = g_malloc(length);
808 snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000809
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200810 if(stat(buffer,&st)<0) {
Stefan Weilce137822011-09-30 23:29:53 +0200811 g_free(buffer);
bellardde167e42005-04-28 21:15:08 +0000812 continue;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200813 }
bellardde167e42005-04-28 21:15:08 +0000814
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200815 /* create directory entry for this file */
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200816 if (!is_dot && !is_dotdot) {
817 direntry = create_short_and_long_name(s, i, entry->d_name, 0);
818 } else {
819 direntry = array_get(&(s->directory), is_dot ? i : i + 1);
820 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200821 direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
822 direntry->reserved[0]=direntry->reserved[1]=0;
823 direntry->ctime=fat_datetime(st.st_ctime,1);
824 direntry->cdate=fat_datetime(st.st_ctime,0);
825 direntry->adate=fat_datetime(st.st_atime,0);
826 direntry->begin_hi=0;
827 direntry->mtime=fat_datetime(st.st_mtime,1);
828 direntry->mdate=fat_datetime(st.st_mtime,0);
829 if(is_dotdot)
830 set_begin_of_direntry(direntry, first_cluster_of_parent);
831 else if(is_dot)
832 set_begin_of_direntry(direntry, first_cluster);
833 else
834 direntry->begin=0; /* do that later */
bellarda0464332005-12-18 18:29:50 +0000835 if (st.st_size > 0x7fffffff) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200836 fprintf(stderr, "File %s is larger than 2GB\n", buffer);
Stefan Weilce137822011-09-30 23:29:53 +0200837 g_free(buffer);
Blue Swirl08089ed2011-01-12 19:48:58 +0000838 closedir(dir);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200839 return -2;
bellarda0464332005-12-18 18:29:50 +0000840 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200841 direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
bellardde167e42005-04-28 21:15:08 +0000842
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200843 /* create mapping for this file */
844 if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
845 s->current_mapping = array_get_next(&(s->mapping));
846 s->current_mapping->begin=0;
847 s->current_mapping->end=st.st_size;
848 /*
849 * we get the direntry of the most recent direntry, which
850 * contains the short name and all the relevant information.
851 */
852 s->current_mapping->dir_index=s->directory.next-1;
853 s->current_mapping->first_mapping_index = -1;
854 if (S_ISDIR(st.st_mode)) {
855 s->current_mapping->mode = MODE_DIRECTORY;
856 s->current_mapping->info.dir.parent_mapping_index =
857 mapping_index;
858 } else {
859 s->current_mapping->mode = MODE_UNDEFINED;
860 s->current_mapping->info.file.offset = 0;
861 }
862 s->current_mapping->path=buffer;
863 s->current_mapping->read_only =
864 (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
Markus Armbrusterb122c3b2014-05-28 11:17:05 +0200865 } else {
866 g_free(buffer);
867 }
bellardde167e42005-04-28 21:15:08 +0000868 }
869 closedir(dir);
870
871 /* fill with zeroes up to the end of the cluster */
872 while(s->directory.next%(0x10*s->sectors_per_cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200873 direntry_t* direntry=array_get_next(&(s->directory));
874 memset(direntry,0,sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000875 }
876
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200877 if (s->fat_type != 32 &&
878 mapping_index == 0 &&
879 s->directory.next < s->root_entries) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200880 /* root directory */
881 int cur = s->directory.next;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200882 array_ensure_allocated(&(s->directory), s->root_entries - 1);
883 s->directory.next = s->root_entries;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200884 memset(array_get(&(s->directory), cur), 0,
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200885 (s->root_entries - cur) * sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000886 }
ths5fafdf22007-09-16 21:08:06 +0000887
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200888 /* re-get the mapping, since s->mapping was possibly realloc()ed */
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200889 mapping = array_get(&(s->mapping), mapping_index);
bellarda0464332005-12-18 18:29:50 +0000890 first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200891 * 0x20 / s->cluster_size;
bellarda0464332005-12-18 18:29:50 +0000892 mapping->end = first_cluster;
bellardde167e42005-04-28 21:15:08 +0000893
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200894 direntry = array_get(&(s->directory), mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +0000895 set_begin_of_direntry(direntry, mapping->begin);
ths3b46e622007-09-17 08:09:54 +0000896
bellardde167e42005-04-28 21:15:08 +0000897 return 0;
898}
899
bellarda0464332005-12-18 18:29:50 +0000900static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
bellardde167e42005-04-28 21:15:08 +0000901{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200902 return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000903}
904
905static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
906{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200907 return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
bellarda0464332005-12-18 18:29:50 +0000908}
909
bellarda0464332005-12-18 18:29:50 +0000910static int init_directories(BDRVVVFATState* s,
Markus Armbrusterd11c8912014-05-16 11:00:18 +0200911 const char *dirname, int heads, int secs,
912 Error **errp)
bellarda0464332005-12-18 18:29:50 +0000913{
Anthony Liguoric227f092009-10-01 16:12:16 -0500914 bootsector_t* bootsector;
915 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +0000916 unsigned int i;
917 unsigned int cluster;
918
919 memset(&(s->first_sectors[0]),0,0x40*0x200);
920
bellardde167e42005-04-28 21:15:08 +0000921 s->cluster_size=s->sectors_per_cluster*0x200;
Anthony Liguori7267c092011-08-20 22:09:37 -0500922 s->cluster_buffer=g_malloc(s->cluster_size);
bellarda0464332005-12-18 18:29:50 +0000923
924 /*
925 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
926 * where sc is sector_count,
927 * spf is sectors_per_fat,
928 * spc is sectors_per_clusters, and
929 * fat_type = 12, 16 or 32.
930 */
931 i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
932 s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
ths3b46e622007-09-17 08:09:54 +0000933
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200934 s->offset_to_fat = s->offset_to_bootsector + 1;
935 s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
936
Anthony Liguoric227f092009-10-01 16:12:16 -0500937 array_init(&(s->mapping),sizeof(mapping_t));
938 array_init(&(s->directory),sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000939
940 /* add volume label */
941 {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200942 direntry_t* entry=array_get_next(&(s->directory));
943 entry->attributes=0x28; /* archive | volume label */
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200944 memcpy(entry->name, s->volume_label, sizeof(entry->name));
bellardde167e42005-04-28 21:15:08 +0000945 }
946
bellardde167e42005-04-28 21:15:08 +0000947 /* Now build FAT, and write back information into directory */
948 init_fat(s);
949
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200950 /* TODO: if there are more entries, bootsector has to be adjusted! */
951 s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000952 s->cluster_count=sector2cluster(s, s->sector_count);
bellardde167e42005-04-28 21:15:08 +0000953
bellarda0464332005-12-18 18:29:50 +0000954 mapping = array_get_next(&(s->mapping));
955 mapping->begin = 0;
956 mapping->dir_index = 0;
957 mapping->info.dir.parent_mapping_index = -1;
958 mapping->first_mapping_index = -1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500959 mapping->path = g_strdup(dirname);
bellarda0464332005-12-18 18:29:50 +0000960 i = strlen(mapping->path);
961 if (i > 0 && mapping->path[i - 1] == '/')
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200962 mapping->path[i - 1] = '\0';
bellarda0464332005-12-18 18:29:50 +0000963 mapping->mode = MODE_DIRECTORY;
964 mapping->read_only = 0;
965 s->path = mapping->path;
bellardde167e42005-04-28 21:15:08 +0000966
bellarda0464332005-12-18 18:29:50 +0000967 for (i = 0, cluster = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200968 /* MS-DOS expects the FAT to be 0 for the root directory
969 * (except for the media byte). */
970 /* LATER TODO: still true for FAT32? */
971 int fix_fat = (i != 0);
972 mapping = array_get(&(s->mapping), i);
bellardde167e42005-04-28 21:15:08 +0000973
bellarda0464332005-12-18 18:29:50 +0000974 if (mapping->mode & MODE_DIRECTORY) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200975 mapping->begin = cluster;
976 if(read_directory(s, i)) {
Markus Armbrusterd11c8912014-05-16 11:00:18 +0200977 error_setg(errp, "Could not read directory %s",
978 mapping->path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200979 return -1;
980 }
981 mapping = array_get(&(s->mapping), i);
982 } else {
983 assert(mapping->mode == MODE_UNDEFINED);
984 mapping->mode=MODE_NORMAL;
985 mapping->begin = cluster;
986 if (mapping->end > 0) {
987 direntry_t* direntry = array_get(&(s->directory),
988 mapping->dir_index);
bellardde167e42005-04-28 21:15:08 +0000989
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200990 mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
991 set_begin_of_direntry(direntry, mapping->begin);
992 } else {
993 mapping->end = cluster + 1;
994 fix_fat = 0;
995 }
996 }
bellarda0464332005-12-18 18:29:50 +0000997
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200998 assert(mapping->begin < mapping->end);
bellarda0464332005-12-18 18:29:50 +0000999
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001000 /* next free cluster */
1001 cluster = mapping->end;
bellarda0464332005-12-18 18:29:50 +00001002
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001003 if(cluster > s->cluster_count) {
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001004 error_setg(errp,
1005 "Directory does not fit in FAT%d (capacity %.2f MB)",
1006 s->fat_type, s->sector_count / 2000.0);
1007 return -1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001008 }
balrog8ce0f862008-11-10 01:34:27 +00001009
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001010 /* fix fat for entry */
1011 if (fix_fat) {
1012 int j;
1013 for(j = mapping->begin; j < mapping->end - 1; j++)
1014 fat_set(s, j, j+1);
1015 fat_set(s, mapping->end - 1, s->max_fat_value);
1016 }
bellardde167e42005-04-28 21:15:08 +00001017 }
1018
bellarda0464332005-12-18 18:29:50 +00001019 mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00001020 s->last_cluster_of_root_directory = mapping->end;
bellardde167e42005-04-28 21:15:08 +00001021
bellarda0464332005-12-18 18:29:50 +00001022 /* the FAT signature */
1023 fat_set(s,0,s->max_fat_value);
1024 fat_set(s,1,s->max_fat_value);
1025
1026 s->current_mapping = NULL;
1027
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001028 bootsector = (bootsector_t *)(s->first_sectors
1029 + s->offset_to_bootsector * 0x200);
bellardde167e42005-04-28 21:15:08 +00001030 bootsector->jump[0]=0xeb;
1031 bootsector->jump[1]=0x3e;
1032 bootsector->jump[2]=0x90;
Hervé Poussineau63d261c2017-07-15 15:28:39 +02001033 memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
bellardde167e42005-04-28 21:15:08 +00001034 bootsector->sector_size=cpu_to_le16(0x200);
1035 bootsector->sectors_per_cluster=s->sectors_per_cluster;
1036 bootsector->reserved_sectors=cpu_to_le16(1);
1037 bootsector->number_of_fats=0x2; /* number of FATs */
Hervé Poussineau6817efe2017-05-22 23:12:03 +02001038 bootsector->root_entries = cpu_to_le16(s->root_entries);
bellarda0464332005-12-18 18:29:50 +00001039 bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001040 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1041 bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
bellarda0464332005-12-18 18:29:50 +00001042 s->fat.pointer[0] = bootsector->media_type;
bellardde167e42005-04-28 21:15:08 +00001043 bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001044 bootsector->sectors_per_track = cpu_to_le16(secs);
1045 bootsector->number_of_heads = cpu_to_le16(heads);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001046 bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
bellarda0464332005-12-18 18:29:50 +00001047 bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
bellardde167e42005-04-28 21:15:08 +00001048
bellarda0464332005-12-18 18:29:50 +00001049 /* LATER TODO: if FAT32, this is wrong */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001050 /* drive_number: fda=0, hda=0x80 */
1051 bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
bellardde167e42005-04-28 21:15:08 +00001052 bootsector->u.fat16.signature=0x29;
1053 bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
1054
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001055 memcpy(bootsector->u.fat16.volume_label, s->volume_label,
1056 sizeof(bootsector->u.fat16.volume_label));
Hervé Poussineau92e28d82017-05-22 23:11:58 +02001057 memcpy(bootsector->u.fat16.fat_type,
1058 s->fat_type == 12 ? "FAT12 " : "FAT16 ", 8);
bellardde167e42005-04-28 21:15:08 +00001059 bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
1060
1061 return 0;
1062}
1063
bellard83f64092006-08-01 16:21:11 +00001064#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001065static BDRVVVFATState *vvv = NULL;
bellard83f64092006-08-01 16:21:11 +00001066#endif
bellarda0464332005-12-18 18:29:50 +00001067
Kevin Wolfeecc7742016-05-30 17:13:09 +02001068static int enable_write_target(BlockDriverState *bs, Error **errp);
bellarda0464332005-12-18 18:29:50 +00001069static int is_consistent(BDRVVVFATState *s);
1070
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001071static QemuOptsList runtime_opts = {
1072 .name = "vvfat",
1073 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1074 .desc = {
1075 {
1076 .name = "dir",
1077 .type = QEMU_OPT_STRING,
1078 .help = "Host directory to map to the vvfat device",
1079 },
1080 {
1081 .name = "fat-type",
1082 .type = QEMU_OPT_NUMBER,
1083 .help = "FAT type (12, 16 or 32)",
1084 },
1085 {
1086 .name = "floppy",
1087 .type = QEMU_OPT_BOOL,
1088 .help = "Create a floppy rather than a hard disk image",
1089 },
1090 {
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001091 .name = "label",
1092 .type = QEMU_OPT_STRING,
1093 .help = "Use a volume label other than QEMU VVFAT",
1094 },
1095 {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001096 .name = "rw",
1097 .type = QEMU_OPT_BOOL,
1098 .help = "Make the image writable",
1099 },
1100 { /* end of list */ }
1101 },
1102};
1103
1104static void vvfat_parse_filename(const char *filename, QDict *options,
1105 Error **errp)
1106{
1107 int fat_type = 0;
1108 bool floppy = false;
1109 bool rw = false;
1110 int i;
1111
1112 if (!strstart(filename, "fat:", NULL)) {
1113 error_setg(errp, "File name string must start with 'fat:'");
1114 return;
1115 }
1116
1117 /* Parse options */
1118 if (strstr(filename, ":32:")) {
1119 fat_type = 32;
1120 } else if (strstr(filename, ":16:")) {
1121 fat_type = 16;
1122 } else if (strstr(filename, ":12:")) {
1123 fat_type = 12;
1124 }
1125
1126 if (strstr(filename, ":floppy:")) {
1127 floppy = true;
1128 }
1129
1130 if (strstr(filename, ":rw:")) {
1131 rw = true;
1132 }
1133
1134 /* Get the directory name without options */
1135 i = strrchr(filename, ':') - filename;
1136 assert(i >= 3);
1137 if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
1138 /* workaround for DOS drive names */
1139 filename += i - 1;
1140 } else {
1141 filename += i + 1;
1142 }
1143
1144 /* Fill in the options QDict */
Eric Blake46f5ac22017-04-27 16:58:17 -05001145 qdict_put_str(options, "dir", filename);
1146 qdict_put_int(options, "fat-type", fat_type);
1147 qdict_put_bool(options, "floppy", floppy);
1148 qdict_put_bool(options, "rw", rw);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001149}
1150
Max Reitz015a1032013-09-05 14:22:29 +02001151static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
1152 Error **errp)
bellardde167e42005-04-28 21:15:08 +00001153{
1154 BDRVVVFATState *s = bs->opaque;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001155 int cyls, heads, secs;
1156 bool floppy;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001157 const char *dirname, *label;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001158 QemuOpts *opts;
1159 Error *local_err = NULL;
1160 int ret;
bellardde167e42005-04-28 21:15:08 +00001161
bellard83f64092006-08-01 16:21:11 +00001162#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001163 vvv = s;
bellard83f64092006-08-01 16:21:11 +00001164#endif
bellarda0464332005-12-18 18:29:50 +00001165
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001166 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001167 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001168 if (local_err) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001169 error_propagate(errp, local_err);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001170 ret = -EINVAL;
1171 goto fail;
1172 }
1173
1174 dirname = qemu_opt_get(opts, "dir");
1175 if (!dirname) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001176 error_setg(errp, "vvfat block driver requires a 'dir' option");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001177 ret = -EINVAL;
1178 goto fail;
1179 }
1180
1181 s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
1182 floppy = qemu_opt_get_bool(opts, "floppy", false);
1183
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001184 memset(s->volume_label, ' ', sizeof(s->volume_label));
1185 label = qemu_opt_get(opts, "label");
1186 if (label) {
1187 size_t label_length = strlen(label);
1188 if (label_length > 11) {
1189 error_setg(errp, "vvfat label cannot be longer than 11 bytes");
1190 ret = -EINVAL;
1191 goto fail;
1192 }
1193 memcpy(s->volume_label, label, label_length);
Kevin Wolfd208c502016-04-27 14:18:16 +02001194 } else {
1195 memcpy(s->volume_label, "QEMU VVFAT", 10);
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001196 }
1197
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001198 if (floppy) {
1199 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1200 if (!s->fat_type) {
1201 s->fat_type = 12;
1202 secs = 36;
1203 s->sectors_per_cluster = 2;
1204 } else {
1205 secs = s->fat_type == 12 ? 18 : 36;
1206 s->sectors_per_cluster = 1;
1207 }
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001208 cyls = 80;
1209 heads = 2;
1210 } else {
1211 /* 32MB or 504MB disk*/
1212 if (!s->fat_type) {
1213 s->fat_type = 16;
1214 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001215 s->offset_to_bootsector = 0x3f;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001216 cyls = s->fat_type == 12 ? 64 : 1024;
1217 heads = 16;
1218 secs = 63;
1219 }
1220
1221 switch (s->fat_type) {
1222 case 32:
Alistair Francisb62e39b2017-09-11 12:52:56 -07001223 warn_report("FAT32 has not been tested. You are welcome to do so!");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001224 break;
1225 case 16:
1226 case 12:
1227 break;
1228 default:
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001229 error_setg(errp, "Valid FAT types are only 12, 16 and 32");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001230 ret = -EINVAL;
1231 goto fail;
1232 }
1233
1234
bellarda0464332005-12-18 18:29:50 +00001235 s->bs = bs;
1236
bellarda0464332005-12-18 18:29:50 +00001237 /* LATER TODO: if FAT32, adjust */
bellarda0464332005-12-18 18:29:50 +00001238 s->sectors_per_cluster=0x10;
bellardde167e42005-04-28 21:15:08 +00001239
1240 s->current_cluster=0xffffffff;
bellardde167e42005-04-28 21:15:08 +00001241
Kevin Wolfeecc7742016-05-30 17:13:09 +02001242 s->qcow = NULL;
bellarda0464332005-12-18 18:29:50 +00001243 s->qcow_filename = NULL;
1244 s->fat2 = NULL;
1245 s->downcase_short_names = 1;
ths3b46e622007-09-17 08:09:54 +00001246
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001247 fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
1248 dirname, cyls, heads, secs);
bellarda0464332005-12-18 18:29:50 +00001249
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001250 s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
Paolo Bonzini5a742b52011-10-05 09:12:06 +02001251
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001252 if (qemu_opt_get_bool(opts, "rw", false)) {
Jeff Codye2b82472017-04-07 16:55:26 -04001253 if (!bdrv_is_read_only(bs)) {
1254 ret = enable_write_target(bs, errp);
1255 if (ret < 0) {
1256 goto fail;
1257 }
1258 } else {
1259 ret = -EPERM;
1260 error_setg(errp,
1261 "Unable to set VVFAT to 'rw' when drive is read-only");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001262 goto fail;
1263 }
Kevin Wolf398e6ad2017-11-07 18:21:41 +01001264 } else if (!bdrv_is_read_only(bs)) {
1265 error_report("Opening non-rw vvfat images without an explicit "
1266 "read-only=on option is deprecated. Future versions "
1267 "will refuse to open the image instead of "
1268 "automatically marking the image read-only.");
Jeff Codye2b82472017-04-07 16:55:26 -04001269 /* read only is the default for safety */
1270 ret = bdrv_set_read_only(bs, true, &local_err);
1271 if (ret < 0) {
1272 error_propagate(errp, local_err);
1273 goto fail;
1274 }
thsb5700942007-09-25 14:47:03 +00001275 }
1276
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001277 bs->total_sectors = cyls * heads * secs;
thsb5700942007-09-25 14:47:03 +00001278
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001279 if (init_directories(s, dirname, heads, secs, errp)) {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001280 ret = -EIO;
1281 goto fail;
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001282 }
bellardde167e42005-04-28 21:15:08 +00001283
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001284 s->sector_count = s->offset_to_root_dir
1285 + s->sectors_per_cluster * s->cluster_count;
thsb5700942007-09-25 14:47:03 +00001286
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001287 /* Disable migration when vvfat is used rw */
1288 if (s->qcow) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001289 error_setg(&s->migration_blocker,
1290 "The vvfat (rw) format used by node '%s' "
1291 "does not support live migration",
1292 bdrv_get_device_or_node_name(bs));
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301293 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1294 if (local_err) {
1295 error_propagate(errp, local_err);
1296 error_free(s->migration_blocker);
1297 goto fail;
1298 }
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001299 }
1300
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001301 if (s->offset_to_bootsector > 0) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301302 init_mbr(s, cyls, heads, secs);
1303 }
1304
1305 qemu_co_mutex_init(&s->lock);
1306
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001307 ret = 0;
1308fail:
1309 qemu_opts_del(opts);
1310 return ret;
bellardde167e42005-04-28 21:15:08 +00001311}
1312
Eric Blakea6506482016-06-23 16:37:17 -06001313static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
1314{
Eric Blakea5b8dd22016-06-23 16:37:24 -06001315 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
Eric Blakea6506482016-06-23 16:37:17 -06001316}
1317
bellardde167e42005-04-28 21:15:08 +00001318static inline void vvfat_close_current_file(BDRVVVFATState *s)
1319{
1320 if(s->current_mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001321 s->current_mapping = NULL;
1322 if (s->current_fd) {
1323 qemu_close(s->current_fd);
1324 s->current_fd = 0;
1325 }
bellardde167e42005-04-28 21:15:08 +00001326 }
bellarda0464332005-12-18 18:29:50 +00001327 s->current_cluster = -1;
bellardde167e42005-04-28 21:15:08 +00001328}
1329
1330/* mappings between index1 and index2-1 are supposed to be ordered
1331 * return value is the index of the last mapping for which end>cluster_num
1332 */
1333static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
1334{
bellardde167e42005-04-28 21:15:08 +00001335 while(1) {
Blue Swirl88bf7952010-04-25 15:27:14 +00001336 int index3;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001337 mapping_t* mapping;
1338 index3=(index1+index2)/2;
1339 mapping=array_get(&(s->mapping),index3);
1340 assert(mapping->begin < mapping->end);
1341 if(mapping->begin>=cluster_num) {
1342 assert(index2!=index3 || index2==0);
1343 if(index2==index3)
1344 return index1;
1345 index2=index3;
1346 } else {
1347 if(index1==index3)
1348 return mapping->end<=cluster_num ? index2 : index1;
1349 index1=index3;
1350 }
1351 assert(index1<=index2);
1352 DLOG(mapping=array_get(&(s->mapping),index1);
1353 assert(mapping->begin<=cluster_num);
1354 assert(index2 >= s->mapping.next ||
1355 ((mapping = array_get(&(s->mapping),index2)) &&
1356 mapping->end>cluster_num)));
bellardde167e42005-04-28 21:15:08 +00001357 }
1358}
1359
Anthony Liguoric227f092009-10-01 16:12:16 -05001360static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
bellardde167e42005-04-28 21:15:08 +00001361{
1362 int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05001363 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +00001364 if(index>=s->mapping.next)
blueswir1511d2b12009-03-07 15:32:56 +00001365 return NULL;
bellardde167e42005-04-28 21:15:08 +00001366 mapping=array_get(&(s->mapping),index);
1367 if(mapping->begin>cluster_num)
blueswir1511d2b12009-03-07 15:32:56 +00001368 return NULL;
bellarda0464332005-12-18 18:29:50 +00001369 assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
bellardde167e42005-04-28 21:15:08 +00001370 return mapping;
1371}
1372
Anthony Liguoric227f092009-10-01 16:12:16 -05001373static int open_file(BDRVVVFATState* s,mapping_t* mapping)
bellardde167e42005-04-28 21:15:08 +00001374{
1375 if(!mapping)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001376 return -1;
bellardde167e42005-04-28 21:15:08 +00001377 if(!s->current_mapping ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001378 strcmp(s->current_mapping->path,mapping->path)) {
1379 /* open file */
1380 int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
1381 if(fd<0)
1382 return -1;
1383 vvfat_close_current_file(s);
1384 s->current_fd = fd;
1385 s->current_mapping = mapping;
bellardde167e42005-04-28 21:15:08 +00001386 }
1387 return 0;
1388}
1389
1390static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
1391{
1392 if(s->current_cluster != cluster_num) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001393 int result=0;
1394 off_t offset;
1395 assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
1396 if(!s->current_mapping
1397 || s->current_mapping->begin>cluster_num
1398 || s->current_mapping->end<=cluster_num) {
1399 /* binary search of mappings for file */
1400 mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
bellardde167e42005-04-28 21:15:08 +00001401
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001402 assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
bellarda0464332005-12-18 18:29:50 +00001403
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001404 if (mapping && mapping->mode & MODE_DIRECTORY) {
1405 vvfat_close_current_file(s);
1406 s->current_mapping = mapping;
bellarda0464332005-12-18 18:29:50 +00001407read_cluster_directory:
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001408 offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
1409 s->cluster = (unsigned char*)s->directory.pointer+offset
1410 + 0x20*s->current_mapping->info.dir.first_dir_index;
1411 assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
1412 assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
1413 s->current_cluster = cluster_num;
1414 return 0;
1415 }
bellarda0464332005-12-18 18:29:50 +00001416
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001417 if(open_file(s,mapping))
1418 return -2;
1419 } else if (s->current_mapping->mode & MODE_DIRECTORY)
1420 goto read_cluster_directory;
bellarda0464332005-12-18 18:29:50 +00001421
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001422 assert(s->current_fd);
bellarda0464332005-12-18 18:29:50 +00001423
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001424 offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
1425 if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
1426 return -3;
1427 s->cluster=s->cluster_buffer;
1428 result=read(s->current_fd,s->cluster,s->cluster_size);
1429 if(result<0) {
1430 s->current_cluster = -1;
1431 return -1;
1432 }
1433 s->current_cluster = cluster_num;
bellardde167e42005-04-28 21:15:08 +00001434 }
1435 return 0;
1436}
1437
bellarda0464332005-12-18 18:29:50 +00001438#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -05001439static void print_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001440{
1441 int j = 0;
1442 char buffer[1024];
1443
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001444 fprintf(stderr, "direntry %p: ", direntry);
bellarda0464332005-12-18 18:29:50 +00001445 if(!direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001446 return;
bellarda0464332005-12-18 18:29:50 +00001447 if(is_long_name(direntry)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001448 unsigned char* c=(unsigned char*)direntry;
1449 int i;
1450 for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
blueswir13891b372008-12-14 09:30:41 +00001451#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001452 ADD_CHAR(c[i]);
1453 for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
1454 ADD_CHAR(c[i]);
1455 for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
1456 ADD_CHAR(c[i]);
1457 buffer[j] = 0;
1458 fprintf(stderr, "%s\n", buffer);
bellarda0464332005-12-18 18:29:50 +00001459 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001460 int i;
1461 for(i=0;i<11;i++)
1462 ADD_CHAR(direntry->name[i]);
1463 buffer[j] = 0;
1464 fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
1465 buffer,
1466 direntry->attributes,
1467 begin_of_direntry(direntry),le32_to_cpu(direntry->size));
bellarda0464332005-12-18 18:29:50 +00001468 }
1469}
1470
Anthony Liguoric227f092009-10-01 16:12:16 -05001471static void print_mapping(const mapping_t* mapping)
bellarda0464332005-12-18 18:29:50 +00001472{
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001473 fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1474 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1475 mapping, mapping->begin, mapping->end, mapping->dir_index,
1476 mapping->first_mapping_index, mapping->path, mapping->mode);
1477
bellarda0464332005-12-18 18:29:50 +00001478 if (mapping->mode & MODE_DIRECTORY)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001479 fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index);
bellarda0464332005-12-18 18:29:50 +00001480 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001481 fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
bellarda0464332005-12-18 18:29:50 +00001482}
1483#endif
1484
ths5fafdf22007-09-16 21:08:06 +00001485static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00001486 uint8_t *buf, int nb_sectors)
1487{
1488 BDRVVVFATState *s = bs->opaque;
1489 int i;
1490
bellardde167e42005-04-28 21:15:08 +00001491 for(i=0;i<nb_sectors;i++,sector_num++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001492 if (sector_num >= bs->total_sectors)
1493 return -1;
1494 if (s->qcow) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001495 int64_t n;
Eric Blake6f712ee2017-03-08 15:34:28 -06001496 int ret;
Eric Blaked6a644b2017-07-07 07:44:57 -05001497 ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
1498 (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
Eric Blake6f712ee2017-03-08 15:34:28 -06001499 if (ret < 0) {
1500 return ret;
1501 }
1502 if (ret) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001503 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
1504 " allocated\n", sector_num,
1505 n >> BDRV_SECTOR_BITS));
1506 if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
1507 n >> BDRV_SECTOR_BITS)) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001508 return -1;
1509 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001510 i += (n >> BDRV_SECTOR_BITS) - 1;
1511 sector_num += (n >> BDRV_SECTOR_BITS) - 1;
Kevin Wolf7704df92011-11-08 10:50:12 +01001512 continue;
1513 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001514 DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
1515 sector_num));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001516 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001517 if (sector_num < s->offset_to_root_dir) {
1518 if (sector_num < s->offset_to_fat) {
1519 memcpy(buf + i * 0x200,
1520 &(s->first_sectors[sector_num * 0x200]),
1521 0x200);
1522 } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
1523 memcpy(buf + i * 0x200,
1524 &(s->fat.pointer[(sector_num
1525 - s->offset_to_fat) * 0x200]),
1526 0x200);
1527 } else if (sector_num < s->offset_to_root_dir) {
1528 memcpy(buf + i * 0x200,
1529 &(s->fat.pointer[(sector_num - s->offset_to_fat
1530 - s->sectors_per_fat) * 0x200]),
1531 0x200);
1532 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001533 } else {
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001534 uint32_t sector = sector_num - s->offset_to_root_dir,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001535 sector_offset_in_cluster=(sector%s->sectors_per_cluster),
1536 cluster_num=sector/s->sectors_per_cluster;
1537 if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
1538 /* LATER TODO: strict: return -1; */
1539 memset(buf+i*0x200,0,0x200);
1540 continue;
1541 }
1542 memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
1543 }
bellarda0464332005-12-18 18:29:50 +00001544 }
1545 return 0;
1546}
1547
Kevin Wolf4575eb42016-04-26 17:14:08 +02001548static int coroutine_fn
1549vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1550 QEMUIOVector *qiov, int flags)
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001551{
1552 int ret;
1553 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02001554 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
1555 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1556 void *buf;
1557
1558 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1559 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1560
1561 buf = g_try_malloc(bytes);
1562 if (bytes && buf == NULL) {
1563 return -ENOMEM;
1564 }
1565
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001566 qemu_co_mutex_lock(&s->lock);
1567 ret = vvfat_read(bs, sector_num, buf, nb_sectors);
1568 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02001569
1570 qemu_iovec_from_buf(qiov, 0, buf, bytes);
1571 g_free(buf);
1572
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001573 return ret;
1574}
1575
bellarda0464332005-12-18 18:29:50 +00001576/* LATER TODO: statify all functions */
1577
1578/*
1579 * Idea of the write support (use snapshot):
1580 *
1581 * 1. check if all data is consistent, recording renames, modifications,
1582 * new files and directories (in s->commits).
1583 *
1584 * 2. if the data is not consistent, stop committing
1585 *
1586 * 3. handle renames, and create new files and directories (do not yet
1587 * write their contents)
1588 *
1589 * 4. walk the directories, fixing the mapping and direntries, and marking
1590 * the handled mappings as not deleted
1591 *
1592 * 5. commit the contents of the files
1593 *
1594 * 6. handle deleted files and directories
1595 *
1596 */
1597
Anthony Liguoric227f092009-10-01 16:12:16 -05001598typedef struct commit_t {
bellarda0464332005-12-18 18:29:50 +00001599 char* path;
1600 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001601 struct { uint32_t cluster; } rename;
1602 struct { int dir_index; uint32_t modified_offset; } writeout;
1603 struct { uint32_t first_cluster; } new_file;
1604 struct { uint32_t cluster; } mkdir;
bellarda0464332005-12-18 18:29:50 +00001605 } param;
1606 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1607 enum {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001608 ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
bellarda0464332005-12-18 18:29:50 +00001609 } action;
Anthony Liguoric227f092009-10-01 16:12:16 -05001610} commit_t;
bellarda0464332005-12-18 18:29:50 +00001611
1612static void clear_commits(BDRVVVFATState* s)
1613{
1614 int i;
1615DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
1616 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001617 commit_t* commit = array_get(&(s->commits), i);
1618 assert(commit->path || commit->action == ACTION_WRITEOUT);
1619 if (commit->action != ACTION_WRITEOUT) {
1620 assert(commit->path);
Stefan Weilce137822011-09-30 23:29:53 +02001621 g_free(commit->path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001622 } else
1623 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00001624 }
1625 s->commits.next = 0;
1626}
1627
1628static void schedule_rename(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001629 uint32_t cluster, char* new_path)
bellarda0464332005-12-18 18:29:50 +00001630{
Anthony Liguoric227f092009-10-01 16:12:16 -05001631 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001632 commit->path = new_path;
1633 commit->param.rename.cluster = cluster;
1634 commit->action = ACTION_RENAME;
1635}
1636
1637static void schedule_writeout(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001638 int dir_index, uint32_t modified_offset)
bellarda0464332005-12-18 18:29:50 +00001639{
Anthony Liguoric227f092009-10-01 16:12:16 -05001640 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001641 commit->path = NULL;
1642 commit->param.writeout.dir_index = dir_index;
1643 commit->param.writeout.modified_offset = modified_offset;
1644 commit->action = ACTION_WRITEOUT;
1645}
1646
1647static void schedule_new_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001648 char* path, uint32_t first_cluster)
bellarda0464332005-12-18 18:29:50 +00001649{
Anthony Liguoric227f092009-10-01 16:12:16 -05001650 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001651 commit->path = path;
1652 commit->param.new_file.first_cluster = first_cluster;
1653 commit->action = ACTION_NEW_FILE;
1654}
1655
1656static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
1657{
Anthony Liguoric227f092009-10-01 16:12:16 -05001658 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001659 commit->path = path;
1660 commit->param.mkdir.cluster = cluster;
1661 commit->action = ACTION_MKDIR;
1662}
1663
1664typedef struct {
ths64eaabd2008-07-03 19:54:19 +00001665 /*
1666 * Since the sequence number is at most 0x3f, and the filename
1667 * length is at most 13 times the sequence number, the maximal
1668 * filename length is 0x3f * 13 bytes.
1669 */
1670 unsigned char name[0x3f * 13 + 1];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001671 gunichar2 name2[0x3f * 13 + 1];
bellarda0464332005-12-18 18:29:50 +00001672 int checksum, len;
1673 int sequence_number;
1674} long_file_name;
1675
1676static void lfn_init(long_file_name* lfn)
1677{
1678 lfn->sequence_number = lfn->len = 0;
1679 lfn->checksum = 0x100;
1680}
1681
1682/* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1683static int parse_long_name(long_file_name* lfn,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001684 const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001685{
1686 int i, j, offset;
1687 const unsigned char* pointer = (const unsigned char*)direntry;
1688
1689 if (!is_long_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001690 return 1;
bellarda0464332005-12-18 18:29:50 +00001691
1692 if (pointer[0] & 0x40) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001693 /* first entry; do some initialization */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001694 lfn->sequence_number = pointer[0] & 0x3f;
1695 lfn->checksum = pointer[13];
1696 lfn->name[0] = 0;
1697 lfn->name[lfn->sequence_number * 13] = 0;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001698 } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
1699 /* not the expected sequence number */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001700 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001701 } else if (pointer[13] != lfn->checksum) {
1702 /* not the expected checksum */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001703 return -2;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001704 } else if (pointer[12] || pointer[26] || pointer[27]) {
1705 /* invalid zero fields */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001706 return -3;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001707 }
bellarda0464332005-12-18 18:29:50 +00001708
1709 offset = 13 * (lfn->sequence_number - 1);
1710 for (i = 0, j = 1; i < 13; i++, j+=2) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001711 if (j == 11)
1712 j = 14;
1713 else if (j == 26)
1714 j = 28;
bellarda0464332005-12-18 18:29:50 +00001715
Hervé Poussineaue03da262017-07-15 15:28:40 +02001716 if (pointer[j] == 0 && pointer[j + 1] == 0) {
1717 /* end of long file name */
1718 break;
1719 }
1720 gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
1721 lfn->name2[offset + i] = c;
bellarda0464332005-12-18 18:29:50 +00001722 }
1723
Hervé Poussineaue03da262017-07-15 15:28:40 +02001724 if (pointer[0] & 0x40) {
1725 /* first entry; set len */
1726 lfn->len = offset + i;
1727 }
1728 if ((pointer[0] & 0x3f) == 0x01) {
1729 /* last entry; finalize entry */
1730 glong olen;
1731 gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
1732 if (!utf8) {
1733 return -4;
1734 }
1735 lfn->len = olen;
1736 memcpy(lfn->name, utf8, olen + 1);
1737 g_free(utf8);
1738 }
bellarda0464332005-12-18 18:29:50 +00001739
1740 return 0;
1741}
1742
1743/* returns 0 if successful, >0 if no short_name, and <0 on error */
1744static int parse_short_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001745 long_file_name* lfn, direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001746{
1747 int i, j;
1748
1749 if (!is_short_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001750 return 1;
bellarda0464332005-12-18 18:29:50 +00001751
1752 for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
1753 for (i = 0; i <= j; i++) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001754 uint8_t c = direntry->name[i];
1755 if (c != to_valid_short_char(c)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001756 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001757 } else if (s->downcase_short_names) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001758 lfn->name[i] = qemu_tolower(direntry->name[i]);
Hervé Poussineaue03da262017-07-15 15:28:40 +02001759 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001760 lfn->name[i] = direntry->name[i];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001761 }
bellarda0464332005-12-18 18:29:50 +00001762 }
1763
Stefan Weilf671d172013-12-11 21:37:11 +01001764 for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
1765 }
bellarda0464332005-12-18 18:29:50 +00001766 if (j >= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001767 lfn->name[i++] = '.';
1768 lfn->name[i + j + 1] = '\0';
1769 for (;j >= 0; j--) {
Stefan Weilf671d172013-12-11 21:37:11 +01001770 uint8_t c = direntry->name[8 + j];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001771 if (c != to_valid_short_char(c)) {
Stefan Weilf671d172013-12-11 21:37:11 +01001772 return -2;
1773 } else if (s->downcase_short_names) {
1774 lfn->name[i + j] = qemu_tolower(c);
1775 } else {
1776 lfn->name[i + j] = c;
1777 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001778 }
bellarda0464332005-12-18 18:29:50 +00001779 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001780 lfn->name[i + j + 1] = '\0';
bellarda0464332005-12-18 18:29:50 +00001781
Hervé Poussineau8c4517f2017-07-15 15:28:38 +02001782 if (lfn->name[0] == DIR_KANJI_FAKE) {
1783 lfn->name[0] = DIR_KANJI;
Hervé Poussineau78f002c2017-05-22 23:12:04 +02001784 }
thsffe8ab82007-12-16 03:16:05 +00001785 lfn->len = strlen((char*)lfn->name);
bellarda0464332005-12-18 18:29:50 +00001786
1787 return 0;
1788}
1789
1790static inline uint32_t modified_fat_get(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001791 unsigned int cluster)
bellarda0464332005-12-18 18:29:50 +00001792{
1793 if (cluster < s->last_cluster_of_root_directory) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001794 if (cluster + 1 == s->last_cluster_of_root_directory)
1795 return s->max_fat_value;
1796 else
1797 return cluster + 1;
bellarda0464332005-12-18 18:29:50 +00001798 }
1799
1800 if (s->fat_type==32) {
1801 uint32_t* entry=((uint32_t*)s->fat2)+cluster;
1802 return le32_to_cpu(*entry);
1803 } else if (s->fat_type==16) {
1804 uint16_t* entry=((uint16_t*)s->fat2)+cluster;
1805 return le16_to_cpu(*entry);
1806 } else {
1807 const uint8_t* x=s->fat2+cluster*3/2;
1808 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
1809 }
1810}
1811
Eric Blake6f712ee2017-03-08 15:34:28 -06001812static inline bool cluster_was_modified(BDRVVVFATState *s,
1813 uint32_t cluster_num)
bellarda0464332005-12-18 18:29:50 +00001814{
1815 int was_modified = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001816 int i;
bellarda0464332005-12-18 18:29:50 +00001817
Kevin Wolfeecc7742016-05-30 17:13:09 +02001818 if (s->qcow == NULL) {
1819 return 0;
1820 }
bellarda0464332005-12-18 18:29:50 +00001821
Kevin Wolfeecc7742016-05-30 17:13:09 +02001822 for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
1823 was_modified = bdrv_is_allocated(s->qcow->bs,
Eric Blaked6a644b2017-07-07 07:44:57 -05001824 (cluster2sector(s, cluster_num) +
1825 i) * BDRV_SECTOR_SIZE,
1826 BDRV_SECTOR_SIZE, NULL);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001827 }
bellarda0464332005-12-18 18:29:50 +00001828
Eric Blake6f712ee2017-03-08 15:34:28 -06001829 /*
1830 * Note that this treats failures to learn allocation status the
1831 * same as if an allocation has occurred. It's as safe as
1832 * anything else, given that a failure to learn allocation status
1833 * will probably result in more failures.
1834 */
1835 return !!was_modified;
bellarda0464332005-12-18 18:29:50 +00001836}
1837
1838static const char* get_basename(const char* path)
1839{
1840 char* basename = strrchr(path, '/');
1841 if (basename == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001842 return path;
bellarda0464332005-12-18 18:29:50 +00001843 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001844 return basename + 1; /* strip '/' */
bellarda0464332005-12-18 18:29:50 +00001845}
1846
1847/*
1848 * The array s->used_clusters holds the states of the clusters. If it is
1849 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1850 * was modified, bit 3 is set.
1851 * If any cluster is allocated, but not part of a file or directory, this
1852 * driver refuses to commit.
1853 */
1854typedef enum {
1855 USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
Anthony Liguoric227f092009-10-01 16:12:16 -05001856} used_t;
bellarda0464332005-12-18 18:29:50 +00001857
1858/*
1859 * get_cluster_count_for_direntry() not only determines how many clusters
1860 * are occupied by direntry, but also if it was renamed or modified.
1861 *
1862 * A file is thought to be renamed *only* if there already was a file with
1863 * exactly the same first cluster, but a different name.
1864 *
1865 * Further, the files/directories handled by this function are
1866 * assumed to be *not* deleted (and *only* those).
1867 */
1868static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001869 direntry_t* direntry, const char* path)
bellarda0464332005-12-18 18:29:50 +00001870{
1871 /*
1872 * This is a little bit tricky:
1873 * IF the guest OS just inserts a cluster into the file chain,
1874 * and leaves the rest alone, (i.e. the original file had clusters
1875 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1876 *
1877 * - do_commit will write the cluster into the file at the given
1878 * offset, but
1879 *
1880 * - the cluster which is overwritten should be moved to a later
1881 * position in the file.
1882 *
1883 * I am not aware that any OS does something as braindead, but this
1884 * situation could happen anyway when not committing for a long time.
1885 * Just to be sure that this does not bite us, detect it, and copy the
1886 * contents of the clusters to-be-overwritten into the qcow.
1887 */
1888 int copy_it = 0;
1889 int was_modified = 0;
1890 int32_t ret = 0;
1891
1892 uint32_t cluster_num = begin_of_direntry(direntry);
1893 uint32_t offset = 0;
1894 int first_mapping_index = -1;
Anthony Liguoric227f092009-10-01 16:12:16 -05001895 mapping_t* mapping = NULL;
bellarda0464332005-12-18 18:29:50 +00001896 const char* basename2 = NULL;
1897
1898 vvfat_close_current_file(s);
1899
1900 /* the root directory */
1901 if (cluster_num == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001902 return 0;
bellarda0464332005-12-18 18:29:50 +00001903
1904 /* write support */
1905 if (s->qcow) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001906 basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00001907
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001908 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001909
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001910 if (mapping) {
1911 const char* basename;
bellardda2414e2006-04-23 14:36:41 +00001912
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001913 assert(mapping->mode & MODE_DELETED);
1914 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00001915
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001916 basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001917
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001918 assert(mapping->mode & MODE_NORMAL);
bellarda0464332005-12-18 18:29:50 +00001919
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001920 /* rename */
1921 if (strcmp(basename, basename2))
1922 schedule_rename(s, cluster_num, g_strdup(path));
1923 } else if (is_file(direntry))
1924 /* new file */
1925 schedule_new_file(s, g_strdup(path), cluster_num);
1926 else {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001927 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001928 return 0;
1929 }
bellarda0464332005-12-18 18:29:50 +00001930 }
1931
1932 while(1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001933 if (s->qcow) {
1934 if (!copy_it && cluster_was_modified(s, cluster_num)) {
1935 if (mapping == NULL ||
1936 mapping->begin > cluster_num ||
1937 mapping->end <= cluster_num)
1938 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001939
1940
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001941 if (mapping &&
1942 (mapping->mode & MODE_DIRECTORY) == 0) {
bellarda0464332005-12-18 18:29:50 +00001943
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001944 /* was modified in qcow */
1945 if (offset != mapping->info.file.offset + s->cluster_size
1946 * (cluster_num - mapping->begin)) {
1947 /* offset of this cluster in file chain has changed */
Blue Swirl43dc2a62010-03-18 18:41:57 +00001948 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001949 copy_it = 1;
1950 } else if (offset == 0) {
1951 const char* basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001952
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001953 if (strcmp(basename, basename2))
1954 copy_it = 1;
1955 first_mapping_index = array_index(&(s->mapping), mapping);
1956 }
bellarda0464332005-12-18 18:29:50 +00001957
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001958 if (mapping->first_mapping_index != first_mapping_index
1959 && mapping->info.file.offset > 0) {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001960 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001961 copy_it = 1;
1962 }
bellarda0464332005-12-18 18:29:50 +00001963
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001964 /* need to write out? */
1965 if (!was_modified && is_file(direntry)) {
1966 was_modified = 1;
1967 schedule_writeout(s, mapping->dir_index, offset);
1968 }
1969 }
1970 }
bellardde167e42005-04-28 21:15:08 +00001971
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001972 if (copy_it) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001973 int i;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001974 /*
1975 * This is horribly inefficient, but that is okay, since
1976 * it is rarely executed, if at all.
1977 */
1978 int64_t offset = cluster2sector(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00001979
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001980 vvfat_close_current_file(s);
Kevin Wolf7704df92011-11-08 10:50:12 +01001981 for (i = 0; i < s->sectors_per_cluster; i++) {
Kevin Wolfeecc7742016-05-30 17:13:09 +02001982 int res;
1983
Eric Blaked6a644b2017-07-07 07:44:57 -05001984 res = bdrv_is_allocated(s->qcow->bs,
1985 (offset + i) * BDRV_SECTOR_SIZE,
1986 BDRV_SECTOR_SIZE, NULL);
Eric Blake6f712ee2017-03-08 15:34:28 -06001987 if (res < 0) {
1988 return -1;
1989 }
Kevin Wolfeecc7742016-05-30 17:13:09 +02001990 if (!res) {
1991 res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
1992 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001993 return -1;
1994 }
Kevin Wolf18d51c42016-05-31 14:42:08 +02001995 res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001996 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001997 return -2;
1998 }
1999 }
2000 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002001 }
2002 }
bellarda0464332005-12-18 18:29:50 +00002003
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002004 ret++;
2005 if (s->used_clusters[cluster_num] & USED_ANY)
2006 return 0;
2007 s->used_clusters[cluster_num] = USED_FILE;
bellarda0464332005-12-18 18:29:50 +00002008
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002009 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002010
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002011 if (fat_eof(s, cluster_num))
2012 return ret;
2013 else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
2014 return -1;
bellarda0464332005-12-18 18:29:50 +00002015
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002016 offset += s->cluster_size;
bellardde167e42005-04-28 21:15:08 +00002017 }
2018}
2019
bellarda0464332005-12-18 18:29:50 +00002020/*
ths5fafdf22007-09-16 21:08:06 +00002021 * This function looks at the modified data (qcow).
bellarda0464332005-12-18 18:29:50 +00002022 * It returns 0 upon inconsistency or error, and the number of clusters
2023 * used by the directory, its subdirectories and their files.
2024 */
2025static int check_directory_consistency(BDRVVVFATState *s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002026 int cluster_num, const char* path)
bellardde167e42005-04-28 21:15:08 +00002027{
bellarda0464332005-12-18 18:29:50 +00002028 int ret = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -05002029 unsigned char* cluster = g_malloc(s->cluster_size);
Anthony Liguoric227f092009-10-01 16:12:16 -05002030 direntry_t* direntries = (direntry_t*)cluster;
2031 mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00002032
bellarda0464332005-12-18 18:29:50 +00002033 long_file_name lfn;
2034 int path_len = strlen(path);
Kevin Wolf0d460d62011-06-01 10:57:00 +02002035 char path2[PATH_MAX + 1];
bellardde167e42005-04-28 21:15:08 +00002036
bellarda0464332005-12-18 18:29:50 +00002037 assert(path_len < PATH_MAX); /* len was tested before! */
blueswir1363a37d2008-08-21 17:58:08 +00002038 pstrcpy(path2, sizeof(path2), path);
bellarda0464332005-12-18 18:29:50 +00002039 path2[path_len] = '/';
2040 path2[path_len + 1] = '\0';
bellardde167e42005-04-28 21:15:08 +00002041
bellarda0464332005-12-18 18:29:50 +00002042 if (mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002043 const char* basename = get_basename(mapping->path);
2044 const char* basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00002045
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002046 assert(mapping->mode & MODE_DIRECTORY);
bellarda0464332005-12-18 18:29:50 +00002047
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002048 assert(mapping->mode & MODE_DELETED);
2049 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00002050
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002051 if (strcmp(basename, basename2))
2052 schedule_rename(s, cluster_num, g_strdup(path));
bellarda0464332005-12-18 18:29:50 +00002053 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002054 /* new directory */
2055 schedule_mkdir(s, cluster_num, g_strdup(path));
ths3b46e622007-09-17 08:09:54 +00002056
bellarda0464332005-12-18 18:29:50 +00002057 lfn_init(&lfn);
2058 do {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002059 int i;
2060 int subret = 0;
bellarda0464332005-12-18 18:29:50 +00002061
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002062 ret++;
bellarda0464332005-12-18 18:29:50 +00002063
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002064 if (s->used_clusters[cluster_num] & USED_ANY) {
2065 fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
Markus Armbruster6262bbd2014-05-28 11:17:04 +02002066 goto fail;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002067 }
2068 s->used_clusters[cluster_num] = USED_DIRECTORY;
bellardde167e42005-04-28 21:15:08 +00002069
bellarda0464332005-12-18 18:29:50 +00002070DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002071 subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
2072 s->sectors_per_cluster);
2073 if (subret) {
2074 fprintf(stderr, "Error fetching direntries\n");
2075 fail:
Stefan Weilce137822011-09-30 23:29:53 +02002076 g_free(cluster);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002077 return 0;
2078 }
bellarda0464332005-12-18 18:29:50 +00002079
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002080 for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
2081 int cluster_count = 0;
bellarda0464332005-12-18 18:29:50 +00002082
Stefan Weilb2bedb22011-09-12 22:33:01 +02002083DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002084 if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
2085 is_free(direntries + i))
2086 continue;
bellarda0464332005-12-18 18:29:50 +00002087
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002088 subret = parse_long_name(&lfn, direntries + i);
2089 if (subret < 0) {
2090 fprintf(stderr, "Error in long name\n");
2091 goto fail;
2092 }
2093 if (subret == 0 || is_free(direntries + i))
2094 continue;
bellarda0464332005-12-18 18:29:50 +00002095
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002096 if (fat_chksum(direntries+i) != lfn.checksum) {
2097 subret = parse_short_name(s, &lfn, direntries + i);
2098 if (subret < 0) {
2099 fprintf(stderr, "Error in short name (%d)\n", subret);
2100 goto fail;
2101 }
2102 if (subret > 0 || !strcmp((char*)lfn.name, ".")
2103 || !strcmp((char*)lfn.name, ".."))
2104 continue;
2105 }
2106 lfn.checksum = 0x100; /* cannot use long name twice */
bellarda0464332005-12-18 18:29:50 +00002107
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002108 if (path_len + 1 + lfn.len >= PATH_MAX) {
2109 fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
2110 goto fail;
2111 }
blueswir1363a37d2008-08-21 17:58:08 +00002112 pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
2113 (char*)lfn.name);
bellarda0464332005-12-18 18:29:50 +00002114
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002115 if (is_directory(direntries + i)) {
2116 if (begin_of_direntry(direntries + i) == 0) {
2117 DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
2118 goto fail;
2119 }
2120 cluster_count = check_directory_consistency(s,
2121 begin_of_direntry(direntries + i), path2);
2122 if (cluster_count == 0) {
2123 DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
2124 goto fail;
2125 }
2126 } else if (is_file(direntries + i)) {
2127 /* check file size with FAT */
2128 cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
2129 if (cluster_count !=
Laurent Vivier13385ae2016-05-31 18:35:54 +02002130 DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002131 DLOG(fprintf(stderr, "Cluster count mismatch\n"));
2132 goto fail;
2133 }
2134 } else
Blue Swirl43dc2a62010-03-18 18:41:57 +00002135 abort(); /* cluster_count = 0; */
bellarda0464332005-12-18 18:29:50 +00002136
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002137 ret += cluster_count;
2138 }
bellarda0464332005-12-18 18:29:50 +00002139
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002140 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002141 } while(!fat_eof(s, cluster_num));
2142
Stefan Weilce137822011-09-30 23:29:53 +02002143 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002144 return ret;
bellardde167e42005-04-28 21:15:08 +00002145}
2146
bellarda0464332005-12-18 18:29:50 +00002147/* returns 1 on success */
2148static int is_consistent(BDRVVVFATState* s)
bellardde167e42005-04-28 21:15:08 +00002149{
bellarda0464332005-12-18 18:29:50 +00002150 int i, check;
2151 int used_clusters_count = 0;
2152
2153DLOG(checkpoint());
2154 /*
2155 * - get modified FAT
2156 * - compare the two FATs (TODO)
2157 * - get buffer for marking used clusters
2158 * - recurse direntries from root (using bs->bdrv_read to make
2159 * sure to get the new data)
2160 * - check that the FAT agrees with the size
2161 * - count the number of clusters occupied by this directory and
2162 * its files
2163 * - check that the cumulative used cluster count agrees with the
2164 * FAT
2165 * - if all is fine, return number of used clusters
2166 */
2167 if (s->fat2 == NULL) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002168 int size = 0x200 * s->sectors_per_fat;
2169 s->fat2 = g_malloc(size);
2170 memcpy(s->fat2, s->fat.pointer, size);
bellarda0464332005-12-18 18:29:50 +00002171 }
2172 check = vvfat_read(s->bs,
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02002173 s->offset_to_fat, s->fat2, s->sectors_per_fat);
bellarda0464332005-12-18 18:29:50 +00002174 if (check) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002175 fprintf(stderr, "Could not copy fat\n");
2176 return 0;
bellardde167e42005-04-28 21:15:08 +00002177 }
bellarda0464332005-12-18 18:29:50 +00002178 assert (s->used_clusters);
2179 for (i = 0; i < sector2cluster(s, s->sector_count); i++)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002180 s->used_clusters[i] &= ~USED_ANY;
bellarda0464332005-12-18 18:29:50 +00002181
2182 clear_commits(s);
2183
2184 /* mark every mapped file/directory as deleted.
2185 * (check_directory_consistency() will unmark those still present). */
2186 if (s->qcow)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002187 for (i = 0; i < s->mapping.next; i++) {
2188 mapping_t* mapping = array_get(&(s->mapping), i);
2189 if (mapping->first_mapping_index < 0)
2190 mapping->mode |= MODE_DELETED;
2191 }
bellarda0464332005-12-18 18:29:50 +00002192
2193 used_clusters_count = check_directory_consistency(s, 0, s->path);
2194 if (used_clusters_count <= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002195 DLOG(fprintf(stderr, "problem in directory\n"));
2196 return 0;
bellarda0464332005-12-18 18:29:50 +00002197 }
2198
2199 check = s->last_cluster_of_root_directory;
2200 for (i = check; i < sector2cluster(s, s->sector_count); i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002201 if (modified_fat_get(s, i)) {
2202 if(!s->used_clusters[i]) {
2203 DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
2204 return 0;
2205 }
2206 check++;
2207 }
bellarda0464332005-12-18 18:29:50 +00002208
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002209 if (s->used_clusters[i] == USED_ALLOCATED) {
2210 /* allocated, but not used... */
2211 DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
2212 return 0;
2213 }
bellarda0464332005-12-18 18:29:50 +00002214 }
2215
2216 if (check != used_clusters_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002217 return 0;
bellarda0464332005-12-18 18:29:50 +00002218
2219 return used_clusters_count;
bellardde167e42005-04-28 21:15:08 +00002220}
2221
bellarda0464332005-12-18 18:29:50 +00002222static inline void adjust_mapping_indices(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002223 int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002224{
bellarda0464332005-12-18 18:29:50 +00002225 int i;
2226
2227 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002228 mapping_t* mapping = array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002229
2230#define ADJUST_MAPPING_INDEX(name) \
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002231 if (mapping->name >= offset) \
2232 mapping->name += adjust
bellarda0464332005-12-18 18:29:50 +00002233
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002234 ADJUST_MAPPING_INDEX(first_mapping_index);
2235 if (mapping->mode & MODE_DIRECTORY)
2236 ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
bellarda0464332005-12-18 18:29:50 +00002237 }
bellardde167e42005-04-28 21:15:08 +00002238}
2239
bellarda0464332005-12-18 18:29:50 +00002240/* insert or update mapping */
Anthony Liguoric227f092009-10-01 16:12:16 -05002241static mapping_t* insert_mapping(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002242 uint32_t begin, uint32_t end)
bellardde167e42005-04-28 21:15:08 +00002243{
bellarda0464332005-12-18 18:29:50 +00002244 /*
2245 * - find mapping where mapping->begin >= begin,
2246 * - if mapping->begin > begin: insert
2247 * - adjust all references to mappings!
2248 * - else: adjust
2249 * - replace name
2250 */
2251 int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05002252 mapping_t* mapping = NULL;
2253 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00002254
2255 if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002256 && mapping->begin < begin) {
2257 mapping->end = begin;
2258 index++;
2259 mapping = array_get(&(s->mapping), index);
bellarda0464332005-12-18 18:29:50 +00002260 }
2261 if (index >= s->mapping.next || mapping->begin > begin) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002262 mapping = array_insert(&(s->mapping), index, 1);
2263 mapping->path = NULL;
2264 adjust_mapping_indices(s, index, +1);
bellarda0464332005-12-18 18:29:50 +00002265 }
2266
bellardde167e42005-04-28 21:15:08 +00002267 mapping->begin = begin;
bellarda0464332005-12-18 18:29:50 +00002268 mapping->end = end;
2269
Anthony Liguoric227f092009-10-01 16:12:16 -05002270DLOG(mapping_t* next_mapping;
bellarda0464332005-12-18 18:29:50 +00002271assert(index + 1 >= s->mapping.next ||
2272((next_mapping = array_get(&(s->mapping), index + 1)) &&
2273 next_mapping->begin >= end)));
2274
Anthony Liguoric227f092009-10-01 16:12:16 -05002275 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002276 s->current_mapping = array_get(&(s->mapping),
2277 s->current_mapping - first_mapping);
bellarda0464332005-12-18 18:29:50 +00002278
2279 return mapping;
bellardde167e42005-04-28 21:15:08 +00002280}
2281
bellarda0464332005-12-18 18:29:50 +00002282static int remove_mapping(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +00002283{
Anthony Liguoric227f092009-10-01 16:12:16 -05002284 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
2285 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellardde167e42005-04-28 21:15:08 +00002286
bellarda0464332005-12-18 18:29:50 +00002287 /* free mapping */
Stefan Weilce137822011-09-30 23:29:53 +02002288 if (mapping->first_mapping_index < 0) {
2289 g_free(mapping->path);
2290 }
bellardde167e42005-04-28 21:15:08 +00002291
bellarda0464332005-12-18 18:29:50 +00002292 /* remove from s->mapping */
2293 array_remove(&(s->mapping), mapping_index);
bellardde167e42005-04-28 21:15:08 +00002294
bellarda0464332005-12-18 18:29:50 +00002295 /* adjust all references to mappings */
2296 adjust_mapping_indices(s, mapping_index, -1);
bellardde167e42005-04-28 21:15:08 +00002297
Anthony Liguoric227f092009-10-01 16:12:16 -05002298 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002299 s->current_mapping = array_get(&(s->mapping),
2300 s->current_mapping - first_mapping);
bellardde167e42005-04-28 21:15:08 +00002301
2302 return 0;
2303}
2304
bellarda0464332005-12-18 18:29:50 +00002305static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002306{
bellarda0464332005-12-18 18:29:50 +00002307 int i;
2308 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002309 mapping_t* mapping = array_get(&(s->mapping), i);
2310 if (mapping->dir_index >= offset)
2311 mapping->dir_index += adjust;
2312 if ((mapping->mode & MODE_DIRECTORY) &&
2313 mapping->info.dir.first_dir_index >= offset)
2314 mapping->info.dir.first_dir_index += adjust;
bellardde167e42005-04-28 21:15:08 +00002315 }
bellardde167e42005-04-28 21:15:08 +00002316}
2317
Anthony Liguoric227f092009-10-01 16:12:16 -05002318static direntry_t* insert_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002319 int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002320{
bellarda0464332005-12-18 18:29:50 +00002321 /*
2322 * make room in s->directory,
2323 * adjust_dirindices
2324 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002325 direntry_t* result = array_insert(&(s->directory), dir_index, count);
bellarda0464332005-12-18 18:29:50 +00002326 if (result == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002327 return NULL;
bellarda0464332005-12-18 18:29:50 +00002328 adjust_dirindices(s, dir_index, count);
bellardde167e42005-04-28 21:15:08 +00002329 return result;
2330}
2331
bellarda0464332005-12-18 18:29:50 +00002332static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002333{
bellarda0464332005-12-18 18:29:50 +00002334 int ret = array_remove_slice(&(s->directory), dir_index, count);
2335 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002336 return ret;
bellarda0464332005-12-18 18:29:50 +00002337 adjust_dirindices(s, dir_index, -count);
bellardde167e42005-04-28 21:15:08 +00002338 return 0;
2339}
2340
bellarda0464332005-12-18 18:29:50 +00002341/*
2342 * Adapt the mappings of the cluster chain starting at first cluster
2343 * (i.e. if a file starts at first_cluster, the chain is followed according
2344 * to the modified fat, and the corresponding entries in s->mapping are
2345 * adjusted)
2346 */
2347static int commit_mappings(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002348 uint32_t first_cluster, int dir_index)
bellardde167e42005-04-28 21:15:08 +00002349{
Anthony Liguoric227f092009-10-01 16:12:16 -05002350 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2351 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002352 uint32_t cluster = first_cluster;
bellardde167e42005-04-28 21:15:08 +00002353
bellarda0464332005-12-18 18:29:50 +00002354 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002355
bellarda0464332005-12-18 18:29:50 +00002356 assert(mapping);
2357 assert(mapping->begin == first_cluster);
2358 mapping->first_mapping_index = -1;
2359 mapping->dir_index = dir_index;
2360 mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002361 MODE_DIRECTORY : MODE_NORMAL;
bellardde167e42005-04-28 21:15:08 +00002362
bellarda0464332005-12-18 18:29:50 +00002363 while (!fat_eof(s, cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002364 uint32_t c, c1;
bellardde167e42005-04-28 21:15:08 +00002365
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002366 for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
2367 c = c1, c1 = modified_fat_get(s, c1));
bellardde167e42005-04-28 21:15:08 +00002368
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002369 c++;
2370 if (c > mapping->end) {
2371 int index = array_index(&(s->mapping), mapping);
2372 int i, max_i = s->mapping.next - index;
2373 for (i = 1; i < max_i && mapping[i].begin < c; i++);
2374 while (--i > 0)
2375 remove_mapping(s, index + 1);
2376 }
2377 assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
2378 || mapping[1].begin >= c);
2379 mapping->end = c;
bellarda0464332005-12-18 18:29:50 +00002380
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002381 if (!fat_eof(s, c1)) {
2382 int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
2383 mapping_t* next_mapping = i >= s->mapping.next ? NULL :
2384 array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002385
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002386 if (next_mapping == NULL || next_mapping->begin > c1) {
2387 int i1 = array_index(&(s->mapping), mapping);
bellarda0464332005-12-18 18:29:50 +00002388
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002389 next_mapping = insert_mapping(s, c1, c1+1);
bellarda0464332005-12-18 18:29:50 +00002390
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002391 if (c1 < c)
2392 i1++;
2393 mapping = array_get(&(s->mapping), i1);
2394 }
bellarda0464332005-12-18 18:29:50 +00002395
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002396 next_mapping->dir_index = mapping->dir_index;
2397 next_mapping->first_mapping_index =
2398 mapping->first_mapping_index < 0 ?
2399 array_index(&(s->mapping), mapping) :
2400 mapping->first_mapping_index;
2401 next_mapping->path = mapping->path;
2402 next_mapping->mode = mapping->mode;
2403 next_mapping->read_only = mapping->read_only;
2404 if (mapping->mode & MODE_DIRECTORY) {
2405 next_mapping->info.dir.parent_mapping_index =
2406 mapping->info.dir.parent_mapping_index;
2407 next_mapping->info.dir.first_dir_index =
2408 mapping->info.dir.first_dir_index +
2409 0x10 * s->sectors_per_cluster *
2410 (mapping->end - mapping->begin);
2411 } else
2412 next_mapping->info.file.offset = mapping->info.file.offset +
2413 mapping->end - mapping->begin;
bellarda0464332005-12-18 18:29:50 +00002414
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002415 mapping = next_mapping;
2416 }
ths3b46e622007-09-17 08:09:54 +00002417
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002418 cluster = c1;
bellarda0464332005-12-18 18:29:50 +00002419 }
2420
2421 return 0;
2422}
2423
2424static int commit_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002425 int dir_index, int parent_mapping_index)
bellarda0464332005-12-18 18:29:50 +00002426{
Anthony Liguoric227f092009-10-01 16:12:16 -05002427 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002428 uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
Anthony Liguoric227f092009-10-01 16:12:16 -05002429 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
bellarda0464332005-12-18 18:29:50 +00002430
2431 int factor = 0x10 * s->sectors_per_cluster;
2432 int old_cluster_count, new_cluster_count;
2433 int current_dir_index = mapping->info.dir.first_dir_index;
2434 int first_dir_index = current_dir_index;
2435 int ret, i;
2436 uint32_t c;
2437
2438DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index));
2439
2440 assert(direntry);
2441 assert(mapping);
2442 assert(mapping->begin == first_cluster);
2443 assert(mapping->info.dir.first_dir_index < s->directory.next);
2444 assert(mapping->mode & MODE_DIRECTORY);
2445 assert(dir_index == 0 || is_directory(direntry));
2446
2447 mapping->info.dir.parent_mapping_index = parent_mapping_index;
2448
2449 if (first_cluster == 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002450 old_cluster_count = new_cluster_count =
2451 s->last_cluster_of_root_directory;
bellarda0464332005-12-18 18:29:50 +00002452 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002453 for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2454 c = fat_get(s, c))
2455 old_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002456
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002457 for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2458 c = modified_fat_get(s, c))
2459 new_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002460 }
2461
2462 if (new_cluster_count > old_cluster_count) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002463 if (insert_direntries(s,
2464 current_dir_index + factor * old_cluster_count,
2465 factor * (new_cluster_count - old_cluster_count)) == NULL)
2466 return -1;
bellarda0464332005-12-18 18:29:50 +00002467 } else if (new_cluster_count < old_cluster_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002468 remove_direntries(s,
2469 current_dir_index + factor * new_cluster_count,
2470 factor * (old_cluster_count - new_cluster_count));
bellarda0464332005-12-18 18:29:50 +00002471
2472 for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
Kevin Wolfebb72c92016-04-27 14:11:38 +02002473 direntry_t *first_direntry;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002474 void* direntry = array_get(&(s->directory), current_dir_index);
2475 int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
2476 s->sectors_per_cluster);
2477 if (ret)
2478 return ret;
Kevin Wolfebb72c92016-04-27 14:11:38 +02002479
2480 /* The first directory entry on the filesystem is the volume name */
2481 first_direntry = (direntry_t*) s->directory.pointer;
2482 assert(!memcmp(first_direntry->name, s->volume_label, 11));
2483
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002484 current_dir_index += factor;
bellarda0464332005-12-18 18:29:50 +00002485 }
2486
2487 ret = commit_mappings(s, first_cluster, dir_index);
2488 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002489 return ret;
bellarda0464332005-12-18 18:29:50 +00002490
2491 /* recurse */
2492 for (i = 0; i < factor * new_cluster_count; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002493 direntry = array_get(&(s->directory), first_dir_index + i);
2494 if (is_directory(direntry) && !is_dot(direntry)) {
2495 mapping = find_mapping_for_cluster(s, first_cluster);
2496 assert(mapping->mode & MODE_DIRECTORY);
2497 ret = commit_direntries(s, first_dir_index + i,
2498 array_index(&(s->mapping), mapping));
2499 if (ret)
2500 return ret;
2501 }
bellardde167e42005-04-28 21:15:08 +00002502 }
bellarda0464332005-12-18 18:29:50 +00002503
bellardde167e42005-04-28 21:15:08 +00002504 return 0;
2505}
2506
bellarda0464332005-12-18 18:29:50 +00002507/* commit one file (adjust contents, adjust mapping),
2508 return first_mapping_index */
2509static int commit_one_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002510 int dir_index, uint32_t offset)
bellarda0464332005-12-18 18:29:50 +00002511{
Anthony Liguoric227f092009-10-01 16:12:16 -05002512 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002513 uint32_t c = begin_of_direntry(direntry);
2514 uint32_t first_cluster = c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002515 mapping_t* mapping = find_mapping_for_cluster(s, c);
bellarda0464332005-12-18 18:29:50 +00002516 uint32_t size = filesize_of_direntry(direntry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002517 char* cluster = g_malloc(s->cluster_size);
bellarda0464332005-12-18 18:29:50 +00002518 uint32_t i;
2519 int fd = 0;
2520
2521 assert(offset < size);
2522 assert((offset % s->cluster_size) == 0);
2523
2524 for (i = s->cluster_size; i < offset; i += s->cluster_size)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002525 c = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002526
Corey Bryant6165f4d2012-08-14 16:43:45 -04002527 fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
bellarda0464332005-12-18 18:29:50 +00002528 if (fd < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002529 fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
2530 strerror(errno), errno);
Stefan Weilce137822011-09-30 23:29:53 +02002531 g_free(cluster);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002532 return fd;
bellarda0464332005-12-18 18:29:50 +00002533 }
Stefan Weilce137822011-09-30 23:29:53 +02002534 if (offset > 0) {
2535 if (lseek(fd, offset, SEEK_SET) != offset) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002536 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002537 g_free(cluster);
2538 return -3;
2539 }
2540 }
bellarda0464332005-12-18 18:29:50 +00002541
2542 while (offset < size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002543 uint32_t c1;
2544 int rest_size = (size - offset > s->cluster_size ?
2545 s->cluster_size : size - offset);
2546 int ret;
bellarda0464332005-12-18 18:29:50 +00002547
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002548 c1 = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002549
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002550 assert((size - offset == 0 && fat_eof(s, c)) ||
2551 (size > offset && c >=2 && !fat_eof(s, c)));
bellarda0464332005-12-18 18:29:50 +00002552
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002553 ret = vvfat_read(s->bs, cluster2sector(s, c),
Marc-André Lureau78ee96d2017-06-22 13:04:16 +02002554 (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
bellarda0464332005-12-18 18:29:50 +00002555
Stefan Weilce137822011-09-30 23:29:53 +02002556 if (ret < 0) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002557 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002558 g_free(cluster);
2559 return ret;
2560 }
bellarda0464332005-12-18 18:29:50 +00002561
Stefan Weilce137822011-09-30 23:29:53 +02002562 if (write(fd, cluster, rest_size) < 0) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002563 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002564 g_free(cluster);
2565 return -2;
2566 }
bellarda0464332005-12-18 18:29:50 +00002567
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002568 offset += rest_size;
2569 c = c1;
bellarda0464332005-12-18 18:29:50 +00002570 }
2571
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002572 if (ftruncate(fd, size)) {
2573 perror("ftruncate()");
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002574 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002575 g_free(cluster);
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002576 return -4;
2577 }
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002578 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002579 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002580
2581 return commit_mappings(s, first_cluster, dir_index);
2582}
2583
2584#ifdef DEBUG
2585/* test, if all mappings point to valid direntries */
2586static void check1(BDRVVVFATState* s)
2587{
2588 int i;
2589 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002590 mapping_t* mapping = array_get(&(s->mapping), i);
2591 if (mapping->mode & MODE_DELETED) {
2592 fprintf(stderr, "deleted\n");
2593 continue;
2594 }
2595 assert(mapping->dir_index < s->directory.next);
2596 direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
2597 assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
2598 if (mapping->mode & MODE_DIRECTORY) {
2599 assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
2600 assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
2601 }
bellarda0464332005-12-18 18:29:50 +00002602 }
2603}
2604
2605/* test, if all direntries have mappings */
2606static void check2(BDRVVVFATState* s)
2607{
2608 int i;
2609 int first_mapping = -1;
2610
2611 for (i = 0; i < s->directory.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002612 direntry_t* direntry = array_get(&(s->directory), i);
bellarda0464332005-12-18 18:29:50 +00002613
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002614 if (is_short_name(direntry) && begin_of_direntry(direntry)) {
2615 mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
2616 assert(mapping);
2617 assert(mapping->dir_index == i || is_dot(direntry));
2618 assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
2619 }
bellarda0464332005-12-18 18:29:50 +00002620
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002621 if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
2622 /* cluster start */
2623 int j, count = 0;
bellarda0464332005-12-18 18:29:50 +00002624
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002625 for (j = 0; j < s->mapping.next; j++) {
2626 mapping_t* mapping = array_get(&(s->mapping), j);
2627 if (mapping->mode & MODE_DELETED)
2628 continue;
2629 if (mapping->mode & MODE_DIRECTORY) {
2630 if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
2631 assert(++count == 1);
2632 if (mapping->first_mapping_index == -1)
2633 first_mapping = array_index(&(s->mapping), mapping);
2634 else
2635 assert(first_mapping == mapping->first_mapping_index);
2636 if (mapping->info.dir.parent_mapping_index < 0)
2637 assert(j == 0);
2638 else {
2639 mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
2640 assert(parent->mode & MODE_DIRECTORY);
2641 assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
2642 }
2643 }
2644 }
2645 }
2646 if (count == 0)
2647 first_mapping = -1;
2648 }
bellarda0464332005-12-18 18:29:50 +00002649 }
2650}
2651#endif
2652
2653static int handle_renames_and_mkdirs(BDRVVVFATState* s)
2654{
2655 int i;
2656
2657#ifdef DEBUG
2658 fprintf(stderr, "handle_renames\n");
2659 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002660 commit_t* commit = array_get(&(s->commits), i);
2661 fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action);
bellarda0464332005-12-18 18:29:50 +00002662 }
2663#endif
2664
2665 for (i = 0; i < s->commits.next;) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002666 commit_t* commit = array_get(&(s->commits), i);
2667 if (commit->action == ACTION_RENAME) {
2668 mapping_t* mapping = find_mapping_for_cluster(s,
2669 commit->param.rename.cluster);
2670 char* old_path = mapping->path;
bellarda0464332005-12-18 18:29:50 +00002671
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002672 assert(commit->path);
2673 mapping->path = commit->path;
2674 if (rename(old_path, mapping->path))
2675 return -2;
bellarda0464332005-12-18 18:29:50 +00002676
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002677 if (mapping->mode & MODE_DIRECTORY) {
2678 int l1 = strlen(mapping->path);
2679 int l2 = strlen(old_path);
2680 int diff = l1 - l2;
2681 direntry_t* direntry = array_get(&(s->directory),
2682 mapping->info.dir.first_dir_index);
2683 uint32_t c = mapping->begin;
2684 int i = 0;
bellarda0464332005-12-18 18:29:50 +00002685
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002686 /* recurse */
2687 while (!fat_eof(s, c)) {
2688 do {
2689 direntry_t* d = direntry + i;
bellarda0464332005-12-18 18:29:50 +00002690
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002691 if (is_file(d) || (is_directory(d) && !is_dot(d))) {
2692 mapping_t* m = find_mapping_for_cluster(s,
2693 begin_of_direntry(d));
2694 int l = strlen(m->path);
2695 char* new_path = g_malloc(l + diff + 1);
bellarda0464332005-12-18 18:29:50 +00002696
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002697 assert(!strncmp(m->path, mapping->path, l2));
bellarda0464332005-12-18 18:29:50 +00002698
blueswir1363a37d2008-08-21 17:58:08 +00002699 pstrcpy(new_path, l + diff + 1, mapping->path);
2700 pstrcpy(new_path + l1, l + diff + 1 - l1,
2701 m->path + l2);
bellarda0464332005-12-18 18:29:50 +00002702
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002703 schedule_rename(s, m->begin, new_path);
2704 }
2705 i++;
2706 } while((i % (0x10 * s->sectors_per_cluster)) != 0);
2707 c = fat_get(s, c);
2708 }
2709 }
bellarda0464332005-12-18 18:29:50 +00002710
Stefan Weilce137822011-09-30 23:29:53 +02002711 g_free(old_path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002712 array_remove(&(s->commits), i);
2713 continue;
2714 } else if (commit->action == ACTION_MKDIR) {
2715 mapping_t* mapping;
2716 int j, parent_path_len;
bellarda0464332005-12-18 18:29:50 +00002717
bellard48c2f062005-12-19 22:11:49 +00002718#ifdef __MINGW32__
2719 if (mkdir(commit->path))
2720 return -5;
2721#else
2722 if (mkdir(commit->path, 0755))
2723 return -5;
2724#endif
bellarda0464332005-12-18 18:29:50 +00002725
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002726 mapping = insert_mapping(s, commit->param.mkdir.cluster,
2727 commit->param.mkdir.cluster + 1);
2728 if (mapping == NULL)
2729 return -6;
bellarda0464332005-12-18 18:29:50 +00002730
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002731 mapping->mode = MODE_DIRECTORY;
2732 mapping->read_only = 0;
2733 mapping->path = commit->path;
2734 j = s->directory.next;
2735 assert(j);
2736 insert_direntries(s, s->directory.next,
2737 0x10 * s->sectors_per_cluster);
2738 mapping->info.dir.first_dir_index = j;
bellarda0464332005-12-18 18:29:50 +00002739
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002740 parent_path_len = strlen(commit->path)
2741 - strlen(get_basename(commit->path)) - 1;
2742 for (j = 0; j < s->mapping.next; j++) {
2743 mapping_t* m = array_get(&(s->mapping), j);
2744 if (m->first_mapping_index < 0 && m != mapping &&
2745 !strncmp(m->path, mapping->path, parent_path_len) &&
2746 strlen(m->path) == parent_path_len)
2747 break;
2748 }
2749 assert(j < s->mapping.next);
2750 mapping->info.dir.parent_mapping_index = j;
bellarda0464332005-12-18 18:29:50 +00002751
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002752 array_remove(&(s->commits), i);
2753 continue;
2754 }
bellarda0464332005-12-18 18:29:50 +00002755
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002756 i++;
bellarda0464332005-12-18 18:29:50 +00002757 }
2758 return 0;
2759}
2760
2761/*
2762 * TODO: make sure that the short name is not matching *another* file
2763 */
2764static int handle_commits(BDRVVVFATState* s)
2765{
2766 int i, fail = 0;
2767
2768 vvfat_close_current_file(s);
2769
2770 for (i = 0; !fail && i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002771 commit_t* commit = array_get(&(s->commits), i);
2772 switch(commit->action) {
2773 case ACTION_RENAME: case ACTION_MKDIR:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002774 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002775 fail = -2;
2776 break;
2777 case ACTION_WRITEOUT: {
Blue Swirla6c6f762010-03-13 14:18:50 +00002778#ifndef NDEBUG
2779 /* these variables are only used by assert() below */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002780 direntry_t* entry = array_get(&(s->directory),
2781 commit->param.writeout.dir_index);
2782 uint32_t begin = begin_of_direntry(entry);
2783 mapping_t* mapping = find_mapping_for_cluster(s, begin);
Blue Swirla6c6f762010-03-13 14:18:50 +00002784#endif
bellarda0464332005-12-18 18:29:50 +00002785
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002786 assert(mapping);
2787 assert(mapping->begin == begin);
2788 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00002789
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002790 if (commit_one_file(s, commit->param.writeout.dir_index,
2791 commit->param.writeout.modified_offset))
2792 fail = -3;
bellarda0464332005-12-18 18:29:50 +00002793
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002794 break;
2795 }
2796 case ACTION_NEW_FILE: {
2797 int begin = commit->param.new_file.first_cluster;
2798 mapping_t* mapping = find_mapping_for_cluster(s, begin);
2799 direntry_t* entry;
2800 int i;
bellarda0464332005-12-18 18:29:50 +00002801
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002802 /* find direntry */
2803 for (i = 0; i < s->directory.next; i++) {
2804 entry = array_get(&(s->directory), i);
2805 if (is_file(entry) && begin_of_direntry(entry) == begin)
2806 break;
2807 }
bellarda0464332005-12-18 18:29:50 +00002808
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002809 if (i >= s->directory.next) {
2810 fail = -6;
2811 continue;
2812 }
bellarda0464332005-12-18 18:29:50 +00002813
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002814 /* make sure there exists an initial mapping */
2815 if (mapping && mapping->begin != begin) {
2816 mapping->end = begin;
2817 mapping = NULL;
2818 }
2819 if (mapping == NULL) {
2820 mapping = insert_mapping(s, begin, begin+1);
2821 }
2822 /* most members will be fixed in commit_mappings() */
2823 assert(commit->path);
2824 mapping->path = commit->path;
2825 mapping->read_only = 0;
2826 mapping->mode = MODE_NORMAL;
2827 mapping->info.file.offset = 0;
bellarda0464332005-12-18 18:29:50 +00002828
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002829 if (commit_one_file(s, i, 0))
2830 fail = -7;
bellarda0464332005-12-18 18:29:50 +00002831
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002832 break;
2833 }
2834 default:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002835 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002836 }
bellarda0464332005-12-18 18:29:50 +00002837 }
2838 if (i > 0 && array_remove_slice(&(s->commits), 0, i))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002839 return -1;
bellarda0464332005-12-18 18:29:50 +00002840 return fail;
2841}
2842
2843static int handle_deletes(BDRVVVFATState* s)
2844{
2845 int i, deferred = 1, deleted = 1;
2846
2847 /* delete files corresponding to mappings marked as deleted */
2848 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2849 while (deferred && deleted) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002850 deferred = 0;
2851 deleted = 0;
bellarda0464332005-12-18 18:29:50 +00002852
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002853 for (i = 1; i < s->mapping.next; i++) {
2854 mapping_t* mapping = array_get(&(s->mapping), i);
2855 if (mapping->mode & MODE_DELETED) {
2856 direntry_t* entry = array_get(&(s->directory),
2857 mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +00002858
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002859 if (is_free(entry)) {
2860 /* remove file/directory */
2861 if (mapping->mode & MODE_DIRECTORY) {
2862 int j, next_dir_index = s->directory.next,
2863 first_dir_index = mapping->info.dir.first_dir_index;
bellarda0464332005-12-18 18:29:50 +00002864
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002865 if (rmdir(mapping->path) < 0) {
2866 if (errno == ENOTEMPTY) {
2867 deferred++;
2868 continue;
2869 } else
2870 return -5;
2871 }
bellarda0464332005-12-18 18:29:50 +00002872
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002873 for (j = 1; j < s->mapping.next; j++) {
2874 mapping_t* m = array_get(&(s->mapping), j);
2875 if (m->mode & MODE_DIRECTORY &&
2876 m->info.dir.first_dir_index >
2877 first_dir_index &&
2878 m->info.dir.first_dir_index <
2879 next_dir_index)
2880 next_dir_index =
2881 m->info.dir.first_dir_index;
2882 }
2883 remove_direntries(s, first_dir_index,
2884 next_dir_index - first_dir_index);
bellarda0464332005-12-18 18:29:50 +00002885
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002886 deleted++;
2887 }
2888 } else {
2889 if (unlink(mapping->path))
2890 return -4;
2891 deleted++;
2892 }
2893 DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
2894 remove_mapping(s, i);
2895 }
2896 }
bellarda0464332005-12-18 18:29:50 +00002897 }
2898
2899 return 0;
2900}
2901
2902/*
2903 * synchronize mapping with new state:
2904 *
2905 * - copy FAT (with bdrv_read)
2906 * - mark all filenames corresponding to mappings as deleted
2907 * - recurse direntries from root (using bs->bdrv_read)
2908 * - delete files corresponding to mappings marked as deleted
2909 */
2910static int do_commit(BDRVVVFATState* s)
2911{
2912 int ret = 0;
2913
2914 /* the real meat are the commits. Nothing to do? Move along! */
2915 if (s->commits.next == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002916 return 0;
bellarda0464332005-12-18 18:29:50 +00002917
2918 vvfat_close_current_file(s);
2919
2920 ret = handle_renames_and_mkdirs(s);
2921 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002922 fprintf(stderr, "Error handling renames (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002923 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002924 return ret;
bellarda0464332005-12-18 18:29:50 +00002925 }
2926
ths5fafdf22007-09-16 21:08:06 +00002927 /* copy FAT (with bdrv_read) */
bellarda0464332005-12-18 18:29:50 +00002928 memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
2929
2930 /* recurse direntries from root (using bs->bdrv_read) */
2931 ret = commit_direntries(s, 0, -1);
2932 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002933 fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002934 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002935 return ret;
bellarda0464332005-12-18 18:29:50 +00002936 }
2937
2938 ret = handle_commits(s);
2939 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002940 fprintf(stderr, "Error handling commits (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002941 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002942 return ret;
bellarda0464332005-12-18 18:29:50 +00002943 }
2944
2945 ret = handle_deletes(s);
2946 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002947 fprintf(stderr, "Error deleting\n");
Blue Swirl43dc2a62010-03-18 18:41:57 +00002948 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002949 return ret;
bellarda0464332005-12-18 18:29:50 +00002950 }
2951
Max Reitzd470ad42017-11-10 21:31:09 +01002952 if (s->qcow->bs->drv && s->qcow->bs->drv->bdrv_make_empty) {
Kevin Wolfeecc7742016-05-30 17:13:09 +02002953 s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
Kevin Wolf7704df92011-11-08 10:50:12 +01002954 }
bellarda0464332005-12-18 18:29:50 +00002955
2956 memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
2957
2958DLOG(checkpoint());
2959 return 0;
2960}
2961
2962static int try_commit(BDRVVVFATState* s)
2963{
2964 vvfat_close_current_file(s);
2965DLOG(checkpoint());
2966 if(!is_consistent(s))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002967 return -1;
bellarda0464332005-12-18 18:29:50 +00002968 return do_commit(s);
2969}
2970
ths5fafdf22007-09-16 21:08:06 +00002971static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00002972 const uint8_t *buf, int nb_sectors)
2973{
ths5fafdf22007-09-16 21:08:06 +00002974 BDRVVVFATState *s = bs->opaque;
bellarda0464332005-12-18 18:29:50 +00002975 int i, ret;
bellardde167e42005-04-28 21:15:08 +00002976
bellarda0464332005-12-18 18:29:50 +00002977DLOG(checkpoint());
bellardde167e42005-04-28 21:15:08 +00002978
Kevin Wolfac48e382010-09-10 12:27:02 +02002979 /* Check if we're operating in read-only mode */
2980 if (s->qcow == NULL) {
2981 return -EACCES;
2982 }
2983
bellarda0464332005-12-18 18:29:50 +00002984 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002985
bellarda0464332005-12-18 18:29:50 +00002986 /*
2987 * Some sanity checks:
2988 * - do not allow writing to the boot sector
bellarda0464332005-12-18 18:29:50 +00002989 */
bellardde167e42005-04-28 21:15:08 +00002990
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02002991 if (sector_num < s->offset_to_fat)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002992 return -1;
bellardde167e42005-04-28 21:15:08 +00002993
bellarda0464332005-12-18 18:29:50 +00002994 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002995 i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
2996 mapping_t* mapping = find_mapping_for_cluster(s, i);
2997 if (mapping) {
2998 if (mapping->read_only) {
2999 fprintf(stderr, "Tried to write to write-protected file %s\n",
3000 mapping->path);
3001 return -1;
3002 }
bellardde167e42005-04-28 21:15:08 +00003003
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003004 if (mapping->mode & MODE_DIRECTORY) {
3005 int begin = cluster2sector(s, i);
3006 int end = begin + s->sectors_per_cluster, k;
3007 int dir_index;
3008 const direntry_t* direntries;
3009 long_file_name lfn;
bellardde167e42005-04-28 21:15:08 +00003010
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003011 lfn_init(&lfn);
bellardde167e42005-04-28 21:15:08 +00003012
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003013 if (begin < sector_num)
3014 begin = sector_num;
3015 if (end > sector_num + nb_sectors)
3016 end = sector_num + nb_sectors;
3017 dir_index = mapping->dir_index +
3018 0x10 * (begin - mapping->begin * s->sectors_per_cluster);
3019 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
bellardde167e42005-04-28 21:15:08 +00003020
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003021 for (k = 0; k < (end - begin) * 0x10; k++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003022 /* no access to the direntry of a read-only file */
Hervé Poussineaue03da262017-07-15 15:28:40 +02003023 if (is_short_name(direntries + k) &&
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003024 (direntries[k].attributes & 1)) {
3025 if (memcmp(direntries + k,
3026 array_get(&(s->directory), dir_index + k),
3027 sizeof(direntry_t))) {
Alistair Francis2ab4b132017-09-11 12:52:50 -07003028 warn_report("tried to write to write-protected "
3029 "file");
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003030 return -1;
3031 }
3032 }
3033 }
3034 }
3035 i = mapping->end;
3036 } else
3037 i++;
bellardde167e42005-04-28 21:15:08 +00003038 }
bellarda0464332005-12-18 18:29:50 +00003039
3040 /*
3041 * Use qcow backend. Commit later.
3042 */
3043DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
Kevin Wolf18d51c42016-05-31 14:42:08 +02003044 ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
bellarda0464332005-12-18 18:29:50 +00003045 if (ret < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003046 fprintf(stderr, "Error writing to qcow backend\n");
3047 return ret;
bellarda0464332005-12-18 18:29:50 +00003048 }
3049
3050 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003051 i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
3052 if (i >= 0)
3053 s->used_clusters[i] |= USED_ALLOCATED;
bellarda0464332005-12-18 18:29:50 +00003054
3055DLOG(checkpoint());
3056 /* TODO: add timeout */
3057 try_commit(s);
3058
3059DLOG(checkpoint());
3060 return 0;
3061}
3062
Kevin Wolf4575eb42016-04-26 17:14:08 +02003063static int coroutine_fn
3064vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3065 QEMUIOVector *qiov, int flags)
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003066{
3067 int ret;
3068 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02003069 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
3070 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
3071 void *buf;
3072
3073 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3074 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
3075
3076 buf = g_try_malloc(bytes);
3077 if (bytes && buf == NULL) {
3078 return -ENOMEM;
3079 }
3080 qemu_iovec_to_buf(qiov, 0, buf, bytes);
3081
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003082 qemu_co_mutex_lock(&s->lock);
3083 ret = vvfat_write(bs, sector_num, buf, nb_sectors);
3084 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02003085
3086 g_free(buf);
3087
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003088 return ret;
3089}
3090
Eric Blakefba39982018-02-13 14:27:00 -06003091static int coroutine_fn vvfat_co_block_status(BlockDriverState *bs,
3092 bool want_zero, int64_t offset,
3093 int64_t bytes, int64_t *n,
3094 int64_t *map,
3095 BlockDriverState **file)
bellarda0464332005-12-18 18:29:50 +00003096{
Eric Blakefba39982018-02-13 14:27:00 -06003097 *n = bytes;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02003098 return BDRV_BLOCK_DATA;
bellarda0464332005-12-18 18:29:50 +00003099}
3100
Kevin Wolf4575eb42016-04-26 17:14:08 +02003101static int coroutine_fn
3102write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3103 QEMUIOVector *qiov, int flags)
3104{
Paolo Bonzini254aee42017-06-29 15:27:43 +02003105 int ret;
3106
Kevin Wolf9217e262010-09-10 12:27:03 +02003107 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Paolo Bonzini254aee42017-06-29 15:27:43 +02003108 qemu_co_mutex_lock(&s->lock);
3109 ret = try_commit(s);
3110 qemu_co_mutex_unlock(&s->lock);
3111
3112 return ret;
bellarda0464332005-12-18 18:29:50 +00003113}
3114
3115static void write_target_close(BlockDriverState *bs) {
Kevin Wolf9217e262010-09-10 12:27:03 +02003116 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Kevin Wolfeecc7742016-05-30 17:13:09 +02003117 bdrv_unref_child(s->bs, s->qcow);
Stefan Weilce137822011-09-30 23:29:53 +02003118 g_free(s->qcow_filename);
bellarda0464332005-12-18 18:29:50 +00003119}
3120
3121static BlockDriver vvfat_write_target = {
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003122 .format_name = "vvfat_write_target",
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003123 .instance_size = sizeof(void*),
Kevin Wolf4575eb42016-04-26 17:14:08 +02003124 .bdrv_co_pwritev = write_target_commit,
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003125 .bdrv_close = write_target_close,
bellarda0464332005-12-18 18:29:50 +00003126};
3127
Kevin Wolfeecc7742016-05-30 17:13:09 +02003128static void vvfat_qcow_options(int *child_flags, QDict *child_options,
3129 int parent_flags, QDict *parent_options)
bellarda0464332005-12-18 18:29:50 +00003130{
Alberto Garciaf87a0e22016-09-15 17:53:02 +03003131 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
3132 *child_flags = BDRV_O_NO_FLUSH;
Kevin Wolfeecc7742016-05-30 17:13:09 +02003133}
3134
3135static const BdrvChildRole child_vvfat_qcow = {
3136 .inherit_options = vvfat_qcow_options,
3137};
3138
3139static int enable_write_target(BlockDriverState *bs, Error **errp)
3140{
3141 BDRVVVFATState *s = bs->opaque;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003142 BlockDriver *bdrv_qcow = NULL;
Kevin Wolf5db15a52015-09-14 15:33:33 +02003143 BlockDriverState *backing;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003144 QemuOpts *opts = NULL;
Kevin Wolfa6552112010-09-10 12:27:04 +02003145 int ret;
bellarda0464332005-12-18 18:29:50 +00003146 int size = sector2cluster(s, s->sector_count);
Max Reitze6641712015-08-26 19:47:48 +02003147 QDict *options;
3148
bellarda0464332005-12-18 18:29:50 +00003149 s->used_clusters = calloc(size, 1);
3150
Anthony Liguoric227f092009-10-01 16:12:16 -05003151 array_init(&(s->commits), sizeof(commit_t));
bellarda0464332005-12-18 18:29:50 +00003152
Jeff Cody9a29e182015-01-22 08:03:30 -05003153 s->qcow_filename = g_malloc(PATH_MAX);
3154 ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
Jim Meyeringeba25052012-05-28 09:27:54 +02003155 if (ret < 0) {
Markus Armbruster68c70af2014-05-16 11:00:17 +02003156 error_setg_errno(errp, -ret, "can't create temporary file");
Fam Zheng78f27bd2013-07-17 17:57:37 +08003157 goto err;
Jim Meyeringeba25052012-05-28 09:27:54 +02003158 }
Kevin Wolf91a073a2009-05-27 14:48:06 +02003159
3160 bdrv_qcow = bdrv_find_format("qcow");
Max Reitz1bcb15c2014-12-02 18:32:43 +01003161 if (!bdrv_qcow) {
3162 error_setg(errp, "Failed to locate qcow driver");
3163 ret = -ENOENT;
3164 goto err;
3165 }
3166
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003167 opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003168 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
3169 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003170 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
Kevin Wolf91a073a2009-05-27 14:48:06 +02003171
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003172 ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
Chunyan Liufacdbb02014-06-05 17:20:52 +08003173 qemu_opts_del(opts);
Fam Zheng78f27bd2013-07-17 17:57:37 +08003174 if (ret < 0) {
3175 goto err;
3176 }
Kevin Wolfa6552112010-09-10 12:27:04 +02003177
Max Reitze6641712015-08-26 19:47:48 +02003178 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003179 qdict_put_str(options, "write-target.driver", "qcow");
Kevin Wolfeecc7742016-05-30 17:13:09 +02003180 s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
3181 &child_vvfat_qcow, false, errp);
Max Reitzc4b48bf2016-07-11 15:54:52 +02003182 QDECREF(options);
Max Reitz5b363932016-05-17 16:41:31 +02003183 if (!s->qcow) {
3184 ret = -EINVAL;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003185 goto err;
Kevin Wolfd6e90982010-03-31 14:40:27 +02003186 }
bellarda0464332005-12-18 18:29:50 +00003187
3188#ifndef _WIN32
3189 unlink(s->qcow_filename);
3190#endif
3191
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003192 backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
3193 &error_abort);
3194 *(void**) backing->opaque = s;
3195
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003196 bdrv_set_backing_hd(s->bs, backing, &error_abort);
Kevin Wolf5db15a52015-09-14 15:33:33 +02003197 bdrv_unref(backing);
3198
bellardde167e42005-04-28 21:15:08 +00003199 return 0;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003200
3201err:
3202 g_free(s->qcow_filename);
3203 s->qcow_filename = NULL;
3204 return ret;
bellardde167e42005-04-28 21:15:08 +00003205}
3206
Kevin Wolf91ef3822016-12-20 16:23:46 +01003207static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
3208 const BdrvChildRole *role,
Kevin Wolfe0995dc2017-09-14 12:47:11 +02003209 BlockReopenQueue *reopen_queue,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003210 uint64_t perm, uint64_t shared,
3211 uint64_t *nperm, uint64_t *nshared)
3212{
3213 BDRVVVFATState *s = bs->opaque;
3214
3215 assert(c == s->qcow || role == &child_backing);
3216
3217 if (c == s->qcow) {
3218 /* This is a private node, nobody should try to attach to it */
3219 *nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
3220 *nshared = BLK_PERM_WRITE_UNCHANGED;
3221 } else {
3222 /* The backing file is there so 'commit' can use it. vvfat doesn't
3223 * access it in any way. */
3224 *nperm = 0;
3225 *nshared = BLK_PERM_ALL;
3226 }
3227}
3228
bellardde167e42005-04-28 21:15:08 +00003229static void vvfat_close(BlockDriverState *bs)
3230{
3231 BDRVVVFATState *s = bs->opaque;
3232
3233 vvfat_close_current_file(s);
3234 array_free(&(s->fat));
3235 array_free(&(s->directory));
3236 array_free(&(s->mapping));
Stefan Weilce137822011-09-30 23:29:53 +02003237 g_free(s->cluster_buffer);
Kevin Wolf3397f0c2011-11-22 16:52:13 +01003238
3239 if (s->qcow) {
3240 migrate_del_blocker(s->migration_blocker);
3241 error_free(s->migration_blocker);
3242 }
bellardde167e42005-04-28 21:15:08 +00003243}
3244
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003245static BlockDriver bdrv_vvfat = {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003246 .format_name = "vvfat",
3247 .protocol_name = "fat",
3248 .instance_size = sizeof(BDRVVVFATState),
3249
3250 .bdrv_parse_filename = vvfat_parse_filename,
3251 .bdrv_file_open = vvfat_open,
Eric Blakea6506482016-06-23 16:37:17 -06003252 .bdrv_refresh_limits = vvfat_refresh_limits,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003253 .bdrv_close = vvfat_close,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003254 .bdrv_child_perm = vvfat_child_perm,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003255
Kevin Wolf4575eb42016-04-26 17:14:08 +02003256 .bdrv_co_preadv = vvfat_co_preadv,
3257 .bdrv_co_pwritev = vvfat_co_pwritev,
Eric Blakefba39982018-02-13 14:27:00 -06003258 .bdrv_co_block_status = vvfat_co_block_status,
bellardde167e42005-04-28 21:15:08 +00003259};
3260
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003261static void bdrv_vvfat_init(void)
3262{
3263 bdrv_register(&bdrv_vvfat);
3264}
3265
3266block_init(bdrv_vvfat_init);
3267
bellarda0464332005-12-18 18:29:50 +00003268#ifdef DEBUG
Thomas Huth7a6ab452017-09-13 12:21:28 +02003269static void checkpoint(void)
3270{
Anthony Liguoric227f092009-10-01 16:12:16 -05003271 assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
bellarda0464332005-12-18 18:29:50 +00003272 check1(vvv);
3273 check2(vvv);
3274 assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
bellarda0464332005-12-18 18:29:50 +00003275}
3276#endif