yaffs.diff 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. diff --git a/dfs_yaffs2.c b/dfs_yaffs2.c
  2. new file mode 100644
  3. index 0000000..4b056ae
  4. --- /dev/null
  5. +++ b/dfs_yaffs2.c
  6. @@ -0,0 +1,405 @@
  7. +/*
  8. + * File : rtthread.h
  9. + * This file is part of RT-Thread RTOS
  10. + * COPYRIGHT (C) 2006-2011, RT-Thread Development Team
  11. + *
  12. + * The license and distribution terms for this file may be
  13. + * found in the file LICENSE in this distribution or at
  14. + * http://www.rt-thread.org/license/LICENSE.
  15. + *
  16. + * Change Logs:
  17. + * Date Author Notes
  18. + * 2011-10-22 prife the first version
  19. + * 2012-04-14 prife use mtd device interface
  20. +*/
  21. +#include <rtthread.h>
  22. +#include <rtdevice.h>
  23. +
  24. +#include "yaffsfs.h"
  25. +#include "yaffs_nandif.h"
  26. +
  27. +#include <dfs_fs.h>
  28. +#include <dfs_def.h>
  29. +
  30. +#define YAFFS_FILE_PATH_MAX 256 /* the longest file path */
  31. +
  32. +#define NAND_DEVICE_PART_MAX 4 /* the max partitions on a nand deivce*/
  33. +struct device_part
  34. +{
  35. + struct rt_mtd_nand_device *dev;
  36. + ynandif_Geometry g;
  37. +};
  38. +static struct device_part nand_part[NAND_DEVICE_PART_MAX] = {0};
  39. +
  40. +extern int yaffs_start(const char * mountpath, ynandif_Geometry * g);
  41. +extern void yaffs_config(ynandif_Geometry * g, struct rt_mtd_nand_device * dev);
  42. +
  43. +static int dfs_yaffs_mount(struct dfs_filesystem* fs,
  44. + unsigned long rwflag,
  45. + const void* data)
  46. +{
  47. + unsigned index;
  48. + ynandif_Geometry *g;
  49. +
  50. + /*1. find a empty entry in partition table */
  51. + for (index = 0; index < NAND_DEVICE_PART_MAX ; index ++)
  52. + {
  53. + if (nand_part[index].dev == RT_NULL)
  54. + break;
  55. + }
  56. + if (index == NAND_DEVICE_PART_MAX)
  57. + return -DFS_STATUS_ENOSPC;
  58. +
  59. + /*2. fill nand_part*/
  60. + nand_part[index].dev = RT_MTD_NAND_DEVICE(fs->dev_id);
  61. + yaffs_config(&nand_part[index].g, RT_MTD_NAND_DEVICE(fs->dev_id));
  62. +
  63. + /*3. start up yaffs2 */
  64. + yaffs_start(fs->path, &nand_part[index].g);
  65. + if (yaffs_mount(fs->path) < 0)
  66. + {
  67. + return yaffsfs_GetLastError();
  68. + }
  69. + return 0;
  70. +}
  71. +
  72. +static int dfs_yaffs_unmount(struct dfs_filesystem* fs)
  73. +{
  74. + unsigned index;
  75. + if (yaffs_unmount(fs->path) < 0)
  76. + return yaffsfs_GetLastError();
  77. +
  78. + /* find device index, then umount it */
  79. + for (index = 0; index < NAND_DEVICE_PART_MAX; index++)
  80. + {
  81. + if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id))
  82. + {
  83. + nand_part[index].dev = RT_NULL;
  84. + return DFS_STATUS_OK;
  85. + }
  86. + }
  87. + return -DFS_STATUS_ENOENT;
  88. +}
  89. +
  90. +static int dfs_yaffs_mkfs(const char* device_name)
  91. +{
  92. + /* just erase all blocks on this nand partition */
  93. + return -DFS_STATUS_ENOSYS;
  94. +}
  95. +
  96. +static int dfs_yaffs_statfs(struct dfs_filesystem* fs,
  97. + struct statfs *buf)
  98. +{
  99. + struct rt_mtd_nand_device * mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
  100. +
  101. + RT_ASSERT(mtd != RT_NULL);
  102. +
  103. + buf->f_bsize = mtd->page_size;
  104. + buf->f_blocks = (mtd->block_size)/(mtd->page_size)*
  105. + (mtd->block_start - mtd->block_end + 1);
  106. + buf->f_bfree = yaffs_freespace(fs->path) / mtd->page_size;
  107. +
  108. + return 0;
  109. +}
  110. +
  111. +static int dfs_yaffs_open(struct dfs_fd* file)
  112. +{
  113. + int fd;
  114. + int oflag;
  115. + int result;
  116. +
  117. + oflag = file->flags;
  118. + if (oflag & DFS_O_DIRECTORY) /* operations about dir */
  119. + {
  120. + yaffs_DIR * dir;
  121. + if (oflag & DFS_O_CREAT) /* create a dir*/
  122. + {
  123. + result = yaffs_mkdir(file->path, 0x777); /* the second args not supported by rtt */
  124. + if (result < 0)
  125. + return yaffsfs_GetLastError();
  126. + }
  127. + /* open dir */
  128. + dir = yaffs_opendir(file->path);
  129. + if (dir == RT_NULL)
  130. + return yaffsfs_GetLastError();
  131. + /* save this pointer,will used by dfs_yaffs_getdents*/
  132. + file->data = dir;
  133. + return 0;
  134. + }
  135. + /* regular file operations */
  136. + fd = yaffs_open(file->path, oflag, S_IREAD | S_IWRITE);
  137. + if (fd < 0)
  138. + return yaffsfs_GetLastError();
  139. +
  140. + /* save this pointer, it will be used when calling read()£¬write(),
  141. + flush(), lessk(), and will be free when calling close()*/
  142. + file->data = (void *)fd;
  143. + file->pos = yaffs_lseek(fd,0,SEEK_CUR);
  144. + file->size = yaffs_lseek(fd,0,SEEK_END);
  145. + yaffs_lseek(fd, file->pos, SEEK_SET);
  146. +
  147. + if (oflag & DFS_O_APPEND)
  148. + {
  149. + file->pos = file->size;
  150. + file->size = yaffs_lseek(fd,0,SEEK_END);
  151. + }
  152. + return 0;
  153. +}
  154. +
  155. +static int dfs_yaffs_close(struct dfs_fd* file)
  156. +{
  157. + int oflag;
  158. + int fd;
  159. +
  160. + oflag = file->flags;
  161. + if (oflag & DFS_O_DIRECTORY) /* operations about dir */
  162. + {
  163. + if (yaffs_closedir((yaffs_DIR *)(file->data)) < 0)
  164. + return yaffsfs_GetLastError();
  165. + return 0;
  166. + }
  167. + /* regular file operations */
  168. + fd = (int)(file->data);
  169. +
  170. + if (yaffs_close(fd) == 0)
  171. + return 0;
  172. +
  173. + /* release memory */
  174. + return yaffsfs_GetLastError();
  175. +}
  176. +
  177. +static int dfs_yaffs_ioctl(struct dfs_fd* file, int cmd, void* args)
  178. +{
  179. + return -DFS_STATUS_ENOSYS;
  180. +}
  181. +
  182. +static int dfs_yaffs_read(struct dfs_fd* file, void* buf, rt_size_t len)
  183. +{
  184. + int fd;
  185. + int char_read;
  186. +
  187. + fd = (int)(file->data);
  188. + char_read = yaffs_read(fd, buf, len);
  189. + if (char_read < 0)
  190. + return yaffsfs_GetLastError();
  191. +
  192. + /* update position */
  193. + file->pos = yaffs_lseek(fd, 0, SEEK_CUR);
  194. + return char_read;
  195. +}
  196. +
  197. +static int dfs_yaffs_write(struct dfs_fd* file,
  198. + const void* buf,
  199. + rt_size_t len)
  200. +{
  201. + int fd;
  202. + int char_write;
  203. +
  204. + fd = (int)(file->data);
  205. +
  206. + char_write = yaffs_write(fd, buf, len);
  207. + if (char_write < 0)
  208. + return yaffsfs_GetLastError();
  209. +
  210. + /* update position */
  211. + file->pos = yaffs_lseek(fd, 0, SEEK_CUR);
  212. + return char_write;
  213. +}
  214. +
  215. +static int dfs_yaffs_flush(struct dfs_fd* file)
  216. +{
  217. + int fd;
  218. + int result;
  219. +
  220. + fd = (int)(file->data);
  221. +
  222. + result = yaffs_flush(fd);
  223. + if (result < 0)
  224. + return yaffsfs_GetLastError();
  225. + return 0;
  226. +}
  227. +
  228. +static int dfs_yaffs_lseek(struct dfs_fd* file,
  229. + rt_off_t offset)
  230. +{
  231. + int fd;
  232. + int result;
  233. +
  234. + fd = (int)(file->data);
  235. +
  236. + /* set offset as current offset */
  237. + result = yaffs_lseek(fd, offset, SEEK_SET);
  238. + if (result < 0)
  239. + return yaffsfs_GetLastError();
  240. + return result;
  241. +}
  242. +
  243. +/* return the size of struct dirent*/
  244. +static int dfs_yaffs_getdents(struct dfs_fd* file,
  245. + struct dirent* dirp,
  246. + rt_uint32_t count)
  247. +{
  248. + rt_uint32_t index;
  249. + char * file_path;
  250. + struct dirent* d;
  251. + yaffs_DIR* dir;
  252. + struct yaffs_dirent * yaffs_d;
  253. +
  254. + dir = (yaffs_DIR*)(file->data);
  255. + RT_ASSERT(dir != RT_NULL);
  256. +
  257. + /* make integer count, usually count is 1 */
  258. + count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  259. + if (count == 0) return -DFS_STATUS_EINVAL;
  260. +
  261. + /* allocate file name */
  262. + file_path = rt_malloc(YAFFS_FILE_PATH_MAX);
  263. + if (file_path == RT_NULL)
  264. + return -DFS_STATUS_ENOMEM;
  265. +
  266. + index = 0;
  267. + /* usually, the while loop should only be looped only once! */
  268. + while (1)
  269. + {
  270. + struct yaffs_stat s;
  271. +
  272. + d = dirp + index;
  273. +
  274. + yaffs_d = yaffs_readdir(dir);
  275. + if (yaffs_d == RT_NULL)
  276. + {
  277. + if (yaffsfs_GetLastError() == EBADF)
  278. + return -DFS_STATUS_EBADF;
  279. +
  280. + rt_free(file_path);
  281. + return -1; /* a general error */
  282. + }
  283. +
  284. + rt_snprintf(file_path, YAFFS_FILE_PATH_MAX, "%s/%s", file->path, yaffs_d->d_name);
  285. +
  286. + yaffs_lstat(file_path, &s);
  287. + switch(s.st_mode & S_IFMT)
  288. + {
  289. + case S_IFREG: d->d_type = DFS_DT_REG; break;
  290. +
  291. + case S_IFDIR: d->d_type = DFS_DT_DIR; break;
  292. +
  293. + case S_IFLNK:
  294. + default: d->d_type = DFS_DT_UNKNOWN; break;
  295. + }
  296. +
  297. + /* write the rest feilds of struct dirent* dirp */
  298. + d->d_namlen = rt_strlen(yaffs_d->d_name);
  299. + d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  300. + rt_strncpy(d->d_name, yaffs_d->d_name, rt_strlen(yaffs_d->d_name) + 1);
  301. +
  302. + index ++;
  303. + if (index * sizeof(struct dirent) >= count)
  304. + break;
  305. + }
  306. +
  307. + /* free file name buf */
  308. + rt_free(file_path);
  309. +
  310. + if (index == 0)
  311. + return yaffsfs_GetLastError();
  312. +
  313. + return index * sizeof(struct dirent);
  314. +}
  315. +
  316. +static int dfs_yaffs_unlink(struct dfs_filesystem* fs, const char* path)
  317. +{
  318. + int result;
  319. + struct yaffs_stat s;
  320. +
  321. + /* judge file type, dir is to be delete by yaffs_rmdir, others by yaffs_unlink */
  322. + if (yaffs_lstat(path, &s) < 0)
  323. + {
  324. + return yaffsfs_GetLastError();
  325. + }
  326. +
  327. + switch(s.st_mode & S_IFMT)
  328. + {
  329. + case S_IFREG:
  330. + result = yaffs_unlink(path);
  331. + break;
  332. + case S_IFDIR:
  333. + result = yaffs_rmdir(path);
  334. + break;
  335. + default:
  336. + /* unknown file type */
  337. + return -1;
  338. + }
  339. + if (result < 0)
  340. + return yaffsfs_GetLastError();
  341. + return 0;
  342. +}
  343. +
  344. +static int dfs_yaffs_rename(struct dfs_filesystem* fs,
  345. + const char* oldpath,
  346. + const char* newpath)
  347. +{
  348. + int result;
  349. +
  350. + result = yaffs_rename(oldpath, newpath);
  351. +
  352. + if (result < 0)
  353. + return yaffsfs_GetLastError();
  354. + return 0;
  355. +}
  356. +
  357. +static int dfs_yaffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
  358. +{
  359. + int result;
  360. + struct yaffs_stat s;
  361. + struct rt_mtd_nand_device * mtd;
  362. +
  363. + result = yaffs_stat(path, &s);
  364. + if (result < 0)
  365. + return yaffsfs_GetLastError();
  366. +
  367. + /* convert to dfs stat structure */
  368. + st->st_dev = 0;
  369. + st->st_mode = s.st_mode;
  370. + st->st_size = s.st_size;
  371. + st->st_mtime = s.yst_mtime;
  372. +
  373. + mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
  374. + st->st_blksize = mtd->page_size;
  375. +
  376. + return 0;
  377. +}
  378. +
  379. +static const struct dfs_filesystem_operation dfs_yaffs_ops =
  380. +{
  381. + "yaffs2", /* file system type: yaffs2 */
  382. +#if RTTHREAD_VERSION >= 10100
  383. + DFS_FS_FLAG_FULLPATH,
  384. +#else
  385. +#error "yaffs2 can only work with rtthread whose version should >= 1.01\n"
  386. +#endif
  387. + dfs_yaffs_mount,
  388. + dfs_yaffs_unmount,
  389. + dfs_yaffs_mkfs,
  390. + dfs_yaffs_statfs,
  391. +
  392. + dfs_yaffs_open,
  393. + dfs_yaffs_close,
  394. + dfs_yaffs_ioctl,
  395. + dfs_yaffs_read,
  396. + dfs_yaffs_write,
  397. + dfs_yaffs_flush,
  398. + dfs_yaffs_lseek,
  399. + dfs_yaffs_getdents,
  400. + dfs_yaffs_unlink,
  401. + dfs_yaffs_stat,
  402. + dfs_yaffs_rename,
  403. +};
  404. +
  405. +int dfs_yaffs_init(void)
  406. +{
  407. + /* register fatfs file system */
  408. + dfs_register(&dfs_yaffs_ops);
  409. +
  410. + return 0;
  411. +}
  412. diff --git a/yaffs/direct/yaffs_list.h b/yaffs/direct/yaffs_list.h
  413. index f1c5254..17044f5 100644
  414. --- a/yaffs/direct/yaffs_list.h
  415. +++ b/yaffs/direct/yaffs_list.h
  416. @@ -39,7 +39,7 @@ struct list_head {
  417. /* Initialise a static list */
  418. #define LIST_HEAD(name) \
  419. -struct list_head name = { &(name), &(name)}
  420. +static struct list_head name = { &(name), &(name)}
  421. diff --git a/yaffs/direct/yaffs_nandif.c b/yaffs/direct/yaffs_nandif.c
  422. index b93b55a..3fae80e 100644
  423. --- a/yaffs/direct/yaffs_nandif.c
  424. +++ b/yaffs/direct/yaffs_nandif.c
  425. @@ -18,7 +18,6 @@
  426. #include "yaffs_nandif.h"
  427. #include "yaffs_packedtags2.h"
  428. -#include "yramsim.h"
  429. #include "yaffs_trace.h"
  430. #include "yaffsfs.h"
  431. @@ -216,8 +215,8 @@ struct yaffs_dev *
  432. yaffs_add_dev_from_geometry(const YCHAR *name,
  433. const ynandif_Geometry *geometry)
  434. {
  435. - YCHAR *clonedName = malloc(sizeof(YCHAR) * (strnlen(name,YAFFS_MAX_NAME_LENGTH)+1));
  436. - struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev));
  437. + YCHAR *clonedName = kmalloc(sizeof(YCHAR) * (strnlen(name,YAFFS_MAX_NAME_LENGTH)+1), GFP_NOFS);
  438. + struct yaffs_dev *dev = kmalloc(sizeof(struct yaffs_dev), GFP_NOFS);
  439. if(dev && clonedName){
  440. memset(dev,0,sizeof(struct yaffs_dev));
  441. @@ -242,16 +241,17 @@ struct yaffs_dev *
  442. dev->param.n_reserved_blocks = 5;
  443. dev->driver_context = (void *)geometry;
  444. + /* save rt_mtd_nand_device * dev*/
  445. + dev->os_context = (void *)(geometry->privateData);
  446. yaffs_add_device(dev);
  447. return dev;
  448. }
  449. if(dev)
  450. - free(dev);
  451. + kfree(dev);
  452. if(clonedName)
  453. - free(clonedName);
  454. + kfree(clonedName);
  455. return NULL;
  456. }
  457. -
  458. diff --git a/yaffs/direct/ydirectenv.h b/yaffs/direct/ydirectenv.h
  459. index 7860b84..5add3ec 100644
  460. --- a/yaffs/direct/ydirectenv.h
  461. +++ b/yaffs/direct/ydirectenv.h
  462. @@ -22,16 +22,13 @@
  463. // Direct interface
  464. -#include "stdlib.h"
  465. -#include "stdio.h"
  466. +#include <rtthread.h>
  467. #include "string.h"
  468. +
  469. #include "yaffs_osglue.h"
  470. #include "yaffs_hweight.h"
  471. -#include "assert.h"
  472. -#define BUG() assert(0)
  473. -//#define BUG() do { *((int *)0) =1;} while(0)
  474. -
  475. +#define BUG() RT_ASSERT(0)
  476. #define YCHAR char
  477. #define YUCHAR unsigned char
  478. @@ -47,30 +44,24 @@ void yaffs_qsort(void *aa, size_t n, size_t es,
  479. #define YAFFS_PATH_DIVIDERS "/"
  480. -#ifdef NO_inline
  481. -#define inline
  482. -#else
  483. -#define inline __inline__
  484. -#endif
  485. -
  486. -#define kmalloc(x,flags) yaffsfs_malloc(x)
  487. +#define GFP_NOFS 0
  488. +#define kmalloc(x, y) yaffsfs_malloc(x)
  489. #define kfree(x) yaffsfs_free(x)
  490. #define vmalloc(x) yaffsfs_malloc(x)
  491. -#define vfree(x) yaffsfs_free(x)
  492. +#define vfree(x) yaffsfs_free(x)
  493. +#define printf rt_kprintf
  494. #define cond_resched() do {} while(0)
  495. #define yaffs_trace(msk, fmt, ...) do { \
  496. if(yaffs_trace_mask & (msk)) \
  497. - printf("yaffs: " fmt "\n", ##__VA_ARGS__); \
  498. + rt_kprintf("yaffs: " fmt "\n", ##__VA_ARGS__); \
  499. } while(0)
  500. #define YAFFS_LOSTNFOUND_NAME "lost+found"
  501. #define YAFFS_LOSTNFOUND_PREFIX "obj"
  502. -#include "yaffscfg.h"
  503. -
  504. #define Y_CURRENT_TIME yaffsfs_CurrentTime()
  505. #define Y_TIME_CONVERT(x) x
  506. @@ -79,8 +70,6 @@ void yaffs_qsort(void *aa, size_t n, size_t es,
  507. #include "yaffs_list.h"
  508. -#include "yaffsfs.h"
  509. -
  510. #endif
  511. diff --git a/yaffs/direct/yportenv.h b/yaffs/direct/yportenv.h
  512. index 939cd3a..f693c16 100644
  513. --- a/yaffs/direct/yportenv.h
  514. +++ b/yaffs/direct/yportenv.h
  515. @@ -17,36 +17,146 @@
  516. #ifndef __YPORTENV_H__
  517. #define __YPORTENV_H__
  518. +#include <dfs_posix.h>
  519. +
  520. +#define CONFIG_YAFFS_DIRECT
  521. +#define CONFIG_YAFFS_PROVIDE_DEFS
  522. +#define CONFIG_YAFFSFS_PROVIDE_VALUES
  523. +
  524. +#ifdef NO_inline
  525. + #define inline
  526. +#else
  527. + #ifdef __CC_ARM
  528. + #define inline __inline
  529. + #elif defined (__GNUC__) /* GNU GCC Compiler */
  530. + #define inline __inline
  531. + #else
  532. + #error "Please set a macro inline with a valid args"
  533. + #endif
  534. +#endif
  535. +
  536. +#if defined(__CC_ARM)
  537. + #ifdef RT_USING_NEWLIB
  538. + #error "error: when use armcc, please Don't USE NEWLIB!!!"
  539. + #endif
  540. +
  541. + #ifndef off_t
  542. + typedef long off_t;
  543. + #endif
  544. +
  545. + #ifndef loff_t
  546. + typedef unsigned long loff_t;
  547. + #endif
  548. +
  549. + #ifndef dev_t
  550. + typedef long dev_t;
  551. + #endif
  552. +
  553. + #ifndef mode_t
  554. + typedef int mode_t;
  555. + #endif
  556. +
  557. + /* just like strlen(3), but cap the number of bytes we count */
  558. + rt_inline size_t strnlen(const char *s, size_t max) {
  559. + register const char *p;
  560. + for(p = s; *p && max--; ++p);
  561. + return(p - s);
  562. + }
  563. +
  564. +#elif defined (__GNUC__) && !defined(__CC_ARM)
  565. +
  566. + #ifndef loff_t
  567. + typedef unsigned long loff_t;
  568. + #endif
  569. +
  570. + #ifndef dev_t
  571. + typedef long dev_t;
  572. + #endif
  573. +#endif
  574. /* Definition of types */
  575. typedef unsigned char u8;
  576. typedef unsigned short u16;
  577. typedef unsigned u32;
  578. -
  579. -
  580. -#ifndef WIN32
  581. -#include <sys/stat.h>
  582. -#endif
  583. -
  584. +
  585. +#ifndef S_ISDIR
  586. + #ifndef _DEV_T_DEFINED
  587. + typedef unsigned int _dev_t; /* device code */
  588. + #if !__STDC__
  589. + /* Non-ANSI name for compatibility */
  590. + typedef unsigned int dev_t;
  591. + #endif
  592. +
  593. + #define _DEV_T_DEFINED
  594. + #endif
  595. +
  596. + #define __S_IFMT 0170000 /* These bits determine file type. */
  597. + /* File types. */
  598. + #define __S_IFDIR 0040000 /* Directory. */
  599. + #define __S_IFCHR 0020000 /* Character device. */
  600. + #define __S_IFBLK 0060000 /* Block device. */
  601. + #define __S_IFREG 0100000 /* Regular file. */
  602. + #define __S_IFIFO 0010000 /* FIFO. */
  603. + #define __S_IFLNK 0120000 /* Symbolic link. */
  604. + #define __S_IFSOCK 0140000 /* Socket. */
  605. +
  606. + /* Protection bits. */
  607. + #define __S_ISUID 04000 /* Set user ID on execution. */
  608. + #define __S_ISGID 02000 /* Set group ID on execution. */
  609. + #define __S_ISVTX 01000 /* Save swapped text after use (sticky). */
  610. + #define __S_IREAD 0400 /* Read by owner. */
  611. + #define __S_IWRITE 0200 /* Write by owner. */
  612. + #define __S_IEXEC 0100 /* Execute by owner. */
  613. + #define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))
  614. +
  615. + #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
  616. + #define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR)
  617. + #define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK)
  618. + #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
  619. + #define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)
  620. + #define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFSOCK)
  621. + #define S_IRUSR __S_IREAD // Read by owner.
  622. + #define S_IWUSR __S_IWRITE // Write by owner.
  623. + #define S_IXUSR __S_IEXEC // Execute by owner.
  624. + #define S_IREAD S_IRUSR
  625. + #define S_IWRITE S_IWUSR
  626. + #define S_IEXEC S_IXUSR
  627. + #define S_IFDIR __S_IFDIR
  628. +#endif
  629. #ifdef CONFIG_YAFFS_PROVIDE_DEFS
  630. /* File types */
  631. +#ifndef DT_UNKNOWN
  632. + #define DT_UNKNOWN 0
  633. +#endif
  634. +#ifndef DT_FIFO
  635. + #define DT_FIFO 1
  636. +#endif
  637. -#define DT_UNKNOWN 0
  638. -#define DT_FIFO 1
  639. -#define DT_CHR 2
  640. -#define DT_DIR 4
  641. -#define DT_BLK 6
  642. -#define DT_REG 8
  643. -#define DT_LNK 10
  644. -#define DT_SOCK 12
  645. -#define DT_WHT 14
  646. -
  647. +#ifndef DT_CHR
  648. + #define DT_CHR 2
  649. +#endif
  650. -#ifndef WIN32
  651. -#include <sys/stat.h>
  652. +#ifndef DT_DIR
  653. + #define DT_DIR 4
  654. +#endif
  655. +#ifndef DT_BLK
  656. + #define DT_BLK 6
  657. +#endif
  658. +#ifndef DT_REG
  659. + #define DT_REG 8
  660. #endif
  661. +#ifndef DT_LNK
  662. + #define DT_LNK 10
  663. +#endif
  664. +#ifndef DT_SOCK
  665. + #define DT_SOCK 12
  666. +#endif
  667. +#ifndef DT_WHT
  668. + #define DT_WHT 14
  669. +#endif
  670. +
  671. /*
  672. * Attribute flags.
  673. @@ -75,7 +185,6 @@ struct iattr {
  674. #endif
  675. -
  676. #if defined CONFIG_YAFFS_WINCE
  677. #include "ywinceenv.h"
  678. @@ -267,9 +376,17 @@ struct iattr {
  679. #endif
  680. #else
  681. -#include <errno.h>
  682. -#include <sys/stat.h>
  683. -#include <fcntl.h>
  684. +//#include <errno.h>
  685. +//#include <sys/stat.h>
  686. +//#include <fcntl.h>
  687. +#ifndef XATTR_CREATE
  688. +#define XATTR_CREATE 1
  689. +#endif
  690. +
  691. +#ifndef XATTR_REPLACE
  692. +#define XATTR_REPLACE 2
  693. +#endif
  694. +
  695. #endif
  696. #endif
  697. diff --git a/yaffs_nandcfg.c b/yaffs_nandcfg.c
  698. new file mode 100644
  699. index 0000000..099fa38
  700. --- /dev/null
  701. +++ b/yaffs_nandcfg.c
  702. @@ -0,0 +1,149 @@
  703. +#include <rtthread.h>
  704. +#include <rtdevice.h>
  705. +#include <dfs_fs.h>
  706. +#include <dfs_def.h>
  707. +
  708. +#include "yaffsfs.h"
  709. +#include "yaffs_nandif.h"
  710. +#include "yaffs_trace.h"
  711. +
  712. +/*
  713. + * RT-Thread Device Interface for yaffs
  714. + */
  715. +static int nand_init(struct yaffs_dev *dev)
  716. +{
  717. + return YAFFS_OK;
  718. +}
  719. +
  720. +static int nand_deinit(struct yaffs_dev *dev)
  721. +{
  722. + return YAFFS_OK;
  723. +}
  724. +
  725. +/* if block is good, return YAFFS_OK, else return YAFFS_FAIL */
  726. +static int nand_checkblock(struct yaffs_dev *dev, unsigned block)
  727. +{
  728. + rt_err_t res;
  729. + res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->os_context), block);
  730. + return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL;
  731. +}
  732. +static int nand_markbadblk(struct yaffs_dev *dev, unsigned block)
  733. +{
  734. + rt_err_t res;
  735. + res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->os_context), block);
  736. + return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL;
  737. +}
  738. +
  739. +static int nand_eraseblock(struct yaffs_dev *dev, unsigned block)
  740. +{
  741. + int res;
  742. + res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->os_context), block);
  743. + return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL;
  744. +}
  745. +
  746. +static int nand_readpage(
  747. + struct yaffs_dev *dev,
  748. + unsigned page,
  749. + unsigned char *data, unsigned data_len, /* page data */
  750. + unsigned char *spare, unsigned spare_len,/* page spare */
  751. + int *ecc_status)
  752. +{
  753. + int res;
  754. + unsigned char spare_buf[64];
  755. +
  756. + res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->os_context),
  757. + page, data, data_len, spare_buf, spare_len + 5);
  758. + rt_memcpy(spare, spare_buf + 5, spare_len);
  759. + if (res == 0)
  760. + *ecc_status = 0;
  761. + else if (res == -1)
  762. + *ecc_status = 1;
  763. + else
  764. + *ecc_status = -1;
  765. +
  766. + return YAFFS_OK;
  767. +}
  768. +
  769. +static int nand_writepage(
  770. + struct yaffs_dev *dev,
  771. + unsigned page,
  772. + const unsigned char *data, unsigned data_len, /* page data */
  773. + const unsigned char *spare, unsigned spare_len) /* page spare */
  774. +{
  775. + int res;
  776. + unsigned char spare_buf[64]; //not use malloc, this can be faster
  777. + rt_memset(spare_buf, 0xFF, sizeof(spare_buf));
  778. + rt_memcpy(spare_buf+5, spare, spare_len);
  779. +
  780. + res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->os_context),
  781. + page, data, data_len, spare_buf, spare_len + 5);
  782. + if (res != RT_EOK)
  783. + goto __error;
  784. +
  785. + return YAFFS_OK;
  786. +
  787. +__error:
  788. + return YAFFS_FAIL;
  789. +}
  790. +
  791. +
  792. +/*
  793. + * yaffs2 configuration
  794. + */
  795. +#define CONFIG_YAFFS_ECC_MODE 1 //1 use ecc, 0 no ecc
  796. +#define CONFIG_YAFFS_INBAND_TAGS 0 //1 use in band tags, 0-no in band tags
  797. +#define CONFIG_YAFFS_USE_YAFFS2 1 //1 yaffs2, 0-yaffs1
  798. +
  799. +void yaffs_config(ynandif_Geometry * g, struct rt_mtd_nand_device * dev)
  800. +{
  801. + rt_memset(g,0,sizeof(ynandif_Geometry));
  802. +
  803. + g->start_block = dev->block_start;
  804. + g->end_block = dev->block_end;
  805. + g->dataSize = dev->page_size;
  806. + g->spareSize = dev->oob_size;
  807. + g->pagesPerBlock = dev->block_size / dev->page_size;
  808. +
  809. + g->hasECC = CONFIG_YAFFS_ECC_MODE;
  810. + g->inband_tags = CONFIG_YAFFS_INBAND_TAGS;
  811. + g->useYaffs2 = CONFIG_YAFFS_USE_YAFFS2;
  812. +
  813. + g->privateData = dev;//will be copy to dev->os_context.
  814. +
  815. + g->initialise = nand_init;
  816. + g->deinitialise = nand_deinit;
  817. + g->readChunk = nand_readpage;
  818. + g->writeChunk = nand_writepage;
  819. + g->eraseBlock = nand_eraseblock;
  820. + g->checkBlockOk = nand_checkblock;
  821. + g->markBlockBad = nand_markbadblk;
  822. +}
  823. +
  824. +static struct yaffs_dev *ynand_CreatePart(const YCHAR *name, ynandif_Geometry * g)
  825. +{
  826. + return yaffs_add_dev_from_geometry(name, g);
  827. +}
  828. +
  829. +/* configuration for yaffs's log */
  830. +unsigned yaffs_trace_mask =
  831. +
  832. + YAFFS_TRACE_SCAN |
  833. + YAFFS_TRACE_GC |
  834. + YAFFS_TRACE_ERASE |
  835. + YAFFS_TRACE_ERROR |
  836. + YAFFS_TRACE_TRACING |
  837. + YAFFS_TRACE_ALLOCATE |
  838. + YAFFS_TRACE_BAD_BLOCKS |
  839. + YAFFS_TRACE_VERIFY |
  840. +
  841. + 0;
  842. +
  843. +int yaffs_start(const char * mount_point, ynandif_Geometry * g)
  844. +{
  845. + // Stuff to configure YAFFS
  846. + // Stuff to initialise anything special (eg lock semaphore).
  847. + yaffsfs_OSInitialisation();
  848. + ynand_CreatePart(mount_point, g);
  849. +
  850. + return 0;
  851. +}
  852. diff --git a/yaffs_osglue.c b/yaffs_osglue.c
  853. new file mode 100644
  854. index 0000000..2c34f99
  855. --- /dev/null
  856. +++ b/yaffs_osglue.c
  857. @@ -0,0 +1,118 @@
  858. +/*
  859. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  860. + *
  861. + * Copyright (C) 2002-2011 Aleph One Ltd.
  862. + * for Toby Churchill Ltd and Brightstar Engineering
  863. + *
  864. + * Created by Charles Manning <charles@aleph1.co.uk>
  865. + *
  866. + * This program is free software; you can redistribute it and/or modify
  867. + * it under the terms of the GNU General Public License version 2 as
  868. + * published by the Free Software Foundation.
  869. + */
  870. +
  871. +
  872. +#include "yaffscfg.h"
  873. +#include "yaffs_guts.h"
  874. +#include "yaffsfs.h"
  875. +#include "yaffs_trace.h"
  876. +#include <rtthread.h>
  877. +
  878. +//#include <errno.h>
  879. +
  880. +
  881. +static int yaffsfs_lastError;
  882. +
  883. +void yaffsfs_SetError(int err)
  884. +{
  885. + //Do whatever to set error
  886. + yaffsfs_lastError = err;
  887. +}
  888. +
  889. +int yaffsfs_GetLastError(void)
  890. +{
  891. + return yaffsfs_lastError;
  892. +}
  893. +
  894. +
  895. +#ifdef CONFIG_YAFFS_USE_PTHREADS
  896. +#include <pthread.h>
  897. +static pthread_mutex_t mutex1;
  898. +
  899. +
  900. +void yaffsfs_Lock(void)
  901. +{
  902. + pthread_mutex_lock( &mutex1 );
  903. +}
  904. +
  905. +void yaffsfs_Unlock(void)
  906. +{
  907. + pthread_mutex_unlock( &mutex1 );
  908. +}
  909. +
  910. +void yaffsfs_LockInit(void)
  911. +{
  912. + pthread_mutex_init( &mutex1, NULL);
  913. +}
  914. +
  915. +#else
  916. +
  917. +static rt_mutex_t mutex = RT_NULL;
  918. +static rt_sem_t sem = RT_NULL;
  919. +void yaffsfs_Lock(void)
  920. +{
  921. + rt_mutex_take(mutex, RT_WAITING_FOREVER);
  922. +}
  923. +
  924. +void yaffsfs_Unlock(void)
  925. +{
  926. + rt_mutex_release(mutex);
  927. +}
  928. +
  929. +void yaffsfs_LockInit(void)
  930. +{
  931. + mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
  932. + if (mutex == RT_NULL)
  933. + {
  934. + yaffs_trace(YAFFS_TRACE_ERROR,
  935. + "**>> yaffs error :yaffs_LockInit can't get a mutex!");
  936. + }
  937. +
  938. +}
  939. +#endif
  940. +
  941. +u32 yaffsfs_CurrentTime(void)
  942. +{
  943. + return 0;
  944. +}
  945. +
  946. +
  947. +static int yaffs_kill_alloc = 0;
  948. +static size_t total_malloced = 0;
  949. +static size_t malloc_limit = 0 & 6000000;
  950. +
  951. +void *yaffsfs_malloc(size_t size)
  952. +{
  953. + void * this;
  954. + if(yaffs_kill_alloc)
  955. + return NULL;
  956. + if(malloc_limit && malloc_limit <(total_malloced + size) )
  957. + return NULL;
  958. +
  959. + this = rt_malloc(size);
  960. + if(this)
  961. + total_malloced += size;
  962. + return this;
  963. +}
  964. +
  965. +void yaffsfs_free(void *ptr)
  966. +{
  967. + rt_free(ptr);
  968. +}
  969. +
  970. +void yaffsfs_OSInitialisation(void)
  971. +{
  972. + yaffsfs_LockInit();
  973. +}
  974. +
  975. +