dfs_cromfs.c 30 KB

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