uffs_fileem_ecc_soft.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. This file is part of UFFS, the Ultra-low-cost Flash File System.
  3. Copyright (C) 2005-2010 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
  4. UFFS is free software; you can redistribute it and/or modify it under
  5. the GNU Library General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any
  7. later version.
  8. UFFS is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. or GNU Library General Public License, as applicable, for more details.
  12. You should have received a copy of the GNU General Public License
  13. and GNU Library General Public License along with UFFS; if not, write
  14. to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. As a special exception, if other files instantiate templates or use
  17. macros or inline functions from this file, or you compile this file
  18. and link it with other works to produce a work based on this file,
  19. this file does not by itself cause the resulting work to be covered
  20. by the GNU General Public License. However the source code for this
  21. file must still be made available in accordance with section (3) of
  22. the GNU General Public License v2.
  23. This exception does not invalidate any other reasons why a work based
  24. on this file might be covered by the GNU General Public License.
  25. */
  26. /**
  27. * \file uffs_fileem_ecc_soft.c
  28. * \brief emulate uffs file system for software ECC
  29. * \author Ricky Zheng @ Oct, 2010
  30. */
  31. #include <sys/types.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "uffs_config.h"
  36. #include "uffs/uffs_device.h"
  37. #include "uffs/uffs_flash.h"
  38. #include "uffs_fileem.h"
  39. #define PFX "femu: "
  40. #define MSG(msg,...) uffs_PerrorRaw(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
  41. #define MSGLN(msg,...) uffs_Perror(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
  42. static int femu_WritePage(uffs_Device *dev, u32 block, u32 page_num,
  43. const u8 *data, int data_len, const u8 *spare, int spare_len)
  44. {
  45. int written;
  46. int abs_page;
  47. int full_page_size;
  48. uffs_FileEmu *emu;
  49. struct uffs_StorageAttrSt *attr = dev->attr;
  50. emu = (uffs_FileEmu *)(dev->attr->_private);
  51. if (!emu || !(emu->fp)) {
  52. goto err;
  53. }
  54. abs_page = attr->pages_per_block * block + page_num;
  55. full_page_size = attr->page_data_size + attr->spare_size;
  56. if (data && data_len > 0) {
  57. if (data_len > attr->page_data_size)
  58. goto err;
  59. emu->em_monitor_page[abs_page]++;
  60. if (emu->em_monitor_page[abs_page] > PAGE_DATA_WRITE_COUNT_LIMIT) {
  61. MSGLN("Warrning: block %d page %d exceed it's maximum write time!", block, page_num);
  62. goto err;
  63. }
  64. fseek(emu->fp, abs_page * full_page_size, SEEK_SET);
  65. written = fwrite(data, 1, data_len, emu->fp);
  66. if (written != data_len) {
  67. MSGLN("write page I/O error ?");
  68. goto err;
  69. }
  70. dev->st.page_write_count++;
  71. dev->st.io_write += written;
  72. }
  73. if (spare && spare_len > 0) {
  74. if (spare_len > attr->spare_size)
  75. goto err;
  76. emu->em_monitor_spare[abs_page]++;
  77. if (emu->em_monitor_spare[abs_page] > PAGE_SPARE_WRITE_COUNT_LIMIT) {
  78. MSGLN("Warrning: block %d page %d (spare) exceed it's maximum write time!", block, page_num);
  79. goto err;
  80. }
  81. fseek(emu->fp, abs_page * full_page_size + attr->page_data_size, SEEK_SET);
  82. written = fwrite(spare, 1, spare_len, emu->fp);
  83. if (written != spare_len) {
  84. MSGLN("write spare I/O error ?");
  85. goto err;
  86. }
  87. dev->st.spare_write_count++;
  88. dev->st.io_write += written;
  89. }
  90. if (data == NULL && spare == NULL) {
  91. // mark bad block
  92. fseek(emu->fp, abs_page * full_page_size + attr->page_data_size + attr->block_status_offs, SEEK_SET);
  93. written = fwrite("\0", 1, 1, emu->fp);
  94. if (written != 1) {
  95. MSGLN("write bad block mark I/O error ?");
  96. goto err;
  97. }
  98. dev->st.io_write++;
  99. }
  100. fflush(emu->fp);
  101. return UFFS_FLASH_NO_ERR;
  102. err:
  103. fflush(emu->fp);
  104. return UFFS_FLASH_IO_ERR;
  105. }
  106. static URET femu_ReadPage(uffs_Device *dev, u32 block, u32 page_num, u8 *data, int data_len, u8 *ecc,
  107. u8 *spare, int spare_len)
  108. {
  109. int nread;
  110. uffs_FileEmu *emu;
  111. int abs_page;
  112. int full_page_size;
  113. struct uffs_StorageAttrSt *attr = dev->attr;
  114. unsigned char status;
  115. emu = (uffs_FileEmu *)(dev->attr->_private);
  116. if (!emu || !(emu->fp)) {
  117. goto err;
  118. }
  119. abs_page = attr->pages_per_block * block + page_num;
  120. full_page_size = attr->page_data_size + attr->spare_size;
  121. if (data && data_len > 0) {
  122. if (data_len > attr->page_data_size)
  123. goto err;
  124. fseek(emu->fp, abs_page * full_page_size, SEEK_SET);
  125. nread = fread(data, 1, data_len, emu->fp);
  126. if (nread != data_len) {
  127. MSGLN("read page I/O error ?");
  128. goto err;
  129. }
  130. dev->st.io_read += nread;
  131. dev->st.page_read_count++;
  132. }
  133. if (spare && spare_len > 0) {
  134. if (spare_len > attr->spare_size)
  135. goto err;
  136. fseek(emu->fp, abs_page * full_page_size + attr->page_data_size, SEEK_SET);
  137. nread = fread(spare, 1, spare_len, emu->fp);
  138. if (nread != spare_len) {
  139. MSGLN("read page spare I/O error ?");
  140. goto err;
  141. }
  142. dev->st.io_read += nread;
  143. dev->st.spare_read_count++;
  144. }
  145. if (data == NULL && spare == NULL) {
  146. // read bad block mark
  147. fseek(emu->fp, abs_page * full_page_size + attr->page_data_size + attr->block_status_offs, SEEK_SET);
  148. nread = fread(&status, 1, 1, emu->fp);
  149. if (nread != 1) {
  150. MSGLN("read badblock mark I/O error ?");
  151. goto err;
  152. }
  153. dev->st.io_read++;
  154. return status == 0xFF ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
  155. }
  156. return UFFS_FLASH_NO_ERR;
  157. err:
  158. return UFFS_FLASH_IO_ERR;
  159. }
  160. uffs_FlashOps g_femu_ops_ecc_soft = {
  161. femu_InitFlash, // InitFlash()
  162. femu_ReleaseFlash, // ReleaseFlash()
  163. femu_ReadPage, // ReadPage()
  164. NULL, // ReadPageWithLayout
  165. femu_WritePage, // WritePage()
  166. NULL, // WirtePageWithLayout
  167. NULL, // IsBadBlock(), let UFFS take care of it.
  168. NULL, // MarkBadBlock(), let UFFS take care of it.
  169. femu_EraseBlock, // EraseBlock()
  170. };