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