blob: a0f2335894ef68f915d98f4dbe9fc144e2d9ee05 [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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
bellardde167e42005-04-28 21:15:08 +000026#include <dirent.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010027#include "qapi/error.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/module.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010030#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020031#include "migration/blocker.h"
Kevin Wolf7ad9be62013-04-12 19:42:04 +020032#include "qapi/qmp/qbool.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010033#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#include "qemu/cutils.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070035#include "qemu/error-report.h"
bellardde167e42005-04-28 21:15:08 +000036
bellarda0464332005-12-18 18:29:50 +000037#ifndef S_IWGRP
38#define S_IWGRP 0
39#endif
40#ifndef S_IWOTH
41#define S_IWOTH 0
42#endif
bellardde167e42005-04-28 21:15:08 +000043
bellarda0464332005-12-18 18:29:50 +000044/* TODO: add ":bootsector=blabla.img:" */
45/* LATER TODO: add automatic boot sector generation from
46 BOOTEASY.ASM and Ranish Partition Manager
ths5fafdf22007-09-16 21:08:06 +000047 Note that DOS assumes the system files to be the first files in the
bellarda0464332005-12-18 18:29:50 +000048 file system (test if the boot sector still relies on that fact)! */
49/* MAYBE TODO: write block-visofs.c */
50/* TODO: call try_commit() only after a timeout */
bellardde167e42005-04-28 21:15:08 +000051
bellarda0464332005-12-18 18:29:50 +000052/* #define DEBUG */
53
54#ifdef DEBUG
55
56#define DLOG(a) a
57
blueswir13f47aa82008-03-09 06:59:01 +000058static void checkpoint(void);
bellarda0464332005-12-18 18:29:50 +000059
bellarda0464332005-12-18 18:29:50 +000060#else
61
62#define DLOG(a)
63
64#endif
bellardde167e42005-04-28 21:15:08 +000065
Hervé Poussineau63d261c2017-07-15 15:28:39 +020066/* bootsector OEM name. see related compatibility problems at:
67 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
68 * http://seasip.info/Misc/oemid.html
69 */
70#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
71
Hervé Poussineau8c4517f2017-07-15 15:28:38 +020072#define DIR_DELETED 0xe5
73#define DIR_KANJI DIR_DELETED
74#define DIR_KANJI_FAKE 0x05
75#define DIR_FREE 0x00
76
bellardde167e42005-04-28 21:15:08 +000077/* dynamic array functions */
Anthony Liguoric227f092009-10-01 16:12:16 -050078typedef struct array_t {
bellardde167e42005-04-28 21:15:08 +000079 char* pointer;
80 unsigned int size,next,item_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050081} array_t;
bellardde167e42005-04-28 21:15:08 +000082
Anthony Liguoric227f092009-10-01 16:12:16 -050083static inline void array_init(array_t* array,unsigned int item_size)
bellardde167e42005-04-28 21:15:08 +000084{
blueswir1511d2b12009-03-07 15:32:56 +000085 array->pointer = NULL;
bellardde167e42005-04-28 21:15:08 +000086 array->size=0;
87 array->next=0;
88 array->item_size=item_size;
89}
90
Anthony Liguoric227f092009-10-01 16:12:16 -050091static inline void array_free(array_t* array)
bellardde167e42005-04-28 21:15:08 +000092{
Stefan Weilce137822011-09-30 23:29:53 +020093 g_free(array->pointer);
bellardde167e42005-04-28 21:15:08 +000094 array->size=array->next=0;
95}
96
bellarda0464332005-12-18 18:29:50 +000097/* does not automatically grow */
Anthony Liguoric227f092009-10-01 16:12:16 -050098static inline void* array_get(array_t* array,unsigned int index) {
bellarda0464332005-12-18 18:29:50 +000099 assert(index < array->next);
100 return array->pointer + index * array->item_size;
101}
102
Anthony Liguoric227f092009-10-01 16:12:16 -0500103static inline int array_ensure_allocated(array_t* array, int index)
bellarda0464332005-12-18 18:29:50 +0000104{
105 if((index + 1) * array->item_size > array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200106 int new_size = (index + 32) * array->item_size;
107 array->pointer = g_realloc(array->pointer, new_size);
108 if (!array->pointer)
109 return -1;
Hervé Poussineauf80256b2017-07-15 15:28:41 +0200110 memset(array->pointer + array->size, 0, new_size - array->size);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200111 array->size = new_size;
112 array->next = index + 1;
bellardde167e42005-04-28 21:15:08 +0000113 }
bellarda0464332005-12-18 18:29:50 +0000114
115 return 0;
bellardde167e42005-04-28 21:15:08 +0000116}
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118static inline void* array_get_next(array_t* array) {
bellarda0464332005-12-18 18:29:50 +0000119 unsigned int next = array->next;
bellarda0464332005-12-18 18:29:50 +0000120
121 if (array_ensure_allocated(array, next) < 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200122 return NULL;
bellarda0464332005-12-18 18:29:50 +0000123
124 array->next = next + 1;
Eduardo Habkost9be38592016-06-13 18:57:58 -0300125 return array_get(array, next);
bellardde167e42005-04-28 21:15:08 +0000126}
127
Anthony Liguoric227f092009-10-01 16:12:16 -0500128static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
bellardde167e42005-04-28 21:15:08 +0000129 if((array->next+count)*array->item_size>array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200130 int increment=count*array->item_size;
131 array->pointer=g_realloc(array->pointer,array->size+increment);
132 if(!array->pointer)
blueswir1511d2b12009-03-07 15:32:56 +0000133 return NULL;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200134 array->size+=increment;
bellardde167e42005-04-28 21:15:08 +0000135 }
136 memmove(array->pointer+(index+count)*array->item_size,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200137 array->pointer+index*array->item_size,
138 (array->next-index)*array->item_size);
bellardde167e42005-04-28 21:15:08 +0000139 array->next+=count;
140 return array->pointer+index*array->item_size;
141}
142
143/* this performs a "roll", so that the element which was at index_from becomes
144 * index_to, but the order of all other elements is preserved. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500145static inline int array_roll(array_t* array,int index_to,int index_from,int count)
bellardde167e42005-04-28 21:15:08 +0000146{
147 char* buf;
148 char* from;
149 char* to;
150 int is;
151
152 if(!array ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200153 index_to<0 || index_to>=array->next ||
154 index_from<0 || index_from>=array->next)
155 return -1;
ths3b46e622007-09-17 08:09:54 +0000156
bellardde167e42005-04-28 21:15:08 +0000157 if(index_to==index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200158 return 0;
bellardde167e42005-04-28 21:15:08 +0000159
160 is=array->item_size;
161 from=array->pointer+index_from*is;
162 to=array->pointer+index_to*is;
Anthony Liguori7267c092011-08-20 22:09:37 -0500163 buf=g_malloc(is*count);
bellardde167e42005-04-28 21:15:08 +0000164 memcpy(buf,from,is*count);
165
166 if(index_to<index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200167 memmove(to+is*count,to,from-to);
bellardde167e42005-04-28 21:15:08 +0000168 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200169 memmove(from,from+is*count,to-from);
ths3b46e622007-09-17 08:09:54 +0000170
bellardde167e42005-04-28 21:15:08 +0000171 memcpy(to,buf,is*count);
172
Stefan Weilce137822011-09-30 23:29:53 +0200173 g_free(buf);
bellardde167e42005-04-28 21:15:08 +0000174
175 return 0;
176}
177
Anthony Liguoric227f092009-10-01 16:12:16 -0500178static inline int array_remove_slice(array_t* array,int index, int count)
bellarda0464332005-12-18 18:29:50 +0000179{
180 assert(index >=0);
181 assert(count > 0);
182 assert(index + count <= array->next);
183 if(array_roll(array,array->next-1,index,count))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200184 return -1;
bellarda0464332005-12-18 18:29:50 +0000185 array->next -= count;
186 return 0;
187}
188
Anthony Liguoric227f092009-10-01 16:12:16 -0500189static int array_remove(array_t* array,int index)
bellardde167e42005-04-28 21:15:08 +0000190{
bellarda0464332005-12-18 18:29:50 +0000191 return array_remove_slice(array, index, 1);
192}
193
194/* return the index for a given member */
Anthony Liguoric227f092009-10-01 16:12:16 -0500195static int array_index(array_t* array, void* pointer)
bellarda0464332005-12-18 18:29:50 +0000196{
197 size_t offset = (char*)pointer - array->pointer;
bellarda0464332005-12-18 18:29:50 +0000198 assert((offset % array->item_size) == 0);
199 assert(offset/array->item_size < array->next);
200 return offset/array->item_size;
bellardde167e42005-04-28 21:15:08 +0000201}
202
203/* These structures are used to fake a disk and the VFAT filesystem.
Stefan Weil541dc0d2011-08-31 12:38:01 +0200204 * For this reason we need to use QEMU_PACKED. */
bellardde167e42005-04-28 21:15:08 +0000205
Anthony Liguoric227f092009-10-01 16:12:16 -0500206typedef struct bootsector_t {
bellardde167e42005-04-28 21:15:08 +0000207 uint8_t jump[3];
208 uint8_t name[8];
209 uint16_t sector_size;
210 uint8_t sectors_per_cluster;
211 uint16_t reserved_sectors;
212 uint8_t number_of_fats;
213 uint16_t root_entries;
bellarda0464332005-12-18 18:29:50 +0000214 uint16_t total_sectors16;
bellardde167e42005-04-28 21:15:08 +0000215 uint8_t media_type;
216 uint16_t sectors_per_fat;
217 uint16_t sectors_per_track;
218 uint16_t number_of_heads;
219 uint32_t hidden_sectors;
220 uint32_t total_sectors;
221 union {
222 struct {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200223 uint8_t drive_number;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200224 uint8_t reserved1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200225 uint8_t signature;
226 uint32_t id;
227 uint8_t volume_label[11];
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200228 uint8_t fat_type[8];
229 uint8_t ignored[0x1c0];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200230 } QEMU_PACKED fat16;
231 struct {
232 uint32_t sectors_per_fat;
233 uint16_t flags;
234 uint8_t major,minor;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200235 uint32_t first_cluster_of_root_dir;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200236 uint16_t info_sector;
237 uint16_t backup_boot_sector;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200238 uint8_t reserved[12];
239 uint8_t drive_number;
240 uint8_t reserved1;
241 uint8_t signature;
242 uint32_t id;
243 uint8_t volume_label[11];
244 uint8_t fat_type[8];
245 uint8_t ignored[0x1a4];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200246 } QEMU_PACKED fat32;
bellardde167e42005-04-28 21:15:08 +0000247 } u;
bellardde167e42005-04-28 21:15:08 +0000248 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200249} QEMU_PACKED bootsector_t;
bellardde167e42005-04-28 21:15:08 +0000250
thsb5700942007-09-25 14:47:03 +0000251typedef struct {
252 uint8_t head;
253 uint8_t sector;
254 uint8_t cylinder;
Anthony Liguoric227f092009-10-01 16:12:16 -0500255} mbr_chs_t;
thsb5700942007-09-25 14:47:03 +0000256
Anthony Liguoric227f092009-10-01 16:12:16 -0500257typedef struct partition_t {
bellardde167e42005-04-28 21:15:08 +0000258 uint8_t attributes; /* 0x80 = bootable */
Anthony Liguoric227f092009-10-01 16:12:16 -0500259 mbr_chs_t start_CHS;
thsb5700942007-09-25 14:47:03 +0000260 uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
Anthony Liguoric227f092009-10-01 16:12:16 -0500261 mbr_chs_t end_CHS;
bellardde167e42005-04-28 21:15:08 +0000262 uint32_t start_sector_long;
thsb5700942007-09-25 14:47:03 +0000263 uint32_t length_sector_long;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200264} QEMU_PACKED partition_t;
bellardde167e42005-04-28 21:15:08 +0000265
Anthony Liguoric227f092009-10-01 16:12:16 -0500266typedef struct mbr_t {
thsb5700942007-09-25 14:47:03 +0000267 uint8_t ignored[0x1b8];
268 uint32_t nt_id;
269 uint8_t ignored2[2];
Anthony Liguoric227f092009-10-01 16:12:16 -0500270 partition_t partition[4];
bellardde167e42005-04-28 21:15:08 +0000271 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200272} QEMU_PACKED mbr_t;
bellardde167e42005-04-28 21:15:08 +0000273
Anthony Liguoric227f092009-10-01 16:12:16 -0500274typedef struct direntry_t {
Stefan Weilf671d172013-12-11 21:37:11 +0100275 uint8_t name[8 + 3];
bellardde167e42005-04-28 21:15:08 +0000276 uint8_t attributes;
277 uint8_t reserved[2];
278 uint16_t ctime;
279 uint16_t cdate;
280 uint16_t adate;
281 uint16_t begin_hi;
282 uint16_t mtime;
283 uint16_t mdate;
284 uint16_t begin;
285 uint32_t size;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200286} QEMU_PACKED direntry_t;
bellardde167e42005-04-28 21:15:08 +0000287
288/* this structure are used to transparently access the files */
289
Anthony Liguoric227f092009-10-01 16:12:16 -0500290typedef struct mapping_t {
bellarda0464332005-12-18 18:29:50 +0000291 /* begin is the first cluster, end is the last+1 */
292 uint32_t begin,end;
bellardde167e42005-04-28 21:15:08 +0000293 /* as s->directory is growable, no pointer may be used here */
294 unsigned int dir_index;
bellarda0464332005-12-18 18:29:50 +0000295 /* the clusters of a file may be in any order; this points to the first */
296 int first_mapping_index;
297 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200298 /* offset is
299 * - the offset in the file (in clusters) for a file, or
Hervé Poussineauad05b312017-05-22 23:11:56 +0200300 * - the next cluster of the directory for a directory
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200301 */
302 struct {
303 uint32_t offset;
304 } file;
305 struct {
306 int parent_mapping_index;
307 int first_dir_index;
308 } dir;
bellarda0464332005-12-18 18:29:50 +0000309 } info;
310 /* path contains the full path, i.e. it always starts with s->path */
311 char* path;
312
Hervé Poussineauad05b312017-05-22 23:11:56 +0200313 enum {
314 MODE_UNDEFINED = 0,
315 MODE_NORMAL = 1,
316 MODE_MODIFIED = 2,
317 MODE_DIRECTORY = 4,
318 MODE_DELETED = 8,
319 } mode;
bellarda0464332005-12-18 18:29:50 +0000320 int read_only;
Anthony Liguoric227f092009-10-01 16:12:16 -0500321} mapping_t;
bellardde167e42005-04-28 21:15:08 +0000322
bellarda0464332005-12-18 18:29:50 +0000323#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -0500324static void print_direntry(const struct direntry_t*);
325static void print_mapping(const struct mapping_t* mapping);
bellarda0464332005-12-18 18:29:50 +0000326#endif
bellardde167e42005-04-28 21:15:08 +0000327
328/* here begins the real VVFAT driver */
329
330typedef struct BDRVVVFATState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200331 CoMutex lock;
bellarda0464332005-12-18 18:29:50 +0000332 BlockDriverState* bs; /* pointer to parent */
bellardde167e42005-04-28 21:15:08 +0000333 unsigned char first_sectors[0x40*0x200];
ths3b46e622007-09-17 08:09:54 +0000334
bellardde167e42005-04-28 21:15:08 +0000335 int fat_type; /* 16 or 32 */
Anthony Liguoric227f092009-10-01 16:12:16 -0500336 array_t fat,directory,mapping;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200337 char volume_label[11];
ths3b46e622007-09-17 08:09:54 +0000338
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200339 uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
340
bellardde167e42005-04-28 21:15:08 +0000341 unsigned int cluster_size;
342 unsigned int sectors_per_cluster;
343 unsigned int sectors_per_fat;
bellarda0464332005-12-18 18:29:50 +0000344 uint32_t last_cluster_of_root_directory;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200345 /* how many entries are available in root directory (0 for FAT32) */
346 uint16_t root_entries;
bellardde167e42005-04-28 21:15:08 +0000347 uint32_t sector_count; /* total number of sectors of the partition */
348 uint32_t cluster_count; /* total number of clusters of this partition */
bellardde167e42005-04-28 21:15:08 +0000349 uint32_t max_fat_value;
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200350 uint32_t offset_to_fat;
351 uint32_t offset_to_root_dir;
ths3b46e622007-09-17 08:09:54 +0000352
bellardde167e42005-04-28 21:15:08 +0000353 int current_fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500354 mapping_t* current_mapping;
bellarda0464332005-12-18 18:29:50 +0000355 unsigned char* cluster; /* points to current cluster */
356 unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
bellardde167e42005-04-28 21:15:08 +0000357 unsigned int current_cluster;
358
359 /* write support */
bellarda0464332005-12-18 18:29:50 +0000360 char* qcow_filename;
Kevin Wolfeecc7742016-05-30 17:13:09 +0200361 BdrvChild* qcow;
bellarda0464332005-12-18 18:29:50 +0000362 void* fat2;
363 char* used_clusters;
Anthony Liguoric227f092009-10-01 16:12:16 -0500364 array_t commits;
bellarda0464332005-12-18 18:29:50 +0000365 const char* path;
366 int downcase_short_names;
Kevin Wolf3397f0c2011-11-22 16:52:13 +0100367
368 Error *migration_blocker;
bellardde167e42005-04-28 21:15:08 +0000369} BDRVVVFATState;
370
thsb5700942007-09-25 14:47:03 +0000371/* take the sector position spos and convert it to Cylinder/Head/Sector position
372 * if the position is outside the specified geometry, fill maximum value for CHS
373 * and return 1 to signal overflow.
374 */
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200375static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
376{
thsb5700942007-09-25 14:47:03 +0000377 int head,sector;
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200378 sector = spos % secs; spos /= secs;
379 head = spos % heads; spos /= heads;
380 if (spos >= cyls) {
thsb5700942007-09-25 14:47:03 +0000381 /* Overflow,
382 it happens if 32bit sector positions are used, while CHS is only 24bit.
383 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
384 chs->head = 0xFF;
385 chs->sector = 0xFF;
386 chs->cylinder = 0xFF;
387 return 1;
388 }
389 chs->head = (uint8_t)head;
390 chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
391 chs->cylinder = (uint8_t)spos;
392 return 0;
393}
bellardde167e42005-04-28 21:15:08 +0000394
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200395static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
bellardde167e42005-04-28 21:15:08 +0000396{
397 /* TODO: if the files mbr.img and bootsect.img exist, use them */
Anthony Liguoric227f092009-10-01 16:12:16 -0500398 mbr_t* real_mbr=(mbr_t*)s->first_sectors;
399 partition_t* partition = &(real_mbr->partition[0]);
thsb5700942007-09-25 14:47:03 +0000400 int lba;
bellardde167e42005-04-28 21:15:08 +0000401
402 memset(s->first_sectors,0,512);
ths3b46e622007-09-17 08:09:54 +0000403
thsb5700942007-09-25 14:47:03 +0000404 /* Win NT Disk Signature */
405 real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
406
bellardde167e42005-04-28 21:15:08 +0000407 partition->attributes=0x80; /* bootable */
thsb5700942007-09-25 14:47:03 +0000408
409 /* LBA is used when partition is outside the CHS geometry */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200410 lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200411 cyls, heads, secs);
412 lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
413 cyls, heads, secs);
thsb5700942007-09-25 14:47:03 +0000414
415 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200416 partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
Markus Armbrusterf91cbef2012-07-10 11:12:28 +0200417 partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200418 - s->offset_to_bootsector);
thsb5700942007-09-25 14:47:03 +0000419
bellarda0464332005-12-18 18:29:50 +0000420 /* FAT12/FAT16/FAT32 */
thsb5700942007-09-25 14:47:03 +0000421 /* DOS uses different types when partition is LBA,
422 probably to prevent older versions from using CHS on them */
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200423 partition->fs_type = s->fat_type == 12 ? 0x1 :
424 s->fat_type == 16 ? (lba ? 0xe : 0x06) :
425 /*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
bellardde167e42005-04-28 21:15:08 +0000426
427 real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
428}
429
bellarda0464332005-12-18 18:29:50 +0000430/* direntry functions */
431
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200432static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
bellardde167e42005-04-28 21:15:08 +0000433{
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200434 int number_of_entries, i;
435 glong length;
436 direntry_t *entry;
bellardde167e42005-04-28 21:15:08 +0000437
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200438 gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
439 if (!longname) {
440 fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
441 return NULL;
442 }
443
Marc-André Lureau78ee96d2017-06-22 13:04:16 +0200444 number_of_entries = DIV_ROUND_UP(length * 2, 26);
bellardde167e42005-04-28 21:15:08 +0000445
446 for(i=0;i<number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200447 entry=array_get_next(&(s->directory));
448 entry->attributes=0xf;
449 entry->reserved[0]=0;
450 entry->begin=0;
451 entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
bellardde167e42005-04-28 21:15:08 +0000452 }
balrog1e080d52007-12-24 13:26:04 +0000453 for(i=0;i<26*number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200454 int offset=(i%26);
455 if(offset<10) offset=1+offset;
456 else if(offset<22) offset=14+offset-10;
457 else offset=28+offset-22;
458 entry=array_get(&(s->directory),s->directory.next-1-(i/26));
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200459 if (i >= 2 * length + 2) {
460 entry->name[offset] = 0xff;
461 } else if (i % 2 == 0) {
462 entry->name[offset] = longname[i / 2] & 0xff;
463 } else {
464 entry->name[offset] = longname[i / 2] >> 8;
465 }
bellardde167e42005-04-28 21:15:08 +0000466 }
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200467 g_free(longname);
bellardde167e42005-04-28 21:15:08 +0000468 return array_get(&(s->directory),s->directory.next-number_of_entries);
469}
470
Anthony Liguoric227f092009-10-01 16:12:16 -0500471static char is_free(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000472{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200473 return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
bellarda0464332005-12-18 18:29:50 +0000474}
475
Anthony Liguoric227f092009-10-01 16:12:16 -0500476static char is_volume_label(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000477{
478 return direntry->attributes == 0x28;
479}
480
Anthony Liguoric227f092009-10-01 16:12:16 -0500481static char is_long_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000482{
483 return direntry->attributes == 0xf;
484}
485
Anthony Liguoric227f092009-10-01 16:12:16 -0500486static char is_short_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000487{
488 return !is_volume_label(direntry) && !is_long_name(direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200489 && !is_free(direntry);
bellarda0464332005-12-18 18:29:50 +0000490}
491
Anthony Liguoric227f092009-10-01 16:12:16 -0500492static char is_directory(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000493{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200494 return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
bellarda0464332005-12-18 18:29:50 +0000495}
496
Anthony Liguoric227f092009-10-01 16:12:16 -0500497static inline char is_dot(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000498{
499 return is_short_name(direntry) && direntry->name[0] == '.';
500}
501
Anthony Liguoric227f092009-10-01 16:12:16 -0500502static char is_file(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000503{
504 return is_short_name(direntry) && !is_directory(direntry);
505}
506
Anthony Liguoric227f092009-10-01 16:12:16 -0500507static inline uint32_t begin_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000508{
509 return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
510}
511
Anthony Liguoric227f092009-10-01 16:12:16 -0500512static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000513{
514 return le32_to_cpu(direntry->size);
515}
516
Anthony Liguoric227f092009-10-01 16:12:16 -0500517static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
bellarda0464332005-12-18 18:29:50 +0000518{
519 direntry->begin = cpu_to_le16(begin & 0xffff);
520 direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
521}
522
Hervé Poussineau0c361112017-05-22 23:12:01 +0200523static uint8_t to_valid_short_char(gunichar c)
524{
525 c = g_unichar_toupper(c);
526 if ((c >= '0' && c <= '9') ||
527 (c >= 'A' && c <= 'Z') ||
528 strchr("$%'-_@~`!(){}^#&", c) != 0) {
529 return c;
530 } else {
531 return 0;
532 }
533}
534
535static direntry_t *create_short_filename(BDRVVVFATState *s,
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200536 const char *filename,
537 unsigned int directory_start)
Hervé Poussineau0c361112017-05-22 23:12:01 +0200538{
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200539 int i, j = 0;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200540 direntry_t *entry = array_get_next(&(s->directory));
541 const gchar *p, *last_dot = NULL;
542 gunichar c;
543 bool lossy_conversion = false;
Max Reitz7c8730d2017-07-17 17:12:07 +0200544 char tail[8];
Hervé Poussineau0c361112017-05-22 23:12:01 +0200545
546 if (!entry) {
547 return NULL;
548 }
549 memset(entry->name, 0x20, sizeof(entry->name));
550
551 /* copy filename and search last dot */
552 for (p = filename; ; p = g_utf8_next_char(p)) {
553 c = g_utf8_get_char(p);
554 if (c == '\0') {
555 break;
556 } else if (c == '.') {
557 if (j == 0) {
558 /* '.' at start of filename */
559 lossy_conversion = true;
560 } else {
561 if (last_dot) {
562 lossy_conversion = true;
563 }
564 last_dot = p;
565 }
566 } else if (!last_dot) {
567 /* first part of the name; copy it */
568 uint8_t v = to_valid_short_char(c);
569 if (j < 8 && v) {
570 entry->name[j++] = v;
571 } else {
572 lossy_conversion = true;
573 }
574 }
575 }
576
577 /* copy extension (if any) */
578 if (last_dot) {
579 j = 0;
580 for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
581 c = g_utf8_get_char(p);
582 if (c == '\0') {
583 break;
584 } else {
585 /* extension; copy it */
586 uint8_t v = to_valid_short_char(c);
587 if (j < 3 && v) {
588 entry->name[8 + (j++)] = v;
589 } else {
590 lossy_conversion = true;
591 }
592 }
593 }
594 }
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200595
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200596 if (entry->name[0] == DIR_KANJI) {
597 entry->name[0] = DIR_KANJI_FAKE;
Hervé Poussineau78f002c2017-05-22 23:12:04 +0200598 }
599
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200600 /* numeric-tail generation */
601 for (j = 0; j < 8; j++) {
602 if (entry->name[j] == ' ') {
603 break;
604 }
605 }
606 for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
607 direntry_t *entry1;
608 if (i > 0) {
Max Reitz7c8730d2017-07-17 17:12:07 +0200609 int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
610 assert(len <= 7);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200611 memcpy(entry->name + MIN(j, 8 - len), tail, len);
612 }
613 for (entry1 = array_get(&(s->directory), directory_start);
614 entry1 < entry; entry1++) {
615 if (!is_long_name(entry1) &&
616 !memcmp(entry1->name, entry->name, 11)) {
617 break; /* found dupe */
618 }
619 }
620 if (entry1 == entry) {
621 /* no dupe found */
622 return entry;
623 }
624 }
625 return NULL;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200626}
627
bellardde167e42005-04-28 21:15:08 +0000628/* fat functions */
629
Anthony Liguoric227f092009-10-01 16:12:16 -0500630static inline uint8_t fat_chksum(const direntry_t* entry)
bellardde167e42005-04-28 21:15:08 +0000631{
632 uint8_t chksum=0;
633 int i;
634
Stefan Weilf671d172013-12-11 21:37:11 +0100635 for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
636 chksum = (((chksum & 0xfe) >> 1) |
637 ((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
Aurelien Jarno5606c222009-04-25 00:08:05 +0200638 }
ths3b46e622007-09-17 08:09:54 +0000639
bellardde167e42005-04-28 21:15:08 +0000640 return chksum;
641}
642
643/* if return_time==0, this returns the fat_date, else the fat_time */
644static uint16_t fat_datetime(time_t time,int return_time) {
645 struct tm* t;
bellardde167e42005-04-28 21:15:08 +0000646 struct tm t1;
Michael S. Tsirkin6ab00ce2009-09-30 19:43:31 +0200647 t = &t1;
bellardde167e42005-04-28 21:15:08 +0000648 localtime_r(&time,t);
bellardde167e42005-04-28 21:15:08 +0000649 if(return_time)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200650 return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
bellardde167e42005-04-28 21:15:08 +0000651 return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
652}
653
654static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
655{
bellarda0464332005-12-18 18:29:50 +0000656 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200657 uint32_t* entry=array_get(&(s->fat),cluster);
658 *entry=cpu_to_le32(value);
bellardde167e42005-04-28 21:15:08 +0000659 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200660 uint16_t* entry=array_get(&(s->fat),cluster);
661 *entry=cpu_to_le16(value&0xffff);
bellardde167e42005-04-28 21:15:08 +0000662 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200663 int offset = (cluster*3/2);
664 unsigned char* p = array_get(&(s->fat), offset);
bellarda0464332005-12-18 18:29:50 +0000665 switch (cluster&1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200666 case 0:
667 p[0] = value&0xff;
668 p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
669 break;
670 case 1:
671 p[0] = (p[0]&0xf) | ((value&0xf)<<4);
672 p[1] = (value>>4);
673 break;
674 }
bellardde167e42005-04-28 21:15:08 +0000675 }
676}
677
678static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
679{
bellarda0464332005-12-18 18:29:50 +0000680 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200681 uint32_t* entry=array_get(&(s->fat),cluster);
682 return le32_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000683 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200684 uint16_t* entry=array_get(&(s->fat),cluster);
685 return le16_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000686 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200687 const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
688 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
bellardde167e42005-04-28 21:15:08 +0000689 }
690}
691
692static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
693{
694 if(fat_entry>s->max_fat_value-8)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200695 return -1;
bellardde167e42005-04-28 21:15:08 +0000696 return 0;
697}
698
699static inline void init_fat(BDRVVVFATState* s)
700{
bellarda0464332005-12-18 18:29:50 +0000701 if (s->fat_type == 12) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200702 array_init(&(s->fat),1);
703 array_ensure_allocated(&(s->fat),
704 s->sectors_per_fat * 0x200 * 3 / 2 - 1);
bellarda0464332005-12-18 18:29:50 +0000705 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200706 array_init(&(s->fat),(s->fat_type==32?4:2));
707 array_ensure_allocated(&(s->fat),
708 s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
bellarda0464332005-12-18 18:29:50 +0000709 }
bellardde167e42005-04-28 21:15:08 +0000710 memset(s->fat.pointer,0,s->fat.size);
ths3b46e622007-09-17 08:09:54 +0000711
bellardde167e42005-04-28 21:15:08 +0000712 switch(s->fat_type) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200713 case 12: s->max_fat_value=0xfff; break;
714 case 16: s->max_fat_value=0xffff; break;
715 case 32: s->max_fat_value=0x0fffffff; break;
716 default: s->max_fat_value=0; /* error... */
bellardde167e42005-04-28 21:15:08 +0000717 }
718
719}
720
Anthony Liguoric227f092009-10-01 16:12:16 -0500721static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200722 unsigned int directory_start, const char* filename, int is_dot)
bellardde167e42005-04-28 21:15:08 +0000723{
Hervé Poussineau0c361112017-05-22 23:12:01 +0200724 int long_index = s->directory.next;
Anthony Liguoric227f092009-10-01 16:12:16 -0500725 direntry_t* entry = NULL;
726 direntry_t* entry_long = NULL;
bellardde167e42005-04-28 21:15:08 +0000727
728 if(is_dot) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200729 entry=array_get_next(&(s->directory));
Stefan Weilf671d172013-12-11 21:37:11 +0100730 memset(entry->name, 0x20, sizeof(entry->name));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200731 memcpy(entry->name,filename,strlen(filename));
732 return entry;
bellardde167e42005-04-28 21:15:08 +0000733 }
ths3b46e622007-09-17 08:09:54 +0000734
bellardde167e42005-04-28 21:15:08 +0000735 entry_long=create_long_filename(s,filename);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200736 entry = create_short_filename(s, filename, directory_start);
bellardde167e42005-04-28 21:15:08 +0000737
738 /* calculate checksum; propagate to long name */
739 if(entry_long) {
740 uint8_t chksum=fat_chksum(entry);
741
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200742 /* calculate anew, because realloc could have taken place */
743 entry_long=array_get(&(s->directory),long_index);
744 while(entry_long<entry && is_long_name(entry_long)) {
745 entry_long->reserved[1]=chksum;
746 entry_long++;
747 }
bellardde167e42005-04-28 21:15:08 +0000748 }
749
750 return entry;
751}
752
bellarda0464332005-12-18 18:29:50 +0000753/*
754 * Read a directory. (the index of the corresponding mapping must be passed).
755 */
756static int read_directory(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +0000757{
Anthony Liguoric227f092009-10-01 16:12:16 -0500758 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
759 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000760 const char* dirname = mapping->path;
761 int first_cluster = mapping->begin;
762 int parent_index = mapping->info.dir.parent_mapping_index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500763 mapping_t* parent_mapping = (mapping_t*)
blueswir1511d2b12009-03-07 15:32:56 +0000764 (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
bellarda0464332005-12-18 18:29:50 +0000765 int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
bellardde167e42005-04-28 21:15:08 +0000766
767 DIR* dir=opendir(dirname);
768 struct dirent* entry;
bellardde167e42005-04-28 21:15:08 +0000769 int i;
770
bellarda0464332005-12-18 18:29:50 +0000771 assert(mapping->mode & MODE_DIRECTORY);
772
773 if(!dir) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200774 mapping->end = mapping->begin;
775 return -1;
bellarda0464332005-12-18 18:29:50 +0000776 }
ths3b46e622007-09-17 08:09:54 +0000777
bellarda0464332005-12-18 18:29:50 +0000778 i = mapping->info.dir.first_dir_index =
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200779 first_cluster == 0 ? 0 : s->directory.next;
bellarda0464332005-12-18 18:29:50 +0000780
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200781 if (first_cluster != 0) {
782 /* create the top entries of a subdirectory */
783 (void)create_short_and_long_name(s, i, ".", 1);
784 (void)create_short_and_long_name(s, i, "..", 1);
785 }
786
ths5fafdf22007-09-16 21:08:06 +0000787 /* actually read the directory, and allocate the mappings */
bellardde167e42005-04-28 21:15:08 +0000788 while((entry=readdir(dir))) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200789 unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000790 char* buffer;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200791 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000792 struct stat st;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200793 int is_dot=!strcmp(entry->d_name,".");
794 int is_dotdot=!strcmp(entry->d_name,"..");
bellardde167e42005-04-28 21:15:08 +0000795
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200796 if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
797 fprintf(stderr, "Too many entries in root directory\n");
798 closedir(dir);
799 return -2;
800 }
801
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200802 if(first_cluster == 0 && (is_dotdot || is_dot))
803 continue;
ths5fafdf22007-09-16 21:08:06 +0000804
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200805 buffer = g_malloc(length);
806 snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000807
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200808 if(stat(buffer,&st)<0) {
Stefan Weilce137822011-09-30 23:29:53 +0200809 g_free(buffer);
bellardde167e42005-04-28 21:15:08 +0000810 continue;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200811 }
bellardde167e42005-04-28 21:15:08 +0000812
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200813 /* create directory entry for this file */
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200814 if (!is_dot && !is_dotdot) {
815 direntry = create_short_and_long_name(s, i, entry->d_name, 0);
816 } else {
817 direntry = array_get(&(s->directory), is_dot ? i : i + 1);
818 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200819 direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
820 direntry->reserved[0]=direntry->reserved[1]=0;
821 direntry->ctime=fat_datetime(st.st_ctime,1);
822 direntry->cdate=fat_datetime(st.st_ctime,0);
823 direntry->adate=fat_datetime(st.st_atime,0);
824 direntry->begin_hi=0;
825 direntry->mtime=fat_datetime(st.st_mtime,1);
826 direntry->mdate=fat_datetime(st.st_mtime,0);
827 if(is_dotdot)
828 set_begin_of_direntry(direntry, first_cluster_of_parent);
829 else if(is_dot)
830 set_begin_of_direntry(direntry, first_cluster);
831 else
832 direntry->begin=0; /* do that later */
bellarda0464332005-12-18 18:29:50 +0000833 if (st.st_size > 0x7fffffff) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200834 fprintf(stderr, "File %s is larger than 2GB\n", buffer);
Stefan Weilce137822011-09-30 23:29:53 +0200835 g_free(buffer);
Blue Swirl08089ed2011-01-12 19:48:58 +0000836 closedir(dir);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200837 return -2;
bellarda0464332005-12-18 18:29:50 +0000838 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200839 direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
bellardde167e42005-04-28 21:15:08 +0000840
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200841 /* create mapping for this file */
842 if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
843 s->current_mapping = array_get_next(&(s->mapping));
844 s->current_mapping->begin=0;
845 s->current_mapping->end=st.st_size;
846 /*
847 * we get the direntry of the most recent direntry, which
848 * contains the short name and all the relevant information.
849 */
850 s->current_mapping->dir_index=s->directory.next-1;
851 s->current_mapping->first_mapping_index = -1;
852 if (S_ISDIR(st.st_mode)) {
853 s->current_mapping->mode = MODE_DIRECTORY;
854 s->current_mapping->info.dir.parent_mapping_index =
855 mapping_index;
856 } else {
857 s->current_mapping->mode = MODE_UNDEFINED;
858 s->current_mapping->info.file.offset = 0;
859 }
860 s->current_mapping->path=buffer;
861 s->current_mapping->read_only =
862 (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
Markus Armbrusterb122c3b2014-05-28 11:17:05 +0200863 } else {
864 g_free(buffer);
865 }
bellardde167e42005-04-28 21:15:08 +0000866 }
867 closedir(dir);
868
869 /* fill with zeroes up to the end of the cluster */
870 while(s->directory.next%(0x10*s->sectors_per_cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200871 direntry_t* direntry=array_get_next(&(s->directory));
872 memset(direntry,0,sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000873 }
874
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200875 if (s->fat_type != 32 &&
876 mapping_index == 0 &&
877 s->directory.next < s->root_entries) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200878 /* root directory */
879 int cur = s->directory.next;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200880 array_ensure_allocated(&(s->directory), s->root_entries - 1);
881 s->directory.next = s->root_entries;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200882 memset(array_get(&(s->directory), cur), 0,
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200883 (s->root_entries - cur) * sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000884 }
ths5fafdf22007-09-16 21:08:06 +0000885
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200886 /* re-get the mapping, since s->mapping was possibly realloc()ed */
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200887 mapping = array_get(&(s->mapping), mapping_index);
bellarda0464332005-12-18 18:29:50 +0000888 first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200889 * 0x20 / s->cluster_size;
bellarda0464332005-12-18 18:29:50 +0000890 mapping->end = first_cluster;
bellardde167e42005-04-28 21:15:08 +0000891
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200892 direntry = array_get(&(s->directory), mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +0000893 set_begin_of_direntry(direntry, mapping->begin);
ths3b46e622007-09-17 08:09:54 +0000894
bellardde167e42005-04-28 21:15:08 +0000895 return 0;
896}
897
bellarda0464332005-12-18 18:29:50 +0000898static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
bellardde167e42005-04-28 21:15:08 +0000899{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200900 return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000901}
902
903static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
904{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200905 return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
bellarda0464332005-12-18 18:29:50 +0000906}
907
bellarda0464332005-12-18 18:29:50 +0000908static int init_directories(BDRVVVFATState* s,
Markus Armbrusterd11c8912014-05-16 11:00:18 +0200909 const char *dirname, int heads, int secs,
910 Error **errp)
bellarda0464332005-12-18 18:29:50 +0000911{
Anthony Liguoric227f092009-10-01 16:12:16 -0500912 bootsector_t* bootsector;
913 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +0000914 unsigned int i;
915 unsigned int cluster;
916
917 memset(&(s->first_sectors[0]),0,0x40*0x200);
918
bellardde167e42005-04-28 21:15:08 +0000919 s->cluster_size=s->sectors_per_cluster*0x200;
Anthony Liguori7267c092011-08-20 22:09:37 -0500920 s->cluster_buffer=g_malloc(s->cluster_size);
bellarda0464332005-12-18 18:29:50 +0000921
922 /*
923 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
924 * where sc is sector_count,
925 * spf is sectors_per_fat,
926 * spc is sectors_per_clusters, and
927 * fat_type = 12, 16 or 32.
928 */
929 i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
930 s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
ths3b46e622007-09-17 08:09:54 +0000931
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200932 s->offset_to_fat = s->offset_to_bootsector + 1;
933 s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
934
Anthony Liguoric227f092009-10-01 16:12:16 -0500935 array_init(&(s->mapping),sizeof(mapping_t));
936 array_init(&(s->directory),sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000937
938 /* add volume label */
939 {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200940 direntry_t* entry=array_get_next(&(s->directory));
941 entry->attributes=0x28; /* archive | volume label */
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200942 memcpy(entry->name, s->volume_label, sizeof(entry->name));
bellardde167e42005-04-28 21:15:08 +0000943 }
944
bellardde167e42005-04-28 21:15:08 +0000945 /* Now build FAT, and write back information into directory */
946 init_fat(s);
947
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200948 /* TODO: if there are more entries, bootsector has to be adjusted! */
949 s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000950 s->cluster_count=sector2cluster(s, s->sector_count);
bellardde167e42005-04-28 21:15:08 +0000951
bellarda0464332005-12-18 18:29:50 +0000952 mapping = array_get_next(&(s->mapping));
953 mapping->begin = 0;
954 mapping->dir_index = 0;
955 mapping->info.dir.parent_mapping_index = -1;
956 mapping->first_mapping_index = -1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500957 mapping->path = g_strdup(dirname);
bellarda0464332005-12-18 18:29:50 +0000958 i = strlen(mapping->path);
959 if (i > 0 && mapping->path[i - 1] == '/')
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200960 mapping->path[i - 1] = '\0';
bellarda0464332005-12-18 18:29:50 +0000961 mapping->mode = MODE_DIRECTORY;
962 mapping->read_only = 0;
963 s->path = mapping->path;
bellardde167e42005-04-28 21:15:08 +0000964
bellarda0464332005-12-18 18:29:50 +0000965 for (i = 0, cluster = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200966 /* MS-DOS expects the FAT to be 0 for the root directory
967 * (except for the media byte). */
968 /* LATER TODO: still true for FAT32? */
969 int fix_fat = (i != 0);
970 mapping = array_get(&(s->mapping), i);
bellardde167e42005-04-28 21:15:08 +0000971
bellarda0464332005-12-18 18:29:50 +0000972 if (mapping->mode & MODE_DIRECTORY) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200973 mapping->begin = cluster;
974 if(read_directory(s, i)) {
Markus Armbrusterd11c8912014-05-16 11:00:18 +0200975 error_setg(errp, "Could not read directory %s",
976 mapping->path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200977 return -1;
978 }
979 mapping = array_get(&(s->mapping), i);
980 } else {
981 assert(mapping->mode == MODE_UNDEFINED);
982 mapping->mode=MODE_NORMAL;
983 mapping->begin = cluster;
984 if (mapping->end > 0) {
985 direntry_t* direntry = array_get(&(s->directory),
986 mapping->dir_index);
bellardde167e42005-04-28 21:15:08 +0000987
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200988 mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
989 set_begin_of_direntry(direntry, mapping->begin);
990 } else {
991 mapping->end = cluster + 1;
992 fix_fat = 0;
993 }
994 }
bellarda0464332005-12-18 18:29:50 +0000995
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200996 assert(mapping->begin < mapping->end);
bellarda0464332005-12-18 18:29:50 +0000997
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200998 /* next free cluster */
999 cluster = mapping->end;
bellarda0464332005-12-18 18:29:50 +00001000
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001001 if(cluster > s->cluster_count) {
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001002 error_setg(errp,
1003 "Directory does not fit in FAT%d (capacity %.2f MB)",
1004 s->fat_type, s->sector_count / 2000.0);
1005 return -1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001006 }
balrog8ce0f862008-11-10 01:34:27 +00001007
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001008 /* fix fat for entry */
1009 if (fix_fat) {
1010 int j;
1011 for(j = mapping->begin; j < mapping->end - 1; j++)
1012 fat_set(s, j, j+1);
1013 fat_set(s, mapping->end - 1, s->max_fat_value);
1014 }
bellardde167e42005-04-28 21:15:08 +00001015 }
1016
bellarda0464332005-12-18 18:29:50 +00001017 mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00001018 s->last_cluster_of_root_directory = mapping->end;
bellardde167e42005-04-28 21:15:08 +00001019
bellarda0464332005-12-18 18:29:50 +00001020 /* the FAT signature */
1021 fat_set(s,0,s->max_fat_value);
1022 fat_set(s,1,s->max_fat_value);
1023
1024 s->current_mapping = NULL;
1025
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001026 bootsector = (bootsector_t *)(s->first_sectors
1027 + s->offset_to_bootsector * 0x200);
bellardde167e42005-04-28 21:15:08 +00001028 bootsector->jump[0]=0xeb;
1029 bootsector->jump[1]=0x3e;
1030 bootsector->jump[2]=0x90;
Hervé Poussineau63d261c2017-07-15 15:28:39 +02001031 memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
bellardde167e42005-04-28 21:15:08 +00001032 bootsector->sector_size=cpu_to_le16(0x200);
1033 bootsector->sectors_per_cluster=s->sectors_per_cluster;
1034 bootsector->reserved_sectors=cpu_to_le16(1);
1035 bootsector->number_of_fats=0x2; /* number of FATs */
Hervé Poussineau6817efe2017-05-22 23:12:03 +02001036 bootsector->root_entries = cpu_to_le16(s->root_entries);
bellarda0464332005-12-18 18:29:50 +00001037 bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001038 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1039 bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
bellarda0464332005-12-18 18:29:50 +00001040 s->fat.pointer[0] = bootsector->media_type;
bellardde167e42005-04-28 21:15:08 +00001041 bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001042 bootsector->sectors_per_track = cpu_to_le16(secs);
1043 bootsector->number_of_heads = cpu_to_le16(heads);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001044 bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
bellarda0464332005-12-18 18:29:50 +00001045 bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
bellardde167e42005-04-28 21:15:08 +00001046
bellarda0464332005-12-18 18:29:50 +00001047 /* LATER TODO: if FAT32, this is wrong */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001048 /* drive_number: fda=0, hda=0x80 */
1049 bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
bellardde167e42005-04-28 21:15:08 +00001050 bootsector->u.fat16.signature=0x29;
1051 bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
1052
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001053 memcpy(bootsector->u.fat16.volume_label, s->volume_label,
1054 sizeof(bootsector->u.fat16.volume_label));
Hervé Poussineau92e28d82017-05-22 23:11:58 +02001055 memcpy(bootsector->u.fat16.fat_type,
1056 s->fat_type == 12 ? "FAT12 " : "FAT16 ", 8);
bellardde167e42005-04-28 21:15:08 +00001057 bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
1058
1059 return 0;
1060}
1061
bellard83f64092006-08-01 16:21:11 +00001062#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001063static BDRVVVFATState *vvv = NULL;
bellard83f64092006-08-01 16:21:11 +00001064#endif
bellarda0464332005-12-18 18:29:50 +00001065
Kevin Wolfeecc7742016-05-30 17:13:09 +02001066static int enable_write_target(BlockDriverState *bs, Error **errp);
bellarda0464332005-12-18 18:29:50 +00001067static int is_consistent(BDRVVVFATState *s);
1068
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001069static QemuOptsList runtime_opts = {
1070 .name = "vvfat",
1071 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1072 .desc = {
1073 {
1074 .name = "dir",
1075 .type = QEMU_OPT_STRING,
1076 .help = "Host directory to map to the vvfat device",
1077 },
1078 {
1079 .name = "fat-type",
1080 .type = QEMU_OPT_NUMBER,
1081 .help = "FAT type (12, 16 or 32)",
1082 },
1083 {
1084 .name = "floppy",
1085 .type = QEMU_OPT_BOOL,
1086 .help = "Create a floppy rather than a hard disk image",
1087 },
1088 {
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001089 .name = "label",
1090 .type = QEMU_OPT_STRING,
1091 .help = "Use a volume label other than QEMU VVFAT",
1092 },
1093 {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001094 .name = "rw",
1095 .type = QEMU_OPT_BOOL,
1096 .help = "Make the image writable",
1097 },
1098 { /* end of list */ }
1099 },
1100};
1101
1102static void vvfat_parse_filename(const char *filename, QDict *options,
1103 Error **errp)
1104{
1105 int fat_type = 0;
1106 bool floppy = false;
1107 bool rw = false;
1108 int i;
1109
1110 if (!strstart(filename, "fat:", NULL)) {
1111 error_setg(errp, "File name string must start with 'fat:'");
1112 return;
1113 }
1114
1115 /* Parse options */
1116 if (strstr(filename, ":32:")) {
1117 fat_type = 32;
1118 } else if (strstr(filename, ":16:")) {
1119 fat_type = 16;
1120 } else if (strstr(filename, ":12:")) {
1121 fat_type = 12;
1122 }
1123
1124 if (strstr(filename, ":floppy:")) {
1125 floppy = true;
1126 }
1127
1128 if (strstr(filename, ":rw:")) {
1129 rw = true;
1130 }
1131
1132 /* Get the directory name without options */
1133 i = strrchr(filename, ':') - filename;
1134 assert(i >= 3);
1135 if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
1136 /* workaround for DOS drive names */
1137 filename += i - 1;
1138 } else {
1139 filename += i + 1;
1140 }
1141
1142 /* Fill in the options QDict */
Eric Blake46f5ac22017-04-27 16:58:17 -05001143 qdict_put_str(options, "dir", filename);
1144 qdict_put_int(options, "fat-type", fat_type);
1145 qdict_put_bool(options, "floppy", floppy);
1146 qdict_put_bool(options, "rw", rw);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001147}
1148
Max Reitz015a1032013-09-05 14:22:29 +02001149static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
1150 Error **errp)
bellardde167e42005-04-28 21:15:08 +00001151{
1152 BDRVVVFATState *s = bs->opaque;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001153 int cyls, heads, secs;
1154 bool floppy;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001155 const char *dirname, *label;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001156 QemuOpts *opts;
1157 Error *local_err = NULL;
1158 int ret;
bellardde167e42005-04-28 21:15:08 +00001159
bellard83f64092006-08-01 16:21:11 +00001160#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001161 vvv = s;
bellard83f64092006-08-01 16:21:11 +00001162#endif
bellarda0464332005-12-18 18:29:50 +00001163
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001164 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001165 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001166 if (local_err) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001167 error_propagate(errp, local_err);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001168 ret = -EINVAL;
1169 goto fail;
1170 }
1171
1172 dirname = qemu_opt_get(opts, "dir");
1173 if (!dirname) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001174 error_setg(errp, "vvfat block driver requires a 'dir' option");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001175 ret = -EINVAL;
1176 goto fail;
1177 }
1178
1179 s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
1180 floppy = qemu_opt_get_bool(opts, "floppy", false);
1181
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001182 memset(s->volume_label, ' ', sizeof(s->volume_label));
1183 label = qemu_opt_get(opts, "label");
1184 if (label) {
1185 size_t label_length = strlen(label);
1186 if (label_length > 11) {
1187 error_setg(errp, "vvfat label cannot be longer than 11 bytes");
1188 ret = -EINVAL;
1189 goto fail;
1190 }
1191 memcpy(s->volume_label, label, label_length);
Kevin Wolfd208c502016-04-27 14:18:16 +02001192 } else {
1193 memcpy(s->volume_label, "QEMU VVFAT", 10);
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001194 }
1195
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001196 if (floppy) {
1197 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1198 if (!s->fat_type) {
1199 s->fat_type = 12;
1200 secs = 36;
1201 s->sectors_per_cluster = 2;
1202 } else {
1203 secs = s->fat_type == 12 ? 18 : 36;
1204 s->sectors_per_cluster = 1;
1205 }
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001206 cyls = 80;
1207 heads = 2;
1208 } else {
1209 /* 32MB or 504MB disk*/
1210 if (!s->fat_type) {
1211 s->fat_type = 16;
1212 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001213 s->offset_to_bootsector = 0x3f;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001214 cyls = s->fat_type == 12 ? 64 : 1024;
1215 heads = 16;
1216 secs = 63;
1217 }
1218
1219 switch (s->fat_type) {
1220 case 32:
Alistair Francisb62e39b2017-09-11 12:52:56 -07001221 warn_report("FAT32 has not been tested. You are welcome to do so!");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001222 break;
1223 case 16:
1224 case 12:
1225 break;
1226 default:
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001227 error_setg(errp, "Valid FAT types are only 12, 16 and 32");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001228 ret = -EINVAL;
1229 goto fail;
1230 }
1231
1232
bellarda0464332005-12-18 18:29:50 +00001233 s->bs = bs;
1234
bellarda0464332005-12-18 18:29:50 +00001235 /* LATER TODO: if FAT32, adjust */
bellarda0464332005-12-18 18:29:50 +00001236 s->sectors_per_cluster=0x10;
bellardde167e42005-04-28 21:15:08 +00001237
1238 s->current_cluster=0xffffffff;
bellardde167e42005-04-28 21:15:08 +00001239
Kevin Wolfeecc7742016-05-30 17:13:09 +02001240 s->qcow = NULL;
bellarda0464332005-12-18 18:29:50 +00001241 s->qcow_filename = NULL;
1242 s->fat2 = NULL;
1243 s->downcase_short_names = 1;
ths3b46e622007-09-17 08:09:54 +00001244
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001245 fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
1246 dirname, cyls, heads, secs);
bellarda0464332005-12-18 18:29:50 +00001247
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001248 s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
Paolo Bonzini5a742b52011-10-05 09:12:06 +02001249
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001250 if (qemu_opt_get_bool(opts, "rw", false)) {
Jeff Codye2b82472017-04-07 16:55:26 -04001251 if (!bdrv_is_read_only(bs)) {
1252 ret = enable_write_target(bs, errp);
1253 if (ret < 0) {
1254 goto fail;
1255 }
1256 } else {
1257 ret = -EPERM;
1258 error_setg(errp,
1259 "Unable to set VVFAT to 'rw' when drive is read-only");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001260 goto fail;
1261 }
Jeff Codye2b82472017-04-07 16:55:26 -04001262 } else {
1263 /* read only is the default for safety */
1264 ret = bdrv_set_read_only(bs, true, &local_err);
1265 if (ret < 0) {
1266 error_propagate(errp, local_err);
1267 goto fail;
1268 }
thsb5700942007-09-25 14:47:03 +00001269 }
1270
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001271 bs->total_sectors = cyls * heads * secs;
thsb5700942007-09-25 14:47:03 +00001272
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001273 if (init_directories(s, dirname, heads, secs, errp)) {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001274 ret = -EIO;
1275 goto fail;
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001276 }
bellardde167e42005-04-28 21:15:08 +00001277
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001278 s->sector_count = s->offset_to_root_dir
1279 + s->sectors_per_cluster * s->cluster_count;
thsb5700942007-09-25 14:47:03 +00001280
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001281 /* Disable migration when vvfat is used rw */
1282 if (s->qcow) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001283 error_setg(&s->migration_blocker,
1284 "The vvfat (rw) format used by node '%s' "
1285 "does not support live migration",
1286 bdrv_get_device_or_node_name(bs));
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301287 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1288 if (local_err) {
1289 error_propagate(errp, local_err);
1290 error_free(s->migration_blocker);
1291 goto fail;
1292 }
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001293 }
1294
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001295 if (s->offset_to_bootsector > 0) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301296 init_mbr(s, cyls, heads, secs);
1297 }
1298
1299 qemu_co_mutex_init(&s->lock);
1300
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001301 ret = 0;
1302fail:
1303 qemu_opts_del(opts);
1304 return ret;
bellardde167e42005-04-28 21:15:08 +00001305}
1306
Eric Blakea6506482016-06-23 16:37:17 -06001307static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
1308{
Eric Blakea5b8dd22016-06-23 16:37:24 -06001309 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
Eric Blakea6506482016-06-23 16:37:17 -06001310}
1311
bellardde167e42005-04-28 21:15:08 +00001312static inline void vvfat_close_current_file(BDRVVVFATState *s)
1313{
1314 if(s->current_mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001315 s->current_mapping = NULL;
1316 if (s->current_fd) {
1317 qemu_close(s->current_fd);
1318 s->current_fd = 0;
1319 }
bellardde167e42005-04-28 21:15:08 +00001320 }
bellarda0464332005-12-18 18:29:50 +00001321 s->current_cluster = -1;
bellardde167e42005-04-28 21:15:08 +00001322}
1323
1324/* mappings between index1 and index2-1 are supposed to be ordered
1325 * return value is the index of the last mapping for which end>cluster_num
1326 */
1327static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
1328{
bellardde167e42005-04-28 21:15:08 +00001329 while(1) {
Blue Swirl88bf7952010-04-25 15:27:14 +00001330 int index3;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001331 mapping_t* mapping;
1332 index3=(index1+index2)/2;
1333 mapping=array_get(&(s->mapping),index3);
1334 assert(mapping->begin < mapping->end);
1335 if(mapping->begin>=cluster_num) {
1336 assert(index2!=index3 || index2==0);
1337 if(index2==index3)
1338 return index1;
1339 index2=index3;
1340 } else {
1341 if(index1==index3)
1342 return mapping->end<=cluster_num ? index2 : index1;
1343 index1=index3;
1344 }
1345 assert(index1<=index2);
1346 DLOG(mapping=array_get(&(s->mapping),index1);
1347 assert(mapping->begin<=cluster_num);
1348 assert(index2 >= s->mapping.next ||
1349 ((mapping = array_get(&(s->mapping),index2)) &&
1350 mapping->end>cluster_num)));
bellardde167e42005-04-28 21:15:08 +00001351 }
1352}
1353
Anthony Liguoric227f092009-10-01 16:12:16 -05001354static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
bellardde167e42005-04-28 21:15:08 +00001355{
1356 int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05001357 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +00001358 if(index>=s->mapping.next)
blueswir1511d2b12009-03-07 15:32:56 +00001359 return NULL;
bellardde167e42005-04-28 21:15:08 +00001360 mapping=array_get(&(s->mapping),index);
1361 if(mapping->begin>cluster_num)
blueswir1511d2b12009-03-07 15:32:56 +00001362 return NULL;
bellarda0464332005-12-18 18:29:50 +00001363 assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
bellardde167e42005-04-28 21:15:08 +00001364 return mapping;
1365}
1366
Anthony Liguoric227f092009-10-01 16:12:16 -05001367static int open_file(BDRVVVFATState* s,mapping_t* mapping)
bellardde167e42005-04-28 21:15:08 +00001368{
1369 if(!mapping)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001370 return -1;
bellardde167e42005-04-28 21:15:08 +00001371 if(!s->current_mapping ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001372 strcmp(s->current_mapping->path,mapping->path)) {
1373 /* open file */
1374 int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
1375 if(fd<0)
1376 return -1;
1377 vvfat_close_current_file(s);
1378 s->current_fd = fd;
1379 s->current_mapping = mapping;
bellardde167e42005-04-28 21:15:08 +00001380 }
1381 return 0;
1382}
1383
1384static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
1385{
1386 if(s->current_cluster != cluster_num) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001387 int result=0;
1388 off_t offset;
1389 assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
1390 if(!s->current_mapping
1391 || s->current_mapping->begin>cluster_num
1392 || s->current_mapping->end<=cluster_num) {
1393 /* binary search of mappings for file */
1394 mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
bellardde167e42005-04-28 21:15:08 +00001395
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001396 assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
bellarda0464332005-12-18 18:29:50 +00001397
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001398 if (mapping && mapping->mode & MODE_DIRECTORY) {
1399 vvfat_close_current_file(s);
1400 s->current_mapping = mapping;
bellarda0464332005-12-18 18:29:50 +00001401read_cluster_directory:
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001402 offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
1403 s->cluster = (unsigned char*)s->directory.pointer+offset
1404 + 0x20*s->current_mapping->info.dir.first_dir_index;
1405 assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
1406 assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
1407 s->current_cluster = cluster_num;
1408 return 0;
1409 }
bellarda0464332005-12-18 18:29:50 +00001410
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001411 if(open_file(s,mapping))
1412 return -2;
1413 } else if (s->current_mapping->mode & MODE_DIRECTORY)
1414 goto read_cluster_directory;
bellarda0464332005-12-18 18:29:50 +00001415
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001416 assert(s->current_fd);
bellarda0464332005-12-18 18:29:50 +00001417
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001418 offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
1419 if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
1420 return -3;
1421 s->cluster=s->cluster_buffer;
1422 result=read(s->current_fd,s->cluster,s->cluster_size);
1423 if(result<0) {
1424 s->current_cluster = -1;
1425 return -1;
1426 }
1427 s->current_cluster = cluster_num;
bellardde167e42005-04-28 21:15:08 +00001428 }
1429 return 0;
1430}
1431
bellarda0464332005-12-18 18:29:50 +00001432#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -05001433static void print_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001434{
1435 int j = 0;
1436 char buffer[1024];
1437
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001438 fprintf(stderr, "direntry %p: ", direntry);
bellarda0464332005-12-18 18:29:50 +00001439 if(!direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001440 return;
bellarda0464332005-12-18 18:29:50 +00001441 if(is_long_name(direntry)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001442 unsigned char* c=(unsigned char*)direntry;
1443 int i;
1444 for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
blueswir13891b372008-12-14 09:30:41 +00001445#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001446 ADD_CHAR(c[i]);
1447 for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
1448 ADD_CHAR(c[i]);
1449 for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
1450 ADD_CHAR(c[i]);
1451 buffer[j] = 0;
1452 fprintf(stderr, "%s\n", buffer);
bellarda0464332005-12-18 18:29:50 +00001453 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001454 int i;
1455 for(i=0;i<11;i++)
1456 ADD_CHAR(direntry->name[i]);
1457 buffer[j] = 0;
1458 fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
1459 buffer,
1460 direntry->attributes,
1461 begin_of_direntry(direntry),le32_to_cpu(direntry->size));
bellarda0464332005-12-18 18:29:50 +00001462 }
1463}
1464
Anthony Liguoric227f092009-10-01 16:12:16 -05001465static void print_mapping(const mapping_t* mapping)
bellarda0464332005-12-18 18:29:50 +00001466{
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001467 fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1468 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1469 mapping, mapping->begin, mapping->end, mapping->dir_index,
1470 mapping->first_mapping_index, mapping->path, mapping->mode);
1471
bellarda0464332005-12-18 18:29:50 +00001472 if (mapping->mode & MODE_DIRECTORY)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001473 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 +00001474 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001475 fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
bellarda0464332005-12-18 18:29:50 +00001476}
1477#endif
1478
ths5fafdf22007-09-16 21:08:06 +00001479static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00001480 uint8_t *buf, int nb_sectors)
1481{
1482 BDRVVVFATState *s = bs->opaque;
1483 int i;
1484
bellardde167e42005-04-28 21:15:08 +00001485 for(i=0;i<nb_sectors;i++,sector_num++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001486 if (sector_num >= bs->total_sectors)
1487 return -1;
1488 if (s->qcow) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001489 int64_t n;
Eric Blake6f712ee2017-03-08 15:34:28 -06001490 int ret;
Eric Blaked6a644b2017-07-07 07:44:57 -05001491 ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
1492 (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
Eric Blake6f712ee2017-03-08 15:34:28 -06001493 if (ret < 0) {
1494 return ret;
1495 }
1496 if (ret) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001497 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
1498 " allocated\n", sector_num,
1499 n >> BDRV_SECTOR_BITS));
1500 if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
1501 n >> BDRV_SECTOR_BITS)) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001502 return -1;
1503 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001504 i += (n >> BDRV_SECTOR_BITS) - 1;
1505 sector_num += (n >> BDRV_SECTOR_BITS) - 1;
Kevin Wolf7704df92011-11-08 10:50:12 +01001506 continue;
1507 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001508 DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
1509 sector_num));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001510 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001511 if (sector_num < s->offset_to_root_dir) {
1512 if (sector_num < s->offset_to_fat) {
1513 memcpy(buf + i * 0x200,
1514 &(s->first_sectors[sector_num * 0x200]),
1515 0x200);
1516 } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
1517 memcpy(buf + i * 0x200,
1518 &(s->fat.pointer[(sector_num
1519 - s->offset_to_fat) * 0x200]),
1520 0x200);
1521 } else if (sector_num < s->offset_to_root_dir) {
1522 memcpy(buf + i * 0x200,
1523 &(s->fat.pointer[(sector_num - s->offset_to_fat
1524 - s->sectors_per_fat) * 0x200]),
1525 0x200);
1526 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001527 } else {
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001528 uint32_t sector = sector_num - s->offset_to_root_dir,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001529 sector_offset_in_cluster=(sector%s->sectors_per_cluster),
1530 cluster_num=sector/s->sectors_per_cluster;
1531 if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
1532 /* LATER TODO: strict: return -1; */
1533 memset(buf+i*0x200,0,0x200);
1534 continue;
1535 }
1536 memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
1537 }
bellarda0464332005-12-18 18:29:50 +00001538 }
1539 return 0;
1540}
1541
Kevin Wolf4575eb42016-04-26 17:14:08 +02001542static int coroutine_fn
1543vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1544 QEMUIOVector *qiov, int flags)
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001545{
1546 int ret;
1547 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02001548 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
1549 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1550 void *buf;
1551
1552 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1553 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1554
1555 buf = g_try_malloc(bytes);
1556 if (bytes && buf == NULL) {
1557 return -ENOMEM;
1558 }
1559
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001560 qemu_co_mutex_lock(&s->lock);
1561 ret = vvfat_read(bs, sector_num, buf, nb_sectors);
1562 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02001563
1564 qemu_iovec_from_buf(qiov, 0, buf, bytes);
1565 g_free(buf);
1566
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001567 return ret;
1568}
1569
bellarda0464332005-12-18 18:29:50 +00001570/* LATER TODO: statify all functions */
1571
1572/*
1573 * Idea of the write support (use snapshot):
1574 *
1575 * 1. check if all data is consistent, recording renames, modifications,
1576 * new files and directories (in s->commits).
1577 *
1578 * 2. if the data is not consistent, stop committing
1579 *
1580 * 3. handle renames, and create new files and directories (do not yet
1581 * write their contents)
1582 *
1583 * 4. walk the directories, fixing the mapping and direntries, and marking
1584 * the handled mappings as not deleted
1585 *
1586 * 5. commit the contents of the files
1587 *
1588 * 6. handle deleted files and directories
1589 *
1590 */
1591
Anthony Liguoric227f092009-10-01 16:12:16 -05001592typedef struct commit_t {
bellarda0464332005-12-18 18:29:50 +00001593 char* path;
1594 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001595 struct { uint32_t cluster; } rename;
1596 struct { int dir_index; uint32_t modified_offset; } writeout;
1597 struct { uint32_t first_cluster; } new_file;
1598 struct { uint32_t cluster; } mkdir;
bellarda0464332005-12-18 18:29:50 +00001599 } param;
1600 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1601 enum {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001602 ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
bellarda0464332005-12-18 18:29:50 +00001603 } action;
Anthony Liguoric227f092009-10-01 16:12:16 -05001604} commit_t;
bellarda0464332005-12-18 18:29:50 +00001605
1606static void clear_commits(BDRVVVFATState* s)
1607{
1608 int i;
1609DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
1610 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001611 commit_t* commit = array_get(&(s->commits), i);
1612 assert(commit->path || commit->action == ACTION_WRITEOUT);
1613 if (commit->action != ACTION_WRITEOUT) {
1614 assert(commit->path);
Stefan Weilce137822011-09-30 23:29:53 +02001615 g_free(commit->path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001616 } else
1617 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00001618 }
1619 s->commits.next = 0;
1620}
1621
1622static void schedule_rename(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001623 uint32_t cluster, char* new_path)
bellarda0464332005-12-18 18:29:50 +00001624{
Anthony Liguoric227f092009-10-01 16:12:16 -05001625 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001626 commit->path = new_path;
1627 commit->param.rename.cluster = cluster;
1628 commit->action = ACTION_RENAME;
1629}
1630
1631static void schedule_writeout(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001632 int dir_index, uint32_t modified_offset)
bellarda0464332005-12-18 18:29:50 +00001633{
Anthony Liguoric227f092009-10-01 16:12:16 -05001634 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001635 commit->path = NULL;
1636 commit->param.writeout.dir_index = dir_index;
1637 commit->param.writeout.modified_offset = modified_offset;
1638 commit->action = ACTION_WRITEOUT;
1639}
1640
1641static void schedule_new_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001642 char* path, uint32_t first_cluster)
bellarda0464332005-12-18 18:29:50 +00001643{
Anthony Liguoric227f092009-10-01 16:12:16 -05001644 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001645 commit->path = path;
1646 commit->param.new_file.first_cluster = first_cluster;
1647 commit->action = ACTION_NEW_FILE;
1648}
1649
1650static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
1651{
Anthony Liguoric227f092009-10-01 16:12:16 -05001652 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001653 commit->path = path;
1654 commit->param.mkdir.cluster = cluster;
1655 commit->action = ACTION_MKDIR;
1656}
1657
1658typedef struct {
ths64eaabd2008-07-03 19:54:19 +00001659 /*
1660 * Since the sequence number is at most 0x3f, and the filename
1661 * length is at most 13 times the sequence number, the maximal
1662 * filename length is 0x3f * 13 bytes.
1663 */
1664 unsigned char name[0x3f * 13 + 1];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001665 gunichar2 name2[0x3f * 13 + 1];
bellarda0464332005-12-18 18:29:50 +00001666 int checksum, len;
1667 int sequence_number;
1668} long_file_name;
1669
1670static void lfn_init(long_file_name* lfn)
1671{
1672 lfn->sequence_number = lfn->len = 0;
1673 lfn->checksum = 0x100;
1674}
1675
1676/* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1677static int parse_long_name(long_file_name* lfn,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001678 const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001679{
1680 int i, j, offset;
1681 const unsigned char* pointer = (const unsigned char*)direntry;
1682
1683 if (!is_long_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001684 return 1;
bellarda0464332005-12-18 18:29:50 +00001685
1686 if (pointer[0] & 0x40) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001687 /* first entry; do some initialization */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001688 lfn->sequence_number = pointer[0] & 0x3f;
1689 lfn->checksum = pointer[13];
1690 lfn->name[0] = 0;
1691 lfn->name[lfn->sequence_number * 13] = 0;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001692 } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
1693 /* not the expected sequence number */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001694 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001695 } else if (pointer[13] != lfn->checksum) {
1696 /* not the expected checksum */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001697 return -2;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001698 } else if (pointer[12] || pointer[26] || pointer[27]) {
1699 /* invalid zero fields */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001700 return -3;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001701 }
bellarda0464332005-12-18 18:29:50 +00001702
1703 offset = 13 * (lfn->sequence_number - 1);
1704 for (i = 0, j = 1; i < 13; i++, j+=2) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001705 if (j == 11)
1706 j = 14;
1707 else if (j == 26)
1708 j = 28;
bellarda0464332005-12-18 18:29:50 +00001709
Hervé Poussineaue03da262017-07-15 15:28:40 +02001710 if (pointer[j] == 0 && pointer[j + 1] == 0) {
1711 /* end of long file name */
1712 break;
1713 }
1714 gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
1715 lfn->name2[offset + i] = c;
bellarda0464332005-12-18 18:29:50 +00001716 }
1717
Hervé Poussineaue03da262017-07-15 15:28:40 +02001718 if (pointer[0] & 0x40) {
1719 /* first entry; set len */
1720 lfn->len = offset + i;
1721 }
1722 if ((pointer[0] & 0x3f) == 0x01) {
1723 /* last entry; finalize entry */
1724 glong olen;
1725 gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
1726 if (!utf8) {
1727 return -4;
1728 }
1729 lfn->len = olen;
1730 memcpy(lfn->name, utf8, olen + 1);
1731 g_free(utf8);
1732 }
bellarda0464332005-12-18 18:29:50 +00001733
1734 return 0;
1735}
1736
1737/* returns 0 if successful, >0 if no short_name, and <0 on error */
1738static int parse_short_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001739 long_file_name* lfn, direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001740{
1741 int i, j;
1742
1743 if (!is_short_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001744 return 1;
bellarda0464332005-12-18 18:29:50 +00001745
1746 for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
1747 for (i = 0; i <= j; i++) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001748 uint8_t c = direntry->name[i];
1749 if (c != to_valid_short_char(c)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001750 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001751 } else if (s->downcase_short_names) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001752 lfn->name[i] = qemu_tolower(direntry->name[i]);
Hervé Poussineaue03da262017-07-15 15:28:40 +02001753 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001754 lfn->name[i] = direntry->name[i];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001755 }
bellarda0464332005-12-18 18:29:50 +00001756 }
1757
Stefan Weilf671d172013-12-11 21:37:11 +01001758 for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
1759 }
bellarda0464332005-12-18 18:29:50 +00001760 if (j >= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001761 lfn->name[i++] = '.';
1762 lfn->name[i + j + 1] = '\0';
1763 for (;j >= 0; j--) {
Stefan Weilf671d172013-12-11 21:37:11 +01001764 uint8_t c = direntry->name[8 + j];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001765 if (c != to_valid_short_char(c)) {
Stefan Weilf671d172013-12-11 21:37:11 +01001766 return -2;
1767 } else if (s->downcase_short_names) {
1768 lfn->name[i + j] = qemu_tolower(c);
1769 } else {
1770 lfn->name[i + j] = c;
1771 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001772 }
bellarda0464332005-12-18 18:29:50 +00001773 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001774 lfn->name[i + j + 1] = '\0';
bellarda0464332005-12-18 18:29:50 +00001775
Hervé Poussineau8c4517f2017-07-15 15:28:38 +02001776 if (lfn->name[0] == DIR_KANJI_FAKE) {
1777 lfn->name[0] = DIR_KANJI;
Hervé Poussineau78f002c2017-05-22 23:12:04 +02001778 }
thsffe8ab82007-12-16 03:16:05 +00001779 lfn->len = strlen((char*)lfn->name);
bellarda0464332005-12-18 18:29:50 +00001780
1781 return 0;
1782}
1783
1784static inline uint32_t modified_fat_get(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001785 unsigned int cluster)
bellarda0464332005-12-18 18:29:50 +00001786{
1787 if (cluster < s->last_cluster_of_root_directory) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001788 if (cluster + 1 == s->last_cluster_of_root_directory)
1789 return s->max_fat_value;
1790 else
1791 return cluster + 1;
bellarda0464332005-12-18 18:29:50 +00001792 }
1793
1794 if (s->fat_type==32) {
1795 uint32_t* entry=((uint32_t*)s->fat2)+cluster;
1796 return le32_to_cpu(*entry);
1797 } else if (s->fat_type==16) {
1798 uint16_t* entry=((uint16_t*)s->fat2)+cluster;
1799 return le16_to_cpu(*entry);
1800 } else {
1801 const uint8_t* x=s->fat2+cluster*3/2;
1802 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
1803 }
1804}
1805
Eric Blake6f712ee2017-03-08 15:34:28 -06001806static inline bool cluster_was_modified(BDRVVVFATState *s,
1807 uint32_t cluster_num)
bellarda0464332005-12-18 18:29:50 +00001808{
1809 int was_modified = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001810 int i;
bellarda0464332005-12-18 18:29:50 +00001811
Kevin Wolfeecc7742016-05-30 17:13:09 +02001812 if (s->qcow == NULL) {
1813 return 0;
1814 }
bellarda0464332005-12-18 18:29:50 +00001815
Kevin Wolfeecc7742016-05-30 17:13:09 +02001816 for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
1817 was_modified = bdrv_is_allocated(s->qcow->bs,
Eric Blaked6a644b2017-07-07 07:44:57 -05001818 (cluster2sector(s, cluster_num) +
1819 i) * BDRV_SECTOR_SIZE,
1820 BDRV_SECTOR_SIZE, NULL);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001821 }
bellarda0464332005-12-18 18:29:50 +00001822
Eric Blake6f712ee2017-03-08 15:34:28 -06001823 /*
1824 * Note that this treats failures to learn allocation status the
1825 * same as if an allocation has occurred. It's as safe as
1826 * anything else, given that a failure to learn allocation status
1827 * will probably result in more failures.
1828 */
1829 return !!was_modified;
bellarda0464332005-12-18 18:29:50 +00001830}
1831
1832static const char* get_basename(const char* path)
1833{
1834 char* basename = strrchr(path, '/');
1835 if (basename == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001836 return path;
bellarda0464332005-12-18 18:29:50 +00001837 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001838 return basename + 1; /* strip '/' */
bellarda0464332005-12-18 18:29:50 +00001839}
1840
1841/*
1842 * The array s->used_clusters holds the states of the clusters. If it is
1843 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1844 * was modified, bit 3 is set.
1845 * If any cluster is allocated, but not part of a file or directory, this
1846 * driver refuses to commit.
1847 */
1848typedef enum {
1849 USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
Anthony Liguoric227f092009-10-01 16:12:16 -05001850} used_t;
bellarda0464332005-12-18 18:29:50 +00001851
1852/*
1853 * get_cluster_count_for_direntry() not only determines how many clusters
1854 * are occupied by direntry, but also if it was renamed or modified.
1855 *
1856 * A file is thought to be renamed *only* if there already was a file with
1857 * exactly the same first cluster, but a different name.
1858 *
1859 * Further, the files/directories handled by this function are
1860 * assumed to be *not* deleted (and *only* those).
1861 */
1862static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001863 direntry_t* direntry, const char* path)
bellarda0464332005-12-18 18:29:50 +00001864{
1865 /*
1866 * This is a little bit tricky:
1867 * IF the guest OS just inserts a cluster into the file chain,
1868 * and leaves the rest alone, (i.e. the original file had clusters
1869 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1870 *
1871 * - do_commit will write the cluster into the file at the given
1872 * offset, but
1873 *
1874 * - the cluster which is overwritten should be moved to a later
1875 * position in the file.
1876 *
1877 * I am not aware that any OS does something as braindead, but this
1878 * situation could happen anyway when not committing for a long time.
1879 * Just to be sure that this does not bite us, detect it, and copy the
1880 * contents of the clusters to-be-overwritten into the qcow.
1881 */
1882 int copy_it = 0;
1883 int was_modified = 0;
1884 int32_t ret = 0;
1885
1886 uint32_t cluster_num = begin_of_direntry(direntry);
1887 uint32_t offset = 0;
1888 int first_mapping_index = -1;
Anthony Liguoric227f092009-10-01 16:12:16 -05001889 mapping_t* mapping = NULL;
bellarda0464332005-12-18 18:29:50 +00001890 const char* basename2 = NULL;
1891
1892 vvfat_close_current_file(s);
1893
1894 /* the root directory */
1895 if (cluster_num == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001896 return 0;
bellarda0464332005-12-18 18:29:50 +00001897
1898 /* write support */
1899 if (s->qcow) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001900 basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00001901
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001902 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001903
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001904 if (mapping) {
1905 const char* basename;
bellardda2414e2006-04-23 14:36:41 +00001906
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001907 assert(mapping->mode & MODE_DELETED);
1908 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00001909
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001910 basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001911
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001912 assert(mapping->mode & MODE_NORMAL);
bellarda0464332005-12-18 18:29:50 +00001913
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001914 /* rename */
1915 if (strcmp(basename, basename2))
1916 schedule_rename(s, cluster_num, g_strdup(path));
1917 } else if (is_file(direntry))
1918 /* new file */
1919 schedule_new_file(s, g_strdup(path), cluster_num);
1920 else {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001921 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001922 return 0;
1923 }
bellarda0464332005-12-18 18:29:50 +00001924 }
1925
1926 while(1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001927 if (s->qcow) {
1928 if (!copy_it && cluster_was_modified(s, cluster_num)) {
1929 if (mapping == NULL ||
1930 mapping->begin > cluster_num ||
1931 mapping->end <= cluster_num)
1932 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001933
1934
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001935 if (mapping &&
1936 (mapping->mode & MODE_DIRECTORY) == 0) {
bellarda0464332005-12-18 18:29:50 +00001937
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001938 /* was modified in qcow */
1939 if (offset != mapping->info.file.offset + s->cluster_size
1940 * (cluster_num - mapping->begin)) {
1941 /* offset of this cluster in file chain has changed */
Blue Swirl43dc2a62010-03-18 18:41:57 +00001942 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001943 copy_it = 1;
1944 } else if (offset == 0) {
1945 const char* basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001946
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001947 if (strcmp(basename, basename2))
1948 copy_it = 1;
1949 first_mapping_index = array_index(&(s->mapping), mapping);
1950 }
bellarda0464332005-12-18 18:29:50 +00001951
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001952 if (mapping->first_mapping_index != first_mapping_index
1953 && mapping->info.file.offset > 0) {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001954 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001955 copy_it = 1;
1956 }
bellarda0464332005-12-18 18:29:50 +00001957
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001958 /* need to write out? */
1959 if (!was_modified && is_file(direntry)) {
1960 was_modified = 1;
1961 schedule_writeout(s, mapping->dir_index, offset);
1962 }
1963 }
1964 }
bellardde167e42005-04-28 21:15:08 +00001965
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001966 if (copy_it) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001967 int i;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001968 /*
1969 * This is horribly inefficient, but that is okay, since
1970 * it is rarely executed, if at all.
1971 */
1972 int64_t offset = cluster2sector(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00001973
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001974 vvfat_close_current_file(s);
Kevin Wolf7704df92011-11-08 10:50:12 +01001975 for (i = 0; i < s->sectors_per_cluster; i++) {
Kevin Wolfeecc7742016-05-30 17:13:09 +02001976 int res;
1977
Eric Blaked6a644b2017-07-07 07:44:57 -05001978 res = bdrv_is_allocated(s->qcow->bs,
1979 (offset + i) * BDRV_SECTOR_SIZE,
1980 BDRV_SECTOR_SIZE, NULL);
Eric Blake6f712ee2017-03-08 15:34:28 -06001981 if (res < 0) {
1982 return -1;
1983 }
Kevin Wolfeecc7742016-05-30 17:13:09 +02001984 if (!res) {
1985 res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
1986 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001987 return -1;
1988 }
Kevin Wolf18d51c42016-05-31 14:42:08 +02001989 res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001990 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001991 return -2;
1992 }
1993 }
1994 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001995 }
1996 }
bellarda0464332005-12-18 18:29:50 +00001997
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001998 ret++;
1999 if (s->used_clusters[cluster_num] & USED_ANY)
2000 return 0;
2001 s->used_clusters[cluster_num] = USED_FILE;
bellarda0464332005-12-18 18:29:50 +00002002
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002003 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002004
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002005 if (fat_eof(s, cluster_num))
2006 return ret;
2007 else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
2008 return -1;
bellarda0464332005-12-18 18:29:50 +00002009
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002010 offset += s->cluster_size;
bellardde167e42005-04-28 21:15:08 +00002011 }
2012}
2013
bellarda0464332005-12-18 18:29:50 +00002014/*
ths5fafdf22007-09-16 21:08:06 +00002015 * This function looks at the modified data (qcow).
bellarda0464332005-12-18 18:29:50 +00002016 * It returns 0 upon inconsistency or error, and the number of clusters
2017 * used by the directory, its subdirectories and their files.
2018 */
2019static int check_directory_consistency(BDRVVVFATState *s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002020 int cluster_num, const char* path)
bellardde167e42005-04-28 21:15:08 +00002021{
bellarda0464332005-12-18 18:29:50 +00002022 int ret = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -05002023 unsigned char* cluster = g_malloc(s->cluster_size);
Anthony Liguoric227f092009-10-01 16:12:16 -05002024 direntry_t* direntries = (direntry_t*)cluster;
2025 mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00002026
bellarda0464332005-12-18 18:29:50 +00002027 long_file_name lfn;
2028 int path_len = strlen(path);
Kevin Wolf0d460d62011-06-01 10:57:00 +02002029 char path2[PATH_MAX + 1];
bellardde167e42005-04-28 21:15:08 +00002030
bellarda0464332005-12-18 18:29:50 +00002031 assert(path_len < PATH_MAX); /* len was tested before! */
blueswir1363a37d2008-08-21 17:58:08 +00002032 pstrcpy(path2, sizeof(path2), path);
bellarda0464332005-12-18 18:29:50 +00002033 path2[path_len] = '/';
2034 path2[path_len + 1] = '\0';
bellardde167e42005-04-28 21:15:08 +00002035
bellarda0464332005-12-18 18:29:50 +00002036 if (mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002037 const char* basename = get_basename(mapping->path);
2038 const char* basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00002039
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002040 assert(mapping->mode & MODE_DIRECTORY);
bellarda0464332005-12-18 18:29:50 +00002041
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002042 assert(mapping->mode & MODE_DELETED);
2043 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00002044
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002045 if (strcmp(basename, basename2))
2046 schedule_rename(s, cluster_num, g_strdup(path));
bellarda0464332005-12-18 18:29:50 +00002047 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002048 /* new directory */
2049 schedule_mkdir(s, cluster_num, g_strdup(path));
ths3b46e622007-09-17 08:09:54 +00002050
bellarda0464332005-12-18 18:29:50 +00002051 lfn_init(&lfn);
2052 do {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002053 int i;
2054 int subret = 0;
bellarda0464332005-12-18 18:29:50 +00002055
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002056 ret++;
bellarda0464332005-12-18 18:29:50 +00002057
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002058 if (s->used_clusters[cluster_num] & USED_ANY) {
2059 fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
Markus Armbruster6262bbd2014-05-28 11:17:04 +02002060 goto fail;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002061 }
2062 s->used_clusters[cluster_num] = USED_DIRECTORY;
bellardde167e42005-04-28 21:15:08 +00002063
bellarda0464332005-12-18 18:29:50 +00002064DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002065 subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
2066 s->sectors_per_cluster);
2067 if (subret) {
2068 fprintf(stderr, "Error fetching direntries\n");
2069 fail:
Stefan Weilce137822011-09-30 23:29:53 +02002070 g_free(cluster);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002071 return 0;
2072 }
bellarda0464332005-12-18 18:29:50 +00002073
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002074 for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
2075 int cluster_count = 0;
bellarda0464332005-12-18 18:29:50 +00002076
Stefan Weilb2bedb22011-09-12 22:33:01 +02002077DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002078 if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
2079 is_free(direntries + i))
2080 continue;
bellarda0464332005-12-18 18:29:50 +00002081
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002082 subret = parse_long_name(&lfn, direntries + i);
2083 if (subret < 0) {
2084 fprintf(stderr, "Error in long name\n");
2085 goto fail;
2086 }
2087 if (subret == 0 || is_free(direntries + i))
2088 continue;
bellarda0464332005-12-18 18:29:50 +00002089
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002090 if (fat_chksum(direntries+i) != lfn.checksum) {
2091 subret = parse_short_name(s, &lfn, direntries + i);
2092 if (subret < 0) {
2093 fprintf(stderr, "Error in short name (%d)\n", subret);
2094 goto fail;
2095 }
2096 if (subret > 0 || !strcmp((char*)lfn.name, ".")
2097 || !strcmp((char*)lfn.name, ".."))
2098 continue;
2099 }
2100 lfn.checksum = 0x100; /* cannot use long name twice */
bellarda0464332005-12-18 18:29:50 +00002101
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002102 if (path_len + 1 + lfn.len >= PATH_MAX) {
2103 fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
2104 goto fail;
2105 }
blueswir1363a37d2008-08-21 17:58:08 +00002106 pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
2107 (char*)lfn.name);
bellarda0464332005-12-18 18:29:50 +00002108
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002109 if (is_directory(direntries + i)) {
2110 if (begin_of_direntry(direntries + i) == 0) {
2111 DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
2112 goto fail;
2113 }
2114 cluster_count = check_directory_consistency(s,
2115 begin_of_direntry(direntries + i), path2);
2116 if (cluster_count == 0) {
2117 DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
2118 goto fail;
2119 }
2120 } else if (is_file(direntries + i)) {
2121 /* check file size with FAT */
2122 cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
2123 if (cluster_count !=
Laurent Vivier13385ae2016-05-31 18:35:54 +02002124 DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002125 DLOG(fprintf(stderr, "Cluster count mismatch\n"));
2126 goto fail;
2127 }
2128 } else
Blue Swirl43dc2a62010-03-18 18:41:57 +00002129 abort(); /* cluster_count = 0; */
bellarda0464332005-12-18 18:29:50 +00002130
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002131 ret += cluster_count;
2132 }
bellarda0464332005-12-18 18:29:50 +00002133
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002134 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002135 } while(!fat_eof(s, cluster_num));
2136
Stefan Weilce137822011-09-30 23:29:53 +02002137 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002138 return ret;
bellardde167e42005-04-28 21:15:08 +00002139}
2140
bellarda0464332005-12-18 18:29:50 +00002141/* returns 1 on success */
2142static int is_consistent(BDRVVVFATState* s)
bellardde167e42005-04-28 21:15:08 +00002143{
bellarda0464332005-12-18 18:29:50 +00002144 int i, check;
2145 int used_clusters_count = 0;
2146
2147DLOG(checkpoint());
2148 /*
2149 * - get modified FAT
2150 * - compare the two FATs (TODO)
2151 * - get buffer for marking used clusters
2152 * - recurse direntries from root (using bs->bdrv_read to make
2153 * sure to get the new data)
2154 * - check that the FAT agrees with the size
2155 * - count the number of clusters occupied by this directory and
2156 * its files
2157 * - check that the cumulative used cluster count agrees with the
2158 * FAT
2159 * - if all is fine, return number of used clusters
2160 */
2161 if (s->fat2 == NULL) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002162 int size = 0x200 * s->sectors_per_fat;
2163 s->fat2 = g_malloc(size);
2164 memcpy(s->fat2, s->fat.pointer, size);
bellarda0464332005-12-18 18:29:50 +00002165 }
2166 check = vvfat_read(s->bs,
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02002167 s->offset_to_fat, s->fat2, s->sectors_per_fat);
bellarda0464332005-12-18 18:29:50 +00002168 if (check) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002169 fprintf(stderr, "Could not copy fat\n");
2170 return 0;
bellardde167e42005-04-28 21:15:08 +00002171 }
bellarda0464332005-12-18 18:29:50 +00002172 assert (s->used_clusters);
2173 for (i = 0; i < sector2cluster(s, s->sector_count); i++)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002174 s->used_clusters[i] &= ~USED_ANY;
bellarda0464332005-12-18 18:29:50 +00002175
2176 clear_commits(s);
2177
2178 /* mark every mapped file/directory as deleted.
2179 * (check_directory_consistency() will unmark those still present). */
2180 if (s->qcow)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002181 for (i = 0; i < s->mapping.next; i++) {
2182 mapping_t* mapping = array_get(&(s->mapping), i);
2183 if (mapping->first_mapping_index < 0)
2184 mapping->mode |= MODE_DELETED;
2185 }
bellarda0464332005-12-18 18:29:50 +00002186
2187 used_clusters_count = check_directory_consistency(s, 0, s->path);
2188 if (used_clusters_count <= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002189 DLOG(fprintf(stderr, "problem in directory\n"));
2190 return 0;
bellarda0464332005-12-18 18:29:50 +00002191 }
2192
2193 check = s->last_cluster_of_root_directory;
2194 for (i = check; i < sector2cluster(s, s->sector_count); i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002195 if (modified_fat_get(s, i)) {
2196 if(!s->used_clusters[i]) {
2197 DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
2198 return 0;
2199 }
2200 check++;
2201 }
bellarda0464332005-12-18 18:29:50 +00002202
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002203 if (s->used_clusters[i] == USED_ALLOCATED) {
2204 /* allocated, but not used... */
2205 DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
2206 return 0;
2207 }
bellarda0464332005-12-18 18:29:50 +00002208 }
2209
2210 if (check != used_clusters_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002211 return 0;
bellarda0464332005-12-18 18:29:50 +00002212
2213 return used_clusters_count;
bellardde167e42005-04-28 21:15:08 +00002214}
2215
bellarda0464332005-12-18 18:29:50 +00002216static inline void adjust_mapping_indices(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002217 int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002218{
bellarda0464332005-12-18 18:29:50 +00002219 int i;
2220
2221 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002222 mapping_t* mapping = array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002223
2224#define ADJUST_MAPPING_INDEX(name) \
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002225 if (mapping->name >= offset) \
2226 mapping->name += adjust
bellarda0464332005-12-18 18:29:50 +00002227
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002228 ADJUST_MAPPING_INDEX(first_mapping_index);
2229 if (mapping->mode & MODE_DIRECTORY)
2230 ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
bellarda0464332005-12-18 18:29:50 +00002231 }
bellardde167e42005-04-28 21:15:08 +00002232}
2233
bellarda0464332005-12-18 18:29:50 +00002234/* insert or update mapping */
Anthony Liguoric227f092009-10-01 16:12:16 -05002235static mapping_t* insert_mapping(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002236 uint32_t begin, uint32_t end)
bellardde167e42005-04-28 21:15:08 +00002237{
bellarda0464332005-12-18 18:29:50 +00002238 /*
2239 * - find mapping where mapping->begin >= begin,
2240 * - if mapping->begin > begin: insert
2241 * - adjust all references to mappings!
2242 * - else: adjust
2243 * - replace name
2244 */
2245 int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05002246 mapping_t* mapping = NULL;
2247 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00002248
2249 if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002250 && mapping->begin < begin) {
2251 mapping->end = begin;
2252 index++;
2253 mapping = array_get(&(s->mapping), index);
bellarda0464332005-12-18 18:29:50 +00002254 }
2255 if (index >= s->mapping.next || mapping->begin > begin) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002256 mapping = array_insert(&(s->mapping), index, 1);
2257 mapping->path = NULL;
2258 adjust_mapping_indices(s, index, +1);
bellarda0464332005-12-18 18:29:50 +00002259 }
2260
bellardde167e42005-04-28 21:15:08 +00002261 mapping->begin = begin;
bellarda0464332005-12-18 18:29:50 +00002262 mapping->end = end;
2263
Anthony Liguoric227f092009-10-01 16:12:16 -05002264DLOG(mapping_t* next_mapping;
bellarda0464332005-12-18 18:29:50 +00002265assert(index + 1 >= s->mapping.next ||
2266((next_mapping = array_get(&(s->mapping), index + 1)) &&
2267 next_mapping->begin >= end)));
2268
Anthony Liguoric227f092009-10-01 16:12:16 -05002269 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002270 s->current_mapping = array_get(&(s->mapping),
2271 s->current_mapping - first_mapping);
bellarda0464332005-12-18 18:29:50 +00002272
2273 return mapping;
bellardde167e42005-04-28 21:15:08 +00002274}
2275
bellarda0464332005-12-18 18:29:50 +00002276static int remove_mapping(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +00002277{
Anthony Liguoric227f092009-10-01 16:12:16 -05002278 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
2279 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellardde167e42005-04-28 21:15:08 +00002280
bellarda0464332005-12-18 18:29:50 +00002281 /* free mapping */
Stefan Weilce137822011-09-30 23:29:53 +02002282 if (mapping->first_mapping_index < 0) {
2283 g_free(mapping->path);
2284 }
bellardde167e42005-04-28 21:15:08 +00002285
bellarda0464332005-12-18 18:29:50 +00002286 /* remove from s->mapping */
2287 array_remove(&(s->mapping), mapping_index);
bellardde167e42005-04-28 21:15:08 +00002288
bellarda0464332005-12-18 18:29:50 +00002289 /* adjust all references to mappings */
2290 adjust_mapping_indices(s, mapping_index, -1);
bellardde167e42005-04-28 21:15:08 +00002291
Anthony Liguoric227f092009-10-01 16:12:16 -05002292 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002293 s->current_mapping = array_get(&(s->mapping),
2294 s->current_mapping - first_mapping);
bellardde167e42005-04-28 21:15:08 +00002295
2296 return 0;
2297}
2298
bellarda0464332005-12-18 18:29:50 +00002299static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002300{
bellarda0464332005-12-18 18:29:50 +00002301 int i;
2302 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002303 mapping_t* mapping = array_get(&(s->mapping), i);
2304 if (mapping->dir_index >= offset)
2305 mapping->dir_index += adjust;
2306 if ((mapping->mode & MODE_DIRECTORY) &&
2307 mapping->info.dir.first_dir_index >= offset)
2308 mapping->info.dir.first_dir_index += adjust;
bellardde167e42005-04-28 21:15:08 +00002309 }
bellardde167e42005-04-28 21:15:08 +00002310}
2311
Anthony Liguoric227f092009-10-01 16:12:16 -05002312static direntry_t* insert_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002313 int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002314{
bellarda0464332005-12-18 18:29:50 +00002315 /*
2316 * make room in s->directory,
2317 * adjust_dirindices
2318 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002319 direntry_t* result = array_insert(&(s->directory), dir_index, count);
bellarda0464332005-12-18 18:29:50 +00002320 if (result == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002321 return NULL;
bellarda0464332005-12-18 18:29:50 +00002322 adjust_dirindices(s, dir_index, count);
bellardde167e42005-04-28 21:15:08 +00002323 return result;
2324}
2325
bellarda0464332005-12-18 18:29:50 +00002326static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002327{
bellarda0464332005-12-18 18:29:50 +00002328 int ret = array_remove_slice(&(s->directory), dir_index, count);
2329 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002330 return ret;
bellarda0464332005-12-18 18:29:50 +00002331 adjust_dirindices(s, dir_index, -count);
bellardde167e42005-04-28 21:15:08 +00002332 return 0;
2333}
2334
bellarda0464332005-12-18 18:29:50 +00002335/*
2336 * Adapt the mappings of the cluster chain starting at first cluster
2337 * (i.e. if a file starts at first_cluster, the chain is followed according
2338 * to the modified fat, and the corresponding entries in s->mapping are
2339 * adjusted)
2340 */
2341static int commit_mappings(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002342 uint32_t first_cluster, int dir_index)
bellardde167e42005-04-28 21:15:08 +00002343{
Anthony Liguoric227f092009-10-01 16:12:16 -05002344 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2345 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002346 uint32_t cluster = first_cluster;
bellardde167e42005-04-28 21:15:08 +00002347
bellarda0464332005-12-18 18:29:50 +00002348 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002349
bellarda0464332005-12-18 18:29:50 +00002350 assert(mapping);
2351 assert(mapping->begin == first_cluster);
2352 mapping->first_mapping_index = -1;
2353 mapping->dir_index = dir_index;
2354 mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002355 MODE_DIRECTORY : MODE_NORMAL;
bellardde167e42005-04-28 21:15:08 +00002356
bellarda0464332005-12-18 18:29:50 +00002357 while (!fat_eof(s, cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002358 uint32_t c, c1;
bellardde167e42005-04-28 21:15:08 +00002359
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002360 for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
2361 c = c1, c1 = modified_fat_get(s, c1));
bellardde167e42005-04-28 21:15:08 +00002362
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002363 c++;
2364 if (c > mapping->end) {
2365 int index = array_index(&(s->mapping), mapping);
2366 int i, max_i = s->mapping.next - index;
2367 for (i = 1; i < max_i && mapping[i].begin < c; i++);
2368 while (--i > 0)
2369 remove_mapping(s, index + 1);
2370 }
2371 assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
2372 || mapping[1].begin >= c);
2373 mapping->end = c;
bellarda0464332005-12-18 18:29:50 +00002374
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002375 if (!fat_eof(s, c1)) {
2376 int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
2377 mapping_t* next_mapping = i >= s->mapping.next ? NULL :
2378 array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002379
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002380 if (next_mapping == NULL || next_mapping->begin > c1) {
2381 int i1 = array_index(&(s->mapping), mapping);
bellarda0464332005-12-18 18:29:50 +00002382
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002383 next_mapping = insert_mapping(s, c1, c1+1);
bellarda0464332005-12-18 18:29:50 +00002384
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002385 if (c1 < c)
2386 i1++;
2387 mapping = array_get(&(s->mapping), i1);
2388 }
bellarda0464332005-12-18 18:29:50 +00002389
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002390 next_mapping->dir_index = mapping->dir_index;
2391 next_mapping->first_mapping_index =
2392 mapping->first_mapping_index < 0 ?
2393 array_index(&(s->mapping), mapping) :
2394 mapping->first_mapping_index;
2395 next_mapping->path = mapping->path;
2396 next_mapping->mode = mapping->mode;
2397 next_mapping->read_only = mapping->read_only;
2398 if (mapping->mode & MODE_DIRECTORY) {
2399 next_mapping->info.dir.parent_mapping_index =
2400 mapping->info.dir.parent_mapping_index;
2401 next_mapping->info.dir.first_dir_index =
2402 mapping->info.dir.first_dir_index +
2403 0x10 * s->sectors_per_cluster *
2404 (mapping->end - mapping->begin);
2405 } else
2406 next_mapping->info.file.offset = mapping->info.file.offset +
2407 mapping->end - mapping->begin;
bellarda0464332005-12-18 18:29:50 +00002408
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002409 mapping = next_mapping;
2410 }
ths3b46e622007-09-17 08:09:54 +00002411
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002412 cluster = c1;
bellarda0464332005-12-18 18:29:50 +00002413 }
2414
2415 return 0;
2416}
2417
2418static int commit_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002419 int dir_index, int parent_mapping_index)
bellarda0464332005-12-18 18:29:50 +00002420{
Anthony Liguoric227f092009-10-01 16:12:16 -05002421 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002422 uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
Anthony Liguoric227f092009-10-01 16:12:16 -05002423 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
bellarda0464332005-12-18 18:29:50 +00002424
2425 int factor = 0x10 * s->sectors_per_cluster;
2426 int old_cluster_count, new_cluster_count;
2427 int current_dir_index = mapping->info.dir.first_dir_index;
2428 int first_dir_index = current_dir_index;
2429 int ret, i;
2430 uint32_t c;
2431
2432DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index));
2433
2434 assert(direntry);
2435 assert(mapping);
2436 assert(mapping->begin == first_cluster);
2437 assert(mapping->info.dir.first_dir_index < s->directory.next);
2438 assert(mapping->mode & MODE_DIRECTORY);
2439 assert(dir_index == 0 || is_directory(direntry));
2440
2441 mapping->info.dir.parent_mapping_index = parent_mapping_index;
2442
2443 if (first_cluster == 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002444 old_cluster_count = new_cluster_count =
2445 s->last_cluster_of_root_directory;
bellarda0464332005-12-18 18:29:50 +00002446 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002447 for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2448 c = fat_get(s, c))
2449 old_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002450
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002451 for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2452 c = modified_fat_get(s, c))
2453 new_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002454 }
2455
2456 if (new_cluster_count > old_cluster_count) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002457 if (insert_direntries(s,
2458 current_dir_index + factor * old_cluster_count,
2459 factor * (new_cluster_count - old_cluster_count)) == NULL)
2460 return -1;
bellarda0464332005-12-18 18:29:50 +00002461 } else if (new_cluster_count < old_cluster_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002462 remove_direntries(s,
2463 current_dir_index + factor * new_cluster_count,
2464 factor * (old_cluster_count - new_cluster_count));
bellarda0464332005-12-18 18:29:50 +00002465
2466 for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
Kevin Wolfebb72c92016-04-27 14:11:38 +02002467 direntry_t *first_direntry;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002468 void* direntry = array_get(&(s->directory), current_dir_index);
2469 int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
2470 s->sectors_per_cluster);
2471 if (ret)
2472 return ret;
Kevin Wolfebb72c92016-04-27 14:11:38 +02002473
2474 /* The first directory entry on the filesystem is the volume name */
2475 first_direntry = (direntry_t*) s->directory.pointer;
2476 assert(!memcmp(first_direntry->name, s->volume_label, 11));
2477
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002478 current_dir_index += factor;
bellarda0464332005-12-18 18:29:50 +00002479 }
2480
2481 ret = commit_mappings(s, first_cluster, dir_index);
2482 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002483 return ret;
bellarda0464332005-12-18 18:29:50 +00002484
2485 /* recurse */
2486 for (i = 0; i < factor * new_cluster_count; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002487 direntry = array_get(&(s->directory), first_dir_index + i);
2488 if (is_directory(direntry) && !is_dot(direntry)) {
2489 mapping = find_mapping_for_cluster(s, first_cluster);
2490 assert(mapping->mode & MODE_DIRECTORY);
2491 ret = commit_direntries(s, first_dir_index + i,
2492 array_index(&(s->mapping), mapping));
2493 if (ret)
2494 return ret;
2495 }
bellardde167e42005-04-28 21:15:08 +00002496 }
bellarda0464332005-12-18 18:29:50 +00002497
bellardde167e42005-04-28 21:15:08 +00002498 return 0;
2499}
2500
bellarda0464332005-12-18 18:29:50 +00002501/* commit one file (adjust contents, adjust mapping),
2502 return first_mapping_index */
2503static int commit_one_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002504 int dir_index, uint32_t offset)
bellarda0464332005-12-18 18:29:50 +00002505{
Anthony Liguoric227f092009-10-01 16:12:16 -05002506 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002507 uint32_t c = begin_of_direntry(direntry);
2508 uint32_t first_cluster = c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002509 mapping_t* mapping = find_mapping_for_cluster(s, c);
bellarda0464332005-12-18 18:29:50 +00002510 uint32_t size = filesize_of_direntry(direntry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002511 char* cluster = g_malloc(s->cluster_size);
bellarda0464332005-12-18 18:29:50 +00002512 uint32_t i;
2513 int fd = 0;
2514
2515 assert(offset < size);
2516 assert((offset % s->cluster_size) == 0);
2517
2518 for (i = s->cluster_size; i < offset; i += s->cluster_size)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002519 c = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002520
Corey Bryant6165f4d2012-08-14 16:43:45 -04002521 fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
bellarda0464332005-12-18 18:29:50 +00002522 if (fd < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002523 fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
2524 strerror(errno), errno);
Stefan Weilce137822011-09-30 23:29:53 +02002525 g_free(cluster);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002526 return fd;
bellarda0464332005-12-18 18:29:50 +00002527 }
Stefan Weilce137822011-09-30 23:29:53 +02002528 if (offset > 0) {
2529 if (lseek(fd, offset, SEEK_SET) != offset) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002530 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002531 g_free(cluster);
2532 return -3;
2533 }
2534 }
bellarda0464332005-12-18 18:29:50 +00002535
2536 while (offset < size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002537 uint32_t c1;
2538 int rest_size = (size - offset > s->cluster_size ?
2539 s->cluster_size : size - offset);
2540 int ret;
bellarda0464332005-12-18 18:29:50 +00002541
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002542 c1 = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002543
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002544 assert((size - offset == 0 && fat_eof(s, c)) ||
2545 (size > offset && c >=2 && !fat_eof(s, c)));
bellarda0464332005-12-18 18:29:50 +00002546
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002547 ret = vvfat_read(s->bs, cluster2sector(s, c),
Marc-André Lureau78ee96d2017-06-22 13:04:16 +02002548 (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
bellarda0464332005-12-18 18:29:50 +00002549
Stefan Weilce137822011-09-30 23:29:53 +02002550 if (ret < 0) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002551 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002552 g_free(cluster);
2553 return ret;
2554 }
bellarda0464332005-12-18 18:29:50 +00002555
Stefan Weilce137822011-09-30 23:29:53 +02002556 if (write(fd, cluster, rest_size) < 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 -2;
2560 }
bellarda0464332005-12-18 18:29:50 +00002561
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002562 offset += rest_size;
2563 c = c1;
bellarda0464332005-12-18 18:29:50 +00002564 }
2565
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002566 if (ftruncate(fd, size)) {
2567 perror("ftruncate()");
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002568 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002569 g_free(cluster);
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002570 return -4;
2571 }
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002572 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002573 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002574
2575 return commit_mappings(s, first_cluster, dir_index);
2576}
2577
2578#ifdef DEBUG
2579/* test, if all mappings point to valid direntries */
2580static void check1(BDRVVVFATState* s)
2581{
2582 int i;
2583 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002584 mapping_t* mapping = array_get(&(s->mapping), i);
2585 if (mapping->mode & MODE_DELETED) {
2586 fprintf(stderr, "deleted\n");
2587 continue;
2588 }
2589 assert(mapping->dir_index < s->directory.next);
2590 direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
2591 assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
2592 if (mapping->mode & MODE_DIRECTORY) {
2593 assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
2594 assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
2595 }
bellarda0464332005-12-18 18:29:50 +00002596 }
2597}
2598
2599/* test, if all direntries have mappings */
2600static void check2(BDRVVVFATState* s)
2601{
2602 int i;
2603 int first_mapping = -1;
2604
2605 for (i = 0; i < s->directory.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002606 direntry_t* direntry = array_get(&(s->directory), i);
bellarda0464332005-12-18 18:29:50 +00002607
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002608 if (is_short_name(direntry) && begin_of_direntry(direntry)) {
2609 mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
2610 assert(mapping);
2611 assert(mapping->dir_index == i || is_dot(direntry));
2612 assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
2613 }
bellarda0464332005-12-18 18:29:50 +00002614
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002615 if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
2616 /* cluster start */
2617 int j, count = 0;
bellarda0464332005-12-18 18:29:50 +00002618
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002619 for (j = 0; j < s->mapping.next; j++) {
2620 mapping_t* mapping = array_get(&(s->mapping), j);
2621 if (mapping->mode & MODE_DELETED)
2622 continue;
2623 if (mapping->mode & MODE_DIRECTORY) {
2624 if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
2625 assert(++count == 1);
2626 if (mapping->first_mapping_index == -1)
2627 first_mapping = array_index(&(s->mapping), mapping);
2628 else
2629 assert(first_mapping == mapping->first_mapping_index);
2630 if (mapping->info.dir.parent_mapping_index < 0)
2631 assert(j == 0);
2632 else {
2633 mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
2634 assert(parent->mode & MODE_DIRECTORY);
2635 assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
2636 }
2637 }
2638 }
2639 }
2640 if (count == 0)
2641 first_mapping = -1;
2642 }
bellarda0464332005-12-18 18:29:50 +00002643 }
2644}
2645#endif
2646
2647static int handle_renames_and_mkdirs(BDRVVVFATState* s)
2648{
2649 int i;
2650
2651#ifdef DEBUG
2652 fprintf(stderr, "handle_renames\n");
2653 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002654 commit_t* commit = array_get(&(s->commits), i);
2655 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 +00002656 }
2657#endif
2658
2659 for (i = 0; i < s->commits.next;) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002660 commit_t* commit = array_get(&(s->commits), i);
2661 if (commit->action == ACTION_RENAME) {
2662 mapping_t* mapping = find_mapping_for_cluster(s,
2663 commit->param.rename.cluster);
2664 char* old_path = mapping->path;
bellarda0464332005-12-18 18:29:50 +00002665
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002666 assert(commit->path);
2667 mapping->path = commit->path;
2668 if (rename(old_path, mapping->path))
2669 return -2;
bellarda0464332005-12-18 18:29:50 +00002670
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002671 if (mapping->mode & MODE_DIRECTORY) {
2672 int l1 = strlen(mapping->path);
2673 int l2 = strlen(old_path);
2674 int diff = l1 - l2;
2675 direntry_t* direntry = array_get(&(s->directory),
2676 mapping->info.dir.first_dir_index);
2677 uint32_t c = mapping->begin;
2678 int i = 0;
bellarda0464332005-12-18 18:29:50 +00002679
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002680 /* recurse */
2681 while (!fat_eof(s, c)) {
2682 do {
2683 direntry_t* d = direntry + i;
bellarda0464332005-12-18 18:29:50 +00002684
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002685 if (is_file(d) || (is_directory(d) && !is_dot(d))) {
2686 mapping_t* m = find_mapping_for_cluster(s,
2687 begin_of_direntry(d));
2688 int l = strlen(m->path);
2689 char* new_path = g_malloc(l + diff + 1);
bellarda0464332005-12-18 18:29:50 +00002690
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002691 assert(!strncmp(m->path, mapping->path, l2));
bellarda0464332005-12-18 18:29:50 +00002692
blueswir1363a37d2008-08-21 17:58:08 +00002693 pstrcpy(new_path, l + diff + 1, mapping->path);
2694 pstrcpy(new_path + l1, l + diff + 1 - l1,
2695 m->path + l2);
bellarda0464332005-12-18 18:29:50 +00002696
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002697 schedule_rename(s, m->begin, new_path);
2698 }
2699 i++;
2700 } while((i % (0x10 * s->sectors_per_cluster)) != 0);
2701 c = fat_get(s, c);
2702 }
2703 }
bellarda0464332005-12-18 18:29:50 +00002704
Stefan Weilce137822011-09-30 23:29:53 +02002705 g_free(old_path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002706 array_remove(&(s->commits), i);
2707 continue;
2708 } else if (commit->action == ACTION_MKDIR) {
2709 mapping_t* mapping;
2710 int j, parent_path_len;
bellarda0464332005-12-18 18:29:50 +00002711
bellard48c2f062005-12-19 22:11:49 +00002712#ifdef __MINGW32__
2713 if (mkdir(commit->path))
2714 return -5;
2715#else
2716 if (mkdir(commit->path, 0755))
2717 return -5;
2718#endif
bellarda0464332005-12-18 18:29:50 +00002719
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002720 mapping = insert_mapping(s, commit->param.mkdir.cluster,
2721 commit->param.mkdir.cluster + 1);
2722 if (mapping == NULL)
2723 return -6;
bellarda0464332005-12-18 18:29:50 +00002724
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002725 mapping->mode = MODE_DIRECTORY;
2726 mapping->read_only = 0;
2727 mapping->path = commit->path;
2728 j = s->directory.next;
2729 assert(j);
2730 insert_direntries(s, s->directory.next,
2731 0x10 * s->sectors_per_cluster);
2732 mapping->info.dir.first_dir_index = j;
bellarda0464332005-12-18 18:29:50 +00002733
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002734 parent_path_len = strlen(commit->path)
2735 - strlen(get_basename(commit->path)) - 1;
2736 for (j = 0; j < s->mapping.next; j++) {
2737 mapping_t* m = array_get(&(s->mapping), j);
2738 if (m->first_mapping_index < 0 && m != mapping &&
2739 !strncmp(m->path, mapping->path, parent_path_len) &&
2740 strlen(m->path) == parent_path_len)
2741 break;
2742 }
2743 assert(j < s->mapping.next);
2744 mapping->info.dir.parent_mapping_index = j;
bellarda0464332005-12-18 18:29:50 +00002745
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002746 array_remove(&(s->commits), i);
2747 continue;
2748 }
bellarda0464332005-12-18 18:29:50 +00002749
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002750 i++;
bellarda0464332005-12-18 18:29:50 +00002751 }
2752 return 0;
2753}
2754
2755/*
2756 * TODO: make sure that the short name is not matching *another* file
2757 */
2758static int handle_commits(BDRVVVFATState* s)
2759{
2760 int i, fail = 0;
2761
2762 vvfat_close_current_file(s);
2763
2764 for (i = 0; !fail && i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002765 commit_t* commit = array_get(&(s->commits), i);
2766 switch(commit->action) {
2767 case ACTION_RENAME: case ACTION_MKDIR:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002768 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002769 fail = -2;
2770 break;
2771 case ACTION_WRITEOUT: {
Blue Swirla6c6f762010-03-13 14:18:50 +00002772#ifndef NDEBUG
2773 /* these variables are only used by assert() below */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002774 direntry_t* entry = array_get(&(s->directory),
2775 commit->param.writeout.dir_index);
2776 uint32_t begin = begin_of_direntry(entry);
2777 mapping_t* mapping = find_mapping_for_cluster(s, begin);
Blue Swirla6c6f762010-03-13 14:18:50 +00002778#endif
bellarda0464332005-12-18 18:29:50 +00002779
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002780 assert(mapping);
2781 assert(mapping->begin == begin);
2782 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00002783
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002784 if (commit_one_file(s, commit->param.writeout.dir_index,
2785 commit->param.writeout.modified_offset))
2786 fail = -3;
bellarda0464332005-12-18 18:29:50 +00002787
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002788 break;
2789 }
2790 case ACTION_NEW_FILE: {
2791 int begin = commit->param.new_file.first_cluster;
2792 mapping_t* mapping = find_mapping_for_cluster(s, begin);
2793 direntry_t* entry;
2794 int i;
bellarda0464332005-12-18 18:29:50 +00002795
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002796 /* find direntry */
2797 for (i = 0; i < s->directory.next; i++) {
2798 entry = array_get(&(s->directory), i);
2799 if (is_file(entry) && begin_of_direntry(entry) == begin)
2800 break;
2801 }
bellarda0464332005-12-18 18:29:50 +00002802
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002803 if (i >= s->directory.next) {
2804 fail = -6;
2805 continue;
2806 }
bellarda0464332005-12-18 18:29:50 +00002807
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002808 /* make sure there exists an initial mapping */
2809 if (mapping && mapping->begin != begin) {
2810 mapping->end = begin;
2811 mapping = NULL;
2812 }
2813 if (mapping == NULL) {
2814 mapping = insert_mapping(s, begin, begin+1);
2815 }
2816 /* most members will be fixed in commit_mappings() */
2817 assert(commit->path);
2818 mapping->path = commit->path;
2819 mapping->read_only = 0;
2820 mapping->mode = MODE_NORMAL;
2821 mapping->info.file.offset = 0;
bellarda0464332005-12-18 18:29:50 +00002822
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002823 if (commit_one_file(s, i, 0))
2824 fail = -7;
bellarda0464332005-12-18 18:29:50 +00002825
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002826 break;
2827 }
2828 default:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002829 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002830 }
bellarda0464332005-12-18 18:29:50 +00002831 }
2832 if (i > 0 && array_remove_slice(&(s->commits), 0, i))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002833 return -1;
bellarda0464332005-12-18 18:29:50 +00002834 return fail;
2835}
2836
2837static int handle_deletes(BDRVVVFATState* s)
2838{
2839 int i, deferred = 1, deleted = 1;
2840
2841 /* delete files corresponding to mappings marked as deleted */
2842 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2843 while (deferred && deleted) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002844 deferred = 0;
2845 deleted = 0;
bellarda0464332005-12-18 18:29:50 +00002846
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002847 for (i = 1; i < s->mapping.next; i++) {
2848 mapping_t* mapping = array_get(&(s->mapping), i);
2849 if (mapping->mode & MODE_DELETED) {
2850 direntry_t* entry = array_get(&(s->directory),
2851 mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +00002852
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002853 if (is_free(entry)) {
2854 /* remove file/directory */
2855 if (mapping->mode & MODE_DIRECTORY) {
2856 int j, next_dir_index = s->directory.next,
2857 first_dir_index = mapping->info.dir.first_dir_index;
bellarda0464332005-12-18 18:29:50 +00002858
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002859 if (rmdir(mapping->path) < 0) {
2860 if (errno == ENOTEMPTY) {
2861 deferred++;
2862 continue;
2863 } else
2864 return -5;
2865 }
bellarda0464332005-12-18 18:29:50 +00002866
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002867 for (j = 1; j < s->mapping.next; j++) {
2868 mapping_t* m = array_get(&(s->mapping), j);
2869 if (m->mode & MODE_DIRECTORY &&
2870 m->info.dir.first_dir_index >
2871 first_dir_index &&
2872 m->info.dir.first_dir_index <
2873 next_dir_index)
2874 next_dir_index =
2875 m->info.dir.first_dir_index;
2876 }
2877 remove_direntries(s, first_dir_index,
2878 next_dir_index - first_dir_index);
bellarda0464332005-12-18 18:29:50 +00002879
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002880 deleted++;
2881 }
2882 } else {
2883 if (unlink(mapping->path))
2884 return -4;
2885 deleted++;
2886 }
2887 DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
2888 remove_mapping(s, i);
2889 }
2890 }
bellarda0464332005-12-18 18:29:50 +00002891 }
2892
2893 return 0;
2894}
2895
2896/*
2897 * synchronize mapping with new state:
2898 *
2899 * - copy FAT (with bdrv_read)
2900 * - mark all filenames corresponding to mappings as deleted
2901 * - recurse direntries from root (using bs->bdrv_read)
2902 * - delete files corresponding to mappings marked as deleted
2903 */
2904static int do_commit(BDRVVVFATState* s)
2905{
2906 int ret = 0;
2907
2908 /* the real meat are the commits. Nothing to do? Move along! */
2909 if (s->commits.next == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002910 return 0;
bellarda0464332005-12-18 18:29:50 +00002911
2912 vvfat_close_current_file(s);
2913
2914 ret = handle_renames_and_mkdirs(s);
2915 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002916 fprintf(stderr, "Error handling renames (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002917 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002918 return ret;
bellarda0464332005-12-18 18:29:50 +00002919 }
2920
ths5fafdf22007-09-16 21:08:06 +00002921 /* copy FAT (with bdrv_read) */
bellarda0464332005-12-18 18:29:50 +00002922 memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
2923
2924 /* recurse direntries from root (using bs->bdrv_read) */
2925 ret = commit_direntries(s, 0, -1);
2926 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002927 fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002928 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002929 return ret;
bellarda0464332005-12-18 18:29:50 +00002930 }
2931
2932 ret = handle_commits(s);
2933 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002934 fprintf(stderr, "Error handling commits (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002935 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002936 return ret;
bellarda0464332005-12-18 18:29:50 +00002937 }
2938
2939 ret = handle_deletes(s);
2940 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002941 fprintf(stderr, "Error deleting\n");
Blue Swirl43dc2a62010-03-18 18:41:57 +00002942 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002943 return ret;
bellarda0464332005-12-18 18:29:50 +00002944 }
2945
Kevin Wolfeecc7742016-05-30 17:13:09 +02002946 if (s->qcow->bs->drv->bdrv_make_empty) {
2947 s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
Kevin Wolf7704df92011-11-08 10:50:12 +01002948 }
bellarda0464332005-12-18 18:29:50 +00002949
2950 memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
2951
2952DLOG(checkpoint());
2953 return 0;
2954}
2955
2956static int try_commit(BDRVVVFATState* s)
2957{
2958 vvfat_close_current_file(s);
2959DLOG(checkpoint());
2960 if(!is_consistent(s))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002961 return -1;
bellarda0464332005-12-18 18:29:50 +00002962 return do_commit(s);
2963}
2964
ths5fafdf22007-09-16 21:08:06 +00002965static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00002966 const uint8_t *buf, int nb_sectors)
2967{
ths5fafdf22007-09-16 21:08:06 +00002968 BDRVVVFATState *s = bs->opaque;
bellarda0464332005-12-18 18:29:50 +00002969 int i, ret;
bellardde167e42005-04-28 21:15:08 +00002970
bellarda0464332005-12-18 18:29:50 +00002971DLOG(checkpoint());
bellardde167e42005-04-28 21:15:08 +00002972
Kevin Wolfac48e382010-09-10 12:27:02 +02002973 /* Check if we're operating in read-only mode */
2974 if (s->qcow == NULL) {
2975 return -EACCES;
2976 }
2977
bellarda0464332005-12-18 18:29:50 +00002978 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002979
bellarda0464332005-12-18 18:29:50 +00002980 /*
2981 * Some sanity checks:
2982 * - do not allow writing to the boot sector
bellarda0464332005-12-18 18:29:50 +00002983 */
bellardde167e42005-04-28 21:15:08 +00002984
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02002985 if (sector_num < s->offset_to_fat)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002986 return -1;
bellardde167e42005-04-28 21:15:08 +00002987
bellarda0464332005-12-18 18:29:50 +00002988 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002989 i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
2990 mapping_t* mapping = find_mapping_for_cluster(s, i);
2991 if (mapping) {
2992 if (mapping->read_only) {
2993 fprintf(stderr, "Tried to write to write-protected file %s\n",
2994 mapping->path);
2995 return -1;
2996 }
bellardde167e42005-04-28 21:15:08 +00002997
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002998 if (mapping->mode & MODE_DIRECTORY) {
2999 int begin = cluster2sector(s, i);
3000 int end = begin + s->sectors_per_cluster, k;
3001 int dir_index;
3002 const direntry_t* direntries;
3003 long_file_name lfn;
bellardde167e42005-04-28 21:15:08 +00003004
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003005 lfn_init(&lfn);
bellardde167e42005-04-28 21:15:08 +00003006
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003007 if (begin < sector_num)
3008 begin = sector_num;
3009 if (end > sector_num + nb_sectors)
3010 end = sector_num + nb_sectors;
3011 dir_index = mapping->dir_index +
3012 0x10 * (begin - mapping->begin * s->sectors_per_cluster);
3013 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
bellardde167e42005-04-28 21:15:08 +00003014
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003015 for (k = 0; k < (end - begin) * 0x10; k++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003016 /* no access to the direntry of a read-only file */
Hervé Poussineaue03da262017-07-15 15:28:40 +02003017 if (is_short_name(direntries + k) &&
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003018 (direntries[k].attributes & 1)) {
3019 if (memcmp(direntries + k,
3020 array_get(&(s->directory), dir_index + k),
3021 sizeof(direntry_t))) {
Alistair Francis2ab4b132017-09-11 12:52:50 -07003022 warn_report("tried to write to write-protected "
3023 "file");
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003024 return -1;
3025 }
3026 }
3027 }
3028 }
3029 i = mapping->end;
3030 } else
3031 i++;
bellardde167e42005-04-28 21:15:08 +00003032 }
bellarda0464332005-12-18 18:29:50 +00003033
3034 /*
3035 * Use qcow backend. Commit later.
3036 */
3037DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
Kevin Wolf18d51c42016-05-31 14:42:08 +02003038 ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
bellarda0464332005-12-18 18:29:50 +00003039 if (ret < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003040 fprintf(stderr, "Error writing to qcow backend\n");
3041 return ret;
bellarda0464332005-12-18 18:29:50 +00003042 }
3043
3044 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003045 i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
3046 if (i >= 0)
3047 s->used_clusters[i] |= USED_ALLOCATED;
bellarda0464332005-12-18 18:29:50 +00003048
3049DLOG(checkpoint());
3050 /* TODO: add timeout */
3051 try_commit(s);
3052
3053DLOG(checkpoint());
3054 return 0;
3055}
3056
Kevin Wolf4575eb42016-04-26 17:14:08 +02003057static int coroutine_fn
3058vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3059 QEMUIOVector *qiov, int flags)
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003060{
3061 int ret;
3062 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02003063 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
3064 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
3065 void *buf;
3066
3067 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3068 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
3069
3070 buf = g_try_malloc(bytes);
3071 if (bytes && buf == NULL) {
3072 return -ENOMEM;
3073 }
3074 qemu_iovec_to_buf(qiov, 0, buf, bytes);
3075
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003076 qemu_co_mutex_lock(&s->lock);
3077 ret = vvfat_write(bs, sector_num, buf, nb_sectors);
3078 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02003079
3080 g_free(buf);
3081
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003082 return ret;
3083}
3084
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003085static int64_t coroutine_fn vvfat_co_get_block_status(BlockDriverState *bs,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003086 int64_t sector_num, int nb_sectors, int *n, BlockDriverState **file)
bellarda0464332005-12-18 18:29:50 +00003087{
Hervé Poussineau139921a2017-05-22 23:11:53 +02003088 *n = bs->total_sectors - sector_num;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02003089 if (*n > nb_sectors) {
3090 *n = nb_sectors;
3091 } else if (*n < 0) {
3092 return 0;
3093 }
3094 return BDRV_BLOCK_DATA;
bellarda0464332005-12-18 18:29:50 +00003095}
3096
Kevin Wolf4575eb42016-04-26 17:14:08 +02003097static int coroutine_fn
3098write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3099 QEMUIOVector *qiov, int flags)
3100{
Paolo Bonzini254aee42017-06-29 15:27:43 +02003101 int ret;
3102
Kevin Wolf9217e262010-09-10 12:27:03 +02003103 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Paolo Bonzini254aee42017-06-29 15:27:43 +02003104 qemu_co_mutex_lock(&s->lock);
3105 ret = try_commit(s);
3106 qemu_co_mutex_unlock(&s->lock);
3107
3108 return ret;
bellarda0464332005-12-18 18:29:50 +00003109}
3110
3111static void write_target_close(BlockDriverState *bs) {
Kevin Wolf9217e262010-09-10 12:27:03 +02003112 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Kevin Wolfeecc7742016-05-30 17:13:09 +02003113 bdrv_unref_child(s->bs, s->qcow);
Stefan Weilce137822011-09-30 23:29:53 +02003114 g_free(s->qcow_filename);
bellarda0464332005-12-18 18:29:50 +00003115}
3116
3117static BlockDriver vvfat_write_target = {
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003118 .format_name = "vvfat_write_target",
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003119 .instance_size = sizeof(void*),
Kevin Wolf4575eb42016-04-26 17:14:08 +02003120 .bdrv_co_pwritev = write_target_commit,
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003121 .bdrv_close = write_target_close,
bellarda0464332005-12-18 18:29:50 +00003122};
3123
Kevin Wolfeecc7742016-05-30 17:13:09 +02003124static void vvfat_qcow_options(int *child_flags, QDict *child_options,
3125 int parent_flags, QDict *parent_options)
bellarda0464332005-12-18 18:29:50 +00003126{
Alberto Garciaf87a0e22016-09-15 17:53:02 +03003127 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
3128 *child_flags = BDRV_O_NO_FLUSH;
Kevin Wolfeecc7742016-05-30 17:13:09 +02003129}
3130
3131static const BdrvChildRole child_vvfat_qcow = {
3132 .inherit_options = vvfat_qcow_options,
3133};
3134
3135static int enable_write_target(BlockDriverState *bs, Error **errp)
3136{
3137 BDRVVVFATState *s = bs->opaque;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003138 BlockDriver *bdrv_qcow = NULL;
Kevin Wolf5db15a52015-09-14 15:33:33 +02003139 BlockDriverState *backing;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003140 QemuOpts *opts = NULL;
Kevin Wolfa6552112010-09-10 12:27:04 +02003141 int ret;
bellarda0464332005-12-18 18:29:50 +00003142 int size = sector2cluster(s, s->sector_count);
Max Reitze6641712015-08-26 19:47:48 +02003143 QDict *options;
3144
bellarda0464332005-12-18 18:29:50 +00003145 s->used_clusters = calloc(size, 1);
3146
Anthony Liguoric227f092009-10-01 16:12:16 -05003147 array_init(&(s->commits), sizeof(commit_t));
bellarda0464332005-12-18 18:29:50 +00003148
Jeff Cody9a29e182015-01-22 08:03:30 -05003149 s->qcow_filename = g_malloc(PATH_MAX);
3150 ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
Jim Meyeringeba25052012-05-28 09:27:54 +02003151 if (ret < 0) {
Markus Armbruster68c70af2014-05-16 11:00:17 +02003152 error_setg_errno(errp, -ret, "can't create temporary file");
Fam Zheng78f27bd2013-07-17 17:57:37 +08003153 goto err;
Jim Meyeringeba25052012-05-28 09:27:54 +02003154 }
Kevin Wolf91a073a2009-05-27 14:48:06 +02003155
3156 bdrv_qcow = bdrv_find_format("qcow");
Max Reitz1bcb15c2014-12-02 18:32:43 +01003157 if (!bdrv_qcow) {
3158 error_setg(errp, "Failed to locate qcow driver");
3159 ret = -ENOENT;
3160 goto err;
3161 }
3162
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003163 opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003164 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
3165 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003166 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
Kevin Wolf91a073a2009-05-27 14:48:06 +02003167
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003168 ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
Chunyan Liufacdbb02014-06-05 17:20:52 +08003169 qemu_opts_del(opts);
Fam Zheng78f27bd2013-07-17 17:57:37 +08003170 if (ret < 0) {
3171 goto err;
3172 }
Kevin Wolfa6552112010-09-10 12:27:04 +02003173
Max Reitze6641712015-08-26 19:47:48 +02003174 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003175 qdict_put_str(options, "write-target.driver", "qcow");
Kevin Wolfeecc7742016-05-30 17:13:09 +02003176 s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
3177 &child_vvfat_qcow, false, errp);
Max Reitzc4b48bf2016-07-11 15:54:52 +02003178 QDECREF(options);
Max Reitz5b363932016-05-17 16:41:31 +02003179 if (!s->qcow) {
3180 ret = -EINVAL;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003181 goto err;
Kevin Wolfd6e90982010-03-31 14:40:27 +02003182 }
bellarda0464332005-12-18 18:29:50 +00003183
3184#ifndef _WIN32
3185 unlink(s->qcow_filename);
3186#endif
3187
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003188 backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
3189 &error_abort);
3190 *(void**) backing->opaque = s;
3191
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003192 bdrv_set_backing_hd(s->bs, backing, &error_abort);
Kevin Wolf5db15a52015-09-14 15:33:33 +02003193 bdrv_unref(backing);
3194
bellardde167e42005-04-28 21:15:08 +00003195 return 0;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003196
3197err:
3198 g_free(s->qcow_filename);
3199 s->qcow_filename = NULL;
3200 return ret;
bellardde167e42005-04-28 21:15:08 +00003201}
3202
Kevin Wolf91ef3822016-12-20 16:23:46 +01003203static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
3204 const BdrvChildRole *role,
Kevin Wolfe0995dc2017-09-14 12:47:11 +02003205 BlockReopenQueue *reopen_queue,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003206 uint64_t perm, uint64_t shared,
3207 uint64_t *nperm, uint64_t *nshared)
3208{
3209 BDRVVVFATState *s = bs->opaque;
3210
3211 assert(c == s->qcow || role == &child_backing);
3212
3213 if (c == s->qcow) {
3214 /* This is a private node, nobody should try to attach to it */
3215 *nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
3216 *nshared = BLK_PERM_WRITE_UNCHANGED;
3217 } else {
3218 /* The backing file is there so 'commit' can use it. vvfat doesn't
3219 * access it in any way. */
3220 *nperm = 0;
3221 *nshared = BLK_PERM_ALL;
3222 }
3223}
3224
bellardde167e42005-04-28 21:15:08 +00003225static void vvfat_close(BlockDriverState *bs)
3226{
3227 BDRVVVFATState *s = bs->opaque;
3228
3229 vvfat_close_current_file(s);
3230 array_free(&(s->fat));
3231 array_free(&(s->directory));
3232 array_free(&(s->mapping));
Stefan Weilce137822011-09-30 23:29:53 +02003233 g_free(s->cluster_buffer);
Kevin Wolf3397f0c2011-11-22 16:52:13 +01003234
3235 if (s->qcow) {
3236 migrate_del_blocker(s->migration_blocker);
3237 error_free(s->migration_blocker);
3238 }
bellardde167e42005-04-28 21:15:08 +00003239}
3240
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003241static BlockDriver bdrv_vvfat = {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003242 .format_name = "vvfat",
3243 .protocol_name = "fat",
3244 .instance_size = sizeof(BDRVVVFATState),
3245
3246 .bdrv_parse_filename = vvfat_parse_filename,
3247 .bdrv_file_open = vvfat_open,
Eric Blakea6506482016-06-23 16:37:17 -06003248 .bdrv_refresh_limits = vvfat_refresh_limits,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003249 .bdrv_close = vvfat_close,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003250 .bdrv_child_perm = vvfat_child_perm,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003251
Kevin Wolf4575eb42016-04-26 17:14:08 +02003252 .bdrv_co_preadv = vvfat_co_preadv,
3253 .bdrv_co_pwritev = vvfat_co_pwritev,
Paolo Bonzinib6b8a332013-09-04 19:00:28 +02003254 .bdrv_co_get_block_status = vvfat_co_get_block_status,
bellardde167e42005-04-28 21:15:08 +00003255};
3256
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003257static void bdrv_vvfat_init(void)
3258{
3259 bdrv_register(&bdrv_vvfat);
3260}
3261
3262block_init(bdrv_vvfat_init);
3263
bellarda0464332005-12-18 18:29:50 +00003264#ifdef DEBUG
Thomas Huth7a6ab452017-09-13 12:21:28 +02003265static void checkpoint(void)
3266{
Anthony Liguoric227f092009-10-01 16:12:16 -05003267 assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
bellarda0464332005-12-18 18:29:50 +00003268 check1(vvv);
3269 check2(vvv);
3270 assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
bellarda0464332005-12-18 18:29:50 +00003271}
3272#endif