1. 07 Feb, 2020 2 commits
    • Marek Szyprowski's avatar
      fat: write: adjust data written in each partial write · a54ece40
      Marek Szyprowski authored
      The code for handing file overwrite incorrectly calculated the amount of
      data to write when writing to the last non-cluster aligned chunk. Fix
      this by ensuring that no more data than the 'filesize' is written to disk.
      While touching min()-based calculations, change it to type-safe min_t()
      function.
      Signed-off-by: default avatarMarek Szyprowski <m.szyprowski@samsung.com>
      
      This patch finally fixes the issue revealed by the test script from the
      previous patch. The correctness of the change has been also verified by
      the following additional test scripts:
      
      --->8-fat_test2.sh---
      #!/bin/bash
      make sandbox_defconfig
      make
      dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k
      mkfs.vfat -v /tmp/10M.img
      cat >/tmp/cmds <<EOF
      x
      host bind 0 /tmp/10M.img
      fatls host 0
      mw 0x1000000 0x0a434241 0x1000 # "ABC\n"
      mw 0x1100000 0x0a464544 0x8000 # "DEF\n"
      fatwrite host 0 0x1000000 file0001.raw 0x1000
      fatwrite host 0 0x1000000 file0002.raw 0x1000
      fatwrite host 0 0x1000000 file0003.raw 0x1000
      fatwrite host 0 0x1000000 file0004.raw 0x1000
      fatwrite host 0 0x1000000 file0005.raw 0x1000
      fatrm host 0 file0002.raw
      fatrm host 0 file0004.raw
      fatls host 0
      fatwrite host 0 0x1100000 file0007.raw 0x2000
      fatwrite host 0 0x1100000 file0007.raw 0x1f00
      reset
      EOF
      ./u-boot </tmp/cmds
      #verify
      rm -r /tmp/result /tmp/model
      mkdir /tmp/result
      mkdir /tmp/model
      yes ABC | head -c 4096 >/tmp/model/file0001.raw
      yes ABC | head -c 4096 >/tmp/model/file0003.raw
      yes ABC | head -c 4096 >/tmp/model/file0005.raw
      yes DEF | head -c 7936 >/tmp/model/file0007.raw
      mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result
      hd /tmp/10M.img
      if diff -urq /tmp/model /tmp/result
      then
      	echo Test okay
      else
      	echo Test fail
      fi
      --->8-fat_test3.sh---
      #!/bin/bash
      make sandbox_defconfig
      make
      dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k
      mkfs.vfat -v /tmp/10M.img
      cat >/tmp/cmds <<EOF
      x
      host bind 0 /tmp/10M.img
      fatls host 0
      mw 0x1000000 0x0a434241 0x1000 # "ABC\n"
      mw 0x1100000 0x0a464544 0x8000 # "DEF\n"
      fatwrite host 0 0x1000000 file0001.raw 0x1000
      fatwrite host 0 0x1000000 file0002.raw 0x1000
      fatwrite host 0 0x1000000 file0003.raw 0x1000
      fatwrite host 0 0x1000000 file0004.raw 0x1000
      fatwrite host 0 0x1000000 file0005.raw 0x1000
      fatrm host 0 file0002.raw
      fatrm host 0 file0004.raw
      fatls host 0
      fatwrite host 0 0x1100000 file0007.raw 0x2000
      fatwrite host 0 0x1100000 file0007.raw 0x2100
      reset
      EOF
      ./u-boot </tmp/cmds
      #verify
      rm -r /tmp/result /tmp/model
      mkdir /tmp/result
      mkdir /tmp/model
      yes ABC | head -c 4096 >/tmp/model/file0001.raw
      yes ABC | head -c 4096 >/tmp/model/file0003.raw
      yes ABC | head -c 4096 >/tmp/model/file0005.raw
      yes DEF | head -c 8448 >/tmp/model/file0007.raw
      mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result
      hd /tmp/10M.img
      if diff -urq /tmp/model /tmp/result
      then
      	echo Test okay
      else
      	echo Test fail
      fi
      --->8-fat_test4.sh---
      #!/bin/bash
      make sandbox_defconfig
      make
      dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k
      mkfs.vfat -v /tmp/10M.img
      cat >/tmp/cmds <<EOF
      x
      host bind 0 /tmp/10M.img
      fatls host 0
      mw 0x1000000 0x0a434241 0x1000 # "ABC\n"
      mw 0x1100000 0x0a464544 0x8000 # "DEF\n"
      mw 0x1200000 0x0a494847 0x8000 # "GHI\n"
      fatwrite host 0 0x1000000 file0001.raw 0x1000
      fatwrite host 0 0x1000000 file0002.raw 0x1000
      fatwrite host 0 0x1000000 file0003.raw 0x1000
      fatwrite host 0 0x1000000 file0004.raw 0x1000
      fatwrite host 0 0x1000000 file0005.raw 0x1000
      fatrm host 0 file0002.raw
      fatrm host 0 file0004.raw
      fatls host 0
      fatwrite host 0 0x1100000 file0007.raw 0x900
      fatwrite host 0 0x1200000 file0007.raw 0x900 0x900
      fatwrite host 0 0x1100000 file0007.raw 0x900 0x1200
      fatwrite host 0 0x1200000 file0007.raw 0x900 0x1b00
      reset
      EOF
      ./u-boot </tmp/cmds
      #verify
      rm -r /tmp/result /tmp/model
      mkdir /tmp/result
      mkdir /tmp/model
      yes ABC | head -c 4096 >/tmp/model/file0001.raw
      yes ABC | head -c 4096 >/tmp/model/file0003.raw
      yes ABC | head -c 4096 >/tmp/model/file0005.raw
      yes DEF | head -c 2304 >/tmp/model/file0007.raw
      yes GHI | head -c 2304 >>/tmp/model/file0007.raw
      yes DEF | head -c 2304 >>/tmp/model/file0007.raw
      yes GHI | head -c 2304 >>/tmp/model/file0007.raw
      mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result
      hd /tmp/10M.img
      if diff -urq /tmp/model /tmp/result
      then
      	echo Test okay
      else
      	echo Test fail
      fi
      --->8---
      Feel free to prepare a proper sandbox/py_test based tests based on
      the provided test scripts.
      a54ece40
    • Marek Szyprowski's avatar
      fat: write: fix broken write to fragmented files · 5e615b74
      Marek Szyprowski authored
      The code for handing file overwrite incorrectly assumed that the file on
      disk is always contiguous. This resulted in corrupting disk structure
      every time when write to existing fragmented file happened. Fix this
      by adding proper check for cluster discontinuity and adjust chunk size
      on each partial write.
      Signed-off-by: default avatarMarek Szyprowski <m.szyprowski@samsung.com>
      
      This patch partially fixes the issue revealed by the following test
      script:
      
      --->8-fat_test1.sh---
      #!/bin/bash
      make sandbox_defconfig
      make
      dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k
      mkfs.vfat -v /tmp/10M.img
      cat >/tmp/cmds <<EOF
      x
      host bind 0 /tmp/10M.img
      fatls host 0
      mw 0x1000000 0x0a434241 0x1000 # "ABC\n"
      mw 0x1100000 0x0a464544 0x8000 # "DEF\n"
      fatwrite host 0 0x1000000 file0001.raw 0x1000
      fatwrite host 0 0x1000000 file0002.raw 0x1000
      fatwrite host 0 0x1000000 file0003.raw 0x1000
      fatwrite host 0 0x1000000 file0004.raw 0x1000
      fatwrite host 0 0x1000000 file0005.raw 0x1000
      fatrm host 0 file0002.raw
      fatrm host 0 file0004.raw
      fatls host 0
      fatwrite host 0 0x1100000 file0007.raw 0x4000
      fatwrite host 0 0x1100000 file0007.raw 0x4000
      reset
      EOF
      ./u-boot </tmp/cmds
      #verify
      rm -r /tmp/result /tmp/model
      mkdir /tmp/result
      mkdir /tmp/model
      yes ABC | head -c 4096 >/tmp/model/file0001.raw
      yes ABC | head -c 4096 >/tmp/model/file0003.raw
      yes ABC | head -c 4096 >/tmp/model/file0005.raw
      yes DEF | head -c 16384 >/tmp/model/file0007.raw
      mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result
      mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result
      hd /tmp/10M.img
      if diff -urq /tmp/model /tmp/result
      then
      	echo Test okay
      else
      	echo Test fail
      fi
      --->8---
      
      Overwritting a discontiguous test file (file0007.raw) no longer causes
      corruption to file0003.raw, which's data lies between the chunks of the
      test file. The amount of data written to disk is still incorrect, what
      causes damage to the file (file0005.raw), which's data lies next to the
      test file. This will be fixed by the next patch.
      
      Feel free to prepare a proper sandbox/py_test based tests based on the
      provided test scripts.
      5e615b74
  2. 06 Feb, 2020 2 commits
  3. 06 Dec, 2019 1 commit
  4. 05 Dec, 2019 1 commit
    • AKASHI Takahiro's avatar
      fs: fat: handle deleted directory entries correctly · 39606d46
      AKASHI Takahiro authored
      Unlink test for FAT file system seems to fail at test_unlink2.
      (When I added this test, I haven't seen any errors though.)
      for example,
      ===8<===
      fs_obj_unlink = ['fat', '/home/akashi/tmp/uboot_sandbox_test/128MB.fat32.img']
      
          def test_unlink2(self, u_boot_console, fs_obj_unlink):
              """
              Test Case 2 - delete many files
              """
              fs_type,fs_img = fs_obj_unlink
              with u_boot_console.log.section('Test Case 2 - unlink (many)'):
                  output = u_boot_console.run_command('host bind 0 %s' % fs_img)
      
                  for i in range(0, 20):
                      output = u_boot_console.run_command_list([
                          '%srm host 0:0 dir2/0123456789abcdef%02x' % (fs_type, i),
                          '%sls host 0:0 dir2/0123456789abcdef%02x' % (fs_type, i)])
                      assert('' == ''.join(output))
      
                  output = u_boot_console.run_command(
                      '%sls host 0:0 dir2' % fs_type)
      >           assert('0 file(s), 2 dir(s)' in output)
      E           AssertionError: assert '0 file(s), 2 dir(s)' in '            ./\r\r\n            ../\r\r\n        0   0123456789abcdef11\r\r\n\r\r\n1 file(s), 2 dir(s)'
      
      test/py/tests/test_fs/test_unlink.py:52: AssertionError
      ===>8===
      
      This can happen when fat_itr_next() wrongly detects an already-
      deleted directory entry.
      
      File deletion, which was added in the commit f8240ce9 ("fs: fat:
      support unlink"), is implemented by marking its entry for a short name
      with DELETED_FLAG, but related entry slots for a long file name are kept
      unmodified. (So entries will never be actually deleted from media.)
      
      To handle this case correctly, an additional check for a directory slot
      will be needed in fat_itr_next().
      
      In addition, I added extra comments about long file name and short file
      name format in FAT file system. Although they are not directly related
      to the issue, I hope it will be helpful for better understandings
      in general.
      Signed-off-by: default avatarAKASHI Takahiro <takahiro.akashi@linaro.org>
      39606d46
  5. 02 Dec, 2019 2 commits
  6. 17 Oct, 2019 3 commits
  7. 11 Oct, 2019 2 commits
  8. 26 Aug, 2019 1 commit
  9. 21 Aug, 2019 1 commit
  10. 20 Aug, 2019 1 commit
  11. 18 Aug, 2019 6 commits
  12. 11 Aug, 2019 2 commits
  13. 24 Jul, 2019 1 commit
  14. 18 Jul, 2019 6 commits
  15. 20 Jun, 2019 1 commit
  16. 28 May, 2019 5 commits
  17. 05 May, 2019 1 commit
  18. 03 May, 2019 2 commits