fileops.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. fileops.c: simple file access functions
  19. */
  20. #include "config.h"
  21. #include "uart.h"
  22. #include "ff.h"
  23. #include "fileops.h"
  24. #include "diskio.h"
  25. /*
  26. WCHAR ff_convert(WCHAR w, UINT dir) {
  27. return w;
  28. }*/
  29. int newcard;
  30. void file_init() {
  31. file_res=f_mount(0, &fatfs);
  32. newcard = 0;
  33. }
  34. void file_reinit(void) {
  35. disk_init();
  36. file_init();
  37. }
  38. FRESULT dir_open_by_filinfo(DIR* dir, FILINFO* fno) {
  39. return l_opendirbycluster(&fatfs, dir, (TCHAR*)"", fno->clust);
  40. }
  41. void file_open_by_filinfo(FILINFO* fno) {
  42. file_res = l_openfilebycluster(&fatfs, &file_handle, (TCHAR*)"", fno->clust, fno->fsize);
  43. }
  44. void file_open(const uint8_t* filename, BYTE flags) {
  45. if (disk_state == DISK_CHANGED) {
  46. file_reinit();
  47. newcard = 1;
  48. }
  49. file_res = f_open(&file_handle, (TCHAR*)filename, flags);
  50. file_block_off = sizeof(file_buf);
  51. file_block_max = sizeof(file_buf);
  52. file_status = file_res ? FILE_ERR : FILE_OK;
  53. }
  54. void file_close() {
  55. file_res = f_close(&file_handle);
  56. }
  57. void file_seek(uint32_t offset) {
  58. file_res = f_lseek(&file_handle, (DWORD)offset);
  59. }
  60. UINT file_read() {
  61. UINT bytes_read;
  62. file_res = f_read(&file_handle, file_buf, sizeof(file_buf), &bytes_read);
  63. return bytes_read;
  64. }
  65. UINT file_write() {
  66. UINT bytes_written;
  67. file_res = f_write(&file_handle, file_buf, sizeof(file_buf), &bytes_written);
  68. if(bytes_written < sizeof(file_buf)) {
  69. printf("wrote less than expected - card full?\n");
  70. }
  71. return bytes_written;
  72. }
  73. UINT file_readblock(void* buf, uint32_t addr, uint16_t size) {
  74. UINT bytes_read;
  75. file_res = f_lseek(&file_handle, addr);
  76. if(file_handle.fptr != addr) {
  77. return 0;
  78. }
  79. file_res = f_read(&file_handle, buf, size, &bytes_read);
  80. return bytes_read;
  81. }
  82. UINT file_writeblock(void* buf, uint32_t addr, uint16_t size) {
  83. UINT bytes_written;
  84. file_res = f_lseek(&file_handle, addr);
  85. if(file_res) return 0;
  86. file_res = f_write(&file_handle, buf, size, &bytes_written);
  87. return bytes_written;
  88. }
  89. uint8_t file_getc() {
  90. if(file_block_off == file_block_max) {
  91. file_block_max = file_read();
  92. if(file_block_max == 0) file_status = FILE_EOF;
  93. file_block_off = 0;
  94. }
  95. return file_buf[file_block_off++];
  96. }