You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 line
11KB

  1. /* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
  2. * https://github.com/PaulStoffregen/SerialFlash
  3. * Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
  4. *
  5. * Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
  6. * Please support PJRC's efforts to develop open source software by purchasing
  7. * Teensy or other genuine PJRC products.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice, development funding notice, and this permission
  17. * notice shall be included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "SerialFlash.h"
  28. #include "util/crc16.h"
  29. /* On-chip SerialFlash file allocation data structures:
  30. uint32_t signature = 0xFA96554C;
  31. uint16_t maxfiles
  32. uint16_t stringssize // div by 4
  33. uint16_t hashes[maxfiles]
  34. struct {
  35. uint32_t file_begin
  36. uint32_t file_length
  37. uint16_t string_index // div4
  38. } fileinfo[maxfiles]
  39. char strings[stringssize]
  40. A 32 bit signature is stored at the beginning of the flash memory.
  41. If 0xFFFFFFFF is seen, the entire chip should be assumed blank.
  42. If any value other than 0xFA96554C is found, a different data format
  43. is stored. This could should refuse to access the flash.
  44. The next 4 bytes store number of files and size of the strings
  45. section, which allow the position of every other item to be found.
  46. The string section size is the 16 bit integer times 4, which allows
  47. up to 262140 bytes for string data.
  48. An array of 16 bit filename hashes allows for quick linear search
  49. for potentially matching filenames. A hash value of 0xFFFF indicates
  50. no file is allocated for the remainder of the array.
  51. Following the hashes, and array of 10 byte structs give the location
  52. and length of the file's actual data, and the offset of its filename
  53. in the strings section.
  54. Strings are null terminated. The remainder of the chip is file data.
  55. */
  56. #define DEFAULT_MAXFILES 600
  57. #define DEFAULT_STRINGS_SIZE 25560
  58. static uint32_t check_signature(void)
  59. {
  60. uint32_t sig[2];
  61. SerialFlash.read(0, sig, 8);
  62. //Serial.printf("sig: %08X %08X\n", sig[0], sig[1]);
  63. if (sig[0] == 0xFA96554C) return sig[1];
  64. if (sig[0] == 0xFFFFFFFF) {
  65. sig[0] = 0xFA96554C;
  66. sig[1] = ((uint32_t)(DEFAULT_STRINGS_SIZE/4) << 16) | DEFAULT_MAXFILES;
  67. SerialFlash.write(0, sig, 8);
  68. while (!SerialFlash.ready()) ; // TODO: timeout
  69. SerialFlash.read(0, sig, 8);
  70. if (sig[0] == 0xFA96554C) return sig[1];
  71. }
  72. return 0;
  73. }
  74. static uint16_t filename_hash(const char *filename)
  75. {
  76. // http://isthe.com/chongo/tech/comp/fnv/
  77. uint32_t hash = 2166136261;
  78. const char *p;
  79. for (p=filename; *p; p++) {
  80. hash ^= *p;
  81. hash *= 16777619;
  82. }
  83. hash = (hash % (uint32_t)0xFFFE) + 1; // all values except 0000 & FFFF
  84. return hash;
  85. }
  86. static bool filename_compare(const char *filename, uint32_t straddr)
  87. {
  88. unsigned int i;
  89. const char *p;
  90. char buf[16];
  91. p = filename;
  92. while (1) {
  93. SerialFlash.read(straddr, buf, sizeof(buf));
  94. straddr += sizeof(buf);
  95. for (i=0; i < sizeof(buf); i++) {
  96. if (*p++ != buf[i]) return false;
  97. if (buf[i] == 0) return true;
  98. }
  99. }
  100. }
  101. #if 0
  102. void pbuf(const void *buf, uint32_t len)
  103. {
  104. const uint8_t *p = (const uint8_t *)buf;
  105. do {
  106. Serial.printf("%02X ", *p++);
  107. } while (--len > 0);
  108. Serial.println();
  109. }
  110. #endif
  111. SerialFlashFile SerialFlashChip::open(const char *filename)
  112. {
  113. uint32_t maxfiles, straddr;
  114. uint16_t hash, hashtable[8];
  115. uint32_t i, n, index=0;
  116. uint32_t buf[3];
  117. SerialFlashFile file;
  118. maxfiles = check_signature();
  119. //Serial.printf("sig: %08X\n", maxfiles);
  120. if (!maxfiles) return file;
  121. maxfiles &= 0xFFFF;
  122. hash = filename_hash(filename);
  123. //Serial.printf("hash %04X for \"%s\"\n", hash, filename);
  124. while (index < maxfiles) {
  125. n = 8;
  126. if (n > maxfiles - index) n = maxfiles - index;
  127. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  128. //Serial.printf(" read %u: ", 8 + index * 2);
  129. //pbuf(hashtable, n * 2);
  130. for (i=0; i < n; i++) {
  131. if (hashtable[i] == hash) {
  132. //Serial.printf(" hash match at index %u\n", index+i);
  133. buf[2] = 0;
  134. SerialFlash.read(8 + maxfiles * 2 + (index+i) * 10, buf, 10);
  135. //Serial.printf(" maxf=%d, index=%d, i=%d\n", maxfiles, index, i);
  136. //Serial.printf(" read %u: ", 8 + maxfiles * 2 + (index+i) * 10);
  137. //pbuf(buf, 10);
  138. straddr = 8 + maxfiles * 12 + buf[2] * 4;
  139. //Serial.printf(" straddr = %u\n", straddr);
  140. if (filename_compare(filename, straddr)) {
  141. //Serial.printf(" match!\n");
  142. //Serial.printf(" addr = %u\n", buf[0]);
  143. //Serial.printf(" len = %u\n", buf[1]);
  144. file.address = buf[0];
  145. file.length = buf[1];
  146. file.offset = 0;
  147. file.dirindex = index + i;
  148. return file;
  149. }
  150. } else if (hashtable[i] == 0xFFFF) {
  151. return file;
  152. }
  153. }
  154. index += n;
  155. }
  156. return file;
  157. }
  158. bool SerialFlashChip::exists(const char *filename)
  159. {
  160. SerialFlashFile file = open(filename);
  161. return (bool)file;
  162. }
  163. bool SerialFlashChip::remove(const char *filename)
  164. {
  165. SerialFlashFile file = open(filename);
  166. return remove(file);
  167. }
  168. bool SerialFlashChip::remove(SerialFlashFile &file)
  169. {
  170. // To "remove" a file, we simply zero its hash in the lookup
  171. // table, so it can't be found by open(). The space on the
  172. // flash memory is not freed.
  173. if (!file) return false;
  174. uint16_t hash;
  175. SerialFlash.read(8 + file.dirindex * 2, &hash, 2);
  176. //Serial.printf("remove hash %04X at %d index\n", hash, file.dirindex);
  177. hash ^= 0xFFFF; // write zeros to all ones
  178. SerialFlash.write(8 + file.dirindex * 2, &hash, 2);
  179. while (!SerialFlash.ready()) ; // wait... TODO: timeout
  180. SerialFlash.read(8 + file.dirindex * 2, &hash, 2);
  181. if (hash != 0) {
  182. //Serial.printf("remove failed, hash %04X\n", hash);
  183. return false;
  184. }
  185. file.address = 0;
  186. file.length = 0;
  187. return true;
  188. }
  189. static uint32_t find_first_unallocated_file_index(uint32_t maxfiles)
  190. {
  191. uint16_t hashtable[8];
  192. uint32_t i, n, index=0;
  193. do {
  194. n = 8;
  195. if (index + n > maxfiles) n = maxfiles - index;
  196. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  197. for (i=0; i < n; i++) {
  198. if (hashtable[i] == 0xFFFF) return index + i;
  199. }
  200. index += n;
  201. } while (index < maxfiles);
  202. return 0xFFFFFFFF;
  203. }
  204. static uint32_t string_length(uint32_t addr)
  205. {
  206. char buf[16];
  207. const char *p;
  208. uint32_t len=0;
  209. while (1) {
  210. SerialFlash.read(addr, buf, sizeof(buf));
  211. for (p=buf; p < buf + sizeof(buf); p++) {
  212. len++;
  213. if (*p == 0) return len;
  214. }
  215. addr += len;
  216. }
  217. }
  218. // uint32_t signature = 0xFA96554C;
  219. // uint16_t maxfiles
  220. // uint16_t stringssize // div by 4
  221. // uint16_t hashes[maxfiles]
  222. // struct {
  223. // uint32_t file_begin
  224. // uint32_t file_length
  225. // uint16_t string_index // div 4
  226. // } fileinfo[maxfiles]
  227. // char strings[stringssize]
  228. bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t align)
  229. {
  230. uint32_t maxfiles, stringsize;
  231. uint32_t index, buf[3];
  232. uint32_t address, straddr, len;
  233. SerialFlashFile file;
  234. // check if the file already exists
  235. if (exists(filename)) return false;
  236. // first, get the filesystem parameters
  237. maxfiles = check_signature();
  238. if (!maxfiles) return false;
  239. stringsize = (maxfiles & 0xFFFF0000) >> 14;
  240. maxfiles &= 0xFFFF;
  241. // find the first unused slot for this file
  242. index = find_first_unallocated_file_index(maxfiles);
  243. if (index >= maxfiles) return false;
  244. //Serial.printf("index = %u\n", index);
  245. // compute where to store the filename and actual data
  246. straddr = 8 + maxfiles * 12;
  247. if (index == 0) {
  248. address = straddr + stringsize;
  249. } else {
  250. buf[2] = 0;
  251. SerialFlash.read(8 + maxfiles * 2 + (index-1) * 10, buf, 10);
  252. address = buf[0] + buf[1];
  253. straddr += buf[2] * 4;
  254. straddr += string_length(straddr);
  255. straddr = (straddr + 3) & 0x0003FFFC;
  256. }
  257. //Serial.printf("straddr = %u\n", straddr);
  258. //Serial.printf("address = %u\n", address);
  259. //Serial.printf("length = %u\n", length);
  260. if (align > 0) {
  261. // for files aligned to sectors, adjust addr & len
  262. address += align - 1;
  263. address /= align;
  264. address *= align;
  265. //Serial.printf("align address = %u\n", address);
  266. length += align - 1;
  267. length /= align;
  268. length *= align;
  269. //Serial.printf("align length = %u\n", length);
  270. } else {
  271. // always align every file to a page boundary
  272. // for predictable write latency and to guarantee
  273. // write suspend for reading another file can't
  274. // conflict on the same page (2 files never share
  275. // a write page).
  276. address = (address + 255) & 0xFFFFFF00;
  277. }
  278. //Serial.printf("address = %u\n", address);
  279. // last check, if enough space exists...
  280. len = strlen(filename);
  281. // TODO: check for enough string space for filename
  282. uint8_t id[3];
  283. SerialFlash.readID(id);
  284. if (address + length > SerialFlash.capacity(id)) return false;
  285. SerialFlash.write(straddr, filename, len+1);
  286. buf[0] = address;
  287. buf[1] = length;
  288. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  289. SerialFlash.write(8 + maxfiles * 2 + index * 10, buf, 10);
  290. //Serial.printf(" write %u: ", 8 + maxfiles * 2 + index * 10);
  291. //pbuf(buf, 10);
  292. while (!SerialFlash.ready()) ; // TODO: timeout
  293. buf[0] = filename_hash(filename);
  294. //Serial.printf("hash = %04X\n", buf[0]);
  295. SerialFlash.write(8 + index * 2, buf, 2);
  296. while (!SerialFlash.ready()) ; // TODO: timeout
  297. return true;
  298. }
  299. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  300. {
  301. uint32_t maxfiles, index, straddr;
  302. uint32_t i, n;
  303. uint32_t buf[2];
  304. uint16_t hash;
  305. char str[16], *p=filename;
  306. filename[0] = 0;
  307. maxfiles = check_signature();
  308. if (!maxfiles) return false;
  309. maxfiles &= 0xFFFF;
  310. index = dirindex;
  311. while (1) {
  312. if (index >= maxfiles) return false;
  313. //Serial.printf("readdir, index = %u\n", index);
  314. SerialFlash.read(8 + index * 2, &hash, 2);
  315. if (hash != 0) break;
  316. index++; // skip deleted entries
  317. }
  318. dirindex = index + 1;
  319. buf[1] = 0;
  320. SerialFlash.read(8 + 4 + maxfiles * 2 + index * 10, buf, 6);
  321. if (buf[0] == 0xFFFFFFFF) return false;
  322. filesize = buf[0];
  323. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  324. //Serial.printf(" length = %u\n", buf[0]);
  325. //Serial.printf(" straddr = %u\n", straddr);
  326. while (strsize) {
  327. n = strsize;
  328. if (n > sizeof(str)) n = sizeof(str);
  329. SerialFlash.read(straddr, str, n);
  330. for (i=0; i < n; i++) {
  331. *p++ = str[i];
  332. if (str[i] == 0) {
  333. //Serial.printf(" name = %s\n", filename);
  334. return true;
  335. }
  336. }
  337. strsize -= n;
  338. straddr += n;
  339. }
  340. *(p - 1) = 0;
  341. //Serial.printf(" name(overflow) = %s\n", filename);
  342. return true;
  343. }
  344. void SerialFlashFile::erase()
  345. {
  346. uint32_t i, blocksize;
  347. blocksize = SerialFlash.blockSize();
  348. if (address & (blocksize - 1)) return; // must begin on a block boundary
  349. if (length & (blocksize - 1)) return; // must be exact number of blocks
  350. for (i=0; i < length; i += blocksize) {
  351. SerialFlash.eraseBlock(address + i);
  352. }
  353. }