Merge branch 'agust@denx.de' of git://git.denx.de/u-boot-staging
[oweals/u-boot.git] / fs / fat / fat_write.c
index 4f1772f29a7dc6ed6f95cee5037851c1b94486e6..a6181e71b558c0f509ef2c67b5d9e40437c9c23d 100644 (file)
@@ -41,23 +41,19 @@ static void uppercase(char *str, int len)
 }
 
 static int total_sector;
-static int disk_write(__u32 startblock, __u32 getsize, __u8 *bufptr)
+static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
 {
-       if (cur_dev == NULL)
+       if (!cur_dev || !cur_dev->block_write)
                return -1;
 
-       if (startblock + getsize > total_sector) {
+       if (cur_part_info.start + block + nr_blocks >
+               cur_part_info.start + total_sector) {
                printf("error: overflow occurs\n");
                return -1;
        }
 
-       startblock += part_offset;
-
-       if (cur_dev->block_read) {
-               return cur_dev->block_write(cur_dev->dev, startblock, getsize,
-                                          (unsigned long *) bufptr);
-       }
-       return -1;
+       return cur_dev->block_write(cur_dev->dev,
+                       cur_part_info.start + block, nr_blocks, buf);
 }
 
 /*
@@ -112,6 +108,7 @@ static void set_name(dir_entry *dirent, const char *filename)
        debug("ext : %s\n", dirent->ext);
 }
 
+static __u8 num_of_fats;
 /*
  * Write fat buffer into block device
  */
@@ -134,6 +131,15 @@ static int flush_fat_buffer(fsdata *mydata)
                return -1;
        }
 
+       if (num_of_fats == 2) {
+               /* Update corresponding second FAT blocks */
+               startblock += mydata->fatlength;
+               if (disk_write(startblock, getsize, bufptr) < 0) {
+                       debug("error: writing second FAT blocks\n");
+                       return -1;
+               }
+       }
+
        return 0;
 }
 
@@ -323,7 +329,7 @@ static void
 fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
 {
        dir_slot *slotptr = (dir_slot *)get_vfatname_block;
-       __u8 counter, checksum;
+       __u8 counter = 0, checksum;
        int idx = 0, ret;
        char s_name[16];
 
@@ -787,7 +793,7 @@ static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
        if (size % mydata->sect_size)
                sect_num++;
 
-       if (startsect + sect_num > total_sector)
+       if (startsect + sect_num > cur_part_info.start + total_sector)
                return -1;
 
        return 0;
@@ -817,7 +823,6 @@ static dir_entry *empty_dentptr;
 static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
        char *filename, dir_entry *retdent, __u32 start)
 {
-       __u16 prevcksum = 0xffff;
        __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
 
        debug("get_dentfromdir: %s\n", filename);
@@ -851,8 +856,6 @@ static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
 #ifdef CONFIG_SUPPORT_VFAT
                                if ((dentptr->attr & ATTR_VFAT) &&
                                    (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
-                                       prevcksum =
-                                       ((dir_slot *)dentptr)->alias_checksum;
                                        get_long_file_name(mydata, curclust,
                                                     get_dentfromdir_block,
                                                     &dentptr, l_name);
@@ -916,7 +919,6 @@ static int do_fat_write(const char *filename, void *buffer,
        unsigned long size)
 {
        dir_entry *dentptr, *retdent;
-       dir_slot *slotptr;
        __u32 startsect;
        __u32 start_cluster;
        boot_sector bs;
@@ -924,8 +926,9 @@ static int do_fat_write(const char *filename, void *buffer,
        fsdata datablock;
        fsdata *mydata = &datablock;
        int cursect;
-       int root_cluster, ret = -1, name_len;
+       int ret = -1, name_len;
        char l_filename[VFAT_MAXLEN_BYTES];
+       int write_size = size;
 
        dir_curclust = 0;
 
@@ -936,9 +939,7 @@ static int do_fat_write(const char *filename, void *buffer,
 
        total_sector = bs.total_sect;
        if (total_sector == 0)
-               total_sector = part_size;
-
-       root_cluster = bs.root_cluster;
+               total_sector = cur_part_info.size;
 
        if (mydata->fatsize == 32)
                mydata->fatlength = bs.fat32_length;
@@ -949,6 +950,7 @@ static int do_fat_write(const char *filename, void *buffer,
 
        cursect = mydata->rootdir_sect
                = mydata->fat_sect + mydata->fatlength * bs.fats;
+       num_of_fats = bs.fats;
 
        mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
        mydata->clust_size = bs.cluster_size;
@@ -985,7 +987,11 @@ static int do_fat_write(const char *filename, void *buffer,
        dentptr = (dir_entry *) do_fat_read_block;
 
        name_len = strlen(filename);
+       if (name_len >= VFAT_MAXLEN_BYTES)
+               name_len = VFAT_MAXLEN_BYTES - 1;
+
        memcpy(l_filename, filename, name_len);
+       l_filename[name_len] = 0; /* terminate the string */
        downcase(l_filename);
 
        startsect = mydata->rootdir_sect;
@@ -1012,10 +1018,12 @@ static int do_fat_write(const char *filename, void *buffer,
                }
 
                ret = set_contents(mydata, retdent, buffer, size);
-               if (ret) {
+               if (ret < 0) {
                        printf("Error: writing contents\n");
                        goto exit;
                }
+               write_size = ret;
+               debug("attempt to write 0x%x bytes\n", write_size);
 
                /* Flush fat buffer */
                ret = flush_fat_buffer(mydata);
@@ -1029,12 +1037,10 @@ static int do_fat_write(const char *filename, void *buffer,
                            get_dentfromdir_block,
                            mydata->clust_size * mydata->sect_size);
                if (ret) {
-                       printf("Error: wrinting directory entry\n");
+                       printf("Error: writing directory entry\n");
                        goto exit;
                }
        } else {
-               slotptr = (dir_slot *)empty_dentptr;
-
                /* Set short name to set alias checksum field in dir_slot */
                set_name(empty_dentptr, filename);
                fill_dir_slot(mydata, &empty_dentptr, filename);
@@ -1056,10 +1062,12 @@ static int do_fat_write(const char *filename, void *buffer,
                        start_cluster, size, 0x20);
 
                ret = set_contents(mydata, empty_dentptr, buffer, size);
-               if (ret) {
+               if (ret < 0) {
                        printf("Error: writing contents\n");
                        goto exit;
                }
+               write_size = ret;
+               debug("attempt to write 0x%x bytes\n", write_size);
 
                /* Flush fat buffer */
                ret = flush_fat_buffer(mydata);
@@ -1080,7 +1088,7 @@ static int do_fat_write(const char *filename, void *buffer,
 
 exit:
        free(mydata->fatbuf);
-       return ret;
+       return ret < 0 ? ret : write_size;
 }
 
 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)