1
0

dfs_cromfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/08/21 ShaoJinchun first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs.h>
  12. #include <dfs_fs.h>
  13. #include <dfs_file.h>
  14. #include "dfs_cromfs.h"
  15. #include <stdint.h>
  16. #include "zlib.h"
  17. /**********************************/
  18. #define CROMFS_PATITION_HEAD_SIZE 256
  19. #define CROMFS_DIRENT_CACHE_SIZE 8
  20. #define CROMFS_MAGIC "CROMFSMG"
  21. #define CROMFS_CT_ASSERT(name, x) \
  22. struct assert_##name {char ary[2 * (x) - 1];}
  23. #define CROMFS_POS_ROOT (0x0UL)
  24. #define CROMFS_POS_ERROR (0x1UL)
  25. typedef struct
  26. {
  27. uint8_t magic[8]; /* CROMFS_MAGIC */
  28. uint32_t version;
  29. uint32_t partition_attr; /* expand, now reserved 0 */
  30. uint32_t partition_size; /* with partition head */
  31. uint32_t root_dir_pos; /* root dir pos */
  32. uint32_t root_dir_size;
  33. } partition_head_data;
  34. typedef struct
  35. {
  36. partition_head_data head;
  37. uint8_t padding[CROMFS_PATITION_HEAD_SIZE - sizeof(partition_head_data)];
  38. } partition_head;
  39. #define CROMFS_DIRENT_ATTR_DIR 0x1UL
  40. #define CROMFS_DIRENT_ATTR_FILE 0x0UL
  41. typedef struct
  42. {
  43. uint16_t attr; /* dir or file add other */
  44. uint16_t name_size; /* name real size */
  45. uint32_t file_size; /* file data size */
  46. uint32_t file_origin_size; /* file size before compress */
  47. uint32_t parition_pos; /* offset of data */
  48. uint8_t name[0]; /* name data */
  49. } cromfs_dirent;
  50. #define CROMFS_ALIGN_SIZE_BIT 4
  51. #define CROMFS_ALIGN_SIZE (1UL << CROMFS_ALIGN_SIZE_BIT) /* must be same as sizeof cromfs_dirent */
  52. #define CROMFS_ALIGN_SIZE_MASK (CROMFS_ALIGN_SIZE - 1)
  53. CROMFS_CT_ASSERT(align_size, CROMFS_ALIGN_SIZE == sizeof(cromfs_dirent));
  54. typedef union
  55. {
  56. cromfs_dirent dirent;
  57. uint8_t name[CROMFS_ALIGN_SIZE];
  58. } cromfs_dirent_item;
  59. /**********************************/
  60. typedef struct
  61. {
  62. rt_list_t list;
  63. uint32_t partition_pos;
  64. uint32_t size;
  65. uint8_t *buff;
  66. } cromfs_dirent_cache;
  67. typedef struct st_cromfs_info
  68. {
  69. rt_device_t device;
  70. uint32_t partition_size;
  71. uint32_t bytes_per_sector;
  72. uint32_t (*read_bytes)(struct st_cromfs_info *ci, uint32_t pos, void *buf, uint32_t size);
  73. partition_head_data part_info;
  74. struct rt_mutex lock;
  75. struct cromfs_avl_struct *cromfs_avl_root;
  76. rt_list_t cromfs_dirent_cache_head;
  77. int cromfs_dirent_cache_nr;
  78. } cromfs_info;
  79. typedef struct
  80. {
  81. uint32_t ref;
  82. uint32_t partition_pos;
  83. cromfs_info *ci;
  84. uint32_t size;
  85. uint8_t *buff;
  86. uint32_t partition_size;
  87. int data_valid;
  88. } file_info;
  89. /**********************************/
  90. #define avl_key_t uint32_t
  91. #define AVL_EMPTY (struct cromfs_avl_struct *)0
  92. #define avl_maxheight 32
  93. #define heightof(tree) ((tree) == AVL_EMPTY ? 0 : (tree)->avl_height)
  94. struct cromfs_avl_struct
  95. {
  96. struct cromfs_avl_struct *avl_left;
  97. struct cromfs_avl_struct *avl_right;
  98. int avl_height;
  99. avl_key_t avl_key;
  100. file_info *fi;
  101. };
  102. static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree);
  103. static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree);
  104. static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct *ptree);
  105. static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count)
  106. {
  107. for (;count > 0; count--)
  108. {
  109. struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr;
  110. struct cromfs_avl_struct *node = *nodeplace;
  111. struct cromfs_avl_struct *nodeleft = node->avl_left;
  112. struct cromfs_avl_struct *noderight = node->avl_right;
  113. int heightleft = heightof(nodeleft);
  114. int heightright = heightof(noderight);
  115. if (heightright + 1 < heightleft)
  116. {
  117. struct cromfs_avl_struct * nodeleftleft = nodeleft->avl_left;
  118. struct cromfs_avl_struct * nodeleftright = nodeleft->avl_right;
  119. int heightleftright = heightof(nodeleftright);
  120. if (heightof(nodeleftleft) >= heightleftright)
  121. {
  122. node->avl_left = nodeleftright;
  123. nodeleft->avl_right = node;
  124. nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
  125. *nodeplace = nodeleft;
  126. }
  127. else
  128. {
  129. nodeleft->avl_right = nodeleftright->avl_left;
  130. node->avl_left = nodeleftright->avl_right;
  131. nodeleftright->avl_left = nodeleft;
  132. nodeleftright->avl_right = node;
  133. nodeleft->avl_height = node->avl_height = heightleftright;
  134. nodeleftright->avl_height = heightleft;
  135. *nodeplace = nodeleftright;
  136. }
  137. }
  138. else if (heightleft + 1 < heightright)
  139. {
  140. struct cromfs_avl_struct *noderightright = noderight->avl_right;
  141. struct cromfs_avl_struct *noderightleft = noderight->avl_left;
  142. int heightrightleft = heightof(noderightleft);
  143. if (heightof(noderightright) >= heightrightleft)
  144. {
  145. node->avl_right = noderightleft;
  146. noderight->avl_left = node;
  147. noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
  148. *nodeplace = noderight;
  149. }
  150. else
  151. {
  152. noderight->avl_left = noderightleft->avl_right;
  153. node->avl_right = noderightleft->avl_left;
  154. noderightleft->avl_right = noderight;
  155. noderightleft->avl_left = node;
  156. noderight->avl_height = node->avl_height = heightrightleft;
  157. noderightleft->avl_height = heightright;
  158. *nodeplace = noderightleft;
  159. }
  160. }
  161. else {
  162. int height = (heightleft<heightright ? heightright : heightleft) + 1;
  163. if (height == node->avl_height)
  164. {
  165. break;
  166. }
  167. node->avl_height = height;
  168. }
  169. }
  170. }
  171. static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree)
  172. {
  173. avl_key_t key = node_to_delete->avl_key;
  174. struct cromfs_avl_struct **nodeplace = ptree;
  175. struct cromfs_avl_struct **stack[avl_maxheight];
  176. uint32_t stack_count = 0;
  177. struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  178. struct cromfs_avl_struct **nodeplace_to_delete;
  179. for (;;)
  180. {
  181. struct cromfs_avl_struct *node = *nodeplace;
  182. if (node == AVL_EMPTY)
  183. {
  184. return;
  185. }
  186. *stack_ptr++ = nodeplace;
  187. stack_count++;
  188. if (key == node->avl_key)
  189. {
  190. break;
  191. }
  192. if (key < node->avl_key)
  193. {
  194. nodeplace = &node->avl_left;
  195. }
  196. else
  197. {
  198. nodeplace = &node->avl_right;
  199. }
  200. }
  201. nodeplace_to_delete = nodeplace;
  202. if (node_to_delete->avl_left == AVL_EMPTY)
  203. {
  204. *nodeplace_to_delete = node_to_delete->avl_right;
  205. stack_ptr--;
  206. stack_count--;
  207. }
  208. else
  209. {
  210. struct cromfs_avl_struct *** stack_ptr_to_delete = stack_ptr;
  211. struct cromfs_avl_struct ** nodeplace = &node_to_delete->avl_left;
  212. struct cromfs_avl_struct * node;
  213. for (;;)
  214. {
  215. node = *nodeplace;
  216. if (node->avl_right == AVL_EMPTY)
  217. {
  218. break;
  219. }
  220. *stack_ptr++ = nodeplace;
  221. stack_count++;
  222. nodeplace = &node->avl_right;
  223. }
  224. *nodeplace = node->avl_left;
  225. node->avl_left = node_to_delete->avl_left;
  226. node->avl_right = node_to_delete->avl_right;
  227. node->avl_height = node_to_delete->avl_height;
  228. *nodeplace_to_delete = node;
  229. *stack_ptr_to_delete = &node->avl_left;
  230. }
  231. cromfs_avl_rebalance(stack_ptr,stack_count);
  232. }
  233. static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree)
  234. {
  235. avl_key_t key = new_node->avl_key;
  236. struct cromfs_avl_struct **nodeplace = ptree;
  237. struct cromfs_avl_struct **stack[avl_maxheight];
  238. int stack_count = 0;
  239. struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  240. for (;;)
  241. {
  242. struct cromfs_avl_struct * node = *nodeplace;
  243. if (node == AVL_EMPTY)
  244. {
  245. break;
  246. }
  247. *stack_ptr++ = nodeplace;
  248. stack_count++;
  249. if (key < node->avl_key)
  250. {
  251. nodeplace = &node->avl_left;
  252. }
  253. else
  254. {
  255. nodeplace = &node->avl_right;
  256. }
  257. }
  258. new_node->avl_left = AVL_EMPTY;
  259. new_node->avl_right = AVL_EMPTY;
  260. new_node->avl_height = 1;
  261. *nodeplace = new_node;
  262. cromfs_avl_rebalance(stack_ptr,stack_count);
  263. }
  264. static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct* ptree)
  265. {
  266. for (;;)
  267. {
  268. if (ptree == AVL_EMPTY)
  269. {
  270. return (struct cromfs_avl_struct *)0;
  271. }
  272. if (key == ptree->avl_key)
  273. {
  274. break;
  275. }
  276. if (key < ptree->avl_key)
  277. {
  278. ptree = ptree->avl_left;
  279. }
  280. else
  281. {
  282. ptree = ptree->avl_right;
  283. }
  284. }
  285. return ptree;
  286. }
  287. /**********************************/
  288. static uint32_t cromfs_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  289. {
  290. if (pos >= ci->partition_size || pos + size > ci->partition_size)
  291. {
  292. return 0;
  293. }
  294. return ci->read_bytes(ci, pos, buf, size);
  295. }
  296. static uint32_t cromfs_noblk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  297. {
  298. uint32_t ret = 0;
  299. ret = rt_device_read(ci->device, pos, buf, size);
  300. if (ret != size)
  301. {
  302. return 0;
  303. }
  304. else
  305. {
  306. return ret;
  307. }
  308. }
  309. static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  310. {
  311. uint32_t ret = 0;
  312. uint32_t size_bak = size;
  313. uint32_t start_blk = 0;
  314. uint32_t end_blk = 0;
  315. uint32_t off_s = 0;
  316. uint32_t sector_nr = 0;
  317. uint8_t *block_buff = NULL;
  318. uint32_t ret_len = 0;
  319. if (!size || !buf)
  320. {
  321. return 0;
  322. }
  323. block_buff = (uint8_t *)malloc(2 * ci->bytes_per_sector);
  324. if (!block_buff)
  325. {
  326. return 0;
  327. }
  328. start_blk = pos / ci->bytes_per_sector;
  329. off_s = pos % ci->bytes_per_sector;
  330. end_blk = (pos + size - 1) / ci->bytes_per_sector;
  331. sector_nr = end_blk - start_blk;
  332. if (sector_nr < 2)
  333. {
  334. ret_len = rt_device_read(ci->device, start_blk, block_buff, sector_nr + 1);
  335. if (ret_len != sector_nr + 1)
  336. {
  337. goto end;
  338. }
  339. memcpy(buf, block_buff + off_s, size);
  340. }
  341. else
  342. {
  343. ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
  344. if (ret_len != 1)
  345. {
  346. goto end;
  347. }
  348. memcpy(buf, block_buff + off_s, ci->bytes_per_sector - off_s);
  349. off_s = (ci->bytes_per_sector - off_s);
  350. size -= off_s;
  351. sector_nr--;
  352. start_blk++;
  353. if (sector_nr)
  354. {
  355. ret_len = rt_device_read(ci->device, start_blk, (char*)buf + off_s, sector_nr);
  356. if (ret_len != sector_nr)
  357. {
  358. goto end;
  359. }
  360. start_blk += sector_nr;
  361. off_s += (sector_nr * ci->bytes_per_sector);
  362. size -= (sector_nr * ci->bytes_per_sector);
  363. }
  364. ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
  365. if (ret_len != 1)
  366. {
  367. goto end;
  368. }
  369. memcpy((char*)buf + off_s, block_buff, size);
  370. }
  371. ret = size_bak;
  372. end:
  373. free(block_buff);
  374. return ret;
  375. }
  376. /**********************************/
  377. static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t size)
  378. {
  379. rt_list_t *l = NULL;
  380. cromfs_dirent_cache *dir = NULL;
  381. uint32_t len = 0;
  382. /* find */
  383. for (l = ci->cromfs_dirent_cache_head.next; l != &ci->cromfs_dirent_cache_head; l = l->next)
  384. {
  385. dir = (cromfs_dirent_cache *)l;
  386. if (dir->partition_pos == pos)
  387. {
  388. RT_ASSERT(dir->size == size);
  389. rt_list_remove(l);
  390. rt_list_insert_after(&ci->cromfs_dirent_cache_head, l);
  391. return dir->buff;
  392. }
  393. }
  394. /* not found */
  395. if (ci->cromfs_dirent_cache_nr >= CROMFS_DIRENT_CACHE_SIZE)
  396. {
  397. l = ci->cromfs_dirent_cache_head.prev;
  398. dir = (cromfs_dirent_cache *)l;
  399. rt_list_remove(l);
  400. free(dir->buff);
  401. free(dir);
  402. ci->cromfs_dirent_cache_nr--;
  403. }
  404. dir = (cromfs_dirent_cache *)malloc(sizeof *dir);
  405. if (!dir)
  406. {
  407. return NULL;
  408. }
  409. dir->buff = (uint8_t *)malloc(size);
  410. if (!dir->buff)
  411. {
  412. free(dir);
  413. return NULL;
  414. }
  415. len = cromfs_read_bytes(ci, pos, dir->buff, size);
  416. if (len != size)
  417. {
  418. free(dir->buff);
  419. free(dir);
  420. return NULL;
  421. }
  422. rt_list_insert_after(&ci->cromfs_dirent_cache_head, (rt_list_t *)dir);
  423. ci->cromfs_dirent_cache_nr++;
  424. dir->partition_pos = pos;
  425. dir->size = size;
  426. return dir->buff;
  427. }
  428. static void cromfs_dirent_cache_destroy(cromfs_info *ci)
  429. {
  430. rt_list_t *l = NULL;
  431. cromfs_dirent_cache *dir = NULL;
  432. while ((l = ci->cromfs_dirent_cache_head.next) != &ci->cromfs_dirent_cache_head)
  433. {
  434. rt_list_remove(l);
  435. dir = (cromfs_dirent_cache *)l;
  436. free(dir->buff);
  437. free(dir);
  438. ci->cromfs_dirent_cache_nr--;
  439. }
  440. }
  441. /**********************************/
  442. static int dfs_cromfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  443. {
  444. struct rt_device_blk_geometry geometry;
  445. uint32_t len = 0;
  446. cromfs_info *ci = NULL;
  447. ci = (cromfs_info *)malloc(sizeof *ci);
  448. if (!ci)
  449. {
  450. return -ENOMEM;
  451. }
  452. memset(ci, 0, sizeof *ci);
  453. ci->device = fs->dev_id;
  454. ci->partition_size = UINT32_MAX;
  455. if (ci->device->type == RT_Device_Class_Block)
  456. {
  457. rt_device_control(ci->device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  458. ci->bytes_per_sector = geometry.bytes_per_sector;
  459. ci->read_bytes = cromfs_blk_read_bytes;
  460. }
  461. else
  462. {
  463. ci->read_bytes = cromfs_noblk_read_bytes;
  464. }
  465. len = cromfs_read_bytes(ci, 0, &ci->part_info, sizeof ci->part_info);
  466. if (len != sizeof ci->part_info ||
  467. memcmp(ci->part_info.magic, CROMFS_MAGIC, sizeof ci->part_info.magic) != 0)
  468. {
  469. free(ci);
  470. return -RT_ERROR;
  471. }
  472. ci->partition_size = ci->part_info.partition_size;
  473. fs->data = ci;
  474. rt_mutex_init(&ci->lock, "crom", RT_IPC_FLAG_FIFO);
  475. ci->cromfs_avl_root = NULL;
  476. rt_list_init(&ci->cromfs_dirent_cache_head);
  477. ci->cromfs_dirent_cache_nr = 0;
  478. return RT_EOK;
  479. }
  480. static int dfs_cromfs_unmount(struct dfs_filesystem *fs)
  481. {
  482. rt_err_t result = RT_EOK;
  483. cromfs_info *ci = NULL;
  484. ci = (cromfs_info *)fs->data;
  485. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  486. if (result != RT_EOK)
  487. {
  488. return -RT_ERROR;
  489. }
  490. cromfs_dirent_cache_destroy(ci);
  491. while (ci->cromfs_avl_root)
  492. {
  493. struct cromfs_avl_struct *node;
  494. file_info *fi = NULL;
  495. node = ci->cromfs_avl_root;
  496. fi = node->fi;
  497. cromfs_avl_remove(node, &ci->cromfs_avl_root);
  498. free(node);
  499. if (fi->buff)
  500. {
  501. free(fi->buff);
  502. }
  503. free(fi);
  504. }
  505. rt_mutex_detach(&ci->lock);
  506. free(ci);
  507. return RT_EOK;
  508. }
  509. static int dfs_cromfs_ioctl(struct dfs_file *file, int cmd, void *args)
  510. {
  511. return -EIO;
  512. }
  513. static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize)
  514. {
  515. uint32_t cur_size = 0, cur_pos = 0, cur_osize = 0;
  516. const char *subpath = NULL, *subpath_end = NULL;
  517. void *di_mem = NULL;
  518. int isdir = 0;
  519. if (path[0] == '\0')
  520. {
  521. return CROMFS_POS_ERROR;
  522. }
  523. cur_size = ci->part_info.root_dir_size;
  524. cur_osize = 0;
  525. cur_pos = ci->part_info.root_dir_pos;
  526. isdir = 1;
  527. subpath_end = path;
  528. while (1)
  529. {
  530. cromfs_dirent_item *di_iter = NULL;
  531. int found = 0;
  532. /* skip /// */
  533. while (*subpath_end && *subpath_end == '/')
  534. {
  535. subpath_end++;
  536. }
  537. subpath = subpath_end;
  538. while ((*subpath_end != '/') && *subpath_end)
  539. {
  540. subpath_end++;
  541. }
  542. if (*subpath == '\0')
  543. {
  544. break;
  545. }
  546. /* if not dir or empty dir, error */
  547. if (!isdir || !cur_size)
  548. {
  549. return CROMFS_POS_ERROR;
  550. }
  551. /* find subpath */
  552. di_mem = cromfs_dirent_cache_get(ci, cur_pos, cur_size);
  553. if (!di_mem)
  554. {
  555. return CROMFS_POS_ERROR;
  556. }
  557. found = 0;
  558. di_iter = (cromfs_dirent_item *)di_mem;
  559. while (1)
  560. {
  561. uint32_t name_len = subpath_end - subpath;
  562. uint32_t name_block = 0;
  563. if (di_iter->dirent.name_size == name_len)
  564. {
  565. if (memcmp(di_iter->dirent.name, subpath, name_len) == 0)
  566. {
  567. found = 1;
  568. cur_size = di_iter->dirent.file_size;
  569. cur_osize = di_iter->dirent.file_origin_size;
  570. cur_pos = di_iter->dirent.parition_pos;
  571. if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
  572. {
  573. isdir = 1;
  574. }
  575. else
  576. {
  577. isdir = 0;
  578. }
  579. break;
  580. }
  581. }
  582. name_block = (di_iter->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) >> CROMFS_ALIGN_SIZE_BIT;
  583. di_iter += (1 + name_block);
  584. if ((uint32_t)di_iter - (uint32_t)di_mem >= cur_size)
  585. {
  586. break;
  587. }
  588. }
  589. if (!found)
  590. {
  591. return CROMFS_POS_ERROR;
  592. }
  593. }
  594. *size = cur_size;
  595. *osize = cur_osize;
  596. *is_dir = isdir;
  597. return cur_pos;
  598. }
  599. static uint32_t dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* is_dir, uint32_t *size, uint32_t *osize)
  600. {
  601. rt_err_t result = RT_EOK;
  602. uint32_t ret = 0;
  603. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  604. if (result != RT_EOK)
  605. {
  606. return CROMFS_POS_ERROR;
  607. }
  608. ret = cromfs_lookup(ci, path, is_dir, size, osize);
  609. rt_mutex_release(&ci->lock);
  610. return ret;
  611. }
  612. static int fill_file_data(file_info *fi)
  613. {
  614. int ret = -1;
  615. cromfs_info *ci = NULL;
  616. void *compressed_file_buff = NULL;
  617. uint32_t size = 0, osize = 0;
  618. if (!fi->data_valid)
  619. {
  620. RT_ASSERT(fi->buff != NULL);
  621. ci = fi->ci;
  622. osize = fi->size;
  623. size = fi->partition_size;
  624. compressed_file_buff = (void *)malloc(size);
  625. if (!compressed_file_buff)
  626. {
  627. goto end;
  628. }
  629. if (cromfs_read_bytes(ci, fi->partition_pos, compressed_file_buff, size) != size)
  630. {
  631. goto end;
  632. }
  633. if (uncompress((uint8_t *)fi->buff, (uLongf *)&osize, (uint8_t *)compressed_file_buff, size) != Z_OK)
  634. {
  635. goto end;
  636. }
  637. fi->data_valid = 1;
  638. }
  639. ret = 0;
  640. end:
  641. if (compressed_file_buff)
  642. {
  643. free(compressed_file_buff);
  644. }
  645. return ret;
  646. }
  647. static int dfs_cromfs_read(struct dfs_file *file, void *buf, size_t count)
  648. {
  649. rt_err_t result = RT_EOK;
  650. struct dfs_filesystem *fs = NULL;
  651. file_info *fi = NULL;
  652. cromfs_info *ci = NULL;
  653. uint32_t length = 0;
  654. fs = (struct dfs_filesystem *)file->vnode->fs;
  655. ci = (cromfs_info *)fs->data;
  656. fi = (file_info *)file->vnode->data;
  657. if (count < file->vnode->size - file->pos)
  658. {
  659. length = count;
  660. }
  661. else
  662. {
  663. length = file->vnode->size - file->pos;
  664. }
  665. if (length > 0)
  666. {
  667. RT_ASSERT(fi->size != 0);
  668. if (fi->buff)
  669. {
  670. int fill_ret = 0;
  671. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  672. if (result != RT_EOK)
  673. {
  674. return 0;
  675. }
  676. fill_ret = fill_file_data(fi);
  677. rt_mutex_release(&ci->lock);
  678. if (fill_ret < 0)
  679. {
  680. return 0;
  681. }
  682. memcpy(buf, fi->buff + file->pos, length);
  683. }
  684. else
  685. {
  686. void *di_mem = NULL;
  687. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  688. if (result != RT_EOK)
  689. {
  690. return 0;
  691. }
  692. di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
  693. if (di_mem)
  694. {
  695. memcpy(buf, (char*)di_mem + file->pos, length);
  696. }
  697. rt_mutex_release(&ci->lock);
  698. if (!di_mem)
  699. {
  700. return 0;
  701. }
  702. }
  703. /* update file current position */
  704. file->pos += length;
  705. }
  706. return length;
  707. }
  708. static int dfs_cromfs_lseek(struct dfs_file *file, off_t offset)
  709. {
  710. if (offset <= file->vnode->size)
  711. {
  712. file->pos = offset;
  713. return file->pos;
  714. }
  715. return -EIO;
  716. }
  717. static file_info *get_file_info(cromfs_info *ci, uint32_t partition_pos, int inc_ref)
  718. {
  719. struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
  720. if (node)
  721. {
  722. if (inc_ref)
  723. {
  724. node->fi->ref++;
  725. }
  726. return node->fi;
  727. }
  728. return NULL;
  729. }
  730. static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int is_dir, uint32_t size, uint32_t osize)
  731. {
  732. file_info *fi = NULL;
  733. void *file_buff = NULL;
  734. struct cromfs_avl_struct *node = NULL;
  735. fi = (file_info *)malloc(sizeof *fi);
  736. if (!fi)
  737. {
  738. goto err;
  739. }
  740. fi->partition_pos = partition_pos;
  741. fi->ci = ci;
  742. if (is_dir)
  743. {
  744. fi->size = size;
  745. }
  746. else
  747. {
  748. fi->size = osize;
  749. fi->partition_size = size;
  750. fi->data_valid = 0;
  751. if (osize)
  752. {
  753. file_buff = (void *)malloc(osize);
  754. if (!file_buff)
  755. {
  756. goto err;
  757. }
  758. }
  759. }
  760. fi->buff = file_buff;
  761. fi->ref = 1;
  762. node = (struct cromfs_avl_struct *)malloc(sizeof *node);
  763. if (!node)
  764. {
  765. goto err;
  766. }
  767. node->avl_key = partition_pos;
  768. node->fi = fi;
  769. cromfs_avl_insert(node, &ci->cromfs_avl_root);
  770. return fi;
  771. err:
  772. if (file_buff)
  773. {
  774. free(file_buff);
  775. }
  776. if (fi)
  777. {
  778. free(fi);
  779. }
  780. return NULL;
  781. }
  782. static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
  783. {
  784. struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
  785. file_info *fi = NULL;
  786. if (node)
  787. {
  788. node->fi->ref--;
  789. if (node->fi->ref == 0)
  790. {
  791. fi = node->fi;
  792. cromfs_avl_remove(node, &ci->cromfs_avl_root);
  793. free(node);
  794. if (fi->buff)
  795. {
  796. free(fi->buff);
  797. }
  798. free(fi);
  799. }
  800. }
  801. }
  802. static int dfs_cromfs_close(struct dfs_file *file)
  803. {
  804. file_info *fi = NULL;
  805. struct dfs_filesystem *fs = NULL;
  806. cromfs_info *ci = NULL;
  807. rt_err_t result = 0;
  808. RT_ASSERT(file->vnode->ref_count > 0);
  809. if (file->vnode->ref_count > 1)
  810. {
  811. return 0;
  812. }
  813. fi = (file_info *)file->vnode->data;
  814. fs = (struct dfs_filesystem *)file->vnode->fs;
  815. ci = (cromfs_info *)fs->data;
  816. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  817. if (result != RT_EOK)
  818. {
  819. return -RT_ERROR;
  820. }
  821. deref_file_info(ci, fi->partition_pos);
  822. rt_mutex_release(&ci->lock);
  823. file->vnode->data = NULL;
  824. return RT_EOK;
  825. }
  826. static int dfs_cromfs_open(struct dfs_file *file)
  827. {
  828. int ret = 0;
  829. struct dfs_filesystem *fs = NULL;
  830. file_info *fi = NULL;
  831. cromfs_info *ci = NULL;
  832. uint32_t file_pos = 0;
  833. uint32_t size = 0, osize = 0;
  834. int is_dir = 0;
  835. rt_err_t result = RT_EOK;
  836. if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
  837. {
  838. return -EINVAL;
  839. }
  840. RT_ASSERT(file->vnode->ref_count > 0);
  841. if (file->vnode->ref_count > 1)
  842. {
  843. if (file->vnode->type == FT_DIRECTORY
  844. && !(file->flags & O_DIRECTORY))
  845. {
  846. return -ENOENT;
  847. }
  848. file->pos = 0;
  849. return 0;
  850. }
  851. fs = file->vnode->fs;
  852. ci = (cromfs_info *)fs->data;
  853. file_pos = dfs_cromfs_lookup(ci, file->vnode->path, &is_dir, &size, &osize);
  854. if (file_pos == CROMFS_POS_ERROR)
  855. {
  856. ret = -ENOENT;
  857. goto end;
  858. }
  859. /* entry is a directory file type */
  860. if (is_dir)
  861. {
  862. if (!(file->flags & O_DIRECTORY))
  863. {
  864. ret = -ENOENT;
  865. goto end;
  866. }
  867. file->vnode->type = FT_DIRECTORY;
  868. }
  869. else
  870. {
  871. /* entry is a file, but open it as a directory */
  872. if (file->flags & O_DIRECTORY)
  873. {
  874. ret = -ENOENT;
  875. goto end;
  876. }
  877. file->vnode->type = FT_REGULAR;
  878. }
  879. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  880. if (result != RT_EOK)
  881. {
  882. ret = -EINTR;
  883. goto end;
  884. }
  885. fi = get_file_info(ci, file_pos, 1);
  886. if (!fi)
  887. {
  888. fi = inset_file_info(ci, file_pos, is_dir, size, osize);
  889. }
  890. rt_mutex_release(&ci->lock);
  891. if (!fi)
  892. {
  893. ret = -ENOENT;
  894. goto end;
  895. }
  896. file->vnode->data = fi;
  897. if (is_dir)
  898. {
  899. file->vnode->size = size;
  900. }
  901. else
  902. {
  903. file->vnode->size = osize;
  904. }
  905. file->pos = 0;
  906. ret = RT_EOK;
  907. end:
  908. return ret;
  909. }
  910. static int dfs_cromfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  911. {
  912. uint32_t size = 0, osize = 0;
  913. int is_dir = 0;
  914. cromfs_info *ci = NULL;
  915. uint32_t file_pos = 0;
  916. ci = (cromfs_info *)fs->data;
  917. file_pos = dfs_cromfs_lookup(ci, path, &is_dir, &size, &osize);
  918. if (file_pos == CROMFS_POS_ERROR)
  919. {
  920. return -ENOENT;
  921. }
  922. st->st_dev = 0;
  923. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  924. S_IWUSR | S_IWGRP | S_IWOTH;
  925. if (is_dir)
  926. {
  927. st->st_mode &= ~S_IFREG;
  928. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  929. st->st_size = size;
  930. }
  931. else
  932. {
  933. st->st_size = osize;
  934. }
  935. st->st_mtime = 0;
  936. return RT_EOK;
  937. }
  938. static int dfs_cromfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
  939. {
  940. uint32_t index = 0;
  941. uint8_t *name = NULL;
  942. struct dirent *d = NULL;
  943. file_info *fi = NULL;
  944. cromfs_info *ci = NULL;
  945. cromfs_dirent_item *dirent = NULL, *sub_dirent = NULL;
  946. void *di_mem = NULL;
  947. rt_err_t result = RT_EOK;
  948. fi = (file_info *)file->vnode->data;
  949. ci = fi->ci;
  950. RT_ASSERT(fi->buff == NULL);
  951. if (!fi->size)
  952. {
  953. return -EINVAL;
  954. }
  955. dirent = (cromfs_dirent_item *)malloc(fi->size);
  956. if (!dirent)
  957. {
  958. return -ENOMEM;
  959. }
  960. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  961. if (result != RT_EOK)
  962. {
  963. free(dirent);
  964. return -EINTR;
  965. }
  966. di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
  967. if (di_mem)
  968. {
  969. memcpy(dirent, di_mem, fi->size);
  970. }
  971. rt_mutex_release(&ci->lock);
  972. if (!di_mem)
  973. {
  974. free(dirent);
  975. return -ENOMEM;
  976. }
  977. /* make integer count */
  978. count = (count / sizeof(struct dirent));
  979. if (count == 0)
  980. {
  981. free(dirent);
  982. return -EINVAL;
  983. }
  984. for (index = 0; index < count && file->pos < file->vnode->size; index++)
  985. {
  986. uint32_t name_size = 0;
  987. d = dirp + index;
  988. sub_dirent = &dirent[file->pos >> CROMFS_ALIGN_SIZE_BIT];
  989. name = sub_dirent->dirent.name;
  990. /* fill dirent */
  991. if (sub_dirent->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
  992. {
  993. d->d_type = DT_DIR;
  994. }
  995. else
  996. {
  997. d->d_type = DT_REG;
  998. }
  999. d->d_namlen = sub_dirent->dirent.name_size;
  1000. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  1001. memcpy(d->d_name, (char *)name, sub_dirent->dirent.name_size);
  1002. d->d_name[sub_dirent->dirent.name_size] = '\0';
  1003. name_size = (sub_dirent->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) & ~CROMFS_ALIGN_SIZE_MASK;
  1004. /* move to next position */
  1005. file->pos += (name_size + sizeof *sub_dirent);
  1006. }
  1007. free(dirent);
  1008. return index * sizeof(struct dirent);
  1009. }
  1010. static const struct dfs_file_ops _crom_fops =
  1011. {
  1012. dfs_cromfs_open,
  1013. dfs_cromfs_close,
  1014. dfs_cromfs_ioctl,
  1015. dfs_cromfs_read,
  1016. NULL,
  1017. NULL,
  1018. dfs_cromfs_lseek,
  1019. dfs_cromfs_getdents,
  1020. };
  1021. static const struct dfs_filesystem_ops _cromfs =
  1022. {
  1023. "crom",
  1024. DFS_FS_FLAG_DEFAULT,
  1025. &_crom_fops,
  1026. dfs_cromfs_mount,
  1027. dfs_cromfs_unmount,
  1028. NULL,
  1029. NULL,
  1030. NULL,
  1031. dfs_cromfs_stat,
  1032. NULL,
  1033. };
  1034. int dfs_cromfs_init(void)
  1035. {
  1036. /* register crom file system */
  1037. dfs_register(&_cromfs);
  1038. return 0;
  1039. }
  1040. INIT_COMPONENT_EXPORT(dfs_cromfs_init);