1c30a15e5SDonggeun Kim /* 2c30a15e5SDonggeun Kim * fat_write.c 3c30a15e5SDonggeun Kim * 4c30a15e5SDonggeun Kim * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim 5c30a15e5SDonggeun Kim * 6c30a15e5SDonggeun Kim * See file CREDITS for list of people who contributed to this 7c30a15e5SDonggeun Kim * project. 8c30a15e5SDonggeun Kim * 9c30a15e5SDonggeun Kim * This program is free software; you can redistribute it and/or 10c30a15e5SDonggeun Kim * modify it under the terms of the GNU General Public License as 11c30a15e5SDonggeun Kim * published by the Free Software Foundation; either version 2 of 12c30a15e5SDonggeun Kim * the License, or (at your option) any later version. 13c30a15e5SDonggeun Kim * 14c30a15e5SDonggeun Kim * This program is distributed in the hope that it will be useful, 15c30a15e5SDonggeun Kim * but WITHOUT ANY WARRANTY; without even the implied warranty of 16c30a15e5SDonggeun Kim * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17c30a15e5SDonggeun Kim * GNU General Public License for more details. 18c30a15e5SDonggeun Kim * 19c30a15e5SDonggeun Kim * You should have received a copy of the GNU General Public License 20c30a15e5SDonggeun Kim * along with this program; if not, write to the Free Software 21c30a15e5SDonggeun Kim * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22c30a15e5SDonggeun Kim * MA 02111-1307 USA 23c30a15e5SDonggeun Kim */ 24c30a15e5SDonggeun Kim 25c30a15e5SDonggeun Kim #include <common.h> 26c30a15e5SDonggeun Kim #include <command.h> 27c30a15e5SDonggeun Kim #include <config.h> 28c30a15e5SDonggeun Kim #include <fat.h> 29c30a15e5SDonggeun Kim #include <asm/byteorder.h> 30c30a15e5SDonggeun Kim #include <part.h> 31c30a15e5SDonggeun Kim #include "fat.c" 32c30a15e5SDonggeun Kim 33c30a15e5SDonggeun Kim static void uppercase(char *str, int len) 34c30a15e5SDonggeun Kim { 35c30a15e5SDonggeun Kim int i; 36c30a15e5SDonggeun Kim 37c30a15e5SDonggeun Kim for (i = 0; i < len; i++) { 38c30a15e5SDonggeun Kim TOUPPER(*str); 39c30a15e5SDonggeun Kim str++; 40c30a15e5SDonggeun Kim } 41c30a15e5SDonggeun Kim } 42c30a15e5SDonggeun Kim 43c30a15e5SDonggeun Kim static int total_sector; 44079df722SDonggeun Kim static int disk_write(__u32 block, __u32 nr_blocks, void *buf) 45c30a15e5SDonggeun Kim { 46079df722SDonggeun Kim if (!cur_dev || !cur_dev->block_write) 47c30a15e5SDonggeun Kim return -1; 48c30a15e5SDonggeun Kim 49079df722SDonggeun Kim if (cur_part_info.start + block + nr_blocks > 50079df722SDonggeun Kim cur_part_info.start + total_sector) { 51c30a15e5SDonggeun Kim printf("error: overflow occurs\n"); 52c30a15e5SDonggeun Kim return -1; 53c30a15e5SDonggeun Kim } 54c30a15e5SDonggeun Kim 55079df722SDonggeun Kim return cur_dev->block_write(cur_dev->dev, 56079df722SDonggeun Kim cur_part_info.start + block, nr_blocks, buf); 57c30a15e5SDonggeun Kim } 58c30a15e5SDonggeun Kim 59c30a15e5SDonggeun Kim /* 60c30a15e5SDonggeun Kim * Set short name in directory entry 61c30a15e5SDonggeun Kim */ 62c30a15e5SDonggeun Kim static void set_name(dir_entry *dirent, const char *filename) 63c30a15e5SDonggeun Kim { 64c30a15e5SDonggeun Kim char s_name[VFAT_MAXLEN_BYTES]; 65c30a15e5SDonggeun Kim char *period; 66c30a15e5SDonggeun Kim int period_location, len, i, ext_num; 67c30a15e5SDonggeun Kim 68c30a15e5SDonggeun Kim if (filename == NULL) 69c30a15e5SDonggeun Kim return; 70c30a15e5SDonggeun Kim 71c30a15e5SDonggeun Kim len = strlen(filename); 72c30a15e5SDonggeun Kim if (len == 0) 73c30a15e5SDonggeun Kim return; 74c30a15e5SDonggeun Kim 75c30a15e5SDonggeun Kim memcpy(s_name, filename, len); 76c30a15e5SDonggeun Kim uppercase(s_name, len); 77c30a15e5SDonggeun Kim 78c30a15e5SDonggeun Kim period = strchr(s_name, '.'); 79c30a15e5SDonggeun Kim if (period == NULL) { 80c30a15e5SDonggeun Kim period_location = len; 81c30a15e5SDonggeun Kim ext_num = 0; 82c30a15e5SDonggeun Kim } else { 83c30a15e5SDonggeun Kim period_location = period - s_name; 84c30a15e5SDonggeun Kim ext_num = len - period_location - 1; 85c30a15e5SDonggeun Kim } 86c30a15e5SDonggeun Kim 87c30a15e5SDonggeun Kim /* Pad spaces when the length of file name is shorter than eight */ 88c30a15e5SDonggeun Kim if (period_location < 8) { 89c30a15e5SDonggeun Kim memcpy(dirent->name, s_name, period_location); 90c30a15e5SDonggeun Kim for (i = period_location; i < 8; i++) 91c30a15e5SDonggeun Kim dirent->name[i] = ' '; 92c30a15e5SDonggeun Kim } else if (period_location == 8) { 93c30a15e5SDonggeun Kim memcpy(dirent->name, s_name, period_location); 94c30a15e5SDonggeun Kim } else { 95c30a15e5SDonggeun Kim memcpy(dirent->name, s_name, 6); 96c30a15e5SDonggeun Kim dirent->name[6] = '~'; 97c30a15e5SDonggeun Kim dirent->name[7] = '1'; 98c30a15e5SDonggeun Kim } 99c30a15e5SDonggeun Kim 100c30a15e5SDonggeun Kim if (ext_num < 3) { 101c30a15e5SDonggeun Kim memcpy(dirent->ext, s_name + period_location + 1, ext_num); 102c30a15e5SDonggeun Kim for (i = ext_num; i < 3; i++) 103c30a15e5SDonggeun Kim dirent->ext[i] = ' '; 104c30a15e5SDonggeun Kim } else 105c30a15e5SDonggeun Kim memcpy(dirent->ext, s_name + period_location + 1, 3); 106c30a15e5SDonggeun Kim 107c30a15e5SDonggeun Kim debug("name : %s\n", dirent->name); 108c30a15e5SDonggeun Kim debug("ext : %s\n", dirent->ext); 109c30a15e5SDonggeun Kim } 110c30a15e5SDonggeun Kim 111627182eaSDonggeun Kim static __u8 num_of_fats; 112c30a15e5SDonggeun Kim /* 113c30a15e5SDonggeun Kim * Write fat buffer into block device 114c30a15e5SDonggeun Kim */ 115c30a15e5SDonggeun Kim static int flush_fat_buffer(fsdata *mydata) 116c30a15e5SDonggeun Kim { 117c30a15e5SDonggeun Kim int getsize = FATBUFBLOCKS; 118c30a15e5SDonggeun Kim __u32 fatlength = mydata->fatlength; 119c30a15e5SDonggeun Kim __u8 *bufptr = mydata->fatbuf; 120c30a15e5SDonggeun Kim __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS; 121c30a15e5SDonggeun Kim 122c30a15e5SDonggeun Kim fatlength *= mydata->sect_size; 123c30a15e5SDonggeun Kim startblock += mydata->fat_sect; 124c30a15e5SDonggeun Kim 125c30a15e5SDonggeun Kim if (getsize > fatlength) 126c30a15e5SDonggeun Kim getsize = fatlength; 127c30a15e5SDonggeun Kim 128c30a15e5SDonggeun Kim /* Write FAT buf */ 129c30a15e5SDonggeun Kim if (disk_write(startblock, getsize, bufptr) < 0) { 130c30a15e5SDonggeun Kim debug("error: writing FAT blocks\n"); 131c30a15e5SDonggeun Kim return -1; 132c30a15e5SDonggeun Kim } 133c30a15e5SDonggeun Kim 134627182eaSDonggeun Kim if (num_of_fats == 2) { 135627182eaSDonggeun Kim /* Update corresponding second FAT blocks */ 136627182eaSDonggeun Kim startblock += mydata->fatlength; 137627182eaSDonggeun Kim if (disk_write(startblock, getsize, bufptr) < 0) { 138627182eaSDonggeun Kim debug("error: writing second FAT blocks\n"); 139627182eaSDonggeun Kim return -1; 140627182eaSDonggeun Kim } 141627182eaSDonggeun Kim } 142627182eaSDonggeun Kim 143c30a15e5SDonggeun Kim return 0; 144c30a15e5SDonggeun Kim } 145c30a15e5SDonggeun Kim 146c30a15e5SDonggeun Kim /* 147c30a15e5SDonggeun Kim * Get the entry at index 'entry' in a FAT (12/16/32) table. 148c30a15e5SDonggeun Kim * On failure 0x00 is returned. 149c30a15e5SDonggeun Kim * When bufnum is changed, write back the previous fatbuf to the disk. 150c30a15e5SDonggeun Kim */ 151c30a15e5SDonggeun Kim static __u32 get_fatent_value(fsdata *mydata, __u32 entry) 152c30a15e5SDonggeun Kim { 153c30a15e5SDonggeun Kim __u32 bufnum; 154c30a15e5SDonggeun Kim __u32 off16, offset; 155c30a15e5SDonggeun Kim __u32 ret = 0x00; 156c30a15e5SDonggeun Kim __u16 val1, val2; 157c30a15e5SDonggeun Kim 158c30a15e5SDonggeun Kim switch (mydata->fatsize) { 159c30a15e5SDonggeun Kim case 32: 160c30a15e5SDonggeun Kim bufnum = entry / FAT32BUFSIZE; 161c30a15e5SDonggeun Kim offset = entry - bufnum * FAT32BUFSIZE; 162c30a15e5SDonggeun Kim break; 163c30a15e5SDonggeun Kim case 16: 164c30a15e5SDonggeun Kim bufnum = entry / FAT16BUFSIZE; 165c30a15e5SDonggeun Kim offset = entry - bufnum * FAT16BUFSIZE; 166c30a15e5SDonggeun Kim break; 167c30a15e5SDonggeun Kim case 12: 168c30a15e5SDonggeun Kim bufnum = entry / FAT12BUFSIZE; 169c30a15e5SDonggeun Kim offset = entry - bufnum * FAT12BUFSIZE; 170c30a15e5SDonggeun Kim break; 171c30a15e5SDonggeun Kim 172c30a15e5SDonggeun Kim default: 173c30a15e5SDonggeun Kim /* Unsupported FAT size */ 174c30a15e5SDonggeun Kim return ret; 175c30a15e5SDonggeun Kim } 176c30a15e5SDonggeun Kim 177c30a15e5SDonggeun Kim debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", 178c30a15e5SDonggeun Kim mydata->fatsize, entry, entry, offset, offset); 179c30a15e5SDonggeun Kim 180c30a15e5SDonggeun Kim /* Read a new block of FAT entries into the cache. */ 181c30a15e5SDonggeun Kim if (bufnum != mydata->fatbufnum) { 182c30a15e5SDonggeun Kim int getsize = FATBUFBLOCKS; 183c30a15e5SDonggeun Kim __u8 *bufptr = mydata->fatbuf; 184c30a15e5SDonggeun Kim __u32 fatlength = mydata->fatlength; 185c30a15e5SDonggeun Kim __u32 startblock = bufnum * FATBUFBLOCKS; 186c30a15e5SDonggeun Kim 187c30a15e5SDonggeun Kim if (getsize > fatlength) 188c30a15e5SDonggeun Kim getsize = fatlength; 189c30a15e5SDonggeun Kim 190c30a15e5SDonggeun Kim fatlength *= mydata->sect_size; /* We want it in bytes now */ 191c30a15e5SDonggeun Kim startblock += mydata->fat_sect; /* Offset from start of disk */ 192c30a15e5SDonggeun Kim 193c30a15e5SDonggeun Kim /* Write back the fatbuf to the disk */ 194c30a15e5SDonggeun Kim if (mydata->fatbufnum != -1) { 195c30a15e5SDonggeun Kim if (flush_fat_buffer(mydata) < 0) 196c30a15e5SDonggeun Kim return -1; 197c30a15e5SDonggeun Kim } 198c30a15e5SDonggeun Kim 199c30a15e5SDonggeun Kim if (disk_read(startblock, getsize, bufptr) < 0) { 200c30a15e5SDonggeun Kim debug("Error reading FAT blocks\n"); 201c30a15e5SDonggeun Kim return ret; 202c30a15e5SDonggeun Kim } 203c30a15e5SDonggeun Kim mydata->fatbufnum = bufnum; 204c30a15e5SDonggeun Kim } 205c30a15e5SDonggeun Kim 206c30a15e5SDonggeun Kim /* Get the actual entry from the table */ 207c30a15e5SDonggeun Kim switch (mydata->fatsize) { 208c30a15e5SDonggeun Kim case 32: 209c30a15e5SDonggeun Kim ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); 210c30a15e5SDonggeun Kim break; 211c30a15e5SDonggeun Kim case 16: 212c30a15e5SDonggeun Kim ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); 213c30a15e5SDonggeun Kim break; 214c30a15e5SDonggeun Kim case 12: 215c30a15e5SDonggeun Kim off16 = (offset * 3) / 4; 216c30a15e5SDonggeun Kim 217c30a15e5SDonggeun Kim switch (offset & 0x3) { 218c30a15e5SDonggeun Kim case 0: 219c30a15e5SDonggeun Kim ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); 220c30a15e5SDonggeun Kim ret &= 0xfff; 221c30a15e5SDonggeun Kim break; 222c30a15e5SDonggeun Kim case 1: 223c30a15e5SDonggeun Kim val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 224c30a15e5SDonggeun Kim val1 &= 0xf000; 225c30a15e5SDonggeun Kim val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 226c30a15e5SDonggeun Kim val2 &= 0x00ff; 227c30a15e5SDonggeun Kim ret = (val2 << 4) | (val1 >> 12); 228c30a15e5SDonggeun Kim break; 229c30a15e5SDonggeun Kim case 2: 230c30a15e5SDonggeun Kim val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 231c30a15e5SDonggeun Kim val1 &= 0xff00; 232c30a15e5SDonggeun Kim val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 233c30a15e5SDonggeun Kim val2 &= 0x000f; 234c30a15e5SDonggeun Kim ret = (val2 << 8) | (val1 >> 8); 235c30a15e5SDonggeun Kim break; 236c30a15e5SDonggeun Kim case 3: 237c30a15e5SDonggeun Kim ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 238c30a15e5SDonggeun Kim ret = (ret & 0xfff0) >> 4; 239c30a15e5SDonggeun Kim break; 240c30a15e5SDonggeun Kim default: 241c30a15e5SDonggeun Kim break; 242c30a15e5SDonggeun Kim } 243c30a15e5SDonggeun Kim break; 244c30a15e5SDonggeun Kim } 245c30a15e5SDonggeun Kim debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n", 246c30a15e5SDonggeun Kim mydata->fatsize, ret, entry, offset); 247c30a15e5SDonggeun Kim 248c30a15e5SDonggeun Kim return ret; 249c30a15e5SDonggeun Kim } 250c30a15e5SDonggeun Kim 251c30a15e5SDonggeun Kim #ifdef CONFIG_SUPPORT_VFAT 252c30a15e5SDonggeun Kim /* 253c30a15e5SDonggeun Kim * Set the file name information from 'name' into 'slotptr', 254c30a15e5SDonggeun Kim */ 255c30a15e5SDonggeun Kim static int str2slot(dir_slot *slotptr, const char *name, int *idx) 256c30a15e5SDonggeun Kim { 257c30a15e5SDonggeun Kim int j, end_idx = 0; 258c30a15e5SDonggeun Kim 259c30a15e5SDonggeun Kim for (j = 0; j <= 8; j += 2) { 260c30a15e5SDonggeun Kim if (name[*idx] == 0x00) { 261c30a15e5SDonggeun Kim slotptr->name0_4[j] = 0; 262c30a15e5SDonggeun Kim slotptr->name0_4[j + 1] = 0; 263c30a15e5SDonggeun Kim end_idx++; 264c30a15e5SDonggeun Kim goto name0_4; 265c30a15e5SDonggeun Kim } 266c30a15e5SDonggeun Kim slotptr->name0_4[j] = name[*idx]; 267c30a15e5SDonggeun Kim (*idx)++; 268c30a15e5SDonggeun Kim end_idx++; 269c30a15e5SDonggeun Kim } 270c30a15e5SDonggeun Kim for (j = 0; j <= 10; j += 2) { 271c30a15e5SDonggeun Kim if (name[*idx] == 0x00) { 272c30a15e5SDonggeun Kim slotptr->name5_10[j] = 0; 273c30a15e5SDonggeun Kim slotptr->name5_10[j + 1] = 0; 274c30a15e5SDonggeun Kim end_idx++; 275c30a15e5SDonggeun Kim goto name5_10; 276c30a15e5SDonggeun Kim } 277c30a15e5SDonggeun Kim slotptr->name5_10[j] = name[*idx]; 278c30a15e5SDonggeun Kim (*idx)++; 279c30a15e5SDonggeun Kim end_idx++; 280c30a15e5SDonggeun Kim } 281c30a15e5SDonggeun Kim for (j = 0; j <= 2; j += 2) { 282c30a15e5SDonggeun Kim if (name[*idx] == 0x00) { 283c30a15e5SDonggeun Kim slotptr->name11_12[j] = 0; 284c30a15e5SDonggeun Kim slotptr->name11_12[j + 1] = 0; 285c30a15e5SDonggeun Kim end_idx++; 286c30a15e5SDonggeun Kim goto name11_12; 287c30a15e5SDonggeun Kim } 288c30a15e5SDonggeun Kim slotptr->name11_12[j] = name[*idx]; 289c30a15e5SDonggeun Kim (*idx)++; 290c30a15e5SDonggeun Kim end_idx++; 291c30a15e5SDonggeun Kim } 292c30a15e5SDonggeun Kim 293c30a15e5SDonggeun Kim if (name[*idx] == 0x00) 294c30a15e5SDonggeun Kim return 1; 295c30a15e5SDonggeun Kim 296c30a15e5SDonggeun Kim return 0; 297c30a15e5SDonggeun Kim /* Not used characters are filled with 0xff 0xff */ 298c30a15e5SDonggeun Kim name0_4: 299c30a15e5SDonggeun Kim for (; end_idx < 5; end_idx++) { 300c30a15e5SDonggeun Kim slotptr->name0_4[end_idx * 2] = 0xff; 301c30a15e5SDonggeun Kim slotptr->name0_4[end_idx * 2 + 1] = 0xff; 302c30a15e5SDonggeun Kim } 303c30a15e5SDonggeun Kim end_idx = 5; 304c30a15e5SDonggeun Kim name5_10: 305c30a15e5SDonggeun Kim end_idx -= 5; 306c30a15e5SDonggeun Kim for (; end_idx < 6; end_idx++) { 307c30a15e5SDonggeun Kim slotptr->name5_10[end_idx * 2] = 0xff; 308c30a15e5SDonggeun Kim slotptr->name5_10[end_idx * 2 + 1] = 0xff; 309c30a15e5SDonggeun Kim } 310c30a15e5SDonggeun Kim end_idx = 11; 311c30a15e5SDonggeun Kim name11_12: 312c30a15e5SDonggeun Kim end_idx -= 11; 313c30a15e5SDonggeun Kim for (; end_idx < 2; end_idx++) { 314c30a15e5SDonggeun Kim slotptr->name11_12[end_idx * 2] = 0xff; 315c30a15e5SDonggeun Kim slotptr->name11_12[end_idx * 2 + 1] = 0xff; 316c30a15e5SDonggeun Kim } 317c30a15e5SDonggeun Kim 318c30a15e5SDonggeun Kim return 1; 319c30a15e5SDonggeun Kim } 320c30a15e5SDonggeun Kim 321c30a15e5SDonggeun Kim static int is_next_clust(fsdata *mydata, dir_entry *dentptr); 322c30a15e5SDonggeun Kim static void flush_dir_table(fsdata *mydata, dir_entry **dentptr); 323c30a15e5SDonggeun Kim 324c30a15e5SDonggeun Kim /* 325c30a15e5SDonggeun Kim * Fill dir_slot entries with appropriate name, id, and attr 326c30a15e5SDonggeun Kim * The real directory entry is returned by 'dentptr' 327c30a15e5SDonggeun Kim */ 328c30a15e5SDonggeun Kim static void 329c30a15e5SDonggeun Kim fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name) 330c30a15e5SDonggeun Kim { 331*1170e634SBenoît Thébaudeau dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block; 3328506eb8dSAnatolij Gustschin __u8 counter = 0, checksum; 333c30a15e5SDonggeun Kim int idx = 0, ret; 334c30a15e5SDonggeun Kim char s_name[16]; 335c30a15e5SDonggeun Kim 336c30a15e5SDonggeun Kim /* Get short file name and checksum value */ 337c30a15e5SDonggeun Kim strncpy(s_name, (*dentptr)->name, 16); 338c30a15e5SDonggeun Kim checksum = mkcksum(s_name); 339c30a15e5SDonggeun Kim 340c30a15e5SDonggeun Kim do { 341c30a15e5SDonggeun Kim memset(slotptr, 0x00, sizeof(dir_slot)); 342c30a15e5SDonggeun Kim ret = str2slot(slotptr, l_name, &idx); 343c30a15e5SDonggeun Kim slotptr->id = ++counter; 344c30a15e5SDonggeun Kim slotptr->attr = ATTR_VFAT; 345c30a15e5SDonggeun Kim slotptr->alias_checksum = checksum; 346c30a15e5SDonggeun Kim slotptr++; 347c30a15e5SDonggeun Kim } while (ret == 0); 348c30a15e5SDonggeun Kim 349c30a15e5SDonggeun Kim slotptr--; 350c30a15e5SDonggeun Kim slotptr->id |= LAST_LONG_ENTRY_MASK; 351c30a15e5SDonggeun Kim 352c30a15e5SDonggeun Kim while (counter >= 1) { 353c30a15e5SDonggeun Kim if (is_next_clust(mydata, *dentptr)) { 354c30a15e5SDonggeun Kim /* A new cluster is allocated for directory table */ 355c30a15e5SDonggeun Kim flush_dir_table(mydata, dentptr); 356c30a15e5SDonggeun Kim } 357c30a15e5SDonggeun Kim memcpy(*dentptr, slotptr, sizeof(dir_slot)); 358c30a15e5SDonggeun Kim (*dentptr)++; 359c30a15e5SDonggeun Kim slotptr--; 360c30a15e5SDonggeun Kim counter--; 361c30a15e5SDonggeun Kim } 362c30a15e5SDonggeun Kim 363c30a15e5SDonggeun Kim if (is_next_clust(mydata, *dentptr)) { 364c30a15e5SDonggeun Kim /* A new cluster is allocated for directory table */ 365c30a15e5SDonggeun Kim flush_dir_table(mydata, dentptr); 366c30a15e5SDonggeun Kim } 367c30a15e5SDonggeun Kim } 368c30a15e5SDonggeun Kim 369c30a15e5SDonggeun Kim static __u32 dir_curclust; 370c30a15e5SDonggeun Kim 371c30a15e5SDonggeun Kim /* 372c30a15e5SDonggeun Kim * Extract the full long filename starting at 'retdent' (which is really 373c30a15e5SDonggeun Kim * a slot) into 'l_name'. If successful also copy the real directory entry 374c30a15e5SDonggeun Kim * into 'retdent' 375c30a15e5SDonggeun Kim * If additional adjacent cluster for directory entries is read into memory, 376*1170e634SBenoît Thébaudeau * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and 377c30a15e5SDonggeun Kim * the location of the real directory entry is returned by 'retdent' 378c30a15e5SDonggeun Kim * Return 0 on success, -1 otherwise. 379c30a15e5SDonggeun Kim */ 380c30a15e5SDonggeun Kim static int 381c30a15e5SDonggeun Kim get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster, 382c30a15e5SDonggeun Kim dir_entry **retdent, char *l_name) 383c30a15e5SDonggeun Kim { 384c30a15e5SDonggeun Kim dir_entry *realdent; 385c30a15e5SDonggeun Kim dir_slot *slotptr = (dir_slot *)(*retdent); 386c30a15e5SDonggeun Kim dir_slot *slotptr2 = NULL; 387c30a15e5SDonggeun Kim __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? 388c30a15e5SDonggeun Kim PREFETCH_BLOCKS : 389c30a15e5SDonggeun Kim mydata->clust_size); 390c30a15e5SDonggeun Kim __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; 391c30a15e5SDonggeun Kim int idx = 0, cur_position = 0; 392c30a15e5SDonggeun Kim 393c30a15e5SDonggeun Kim if (counter > VFAT_MAXSEQ) { 394c30a15e5SDonggeun Kim debug("Error: VFAT name is too long\n"); 395c30a15e5SDonggeun Kim return -1; 396c30a15e5SDonggeun Kim } 397c30a15e5SDonggeun Kim 398c30a15e5SDonggeun Kim while ((__u8 *)slotptr < buflimit) { 399c30a15e5SDonggeun Kim if (counter == 0) 400c30a15e5SDonggeun Kim break; 401c30a15e5SDonggeun Kim if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) 402c30a15e5SDonggeun Kim return -1; 403c30a15e5SDonggeun Kim slotptr++; 404c30a15e5SDonggeun Kim counter--; 405c30a15e5SDonggeun Kim } 406c30a15e5SDonggeun Kim 407c30a15e5SDonggeun Kim if ((__u8 *)slotptr >= buflimit) { 408c30a15e5SDonggeun Kim if (curclust == 0) 409c30a15e5SDonggeun Kim return -1; 410c30a15e5SDonggeun Kim curclust = get_fatent_value(mydata, dir_curclust); 411c30a15e5SDonggeun Kim if (CHECK_CLUST(curclust, mydata->fatsize)) { 412c30a15e5SDonggeun Kim debug("curclust: 0x%x\n", curclust); 413c30a15e5SDonggeun Kim printf("Invalid FAT entry\n"); 414c30a15e5SDonggeun Kim return -1; 415c30a15e5SDonggeun Kim } 416c30a15e5SDonggeun Kim 417c30a15e5SDonggeun Kim dir_curclust = curclust; 418c30a15e5SDonggeun Kim 419*1170e634SBenoît Thébaudeau if (get_cluster(mydata, curclust, get_contents_vfatname_block, 420c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size) != 0) { 421c30a15e5SDonggeun Kim debug("Error: reading directory block\n"); 422c30a15e5SDonggeun Kim return -1; 423c30a15e5SDonggeun Kim } 424c30a15e5SDonggeun Kim 425*1170e634SBenoît Thébaudeau slotptr2 = (dir_slot *)get_contents_vfatname_block; 426c30a15e5SDonggeun Kim while (counter > 0) { 427c30a15e5SDonggeun Kim if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) 428c30a15e5SDonggeun Kim & 0xff) != counter) 429c30a15e5SDonggeun Kim return -1; 430c30a15e5SDonggeun Kim slotptr2++; 431c30a15e5SDonggeun Kim counter--; 432c30a15e5SDonggeun Kim } 433c30a15e5SDonggeun Kim 434c30a15e5SDonggeun Kim /* Save the real directory entry */ 435c30a15e5SDonggeun Kim realdent = (dir_entry *)slotptr2; 436*1170e634SBenoît Thébaudeau while ((__u8 *)slotptr2 > get_contents_vfatname_block) { 437c30a15e5SDonggeun Kim slotptr2--; 438c30a15e5SDonggeun Kim slot2str(slotptr2, l_name, &idx); 439c30a15e5SDonggeun Kim } 440c30a15e5SDonggeun Kim } else { 441c30a15e5SDonggeun Kim /* Save the real directory entry */ 442c30a15e5SDonggeun Kim realdent = (dir_entry *)slotptr; 443c30a15e5SDonggeun Kim } 444c30a15e5SDonggeun Kim 445c30a15e5SDonggeun Kim do { 446c30a15e5SDonggeun Kim slotptr--; 447c30a15e5SDonggeun Kim if (slot2str(slotptr, l_name, &idx)) 448c30a15e5SDonggeun Kim break; 449c30a15e5SDonggeun Kim } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); 450c30a15e5SDonggeun Kim 451c30a15e5SDonggeun Kim l_name[idx] = '\0'; 452c30a15e5SDonggeun Kim if (*l_name == DELETED_FLAG) 453c30a15e5SDonggeun Kim *l_name = '\0'; 454c30a15e5SDonggeun Kim else if (*l_name == aRING) 455c30a15e5SDonggeun Kim *l_name = DELETED_FLAG; 456c30a15e5SDonggeun Kim downcase(l_name); 457c30a15e5SDonggeun Kim 458c30a15e5SDonggeun Kim /* Return the real directory entry */ 459c30a15e5SDonggeun Kim *retdent = realdent; 460c30a15e5SDonggeun Kim 461c30a15e5SDonggeun Kim if (slotptr2) { 462*1170e634SBenoît Thébaudeau memcpy(get_dentfromdir_block, get_contents_vfatname_block, 463c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size); 464*1170e634SBenoît Thébaudeau cur_position = (__u8 *)realdent - get_contents_vfatname_block; 465c30a15e5SDonggeun Kim *retdent = (dir_entry *) &get_dentfromdir_block[cur_position]; 466c30a15e5SDonggeun Kim } 467c30a15e5SDonggeun Kim 468c30a15e5SDonggeun Kim return 0; 469c30a15e5SDonggeun Kim } 470c30a15e5SDonggeun Kim 471c30a15e5SDonggeun Kim #endif 472c30a15e5SDonggeun Kim 473c30a15e5SDonggeun Kim /* 474c30a15e5SDonggeun Kim * Set the entry at index 'entry' in a FAT (16/32) table. 475c30a15e5SDonggeun Kim */ 476c30a15e5SDonggeun Kim static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value) 477c30a15e5SDonggeun Kim { 478c30a15e5SDonggeun Kim __u32 bufnum, offset; 479c30a15e5SDonggeun Kim 480c30a15e5SDonggeun Kim switch (mydata->fatsize) { 481c30a15e5SDonggeun Kim case 32: 482c30a15e5SDonggeun Kim bufnum = entry / FAT32BUFSIZE; 483c30a15e5SDonggeun Kim offset = entry - bufnum * FAT32BUFSIZE; 484c30a15e5SDonggeun Kim break; 485c30a15e5SDonggeun Kim case 16: 486c30a15e5SDonggeun Kim bufnum = entry / FAT16BUFSIZE; 487c30a15e5SDonggeun Kim offset = entry - bufnum * FAT16BUFSIZE; 488c30a15e5SDonggeun Kim break; 489c30a15e5SDonggeun Kim default: 490c30a15e5SDonggeun Kim /* Unsupported FAT size */ 491c30a15e5SDonggeun Kim return -1; 492c30a15e5SDonggeun Kim } 493c30a15e5SDonggeun Kim 494c30a15e5SDonggeun Kim /* Read a new block of FAT entries into the cache. */ 495c30a15e5SDonggeun Kim if (bufnum != mydata->fatbufnum) { 496c30a15e5SDonggeun Kim int getsize = FATBUFBLOCKS; 497c30a15e5SDonggeun Kim __u8 *bufptr = mydata->fatbuf; 498c30a15e5SDonggeun Kim __u32 fatlength = mydata->fatlength; 499c30a15e5SDonggeun Kim __u32 startblock = bufnum * FATBUFBLOCKS; 500c30a15e5SDonggeun Kim 501c30a15e5SDonggeun Kim fatlength *= mydata->sect_size; 502c30a15e5SDonggeun Kim startblock += mydata->fat_sect; 503c30a15e5SDonggeun Kim 504c30a15e5SDonggeun Kim if (getsize > fatlength) 505c30a15e5SDonggeun Kim getsize = fatlength; 506c30a15e5SDonggeun Kim 507c30a15e5SDonggeun Kim if (mydata->fatbufnum != -1) { 508c30a15e5SDonggeun Kim if (flush_fat_buffer(mydata) < 0) 509c30a15e5SDonggeun Kim return -1; 510c30a15e5SDonggeun Kim } 511c30a15e5SDonggeun Kim 512c30a15e5SDonggeun Kim if (disk_read(startblock, getsize, bufptr) < 0) { 513c30a15e5SDonggeun Kim debug("Error reading FAT blocks\n"); 514c30a15e5SDonggeun Kim return -1; 515c30a15e5SDonggeun Kim } 516c30a15e5SDonggeun Kim mydata->fatbufnum = bufnum; 517c30a15e5SDonggeun Kim } 518c30a15e5SDonggeun Kim 519c30a15e5SDonggeun Kim /* Set the actual entry */ 520c30a15e5SDonggeun Kim switch (mydata->fatsize) { 521c30a15e5SDonggeun Kim case 32: 522c30a15e5SDonggeun Kim ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value); 523c30a15e5SDonggeun Kim break; 524c30a15e5SDonggeun Kim case 16: 525c30a15e5SDonggeun Kim ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value); 526c30a15e5SDonggeun Kim break; 527c30a15e5SDonggeun Kim default: 528c30a15e5SDonggeun Kim return -1; 529c30a15e5SDonggeun Kim } 530c30a15e5SDonggeun Kim 531c30a15e5SDonggeun Kim return 0; 532c30a15e5SDonggeun Kim } 533c30a15e5SDonggeun Kim 534c30a15e5SDonggeun Kim /* 535c30a15e5SDonggeun Kim * Determine the entry value at index 'entry' in a FAT (16/32) table 536c30a15e5SDonggeun Kim */ 537c30a15e5SDonggeun Kim static __u32 determine_fatent(fsdata *mydata, __u32 entry) 538c30a15e5SDonggeun Kim { 539c30a15e5SDonggeun Kim __u32 next_fat, next_entry = entry + 1; 540c30a15e5SDonggeun Kim 541c30a15e5SDonggeun Kim while (1) { 542c30a15e5SDonggeun Kim next_fat = get_fatent_value(mydata, next_entry); 543c30a15e5SDonggeun Kim if (next_fat == 0) { 544c30a15e5SDonggeun Kim set_fatent_value(mydata, entry, next_entry); 545c30a15e5SDonggeun Kim break; 546c30a15e5SDonggeun Kim } 547c30a15e5SDonggeun Kim next_entry++; 548c30a15e5SDonggeun Kim } 549c30a15e5SDonggeun Kim debug("FAT%d: entry: %08x, entry_value: %04x\n", 550c30a15e5SDonggeun Kim mydata->fatsize, entry, next_entry); 551c30a15e5SDonggeun Kim 552c30a15e5SDonggeun Kim return next_entry; 553c30a15e5SDonggeun Kim } 554c30a15e5SDonggeun Kim 555c30a15e5SDonggeun Kim /* 556c30a15e5SDonggeun Kim * Write at most 'size' bytes from 'buffer' into the specified cluster. 557c30a15e5SDonggeun Kim * Return 0 on success, -1 otherwise. 558c30a15e5SDonggeun Kim */ 559c30a15e5SDonggeun Kim static int 560c30a15e5SDonggeun Kim set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, 561c30a15e5SDonggeun Kim unsigned long size) 562c30a15e5SDonggeun Kim { 563c30a15e5SDonggeun Kim int idx = 0; 564c30a15e5SDonggeun Kim __u32 startsect; 565c30a15e5SDonggeun Kim 566c30a15e5SDonggeun Kim if (clustnum > 0) 567c30a15e5SDonggeun Kim startsect = mydata->data_begin + 568c30a15e5SDonggeun Kim clustnum * mydata->clust_size; 569c30a15e5SDonggeun Kim else 570c30a15e5SDonggeun Kim startsect = mydata->rootdir_sect; 571c30a15e5SDonggeun Kim 572c30a15e5SDonggeun Kim debug("clustnum: %d, startsect: %d\n", clustnum, startsect); 573c30a15e5SDonggeun Kim 574c30a15e5SDonggeun Kim if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) { 575c30a15e5SDonggeun Kim debug("Error writing data\n"); 576c30a15e5SDonggeun Kim return -1; 577c30a15e5SDonggeun Kim } 578c30a15e5SDonggeun Kim 579c30a15e5SDonggeun Kim if (size % mydata->sect_size) { 580c30a15e5SDonggeun Kim __u8 tmpbuf[mydata->sect_size]; 581c30a15e5SDonggeun Kim 582c30a15e5SDonggeun Kim idx = size / mydata->sect_size; 583c30a15e5SDonggeun Kim buffer += idx * mydata->sect_size; 584c30a15e5SDonggeun Kim memcpy(tmpbuf, buffer, size % mydata->sect_size); 585c30a15e5SDonggeun Kim 586c30a15e5SDonggeun Kim if (disk_write(startsect + idx, 1, tmpbuf) < 0) { 587c30a15e5SDonggeun Kim debug("Error writing data\n"); 588c30a15e5SDonggeun Kim return -1; 589c30a15e5SDonggeun Kim } 590c30a15e5SDonggeun Kim 591c30a15e5SDonggeun Kim return 0; 592c30a15e5SDonggeun Kim } 593c30a15e5SDonggeun Kim 594c30a15e5SDonggeun Kim return 0; 595c30a15e5SDonggeun Kim } 596c30a15e5SDonggeun Kim 597c30a15e5SDonggeun Kim /* 598c30a15e5SDonggeun Kim * Find the first empty cluster 599c30a15e5SDonggeun Kim */ 600c30a15e5SDonggeun Kim static int find_empty_cluster(fsdata *mydata) 601c30a15e5SDonggeun Kim { 602c30a15e5SDonggeun Kim __u32 fat_val, entry = 3; 603c30a15e5SDonggeun Kim 604c30a15e5SDonggeun Kim while (1) { 605c30a15e5SDonggeun Kim fat_val = get_fatent_value(mydata, entry); 606c30a15e5SDonggeun Kim if (fat_val == 0) 607c30a15e5SDonggeun Kim break; 608c30a15e5SDonggeun Kim entry++; 609c30a15e5SDonggeun Kim } 610c30a15e5SDonggeun Kim 611c30a15e5SDonggeun Kim return entry; 612c30a15e5SDonggeun Kim } 613c30a15e5SDonggeun Kim 614c30a15e5SDonggeun Kim /* 615c30a15e5SDonggeun Kim * Write directory entries in 'get_dentfromdir_block' to block device 616c30a15e5SDonggeun Kim */ 617c30a15e5SDonggeun Kim static void flush_dir_table(fsdata *mydata, dir_entry **dentptr) 618c30a15e5SDonggeun Kim { 619c30a15e5SDonggeun Kim int dir_newclust = 0; 620c30a15e5SDonggeun Kim 621c30a15e5SDonggeun Kim if (set_cluster(mydata, dir_curclust, 622c30a15e5SDonggeun Kim get_dentfromdir_block, 623c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size) != 0) { 624c30a15e5SDonggeun Kim printf("error: wrinting directory entry\n"); 625c30a15e5SDonggeun Kim return; 626c30a15e5SDonggeun Kim } 627c30a15e5SDonggeun Kim dir_newclust = find_empty_cluster(mydata); 628c30a15e5SDonggeun Kim set_fatent_value(mydata, dir_curclust, dir_newclust); 629c30a15e5SDonggeun Kim if (mydata->fatsize == 32) 630c30a15e5SDonggeun Kim set_fatent_value(mydata, dir_newclust, 0xffffff8); 631c30a15e5SDonggeun Kim else if (mydata->fatsize == 16) 632c30a15e5SDonggeun Kim set_fatent_value(mydata, dir_newclust, 0xfff8); 633c30a15e5SDonggeun Kim 634c30a15e5SDonggeun Kim dir_curclust = dir_newclust; 635c30a15e5SDonggeun Kim 636c30a15e5SDonggeun Kim if (flush_fat_buffer(mydata) < 0) 637c30a15e5SDonggeun Kim return; 638c30a15e5SDonggeun Kim 639c30a15e5SDonggeun Kim memset(get_dentfromdir_block, 0x00, 640c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size); 641c30a15e5SDonggeun Kim 642c30a15e5SDonggeun Kim *dentptr = (dir_entry *) get_dentfromdir_block; 643c30a15e5SDonggeun Kim } 644c30a15e5SDonggeun Kim 645c30a15e5SDonggeun Kim /* 646c30a15e5SDonggeun Kim * Set empty cluster from 'entry' to the end of a file 647c30a15e5SDonggeun Kim */ 648c30a15e5SDonggeun Kim static int clear_fatent(fsdata *mydata, __u32 entry) 649c30a15e5SDonggeun Kim { 650c30a15e5SDonggeun Kim __u32 fat_val; 651c30a15e5SDonggeun Kim 652c30a15e5SDonggeun Kim while (1) { 653c30a15e5SDonggeun Kim fat_val = get_fatent_value(mydata, entry); 654c30a15e5SDonggeun Kim if (fat_val != 0) 655c30a15e5SDonggeun Kim set_fatent_value(mydata, entry, 0); 656c30a15e5SDonggeun Kim else 657c30a15e5SDonggeun Kim break; 658c30a15e5SDonggeun Kim 659c30a15e5SDonggeun Kim if (fat_val == 0xfffffff || fat_val == 0xffff) 660c30a15e5SDonggeun Kim break; 661c30a15e5SDonggeun Kim 662c30a15e5SDonggeun Kim entry = fat_val; 663c30a15e5SDonggeun Kim } 664c30a15e5SDonggeun Kim 665c30a15e5SDonggeun Kim /* Flush fat buffer */ 666c30a15e5SDonggeun Kim if (flush_fat_buffer(mydata) < 0) 667c30a15e5SDonggeun Kim return -1; 668c30a15e5SDonggeun Kim 669c30a15e5SDonggeun Kim return 0; 670c30a15e5SDonggeun Kim } 671c30a15e5SDonggeun Kim 672c30a15e5SDonggeun Kim /* 673c30a15e5SDonggeun Kim * Write at most 'maxsize' bytes from 'buffer' into 674c30a15e5SDonggeun Kim * the file associated with 'dentptr' 675c30a15e5SDonggeun Kim * Return the number of bytes read or -1 on fatal errors. 676c30a15e5SDonggeun Kim */ 677c30a15e5SDonggeun Kim static int 678c30a15e5SDonggeun Kim set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer, 679c30a15e5SDonggeun Kim unsigned long maxsize) 680c30a15e5SDonggeun Kim { 681c30a15e5SDonggeun Kim unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0; 682c30a15e5SDonggeun Kim unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; 683c30a15e5SDonggeun Kim __u32 curclust = START(dentptr); 684c30a15e5SDonggeun Kim __u32 endclust = 0, newclust = 0; 685c30a15e5SDonggeun Kim unsigned long actsize; 686c30a15e5SDonggeun Kim 687c30a15e5SDonggeun Kim debug("Filesize: %ld bytes\n", filesize); 688c30a15e5SDonggeun Kim 689c30a15e5SDonggeun Kim if (maxsize > 0 && filesize > maxsize) 690c30a15e5SDonggeun Kim filesize = maxsize; 691c30a15e5SDonggeun Kim 692c30a15e5SDonggeun Kim debug("%ld bytes\n", filesize); 693c30a15e5SDonggeun Kim 694c30a15e5SDonggeun Kim actsize = bytesperclust; 695c30a15e5SDonggeun Kim endclust = curclust; 696c30a15e5SDonggeun Kim do { 697c30a15e5SDonggeun Kim /* search for consecutive clusters */ 698c30a15e5SDonggeun Kim while (actsize < filesize) { 699c30a15e5SDonggeun Kim newclust = determine_fatent(mydata, endclust); 700c30a15e5SDonggeun Kim 701c30a15e5SDonggeun Kim if ((newclust - 1) != endclust) 702c30a15e5SDonggeun Kim goto getit; 703c30a15e5SDonggeun Kim 704c30a15e5SDonggeun Kim if (CHECK_CLUST(newclust, mydata->fatsize)) { 705c30a15e5SDonggeun Kim debug("curclust: 0x%x\n", newclust); 706c30a15e5SDonggeun Kim debug("Invalid FAT entry\n"); 707c30a15e5SDonggeun Kim return gotsize; 708c30a15e5SDonggeun Kim } 709c30a15e5SDonggeun Kim endclust = newclust; 710c30a15e5SDonggeun Kim actsize += bytesperclust; 711c30a15e5SDonggeun Kim } 712c30a15e5SDonggeun Kim /* actsize >= file size */ 713c30a15e5SDonggeun Kim actsize -= bytesperclust; 714c30a15e5SDonggeun Kim /* set remaining clusters */ 715c30a15e5SDonggeun Kim if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 716c30a15e5SDonggeun Kim debug("error: writing cluster\n"); 717c30a15e5SDonggeun Kim return -1; 718c30a15e5SDonggeun Kim } 719c30a15e5SDonggeun Kim 720c30a15e5SDonggeun Kim /* set remaining bytes */ 721c30a15e5SDonggeun Kim gotsize += (int)actsize; 722c30a15e5SDonggeun Kim filesize -= actsize; 723c30a15e5SDonggeun Kim buffer += actsize; 724c30a15e5SDonggeun Kim actsize = filesize; 725c30a15e5SDonggeun Kim 726c30a15e5SDonggeun Kim if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) { 727c30a15e5SDonggeun Kim debug("error: writing cluster\n"); 728c30a15e5SDonggeun Kim return -1; 729c30a15e5SDonggeun Kim } 730c30a15e5SDonggeun Kim gotsize += actsize; 731c30a15e5SDonggeun Kim 732c30a15e5SDonggeun Kim /* Mark end of file in FAT */ 733c30a15e5SDonggeun Kim if (mydata->fatsize == 16) 734c30a15e5SDonggeun Kim newclust = 0xffff; 735c30a15e5SDonggeun Kim else if (mydata->fatsize == 32) 736c30a15e5SDonggeun Kim newclust = 0xfffffff; 737c30a15e5SDonggeun Kim set_fatent_value(mydata, endclust, newclust); 738c30a15e5SDonggeun Kim 739c30a15e5SDonggeun Kim return gotsize; 740c30a15e5SDonggeun Kim getit: 741c30a15e5SDonggeun Kim if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 742c30a15e5SDonggeun Kim debug("error: writing cluster\n"); 743c30a15e5SDonggeun Kim return -1; 744c30a15e5SDonggeun Kim } 745c30a15e5SDonggeun Kim gotsize += (int)actsize; 746c30a15e5SDonggeun Kim filesize -= actsize; 747c30a15e5SDonggeun Kim buffer += actsize; 748c30a15e5SDonggeun Kim 749c30a15e5SDonggeun Kim if (CHECK_CLUST(curclust, mydata->fatsize)) { 750c30a15e5SDonggeun Kim debug("curclust: 0x%x\n", curclust); 751c30a15e5SDonggeun Kim debug("Invalid FAT entry\n"); 752c30a15e5SDonggeun Kim return gotsize; 753c30a15e5SDonggeun Kim } 754c30a15e5SDonggeun Kim actsize = bytesperclust; 755c30a15e5SDonggeun Kim curclust = endclust = newclust; 756c30a15e5SDonggeun Kim } while (1); 757c30a15e5SDonggeun Kim } 758c30a15e5SDonggeun Kim 759c30a15e5SDonggeun Kim /* 760c30a15e5SDonggeun Kim * Fill dir_entry 761c30a15e5SDonggeun Kim */ 762c30a15e5SDonggeun Kim static void fill_dentry(fsdata *mydata, dir_entry *dentptr, 763c30a15e5SDonggeun Kim const char *filename, __u32 start_cluster, __u32 size, __u8 attr) 764c30a15e5SDonggeun Kim { 765c30a15e5SDonggeun Kim if (mydata->fatsize == 32) 766c30a15e5SDonggeun Kim dentptr->starthi = 767c30a15e5SDonggeun Kim cpu_to_le16((start_cluster & 0xffff0000) >> 16); 768c30a15e5SDonggeun Kim dentptr->start = cpu_to_le16(start_cluster & 0xffff); 769c30a15e5SDonggeun Kim dentptr->size = cpu_to_le32(size); 770c30a15e5SDonggeun Kim 771c30a15e5SDonggeun Kim dentptr->attr = attr; 772c30a15e5SDonggeun Kim 773c30a15e5SDonggeun Kim set_name(dentptr, filename); 774c30a15e5SDonggeun Kim } 775c30a15e5SDonggeun Kim 776c30a15e5SDonggeun Kim /* 777c30a15e5SDonggeun Kim * Check whether adding a file makes the file system to 778c30a15e5SDonggeun Kim * exceed the size of the block device 779c30a15e5SDonggeun Kim * Return -1 when overflow occurs, otherwise return 0 780c30a15e5SDonggeun Kim */ 781c30a15e5SDonggeun Kim static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size) 782c30a15e5SDonggeun Kim { 783c30a15e5SDonggeun Kim __u32 startsect, sect_num; 784c30a15e5SDonggeun Kim 785c30a15e5SDonggeun Kim if (clustnum > 0) { 786c30a15e5SDonggeun Kim startsect = mydata->data_begin + 787c30a15e5SDonggeun Kim clustnum * mydata->clust_size; 788c30a15e5SDonggeun Kim } else { 789c30a15e5SDonggeun Kim startsect = mydata->rootdir_sect; 790c30a15e5SDonggeun Kim } 791c30a15e5SDonggeun Kim 792c30a15e5SDonggeun Kim sect_num = size / mydata->sect_size; 793c30a15e5SDonggeun Kim if (size % mydata->sect_size) 794c30a15e5SDonggeun Kim sect_num++; 795c30a15e5SDonggeun Kim 796079df722SDonggeun Kim if (startsect + sect_num > cur_part_info.start + total_sector) 797c30a15e5SDonggeun Kim return -1; 798c30a15e5SDonggeun Kim 799c30a15e5SDonggeun Kim return 0; 800c30a15e5SDonggeun Kim } 801c30a15e5SDonggeun Kim 802c30a15e5SDonggeun Kim /* 803c30a15e5SDonggeun Kim * Check if adding several entries exceed one cluster boundary 804c30a15e5SDonggeun Kim */ 805c30a15e5SDonggeun Kim static int is_next_clust(fsdata *mydata, dir_entry *dentptr) 806c30a15e5SDonggeun Kim { 807c30a15e5SDonggeun Kim int cur_position; 808c30a15e5SDonggeun Kim 809c30a15e5SDonggeun Kim cur_position = (__u8 *)dentptr - get_dentfromdir_block; 810c30a15e5SDonggeun Kim 811c30a15e5SDonggeun Kim if (cur_position >= mydata->clust_size * mydata->sect_size) 812c30a15e5SDonggeun Kim return 1; 813c30a15e5SDonggeun Kim else 814c30a15e5SDonggeun Kim return 0; 815c30a15e5SDonggeun Kim } 816c30a15e5SDonggeun Kim 817c30a15e5SDonggeun Kim static dir_entry *empty_dentptr; 818c30a15e5SDonggeun Kim /* 819c30a15e5SDonggeun Kim * Find a directory entry based on filename or start cluster number 820c30a15e5SDonggeun Kim * If the directory entry is not found, 821c30a15e5SDonggeun Kim * the new position for writing a directory entry will be returned 822c30a15e5SDonggeun Kim */ 823c30a15e5SDonggeun Kim static dir_entry *find_directory_entry(fsdata *mydata, int startsect, 824c30a15e5SDonggeun Kim char *filename, dir_entry *retdent, __u32 start) 825c30a15e5SDonggeun Kim { 826c30a15e5SDonggeun Kim __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size; 827c30a15e5SDonggeun Kim 828c30a15e5SDonggeun Kim debug("get_dentfromdir: %s\n", filename); 829c30a15e5SDonggeun Kim 830c30a15e5SDonggeun Kim while (1) { 831c30a15e5SDonggeun Kim dir_entry *dentptr; 832c30a15e5SDonggeun Kim 833c30a15e5SDonggeun Kim int i; 834c30a15e5SDonggeun Kim 835c30a15e5SDonggeun Kim if (get_cluster(mydata, curclust, get_dentfromdir_block, 836c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size) != 0) { 837c30a15e5SDonggeun Kim printf("Error: reading directory block\n"); 838c30a15e5SDonggeun Kim return NULL; 839c30a15e5SDonggeun Kim } 840c30a15e5SDonggeun Kim 841c30a15e5SDonggeun Kim dentptr = (dir_entry *)get_dentfromdir_block; 842c30a15e5SDonggeun Kim 843c30a15e5SDonggeun Kim dir_curclust = curclust; 844c30a15e5SDonggeun Kim 845c30a15e5SDonggeun Kim for (i = 0; i < DIRENTSPERCLUST; i++) { 846c30a15e5SDonggeun Kim char s_name[14], l_name[VFAT_MAXLEN_BYTES]; 847c30a15e5SDonggeun Kim 848c30a15e5SDonggeun Kim l_name[0] = '\0'; 849c30a15e5SDonggeun Kim if (dentptr->name[0] == DELETED_FLAG) { 850c30a15e5SDonggeun Kim dentptr++; 851c30a15e5SDonggeun Kim if (is_next_clust(mydata, dentptr)) 852c30a15e5SDonggeun Kim break; 853c30a15e5SDonggeun Kim continue; 854c30a15e5SDonggeun Kim } 855c30a15e5SDonggeun Kim if ((dentptr->attr & ATTR_VOLUME)) { 856c30a15e5SDonggeun Kim #ifdef CONFIG_SUPPORT_VFAT 857c30a15e5SDonggeun Kim if ((dentptr->attr & ATTR_VFAT) && 858c30a15e5SDonggeun Kim (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { 859c30a15e5SDonggeun Kim get_long_file_name(mydata, curclust, 860c30a15e5SDonggeun Kim get_dentfromdir_block, 861c30a15e5SDonggeun Kim &dentptr, l_name); 862c30a15e5SDonggeun Kim debug("vfatname: |%s|\n", l_name); 863c30a15e5SDonggeun Kim } else 864c30a15e5SDonggeun Kim #endif 865c30a15e5SDonggeun Kim { 866c30a15e5SDonggeun Kim /* Volume label or VFAT entry */ 867c30a15e5SDonggeun Kim dentptr++; 868c30a15e5SDonggeun Kim if (is_next_clust(mydata, dentptr)) 869c30a15e5SDonggeun Kim break; 870c30a15e5SDonggeun Kim continue; 871c30a15e5SDonggeun Kim } 872c30a15e5SDonggeun Kim } 873c30a15e5SDonggeun Kim if (dentptr->name[0] == 0) { 874c30a15e5SDonggeun Kim debug("Dentname == NULL - %d\n", i); 875c30a15e5SDonggeun Kim empty_dentptr = dentptr; 876c30a15e5SDonggeun Kim return NULL; 877c30a15e5SDonggeun Kim } 878c30a15e5SDonggeun Kim 879c30a15e5SDonggeun Kim get_name(dentptr, s_name); 880c30a15e5SDonggeun Kim 881c30a15e5SDonggeun Kim if (strcmp(filename, s_name) 882c30a15e5SDonggeun Kim && strcmp(filename, l_name)) { 883c30a15e5SDonggeun Kim debug("Mismatch: |%s|%s|\n", 884c30a15e5SDonggeun Kim s_name, l_name); 885c30a15e5SDonggeun Kim dentptr++; 886c30a15e5SDonggeun Kim if (is_next_clust(mydata, dentptr)) 887c30a15e5SDonggeun Kim break; 888c30a15e5SDonggeun Kim continue; 889c30a15e5SDonggeun Kim } 890c30a15e5SDonggeun Kim 891c30a15e5SDonggeun Kim memcpy(retdent, dentptr, sizeof(dir_entry)); 892c30a15e5SDonggeun Kim 893c30a15e5SDonggeun Kim debug("DentName: %s", s_name); 894c30a15e5SDonggeun Kim debug(", start: 0x%x", START(dentptr)); 895c30a15e5SDonggeun Kim debug(", size: 0x%x %s\n", 896c30a15e5SDonggeun Kim FAT2CPU32(dentptr->size), 897c30a15e5SDonggeun Kim (dentptr->attr & ATTR_DIR) ? 898c30a15e5SDonggeun Kim "(DIR)" : ""); 899c30a15e5SDonggeun Kim 900c30a15e5SDonggeun Kim return dentptr; 901c30a15e5SDonggeun Kim } 902c30a15e5SDonggeun Kim 903c30a15e5SDonggeun Kim curclust = get_fatent_value(mydata, dir_curclust); 904c30a15e5SDonggeun Kim if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) { 905c30a15e5SDonggeun Kim empty_dentptr = dentptr; 906c30a15e5SDonggeun Kim return NULL; 907c30a15e5SDonggeun Kim } 908c30a15e5SDonggeun Kim if (CHECK_CLUST(curclust, mydata->fatsize)) { 909c30a15e5SDonggeun Kim debug("curclust: 0x%x\n", curclust); 910c30a15e5SDonggeun Kim debug("Invalid FAT entry\n"); 911c30a15e5SDonggeun Kim return NULL; 912c30a15e5SDonggeun Kim } 913c30a15e5SDonggeun Kim } 914c30a15e5SDonggeun Kim 915c30a15e5SDonggeun Kim return NULL; 916c30a15e5SDonggeun Kim } 917c30a15e5SDonggeun Kim 918c30a15e5SDonggeun Kim static int do_fat_write(const char *filename, void *buffer, 919c30a15e5SDonggeun Kim unsigned long size) 920c30a15e5SDonggeun Kim { 921c30a15e5SDonggeun Kim dir_entry *dentptr, *retdent; 922c30a15e5SDonggeun Kim __u32 startsect; 923c30a15e5SDonggeun Kim __u32 start_cluster; 924c30a15e5SDonggeun Kim boot_sector bs; 925c30a15e5SDonggeun Kim volume_info volinfo; 926c30a15e5SDonggeun Kim fsdata datablock; 927c30a15e5SDonggeun Kim fsdata *mydata = &datablock; 928c30a15e5SDonggeun Kim int cursect; 929bf6b6af7SAnatolij Gustschin int ret = -1, name_len; 930c30a15e5SDonggeun Kim char l_filename[VFAT_MAXLEN_BYTES]; 9318506eb8dSAnatolij Gustschin int write_size = size; 932c30a15e5SDonggeun Kim 933c30a15e5SDonggeun Kim dir_curclust = 0; 934c30a15e5SDonggeun Kim 935c30a15e5SDonggeun Kim if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { 936c30a15e5SDonggeun Kim debug("error: reading boot sector\n"); 937c30a15e5SDonggeun Kim return -1; 938c30a15e5SDonggeun Kim } 939c30a15e5SDonggeun Kim 940c30a15e5SDonggeun Kim total_sector = bs.total_sect; 941c30a15e5SDonggeun Kim if (total_sector == 0) 942079df722SDonggeun Kim total_sector = cur_part_info.size; 943c30a15e5SDonggeun Kim 944c30a15e5SDonggeun Kim if (mydata->fatsize == 32) 945c30a15e5SDonggeun Kim mydata->fatlength = bs.fat32_length; 946c30a15e5SDonggeun Kim else 947c30a15e5SDonggeun Kim mydata->fatlength = bs.fat_length; 948c30a15e5SDonggeun Kim 949c30a15e5SDonggeun Kim mydata->fat_sect = bs.reserved; 950c30a15e5SDonggeun Kim 951c30a15e5SDonggeun Kim cursect = mydata->rootdir_sect 952c30a15e5SDonggeun Kim = mydata->fat_sect + mydata->fatlength * bs.fats; 953627182eaSDonggeun Kim num_of_fats = bs.fats; 954c30a15e5SDonggeun Kim 955c30a15e5SDonggeun Kim mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; 956c30a15e5SDonggeun Kim mydata->clust_size = bs.cluster_size; 957c30a15e5SDonggeun Kim 958c30a15e5SDonggeun Kim if (mydata->fatsize == 32) { 959c30a15e5SDonggeun Kim mydata->data_begin = mydata->rootdir_sect - 960c30a15e5SDonggeun Kim (mydata->clust_size * 2); 961c30a15e5SDonggeun Kim } else { 962c30a15e5SDonggeun Kim int rootdir_size; 963c30a15e5SDonggeun Kim 964c30a15e5SDonggeun Kim rootdir_size = ((bs.dir_entries[1] * (int)256 + 965c30a15e5SDonggeun Kim bs.dir_entries[0]) * 966c30a15e5SDonggeun Kim sizeof(dir_entry)) / 967c30a15e5SDonggeun Kim mydata->sect_size; 968c30a15e5SDonggeun Kim mydata->data_begin = mydata->rootdir_sect + 969c30a15e5SDonggeun Kim rootdir_size - 970c30a15e5SDonggeun Kim (mydata->clust_size * 2); 971c30a15e5SDonggeun Kim } 972c30a15e5SDonggeun Kim 973c30a15e5SDonggeun Kim mydata->fatbufnum = -1; 974c30a15e5SDonggeun Kim mydata->fatbuf = malloc(FATBUFSIZE); 975c30a15e5SDonggeun Kim if (mydata->fatbuf == NULL) { 976c30a15e5SDonggeun Kim debug("Error: allocating memory\n"); 977c30a15e5SDonggeun Kim return -1; 978c30a15e5SDonggeun Kim } 979c30a15e5SDonggeun Kim 980c30a15e5SDonggeun Kim if (disk_read(cursect, 981c30a15e5SDonggeun Kim (mydata->fatsize == 32) ? 982c30a15e5SDonggeun Kim (mydata->clust_size) : 983*1170e634SBenoît Thébaudeau PREFETCH_BLOCKS, do_fat_read_at_block) < 0) { 984c30a15e5SDonggeun Kim debug("Error: reading rootdir block\n"); 985c30a15e5SDonggeun Kim goto exit; 986c30a15e5SDonggeun Kim } 987*1170e634SBenoît Thébaudeau dentptr = (dir_entry *) do_fat_read_at_block; 988c30a15e5SDonggeun Kim 989c30a15e5SDonggeun Kim name_len = strlen(filename); 9908506eb8dSAnatolij Gustschin if (name_len >= VFAT_MAXLEN_BYTES) 9918506eb8dSAnatolij Gustschin name_len = VFAT_MAXLEN_BYTES - 1; 9928506eb8dSAnatolij Gustschin 993c30a15e5SDonggeun Kim memcpy(l_filename, filename, name_len); 9948506eb8dSAnatolij Gustschin l_filename[name_len] = 0; /* terminate the string */ 995c30a15e5SDonggeun Kim downcase(l_filename); 996c30a15e5SDonggeun Kim 997c30a15e5SDonggeun Kim startsect = mydata->rootdir_sect; 998c30a15e5SDonggeun Kim retdent = find_directory_entry(mydata, startsect, 999c30a15e5SDonggeun Kim l_filename, dentptr, 0); 1000c30a15e5SDonggeun Kim if (retdent) { 1001c30a15e5SDonggeun Kim /* Update file size and start_cluster in a directory entry */ 1002c30a15e5SDonggeun Kim retdent->size = cpu_to_le32(size); 1003c30a15e5SDonggeun Kim start_cluster = FAT2CPU16(retdent->start); 1004c30a15e5SDonggeun Kim if (mydata->fatsize == 32) 1005c30a15e5SDonggeun Kim start_cluster |= 1006c30a15e5SDonggeun Kim (FAT2CPU16(retdent->starthi) << 16); 1007c30a15e5SDonggeun Kim 1008c30a15e5SDonggeun Kim ret = check_overflow(mydata, start_cluster, size); 1009c30a15e5SDonggeun Kim if (ret) { 1010c30a15e5SDonggeun Kim printf("Error: %ld overflow\n", size); 1011c30a15e5SDonggeun Kim goto exit; 1012c30a15e5SDonggeun Kim } 1013c30a15e5SDonggeun Kim 1014c30a15e5SDonggeun Kim ret = clear_fatent(mydata, start_cluster); 1015c30a15e5SDonggeun Kim if (ret) { 1016c30a15e5SDonggeun Kim printf("Error: clearing FAT entries\n"); 1017c30a15e5SDonggeun Kim goto exit; 1018c30a15e5SDonggeun Kim } 1019c30a15e5SDonggeun Kim 1020c30a15e5SDonggeun Kim ret = set_contents(mydata, retdent, buffer, size); 10218506eb8dSAnatolij Gustschin if (ret < 0) { 1022c30a15e5SDonggeun Kim printf("Error: writing contents\n"); 1023c30a15e5SDonggeun Kim goto exit; 1024c30a15e5SDonggeun Kim } 10258506eb8dSAnatolij Gustschin write_size = ret; 10268506eb8dSAnatolij Gustschin debug("attempt to write 0x%x bytes\n", write_size); 1027c30a15e5SDonggeun Kim 1028c30a15e5SDonggeun Kim /* Flush fat buffer */ 1029c30a15e5SDonggeun Kim ret = flush_fat_buffer(mydata); 1030c30a15e5SDonggeun Kim if (ret) { 1031c30a15e5SDonggeun Kim printf("Error: flush fat buffer\n"); 1032c30a15e5SDonggeun Kim goto exit; 1033c30a15e5SDonggeun Kim } 1034c30a15e5SDonggeun Kim 1035c30a15e5SDonggeun Kim /* Write directory table to device */ 1036c30a15e5SDonggeun Kim ret = set_cluster(mydata, dir_curclust, 1037c30a15e5SDonggeun Kim get_dentfromdir_block, 1038c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size); 1039c30a15e5SDonggeun Kim if (ret) { 10408506eb8dSAnatolij Gustschin printf("Error: writing directory entry\n"); 1041c30a15e5SDonggeun Kim goto exit; 1042c30a15e5SDonggeun Kim } 1043c30a15e5SDonggeun Kim } else { 1044c30a15e5SDonggeun Kim /* Set short name to set alias checksum field in dir_slot */ 1045c30a15e5SDonggeun Kim set_name(empty_dentptr, filename); 1046c30a15e5SDonggeun Kim fill_dir_slot(mydata, &empty_dentptr, filename); 1047c30a15e5SDonggeun Kim 1048c30a15e5SDonggeun Kim ret = start_cluster = find_empty_cluster(mydata); 1049c30a15e5SDonggeun Kim if (ret < 0) { 1050c30a15e5SDonggeun Kim printf("Error: finding empty cluster\n"); 1051c30a15e5SDonggeun Kim goto exit; 1052c30a15e5SDonggeun Kim } 1053c30a15e5SDonggeun Kim 1054c30a15e5SDonggeun Kim ret = check_overflow(mydata, start_cluster, size); 1055c30a15e5SDonggeun Kim if (ret) { 1056c30a15e5SDonggeun Kim printf("Error: %ld overflow\n", size); 1057c30a15e5SDonggeun Kim goto exit; 1058c30a15e5SDonggeun Kim } 1059c30a15e5SDonggeun Kim 1060c30a15e5SDonggeun Kim /* Set attribute as archieve for regular file */ 1061c30a15e5SDonggeun Kim fill_dentry(mydata, empty_dentptr, filename, 1062c30a15e5SDonggeun Kim start_cluster, size, 0x20); 1063c30a15e5SDonggeun Kim 1064c30a15e5SDonggeun Kim ret = set_contents(mydata, empty_dentptr, buffer, size); 10658506eb8dSAnatolij Gustschin if (ret < 0) { 1066c30a15e5SDonggeun Kim printf("Error: writing contents\n"); 1067c30a15e5SDonggeun Kim goto exit; 1068c30a15e5SDonggeun Kim } 10698506eb8dSAnatolij Gustschin write_size = ret; 10708506eb8dSAnatolij Gustschin debug("attempt to write 0x%x bytes\n", write_size); 1071c30a15e5SDonggeun Kim 1072c30a15e5SDonggeun Kim /* Flush fat buffer */ 1073c30a15e5SDonggeun Kim ret = flush_fat_buffer(mydata); 1074c30a15e5SDonggeun Kim if (ret) { 1075c30a15e5SDonggeun Kim printf("Error: flush fat buffer\n"); 1076c30a15e5SDonggeun Kim goto exit; 1077c30a15e5SDonggeun Kim } 1078c30a15e5SDonggeun Kim 1079c30a15e5SDonggeun Kim /* Write directory table to device */ 1080c30a15e5SDonggeun Kim ret = set_cluster(mydata, dir_curclust, 1081c30a15e5SDonggeun Kim get_dentfromdir_block, 1082c30a15e5SDonggeun Kim mydata->clust_size * mydata->sect_size); 1083c30a15e5SDonggeun Kim if (ret) { 1084c30a15e5SDonggeun Kim printf("Error: writing directory entry\n"); 1085c30a15e5SDonggeun Kim goto exit; 1086c30a15e5SDonggeun Kim } 1087c30a15e5SDonggeun Kim } 1088c30a15e5SDonggeun Kim 1089c30a15e5SDonggeun Kim exit: 1090c30a15e5SDonggeun Kim free(mydata->fatbuf); 10918506eb8dSAnatolij Gustschin return ret < 0 ? ret : write_size; 1092c30a15e5SDonggeun Kim } 1093c30a15e5SDonggeun Kim 1094c30a15e5SDonggeun Kim int file_fat_write(const char *filename, void *buffer, unsigned long maxsize) 1095c30a15e5SDonggeun Kim { 1096c30a15e5SDonggeun Kim printf("writing %s\n", filename); 1097c30a15e5SDonggeun Kim return do_fat_write(filename, buffer, maxsize); 1098c30a15e5SDonggeun Kim } 1099