blob: 5f6678789047690c7b7c1de21c4a8d5c963fbea1 [file] [log] [blame]
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001/* vim:set shiftwidth=4 ts=4: */
bellardde167e42005-04-28 21:15:08 +00002/*
3 * QEMU Block driver for virtual VFAT (shadows a local directory)
ths5fafdf22007-09-16 21:08:06 +00004 *
bellarda0464332005-12-18 18:29:50 +00005 * Copyright (c) 2004,2005 Johannes E. Schindelin
ths5fafdf22007-09-16 21:08:06 +00006 *
bellardde167e42005-04-28 21:15:08 +00007 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010025
Peter Maydell80c71a22016-01-18 18:01:42 +000026#include "qemu/osdep.h"
bellardde167e42005-04-28 21:15:08 +000027#include <dirent.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010029#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020030#include "block/qdict.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010032#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010033#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020034#include "migration/blocker.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010035#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010036#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020037#include "qemu/cutils.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070038#include "qemu/error-report.h"
bellardde167e42005-04-28 21:15:08 +000039
bellarda0464332005-12-18 18:29:50 +000040#ifndef S_IWGRP
41#define S_IWGRP 0
42#endif
43#ifndef S_IWOTH
44#define S_IWOTH 0
45#endif
bellardde167e42005-04-28 21:15:08 +000046
bellarda0464332005-12-18 18:29:50 +000047/* TODO: add ":bootsector=blabla.img:" */
48/* LATER TODO: add automatic boot sector generation from
49 BOOTEASY.ASM and Ranish Partition Manager
ths5fafdf22007-09-16 21:08:06 +000050 Note that DOS assumes the system files to be the first files in the
bellarda0464332005-12-18 18:29:50 +000051 file system (test if the boot sector still relies on that fact)! */
52/* MAYBE TODO: write block-visofs.c */
53/* TODO: call try_commit() only after a timeout */
bellardde167e42005-04-28 21:15:08 +000054
bellarda0464332005-12-18 18:29:50 +000055/* #define DEBUG */
56
57#ifdef DEBUG
58
59#define DLOG(a) a
60
blueswir13f47aa82008-03-09 06:59:01 +000061static void checkpoint(void);
bellarda0464332005-12-18 18:29:50 +000062
bellarda0464332005-12-18 18:29:50 +000063#else
64
65#define DLOG(a)
66
67#endif
bellardde167e42005-04-28 21:15:08 +000068
Hervé Poussineau63d261c2017-07-15 15:28:39 +020069/* bootsector OEM name. see related compatibility problems at:
70 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
71 * http://seasip.info/Misc/oemid.html
72 */
73#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
74
Hervé Poussineau8c4517f2017-07-15 15:28:38 +020075#define DIR_DELETED 0xe5
76#define DIR_KANJI DIR_DELETED
77#define DIR_KANJI_FAKE 0x05
78#define DIR_FREE 0x00
79
bellardde167e42005-04-28 21:15:08 +000080/* dynamic array functions */
Anthony Liguoric227f092009-10-01 16:12:16 -050081typedef struct array_t {
bellardde167e42005-04-28 21:15:08 +000082 char* pointer;
83 unsigned int size,next,item_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050084} array_t;
bellardde167e42005-04-28 21:15:08 +000085
Anthony Liguoric227f092009-10-01 16:12:16 -050086static inline void array_init(array_t* array,unsigned int item_size)
bellardde167e42005-04-28 21:15:08 +000087{
blueswir1511d2b12009-03-07 15:32:56 +000088 array->pointer = NULL;
bellardde167e42005-04-28 21:15:08 +000089 array->size=0;
90 array->next=0;
91 array->item_size=item_size;
92}
93
Anthony Liguoric227f092009-10-01 16:12:16 -050094static inline void array_free(array_t* array)
bellardde167e42005-04-28 21:15:08 +000095{
Stefan Weilce137822011-09-30 23:29:53 +020096 g_free(array->pointer);
bellardde167e42005-04-28 21:15:08 +000097 array->size=array->next=0;
98}
99
bellarda0464332005-12-18 18:29:50 +0000100/* does not automatically grow */
Anthony Liguoric227f092009-10-01 16:12:16 -0500101static inline void* array_get(array_t* array,unsigned int index) {
bellarda0464332005-12-18 18:29:50 +0000102 assert(index < array->next);
Liam Merwick8d9401c2018-11-05 21:38:38 +0000103 assert(array->pointer);
bellarda0464332005-12-18 18:29:50 +0000104 return array->pointer + index * array->item_size;
105}
106
Liam Merwick8d9401c2018-11-05 21:38:38 +0000107static inline void array_ensure_allocated(array_t *array, int index)
bellarda0464332005-12-18 18:29:50 +0000108{
109 if((index + 1) * array->item_size > array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200110 int new_size = (index + 32) * array->item_size;
111 array->pointer = g_realloc(array->pointer, new_size);
Liam Merwick8d9401c2018-11-05 21:38:38 +0000112 assert(array->pointer);
Hervé Poussineauf80256b2017-07-15 15:28:41 +0200113 memset(array->pointer + array->size, 0, new_size - array->size);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200114 array->size = new_size;
115 array->next = index + 1;
bellardde167e42005-04-28 21:15:08 +0000116 }
bellardde167e42005-04-28 21:15:08 +0000117}
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119static inline void* array_get_next(array_t* array) {
bellarda0464332005-12-18 18:29:50 +0000120 unsigned int next = array->next;
bellarda0464332005-12-18 18:29:50 +0000121
Liam Merwick8d9401c2018-11-05 21:38:38 +0000122 array_ensure_allocated(array, next);
bellarda0464332005-12-18 18:29:50 +0000123 array->next = next + 1;
Eduardo Habkost9be38592016-06-13 18:57:58 -0300124 return array_get(array, next);
bellardde167e42005-04-28 21:15:08 +0000125}
126
Anthony Liguoric227f092009-10-01 16:12:16 -0500127static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
bellardde167e42005-04-28 21:15:08 +0000128 if((array->next+count)*array->item_size>array->size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200129 int increment=count*array->item_size;
130 array->pointer=g_realloc(array->pointer,array->size+increment);
131 if(!array->pointer)
blueswir1511d2b12009-03-07 15:32:56 +0000132 return NULL;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200133 array->size+=increment;
bellardde167e42005-04-28 21:15:08 +0000134 }
135 memmove(array->pointer+(index+count)*array->item_size,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200136 array->pointer+index*array->item_size,
137 (array->next-index)*array->item_size);
bellardde167e42005-04-28 21:15:08 +0000138 array->next+=count;
139 return array->pointer+index*array->item_size;
140}
141
142/* this performs a "roll", so that the element which was at index_from becomes
143 * index_to, but the order of all other elements is preserved. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500144static inline int array_roll(array_t* array,int index_to,int index_from,int count)
bellardde167e42005-04-28 21:15:08 +0000145{
146 char* buf;
147 char* from;
148 char* to;
149 int is;
150
151 if(!array ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200152 index_to<0 || index_to>=array->next ||
153 index_from<0 || index_from>=array->next)
154 return -1;
ths3b46e622007-09-17 08:09:54 +0000155
bellardde167e42005-04-28 21:15:08 +0000156 if(index_to==index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200157 return 0;
bellardde167e42005-04-28 21:15:08 +0000158
159 is=array->item_size;
160 from=array->pointer+index_from*is;
161 to=array->pointer+index_to*is;
Anthony Liguori7267c092011-08-20 22:09:37 -0500162 buf=g_malloc(is*count);
bellardde167e42005-04-28 21:15:08 +0000163 memcpy(buf,from,is*count);
164
165 if(index_to<index_from)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200166 memmove(to+is*count,to,from-to);
bellardde167e42005-04-28 21:15:08 +0000167 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200168 memmove(from,from+is*count,to-from);
ths3b46e622007-09-17 08:09:54 +0000169
bellardde167e42005-04-28 21:15:08 +0000170 memcpy(to,buf,is*count);
171
Stefan Weilce137822011-09-30 23:29:53 +0200172 g_free(buf);
bellardde167e42005-04-28 21:15:08 +0000173
174 return 0;
175}
176
Anthony Liguoric227f092009-10-01 16:12:16 -0500177static inline int array_remove_slice(array_t* array,int index, int count)
bellarda0464332005-12-18 18:29:50 +0000178{
179 assert(index >=0);
180 assert(count > 0);
181 assert(index + count <= array->next);
182 if(array_roll(array,array->next-1,index,count))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200183 return -1;
bellarda0464332005-12-18 18:29:50 +0000184 array->next -= count;
185 return 0;
186}
187
Anthony Liguoric227f092009-10-01 16:12:16 -0500188static int array_remove(array_t* array,int index)
bellardde167e42005-04-28 21:15:08 +0000189{
bellarda0464332005-12-18 18:29:50 +0000190 return array_remove_slice(array, index, 1);
191}
192
193/* return the index for a given member */
Anthony Liguoric227f092009-10-01 16:12:16 -0500194static int array_index(array_t* array, void* pointer)
bellarda0464332005-12-18 18:29:50 +0000195{
196 size_t offset = (char*)pointer - array->pointer;
bellarda0464332005-12-18 18:29:50 +0000197 assert((offset % array->item_size) == 0);
198 assert(offset/array->item_size < array->next);
199 return offset/array->item_size;
bellardde167e42005-04-28 21:15:08 +0000200}
201
202/* These structures are used to fake a disk and the VFAT filesystem.
Stefan Weil541dc0d2011-08-31 12:38:01 +0200203 * For this reason we need to use QEMU_PACKED. */
bellardde167e42005-04-28 21:15:08 +0000204
Anthony Liguoric227f092009-10-01 16:12:16 -0500205typedef struct bootsector_t {
bellardde167e42005-04-28 21:15:08 +0000206 uint8_t jump[3];
207 uint8_t name[8];
208 uint16_t sector_size;
209 uint8_t sectors_per_cluster;
210 uint16_t reserved_sectors;
211 uint8_t number_of_fats;
212 uint16_t root_entries;
bellarda0464332005-12-18 18:29:50 +0000213 uint16_t total_sectors16;
bellardde167e42005-04-28 21:15:08 +0000214 uint8_t media_type;
215 uint16_t sectors_per_fat;
216 uint16_t sectors_per_track;
217 uint16_t number_of_heads;
218 uint32_t hidden_sectors;
219 uint32_t total_sectors;
220 union {
221 struct {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200222 uint8_t drive_number;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200223 uint8_t reserved1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200224 uint8_t signature;
225 uint32_t id;
226 uint8_t volume_label[11];
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200227 uint8_t fat_type[8];
228 uint8_t ignored[0x1c0];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200229 } QEMU_PACKED fat16;
230 struct {
231 uint32_t sectors_per_fat;
232 uint16_t flags;
233 uint8_t major,minor;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200234 uint32_t first_cluster_of_root_dir;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200235 uint16_t info_sector;
236 uint16_t backup_boot_sector;
Hervé Poussineau92e28d82017-05-22 23:11:58 +0200237 uint8_t reserved[12];
238 uint8_t drive_number;
239 uint8_t reserved1;
240 uint8_t signature;
241 uint32_t id;
242 uint8_t volume_label[11];
243 uint8_t fat_type[8];
244 uint8_t ignored[0x1a4];
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200245 } QEMU_PACKED fat32;
bellardde167e42005-04-28 21:15:08 +0000246 } u;
bellardde167e42005-04-28 21:15:08 +0000247 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200248} QEMU_PACKED bootsector_t;
bellardde167e42005-04-28 21:15:08 +0000249
thsb5700942007-09-25 14:47:03 +0000250typedef struct {
251 uint8_t head;
252 uint8_t sector;
253 uint8_t cylinder;
Anthony Liguoric227f092009-10-01 16:12:16 -0500254} mbr_chs_t;
thsb5700942007-09-25 14:47:03 +0000255
Anthony Liguoric227f092009-10-01 16:12:16 -0500256typedef struct partition_t {
bellardde167e42005-04-28 21:15:08 +0000257 uint8_t attributes; /* 0x80 = bootable */
Anthony Liguoric227f092009-10-01 16:12:16 -0500258 mbr_chs_t start_CHS;
thsb5700942007-09-25 14:47:03 +0000259 uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
Anthony Liguoric227f092009-10-01 16:12:16 -0500260 mbr_chs_t end_CHS;
bellardde167e42005-04-28 21:15:08 +0000261 uint32_t start_sector_long;
thsb5700942007-09-25 14:47:03 +0000262 uint32_t length_sector_long;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200263} QEMU_PACKED partition_t;
bellardde167e42005-04-28 21:15:08 +0000264
Anthony Liguoric227f092009-10-01 16:12:16 -0500265typedef struct mbr_t {
thsb5700942007-09-25 14:47:03 +0000266 uint8_t ignored[0x1b8];
267 uint32_t nt_id;
268 uint8_t ignored2[2];
Anthony Liguoric227f092009-10-01 16:12:16 -0500269 partition_t partition[4];
bellardde167e42005-04-28 21:15:08 +0000270 uint8_t magic[2];
Stefan Weil541dc0d2011-08-31 12:38:01 +0200271} QEMU_PACKED mbr_t;
bellardde167e42005-04-28 21:15:08 +0000272
Anthony Liguoric227f092009-10-01 16:12:16 -0500273typedef struct direntry_t {
Stefan Weilf671d172013-12-11 21:37:11 +0100274 uint8_t name[8 + 3];
bellardde167e42005-04-28 21:15:08 +0000275 uint8_t attributes;
276 uint8_t reserved[2];
277 uint16_t ctime;
278 uint16_t cdate;
279 uint16_t adate;
280 uint16_t begin_hi;
281 uint16_t mtime;
282 uint16_t mdate;
283 uint16_t begin;
284 uint32_t size;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200285} QEMU_PACKED direntry_t;
bellardde167e42005-04-28 21:15:08 +0000286
287/* this structure are used to transparently access the files */
288
Anthony Liguoric227f092009-10-01 16:12:16 -0500289typedef struct mapping_t {
bellarda0464332005-12-18 18:29:50 +0000290 /* begin is the first cluster, end is the last+1 */
291 uint32_t begin,end;
bellardde167e42005-04-28 21:15:08 +0000292 /* as s->directory is growable, no pointer may be used here */
293 unsigned int dir_index;
bellarda0464332005-12-18 18:29:50 +0000294 /* the clusters of a file may be in any order; this points to the first */
295 int first_mapping_index;
296 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200297 /* offset is
298 * - the offset in the file (in clusters) for a file, or
Hervé Poussineauad05b312017-05-22 23:11:56 +0200299 * - the next cluster of the directory for a directory
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200300 */
301 struct {
302 uint32_t offset;
303 } file;
304 struct {
305 int parent_mapping_index;
306 int first_dir_index;
307 } dir;
bellarda0464332005-12-18 18:29:50 +0000308 } info;
309 /* path contains the full path, i.e. it always starts with s->path */
310 char* path;
311
Hervé Poussineauad05b312017-05-22 23:11:56 +0200312 enum {
313 MODE_UNDEFINED = 0,
314 MODE_NORMAL = 1,
315 MODE_MODIFIED = 2,
316 MODE_DIRECTORY = 4,
317 MODE_DELETED = 8,
318 } mode;
bellarda0464332005-12-18 18:29:50 +0000319 int read_only;
Anthony Liguoric227f092009-10-01 16:12:16 -0500320} mapping_t;
bellardde167e42005-04-28 21:15:08 +0000321
bellarda0464332005-12-18 18:29:50 +0000322#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -0500323static void print_direntry(const struct direntry_t*);
324static void print_mapping(const struct mapping_t* mapping);
bellarda0464332005-12-18 18:29:50 +0000325#endif
bellardde167e42005-04-28 21:15:08 +0000326
327/* here begins the real VVFAT driver */
328
329typedef struct BDRVVVFATState {
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200330 CoMutex lock;
bellarda0464332005-12-18 18:29:50 +0000331 BlockDriverState* bs; /* pointer to parent */
bellardde167e42005-04-28 21:15:08 +0000332 unsigned char first_sectors[0x40*0x200];
ths3b46e622007-09-17 08:09:54 +0000333
bellardde167e42005-04-28 21:15:08 +0000334 int fat_type; /* 16 or 32 */
Anthony Liguoric227f092009-10-01 16:12:16 -0500335 array_t fat,directory,mapping;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200336 char volume_label[11];
ths3b46e622007-09-17 08:09:54 +0000337
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200338 uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
339
bellardde167e42005-04-28 21:15:08 +0000340 unsigned int cluster_size;
341 unsigned int sectors_per_cluster;
342 unsigned int sectors_per_fat;
bellarda0464332005-12-18 18:29:50 +0000343 uint32_t last_cluster_of_root_directory;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200344 /* how many entries are available in root directory (0 for FAT32) */
345 uint16_t root_entries;
bellardde167e42005-04-28 21:15:08 +0000346 uint32_t sector_count; /* total number of sectors of the partition */
347 uint32_t cluster_count; /* total number of clusters of this partition */
bellardde167e42005-04-28 21:15:08 +0000348 uint32_t max_fat_value;
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200349 uint32_t offset_to_fat;
350 uint32_t offset_to_root_dir;
ths3b46e622007-09-17 08:09:54 +0000351
bellardde167e42005-04-28 21:15:08 +0000352 int current_fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500353 mapping_t* current_mapping;
bellarda0464332005-12-18 18:29:50 +0000354 unsigned char* cluster; /* points to current cluster */
355 unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
bellardde167e42005-04-28 21:15:08 +0000356 unsigned int current_cluster;
357
358 /* write support */
bellarda0464332005-12-18 18:29:50 +0000359 char* qcow_filename;
Kevin Wolfeecc7742016-05-30 17:13:09 +0200360 BdrvChild* qcow;
bellarda0464332005-12-18 18:29:50 +0000361 void* fat2;
362 char* used_clusters;
Anthony Liguoric227f092009-10-01 16:12:16 -0500363 array_t commits;
bellarda0464332005-12-18 18:29:50 +0000364 const char* path;
365 int downcase_short_names;
Kevin Wolf3397f0c2011-11-22 16:52:13 +0100366
367 Error *migration_blocker;
bellardde167e42005-04-28 21:15:08 +0000368} BDRVVVFATState;
369
thsb5700942007-09-25 14:47:03 +0000370/* take the sector position spos and convert it to Cylinder/Head/Sector position
371 * if the position is outside the specified geometry, fill maximum value for CHS
372 * and return 1 to signal overflow.
373 */
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200374static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
375{
thsb5700942007-09-25 14:47:03 +0000376 int head,sector;
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200377 sector = spos % secs; spos /= secs;
378 head = spos % heads; spos /= heads;
379 if (spos >= cyls) {
thsb5700942007-09-25 14:47:03 +0000380 /* Overflow,
381 it happens if 32bit sector positions are used, while CHS is only 24bit.
382 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
383 chs->head = 0xFF;
384 chs->sector = 0xFF;
385 chs->cylinder = 0xFF;
386 return 1;
387 }
388 chs->head = (uint8_t)head;
389 chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
390 chs->cylinder = (uint8_t)spos;
391 return 0;
392}
bellardde167e42005-04-28 21:15:08 +0000393
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200394static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
bellardde167e42005-04-28 21:15:08 +0000395{
396 /* TODO: if the files mbr.img and bootsect.img exist, use them */
Anthony Liguoric227f092009-10-01 16:12:16 -0500397 mbr_t* real_mbr=(mbr_t*)s->first_sectors;
398 partition_t* partition = &(real_mbr->partition[0]);
thsb5700942007-09-25 14:47:03 +0000399 int lba;
bellardde167e42005-04-28 21:15:08 +0000400
401 memset(s->first_sectors,0,512);
ths3b46e622007-09-17 08:09:54 +0000402
thsb5700942007-09-25 14:47:03 +0000403 /* Win NT Disk Signature */
404 real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
405
bellardde167e42005-04-28 21:15:08 +0000406 partition->attributes=0x80; /* bootable */
thsb5700942007-09-25 14:47:03 +0000407
408 /* LBA is used when partition is outside the CHS geometry */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200409 lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
Markus Armbruster4480e0f2012-07-10 11:12:29 +0200410 cyls, heads, secs);
411 lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
412 cyls, heads, secs);
thsb5700942007-09-25 14:47:03 +0000413
414 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200415 partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
Markus Armbrusterf91cbef2012-07-10 11:12:28 +0200416 partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200417 - s->offset_to_bootsector);
thsb5700942007-09-25 14:47:03 +0000418
bellarda0464332005-12-18 18:29:50 +0000419 /* FAT12/FAT16/FAT32 */
thsb5700942007-09-25 14:47:03 +0000420 /* DOS uses different types when partition is LBA,
421 probably to prevent older versions from using CHS on them */
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200422 partition->fs_type = s->fat_type == 12 ? 0x1 :
423 s->fat_type == 16 ? (lba ? 0xe : 0x06) :
424 /*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
bellardde167e42005-04-28 21:15:08 +0000425
426 real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
427}
428
bellarda0464332005-12-18 18:29:50 +0000429/* direntry functions */
430
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200431static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
bellardde167e42005-04-28 21:15:08 +0000432{
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200433 int number_of_entries, i;
434 glong length;
435 direntry_t *entry;
bellardde167e42005-04-28 21:15:08 +0000436
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200437 gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
438 if (!longname) {
439 fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
440 return NULL;
441 }
442
Marc-André Lureau78ee96d2017-06-22 13:04:16 +0200443 number_of_entries = DIV_ROUND_UP(length * 2, 26);
bellardde167e42005-04-28 21:15:08 +0000444
445 for(i=0;i<number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200446 entry=array_get_next(&(s->directory));
447 entry->attributes=0xf;
448 entry->reserved[0]=0;
449 entry->begin=0;
450 entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
bellardde167e42005-04-28 21:15:08 +0000451 }
balrog1e080d52007-12-24 13:26:04 +0000452 for(i=0;i<26*number_of_entries;i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200453 int offset=(i%26);
454 if(offset<10) offset=1+offset;
455 else if(offset<22) offset=14+offset-10;
456 else offset=28+offset-22;
457 entry=array_get(&(s->directory),s->directory.next-1-(i/26));
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200458 if (i >= 2 * length + 2) {
459 entry->name[offset] = 0xff;
460 } else if (i % 2 == 0) {
461 entry->name[offset] = longname[i / 2] & 0xff;
462 } else {
463 entry->name[offset] = longname[i / 2] >> 8;
464 }
bellardde167e42005-04-28 21:15:08 +0000465 }
Hervé Poussineau09ec4112017-05-22 23:12:00 +0200466 g_free(longname);
bellardde167e42005-04-28 21:15:08 +0000467 return array_get(&(s->directory),s->directory.next-number_of_entries);
468}
469
Anthony Liguoric227f092009-10-01 16:12:16 -0500470static char is_free(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000471{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200472 return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
bellarda0464332005-12-18 18:29:50 +0000473}
474
Anthony Liguoric227f092009-10-01 16:12:16 -0500475static char is_volume_label(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000476{
477 return direntry->attributes == 0x28;
478}
479
Anthony Liguoric227f092009-10-01 16:12:16 -0500480static char is_long_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000481{
482 return direntry->attributes == 0xf;
483}
484
Anthony Liguoric227f092009-10-01 16:12:16 -0500485static char is_short_name(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000486{
487 return !is_volume_label(direntry) && !is_long_name(direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200488 && !is_free(direntry);
bellarda0464332005-12-18 18:29:50 +0000489}
490
Anthony Liguoric227f092009-10-01 16:12:16 -0500491static char is_directory(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000492{
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200493 return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
bellarda0464332005-12-18 18:29:50 +0000494}
495
Anthony Liguoric227f092009-10-01 16:12:16 -0500496static inline char is_dot(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000497{
498 return is_short_name(direntry) && direntry->name[0] == '.';
499}
500
Anthony Liguoric227f092009-10-01 16:12:16 -0500501static char is_file(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000502{
503 return is_short_name(direntry) && !is_directory(direntry);
504}
505
Anthony Liguoric227f092009-10-01 16:12:16 -0500506static inline uint32_t begin_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000507{
508 return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
509}
510
Anthony Liguoric227f092009-10-01 16:12:16 -0500511static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +0000512{
513 return le32_to_cpu(direntry->size);
514}
515
Anthony Liguoric227f092009-10-01 16:12:16 -0500516static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
bellarda0464332005-12-18 18:29:50 +0000517{
518 direntry->begin = cpu_to_le16(begin & 0xffff);
519 direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
520}
521
Hervé Poussineau0c361112017-05-22 23:12:01 +0200522static uint8_t to_valid_short_char(gunichar c)
523{
524 c = g_unichar_toupper(c);
525 if ((c >= '0' && c <= '9') ||
526 (c >= 'A' && c <= 'Z') ||
527 strchr("$%'-_@~`!(){}^#&", c) != 0) {
528 return c;
529 } else {
530 return 0;
531 }
532}
533
534static direntry_t *create_short_filename(BDRVVVFATState *s,
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200535 const char *filename,
536 unsigned int directory_start)
Hervé Poussineau0c361112017-05-22 23:12:01 +0200537{
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200538 int i, j = 0;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200539 direntry_t *entry = array_get_next(&(s->directory));
540 const gchar *p, *last_dot = NULL;
541 gunichar c;
542 bool lossy_conversion = false;
Max Reitz7c8730d2017-07-17 17:12:07 +0200543 char tail[8];
Hervé Poussineau0c361112017-05-22 23:12:01 +0200544
545 if (!entry) {
546 return NULL;
547 }
548 memset(entry->name, 0x20, sizeof(entry->name));
549
550 /* copy filename and search last dot */
551 for (p = filename; ; p = g_utf8_next_char(p)) {
552 c = g_utf8_get_char(p);
553 if (c == '\0') {
554 break;
555 } else if (c == '.') {
556 if (j == 0) {
557 /* '.' at start of filename */
558 lossy_conversion = true;
559 } else {
560 if (last_dot) {
561 lossy_conversion = true;
562 }
563 last_dot = p;
564 }
565 } else if (!last_dot) {
566 /* first part of the name; copy it */
567 uint8_t v = to_valid_short_char(c);
568 if (j < 8 && v) {
569 entry->name[j++] = v;
570 } else {
571 lossy_conversion = true;
572 }
573 }
574 }
575
576 /* copy extension (if any) */
577 if (last_dot) {
578 j = 0;
579 for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
580 c = g_utf8_get_char(p);
581 if (c == '\0') {
582 break;
583 } else {
584 /* extension; copy it */
585 uint8_t v = to_valid_short_char(c);
586 if (j < 3 && v) {
587 entry->name[8 + (j++)] = v;
588 } else {
589 lossy_conversion = true;
590 }
591 }
592 }
593 }
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200594
Hervé Poussineau8c4517f2017-07-15 15:28:38 +0200595 if (entry->name[0] == DIR_KANJI) {
596 entry->name[0] = DIR_KANJI_FAKE;
Hervé Poussineau78f002c2017-05-22 23:12:04 +0200597 }
598
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200599 /* numeric-tail generation */
600 for (j = 0; j < 8; j++) {
601 if (entry->name[j] == ' ') {
602 break;
603 }
604 }
605 for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
606 direntry_t *entry1;
607 if (i > 0) {
Max Reitz7c8730d2017-07-17 17:12:07 +0200608 int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
609 assert(len <= 7);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200610 memcpy(entry->name + MIN(j, 8 - len), tail, len);
611 }
612 for (entry1 = array_get(&(s->directory), directory_start);
613 entry1 < entry; entry1++) {
614 if (!is_long_name(entry1) &&
615 !memcmp(entry1->name, entry->name, 11)) {
616 break; /* found dupe */
617 }
618 }
619 if (entry1 == entry) {
620 /* no dupe found */
621 return entry;
622 }
623 }
624 return NULL;
Hervé Poussineau0c361112017-05-22 23:12:01 +0200625}
626
bellardde167e42005-04-28 21:15:08 +0000627/* fat functions */
628
Anthony Liguoric227f092009-10-01 16:12:16 -0500629static inline uint8_t fat_chksum(const direntry_t* entry)
bellardde167e42005-04-28 21:15:08 +0000630{
631 uint8_t chksum=0;
632 int i;
633
Stefan Weilf671d172013-12-11 21:37:11 +0100634 for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
635 chksum = (((chksum & 0xfe) >> 1) |
636 ((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
Aurelien Jarno5606c222009-04-25 00:08:05 +0200637 }
ths3b46e622007-09-17 08:09:54 +0000638
bellardde167e42005-04-28 21:15:08 +0000639 return chksum;
640}
641
642/* if return_time==0, this returns the fat_date, else the fat_time */
643static uint16_t fat_datetime(time_t time,int return_time) {
644 struct tm* t;
bellardde167e42005-04-28 21:15:08 +0000645 struct tm t1;
Michael S. Tsirkin6ab00ce2009-09-30 19:43:31 +0200646 t = &t1;
bellardde167e42005-04-28 21:15:08 +0000647 localtime_r(&time,t);
bellardde167e42005-04-28 21:15:08 +0000648 if(return_time)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200649 return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
bellardde167e42005-04-28 21:15:08 +0000650 return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
651}
652
653static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
654{
bellarda0464332005-12-18 18:29:50 +0000655 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200656 uint32_t* entry=array_get(&(s->fat),cluster);
657 *entry=cpu_to_le32(value);
bellardde167e42005-04-28 21:15:08 +0000658 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200659 uint16_t* entry=array_get(&(s->fat),cluster);
660 *entry=cpu_to_le16(value&0xffff);
bellardde167e42005-04-28 21:15:08 +0000661 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200662 int offset = (cluster*3/2);
663 unsigned char* p = array_get(&(s->fat), offset);
bellarda0464332005-12-18 18:29:50 +0000664 switch (cluster&1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200665 case 0:
666 p[0] = value&0xff;
667 p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
668 break;
669 case 1:
670 p[0] = (p[0]&0xf) | ((value&0xf)<<4);
671 p[1] = (value>>4);
672 break;
673 }
bellardde167e42005-04-28 21:15:08 +0000674 }
675}
676
677static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
678{
bellarda0464332005-12-18 18:29:50 +0000679 if(s->fat_type==32) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200680 uint32_t* entry=array_get(&(s->fat),cluster);
681 return le32_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000682 } else if(s->fat_type==16) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200683 uint16_t* entry=array_get(&(s->fat),cluster);
684 return le16_to_cpu(*entry);
bellardde167e42005-04-28 21:15:08 +0000685 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200686 const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
687 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
bellardde167e42005-04-28 21:15:08 +0000688 }
689}
690
691static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
692{
693 if(fat_entry>s->max_fat_value-8)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200694 return -1;
bellardde167e42005-04-28 21:15:08 +0000695 return 0;
696}
697
698static inline void init_fat(BDRVVVFATState* s)
699{
bellarda0464332005-12-18 18:29:50 +0000700 if (s->fat_type == 12) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200701 array_init(&(s->fat),1);
702 array_ensure_allocated(&(s->fat),
703 s->sectors_per_fat * 0x200 * 3 / 2 - 1);
bellarda0464332005-12-18 18:29:50 +0000704 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200705 array_init(&(s->fat),(s->fat_type==32?4:2));
706 array_ensure_allocated(&(s->fat),
707 s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
bellarda0464332005-12-18 18:29:50 +0000708 }
bellardde167e42005-04-28 21:15:08 +0000709 memset(s->fat.pointer,0,s->fat.size);
ths3b46e622007-09-17 08:09:54 +0000710
bellardde167e42005-04-28 21:15:08 +0000711 switch(s->fat_type) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200712 case 12: s->max_fat_value=0xfff; break;
713 case 16: s->max_fat_value=0xffff; break;
714 case 32: s->max_fat_value=0x0fffffff; break;
715 default: s->max_fat_value=0; /* error... */
bellardde167e42005-04-28 21:15:08 +0000716 }
717
718}
719
Anthony Liguoric227f092009-10-01 16:12:16 -0500720static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200721 unsigned int directory_start, const char* filename, int is_dot)
bellardde167e42005-04-28 21:15:08 +0000722{
Hervé Poussineau0c361112017-05-22 23:12:01 +0200723 int long_index = s->directory.next;
Anthony Liguoric227f092009-10-01 16:12:16 -0500724 direntry_t* entry = NULL;
725 direntry_t* entry_long = NULL;
bellardde167e42005-04-28 21:15:08 +0000726
727 if(is_dot) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200728 entry=array_get_next(&(s->directory));
Stefan Weilf671d172013-12-11 21:37:11 +0100729 memset(entry->name, 0x20, sizeof(entry->name));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200730 memcpy(entry->name,filename,strlen(filename));
731 return entry;
bellardde167e42005-04-28 21:15:08 +0000732 }
ths3b46e622007-09-17 08:09:54 +0000733
bellardde167e42005-04-28 21:15:08 +0000734 entry_long=create_long_filename(s,filename);
Hervé Poussineau339cebc2017-05-22 23:12:02 +0200735 entry = create_short_filename(s, filename, directory_start);
bellardde167e42005-04-28 21:15:08 +0000736
737 /* calculate checksum; propagate to long name */
738 if(entry_long) {
739 uint8_t chksum=fat_chksum(entry);
740
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200741 /* calculate anew, because realloc could have taken place */
742 entry_long=array_get(&(s->directory),long_index);
743 while(entry_long<entry && is_long_name(entry_long)) {
744 entry_long->reserved[1]=chksum;
745 entry_long++;
746 }
bellardde167e42005-04-28 21:15:08 +0000747 }
748
749 return entry;
750}
751
bellarda0464332005-12-18 18:29:50 +0000752/*
753 * Read a directory. (the index of the corresponding mapping must be passed).
754 */
755static int read_directory(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +0000756{
Anthony Liguoric227f092009-10-01 16:12:16 -0500757 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
758 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000759 const char* dirname = mapping->path;
760 int first_cluster = mapping->begin;
761 int parent_index = mapping->info.dir.parent_mapping_index;
Anthony Liguoric227f092009-10-01 16:12:16 -0500762 mapping_t* parent_mapping = (mapping_t*)
blueswir1511d2b12009-03-07 15:32:56 +0000763 (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
bellarda0464332005-12-18 18:29:50 +0000764 int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
bellardde167e42005-04-28 21:15:08 +0000765
766 DIR* dir=opendir(dirname);
767 struct dirent* entry;
bellardde167e42005-04-28 21:15:08 +0000768 int i;
769
bellarda0464332005-12-18 18:29:50 +0000770 assert(mapping->mode & MODE_DIRECTORY);
771
772 if(!dir) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200773 mapping->end = mapping->begin;
774 return -1;
bellarda0464332005-12-18 18:29:50 +0000775 }
ths3b46e622007-09-17 08:09:54 +0000776
bellarda0464332005-12-18 18:29:50 +0000777 i = mapping->info.dir.first_dir_index =
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200778 first_cluster == 0 ? 0 : s->directory.next;
bellarda0464332005-12-18 18:29:50 +0000779
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200780 if (first_cluster != 0) {
781 /* create the top entries of a subdirectory */
782 (void)create_short_and_long_name(s, i, ".", 1);
783 (void)create_short_and_long_name(s, i, "..", 1);
784 }
785
ths5fafdf22007-09-16 21:08:06 +0000786 /* actually read the directory, and allocate the mappings */
bellardde167e42005-04-28 21:15:08 +0000787 while((entry=readdir(dir))) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200788 unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000789 char* buffer;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200790 direntry_t* direntry;
bellarda0464332005-12-18 18:29:50 +0000791 struct stat st;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200792 int is_dot=!strcmp(entry->d_name,".");
793 int is_dotdot=!strcmp(entry->d_name,"..");
bellardde167e42005-04-28 21:15:08 +0000794
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200795 if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
796 fprintf(stderr, "Too many entries in root directory\n");
797 closedir(dir);
798 return -2;
799 }
800
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200801 if(first_cluster == 0 && (is_dotdot || is_dot))
802 continue;
ths5fafdf22007-09-16 21:08:06 +0000803
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200804 buffer = g_malloc(length);
805 snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
bellardde167e42005-04-28 21:15:08 +0000806
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200807 if(stat(buffer,&st)<0) {
Stefan Weilce137822011-09-30 23:29:53 +0200808 g_free(buffer);
bellardde167e42005-04-28 21:15:08 +0000809 continue;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200810 }
bellardde167e42005-04-28 21:15:08 +0000811
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200812 /* create directory entry for this file */
Hervé Poussineauf82d92b2017-05-22 23:11:59 +0200813 if (!is_dot && !is_dotdot) {
814 direntry = create_short_and_long_name(s, i, entry->d_name, 0);
815 } else {
816 direntry = array_get(&(s->directory), is_dot ? i : i + 1);
817 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200818 direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
819 direntry->reserved[0]=direntry->reserved[1]=0;
820 direntry->ctime=fat_datetime(st.st_ctime,1);
821 direntry->cdate=fat_datetime(st.st_ctime,0);
822 direntry->adate=fat_datetime(st.st_atime,0);
823 direntry->begin_hi=0;
824 direntry->mtime=fat_datetime(st.st_mtime,1);
825 direntry->mdate=fat_datetime(st.st_mtime,0);
826 if(is_dotdot)
827 set_begin_of_direntry(direntry, first_cluster_of_parent);
828 else if(is_dot)
829 set_begin_of_direntry(direntry, first_cluster);
830 else
831 direntry->begin=0; /* do that later */
bellarda0464332005-12-18 18:29:50 +0000832 if (st.st_size > 0x7fffffff) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200833 fprintf(stderr, "File %s is larger than 2GB\n", buffer);
Stefan Weilce137822011-09-30 23:29:53 +0200834 g_free(buffer);
Blue Swirl08089ed2011-01-12 19:48:58 +0000835 closedir(dir);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200836 return -2;
bellarda0464332005-12-18 18:29:50 +0000837 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200838 direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
bellardde167e42005-04-28 21:15:08 +0000839
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200840 /* create mapping for this file */
841 if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
842 s->current_mapping = array_get_next(&(s->mapping));
843 s->current_mapping->begin=0;
844 s->current_mapping->end=st.st_size;
845 /*
846 * we get the direntry of the most recent direntry, which
847 * contains the short name and all the relevant information.
848 */
849 s->current_mapping->dir_index=s->directory.next-1;
850 s->current_mapping->first_mapping_index = -1;
851 if (S_ISDIR(st.st_mode)) {
852 s->current_mapping->mode = MODE_DIRECTORY;
853 s->current_mapping->info.dir.parent_mapping_index =
854 mapping_index;
855 } else {
856 s->current_mapping->mode = MODE_UNDEFINED;
857 s->current_mapping->info.file.offset = 0;
858 }
859 s->current_mapping->path=buffer;
860 s->current_mapping->read_only =
861 (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
Markus Armbrusterb122c3b2014-05-28 11:17:05 +0200862 } else {
863 g_free(buffer);
864 }
bellardde167e42005-04-28 21:15:08 +0000865 }
866 closedir(dir);
867
868 /* fill with zeroes up to the end of the cluster */
869 while(s->directory.next%(0x10*s->sectors_per_cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200870 direntry_t* direntry=array_get_next(&(s->directory));
871 memset(direntry,0,sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000872 }
873
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200874 if (s->fat_type != 32 &&
875 mapping_index == 0 &&
876 s->directory.next < s->root_entries) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200877 /* root directory */
878 int cur = s->directory.next;
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200879 array_ensure_allocated(&(s->directory), s->root_entries - 1);
880 s->directory.next = s->root_entries;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200881 memset(array_get(&(s->directory), cur), 0,
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200882 (s->root_entries - cur) * sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000883 }
ths5fafdf22007-09-16 21:08:06 +0000884
Hervé Poussineau5f5b29d2017-05-22 23:11:55 +0200885 /* re-get the mapping, since s->mapping was possibly realloc()ed */
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200886 mapping = array_get(&(s->mapping), mapping_index);
bellarda0464332005-12-18 18:29:50 +0000887 first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200888 * 0x20 / s->cluster_size;
bellarda0464332005-12-18 18:29:50 +0000889 mapping->end = first_cluster;
bellardde167e42005-04-28 21:15:08 +0000890
Markus Armbrusterd4df3db2014-08-19 10:31:11 +0200891 direntry = array_get(&(s->directory), mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +0000892 set_begin_of_direntry(direntry, mapping->begin);
ths3b46e622007-09-17 08:09:54 +0000893
bellardde167e42005-04-28 21:15:08 +0000894 return 0;
895}
896
bellarda0464332005-12-18 18:29:50 +0000897static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
bellardde167e42005-04-28 21:15:08 +0000898{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200899 return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000900}
901
902static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
903{
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200904 return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
bellarda0464332005-12-18 18:29:50 +0000905}
906
bellarda0464332005-12-18 18:29:50 +0000907static int init_directories(BDRVVVFATState* s,
Markus Armbrusterd11c8912014-05-16 11:00:18 +0200908 const char *dirname, int heads, int secs,
909 Error **errp)
bellarda0464332005-12-18 18:29:50 +0000910{
Anthony Liguoric227f092009-10-01 16:12:16 -0500911 bootsector_t* bootsector;
912 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +0000913 unsigned int i;
914 unsigned int cluster;
915
916 memset(&(s->first_sectors[0]),0,0x40*0x200);
917
bellardde167e42005-04-28 21:15:08 +0000918 s->cluster_size=s->sectors_per_cluster*0x200;
Anthony Liguori7267c092011-08-20 22:09:37 -0500919 s->cluster_buffer=g_malloc(s->cluster_size);
bellarda0464332005-12-18 18:29:50 +0000920
921 /*
922 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
923 * where sc is sector_count,
924 * spf is sectors_per_fat,
925 * spc is sectors_per_clusters, and
926 * fat_type = 12, 16 or 32.
927 */
928 i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
929 s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
ths3b46e622007-09-17 08:09:54 +0000930
Hervé Poussineau4dc705d2017-05-22 23:11:57 +0200931 s->offset_to_fat = s->offset_to_bootsector + 1;
932 s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
933
Anthony Liguoric227f092009-10-01 16:12:16 -0500934 array_init(&(s->mapping),sizeof(mapping_t));
935 array_init(&(s->directory),sizeof(direntry_t));
bellardde167e42005-04-28 21:15:08 +0000936
937 /* add volume label */
938 {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200939 direntry_t* entry=array_get_next(&(s->directory));
940 entry->attributes=0x28; /* archive | volume label */
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +0200941 memcpy(entry->name, s->volume_label, sizeof(entry->name));
bellardde167e42005-04-28 21:15:08 +0000942 }
943
bellardde167e42005-04-28 21:15:08 +0000944 /* Now build FAT, and write back information into directory */
945 init_fat(s);
946
Hervé Poussineau6817efe2017-05-22 23:12:03 +0200947 /* TODO: if there are more entries, bootsector has to be adjusted! */
948 s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
bellarda0464332005-12-18 18:29:50 +0000949 s->cluster_count=sector2cluster(s, s->sector_count);
bellardde167e42005-04-28 21:15:08 +0000950
bellarda0464332005-12-18 18:29:50 +0000951 mapping = array_get_next(&(s->mapping));
952 mapping->begin = 0;
953 mapping->dir_index = 0;
954 mapping->info.dir.parent_mapping_index = -1;
955 mapping->first_mapping_index = -1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500956 mapping->path = g_strdup(dirname);
bellarda0464332005-12-18 18:29:50 +0000957 i = strlen(mapping->path);
958 if (i > 0 && mapping->path[i - 1] == '/')
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200959 mapping->path[i - 1] = '\0';
bellarda0464332005-12-18 18:29:50 +0000960 mapping->mode = MODE_DIRECTORY;
961 mapping->read_only = 0;
962 s->path = mapping->path;
bellardde167e42005-04-28 21:15:08 +0000963
bellarda0464332005-12-18 18:29:50 +0000964 for (i = 0, cluster = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200965 /* MS-DOS expects the FAT to be 0 for the root directory
966 * (except for the media byte). */
967 /* LATER TODO: still true for FAT32? */
968 int fix_fat = (i != 0);
969 mapping = array_get(&(s->mapping), i);
bellardde167e42005-04-28 21:15:08 +0000970
bellarda0464332005-12-18 18:29:50 +0000971 if (mapping->mode & MODE_DIRECTORY) {
Thomas Hutha2b83a52018-07-24 13:52:04 +0200972 char *path = mapping->path;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200973 mapping->begin = cluster;
974 if(read_directory(s, i)) {
Thomas Hutha2b83a52018-07-24 13:52:04 +0200975 error_setg(errp, "Could not read directory %s", path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200976 return -1;
977 }
978 mapping = array_get(&(s->mapping), i);
979 } else {
980 assert(mapping->mode == MODE_UNDEFINED);
981 mapping->mode=MODE_NORMAL;
982 mapping->begin = cluster;
983 if (mapping->end > 0) {
984 direntry_t* direntry = array_get(&(s->directory),
985 mapping->dir_index);
bellardde167e42005-04-28 21:15:08 +0000986
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200987 mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
988 set_begin_of_direntry(direntry, mapping->begin);
989 } else {
990 mapping->end = cluster + 1;
991 fix_fat = 0;
992 }
993 }
bellarda0464332005-12-18 18:29:50 +0000994
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200995 assert(mapping->begin < mapping->end);
bellarda0464332005-12-18 18:29:50 +0000996
Hervé Poussineaud6a7e542017-05-22 23:11:54 +0200997 /* next free cluster */
998 cluster = mapping->end;
bellarda0464332005-12-18 18:29:50 +0000999
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001000 if(cluster > s->cluster_count) {
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001001 error_setg(errp,
1002 "Directory does not fit in FAT%d (capacity %.2f MB)",
1003 s->fat_type, s->sector_count / 2000.0);
1004 return -1;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001005 }
balrog8ce0f862008-11-10 01:34:27 +00001006
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001007 /* fix fat for entry */
1008 if (fix_fat) {
1009 int j;
1010 for(j = mapping->begin; j < mapping->end - 1; j++)
1011 fat_set(s, j, j+1);
1012 fat_set(s, mapping->end - 1, s->max_fat_value);
1013 }
bellardde167e42005-04-28 21:15:08 +00001014 }
1015
bellarda0464332005-12-18 18:29:50 +00001016 mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00001017 s->last_cluster_of_root_directory = mapping->end;
bellardde167e42005-04-28 21:15:08 +00001018
bellarda0464332005-12-18 18:29:50 +00001019 /* the FAT signature */
1020 fat_set(s,0,s->max_fat_value);
1021 fat_set(s,1,s->max_fat_value);
1022
1023 s->current_mapping = NULL;
1024
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001025 bootsector = (bootsector_t *)(s->first_sectors
1026 + s->offset_to_bootsector * 0x200);
bellardde167e42005-04-28 21:15:08 +00001027 bootsector->jump[0]=0xeb;
1028 bootsector->jump[1]=0x3e;
1029 bootsector->jump[2]=0x90;
Hervé Poussineau63d261c2017-07-15 15:28:39 +02001030 memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
bellardde167e42005-04-28 21:15:08 +00001031 bootsector->sector_size=cpu_to_le16(0x200);
1032 bootsector->sectors_per_cluster=s->sectors_per_cluster;
1033 bootsector->reserved_sectors=cpu_to_le16(1);
1034 bootsector->number_of_fats=0x2; /* number of FATs */
Hervé Poussineau6817efe2017-05-22 23:12:03 +02001035 bootsector->root_entries = cpu_to_le16(s->root_entries);
bellarda0464332005-12-18 18:29:50 +00001036 bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001037 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1038 bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
bellarda0464332005-12-18 18:29:50 +00001039 s->fat.pointer[0] = bootsector->media_type;
bellardde167e42005-04-28 21:15:08 +00001040 bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001041 bootsector->sectors_per_track = cpu_to_le16(secs);
1042 bootsector->number_of_heads = cpu_to_le16(heads);
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001043 bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
bellarda0464332005-12-18 18:29:50 +00001044 bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
bellardde167e42005-04-28 21:15:08 +00001045
bellarda0464332005-12-18 18:29:50 +00001046 /* LATER TODO: if FAT32, this is wrong */
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001047 /* drive_number: fda=0, hda=0x80 */
1048 bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
bellardde167e42005-04-28 21:15:08 +00001049 bootsector->u.fat16.signature=0x29;
1050 bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
1051
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001052 memcpy(bootsector->u.fat16.volume_label, s->volume_label,
1053 sizeof(bootsector->u.fat16.volume_label));
Hervé Poussineau92e28d82017-05-22 23:11:58 +02001054 memcpy(bootsector->u.fat16.fat_type,
1055 s->fat_type == 12 ? "FAT12 " : "FAT16 ", 8);
bellardde167e42005-04-28 21:15:08 +00001056 bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
1057
1058 return 0;
1059}
1060
bellard83f64092006-08-01 16:21:11 +00001061#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001062static BDRVVVFATState *vvv = NULL;
bellard83f64092006-08-01 16:21:11 +00001063#endif
bellarda0464332005-12-18 18:29:50 +00001064
Kevin Wolfeecc7742016-05-30 17:13:09 +02001065static int enable_write_target(BlockDriverState *bs, Error **errp);
bellarda0464332005-12-18 18:29:50 +00001066static int is_consistent(BDRVVVFATState *s);
1067
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001068static QemuOptsList runtime_opts = {
1069 .name = "vvfat",
1070 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1071 .desc = {
1072 {
1073 .name = "dir",
1074 .type = QEMU_OPT_STRING,
1075 .help = "Host directory to map to the vvfat device",
1076 },
1077 {
1078 .name = "fat-type",
1079 .type = QEMU_OPT_NUMBER,
1080 .help = "FAT type (12, 16 or 32)",
1081 },
1082 {
1083 .name = "floppy",
1084 .type = QEMU_OPT_BOOL,
1085 .help = "Create a floppy rather than a hard disk image",
1086 },
1087 {
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001088 .name = "label",
1089 .type = QEMU_OPT_STRING,
1090 .help = "Use a volume label other than QEMU VVFAT",
1091 },
1092 {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001093 .name = "rw",
1094 .type = QEMU_OPT_BOOL,
1095 .help = "Make the image writable",
1096 },
1097 { /* end of list */ }
1098 },
1099};
1100
1101static void vvfat_parse_filename(const char *filename, QDict *options,
1102 Error **errp)
1103{
1104 int fat_type = 0;
1105 bool floppy = false;
1106 bool rw = false;
1107 int i;
1108
1109 if (!strstart(filename, "fat:", NULL)) {
1110 error_setg(errp, "File name string must start with 'fat:'");
1111 return;
1112 }
1113
1114 /* Parse options */
1115 if (strstr(filename, ":32:")) {
1116 fat_type = 32;
1117 } else if (strstr(filename, ":16:")) {
1118 fat_type = 16;
1119 } else if (strstr(filename, ":12:")) {
1120 fat_type = 12;
1121 }
1122
1123 if (strstr(filename, ":floppy:")) {
1124 floppy = true;
1125 }
1126
1127 if (strstr(filename, ":rw:")) {
1128 rw = true;
1129 }
1130
1131 /* Get the directory name without options */
1132 i = strrchr(filename, ':') - filename;
1133 assert(i >= 3);
1134 if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
1135 /* workaround for DOS drive names */
1136 filename += i - 1;
1137 } else {
1138 filename += i + 1;
1139 }
1140
1141 /* Fill in the options QDict */
Eric Blake46f5ac22017-04-27 16:58:17 -05001142 qdict_put_str(options, "dir", filename);
1143 qdict_put_int(options, "fat-type", fat_type);
1144 qdict_put_bool(options, "floppy", floppy);
1145 qdict_put_bool(options, "rw", rw);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001146}
1147
Max Reitz015a1032013-09-05 14:22:29 +02001148static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
1149 Error **errp)
bellardde167e42005-04-28 21:15:08 +00001150{
1151 BDRVVVFATState *s = bs->opaque;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001152 int cyls, heads, secs;
1153 bool floppy;
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001154 const char *dirname, *label;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001155 QemuOpts *opts;
1156 Error *local_err = NULL;
1157 int ret;
bellardde167e42005-04-28 21:15:08 +00001158
bellard83f64092006-08-01 16:21:11 +00001159#ifdef DEBUG
bellarda0464332005-12-18 18:29:50 +00001160 vvv = s;
bellard83f64092006-08-01 16:21:11 +00001161#endif
bellarda0464332005-12-18 18:29:50 +00001162
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001163 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001164 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001165 if (local_err) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001166 error_propagate(errp, local_err);
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001167 ret = -EINVAL;
1168 goto fail;
1169 }
1170
1171 dirname = qemu_opt_get(opts, "dir");
1172 if (!dirname) {
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001173 error_setg(errp, "vvfat block driver requires a 'dir' option");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001174 ret = -EINVAL;
1175 goto fail;
1176 }
1177
1178 s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
1179 floppy = qemu_opt_get_bool(opts, "floppy", false);
1180
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001181 memset(s->volume_label, ' ', sizeof(s->volume_label));
1182 label = qemu_opt_get(opts, "label");
1183 if (label) {
1184 size_t label_length = strlen(label);
1185 if (label_length > 11) {
1186 error_setg(errp, "vvfat label cannot be longer than 11 bytes");
1187 ret = -EINVAL;
1188 goto fail;
1189 }
1190 memcpy(s->volume_label, label, label_length);
Kevin Wolfd208c502016-04-27 14:18:16 +02001191 } else {
1192 memcpy(s->volume_label, "QEMU VVFAT", 10);
Wolfgang Bumillerd5941dd2015-06-19 11:35:29 +02001193 }
1194
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001195 if (floppy) {
1196 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1197 if (!s->fat_type) {
1198 s->fat_type = 12;
1199 secs = 36;
1200 s->sectors_per_cluster = 2;
1201 } else {
1202 secs = s->fat_type == 12 ? 18 : 36;
1203 s->sectors_per_cluster = 1;
1204 }
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001205 cyls = 80;
1206 heads = 2;
1207 } else {
1208 /* 32MB or 504MB disk*/
1209 if (!s->fat_type) {
1210 s->fat_type = 16;
1211 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001212 s->offset_to_bootsector = 0x3f;
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001213 cyls = s->fat_type == 12 ? 64 : 1024;
1214 heads = 16;
1215 secs = 63;
1216 }
1217
1218 switch (s->fat_type) {
1219 case 32:
Alistair Francisb62e39b2017-09-11 12:52:56 -07001220 warn_report("FAT32 has not been tested. You are welcome to do so!");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001221 break;
1222 case 16:
1223 case 12:
1224 break;
1225 default:
Paolo Bonzinic0f92b52014-02-17 14:44:01 +01001226 error_setg(errp, "Valid FAT types are only 12, 16 and 32");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001227 ret = -EINVAL;
1228 goto fail;
1229 }
1230
1231
bellarda0464332005-12-18 18:29:50 +00001232 s->bs = bs;
1233
bellarda0464332005-12-18 18:29:50 +00001234 /* LATER TODO: if FAT32, adjust */
bellarda0464332005-12-18 18:29:50 +00001235 s->sectors_per_cluster=0x10;
bellardde167e42005-04-28 21:15:08 +00001236
1237 s->current_cluster=0xffffffff;
bellardde167e42005-04-28 21:15:08 +00001238
Kevin Wolfeecc7742016-05-30 17:13:09 +02001239 s->qcow = NULL;
bellarda0464332005-12-18 18:29:50 +00001240 s->qcow_filename = NULL;
1241 s->fat2 = NULL;
1242 s->downcase_short_names = 1;
ths3b46e622007-09-17 08:09:54 +00001243
Thomas Huth3e31b4e2018-07-18 17:08:29 +02001244 DLOG(fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
1245 dirname, cyls, heads, secs));
bellarda0464332005-12-18 18:29:50 +00001246
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001247 s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
Paolo Bonzini5a742b52011-10-05 09:12:06 +02001248
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001249 if (qemu_opt_get_bool(opts, "rw", false)) {
Jeff Codye2b82472017-04-07 16:55:26 -04001250 if (!bdrv_is_read_only(bs)) {
1251 ret = enable_write_target(bs, errp);
1252 if (ret < 0) {
1253 goto fail;
1254 }
1255 } else {
1256 ret = -EPERM;
1257 error_setg(errp,
1258 "Unable to set VVFAT to 'rw' when drive is read-only");
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001259 goto fail;
1260 }
Kevin Wolfeaa24102018-10-12 11:27:41 +02001261 } else {
1262 ret = bdrv_apply_auto_read_only(bs, NULL, errp);
Jeff Codye2b82472017-04-07 16:55:26 -04001263 if (ret < 0) {
Jeff Codye2b82472017-04-07 16:55:26 -04001264 goto fail;
1265 }
thsb5700942007-09-25 14:47:03 +00001266 }
1267
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001268 bs->total_sectors = cyls * heads * secs;
thsb5700942007-09-25 14:47:03 +00001269
Markus Armbrusterd11c8912014-05-16 11:00:18 +02001270 if (init_directories(s, dirname, heads, secs, errp)) {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001271 ret = -EIO;
1272 goto fail;
Markus Armbruster4480e0f2012-07-10 11:12:29 +02001273 }
bellardde167e42005-04-28 21:15:08 +00001274
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001275 s->sector_count = s->offset_to_root_dir
1276 + s->sectors_per_cluster * s->cluster_count;
thsb5700942007-09-25 14:47:03 +00001277
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001278 /* Disable migration when vvfat is used rw */
1279 if (s->qcow) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001280 error_setg(&s->migration_blocker,
1281 "The vvfat (rw) format used by node '%s' "
1282 "does not support live migration",
1283 bdrv_get_device_or_node_name(bs));
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301284 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1285 if (local_err) {
1286 error_propagate(errp, local_err);
1287 error_free(s->migration_blocker);
1288 goto fail;
1289 }
Kevin Wolf3397f0c2011-11-22 16:52:13 +01001290 }
1291
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001292 if (s->offset_to_bootsector > 0) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +05301293 init_mbr(s, cyls, heads, secs);
1294 }
1295
1296 qemu_co_mutex_init(&s->lock);
1297
Kevin Wolf7ad9be62013-04-12 19:42:04 +02001298 ret = 0;
1299fail:
1300 qemu_opts_del(opts);
1301 return ret;
bellardde167e42005-04-28 21:15:08 +00001302}
1303
Eric Blakea6506482016-06-23 16:37:17 -06001304static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
1305{
Eric Blakea5b8dd22016-06-23 16:37:24 -06001306 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
Eric Blakea6506482016-06-23 16:37:17 -06001307}
1308
bellardde167e42005-04-28 21:15:08 +00001309static inline void vvfat_close_current_file(BDRVVVFATState *s)
1310{
1311 if(s->current_mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001312 s->current_mapping = NULL;
1313 if (s->current_fd) {
1314 qemu_close(s->current_fd);
1315 s->current_fd = 0;
1316 }
bellardde167e42005-04-28 21:15:08 +00001317 }
bellarda0464332005-12-18 18:29:50 +00001318 s->current_cluster = -1;
bellardde167e42005-04-28 21:15:08 +00001319}
1320
1321/* mappings between index1 and index2-1 are supposed to be ordered
1322 * return value is the index of the last mapping for which end>cluster_num
1323 */
1324static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
1325{
bellardde167e42005-04-28 21:15:08 +00001326 while(1) {
Blue Swirl88bf7952010-04-25 15:27:14 +00001327 int index3;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001328 mapping_t* mapping;
1329 index3=(index1+index2)/2;
1330 mapping=array_get(&(s->mapping),index3);
1331 assert(mapping->begin < mapping->end);
1332 if(mapping->begin>=cluster_num) {
1333 assert(index2!=index3 || index2==0);
1334 if(index2==index3)
1335 return index1;
1336 index2=index3;
1337 } else {
1338 if(index1==index3)
1339 return mapping->end<=cluster_num ? index2 : index1;
1340 index1=index3;
1341 }
1342 assert(index1<=index2);
1343 DLOG(mapping=array_get(&(s->mapping),index1);
1344 assert(mapping->begin<=cluster_num);
1345 assert(index2 >= s->mapping.next ||
1346 ((mapping = array_get(&(s->mapping),index2)) &&
1347 mapping->end>cluster_num)));
bellardde167e42005-04-28 21:15:08 +00001348 }
1349}
1350
Anthony Liguoric227f092009-10-01 16:12:16 -05001351static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
bellardde167e42005-04-28 21:15:08 +00001352{
1353 int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05001354 mapping_t* mapping;
bellardde167e42005-04-28 21:15:08 +00001355 if(index>=s->mapping.next)
blueswir1511d2b12009-03-07 15:32:56 +00001356 return NULL;
bellardde167e42005-04-28 21:15:08 +00001357 mapping=array_get(&(s->mapping),index);
1358 if(mapping->begin>cluster_num)
blueswir1511d2b12009-03-07 15:32:56 +00001359 return NULL;
bellarda0464332005-12-18 18:29:50 +00001360 assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
bellardde167e42005-04-28 21:15:08 +00001361 return mapping;
1362}
1363
Anthony Liguoric227f092009-10-01 16:12:16 -05001364static int open_file(BDRVVVFATState* s,mapping_t* mapping)
bellardde167e42005-04-28 21:15:08 +00001365{
1366 if(!mapping)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001367 return -1;
bellardde167e42005-04-28 21:15:08 +00001368 if(!s->current_mapping ||
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001369 strcmp(s->current_mapping->path,mapping->path)) {
1370 /* open file */
1371 int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
1372 if(fd<0)
1373 return -1;
1374 vvfat_close_current_file(s);
1375 s->current_fd = fd;
1376 s->current_mapping = mapping;
bellardde167e42005-04-28 21:15:08 +00001377 }
1378 return 0;
1379}
1380
1381static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
1382{
1383 if(s->current_cluster != cluster_num) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001384 int result=0;
1385 off_t offset;
1386 assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
1387 if(!s->current_mapping
1388 || s->current_mapping->begin>cluster_num
1389 || s->current_mapping->end<=cluster_num) {
1390 /* binary search of mappings for file */
1391 mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
bellardde167e42005-04-28 21:15:08 +00001392
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001393 assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
bellarda0464332005-12-18 18:29:50 +00001394
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001395 if (mapping && mapping->mode & MODE_DIRECTORY) {
1396 vvfat_close_current_file(s);
1397 s->current_mapping = mapping;
bellarda0464332005-12-18 18:29:50 +00001398read_cluster_directory:
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001399 offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
1400 s->cluster = (unsigned char*)s->directory.pointer+offset
1401 + 0x20*s->current_mapping->info.dir.first_dir_index;
1402 assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
1403 assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
1404 s->current_cluster = cluster_num;
1405 return 0;
1406 }
bellarda0464332005-12-18 18:29:50 +00001407
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001408 if(open_file(s,mapping))
1409 return -2;
1410 } else if (s->current_mapping->mode & MODE_DIRECTORY)
1411 goto read_cluster_directory;
bellarda0464332005-12-18 18:29:50 +00001412
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001413 assert(s->current_fd);
bellarda0464332005-12-18 18:29:50 +00001414
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001415 offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
1416 if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
1417 return -3;
1418 s->cluster=s->cluster_buffer;
1419 result=read(s->current_fd,s->cluster,s->cluster_size);
1420 if(result<0) {
1421 s->current_cluster = -1;
1422 return -1;
1423 }
1424 s->current_cluster = cluster_num;
bellardde167e42005-04-28 21:15:08 +00001425 }
1426 return 0;
1427}
1428
bellarda0464332005-12-18 18:29:50 +00001429#ifdef DEBUG
Anthony Liguoric227f092009-10-01 16:12:16 -05001430static void print_direntry(const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001431{
1432 int j = 0;
1433 char buffer[1024];
1434
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001435 fprintf(stderr, "direntry %p: ", direntry);
bellarda0464332005-12-18 18:29:50 +00001436 if(!direntry)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001437 return;
bellarda0464332005-12-18 18:29:50 +00001438 if(is_long_name(direntry)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001439 unsigned char* c=(unsigned char*)direntry;
1440 int i;
1441 for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
blueswir13891b372008-12-14 09:30:41 +00001442#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001443 ADD_CHAR(c[i]);
1444 for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
1445 ADD_CHAR(c[i]);
1446 for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
1447 ADD_CHAR(c[i]);
1448 buffer[j] = 0;
1449 fprintf(stderr, "%s\n", buffer);
bellarda0464332005-12-18 18:29:50 +00001450 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001451 int i;
1452 for(i=0;i<11;i++)
1453 ADD_CHAR(direntry->name[i]);
1454 buffer[j] = 0;
1455 fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
1456 buffer,
1457 direntry->attributes,
1458 begin_of_direntry(direntry),le32_to_cpu(direntry->size));
bellarda0464332005-12-18 18:29:50 +00001459 }
1460}
1461
Anthony Liguoric227f092009-10-01 16:12:16 -05001462static void print_mapping(const mapping_t* mapping)
bellarda0464332005-12-18 18:29:50 +00001463{
Kevin Wolf3e89cb02010-05-20 10:34:50 +02001464 fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1465 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1466 mapping, mapping->begin, mapping->end, mapping->dir_index,
1467 mapping->first_mapping_index, mapping->path, mapping->mode);
1468
bellarda0464332005-12-18 18:29:50 +00001469 if (mapping->mode & MODE_DIRECTORY)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001470 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 +00001471 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001472 fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
bellarda0464332005-12-18 18:29:50 +00001473}
1474#endif
1475
ths5fafdf22007-09-16 21:08:06 +00001476static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00001477 uint8_t *buf, int nb_sectors)
1478{
1479 BDRVVVFATState *s = bs->opaque;
1480 int i;
1481
bellardde167e42005-04-28 21:15:08 +00001482 for(i=0;i<nb_sectors;i++,sector_num++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001483 if (sector_num >= bs->total_sectors)
1484 return -1;
1485 if (s->qcow) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001486 int64_t n;
Eric Blake6f712ee2017-03-08 15:34:28 -06001487 int ret;
Eric Blaked6a644b2017-07-07 07:44:57 -05001488 ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
1489 (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
Eric Blake6f712ee2017-03-08 15:34:28 -06001490 if (ret < 0) {
1491 return ret;
1492 }
1493 if (ret) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001494 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
1495 " allocated\n", sector_num,
1496 n >> BDRV_SECTOR_BITS));
1497 if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
1498 n >> BDRV_SECTOR_BITS)) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001499 return -1;
1500 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001501 i += (n >> BDRV_SECTOR_BITS) - 1;
1502 sector_num += (n >> BDRV_SECTOR_BITS) - 1;
Kevin Wolf7704df92011-11-08 10:50:12 +01001503 continue;
1504 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001505 DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
1506 sector_num));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001507 }
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001508 if (sector_num < s->offset_to_root_dir) {
1509 if (sector_num < s->offset_to_fat) {
1510 memcpy(buf + i * 0x200,
1511 &(s->first_sectors[sector_num * 0x200]),
1512 0x200);
1513 } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
1514 memcpy(buf + i * 0x200,
1515 &(s->fat.pointer[(sector_num
1516 - s->offset_to_fat) * 0x200]),
1517 0x200);
1518 } else if (sector_num < s->offset_to_root_dir) {
1519 memcpy(buf + i * 0x200,
1520 &(s->fat.pointer[(sector_num - s->offset_to_fat
1521 - s->sectors_per_fat) * 0x200]),
1522 0x200);
1523 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001524 } else {
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02001525 uint32_t sector = sector_num - s->offset_to_root_dir,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001526 sector_offset_in_cluster=(sector%s->sectors_per_cluster),
1527 cluster_num=sector/s->sectors_per_cluster;
1528 if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
1529 /* LATER TODO: strict: return -1; */
1530 memset(buf+i*0x200,0,0x200);
1531 continue;
1532 }
1533 memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
1534 }
bellarda0464332005-12-18 18:29:50 +00001535 }
1536 return 0;
1537}
1538
Kevin Wolf4575eb42016-04-26 17:14:08 +02001539static int coroutine_fn
1540vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1541 QEMUIOVector *qiov, int flags)
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001542{
1543 int ret;
1544 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02001545 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
1546 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1547 void *buf;
1548
1549 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1550 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1551
1552 buf = g_try_malloc(bytes);
1553 if (bytes && buf == NULL) {
1554 return -ENOMEM;
1555 }
1556
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001557 qemu_co_mutex_lock(&s->lock);
1558 ret = vvfat_read(bs, sector_num, buf, nb_sectors);
1559 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02001560
1561 qemu_iovec_from_buf(qiov, 0, buf, bytes);
1562 g_free(buf);
1563
Paolo Bonzini2914caa2011-10-20 13:16:22 +02001564 return ret;
1565}
1566
bellarda0464332005-12-18 18:29:50 +00001567/* LATER TODO: statify all functions */
1568
1569/*
1570 * Idea of the write support (use snapshot):
1571 *
1572 * 1. check if all data is consistent, recording renames, modifications,
1573 * new files and directories (in s->commits).
1574 *
1575 * 2. if the data is not consistent, stop committing
1576 *
1577 * 3. handle renames, and create new files and directories (do not yet
1578 * write their contents)
1579 *
1580 * 4. walk the directories, fixing the mapping and direntries, and marking
1581 * the handled mappings as not deleted
1582 *
1583 * 5. commit the contents of the files
1584 *
1585 * 6. handle deleted files and directories
1586 *
1587 */
1588
Anthony Liguoric227f092009-10-01 16:12:16 -05001589typedef struct commit_t {
bellarda0464332005-12-18 18:29:50 +00001590 char* path;
1591 union {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001592 struct { uint32_t cluster; } rename;
1593 struct { int dir_index; uint32_t modified_offset; } writeout;
1594 struct { uint32_t first_cluster; } new_file;
1595 struct { uint32_t cluster; } mkdir;
bellarda0464332005-12-18 18:29:50 +00001596 } param;
1597 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1598 enum {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001599 ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
bellarda0464332005-12-18 18:29:50 +00001600 } action;
Anthony Liguoric227f092009-10-01 16:12:16 -05001601} commit_t;
bellarda0464332005-12-18 18:29:50 +00001602
1603static void clear_commits(BDRVVVFATState* s)
1604{
1605 int i;
1606DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
1607 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001608 commit_t* commit = array_get(&(s->commits), i);
1609 assert(commit->path || commit->action == ACTION_WRITEOUT);
1610 if (commit->action != ACTION_WRITEOUT) {
1611 assert(commit->path);
Stefan Weilce137822011-09-30 23:29:53 +02001612 g_free(commit->path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001613 } else
1614 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00001615 }
1616 s->commits.next = 0;
1617}
1618
1619static void schedule_rename(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001620 uint32_t cluster, char* new_path)
bellarda0464332005-12-18 18:29:50 +00001621{
Anthony Liguoric227f092009-10-01 16:12:16 -05001622 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001623 commit->path = new_path;
1624 commit->param.rename.cluster = cluster;
1625 commit->action = ACTION_RENAME;
1626}
1627
1628static void schedule_writeout(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001629 int dir_index, uint32_t modified_offset)
bellarda0464332005-12-18 18:29:50 +00001630{
Anthony Liguoric227f092009-10-01 16:12:16 -05001631 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001632 commit->path = NULL;
1633 commit->param.writeout.dir_index = dir_index;
1634 commit->param.writeout.modified_offset = modified_offset;
1635 commit->action = ACTION_WRITEOUT;
1636}
1637
1638static void schedule_new_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001639 char* path, uint32_t first_cluster)
bellarda0464332005-12-18 18:29:50 +00001640{
Anthony Liguoric227f092009-10-01 16:12:16 -05001641 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001642 commit->path = path;
1643 commit->param.new_file.first_cluster = first_cluster;
1644 commit->action = ACTION_NEW_FILE;
1645}
1646
1647static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
1648{
Anthony Liguoric227f092009-10-01 16:12:16 -05001649 commit_t* commit = array_get_next(&(s->commits));
bellarda0464332005-12-18 18:29:50 +00001650 commit->path = path;
1651 commit->param.mkdir.cluster = cluster;
1652 commit->action = ACTION_MKDIR;
1653}
1654
1655typedef struct {
ths64eaabd2008-07-03 19:54:19 +00001656 /*
1657 * Since the sequence number is at most 0x3f, and the filename
1658 * length is at most 13 times the sequence number, the maximal
1659 * filename length is 0x3f * 13 bytes.
1660 */
1661 unsigned char name[0x3f * 13 + 1];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001662 gunichar2 name2[0x3f * 13 + 1];
bellarda0464332005-12-18 18:29:50 +00001663 int checksum, len;
1664 int sequence_number;
1665} long_file_name;
1666
1667static void lfn_init(long_file_name* lfn)
1668{
1669 lfn->sequence_number = lfn->len = 0;
1670 lfn->checksum = 0x100;
1671}
1672
1673/* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1674static int parse_long_name(long_file_name* lfn,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001675 const direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001676{
1677 int i, j, offset;
1678 const unsigned char* pointer = (const unsigned char*)direntry;
1679
1680 if (!is_long_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001681 return 1;
bellarda0464332005-12-18 18:29:50 +00001682
1683 if (pointer[0] & 0x40) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001684 /* first entry; do some initialization */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001685 lfn->sequence_number = pointer[0] & 0x3f;
1686 lfn->checksum = pointer[13];
1687 lfn->name[0] = 0;
1688 lfn->name[lfn->sequence_number * 13] = 0;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001689 } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
1690 /* not the expected sequence number */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001691 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001692 } else if (pointer[13] != lfn->checksum) {
1693 /* not the expected checksum */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001694 return -2;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001695 } else if (pointer[12] || pointer[26] || pointer[27]) {
1696 /* invalid zero fields */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001697 return -3;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001698 }
bellarda0464332005-12-18 18:29:50 +00001699
1700 offset = 13 * (lfn->sequence_number - 1);
1701 for (i = 0, j = 1; i < 13; i++, j+=2) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001702 if (j == 11)
1703 j = 14;
1704 else if (j == 26)
1705 j = 28;
bellarda0464332005-12-18 18:29:50 +00001706
Hervé Poussineaue03da262017-07-15 15:28:40 +02001707 if (pointer[j] == 0 && pointer[j + 1] == 0) {
1708 /* end of long file name */
1709 break;
1710 }
1711 gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
1712 lfn->name2[offset + i] = c;
bellarda0464332005-12-18 18:29:50 +00001713 }
1714
Hervé Poussineaue03da262017-07-15 15:28:40 +02001715 if (pointer[0] & 0x40) {
1716 /* first entry; set len */
1717 lfn->len = offset + i;
1718 }
1719 if ((pointer[0] & 0x3f) == 0x01) {
1720 /* last entry; finalize entry */
1721 glong olen;
1722 gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
1723 if (!utf8) {
1724 return -4;
1725 }
1726 lfn->len = olen;
1727 memcpy(lfn->name, utf8, olen + 1);
1728 g_free(utf8);
1729 }
bellarda0464332005-12-18 18:29:50 +00001730
1731 return 0;
1732}
1733
1734/* returns 0 if successful, >0 if no short_name, and <0 on error */
1735static int parse_short_name(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001736 long_file_name* lfn, direntry_t* direntry)
bellarda0464332005-12-18 18:29:50 +00001737{
1738 int i, j;
1739
1740 if (!is_short_name(direntry))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001741 return 1;
bellarda0464332005-12-18 18:29:50 +00001742
1743 for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
1744 for (i = 0; i <= j; i++) {
Hervé Poussineaue03da262017-07-15 15:28:40 +02001745 uint8_t c = direntry->name[i];
1746 if (c != to_valid_short_char(c)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001747 return -1;
Hervé Poussineaue03da262017-07-15 15:28:40 +02001748 } else if (s->downcase_short_names) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001749 lfn->name[i] = qemu_tolower(direntry->name[i]);
Hervé Poussineaue03da262017-07-15 15:28:40 +02001750 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001751 lfn->name[i] = direntry->name[i];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001752 }
bellarda0464332005-12-18 18:29:50 +00001753 }
1754
Stefan Weilf671d172013-12-11 21:37:11 +01001755 for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
1756 }
bellarda0464332005-12-18 18:29:50 +00001757 if (j >= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001758 lfn->name[i++] = '.';
1759 lfn->name[i + j + 1] = '\0';
1760 for (;j >= 0; j--) {
Stefan Weilf671d172013-12-11 21:37:11 +01001761 uint8_t c = direntry->name[8 + j];
Hervé Poussineaue03da262017-07-15 15:28:40 +02001762 if (c != to_valid_short_char(c)) {
Stefan Weilf671d172013-12-11 21:37:11 +01001763 return -2;
1764 } else if (s->downcase_short_names) {
1765 lfn->name[i + j] = qemu_tolower(c);
1766 } else {
1767 lfn->name[i + j] = c;
1768 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001769 }
bellarda0464332005-12-18 18:29:50 +00001770 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001771 lfn->name[i + j + 1] = '\0';
bellarda0464332005-12-18 18:29:50 +00001772
Hervé Poussineau8c4517f2017-07-15 15:28:38 +02001773 if (lfn->name[0] == DIR_KANJI_FAKE) {
1774 lfn->name[0] = DIR_KANJI;
Hervé Poussineau78f002c2017-05-22 23:12:04 +02001775 }
thsffe8ab82007-12-16 03:16:05 +00001776 lfn->len = strlen((char*)lfn->name);
bellarda0464332005-12-18 18:29:50 +00001777
1778 return 0;
1779}
1780
1781static inline uint32_t modified_fat_get(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001782 unsigned int cluster)
bellarda0464332005-12-18 18:29:50 +00001783{
1784 if (cluster < s->last_cluster_of_root_directory) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001785 if (cluster + 1 == s->last_cluster_of_root_directory)
1786 return s->max_fat_value;
1787 else
1788 return cluster + 1;
bellarda0464332005-12-18 18:29:50 +00001789 }
1790
1791 if (s->fat_type==32) {
1792 uint32_t* entry=((uint32_t*)s->fat2)+cluster;
1793 return le32_to_cpu(*entry);
1794 } else if (s->fat_type==16) {
1795 uint16_t* entry=((uint16_t*)s->fat2)+cluster;
1796 return le16_to_cpu(*entry);
1797 } else {
1798 const uint8_t* x=s->fat2+cluster*3/2;
1799 return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
1800 }
1801}
1802
Eric Blake6f712ee2017-03-08 15:34:28 -06001803static inline bool cluster_was_modified(BDRVVVFATState *s,
1804 uint32_t cluster_num)
bellarda0464332005-12-18 18:29:50 +00001805{
1806 int was_modified = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001807 int i;
bellarda0464332005-12-18 18:29:50 +00001808
Kevin Wolfeecc7742016-05-30 17:13:09 +02001809 if (s->qcow == NULL) {
1810 return 0;
1811 }
bellarda0464332005-12-18 18:29:50 +00001812
Kevin Wolfeecc7742016-05-30 17:13:09 +02001813 for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
1814 was_modified = bdrv_is_allocated(s->qcow->bs,
Eric Blaked6a644b2017-07-07 07:44:57 -05001815 (cluster2sector(s, cluster_num) +
1816 i) * BDRV_SECTOR_SIZE,
1817 BDRV_SECTOR_SIZE, NULL);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001818 }
bellarda0464332005-12-18 18:29:50 +00001819
Eric Blake6f712ee2017-03-08 15:34:28 -06001820 /*
1821 * Note that this treats failures to learn allocation status the
1822 * same as if an allocation has occurred. It's as safe as
1823 * anything else, given that a failure to learn allocation status
1824 * will probably result in more failures.
1825 */
1826 return !!was_modified;
bellarda0464332005-12-18 18:29:50 +00001827}
1828
1829static const char* get_basename(const char* path)
1830{
1831 char* basename = strrchr(path, '/');
1832 if (basename == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001833 return path;
bellarda0464332005-12-18 18:29:50 +00001834 else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001835 return basename + 1; /* strip '/' */
bellarda0464332005-12-18 18:29:50 +00001836}
1837
1838/*
1839 * The array s->used_clusters holds the states of the clusters. If it is
1840 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1841 * was modified, bit 3 is set.
1842 * If any cluster is allocated, but not part of a file or directory, this
1843 * driver refuses to commit.
1844 */
1845typedef enum {
1846 USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
Anthony Liguoric227f092009-10-01 16:12:16 -05001847} used_t;
bellarda0464332005-12-18 18:29:50 +00001848
1849/*
1850 * get_cluster_count_for_direntry() not only determines how many clusters
1851 * are occupied by direntry, but also if it was renamed or modified.
1852 *
1853 * A file is thought to be renamed *only* if there already was a file with
1854 * exactly the same first cluster, but a different name.
1855 *
1856 * Further, the files/directories handled by this function are
1857 * assumed to be *not* deleted (and *only* those).
1858 */
1859static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001860 direntry_t* direntry, const char* path)
bellarda0464332005-12-18 18:29:50 +00001861{
1862 /*
1863 * This is a little bit tricky:
1864 * IF the guest OS just inserts a cluster into the file chain,
1865 * and leaves the rest alone, (i.e. the original file had clusters
1866 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1867 *
1868 * - do_commit will write the cluster into the file at the given
1869 * offset, but
1870 *
1871 * - the cluster which is overwritten should be moved to a later
1872 * position in the file.
1873 *
1874 * I am not aware that any OS does something as braindead, but this
1875 * situation could happen anyway when not committing for a long time.
1876 * Just to be sure that this does not bite us, detect it, and copy the
1877 * contents of the clusters to-be-overwritten into the qcow.
1878 */
1879 int copy_it = 0;
1880 int was_modified = 0;
1881 int32_t ret = 0;
1882
1883 uint32_t cluster_num = begin_of_direntry(direntry);
1884 uint32_t offset = 0;
1885 int first_mapping_index = -1;
Anthony Liguoric227f092009-10-01 16:12:16 -05001886 mapping_t* mapping = NULL;
bellarda0464332005-12-18 18:29:50 +00001887 const char* basename2 = NULL;
1888
1889 vvfat_close_current_file(s);
1890
1891 /* the root directory */
1892 if (cluster_num == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001893 return 0;
bellarda0464332005-12-18 18:29:50 +00001894
1895 /* write support */
1896 if (s->qcow) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001897 basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00001898
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001899 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001900
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001901 if (mapping) {
1902 const char* basename;
bellardda2414e2006-04-23 14:36:41 +00001903
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001904 assert(mapping->mode & MODE_DELETED);
1905 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00001906
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001907 basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001908
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001909 assert(mapping->mode & MODE_NORMAL);
bellarda0464332005-12-18 18:29:50 +00001910
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001911 /* rename */
1912 if (strcmp(basename, basename2))
1913 schedule_rename(s, cluster_num, g_strdup(path));
1914 } else if (is_file(direntry))
1915 /* new file */
1916 schedule_new_file(s, g_strdup(path), cluster_num);
1917 else {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001918 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001919 return 0;
1920 }
bellarda0464332005-12-18 18:29:50 +00001921 }
1922
1923 while(1) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001924 if (s->qcow) {
1925 if (!copy_it && cluster_was_modified(s, cluster_num)) {
1926 if (mapping == NULL ||
1927 mapping->begin > cluster_num ||
1928 mapping->end <= cluster_num)
1929 mapping = find_mapping_for_cluster(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00001930
1931
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001932 if (mapping &&
1933 (mapping->mode & MODE_DIRECTORY) == 0) {
bellarda0464332005-12-18 18:29:50 +00001934
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001935 /* was modified in qcow */
1936 if (offset != mapping->info.file.offset + s->cluster_size
1937 * (cluster_num - mapping->begin)) {
1938 /* offset of this cluster in file chain has changed */
Blue Swirl43dc2a62010-03-18 18:41:57 +00001939 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001940 copy_it = 1;
1941 } else if (offset == 0) {
1942 const char* basename = get_basename(mapping->path);
bellarda0464332005-12-18 18:29:50 +00001943
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001944 if (strcmp(basename, basename2))
1945 copy_it = 1;
1946 first_mapping_index = array_index(&(s->mapping), mapping);
1947 }
bellarda0464332005-12-18 18:29:50 +00001948
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001949 if (mapping->first_mapping_index != first_mapping_index
1950 && mapping->info.file.offset > 0) {
Blue Swirl43dc2a62010-03-18 18:41:57 +00001951 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001952 copy_it = 1;
1953 }
bellarda0464332005-12-18 18:29:50 +00001954
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001955 /* need to write out? */
1956 if (!was_modified && is_file(direntry)) {
1957 was_modified = 1;
1958 schedule_writeout(s, mapping->dir_index, offset);
1959 }
1960 }
1961 }
bellardde167e42005-04-28 21:15:08 +00001962
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001963 if (copy_it) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001964 int i;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001965 /*
1966 * This is horribly inefficient, but that is okay, since
1967 * it is rarely executed, if at all.
1968 */
1969 int64_t offset = cluster2sector(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00001970
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001971 vvfat_close_current_file(s);
Kevin Wolf7704df92011-11-08 10:50:12 +01001972 for (i = 0; i < s->sectors_per_cluster; i++) {
Kevin Wolfeecc7742016-05-30 17:13:09 +02001973 int res;
1974
Eric Blaked6a644b2017-07-07 07:44:57 -05001975 res = bdrv_is_allocated(s->qcow->bs,
1976 (offset + i) * BDRV_SECTOR_SIZE,
1977 BDRV_SECTOR_SIZE, NULL);
Eric Blake6f712ee2017-03-08 15:34:28 -06001978 if (res < 0) {
1979 return -1;
1980 }
Kevin Wolfeecc7742016-05-30 17:13:09 +02001981 if (!res) {
1982 res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
1983 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001984 return -1;
1985 }
Kevin Wolf18d51c42016-05-31 14:42:08 +02001986 res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
Kevin Wolfeecc7742016-05-30 17:13:09 +02001987 if (res) {
Kevin Wolf7704df92011-11-08 10:50:12 +01001988 return -2;
1989 }
1990 }
1991 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001992 }
1993 }
bellarda0464332005-12-18 18:29:50 +00001994
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02001995 ret++;
1996 if (s->used_clusters[cluster_num] & USED_ANY)
1997 return 0;
1998 s->used_clusters[cluster_num] = USED_FILE;
bellarda0464332005-12-18 18:29:50 +00001999
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002000 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002001
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002002 if (fat_eof(s, cluster_num))
2003 return ret;
2004 else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
2005 return -1;
bellarda0464332005-12-18 18:29:50 +00002006
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002007 offset += s->cluster_size;
bellardde167e42005-04-28 21:15:08 +00002008 }
2009}
2010
bellarda0464332005-12-18 18:29:50 +00002011/*
ths5fafdf22007-09-16 21:08:06 +00002012 * This function looks at the modified data (qcow).
bellarda0464332005-12-18 18:29:50 +00002013 * It returns 0 upon inconsistency or error, and the number of clusters
2014 * used by the directory, its subdirectories and their files.
2015 */
2016static int check_directory_consistency(BDRVVVFATState *s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002017 int cluster_num, const char* path)
bellardde167e42005-04-28 21:15:08 +00002018{
bellarda0464332005-12-18 18:29:50 +00002019 int ret = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -05002020 unsigned char* cluster = g_malloc(s->cluster_size);
Anthony Liguoric227f092009-10-01 16:12:16 -05002021 direntry_t* direntries = (direntry_t*)cluster;
2022 mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
bellardde167e42005-04-28 21:15:08 +00002023
bellarda0464332005-12-18 18:29:50 +00002024 long_file_name lfn;
2025 int path_len = strlen(path);
Kevin Wolf0d460d62011-06-01 10:57:00 +02002026 char path2[PATH_MAX + 1];
bellardde167e42005-04-28 21:15:08 +00002027
bellarda0464332005-12-18 18:29:50 +00002028 assert(path_len < PATH_MAX); /* len was tested before! */
blueswir1363a37d2008-08-21 17:58:08 +00002029 pstrcpy(path2, sizeof(path2), path);
bellarda0464332005-12-18 18:29:50 +00002030 path2[path_len] = '/';
2031 path2[path_len + 1] = '\0';
bellardde167e42005-04-28 21:15:08 +00002032
bellarda0464332005-12-18 18:29:50 +00002033 if (mapping) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002034 const char* basename = get_basename(mapping->path);
2035 const char* basename2 = get_basename(path);
bellarda0464332005-12-18 18:29:50 +00002036
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002037 assert(mapping->mode & MODE_DIRECTORY);
bellarda0464332005-12-18 18:29:50 +00002038
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002039 assert(mapping->mode & MODE_DELETED);
2040 mapping->mode &= ~MODE_DELETED;
bellarda0464332005-12-18 18:29:50 +00002041
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002042 if (strcmp(basename, basename2))
2043 schedule_rename(s, cluster_num, g_strdup(path));
bellarda0464332005-12-18 18:29:50 +00002044 } else
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002045 /* new directory */
2046 schedule_mkdir(s, cluster_num, g_strdup(path));
ths3b46e622007-09-17 08:09:54 +00002047
bellarda0464332005-12-18 18:29:50 +00002048 lfn_init(&lfn);
2049 do {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002050 int i;
2051 int subret = 0;
bellarda0464332005-12-18 18:29:50 +00002052
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002053 ret++;
bellarda0464332005-12-18 18:29:50 +00002054
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002055 if (s->used_clusters[cluster_num] & USED_ANY) {
2056 fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
Markus Armbruster6262bbd2014-05-28 11:17:04 +02002057 goto fail;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002058 }
2059 s->used_clusters[cluster_num] = USED_DIRECTORY;
bellardde167e42005-04-28 21:15:08 +00002060
bellarda0464332005-12-18 18:29:50 +00002061DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002062 subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
2063 s->sectors_per_cluster);
2064 if (subret) {
2065 fprintf(stderr, "Error fetching direntries\n");
2066 fail:
Stefan Weilce137822011-09-30 23:29:53 +02002067 g_free(cluster);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002068 return 0;
2069 }
bellarda0464332005-12-18 18:29:50 +00002070
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002071 for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
2072 int cluster_count = 0;
bellarda0464332005-12-18 18:29:50 +00002073
Stefan Weilb2bedb22011-09-12 22:33:01 +02002074DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002075 if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
2076 is_free(direntries + i))
2077 continue;
bellarda0464332005-12-18 18:29:50 +00002078
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002079 subret = parse_long_name(&lfn, direntries + i);
2080 if (subret < 0) {
2081 fprintf(stderr, "Error in long name\n");
2082 goto fail;
2083 }
2084 if (subret == 0 || is_free(direntries + i))
2085 continue;
bellarda0464332005-12-18 18:29:50 +00002086
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002087 if (fat_chksum(direntries+i) != lfn.checksum) {
2088 subret = parse_short_name(s, &lfn, direntries + i);
2089 if (subret < 0) {
2090 fprintf(stderr, "Error in short name (%d)\n", subret);
2091 goto fail;
2092 }
2093 if (subret > 0 || !strcmp((char*)lfn.name, ".")
2094 || !strcmp((char*)lfn.name, ".."))
2095 continue;
2096 }
2097 lfn.checksum = 0x100; /* cannot use long name twice */
bellarda0464332005-12-18 18:29:50 +00002098
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002099 if (path_len + 1 + lfn.len >= PATH_MAX) {
2100 fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
2101 goto fail;
2102 }
blueswir1363a37d2008-08-21 17:58:08 +00002103 pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
2104 (char*)lfn.name);
bellarda0464332005-12-18 18:29:50 +00002105
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002106 if (is_directory(direntries + i)) {
2107 if (begin_of_direntry(direntries + i) == 0) {
2108 DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
2109 goto fail;
2110 }
2111 cluster_count = check_directory_consistency(s,
2112 begin_of_direntry(direntries + i), path2);
2113 if (cluster_count == 0) {
2114 DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
2115 goto fail;
2116 }
2117 } else if (is_file(direntries + i)) {
2118 /* check file size with FAT */
2119 cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
2120 if (cluster_count !=
Laurent Vivier13385ae2016-05-31 18:35:54 +02002121 DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002122 DLOG(fprintf(stderr, "Cluster count mismatch\n"));
2123 goto fail;
2124 }
2125 } else
Blue Swirl43dc2a62010-03-18 18:41:57 +00002126 abort(); /* cluster_count = 0; */
bellarda0464332005-12-18 18:29:50 +00002127
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002128 ret += cluster_count;
2129 }
bellarda0464332005-12-18 18:29:50 +00002130
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002131 cluster_num = modified_fat_get(s, cluster_num);
bellarda0464332005-12-18 18:29:50 +00002132 } while(!fat_eof(s, cluster_num));
2133
Stefan Weilce137822011-09-30 23:29:53 +02002134 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002135 return ret;
bellardde167e42005-04-28 21:15:08 +00002136}
2137
bellarda0464332005-12-18 18:29:50 +00002138/* returns 1 on success */
2139static int is_consistent(BDRVVVFATState* s)
bellardde167e42005-04-28 21:15:08 +00002140{
bellarda0464332005-12-18 18:29:50 +00002141 int i, check;
2142 int used_clusters_count = 0;
2143
2144DLOG(checkpoint());
2145 /*
2146 * - get modified FAT
2147 * - compare the two FATs (TODO)
2148 * - get buffer for marking used clusters
2149 * - recurse direntries from root (using bs->bdrv_read to make
2150 * sure to get the new data)
2151 * - check that the FAT agrees with the size
2152 * - count the number of clusters occupied by this directory and
2153 * its files
2154 * - check that the cumulative used cluster count agrees with the
2155 * FAT
2156 * - if all is fine, return number of used clusters
2157 */
2158 if (s->fat2 == NULL) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002159 int size = 0x200 * s->sectors_per_fat;
2160 s->fat2 = g_malloc(size);
2161 memcpy(s->fat2, s->fat.pointer, size);
bellarda0464332005-12-18 18:29:50 +00002162 }
2163 check = vvfat_read(s->bs,
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02002164 s->offset_to_fat, s->fat2, s->sectors_per_fat);
bellarda0464332005-12-18 18:29:50 +00002165 if (check) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002166 fprintf(stderr, "Could not copy fat\n");
2167 return 0;
bellardde167e42005-04-28 21:15:08 +00002168 }
bellarda0464332005-12-18 18:29:50 +00002169 assert (s->used_clusters);
2170 for (i = 0; i < sector2cluster(s, s->sector_count); i++)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002171 s->used_clusters[i] &= ~USED_ANY;
bellarda0464332005-12-18 18:29:50 +00002172
2173 clear_commits(s);
2174
2175 /* mark every mapped file/directory as deleted.
2176 * (check_directory_consistency() will unmark those still present). */
2177 if (s->qcow)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002178 for (i = 0; i < s->mapping.next; i++) {
2179 mapping_t* mapping = array_get(&(s->mapping), i);
2180 if (mapping->first_mapping_index < 0)
2181 mapping->mode |= MODE_DELETED;
2182 }
bellarda0464332005-12-18 18:29:50 +00002183
2184 used_clusters_count = check_directory_consistency(s, 0, s->path);
2185 if (used_clusters_count <= 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002186 DLOG(fprintf(stderr, "problem in directory\n"));
2187 return 0;
bellarda0464332005-12-18 18:29:50 +00002188 }
2189
2190 check = s->last_cluster_of_root_directory;
2191 for (i = check; i < sector2cluster(s, s->sector_count); i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002192 if (modified_fat_get(s, i)) {
2193 if(!s->used_clusters[i]) {
2194 DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
2195 return 0;
2196 }
2197 check++;
2198 }
bellarda0464332005-12-18 18:29:50 +00002199
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002200 if (s->used_clusters[i] == USED_ALLOCATED) {
2201 /* allocated, but not used... */
2202 DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
2203 return 0;
2204 }
bellarda0464332005-12-18 18:29:50 +00002205 }
2206
2207 if (check != used_clusters_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002208 return 0;
bellarda0464332005-12-18 18:29:50 +00002209
2210 return used_clusters_count;
bellardde167e42005-04-28 21:15:08 +00002211}
2212
bellarda0464332005-12-18 18:29:50 +00002213static inline void adjust_mapping_indices(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002214 int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002215{
bellarda0464332005-12-18 18:29:50 +00002216 int i;
2217
2218 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002219 mapping_t* mapping = array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002220
2221#define ADJUST_MAPPING_INDEX(name) \
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002222 if (mapping->name >= offset) \
2223 mapping->name += adjust
bellarda0464332005-12-18 18:29:50 +00002224
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002225 ADJUST_MAPPING_INDEX(first_mapping_index);
2226 if (mapping->mode & MODE_DIRECTORY)
2227 ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
bellarda0464332005-12-18 18:29:50 +00002228 }
bellardde167e42005-04-28 21:15:08 +00002229}
2230
bellarda0464332005-12-18 18:29:50 +00002231/* insert or update mapping */
Anthony Liguoric227f092009-10-01 16:12:16 -05002232static mapping_t* insert_mapping(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002233 uint32_t begin, uint32_t end)
bellardde167e42005-04-28 21:15:08 +00002234{
bellarda0464332005-12-18 18:29:50 +00002235 /*
2236 * - find mapping where mapping->begin >= begin,
2237 * - if mapping->begin > begin: insert
2238 * - adjust all references to mappings!
2239 * - else: adjust
2240 * - replace name
2241 */
2242 int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
Anthony Liguoric227f092009-10-01 16:12:16 -05002243 mapping_t* mapping = NULL;
2244 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellarda0464332005-12-18 18:29:50 +00002245
2246 if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002247 && mapping->begin < begin) {
2248 mapping->end = begin;
2249 index++;
2250 mapping = array_get(&(s->mapping), index);
bellarda0464332005-12-18 18:29:50 +00002251 }
2252 if (index >= s->mapping.next || mapping->begin > begin) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002253 mapping = array_insert(&(s->mapping), index, 1);
2254 mapping->path = NULL;
2255 adjust_mapping_indices(s, index, +1);
bellarda0464332005-12-18 18:29:50 +00002256 }
2257
bellardde167e42005-04-28 21:15:08 +00002258 mapping->begin = begin;
bellarda0464332005-12-18 18:29:50 +00002259 mapping->end = end;
2260
Anthony Liguoric227f092009-10-01 16:12:16 -05002261DLOG(mapping_t* next_mapping;
bellarda0464332005-12-18 18:29:50 +00002262assert(index + 1 >= s->mapping.next ||
2263((next_mapping = array_get(&(s->mapping), index + 1)) &&
2264 next_mapping->begin >= end)));
2265
Anthony Liguoric227f092009-10-01 16:12:16 -05002266 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002267 s->current_mapping = array_get(&(s->mapping),
2268 s->current_mapping - first_mapping);
bellarda0464332005-12-18 18:29:50 +00002269
2270 return mapping;
bellardde167e42005-04-28 21:15:08 +00002271}
2272
bellarda0464332005-12-18 18:29:50 +00002273static int remove_mapping(BDRVVVFATState* s, int mapping_index)
bellardde167e42005-04-28 21:15:08 +00002274{
Anthony Liguoric227f092009-10-01 16:12:16 -05002275 mapping_t* mapping = array_get(&(s->mapping), mapping_index);
2276 mapping_t* first_mapping = array_get(&(s->mapping), 0);
bellardde167e42005-04-28 21:15:08 +00002277
bellarda0464332005-12-18 18:29:50 +00002278 /* free mapping */
Stefan Weilce137822011-09-30 23:29:53 +02002279 if (mapping->first_mapping_index < 0) {
2280 g_free(mapping->path);
2281 }
bellardde167e42005-04-28 21:15:08 +00002282
bellarda0464332005-12-18 18:29:50 +00002283 /* remove from s->mapping */
2284 array_remove(&(s->mapping), mapping_index);
bellardde167e42005-04-28 21:15:08 +00002285
bellarda0464332005-12-18 18:29:50 +00002286 /* adjust all references to mappings */
2287 adjust_mapping_indices(s, mapping_index, -1);
bellardde167e42005-04-28 21:15:08 +00002288
Anthony Liguoric227f092009-10-01 16:12:16 -05002289 if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002290 s->current_mapping = array_get(&(s->mapping),
2291 s->current_mapping - first_mapping);
bellardde167e42005-04-28 21:15:08 +00002292
2293 return 0;
2294}
2295
bellarda0464332005-12-18 18:29:50 +00002296static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
bellardde167e42005-04-28 21:15:08 +00002297{
bellarda0464332005-12-18 18:29:50 +00002298 int i;
2299 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002300 mapping_t* mapping = array_get(&(s->mapping), i);
2301 if (mapping->dir_index >= offset)
2302 mapping->dir_index += adjust;
2303 if ((mapping->mode & MODE_DIRECTORY) &&
2304 mapping->info.dir.first_dir_index >= offset)
2305 mapping->info.dir.first_dir_index += adjust;
bellardde167e42005-04-28 21:15:08 +00002306 }
bellardde167e42005-04-28 21:15:08 +00002307}
2308
Anthony Liguoric227f092009-10-01 16:12:16 -05002309static direntry_t* insert_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002310 int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002311{
bellarda0464332005-12-18 18:29:50 +00002312 /*
2313 * make room in s->directory,
2314 * adjust_dirindices
2315 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002316 direntry_t* result = array_insert(&(s->directory), dir_index, count);
bellarda0464332005-12-18 18:29:50 +00002317 if (result == NULL)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002318 return NULL;
bellarda0464332005-12-18 18:29:50 +00002319 adjust_dirindices(s, dir_index, count);
bellardde167e42005-04-28 21:15:08 +00002320 return result;
2321}
2322
bellarda0464332005-12-18 18:29:50 +00002323static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
bellardde167e42005-04-28 21:15:08 +00002324{
bellarda0464332005-12-18 18:29:50 +00002325 int ret = array_remove_slice(&(s->directory), dir_index, count);
2326 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002327 return ret;
bellarda0464332005-12-18 18:29:50 +00002328 adjust_dirindices(s, dir_index, -count);
bellardde167e42005-04-28 21:15:08 +00002329 return 0;
2330}
2331
bellarda0464332005-12-18 18:29:50 +00002332/*
2333 * Adapt the mappings of the cluster chain starting at first cluster
2334 * (i.e. if a file starts at first_cluster, the chain is followed according
2335 * to the modified fat, and the corresponding entries in s->mapping are
2336 * adjusted)
2337 */
2338static int commit_mappings(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002339 uint32_t first_cluster, int dir_index)
bellardde167e42005-04-28 21:15:08 +00002340{
Anthony Liguoric227f092009-10-01 16:12:16 -05002341 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2342 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002343 uint32_t cluster = first_cluster;
bellardde167e42005-04-28 21:15:08 +00002344
bellarda0464332005-12-18 18:29:50 +00002345 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002346
bellarda0464332005-12-18 18:29:50 +00002347 assert(mapping);
2348 assert(mapping->begin == first_cluster);
2349 mapping->first_mapping_index = -1;
2350 mapping->dir_index = dir_index;
2351 mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002352 MODE_DIRECTORY : MODE_NORMAL;
bellardde167e42005-04-28 21:15:08 +00002353
bellarda0464332005-12-18 18:29:50 +00002354 while (!fat_eof(s, cluster)) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002355 uint32_t c, c1;
bellardde167e42005-04-28 21:15:08 +00002356
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002357 for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
2358 c = c1, c1 = modified_fat_get(s, c1));
bellardde167e42005-04-28 21:15:08 +00002359
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002360 c++;
2361 if (c > mapping->end) {
2362 int index = array_index(&(s->mapping), mapping);
2363 int i, max_i = s->mapping.next - index;
2364 for (i = 1; i < max_i && mapping[i].begin < c; i++);
2365 while (--i > 0)
2366 remove_mapping(s, index + 1);
2367 }
2368 assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
2369 || mapping[1].begin >= c);
2370 mapping->end = c;
bellarda0464332005-12-18 18:29:50 +00002371
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002372 if (!fat_eof(s, c1)) {
2373 int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
2374 mapping_t* next_mapping = i >= s->mapping.next ? NULL :
2375 array_get(&(s->mapping), i);
bellarda0464332005-12-18 18:29:50 +00002376
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002377 if (next_mapping == NULL || next_mapping->begin > c1) {
2378 int i1 = array_index(&(s->mapping), mapping);
bellarda0464332005-12-18 18:29:50 +00002379
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002380 next_mapping = insert_mapping(s, c1, c1+1);
bellarda0464332005-12-18 18:29:50 +00002381
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002382 if (c1 < c)
2383 i1++;
2384 mapping = array_get(&(s->mapping), i1);
2385 }
bellarda0464332005-12-18 18:29:50 +00002386
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002387 next_mapping->dir_index = mapping->dir_index;
2388 next_mapping->first_mapping_index =
2389 mapping->first_mapping_index < 0 ?
2390 array_index(&(s->mapping), mapping) :
2391 mapping->first_mapping_index;
2392 next_mapping->path = mapping->path;
2393 next_mapping->mode = mapping->mode;
2394 next_mapping->read_only = mapping->read_only;
2395 if (mapping->mode & MODE_DIRECTORY) {
2396 next_mapping->info.dir.parent_mapping_index =
2397 mapping->info.dir.parent_mapping_index;
2398 next_mapping->info.dir.first_dir_index =
2399 mapping->info.dir.first_dir_index +
2400 0x10 * s->sectors_per_cluster *
2401 (mapping->end - mapping->begin);
2402 } else
2403 next_mapping->info.file.offset = mapping->info.file.offset +
2404 mapping->end - mapping->begin;
bellarda0464332005-12-18 18:29:50 +00002405
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002406 mapping = next_mapping;
2407 }
ths3b46e622007-09-17 08:09:54 +00002408
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002409 cluster = c1;
bellarda0464332005-12-18 18:29:50 +00002410 }
2411
2412 return 0;
2413}
2414
2415static int commit_direntries(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002416 int dir_index, int parent_mapping_index)
bellarda0464332005-12-18 18:29:50 +00002417{
Anthony Liguoric227f092009-10-01 16:12:16 -05002418 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002419 uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
Anthony Liguoric227f092009-10-01 16:12:16 -05002420 mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
bellarda0464332005-12-18 18:29:50 +00002421 int factor = 0x10 * s->sectors_per_cluster;
2422 int old_cluster_count, new_cluster_count;
Liam Merwick8d9401c2018-11-05 21:38:38 +00002423 int current_dir_index;
2424 int first_dir_index;
bellarda0464332005-12-18 18:29:50 +00002425 int ret, i;
2426 uint32_t c;
2427
bellarda0464332005-12-18 18:29:50 +00002428 assert(direntry);
2429 assert(mapping);
2430 assert(mapping->begin == first_cluster);
2431 assert(mapping->info.dir.first_dir_index < s->directory.next);
2432 assert(mapping->mode & MODE_DIRECTORY);
2433 assert(dir_index == 0 || is_directory(direntry));
2434
Liam Merwick8d9401c2018-11-05 21:38:38 +00002435 DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n",
2436 mapping->path, parent_mapping_index));
2437
2438 current_dir_index = mapping->info.dir.first_dir_index;
2439 first_dir_index = current_dir_index;
bellarda0464332005-12-18 18:29:50 +00002440 mapping->info.dir.parent_mapping_index = parent_mapping_index;
2441
2442 if (first_cluster == 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002443 old_cluster_count = new_cluster_count =
2444 s->last_cluster_of_root_directory;
bellarda0464332005-12-18 18:29:50 +00002445 } else {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002446 for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2447 c = fat_get(s, c))
2448 old_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002449
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002450 for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2451 c = modified_fat_get(s, c))
2452 new_cluster_count++;
bellarda0464332005-12-18 18:29:50 +00002453 }
2454
2455 if (new_cluster_count > old_cluster_count) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002456 if (insert_direntries(s,
2457 current_dir_index + factor * old_cluster_count,
2458 factor * (new_cluster_count - old_cluster_count)) == NULL)
2459 return -1;
bellarda0464332005-12-18 18:29:50 +00002460 } else if (new_cluster_count < old_cluster_count)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002461 remove_direntries(s,
2462 current_dir_index + factor * new_cluster_count,
2463 factor * (old_cluster_count - new_cluster_count));
bellarda0464332005-12-18 18:29:50 +00002464
2465 for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
Kevin Wolfebb72c92016-04-27 14:11:38 +02002466 direntry_t *first_direntry;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002467 void* direntry = array_get(&(s->directory), current_dir_index);
2468 int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
2469 s->sectors_per_cluster);
2470 if (ret)
2471 return ret;
Kevin Wolfebb72c92016-04-27 14:11:38 +02002472
2473 /* The first directory entry on the filesystem is the volume name */
2474 first_direntry = (direntry_t*) s->directory.pointer;
2475 assert(!memcmp(first_direntry->name, s->volume_label, 11));
2476
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002477 current_dir_index += factor;
bellarda0464332005-12-18 18:29:50 +00002478 }
2479
2480 ret = commit_mappings(s, first_cluster, dir_index);
2481 if (ret)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002482 return ret;
bellarda0464332005-12-18 18:29:50 +00002483
2484 /* recurse */
2485 for (i = 0; i < factor * new_cluster_count; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002486 direntry = array_get(&(s->directory), first_dir_index + i);
2487 if (is_directory(direntry) && !is_dot(direntry)) {
2488 mapping = find_mapping_for_cluster(s, first_cluster);
Liam Merwick8d9401c2018-11-05 21:38:38 +00002489 if (mapping == NULL) {
2490 return -1;
2491 }
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002492 assert(mapping->mode & MODE_DIRECTORY);
2493 ret = commit_direntries(s, first_dir_index + i,
2494 array_index(&(s->mapping), mapping));
2495 if (ret)
2496 return ret;
2497 }
bellardde167e42005-04-28 21:15:08 +00002498 }
bellarda0464332005-12-18 18:29:50 +00002499
bellardde167e42005-04-28 21:15:08 +00002500 return 0;
2501}
2502
bellarda0464332005-12-18 18:29:50 +00002503/* commit one file (adjust contents, adjust mapping),
2504 return first_mapping_index */
2505static int commit_one_file(BDRVVVFATState* s,
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002506 int dir_index, uint32_t offset)
bellarda0464332005-12-18 18:29:50 +00002507{
Anthony Liguoric227f092009-10-01 16:12:16 -05002508 direntry_t* direntry = array_get(&(s->directory), dir_index);
bellarda0464332005-12-18 18:29:50 +00002509 uint32_t c = begin_of_direntry(direntry);
2510 uint32_t first_cluster = c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002511 mapping_t* mapping = find_mapping_for_cluster(s, c);
bellarda0464332005-12-18 18:29:50 +00002512 uint32_t size = filesize_of_direntry(direntry);
Kevin Wolf443ba6b2018-11-14 13:50:16 +01002513 char *cluster;
bellarda0464332005-12-18 18:29:50 +00002514 uint32_t i;
2515 int fd = 0;
2516
2517 assert(offset < size);
2518 assert((offset % s->cluster_size) == 0);
2519
Liam Merwick8d9401c2018-11-05 21:38:38 +00002520 if (mapping == NULL) {
2521 return -1;
2522 }
2523
bellarda0464332005-12-18 18:29:50 +00002524 for (i = s->cluster_size; i < offset; i += s->cluster_size)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002525 c = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002526
Corey Bryant6165f4d2012-08-14 16:43:45 -04002527 fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
bellarda0464332005-12-18 18:29:50 +00002528 if (fd < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002529 fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
2530 strerror(errno), errno);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002531 return fd;
bellarda0464332005-12-18 18:29:50 +00002532 }
Stefan Weilce137822011-09-30 23:29:53 +02002533 if (offset > 0) {
2534 if (lseek(fd, offset, SEEK_SET) != offset) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002535 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002536 return -3;
2537 }
2538 }
bellarda0464332005-12-18 18:29:50 +00002539
Kevin Wolf443ba6b2018-11-14 13:50:16 +01002540 cluster = g_malloc(s->cluster_size);
2541
bellarda0464332005-12-18 18:29:50 +00002542 while (offset < size) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002543 uint32_t c1;
2544 int rest_size = (size - offset > s->cluster_size ?
2545 s->cluster_size : size - offset);
2546 int ret;
bellarda0464332005-12-18 18:29:50 +00002547
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002548 c1 = modified_fat_get(s, c);
bellarda0464332005-12-18 18:29:50 +00002549
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002550 assert((size - offset == 0 && fat_eof(s, c)) ||
2551 (size > offset && c >=2 && !fat_eof(s, c)));
bellarda0464332005-12-18 18:29:50 +00002552
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002553 ret = vvfat_read(s->bs, cluster2sector(s, c),
Marc-André Lureau78ee96d2017-06-22 13:04:16 +02002554 (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
bellarda0464332005-12-18 18:29:50 +00002555
Stefan Weilce137822011-09-30 23:29:53 +02002556 if (ret < 0) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002557 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002558 g_free(cluster);
2559 return ret;
2560 }
bellarda0464332005-12-18 18:29:50 +00002561
Stefan Weilce137822011-09-30 23:29:53 +02002562 if (write(fd, cluster, rest_size) < 0) {
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002563 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002564 g_free(cluster);
2565 return -2;
2566 }
bellarda0464332005-12-18 18:29:50 +00002567
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002568 offset += rest_size;
2569 c = c1;
bellarda0464332005-12-18 18:29:50 +00002570 }
2571
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002572 if (ftruncate(fd, size)) {
2573 perror("ftruncate()");
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002574 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002575 g_free(cluster);
Kirill A. Shutemov2dedf832010-01-20 00:56:14 +01002576 return -4;
2577 }
Corey Bryant2e1e79d2012-08-14 16:43:46 -04002578 qemu_close(fd);
Stefan Weilce137822011-09-30 23:29:53 +02002579 g_free(cluster);
bellarda0464332005-12-18 18:29:50 +00002580
2581 return commit_mappings(s, first_cluster, dir_index);
2582}
2583
2584#ifdef DEBUG
2585/* test, if all mappings point to valid direntries */
2586static void check1(BDRVVVFATState* s)
2587{
2588 int i;
2589 for (i = 0; i < s->mapping.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002590 mapping_t* mapping = array_get(&(s->mapping), i);
2591 if (mapping->mode & MODE_DELETED) {
2592 fprintf(stderr, "deleted\n");
2593 continue;
2594 }
2595 assert(mapping->dir_index < s->directory.next);
2596 direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
2597 assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
2598 if (mapping->mode & MODE_DIRECTORY) {
2599 assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
2600 assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
2601 }
bellarda0464332005-12-18 18:29:50 +00002602 }
2603}
2604
2605/* test, if all direntries have mappings */
2606static void check2(BDRVVVFATState* s)
2607{
2608 int i;
2609 int first_mapping = -1;
2610
2611 for (i = 0; i < s->directory.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002612 direntry_t* direntry = array_get(&(s->directory), i);
bellarda0464332005-12-18 18:29:50 +00002613
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002614 if (is_short_name(direntry) && begin_of_direntry(direntry)) {
2615 mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
2616 assert(mapping);
2617 assert(mapping->dir_index == i || is_dot(direntry));
2618 assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
2619 }
bellarda0464332005-12-18 18:29:50 +00002620
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002621 if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
2622 /* cluster start */
2623 int j, count = 0;
bellarda0464332005-12-18 18:29:50 +00002624
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002625 for (j = 0; j < s->mapping.next; j++) {
2626 mapping_t* mapping = array_get(&(s->mapping), j);
2627 if (mapping->mode & MODE_DELETED)
2628 continue;
2629 if (mapping->mode & MODE_DIRECTORY) {
2630 if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
2631 assert(++count == 1);
2632 if (mapping->first_mapping_index == -1)
2633 first_mapping = array_index(&(s->mapping), mapping);
2634 else
2635 assert(first_mapping == mapping->first_mapping_index);
2636 if (mapping->info.dir.parent_mapping_index < 0)
2637 assert(j == 0);
2638 else {
2639 mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
2640 assert(parent->mode & MODE_DIRECTORY);
2641 assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
2642 }
2643 }
2644 }
2645 }
2646 if (count == 0)
2647 first_mapping = -1;
2648 }
bellarda0464332005-12-18 18:29:50 +00002649 }
2650}
2651#endif
2652
2653static int handle_renames_and_mkdirs(BDRVVVFATState* s)
2654{
2655 int i;
2656
2657#ifdef DEBUG
2658 fprintf(stderr, "handle_renames\n");
2659 for (i = 0; i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002660 commit_t* commit = array_get(&(s->commits), i);
2661 fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action);
bellarda0464332005-12-18 18:29:50 +00002662 }
2663#endif
2664
2665 for (i = 0; i < s->commits.next;) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002666 commit_t* commit = array_get(&(s->commits), i);
2667 if (commit->action == ACTION_RENAME) {
2668 mapping_t* mapping = find_mapping_for_cluster(s,
2669 commit->param.rename.cluster);
Liam Merwick8d9401c2018-11-05 21:38:38 +00002670 char *old_path;
bellarda0464332005-12-18 18:29:50 +00002671
Liam Merwick8d9401c2018-11-05 21:38:38 +00002672 if (mapping == NULL) {
2673 return -1;
2674 }
2675 old_path = mapping->path;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002676 assert(commit->path);
2677 mapping->path = commit->path;
2678 if (rename(old_path, mapping->path))
2679 return -2;
bellarda0464332005-12-18 18:29:50 +00002680
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002681 if (mapping->mode & MODE_DIRECTORY) {
2682 int l1 = strlen(mapping->path);
2683 int l2 = strlen(old_path);
2684 int diff = l1 - l2;
2685 direntry_t* direntry = array_get(&(s->directory),
2686 mapping->info.dir.first_dir_index);
2687 uint32_t c = mapping->begin;
2688 int i = 0;
bellarda0464332005-12-18 18:29:50 +00002689
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002690 /* recurse */
2691 while (!fat_eof(s, c)) {
2692 do {
2693 direntry_t* d = direntry + i;
bellarda0464332005-12-18 18:29:50 +00002694
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002695 if (is_file(d) || (is_directory(d) && !is_dot(d))) {
Liam Merwick8d9401c2018-11-05 21:38:38 +00002696 int l;
2697 char *new_path;
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002698 mapping_t* m = find_mapping_for_cluster(s,
2699 begin_of_direntry(d));
Liam Merwick8d9401c2018-11-05 21:38:38 +00002700 if (m == NULL) {
2701 return -1;
2702 }
2703 l = strlen(m->path);
2704 new_path = g_malloc(l + diff + 1);
bellarda0464332005-12-18 18:29:50 +00002705
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002706 assert(!strncmp(m->path, mapping->path, l2));
bellarda0464332005-12-18 18:29:50 +00002707
blueswir1363a37d2008-08-21 17:58:08 +00002708 pstrcpy(new_path, l + diff + 1, mapping->path);
2709 pstrcpy(new_path + l1, l + diff + 1 - l1,
2710 m->path + l2);
bellarda0464332005-12-18 18:29:50 +00002711
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002712 schedule_rename(s, m->begin, new_path);
2713 }
2714 i++;
2715 } while((i % (0x10 * s->sectors_per_cluster)) != 0);
2716 c = fat_get(s, c);
2717 }
2718 }
bellarda0464332005-12-18 18:29:50 +00002719
Stefan Weilce137822011-09-30 23:29:53 +02002720 g_free(old_path);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002721 array_remove(&(s->commits), i);
2722 continue;
2723 } else if (commit->action == ACTION_MKDIR) {
2724 mapping_t* mapping;
2725 int j, parent_path_len;
bellarda0464332005-12-18 18:29:50 +00002726
bellard48c2f062005-12-19 22:11:49 +00002727#ifdef __MINGW32__
2728 if (mkdir(commit->path))
2729 return -5;
2730#else
2731 if (mkdir(commit->path, 0755))
2732 return -5;
2733#endif
bellarda0464332005-12-18 18:29:50 +00002734
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002735 mapping = insert_mapping(s, commit->param.mkdir.cluster,
2736 commit->param.mkdir.cluster + 1);
2737 if (mapping == NULL)
2738 return -6;
bellarda0464332005-12-18 18:29:50 +00002739
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002740 mapping->mode = MODE_DIRECTORY;
2741 mapping->read_only = 0;
2742 mapping->path = commit->path;
2743 j = s->directory.next;
2744 assert(j);
2745 insert_direntries(s, s->directory.next,
2746 0x10 * s->sectors_per_cluster);
2747 mapping->info.dir.first_dir_index = j;
bellarda0464332005-12-18 18:29:50 +00002748
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002749 parent_path_len = strlen(commit->path)
2750 - strlen(get_basename(commit->path)) - 1;
2751 for (j = 0; j < s->mapping.next; j++) {
2752 mapping_t* m = array_get(&(s->mapping), j);
2753 if (m->first_mapping_index < 0 && m != mapping &&
2754 !strncmp(m->path, mapping->path, parent_path_len) &&
2755 strlen(m->path) == parent_path_len)
2756 break;
2757 }
2758 assert(j < s->mapping.next);
2759 mapping->info.dir.parent_mapping_index = j;
bellarda0464332005-12-18 18:29:50 +00002760
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002761 array_remove(&(s->commits), i);
2762 continue;
2763 }
bellarda0464332005-12-18 18:29:50 +00002764
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002765 i++;
bellarda0464332005-12-18 18:29:50 +00002766 }
2767 return 0;
2768}
2769
2770/*
2771 * TODO: make sure that the short name is not matching *another* file
2772 */
2773static int handle_commits(BDRVVVFATState* s)
2774{
2775 int i, fail = 0;
2776
2777 vvfat_close_current_file(s);
2778
2779 for (i = 0; !fail && i < s->commits.next; i++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002780 commit_t* commit = array_get(&(s->commits), i);
2781 switch(commit->action) {
2782 case ACTION_RENAME: case ACTION_MKDIR:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002783 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002784 fail = -2;
2785 break;
2786 case ACTION_WRITEOUT: {
Blue Swirla6c6f762010-03-13 14:18:50 +00002787#ifndef NDEBUG
2788 /* these variables are only used by assert() below */
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002789 direntry_t* entry = array_get(&(s->directory),
2790 commit->param.writeout.dir_index);
2791 uint32_t begin = begin_of_direntry(entry);
2792 mapping_t* mapping = find_mapping_for_cluster(s, begin);
Blue Swirla6c6f762010-03-13 14:18:50 +00002793#endif
bellarda0464332005-12-18 18:29:50 +00002794
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002795 assert(mapping);
2796 assert(mapping->begin == begin);
2797 assert(commit->path == NULL);
bellarda0464332005-12-18 18:29:50 +00002798
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002799 if (commit_one_file(s, commit->param.writeout.dir_index,
2800 commit->param.writeout.modified_offset))
2801 fail = -3;
bellarda0464332005-12-18 18:29:50 +00002802
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002803 break;
2804 }
2805 case ACTION_NEW_FILE: {
2806 int begin = commit->param.new_file.first_cluster;
2807 mapping_t* mapping = find_mapping_for_cluster(s, begin);
2808 direntry_t* entry;
2809 int i;
bellarda0464332005-12-18 18:29:50 +00002810
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002811 /* find direntry */
2812 for (i = 0; i < s->directory.next; i++) {
2813 entry = array_get(&(s->directory), i);
2814 if (is_file(entry) && begin_of_direntry(entry) == begin)
2815 break;
2816 }
bellarda0464332005-12-18 18:29:50 +00002817
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002818 if (i >= s->directory.next) {
2819 fail = -6;
2820 continue;
2821 }
bellarda0464332005-12-18 18:29:50 +00002822
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002823 /* make sure there exists an initial mapping */
2824 if (mapping && mapping->begin != begin) {
2825 mapping->end = begin;
2826 mapping = NULL;
2827 }
2828 if (mapping == NULL) {
2829 mapping = insert_mapping(s, begin, begin+1);
2830 }
2831 /* most members will be fixed in commit_mappings() */
2832 assert(commit->path);
2833 mapping->path = commit->path;
2834 mapping->read_only = 0;
2835 mapping->mode = MODE_NORMAL;
2836 mapping->info.file.offset = 0;
bellarda0464332005-12-18 18:29:50 +00002837
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002838 if (commit_one_file(s, i, 0))
2839 fail = -7;
bellarda0464332005-12-18 18:29:50 +00002840
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002841 break;
2842 }
2843 default:
Blue Swirl43dc2a62010-03-18 18:41:57 +00002844 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002845 }
bellarda0464332005-12-18 18:29:50 +00002846 }
2847 if (i > 0 && array_remove_slice(&(s->commits), 0, i))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002848 return -1;
bellarda0464332005-12-18 18:29:50 +00002849 return fail;
2850}
2851
2852static int handle_deletes(BDRVVVFATState* s)
2853{
2854 int i, deferred = 1, deleted = 1;
2855
2856 /* delete files corresponding to mappings marked as deleted */
2857 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2858 while (deferred && deleted) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002859 deferred = 0;
2860 deleted = 0;
bellarda0464332005-12-18 18:29:50 +00002861
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002862 for (i = 1; i < s->mapping.next; i++) {
2863 mapping_t* mapping = array_get(&(s->mapping), i);
2864 if (mapping->mode & MODE_DELETED) {
2865 direntry_t* entry = array_get(&(s->directory),
2866 mapping->dir_index);
bellarda0464332005-12-18 18:29:50 +00002867
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002868 if (is_free(entry)) {
2869 /* remove file/directory */
2870 if (mapping->mode & MODE_DIRECTORY) {
2871 int j, next_dir_index = s->directory.next,
2872 first_dir_index = mapping->info.dir.first_dir_index;
bellarda0464332005-12-18 18:29:50 +00002873
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002874 if (rmdir(mapping->path) < 0) {
2875 if (errno == ENOTEMPTY) {
2876 deferred++;
2877 continue;
2878 } else
2879 return -5;
2880 }
bellarda0464332005-12-18 18:29:50 +00002881
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002882 for (j = 1; j < s->mapping.next; j++) {
2883 mapping_t* m = array_get(&(s->mapping), j);
2884 if (m->mode & MODE_DIRECTORY &&
2885 m->info.dir.first_dir_index >
2886 first_dir_index &&
2887 m->info.dir.first_dir_index <
2888 next_dir_index)
2889 next_dir_index =
2890 m->info.dir.first_dir_index;
2891 }
2892 remove_direntries(s, first_dir_index,
2893 next_dir_index - first_dir_index);
bellarda0464332005-12-18 18:29:50 +00002894
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002895 deleted++;
2896 }
2897 } else {
2898 if (unlink(mapping->path))
2899 return -4;
2900 deleted++;
2901 }
2902 DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
2903 remove_mapping(s, i);
2904 }
2905 }
bellarda0464332005-12-18 18:29:50 +00002906 }
2907
2908 return 0;
2909}
2910
2911/*
2912 * synchronize mapping with new state:
2913 *
2914 * - copy FAT (with bdrv_read)
2915 * - mark all filenames corresponding to mappings as deleted
2916 * - recurse direntries from root (using bs->bdrv_read)
2917 * - delete files corresponding to mappings marked as deleted
2918 */
2919static int do_commit(BDRVVVFATState* s)
2920{
2921 int ret = 0;
2922
2923 /* the real meat are the commits. Nothing to do? Move along! */
2924 if (s->commits.next == 0)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002925 return 0;
bellarda0464332005-12-18 18:29:50 +00002926
2927 vvfat_close_current_file(s);
2928
2929 ret = handle_renames_and_mkdirs(s);
2930 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002931 fprintf(stderr, "Error handling renames (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002932 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002933 return ret;
bellarda0464332005-12-18 18:29:50 +00002934 }
2935
ths5fafdf22007-09-16 21:08:06 +00002936 /* copy FAT (with bdrv_read) */
bellarda0464332005-12-18 18:29:50 +00002937 memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
2938
2939 /* recurse direntries from root (using bs->bdrv_read) */
2940 ret = commit_direntries(s, 0, -1);
2941 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002942 fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002943 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002944 return ret;
bellarda0464332005-12-18 18:29:50 +00002945 }
2946
2947 ret = handle_commits(s);
2948 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002949 fprintf(stderr, "Error handling commits (%d)\n", ret);
Blue Swirl43dc2a62010-03-18 18:41:57 +00002950 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002951 return ret;
bellarda0464332005-12-18 18:29:50 +00002952 }
2953
2954 ret = handle_deletes(s);
2955 if (ret) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002956 fprintf(stderr, "Error deleting\n");
Blue Swirl43dc2a62010-03-18 18:41:57 +00002957 abort();
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002958 return ret;
bellarda0464332005-12-18 18:29:50 +00002959 }
2960
Max Reitzd470ad42017-11-10 21:31:09 +01002961 if (s->qcow->bs->drv && s->qcow->bs->drv->bdrv_make_empty) {
Kevin Wolfeecc7742016-05-30 17:13:09 +02002962 s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
Kevin Wolf7704df92011-11-08 10:50:12 +01002963 }
bellarda0464332005-12-18 18:29:50 +00002964
2965 memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
2966
2967DLOG(checkpoint());
2968 return 0;
2969}
2970
2971static int try_commit(BDRVVVFATState* s)
2972{
2973 vvfat_close_current_file(s);
2974DLOG(checkpoint());
2975 if(!is_consistent(s))
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02002976 return -1;
bellarda0464332005-12-18 18:29:50 +00002977 return do_commit(s);
2978}
2979
ths5fafdf22007-09-16 21:08:06 +00002980static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
bellardde167e42005-04-28 21:15:08 +00002981 const uint8_t *buf, int nb_sectors)
2982{
ths5fafdf22007-09-16 21:08:06 +00002983 BDRVVVFATState *s = bs->opaque;
bellarda0464332005-12-18 18:29:50 +00002984 int i, ret;
bellardde167e42005-04-28 21:15:08 +00002985
bellarda0464332005-12-18 18:29:50 +00002986DLOG(checkpoint());
bellardde167e42005-04-28 21:15:08 +00002987
Kevin Wolfac48e382010-09-10 12:27:02 +02002988 /* Check if we're operating in read-only mode */
2989 if (s->qcow == NULL) {
2990 return -EACCES;
2991 }
2992
bellarda0464332005-12-18 18:29:50 +00002993 vvfat_close_current_file(s);
bellardde167e42005-04-28 21:15:08 +00002994
bellarda0464332005-12-18 18:29:50 +00002995 /*
2996 * Some sanity checks:
2997 * - do not allow writing to the boot sector
bellarda0464332005-12-18 18:29:50 +00002998 */
bellardde167e42005-04-28 21:15:08 +00002999
Hervé Poussineau4dc705d2017-05-22 23:11:57 +02003000 if (sector_num < s->offset_to_fat)
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003001 return -1;
bellardde167e42005-04-28 21:15:08 +00003002
bellarda0464332005-12-18 18:29:50 +00003003 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003004 i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
3005 mapping_t* mapping = find_mapping_for_cluster(s, i);
3006 if (mapping) {
3007 if (mapping->read_only) {
3008 fprintf(stderr, "Tried to write to write-protected file %s\n",
3009 mapping->path);
3010 return -1;
3011 }
bellardde167e42005-04-28 21:15:08 +00003012
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003013 if (mapping->mode & MODE_DIRECTORY) {
3014 int begin = cluster2sector(s, i);
3015 int end = begin + s->sectors_per_cluster, k;
3016 int dir_index;
3017 const direntry_t* direntries;
3018 long_file_name lfn;
bellardde167e42005-04-28 21:15:08 +00003019
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003020 lfn_init(&lfn);
bellardde167e42005-04-28 21:15:08 +00003021
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003022 if (begin < sector_num)
3023 begin = sector_num;
3024 if (end > sector_num + nb_sectors)
3025 end = sector_num + nb_sectors;
3026 dir_index = mapping->dir_index +
3027 0x10 * (begin - mapping->begin * s->sectors_per_cluster);
3028 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
bellardde167e42005-04-28 21:15:08 +00003029
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003030 for (k = 0; k < (end - begin) * 0x10; k++) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003031 /* no access to the direntry of a read-only file */
Hervé Poussineaue03da262017-07-15 15:28:40 +02003032 if (is_short_name(direntries + k) &&
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003033 (direntries[k].attributes & 1)) {
3034 if (memcmp(direntries + k,
3035 array_get(&(s->directory), dir_index + k),
3036 sizeof(direntry_t))) {
Alistair Francis2ab4b132017-09-11 12:52:50 -07003037 warn_report("tried to write to write-protected "
3038 "file");
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003039 return -1;
3040 }
3041 }
3042 }
3043 }
3044 i = mapping->end;
3045 } else
3046 i++;
bellardde167e42005-04-28 21:15:08 +00003047 }
bellarda0464332005-12-18 18:29:50 +00003048
3049 /*
3050 * Use qcow backend. Commit later.
3051 */
3052DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
Kevin Wolf18d51c42016-05-31 14:42:08 +02003053 ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
bellarda0464332005-12-18 18:29:50 +00003054 if (ret < 0) {
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003055 fprintf(stderr, "Error writing to qcow backend\n");
3056 return ret;
bellarda0464332005-12-18 18:29:50 +00003057 }
3058
3059 for (i = sector2cluster(s, sector_num);
Hervé Poussineaud6a7e542017-05-22 23:11:54 +02003060 i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
3061 if (i >= 0)
3062 s->used_clusters[i] |= USED_ALLOCATED;
bellarda0464332005-12-18 18:29:50 +00003063
3064DLOG(checkpoint());
3065 /* TODO: add timeout */
3066 try_commit(s);
3067
3068DLOG(checkpoint());
3069 return 0;
3070}
3071
Kevin Wolf4575eb42016-04-26 17:14:08 +02003072static int coroutine_fn
3073vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3074 QEMUIOVector *qiov, int flags)
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003075{
3076 int ret;
3077 BDRVVVFATState *s = bs->opaque;
Kevin Wolf4575eb42016-04-26 17:14:08 +02003078 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
3079 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
3080 void *buf;
3081
3082 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3083 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
3084
3085 buf = g_try_malloc(bytes);
3086 if (bytes && buf == NULL) {
3087 return -ENOMEM;
3088 }
3089 qemu_iovec_to_buf(qiov, 0, buf, bytes);
3090
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003091 qemu_co_mutex_lock(&s->lock);
3092 ret = vvfat_write(bs, sector_num, buf, nb_sectors);
3093 qemu_co_mutex_unlock(&s->lock);
Kevin Wolf4575eb42016-04-26 17:14:08 +02003094
3095 g_free(buf);
3096
Paolo Bonzinie183ef72011-10-20 13:16:23 +02003097 return ret;
3098}
3099
Eric Blakefba39982018-02-13 14:27:00 -06003100static int coroutine_fn vvfat_co_block_status(BlockDriverState *bs,
3101 bool want_zero, int64_t offset,
3102 int64_t bytes, int64_t *n,
3103 int64_t *map,
3104 BlockDriverState **file)
bellarda0464332005-12-18 18:29:50 +00003105{
Eric Blakefba39982018-02-13 14:27:00 -06003106 *n = bytes;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +02003107 return BDRV_BLOCK_DATA;
bellarda0464332005-12-18 18:29:50 +00003108}
3109
Kevin Wolf4575eb42016-04-26 17:14:08 +02003110static int coroutine_fn
3111write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3112 QEMUIOVector *qiov, int flags)
3113{
Paolo Bonzini254aee42017-06-29 15:27:43 +02003114 int ret;
3115
Kevin Wolf9217e262010-09-10 12:27:03 +02003116 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Paolo Bonzini254aee42017-06-29 15:27:43 +02003117 qemu_co_mutex_lock(&s->lock);
3118 ret = try_commit(s);
3119 qemu_co_mutex_unlock(&s->lock);
3120
3121 return ret;
bellarda0464332005-12-18 18:29:50 +00003122}
3123
3124static void write_target_close(BlockDriverState *bs) {
Kevin Wolf9217e262010-09-10 12:27:03 +02003125 BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
Kevin Wolfeecc7742016-05-30 17:13:09 +02003126 bdrv_unref_child(s->bs, s->qcow);
Stefan Weilce137822011-09-30 23:29:53 +02003127 g_free(s->qcow_filename);
bellarda0464332005-12-18 18:29:50 +00003128}
3129
3130static BlockDriver vvfat_write_target = {
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003131 .format_name = "vvfat_write_target",
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003132 .instance_size = sizeof(void*),
Kevin Wolf4575eb42016-04-26 17:14:08 +02003133 .bdrv_co_pwritev = write_target_commit,
Christoph Hellwigf9e96432009-05-27 16:14:13 +02003134 .bdrv_close = write_target_close,
bellarda0464332005-12-18 18:29:50 +00003135};
3136
Kevin Wolfeecc7742016-05-30 17:13:09 +02003137static void vvfat_qcow_options(int *child_flags, QDict *child_options,
3138 int parent_flags, QDict *parent_options)
bellarda0464332005-12-18 18:29:50 +00003139{
Alberto Garciaf87a0e22016-09-15 17:53:02 +03003140 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfe35bdc12018-10-05 18:57:40 +02003141 qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off");
Fam Zheng4f8e3a12018-03-15 11:45:07 +08003142 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolfeecc7742016-05-30 17:13:09 +02003143}
3144
3145static const BdrvChildRole child_vvfat_qcow = {
Kevin Wolf6cd5c9d2018-05-29 17:17:45 +02003146 .parent_is_bds = true,
Kevin Wolfeecc7742016-05-30 17:13:09 +02003147 .inherit_options = vvfat_qcow_options,
3148};
3149
3150static int enable_write_target(BlockDriverState *bs, Error **errp)
3151{
3152 BDRVVVFATState *s = bs->opaque;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003153 BlockDriver *bdrv_qcow = NULL;
Kevin Wolf5db15a52015-09-14 15:33:33 +02003154 BlockDriverState *backing;
Chunyan Liufacdbb02014-06-05 17:20:52 +08003155 QemuOpts *opts = NULL;
Kevin Wolfa6552112010-09-10 12:27:04 +02003156 int ret;
bellarda0464332005-12-18 18:29:50 +00003157 int size = sector2cluster(s, s->sector_count);
Max Reitze6641712015-08-26 19:47:48 +02003158 QDict *options;
3159
bellarda0464332005-12-18 18:29:50 +00003160 s->used_clusters = calloc(size, 1);
3161
Anthony Liguoric227f092009-10-01 16:12:16 -05003162 array_init(&(s->commits), sizeof(commit_t));
bellarda0464332005-12-18 18:29:50 +00003163
Jeff Cody9a29e182015-01-22 08:03:30 -05003164 s->qcow_filename = g_malloc(PATH_MAX);
3165 ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
Jim Meyeringeba25052012-05-28 09:27:54 +02003166 if (ret < 0) {
Markus Armbruster68c70af2014-05-16 11:00:17 +02003167 error_setg_errno(errp, -ret, "can't create temporary file");
Fam Zheng78f27bd2013-07-17 17:57:37 +08003168 goto err;
Jim Meyeringeba25052012-05-28 09:27:54 +02003169 }
Kevin Wolf91a073a2009-05-27 14:48:06 +02003170
3171 bdrv_qcow = bdrv_find_format("qcow");
Max Reitz1bcb15c2014-12-02 18:32:43 +01003172 if (!bdrv_qcow) {
3173 error_setg(errp, "Failed to locate qcow driver");
3174 ret = -ENOENT;
3175 goto err;
3176 }
3177
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003178 opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003179 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
3180 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003181 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
Kevin Wolf91a073a2009-05-27 14:48:06 +02003182
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003183 ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
Chunyan Liufacdbb02014-06-05 17:20:52 +08003184 qemu_opts_del(opts);
Fam Zheng78f27bd2013-07-17 17:57:37 +08003185 if (ret < 0) {
3186 goto err;
3187 }
Kevin Wolfa6552112010-09-10 12:27:04 +02003188
Max Reitze6641712015-08-26 19:47:48 +02003189 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003190 qdict_put_str(options, "write-target.driver", "qcow");
Kevin Wolfeecc7742016-05-30 17:13:09 +02003191 s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
3192 &child_vvfat_qcow, false, errp);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003193 qobject_unref(options);
Max Reitz5b363932016-05-17 16:41:31 +02003194 if (!s->qcow) {
3195 ret = -EINVAL;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003196 goto err;
Kevin Wolfd6e90982010-03-31 14:40:27 +02003197 }
bellarda0464332005-12-18 18:29:50 +00003198
3199#ifndef _WIN32
3200 unlink(s->qcow_filename);
3201#endif
3202
Kevin Wolfa8a4d152017-02-15 20:51:58 +01003203 backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
3204 &error_abort);
3205 *(void**) backing->opaque = s;
3206
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003207 bdrv_set_backing_hd(s->bs, backing, &error_abort);
Kevin Wolf5db15a52015-09-14 15:33:33 +02003208 bdrv_unref(backing);
3209
bellardde167e42005-04-28 21:15:08 +00003210 return 0;
Fam Zheng78f27bd2013-07-17 17:57:37 +08003211
3212err:
3213 g_free(s->qcow_filename);
3214 s->qcow_filename = NULL;
3215 return ret;
bellardde167e42005-04-28 21:15:08 +00003216}
3217
Kevin Wolf91ef3822016-12-20 16:23:46 +01003218static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
3219 const BdrvChildRole *role,
Kevin Wolfe0995dc2017-09-14 12:47:11 +02003220 BlockReopenQueue *reopen_queue,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003221 uint64_t perm, uint64_t shared,
3222 uint64_t *nperm, uint64_t *nshared)
3223{
3224 BDRVVVFATState *s = bs->opaque;
3225
3226 assert(c == s->qcow || role == &child_backing);
3227
3228 if (c == s->qcow) {
3229 /* This is a private node, nobody should try to attach to it */
3230 *nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
3231 *nshared = BLK_PERM_WRITE_UNCHANGED;
3232 } else {
3233 /* The backing file is there so 'commit' can use it. vvfat doesn't
3234 * access it in any way. */
3235 *nperm = 0;
3236 *nshared = BLK_PERM_ALL;
3237 }
3238}
3239
bellardde167e42005-04-28 21:15:08 +00003240static void vvfat_close(BlockDriverState *bs)
3241{
3242 BDRVVVFATState *s = bs->opaque;
3243
3244 vvfat_close_current_file(s);
3245 array_free(&(s->fat));
3246 array_free(&(s->directory));
3247 array_free(&(s->mapping));
Stefan Weilce137822011-09-30 23:29:53 +02003248 g_free(s->cluster_buffer);
Kevin Wolf3397f0c2011-11-22 16:52:13 +01003249
3250 if (s->qcow) {
3251 migrate_del_blocker(s->migration_blocker);
3252 error_free(s->migration_blocker);
3253 }
bellardde167e42005-04-28 21:15:08 +00003254}
3255
Max Reitz26542672019-02-01 20:29:25 +01003256static const char *const vvfat_strong_runtime_opts[] = {
3257 "dir",
3258 "fat-type",
3259 "floppy",
3260 "label",
3261 "rw",
3262
3263 NULL
3264};
3265
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003266static BlockDriver bdrv_vvfat = {
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003267 .format_name = "vvfat",
3268 .protocol_name = "fat",
3269 .instance_size = sizeof(BDRVVVFATState),
3270
3271 .bdrv_parse_filename = vvfat_parse_filename,
3272 .bdrv_file_open = vvfat_open,
Eric Blakea6506482016-06-23 16:37:17 -06003273 .bdrv_refresh_limits = vvfat_refresh_limits,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003274 .bdrv_close = vvfat_close,
Kevin Wolf91ef3822016-12-20 16:23:46 +01003275 .bdrv_child_perm = vvfat_child_perm,
Kevin Wolf7ad9be62013-04-12 19:42:04 +02003276
Kevin Wolf4575eb42016-04-26 17:14:08 +02003277 .bdrv_co_preadv = vvfat_co_preadv,
3278 .bdrv_co_pwritev = vvfat_co_pwritev,
Eric Blakefba39982018-02-13 14:27:00 -06003279 .bdrv_co_block_status = vvfat_co_block_status,
Max Reitz26542672019-02-01 20:29:25 +01003280
3281 .strong_runtime_opts = vvfat_strong_runtime_opts,
bellardde167e42005-04-28 21:15:08 +00003282};
3283
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003284static void bdrv_vvfat_init(void)
3285{
3286 bdrv_register(&bdrv_vvfat);
3287}
3288
3289block_init(bdrv_vvfat_init);
3290
bellarda0464332005-12-18 18:29:50 +00003291#ifdef DEBUG
Thomas Huth7a6ab452017-09-13 12:21:28 +02003292static void checkpoint(void)
3293{
Anthony Liguoric227f092009-10-01 16:12:16 -05003294 assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
bellarda0464332005-12-18 18:29:50 +00003295 check1(vvv);
3296 check2(vvv);
3297 assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
bellarda0464332005-12-18 18:29:50 +00003298}
3299#endif